a2d_api.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418
  1. /******************************************************************************
  2. *
  3. * Copyright (C) 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. * ommon API for the Advanced Audio Distribution Profile (A2DP)
  21. *
  22. ******************************************************************************/
  23. #include <string.h>
  24. #include "common/bt_target.h"
  25. #include "stack/sdpdefs.h"
  26. #include "stack/a2d_api.h"
  27. #include "a2d_int.h"
  28. #include "stack/avdt_api.h"
  29. #include "osi/allocator.h"
  30. #if (defined(A2D_INCLUDED) && A2D_INCLUDED == TRUE)
  31. /*****************************************************************************
  32. ** Global data
  33. *****************************************************************************/
  34. #if A2D_DYNAMIC_MEMORY == FALSE
  35. tA2D_CB a2d_cb;
  36. #else
  37. tA2D_CB *a2d_cb_ptr;
  38. #endif
  39. /******************************************************************************
  40. **
  41. ** Function a2d_sdp_cback
  42. **
  43. ** Description This is the SDP callback function used by A2D_FindService.
  44. ** This function will be executed by SDP when the service
  45. ** search is completed. If the search is successful, it
  46. ** finds the first record in the database that matches the
  47. ** UUID of the search. Then retrieves various parameters
  48. ** from the record. When it is finished it calls the
  49. ** application callback function.
  50. **
  51. ** Returns Nothing.
  52. **
  53. ******************************************************************************/
  54. static void a2d_sdp_cback(UINT16 status)
  55. {
  56. tSDP_DISC_REC *p_rec = NULL;
  57. tSDP_DISC_ATTR *p_attr;
  58. BOOLEAN found = FALSE;
  59. tA2D_Service a2d_svc;
  60. tSDP_PROTOCOL_ELEM elem;
  61. A2D_TRACE_API("a2d_sdp_cback status: %d", status);
  62. if (status == SDP_SUCCESS) {
  63. /* loop through all records we found */
  64. do {
  65. /* get next record; if none found, we're done */
  66. if ((p_rec = SDP_FindServiceInDb(a2d_cb.find.p_db,
  67. a2d_cb.find.service_uuid, p_rec)) == NULL) {
  68. break;
  69. }
  70. memset(&a2d_svc, 0, sizeof(tA2D_Service));
  71. /* get service name */
  72. if ((p_attr = SDP_FindAttributeInRec(p_rec,
  73. ATTR_ID_SERVICE_NAME)) != NULL) {
  74. a2d_svc.p_service_name = (char *) p_attr->attr_value.v.array;
  75. a2d_svc.service_len = SDP_DISC_ATTR_LEN(p_attr->attr_len_type);
  76. }
  77. /* get provider name */
  78. if ((p_attr = SDP_FindAttributeInRec(p_rec,
  79. ATTR_ID_PROVIDER_NAME)) != NULL) {
  80. a2d_svc.p_provider_name = (char *) p_attr->attr_value.v.array;
  81. a2d_svc.provider_len = SDP_DISC_ATTR_LEN(p_attr->attr_len_type);
  82. }
  83. /* get supported features */
  84. if ((p_attr = SDP_FindAttributeInRec(p_rec,
  85. ATTR_ID_SUPPORTED_FEATURES)) != NULL) {
  86. a2d_svc.features = p_attr->attr_value.v.u16;
  87. }
  88. /* get AVDTP version */
  89. if (SDP_FindProtocolListElemInRec(p_rec, UUID_PROTOCOL_AVDTP, &elem)) {
  90. a2d_svc.avdt_version = elem.params[0];
  91. A2D_TRACE_DEBUG("avdt_version: 0x%x", a2d_svc.avdt_version);
  92. }
  93. /* we've got everything, we're done */
  94. found = TRUE;
  95. break;
  96. } while (TRUE);
  97. }
  98. a2d_cb.find.service_uuid = 0;
  99. /* return info from sdp record in app callback function */
  100. if (a2d_cb.find.p_cback != NULL) {
  101. (*a2d_cb.find.p_cback)(found, &a2d_svc);
  102. }
  103. return;
  104. }
  105. /*******************************************************************************
  106. **
  107. ** Function a2d_set_avdt_sdp_ver
  108. **
  109. ** Description This function allows the script wrapper to change the
  110. ** avdt version of a2dp.
  111. **
  112. ** Returns None
  113. **
  114. *******************************************************************************/
  115. void a2d_set_avdt_sdp_ver (UINT16 avdt_sdp_ver)
  116. {
  117. a2d_cb.avdt_sdp_ver = avdt_sdp_ver;
  118. }
  119. /******************************************************************************
  120. **
  121. ** Function A2D_AddRecord
  122. **
  123. ** Description This function is called by a server application to add
  124. ** SRC or SNK information to an SDP record. Prior to
  125. ** calling this function the application must call
  126. ** SDP_CreateRecord() to create an SDP record.
  127. **
  128. ** Input Parameters:
  129. ** service_uuid: Indicates SRC or SNK.
  130. **
  131. ** p_service_name: Pointer to a null-terminated character
  132. ** string containing the service name.
  133. **
  134. ** p_provider_name: Pointer to a null-terminated character
  135. ** string containing the provider name.
  136. **
  137. ** features: Profile supported features.
  138. **
  139. ** sdp_handle: SDP handle returned by SDP_CreateRecord().
  140. **
  141. ** Output Parameters:
  142. ** None.
  143. **
  144. ** Returns A2D_SUCCESS if function execution succeeded,
  145. ** A2D_INVALID_PARAMS if bad parameters are given.
  146. ** A2D_FAIL if function execution failed.
  147. **
  148. ******************************************************************************/
  149. tA2D_STATUS A2D_AddRecord(UINT16 service_uuid, char *p_service_name, char *p_provider_name,
  150. UINT16 features, UINT32 sdp_handle)
  151. {
  152. UINT16 browse_list[1];
  153. BOOLEAN result = TRUE;
  154. UINT8 temp[8];
  155. UINT8 *p;
  156. tSDP_PROTOCOL_ELEM proto_list [A2D_NUM_PROTO_ELEMS];
  157. A2D_TRACE_API("A2D_AddRecord uuid: %x", service_uuid);
  158. if ( (sdp_handle == 0) ||
  159. (service_uuid != UUID_SERVCLASS_AUDIO_SOURCE && service_uuid != UUID_SERVCLASS_AUDIO_SINK) ) {
  160. return A2D_INVALID_PARAMS;
  161. }
  162. /* add service class id list */
  163. result &= SDP_AddServiceClassIdList(sdp_handle, 1, &service_uuid);
  164. memset((void *) proto_list, 0 , A2D_NUM_PROTO_ELEMS * sizeof(tSDP_PROTOCOL_ELEM));
  165. /* add protocol descriptor list */
  166. proto_list[0].protocol_uuid = UUID_PROTOCOL_L2CAP;
  167. proto_list[0].num_params = 1;
  168. proto_list[0].params[0] = AVDT_PSM;
  169. proto_list[1].protocol_uuid = UUID_PROTOCOL_AVDTP;
  170. proto_list[1].num_params = 1;
  171. proto_list[1].params[0] = a2d_cb.avdt_sdp_ver;
  172. result &= SDP_AddProtocolList(sdp_handle, A2D_NUM_PROTO_ELEMS, proto_list);
  173. /* add profile descriptor list */
  174. result &= SDP_AddProfileDescriptorList(sdp_handle, UUID_SERVCLASS_ADV_AUDIO_DISTRIBUTION, A2D_VERSION);
  175. /* add supported feature */
  176. if (features != 0) {
  177. p = temp;
  178. UINT16_TO_BE_STREAM(p, features);
  179. result &= SDP_AddAttribute(sdp_handle, ATTR_ID_SUPPORTED_FEATURES, UINT_DESC_TYPE,
  180. (UINT32)2, (UINT8 *)temp);
  181. }
  182. /* add provider name */
  183. if (p_provider_name != NULL) {
  184. result &= SDP_AddAttribute(sdp_handle, ATTR_ID_PROVIDER_NAME, TEXT_STR_DESC_TYPE,
  185. (UINT32)(strlen(p_provider_name) + 1), (UINT8 *) p_provider_name);
  186. }
  187. /* add service name */
  188. if (p_service_name != NULL) {
  189. result &= SDP_AddAttribute(sdp_handle, ATTR_ID_SERVICE_NAME, TEXT_STR_DESC_TYPE,
  190. (UINT32)(strlen(p_service_name) + 1), (UINT8 *) p_service_name);
  191. }
  192. /* add browse group list */
  193. browse_list[0] = UUID_SERVCLASS_PUBLIC_BROWSE_GROUP;
  194. result &= SDP_AddUuidSequence(sdp_handle, ATTR_ID_BROWSE_GROUP_LIST, 1, browse_list);
  195. return (result ? A2D_SUCCESS : A2D_FAIL);
  196. }
  197. /******************************************************************************
  198. **
  199. ** Function A2D_FindService
  200. **
  201. ** Description This function is called by a client application to
  202. ** perform service discovery and retrieve SRC or SNK SDP
  203. ** record information from a server. Information is
  204. ** returned for the first service record found on the
  205. ** server that matches the service UUID. The callback
  206. ** function will be executed when service discovery is
  207. ** complete. There can only be one outstanding call to
  208. ** A2D_FindService() at a time; the application must wait
  209. ** for the callback before it makes another call to
  210. ** the function.
  211. **
  212. ** Input Parameters:
  213. ** service_uuid: Indicates SRC or SNK.
  214. **
  215. ** bd_addr: BD address of the peer device.
  216. **
  217. ** p_db: Pointer to the information to initialize
  218. ** the discovery database.
  219. **
  220. ** p_cback: Pointer to the A2D_FindService()
  221. ** callback function.
  222. **
  223. ** Output Parameters:
  224. ** None.
  225. **
  226. ** Returns A2D_SUCCESS if function execution succeeded,
  227. ** A2D_INVALID_PARAMS if bad parameters are given.
  228. ** A2D_BUSY if discovery is already in progress.
  229. ** A2D_FAIL if function execution failed.
  230. **
  231. ******************************************************************************/
  232. tA2D_STATUS A2D_FindService(UINT16 service_uuid, BD_ADDR bd_addr,
  233. tA2D_SDP_DB_PARAMS *p_db, tA2D_FIND_CBACK *p_cback)
  234. {
  235. tSDP_UUID uuid_list;
  236. BOOLEAN result = TRUE;
  237. UINT16 a2d_attr_list[] = {ATTR_ID_SERVICE_CLASS_ID_LIST, /* update A2D_NUM_ATTR, if changed */
  238. ATTR_ID_BT_PROFILE_DESC_LIST,
  239. ATTR_ID_SUPPORTED_FEATURES,
  240. ATTR_ID_SERVICE_NAME,
  241. ATTR_ID_PROTOCOL_DESC_LIST,
  242. ATTR_ID_PROVIDER_NAME
  243. };
  244. A2D_TRACE_API("A2D_FindService uuid: %x", service_uuid);
  245. if ( (service_uuid != UUID_SERVCLASS_AUDIO_SOURCE && service_uuid != UUID_SERVCLASS_AUDIO_SINK) ||
  246. p_db == NULL || p_db->p_db == NULL || p_cback == NULL) {
  247. return A2D_INVALID_PARAMS;
  248. }
  249. if ( a2d_cb.find.service_uuid == UUID_SERVCLASS_AUDIO_SOURCE ||
  250. a2d_cb.find.service_uuid == UUID_SERVCLASS_AUDIO_SINK) {
  251. return A2D_BUSY;
  252. }
  253. /* set up discovery database */
  254. uuid_list.len = LEN_UUID_16;
  255. uuid_list.uu.uuid16 = service_uuid;
  256. if (p_db->p_attrs == NULL || p_db->num_attr == 0) {
  257. p_db->p_attrs = a2d_attr_list;
  258. p_db->num_attr = A2D_NUM_ATTR;
  259. }
  260. result = SDP_InitDiscoveryDb(p_db->p_db, p_db->db_len, 1, &uuid_list, p_db->num_attr,
  261. p_db->p_attrs);
  262. if (result == TRUE) {
  263. /* store service_uuid and discovery db pointer */
  264. a2d_cb.find.p_db = p_db->p_db;
  265. a2d_cb.find.service_uuid = service_uuid;
  266. a2d_cb.find.p_cback = p_cback;
  267. /* perform service search */
  268. result = SDP_ServiceSearchAttributeRequest(bd_addr, p_db->p_db, a2d_sdp_cback);
  269. if (FALSE == result) {
  270. a2d_cb.find.service_uuid = 0;
  271. }
  272. }
  273. return (result ? A2D_SUCCESS : A2D_FAIL);
  274. }
  275. /******************************************************************************
  276. **
  277. ** Function A2D_SetTraceLevel
  278. **
  279. ** Description Sets the trace level for A2D. If 0xff is passed, the
  280. ** current trace level is returned.
  281. **
  282. ** Input Parameters:
  283. ** new_level: The level to set the A2D tracing to:
  284. ** 0xff-returns the current setting.
  285. ** 0-turns off tracing.
  286. ** >= 1-Errors.
  287. ** >= 2-Warnings.
  288. ** >= 3-APIs.
  289. ** >= 4-Events.
  290. ** >= 5-Debug.
  291. **
  292. ** Returns The new trace level or current trace level if
  293. ** the input parameter is 0xff.
  294. **
  295. ******************************************************************************/
  296. UINT8 A2D_SetTraceLevel (UINT8 new_level)
  297. {
  298. if (new_level != 0xFF) {
  299. a2d_cb.trace_level = new_level;
  300. }
  301. return (a2d_cb.trace_level);
  302. }
  303. /******************************************************************************
  304. ** Function A2D_BitsSet
  305. **
  306. ** Description Check the given num for the number of bits set
  307. ** Returns A2D_SET_ONE_BIT, if one and only one bit is set
  308. ** A2D_SET_ZERO_BIT, if all bits clear
  309. ** A2D_SET_MULTL_BIT, if multiple bits are set
  310. ******************************************************************************/
  311. UINT8 A2D_BitsSet(UINT8 num)
  312. {
  313. UINT8 count;
  314. UINT8 res;
  315. if (num == 0) {
  316. res = A2D_SET_ZERO_BIT;
  317. } else {
  318. count = (num & (num - 1));
  319. res = ((count == 0) ? A2D_SET_ONE_BIT : A2D_SET_MULTL_BIT);
  320. }
  321. return res;
  322. }
  323. /*******************************************************************************
  324. **
  325. ** Function A2D_Init
  326. **
  327. ** Description This function is called to initialize the control block
  328. ** for this layer. It must be called before accessing any
  329. ** other API functions for this layer. It is typically called
  330. ** once during the start up of the stack.
  331. **
  332. ** Returns status
  333. **
  334. *******************************************************************************/
  335. bt_status_t A2D_Init(void)
  336. {
  337. #if (A2D_DYNAMIC_MEMORY)
  338. a2d_cb_ptr = (tA2D_CB *)osi_malloc(sizeof(tA2D_CB));
  339. if (!a2d_cb_ptr) {
  340. return BT_STATUS_NOMEM;
  341. }
  342. #endif /* #if (A2D_DYNAMIC_MEMORY) */
  343. memset(&a2d_cb, 0, sizeof(tA2D_CB));
  344. a2d_cb.avdt_sdp_ver = AVDT_VERSION;
  345. #if defined(A2D_INITIAL_TRACE_LEVEL)
  346. a2d_cb.trace_level = A2D_INITIAL_TRACE_LEVEL;
  347. #else
  348. a2d_cb.trace_level = BT_TRACE_LEVEL_NONE;
  349. #endif
  350. return BT_STATUS_SUCCESS;
  351. }
  352. /*******************************************************************************
  353. **
  354. ** Function A2D_Deinit
  355. **
  356. ** Description This function is called to deinitialize the control block
  357. ** for this layer.
  358. **
  359. ** Returns void
  360. **
  361. *******************************************************************************/
  362. void A2D_Deinit(void)
  363. {
  364. #if (A2D_DYNAMIC_MEMORY)
  365. if (a2d_cb_ptr) {
  366. osi_free(a2d_cb_ptr);
  367. a2d_cb_ptr = NULL;
  368. }
  369. #endif /* #if (A2D_DYNAMIC_MEMORY) */
  370. }
  371. #endif /* #if (defined(A2D_INCLUDED) && A2D_INCLUDED == TRUE) */