1 /******************************************************************************
2  *
3  *  Copyright 2003-2016 Broadcom Corporation
4  *
5  *  Licensed under the Apache License, Version 2.0 (the "License");
6  *  you may not use this file except in compliance with the License.
7  *  You may obtain a copy of the License at:
8  *
9  *  http://www.apache.org/licenses/LICENSE-2.0
10  *
11  *  Unless required by applicable law or agreed to in writing, software
12  *  distributed under the License is distributed on an "AS IS" BASIS,
13  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  *  See the License for the specific language governing permissions and
15  *  limitations under the License.
16  *
17  ******************************************************************************/
18 
19 /******************************************************************************
20  *
21  *  AVRCP SDP related functions
22  *
23  ******************************************************************************/
24 #define LOG_TAG "avrcp"
25 
26 #include <bluetooth/log.h>
27 #include <string.h>
28 
29 #include "avrc_api.h"
30 #include "avrc_int.h"
31 #include "os/log.h"
32 #include "stack/include/bt_types.h"
33 #include "stack/include/bt_uuid16.h"
34 #include "stack/include/sdp_api.h"
35 #include "stack/include/sdpdefs.h"
36 #include "types/bluetooth/uuid.h"
37 #include "types/raw_address.h"
38 
39 using namespace bluetooth;
40 using namespace bluetooth::legacy::stack::sdp;
41 
42 using bluetooth::Uuid;
43 
44 /*****************************************************************************
45  *  Global data
46  ****************************************************************************/
47 tAVRC_CB avrc_cb;
48 static uint16_t a2dp_attr_list_sdp[] = {
49     ATTR_ID_SERVICE_CLASS_ID_LIST, /* update A2DP_NUM_ATTR, if changed */
50     ATTR_ID_BT_PROFILE_DESC_LIST,  ATTR_ID_SUPPORTED_FEATURES,
51     ATTR_ID_SERVICE_NAME,          ATTR_ID_PROTOCOL_DESC_LIST,
52     ATTR_ID_PROVIDER_NAME};
53 
54 /******************************************************************************
55  *
56  * Function         avrc_sdp_cback
57  *
58  * Description      This is the SDP callback function used by A2DP_FindService.
59  *                  This function will be executed by SDP when the service
60  *                  search is completed.  If the search is successful, it
61  *                  finds the first record in the database that matches the
62  *                  UUID of the search.  Then retrieves various parameters
63  *                  from the record.  When it is finished it calls the
64  *                  application callback function.
65  *
66  * Returns          Nothing.
67  *
68  *****************************************************************************/
avrc_sdp_cback(const RawAddress &,tSDP_STATUS status)69 static void avrc_sdp_cback(const RawAddress& /* bd_addr */,
70                            tSDP_STATUS status) {
71   log::verbose("status: {}", status);
72 
73   /* reset service_uuid, so can start another find service */
74   avrc_cb.service_uuid = 0;
75 
76   /* return info from sdp record in app callback function */
77   avrc_cb.find_cback.Run(status);
78 
79   return;
80 }
81 
82 /******************************************************************************
83  *
84  * Function         AVRC_FindService
85  *
86  * Description      This function is called by the application to perform
87  *                  service discovery and retrieve AVRCP SDP record information
88  *                  from a peer device.  Information is returned for the first
89  *                  service record found on the server that matches the service
90  *                  UUID. The callback function will be executed when service
91  *                  discovery is complete.  There can only be one outstanding
92  *                  call to AVRC_FindService() at a time; the application must
93  *                  wait for the callback before it makes another call to the
94  *                  function.  The application is responsible for allocating
95  *                  memory for the discovery database.  It is recommended that
96  *                  the size of the discovery database be at least 300 bytes.
97  *                  The application can deallocate the memory after the
98  *                  callback function has executed.
99  *
100  *                  Input Parameters:
101  *                      service_uuid: Indicates
102  *                                       TG(UUID_SERVCLASS_AV_REM_CTRL_TARGET)
103  *                                     r CT(UUID_SERVCLASS_AV_REMOTE_CONTROL)
104  *
105  *                      bd_addr:  BD address of the peer device.
106  *
107  *                      p_db:  SDP discovery database parameters.
108  *
109  *                      p_cback:  Pointer to the callback function.
110  *
111  *                  Output Parameters:
112  *                      None.
113  *
114  * Returns          AVRC_SUCCESS if successful.
115  *                  AVRC_BAD_PARAMS if discovery database parameters are
116  *                  invalid.
117  *                  AVRC_NO_RESOURCES if there are not enough resources to
118  *                                    perform the service search.
119  *
120  *****************************************************************************/
AVRC_FindService(uint16_t service_uuid,const RawAddress & bd_addr,tAVRC_SDP_DB_PARAMS * p_db,const tAVRC_FIND_CBACK & find_cback)121 uint16_t AVRC_FindService(uint16_t service_uuid, const RawAddress& bd_addr,
122                           tAVRC_SDP_DB_PARAMS* p_db,
123                           const tAVRC_FIND_CBACK& find_cback) {
124   bool result = true;
125 
126   log::verbose("uuid: {:x}", service_uuid);
127   if ((service_uuid != UUID_SERVCLASS_AV_REM_CTRL_TARGET &&
128        service_uuid != UUID_SERVCLASS_AV_REMOTE_CONTROL) ||
129       p_db == NULL || p_db->p_db == NULL || find_cback.is_null())
130     return AVRC_BAD_PARAM;
131 
132   /* check if it is busy */
133   if (avrc_cb.service_uuid == UUID_SERVCLASS_AV_REM_CTRL_TARGET ||
134       avrc_cb.service_uuid == UUID_SERVCLASS_AV_REMOTE_CONTROL)
135     return AVRC_NO_RESOURCES;
136 
137 
138   if (p_db->p_attrs == NULL || p_db->num_attr == 0) {
139     p_db->p_attrs = a2dp_attr_list_sdp;
140     p_db->num_attr = AVRC_NUM_ATTR;
141   }
142 
143   Uuid uuid_list = Uuid::From16Bit(service_uuid);
144   result = get_legacy_stack_sdp_api()->service.SDP_InitDiscoveryDb(
145       p_db->p_db, p_db->db_len, 1, &uuid_list, p_db->num_attr, p_db->p_attrs);
146 
147   if (result) {
148     /* store service_uuid and discovery db pointer */
149     avrc_cb.p_db = p_db->p_db;
150     avrc_cb.service_uuid = service_uuid;
151     avrc_cb.find_cback = find_cback;
152 
153     /* perform service search */
154     result =
155         get_legacy_stack_sdp_api()->service.SDP_ServiceSearchAttributeRequest(
156             bd_addr, p_db->p_db, avrc_sdp_cback);
157 
158     if (!result) {
159       log::error("Failed to init SDP for peer {}", bd_addr);
160       avrc_sdp_cback(bd_addr, SDP_GENERIC_ERROR);
161     }
162   }
163 
164   return (result ? AVRC_SUCCESS : AVRC_FAIL);
165 }
166 
167 /******************************************************************************
168  *
169  * Function         AVRC_AddRecord
170  *
171  * Description      This function is called to build an AVRCP SDP record.
172  *                  Prior to calling this function the application must
173  *                  call get_legacy_stack_sdp_api()->handle.SDP_CreateRecord()
174  *                  to create an SDP record.
175  *
176  *                  Input Parameters:
177  *                      service_uuid:  Indicates
178  *                                        TG(UUID_SERVCLASS_AV_REM_CTRL_TARGET)
179  *                                     or CT(UUID_SERVCLASS_AV_REMOTE_CONTROL)
180  *
181  *                      p_service_name:  Pointer to a null-terminated character
182  *                                       string containing the service name.
183  *                      If service name is not used set this to NULL.
184  *
185  *                      p_provider_name:  Pointer to a null-terminated character
186  *                                        string containing the provider name.
187  *                      If provider name is not used set this to NULL.
188  *
189  *                      categories:  Supported categories.
190  *
191  *                      sdp_handle:  SDP handle returned by
192  *                      get_legacy_stack_sdp_api()->handle.SDP_CreateRecord().
193  *
194  *                      browse_supported:  browse support info.
195  *
196  *                      profile_version:  profile version of avrcp record.
197  *
198  *                      cover_art_psm: The PSM of a cover art service, if
199  *                      supported. Use 0 Otherwise. Ignored on controller
200  *
201  *                  Output Parameters:
202  *                      None.
203  *
204  * Returns          AVRC_SUCCESS if successful.
205  *                  AVRC_NO_RESOURCES if not enough resources to build the SDP
206  *                                    record.
207  *
208  *****************************************************************************/
AVRC_AddRecord(uint16_t service_uuid,const char * p_service_name,const char * p_provider_name,uint16_t categories,uint32_t sdp_handle,bool browse_supported,uint16_t profile_version,uint16_t cover_art_psm)209 uint16_t AVRC_AddRecord(uint16_t service_uuid, const char* p_service_name,
210                         const char* p_provider_name, uint16_t categories,
211                         uint32_t sdp_handle, bool browse_supported,
212                         uint16_t profile_version, uint16_t cover_art_psm) {
213   uint16_t browse_list[1];
214   bool result = true;
215   uint8_t temp[8];
216   uint8_t* p;
217   uint16_t count = 1;
218   uint8_t index = 0;
219   uint16_t class_list[2];
220 
221   log::verbose(
222       "Add AVRCP SDP record, uuid: {:x}, profile_version: 0x{:x}, "
223       "supported_features: 0x{:x}, psm: 0x{:x}",
224       service_uuid, profile_version, categories, cover_art_psm);
225 
226   if (service_uuid != UUID_SERVCLASS_AV_REM_CTRL_TARGET &&
227       service_uuid != UUID_SERVCLASS_AV_REMOTE_CONTROL)
228     return AVRC_BAD_PARAM;
229 
230   /* add service class id list */
231   class_list[0] = service_uuid;
232   if ((service_uuid == UUID_SERVCLASS_AV_REMOTE_CONTROL) &&
233       (profile_version > AVRC_REV_1_3)) {
234     class_list[1] = UUID_SERVCLASS_AV_REM_CTRL_CONTROL;
235     count = 2;
236   }
237   result &= get_legacy_stack_sdp_api()->handle.SDP_AddServiceClassIdList(
238       sdp_handle, count, class_list);
239 
240   uint16_t protocol_reported_version;
241   /* AVRCP versions 1.3 to 1.5 report (version - 1) in the protocol
242      descriptor list. Oh, and 1.6 and 1.6.1 report version 1.4.
243      /because-we-smart */
244   if (profile_version < AVRC_REV_1_6) {
245     protocol_reported_version = profile_version - 1;
246   } else {
247     protocol_reported_version = AVCT_REV_1_4;
248   }
249 
250   /* add protocol descriptor list */
251   tSDP_PROTOCOL_ELEM avrc_proto_desc_list[AVRC_NUM_PROTO_ELEMS];
252   avrc_proto_desc_list[0].num_params = 1;
253   avrc_proto_desc_list[0].protocol_uuid = UUID_PROTOCOL_L2CAP;
254   avrc_proto_desc_list[0].params[0] = AVCT_PSM;
255   avrc_proto_desc_list[0].params[1] = 0;
256   for (index = 1; index < AVRC_NUM_PROTO_ELEMS; index++) {
257     avrc_proto_desc_list[index].num_params = 1;
258     avrc_proto_desc_list[index].protocol_uuid = UUID_PROTOCOL_AVCTP;
259     avrc_proto_desc_list[index].params[0] = protocol_reported_version;
260     avrc_proto_desc_list[index].params[1] = 0;
261   }
262   result &= get_legacy_stack_sdp_api()->handle.SDP_AddProtocolList(
263       sdp_handle, AVRC_NUM_PROTO_ELEMS, &avrc_proto_desc_list[0]);
264 
265   /* additional protocal descriptor, required only for version > 1.3 */
266   if (profile_version > AVRC_REV_1_3) {
267     int num_additional_protocols = 0;
268     int i = 0;
269     tSDP_PROTO_LIST_ELEM avrc_add_proto_desc_lists[2];
270 
271     /* If we support browsing then add the list */
272     if (browse_supported) {
273       log::verbose("Add Browsing PSM to additional protocol descriptor lists");
274       num_additional_protocols++;
275       avrc_add_proto_desc_lists[i].num_elems = 2;
276       avrc_add_proto_desc_lists[i].list_elem[0].num_params = 1;
277       avrc_add_proto_desc_lists[i].list_elem[0].protocol_uuid =
278           UUID_PROTOCOL_L2CAP;
279       avrc_add_proto_desc_lists[i].list_elem[0].params[0] = AVCT_BR_PSM;
280       avrc_add_proto_desc_lists[i].list_elem[0].params[1] = 0;
281       avrc_add_proto_desc_lists[i].list_elem[1].num_params = 1;
282       avrc_add_proto_desc_lists[i].list_elem[1].protocol_uuid =
283           UUID_PROTOCOL_AVCTP;
284       avrc_add_proto_desc_lists[i].list_elem[1].params[0] =
285           protocol_reported_version;
286       avrc_add_proto_desc_lists[i].list_elem[1].params[1] = 0;
287       i++;
288     }
289 
290     /* Add the BIP PSM for cover art on 1.6+ target devices that support it */
291     if (profile_version >= AVRC_REV_1_6 &&
292         service_uuid == UUID_SERVCLASS_AV_REM_CTRL_TARGET &&
293         cover_art_psm > 0) {
294       log::verbose(
295           "Add AVRCP BIP PSM to additional protocol descriptor lists, psm: "
296           "0x{:x}",
297           cover_art_psm);
298       num_additional_protocols++;
299       avrc_add_proto_desc_lists[i].num_elems = 2;
300       avrc_add_proto_desc_lists[i].list_elem[0].num_params = 1;
301       avrc_add_proto_desc_lists[i].list_elem[0].protocol_uuid =
302           UUID_PROTOCOL_L2CAP;
303       avrc_add_proto_desc_lists[i].list_elem[0].params[0] = cover_art_psm;
304       avrc_add_proto_desc_lists[i].list_elem[0].params[1] = 0;
305       avrc_add_proto_desc_lists[i].list_elem[1].num_params = 0;
306       avrc_add_proto_desc_lists[i].list_elem[1].protocol_uuid =
307           UUID_PROTOCOL_OBEX;
308       avrc_add_proto_desc_lists[i].list_elem[1].params[0] = 0;
309       i++;
310     }
311 
312     /* Add the additional lists if we support any */
313     if (num_additional_protocols > 0) {
314       log::verbose("Add {} additional protocol descriptor lists",
315                    num_additional_protocols);
316       result &= get_legacy_stack_sdp_api()->handle.SDP_AddAdditionProtoLists(
317           sdp_handle, num_additional_protocols, avrc_add_proto_desc_lists);
318     }
319   }
320   /* add profile descriptor list   */
321   result &= get_legacy_stack_sdp_api()->handle.SDP_AddProfileDescriptorList(
322       sdp_handle, UUID_SERVCLASS_AV_REMOTE_CONTROL, profile_version);
323 
324   /* add supported categories */
325   p = temp;
326   UINT16_TO_BE_STREAM(p, categories);
327   result &= get_legacy_stack_sdp_api()->handle.SDP_AddAttribute(
328       sdp_handle, ATTR_ID_SUPPORTED_FEATURES, UINT_DESC_TYPE, (uint32_t)2,
329       (uint8_t*)temp);
330 
331   /* add provider name */
332   if (p_provider_name != NULL) {
333     result &= get_legacy_stack_sdp_api()->handle.SDP_AddAttribute(
334         sdp_handle, ATTR_ID_PROVIDER_NAME, TEXT_STR_DESC_TYPE,
335         (uint32_t)(strlen(p_provider_name) + 1), (uint8_t*)p_provider_name);
336   }
337 
338   /* add service name */
339   if (p_service_name != NULL) {
340     result &= get_legacy_stack_sdp_api()->handle.SDP_AddAttribute(
341         sdp_handle, ATTR_ID_SERVICE_NAME, TEXT_STR_DESC_TYPE,
342         (uint32_t)(strlen(p_service_name) + 1), (uint8_t*)p_service_name);
343   }
344 
345   /* add browse group list */
346   browse_list[0] = UUID_SERVCLASS_PUBLIC_BROWSE_GROUP;
347   result &= get_legacy_stack_sdp_api()->handle.SDP_AddUuidSequence(
348       sdp_handle, ATTR_ID_BROWSE_GROUP_LIST, 1, browse_list);
349 
350   return (result ? AVRC_SUCCESS : AVRC_FAIL);
351 }
352 
353 /*******************************************************************************
354  *
355  * Function          AVRC_RemoveRecord
356  *
357  * Description       This function is called to remove an AVRCP SDP record.
358  *
359  *                   Input Parameters:
360  *                       sdp_handle:  Handle you used with AVRC_AddRecord
361  *
362  * Returns           AVRC_SUCCESS if successful.
363  *                   AVRC_FAIL otherwise
364  *
365  *******************************************************************************/
AVRC_RemoveRecord(uint32_t sdp_handle)366 uint16_t AVRC_RemoveRecord(uint32_t sdp_handle) {
367   log::verbose("remove AVRCP SDP record");
368   bool result = get_legacy_stack_sdp_api()->handle.SDP_DeleteRecord(sdp_handle);
369   return (result ? AVRC_SUCCESS : AVRC_FAIL);
370 }
371 
372 /*******************************************************************************
373  *
374  * Function         AVRC_Init
375  *
376  * Description      This function is called at stack startup to allocate the
377  *                  control block (if using dynamic memory), and initializes the
378  *                  control block and tracing level.
379  *
380  * Returns          void
381  *
382  ******************************************************************************/
AVRC_Init(void)383 void AVRC_Init(void) {
384   memset(&avrc_cb, 0, sizeof(tAVRC_CB));
385 }
386