a2d_api.c 14 KB

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