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