1 /******************************************************************************
2 *
3 * Copyright 2002-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 * Common API for the Advanced Audio Distribution Profile (A2DP)
22 *
23 ******************************************************************************/
24
25 #define LOG_TAG "bluetooth-a2dp"
26
27 #include "a2dp_api.h"
28
29 #include <bluetooth/log.h>
30 #include <string.h>
31
32 #include "a2dp_int.h"
33 #include "avdt_api.h"
34 #include "internal_include/bt_target.h"
35 #include "osi/include/allocator.h"
36 #include "sdpdefs.h"
37 #include "stack/include/bt_types.h"
38 #include "stack/include/bt_uuid16.h"
39 #include "stack/include/sdp_api.h"
40 #include "types/bluetooth/uuid.h"
41 #include "types/raw_address.h"
42
43 using namespace bluetooth;
44 using namespace bluetooth::legacy::stack::sdp;
45
46 using bluetooth::Uuid;
47
48 /*****************************************************************************
49 * Global data
50 ****************************************************************************/
51 tA2DP_CB a2dp_cb;
52 static uint16_t a2dp_attr_list[] = {
53 ATTR_ID_SERVICE_CLASS_ID_LIST, /* update A2DP_NUM_ATTR, if changed */
54 ATTR_ID_BT_PROFILE_DESC_LIST, ATTR_ID_SUPPORTED_FEATURES,
55 ATTR_ID_SERVICE_NAME, ATTR_ID_PROTOCOL_DESC_LIST,
56 ATTR_ID_PROVIDER_NAME};
57
58 /******************************************************************************
59 *
60 * Function a2dp_sdp_cback
61 *
62 * Description This is the SDP callback function used by A2DP_FindService.
63 * This function will be executed by SDP when the service
64 * search is completed. If the search is successful, it
65 * finds the first record in the database that matches the
66 * UUID of the search. Then retrieves various parameters
67 * from the record. When it is finished it calls the
68 * application callback function.
69 *
70 * Returns Nothing.
71 *
72 *****************************************************************************/
a2dp_sdp_cback(const RawAddress &,tSDP_STATUS status)73 static void a2dp_sdp_cback(const RawAddress& /* bd_addr */,
74 tSDP_STATUS status) {
75 tSDP_DISC_REC* p_rec = NULL;
76 tSDP_DISC_ATTR* p_attr;
77 bool found = false;
78 tA2DP_Service a2dp_svc;
79 tSDP_PROTOCOL_ELEM elem;
80 RawAddress peer_address = RawAddress::kEmpty;
81
82 log::info("status: {}", status);
83
84 if (status == SDP_SUCCESS) {
85 /* loop through all records we found */
86 do {
87 /* get next record; if none found, we're done */
88 if ((p_rec = get_legacy_stack_sdp_api()->db.SDP_FindServiceInDb(
89 a2dp_cb.find.p_db, a2dp_cb.find.service_uuid, p_rec)) == NULL) {
90 break;
91 }
92 memset(&a2dp_svc, 0, sizeof(tA2DP_Service));
93 peer_address = p_rec->remote_bd_addr;
94
95 /* get service name */
96 if ((p_attr = get_legacy_stack_sdp_api()->record.SDP_FindAttributeInRec(
97 p_rec, ATTR_ID_SERVICE_NAME)) != NULL) {
98 if (SDP_DISC_ATTR_TYPE(p_attr->attr_len_type) == TEXT_STR_DESC_TYPE) {
99 a2dp_svc.p_service_name = (char*)p_attr->attr_value.v.array;
100 a2dp_svc.service_len = SDP_DISC_ATTR_LEN(p_attr->attr_len_type);
101 } else {
102 log::error("ATTR_ID_SERVICE_NAME attr type not STR!!");
103 }
104 } else {
105 log::error("ATTR_ID_SERVICE_NAME attr not found!!");
106 }
107
108 /* get provider name */
109 if ((p_attr = get_legacy_stack_sdp_api()->record.SDP_FindAttributeInRec(
110 p_rec, ATTR_ID_PROVIDER_NAME)) != NULL) {
111 if (SDP_DISC_ATTR_TYPE(p_attr->attr_len_type) == TEXT_STR_DESC_TYPE) {
112 a2dp_svc.p_provider_name = (char*)p_attr->attr_value.v.array;
113 a2dp_svc.provider_len = SDP_DISC_ATTR_LEN(p_attr->attr_len_type);
114 } else {
115 log::error("ATTR_ID_PROVIDER_NAME attr type not STR!!");
116 }
117 } else {
118 log::error("ATTR_ID_PROVIDER_NAME attr not found!!");
119 }
120
121 /* get supported features */
122 if ((p_attr = get_legacy_stack_sdp_api()->record.SDP_FindAttributeInRec(
123 p_rec, ATTR_ID_SUPPORTED_FEATURES)) != NULL) {
124 if (SDP_DISC_ATTR_TYPE(p_attr->attr_len_type) == UINT_DESC_TYPE &&
125 SDP_DISC_ATTR_LEN(p_attr->attr_len_type) >= 2) {
126 a2dp_svc.features = p_attr->attr_value.v.u16;
127 } else {
128 log::error("ATTR_ID_SUPPORTED_FEATURES attr type not STR!!");
129 }
130 } else {
131 log::error("ATTR_ID_SUPPORTED_FEATURES attr not found!!");
132 }
133
134 /* get AVDTP version */
135 if (get_legacy_stack_sdp_api()->record.SDP_FindProtocolListElemInRec(
136 p_rec, UUID_PROTOCOL_AVDTP, &elem)) {
137 a2dp_svc.avdt_version = elem.params[0];
138 log::verbose("avdt_version: 0x{:x}", a2dp_svc.avdt_version);
139 }
140
141 /* we've got everything, we're done */
142 found = true;
143 break;
144
145 } while (true);
146 }
147
148 a2dp_cb.find.service_uuid = 0;
149 osi_free_and_reset((void**)&a2dp_cb.find.p_db);
150 /* return info from sdp record in app callback function */
151 if (!a2dp_cb.find.p_cback.is_null()) {
152 a2dp_cb.find.p_cback.Run(found, &a2dp_svc, peer_address);
153 }
154
155 return;
156 }
157
158 /*******************************************************************************
159 *
160 * Function a2dp_set_avdt_sdp_ver
161 *
162 * Description This function allows the script wrapper to change the
163 * avdt version of a2dp.
164 *
165 * Returns None
166 *
167 ******************************************************************************/
a2dp_set_avdt_sdp_ver(uint16_t avdt_sdp_ver)168 void a2dp_set_avdt_sdp_ver(uint16_t avdt_sdp_ver) {
169 a2dp_cb.avdt_sdp_ver = avdt_sdp_ver;
170 }
171
172 /******************************************************************************
173 *
174 * Function A2DP_AddRecord
175 *
176 * Description This function is called by a server application to add
177 * SRC or SNK information to an SDP record. Prior to
178 * calling this function the application must call
179 * SDP_CreateRecord() to create an SDP record.
180 *
181 * Input Parameters:
182 * service_uuid: Indicates SRC or SNK.
183 *
184 * p_service_name: Pointer to a null-terminated character
185 * string containing the service name.
186 *
187 * p_provider_name: Pointer to a null-terminated character
188 * string containing the provider name.
189 *
190 * features: Profile supported features.
191 *
192 * sdp_handle: SDP handle returned by SDP_CreateRecord().
193 *
194 * Output Parameters:
195 * None.
196 *
197 * Returns A2DP_SUCCESS if function execution succeeded,
198 * A2DP_INVALID_PARAMS if bad parameters are given.
199 * A2DP_FAIL if function execution failed.
200 *
201 *****************************************************************************/
A2DP_AddRecord(uint16_t service_uuid,char * p_service_name,char * p_provider_name,uint16_t features,uint32_t sdp_handle)202 tA2DP_STATUS A2DP_AddRecord(uint16_t service_uuid, char* p_service_name,
203 char* p_provider_name, uint16_t features,
204 uint32_t sdp_handle) {
205 uint16_t browse_list[1];
206 bool result = true;
207 uint8_t temp[8];
208 uint8_t* p;
209 tSDP_PROTOCOL_ELEM proto_list[A2DP_NUM_PROTO_ELEMS];
210
211 log::verbose("uuid: 0x{:x}", service_uuid);
212
213 if ((sdp_handle == 0) || (service_uuid != UUID_SERVCLASS_AUDIO_SOURCE &&
214 service_uuid != UUID_SERVCLASS_AUDIO_SINK))
215 return A2DP_INVALID_PARAMS;
216
217 /* add service class id list */
218 result &= get_legacy_stack_sdp_api()->handle.SDP_AddServiceClassIdList(
219 sdp_handle, 1, &service_uuid);
220
221 memset((void*)proto_list, 0,
222 A2DP_NUM_PROTO_ELEMS * sizeof(tSDP_PROTOCOL_ELEM));
223
224 /* add protocol descriptor list */
225 proto_list[0].protocol_uuid = UUID_PROTOCOL_L2CAP;
226 proto_list[0].num_params = 1;
227 proto_list[0].params[0] = AVDT_PSM;
228 proto_list[1].protocol_uuid = UUID_PROTOCOL_AVDTP;
229 proto_list[1].num_params = 1;
230 proto_list[1].params[0] = a2dp_cb.avdt_sdp_ver;
231
232 result &= get_legacy_stack_sdp_api()->handle.SDP_AddProtocolList(
233 sdp_handle, A2DP_NUM_PROTO_ELEMS, proto_list);
234
235 /* add profile descriptor list */
236 result &= get_legacy_stack_sdp_api()->handle.SDP_AddProfileDescriptorList(
237 sdp_handle, UUID_SERVCLASS_ADV_AUDIO_DISTRIBUTION, A2DP_VERSION);
238
239 /* add supported feature */
240 if (features != 0) {
241 p = temp;
242 UINT16_TO_BE_STREAM(p, features);
243 result &= get_legacy_stack_sdp_api()->handle.SDP_AddAttribute(
244 sdp_handle, ATTR_ID_SUPPORTED_FEATURES, UINT_DESC_TYPE, (uint32_t)2,
245 (uint8_t*)temp);
246 }
247
248 /* add provider name */
249 if (p_provider_name != NULL) {
250 result &= get_legacy_stack_sdp_api()->handle.SDP_AddAttribute(
251 sdp_handle, ATTR_ID_PROVIDER_NAME, TEXT_STR_DESC_TYPE,
252 (uint32_t)(strlen(p_provider_name) + 1), (uint8_t*)p_provider_name);
253 }
254
255 /* add service name */
256 if (p_service_name != NULL) {
257 result &= get_legacy_stack_sdp_api()->handle.SDP_AddAttribute(
258 sdp_handle, ATTR_ID_SERVICE_NAME, TEXT_STR_DESC_TYPE,
259 (uint32_t)(strlen(p_service_name) + 1), (uint8_t*)p_service_name);
260 }
261
262 /* add browse group list */
263 browse_list[0] = UUID_SERVCLASS_PUBLIC_BROWSE_GROUP;
264 result &= get_legacy_stack_sdp_api()->handle.SDP_AddUuidSequence(
265 sdp_handle, ATTR_ID_BROWSE_GROUP_LIST, 1, browse_list);
266
267 return (result ? A2DP_SUCCESS : A2DP_FAIL);
268 }
269
270 /******************************************************************************
271 *
272 * Function A2DP_FindService
273 *
274 * Description This function is called by a client application to
275 * perform service discovery and retrieve SRC or SNK SDP
276 * record information from a server. Information is
277 * returned for the first service record found on the
278 * server that matches the service UUID. The callback
279 * function will be executed when service discovery is
280 * complete. There can only be one outstanding call to
281 * A2DP_FindService() at a time; the application must wait
282 * for the callback before it makes another call to
283 * the function.
284 *
285 * Input Parameters:
286 * service_uuid: Indicates SRC or SNK.
287 *
288 * bd_addr: BD address of the peer device.
289 *
290 * p_db: Pointer to the information to initialize
291 * the discovery database.
292 *
293 * p_cback: Pointer to the A2DP_FindService()
294 * callback function.
295 *
296 * Output Parameters:
297 * None.
298 *
299 * Returns A2DP_SUCCESS if function execution succeeded,
300 * A2DP_INVALID_PARAMS if bad parameters are given.
301 * A2DP_BUSY if discovery is already in progress.
302 * A2DP_FAIL if function execution failed.
303 *
304 *****************************************************************************/
A2DP_FindService(uint16_t service_uuid,const RawAddress & bd_addr,tA2DP_SDP_DB_PARAMS * p_db,tA2DP_FIND_CBACK p_cback)305 tA2DP_STATUS A2DP_FindService(uint16_t service_uuid, const RawAddress& bd_addr,
306 tA2DP_SDP_DB_PARAMS* p_db,
307 tA2DP_FIND_CBACK p_cback) {
308 if ((service_uuid != UUID_SERVCLASS_AUDIO_SOURCE &&
309 service_uuid != UUID_SERVCLASS_AUDIO_SINK) ||
310 p_db == NULL || p_cback.is_null()) {
311 log::error(
312 "Cannot find service for peer {} UUID 0x{:04x}: invalid parameters",
313 bd_addr, service_uuid);
314 return A2DP_INVALID_PARAMS;
315 }
316
317 if (a2dp_cb.find.service_uuid == UUID_SERVCLASS_AUDIO_SOURCE ||
318 a2dp_cb.find.service_uuid == UUID_SERVCLASS_AUDIO_SINK ||
319 a2dp_cb.find.p_db != NULL) {
320 log::error("Cannot find service for peer {} UUID 0x{:04x}: busy", bd_addr,
321 service_uuid);
322 return A2DP_BUSY;
323 }
324
325 if (p_db->p_attrs == NULL || p_db->num_attr == 0) {
326 p_db->p_attrs = a2dp_attr_list;
327 p_db->num_attr = A2DP_NUM_ATTR;
328 }
329
330 a2dp_cb.find.p_db = (tSDP_DISCOVERY_DB*)osi_malloc(p_db->db_len);
331 Uuid uuid_list = Uuid::From16Bit(service_uuid);
332
333 if (!get_legacy_stack_sdp_api()->service.SDP_InitDiscoveryDb(
334 a2dp_cb.find.p_db, p_db->db_len, 1, &uuid_list, p_db->num_attr,
335 p_db->p_attrs)) {
336 osi_free_and_reset((void**)&a2dp_cb.find.p_db);
337 log::error("Unable to initialize SDP discovery for peer {} UUID 0x{:04X}",
338 bd_addr, service_uuid);
339 return A2DP_FAIL;
340 }
341
342 /* store service_uuid */
343 a2dp_cb.find.service_uuid = service_uuid;
344 a2dp_cb.find.p_cback = p_cback;
345
346 /* perform service search */
347 if (!get_legacy_stack_sdp_api()->service.SDP_ServiceSearchAttributeRequest(
348 bd_addr, a2dp_cb.find.p_db, a2dp_sdp_cback)) {
349 a2dp_cb.find.service_uuid = 0;
350 a2dp_cb.find.p_cback.Reset();
351 osi_free_and_reset((void**)&a2dp_cb.find.p_db);
352 log::error("Cannot find service for peer {} UUID 0x{:04x}: SDP error",
353 bd_addr, service_uuid);
354 return A2DP_FAIL;
355 }
356 log::info(
357 "A2DP service discovery for peer {} UUID 0x{:04x}: SDP search started",
358 bd_addr, service_uuid);
359 return A2DP_SUCCESS;
360 }
361
362 /******************************************************************************
363 * Function A2DP_BitsSet
364 *
365 * Description Check the given num for the number of bits set
366 * Returns A2DP_SET_ONE_BIT, if one and only one bit is set
367 * A2DP_SET_ZERO_BIT, if all bits clear
368 * A2DP_SET_MULTL_BIT, if multiple bits are set
369 *****************************************************************************/
A2DP_BitsSet(uint64_t num)370 uint8_t A2DP_BitsSet(uint64_t num) {
371 if (num == 0) return A2DP_SET_ZERO_BIT;
372 if ((num & (num - 1)) == 0) return A2DP_SET_ONE_BIT;
373 return A2DP_SET_MULTL_BIT;
374 }
375
376 /*******************************************************************************
377 *
378 * Function A2DP_Init
379 *
380 * Description This function is called to initialize the control block
381 * for this layer. It must be called before accessing any
382 * other API functions for this layer. It is typically called
383 * once during the start up of the stack.
384 *
385 * Returns void
386 *
387 ******************************************************************************/
A2DP_Init(void)388 void A2DP_Init(void) {
389 memset(&a2dp_cb, 0, sizeof(tA2DP_CB));
390
391 a2dp_cb.avdt_sdp_ver = AVDT_VERSION;
392 }
393
A2DP_GetAvdtpVersion()394 uint16_t A2DP_GetAvdtpVersion() { return a2dp_cb.avdt_sdp_ver; }
395