l2c_api.c 83 KB

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