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