l2c_api.c 79 KB

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