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