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