avrc_sdp.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403
  1. /******************************************************************************
  2. *
  3. * Copyright (C) 2003-2013 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. * AVRCP SDP related functions
  21. *
  22. ******************************************************************************/
  23. #include <string.h>
  24. #include "common/bt_target.h"
  25. #include "stack/avrc_api.h"
  26. #include "avrc_int.h"
  27. #include "osi/allocator.h"
  28. #if (defined(AVRC_INCLUDED) && AVRC_INCLUDED == TRUE)
  29. #ifndef SDP_AVRCP_1_5
  30. #define SDP_AVRCP_1_5 TRUE
  31. #endif
  32. #ifndef SDP_AVCTP_1_4
  33. #define SDP_AVCTP_1_4 TRUE
  34. #endif
  35. /*****************************************************************************
  36. ** Global data
  37. *****************************************************************************/
  38. #if AVRC_DYNAMIC_MEMORY == FALSE
  39. tAVRC_CB avrc_cb;
  40. #else
  41. tAVRC_CB *avrc_cb_ptr;
  42. #endif
  43. /* update AVRC_NUM_PROTO_ELEMS if this constant is changed */
  44. const tSDP_PROTOCOL_ELEM avrc_proto_list [] = {
  45. {UUID_PROTOCOL_L2CAP, 1, {AVCT_PSM, 0} },
  46. #if SDP_AVCTP_1_4 == TRUE
  47. {UUID_PROTOCOL_AVCTP, 1, {AVCT_REV_1_4, 0} }
  48. #else
  49. #if (SDP_AVRCP_1_4 == TRUE || SDP_AVRCP_1_5 == TRUE)
  50. {UUID_PROTOCOL_AVCTP, 1, {AVCT_REV_1_3, 0} }
  51. #else
  52. #if AVRC_METADATA_INCLUDED == TRUE
  53. {UUID_PROTOCOL_AVCTP, 1, {AVCT_REV_1_2, 0} }
  54. #else
  55. {UUID_PROTOCOL_AVCTP, 1, {AVCT_REV_1_0, 0} }
  56. #endif
  57. #endif
  58. #endif
  59. };
  60. #if SDP_AVRCP_1_5 == TRUE
  61. const tSDP_PROTO_LIST_ELEM avrc_add_proto_list [] = {
  62. {
  63. AVRC_NUM_PROTO_ELEMS,
  64. {
  65. {UUID_PROTOCOL_L2CAP, 1, {AVCT_BR_PSM, 0} },
  66. #if SDP_AVCTP_1_4 == TRUE
  67. {UUID_PROTOCOL_AVCTP, 1, {AVCT_REV_1_4, 0} }
  68. #else
  69. {UUID_PROTOCOL_AVCTP, 1, {AVCT_REV_1_3, 0} }
  70. #endif
  71. }
  72. }
  73. };
  74. #endif
  75. /******************************************************************************
  76. **
  77. ** Function avrc_sdp_cback
  78. **
  79. ** Description This is the SDP callback function used by A2D_FindService.
  80. ** This function will be executed by SDP when the service
  81. ** search is completed. If the search is successful, it
  82. ** finds the first record in the database that matches the
  83. ** UUID of the search. Then retrieves various parameters
  84. ** from the record. When it is finished it calls the
  85. ** application callback function.
  86. **
  87. ** Returns Nothing.
  88. **
  89. ******************************************************************************/
  90. static void avrc_sdp_cback(UINT16 status)
  91. {
  92. AVRC_TRACE_API("avrc_sdp_cback status: %d", status);
  93. /* reset service_uuid, so can start another find service */
  94. avrc_cb.service_uuid = 0;
  95. /* return info from sdp record in app callback function */
  96. (*avrc_cb.p_cback) (status);
  97. return;
  98. }
  99. /******************************************************************************
  100. **
  101. ** Function AVRC_FindService
  102. **
  103. ** Description This function is called by the application to perform service
  104. ** discovery and retrieve AVRCP SDP record information from a
  105. ** peer device. Information is returned for the first service
  106. ** record found on the server that matches the service UUID.
  107. ** The callback function will be executed when service discovery
  108. ** is complete. There can only be one outstanding call to
  109. ** AVRC_FindService() at a time; the application must wait for
  110. ** the callback before it makes another call to the function.
  111. ** The application is responsible for allocating memory for the
  112. ** discovery database. It is recommended that the size of the
  113. ** discovery database be at least 300 bytes. The application
  114. ** can deallocate the memory after the callback function has
  115. ** executed.
  116. **
  117. ** Input Parameters:
  118. ** service_uuid: Indicates TG(UUID_SERVCLASS_AV_REM_CTRL_TARGET)
  119. ** or CT(UUID_SERVCLASS_AV_REMOTE_CONTROL)
  120. **
  121. ** bd_addr: BD address of the peer device.
  122. **
  123. ** p_db: SDP discovery database parameters.
  124. **
  125. ** p_cback: Pointer to the callback function.
  126. **
  127. ** Output Parameters:
  128. ** None.
  129. **
  130. ** Returns AVRC_SUCCESS if successful.
  131. ** AVRC_BAD_PARAMS if discovery database parameters are invalid.
  132. ** AVRC_NO_RESOURCES if there are not enough resources to
  133. ** perform the service search.
  134. **
  135. ******************************************************************************/
  136. UINT16 AVRC_FindService(UINT16 service_uuid, BD_ADDR bd_addr,
  137. tAVRC_SDP_DB_PARAMS *p_db, tAVRC_FIND_CBACK *p_cback)
  138. {
  139. tSDP_UUID uuid_list;
  140. BOOLEAN result = TRUE;
  141. UINT16 a2d_attr_list[] = {ATTR_ID_SERVICE_CLASS_ID_LIST, /* update AVRC_NUM_ATTR, if changed */
  142. ATTR_ID_PROTOCOL_DESC_LIST,
  143. ATTR_ID_BT_PROFILE_DESC_LIST,
  144. ATTR_ID_SERVICE_NAME,
  145. ATTR_ID_SUPPORTED_FEATURES,
  146. ATTR_ID_PROVIDER_NAME
  147. };
  148. AVRC_TRACE_API("AVRC_FindService uuid: %x", service_uuid);
  149. if ( (service_uuid != UUID_SERVCLASS_AV_REM_CTRL_TARGET && service_uuid != UUID_SERVCLASS_AV_REMOTE_CONTROL) ||
  150. p_db == NULL || p_db->p_db == NULL || p_cback == NULL) {
  151. return AVRC_BAD_PARAM;
  152. }
  153. /* check if it is busy */
  154. if ( avrc_cb.service_uuid == UUID_SERVCLASS_AV_REM_CTRL_TARGET ||
  155. avrc_cb.service_uuid == UUID_SERVCLASS_AV_REMOTE_CONTROL) {
  156. return AVRC_NO_RESOURCES;
  157. }
  158. /* set up discovery database */
  159. uuid_list.len = LEN_UUID_16;
  160. uuid_list.uu.uuid16 = service_uuid;
  161. if (p_db->p_attrs == NULL || p_db->num_attr == 0) {
  162. p_db->p_attrs = a2d_attr_list;
  163. p_db->num_attr = AVRC_NUM_ATTR;
  164. }
  165. result = SDP_InitDiscoveryDb(p_db->p_db, p_db->db_len, 1, &uuid_list, p_db->num_attr,
  166. p_db->p_attrs);
  167. if (result == TRUE) {
  168. /* store service_uuid and discovery db pointer */
  169. avrc_cb.p_db = p_db->p_db;
  170. avrc_cb.service_uuid = service_uuid;
  171. avrc_cb.p_cback = p_cback;
  172. /* perform service search */
  173. result = SDP_ServiceSearchAttributeRequest(bd_addr, p_db->p_db, avrc_sdp_cback);
  174. }
  175. return (result ? AVRC_SUCCESS : AVRC_FAIL);
  176. }
  177. /******************************************************************************
  178. **
  179. ** Function AVRC_AddRecord
  180. **
  181. ** Description This function is called to build an AVRCP SDP record.
  182. ** Prior to calling this function the application must
  183. ** call SDP_CreateRecord() to create an SDP record.
  184. **
  185. ** Input Parameters:
  186. ** service_uuid: Indicates TG(UUID_SERVCLASS_AV_REM_CTRL_TARGET)
  187. ** or CT(UUID_SERVCLASS_AV_REMOTE_CONTROL)
  188. **
  189. ** p_service_name: Pointer to a null-terminated character
  190. ** string containing the service name.
  191. ** If service name is not used set this to NULL.
  192. **
  193. ** p_provider_name: Pointer to a null-terminated character
  194. ** string containing the provider name.
  195. ** If provider name is not used set this to NULL.
  196. **
  197. ** categories: Supported categories.
  198. **
  199. ** sdp_handle: SDP handle returned by SDP_CreateRecord().
  200. **
  201. ** browsing_en: Supported browsing
  202. **
  203. ** Output Parameters:
  204. ** None.
  205. **
  206. ** Returns AVRC_SUCCESS if successful.
  207. ** AVRC_NO_RESOURCES if not enough resources to build the SDP record.
  208. **
  209. ******************************************************************************/
  210. UINT16 AVRC_AddRecord(UINT16 service_uuid, char *p_service_name, char *p_provider_name,
  211. UINT16 categories, UINT32 sdp_handle, BOOLEAN browsing_en)
  212. {
  213. UINT16 browse_list[1];
  214. BOOLEAN result = TRUE;
  215. UINT8 temp[8];
  216. UINT8 *p;
  217. UINT16 count = 1;
  218. UINT16 class_list[2];
  219. UINT16 supported_feature = categories;
  220. BOOLEAN media_player_virtual_filesystem_supported = FALSE;
  221. BOOLEAN add_additional_protocol_list = FALSE;
  222. AVRC_TRACE_API("AVRC_AddRecord uuid: %x, browsing_en:%d", service_uuid, browsing_en);
  223. if ( service_uuid != UUID_SERVCLASS_AV_REM_CTRL_TARGET && service_uuid != UUID_SERVCLASS_AV_REMOTE_CONTROL ) {
  224. return AVRC_BAD_PARAM;
  225. }
  226. /* add service class id list */
  227. class_list[0] = service_uuid;
  228. #if (SDP_AVCTP_1_4 == TRUE || SDP_AVRCP_1_5 == TRUE)
  229. if ( service_uuid == UUID_SERVCLASS_AV_REMOTE_CONTROL ) {
  230. class_list[1] = UUID_SERVCLASS_AV_REM_CTRL_CONTROL;
  231. count = 2;
  232. }
  233. #endif
  234. result &= SDP_AddServiceClassIdList(sdp_handle, count, class_list);
  235. /* add protocol descriptor list */
  236. result &= SDP_AddProtocolList(sdp_handle, AVRC_NUM_PROTO_ELEMS, (tSDP_PROTOCOL_ELEM *)avrc_proto_list);
  237. /* add profile descriptor list */
  238. #if SDP_AVRCP_1_5 == TRUE
  239. if (browsing_en) {
  240. add_additional_protocol_list = TRUE;
  241. } else if (service_uuid == UUID_SERVCLASS_AV_REM_CTRL_TARGET &&
  242. (categories & (AVRC_SUPF_TG_CAT1 | AVRC_SUPF_TG_CAT3))) {
  243. AVRC_TRACE_WARNING("For AVRCP Target Cateory 1 and 3, SDP record shall contain additional protocol list");
  244. add_additional_protocol_list = TRUE;
  245. }
  246. if (add_additional_protocol_list) {
  247. /* additional protocol list to include browsing channel */
  248. result &= SDP_AddAdditionProtoLists( sdp_handle, 1, (tSDP_PROTO_LIST_ELEM *)avrc_add_proto_list);
  249. }
  250. result &= SDP_AddProfileDescriptorList(sdp_handle, UUID_SERVCLASS_AV_REMOTE_CONTROL, AVRC_REV_1_5);
  251. #else
  252. #if AVRC_METADATA_INCLUDED == TRUE
  253. result &= SDP_AddProfileDescriptorList(sdp_handle, UUID_SERVCLASS_AV_REMOTE_CONTROL, AVRC_REV_1_3);
  254. #else
  255. result &= SDP_AddProfileDescriptorList(sdp_handle, UUID_SERVCLASS_AV_REMOTE_CONTROL, AVRC_REV_1_0);
  256. #endif
  257. #endif
  258. /* Check if browsing is supported */
  259. if (service_uuid == UUID_SERVCLASS_AV_REM_CTRL_CONTROL && browsing_en) {
  260. supported_feature |= AVRC_SUPF_CT_BROWSE;
  261. } else if (service_uuid == UUID_SERVCLASS_AV_REM_CTRL_TARGET && media_player_virtual_filesystem_supported) {
  262. supported_feature |= AVRC_SUPF_TG_BROWSE;
  263. }
  264. /* add supported feature */
  265. p = temp;
  266. UINT16_TO_BE_STREAM(p, supported_feature);
  267. result &= SDP_AddAttribute(sdp_handle, ATTR_ID_SUPPORTED_FEATURES, UINT_DESC_TYPE,
  268. (UINT32)2, (UINT8 *)temp);
  269. /* add provider name */
  270. if (p_provider_name != NULL) {
  271. result &= SDP_AddAttribute(sdp_handle, ATTR_ID_PROVIDER_NAME, TEXT_STR_DESC_TYPE,
  272. (UINT32)(strlen(p_provider_name) + 1), (UINT8 *) p_provider_name);
  273. }
  274. /* add service name */
  275. if (p_service_name != NULL) {
  276. result &= SDP_AddAttribute(sdp_handle, ATTR_ID_SERVICE_NAME, TEXT_STR_DESC_TYPE,
  277. (UINT32)(strlen(p_service_name) + 1), (UINT8 *) p_service_name);
  278. }
  279. /* add browse group list */
  280. browse_list[0] = UUID_SERVCLASS_PUBLIC_BROWSE_GROUP;
  281. result &= SDP_AddUuidSequence(sdp_handle, ATTR_ID_BROWSE_GROUP_LIST, 1, browse_list);
  282. return (result ? AVRC_SUCCESS : AVRC_FAIL);
  283. }
  284. /******************************************************************************
  285. **
  286. ** Function AVRC_SetTraceLevel
  287. **
  288. ** Description Sets the trace level for AVRC. If 0xff is passed, the
  289. ** current trace level is returned.
  290. **
  291. ** Input Parameters:
  292. ** new_level: The level to set the AVRC tracing to:
  293. ** 0xff-returns the current setting.
  294. ** 0-turns off tracing.
  295. ** >= 1-Errors.
  296. ** >= 2-Warnings.
  297. ** >= 3-APIs.
  298. ** >= 4-Events.
  299. ** >= 5-Debug.
  300. **
  301. ** Returns The new trace level or current trace level if
  302. ** the input parameter is 0xff.
  303. **
  304. ******************************************************************************/
  305. UINT8 AVRC_SetTraceLevel (UINT8 new_level)
  306. {
  307. if (new_level != 0xFF) {
  308. avrc_cb.trace_level = new_level;
  309. }
  310. return (avrc_cb.trace_level);
  311. }
  312. /*******************************************************************************
  313. **
  314. ** Function AVRC_Init
  315. **
  316. ** Description This function is called at stack startup to allocate the
  317. ** control block (if using dynamic memory), and initializes the
  318. ** control block and tracing level.
  319. **
  320. ** Returns status
  321. **
  322. *******************************************************************************/
  323. bt_status_t AVRC_Init(void)
  324. {
  325. #if AVRC_DYNAMIC_MEMORY
  326. avrc_cb_ptr = (tAVRC_CB *)osi_malloc(sizeof(tAVRC_CB));
  327. if (!avrc_cb_ptr) {
  328. return BT_STATUS_NOMEM;
  329. }
  330. #endif /* #if AVRC_DYNAMIC_MEMORY */
  331. memset(&avrc_cb, 0, sizeof(tAVRC_CB));
  332. #if defined(AVRC_INITIAL_TRACE_LEVEL)
  333. avrc_cb.trace_level = AVRC_INITIAL_TRACE_LEVEL;
  334. #else
  335. avrc_cb.trace_level = BT_TRACE_LEVEL_NONE;
  336. #endif
  337. return BT_STATUS_SUCCESS;
  338. }
  339. /*******************************************************************************
  340. **
  341. ** Function AVRC_Deinit
  342. **
  343. ** Description This function is called at stack shotdown to free the
  344. ** control block (if using dynamic memory), and deinitializes the
  345. ** control block and tracing level.
  346. **
  347. ** Returns void
  348. **
  349. *******************************************************************************/
  350. void AVRC_Deinit(void)
  351. {
  352. #if AVRC_DYNAMIC_MEMORY
  353. if (avrc_cb_ptr){
  354. osi_free(avrc_cb_ptr);
  355. avrc_cb_ptr = NULL;
  356. }
  357. #endif /* #if AVRC_DYNAMIC_MEMORY */
  358. }
  359. #endif /* #if (defined(AVRC_INCLUDED) && AVRC_INCLUDED == TRUE) */