l2c_api.c 80 KB

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