avct_api.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444
  1. /******************************************************************************
  2. *
  3. * Copyright (C) 2003-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. * This module contains API of the audio/video control transport protocol.
  21. *
  22. ******************************************************************************/
  23. #include <string.h>
  24. #include "stack/bt_types.h"
  25. #include "common/bt_target.h"
  26. #include "common/bt_defs.h"
  27. #include "stack/l2c_api.h"
  28. #include "stack/l2cdefs.h"
  29. #include "stack/btm_api.h"
  30. #include "stack/avct_api.h"
  31. #include "avct_int.h"
  32. #include "osi/allocator.h"
  33. #if (defined(AVCT_INCLUDED) && AVCT_INCLUDED == TRUE)
  34. /* Control block for AVCT */
  35. #if AVCT_DYNAMIC_MEMORY == FALSE
  36. tAVCT_CB avct_cb;
  37. #else
  38. tAVCT_CB *avct_cb_ptr;
  39. #endif
  40. /*******************************************************************************
  41. **
  42. ** Function AVCT_Register
  43. **
  44. ** Description This is the system level registration function for the
  45. ** AVCTP protocol. This function initializes AVCTP and
  46. ** prepares the protocol stack for its use. This function
  47. ** must be called once by the system or platform using AVCTP
  48. ** before the other functions of the API an be used.
  49. **
  50. **
  51. ** Returns void
  52. **
  53. *******************************************************************************/
  54. void AVCT_Register(UINT16 mtu, UINT16 mtu_br, UINT8 sec_mask)
  55. {
  56. UNUSED(mtu_br);
  57. AVCT_TRACE_API("AVCT_Register");
  58. /* register PSM with L2CAP */
  59. L2CA_Register(AVCT_PSM, (tL2CAP_APPL_INFO *) &avct_l2c_appl);
  60. /* set security level */
  61. BTM_SetSecurityLevel(TRUE, "", BTM_SEC_SERVICE_AVCTP, sec_mask, AVCT_PSM, 0, 0);
  62. BTM_SetSecurityLevel(FALSE, "", BTM_SEC_SERVICE_AVCTP, sec_mask, AVCT_PSM, 0, 0);
  63. /* initialize AVCTP data structures */
  64. memset(&avct_cb, 0, sizeof(tAVCT_CB));
  65. #if (AVCT_BROWSE_INCLUDED == TRUE)
  66. /* Include the browsing channel which uses eFCR */
  67. L2CA_Register(AVCT_BR_PSM, (tL2CAP_APPL_INFO *) &avct_l2c_br_appl);
  68. BTM_SetSecurityLevel(TRUE, "", BTM_SEC_SERVICE_AVCTP_BROWSE, sec_mask, AVCT_BR_PSM, 0, 0);
  69. BTM_SetSecurityLevel(FALSE, "", BTM_SEC_SERVICE_AVCTP_BROWSE, sec_mask, AVCT_BR_PSM, 0, 0);
  70. if (mtu_br < AVCT_MIN_BROWSE_MTU) {
  71. mtu_br = AVCT_MIN_BROWSE_MTU;
  72. }
  73. avct_cb.mtu_br = mtu_br;
  74. #endif
  75. #if defined(AVCT_INITIAL_TRACE_LEVEL)
  76. avct_cb.trace_level = AVCT_INITIAL_TRACE_LEVEL;
  77. #else
  78. avct_cb.trace_level = BT_TRACE_LEVEL_NONE;
  79. #endif
  80. if (mtu < AVCT_MIN_CONTROL_MTU) {
  81. mtu = AVCT_MIN_CONTROL_MTU;
  82. }
  83. /* store mtu */
  84. avct_cb.mtu = mtu;
  85. }
  86. /*******************************************************************************
  87. **
  88. ** Function AVCT_Deregister
  89. **
  90. ** Description This function is called to deregister use AVCTP protocol.
  91. ** It is called when AVCTP is no longer being used by any
  92. ** application in the system. Before this function can be
  93. ** called, all connections must be removed with
  94. ** AVCT_RemoveConn().
  95. **
  96. **
  97. ** Returns void
  98. **
  99. *******************************************************************************/
  100. void AVCT_Deregister(void)
  101. {
  102. AVCT_TRACE_API("AVCT_Deregister");
  103. /* deregister PSM with L2CAP */
  104. L2CA_Deregister(AVCT_PSM);
  105. }
  106. /*******************************************************************************
  107. **
  108. ** Function AVCT_CreateConn
  109. **
  110. ** Description Create an AVCTP connection. There are two types of
  111. ** connections, initiator and acceptor, as determined by
  112. ** the p_cc->role parameter. When this function is called to
  113. ** create an initiator connection, an AVCTP connection to
  114. ** the peer device is initiated if one does not already exist.
  115. ** If an acceptor connection is created, the connection waits
  116. ** passively for an incoming AVCTP connection from a peer device.
  117. **
  118. **
  119. ** Returns AVCT_SUCCESS if successful, otherwise error.
  120. **
  121. *******************************************************************************/
  122. UINT16 AVCT_CreateConn(UINT8 *p_handle, tAVCT_CC *p_cc, BD_ADDR peer_addr)
  123. {
  124. UINT16 result = AVCT_SUCCESS;
  125. tAVCT_CCB *p_ccb;
  126. tAVCT_LCB *p_lcb;
  127. AVCT_TRACE_API("AVCT_CreateConn: %d, control:%d", p_cc->role, p_cc->control);
  128. /* Allocate ccb; if no ccbs, return failure */
  129. if ((p_ccb = avct_ccb_alloc(p_cc)) == NULL) {
  130. result = AVCT_NO_RESOURCES;
  131. } else {
  132. /* get handle */
  133. *p_handle = avct_ccb_to_idx(p_ccb);
  134. /* if initiator connection */
  135. if (p_cc->role == AVCT_INT) {
  136. /* find link; if none allocate a new one */
  137. if ((p_lcb = avct_lcb_by_bd(peer_addr)) == NULL) {
  138. if ((p_lcb = avct_lcb_alloc(peer_addr)) == NULL) {
  139. /* no link resources; free ccb as well */
  140. avct_ccb_dealloc(p_ccb, AVCT_NO_EVT, 0, NULL);
  141. result = AVCT_NO_RESOURCES;
  142. }
  143. }
  144. /* check if PID already in use */
  145. else if (avct_lcb_has_pid(p_lcb, p_cc->pid)) {
  146. avct_ccb_dealloc(p_ccb, AVCT_NO_EVT, 0, NULL);
  147. result = AVCT_PID_IN_USE;
  148. }
  149. if (result == AVCT_SUCCESS) {
  150. /* bind lcb to ccb */
  151. p_ccb->p_lcb = p_lcb;
  152. AVCT_TRACE_DEBUG("ch_state: %d", p_lcb->ch_state);
  153. avct_lcb_event(p_lcb, AVCT_LCB_UL_BIND_EVT, (tAVCT_LCB_EVT *) &p_ccb);
  154. }
  155. }
  156. }
  157. return result;
  158. }
  159. /*******************************************************************************
  160. **
  161. ** Function AVCT_RemoveConn
  162. **
  163. ** Description Remove an AVCTP connection. This function is called when
  164. ** the application is no longer using a connection. If this
  165. ** is the last connection to a peer the L2CAP channel for AVCTP
  166. ** will be closed.
  167. **
  168. **
  169. ** Returns AVCT_SUCCESS if successful, otherwise error.
  170. **
  171. *******************************************************************************/
  172. UINT16 AVCT_RemoveConn(UINT8 handle)
  173. {
  174. UINT16 result = AVCT_SUCCESS;
  175. tAVCT_CCB *p_ccb;
  176. AVCT_TRACE_API("AVCT_RemoveConn");
  177. /* map handle to ccb */
  178. if ((p_ccb = avct_ccb_by_idx(handle)) == NULL) {
  179. result = AVCT_BAD_HANDLE;
  180. }
  181. /* if connection not bound to lcb, dealloc */
  182. else if (p_ccb->p_lcb == NULL) {
  183. avct_ccb_dealloc(p_ccb, AVCT_NO_EVT, 0, NULL);
  184. }
  185. /* send unbind event to lcb */
  186. else {
  187. avct_lcb_event(p_ccb->p_lcb, AVCT_LCB_UL_UNBIND_EVT, (tAVCT_LCB_EVT *) &p_ccb);
  188. }
  189. return result;
  190. }
  191. /*******************************************************************************
  192. **
  193. ** Function AVCT_CreateBrowse
  194. **
  195. ** Description Create an AVCTP Browse channel. There are two types of
  196. ** connections, initiator and acceptor, as determined by
  197. ** the role parameter. When this function is called to
  198. ** create an initiator connection, the Browse channel to
  199. ** the peer device is initiated if one does not already exist.
  200. ** If an acceptor connection is created, the connection waits
  201. ** passively for an incoming AVCTP connection from a peer device.
  202. **
  203. **
  204. ** Returns AVCT_SUCCESS if successful, otherwise error.
  205. **
  206. *******************************************************************************/
  207. UINT16 AVCT_CreateBrowse (UINT8 handle, UINT8 role)
  208. {
  209. #if (AVCT_BROWSE_INCLUDED == TRUE)
  210. UINT16 result = AVCT_SUCCESS;
  211. tAVCT_CCB *p_ccb;
  212. tAVCT_BCB *p_bcb;
  213. int index;
  214. AVCT_TRACE_API("AVCT_CreateBrowse: %d", role);
  215. /* map handle to ccb */
  216. if ((p_ccb = avct_ccb_by_idx(handle)) == NULL) {
  217. return AVCT_BAD_HANDLE;
  218. } else {
  219. /* mark this CCB as supporting browsing channel */
  220. if ((p_ccb->allocated & AVCT_ALOC_BCB) == 0) {
  221. p_ccb->allocated |= AVCT_ALOC_BCB;
  222. }
  223. }
  224. /* if initiator connection */
  225. if (role == AVCT_INT) {
  226. /* the link control block must exist before this function is called as INT. */
  227. if ((p_ccb->p_lcb == NULL) || (p_ccb->p_lcb->allocated == 0)) {
  228. result = AVCT_NOT_OPEN;
  229. } else {
  230. /* find link; if none allocate a new one */
  231. index = p_ccb->p_lcb->allocated;
  232. if (index > AVCT_NUM_LINKS) {
  233. result = AVCT_BAD_HANDLE;
  234. } else {
  235. p_bcb = &avct_cb.bcb[index - 1];
  236. p_bcb->allocated = index;
  237. }
  238. }
  239. if (result == AVCT_SUCCESS) {
  240. /* bind bcb to ccb */
  241. p_ccb->p_bcb = p_bcb;
  242. AVCT_TRACE_DEBUG("ch_state: %d", p_bcb->ch_state);
  243. avct_bcb_event(p_bcb, AVCT_LCB_UL_BIND_EVT, (tAVCT_LCB_EVT *) &p_ccb);
  244. }
  245. }
  246. return result;
  247. #else
  248. UNUSED(handle);
  249. UNUSED(role);
  250. return AVCT_NO_RESOURCES;
  251. #endif
  252. }
  253. /*******************************************************************************
  254. **
  255. ** Function AVCT_RemoveBrowse
  256. **
  257. ** Description Remove an AVCTP Browse channel. This function is called when
  258. ** the application is no longer using a connection. If this
  259. ** is the last connection to a peer the L2CAP channel for AVCTP
  260. ** will be closed.
  261. **
  262. **
  263. ** Returns AVCT_SUCCESS if successful, otherwise error.
  264. **
  265. *******************************************************************************/
  266. UINT16 AVCT_RemoveBrowse (UINT8 handle)
  267. {
  268. #if (AVCT_BROWSE_INCLUDED == TRUE)
  269. UINT16 result = AVCT_SUCCESS;
  270. tAVCT_CCB *p_ccb;
  271. AVCT_TRACE_API("AVCT_RemoveBrowse");
  272. /* map handle to ccb */
  273. if ((p_ccb = avct_ccb_by_idx(handle)) == NULL) {
  274. result = AVCT_BAD_HANDLE;
  275. } else if (p_ccb->p_bcb != NULL)
  276. /* send unbind event to bcb */
  277. {
  278. avct_bcb_event(p_ccb->p_bcb, AVCT_LCB_UL_UNBIND_EVT, (tAVCT_LCB_EVT *) &p_ccb);
  279. }
  280. return result;
  281. #else
  282. UNUSED(handle);
  283. return AVCT_NO_RESOURCES;
  284. #endif
  285. }
  286. /*******************************************************************************
  287. **
  288. ** Function AVCT_GetBrowseMtu
  289. **
  290. ** Description Get the peer_mtu for the AVCTP Browse channel of the given
  291. ** connection.
  292. **
  293. ** Returns the peer browsing channel MTU.
  294. **
  295. *******************************************************************************/
  296. UINT16 AVCT_GetBrowseMtu (UINT8 handle)
  297. {
  298. UINT16 peer_mtu = AVCT_MIN_BROWSE_MTU;
  299. #if (AVCT_BROWSE_INCLUDED == TRUE)
  300. tAVCT_CCB *p_ccb;
  301. if ((p_ccb = avct_ccb_by_idx(handle)) != NULL && p_ccb->p_bcb != NULL) {
  302. peer_mtu = p_ccb->p_bcb->peer_mtu;
  303. }
  304. #else
  305. UNUSED(handle);
  306. #endif
  307. return peer_mtu;
  308. }
  309. /*******************************************************************************
  310. **
  311. ** Function AVCT_GetPeerMtu
  312. **
  313. ** Description Get the peer_mtu for the AVCTP channel of the given
  314. ** connection.
  315. **
  316. ** Returns the peer MTU size.
  317. **
  318. *******************************************************************************/
  319. UINT16 AVCT_GetPeerMtu (UINT8 handle)
  320. {
  321. UINT16 peer_mtu = L2CAP_DEFAULT_MTU;
  322. tAVCT_CCB *p_ccb;
  323. /* map handle to ccb */
  324. if ((p_ccb = avct_ccb_by_idx(handle)) != NULL) {
  325. if (p_ccb->p_lcb) {
  326. peer_mtu = p_ccb->p_lcb->peer_mtu;
  327. }
  328. }
  329. return peer_mtu;
  330. }
  331. /*******************************************************************************
  332. **
  333. ** Function AVCT_MsgReq
  334. **
  335. ** Description Send an AVCTP message to a peer device. In calling
  336. ** AVCT_MsgReq(), the application should keep track of the
  337. ** congestion state of AVCTP as communicated with events
  338. ** AVCT_CONG_IND_EVT and AVCT_UNCONG_IND_EVT. If the
  339. ** application calls AVCT_MsgReq() when AVCTP is congested
  340. ** the message may be discarded. The application may make its
  341. ** first call to AVCT_MsgReq() after it receives an
  342. ** AVCT_CONNECT_CFM_EVT or AVCT_CONNECT_IND_EVT on control channel or
  343. ** AVCT_BROWSE_CONN_CFM_EVT or AVCT_BROWSE_CONN_IND_EVT on browsing channel.
  344. **
  345. ** p_msg->layer_specific must be set to
  346. ** AVCT_DATA_CTRL for control channel traffic;
  347. ** AVCT_DATA_BROWSE for for browse channel traffic.
  348. **
  349. ** Returns AVCT_SUCCESS if successful, otherwise error.
  350. **
  351. *******************************************************************************/
  352. UINT16 AVCT_MsgReq(UINT8 handle, UINT8 label, UINT8 cr, BT_HDR *p_msg)
  353. {
  354. UINT16 result = AVCT_SUCCESS;
  355. tAVCT_CCB *p_ccb;
  356. tAVCT_UL_MSG ul_msg;
  357. AVCT_TRACE_API("AVCT_MsgReq");
  358. /* verify p_msg parameter */
  359. if (p_msg == NULL) {
  360. return AVCT_NO_RESOURCES;
  361. }
  362. AVCT_TRACE_API("len: %d", p_msg->len);
  363. /* map handle to ccb */
  364. if ((p_ccb = avct_ccb_by_idx(handle)) == NULL) {
  365. result = AVCT_BAD_HANDLE;
  366. osi_free(p_msg);
  367. }
  368. /* verify channel is bound to link */
  369. else if (p_ccb->p_lcb == NULL) {
  370. result = AVCT_NOT_OPEN;
  371. osi_free(p_msg);
  372. }
  373. if (result == AVCT_SUCCESS) {
  374. ul_msg.p_buf = p_msg;
  375. ul_msg.p_ccb = p_ccb;
  376. ul_msg.label = label;
  377. ul_msg.cr = cr;
  378. #if (AVCT_BROWSE_INCLUDED == TRUE)
  379. /* send msg event to bcb */
  380. if (p_msg->layer_specific == AVCT_DATA_BROWSE) {
  381. if (p_ccb->p_bcb == NULL && (p_ccb->allocated & AVCT_ALOC_BCB) == 0) {
  382. /* BCB channel is not open and not allocated */
  383. result = AVCT_BAD_HANDLE;
  384. osi_free(p_msg);
  385. } else {
  386. p_ccb->p_bcb = avct_bcb_by_lcb(p_ccb->p_lcb);
  387. avct_bcb_event(p_ccb->p_bcb, AVCT_LCB_UL_MSG_EVT, (tAVCT_LCB_EVT *) &ul_msg);
  388. }
  389. }
  390. /* send msg event to lcb */
  391. else
  392. #endif
  393. {
  394. avct_lcb_event(p_ccb->p_lcb, AVCT_LCB_UL_MSG_EVT, (tAVCT_LCB_EVT *) &ul_msg);
  395. }
  396. }
  397. return result;
  398. }
  399. #endif /* #if (defined(AVCT_INCLUDED) && AVCT_INCLUDED == TRUE) */