l2c_api.c 83 KB

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