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