avrc_sdp.c 14 KB

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