a2d_api.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434
  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_set_a2dp_sdp_ver
  122. *
  123. * Description This function allows the script wrapper to change the
  124. * a2dp version
  125. *
  126. * Returns None
  127. *
  128. ******************************************************************************/
  129. void a2d_set_a2dp_sdp_ver(UINT16 a2dp_sdp_ver)
  130. {
  131. a2d_cb.a2dp_sdp_ver = a2dp_sdp_ver;
  132. }
  133. /******************************************************************************
  134. **
  135. ** Function A2D_AddRecord
  136. **
  137. ** Description This function is called by a server application to add
  138. ** SRC or SNK information to an SDP record. Prior to
  139. ** calling this function the application must call
  140. ** SDP_CreateRecord() to create an SDP record.
  141. **
  142. ** Input Parameters:
  143. ** service_uuid: Indicates SRC or SNK.
  144. **
  145. ** p_service_name: Pointer to a null-terminated character
  146. ** string containing the service name.
  147. **
  148. ** p_provider_name: Pointer to a null-terminated character
  149. ** string containing the provider name.
  150. **
  151. ** features: Profile supported features.
  152. **
  153. ** sdp_handle: SDP handle returned by SDP_CreateRecord().
  154. **
  155. ** Output Parameters:
  156. ** None.
  157. **
  158. ** Returns A2D_SUCCESS if function execution succeeded,
  159. ** A2D_INVALID_PARAMS if bad parameters are given.
  160. ** A2D_FAIL if function execution failed.
  161. **
  162. ******************************************************************************/
  163. tA2D_STATUS A2D_AddRecord(UINT16 service_uuid, char *p_service_name, char *p_provider_name,
  164. UINT16 features, UINT32 sdp_handle)
  165. {
  166. UINT16 browse_list[1];
  167. BOOLEAN result = TRUE;
  168. UINT8 temp[8];
  169. UINT8 *p;
  170. tSDP_PROTOCOL_ELEM proto_list [A2D_NUM_PROTO_ELEMS];
  171. A2D_TRACE_API("A2D_AddRecord uuid: %x", service_uuid);
  172. if ( (sdp_handle == 0) ||
  173. (service_uuid != UUID_SERVCLASS_AUDIO_SOURCE && service_uuid != UUID_SERVCLASS_AUDIO_SINK) ) {
  174. return A2D_INVALID_PARAMS;
  175. }
  176. /* add service class id list */
  177. result &= SDP_AddServiceClassIdList(sdp_handle, 1, &service_uuid);
  178. memset((void *) proto_list, 0 , A2D_NUM_PROTO_ELEMS * sizeof(tSDP_PROTOCOL_ELEM));
  179. /* add protocol descriptor list */
  180. proto_list[0].protocol_uuid = UUID_PROTOCOL_L2CAP;
  181. proto_list[0].num_params = 1;
  182. proto_list[0].params[0] = AVDT_PSM;
  183. proto_list[1].protocol_uuid = UUID_PROTOCOL_AVDTP;
  184. proto_list[1].num_params = 1;
  185. proto_list[1].params[0] = a2d_cb.avdt_sdp_ver;
  186. result &= SDP_AddProtocolList(sdp_handle, A2D_NUM_PROTO_ELEMS, proto_list);
  187. /* add profile descriptor list */
  188. result &= SDP_AddProfileDescriptorList(sdp_handle, UUID_SERVCLASS_ADV_AUDIO_DISTRIBUTION, a2d_cb.a2dp_sdp_ver);
  189. /* add supported feature */
  190. if (features != 0) {
  191. p = temp;
  192. UINT16_TO_BE_STREAM(p, features);
  193. result &= SDP_AddAttribute(sdp_handle, ATTR_ID_SUPPORTED_FEATURES, UINT_DESC_TYPE,
  194. (UINT32)2, (UINT8 *)temp);
  195. }
  196. /* add provider name */
  197. if (p_provider_name != NULL) {
  198. result &= SDP_AddAttribute(sdp_handle, ATTR_ID_PROVIDER_NAME, TEXT_STR_DESC_TYPE,
  199. (UINT32)(strlen(p_provider_name) + 1), (UINT8 *) p_provider_name);
  200. }
  201. /* add service name */
  202. if (p_service_name != NULL) {
  203. result &= SDP_AddAttribute(sdp_handle, ATTR_ID_SERVICE_NAME, TEXT_STR_DESC_TYPE,
  204. (UINT32)(strlen(p_service_name) + 1), (UINT8 *) p_service_name);
  205. }
  206. /* add browse group list */
  207. browse_list[0] = UUID_SERVCLASS_PUBLIC_BROWSE_GROUP;
  208. result &= SDP_AddUuidSequence(sdp_handle, ATTR_ID_BROWSE_GROUP_LIST, 1, browse_list);
  209. return (result ? A2D_SUCCESS : A2D_FAIL);
  210. }
  211. /******************************************************************************
  212. **
  213. ** Function A2D_FindService
  214. **
  215. ** Description This function is called by a client application to
  216. ** perform service discovery and retrieve SRC or SNK SDP
  217. ** record information from a server. Information is
  218. ** returned for the first service record found on the
  219. ** server that matches the service UUID. The callback
  220. ** function will be executed when service discovery is
  221. ** complete. There can only be one outstanding call to
  222. ** A2D_FindService() at a time; the application must wait
  223. ** for the callback before it makes another call to
  224. ** the function.
  225. **
  226. ** Input Parameters:
  227. ** service_uuid: Indicates SRC or SNK.
  228. **
  229. ** bd_addr: BD address of the peer device.
  230. **
  231. ** p_db: Pointer to the information to initialize
  232. ** the discovery database.
  233. **
  234. ** p_cback: Pointer to the A2D_FindService()
  235. ** callback function.
  236. **
  237. ** Output Parameters:
  238. ** None.
  239. **
  240. ** Returns A2D_SUCCESS if function execution succeeded,
  241. ** A2D_INVALID_PARAMS if bad parameters are given.
  242. ** A2D_BUSY if discovery is already in progress.
  243. ** A2D_FAIL if function execution failed.
  244. **
  245. ******************************************************************************/
  246. tA2D_STATUS A2D_FindService(UINT16 service_uuid, BD_ADDR bd_addr,
  247. tA2D_SDP_DB_PARAMS *p_db, tA2D_FIND_CBACK *p_cback)
  248. {
  249. tSDP_UUID uuid_list;
  250. BOOLEAN result = TRUE;
  251. UINT16 a2d_attr_list[] = {ATTR_ID_SERVICE_CLASS_ID_LIST, /* update A2D_NUM_ATTR, if changed */
  252. ATTR_ID_BT_PROFILE_DESC_LIST,
  253. ATTR_ID_SUPPORTED_FEATURES,
  254. ATTR_ID_SERVICE_NAME,
  255. ATTR_ID_PROTOCOL_DESC_LIST,
  256. ATTR_ID_PROVIDER_NAME
  257. };
  258. A2D_TRACE_API("A2D_FindService uuid: %x", service_uuid);
  259. if ( (service_uuid != UUID_SERVCLASS_AUDIO_SOURCE && service_uuid != UUID_SERVCLASS_AUDIO_SINK) ||
  260. p_db == NULL || p_db->p_db == NULL || p_cback == NULL) {
  261. return A2D_INVALID_PARAMS;
  262. }
  263. if ( a2d_cb.find.service_uuid == UUID_SERVCLASS_AUDIO_SOURCE ||
  264. a2d_cb.find.service_uuid == UUID_SERVCLASS_AUDIO_SINK) {
  265. return A2D_BUSY;
  266. }
  267. /* set up discovery database */
  268. uuid_list.len = LEN_UUID_16;
  269. uuid_list.uu.uuid16 = service_uuid;
  270. if (p_db->p_attrs == NULL || p_db->num_attr == 0) {
  271. p_db->p_attrs = a2d_attr_list;
  272. p_db->num_attr = A2D_NUM_ATTR;
  273. }
  274. result = SDP_InitDiscoveryDb(p_db->p_db, p_db->db_len, 1, &uuid_list, p_db->num_attr,
  275. p_db->p_attrs);
  276. if (result == TRUE) {
  277. /* store service_uuid and discovery db pointer */
  278. a2d_cb.find.p_db = p_db->p_db;
  279. a2d_cb.find.service_uuid = service_uuid;
  280. a2d_cb.find.p_cback = p_cback;
  281. /* perform service search */
  282. result = SDP_ServiceSearchAttributeRequest(bd_addr, p_db->p_db, a2d_sdp_cback);
  283. if (FALSE == result) {
  284. a2d_cb.find.service_uuid = 0;
  285. }
  286. }
  287. return (result ? A2D_SUCCESS : A2D_FAIL);
  288. }
  289. /******************************************************************************
  290. **
  291. ** Function A2D_SetTraceLevel
  292. **
  293. ** Description Sets the trace level for A2D. If 0xff is passed, the
  294. ** current trace level is returned.
  295. **
  296. ** Input Parameters:
  297. ** new_level: The level to set the A2D tracing to:
  298. ** 0xff-returns the current setting.
  299. ** 0-turns off tracing.
  300. ** >= 1-Errors.
  301. ** >= 2-Warnings.
  302. ** >= 3-APIs.
  303. ** >= 4-Events.
  304. ** >= 5-Debug.
  305. **
  306. ** Returns The new trace level or current trace level if
  307. ** the input parameter is 0xff.
  308. **
  309. ******************************************************************************/
  310. UINT8 A2D_SetTraceLevel (UINT8 new_level)
  311. {
  312. if (new_level != 0xFF) {
  313. a2d_cb.trace_level = new_level;
  314. }
  315. return (a2d_cb.trace_level);
  316. }
  317. /******************************************************************************
  318. ** Function A2D_BitsSet
  319. **
  320. ** Description Check the given num for the number of bits set
  321. ** Returns A2D_SET_ONE_BIT, if one and only one bit is set
  322. ** A2D_SET_ZERO_BIT, if all bits clear
  323. ** A2D_SET_MULTL_BIT, if multiple bits are set
  324. ******************************************************************************/
  325. UINT8 A2D_BitsSet(UINT8 num)
  326. {
  327. UINT8 count;
  328. UINT8 res;
  329. if (num == 0) {
  330. res = A2D_SET_ZERO_BIT;
  331. } else {
  332. count = (num & (num - 1));
  333. res = ((count == 0) ? A2D_SET_ONE_BIT : A2D_SET_MULTL_BIT);
  334. }
  335. return res;
  336. }
  337. /*******************************************************************************
  338. **
  339. ** Function A2D_Init
  340. **
  341. ** Description This function is called to initialize the control block
  342. ** for this layer. It must be called before accessing any
  343. ** other API functions for this layer. It is typically called
  344. ** once during the start up of the stack.
  345. **
  346. ** Returns status
  347. **
  348. *******************************************************************************/
  349. bt_status_t A2D_Init(void)
  350. {
  351. #if (A2D_DYNAMIC_MEMORY)
  352. a2d_cb_ptr = (tA2D_CB *)osi_malloc(sizeof(tA2D_CB));
  353. if (!a2d_cb_ptr) {
  354. return BT_STATUS_NOMEM;
  355. }
  356. #endif /* #if (A2D_DYNAMIC_MEMORY) */
  357. memset(&a2d_cb, 0, sizeof(tA2D_CB));
  358. a2d_cb.avdt_sdp_ver = AVDT_VERSION;
  359. a2d_cb.a2dp_sdp_ver = A2D_VERSION;
  360. #if defined(A2D_INITIAL_TRACE_LEVEL)
  361. a2d_cb.trace_level = A2D_INITIAL_TRACE_LEVEL;
  362. #else
  363. a2d_cb.trace_level = BT_TRACE_LEVEL_NONE;
  364. #endif
  365. return BT_STATUS_SUCCESS;
  366. }
  367. /*******************************************************************************
  368. **
  369. ** Function A2D_Deinit
  370. **
  371. ** Description This function is called to deinitialize the control block
  372. ** for this layer.
  373. **
  374. ** Returns void
  375. **
  376. *******************************************************************************/
  377. void A2D_Deinit(void)
  378. {
  379. #if (A2D_DYNAMIC_MEMORY)
  380. if (a2d_cb_ptr) {
  381. osi_free(a2d_cb_ptr);
  382. a2d_cb_ptr = NULL;
  383. }
  384. #endif /* #if (A2D_DYNAMIC_MEMORY) */
  385. }
  386. #endif /* #if (defined(A2D_INCLUDED) && A2D_INCLUDED == TRUE) */