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