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