avdt_api.c 49 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345
  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. * This module contains API of the audio/video distribution transport
  21. * protocol.
  22. *
  23. ******************************************************************************/
  24. #include <string.h>
  25. #include "bt_types.h"
  26. #include "bt_target.h"
  27. #include "avdt_api.h"
  28. #include "avdtc_api.h"
  29. #include "avdt_int.h"
  30. #include "l2c_api.h"
  31. #include "btm_api.h"
  32. #include "btu.h"
  33. #if (defined(AVDT_INCLUDED) && AVDT_INCLUDED == TRUE)
  34. /* Control block for AVDT */
  35. #if AVDT_DYNAMIC_MEMORY == FALSE
  36. tAVDT_CB avdt_cb;
  37. #endif
  38. /*******************************************************************************
  39. **
  40. ** Function avdt_process_timeout
  41. **
  42. ** Description This function is called by BTU when an AVDTP timer
  43. ** expires. The function sends a timer event to the
  44. ** appropriate CCB or SCB state machine.
  45. **
  46. ** This function is for use internal to the stack only.
  47. **
  48. **
  49. ** Returns void
  50. **
  51. *******************************************************************************/
  52. void avdt_process_timeout(TIMER_LIST_ENT *p_tle)
  53. {
  54. UINT8 event = 0;
  55. UINT8 err_code = AVDT_ERR_TIMEOUT;
  56. switch (p_tle->event) {
  57. case BTU_TTYPE_AVDT_CCB_RET:
  58. event = AVDT_CCB_RET_TOUT_EVT + AVDT_CCB_MKR;
  59. break;
  60. case BTU_TTYPE_AVDT_CCB_RSP:
  61. event = AVDT_CCB_RSP_TOUT_EVT + AVDT_CCB_MKR;
  62. break;
  63. case BTU_TTYPE_AVDT_CCB_IDLE:
  64. event = AVDT_CCB_IDLE_TOUT_EVT + AVDT_CCB_MKR;
  65. break;
  66. case BTU_TTYPE_AVDT_SCB_TC:
  67. event = AVDT_SCB_TC_TOUT_EVT;
  68. break;
  69. default:
  70. break;
  71. }
  72. if (event & AVDT_CCB_MKR) {
  73. avdt_ccb_event((tAVDT_CCB *) p_tle->param, (UINT8) (event & ~AVDT_CCB_MKR),
  74. (tAVDT_CCB_EVT *) &err_code);
  75. } else {
  76. avdt_scb_event((tAVDT_SCB *) p_tle->param, event, NULL);
  77. }
  78. }
  79. /*******************************************************************************
  80. **
  81. ** Function AVDT_Register
  82. **
  83. ** Description This is the system level registration function for the
  84. ** AVDTP protocol. This function initializes AVDTP and
  85. ** prepares the protocol stack for its use. This function
  86. ** must be called once by the system or platform using AVDTP
  87. ** before the other functions of the API an be used.
  88. **
  89. **
  90. ** Returns void
  91. **
  92. *******************************************************************************/
  93. void AVDT_Register(tAVDT_REG *p_reg, tAVDT_CTRL_CBACK *p_cback)
  94. {
  95. /* register PSM with L2CAP */
  96. L2CA_Register(AVDT_PSM, (tL2CAP_APPL_INFO *) &avdt_l2c_appl);
  97. /* set security level */
  98. BTM_SetSecurityLevel(TRUE, "", BTM_SEC_SERVICE_AVDTP, p_reg->sec_mask,
  99. AVDT_PSM, BTM_SEC_PROTO_AVDT, AVDT_CHAN_SIG);
  100. BTM_SetSecurityLevel(FALSE, "", BTM_SEC_SERVICE_AVDTP, p_reg->sec_mask,
  101. AVDT_PSM, BTM_SEC_PROTO_AVDT, AVDT_CHAN_SIG);
  102. /* do not use security on the media channel */
  103. BTM_SetSecurityLevel(TRUE, "", BTM_SEC_SERVICE_AVDTP_NOSEC, BTM_SEC_NONE,
  104. AVDT_PSM, BTM_SEC_PROTO_AVDT, AVDT_CHAN_MEDIA);
  105. BTM_SetSecurityLevel(FALSE, "", BTM_SEC_SERVICE_AVDTP_NOSEC, BTM_SEC_NONE,
  106. AVDT_PSM, BTM_SEC_PROTO_AVDT, AVDT_CHAN_MEDIA);
  107. #if AVDT_REPORTING == TRUE
  108. /* do not use security on the reporting channel */
  109. BTM_SetSecurityLevel(TRUE, "", BTM_SEC_SERVICE_AVDTP_NOSEC, BTM_SEC_NONE,
  110. AVDT_PSM, BTM_SEC_PROTO_AVDT, AVDT_CHAN_REPORT);
  111. BTM_SetSecurityLevel(FALSE, "", BTM_SEC_SERVICE_AVDTP_NOSEC, BTM_SEC_NONE,
  112. AVDT_PSM, BTM_SEC_PROTO_AVDT, AVDT_CHAN_REPORT);
  113. #endif
  114. /* initialize AVDTP data structures */
  115. avdt_scb_init();
  116. avdt_ccb_init();
  117. avdt_ad_init();
  118. /* copy registration struct */
  119. memcpy(&avdt_cb.rcb, p_reg, sizeof(tAVDT_REG));
  120. avdt_cb.p_conn_cback = p_cback;
  121. }
  122. /*******************************************************************************
  123. **
  124. ** Function AVDT_Deregister
  125. **
  126. ** Description This function is called to deregister use AVDTP protocol.
  127. ** It is called when AVDTP is no longer being used by any
  128. ** application in the system. Before this function can be
  129. ** called, all streams must be removed with AVDT_RemoveStream().
  130. **
  131. **
  132. ** Returns void
  133. **
  134. *******************************************************************************/
  135. void AVDT_Deregister(void)
  136. {
  137. /* deregister PSM with L2CAP */
  138. L2CA_Deregister(AVDT_PSM);
  139. }
  140. /*******************************************************************************
  141. **
  142. ** Function AVDT_SINK_Activate
  143. **
  144. ** Description Activate SEP of A2DP Sink. In Use parameter is adjusted.
  145. ** In Use will be made false in case of activation. A2DP SRC
  146. ** will receive in_use as false and can open A2DP Sink
  147. ** connection
  148. **
  149. ** Returns void.
  150. **
  151. *******************************************************************************/
  152. void AVDT_SINK_Activate()
  153. {
  154. tAVDT_SCB *p_scb = &avdt_cb.scb[0];
  155. int i;
  156. AVDT_TRACE_DEBUG("AVDT_SINK_Activate");
  157. /* for all allocated scbs */
  158. for (i = 0; i < AVDT_NUM_SEPS; i++, p_scb++) {
  159. if ((p_scb->allocated) && (p_scb->cs.tsep == AVDT_TSEP_SNK)) {
  160. AVDT_TRACE_DEBUG("AVDT_SINK_Activate found scb");
  161. p_scb->sink_activated = TRUE;
  162. /* update in_use */
  163. p_scb->in_use = FALSE;
  164. break;
  165. }
  166. }
  167. }
  168. /*******************************************************************************
  169. **
  170. ** Function AVDT_SINK_Deactivate
  171. **
  172. ** Description Deactivate SEP of A2DP Sink. In Use parameter is adjusted.
  173. ** In Use will be made TRUE in case of activation. A2DP SRC
  174. ** will receive in_use as true and will not open A2DP Sink
  175. ** connection
  176. **
  177. ** Returns void.
  178. **
  179. *******************************************************************************/
  180. void AVDT_SINK_Deactivate()
  181. {
  182. tAVDT_SCB *p_scb = &avdt_cb.scb[0];
  183. int i;
  184. AVDT_TRACE_DEBUG("AVDT_SINK_Deactivate");
  185. /* for all allocated scbs */
  186. for (i = 0; i < AVDT_NUM_SEPS; i++, p_scb++) {
  187. if ((p_scb->allocated) && (p_scb->cs.tsep == AVDT_TSEP_SNK)) {
  188. AVDT_TRACE_DEBUG("AVDT_SINK_Deactivate, found scb");
  189. p_scb->sink_activated = FALSE;
  190. /* update in_use */
  191. p_scb->in_use = TRUE;
  192. break;
  193. }
  194. }
  195. }
  196. void AVDT_AbortReq(UINT8 handle)
  197. {
  198. AVDT_TRACE_ERROR("%s\n", __func__);
  199. tAVDT_SCB *p_scb = avdt_scb_by_hdl(handle);
  200. if (p_scb != NULL) {
  201. avdt_scb_event(p_scb, AVDT_SCB_API_ABORT_REQ_EVT, NULL);
  202. } else {
  203. AVDT_TRACE_ERROR("%s Improper SCB, can not abort the stream\n", __func__);
  204. }
  205. }
  206. /*******************************************************************************
  207. **
  208. ** Function AVDT_CreateStream
  209. **
  210. ** Description Create a stream endpoint. After a stream endpoint is
  211. ** created an application can initiate a connection between
  212. ** this endpoint and an endpoint on a peer device. In
  213. ** addition, a peer device can discover, get the capabilities,
  214. ** and connect to this endpoint.
  215. **
  216. **
  217. ** Returns AVDT_SUCCESS if successful, otherwise error.
  218. **
  219. *******************************************************************************/
  220. UINT16 AVDT_CreateStream(UINT8 *p_handle, tAVDT_CS *p_cs)
  221. {
  222. UINT16 result = AVDT_SUCCESS;
  223. tAVDT_SCB *p_scb;
  224. /* Verify parameters; if invalid, return failure */
  225. if (((p_cs->cfg.psc_mask & (~AVDT_PSC)) != 0) || (p_cs->p_ctrl_cback == NULL)) {
  226. result = AVDT_BAD_PARAMS;
  227. }
  228. /* Allocate scb; if no scbs, return failure */
  229. else if ((p_scb = avdt_scb_alloc(p_cs)) == NULL) {
  230. result = AVDT_NO_RESOURCES;
  231. } else {
  232. *p_handle = avdt_scb_to_hdl(p_scb);
  233. }
  234. return result;
  235. }
  236. /*******************************************************************************
  237. **
  238. ** Function AVDT_RemoveStream
  239. **
  240. ** Description Remove a stream endpoint. This function is called when
  241. ** the application is no longer using a stream endpoint.
  242. ** If this function is called when the endpoint is connected
  243. ** the connection is closed and then the stream endpoint
  244. ** is removed.
  245. **
  246. **
  247. ** Returns AVDT_SUCCESS if successful, otherwise error.
  248. **
  249. *******************************************************************************/
  250. UINT16 AVDT_RemoveStream(UINT8 handle)
  251. {
  252. UINT16 result = AVDT_SUCCESS;
  253. tAVDT_SCB *p_scb;
  254. /* look up scb */
  255. if ((p_scb = avdt_scb_by_hdl(handle)) == NULL) {
  256. result = AVDT_BAD_HANDLE;
  257. } else {
  258. /* send remove event to scb */
  259. avdt_scb_event(p_scb, AVDT_SCB_API_REMOVE_EVT, NULL);
  260. }
  261. return result;
  262. }
  263. /*******************************************************************************
  264. **
  265. ** Function AVDT_DiscoverReq
  266. **
  267. ** Description This function initiates a connection to the AVDTP service
  268. ** on the peer device, if not already present, and discovers
  269. ** the stream endpoints on the peer device. (Please note
  270. ** that AVDTP discovery is unrelated to SDP discovery).
  271. ** This function can be called at any time regardless of whether
  272. ** there is an AVDTP connection to the peer device.
  273. **
  274. ** When discovery is complete, an AVDT_DISCOVER_CFM_EVT
  275. ** is sent to the application via its callback function.
  276. ** The application must not call AVDT_GetCapReq() or
  277. ** AVDT_DiscoverReq() again to the same device until
  278. ** discovery is complete.
  279. **
  280. ** The memory addressed by sep_info is allocated by the
  281. ** application. This memory is written to by AVDTP as part
  282. ** of the discovery procedure. This memory must remain
  283. ** accessible until the application receives the
  284. ** AVDT_DISCOVER_CFM_EVT.
  285. **
  286. ** Returns AVDT_SUCCESS if successful, otherwise error.
  287. **
  288. *******************************************************************************/
  289. UINT16 AVDT_DiscoverReq(BD_ADDR bd_addr, tAVDT_SEP_INFO *p_sep_info,
  290. UINT8 max_seps, tAVDT_CTRL_CBACK *p_cback)
  291. {
  292. tAVDT_CCB *p_ccb;
  293. UINT16 result = AVDT_SUCCESS;
  294. tAVDT_CCB_EVT evt;
  295. /* find channel control block for this bd addr; if none, allocate one */
  296. if ((p_ccb = avdt_ccb_by_bd(bd_addr)) == NULL) {
  297. if ((p_ccb = avdt_ccb_alloc(bd_addr)) == NULL) {
  298. /* could not allocate channel control block */
  299. result = AVDT_NO_RESOURCES;
  300. }
  301. }
  302. if (result == AVDT_SUCCESS) {
  303. /* make sure no discovery or get capabilities req already in progress */
  304. if (p_ccb->proc_busy) {
  305. result = AVDT_BUSY;
  306. }
  307. /* send event to ccb */
  308. else {
  309. evt.discover.p_sep_info = p_sep_info;
  310. evt.discover.num_seps = max_seps;
  311. evt.discover.p_cback = p_cback;
  312. avdt_ccb_event(p_ccb, AVDT_CCB_API_DISCOVER_REQ_EVT, &evt);
  313. }
  314. }
  315. return result;
  316. }
  317. /*******************************************************************************
  318. **
  319. ** Function avdt_get_cap_req
  320. **
  321. ** Description internal function to serve both AVDT_GetCapReq and
  322. ** AVDT_GetAllCapReq
  323. **
  324. ** Returns AVDT_SUCCESS if successful, otherwise error.
  325. **
  326. *******************************************************************************/
  327. static UINT16 avdt_get_cap_req(BD_ADDR bd_addr, tAVDT_CCB_API_GETCAP *p_evt)
  328. {
  329. tAVDT_CCB *p_ccb = NULL;
  330. UINT16 result = AVDT_SUCCESS;
  331. /* verify SEID */
  332. if ((p_evt->single.seid < AVDT_SEID_MIN) || (p_evt->single.seid > AVDT_SEID_MAX)) {
  333. AVDT_TRACE_ERROR("seid: %d\n", p_evt->single.seid);
  334. result = AVDT_BAD_PARAMS;
  335. }
  336. /* find channel control block for this bd addr; if none, allocate one */
  337. else if ((p_ccb = avdt_ccb_by_bd(bd_addr)) == NULL) {
  338. if ((p_ccb = avdt_ccb_alloc(bd_addr)) == NULL) {
  339. /* could not allocate channel control block */
  340. result = AVDT_NO_RESOURCES;
  341. }
  342. }
  343. if (result == AVDT_SUCCESS) {
  344. /* make sure no discovery or get capabilities req already in progress */
  345. if (p_ccb->proc_busy) {
  346. result = AVDT_BUSY;
  347. }
  348. /* send event to ccb */
  349. else {
  350. avdt_ccb_event(p_ccb, AVDT_CCB_API_GETCAP_REQ_EVT, (tAVDT_CCB_EVT *)p_evt);
  351. }
  352. }
  353. return result;
  354. }
  355. /*******************************************************************************
  356. **
  357. ** Function AVDT_GetCapReq
  358. **
  359. ** Description This function initiates a connection to the AVDTP service
  360. ** on the peer device, if not already present, and gets the
  361. ** capabilities of a stream endpoint on the peer device.
  362. ** This function can be called at any time regardless of
  363. ** whether there is an AVDTP connection to the peer device.
  364. **
  365. ** When the procedure is complete, an AVDT_GETCAP_CFM_EVT is
  366. ** sent to the application via its callback function. The
  367. ** application must not call AVDT_GetCapReq() or
  368. ** AVDT_DiscoverReq() again until the procedure is complete.
  369. **
  370. ** The memory pointed to by p_cfg is allocated by the
  371. ** application. This memory is written to by AVDTP as part
  372. ** of the get capabilities procedure. This memory must
  373. ** remain accessible until the application receives
  374. ** the AVDT_GETCAP_CFM_EVT.
  375. **
  376. ** Returns AVDT_SUCCESS if successful, otherwise error.
  377. **
  378. *******************************************************************************/
  379. UINT16 AVDT_GetCapReq(BD_ADDR bd_addr, UINT8 seid, tAVDT_CFG *p_cfg, tAVDT_CTRL_CBACK *p_cback)
  380. {
  381. tAVDT_CCB_API_GETCAP getcap;
  382. getcap.single.seid = seid;
  383. getcap.single.sig_id = AVDT_SIG_GETCAP;
  384. getcap.p_cfg = p_cfg;
  385. getcap.p_cback = p_cback;
  386. return avdt_get_cap_req (bd_addr, &getcap);
  387. }
  388. /*******************************************************************************
  389. **
  390. ** Function AVDT_GetAllCapReq
  391. **
  392. ** Description This function initiates a connection to the AVDTP service
  393. ** on the peer device, if not already present, and gets the
  394. ** capabilities of a stream endpoint on the peer device.
  395. ** This function can be called at any time regardless of
  396. ** whether there is an AVDTP connection to the peer device.
  397. **
  398. ** When the procedure is complete, an AVDT_GETCAP_CFM_EVT is
  399. ** sent to the application via its callback function. The
  400. ** application must not call AVDT_GetCapReq() or
  401. ** AVDT_DiscoverReq() again until the procedure is complete.
  402. **
  403. ** The memory pointed to by p_cfg is allocated by the
  404. ** application. This memory is written to by AVDTP as part
  405. ** of the get capabilities procedure. This memory must
  406. ** remain accessible until the application receives
  407. ** the AVDT_GETCAP_CFM_EVT.
  408. **
  409. ** Returns AVDT_SUCCESS if successful, otherwise error.
  410. **
  411. *******************************************************************************/
  412. UINT16 AVDT_GetAllCapReq(BD_ADDR bd_addr, UINT8 seid, tAVDT_CFG *p_cfg, tAVDT_CTRL_CBACK *p_cback)
  413. {
  414. tAVDT_CCB_API_GETCAP getcap;
  415. getcap.single.seid = seid;
  416. getcap.single.sig_id = AVDT_SIG_GET_ALLCAP;
  417. getcap.p_cfg = p_cfg;
  418. getcap.p_cback = p_cback;
  419. return avdt_get_cap_req (bd_addr, &getcap);
  420. }
  421. /*******************************************************************************
  422. **
  423. ** Function AVDT_DelayReport
  424. **
  425. ** Description This functions sends a Delay Report to the peer device
  426. ** that is associated with a particular SEID.
  427. ** This function is called by SNK device.
  428. **
  429. ** Returns AVDT_SUCCESS if successful, otherwise error.
  430. **
  431. *******************************************************************************/
  432. UINT16 AVDT_DelayReport(UINT8 handle, UINT8 seid, UINT16 delay)
  433. {
  434. tAVDT_SCB *p_scb;
  435. UINT16 result = AVDT_SUCCESS;
  436. tAVDT_SCB_EVT evt;
  437. /* map handle to scb */
  438. if ((p_scb = avdt_scb_by_hdl(handle)) == NULL) {
  439. result = AVDT_BAD_HANDLE;
  440. } else
  441. /* send event to scb */
  442. {
  443. evt.apidelay.hdr.seid = seid;
  444. evt.apidelay.delay = delay;
  445. avdt_scb_event(p_scb, AVDT_SCB_API_DELAY_RPT_REQ_EVT, &evt);
  446. }
  447. return result;
  448. }
  449. /*******************************************************************************
  450. **
  451. ** Function AVDT_OpenReq
  452. **
  453. ** Description This function initiates a connection to the AVDTP service
  454. ** on the peer device, if not already present, and connects
  455. ** to a stream endpoint on a peer device. When the connection
  456. ** is completed, an AVDT_OPEN_CFM_EVT is sent to the
  457. ** application via the control callback function for this handle.
  458. **
  459. ** Returns AVDT_SUCCESS if successful, otherwise error.
  460. **
  461. *******************************************************************************/
  462. UINT16 AVDT_OpenReq(UINT8 handle, BD_ADDR bd_addr, UINT8 seid, tAVDT_CFG *p_cfg)
  463. {
  464. tAVDT_CCB *p_ccb = NULL;
  465. tAVDT_SCB *p_scb = NULL;
  466. UINT16 result = AVDT_SUCCESS;
  467. tAVDT_SCB_EVT evt;
  468. /* verify SEID */
  469. if ((seid < AVDT_SEID_MIN) || (seid > AVDT_SEID_MAX)) {
  470. result = AVDT_BAD_PARAMS;
  471. }
  472. /* map handle to scb */
  473. else if ((p_scb = avdt_scb_by_hdl(handle)) == NULL) {
  474. result = AVDT_BAD_HANDLE;
  475. }
  476. /* find channel control block for this bd addr; if none, allocate one */
  477. else if ((p_ccb = avdt_ccb_by_bd(bd_addr)) == NULL) {
  478. if ((p_ccb = avdt_ccb_alloc(bd_addr)) == NULL) {
  479. /* could not allocate channel control block */
  480. result = AVDT_NO_RESOURCES;
  481. }
  482. }
  483. /* send event to scb */
  484. if (result == AVDT_SUCCESS) {
  485. evt.msg.config_cmd.hdr.seid = seid;
  486. evt.msg.config_cmd.hdr.ccb_idx = avdt_ccb_to_idx(p_ccb);
  487. evt.msg.config_cmd.int_seid = handle;
  488. evt.msg.config_cmd.p_cfg = p_cfg;
  489. avdt_scb_event(p_scb, AVDT_SCB_API_SETCONFIG_REQ_EVT, &evt);
  490. }
  491. return result;
  492. }
  493. /*******************************************************************************
  494. **
  495. ** Function AVDT_ConfigRsp
  496. **
  497. ** Description Respond to a configure request from the peer device. This
  498. ** function must be called if the application receives an
  499. ** AVDT_CONFIG_IND_EVT through its control callback.
  500. **
  501. **
  502. ** Returns AVDT_SUCCESS if successful, otherwise error.
  503. **
  504. *******************************************************************************/
  505. UINT16 AVDT_ConfigRsp(UINT8 handle, UINT8 label, UINT8 error_code, UINT8 category)
  506. {
  507. tAVDT_SCB *p_scb;
  508. tAVDT_SCB_EVT evt;
  509. UINT16 result = AVDT_SUCCESS;
  510. UINT8 event_code;
  511. /* map handle to scb */
  512. if ((p_scb = avdt_scb_by_hdl(handle)) == NULL) {
  513. result = AVDT_BAD_HANDLE;
  514. }
  515. /* handle special case when this function is called but peer has not send
  516. ** a configuration cmd; ignore and return error result
  517. */
  518. else if (!p_scb->in_use) {
  519. result = AVDT_BAD_HANDLE;
  520. }
  521. /* send event to scb */
  522. else {
  523. evt.msg.hdr.err_code = error_code;
  524. evt.msg.hdr.err_param = category;
  525. evt.msg.hdr.label = label;
  526. if (error_code == 0) {
  527. event_code = AVDT_SCB_API_SETCONFIG_RSP_EVT;
  528. } else {
  529. event_code = AVDT_SCB_API_SETCONFIG_REJ_EVT;
  530. }
  531. avdt_scb_event(p_scb, event_code, &evt);
  532. }
  533. return result;
  534. }
  535. /*******************************************************************************
  536. **
  537. ** Function AVDT_StartReq
  538. **
  539. ** Description Start one or more stream endpoints. This initiates the
  540. ** transfer of media packets for the streams. All stream
  541. ** endpoints must previously be opened. When the streams
  542. ** are started, an AVDT_START_CFM_EVT is sent to the
  543. ** application via the control callback function for each stream.
  544. **
  545. **
  546. ** Returns AVDT_SUCCESS if successful, otherwise error.
  547. **
  548. *******************************************************************************/
  549. UINT16 AVDT_StartReq(UINT8 *p_handles, UINT8 num_handles)
  550. {
  551. tAVDT_SCB *p_scb = NULL;
  552. tAVDT_CCB_EVT evt;
  553. UINT16 result = AVDT_SUCCESS;
  554. int i;
  555. if ((num_handles == 0) || (num_handles > AVDT_NUM_SEPS)) {
  556. result = AVDT_BAD_PARAMS;
  557. } else {
  558. /* verify handles */
  559. for (i = 0; i < num_handles; i++) {
  560. if ((p_scb = avdt_scb_by_hdl(p_handles[i])) == NULL) {
  561. result = AVDT_BAD_HANDLE;
  562. break;
  563. }
  564. }
  565. }
  566. if (result == AVDT_SUCCESS) {
  567. if (p_scb->p_ccb == NULL) {
  568. result = AVDT_BAD_HANDLE;
  569. } else {
  570. /* send event to ccb */
  571. memcpy(evt.msg.multi.seid_list, p_handles, num_handles);
  572. evt.msg.multi.num_seps = num_handles;
  573. avdt_ccb_event(p_scb->p_ccb, AVDT_CCB_API_START_REQ_EVT, &evt);
  574. }
  575. }
  576. return result;
  577. }
  578. /*******************************************************************************
  579. **
  580. ** Function AVDT_SuspendReq
  581. **
  582. ** Description Suspend one or more stream endpoints. This suspends the
  583. ** transfer of media packets for the streams. All stream
  584. ** endpoints must previously be open and started. When the
  585. ** streams are suspended, an AVDT_SUSPEND_CFM_EVT is sent to
  586. ** the application via the control callback function for
  587. ** each stream.
  588. **
  589. **
  590. ** Returns AVDT_SUCCESS if successful, otherwise error.
  591. **
  592. *******************************************************************************/
  593. UINT16 AVDT_SuspendReq(UINT8 *p_handles, UINT8 num_handles)
  594. {
  595. tAVDT_SCB *p_scb = NULL;
  596. tAVDT_CCB_EVT evt;
  597. UINT16 result = AVDT_SUCCESS;
  598. int i;
  599. if ((num_handles == 0) || (num_handles > AVDT_NUM_SEPS)) {
  600. result = AVDT_BAD_PARAMS;
  601. } else {
  602. /* verify handles */
  603. for (i = 0; i < num_handles; i++) {
  604. if ((p_scb = avdt_scb_by_hdl(p_handles[i])) == NULL) {
  605. result = AVDT_BAD_HANDLE;
  606. break;
  607. }
  608. }
  609. }
  610. if (result == AVDT_SUCCESS) {
  611. if (p_scb->p_ccb == NULL) {
  612. result = AVDT_BAD_HANDLE;
  613. } else {
  614. /* send event to ccb */
  615. memcpy(evt.msg.multi.seid_list, p_handles, num_handles);
  616. evt.msg.multi.num_seps = num_handles;
  617. avdt_ccb_event(p_scb->p_ccb, AVDT_CCB_API_SUSPEND_REQ_EVT, &evt);
  618. }
  619. }
  620. return result;
  621. }
  622. /*******************************************************************************
  623. **
  624. ** Function AVDT_CloseReq
  625. **
  626. ** Description Close a stream endpoint. This stops the transfer of media
  627. ** packets and closes the transport channel associated with
  628. ** this stream endpoint. When the stream is closed, an
  629. ** AVDT_CLOSE_CFM_EVT is sent to the application via the
  630. ** control callback function for this handle.
  631. **
  632. **
  633. ** Returns AVDT_SUCCESS if successful, otherwise error.
  634. **
  635. *******************************************************************************/
  636. UINT16 AVDT_CloseReq(UINT8 handle)
  637. {
  638. tAVDT_SCB *p_scb;
  639. UINT16 result = AVDT_SUCCESS;
  640. /* map handle to scb */
  641. if ((p_scb = avdt_scb_by_hdl(handle)) == NULL) {
  642. result = AVDT_BAD_HANDLE;
  643. } else
  644. /* send event to scb */
  645. {
  646. avdt_scb_event(p_scb, AVDT_SCB_API_CLOSE_REQ_EVT, NULL);
  647. }
  648. return result;
  649. }
  650. /*******************************************************************************
  651. **
  652. ** Function AVDT_ReconfigReq
  653. **
  654. ** Description Reconfigure a stream endpoint. This allows the application
  655. ** to change the codec or content protection capabilities of
  656. ** a stream endpoint after it has been opened. This function
  657. ** can only be called if the stream is opened but not started
  658. ** or if the stream has been suspended. When the procedure
  659. ** is completed, an AVDT_RECONFIG_CFM_EVT is sent to the
  660. ** application via the control callback function for this handle.
  661. **
  662. **
  663. ** Returns AVDT_SUCCESS if successful, otherwise error.
  664. **
  665. *******************************************************************************/
  666. UINT16 AVDT_ReconfigReq(UINT8 handle, tAVDT_CFG *p_cfg)
  667. {
  668. tAVDT_SCB *p_scb;
  669. UINT16 result = AVDT_SUCCESS;
  670. tAVDT_SCB_EVT evt;
  671. /* map handle to scb */
  672. if ((p_scb = avdt_scb_by_hdl(handle)) == NULL) {
  673. result = AVDT_BAD_HANDLE;
  674. }
  675. /* send event to scb */
  676. else {
  677. /* force psc_mask to zero */
  678. p_cfg->psc_mask = 0;
  679. evt.msg.reconfig_cmd.p_cfg = p_cfg;
  680. avdt_scb_event(p_scb, AVDT_SCB_API_RECONFIG_REQ_EVT, &evt);
  681. }
  682. return result;
  683. }
  684. /*******************************************************************************
  685. **
  686. ** Function AVDT_ReconfigRsp
  687. **
  688. ** Description Respond to a reconfigure request from the peer device.
  689. ** This function must be called if the application receives
  690. ** an AVDT_RECONFIG_IND_EVT through its control callback.
  691. **
  692. **
  693. ** Returns AVDT_SUCCESS if successful, otherwise error.
  694. **
  695. *******************************************************************************/
  696. UINT16 AVDT_ReconfigRsp(UINT8 handle, UINT8 label, UINT8 error_code, UINT8 category)
  697. {
  698. tAVDT_SCB *p_scb;
  699. tAVDT_SCB_EVT evt;
  700. UINT16 result = AVDT_SUCCESS;
  701. /* map handle to scb */
  702. if ((p_scb = avdt_scb_by_hdl(handle)) == NULL) {
  703. result = AVDT_BAD_HANDLE;
  704. }
  705. /* send event to scb */
  706. else {
  707. evt.msg.hdr.err_code = error_code;
  708. evt.msg.hdr.err_param = category;
  709. evt.msg.hdr.label = label;
  710. avdt_scb_event(p_scb, AVDT_SCB_API_RECONFIG_RSP_EVT, &evt);
  711. }
  712. return result;
  713. }
  714. /*******************************************************************************
  715. **
  716. ** Function AVDT_SecurityReq
  717. **
  718. ** Description Send a security request to the peer device. When the
  719. ** security procedure is completed, an AVDT_SECURITY_CFM_EVT
  720. ** is sent to the application via the control callback function
  721. ** for this handle. (Please note that AVDTP security procedures
  722. ** are unrelated to Bluetooth link level security.)
  723. **
  724. **
  725. ** Returns AVDT_SUCCESS if successful, otherwise error.
  726. **
  727. *******************************************************************************/
  728. UINT16 AVDT_SecurityReq(UINT8 handle, UINT8 *p_data, UINT16 len)
  729. {
  730. tAVDT_SCB *p_scb;
  731. UINT16 result = AVDT_SUCCESS;
  732. tAVDT_SCB_EVT evt;
  733. /* map handle to scb */
  734. if ((p_scb = avdt_scb_by_hdl(handle)) == NULL) {
  735. result = AVDT_BAD_HANDLE;
  736. }
  737. /* send event to scb */
  738. else {
  739. evt.msg.security_rsp.p_data = p_data;
  740. evt.msg.security_rsp.len = len;
  741. avdt_scb_event(p_scb, AVDT_SCB_API_SECURITY_REQ_EVT, &evt);
  742. }
  743. return result;
  744. }
  745. /*******************************************************************************
  746. **
  747. ** Function AVDT_SecurityRsp
  748. **
  749. ** Description Respond to a security request from the peer device.
  750. ** This function must be called if the application receives
  751. ** an AVDT_SECURITY_IND_EVT through its control callback.
  752. ** (Please note that AVDTP security procedures are unrelated
  753. ** to Bluetooth link level security.)
  754. **
  755. **
  756. ** Returns AVDT_SUCCESS if successful, otherwise error.
  757. **
  758. *******************************************************************************/
  759. UINT16 AVDT_SecurityRsp(UINT8 handle, UINT8 label, UINT8 error_code,
  760. UINT8 *p_data, UINT16 len)
  761. {
  762. tAVDT_SCB *p_scb;
  763. UINT16 result = AVDT_SUCCESS;
  764. tAVDT_SCB_EVT evt;
  765. /* map handle to scb */
  766. if ((p_scb = avdt_scb_by_hdl(handle)) == NULL) {
  767. result = AVDT_BAD_HANDLE;
  768. }
  769. /* send event to scb */
  770. else {
  771. evt.msg.security_rsp.hdr.err_code = error_code;
  772. evt.msg.security_rsp.hdr.label = label;
  773. evt.msg.security_rsp.p_data = p_data;
  774. evt.msg.security_rsp.len = len;
  775. avdt_scb_event(p_scb, AVDT_SCB_API_SECURITY_RSP_EVT, &evt);
  776. }
  777. return result;
  778. }
  779. /*******************************************************************************
  780. **
  781. ** Function AVDT_WriteReqOpt
  782. **
  783. ** Description Send a media packet to the peer device. The stream must
  784. ** be started before this function is called. Also, this
  785. ** function can only be called if the stream is a SRC.
  786. **
  787. ** When AVDTP has sent the media packet and is ready for the
  788. ** next packet, an AVDT_WRITE_CFM_EVT is sent to the
  789. ** application via the control callback. The application must
  790. ** wait for the AVDT_WRITE_CFM_EVT before it makes the next
  791. ** call to AVDT_WriteReq(). If the applications calls
  792. ** AVDT_WriteReq() before it receives the event the packet
  793. ** will not be sent. The application may make its first call
  794. ** to AVDT_WriteReq() after it receives an AVDT_START_CFM_EVT
  795. ** or AVDT_START_IND_EVT.
  796. **
  797. ** The application passes the packet using the BT_HDR structure.
  798. ** This structure is described in section 2.1. The offset
  799. ** field must be equal to or greater than AVDT_MEDIA_OFFSET
  800. ** (if NO_RTP is specified, L2CAP_MIN_OFFSET can be used).
  801. ** This allows enough space in the buffer for the L2CAP and
  802. ** AVDTP headers.
  803. **
  804. ** The memory pointed to by p_pkt must be a GKI buffer
  805. ** allocated by the application. This buffer will be freed
  806. ** by the protocol stack; the application must not free
  807. ** this buffer.
  808. **
  809. ** The opt parameter allows passing specific options like:
  810. ** - NO_RTP : do not add the RTP header to buffer
  811. **
  812. ** Returns AVDT_SUCCESS if successful, otherwise error.
  813. **
  814. *******************************************************************************/
  815. UINT16 AVDT_WriteReqOpt(UINT8 handle, BT_HDR *p_pkt, UINT32 time_stamp, UINT8 m_pt, tAVDT_DATA_OPT_MASK opt)
  816. {
  817. tAVDT_SCB *p_scb;
  818. tAVDT_SCB_EVT evt;
  819. UINT16 result = AVDT_SUCCESS;
  820. /* map handle to scb */
  821. if ((p_scb = avdt_scb_by_hdl(handle)) == NULL) {
  822. result = AVDT_BAD_HANDLE;
  823. } else {
  824. evt.apiwrite.p_buf = p_pkt;
  825. evt.apiwrite.time_stamp = time_stamp;
  826. evt.apiwrite.m_pt = m_pt;
  827. evt.apiwrite.opt = opt;
  828. #if AVDT_MULTIPLEXING == TRUE
  829. GKI_init_q (&evt.apiwrite.frag_q);
  830. #endif
  831. avdt_scb_event(p_scb, AVDT_SCB_API_WRITE_REQ_EVT, &evt);
  832. }
  833. return result;
  834. }
  835. /*******************************************************************************
  836. **
  837. ** Function AVDT_WriteReq
  838. **
  839. ** Description Send a media packet to the peer device. The stream must
  840. ** be started before this function is called. Also, this
  841. ** function can only be called if the stream is a SRC.
  842. **
  843. ** When AVDTP has sent the media packet and is ready for the
  844. ** next packet, an AVDT_WRITE_CFM_EVT is sent to the
  845. ** application via the control callback. The application must
  846. ** wait for the AVDT_WRITE_CFM_EVT before it makes the next
  847. ** call to AVDT_WriteReq(). If the applications calls
  848. ** AVDT_WriteReq() before it receives the event the packet
  849. ** will not be sent. The application may make its first call
  850. ** to AVDT_WriteReq() after it receives an AVDT_START_CFM_EVT
  851. ** or AVDT_START_IND_EVT.
  852. **
  853. ** The application passes the packet using the BT_HDR structure.
  854. ** This structure is described in section 2.1. The offset
  855. ** field must be equal to or greater than AVDT_MEDIA_OFFSET.
  856. ** This allows enough space in the buffer for the L2CAP and
  857. ** AVDTP headers.
  858. **
  859. ** The memory pointed to by p_pkt must be a GKI buffer
  860. ** allocated by the application. This buffer will be freed
  861. ** by the protocol stack; the application must not free
  862. ** this buffer.
  863. **
  864. **
  865. ** Returns AVDT_SUCCESS if successful, otherwise error.
  866. **
  867. *******************************************************************************/
  868. UINT16 AVDT_WriteReq(UINT8 handle, BT_HDR *p_pkt, UINT32 time_stamp, UINT8 m_pt)
  869. {
  870. return AVDT_WriteReqOpt(handle, p_pkt, time_stamp, m_pt, AVDT_DATA_OPT_NONE);
  871. }
  872. /*******************************************************************************
  873. **
  874. ** Function AVDT_ConnectReq
  875. **
  876. ** Description This function initiates an AVDTP signaling connection
  877. ** to the peer device. When the connection is completed, an
  878. ** AVDT_CONNECT_IND_EVT is sent to the application via its
  879. ** control callback function. If the connection attempt fails
  880. ** an AVDT_DISCONNECT_IND_EVT is sent. The security mask
  881. ** parameter overrides the outgoing security mask set in
  882. ** AVDT_Register().
  883. **
  884. ** Returns AVDT_SUCCESS if successful, otherwise error.
  885. **
  886. *******************************************************************************/
  887. UINT16 AVDT_ConnectReq(BD_ADDR bd_addr, UINT8 sec_mask, tAVDT_CTRL_CBACK *p_cback)
  888. {
  889. tAVDT_CCB *p_ccb = NULL;
  890. UINT16 result = AVDT_SUCCESS;
  891. tAVDT_CCB_EVT evt;
  892. /* find channel control block for this bd addr; if none, allocate one */
  893. if ((p_ccb = avdt_ccb_by_bd(bd_addr)) == NULL) {
  894. if ((p_ccb = avdt_ccb_alloc(bd_addr)) == NULL) {
  895. /* could not allocate channel control block */
  896. result = AVDT_NO_RESOURCES;
  897. }
  898. } else if (p_ccb->ll_opened == FALSE) {
  899. AVDT_TRACE_WARNING("AVDT_ConnectReq: CCB LL is in the middle of opening");
  900. /* ccb was already allocated for the incoming signalling. */
  901. result = AVDT_BUSY;
  902. }
  903. if (result == AVDT_SUCCESS) {
  904. /* send event to ccb */
  905. evt.connect.p_cback = p_cback;
  906. evt.connect.sec_mask = sec_mask;
  907. avdt_ccb_event(p_ccb, AVDT_CCB_API_CONNECT_REQ_EVT, &evt);
  908. }
  909. return result;
  910. }
  911. /*******************************************************************************
  912. **
  913. ** Function AVDT_DisconnectReq
  914. **
  915. ** Description This function disconnect an AVDTP signaling connection
  916. ** to the peer device. When disconnected an
  917. ** AVDT_DISCONNECT_IND_EVT is sent to the application via its
  918. ** control callback function.
  919. **
  920. ** Returns AVDT_SUCCESS if successful, otherwise error.
  921. **
  922. *******************************************************************************/
  923. UINT16 AVDT_DisconnectReq(BD_ADDR bd_addr, tAVDT_CTRL_CBACK *p_cback)
  924. {
  925. tAVDT_CCB *p_ccb = NULL;
  926. UINT16 result = AVDT_SUCCESS;
  927. tAVDT_CCB_EVT evt;
  928. /* find channel control block for this bd addr; if none, error */
  929. if ((p_ccb = avdt_ccb_by_bd(bd_addr)) == NULL) {
  930. result = AVDT_BAD_PARAMS;
  931. }
  932. if (result == AVDT_SUCCESS) {
  933. /* send event to ccb */
  934. evt.disconnect.p_cback = p_cback;
  935. avdt_ccb_event(p_ccb, AVDT_CCB_API_DISCONNECT_REQ_EVT, &evt);
  936. }
  937. return result;
  938. }
  939. /*******************************************************************************
  940. **
  941. ** Function AVDT_GetL2CapChannel
  942. **
  943. ** Description Get the L2CAP CID used by the handle.
  944. **
  945. ** Returns CID if successful, otherwise 0.
  946. **
  947. *******************************************************************************/
  948. UINT16 AVDT_GetL2CapChannel(UINT8 handle)
  949. {
  950. tAVDT_SCB *p_scb;
  951. tAVDT_CCB *p_ccb;
  952. UINT8 tcid;
  953. UINT16 lcid = 0;
  954. /* map handle to scb */
  955. if (((p_scb = avdt_scb_by_hdl(handle)) != NULL)
  956. && ((p_ccb = p_scb->p_ccb) != NULL)) {
  957. /* get tcid from type, scb */
  958. tcid = avdt_ad_type_to_tcid(AVDT_CHAN_MEDIA, p_scb);
  959. lcid = avdt_cb.ad.rt_tbl[avdt_ccb_to_idx(p_ccb)][tcid].lcid;
  960. }
  961. return (lcid);
  962. }
  963. /*******************************************************************************
  964. **
  965. ** Function AVDT_GetSignalChannel
  966. **
  967. ** Description Get the L2CAP CID used by the signal channel of the given handle.
  968. **
  969. ** Returns CID if successful, otherwise 0.
  970. **
  971. *******************************************************************************/
  972. UINT16 AVDT_GetSignalChannel(UINT8 handle, BD_ADDR bd_addr)
  973. {
  974. tAVDT_SCB *p_scb;
  975. tAVDT_CCB *p_ccb;
  976. UINT8 tcid = 0; /* tcid is always 0 for signal channel */
  977. UINT16 lcid = 0;
  978. /* map handle to scb */
  979. if (((p_scb = avdt_scb_by_hdl(handle)) != NULL)
  980. && ((p_ccb = p_scb->p_ccb) != NULL)) {
  981. lcid = avdt_cb.ad.rt_tbl[avdt_ccb_to_idx(p_ccb)][tcid].lcid;
  982. } else if ((p_ccb = avdt_ccb_by_bd(bd_addr)) != NULL) {
  983. lcid = avdt_cb.ad.rt_tbl[avdt_ccb_to_idx(p_ccb)][tcid].lcid;
  984. }
  985. return (lcid);
  986. }
  987. #if AVDT_MULTIPLEXING == TRUE
  988. /*******************************************************************************
  989. **
  990. ** Function AVDT_WriteDataReq
  991. **
  992. ** Description Send a media packet to the peer device. The stream must
  993. ** be started before this function is called. Also, this
  994. ** function can only be called if the stream is a SRC.
  995. **
  996. ** When AVDTP has sent the media packet and is ready for the
  997. ** next packet, an AVDT_WRITE_CFM_EVT is sent to the
  998. ** application via the control callback. The application must
  999. ** wait for the AVDT_WRITE_CFM_EVT before it makes the next
  1000. ** call to AVDT_WriteDataReq(). If the applications calls
  1001. ** AVDT_WriteDataReq() before it receives the event the packet
  1002. ** will not be sent. The application may make its first call
  1003. ** to AVDT_WriteDataReq() after it receives an
  1004. ** AVDT_START_CFM_EVT or AVDT_START_IND_EVT.
  1005. **
  1006. ** Returns AVDT_SUCCESS if successful, otherwise error.
  1007. **
  1008. *******************************************************************************/
  1009. extern UINT16 AVDT_WriteDataReq(UINT8 handle, UINT8 *p_data, UINT32 data_len,
  1010. UINT32 time_stamp, UINT8 m_pt, UINT8 marker)
  1011. {
  1012. tAVDT_SCB *p_scb;
  1013. tAVDT_SCB_EVT evt;
  1014. UINT16 result = AVDT_SUCCESS;
  1015. do {
  1016. /* check length of media frame */
  1017. if (data_len > AVDT_MAX_MEDIA_SIZE) {
  1018. result = AVDT_BAD_PARAMS;
  1019. break;
  1020. }
  1021. /* map handle to scb */
  1022. if ((p_scb = avdt_scb_by_hdl(handle)) == NULL) {
  1023. result = AVDT_BAD_HANDLE;
  1024. break;
  1025. }
  1026. AVDT_TRACE_WARNING("mux_tsid_media:%d\n", p_scb->curr_cfg.mux_tsid_media);
  1027. if (p_scb->p_pkt != NULL
  1028. || p_scb->p_ccb == NULL
  1029. || !GKI_queue_is_empty(&p_scb->frag_q)
  1030. || p_scb->frag_off != 0
  1031. || p_scb->curr_cfg.mux_tsid_media == 0) {
  1032. result = AVDT_ERR_BAD_STATE;
  1033. AVDT_TRACE_WARNING("p_scb->p_pkt=%p, p_scb->p_ccb=%p, IsQueueEmpty=%x, p_scb->frag_off=%x\n",
  1034. p_scb->p_pkt, p_scb->p_ccb, GKI_queue_is_empty(&p_scb->frag_q), p_scb->frag_off);
  1035. break;
  1036. }
  1037. evt.apiwrite.p_buf = 0; /* it will indicate using of fragments queue frag_q */
  1038. /* create queue of media fragments */
  1039. GKI_init_q (&evt.apiwrite.frag_q);
  1040. /* compose fragments from media payload and put fragments into gueue */
  1041. avdt_scb_queue_frags(p_scb, &p_data, &data_len, &evt.apiwrite.frag_q);
  1042. if (GKI_queue_is_empty(&evt.apiwrite.frag_q)) {
  1043. AVDT_TRACE_WARNING("AVDT_WriteDataReq out of GKI buffers");
  1044. result = AVDT_ERR_RESOURCE;
  1045. break;
  1046. }
  1047. evt.apiwrite.data_len = data_len;
  1048. evt.apiwrite.p_data = p_data;
  1049. /* process the fragments queue */
  1050. evt.apiwrite.time_stamp = time_stamp;
  1051. evt.apiwrite.m_pt = m_pt | (marker << 7);
  1052. avdt_scb_event(p_scb, AVDT_SCB_API_WRITE_REQ_EVT, &evt);
  1053. } while (0);
  1054. #if (BT_USE_TRACES == TRUE)
  1055. if (result != AVDT_SUCCESS) {
  1056. AVDT_TRACE_WARNING("*** AVDT_WriteDataReq failed result=%d\n", result);
  1057. }
  1058. #endif
  1059. return result;
  1060. }
  1061. #endif
  1062. #if AVDT_MULTIPLEXING == TRUE
  1063. /*******************************************************************************
  1064. **
  1065. ** Function AVDT_SetMediaBuf
  1066. **
  1067. ** Description Assigns buffer for media packets or forbids using of assigned
  1068. ** buffer if argument p_buf is NULL. This function can only
  1069. ** be called if the stream is a SNK.
  1070. **
  1071. ** AVDTP uses this buffer to reassemble fragmented media packets.
  1072. ** When AVDTP receives a complete media packet, it calls the
  1073. ** p_media_cback assigned by AVDT_CreateStream().
  1074. ** This function can be called during callback to assign a
  1075. ** different buffer for next media packet or can leave the current
  1076. ** buffer for next packet.
  1077. **
  1078. ** Returns AVDT_SUCCESS if successful, otherwise error.
  1079. **
  1080. *******************************************************************************/
  1081. extern UINT16 AVDT_SetMediaBuf(UINT8 handle, UINT8 *p_buf, UINT32 buf_len)
  1082. {
  1083. tAVDT_SCB *p_scb;
  1084. UINT16 result = AVDT_SUCCESS;
  1085. /* map handle to scb */
  1086. if ((p_scb = avdt_scb_by_hdl(handle)) == NULL) {
  1087. result = AVDT_BAD_HANDLE;
  1088. } else {
  1089. if (p_buf && p_scb->cs.p_media_cback == NULL) {
  1090. result = AVDT_NO_RESOURCES;
  1091. } else {
  1092. p_scb->p_media_buf = p_buf;
  1093. p_scb->media_buf_len = buf_len;
  1094. }
  1095. }
  1096. return result;
  1097. }
  1098. #endif
  1099. #if AVDT_REPORTING == TRUE
  1100. /*******************************************************************************
  1101. **
  1102. ** Function AVDT_SendReport
  1103. **
  1104. ** Description
  1105. **
  1106. **
  1107. **
  1108. ** Returns
  1109. **
  1110. *******************************************************************************/
  1111. UINT16 AVDT_SendReport(UINT8 handle, AVDT_REPORT_TYPE type,
  1112. tAVDT_REPORT_DATA *p_data)
  1113. {
  1114. tAVDT_SCB *p_scb;
  1115. UINT16 result = AVDT_BAD_PARAMS;
  1116. BT_HDR *p_pkt;
  1117. tAVDT_TC_TBL *p_tbl;
  1118. UINT8 *p, *plen, *pm1, *p_end;
  1119. #if AVDT_MULTIPLEXING == TRUE
  1120. UINT8 *p_al = NULL, u;
  1121. #endif
  1122. UINT32 ssrc;
  1123. UINT16 len;
  1124. /* map handle to scb && verify parameters */
  1125. if (((p_scb = avdt_scb_by_hdl(handle)) != NULL)
  1126. && (p_scb->p_ccb != NULL)
  1127. && (((type == AVDT_RTCP_PT_SR) && (p_scb->cs.tsep == AVDT_TSEP_SRC)) ||
  1128. ((type == AVDT_RTCP_PT_RR) && (p_scb->cs.tsep == AVDT_TSEP_SNK)) ||
  1129. (type == AVDT_RTCP_PT_SDES)) ) {
  1130. result = AVDT_NO_RESOURCES;
  1131. /* build SR - assume fit in one packet */
  1132. p_tbl = avdt_ad_tc_tbl_by_type(AVDT_CHAN_REPORT, p_scb->p_ccb, p_scb);
  1133. if ((p_tbl->state == AVDT_AD_ST_OPEN) &&
  1134. (p_pkt = (BT_HDR *)GKI_getbuf(p_tbl->peer_mtu)) != NULL) {
  1135. p_pkt->offset = L2CAP_MIN_OFFSET;
  1136. p = (UINT8 *)(p_pkt + 1) + p_pkt->offset;
  1137. #if AVDT_MULTIPLEXING == TRUE
  1138. if (p_scb->curr_cfg.psc_mask & AVDT_PSC_MUX) {
  1139. /* Adaptation Layer header later */
  1140. p_al = p;
  1141. p += 2;
  1142. }
  1143. #endif
  1144. pm1 = p;
  1145. *p++ = AVDT_MEDIA_OCTET1 | 1;
  1146. *p++ = type;
  1147. /* save the location for length */
  1148. plen = p;
  1149. p += 2;
  1150. ssrc = avdt_scb_gen_ssrc(p_scb);
  1151. UINT32_TO_BE_STREAM(p, ssrc);
  1152. switch (type) {
  1153. case AVDT_RTCP_PT_SR: /* Sender Report */
  1154. *pm1 = AVDT_MEDIA_OCTET1;
  1155. UINT32_TO_BE_STREAM(p, p_data->sr.ntp_sec);
  1156. UINT32_TO_BE_STREAM(p, p_data->sr.ntp_frac);
  1157. UINT32_TO_BE_STREAM(p, p_data->sr.rtp_time);
  1158. UINT32_TO_BE_STREAM(p, p_data->sr.pkt_count);
  1159. UINT32_TO_BE_STREAM(p, p_data->sr.octet_count);
  1160. break;
  1161. case AVDT_RTCP_PT_RR: /* Receiver Report */
  1162. *p++ = p_data->rr.frag_lost;
  1163. AVDT_TRACE_API("packet_lost: %d\n", p_data->rr.packet_lost);
  1164. p_data->rr.packet_lost &= 0xFFFFFF;
  1165. AVDT_TRACE_API("packet_lost: %d\n", p_data->rr.packet_lost);
  1166. UINT24_TO_BE_STREAM(p, p_data->rr.packet_lost);
  1167. UINT32_TO_BE_STREAM(p, p_data->rr.seq_num_rcvd);
  1168. UINT32_TO_BE_STREAM(p, p_data->rr.jitter);
  1169. UINT32_TO_BE_STREAM(p, p_data->rr.lsr);
  1170. UINT32_TO_BE_STREAM(p, p_data->rr.dlsr);
  1171. break;
  1172. case AVDT_RTCP_PT_SDES: /* Source Description */
  1173. *p++ = AVDT_RTCP_SDES_CNAME;
  1174. len = strlen((char *)p_data->cname);
  1175. if (len > AVDT_MAX_CNAME_SIZE) {
  1176. len = AVDT_MAX_CNAME_SIZE;
  1177. }
  1178. *p++ = (UINT8)len;
  1179. BCM_STRNCPY_S((char *)p, len + 1, (char *)p_data->cname, len + 1);
  1180. p += len;
  1181. break;
  1182. }
  1183. p_end = p;
  1184. len = p - pm1 - 1;
  1185. UINT16_TO_BE_STREAM(plen, len);
  1186. #if AVDT_MULTIPLEXING == TRUE
  1187. if (p_scb->curr_cfg.psc_mask & AVDT_PSC_MUX) {
  1188. /* Adaptation Layer header */
  1189. p = p_al;
  1190. len++;
  1191. UINT16_TO_BE_STREAM(p_al, len );
  1192. /* TSID, no-fragment bit and coding of length(9-bit length field) */
  1193. u = *p;
  1194. *p = (p_scb->curr_cfg.mux_tsid_report << 3) | AVDT_ALH_LCODE_9BITM0;
  1195. if (u) {
  1196. *p |= AVDT_ALH_LCODE_9BITM1;
  1197. }
  1198. }
  1199. #endif
  1200. /* set the actual payload length */
  1201. p_pkt->len = p_end - p;
  1202. /* send the packet */
  1203. if (L2CAP_DW_FAILED != avdt_ad_write_req(AVDT_CHAN_REPORT, p_scb->p_ccb, p_scb, p_pkt)) {
  1204. result = AVDT_SUCCESS;
  1205. }
  1206. }
  1207. }
  1208. return result;
  1209. }
  1210. #endif
  1211. /******************************************************************************
  1212. **
  1213. ** Function AVDT_SetTraceLevel
  1214. **
  1215. ** Description Sets the trace level for AVDT. If 0xff is passed, the
  1216. ** current trace level is returned.
  1217. **
  1218. ** Input Parameters:
  1219. ** new_level: The level to set the AVDT tracing to:
  1220. ** 0xff-returns the current setting.
  1221. ** 0-turns off tracing.
  1222. ** >= 1-Errors.
  1223. ** >= 2-Warnings.
  1224. ** >= 3-APIs.
  1225. ** >= 4-Events.
  1226. ** >= 5-Debug.
  1227. **
  1228. ** Returns The new trace level or current trace level if
  1229. ** the input parameter is 0xff.
  1230. **
  1231. ******************************************************************************/
  1232. UINT8 AVDT_SetTraceLevel (UINT8 new_level)
  1233. {
  1234. if (new_level != 0xFF) {
  1235. avdt_cb.trace_level = new_level;
  1236. }
  1237. return (avdt_cb.trace_level);
  1238. }
  1239. #endif /* #if (defined(AVDT_INCLUDED) && AVDT_INCLUDED == TRUE) */