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