l2c_api.c 80 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267
  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. #if (BLE_INCLUDED == TRUE)
  1098. /*******************************************************************************
  1099. **
  1100. ** Function L2CA_RegisterLECoc
  1101. **
  1102. ** Description Other layers call this function to register for L2CAP
  1103. ** Connection Oriented Channel.
  1104. **
  1105. ** Returns PSM to use or zero if error. Typically, the PSM returned
  1106. ** is the same as was passed in, but for an outgoing-only
  1107. ** connection to a dynamic PSM, a "virtual" PSM is returned
  1108. ** and should be used in the calls to L2CA_ConnectLECocReq()
  1109. ** and L2CA_DeregisterLECoc()
  1110. **
  1111. *******************************************************************************/
  1112. UINT16 L2CA_RegisterLECoc(UINT16 psm, tL2CAP_APPL_INFO *p_cb_info)
  1113. {
  1114. L2CAP_TRACE_API("%s called for LE PSM: 0x%04x", __func__, psm);
  1115. /* Verify that the required callback info has been filled in
  1116. ** Note: Connection callbacks are required but not checked
  1117. ** for here because it is possible to be only a client
  1118. ** or only a server.
  1119. */
  1120. if ((!p_cb_info->pL2CA_DataInd_Cb)
  1121. || (!p_cb_info->pL2CA_DisconnectInd_Cb))
  1122. {
  1123. L2CAP_TRACE_ERROR("%s No cb registering BLE PSM: 0x%04x", __func__, psm);
  1124. return 0;
  1125. }
  1126. /* Verify PSM is valid */
  1127. if (!L2C_IS_VALID_LE_PSM(psm))
  1128. {
  1129. L2CAP_TRACE_ERROR("%s Invalid BLE PSM value, PSM: 0x%04x", __func__, psm);
  1130. return 0;
  1131. }
  1132. tL2C_RCB *p_rcb;
  1133. UINT16 vpsm = psm;
  1134. /* Check if this is a registration for an outgoing-only connection to */
  1135. /* a dynamic PSM. If so, allocate a "virtual" PSM for the app to use. */
  1136. if ((psm >= 0x0080) && (p_cb_info->pL2CA_ConnectInd_Cb == NULL))
  1137. {
  1138. for (vpsm = 0x0080; vpsm < 0x0100; vpsm++)
  1139. {
  1140. p_rcb = l2cu_find_ble_rcb_by_psm(vpsm);
  1141. if (p_rcb == NULL) {
  1142. break;
  1143. }
  1144. }
  1145. L2CAP_TRACE_API("%s Real PSM: 0x%04x Virtual PSM: 0x%04x", __func__, psm, vpsm);
  1146. }
  1147. /* If registration block already there, just overwrite it */
  1148. p_rcb = l2cu_find_ble_rcb_by_psm(vpsm);
  1149. if (p_rcb == NULL)
  1150. {
  1151. p_rcb = l2cu_allocate_ble_rcb(vpsm);
  1152. if (p_rcb == NULL)
  1153. {
  1154. L2CAP_TRACE_WARNING("%s No BLE RCB available, PSM: 0x%04x vPSM: 0x%04x",
  1155. __func__, psm, vpsm);
  1156. return 0;
  1157. }
  1158. }
  1159. p_rcb->api = *p_cb_info;
  1160. p_rcb->real_psm = psm;
  1161. return vpsm;
  1162. }
  1163. /*******************************************************************************
  1164. **
  1165. ** Function L2CA_DeregisterLECoc
  1166. **
  1167. ** Description Other layers call this function to de-register for L2CAP
  1168. ** Connection Oriented Channel.
  1169. **
  1170. ** Returns void
  1171. **
  1172. *******************************************************************************/
  1173. void L2CA_DeregisterLECoc(UINT16 psm)
  1174. {
  1175. L2CAP_TRACE_API("%s called for PSM: 0x%04x", __func__, psm);
  1176. tL2C_RCB *p_rcb = l2cu_find_ble_rcb_by_psm(psm);
  1177. if (p_rcb == NULL)
  1178. {
  1179. L2CAP_TRACE_WARNING("%s PSM: 0x%04x not found for deregistration", __func__, psm);
  1180. return;
  1181. }
  1182. tL2C_LCB *p_lcb = &l2cb.lcb_pool[0];
  1183. for (int i = 0; i < MAX_L2CAP_LINKS; i++, p_lcb++)
  1184. {
  1185. if (!p_lcb->in_use || p_lcb->transport != BT_TRANSPORT_LE) {
  1186. continue;
  1187. }
  1188. tL2C_CCB *p_ccb = p_lcb->ccb_queue.p_first_ccb;
  1189. if ((p_ccb == NULL) || (p_lcb->link_state == LST_DISCONNECTING)) {
  1190. continue;
  1191. }
  1192. if (p_ccb->in_use &&
  1193. (p_ccb->chnl_state == CST_W4_L2CAP_DISCONNECT_RSP ||
  1194. p_ccb->chnl_state == CST_W4_L2CA_DISCONNECT_RSP)) {
  1195. continue;
  1196. }
  1197. if (p_ccb->p_rcb == p_rcb) {
  1198. l2c_csm_execute(p_ccb, L2CEVT_L2CA_DISCONNECT_REQ, NULL);
  1199. }
  1200. }
  1201. l2cu_release_rcb (p_rcb);
  1202. }
  1203. /*******************************************************************************
  1204. **
  1205. ** Function L2CA_ConnectLECocReq
  1206. **
  1207. ** Description Higher layers call this function to create an L2CAP connection.
  1208. ** Note that the connection is not established at this time, but
  1209. ** connection establishment gets started. The callback function
  1210. ** will be invoked when connection establishes or fails.
  1211. **
  1212. ** Parameters: PSM: L2CAP PSM for the connection
  1213. ** BD address of the peer
  1214. ** Local Coc configurations
  1215. ** Returns the CID of the connection, or 0 if it failed to start
  1216. **
  1217. *******************************************************************************/
  1218. UINT16 L2CA_ConnectLECocReq(UINT16 psm, BD_ADDR p_bd_addr, tL2CAP_LE_CFG_INFO *p_cfg)
  1219. {
  1220. L2CAP_TRACE_API("%s PSM: 0x%04x BDA: %02x:%02x:%02x:%02x:%02x:%02x", __func__, psm,
  1221. p_bd_addr[0], p_bd_addr[1], p_bd_addr[2], p_bd_addr[3], p_bd_addr[4], p_bd_addr[5]);
  1222. /* Fail if we have not established communications with the controller */
  1223. if (!BTM_IsDeviceUp())
  1224. {
  1225. L2CAP_TRACE_WARNING("%s BTU not ready", __func__);
  1226. return 0;
  1227. }
  1228. /* Fail if the PSM is not registered */
  1229. tL2C_RCB *p_rcb = l2cu_find_ble_rcb_by_psm(psm);
  1230. if (p_rcb == NULL)
  1231. {
  1232. L2CAP_TRACE_WARNING("%s No BLE RCB, PSM: 0x%04x", __func__, psm);
  1233. return 0;
  1234. }
  1235. /* First, see if we already have a le link to the remote */
  1236. tL2C_LCB *p_lcb = l2cu_find_lcb_by_bd_addr(p_bd_addr, BT_TRANSPORT_LE);
  1237. if (p_lcb == NULL)
  1238. {
  1239. /* No link. Get an LCB and start link establishment */
  1240. p_lcb = l2cu_allocate_lcb(p_bd_addr, FALSE, BT_TRANSPORT_LE);
  1241. if ((p_lcb == NULL)
  1242. /* currently use BR/EDR for ERTM mode l2cap connection */
  1243. || (l2cu_create_conn(p_lcb, BT_TRANSPORT_LE) == FALSE) )
  1244. {
  1245. L2CAP_TRACE_WARNING("%s conn not started for PSM: 0x%04x p_lcb: 0x%p",
  1246. __func__, psm, p_lcb);
  1247. return 0;
  1248. }
  1249. }
  1250. /* Allocate a channel control block */
  1251. tL2C_CCB *p_ccb = l2cu_allocate_ccb(p_lcb, 0);
  1252. if (p_ccb == NULL)
  1253. {
  1254. L2CAP_TRACE_WARNING("%s no CCB, PSM: 0x%04x", __func__, psm);
  1255. return 0;
  1256. }
  1257. /* Save registration info */
  1258. p_ccb->p_rcb = p_rcb;
  1259. /* Save the configuration */
  1260. if (p_cfg) {
  1261. memcpy(&p_ccb->local_conn_cfg, p_cfg, sizeof(tL2CAP_LE_CFG_INFO));
  1262. }
  1263. /* If link is up, start the L2CAP connection */
  1264. if (p_lcb->link_state == LST_CONNECTED)
  1265. {
  1266. if (p_ccb->p_lcb->transport == BT_TRANSPORT_LE)
  1267. {
  1268. L2CAP_TRACE_DEBUG("%s LE Link is up", __func__);
  1269. l2c_csm_execute(p_ccb, L2CEVT_L2CA_CONNECT_REQ, NULL);
  1270. }
  1271. }
  1272. /* If link is disconnecting, save link info to retry after disconnect
  1273. * Possible Race condition when a reconnect occurs
  1274. * on the channel during a disconnect of link. This
  1275. * ccb will be automatically retried after link disconnect
  1276. * arrives
  1277. */
  1278. else if (p_lcb->link_state == LST_DISCONNECTING)
  1279. {
  1280. L2CAP_TRACE_DEBUG("%s link disconnecting: RETRY LATER", __func__);
  1281. /* Save ccb so it can be started after disconnect is finished */
  1282. p_lcb->p_pending_ccb = p_ccb;
  1283. }
  1284. L2CAP_TRACE_API("%s(psm: 0x%04x) returned CID: 0x%04x", __func__, psm, p_ccb->local_cid);
  1285. /* Return the local CID as our handle */
  1286. return p_ccb->local_cid;
  1287. }
  1288. /*******************************************************************************
  1289. **
  1290. ** Function L2CA_ConnectLECocRsp
  1291. **
  1292. ** Description Higher layers call this function to accept an incoming
  1293. ** L2CAP COC connection, for which they had gotten an connect
  1294. ** indication callback.
  1295. **
  1296. ** Returns TRUE for success, FALSE for failure
  1297. **
  1298. *******************************************************************************/
  1299. BOOLEAN L2CA_ConnectLECocRsp (BD_ADDR p_bd_addr, UINT8 id, UINT16 lcid, UINT16 result,
  1300. UINT16 status, tL2CAP_LE_CFG_INFO *p_cfg)
  1301. {
  1302. L2CAP_TRACE_API("%s CID: 0x%04x Result: %d Status: %d BDA: %02x:%02x:%02x:%02x:%02x:%02x",
  1303. __func__, lcid, result, status,
  1304. p_bd_addr[0], p_bd_addr[1], p_bd_addr[2], p_bd_addr[3], p_bd_addr[4], p_bd_addr[5]);
  1305. /* First, find the link control block */
  1306. tL2C_LCB *p_lcb = l2cu_find_lcb_by_bd_addr(p_bd_addr, BT_TRANSPORT_LE);
  1307. if (p_lcb == NULL)
  1308. {
  1309. /* No link. Get an LCB and start link establishment */
  1310. L2CAP_TRACE_WARNING("%s no LCB", __func__);
  1311. return FALSE;
  1312. }
  1313. /* Now, find the channel control block */
  1314. tL2C_CCB *p_ccb = l2cu_find_ccb_by_cid(p_lcb, lcid);
  1315. if (p_ccb == NULL)
  1316. {
  1317. L2CAP_TRACE_WARNING("%s no CCB", __func__);
  1318. return FALSE;
  1319. }
  1320. /* The IDs must match */
  1321. if (p_ccb->remote_id != id)
  1322. {
  1323. L2CAP_TRACE_WARNING("%s bad id. Expected: %d Got: %d", __func__, p_ccb->remote_id, id);
  1324. return FALSE;
  1325. }
  1326. if (p_cfg) {
  1327. memcpy(&p_ccb->local_conn_cfg, p_cfg, sizeof(tL2CAP_LE_CFG_INFO));
  1328. }
  1329. if (result == L2CAP_CONN_OK)
  1330. l2c_csm_execute (p_ccb, L2CEVT_L2CA_CONNECT_RSP, NULL);
  1331. else
  1332. {
  1333. tL2C_CONN_INFO conn_info;
  1334. memcpy(conn_info.bd_addr, p_bd_addr, BD_ADDR_LEN);
  1335. conn_info.l2cap_result = result;
  1336. conn_info.l2cap_status = status;
  1337. l2c_csm_execute(p_ccb, L2CEVT_L2CA_CONNECT_RSP_NEG, &conn_info);
  1338. }
  1339. return TRUE;
  1340. }
  1341. /*******************************************************************************
  1342. **
  1343. ** Function L2CA_GetPeerLECocConfig
  1344. **
  1345. ** Description Get a peers configuration for LE Connection Oriented Channel.
  1346. **
  1347. ** Parameters: local channel id
  1348. ** Pointers to peers configuration storage area
  1349. **
  1350. ** Return value: TRUE if peer is connected
  1351. **
  1352. *******************************************************************************/
  1353. BOOLEAN L2CA_GetPeerLECocConfig (UINT16 lcid, tL2CAP_LE_CFG_INFO* peer_cfg)
  1354. {
  1355. L2CAP_TRACE_API ("%s CID: 0x%04x", __func__, lcid);
  1356. tL2C_CCB *p_ccb = l2cu_find_ccb_by_cid(NULL, lcid);
  1357. if (p_ccb == NULL)
  1358. {
  1359. L2CAP_TRACE_ERROR("%s No CCB for CID:0x%04x", __func__, lcid);
  1360. return FALSE;
  1361. }
  1362. if (peer_cfg != NULL) {
  1363. memcpy(peer_cfg, &p_ccb->peer_conn_cfg, sizeof(tL2CAP_LE_CFG_INFO));
  1364. }
  1365. return TRUE;
  1366. }
  1367. #endif ///BLE_INCLUDED == TRUE
  1368. #if (L2CAP_NUM_FIXED_CHNLS > 0)
  1369. /*******************************************************************************
  1370. **
  1371. ** Function L2CA_RegisterFixedChannel
  1372. **
  1373. ** Description Register a fixed channel.
  1374. **
  1375. ** Parameters: Fixed Channel #
  1376. ** Channel Callbacks and config
  1377. **
  1378. ** Return value: -
  1379. **
  1380. *******************************************************************************/
  1381. BOOLEAN L2CA_RegisterFixedChannel (UINT16 fixed_cid, tL2CAP_FIXED_CHNL_REG *p_freg)
  1382. {
  1383. L2CAP_TRACE_DEBUG ("L2CA_RegisterFixedChannel() CID: 0x%04x, %p", fixed_cid,p_freg);
  1384. if ( (fixed_cid < L2CAP_FIRST_FIXED_CHNL) || (fixed_cid > L2CAP_LAST_FIXED_CHNL) ) {
  1385. L2CAP_TRACE_ERROR ("L2CA_RegisterFixedChannel() Invalid CID: 0x%04x", fixed_cid);
  1386. return (FALSE);
  1387. }
  1388. l2cb.fixed_reg[fixed_cid - L2CAP_FIRST_FIXED_CHNL] = *p_freg;
  1389. return (TRUE);
  1390. }
  1391. /*******************************************************************************
  1392. **
  1393. ** Function L2CA_ConnectFixedChnl
  1394. **
  1395. ** Description Connect an fixed signalling channel to a remote device.
  1396. **
  1397. ** Parameters: Fixed CID
  1398. ** BD Address of remote
  1399. ** BD Address type
  1400. **
  1401. ** Return value: TRUE if connection started
  1402. **
  1403. *******************************************************************************/
  1404. BOOLEAN L2CA_ConnectFixedChnl (UINT16 fixed_cid, BD_ADDR rem_bda, tBLE_ADDR_TYPE bd_addr_type)
  1405. {
  1406. tL2C_LCB *p_lcb;
  1407. tBT_TRANSPORT transport = BT_TRANSPORT_BR_EDR;
  1408. L2CAP_TRACE_API ("%s() CID: 0x%04x BDA: %08x%04x", __func__, fixed_cid,
  1409. (rem_bda[0] << 24) + (rem_bda[1] << 16) + (rem_bda[2] << 8) + rem_bda[3], (rem_bda[4] << 8) + rem_bda[5]);
  1410. // Check CID is valid and registered
  1411. if ( (fixed_cid < L2CAP_FIRST_FIXED_CHNL) || (fixed_cid > L2CAP_LAST_FIXED_CHNL)
  1412. || (l2cb.fixed_reg[fixed_cid - L2CAP_FIRST_FIXED_CHNL].pL2CA_FixedData_Cb == NULL) ) {
  1413. L2CAP_TRACE_ERROR ("%s() Invalid CID: 0x%04x", __func__, fixed_cid);
  1414. return (FALSE);
  1415. }
  1416. // Fail if BT is not yet up
  1417. if (!BTM_IsDeviceUp()) {
  1418. L2CAP_TRACE_WARNING ("%s(0x%04x) - BTU not ready", __func__, fixed_cid);
  1419. return (FALSE);
  1420. }
  1421. #if BLE_INCLUDED == TRUE
  1422. if (fixed_cid >= L2CAP_ATT_CID && fixed_cid <= L2CAP_SMP_CID) {
  1423. transport = BT_TRANSPORT_LE;
  1424. }
  1425. #endif
  1426. tL2C_BLE_FIXED_CHNLS_MASK peer_channel_mask;
  1427. // If we already have a link to the remote, check if it supports that CID
  1428. if ((p_lcb = l2cu_find_lcb_by_bd_addr (rem_bda, transport)) != NULL) {
  1429. // Fixed channels are mandatory on LE transports so ignore the received
  1430. // channel mask and use the locally cached LE channel mask.
  1431. #if BLE_INCLUDED == TRUE
  1432. if (transport == BT_TRANSPORT_LE) {
  1433. peer_channel_mask = l2cb.l2c_ble_fixed_chnls_mask;
  1434. } else
  1435. #endif
  1436. {
  1437. peer_channel_mask = p_lcb->peer_chnl_mask[0];
  1438. }
  1439. // Check for supported channel
  1440. if (!(peer_channel_mask & (1 << fixed_cid))) {
  1441. L2CAP_TRACE_EVENT ("%s() CID:0x%04x BDA: %08x%04x not supported", __func__,
  1442. fixed_cid, (rem_bda[0] << 24) + (rem_bda[1] << 16) + (rem_bda[2] << 8) + rem_bda[3],
  1443. (rem_bda[4] << 8) + rem_bda[5]);
  1444. return FALSE;
  1445. }
  1446. // Get a CCB and link the lcb to it
  1447. if (!l2cu_initialize_fixed_ccb (p_lcb, fixed_cid,
  1448. &l2cb.fixed_reg[fixed_cid - L2CAP_FIRST_FIXED_CHNL].fixed_chnl_opts)) {
  1449. L2CAP_TRACE_WARNING ("%s(0x%04x) - LCB but no CCB", __func__, fixed_cid);
  1450. return FALSE;
  1451. }
  1452. // racing with disconnecting, queue the connection request
  1453. if (p_lcb->link_state == LST_DISCONNECTING) {
  1454. L2CAP_TRACE_DEBUG ("%s() - link disconnecting: RETRY LATER", __func__);
  1455. /* Save ccb so it can be started after disconnect is finished */
  1456. p_lcb->p_pending_ccb = p_lcb->p_fixed_ccbs[fixed_cid - L2CAP_FIRST_FIXED_CHNL];
  1457. return TRUE;
  1458. }
  1459. #if BLE_INCLUDED == TRUE
  1460. (*l2cb.fixed_reg[fixed_cid - L2CAP_FIRST_FIXED_CHNL].pL2CA_FixedConn_Cb)
  1461. (fixed_cid, p_lcb->remote_bd_addr, TRUE, 0, p_lcb->transport);
  1462. #else
  1463. (*l2cb.fixed_reg[fixed_cid - L2CAP_FIRST_FIXED_CHNL].pL2CA_FixedConn_Cb)
  1464. (fixed_cid, p_lcb->remote_bd_addr, TRUE, 0, BT_TRANSPORT_BR_EDR);
  1465. #endif
  1466. return TRUE;
  1467. }
  1468. // No link. Get an LCB and start link establishment
  1469. if ((p_lcb = l2cu_allocate_lcb (rem_bda, FALSE, transport)) == NULL) {
  1470. L2CAP_TRACE_WARNING ("%s(0x%04x) - no LCB", __func__, fixed_cid);
  1471. return FALSE;
  1472. }
  1473. // Get a CCB and link the lcb to it
  1474. if (!l2cu_initialize_fixed_ccb (p_lcb, fixed_cid,
  1475. &l2cb.fixed_reg[fixed_cid - L2CAP_FIRST_FIXED_CHNL].fixed_chnl_opts)) {
  1476. p_lcb->disc_reason = L2CAP_CONN_NO_RESOURCES;
  1477. L2CAP_TRACE_WARNING ("%s(0x%04x) - no CCB", __func__, fixed_cid);
  1478. l2cu_release_lcb (p_lcb);
  1479. return FALSE;
  1480. }
  1481. #if (BLE_INCLUDED == TRUE)
  1482. p_lcb->open_addr_type = bd_addr_type;
  1483. #endif
  1484. if (!l2cu_create_conn(p_lcb, transport)) {
  1485. L2CAP_TRACE_WARNING ("%s() - create_conn failed", __func__);
  1486. l2cu_release_lcb (p_lcb);
  1487. return FALSE;
  1488. }
  1489. return TRUE;
  1490. }
  1491. /*******************************************************************************
  1492. **
  1493. ** Function L2CA_SendFixedChnlData
  1494. **
  1495. ** Description Write data on a fixed channel.
  1496. **
  1497. ** Parameters: Fixed CID
  1498. ** BD Address of remote
  1499. ** Pointer to buffer of type BT_HDR
  1500. **
  1501. ** Return value L2CAP_DW_SUCCESS, if data accepted
  1502. ** L2CAP_DW_FAILED, if error
  1503. **
  1504. *******************************************************************************/
  1505. UINT16 L2CA_SendFixedChnlData (UINT16 fixed_cid, BD_ADDR rem_bda, BT_HDR *p_buf)
  1506. {
  1507. tL2C_LCB *p_lcb;
  1508. tBT_TRANSPORT transport = BT_TRANSPORT_BR_EDR;
  1509. L2CAP_TRACE_API ("L2CA_SendFixedChnlData() CID: 0x%04x BDA: %08x%04x", fixed_cid,
  1510. (rem_bda[0] << 24) + (rem_bda[1] << 16) + (rem_bda[2] << 8) + rem_bda[3], (rem_bda[4] << 8) + rem_bda[5]);
  1511. #if BLE_INCLUDED == TRUE
  1512. if (fixed_cid >= L2CAP_ATT_CID && fixed_cid <= L2CAP_SMP_CID) {
  1513. transport = BT_TRANSPORT_LE;
  1514. }
  1515. #endif
  1516. // Check CID is valid and registered
  1517. if ( (fixed_cid < L2CAP_FIRST_FIXED_CHNL) || (fixed_cid > L2CAP_LAST_FIXED_CHNL)
  1518. || (l2cb.fixed_reg[fixed_cid - L2CAP_FIRST_FIXED_CHNL].pL2CA_FixedData_Cb == NULL) ) {
  1519. L2CAP_TRACE_ERROR ("L2CA_SendFixedChnlData() Invalid CID: 0x%04x", fixed_cid);
  1520. osi_free (p_buf);
  1521. return (L2CAP_DW_FAILED);
  1522. }
  1523. // Fail if BT is not yet up
  1524. if (!BTM_IsDeviceUp()) {
  1525. L2CAP_TRACE_WARNING ("L2CA_SendFixedChnlData(0x%04x) - BTU not ready", fixed_cid);
  1526. osi_free (p_buf);
  1527. return (L2CAP_DW_FAILED);
  1528. }
  1529. // We need to have a link up
  1530. if ((p_lcb = l2cu_find_lcb_by_bd_addr (rem_bda, transport)) == NULL ||
  1531. /* if link is disconnecting, also report data sending failure */
  1532. p_lcb->link_state == LST_DISCONNECTING) {
  1533. L2CAP_TRACE_WARNING ("L2CA_SendFixedChnlData(0x%04x) - no LCB", fixed_cid);
  1534. osi_free (p_buf);
  1535. return (L2CAP_DW_FAILED);
  1536. }
  1537. tL2C_BLE_FIXED_CHNLS_MASK peer_channel_mask;
  1538. // Select peer channels mask to use depending on transport
  1539. #if BLE_INCLUDED == TRUE
  1540. if (transport == BT_TRANSPORT_LE) {
  1541. peer_channel_mask = l2cb.l2c_ble_fixed_chnls_mask;
  1542. } else
  1543. #endif
  1544. {
  1545. peer_channel_mask = p_lcb->peer_chnl_mask[0];
  1546. }
  1547. if ((peer_channel_mask & (1 << fixed_cid)) == 0) {
  1548. L2CAP_TRACE_WARNING ("L2CA_SendFixedChnlData() - peer does not support fixed chnl: 0x%04x", fixed_cid);
  1549. osi_free (p_buf);
  1550. return (L2CAP_DW_FAILED);
  1551. }
  1552. p_buf->event = 0;
  1553. p_buf->layer_specific = L2CAP_FLUSHABLE_CH_BASED;
  1554. if (!p_lcb->p_fixed_ccbs[fixed_cid - L2CAP_FIRST_FIXED_CHNL]) {
  1555. if (!l2cu_initialize_fixed_ccb (p_lcb, fixed_cid, &l2cb.fixed_reg[fixed_cid - L2CAP_FIRST_FIXED_CHNL].fixed_chnl_opts)) {
  1556. L2CAP_TRACE_WARNING ("L2CA_SendFixedChnlData() - no CCB for chnl: 0x%4x", fixed_cid);
  1557. osi_free (p_buf);
  1558. return (L2CAP_DW_FAILED);
  1559. }
  1560. }
  1561. // If already congested, do not accept any more packets
  1562. if (p_lcb->p_fixed_ccbs[fixed_cid - L2CAP_FIRST_FIXED_CHNL]->cong_sent) {
  1563. L2CAP_TRACE_DEBUG ("L2CAP - CID: 0x%04x cannot send, already congested\
  1564. xmit_hold_q.count: %u buff_quota: %u", fixed_cid,
  1565. fixed_queue_length(p_lcb->p_fixed_ccbs[fixed_cid - L2CAP_FIRST_FIXED_CHNL]->xmit_hold_q),
  1566. p_lcb->p_fixed_ccbs[fixed_cid - L2CAP_FIRST_FIXED_CHNL]->buff_quota);
  1567. osi_free(p_buf);
  1568. return (L2CAP_DW_FAILED);
  1569. }
  1570. l2c_enqueue_peer_data (p_lcb->p_fixed_ccbs[fixed_cid - L2CAP_FIRST_FIXED_CHNL], p_buf);
  1571. l2c_link_check_send_pkts (p_lcb, NULL, NULL);
  1572. // If there is no dynamic CCB on the link, restart the idle timer each time something is sent
  1573. if (p_lcb->in_use && p_lcb->link_state == LST_CONNECTED && !p_lcb->ccb_queue.p_first_ccb) {
  1574. l2cu_no_dynamic_ccbs (p_lcb);
  1575. }
  1576. if (p_lcb->p_fixed_ccbs[fixed_cid - L2CAP_FIRST_FIXED_CHNL]->cong_sent) {
  1577. return (L2CAP_DW_CONGESTED);
  1578. }
  1579. return (L2CAP_DW_SUCCESS);
  1580. }
  1581. BOOLEAN L2CA_CheckIsCongest(UINT16 fixed_cid, UINT16 handle)
  1582. {
  1583. tL2C_LCB *p_lcb;
  1584. p_lcb = l2cu_find_lcb_by_handle(handle);
  1585. if (p_lcb != NULL && p_lcb->p_fixed_ccbs[fixed_cid - L2CAP_FIRST_FIXED_CHNL] != NULL) {
  1586. return p_lcb->p_fixed_ccbs[fixed_cid - L2CAP_FIRST_FIXED_CHNL]->cong_sent;
  1587. }
  1588. return TRUE;
  1589. }
  1590. #if (BLE_INCLUDED == TRUE)
  1591. UINT16 L2CA_GetFreePktBufferNum_LE(void)
  1592. {
  1593. return l2cb.controller_le_xmit_window;
  1594. }
  1595. #endif
  1596. /*******************************************************************************
  1597. **
  1598. ** Function L2CA_RemoveFixedChnl
  1599. **
  1600. ** Description Remove a fixed channel to a remote device.
  1601. **
  1602. ** Parameters: Fixed CID
  1603. ** BD Address of remote
  1604. ** Idle timeout to use (or 0xFFFF if don't care)
  1605. **
  1606. ** Return value: TRUE if channel removed
  1607. **
  1608. *******************************************************************************/
  1609. BOOLEAN L2CA_RemoveFixedChnl (UINT16 fixed_cid, BD_ADDR rem_bda)
  1610. {
  1611. tL2C_LCB *p_lcb;
  1612. tL2C_CCB *p_ccb;
  1613. tBT_TRANSPORT transport = BT_TRANSPORT_BR_EDR;
  1614. /* Check CID is valid and registered */
  1615. if ( (fixed_cid < L2CAP_FIRST_FIXED_CHNL) || (fixed_cid > L2CAP_LAST_FIXED_CHNL)
  1616. || (l2cb.fixed_reg[fixed_cid - L2CAP_FIRST_FIXED_CHNL].pL2CA_FixedData_Cb == NULL) ) {
  1617. L2CAP_TRACE_ERROR ("L2CA_RemoveFixedChnl() Invalid CID: 0x%04x", fixed_cid);
  1618. return (FALSE);
  1619. }
  1620. #if BLE_INCLUDED == TRUE
  1621. if (fixed_cid >= L2CAP_ATT_CID && fixed_cid <= L2CAP_SMP_CID) {
  1622. transport = BT_TRANSPORT_LE;
  1623. }
  1624. #endif
  1625. /* Is a fixed channel connected to the remote BDA ?*/
  1626. p_lcb = l2cu_find_lcb_by_bd_addr (rem_bda, transport);
  1627. if ( ((p_lcb) == NULL) || (!p_lcb->p_fixed_ccbs[fixed_cid - L2CAP_FIRST_FIXED_CHNL]) ) {
  1628. L2CAP_TRACE_DEBUG ("L2CA_RemoveFixedChnl() CID: 0x%04x BDA: %08x%04x not connected", fixed_cid,
  1629. (rem_bda[0] << 24) + (rem_bda[1] << 16) + (rem_bda[2] << 8) + rem_bda[3], (rem_bda[4] << 8) + rem_bda[5]);
  1630. return (FALSE);
  1631. }
  1632. L2CAP_TRACE_API ("L2CA_RemoveFixedChnl() CID: 0x%04x BDA: %08x%04x", fixed_cid,
  1633. (rem_bda[0] << 24) + (rem_bda[1] << 16) + (rem_bda[2] << 8) + rem_bda[3], (rem_bda[4] << 8) + rem_bda[5]);
  1634. /* Release the CCB, starting an inactivity timeout on the LCB if no other CCBs exist */
  1635. p_ccb = p_lcb->p_fixed_ccbs[fixed_cid - L2CAP_FIRST_FIXED_CHNL];
  1636. p_lcb->p_fixed_ccbs[fixed_cid - L2CAP_FIRST_FIXED_CHNL] = NULL;
  1637. p_lcb->disc_reason = HCI_ERR_CONN_CAUSE_LOCAL_HOST;
  1638. #if BLE_INCLUDED == TRUE
  1639. // Retain the link for a few more seconds after SMP pairing is done, since the Android
  1640. // platform always does service discovery after pairing is complete. This will avoid
  1641. // the link down (pairing is complete) and an immediate re-connection for service
  1642. // discovery.
  1643. // Some devices do not do auto advertising when link is dropped, thus fail the second
  1644. // connection and service discovery.
  1645. if ((fixed_cid == L2CAP_ATT_CID ) && !p_lcb->ccb_queue.p_first_ccb) {
  1646. p_lcb->idle_timeout = 0;
  1647. }
  1648. #endif
  1649. l2cu_release_ccb (p_ccb);
  1650. return (TRUE);
  1651. }
  1652. /*******************************************************************************
  1653. **
  1654. ** Function L2CA_SetFixedChannelTout
  1655. **
  1656. ** Description Higher layers call this function to set the idle timeout for
  1657. ** a fixed channel. The "idle timeout" is the amount of time that
  1658. ** a connection can remain up with no L2CAP channels on it.
  1659. ** A timeout of zero means that the connection will be torn
  1660. ** down immediately when the last channel is removed.
  1661. ** A timeout of 0xFFFF means no timeout. Values are in seconds.
  1662. ** A bd_addr is the remote BD address. If bd_addr = BT_BD_ANY,
  1663. ** then the idle timeouts for all active l2cap links will be
  1664. ** changed.
  1665. **
  1666. ** Returns TRUE if command succeeded, FALSE if failed
  1667. **
  1668. *******************************************************************************/
  1669. BOOLEAN L2CA_SetFixedChannelTout (BD_ADDR rem_bda, UINT16 fixed_cid, UINT16 idle_tout)
  1670. {
  1671. tL2C_LCB *p_lcb;
  1672. tBT_TRANSPORT transport = BT_TRANSPORT_BR_EDR;
  1673. #if BLE_INCLUDED == TRUE
  1674. if (fixed_cid >= L2CAP_ATT_CID && fixed_cid <= L2CAP_SMP_CID) {
  1675. transport = BT_TRANSPORT_LE;
  1676. }
  1677. #endif
  1678. /* Is a fixed channel connected to the remote BDA ?*/
  1679. p_lcb = l2cu_find_lcb_by_bd_addr (rem_bda, transport);
  1680. if ( ((p_lcb) == NULL) || (!p_lcb->p_fixed_ccbs[fixed_cid - L2CAP_FIRST_FIXED_CHNL]) ) {
  1681. L2CAP_TRACE_WARNING ("L2CA_SetFixedChannelTout() CID: 0x%04x BDA: %08x%04x not connected", fixed_cid,
  1682. (rem_bda[0] << 24) + (rem_bda[1] << 16) + (rem_bda[2] << 8) + rem_bda[3], (rem_bda[4] << 8) + rem_bda[5]);
  1683. return (FALSE);
  1684. }
  1685. p_lcb->p_fixed_ccbs[fixed_cid - L2CAP_FIRST_FIXED_CHNL]->fixed_chnl_idle_tout = idle_tout;
  1686. if (p_lcb->in_use && p_lcb->link_state == LST_CONNECTED && !p_lcb->ccb_queue.p_first_ccb) {
  1687. /* If there are no dynamic CCBs, (re)start the idle timer in case we changed it */
  1688. l2cu_no_dynamic_ccbs (p_lcb);
  1689. }
  1690. return TRUE;
  1691. }
  1692. #endif /* #if (L2CAP_NUM_FIXED_CHNLS > 0) */
  1693. #if (CLASSIC_BT_INCLUDED == TRUE)
  1694. /*******************************************************************************
  1695. **
  1696. ** Function L2CA_GetCurrentConfig
  1697. **
  1698. ** Description This function returns configurations of L2CAP channel
  1699. ** pp_our_cfg : pointer of our saved configuration options
  1700. ** p_our_cfg_bits : valid config in bitmap
  1701. ** pp_peer_cfg: pointer of peer's saved configuration options
  1702. ** p_peer_cfg_bits : valid config in bitmap
  1703. **
  1704. ** Returns TRUE if successful
  1705. **
  1706. *******************************************************************************/
  1707. BOOLEAN L2CA_GetCurrentConfig (UINT16 lcid,
  1708. tL2CAP_CFG_INFO **pp_our_cfg, tL2CAP_CH_CFG_BITS *p_our_cfg_bits,
  1709. tL2CAP_CFG_INFO **pp_peer_cfg, tL2CAP_CH_CFG_BITS *p_peer_cfg_bits)
  1710. {
  1711. tL2C_CCB *p_ccb;
  1712. L2CAP_TRACE_API ("L2CA_GetCurrentConfig() CID: 0x%04x", lcid);
  1713. p_ccb = l2cu_find_ccb_by_cid(NULL, lcid);
  1714. if (p_ccb) {
  1715. *pp_our_cfg = &(p_ccb->our_cfg);
  1716. /* convert valid config items into bitmap */
  1717. *p_our_cfg_bits = 0;
  1718. if (p_ccb->our_cfg.mtu_present) {
  1719. *p_our_cfg_bits |= L2CAP_CH_CFG_MASK_MTU;
  1720. }
  1721. if (p_ccb->our_cfg.qos_present) {
  1722. *p_our_cfg_bits |= L2CAP_CH_CFG_MASK_QOS;
  1723. }
  1724. if (p_ccb->our_cfg.flush_to_present) {
  1725. *p_our_cfg_bits |= L2CAP_CH_CFG_MASK_FLUSH_TO;
  1726. }
  1727. if (p_ccb->our_cfg.fcr_present) {
  1728. *p_our_cfg_bits |= L2CAP_CH_CFG_MASK_FCR;
  1729. }
  1730. if (p_ccb->our_cfg.fcs_present) {
  1731. *p_our_cfg_bits |= L2CAP_CH_CFG_MASK_FCS;
  1732. }
  1733. if (p_ccb->our_cfg.ext_flow_spec_present) {
  1734. *p_our_cfg_bits |= L2CAP_CH_CFG_MASK_EXT_FLOW_SPEC;
  1735. }
  1736. *pp_peer_cfg = &(p_ccb->peer_cfg);
  1737. *p_peer_cfg_bits = p_ccb->peer_cfg_bits;
  1738. return TRUE;
  1739. } else {
  1740. L2CAP_TRACE_ERROR ("No CCB for CID:0x%04x", lcid);
  1741. return FALSE;
  1742. }
  1743. }
  1744. /*******************************************************************************
  1745. **
  1746. ** Function L2CA_RegForNoCPEvt
  1747. **
  1748. ** Description Register callback for Number of Completed Packets event.
  1749. **
  1750. ** Input Param p_cb - callback for Number of completed packets event
  1751. ** p_bda - BT address of remote device
  1752. **
  1753. ** Returns TRUE if registered OK, else FALSE
  1754. **
  1755. *******************************************************************************/
  1756. BOOLEAN L2CA_RegForNoCPEvt(tL2CA_NOCP_CB *p_cb, BD_ADDR p_bda)
  1757. {
  1758. tL2C_LCB *p_lcb;
  1759. /* Find the link that is associated with this remote bdaddr */
  1760. p_lcb = l2cu_find_lcb_by_bd_addr (p_bda, BT_TRANSPORT_BR_EDR);
  1761. /* If no link for this handle, nothing to do. */
  1762. if (!p_lcb) {
  1763. return FALSE;
  1764. }
  1765. p_lcb->p_nocp_cb = p_cb;
  1766. return TRUE;
  1767. }
  1768. #endif ///CLASSIC_BT_INCLUDED == TRUE
  1769. /*******************************************************************************
  1770. **
  1771. ** Function L2CA_DataWrite
  1772. **
  1773. ** Description Higher layers call this function to write data.
  1774. **
  1775. ** Returns L2CAP_DW_SUCCESS, if data accepted, else FALSE
  1776. ** L2CAP_DW_CONGESTED, if data accepted and the channel is congested
  1777. ** L2CAP_DW_FAILED, if error
  1778. **
  1779. *******************************************************************************/
  1780. #if (CLASSIC_BT_INCLUDED == TRUE)
  1781. UINT8 L2CA_DataWrite (UINT16 cid, BT_HDR *p_data)
  1782. {
  1783. L2CAP_TRACE_API ("L2CA_DataWrite() CID: 0x%04x Len: %d", cid, p_data->len);
  1784. return l2c_data_write (cid, p_data, L2CAP_FLUSHABLE_CH_BASED);
  1785. }
  1786. #endif ///CLASSIC_BT_INCLUDED == TRUE
  1787. /*******************************************************************************
  1788. **
  1789. ** Function L2CA_SetChnlFlushability
  1790. **
  1791. ** Description Higher layers call this function to set a channels
  1792. ** flushability flags
  1793. **
  1794. ** Returns TRUE if CID found, else FALSE
  1795. **
  1796. *******************************************************************************/
  1797. BOOLEAN L2CA_SetChnlFlushability (UINT16 cid, BOOLEAN is_flushable)
  1798. {
  1799. #if (L2CAP_NON_FLUSHABLE_PB_INCLUDED == TRUE)
  1800. tL2C_CCB *p_ccb;
  1801. /* Find the channel control block. We don't know the link it is on. */
  1802. if ((p_ccb = l2cu_find_ccb_by_cid (NULL, cid)) == NULL) {
  1803. L2CAP_TRACE_WARNING ("L2CAP - no CCB for L2CA_SetChnlFlushability, CID: %d", cid);
  1804. return (FALSE);
  1805. }
  1806. p_ccb->is_flushable = is_flushable;
  1807. L2CAP_TRACE_API ("L2CA_SetChnlFlushability() CID: 0x%04x is_flushable: %d", cid, is_flushable);
  1808. #endif
  1809. return (TRUE);
  1810. }
  1811. /*******************************************************************************
  1812. **
  1813. ** Function L2CA_DataWriteEx
  1814. **
  1815. ** Description Higher layers call this function to write data with extended
  1816. ** flags.
  1817. ** flags : L2CAP_FLUSHABLE_CH_BASED
  1818. ** L2CAP_FLUSHABLE_PKT
  1819. ** L2CAP_NON_FLUSHABLE_PKT
  1820. **
  1821. ** Returns L2CAP_DW_SUCCESS, if data accepted, else FALSE
  1822. ** L2CAP_DW_CONGESTED, if data accepted and the channel is congested
  1823. ** L2CAP_DW_FAILED, if error
  1824. **
  1825. *******************************************************************************/
  1826. #if (CLASSIC_BT_INCLUDED == TRUE)
  1827. UINT8 L2CA_DataWriteEx (UINT16 cid, BT_HDR *p_data, UINT16 flags)
  1828. {
  1829. L2CAP_TRACE_API ("L2CA_DataWriteEx() CID: 0x%04x Len: %d Flags:0x%04X",
  1830. cid, p_data->len, flags);
  1831. return l2c_data_write (cid, p_data, flags);
  1832. }
  1833. #endif ///CLASSIC_BT_INCLUDED == TRUE
  1834. /*******************************************************************************
  1835. **
  1836. ** Function L2CA_FlushChannel
  1837. **
  1838. ** Description This function flushes none, some or all buffers queued up
  1839. ** for xmission for a particular CID. If called with
  1840. ** L2CAP_FLUSH_CHANS_GET (0), it simply returns the number
  1841. ** of buffers queued for that CID L2CAP_FLUSH_CHANS_ALL (0xffff)
  1842. ** flushes all buffers. All other values specifies the maximum
  1843. ** buffers to flush.
  1844. **
  1845. ** Returns Number of buffers left queued for that CID
  1846. **
  1847. *******************************************************************************/
  1848. UINT16 L2CA_FlushChannel (UINT16 lcid, UINT16 num_to_flush)
  1849. {
  1850. tL2C_CCB *p_ccb;
  1851. tL2C_LCB *p_lcb;
  1852. UINT16 num_left = 0,
  1853. num_flushed1 = 0,
  1854. num_flushed2 = 0;
  1855. p_ccb = l2cu_find_ccb_by_cid(NULL, lcid);
  1856. if ( !p_ccb || ((p_lcb = p_ccb->p_lcb) == NULL) ) {
  1857. L2CAP_TRACE_WARNING ("L2CA_FlushChannel() abnormally returning 0 CID: 0x%04x", lcid);
  1858. return (0);
  1859. }
  1860. if (num_to_flush != L2CAP_FLUSH_CHANS_GET) {
  1861. L2CAP_TRACE_API ("L2CA_FlushChannel (FLUSH) CID: 0x%04x NumToFlush: %d QC: %u pFirst: %p",
  1862. lcid, num_to_flush,
  1863. fixed_queue_length(p_ccb->xmit_hold_q),
  1864. fixed_queue_try_peek_first(p_ccb->xmit_hold_q));
  1865. } else {
  1866. L2CAP_TRACE_API ("L2CA_FlushChannel (QUERY) CID: 0x%04x", lcid);
  1867. }
  1868. /* Cannot flush eRTM buffers once they have a sequence number */
  1869. if (p_ccb->peer_cfg.fcr.mode != L2CAP_FCR_ERTM_MODE) {
  1870. #if L2CAP_NON_FLUSHABLE_PB_INCLUDED == TRUE
  1871. if (num_to_flush != L2CAP_FLUSH_CHANS_GET) {
  1872. /* If the controller supports enhanced flush, flush the data queued at the controller */
  1873. if ( (HCI_NON_FLUSHABLE_PB_SUPPORTED(BTM_ReadLocalFeatures ()))
  1874. && (BTM_GetNumScoLinks() == 0) ) {
  1875. if ( l2cb.is_flush_active == FALSE ) {
  1876. l2cb.is_flush_active = TRUE;
  1877. /* The only packet type defined - 0 - Automatically-Flushable Only */
  1878. btsnd_hcic_enhanced_flush (p_lcb->handle, 0);
  1879. }
  1880. }
  1881. }
  1882. #endif
  1883. // Iterate though list and flush the amount requested from
  1884. // the transmit data queue that satisfy the layer and event conditions.
  1885. for (const list_node_t *node = list_begin(p_lcb->link_xmit_data_q);
  1886. (num_to_flush > 0) && node != list_end(p_lcb->link_xmit_data_q);) {
  1887. BT_HDR *p_buf = (BT_HDR *)list_node(node);
  1888. node = list_next(node);
  1889. if ((p_buf->layer_specific == 0) && (p_buf->event == lcid)) {
  1890. num_to_flush--;
  1891. num_flushed1++;
  1892. list_remove(p_lcb->link_xmit_data_q, p_buf);
  1893. osi_free(p_buf);
  1894. }
  1895. }
  1896. }
  1897. /* If needed, flush buffers in the CCB xmit hold queue */
  1898. while ( (num_to_flush != 0) && (!fixed_queue_is_empty(p_ccb->xmit_hold_q))) {
  1899. BT_HDR *p_buf = (BT_HDR *)fixed_queue_dequeue(p_ccb->xmit_hold_q, 0);
  1900. if (p_buf) {
  1901. osi_free (p_buf);
  1902. }
  1903. num_to_flush--;
  1904. num_flushed2++;
  1905. }
  1906. /* If app needs to track all packets, call him */
  1907. if ( (p_ccb->p_rcb) && (p_ccb->p_rcb->api.pL2CA_TxComplete_Cb) && (num_flushed2) ) {
  1908. (*p_ccb->p_rcb->api.pL2CA_TxComplete_Cb)(p_ccb->local_cid, num_flushed2);
  1909. }
  1910. /* Now count how many are left */
  1911. for (const list_node_t *node = list_begin(p_lcb->link_xmit_data_q);
  1912. node != list_end(p_lcb->link_xmit_data_q);
  1913. node = list_next(node)) {
  1914. BT_HDR *p_buf = (BT_HDR *)list_node(node);
  1915. if (p_buf->event == lcid) {
  1916. num_left++;
  1917. }
  1918. }
  1919. /* Add in the number in the CCB xmit queue */
  1920. num_left += fixed_queue_length(p_ccb->xmit_hold_q);
  1921. /* Return the local number of buffers left for the CID */
  1922. L2CAP_TRACE_DEBUG ("L2CA_FlushChannel() flushed: %u + %u, num_left: %u", num_flushed1, num_flushed2, num_left);
  1923. /* If we were congested, and now we are not, tell the app */
  1924. l2cu_check_channel_congestion (p_ccb);
  1925. return (num_left);
  1926. }