l2c_api.c 86 KB

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