l2c_api.c 69 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903
  1. /******************************************************************************
  2. *
  3. * Copyright (C) 1999-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 file contains the L2CAP API code
  21. *
  22. ******************************************************************************/
  23. //#define LOG_TAG "bt_l2cap"
  24. //#include <stdlib.h>
  25. #include <string.h>
  26. #include <stdio.h>
  27. #include "bt_trace.h"
  28. #include "gki.h"
  29. #include "bt_types.h"
  30. #include "hcidefs.h"
  31. #include "hcimsgs.h"
  32. #include "l2cdefs.h"
  33. #include "l2c_int.h"
  34. #include "btu.h"
  35. #include "btm_api.h"
  36. #include "allocator.h"
  37. /*******************************************************************************
  38. **
  39. ** Function L2CA_Register
  40. **
  41. ** Description Other layers call this function to register for L2CAP
  42. ** services.
  43. **
  44. ** Returns PSM to use or zero if error. Typically, the PSM returned
  45. ** is the same as was passed in, but for an outgoing-only
  46. ** connection to a dynamic PSM, a "virtual" PSM is returned
  47. ** and should be used in the calls to L2CA_ConnectReq(),
  48. ** L2CA_ErtmConnectReq() and L2CA_Deregister()
  49. **
  50. *******************************************************************************/
  51. UINT16 L2CA_Register (UINT16 psm, tL2CAP_APPL_INFO *p_cb_info)
  52. {
  53. tL2C_RCB *p_rcb;
  54. UINT16 vpsm = psm;
  55. /* Verify that the required callback info has been filled in
  56. ** Note: Connection callbacks are required but not checked
  57. ** for here because it is possible to be only a client
  58. ** or only a server.
  59. */
  60. if ((!p_cb_info->pL2CA_ConfigCfm_Cb)
  61. || (!p_cb_info->pL2CA_ConfigInd_Cb)
  62. || (!p_cb_info->pL2CA_DataInd_Cb)
  63. || (!p_cb_info->pL2CA_DisconnectInd_Cb)) {
  64. L2CAP_TRACE_ERROR ("L2CAP - no cb registering PSM: 0x%04x", psm);
  65. return (0);
  66. }
  67. /* Verify PSM is valid */
  68. if (L2C_INVALID_PSM(psm)) {
  69. L2CAP_TRACE_ERROR ("L2CAP - invalid PSM value, PSM: 0x%04x", psm);
  70. return (0);
  71. }
  72. /* Check if this is a registration for an outgoing-only connection to */
  73. /* a dynamic PSM. If so, allocate a "virtual" PSM for the app to use. */
  74. if ( (psm >= 0x1001) && (p_cb_info->pL2CA_ConnectInd_Cb == NULL) ) {
  75. for (vpsm = 0x1002; vpsm < 0x8000; vpsm += 2) {
  76. if ((p_rcb = l2cu_find_rcb_by_psm (vpsm)) == NULL) {
  77. break;
  78. }
  79. }
  80. //L2CAP_TRACE_API ("L2CA_Register - Real PSM: 0x%04x Virtual PSM: 0x%04x", psm, vpsm);
  81. }
  82. /* If registration block already there, just overwrite it */
  83. if ((p_rcb = l2cu_find_rcb_by_psm (vpsm)) == NULL) {
  84. if ((p_rcb = l2cu_allocate_rcb (vpsm)) == NULL) {
  85. L2CAP_TRACE_WARNING ("L2CAP - no RCB available, PSM: 0x%04x vPSM: 0x%04x", psm, vpsm);
  86. return (0);
  87. }
  88. }
  89. p_rcb->api = *p_cb_info;
  90. p_rcb->real_psm = psm;
  91. return (vpsm);
  92. }
  93. /*******************************************************************************
  94. **
  95. ** Function L2CA_Deregister
  96. **
  97. ** Description Other layers call this function to de-register for L2CAP
  98. ** services.
  99. **
  100. ** Returns void
  101. **
  102. *******************************************************************************/
  103. void L2CA_Deregister (UINT16 psm)
  104. {
  105. tL2C_RCB *p_rcb;
  106. tL2C_CCB *p_ccb;
  107. tL2C_LCB *p_lcb;
  108. int ii;
  109. if ((p_rcb = l2cu_find_rcb_by_psm (psm)) != NULL) {
  110. p_lcb = &l2cb.lcb_pool[0];
  111. for (ii = 0; ii < MAX_L2CAP_LINKS; ii++, p_lcb++) {
  112. if (p_lcb->in_use) {
  113. if (((p_ccb = p_lcb->ccb_queue.p_first_ccb) == NULL)
  114. || (p_lcb->link_state == LST_DISCONNECTING)) {
  115. continue;
  116. }
  117. if ((p_ccb->in_use) &&
  118. ((p_ccb->chnl_state == CST_W4_L2CAP_DISCONNECT_RSP) ||
  119. (p_ccb->chnl_state == CST_W4_L2CA_DISCONNECT_RSP))) {
  120. continue;
  121. }
  122. if (p_ccb->p_rcb == p_rcb) {
  123. l2c_csm_execute (p_ccb, L2CEVT_L2CA_DISCONNECT_REQ, NULL);
  124. }
  125. }
  126. }
  127. l2cu_release_rcb (p_rcb);
  128. } else {
  129. L2CAP_TRACE_WARNING ("L2CAP - PSM: 0x%04x not found for deregistration", psm);
  130. }
  131. }
  132. /*******************************************************************************
  133. **
  134. ** Function L2CA_AllocatePSM
  135. **
  136. ** Description Other layers call this function to find an unused PSM for L2CAP
  137. ** services.
  138. **
  139. ** Returns PSM to use.
  140. **
  141. *******************************************************************************/
  142. UINT16 L2CA_AllocatePSM(void)
  143. {
  144. BOOLEAN done = FALSE;
  145. UINT16 psm = l2cb.dyn_psm;
  146. while (!done) {
  147. psm += 2;
  148. if (psm > 0xfeff) {
  149. psm = 0x1001;
  150. } else if (psm & 0x0100) {
  151. /* the upper byte must be even */
  152. psm += 0x0100;
  153. }
  154. /* if psm is in range of reserved BRCM Aware features */
  155. if ((BRCM_RESERVED_PSM_START <= psm) && (psm <= BRCM_RESERVED_PSM_END)) {
  156. continue;
  157. }
  158. /* make sure the newlly allocated psm is not used right now */
  159. if ((l2cu_find_rcb_by_psm (psm)) == NULL) {
  160. done = TRUE;
  161. }
  162. }
  163. l2cb.dyn_psm = psm;
  164. return (psm);
  165. }
  166. /*******************************************************************************
  167. **
  168. ** Function L2CA_ConnectReq
  169. **
  170. ** Description Higher layers call this function to create an L2CAP connection.
  171. ** Note that the connection is not established at this time, but
  172. ** connection establishment gets started. The callback function
  173. ** will be invoked when connection establishes or fails.
  174. **
  175. ** Returns the CID of the connection, or 0 if it failed to start
  176. **
  177. *******************************************************************************/
  178. UINT16 L2CA_ConnectReq (UINT16 psm, BD_ADDR p_bd_addr)
  179. {
  180. return L2CA_ErtmConnectReq (psm, p_bd_addr, NULL);
  181. }
  182. /*******************************************************************************
  183. **
  184. ** Function L2CA_ErtmConnectReq
  185. **
  186. ** Description Higher layers call this function to create an L2CAP connection.
  187. ** Note that the connection is not established at this time, but
  188. ** connection establishment gets started. The callback function
  189. ** will be invoked when connection establishes or fails.
  190. **
  191. ** Parameters: PSM: L2CAP PSM for the connection
  192. ** BD address of the peer
  193. ** Enhaced retransmission mode configurations
  194. ** Returns the CID of the connection, or 0 if it failed to start
  195. **
  196. *******************************************************************************/
  197. UINT16 L2CA_ErtmConnectReq (UINT16 psm, BD_ADDR p_bd_addr, tL2CAP_ERTM_INFO *p_ertm_info)
  198. {
  199. tL2C_LCB *p_lcb;
  200. tL2C_CCB *p_ccb;
  201. tL2C_RCB *p_rcb;
  202. //counter_add("l2cap.conn.req", 1);
  203. L2CAP_TRACE_API ("L2CA_ErtmConnectReq() PSM: 0x%04x BDA: %08x%04x p_ertm_info: %p allowed:0x%x preferred:%d", psm,
  204. (p_bd_addr[0] << 24) + (p_bd_addr[1] << 16) + (p_bd_addr[2] << 8) + p_bd_addr[3],
  205. (p_bd_addr[4] << 8) + p_bd_addr[5], p_ertm_info,
  206. (p_ertm_info) ? p_ertm_info->allowed_modes : 0,
  207. (p_ertm_info) ? p_ertm_info->preferred_mode : 0);
  208. /* Fail if we have not established communications with the controller */
  209. if (!BTM_IsDeviceUp()) {
  210. L2CAP_TRACE_WARNING ("L2CAP connect req - BTU not ready");
  211. return (0);
  212. }
  213. /* Fail if the PSM is not registered */
  214. if ((p_rcb = l2cu_find_rcb_by_psm (psm)) == NULL) {
  215. L2CAP_TRACE_WARNING ("L2CAP - no RCB for L2CA_conn_req, PSM: 0x%04x", psm);
  216. return (0);
  217. }
  218. /* First, see if we already have a link to the remote */
  219. /* assume all ERTM l2cap connection is going over BR/EDR for now */
  220. if ((p_lcb = l2cu_find_lcb_by_bd_addr (p_bd_addr, BT_TRANSPORT_BR_EDR)) == NULL) {
  221. /* No link. Get an LCB and start link establishment */
  222. if ( ((p_lcb = l2cu_allocate_lcb (p_bd_addr, FALSE, BT_TRANSPORT_BR_EDR)) == NULL)
  223. /* currently use BR/EDR for ERTM mode l2cap connection */
  224. || (l2cu_create_conn(p_lcb, BT_TRANSPORT_BR_EDR) == FALSE) ) {
  225. L2CAP_TRACE_WARNING ("L2CAP - conn not started for PSM: 0x%04x p_lcb: %p", psm, p_lcb);
  226. return (0);
  227. }
  228. }
  229. /* Allocate a channel control block */
  230. if ((p_ccb = l2cu_allocate_ccb (p_lcb, 0)) == NULL) {
  231. L2CAP_TRACE_WARNING ("L2CAP - no CCB for L2CA_conn_req, PSM: 0x%04x", psm);
  232. return (0);
  233. }
  234. /* Save registration info */
  235. p_ccb->p_rcb = p_rcb;
  236. if (p_ertm_info) {
  237. p_ccb->ertm_info = *p_ertm_info;
  238. /* Replace default indicators with the actual default pool */
  239. if (p_ccb->ertm_info.fcr_rx_pool_id == L2CAP_DEFAULT_ERM_POOL_ID) {
  240. p_ccb->ertm_info.fcr_rx_pool_id = L2CAP_FCR_RX_POOL_ID;
  241. }
  242. if (p_ccb->ertm_info.fcr_tx_pool_id == L2CAP_DEFAULT_ERM_POOL_ID) {
  243. p_ccb->ertm_info.fcr_tx_pool_id = L2CAP_FCR_TX_POOL_ID;
  244. }
  245. if (p_ccb->ertm_info.user_rx_pool_id == L2CAP_DEFAULT_ERM_POOL_ID) {
  246. p_ccb->ertm_info.user_rx_pool_id = HCI_ACL_POOL_ID;
  247. }
  248. if (p_ccb->ertm_info.user_tx_pool_id == L2CAP_DEFAULT_ERM_POOL_ID) {
  249. p_ccb->ertm_info.user_tx_pool_id = HCI_ACL_POOL_ID;
  250. }
  251. p_ccb->max_rx_mtu = GKI_get_pool_bufsize (p_ertm_info->user_rx_pool_id) -
  252. (L2CAP_MIN_OFFSET + L2CAP_SDU_LEN_OFFSET + L2CAP_FCS_LEN);
  253. }
  254. /* If link is up, start the L2CAP connection */
  255. if (p_lcb->link_state == LST_CONNECTED) {
  256. l2c_csm_execute (p_ccb, L2CEVT_L2CA_CONNECT_REQ, NULL);
  257. }
  258. /* If link is disconnecting, save link info to retry after disconnect
  259. * Possible Race condition when a reconnect occurs
  260. * on the channel during a disconnect of link. This
  261. * ccb will be automatically retried after link disconnect
  262. * arrives
  263. */
  264. else if (p_lcb->link_state == LST_DISCONNECTING) {
  265. L2CAP_TRACE_DEBUG ("L2CAP API - link disconnecting: RETRY LATER");
  266. /* Save ccb so it can be started after disconnect is finished */
  267. p_lcb->p_pending_ccb = p_ccb;
  268. }
  269. L2CAP_TRACE_API ("L2CAP - L2CA_conn_req(psm: 0x%04x) returned CID: 0x%04x", psm, p_ccb->local_cid);
  270. /* Return the local CID as our handle */
  271. return (p_ccb->local_cid);
  272. }
  273. bool L2CA_SetConnectionCallbacks(uint16_t local_cid, const tL2CAP_APPL_INFO *callbacks)
  274. {
  275. assert(callbacks != NULL);
  276. assert(callbacks->pL2CA_ConnectInd_Cb == NULL);
  277. assert(callbacks->pL2CA_ConnectCfm_Cb != NULL);
  278. assert(callbacks->pL2CA_ConfigInd_Cb != NULL);
  279. assert(callbacks->pL2CA_ConfigCfm_Cb != NULL);
  280. assert(callbacks->pL2CA_DisconnectInd_Cb != NULL);
  281. assert(callbacks->pL2CA_DisconnectCfm_Cb != NULL);
  282. assert(callbacks->pL2CA_CongestionStatus_Cb != NULL);
  283. assert(callbacks->pL2CA_DataInd_Cb != NULL);
  284. assert(callbacks->pL2CA_TxComplete_Cb != NULL);
  285. tL2C_CCB *channel_control_block = l2cu_find_ccb_by_cid(NULL, local_cid);
  286. if (!channel_control_block) {
  287. LOG_ERROR("%s no channel control block found for L2CAP LCID=0x%04x.", __func__, local_cid);
  288. return false;
  289. }
  290. // We're making a connection-specific registration control block so we check if
  291. // we already have a private one allocated to us on the heap. If not, we make a
  292. // new allocation, mark it as heap-allocated, and inherit the fields from the old
  293. // control block.
  294. tL2C_RCB *registration_control_block = channel_control_block->p_rcb;
  295. if (!channel_control_block->should_free_rcb) {
  296. registration_control_block = (tL2C_RCB *)osi_calloc(sizeof(tL2C_RCB));
  297. if (!registration_control_block) {
  298. LOG_ERROR("%s unable to allocate registration control block.", __func__);
  299. return false;
  300. }
  301. *registration_control_block = *channel_control_block->p_rcb;
  302. channel_control_block->p_rcb = registration_control_block;
  303. channel_control_block->should_free_rcb = true;
  304. }
  305. registration_control_block->api = *callbacks;
  306. return true;
  307. }
  308. /*******************************************************************************
  309. **
  310. ** Function L2CA_ConnectRsp
  311. **
  312. ** Description Higher layers call this function to accept an incoming
  313. ** L2CAP connection, for which they had gotten an connect
  314. ** indication callback.
  315. **
  316. ** Returns TRUE for success, FALSE for failure
  317. **
  318. *******************************************************************************/
  319. BOOLEAN L2CA_ConnectRsp (BD_ADDR p_bd_addr, UINT8 id, UINT16 lcid,
  320. UINT16 result, UINT16 status)
  321. {
  322. return L2CA_ErtmConnectRsp (p_bd_addr, id, lcid, result, status, NULL);
  323. }
  324. /*******************************************************************************
  325. **
  326. ** Function L2CA_ErtmConnectRsp
  327. **
  328. ** Description Higher layers call this function to accept an incoming
  329. ** L2CAP connection, for which they had gotten an connect
  330. ** indication callback.
  331. **
  332. ** Returns TRUE for success, FALSE for failure
  333. **
  334. *******************************************************************************/
  335. BOOLEAN L2CA_ErtmConnectRsp (BD_ADDR p_bd_addr, UINT8 id, UINT16 lcid, UINT16 result,
  336. UINT16 status, tL2CAP_ERTM_INFO *p_ertm_info)
  337. {
  338. tL2C_LCB *p_lcb;
  339. tL2C_CCB *p_ccb;
  340. //counter_add("l2cap.conn.rsp", 1);
  341. L2CAP_TRACE_API ("L2CA_ErtmConnectRsp() CID: 0x%04x Result: %d Status: %d BDA: %08x%04x p_ertm_info:%p",
  342. lcid, result, status,
  343. (p_bd_addr[0] << 24) + (p_bd_addr[1] << 16) + (p_bd_addr[2] << 8) + p_bd_addr[3],
  344. (p_bd_addr[4] << 8) + p_bd_addr[5], p_ertm_info);
  345. /* First, find the link control block */
  346. if ((p_lcb = l2cu_find_lcb_by_bd_addr (p_bd_addr, BT_TRANSPORT_BR_EDR)) == NULL) {
  347. /* No link. Get an LCB and start link establishment */
  348. L2CAP_TRACE_WARNING ("L2CAP - no LCB for L2CA_conn_rsp");
  349. return (FALSE);
  350. }
  351. /* Now, find the channel control block */
  352. if ((p_ccb = l2cu_find_ccb_by_cid (p_lcb, lcid)) == NULL) {
  353. L2CAP_TRACE_WARNING ("L2CAP - no CCB for L2CA_conn_rsp");
  354. return (FALSE);
  355. }
  356. /* The IDs must match */
  357. if (p_ccb->remote_id != id) {
  358. L2CAP_TRACE_WARNING ("L2CAP - bad id in L2CA_conn_rsp. Exp: %d Got: %d", p_ccb->remote_id, id);
  359. return (FALSE);
  360. }
  361. if (p_ertm_info) {
  362. p_ccb->ertm_info = *p_ertm_info;
  363. /* Replace default indicators with the actual default pool */
  364. if (p_ccb->ertm_info.fcr_rx_pool_id == L2CAP_DEFAULT_ERM_POOL_ID) {
  365. p_ccb->ertm_info.fcr_rx_pool_id = L2CAP_FCR_RX_POOL_ID;
  366. }
  367. if (p_ccb->ertm_info.fcr_tx_pool_id == L2CAP_DEFAULT_ERM_POOL_ID) {
  368. p_ccb->ertm_info.fcr_tx_pool_id = L2CAP_FCR_TX_POOL_ID;
  369. }
  370. if (p_ccb->ertm_info.user_rx_pool_id == L2CAP_DEFAULT_ERM_POOL_ID) {
  371. p_ccb->ertm_info.user_rx_pool_id = HCI_ACL_POOL_ID;
  372. }
  373. if (p_ccb->ertm_info.user_tx_pool_id == L2CAP_DEFAULT_ERM_POOL_ID) {
  374. p_ccb->ertm_info.user_tx_pool_id = HCI_ACL_POOL_ID;
  375. }
  376. p_ccb->max_rx_mtu = GKI_get_pool_bufsize (p_ertm_info->user_rx_pool_id) - (L2CAP_MIN_OFFSET + L2CAP_SDU_LEN_OFFSET + L2CAP_FCS_LEN);
  377. }
  378. if (result == L2CAP_CONN_OK) {
  379. l2c_csm_execute (p_ccb, L2CEVT_L2CA_CONNECT_RSP, NULL);
  380. } else {
  381. tL2C_CONN_INFO conn_info;
  382. conn_info.l2cap_result = result;
  383. conn_info.l2cap_status = status;
  384. if (result == L2CAP_CONN_PENDING) {
  385. l2c_csm_execute (p_ccb, L2CEVT_L2CA_CONNECT_RSP, &conn_info);
  386. } else {
  387. l2c_csm_execute (p_ccb, L2CEVT_L2CA_CONNECT_RSP_NEG, &conn_info);
  388. }
  389. }
  390. return (TRUE);
  391. }
  392. /*******************************************************************************
  393. **
  394. ** Function L2CA_ConfigReq
  395. **
  396. ** Description Higher layers call this function to send configuration.
  397. **
  398. ** Note: The FCR options of p_cfg are not used.
  399. **
  400. ** Returns TRUE if configuration sent, else FALSE
  401. **
  402. *******************************************************************************/
  403. BOOLEAN L2CA_ConfigReq (UINT16 cid, tL2CAP_CFG_INFO *p_cfg)
  404. {
  405. tL2C_CCB *p_ccb;
  406. //counter_add("l2cap.cfg.req", 1);
  407. L2CAP_TRACE_API ("L2CA_ConfigReq() CID 0x%04x: fcr_present:%d (mode %d) mtu_present:%d (%d)",
  408. cid, p_cfg->fcr_present, p_cfg->fcr.mode, p_cfg->mtu_present, p_cfg->mtu);
  409. /* Find the channel control block. We don't know the link it is on. */
  410. if ((p_ccb = l2cu_find_ccb_by_cid (NULL, cid)) == NULL) {
  411. L2CAP_TRACE_WARNING ("L2CAP - no CCB for L2CA_cfg_req, CID: %d", cid);
  412. return (FALSE);
  413. }
  414. /* We need to have at least one mode type common with the peer */
  415. if (!l2c_fcr_adj_our_req_options(p_ccb, p_cfg)) {
  416. return (FALSE);
  417. }
  418. /* Don't adjust FCR options if not used */
  419. if ((!p_cfg->fcr_present) || (p_cfg->fcr.mode == L2CAP_FCR_BASIC_MODE)) {
  420. /* FCR and FCS options are not used in basic mode */
  421. p_cfg->fcs_present = FALSE;
  422. p_cfg->ext_flow_spec_present = FALSE;
  423. if ( (p_cfg->mtu_present) && (p_cfg->mtu > L2CAP_MTU_SIZE) ) {
  424. L2CAP_TRACE_WARNING ("L2CAP - adjust MTU: %u too large", p_cfg->mtu);
  425. p_cfg->mtu = L2CAP_MTU_SIZE;
  426. }
  427. }
  428. /* Save the adjusted configuration in case it needs to be used for renegotiation */
  429. p_ccb->our_cfg = *p_cfg;
  430. l2c_csm_execute (p_ccb, L2CEVT_L2CA_CONFIG_REQ, p_cfg);
  431. return (TRUE);
  432. }
  433. /*******************************************************************************
  434. **
  435. ** Function L2CA_ConfigRsp
  436. **
  437. ** Description Higher layers call this function to send a configuration
  438. ** response.
  439. **
  440. ** Returns TRUE if configuration response sent, else FALSE
  441. **
  442. *******************************************************************************/
  443. BOOLEAN L2CA_ConfigRsp (UINT16 cid, tL2CAP_CFG_INFO *p_cfg)
  444. {
  445. tL2C_CCB *p_ccb;
  446. //counter_add("l2cap.cfg.rsp", 1);
  447. L2CAP_TRACE_API ("L2CA_ConfigRsp() CID: 0x%04x Result: %d MTU present:%d Flush TO:%d FCR:%d FCS:%d",
  448. cid, p_cfg->result, p_cfg->mtu_present, p_cfg->flush_to_present, p_cfg->fcr_present, p_cfg->fcs_present);
  449. /* Find the channel control block. We don't know the link it is on. */
  450. if ((p_ccb = l2cu_find_ccb_by_cid (NULL, cid)) == NULL) {
  451. L2CAP_TRACE_WARNING ("L2CAP - no CCB for L2CA_cfg_rsp, CID: %d", cid);
  452. return (FALSE);
  453. }
  454. if ( (p_cfg->result == L2CAP_CFG_OK) || (p_cfg->result == L2CAP_CFG_PENDING) ) {
  455. l2c_csm_execute (p_ccb, L2CEVT_L2CA_CONFIG_RSP, p_cfg);
  456. } else {
  457. p_cfg->fcr_present = FALSE; /* FCR options already negotiated before this point */
  458. /* Clear out any cached options that are being returned as an error (excluding FCR) */
  459. if (p_cfg->mtu_present) {
  460. p_ccb->peer_cfg.mtu_present = FALSE;
  461. }
  462. if (p_cfg->flush_to_present) {
  463. p_ccb->peer_cfg.flush_to_present = FALSE;
  464. }
  465. if (p_cfg->qos_present) {
  466. p_ccb->peer_cfg.qos_present = FALSE;
  467. }
  468. l2c_csm_execute (p_ccb, L2CEVT_L2CA_CONFIG_RSP_NEG, p_cfg);
  469. }
  470. return (TRUE);
  471. }
  472. /*******************************************************************************
  473. **
  474. ** Function L2CA_DisconnectReq
  475. **
  476. ** Description Higher layers call this function to disconnect a channel.
  477. **
  478. ** Returns TRUE if disconnect sent, else FALSE
  479. **
  480. *******************************************************************************/
  481. BOOLEAN L2CA_DisconnectReq (UINT16 cid)
  482. {
  483. tL2C_CCB *p_ccb;
  484. //counter_add("l2cap.disconn.req", 1);
  485. L2CAP_TRACE_API ("L2CA_DisconnectReq() CID: 0x%04x", cid);
  486. /* Find the channel control block. We don't know the link it is on. */
  487. if ((p_ccb = l2cu_find_ccb_by_cid (NULL, cid)) == NULL) {
  488. L2CAP_TRACE_WARNING ("L2CAP - no CCB for L2CA_disc_req, CID: %d", cid);
  489. return (FALSE);
  490. }
  491. l2c_csm_execute (p_ccb, L2CEVT_L2CA_DISCONNECT_REQ, NULL);
  492. return (TRUE);
  493. }
  494. /*******************************************************************************
  495. **
  496. ** Function L2CA_DisconnectRsp
  497. **
  498. ** Description Higher layers call this function to acknowledge the
  499. ** disconnection of a channel.
  500. **
  501. ** Returns void
  502. **
  503. *******************************************************************************/
  504. BOOLEAN L2CA_DisconnectRsp (UINT16 cid)
  505. {
  506. tL2C_CCB *p_ccb;
  507. //counter_add("l2cap.disconn.rsp", 1);
  508. L2CAP_TRACE_API ("L2CA_DisconnectRsp() CID: 0x%04x", cid);
  509. /* Find the channel control block. We don't know the link it is on. */
  510. if ((p_ccb = l2cu_find_ccb_by_cid (NULL, cid)) == NULL) {
  511. L2CAP_TRACE_WARNING ("L2CAP - no CCB for L2CA_disc_rsp, CID: %d", cid);
  512. return (FALSE);
  513. }
  514. l2c_csm_execute (p_ccb, L2CEVT_L2CA_DISCONNECT_RSP, NULL);
  515. return (TRUE);
  516. }
  517. /*******************************************************************************
  518. **
  519. ** Function L2CA_Ping
  520. **
  521. ** Description Higher layers call this function to send an echo request.
  522. **
  523. ** Returns TRUE if echo request sent, else FALSE.
  524. **
  525. *******************************************************************************/
  526. BOOLEAN L2CA_Ping (BD_ADDR p_bd_addr, tL2CA_ECHO_RSP_CB *p_callback)
  527. {
  528. tL2C_LCB *p_lcb;
  529. L2CAP_TRACE_API ("L2CA_Ping() BDA: %02x-%02x-%02x-%02x-%02x-%02x",
  530. p_bd_addr[0], p_bd_addr[1], p_bd_addr[2], p_bd_addr[3], p_bd_addr[4], p_bd_addr[5]);
  531. /* Fail if we have not established communications with the controller */
  532. if (!BTM_IsDeviceUp()) {
  533. return (FALSE);
  534. }
  535. /* First, see if we already have a link to the remote */
  536. if ((p_lcb = l2cu_find_lcb_by_bd_addr (p_bd_addr, BT_TRANSPORT_BR_EDR)) == NULL) {
  537. /* No link. Get an LCB and start link establishment */
  538. if ((p_lcb = l2cu_allocate_lcb (p_bd_addr, FALSE, BT_TRANSPORT_BR_EDR)) == NULL) {
  539. L2CAP_TRACE_WARNING ("L2CAP - no LCB for L2CA_ping");
  540. return (FALSE);
  541. }
  542. if (l2cu_create_conn(p_lcb, BT_TRANSPORT_BR_EDR) == FALSE) {
  543. return (FALSE);
  544. }
  545. p_lcb->p_echo_rsp_cb = p_callback;
  546. return (TRUE);
  547. }
  548. /* We only allow 1 ping outstanding at a time */
  549. if (p_lcb->p_echo_rsp_cb != NULL) {
  550. L2CAP_TRACE_WARNING ("L2CAP - rejected second L2CA_ping");
  551. return (FALSE);
  552. }
  553. /* Have a link control block. If link is disconnecting, tell user to retry later */
  554. if (p_lcb->link_state == LST_DISCONNECTING) {
  555. L2CAP_TRACE_WARNING ("L2CAP - L2CA_ping rejected - link disconnecting");
  556. return (FALSE);
  557. }
  558. /* Save address of callback */
  559. p_lcb->p_echo_rsp_cb = p_callback;
  560. if (p_lcb->link_state == LST_CONNECTED) {
  561. l2cu_adj_id(p_lcb, L2CAP_ADJ_BRCM_ID); /* Make sure not using Broadcom ID */
  562. l2cu_send_peer_echo_req (p_lcb, NULL, 0);
  563. btu_start_timer (&p_lcb->timer_entry, BTU_TTYPE_L2CAP_LINK, L2CAP_ECHO_RSP_TOUT);
  564. }
  565. return (TRUE);
  566. }
  567. /*******************************************************************************
  568. **
  569. ** Function L2CA_Echo
  570. **
  571. ** Description Higher layers call this function to send an echo request
  572. ** with application-specific data.
  573. **
  574. ** Returns TRUE if echo request sent, else FALSE.
  575. **
  576. *******************************************************************************/
  577. BOOLEAN L2CA_Echo (BD_ADDR p_bd_addr, BT_HDR *p_data, tL2CA_ECHO_DATA_CB *p_callback)
  578. {
  579. tL2C_LCB *p_lcb;
  580. UINT8 *pp;
  581. L2CAP_TRACE_API ("L2CA_Echo() BDA: %08X%04X",
  582. ((p_bd_addr[0] << 24) + (p_bd_addr[1] << 16) + (p_bd_addr[2] << 8) + (p_bd_addr[3])),
  583. ((p_bd_addr[4] << 8) + (p_bd_addr[5])));
  584. /* Fail if we have not established communications with the controller */
  585. if (!BTM_IsDeviceUp()) {
  586. return (FALSE);
  587. }
  588. if ((memcmp(BT_BD_ANY, p_bd_addr, BD_ADDR_LEN) == 0) && (p_data == NULL)) {
  589. /* Only register callback without sending message. */
  590. l2cb.p_echo_data_cb = p_callback;
  591. return TRUE;
  592. }
  593. /* We assume the upper layer will call this function only when the link is established. */
  594. if ((p_lcb = l2cu_find_lcb_by_bd_addr (p_bd_addr, BT_TRANSPORT_BR_EDR)) == NULL) {
  595. L2CAP_TRACE_ERROR ("L2CA_Echo ERROR : link not established");
  596. return FALSE;
  597. }
  598. if (p_lcb->link_state != LST_CONNECTED) {
  599. L2CAP_TRACE_ERROR ("L2CA_Echo ERROR : link is not connected");
  600. return FALSE;
  601. }
  602. /* Save address of callback */
  603. l2cb.p_echo_data_cb = p_callback;
  604. /* Set the pointer to the beginning of the data */
  605. pp = (UINT8 *)(p_data + 1) + p_data->offset;
  606. l2cu_adj_id(p_lcb, L2CAP_ADJ_BRCM_ID); /* Make sure not using Broadcom ID */
  607. l2cu_send_peer_echo_req (p_lcb, pp, p_data->len);
  608. return (TRUE);
  609. }
  610. bool L2CA_GetIdentifiers(uint16_t lcid, uint16_t *rcid, uint16_t *handle)
  611. {
  612. tL2C_CCB *control_block = l2cu_find_ccb_by_cid(NULL, lcid);
  613. if (!control_block) {
  614. return false;
  615. }
  616. if (rcid) {
  617. *rcid = control_block->remote_cid;
  618. }
  619. if (handle) {
  620. *handle = control_block->p_lcb->handle;
  621. }
  622. return true;
  623. }
  624. /*******************************************************************************
  625. **
  626. ** Function L2CA_SetIdleTimeout
  627. **
  628. ** Description Higher layers call this function to set the idle timeout for
  629. ** a connection, or for all future connections. The "idle timeout"
  630. ** is the amount of time that a connection can remain up with
  631. ** no L2CAP channels on it. A timeout of zero means that the
  632. ** connection will be torn down immediately when the last channel
  633. ** is removed. A timeout of 0xFFFF means no timeout. Values are
  634. ** in seconds.
  635. **
  636. ** Returns TRUE if command succeeded, FALSE if failed
  637. **
  638. ** NOTE This timeout takes effect after at least 1 channel has been
  639. ** established and removed. L2CAP maintains its own timer from
  640. ** whan a connection is established till the first channel is
  641. ** set up.
  642. *******************************************************************************/
  643. BOOLEAN L2CA_SetIdleTimeout (UINT16 cid, UINT16 timeout, BOOLEAN is_global)
  644. {
  645. tL2C_CCB *p_ccb;
  646. tL2C_LCB *p_lcb;
  647. if (is_global) {
  648. l2cb.idle_timeout = timeout;
  649. } else {
  650. /* Find the channel control block. We don't know the link it is on. */
  651. if ((p_ccb = l2cu_find_ccb_by_cid (NULL, cid)) == NULL) {
  652. L2CAP_TRACE_WARNING ("L2CAP - no CCB for L2CA_SetIdleTimeout, CID: %d", cid);
  653. return (FALSE);
  654. }
  655. p_lcb = p_ccb->p_lcb;
  656. if ((p_lcb) && (p_lcb->in_use) && (p_lcb->link_state == LST_CONNECTED)) {
  657. p_lcb->idle_timeout = timeout;
  658. } else {
  659. return (FALSE);
  660. }
  661. }
  662. return (TRUE);
  663. }
  664. /*******************************************************************************
  665. **
  666. ** Function L2CA_SetIdleTimeoutByBdAddr
  667. **
  668. ** Description Higher layers call this function to set the idle timeout for
  669. ** a connection. The "idle timeout" is the amount of time that
  670. ** a connection can remain up with no L2CAP channels on it.
  671. ** A timeout of zero means that the connection will be torn
  672. ** down immediately when the last channel is removed.
  673. ** A timeout of 0xFFFF means no timeout. Values are in seconds.
  674. ** A bd_addr is the remote BD address. If bd_addr = BT_BD_ANY,
  675. ** then the idle timeouts for all active l2cap links will be
  676. ** changed.
  677. **
  678. ** Returns TRUE if command succeeded, FALSE if failed
  679. **
  680. ** NOTE This timeout applies to all logical channels active on the
  681. ** ACL link.
  682. *******************************************************************************/
  683. BOOLEAN L2CA_SetIdleTimeoutByBdAddr(BD_ADDR bd_addr, UINT16 timeout, tBT_TRANSPORT transport)
  684. {
  685. tL2C_LCB *p_lcb;
  686. if (memcmp (BT_BD_ANY, bd_addr, BD_ADDR_LEN)) {
  687. p_lcb = l2cu_find_lcb_by_bd_addr( bd_addr, transport);
  688. if ((p_lcb) && (p_lcb->in_use) && (p_lcb->link_state == LST_CONNECTED)) {
  689. p_lcb->idle_timeout = timeout;
  690. if (!p_lcb->ccb_queue.p_first_ccb) {
  691. l2cu_no_dynamic_ccbs (p_lcb);
  692. }
  693. } else {
  694. return FALSE;
  695. }
  696. } else {
  697. int xx;
  698. tL2C_LCB *p_lcb = &l2cb.lcb_pool[0];
  699. for (xx = 0; xx < MAX_L2CAP_LINKS; xx++, p_lcb++) {
  700. if ((p_lcb->in_use) && (p_lcb->link_state == LST_CONNECTED)) {
  701. p_lcb->idle_timeout = timeout;
  702. if (!p_lcb->ccb_queue.p_first_ccb) {
  703. l2cu_no_dynamic_ccbs (p_lcb);
  704. }
  705. }
  706. }
  707. }
  708. return TRUE;
  709. }
  710. /*******************************************************************************
  711. **
  712. ** Function L2CA_SetTraceLevel
  713. **
  714. ** Description This function sets the trace level for L2CAP. If called with
  715. ** a value of 0xFF, it simply reads the current trace level.
  716. **
  717. ** Returns the new (current) trace level
  718. **
  719. *******************************************************************************/
  720. UINT8 L2CA_SetTraceLevel (UINT8 new_level)
  721. {
  722. if (new_level != 0xFF) {
  723. l2cb.l2cap_trace_level = new_level;
  724. }
  725. return (l2cb.l2cap_trace_level);
  726. }
  727. /*******************************************************************************
  728. **
  729. ** Function L2CA_SetDesireRole
  730. **
  731. ** Description This function sets the desire role for L2CAP.
  732. ** If the new role is L2CAP_ROLE_ALLOW_SWITCH, allow switch on
  733. ** HciCreateConnection.
  734. ** If the new role is L2CAP_ROLE_DISALLOW_SWITCH, do not allow switch on
  735. ** HciCreateConnection.
  736. **
  737. ** If the new role is a valid role (HCI_ROLE_MASTER or HCI_ROLE_SLAVE),
  738. ** the desire role is set to the new value. Otherwise, it is not changed.
  739. **
  740. ** Returns the new (current) role
  741. **
  742. *******************************************************************************/
  743. UINT8 L2CA_SetDesireRole (UINT8 new_role)
  744. {
  745. L2CAP_TRACE_API ("L2CA_SetDesireRole() new:x%x, disallow_switch:%d",
  746. new_role, l2cb.disallow_switch);
  747. if (L2CAP_ROLE_CHECK_SWITCH != (L2CAP_ROLE_CHECK_SWITCH & new_role)) {
  748. /* do not process the allow_switch when both bits are set */
  749. if (new_role & L2CAP_ROLE_ALLOW_SWITCH) {
  750. l2cb.disallow_switch = FALSE;
  751. }
  752. if (new_role & L2CAP_ROLE_DISALLOW_SWITCH) {
  753. l2cb.disallow_switch = TRUE;
  754. }
  755. }
  756. if (new_role == HCI_ROLE_MASTER || new_role == HCI_ROLE_SLAVE) {
  757. l2cb.desire_role = new_role;
  758. }
  759. return (l2cb.desire_role);
  760. }
  761. /*******************************************************************************
  762. **
  763. ** Function L2CA_LocalLoopbackReq
  764. **
  765. ** Description This function sets up a CID for local loopback
  766. **
  767. ** Returns CID of 0 if none.
  768. **
  769. *******************************************************************************/
  770. UINT16 L2CA_LocalLoopbackReq (UINT16 psm, UINT16 handle, BD_ADDR p_bd_addr)
  771. {
  772. tL2C_LCB *p_lcb;
  773. tL2C_CCB *p_ccb;
  774. tL2C_RCB *p_rcb;
  775. L2CAP_TRACE_API ("L2CA_LocalLoopbackReq() PSM: %d Handle: 0x%04x", psm, handle);
  776. /* Fail if we have not established communications with the controller */
  777. if (!BTM_IsDeviceUp()) {
  778. L2CAP_TRACE_WARNING ("L2CAP loop req - BTU not ready");
  779. return (0);
  780. }
  781. /* Fail if the PSM is not registered */
  782. if ((p_rcb = l2cu_find_rcb_by_psm (psm)) == NULL) {
  783. L2CAP_TRACE_WARNING ("L2CAP - no RCB for L2CA_conn_req, PSM: %d", psm);
  784. return (0);
  785. }
  786. if ((p_lcb = l2cu_allocate_lcb (p_bd_addr, FALSE, BT_TRANSPORT_BR_EDR)) == NULL) {
  787. L2CAP_TRACE_WARNING ("L2CAP - no LCB for L2CA_conn_req");
  788. return (0);
  789. }
  790. p_lcb->link_state = LST_CONNECTED;
  791. p_lcb->handle = handle;
  792. /* Allocate a channel control block */
  793. if ((p_ccb = l2cu_allocate_ccb (p_lcb, 0)) == NULL) {
  794. L2CAP_TRACE_WARNING ("L2CAP - no CCB for L2CA_conn_req");
  795. return (0);
  796. }
  797. /* Save registration info */
  798. p_ccb->p_rcb = p_rcb;
  799. p_ccb->chnl_state = CST_OPEN;
  800. p_ccb->remote_cid = p_ccb->local_cid;
  801. p_ccb->config_done = CFG_DONE_MASK;
  802. /* Return the local CID as our handle */
  803. return (p_ccb->local_cid);
  804. }
  805. /*******************************************************************************
  806. **
  807. ** Function L2CA_SetAclPriority
  808. **
  809. ** Description Sets the transmission priority for a channel.
  810. ** (For initial implementation only two values are valid.
  811. ** L2CAP_PRIORITY_NORMAL and L2CAP_PRIORITY_HIGH).
  812. **
  813. ** Returns TRUE if a valid channel, else FALSE
  814. **
  815. *******************************************************************************/
  816. BOOLEAN L2CA_SetAclPriority (BD_ADDR bd_addr, UINT8 priority)
  817. {
  818. L2CAP_TRACE_API ("L2CA_SetAclPriority() bdaddr: %02x%02x%02x%02x%04x, priority:%d",
  819. bd_addr[0], bd_addr[1], bd_addr[2],
  820. bd_addr[3], (bd_addr[4] << 8) + bd_addr[5], priority);
  821. return (l2cu_set_acl_priority(bd_addr, priority, FALSE));
  822. }
  823. /*******************************************************************************
  824. **
  825. ** Function L2CA_FlowControl
  826. **
  827. ** Description Higher layers call this function to flow control a channel.
  828. **
  829. ** data_enabled - TRUE data flows, FALSE data is stopped
  830. **
  831. ** Returns TRUE if valid channel, else FALSE
  832. **
  833. *******************************************************************************/
  834. BOOLEAN L2CA_FlowControl (UINT16 cid, BOOLEAN data_enabled)
  835. {
  836. tL2C_CCB *p_ccb;
  837. BOOLEAN on_off = !data_enabled;
  838. L2CAP_TRACE_API ("L2CA_FlowControl(%d) CID: 0x%04x", on_off, cid);
  839. /* Find the channel control block. We don't know the link it is on. */
  840. if ((p_ccb = l2cu_find_ccb_by_cid (NULL, cid)) == NULL) {
  841. L2CAP_TRACE_WARNING ("L2CAP - no CCB for L2CA_FlowControl, CID: 0x%04x data_enabled: %d", cid, data_enabled);
  842. return (FALSE);
  843. }
  844. if (p_ccb->peer_cfg.fcr.mode != L2CAP_FCR_ERTM_MODE) {
  845. L2CAP_TRACE_EVENT ("L2CA_FlowControl() invalid mode:%d", p_ccb->peer_cfg.fcr.mode);
  846. return (FALSE);
  847. }
  848. if (p_ccb->fcrb.local_busy != on_off) {
  849. p_ccb->fcrb.local_busy = on_off;
  850. if ( (p_ccb->chnl_state == CST_OPEN) && (!p_ccb->fcrb.wait_ack) ) {
  851. if (on_off) {
  852. l2c_fcr_send_S_frame (p_ccb, L2CAP_FCR_SUP_RNR, 0);
  853. } else {
  854. l2c_fcr_send_S_frame (p_ccb, L2CAP_FCR_SUP_RR, L2CAP_FCR_P_BIT);
  855. }
  856. }
  857. }
  858. return (TRUE);
  859. }
  860. /*******************************************************************************
  861. **
  862. ** Function L2CA_SendTestSFrame
  863. **
  864. ** Description Higher layers call this function to send a test S-frame.
  865. **
  866. ** Returns TRUE if valid Channel, else FALSE
  867. **
  868. *******************************************************************************/
  869. BOOLEAN L2CA_SendTestSFrame (UINT16 cid, UINT8 sup_type, UINT8 back_track)
  870. {
  871. tL2C_CCB *p_ccb;
  872. L2CAP_TRACE_API ("L2CA_SendTestSFrame() CID: 0x%04x Type: 0x%02x back_track: %u", cid, sup_type, back_track);
  873. /* Find the channel control block. We don't know the link it is on. */
  874. if ((p_ccb = l2cu_find_ccb_by_cid (NULL, cid)) == NULL) {
  875. L2CAP_TRACE_WARNING ("L2CAP - no CCB for L2CA_SendTestSFrame, CID: %d", cid);
  876. return (FALSE);
  877. }
  878. if ( (p_ccb->chnl_state != CST_OPEN) || (p_ccb->peer_cfg.fcr.mode != L2CAP_FCR_ERTM_MODE) ) {
  879. return (FALSE);
  880. }
  881. p_ccb->fcrb.next_seq_expected -= back_track;
  882. l2c_fcr_send_S_frame (p_ccb, (UINT16)(sup_type & 3), (UINT16)(sup_type & (L2CAP_FCR_P_BIT | L2CAP_FCR_F_BIT)));
  883. return (TRUE);
  884. }
  885. /*******************************************************************************
  886. **
  887. ** Function L2CA_SetTxPriority
  888. **
  889. ** Description Sets the transmission priority for a channel.
  890. **
  891. ** Returns TRUE if a valid channel, else FALSE
  892. **
  893. *******************************************************************************/
  894. BOOLEAN L2CA_SetTxPriority (UINT16 cid, tL2CAP_CHNL_PRIORITY priority)
  895. {
  896. tL2C_CCB *p_ccb;
  897. L2CAP_TRACE_API ("L2CA_SetTxPriority() CID: 0x%04x, priority:%d", cid, priority);
  898. /* Find the channel control block. We don't know the link it is on. */
  899. if ((p_ccb = l2cu_find_ccb_by_cid (NULL, cid)) == NULL) {
  900. L2CAP_TRACE_WARNING ("L2CAP - no CCB for L2CA_SetTxPriority, CID: %d", cid);
  901. return (FALSE);
  902. }
  903. /* it will update the order of CCB in LCB by priority and update round robin service variables */
  904. l2cu_change_pri_ccb (p_ccb, priority);
  905. return (TRUE);
  906. }
  907. /*******************************************************************************
  908. **
  909. ** Function L2CA_SetChnlDataRate
  910. **
  911. ** Description Sets the tx/rx data rate for a channel.
  912. **
  913. ** Returns TRUE if a valid channel, else FALSE
  914. **
  915. *******************************************************************************/
  916. BOOLEAN L2CA_SetChnlDataRate (UINT16 cid, tL2CAP_CHNL_DATA_RATE tx, tL2CAP_CHNL_DATA_RATE rx)
  917. {
  918. tL2C_CCB *p_ccb;
  919. L2CAP_TRACE_API ("L2CA_SetChnlDataRate() CID: 0x%04x, tx:%d, rx:%d", cid, tx, rx);
  920. /* Find the channel control block. We don't know the link it is on. */
  921. if ((p_ccb = l2cu_find_ccb_by_cid (NULL, cid)) == NULL) {
  922. L2CAP_TRACE_WARNING ("L2CAP - no CCB for L2CA_SetChnlDataRate, CID: %d", cid);
  923. return (FALSE);
  924. }
  925. p_ccb->tx_data_rate = tx;
  926. p_ccb->rx_data_rate = rx;
  927. /* Adjust channel buffer allocation */
  928. l2c_link_adjust_chnl_allocation ();
  929. return (TRUE);
  930. }
  931. /*******************************************************************************
  932. **
  933. ** Function L2CA_SetFlushTimeout
  934. **
  935. ** Description This function set the automatic flush time out in Baseband
  936. ** for ACL-U packets.
  937. ** BdAddr : the remote BD address of ACL link. If it is BT_DB_ANY
  938. ** then the flush time out will be applied to all ACL link.
  939. ** FlushTimeout: flush time out in ms
  940. ** 0x0000 : No automatic flush
  941. ** L2CAP_NO_RETRANSMISSION : No retransmission
  942. ** 0x0002 - 0xFFFE : flush time out, if (flush_tout*8)+3/5)
  943. ** <= HCI_MAX_AUTO_FLUSH_TOUT (in 625us slot).
  944. ** Otherwise, return FALSE.
  945. ** L2CAP_NO_AUTOMATIC_FLUSH : No automatic flush
  946. **
  947. ** Returns TRUE if command succeeded, FALSE if failed
  948. **
  949. ** NOTE This flush timeout applies to all logical channels active on the
  950. ** ACL link.
  951. *******************************************************************************/
  952. BOOLEAN L2CA_SetFlushTimeout (BD_ADDR bd_addr, UINT16 flush_tout)
  953. {
  954. tL2C_LCB *p_lcb;
  955. UINT16 hci_flush_to;
  956. UINT32 temp;
  957. /* no automatic flush (infinite timeout) */
  958. if (flush_tout == 0x0000) {
  959. hci_flush_to = flush_tout;
  960. flush_tout = L2CAP_NO_AUTOMATIC_FLUSH;
  961. }
  962. /* no retransmission */
  963. else if (flush_tout == L2CAP_NO_RETRANSMISSION) {
  964. /* not mandatory range for controller */
  965. /* Packet is flushed before getting any ACK/NACK */
  966. /* To do this, flush timeout should be 1 baseband slot */
  967. hci_flush_to = flush_tout;
  968. }
  969. /* no automatic flush (infinite timeout) */
  970. else if (flush_tout == L2CAP_NO_AUTOMATIC_FLUSH) {
  971. hci_flush_to = 0x0000;
  972. } else {
  973. /* convert L2CAP flush_to to 0.625 ms units, with round */
  974. temp = (((UINT32)flush_tout * 8) + 3) / 5;
  975. /* if L2CAP flush_to within range of HCI, set HCI flush timeout */
  976. if (temp > HCI_MAX_AUTO_FLUSH_TOUT) {
  977. L2CAP_TRACE_WARNING("WARNING L2CA_SetFlushTimeout timeout(0x%x) is out of range", flush_tout);
  978. return FALSE;
  979. } else {
  980. hci_flush_to = (UINT16)temp;
  981. }
  982. }
  983. if (memcmp (BT_BD_ANY, bd_addr, BD_ADDR_LEN)) {
  984. p_lcb = l2cu_find_lcb_by_bd_addr (bd_addr, BT_TRANSPORT_BR_EDR);
  985. if ((p_lcb) && (p_lcb->in_use) && (p_lcb->link_state == LST_CONNECTED)) {
  986. if (p_lcb->link_flush_tout != flush_tout) {
  987. p_lcb->link_flush_tout = flush_tout;
  988. L2CAP_TRACE_API ("L2CA_SetFlushTimeout 0x%04x ms for bd_addr [...;%02x%02x%02x]",
  989. flush_tout, bd_addr[3], bd_addr[4], bd_addr[5]);
  990. if (!btsnd_hcic_write_auto_flush_tout (p_lcb->handle, hci_flush_to)) {
  991. return (FALSE);
  992. }
  993. }
  994. } else {
  995. L2CAP_TRACE_WARNING ("WARNING L2CA_SetFlushTimeout No lcb for bd_addr [...;%02x%02x%02x]",
  996. bd_addr[3], bd_addr[4], bd_addr[5]);
  997. return (FALSE);
  998. }
  999. } else {
  1000. int xx;
  1001. p_lcb = &l2cb.lcb_pool[0];
  1002. for (xx = 0; xx < MAX_L2CAP_LINKS; xx++, p_lcb++) {
  1003. if ((p_lcb->in_use) && (p_lcb->link_state == LST_CONNECTED)) {
  1004. if (p_lcb->link_flush_tout != flush_tout) {
  1005. p_lcb->link_flush_tout = flush_tout;
  1006. L2CAP_TRACE_API ("L2CA_SetFlushTimeout 0x%04x ms for bd_addr [...;%02x%02x%02x]",
  1007. flush_tout, p_lcb->remote_bd_addr[3],
  1008. p_lcb->remote_bd_addr[4], p_lcb->remote_bd_addr[5]);
  1009. if (!btsnd_hcic_write_auto_flush_tout(p_lcb->handle, hci_flush_to)) {
  1010. return (FALSE);
  1011. }
  1012. }
  1013. }
  1014. }
  1015. }
  1016. return (TRUE);
  1017. }
  1018. /*******************************************************************************
  1019. **
  1020. ** Function L2CA_GetPeerFeatures
  1021. **
  1022. ** Description Get a peers features and fixed channel map
  1023. **
  1024. ** Parameters: BD address of the peer
  1025. ** Pointers to features and channel mask storage area
  1026. **
  1027. ** Return value: TRUE if peer is connected
  1028. **
  1029. *******************************************************************************/
  1030. BOOLEAN L2CA_GetPeerFeatures (BD_ADDR bd_addr, UINT32 *p_ext_feat, UINT8 *p_chnl_mask)
  1031. {
  1032. tL2C_LCB *p_lcb;
  1033. /* We must already have a link to the remote */
  1034. if ((p_lcb = l2cu_find_lcb_by_bd_addr (bd_addr, BT_TRANSPORT_BR_EDR)) == NULL) {
  1035. L2CAP_TRACE_WARNING ("L2CA_GetPeerFeatures() No BDA: %08x%04x",
  1036. (bd_addr[0] << 24) + (bd_addr[1] << 16) + (bd_addr[2] << 8) + bd_addr[3],
  1037. (bd_addr[4] << 8) + bd_addr[5]);
  1038. return (FALSE);
  1039. }
  1040. L2CAP_TRACE_API ("L2CA_GetPeerFeatures() BDA: %08x%04x ExtFea: 0x%08x Chnl_Mask[0]: 0x%02x",
  1041. (bd_addr[0] << 24) + (bd_addr[1] << 16) + (bd_addr[2] << 8) + bd_addr[3],
  1042. (bd_addr[4] << 8) + bd_addr[5], p_lcb->peer_ext_fea, p_lcb->peer_chnl_mask[0]);
  1043. *p_ext_feat = p_lcb->peer_ext_fea;
  1044. memcpy (p_chnl_mask, p_lcb->peer_chnl_mask, L2CAP_FIXED_CHNL_ARRAY_SIZE);
  1045. return (TRUE);
  1046. }
  1047. /*******************************************************************************
  1048. **
  1049. ** Function L2CA_GetBDAddrbyHandle
  1050. **
  1051. ** Description Get BD address for the given HCI handle
  1052. **
  1053. ** Parameters: HCI handle
  1054. ** BD address of the peer
  1055. **
  1056. ** Return value: TRUE if found lcb for the given handle, FALSE otherwise
  1057. **
  1058. *******************************************************************************/
  1059. BOOLEAN L2CA_GetBDAddrbyHandle (UINT16 handle, BD_ADDR bd_addr)
  1060. {
  1061. tL2C_LCB *p_lcb = NULL;
  1062. BOOLEAN found_dev = FALSE;
  1063. p_lcb = l2cu_find_lcb_by_handle (handle);
  1064. if (p_lcb) {
  1065. found_dev = TRUE;
  1066. memcpy (bd_addr, p_lcb->remote_bd_addr, BD_ADDR_LEN);
  1067. }
  1068. return found_dev;
  1069. }
  1070. /*******************************************************************************
  1071. **
  1072. ** Function L2CA_GetChnlFcrMode
  1073. **
  1074. ** Description Get the channel FCR mode
  1075. **
  1076. ** Parameters: Local CID
  1077. **
  1078. ** Return value: Channel mode
  1079. **
  1080. *******************************************************************************/
  1081. UINT8 L2CA_GetChnlFcrMode (UINT16 lcid)
  1082. {
  1083. tL2C_CCB *p_ccb = l2cu_find_ccb_by_cid (NULL, lcid);
  1084. if (p_ccb) {
  1085. L2CAP_TRACE_API ("L2CA_GetChnlFcrMode() returns mode %d", p_ccb->peer_cfg.fcr.mode);
  1086. return (p_ccb->peer_cfg.fcr.mode);
  1087. }
  1088. L2CAP_TRACE_API ("L2CA_GetChnlFcrMode() returns mode L2CAP_FCR_BASIC_MODE");
  1089. return (L2CAP_FCR_BASIC_MODE);
  1090. }
  1091. #if (L2CAP_NUM_FIXED_CHNLS > 0)
  1092. /*******************************************************************************
  1093. **
  1094. ** Function L2CA_RegisterFixedChannel
  1095. **
  1096. ** Description Register a fixed channel.
  1097. **
  1098. ** Parameters: Fixed Channel #
  1099. ** Channel Callbacks and config
  1100. **
  1101. ** Return value: -
  1102. **
  1103. *******************************************************************************/
  1104. BOOLEAN L2CA_RegisterFixedChannel (UINT16 fixed_cid, tL2CAP_FIXED_CHNL_REG *p_freg)
  1105. {
  1106. if ( (fixed_cid < L2CAP_FIRST_FIXED_CHNL) || (fixed_cid > L2CAP_LAST_FIXED_CHNL) ) {
  1107. L2CAP_TRACE_ERROR ("L2CA_RegisterFixedChannel() Invalid CID: 0x%04x", fixed_cid);
  1108. return (FALSE);
  1109. }
  1110. l2cb.fixed_reg[fixed_cid - L2CAP_FIRST_FIXED_CHNL] = *p_freg;
  1111. return (TRUE);
  1112. }
  1113. /*******************************************************************************
  1114. **
  1115. ** Function L2CA_ConnectFixedChnl
  1116. **
  1117. ** Description Connect an fixed signalling channel to a remote device.
  1118. **
  1119. ** Parameters: Fixed CID
  1120. ** BD Address of remote
  1121. **
  1122. ** Return value: TRUE if connection started
  1123. **
  1124. *******************************************************************************/
  1125. BOOLEAN L2CA_ConnectFixedChnl (UINT16 fixed_cid, BD_ADDR rem_bda)
  1126. {
  1127. tL2C_LCB *p_lcb;
  1128. tBT_TRANSPORT transport = BT_TRANSPORT_BR_EDR;
  1129. L2CAP_TRACE_API ("%s() CID: 0x%04x BDA: %08x%04x", __func__, fixed_cid,
  1130. (rem_bda[0] << 24) + (rem_bda[1] << 16) + (rem_bda[2] << 8) + rem_bda[3], (rem_bda[4] << 8) + rem_bda[5]);
  1131. // Check CID is valid and registered
  1132. if ( (fixed_cid < L2CAP_FIRST_FIXED_CHNL) || (fixed_cid > L2CAP_LAST_FIXED_CHNL)
  1133. || (l2cb.fixed_reg[fixed_cid - L2CAP_FIRST_FIXED_CHNL].pL2CA_FixedData_Cb == NULL) ) {
  1134. L2CAP_TRACE_ERROR ("%s() Invalid CID: 0x%04x", __func__, fixed_cid);
  1135. return (FALSE);
  1136. }
  1137. // Fail if BT is not yet up
  1138. if (!BTM_IsDeviceUp()) {
  1139. L2CAP_TRACE_WARNING ("%s(0x%04x) - BTU not ready", __func__, fixed_cid);
  1140. return (FALSE);
  1141. }
  1142. #if BLE_INCLUDED == TRUE
  1143. if (fixed_cid >= L2CAP_ATT_CID && fixed_cid <= L2CAP_SMP_CID) {
  1144. transport = BT_TRANSPORT_LE;
  1145. }
  1146. #endif
  1147. tL2C_BLE_FIXED_CHNLS_MASK peer_channel_mask;
  1148. // If we already have a link to the remote, check if it supports that CID
  1149. if ((p_lcb = l2cu_find_lcb_by_bd_addr (rem_bda, transport)) != NULL) {
  1150. // Fixed channels are mandatory on LE transports so ignore the received
  1151. // channel mask and use the locally cached LE channel mask.
  1152. #if BLE_INCLUDED == TRUE
  1153. if (transport == BT_TRANSPORT_LE) {
  1154. peer_channel_mask = l2cb.l2c_ble_fixed_chnls_mask;
  1155. } else
  1156. #endif
  1157. peer_channel_mask = p_lcb->peer_chnl_mask[0];
  1158. // Check for supported channel
  1159. if (!(peer_channel_mask & (1 << fixed_cid))) {
  1160. L2CAP_TRACE_EVENT ("%s() CID:0x%04x BDA: %08x%04x not supported", __func__,
  1161. fixed_cid, (rem_bda[0] << 24) + (rem_bda[1] << 16) + (rem_bda[2] << 8) + rem_bda[3],
  1162. (rem_bda[4] << 8) + rem_bda[5]);
  1163. return FALSE;
  1164. }
  1165. // Get a CCB and link the lcb to it
  1166. if (!l2cu_initialize_fixed_ccb (p_lcb, fixed_cid,
  1167. &l2cb.fixed_reg[fixed_cid - L2CAP_FIRST_FIXED_CHNL].fixed_chnl_opts)) {
  1168. L2CAP_TRACE_WARNING ("%s(0x%04x) - LCB but no CCB", __func__, fixed_cid);
  1169. return FALSE;
  1170. }
  1171. // racing with disconnecting, queue the connection request
  1172. if (p_lcb->link_state == LST_DISCONNECTING) {
  1173. L2CAP_TRACE_DEBUG ("%s() - link disconnecting: RETRY LATER", __func__);
  1174. /* Save ccb so it can be started after disconnect is finished */
  1175. p_lcb->p_pending_ccb = p_lcb->p_fixed_ccbs[fixed_cid - L2CAP_FIRST_FIXED_CHNL];
  1176. return TRUE;
  1177. }
  1178. #if BLE_INCLUDED == TRUE
  1179. (*l2cb.fixed_reg[fixed_cid - L2CAP_FIRST_FIXED_CHNL].pL2CA_FixedConn_Cb)
  1180. (fixed_cid, p_lcb->remote_bd_addr, TRUE, 0, p_lcb->transport);
  1181. #else
  1182. (*l2cb.fixed_reg[fixed_cid - L2CAP_FIRST_FIXED_CHNL].pL2CA_FixedConn_Cb)
  1183. (fixed_cid, p_lcb->remote_bd_addr, TRUE, 0, BT_TRANSPORT_BR_EDR);
  1184. #endif
  1185. return TRUE;
  1186. }
  1187. // No link. Get an LCB and start link establishment
  1188. if ((p_lcb = l2cu_allocate_lcb (rem_bda, FALSE, transport)) == NULL) {
  1189. L2CAP_TRACE_WARNING ("%s(0x%04x) - no LCB", __func__, fixed_cid);
  1190. return FALSE;
  1191. }
  1192. // Get a CCB and link the lcb to it
  1193. if (!l2cu_initialize_fixed_ccb (p_lcb, fixed_cid,
  1194. &l2cb.fixed_reg[fixed_cid - L2CAP_FIRST_FIXED_CHNL].fixed_chnl_opts)) {
  1195. p_lcb->disc_reason = L2CAP_CONN_NO_RESOURCES;
  1196. L2CAP_TRACE_WARNING ("%s(0x%04x) - no CCB", __func__, fixed_cid);
  1197. l2cu_release_lcb (p_lcb);
  1198. return FALSE;
  1199. }
  1200. if (!l2cu_create_conn(p_lcb, transport)) {
  1201. L2CAP_TRACE_WARNING ("%s() - create_conn failed", __func__);
  1202. l2cu_release_lcb (p_lcb);
  1203. return FALSE;
  1204. }
  1205. return TRUE;
  1206. }
  1207. /*******************************************************************************
  1208. **
  1209. ** Function L2CA_SendFixedChnlData
  1210. **
  1211. ** Description Write data on a fixed channel.
  1212. **
  1213. ** Parameters: Fixed CID
  1214. ** BD Address of remote
  1215. ** Pointer to buffer of type BT_HDR
  1216. **
  1217. ** Return value L2CAP_DW_SUCCESS, if data accepted
  1218. ** L2CAP_DW_FAILED, if error
  1219. **
  1220. *******************************************************************************/
  1221. UINT16 L2CA_SendFixedChnlData (UINT16 fixed_cid, BD_ADDR rem_bda, BT_HDR *p_buf)
  1222. {
  1223. tL2C_LCB *p_lcb;
  1224. tBT_TRANSPORT transport = BT_TRANSPORT_BR_EDR;
  1225. L2CAP_TRACE_API ("L2CA_SendFixedChnlData() CID: 0x%04x BDA: %08x%04x", fixed_cid,
  1226. (rem_bda[0] << 24) + (rem_bda[1] << 16) + (rem_bda[2] << 8) + rem_bda[3], (rem_bda[4] << 8) + rem_bda[5]);
  1227. #if BLE_INCLUDED == TRUE
  1228. if (fixed_cid >= L2CAP_ATT_CID && fixed_cid <= L2CAP_SMP_CID) {
  1229. transport = BT_TRANSPORT_LE;
  1230. }
  1231. #endif
  1232. // Check CID is valid and registered
  1233. if ( (fixed_cid < L2CAP_FIRST_FIXED_CHNL) || (fixed_cid > L2CAP_LAST_FIXED_CHNL)
  1234. || (l2cb.fixed_reg[fixed_cid - L2CAP_FIRST_FIXED_CHNL].pL2CA_FixedData_Cb == NULL) ) {
  1235. L2CAP_TRACE_ERROR ("L2CA_SendFixedChnlData() Invalid CID: 0x%04x", fixed_cid);
  1236. GKI_freebuf (p_buf);
  1237. return (L2CAP_DW_FAILED);
  1238. }
  1239. // Fail if BT is not yet up
  1240. if (!BTM_IsDeviceUp()) {
  1241. L2CAP_TRACE_WARNING ("L2CA_SendFixedChnlData(0x%04x) - BTU not ready", fixed_cid);
  1242. GKI_freebuf (p_buf);
  1243. return (L2CAP_DW_FAILED);
  1244. }
  1245. // We need to have a link up
  1246. if ((p_lcb = l2cu_find_lcb_by_bd_addr (rem_bda, transport)) == NULL ||
  1247. /* if link is disconnecting, also report data sending failure */
  1248. p_lcb->link_state == LST_DISCONNECTING) {
  1249. L2CAP_TRACE_WARNING ("L2CA_SendFixedChnlData(0x%04x) - no LCB", fixed_cid);
  1250. GKI_freebuf (p_buf);
  1251. return (L2CAP_DW_FAILED);
  1252. }
  1253. tL2C_BLE_FIXED_CHNLS_MASK peer_channel_mask;
  1254. // Select peer channels mask to use depending on transport
  1255. #if BLE_INCLUDED == TRUE
  1256. if (transport == BT_TRANSPORT_LE) {
  1257. peer_channel_mask = l2cb.l2c_ble_fixed_chnls_mask;
  1258. } else
  1259. #endif
  1260. peer_channel_mask = p_lcb->peer_chnl_mask[0];
  1261. if ((peer_channel_mask & (1 << fixed_cid)) == 0) {
  1262. L2CAP_TRACE_WARNING ("L2CA_SendFixedChnlData() - peer does not support fixed chnl: 0x%04x", fixed_cid);
  1263. GKI_freebuf (p_buf);
  1264. return (L2CAP_DW_FAILED);
  1265. }
  1266. p_buf->event = 0;
  1267. p_buf->layer_specific = L2CAP_FLUSHABLE_CH_BASED;
  1268. if (!p_lcb->p_fixed_ccbs[fixed_cid - L2CAP_FIRST_FIXED_CHNL]) {
  1269. if (!l2cu_initialize_fixed_ccb (p_lcb, fixed_cid, &l2cb.fixed_reg[fixed_cid - L2CAP_FIRST_FIXED_CHNL].fixed_chnl_opts)) {
  1270. L2CAP_TRACE_WARNING ("L2CA_SendFixedChnlData() - no CCB for chnl: 0x%4x", fixed_cid);
  1271. GKI_freebuf (p_buf);
  1272. return (L2CAP_DW_FAILED);
  1273. }
  1274. }
  1275. // If already congested, do not accept any more packets
  1276. if (p_lcb->p_fixed_ccbs[fixed_cid - L2CAP_FIRST_FIXED_CHNL]->cong_sent) {
  1277. L2CAP_TRACE_ERROR ("L2CAP - CID: 0x%04x cannot send, already congested \
  1278. xmit_hold_q.count: %u buff_quota: %u", fixed_cid,
  1279. GKI_queue_length(&p_lcb->p_fixed_ccbs[fixed_cid - L2CAP_FIRST_FIXED_CHNL]->xmit_hold_q),
  1280. p_lcb->p_fixed_ccbs[fixed_cid - L2CAP_FIRST_FIXED_CHNL]->buff_quota);
  1281. GKI_freebuf (p_buf);
  1282. return (L2CAP_DW_FAILED);
  1283. }
  1284. l2c_enqueue_peer_data (p_lcb->p_fixed_ccbs[fixed_cid - L2CAP_FIRST_FIXED_CHNL], p_buf);
  1285. l2c_link_check_send_pkts (p_lcb, NULL, NULL);
  1286. // If there is no dynamic CCB on the link, restart the idle timer each time something is sent
  1287. if (p_lcb->in_use && p_lcb->link_state == LST_CONNECTED && !p_lcb->ccb_queue.p_first_ccb) {
  1288. l2cu_no_dynamic_ccbs (p_lcb);
  1289. }
  1290. if (p_lcb->p_fixed_ccbs[fixed_cid - L2CAP_FIRST_FIXED_CHNL]->cong_sent) {
  1291. return (L2CAP_DW_CONGESTED);
  1292. }
  1293. return (L2CAP_DW_SUCCESS);
  1294. }
  1295. /*******************************************************************************
  1296. **
  1297. ** Function L2CA_RemoveFixedChnl
  1298. **
  1299. ** Description Remove a fixed channel to a remote device.
  1300. **
  1301. ** Parameters: Fixed CID
  1302. ** BD Address of remote
  1303. ** Idle timeout to use (or 0xFFFF if don't care)
  1304. **
  1305. ** Return value: TRUE if channel removed
  1306. **
  1307. *******************************************************************************/
  1308. BOOLEAN L2CA_RemoveFixedChnl (UINT16 fixed_cid, BD_ADDR rem_bda)
  1309. {
  1310. tL2C_LCB *p_lcb;
  1311. tL2C_CCB *p_ccb;
  1312. tBT_TRANSPORT transport = BT_TRANSPORT_BR_EDR;
  1313. /* Check CID is valid and registered */
  1314. if ( (fixed_cid < L2CAP_FIRST_FIXED_CHNL) || (fixed_cid > L2CAP_LAST_FIXED_CHNL)
  1315. || (l2cb.fixed_reg[fixed_cid - L2CAP_FIRST_FIXED_CHNL].pL2CA_FixedData_Cb == NULL) ) {
  1316. L2CAP_TRACE_ERROR ("L2CA_RemoveFixedChnl() Invalid CID: 0x%04x", fixed_cid);
  1317. return (FALSE);
  1318. }
  1319. #if BLE_INCLUDED == TRUE
  1320. if (fixed_cid >= L2CAP_ATT_CID && fixed_cid <= L2CAP_SMP_CID) {
  1321. transport = BT_TRANSPORT_LE;
  1322. }
  1323. #endif
  1324. /* Is a fixed channel connected to the remote BDA ?*/
  1325. p_lcb = l2cu_find_lcb_by_bd_addr (rem_bda, transport);
  1326. if ( ((p_lcb) == NULL) || (!p_lcb->p_fixed_ccbs[fixed_cid - L2CAP_FIRST_FIXED_CHNL]) ) {
  1327. L2CAP_TRACE_WARNING ("L2CA_RemoveFixedChnl() CID: 0x%04x BDA: %08x%04x not connected", fixed_cid,
  1328. (rem_bda[0] << 24) + (rem_bda[1] << 16) + (rem_bda[2] << 8) + rem_bda[3], (rem_bda[4] << 8) + rem_bda[5]);
  1329. return (FALSE);
  1330. }
  1331. L2CAP_TRACE_API ("L2CA_RemoveFixedChnl() CID: 0x%04x BDA: %08x%04x", fixed_cid,
  1332. (rem_bda[0] << 24) + (rem_bda[1] << 16) + (rem_bda[2] << 8) + rem_bda[3], (rem_bda[4] << 8) + rem_bda[5]);
  1333. /* Release the CCB, starting an inactivity timeout on the LCB if no other CCBs exist */
  1334. p_ccb = p_lcb->p_fixed_ccbs[fixed_cid - L2CAP_FIRST_FIXED_CHNL];
  1335. p_lcb->p_fixed_ccbs[fixed_cid - L2CAP_FIRST_FIXED_CHNL] = NULL;
  1336. p_lcb->disc_reason = HCI_ERR_CONN_CAUSE_LOCAL_HOST;
  1337. #if BLE_INCLUDED == TRUE
  1338. // Retain the link for a few more seconds after SMP pairing is done, since the Android
  1339. // platform always does service discovery after pairing is complete. This will avoid
  1340. // the link down (pairing is complete) and an immediate re-connection for service
  1341. // discovery.
  1342. // Some devices do not do auto advertising when link is dropped, thus fail the second
  1343. // connection and service discovery.
  1344. if ((fixed_cid == L2CAP_ATT_CID ) && !p_lcb->ccb_queue.p_first_ccb) {
  1345. p_lcb->idle_timeout = 0;
  1346. }
  1347. #endif
  1348. l2cu_release_ccb (p_ccb);
  1349. return (TRUE);
  1350. }
  1351. /*******************************************************************************
  1352. **
  1353. ** Function L2CA_SetFixedChannelTout
  1354. **
  1355. ** Description Higher layers call this function to set the idle timeout for
  1356. ** a fixed channel. The "idle timeout" is the amount of time that
  1357. ** a connection can remain up with no L2CAP channels on it.
  1358. ** A timeout of zero means that the connection will be torn
  1359. ** down immediately when the last channel is removed.
  1360. ** A timeout of 0xFFFF means no timeout. Values are in seconds.
  1361. ** A bd_addr is the remote BD address. If bd_addr = BT_BD_ANY,
  1362. ** then the idle timeouts for all active l2cap links will be
  1363. ** changed.
  1364. **
  1365. ** Returns TRUE if command succeeded, FALSE if failed
  1366. **
  1367. *******************************************************************************/
  1368. BOOLEAN L2CA_SetFixedChannelTout (BD_ADDR rem_bda, UINT16 fixed_cid, UINT16 idle_tout)
  1369. {
  1370. tL2C_LCB *p_lcb;
  1371. tBT_TRANSPORT transport = BT_TRANSPORT_BR_EDR;
  1372. #if BLE_INCLUDED == TRUE
  1373. if (fixed_cid >= L2CAP_ATT_CID && fixed_cid <= L2CAP_SMP_CID) {
  1374. transport = BT_TRANSPORT_LE;
  1375. }
  1376. #endif
  1377. /* Is a fixed channel connected to the remote BDA ?*/
  1378. p_lcb = l2cu_find_lcb_by_bd_addr (rem_bda, transport);
  1379. if ( ((p_lcb) == NULL) || (!p_lcb->p_fixed_ccbs[fixed_cid - L2CAP_FIRST_FIXED_CHNL]) ) {
  1380. L2CAP_TRACE_WARNING ("L2CA_SetFixedChannelTout() CID: 0x%04x BDA: %08x%04x not connected", fixed_cid,
  1381. (rem_bda[0] << 24) + (rem_bda[1] << 16) + (rem_bda[2] << 8) + rem_bda[3], (rem_bda[4] << 8) + rem_bda[5]);
  1382. return (FALSE);
  1383. }
  1384. p_lcb->p_fixed_ccbs[fixed_cid - L2CAP_FIRST_FIXED_CHNL]->fixed_chnl_idle_tout = idle_tout;
  1385. if (p_lcb->in_use && p_lcb->link_state == LST_CONNECTED && !p_lcb->ccb_queue.p_first_ccb) {
  1386. /* If there are no dynamic CCBs, (re)start the idle timer in case we changed it */
  1387. l2cu_no_dynamic_ccbs (p_lcb);
  1388. }
  1389. return TRUE;
  1390. }
  1391. #endif /* #if (L2CAP_NUM_FIXED_CHNLS > 0) */
  1392. /*******************************************************************************
  1393. **
  1394. ** Function L2CA_GetCurrentConfig
  1395. **
  1396. ** Description This function returns configurations of L2CAP channel
  1397. ** pp_our_cfg : pointer of our saved configuration options
  1398. ** p_our_cfg_bits : valid config in bitmap
  1399. ** pp_peer_cfg: pointer of peer's saved configuration options
  1400. ** p_peer_cfg_bits : valid config in bitmap
  1401. **
  1402. ** Returns TRUE if successful
  1403. **
  1404. *******************************************************************************/
  1405. BOOLEAN L2CA_GetCurrentConfig (UINT16 lcid,
  1406. tL2CAP_CFG_INFO **pp_our_cfg, tL2CAP_CH_CFG_BITS *p_our_cfg_bits,
  1407. tL2CAP_CFG_INFO **pp_peer_cfg, tL2CAP_CH_CFG_BITS *p_peer_cfg_bits)
  1408. {
  1409. tL2C_CCB *p_ccb;
  1410. L2CAP_TRACE_API ("L2CA_GetCurrentConfig() CID: 0x%04x", lcid);
  1411. p_ccb = l2cu_find_ccb_by_cid(NULL, lcid);
  1412. if (p_ccb) {
  1413. *pp_our_cfg = &(p_ccb->our_cfg);
  1414. /* convert valid config items into bitmap */
  1415. *p_our_cfg_bits = 0;
  1416. if (p_ccb->our_cfg.mtu_present) {
  1417. *p_our_cfg_bits |= L2CAP_CH_CFG_MASK_MTU;
  1418. }
  1419. if (p_ccb->our_cfg.qos_present) {
  1420. *p_our_cfg_bits |= L2CAP_CH_CFG_MASK_QOS;
  1421. }
  1422. if (p_ccb->our_cfg.flush_to_present) {
  1423. *p_our_cfg_bits |= L2CAP_CH_CFG_MASK_FLUSH_TO;
  1424. }
  1425. if (p_ccb->our_cfg.fcr_present) {
  1426. *p_our_cfg_bits |= L2CAP_CH_CFG_MASK_FCR;
  1427. }
  1428. if (p_ccb->our_cfg.fcs_present) {
  1429. *p_our_cfg_bits |= L2CAP_CH_CFG_MASK_FCS;
  1430. }
  1431. if (p_ccb->our_cfg.ext_flow_spec_present) {
  1432. *p_our_cfg_bits |= L2CAP_CH_CFG_MASK_EXT_FLOW_SPEC;
  1433. }
  1434. *pp_peer_cfg = &(p_ccb->peer_cfg);
  1435. *p_peer_cfg_bits = p_ccb->peer_cfg_bits;
  1436. return TRUE;
  1437. } else {
  1438. L2CAP_TRACE_ERROR ("No CCB for CID:0x%04x", lcid);
  1439. return FALSE;
  1440. }
  1441. }
  1442. /*******************************************************************************
  1443. **
  1444. ** Function L2CA_RegForNoCPEvt
  1445. **
  1446. ** Description Register callback for Number of Completed Packets event.
  1447. **
  1448. ** Input Param p_cb - callback for Number of completed packets event
  1449. ** p_bda - BT address of remote device
  1450. **
  1451. ** Returns TRUE if registered OK, else FALSE
  1452. **
  1453. *******************************************************************************/
  1454. BOOLEAN L2CA_RegForNoCPEvt(tL2CA_NOCP_CB *p_cb, BD_ADDR p_bda)
  1455. {
  1456. tL2C_LCB *p_lcb;
  1457. /* Find the link that is associated with this remote bdaddr */
  1458. p_lcb = l2cu_find_lcb_by_bd_addr (p_bda, BT_TRANSPORT_BR_EDR);
  1459. /* If no link for this handle, nothing to do. */
  1460. if (!p_lcb) {
  1461. return FALSE;
  1462. }
  1463. p_lcb->p_nocp_cb = p_cb;
  1464. return TRUE;
  1465. }
  1466. /*******************************************************************************
  1467. **
  1468. ** Function L2CA_DataWrite
  1469. **
  1470. ** Description Higher layers call this function to write data.
  1471. **
  1472. ** Returns L2CAP_DW_SUCCESS, if data accepted, else FALSE
  1473. ** L2CAP_DW_CONGESTED, if data accepted and the channel is congested
  1474. ** L2CAP_DW_FAILED, if error
  1475. **
  1476. *******************************************************************************/
  1477. UINT8 L2CA_DataWrite (UINT16 cid, BT_HDR *p_data)
  1478. {
  1479. L2CAP_TRACE_API ("L2CA_DataWrite() CID: 0x%04x Len: %d", cid, p_data->len);
  1480. return l2c_data_write (cid, p_data, L2CAP_FLUSHABLE_CH_BASED);
  1481. }
  1482. /*******************************************************************************
  1483. **
  1484. ** Function L2CA_SetChnlFlushability
  1485. **
  1486. ** Description Higher layers call this function to set a channels
  1487. ** flushability flags
  1488. **
  1489. ** Returns TRUE if CID found, else FALSE
  1490. **
  1491. *******************************************************************************/
  1492. BOOLEAN L2CA_SetChnlFlushability (UINT16 cid, BOOLEAN is_flushable)
  1493. {
  1494. #if (L2CAP_NON_FLUSHABLE_PB_INCLUDED == TRUE)
  1495. tL2C_CCB *p_ccb;
  1496. /* Find the channel control block. We don't know the link it is on. */
  1497. if ((p_ccb = l2cu_find_ccb_by_cid (NULL, cid)) == NULL) {
  1498. L2CAP_TRACE_WARNING ("L2CAP - no CCB for L2CA_SetChnlFlushability, CID: %d", cid);
  1499. return (FALSE);
  1500. }
  1501. p_ccb->is_flushable = is_flushable;
  1502. L2CAP_TRACE_API ("L2CA_SetChnlFlushability() CID: 0x%04x is_flushable: %d", cid, is_flushable);
  1503. #endif
  1504. return (TRUE);
  1505. }
  1506. /*******************************************************************************
  1507. **
  1508. ** Function L2CA_DataWriteEx
  1509. **
  1510. ** Description Higher layers call this function to write data with extended
  1511. ** flags.
  1512. ** flags : L2CAP_FLUSHABLE_CH_BASED
  1513. ** L2CAP_FLUSHABLE_PKT
  1514. ** L2CAP_NON_FLUSHABLE_PKT
  1515. **
  1516. ** Returns L2CAP_DW_SUCCESS, if data accepted, else FALSE
  1517. ** L2CAP_DW_CONGESTED, if data accepted and the channel is congested
  1518. ** L2CAP_DW_FAILED, if error
  1519. **
  1520. *******************************************************************************/
  1521. UINT8 L2CA_DataWriteEx (UINT16 cid, BT_HDR *p_data, UINT16 flags)
  1522. {
  1523. L2CAP_TRACE_API ("L2CA_DataWriteEx() CID: 0x%04x Len: %d Flags:0x%04X",
  1524. cid, p_data->len, flags);
  1525. return l2c_data_write (cid, p_data, flags);
  1526. }
  1527. /*******************************************************************************
  1528. **
  1529. ** Function L2CA_FlushChannel
  1530. **
  1531. ** Description This function flushes none, some or all buffers queued up
  1532. ** for xmission for a particular CID. If called with
  1533. ** L2CAP_FLUSH_CHANS_GET (0), it simply returns the number
  1534. ** of buffers queued for that CID L2CAP_FLUSH_CHANS_ALL (0xffff)
  1535. ** flushes all buffers. All other values specifies the maximum
  1536. ** buffers to flush.
  1537. **
  1538. ** Returns Number of buffers left queued for that CID
  1539. **
  1540. *******************************************************************************/
  1541. UINT16 L2CA_FlushChannel (UINT16 lcid, UINT16 num_to_flush)
  1542. {
  1543. tL2C_CCB *p_ccb;
  1544. tL2C_LCB *p_lcb;
  1545. UINT16 num_left = 0,
  1546. num_flushed1 = 0,
  1547. num_flushed2 = 0;
  1548. p_ccb = l2cu_find_ccb_by_cid(NULL, lcid);
  1549. if ( !p_ccb || ((p_lcb = p_ccb->p_lcb) == NULL) ) {
  1550. L2CAP_TRACE_WARNING ("L2CA_FlushChannel() abnormally returning 0 CID: 0x%04x", lcid);
  1551. return (0);
  1552. }
  1553. if (num_to_flush != L2CAP_FLUSH_CHANS_GET) {
  1554. L2CAP_TRACE_API ("L2CA_FlushChannel (FLUSH) CID: 0x%04x NumToFlush: %d QC: %u pFirst: %p",
  1555. lcid, num_to_flush, GKI_queue_length(&p_ccb->xmit_hold_q), GKI_getfirst(&p_ccb->xmit_hold_q));
  1556. } else {
  1557. L2CAP_TRACE_API ("L2CA_FlushChannel (QUERY) CID: 0x%04x", lcid);
  1558. }
  1559. /* Cannot flush eRTM buffers once they have a sequence number */
  1560. if (p_ccb->peer_cfg.fcr.mode != L2CAP_FCR_ERTM_MODE) {
  1561. #if L2CAP_NON_FLUSHABLE_PB_INCLUDED == TRUE
  1562. if (num_to_flush != L2CAP_FLUSH_CHANS_GET) {
  1563. /* If the controller supports enhanced flush, flush the data queued at the controller */
  1564. if ( (HCI_NON_FLUSHABLE_PB_SUPPORTED(BTM_ReadLocalFeatures ()))
  1565. && (BTM_GetNumScoLinks() == 0) ) {
  1566. if ( l2cb.is_flush_active == FALSE ) {
  1567. l2cb.is_flush_active = TRUE;
  1568. /* The only packet type defined - 0 - Automatically-Flushable Only */
  1569. btsnd_hcic_enhanced_flush (p_lcb->handle, 0);
  1570. }
  1571. }
  1572. }
  1573. #endif
  1574. // Iterate though list and flush the amount requested from
  1575. // the transmit data queue that satisfy the layer and event conditions.
  1576. for (const list_node_t *node = list_begin(p_lcb->link_xmit_data_q);
  1577. (num_to_flush > 0) && node != list_end(p_lcb->link_xmit_data_q);) {
  1578. BT_HDR *p_buf = (BT_HDR *)list_node(node);
  1579. node = list_next(node);
  1580. if ((p_buf->layer_specific == 0) && (p_buf->event == lcid)) {
  1581. num_to_flush--;
  1582. num_flushed1++;
  1583. list_remove(p_lcb->link_xmit_data_q, p_buf);
  1584. GKI_freebuf(p_buf);
  1585. }
  1586. }
  1587. }
  1588. /* If needed, flush buffers in the CCB xmit hold queue */
  1589. while ( (num_to_flush != 0) && (!GKI_queue_is_empty(&p_ccb->xmit_hold_q))) {
  1590. BT_HDR *p_buf = (BT_HDR *)GKI_dequeue (&p_ccb->xmit_hold_q);
  1591. if (p_buf) {
  1592. GKI_freebuf (p_buf);
  1593. }
  1594. num_to_flush--;
  1595. num_flushed2++;
  1596. }
  1597. /* If app needs to track all packets, call him */
  1598. if ( (p_ccb->p_rcb) && (p_ccb->p_rcb->api.pL2CA_TxComplete_Cb) && (num_flushed2) ) {
  1599. (*p_ccb->p_rcb->api.pL2CA_TxComplete_Cb)(p_ccb->local_cid, num_flushed2);
  1600. }
  1601. /* Now count how many are left */
  1602. for (const list_node_t *node = list_begin(p_lcb->link_xmit_data_q);
  1603. node != list_end(p_lcb->link_xmit_data_q);
  1604. node = list_next(node)) {
  1605. BT_HDR *p_buf = (BT_HDR *)list_node(node);
  1606. if (p_buf->event == lcid) {
  1607. num_left++;
  1608. }
  1609. }
  1610. /* Add in the number in the CCB xmit queue */
  1611. num_left += GKI_queue_length(&p_ccb->xmit_hold_q);
  1612. /* Return the local number of buffers left for the CID */
  1613. L2CAP_TRACE_DEBUG ("L2CA_FlushChannel() flushed: %u + %u, num_left: %u", num_flushed1, num_flushed2, num_left);
  1614. /* If we were congested, and now we are not, tell the app */
  1615. l2cu_check_channel_congestion (p_ccb);
  1616. return (num_left);
  1617. }