btm_acl.c 92 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577
  1. /******************************************************************************
  2. *
  3. * Copyright (C) 2000-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. ** Name: btm_acl.c
  21. **
  22. ** Description: This file contains functions that handle ACL connections.
  23. ** This includes operations such as hold and sniff modes,
  24. ** supported packet types.
  25. **
  26. ** This module contains both internal and external (API)
  27. ** functions. External (API) functions are distinguishable
  28. ** by their names beginning with uppercase BTM.
  29. **
  30. **
  31. ******************************************************************************/
  32. #include <stdlib.h>
  33. #include <string.h>
  34. //#include <stdio.h>
  35. #include <stddef.h>
  36. #include "stack/bt_types.h"
  37. #include "common/bt_target.h"
  38. #include "device/controller.h"
  39. #include "stack/hcimsgs.h"
  40. #include "stack/btu.h"
  41. #include "stack/btm_api.h"
  42. #include "btm_int.h"
  43. #include "l2c_int.h"
  44. #include "stack/hcidefs.h"
  45. //#include "bt_utils.h"
  46. static void btm_read_remote_features (UINT16 handle);
  47. static void btm_read_remote_ext_features (UINT16 handle, UINT8 page_number);
  48. static void btm_process_remote_ext_features (tACL_CONN *p_acl_cb, UINT8 num_read_pages);
  49. #define BTM_DEV_REPLY_TIMEOUT 3 /* 3 second timeout waiting for responses */
  50. /*******************************************************************************
  51. **
  52. ** Function btm_acl_init
  53. **
  54. ** Description This function is called at BTM startup to initialize
  55. **
  56. ** Returns void
  57. **
  58. *******************************************************************************/
  59. void btm_acl_init (void)
  60. {
  61. BTM_TRACE_DEBUG ("btm_acl_init\n");
  62. #if 0 /* cleared in btm_init; put back in if called from anywhere else! */
  63. memset (&btm_cb.acl_db, 0, sizeof (btm_cb.acl_db));
  64. memset (btm_cb.btm_scn, 0, BTM_MAX_SCN); /* Initialize the SCN usage to FALSE */
  65. btm_cb.btm_def_link_policy = 0;
  66. btm_cb.p_bl_changed_cb = NULL;
  67. #endif
  68. /* Initialize nonzero defaults */
  69. btm_cb.btm_def_link_super_tout = HCI_DEFAULT_INACT_TOUT;
  70. btm_cb.acl_disc_reason = 0xff ;
  71. }
  72. /*******************************************************************************
  73. **
  74. ** Function btm_bda_to_acl
  75. **
  76. ** Description This function returns the FIRST acl_db entry for the passed BDA.
  77. **
  78. ** Parameters bda : BD address of the remote device
  79. ** transport : Physical transport used for ACL connection (BR/EDR or LE)
  80. **
  81. ** Returns Returns pointer to the ACL DB for the requested BDA if found.
  82. ** NULL if not found.
  83. **
  84. *******************************************************************************/
  85. tACL_CONN *btm_bda_to_acl (BD_ADDR bda, tBT_TRANSPORT transport)
  86. {
  87. tACL_CONN *p = &btm_cb.acl_db[0];
  88. UINT16 xx;
  89. if (bda) {
  90. for (xx = 0; xx < MAX_L2CAP_LINKS; xx++, p++) {
  91. if ((p->in_use) && (!memcmp (p->remote_addr, bda, BD_ADDR_LEN))
  92. #if BLE_INCLUDED == TRUE
  93. && p->transport == transport
  94. #endif
  95. ) {
  96. BTM_TRACE_DEBUG ("btm_bda_to_acl found\n");
  97. return (p);
  98. }
  99. }
  100. }
  101. /* If here, no BD Addr found */
  102. return ((tACL_CONN *)NULL);
  103. }
  104. /*******************************************************************************
  105. **
  106. ** Function btm_handle_to_acl_index
  107. **
  108. ** Description This function returns the FIRST acl_db entry for the passed hci_handle.
  109. **
  110. ** Returns index to the acl_db or MAX_L2CAP_LINKS.
  111. **
  112. *******************************************************************************/
  113. UINT8 btm_handle_to_acl_index (UINT16 hci_handle)
  114. {
  115. tACL_CONN *p = &btm_cb.acl_db[0];
  116. UINT8 xx;
  117. BTM_TRACE_DEBUG ("btm_handle_to_acl_index\n");
  118. for (xx = 0; xx < MAX_L2CAP_LINKS; xx++, p++) {
  119. if ((p->in_use) && (p->hci_handle == hci_handle)) {
  120. break;
  121. }
  122. }
  123. /* If here, no BD Addr found */
  124. return (xx);
  125. }
  126. /*******************************************************************************
  127. **
  128. ** Function btm_handle_to_acl
  129. **
  130. ** Description This function returns the FIRST acl_db entry for the passed hci_handle.
  131. **
  132. ** Returns Returns pointer to the ACL DB for the requested BDA if found.
  133. ** NULL if not found.
  134. **
  135. *******************************************************************************/
  136. tACL_CONN *btm_handle_to_acl (UINT16 hci_handle)
  137. {
  138. tACL_CONN *p = &btm_cb.acl_db[0];
  139. UINT8 xx;
  140. BTM_TRACE_DEBUG ("btm_handle_to_acl_index\n");
  141. for (xx = 0; xx < MAX_L2CAP_LINKS; xx++, p++) {
  142. if ((p->in_use) && (p->hci_handle == hci_handle)) {
  143. return(p);
  144. }
  145. }
  146. /* If here, no BD Addr found */
  147. return ((tACL_CONN *)NULL);
  148. }
  149. #if BLE_PRIVACY_SPT == TRUE
  150. /*******************************************************************************
  151. **
  152. ** Function btm_ble_get_acl_remote_addr
  153. **
  154. ** Description This function reads the active remote address used for the
  155. ** connection.
  156. **
  157. ** Returns success return TRUE, otherwise FALSE.
  158. **
  159. *******************************************************************************/
  160. BOOLEAN btm_ble_get_acl_remote_addr(tBTM_SEC_DEV_REC *p_dev_rec, BD_ADDR conn_addr,
  161. tBLE_ADDR_TYPE *p_addr_type)
  162. {
  163. #if BLE_INCLUDED == TRUE
  164. BOOLEAN st = TRUE;
  165. if (p_dev_rec == NULL) {
  166. BTM_TRACE_ERROR("btm_ble_get_acl_remote_addr can not find device with matching address\n");
  167. return FALSE;
  168. }
  169. switch (p_dev_rec->ble.active_addr_type) {
  170. case BTM_BLE_ADDR_PSEUDO:
  171. memcpy(conn_addr, p_dev_rec->bd_addr, BD_ADDR_LEN);
  172. * p_addr_type = p_dev_rec->ble.ble_addr_type;
  173. break;
  174. case BTM_BLE_ADDR_RRA:
  175. memcpy(conn_addr, p_dev_rec->ble.cur_rand_addr, BD_ADDR_LEN);
  176. * p_addr_type = BLE_ADDR_RANDOM;
  177. break;
  178. case BTM_BLE_ADDR_STATIC:
  179. memcpy(conn_addr, p_dev_rec->ble.static_addr, BD_ADDR_LEN);
  180. * p_addr_type = p_dev_rec->ble.static_addr_type;
  181. break;
  182. default:
  183. BTM_TRACE_ERROR("Unknown active address: %d\n", p_dev_rec->ble.active_addr_type);
  184. st = FALSE;
  185. break;
  186. }
  187. return st;
  188. #else
  189. UNUSED(p_dev_rec);
  190. UNUSED(conn_addr);
  191. UNUSED(p_addr_type);
  192. return FALSE;
  193. #endif
  194. }
  195. #endif
  196. /*******************************************************************************
  197. **
  198. ** Function btm_acl_created
  199. **
  200. ** Description This function is called by L2CAP when an ACL connection
  201. ** is created.
  202. **
  203. ** Returns void
  204. **
  205. *******************************************************************************/
  206. void btm_acl_created (BD_ADDR bda, DEV_CLASS dc, BD_NAME bdn,
  207. UINT16 hci_handle, UINT8 link_role, tBT_TRANSPORT transport)
  208. {
  209. tBTM_SEC_DEV_REC *p_dev_rec = NULL;
  210. tACL_CONN *p;
  211. UINT8 xx;
  212. BTM_TRACE_DEBUG ("btm_acl_created hci_handle=%d link_role=%d transport=%d\n",
  213. hci_handle, link_role, transport);
  214. /* Ensure we don't have duplicates */
  215. p = btm_bda_to_acl(bda, transport);
  216. if (p != (tACL_CONN *)NULL) {
  217. p->hci_handle = hci_handle;
  218. p->link_role = link_role;
  219. #if BLE_INCLUDED == TRUE
  220. p->transport = transport;
  221. #endif
  222. BTM_TRACE_DEBUG ("Duplicate btm_acl_created: RemBdAddr: %02x%02x%02x%02x%02x%02x\n",
  223. bda[0], bda[1], bda[2], bda[3], bda[4], bda[5]);
  224. BTM_SetLinkPolicy(p->remote_addr, &btm_cb.btm_def_link_policy);
  225. return;
  226. }
  227. /* Allocate acl_db entry */
  228. for (xx = 0, p = &btm_cb.acl_db[0]; xx < MAX_L2CAP_LINKS; xx++, p++) {
  229. if (!p->in_use) {
  230. p->in_use = TRUE;
  231. p->hci_handle = hci_handle;
  232. p->link_role = link_role;
  233. p->link_up_issued = FALSE;
  234. memcpy (p->remote_addr, bda, BD_ADDR_LEN);
  235. /* Set the default version of the peer device to version4.0 before exchange the version with it.
  236. If the peer device act as a master and don't exchange the version with us, then it can only use the
  237. legacy connect instead of secure connection in the pairing step. */
  238. p->lmp_version = HCI_PROTO_VERSION_4_0;
  239. #if BLE_INCLUDED == TRUE
  240. p->transport = transport;
  241. #if BLE_PRIVACY_SPT == TRUE
  242. if (transport == BT_TRANSPORT_LE) {
  243. btm_ble_refresh_local_resolvable_private_addr(bda,
  244. btm_cb.ble_ctr_cb.addr_mgnt_cb.private_addr);
  245. }
  246. #else
  247. p->conn_addr_type = BLE_ADDR_PUBLIC;
  248. memcpy(p->conn_addr, &controller_get_interface()->get_address()->address, BD_ADDR_LEN);
  249. BTM_TRACE_DEBUG ("conn_addr: RemBdAddr: %02x%02x%02x%02x%02x%02x\n",
  250. p->conn_addr[0], p->conn_addr[1], p->conn_addr[2], p->conn_addr[3], p->conn_addr[4], p->conn_addr[5]);
  251. #endif
  252. #endif
  253. p->switch_role_state = BTM_ACL_SWKEY_STATE_IDLE;
  254. btm_pm_sm_alloc(xx);
  255. #if (CLASSIC_BT_INCLUDED == TRUE)
  256. btm_sec_update_legacy_auth_state(p, BTM_ACL_LEGACY_AUTH_NONE);
  257. #endif
  258. if (dc) {
  259. memcpy (p->remote_dc, dc, DEV_CLASS_LEN);
  260. }
  261. if (bdn) {
  262. memcpy (p->remote_name, bdn, BTM_MAX_REM_BD_NAME_LEN);
  263. }
  264. /* if BR/EDR do something more */
  265. if (transport == BT_TRANSPORT_BR_EDR) {
  266. btsnd_hcic_read_rmt_clk_offset (p->hci_handle);
  267. btsnd_hcic_rmt_ver_req (p->hci_handle);
  268. }
  269. p_dev_rec = btm_find_dev_by_handle (hci_handle);
  270. #if (BLE_INCLUDED == TRUE)
  271. if (p_dev_rec ) {
  272. BTM_TRACE_DEBUG ("device_type=0x%x\n", p_dev_rec->device_type);
  273. }
  274. #endif
  275. if (p_dev_rec && !(transport == BT_TRANSPORT_LE)) {
  276. if (!p_dev_rec->remote_secure_connection_previous_state) {
  277. /* If remote features already known, copy them and continue connection setup */
  278. if ((p_dev_rec->num_read_pages) &&
  279. (p_dev_rec->num_read_pages <= (HCI_EXT_FEATURES_PAGE_MAX + 1))) {
  280. memcpy (p->peer_lmp_features, p_dev_rec->features,
  281. (HCI_FEATURE_BYTES_PER_PAGE * p_dev_rec->num_read_pages));
  282. p->num_read_pages = p_dev_rec->num_read_pages;
  283. #if (CLASSIC_BT_INCLUDED == TRUE)
  284. const UINT8 req_pend = (p_dev_rec->sm4 & BTM_SM4_REQ_PEND);
  285. #endif ///CLASSIC_BT_INCLUDED == TRUE
  286. /* Store the Peer Security Capabilites (in SM4 and rmt_sec_caps) */
  287. #if (SMP_INCLUDED == TRUE)
  288. btm_sec_set_peer_sec_caps(p, p_dev_rec);
  289. #endif ///SMP_INCLUDED == TRUE
  290. #if (CLASSIC_BT_INCLUDED == TRUE)
  291. BTM_TRACE_API("%s: pend:%d\n", __FUNCTION__, req_pend);
  292. if (req_pend) {
  293. /* Request for remaining Security Features (if any) */
  294. l2cu_resubmit_pending_sec_req (p_dev_rec->bd_addr);
  295. }
  296. #endif ///CLASSIC_BT_INCLUDED == TRUE
  297. btm_establish_continue (p);
  298. return;
  299. }
  300. } else {
  301. /* If remote features indicated secure connection (SC) mode, check the remote feautres again*/
  302. /* this is to prevent from BIAS attack where attacker can downgrade SC mode*/
  303. btm_read_remote_features (p->hci_handle);
  304. }
  305. }
  306. #if (BLE_INCLUDED == TRUE)
  307. /* If here, features are not known yet */
  308. if (p_dev_rec && transport == BT_TRANSPORT_LE) {
  309. #if BLE_PRIVACY_SPT == TRUE
  310. btm_ble_get_acl_remote_addr (p_dev_rec, p->active_remote_addr,
  311. &p->active_remote_addr_type);
  312. #endif
  313. if (link_role == HCI_ROLE_MASTER) {
  314. btsnd_hcic_ble_read_remote_feat(p->hci_handle);
  315. } else if (HCI_LE_SLAVE_INIT_FEAT_EXC_SUPPORTED(controller_get_interface()->get_features_ble()->as_array)
  316. && link_role == HCI_ROLE_SLAVE) {
  317. btsnd_hcic_rmt_ver_req (p->hci_handle);
  318. } else {
  319. btm_establish_continue(p);
  320. }
  321. } else
  322. #endif
  323. {
  324. btm_read_remote_features (p->hci_handle);
  325. }
  326. /* read page 1 - on rmt feature event for buffer reasons */
  327. return;
  328. }
  329. }
  330. }
  331. /*******************************************************************************
  332. **
  333. ** Function btm_acl_report_role_change
  334. **
  335. ** Description This function is called when the local device is deemed
  336. ** to be down. It notifies L2CAP of the failure.
  337. **
  338. ** Returns void
  339. **
  340. *******************************************************************************/
  341. void btm_acl_report_role_change (UINT8 hci_status, BD_ADDR bda)
  342. {
  343. tBTM_ROLE_SWITCH_CMPL ref_data;
  344. BTM_TRACE_DEBUG ("btm_acl_report_role_change\n");
  345. if (btm_cb.devcb.p_switch_role_cb
  346. && (bda && (0 == memcmp(btm_cb.devcb.switch_role_ref_data.remote_bd_addr, bda, BD_ADDR_LEN)))) {
  347. memcpy (&ref_data, &btm_cb.devcb.switch_role_ref_data, sizeof(tBTM_ROLE_SWITCH_CMPL));
  348. ref_data.hci_status = hci_status;
  349. (*btm_cb.devcb.p_switch_role_cb)(&ref_data);
  350. memset (&btm_cb.devcb.switch_role_ref_data, 0, sizeof(tBTM_ROLE_SWITCH_CMPL));
  351. btm_cb.devcb.p_switch_role_cb = NULL;
  352. }
  353. }
  354. /*******************************************************************************
  355. **
  356. ** Function btm_acl_removed
  357. **
  358. ** Description This function is called by L2CAP when an ACL connection
  359. ** is removed. Since only L2CAP creates ACL links, we use
  360. ** the L2CAP link index as our index into the control blocks.
  361. **
  362. ** Returns void
  363. **
  364. *******************************************************************************/
  365. void btm_acl_removed (BD_ADDR bda, tBT_TRANSPORT transport)
  366. {
  367. tACL_CONN *p;
  368. tBTM_BL_EVENT_DATA evt_data;
  369. #if (defined BLE_INCLUDED && BLE_INCLUDED == TRUE)
  370. tBTM_SEC_DEV_REC *p_dev_rec = NULL;
  371. #endif
  372. BTM_TRACE_DEBUG ("btm_acl_removed\n");
  373. p = btm_bda_to_acl(bda, transport);
  374. if (p != (tACL_CONN *)NULL) {
  375. p->in_use = FALSE;
  376. /* if the disconnected channel has a pending role switch, clear it now */
  377. btm_acl_report_role_change(HCI_ERR_NO_CONNECTION, bda);
  378. /* Only notify if link up has had a chance to be issued */
  379. if (p->link_up_issued) {
  380. p->link_up_issued = FALSE;
  381. /* If anyone cares, tell him database changed */
  382. if (btm_cb.p_bl_changed_cb) {
  383. evt_data.event = BTM_BL_DISCN_EVT;
  384. evt_data.discn.p_bda = bda;
  385. #if BLE_INCLUDED == TRUE
  386. evt_data.discn.handle = p->hci_handle;
  387. evt_data.discn.transport = p->transport;
  388. #endif
  389. (*btm_cb.p_bl_changed_cb)(&evt_data);
  390. }
  391. btm_acl_update_busy_level (BTM_BLI_ACL_DOWN_EVT);
  392. }
  393. #if (defined BLE_INCLUDED && BLE_INCLUDED == TRUE)
  394. BTM_TRACE_DEBUG ("acl hci_handle=%d transport=%d connectable_mode=0x%0x link_role=%d\n",
  395. p->hci_handle,
  396. p->transport,
  397. btm_cb.ble_ctr_cb.inq_var.connectable_mode,
  398. p->link_role);
  399. p_dev_rec = btm_find_dev(bda);
  400. if ( p_dev_rec) {
  401. BTM_TRACE_DEBUG("before update p_dev_rec->sec_flags=0x%x\n", p_dev_rec->sec_flags);
  402. if (p->transport == BT_TRANSPORT_LE) {
  403. BTM_TRACE_DEBUG("LE link down\n");
  404. p_dev_rec->sec_flags &= ~(BTM_SEC_LE_ENCRYPTED | BTM_SEC_ROLE_SWITCHED);
  405. if ( (p_dev_rec->sec_flags & BTM_SEC_LE_LINK_KEY_KNOWN) == 0) {
  406. BTM_TRACE_DEBUG("Not Bonded\n");
  407. p_dev_rec->sec_flags &= ~(BTM_SEC_LE_LINK_KEY_AUTHED | BTM_SEC_LE_AUTHENTICATED);
  408. } else {
  409. BTM_TRACE_DEBUG("Bonded\n");
  410. }
  411. } else {
  412. BTM_TRACE_DEBUG("Bletooth link down\n");
  413. p_dev_rec->sec_flags &= ~(BTM_SEC_AUTHORIZED | BTM_SEC_AUTHENTICATED
  414. | BTM_SEC_ENCRYPTED | BTM_SEC_ROLE_SWITCHED);
  415. }
  416. BTM_TRACE_DEBUG("after update p_dev_rec->sec_flags=0x%x\n", p_dev_rec->sec_flags);
  417. } else {
  418. BTM_TRACE_ERROR("Device not found\n");
  419. }
  420. #endif
  421. /* Clear the ACL connection data */
  422. memset(p, 0, sizeof(tACL_CONN));
  423. }
  424. }
  425. /*******************************************************************************
  426. **
  427. ** Function btm_acl_device_down
  428. **
  429. ** Description This function is called when the local device is deemed
  430. ** to be down. It notifies L2CAP of the failure.
  431. **
  432. ** Returns void
  433. **
  434. *******************************************************************************/
  435. void btm_acl_device_down (void)
  436. {
  437. tACL_CONN *p = &btm_cb.acl_db[0];
  438. UINT16 xx;
  439. BTM_TRACE_DEBUG ("btm_acl_device_down\n");
  440. for (xx = 0; xx < MAX_L2CAP_LINKS; xx++, p++) {
  441. if (p->in_use) {
  442. BTM_TRACE_DEBUG ("hci_handle=%d HCI_ERR_HW_FAILURE \n", p->hci_handle );
  443. l2c_link_hci_disc_comp (p->hci_handle, HCI_ERR_HW_FAILURE);
  444. }
  445. }
  446. }
  447. /*******************************************************************************
  448. **
  449. ** Function btm_acl_update_busy_level
  450. **
  451. ** Description This function is called to update the busy level of the system
  452. ** .
  453. **
  454. ** Returns void
  455. **
  456. *******************************************************************************/
  457. void btm_acl_update_busy_level (tBTM_BLI_EVENT event)
  458. {
  459. tBTM_BL_UPDATE_DATA evt;
  460. UINT8 busy_level;
  461. BTM_TRACE_DEBUG ("btm_acl_update_busy_level\n");
  462. BOOLEAN old_inquiry_state = btm_cb.is_inquiry;
  463. switch (event) {
  464. case BTM_BLI_ACL_UP_EVT:
  465. BTM_TRACE_DEBUG ("BTM_BLI_ACL_UP_EVT\n");
  466. break;
  467. case BTM_BLI_ACL_DOWN_EVT:
  468. BTM_TRACE_DEBUG ("BTM_BLI_ACL_DOWN_EVT\n");
  469. break;
  470. case BTM_BLI_PAGE_EVT:
  471. BTM_TRACE_DEBUG ("BTM_BLI_PAGE_EVT\n");
  472. btm_cb.is_paging = TRUE;
  473. evt.busy_level_flags = BTM_BL_PAGING_STARTED;
  474. break;
  475. case BTM_BLI_PAGE_DONE_EVT:
  476. BTM_TRACE_DEBUG ("BTM_BLI_PAGE_DONE_EVT\n");
  477. btm_cb.is_paging = FALSE;
  478. evt.busy_level_flags = BTM_BL_PAGING_COMPLETE;
  479. break;
  480. case BTM_BLI_INQ_EVT:
  481. BTM_TRACE_DEBUG ("BTM_BLI_INQ_EVT\n");
  482. btm_cb.is_inquiry = TRUE;
  483. evt.busy_level_flags = BTM_BL_INQUIRY_STARTED;
  484. break;
  485. case BTM_BLI_INQ_CANCEL_EVT:
  486. BTM_TRACE_DEBUG ("BTM_BLI_INQ_CANCEL_EVT\n");
  487. btm_cb.is_inquiry = FALSE;
  488. evt.busy_level_flags = BTM_BL_INQUIRY_CANCELLED;
  489. break;
  490. case BTM_BLI_INQ_DONE_EVT:
  491. BTM_TRACE_DEBUG ("BTM_BLI_INQ_DONE_EVT\n");
  492. btm_cb.is_inquiry = FALSE;
  493. evt.busy_level_flags = BTM_BL_INQUIRY_COMPLETE;
  494. break;
  495. }
  496. if (btm_cb.is_paging || btm_cb.is_inquiry) {
  497. busy_level = 10;
  498. } else {
  499. busy_level = BTM_GetNumAclLinks();
  500. }
  501. if ((busy_level != btm_cb.busy_level) || (old_inquiry_state != btm_cb.is_inquiry)) {
  502. evt.event = BTM_BL_UPDATE_EVT;
  503. evt.busy_level = busy_level;
  504. btm_cb.busy_level = busy_level;
  505. if (btm_cb.p_bl_changed_cb && (btm_cb.bl_evt_mask & BTM_BL_UPDATE_MASK)) {
  506. (*btm_cb.p_bl_changed_cb)((tBTM_BL_EVENT_DATA *)&evt);
  507. }
  508. }
  509. }
  510. /*******************************************************************************
  511. **
  512. ** Function BTM_GetRole
  513. **
  514. ** Description This function is called to get the role of the local device
  515. ** for the ACL connection with the specified remote device
  516. **
  517. ** Returns BTM_SUCCESS if connection exists.
  518. ** BTM_UNKNOWN_ADDR if no active link with bd addr specified
  519. **
  520. *******************************************************************************/
  521. tBTM_STATUS BTM_GetRole (BD_ADDR remote_bd_addr, UINT8 *p_role)
  522. {
  523. tACL_CONN *p;
  524. BTM_TRACE_DEBUG ("BTM_GetRole\n");
  525. if ((p = btm_bda_to_acl(remote_bd_addr, BT_TRANSPORT_BR_EDR)) == NULL) {
  526. *p_role = BTM_ROLE_UNDEFINED;
  527. return (BTM_UNKNOWN_ADDR);
  528. }
  529. /* Get the current role */
  530. *p_role = p->link_role;
  531. return (BTM_SUCCESS);
  532. }
  533. /*******************************************************************************
  534. **
  535. ** Function BTM_SwitchRole
  536. **
  537. ** Description This function is called to switch role between master and
  538. ** slave. If role is already set it will do nothing. If the
  539. ** command was initiated, the callback function is called upon
  540. ** completion.
  541. **
  542. ** Returns BTM_SUCCESS if already in specified role.
  543. ** BTM_CMD_STARTED if command issued to controller.
  544. ** BTM_NO_RESOURCES if couldn't allocate memory to issue command
  545. ** BTM_UNKNOWN_ADDR if no active link with bd addr specified
  546. ** BTM_MODE_UNSUPPORTED if local device does not support role switching
  547. ** BTM_BUSY if the previous command is not completed
  548. **
  549. *******************************************************************************/
  550. tBTM_STATUS BTM_SwitchRole (BD_ADDR remote_bd_addr, UINT8 new_role, tBTM_CMPL_CB *p_cb)
  551. {
  552. tACL_CONN *p;
  553. tBTM_SEC_DEV_REC *p_dev_rec = NULL;
  554. #if BTM_SCO_INCLUDED == TRUE
  555. BOOLEAN is_sco_active;
  556. #endif
  557. tBTM_STATUS status;
  558. tBTM_PM_MODE pwr_mode;
  559. tBTM_PM_PWR_MD settings;
  560. #if (BT_USE_TRACES == TRUE)
  561. BD_ADDR_PTR p_bda;
  562. #endif
  563. BTM_TRACE_API ("BTM_SwitchRole BDA: %02x-%02x-%02x-%02x-%02x-%02x\n",
  564. remote_bd_addr[0], remote_bd_addr[1], remote_bd_addr[2],
  565. remote_bd_addr[3], remote_bd_addr[4], remote_bd_addr[5]);
  566. /* Make sure the local device supports switching */
  567. if (!controller_get_interface()->supports_master_slave_role_switch()) {
  568. return (BTM_MODE_UNSUPPORTED);
  569. }
  570. if (btm_cb.devcb.p_switch_role_cb && p_cb) {
  571. #if (BT_USE_TRACES == TRUE)
  572. p_bda = btm_cb.devcb.switch_role_ref_data.remote_bd_addr;
  573. BTM_TRACE_DEBUG ("Role switch on other device is in progress 0x%02x%02x%02x%02x%02x%02x\n",
  574. p_bda[0], p_bda[1], p_bda[2],
  575. p_bda[3], p_bda[4], p_bda[5]);
  576. #endif
  577. return (BTM_BUSY);
  578. }
  579. if ((p = btm_bda_to_acl(remote_bd_addr, BT_TRANSPORT_BR_EDR)) == NULL) {
  580. return (BTM_UNKNOWN_ADDR);
  581. }
  582. /* Finished if already in desired role */
  583. if (p->link_role == new_role) {
  584. return (BTM_SUCCESS);
  585. }
  586. #if BTM_SCO_INCLUDED == TRUE
  587. /* Check if there is any SCO Active on this BD Address */
  588. is_sco_active = btm_is_sco_active_by_bdaddr(remote_bd_addr);
  589. if (is_sco_active == TRUE) {
  590. return (BTM_NO_RESOURCES);
  591. }
  592. #endif
  593. /* Ignore role switch request if the previous request was not completed */
  594. if (p->switch_role_state != BTM_ACL_SWKEY_STATE_IDLE) {
  595. BTM_TRACE_DEBUG ("BTM_SwitchRole busy: %d\n",
  596. p->switch_role_state);
  597. return (BTM_BUSY);
  598. }
  599. if ((status = BTM_ReadPowerMode(p->remote_addr, &pwr_mode)) != BTM_SUCCESS) {
  600. return (status);
  601. }
  602. /* Wake up the link if in sniff or park before attempting switch */
  603. if (pwr_mode == BTM_PM_MD_PARK || pwr_mode == BTM_PM_MD_SNIFF) {
  604. memset( (void *)&settings, 0, sizeof(settings));
  605. settings.mode = BTM_PM_MD_ACTIVE;
  606. status = BTM_SetPowerMode (BTM_PM_SET_ONLY_ID, p->remote_addr, &settings);
  607. if (status != BTM_CMD_STARTED) {
  608. return (BTM_WRONG_MODE);
  609. }
  610. p->switch_role_state = BTM_ACL_SWKEY_STATE_MODE_CHANGE;
  611. }
  612. /* some devices do not support switch while encryption is on */
  613. else {
  614. p_dev_rec = btm_find_dev (remote_bd_addr);
  615. if ((p_dev_rec != NULL)
  616. && ((p_dev_rec->sec_flags & BTM_SEC_ENCRYPTED) != 0)
  617. && !BTM_EPR_AVAILABLE(p)) {
  618. /* bypass turning off encryption if change link key is already doing it */
  619. if (p->encrypt_state != BTM_ACL_ENCRYPT_STATE_ENCRYPT_OFF) {
  620. if (!btsnd_hcic_set_conn_encrypt (p->hci_handle, FALSE)) {
  621. return (BTM_NO_RESOURCES);
  622. } else {
  623. p->encrypt_state = BTM_ACL_ENCRYPT_STATE_ENCRYPT_OFF;
  624. }
  625. }
  626. p->switch_role_state = BTM_ACL_SWKEY_STATE_ENCRYPTION_OFF;
  627. } else {
  628. if (!btsnd_hcic_switch_role (remote_bd_addr, new_role)) {
  629. return (BTM_NO_RESOURCES);
  630. }
  631. p->switch_role_state = BTM_ACL_SWKEY_STATE_IN_PROGRESS;
  632. #if BTM_DISC_DURING_RS == TRUE
  633. if (p_dev_rec) {
  634. p_dev_rec->rs_disc_pending = BTM_SEC_RS_PENDING;
  635. }
  636. #endif
  637. }
  638. }
  639. /* Initialize return structure in case request fails */
  640. if (p_cb) {
  641. memcpy (btm_cb.devcb.switch_role_ref_data.remote_bd_addr, remote_bd_addr,
  642. BD_ADDR_LEN);
  643. btm_cb.devcb.switch_role_ref_data.role = new_role;
  644. /* initialized to an error code */
  645. btm_cb.devcb.switch_role_ref_data.hci_status = HCI_ERR_UNSUPPORTED_VALUE;
  646. btm_cb.devcb.p_switch_role_cb = p_cb;
  647. }
  648. return (BTM_CMD_STARTED);
  649. }
  650. /*******************************************************************************
  651. **
  652. ** Function btm_acl_encrypt_change
  653. **
  654. ** Description This function is when encryption of the connection is
  655. ** completed by the LM. Checks to see if a role switch or
  656. ** change of link key was active and initiates or continues
  657. ** process if needed.
  658. **
  659. ** Returns void
  660. **
  661. *******************************************************************************/
  662. void btm_acl_encrypt_change (UINT16 handle, UINT8 status, UINT8 encr_enable)
  663. {
  664. tACL_CONN *p;
  665. UINT8 xx;
  666. tBTM_SEC_DEV_REC *p_dev_rec;
  667. tBTM_BL_ROLE_CHG_DATA evt;
  668. BTM_TRACE_DEBUG ("btm_acl_encrypt_change handle=%d status=%d encr_enabl=%d\n",
  669. handle, status, encr_enable);
  670. xx = btm_handle_to_acl_index(handle);
  671. /* don't assume that we can never get a bad hci_handle */
  672. if (xx < MAX_L2CAP_LINKS) {
  673. p = &btm_cb.acl_db[xx];
  674. } else {
  675. return;
  676. }
  677. /* Process Role Switch if active */
  678. if (p->switch_role_state == BTM_ACL_SWKEY_STATE_ENCRYPTION_OFF) {
  679. /* if encryption turn off failed we still will try to switch role */
  680. if (encr_enable) {
  681. p->switch_role_state = BTM_ACL_SWKEY_STATE_IDLE;
  682. p->encrypt_state = BTM_ACL_ENCRYPT_STATE_IDLE;
  683. } else {
  684. p->switch_role_state = BTM_ACL_SWKEY_STATE_SWITCHING;
  685. p->encrypt_state = BTM_ACL_ENCRYPT_STATE_TEMP_FUNC;
  686. }
  687. if (!btsnd_hcic_switch_role (p->remote_addr, (UINT8)!p->link_role)) {
  688. p->switch_role_state = BTM_ACL_SWKEY_STATE_IDLE;
  689. p->encrypt_state = BTM_ACL_ENCRYPT_STATE_IDLE;
  690. btm_acl_report_role_change(btm_cb.devcb.switch_role_ref_data.hci_status, p->remote_addr);
  691. }
  692. #if BTM_DISC_DURING_RS == TRUE
  693. else {
  694. if ((p_dev_rec = btm_find_dev (p->remote_addr)) != NULL) {
  695. p_dev_rec->rs_disc_pending = BTM_SEC_RS_PENDING;
  696. }
  697. }
  698. #endif
  699. }
  700. /* Finished enabling Encryption after role switch */
  701. else if (p->switch_role_state == BTM_ACL_SWKEY_STATE_ENCRYPTION_ON) {
  702. p->switch_role_state = BTM_ACL_SWKEY_STATE_IDLE;
  703. p->encrypt_state = BTM_ACL_ENCRYPT_STATE_IDLE;
  704. btm_acl_report_role_change(btm_cb.devcb.switch_role_ref_data.hci_status, p->remote_addr);
  705. /* if role change event is registered, report it now */
  706. if (btm_cb.p_bl_changed_cb && (btm_cb.bl_evt_mask & BTM_BL_ROLE_CHG_MASK)) {
  707. evt.event = BTM_BL_ROLE_CHG_EVT;
  708. evt.new_role = btm_cb.devcb.switch_role_ref_data.role;
  709. evt.p_bda = btm_cb.devcb.switch_role_ref_data.remote_bd_addr;
  710. evt.hci_status = btm_cb.devcb.switch_role_ref_data.hci_status;
  711. (*btm_cb.p_bl_changed_cb)((tBTM_BL_EVENT_DATA *)&evt);
  712. BTM_TRACE_DEBUG("Role Switch Event: new_role 0x%02x, HCI Status 0x%02x, rs_st:%d\n",
  713. evt.new_role, evt.hci_status, p->switch_role_state);
  714. }
  715. #if BTM_DISC_DURING_RS == TRUE
  716. /* If a disconnect is pending, issue it now that role switch has completed */
  717. if ((p_dev_rec = btm_find_dev (p->remote_addr)) != NULL) {
  718. if (p_dev_rec->rs_disc_pending == BTM_SEC_DISC_PENDING) {
  719. BTM_TRACE_WARNING("btm_acl_encrypt_change -> Issuing delayed HCI_Disconnect!!!\n");
  720. btsnd_hcic_disconnect(p_dev_rec->hci_handle, HCI_ERR_PEER_USER);
  721. }
  722. BTM_TRACE_WARNING("btm_acl_encrypt_change: tBTM_SEC_DEV:0x%x rs_disc_pending=%d\n",
  723. (UINT32)p_dev_rec, p_dev_rec->rs_disc_pending);
  724. p_dev_rec->rs_disc_pending = BTM_SEC_RS_NOT_PENDING; /* reset flag */
  725. }
  726. #endif
  727. }
  728. #if (CLASSIC_BT_INCLUDED == TRUE)
  729. /* If authentication is done through legacy authentication and esp32 has
  730. * not authenticated peer deivce yet, do not proceed for encrytion and
  731. * first authenticate it. */
  732. else if ((BTM_BothEndsSupportSecureConnections(p->remote_addr) == 0) &&
  733. ((p->legacy_auth_state & BTM_ACL_LEGACY_AUTH_SELF) == 0)) {
  734. if ((p_dev_rec = btm_find_dev (p->remote_addr)) != NULL) {
  735. if (btm_sec_legacy_authentication_mutual(p_dev_rec)) {
  736. btm_sec_update_legacy_auth_state(btm_bda_to_acl(p_dev_rec->bd_addr, BT_TRANSPORT_BR_EDR), BTM_ACL_LEGACY_AUTH_SELF);
  737. } else {
  738. BTM_TRACE_ERROR("%s failed, Resources not available for Authentication procedure", __FUNCTION__);
  739. }
  740. }
  741. }
  742. #endif
  743. }
  744. /*******************************************************************************
  745. **
  746. ** Function BTM_SetLinkPolicy
  747. **
  748. ** Description Create and send HCI "Write Policy Set" command
  749. **
  750. ** Returns status of the operation
  751. **
  752. *******************************************************************************/
  753. tBTM_STATUS BTM_SetLinkPolicy (BD_ADDR remote_bda, UINT16 *settings)
  754. {
  755. tACL_CONN *p;
  756. UINT8 *localFeatures = BTM_ReadLocalFeatures();
  757. BTM_TRACE_DEBUG ("BTM_SetLinkPolicy\n");
  758. /* BTM_TRACE_API ("BTM_SetLinkPolicy: requested settings: 0x%04x", *settings ); */
  759. /* First, check if hold mode is supported */
  760. if (*settings != HCI_DISABLE_ALL_LM_MODES) {
  761. if ( (*settings & HCI_ENABLE_MASTER_SLAVE_SWITCH) && (!HCI_SWITCH_SUPPORTED(localFeatures)) ) {
  762. *settings &= (~HCI_ENABLE_MASTER_SLAVE_SWITCH);
  763. BTM_TRACE_API ("BTM_SetLinkPolicy switch not supported (settings: 0x%04x)\n", *settings );
  764. }
  765. if ( (*settings & HCI_ENABLE_HOLD_MODE) && (!HCI_HOLD_MODE_SUPPORTED(localFeatures)) ) {
  766. *settings &= (~HCI_ENABLE_HOLD_MODE);
  767. BTM_TRACE_API ("BTM_SetLinkPolicy hold not supported (settings: 0x%04x)\n", *settings );
  768. }
  769. if ( (*settings & HCI_ENABLE_SNIFF_MODE) && (!HCI_SNIFF_MODE_SUPPORTED(localFeatures)) ) {
  770. *settings &= (~HCI_ENABLE_SNIFF_MODE);
  771. BTM_TRACE_API ("BTM_SetLinkPolicy sniff not supported (settings: 0x%04x)\n", *settings );
  772. }
  773. if ( (*settings & HCI_ENABLE_PARK_MODE) && (!HCI_PARK_MODE_SUPPORTED(localFeatures)) ) {
  774. *settings &= (~HCI_ENABLE_PARK_MODE);
  775. BTM_TRACE_API ("BTM_SetLinkPolicy park not supported (settings: 0x%04x)\n", *settings );
  776. }
  777. }
  778. if ((p = btm_bda_to_acl(remote_bda, BT_TRANSPORT_BR_EDR)) != NULL) {
  779. return (btsnd_hcic_write_policy_set (p->hci_handle, *settings) ? BTM_CMD_STARTED : BTM_NO_RESOURCES);
  780. }
  781. /* If here, no BD Addr found */
  782. return (BTM_UNKNOWN_ADDR);
  783. }
  784. /*******************************************************************************
  785. **
  786. ** Function BTM_SetDefaultLinkPolicy
  787. **
  788. ** Description Set the default value for HCI "Write Policy Set" command
  789. ** to use when an ACL link is created.
  790. **
  791. ** Returns void
  792. **
  793. *******************************************************************************/
  794. void BTM_SetDefaultLinkPolicy (UINT16 settings)
  795. {
  796. UINT8 *localFeatures = BTM_ReadLocalFeatures();
  797. BTM_TRACE_DEBUG("BTM_SetDefaultLinkPolicy setting:0x%04x\n", settings);
  798. if ((settings & HCI_ENABLE_MASTER_SLAVE_SWITCH) && (!HCI_SWITCH_SUPPORTED(localFeatures))) {
  799. settings &= ~HCI_ENABLE_MASTER_SLAVE_SWITCH;
  800. BTM_TRACE_DEBUG("BTM_SetDefaultLinkPolicy switch not supported (settings: 0x%04x)\n", settings);
  801. }
  802. if ((settings & HCI_ENABLE_HOLD_MODE) && (!HCI_HOLD_MODE_SUPPORTED(localFeatures))) {
  803. settings &= ~HCI_ENABLE_HOLD_MODE;
  804. BTM_TRACE_DEBUG("BTM_SetDefaultLinkPolicy hold not supported (settings: 0x%04x)\n", settings);
  805. }
  806. if ((settings & HCI_ENABLE_SNIFF_MODE) && (!HCI_SNIFF_MODE_SUPPORTED(localFeatures))) {
  807. settings &= ~HCI_ENABLE_SNIFF_MODE;
  808. BTM_TRACE_DEBUG("BTM_SetDefaultLinkPolicy sniff not supported (settings: 0x%04x)\n", settings);
  809. }
  810. if ((settings & HCI_ENABLE_PARK_MODE) && (!HCI_PARK_MODE_SUPPORTED(localFeatures))) {
  811. settings &= ~HCI_ENABLE_PARK_MODE;
  812. BTM_TRACE_DEBUG("BTM_SetDefaultLinkPolicy park not supported (settings: 0x%04x)\n", settings);
  813. }
  814. BTM_TRACE_DEBUG("Set DefaultLinkPolicy:0x%04x\n", settings);
  815. btm_cb.btm_def_link_policy = settings;
  816. /* Set the default Link Policy of the controller */
  817. btsnd_hcic_write_def_policy_set(settings);
  818. }
  819. /*******************************************************************************
  820. **
  821. ** Function btm_read_remote_version_complete
  822. **
  823. ** Description This function is called when the command complete message
  824. ** is received from the HCI for the remote version info.
  825. **
  826. ** Returns void
  827. **
  828. *******************************************************************************/
  829. void btm_read_remote_version_complete (UINT8 *p)
  830. {
  831. tACL_CONN *p_acl_cb = &btm_cb.acl_db[0];
  832. UINT8 status;
  833. UINT16 handle;
  834. int xx;
  835. BTM_TRACE_DEBUG ("btm_read_remote_version_complete\n");
  836. STREAM_TO_UINT8 (status, p);
  837. STREAM_TO_UINT16 (handle, p);
  838. /* Look up the connection by handle and copy features */
  839. for (xx = 0; xx < MAX_L2CAP_LINKS; xx++, p_acl_cb++) {
  840. if ((p_acl_cb->in_use) && (p_acl_cb->hci_handle == handle)) {
  841. if (status == HCI_SUCCESS) {
  842. STREAM_TO_UINT8 (p_acl_cb->lmp_version, p);
  843. STREAM_TO_UINT16 (p_acl_cb->manufacturer, p);
  844. STREAM_TO_UINT16 (p_acl_cb->lmp_subversion, p);
  845. }
  846. #if BLE_INCLUDED == TRUE
  847. if (p_acl_cb->transport == BT_TRANSPORT_LE) {
  848. if(p_acl_cb->link_role == HCI_ROLE_MASTER) {
  849. if (HCI_LE_DATA_LEN_EXT_SUPPORTED(p_acl_cb->peer_le_features)) {
  850. uint16_t data_length = controller_get_interface()->get_ble_default_data_packet_length();
  851. uint16_t data_txtime = controller_get_interface()->get_ble_default_data_packet_txtime();
  852. if (data_length != p_acl_cb->data_length_params.tx_len) {
  853. p_acl_cb->data_len_updating = true;
  854. btsnd_hcic_ble_set_data_length(p_acl_cb->hci_handle, data_length, data_txtime);
  855. }
  856. }
  857. l2cble_notify_le_connection (p_acl_cb->remote_addr);
  858. } else {
  859. //slave role, read remote feature
  860. btsnd_hcic_ble_read_remote_feat(p_acl_cb->hci_handle);
  861. }
  862. }
  863. #endif
  864. break;
  865. }
  866. }
  867. }
  868. /*******************************************************************************
  869. **
  870. ** Function btm_process_remote_ext_features
  871. **
  872. ** Description Local function called to process all extended features pages
  873. ** read from a remote device.
  874. **
  875. ** Returns void
  876. **
  877. *******************************************************************************/
  878. void btm_process_remote_ext_features (tACL_CONN *p_acl_cb, UINT8 num_read_pages)
  879. {
  880. UINT16 handle = p_acl_cb->hci_handle;
  881. tBTM_SEC_DEV_REC *p_dev_rec = btm_find_dev_by_handle (handle);
  882. UINT8 page_idx;
  883. BTM_TRACE_DEBUG ("btm_process_remote_ext_features\n");
  884. /* Make sure we have the record to save remote features information */
  885. if (p_dev_rec == NULL) {
  886. /* Get a new device; might be doing dedicated bonding */
  887. p_dev_rec = btm_find_or_alloc_dev (p_acl_cb->remote_addr);
  888. }
  889. p_acl_cb->num_read_pages = num_read_pages;
  890. p_dev_rec->num_read_pages = num_read_pages;
  891. /* Move the pages to placeholder */
  892. for (page_idx = 0; page_idx < num_read_pages; page_idx++) {
  893. if (page_idx > HCI_EXT_FEATURES_PAGE_MAX) {
  894. BTM_TRACE_ERROR("%s: page=%d unexpected\n", __FUNCTION__, page_idx);
  895. break;
  896. }
  897. memcpy (p_dev_rec->features[page_idx], p_acl_cb->peer_lmp_features[page_idx],
  898. HCI_FEATURE_BYTES_PER_PAGE);
  899. }
  900. const UINT8 req_pend = (p_dev_rec->sm4 & BTM_SM4_REQ_PEND);
  901. #if (SMP_INCLUDED == TRUE)
  902. /* Store the Peer Security Capabilites (in SM4 and rmt_sec_caps) */
  903. btm_sec_set_peer_sec_caps(p_acl_cb, p_dev_rec);
  904. #endif ///SMP_INCLUDED == TRUE
  905. BTM_TRACE_API("%s: pend:%d\n", __FUNCTION__, req_pend);
  906. if (req_pend) {
  907. #if (CLASSIC_BT_INCLUDED == TRUE)
  908. /* Request for remaining Security Features (if any) */
  909. l2cu_resubmit_pending_sec_req (p_dev_rec->bd_addr);
  910. #endif ///CLASSIC_BT_INCLUDED == TRUE
  911. }
  912. }
  913. /*******************************************************************************
  914. **
  915. ** Function btm_read_remote_features
  916. **
  917. ** Description Local function called to send a read remote supported features/
  918. ** remote extended features page[0].
  919. **
  920. ** Returns void
  921. **
  922. *******************************************************************************/
  923. void btm_read_remote_features (UINT16 handle)
  924. {
  925. UINT8 acl_idx;
  926. tACL_CONN *p_acl_cb;
  927. BTM_TRACE_DEBUG("btm_read_remote_features() handle: %d\n", handle);
  928. if ((acl_idx = btm_handle_to_acl_index(handle)) >= MAX_L2CAP_LINKS) {
  929. BTM_TRACE_ERROR("btm_read_remote_features handle=%d invalid\n", handle);
  930. return;
  931. }
  932. p_acl_cb = &btm_cb.acl_db[acl_idx];
  933. p_acl_cb->num_read_pages = 0;
  934. memset (p_acl_cb->peer_lmp_features, 0, sizeof(p_acl_cb->peer_lmp_features));
  935. /* first send read remote supported features HCI command */
  936. /* because we don't know whether the remote support extended feature command */
  937. btsnd_hcic_rmt_features_req (handle);
  938. }
  939. /*******************************************************************************
  940. **
  941. ** Function btm_read_remote_ext_features
  942. **
  943. ** Description Local function called to send a read remote extended features
  944. **
  945. ** Returns void
  946. **
  947. *******************************************************************************/
  948. void btm_read_remote_ext_features (UINT16 handle, UINT8 page_number)
  949. {
  950. BTM_TRACE_DEBUG("btm_read_remote_ext_features() handle: %d page: %d\n", handle, page_number);
  951. btsnd_hcic_rmt_ext_features(handle, page_number);
  952. }
  953. /*******************************************************************************
  954. **
  955. ** Function btm_read_remote_features_complete
  956. **
  957. ** Description This function is called when the remote supported features
  958. ** complete event is received from the HCI.
  959. **
  960. ** Returns void
  961. **
  962. *******************************************************************************/
  963. void btm_read_remote_features_complete (UINT8 *p)
  964. {
  965. tACL_CONN *p_acl_cb;
  966. UINT8 status;
  967. UINT16 handle;
  968. UINT8 acl_idx;
  969. BTM_TRACE_DEBUG ("btm_read_remote_features_complete\n");
  970. STREAM_TO_UINT8 (status, p);
  971. if (status != HCI_SUCCESS) {
  972. BTM_TRACE_ERROR ("btm_read_remote_features_complete failed (status 0x%02x)\n", status);
  973. return;
  974. }
  975. STREAM_TO_UINT16 (handle, p);
  976. if ((acl_idx = btm_handle_to_acl_index(handle)) >= MAX_L2CAP_LINKS) {
  977. BTM_TRACE_ERROR("btm_read_remote_features_complete handle=%d invalid\n", handle);
  978. return;
  979. }
  980. p_acl_cb = &btm_cb.acl_db[acl_idx];
  981. /* Copy the received features page */
  982. STREAM_TO_ARRAY(p_acl_cb->peer_lmp_features[HCI_EXT_FEATURES_PAGE_0], p,
  983. HCI_FEATURE_BYTES_PER_PAGE);
  984. if ((HCI_LMP_EXTENDED_SUPPORTED(p_acl_cb->peer_lmp_features[HCI_EXT_FEATURES_PAGE_0])) &&
  985. (controller_get_interface()->supports_reading_remote_extended_features())) {
  986. /* if the remote controller has extended features and local controller supports
  987. ** HCI_Read_Remote_Extended_Features command then start reading these feature starting
  988. ** with extended features page 1 */
  989. BTM_TRACE_DEBUG ("Start reading remote extended features\n");
  990. btm_read_remote_ext_features(handle, HCI_EXT_FEATURES_PAGE_1);
  991. return;
  992. }
  993. /* Remote controller has no extended features. Process remote controller supported features
  994. (features page HCI_EXT_FEATURES_PAGE_0). */
  995. btm_process_remote_ext_features (p_acl_cb, 1);
  996. /* Continue with HCI connection establishment */
  997. btm_establish_continue (p_acl_cb);
  998. }
  999. /*******************************************************************************
  1000. **
  1001. ** Function btm_read_remote_ext_features_complete
  1002. **
  1003. ** Description This function is called when the remote extended features
  1004. ** complete event is received from the HCI.
  1005. **
  1006. ** Returns void
  1007. **
  1008. *******************************************************************************/
  1009. void btm_read_remote_ext_features_complete (UINT8 *p)
  1010. {
  1011. tACL_CONN *p_acl_cb;
  1012. UINT8 page_num, max_page;
  1013. UINT16 handle;
  1014. UINT8 acl_idx;
  1015. BTM_TRACE_DEBUG ("btm_read_remote_ext_features_complete\n");
  1016. ++p;
  1017. STREAM_TO_UINT16 (handle, p);
  1018. STREAM_TO_UINT8 (page_num, p);
  1019. STREAM_TO_UINT8 (max_page, p);
  1020. /* Validate parameters */
  1021. if ((acl_idx = btm_handle_to_acl_index(handle)) >= MAX_L2CAP_LINKS) {
  1022. BTM_TRACE_ERROR("btm_read_remote_ext_features_complete handle=%d invalid\n", handle);
  1023. return;
  1024. }
  1025. if (max_page > HCI_EXT_FEATURES_PAGE_MAX) {
  1026. BTM_TRACE_ERROR("btm_read_remote_ext_features_complete page=%d unknown", max_page);
  1027. }
  1028. p_acl_cb = &btm_cb.acl_db[acl_idx];
  1029. /* Copy the received features page */
  1030. STREAM_TO_ARRAY(p_acl_cb->peer_lmp_features[page_num], p, HCI_FEATURE_BYTES_PER_PAGE);
  1031. /* If there is the next remote features page and
  1032. * we have space to keep this page data - read this page */
  1033. if ((page_num < max_page) && (page_num < HCI_EXT_FEATURES_PAGE_MAX)) {
  1034. page_num++;
  1035. BTM_TRACE_DEBUG("BTM reads next remote extended features page (%d)\n", page_num);
  1036. btm_read_remote_ext_features (handle, page_num);
  1037. return;
  1038. }
  1039. /* Reading of remote feature pages is complete */
  1040. BTM_TRACE_DEBUG("BTM reached last remote extended features page (%d)\n", page_num);
  1041. /* Process the pages */
  1042. btm_process_remote_ext_features (p_acl_cb, (UINT8) (page_num + 1));
  1043. /* Continue with HCI connection establishment */
  1044. btm_establish_continue (p_acl_cb);
  1045. }
  1046. /*******************************************************************************
  1047. **
  1048. ** Function btm_read_remote_ext_features_failed
  1049. **
  1050. ** Description This function is called when the remote extended features
  1051. ** complete event returns a failed status.
  1052. **
  1053. ** Returns void
  1054. **
  1055. *******************************************************************************/
  1056. void btm_read_remote_ext_features_failed (UINT8 status, UINT16 handle)
  1057. {
  1058. tACL_CONN *p_acl_cb;
  1059. UINT8 acl_idx;
  1060. BTM_TRACE_WARNING ("btm_read_remote_ext_features_failed (status 0x%02x) for handle %d\n",
  1061. status, handle);
  1062. if ((acl_idx = btm_handle_to_acl_index(handle)) >= MAX_L2CAP_LINKS) {
  1063. BTM_TRACE_ERROR("btm_read_remote_ext_features_failed handle=%d invalid\n", handle);
  1064. return;
  1065. }
  1066. p_acl_cb = &btm_cb.acl_db[acl_idx];
  1067. /* Process supported features only */
  1068. btm_process_remote_ext_features (p_acl_cb, 1);
  1069. /* Continue HCI connection establishment */
  1070. btm_establish_continue (p_acl_cb);
  1071. }
  1072. /*******************************************************************************
  1073. **
  1074. ** Function btm_establish_continue
  1075. **
  1076. ** Description This function is called when the command complete message
  1077. ** is received from the HCI for the read local link policy request.
  1078. **
  1079. ** Returns void
  1080. **
  1081. *******************************************************************************/
  1082. void btm_establish_continue (tACL_CONN *p_acl_cb)
  1083. {
  1084. tBTM_BL_EVENT_DATA evt_data;
  1085. BTM_TRACE_DEBUG ("btm_establish_continue\n");
  1086. #if (!defined(BTM_BYPASS_EXTRA_ACL_SETUP) || BTM_BYPASS_EXTRA_ACL_SETUP == FALSE)
  1087. #if (defined BLE_INCLUDED && BLE_INCLUDED == TRUE)
  1088. if (p_acl_cb->transport == BT_TRANSPORT_BR_EDR)
  1089. #endif
  1090. {
  1091. /* For now there are a some devices that do not like sending */
  1092. /* commands events and data at the same time. */
  1093. /* Set the packet types to the default allowed by the device */
  1094. btm_set_packet_types (p_acl_cb, btm_cb.btm_acl_pkt_types_supported);
  1095. if (btm_cb.btm_def_link_policy) {
  1096. BTM_SetLinkPolicy (p_acl_cb->remote_addr, &btm_cb.btm_def_link_policy);
  1097. }
  1098. }
  1099. #endif
  1100. p_acl_cb->link_up_issued = TRUE;
  1101. /* If anyone cares, tell him database changed */
  1102. if (btm_cb.p_bl_changed_cb) {
  1103. evt_data.event = BTM_BL_CONN_EVT;
  1104. evt_data.conn.p_bda = p_acl_cb->remote_addr;
  1105. evt_data.conn.p_bdn = p_acl_cb->remote_name;
  1106. evt_data.conn.p_dc = p_acl_cb->remote_dc;
  1107. evt_data.conn.p_features = p_acl_cb->peer_lmp_features[HCI_EXT_FEATURES_PAGE_0];
  1108. evt_data.conn.sc_downgrade = p_acl_cb->sc_downgrade;
  1109. #if BLE_INCLUDED == TRUE
  1110. evt_data.conn.handle = p_acl_cb->hci_handle;
  1111. evt_data.conn.transport = p_acl_cb->transport;
  1112. #endif
  1113. (*btm_cb.p_bl_changed_cb)(&evt_data);
  1114. }
  1115. btm_acl_update_busy_level (BTM_BLI_ACL_UP_EVT);
  1116. }
  1117. /*******************************************************************************
  1118. **
  1119. ** Function BTM_SetDefaultLinkSuperTout
  1120. **
  1121. ** Description Set the default value for HCI "Write Link Supervision Timeout"
  1122. ** command to use when an ACL link is created.
  1123. **
  1124. ** Returns void
  1125. **
  1126. *******************************************************************************/
  1127. void BTM_SetDefaultLinkSuperTout (UINT16 timeout)
  1128. {
  1129. BTM_TRACE_DEBUG ("BTM_SetDefaultLinkSuperTout\n");
  1130. btm_cb.btm_def_link_super_tout = timeout;
  1131. }
  1132. /*******************************************************************************
  1133. **
  1134. ** Function BTM_GetLinkSuperTout
  1135. **
  1136. ** Description Read the link supervision timeout value of the connection
  1137. **
  1138. ** Returns status of the operation
  1139. **
  1140. *******************************************************************************/
  1141. tBTM_STATUS BTM_GetLinkSuperTout (BD_ADDR remote_bda, UINT16 *p_timeout)
  1142. {
  1143. tACL_CONN *p = btm_bda_to_acl(remote_bda, BT_TRANSPORT_BR_EDR);
  1144. BTM_TRACE_DEBUG ("BTM_GetLinkSuperTout\n");
  1145. if (p != (tACL_CONN *)NULL) {
  1146. *p_timeout = p->link_super_tout;
  1147. return (BTM_SUCCESS);
  1148. }
  1149. /* If here, no BD Addr found */
  1150. return (BTM_UNKNOWN_ADDR);
  1151. }
  1152. /*******************************************************************************
  1153. **
  1154. ** Function BTM_SetLinkSuperTout
  1155. **
  1156. ** Description Create and send HCI "Write Link Supervision Timeout" command
  1157. **
  1158. ** Returns status of the operation
  1159. **
  1160. *******************************************************************************/
  1161. tBTM_STATUS BTM_SetLinkSuperTout (BD_ADDR remote_bda, UINT16 timeout)
  1162. {
  1163. tACL_CONN *p = btm_bda_to_acl(remote_bda, BT_TRANSPORT_BR_EDR);
  1164. BTM_TRACE_DEBUG ("BTM_SetLinkSuperTout\n");
  1165. if (p != (tACL_CONN *)NULL) {
  1166. p->link_super_tout = timeout;
  1167. /* Only send if current role is Master; 2.0 spec requires this */
  1168. if (p->link_role == BTM_ROLE_MASTER) {
  1169. if (!btsnd_hcic_write_link_super_tout (LOCAL_BR_EDR_CONTROLLER_ID,
  1170. p->hci_handle, timeout)) {
  1171. return (BTM_NO_RESOURCES);
  1172. }
  1173. return (BTM_CMD_STARTED);
  1174. } else {
  1175. return (BTM_SUCCESS);
  1176. }
  1177. }
  1178. /* If here, no BD Addr found */
  1179. return (BTM_UNKNOWN_ADDR);
  1180. }
  1181. /*******************************************************************************
  1182. **
  1183. ** Function BTM_IsAclConnectionUp
  1184. **
  1185. ** Description This function is called to check if an ACL connection exists
  1186. ** to a specific remote BD Address.
  1187. **
  1188. ** Returns TRUE if connection is up, else FALSE.
  1189. **
  1190. *******************************************************************************/
  1191. BOOLEAN BTM_IsAclConnectionUp (BD_ADDR remote_bda, tBT_TRANSPORT transport)
  1192. {
  1193. tACL_CONN *p;
  1194. BTM_TRACE_API ("BTM_IsAclConnectionUp: RemBdAddr: %02x%02x%02x%02x%02x%02x\n",
  1195. remote_bda[0], remote_bda[1], remote_bda[2],
  1196. remote_bda[3], remote_bda[4], remote_bda[5]);
  1197. p = btm_bda_to_acl(remote_bda, transport);
  1198. if (p != (tACL_CONN *)NULL) {
  1199. return (TRUE);
  1200. }
  1201. /* If here, no BD Addr found */
  1202. return (FALSE);
  1203. }
  1204. /*******************************************************************************
  1205. **
  1206. ** Function BTM_GetNumAclLinks
  1207. **
  1208. ** Description This function is called to count the number of
  1209. ** ACL links that are active.
  1210. **
  1211. ** Returns UINT16 Number of active ACL links
  1212. **
  1213. *******************************************************************************/
  1214. UINT16 BTM_GetNumAclLinks (void)
  1215. {
  1216. uint16_t num_acl = 0;
  1217. for (uint16_t i = 0; i < MAX_L2CAP_LINKS; ++i) {
  1218. if (btm_cb.acl_db[i].in_use) {
  1219. ++num_acl;
  1220. }
  1221. }
  1222. return num_acl;
  1223. }
  1224. /*******************************************************************************
  1225. **
  1226. ** Function btm_get_acl_disc_reason_code
  1227. **
  1228. ** Description This function is called to get the disconnection reason code
  1229. ** returned by the HCI at disconnection complete event.
  1230. **
  1231. ** Returns TRUE if connection is up, else FALSE.
  1232. **
  1233. *******************************************************************************/
  1234. UINT16 btm_get_acl_disc_reason_code (void)
  1235. {
  1236. UINT8 res = btm_cb.acl_disc_reason;
  1237. BTM_TRACE_DEBUG ("btm_get_acl_disc_reason_code\n");
  1238. return (res);
  1239. }
  1240. /*******************************************************************************
  1241. **
  1242. ** Function BTM_GetHCIConnHandle
  1243. **
  1244. ** Description This function is called to get the handle for an ACL connection
  1245. ** to a specific remote BD Address.
  1246. **
  1247. ** Returns the handle of the connection, or 0xFFFF if none.
  1248. **
  1249. *******************************************************************************/
  1250. UINT16 BTM_GetHCIConnHandle (BD_ADDR remote_bda, tBT_TRANSPORT transport)
  1251. {
  1252. tACL_CONN *p;
  1253. BTM_TRACE_DEBUG ("BTM_GetHCIConnHandle\n");
  1254. p = btm_bda_to_acl(remote_bda, transport);
  1255. if (p != (tACL_CONN *)NULL) {
  1256. return (p->hci_handle);
  1257. }
  1258. /* If here, no BD Addr found */
  1259. return (0xFFFF);
  1260. }
  1261. /*******************************************************************************
  1262. **
  1263. ** Function btm_process_clk_off_comp_evt
  1264. **
  1265. ** Description This function is called when clock offset command completes.
  1266. **
  1267. ** Input Parms hci_handle - connection handle associated with the change
  1268. ** clock offset
  1269. **
  1270. ** Returns void
  1271. **
  1272. *******************************************************************************/
  1273. void btm_process_clk_off_comp_evt (UINT16 hci_handle, UINT16 clock_offset)
  1274. {
  1275. UINT8 xx;
  1276. BTM_TRACE_DEBUG ("btm_process_clk_off_comp_evt\n");
  1277. /* Look up the connection by handle and set the current mode */
  1278. if ((xx = btm_handle_to_acl_index(hci_handle)) < MAX_L2CAP_LINKS) {
  1279. btm_cb.acl_db[xx].clock_offset = clock_offset;
  1280. }
  1281. }
  1282. /*******************************************************************************
  1283. **
  1284. ** Function btm_acl_role_changed
  1285. **
  1286. ** Description This function is called whan a link's master/slave role change
  1287. ** event or command status event (with error) is received.
  1288. ** It updates the link control block, and calls
  1289. ** the registered callback with status and role (if registered).
  1290. **
  1291. ** Returns void
  1292. **
  1293. *******************************************************************************/
  1294. void btm_acl_role_changed (UINT8 hci_status, BD_ADDR bd_addr, UINT8 new_role)
  1295. {
  1296. UINT8 *p_bda = (bd_addr) ? bd_addr :
  1297. btm_cb.devcb.switch_role_ref_data.remote_bd_addr;
  1298. tACL_CONN *p = btm_bda_to_acl(p_bda, BT_TRANSPORT_BR_EDR);
  1299. tBTM_ROLE_SWITCH_CMPL *p_data = &btm_cb.devcb.switch_role_ref_data;
  1300. tBTM_SEC_DEV_REC *p_dev_rec;
  1301. tBTM_BL_ROLE_CHG_DATA evt;
  1302. BTM_TRACE_DEBUG ("btm_acl_role_changed\n");
  1303. /* Ignore any stray events */
  1304. if (p == NULL) {
  1305. /* it could be a failure */
  1306. if (hci_status != HCI_SUCCESS) {
  1307. btm_acl_report_role_change(hci_status, bd_addr);
  1308. }
  1309. return;
  1310. }
  1311. p_data->hci_status = hci_status;
  1312. if (hci_status == HCI_SUCCESS) {
  1313. p_data->role = new_role;
  1314. memcpy(p_data->remote_bd_addr, p_bda, BD_ADDR_LEN);
  1315. /* Update cached value */
  1316. p->link_role = new_role;
  1317. /* Reload LSTO: link supervision timeout is reset in the LM after a role switch */
  1318. if (new_role == BTM_ROLE_MASTER) {
  1319. BTM_SetLinkSuperTout (p->remote_addr, p->link_super_tout);
  1320. }
  1321. } else {
  1322. /* so the BTM_BL_ROLE_CHG_EVT uses the old role */
  1323. new_role = p->link_role;
  1324. }
  1325. /* Check if any SCO req is pending for role change */
  1326. btm_sco_chk_pend_rolechange (p->hci_handle);
  1327. /* if switching state is switching we need to turn encryption on */
  1328. /* if idle, we did not change encryption */
  1329. if (p->switch_role_state == BTM_ACL_SWKEY_STATE_SWITCHING) {
  1330. if (btsnd_hcic_set_conn_encrypt (p->hci_handle, TRUE)) {
  1331. p->encrypt_state = BTM_ACL_ENCRYPT_STATE_ENCRYPT_ON;
  1332. p->switch_role_state = BTM_ACL_SWKEY_STATE_ENCRYPTION_ON;
  1333. return;
  1334. }
  1335. }
  1336. /* Set the switch_role_state to IDLE since the reply received from HCI */
  1337. /* regardless of its result either success or failed. */
  1338. if (p->switch_role_state == BTM_ACL_SWKEY_STATE_IN_PROGRESS) {
  1339. p->switch_role_state = BTM_ACL_SWKEY_STATE_IDLE;
  1340. p->encrypt_state = BTM_ACL_ENCRYPT_STATE_IDLE;
  1341. }
  1342. /* if role switch complete is needed, report it now */
  1343. btm_acl_report_role_change(hci_status, bd_addr);
  1344. /* if role change event is registered, report it now */
  1345. if (btm_cb.p_bl_changed_cb && (btm_cb.bl_evt_mask & BTM_BL_ROLE_CHG_MASK)) {
  1346. evt.event = BTM_BL_ROLE_CHG_EVT;
  1347. evt.new_role = new_role;
  1348. evt.p_bda = p_bda;
  1349. evt.hci_status = hci_status;
  1350. (*btm_cb.p_bl_changed_cb)((tBTM_BL_EVENT_DATA *)&evt);
  1351. }
  1352. BTM_TRACE_DEBUG("Role Switch Event: new_role 0x%02x, HCI Status 0x%02x, rs_st:%d\n",
  1353. p_data->role, p_data->hci_status, p->switch_role_state);
  1354. #if BTM_DISC_DURING_RS == TRUE
  1355. /* If a disconnect is pending, issue it now that role switch has completed */
  1356. if ((p_dev_rec = btm_find_dev (p_bda)) != NULL) {
  1357. if (p_dev_rec->rs_disc_pending == BTM_SEC_DISC_PENDING) {
  1358. BTM_TRACE_WARNING("btm_acl_role_changed -> Issuing delayed HCI_Disconnect!!!\n");
  1359. btsnd_hcic_disconnect(p_dev_rec->hci_handle, HCI_ERR_PEER_USER);
  1360. }
  1361. BTM_TRACE_ERROR("tBTM_SEC_DEV:0x%x rs_disc_pending=%d\n",
  1362. (UINT32)p_dev_rec, p_dev_rec->rs_disc_pending);
  1363. p_dev_rec->rs_disc_pending = BTM_SEC_RS_NOT_PENDING; /* reset flag */
  1364. }
  1365. #endif
  1366. }
  1367. /*******************************************************************************
  1368. **
  1369. ** Function BTM_AllocateSCN
  1370. **
  1371. ** Description Look through the Server Channel Numbers for a free one.
  1372. **
  1373. ** Returns Allocated SCN number or 0 if none.
  1374. **
  1375. *******************************************************************************/
  1376. #if (CLASSIC_BT_INCLUDED == TRUE)
  1377. UINT8 BTM_AllocateSCN(void)
  1378. {
  1379. UINT8 x;
  1380. BTM_TRACE_DEBUG ("BTM_AllocateSCN\n");
  1381. for (x = 1; x < BTM_MAX_SCN; x++) {
  1382. if (!btm_cb.btm_scn[x - 1]) {
  1383. btm_cb.btm_scn[x - 1] = TRUE;
  1384. return x;
  1385. }
  1386. }
  1387. return (0); /* No free ports */
  1388. }
  1389. #endif ///CLASSIC_BT_INCLUDED == TRUE
  1390. /*******************************************************************************
  1391. **
  1392. ** Function BTM_TryAllocateSCN
  1393. **
  1394. ** Description Try to allocate a fixed server channel
  1395. **
  1396. ** Returns Returns TRUE if server channel was available
  1397. **
  1398. *******************************************************************************/
  1399. #if (CLASSIC_BT_INCLUDED == TRUE)
  1400. BOOLEAN BTM_TryAllocateSCN(UINT8 scn)
  1401. {
  1402. if (scn >= BTM_MAX_SCN) {
  1403. return FALSE;
  1404. }
  1405. /* check if this port is available */
  1406. if (!btm_cb.btm_scn[scn - 1]) {
  1407. btm_cb.btm_scn[scn - 1] = TRUE;
  1408. return TRUE;
  1409. }
  1410. return (FALSE); /* Port was busy */
  1411. }
  1412. /*******************************************************************************
  1413. **
  1414. ** Function BTM_FreeSCN
  1415. **
  1416. ** Description Free the specified SCN.
  1417. **
  1418. ** Returns TRUE or FALSE
  1419. **
  1420. *******************************************************************************/
  1421. BOOLEAN BTM_FreeSCN(UINT8 scn)
  1422. {
  1423. BTM_TRACE_DEBUG ("BTM_FreeSCN \n");
  1424. if (scn <= BTM_MAX_SCN) {
  1425. btm_cb.btm_scn[scn - 1] = FALSE;
  1426. return (TRUE);
  1427. } else {
  1428. return (FALSE); /* Illegal SCN passed in */
  1429. }
  1430. return (FALSE);
  1431. }
  1432. #endif ///CLASSIC_BT_INCLUDED == TRUE
  1433. /*******************************************************************************
  1434. **
  1435. ** Function btm_set_packet_types
  1436. **
  1437. ** Description This function sets the packet types used for a specific
  1438. ** ACL connection. It is called internally by btm_acl_created
  1439. ** or by an application/profile by BTM_SetPacketTypes.
  1440. **
  1441. ** Returns status of the operation
  1442. **
  1443. *******************************************************************************/
  1444. tBTM_STATUS btm_set_packet_types (tACL_CONN *p, UINT16 pkt_types)
  1445. {
  1446. UINT16 temp_pkt_types;
  1447. BTM_TRACE_DEBUG ("btm_set_packet_types\n");
  1448. /* Save in the ACL control blocks, types that we support */
  1449. temp_pkt_types = (pkt_types & BTM_ACL_SUPPORTED_PKTS_MASK &
  1450. btm_cb.btm_acl_pkt_types_supported);
  1451. /* OR in any exception packet types if at least 2.0 version of spec */
  1452. temp_pkt_types |= ((pkt_types & BTM_ACL_EXCEPTION_PKTS_MASK) |
  1453. (btm_cb.btm_acl_pkt_types_supported & BTM_ACL_EXCEPTION_PKTS_MASK));
  1454. /* Exclude packet types not supported by the peer */
  1455. btm_acl_chk_peer_pkt_type_support (p, &temp_pkt_types);
  1456. BTM_TRACE_DEBUG ("SetPacketType Mask -> 0x%04x\n", temp_pkt_types);
  1457. if (!btsnd_hcic_change_conn_type (p->hci_handle, temp_pkt_types)) {
  1458. return (BTM_NO_RESOURCES);
  1459. }
  1460. p->pkt_types_mask = temp_pkt_types;
  1461. return (BTM_CMD_STARTED);
  1462. }
  1463. /*******************************************************************************
  1464. **
  1465. ** Function btm_get_max_packet_size
  1466. **
  1467. ** Returns Returns maximum packet size that can be used for current
  1468. ** connection, 0 if connection is not established
  1469. **
  1470. *******************************************************************************/
  1471. UINT16 btm_get_max_packet_size (BD_ADDR addr)
  1472. {
  1473. tACL_CONN *p = btm_bda_to_acl(addr, BT_TRANSPORT_BR_EDR);
  1474. UINT16 pkt_types = 0;
  1475. UINT16 pkt_size = 0;
  1476. BTM_TRACE_DEBUG ("btm_get_max_packet_size\n");
  1477. if (p != NULL) {
  1478. pkt_types = p->pkt_types_mask;
  1479. } else {
  1480. /* Special case for when info for the local device is requested */
  1481. if (memcmp (controller_get_interface()->get_address(), addr, BD_ADDR_LEN) == 0) {
  1482. pkt_types = btm_cb.btm_acl_pkt_types_supported;
  1483. }
  1484. }
  1485. if (pkt_types) {
  1486. if (!(pkt_types & BTM_ACL_PKT_TYPES_MASK_NO_3_DH5)) {
  1487. pkt_size = HCI_EDR3_DH5_PACKET_SIZE;
  1488. } else if (!(pkt_types & BTM_ACL_PKT_TYPES_MASK_NO_2_DH5)) {
  1489. pkt_size = HCI_EDR2_DH5_PACKET_SIZE;
  1490. } else if (!(pkt_types & BTM_ACL_PKT_TYPES_MASK_NO_3_DH3)) {
  1491. pkt_size = HCI_EDR3_DH3_PACKET_SIZE;
  1492. } else if (pkt_types & BTM_ACL_PKT_TYPES_MASK_DH5) {
  1493. pkt_size = HCI_DH5_PACKET_SIZE;
  1494. } else if (!(pkt_types & BTM_ACL_PKT_TYPES_MASK_NO_2_DH3)) {
  1495. pkt_size = HCI_EDR2_DH3_PACKET_SIZE;
  1496. } else if (pkt_types & BTM_ACL_PKT_TYPES_MASK_DM5) {
  1497. pkt_size = HCI_DM5_PACKET_SIZE;
  1498. } else if (pkt_types & BTM_ACL_PKT_TYPES_MASK_DH3) {
  1499. pkt_size = HCI_DH3_PACKET_SIZE;
  1500. } else if (pkt_types & BTM_ACL_PKT_TYPES_MASK_DM3) {
  1501. pkt_size = HCI_DM3_PACKET_SIZE;
  1502. } else if (!(pkt_types & BTM_ACL_PKT_TYPES_MASK_NO_3_DH1)) {
  1503. pkt_size = HCI_EDR3_DH1_PACKET_SIZE;
  1504. } else if (!(pkt_types & BTM_ACL_PKT_TYPES_MASK_NO_2_DH1)) {
  1505. pkt_size = HCI_EDR2_DH1_PACKET_SIZE;
  1506. } else if (pkt_types & BTM_ACL_PKT_TYPES_MASK_DH1) {
  1507. pkt_size = HCI_DH1_PACKET_SIZE;
  1508. } else if (pkt_types & BTM_ACL_PKT_TYPES_MASK_DM1) {
  1509. pkt_size = HCI_DM1_PACKET_SIZE;
  1510. }
  1511. }
  1512. return (pkt_size);
  1513. }
  1514. /*******************************************************************************
  1515. **
  1516. ** Function BTM_ReadRemoteVersion
  1517. **
  1518. ** Returns If connected report peer device info
  1519. **
  1520. *******************************************************************************/
  1521. tBTM_STATUS BTM_ReadRemoteVersion (BD_ADDR addr, UINT8 *lmp_version,
  1522. UINT16 *manufacturer, UINT16 *lmp_sub_version)
  1523. {
  1524. tACL_CONN *p = btm_bda_to_acl(addr, BT_TRANSPORT_BR_EDR);
  1525. BTM_TRACE_DEBUG ("BTM_ReadRemoteVersion\n");
  1526. if (p == NULL) {
  1527. return (BTM_UNKNOWN_ADDR);
  1528. }
  1529. if (lmp_version) {
  1530. *lmp_version = p->lmp_version;
  1531. }
  1532. if (manufacturer) {
  1533. *manufacturer = p->manufacturer;
  1534. }
  1535. if (lmp_sub_version) {
  1536. *lmp_sub_version = p->lmp_subversion;
  1537. }
  1538. return (BTM_SUCCESS);
  1539. }
  1540. /*******************************************************************************
  1541. **
  1542. ** Function BTM_ReadRemoteFeatures
  1543. **
  1544. ** Returns pointer to the remote supported features mask (8 bytes)
  1545. **
  1546. *******************************************************************************/
  1547. UINT8 *BTM_ReadRemoteFeatures (BD_ADDR addr)
  1548. {
  1549. tACL_CONN *p = btm_bda_to_acl(addr, BT_TRANSPORT_BR_EDR);
  1550. BTM_TRACE_DEBUG ("BTM_ReadRemoteFeatures\n");
  1551. if (p == NULL) {
  1552. return (NULL);
  1553. }
  1554. return (p->peer_lmp_features[HCI_EXT_FEATURES_PAGE_0]);
  1555. }
  1556. /*******************************************************************************
  1557. **
  1558. ** Function BTM_ReadRemoteExtendedFeatures
  1559. **
  1560. ** Returns pointer to the remote extended features mask (8 bytes)
  1561. ** or NULL if bad page
  1562. **
  1563. *******************************************************************************/
  1564. UINT8 *BTM_ReadRemoteExtendedFeatures (BD_ADDR addr, UINT8 page_number)
  1565. {
  1566. tACL_CONN *p = btm_bda_to_acl(addr, BT_TRANSPORT_BR_EDR);
  1567. BTM_TRACE_DEBUG ("BTM_ReadRemoteExtendedFeatures\n");
  1568. if (p == NULL) {
  1569. return (NULL);
  1570. }
  1571. if (page_number > HCI_EXT_FEATURES_PAGE_MAX) {
  1572. BTM_TRACE_ERROR("Warning: BTM_ReadRemoteExtendedFeatures page %d unknown\n", page_number);
  1573. return NULL;
  1574. }
  1575. return (p->peer_lmp_features[page_number]);
  1576. }
  1577. /*******************************************************************************
  1578. **
  1579. ** Function BTM_ReadNumberRemoteFeaturesPages
  1580. **
  1581. ** Returns number of features pages read from the remote device.
  1582. **
  1583. *******************************************************************************/
  1584. UINT8 BTM_ReadNumberRemoteFeaturesPages (BD_ADDR addr)
  1585. {
  1586. tACL_CONN *p = btm_bda_to_acl(addr, BT_TRANSPORT_BR_EDR);
  1587. BTM_TRACE_DEBUG ("BTM_ReadNumberRemoteFeaturesPages\n");
  1588. if (p == NULL) {
  1589. return (0);
  1590. }
  1591. return (p->num_read_pages);
  1592. }
  1593. /*******************************************************************************
  1594. **
  1595. ** Function BTM_ReadAllRemoteFeatures
  1596. **
  1597. ** Returns pointer to all features of the remote (24 bytes).
  1598. **
  1599. *******************************************************************************/
  1600. UINT8 *BTM_ReadAllRemoteFeatures (BD_ADDR addr)
  1601. {
  1602. tACL_CONN *p = btm_bda_to_acl(addr, BT_TRANSPORT_BR_EDR);
  1603. BTM_TRACE_DEBUG ("BTM_ReadAllRemoteFeatures\n");
  1604. if (p == NULL) {
  1605. return (NULL);
  1606. }
  1607. return (p->peer_lmp_features[HCI_EXT_FEATURES_PAGE_0]);
  1608. }
  1609. /*******************************************************************************
  1610. **
  1611. ** Function BTM_RegBusyLevelNotif
  1612. **
  1613. ** Description This function is called to register a callback to receive
  1614. ** busy level change events.
  1615. **
  1616. ** Returns BTM_SUCCESS if successfully registered, otherwise error
  1617. **
  1618. *******************************************************************************/
  1619. tBTM_STATUS BTM_RegBusyLevelNotif (tBTM_BL_CHANGE_CB *p_cb, UINT8 *p_level,
  1620. tBTM_BL_EVENT_MASK evt_mask)
  1621. {
  1622. BTM_TRACE_DEBUG ("BTM_RegBusyLevelNotif\n");
  1623. if (p_level) {
  1624. *p_level = btm_cb.busy_level;
  1625. }
  1626. btm_cb.bl_evt_mask = evt_mask;
  1627. if (!p_cb) {
  1628. btm_cb.p_bl_changed_cb = NULL;
  1629. } else if (btm_cb.p_bl_changed_cb) {
  1630. return (BTM_BUSY);
  1631. } else {
  1632. btm_cb.p_bl_changed_cb = p_cb;
  1633. }
  1634. return (BTM_SUCCESS);
  1635. }
  1636. /*******************************************************************************
  1637. **
  1638. ** Function BTM_SetQoS
  1639. **
  1640. ** Description This function is called to setup QoS
  1641. **
  1642. ** Returns status of the operation
  1643. **
  1644. *******************************************************************************/
  1645. tBTM_STATUS BTM_SetQoS (BD_ADDR bd, FLOW_SPEC *p_flow, tBTM_CMPL_CB *p_cb)
  1646. {
  1647. tACL_CONN *p = &btm_cb.acl_db[0];
  1648. BTM_TRACE_API ("BTM_SetQoS: BdAddr: %02x%02x%02x%02x%02x%02x\n",
  1649. bd[0], bd[1], bd[2],
  1650. bd[3], bd[4], bd[5]);
  1651. /* If someone already waiting on the version, do not allow another */
  1652. if (btm_cb.devcb.p_qossu_cmpl_cb) {
  1653. return (BTM_BUSY);
  1654. }
  1655. if ( (p = btm_bda_to_acl(bd, BT_TRANSPORT_BR_EDR)) != NULL) {
  1656. btu_start_timer (&btm_cb.devcb.qossu_timer, BTU_TTYPE_BTM_QOS, BTM_DEV_REPLY_TIMEOUT);
  1657. btm_cb.devcb.p_qossu_cmpl_cb = p_cb;
  1658. if (!btsnd_hcic_qos_setup (p->hci_handle, p_flow->qos_flags, p_flow->service_type,
  1659. p_flow->token_rate, p_flow->peak_bandwidth,
  1660. p_flow->latency, p_flow->delay_variation)) {
  1661. btm_cb.devcb.p_qossu_cmpl_cb = NULL;
  1662. btu_stop_timer(&btm_cb.devcb.qossu_timer);
  1663. return (BTM_NO_RESOURCES);
  1664. } else {
  1665. return (BTM_CMD_STARTED);
  1666. }
  1667. }
  1668. /* If here, no BD Addr found */
  1669. return (BTM_UNKNOWN_ADDR);
  1670. }
  1671. /*******************************************************************************
  1672. **
  1673. ** Function btm_qos_setup_complete
  1674. **
  1675. ** Description This function is called when the command complete message
  1676. ** is received from the HCI for the qos setup request.
  1677. **
  1678. ** Returns void
  1679. **
  1680. *******************************************************************************/
  1681. void btm_qos_setup_complete (UINT8 status, UINT16 handle, FLOW_SPEC *p_flow)
  1682. {
  1683. tBTM_CMPL_CB *p_cb = btm_cb.devcb.p_qossu_cmpl_cb;
  1684. tBTM_QOS_SETUP_CMPL qossu;
  1685. BTM_TRACE_DEBUG ("btm_qos_setup_complete\n");
  1686. btu_stop_timer (&btm_cb.devcb.qossu_timer);
  1687. btm_cb.devcb.p_qossu_cmpl_cb = NULL;
  1688. if (p_cb) {
  1689. memset(&qossu, 0, sizeof(tBTM_QOS_SETUP_CMPL));
  1690. qossu.status = status;
  1691. qossu.handle = handle;
  1692. tACL_CONN *p = btm_handle_to_acl(handle);
  1693. if (p != NULL) {
  1694. memcpy (qossu.rem_bda, p->remote_addr, BD_ADDR_LEN);
  1695. }
  1696. if (p_flow != NULL) {
  1697. qossu.flow.qos_flags = p_flow->qos_flags;
  1698. qossu.flow.service_type = p_flow->service_type;
  1699. qossu.flow.token_rate = p_flow->token_rate;
  1700. qossu.flow.peak_bandwidth = p_flow->peak_bandwidth;
  1701. qossu.flow.latency = p_flow->latency;
  1702. qossu.flow.delay_variation = p_flow->delay_variation;
  1703. }
  1704. BTM_TRACE_DEBUG ("BTM: p_flow->delay_variation: 0x%02x\n",
  1705. qossu.flow.delay_variation);
  1706. (*p_cb)(&qossu);
  1707. }
  1708. }
  1709. /*******************************************************************************
  1710. **
  1711. ** Function btm_qos_setup_timeout
  1712. **
  1713. ** Description This function processes a timeout.
  1714. ** Currently, we just report an error log
  1715. **
  1716. ** Returns void
  1717. **
  1718. *******************************************************************************/
  1719. void btm_qos_setup_timeout (void *p_tle)
  1720. {
  1721. BTM_TRACE_DEBUG ("%s\n", __func__);
  1722. btm_qos_setup_complete (HCI_ERR_HOST_TIMEOUT, 0, NULL);
  1723. }
  1724. /*******************************************************************************
  1725. **
  1726. ** Function BTM_ReadRSSI
  1727. **
  1728. ** Description This function is called to read the link policy settings.
  1729. ** The address of link policy results are returned in the callback.
  1730. ** (tBTM_RSSI_RESULTS)
  1731. **
  1732. ** Returns BTM_CMD_STARTED if successfully initiated or error code
  1733. **
  1734. *******************************************************************************/
  1735. tBTM_STATUS BTM_ReadRSSI (BD_ADDR remote_bda, tBT_TRANSPORT transport, tBTM_CMPL_CB *p_cb)
  1736. {
  1737. tACL_CONN *p;
  1738. BTM_TRACE_API ("BTM_ReadRSSI: RemBdAddr: %02x%02x%02x%02x%02x%02x\n",
  1739. remote_bda[0], remote_bda[1], remote_bda[2],
  1740. remote_bda[3], remote_bda[4], remote_bda[5]);
  1741. tBTM_RSSI_RESULTS result;
  1742. /* If someone already waiting on the version, do not allow another */
  1743. if (btm_cb.devcb.p_rssi_cmpl_cb) {
  1744. result.status = BTM_BUSY;
  1745. (*p_cb)(&result);
  1746. return (BTM_BUSY);
  1747. }
  1748. p = btm_bda_to_acl(remote_bda, transport);
  1749. if (p != (tACL_CONN *)NULL) {
  1750. btu_start_timer (&btm_cb.devcb.rssi_timer, BTU_TTYPE_BTM_ACL,
  1751. BTM_DEV_REPLY_TIMEOUT);
  1752. btm_cb.devcb.p_rssi_cmpl_cb = p_cb;
  1753. if (!btsnd_hcic_read_rssi (p->hci_handle)) {
  1754. btm_cb.devcb.p_rssi_cmpl_cb = NULL;
  1755. btu_stop_timer (&btm_cb.devcb.rssi_timer);
  1756. result.status = BTM_NO_RESOURCES;
  1757. (*p_cb)(&result);
  1758. return (BTM_NO_RESOURCES);
  1759. } else {
  1760. return (BTM_CMD_STARTED);
  1761. }
  1762. }
  1763. /* If here, no BD Addr found */
  1764. return (BTM_UNKNOWN_ADDR);
  1765. }
  1766. /*******************************************************************************
  1767. **
  1768. ** Function BTM_ReadLinkQuality
  1769. **
  1770. ** Description This function is called to read the link qulaity.
  1771. ** The value of the link quality is returned in the callback.
  1772. ** (tBTM_LINK_QUALITY_RESULTS)
  1773. **
  1774. ** Returns BTM_CMD_STARTED if successfully initiated or error code
  1775. **
  1776. *******************************************************************************/
  1777. tBTM_STATUS BTM_ReadLinkQuality (BD_ADDR remote_bda, tBTM_CMPL_CB *p_cb)
  1778. {
  1779. tACL_CONN *p;
  1780. BTM_TRACE_API ("BTM_ReadLinkQuality: RemBdAddr: %02x%02x%02x%02x%02x%02x\n",
  1781. remote_bda[0], remote_bda[1], remote_bda[2],
  1782. remote_bda[3], remote_bda[4], remote_bda[5]);
  1783. /* If someone already waiting on the version, do not allow another */
  1784. if (btm_cb.devcb.p_lnk_qual_cmpl_cb) {
  1785. return (BTM_BUSY);
  1786. }
  1787. p = btm_bda_to_acl(remote_bda, BT_TRANSPORT_BR_EDR);
  1788. if (p != (tACL_CONN *)NULL) {
  1789. btu_start_timer (&btm_cb.devcb.lnk_quality_timer, BTU_TTYPE_BTM_ACL,
  1790. BTM_DEV_REPLY_TIMEOUT);
  1791. btm_cb.devcb.p_lnk_qual_cmpl_cb = p_cb;
  1792. if (!btsnd_hcic_get_link_quality (p->hci_handle)) {
  1793. btu_stop_timer (&btm_cb.devcb.lnk_quality_timer);
  1794. btm_cb.devcb.p_lnk_qual_cmpl_cb = NULL;
  1795. return (BTM_NO_RESOURCES);
  1796. } else {
  1797. return (BTM_CMD_STARTED);
  1798. }
  1799. }
  1800. /* If here, no BD Addr found */
  1801. return (BTM_UNKNOWN_ADDR);
  1802. }
  1803. /*******************************************************************************
  1804. **
  1805. ** Function BTM_ReadTxPower
  1806. **
  1807. ** Description This function is called to read the current
  1808. ** TX power of the connection. The tx power level results
  1809. ** are returned in the callback.
  1810. ** (tBTM_RSSI_RESULTS)
  1811. **
  1812. ** Returns BTM_CMD_STARTED if successfully initiated or error code
  1813. **
  1814. *******************************************************************************/
  1815. tBTM_STATUS BTM_ReadTxPower (BD_ADDR remote_bda, tBT_TRANSPORT transport, tBTM_CMPL_CB *p_cb)
  1816. {
  1817. tACL_CONN *p;
  1818. BOOLEAN ret;
  1819. #define BTM_READ_RSSI_TYPE_CUR 0x00
  1820. #define BTM_READ_RSSI_TYPE_MAX 0X01
  1821. BTM_TRACE_API ("BTM_ReadTxPower: RemBdAddr: %02x%02x%02x%02x%02x%02x\n",
  1822. remote_bda[0], remote_bda[1], remote_bda[2],
  1823. remote_bda[3], remote_bda[4], remote_bda[5]);
  1824. /* If someone already waiting on the version, do not allow another */
  1825. if (btm_cb.devcb.p_tx_power_cmpl_cb) {
  1826. return (BTM_BUSY);
  1827. }
  1828. p = btm_bda_to_acl(remote_bda, transport);
  1829. if (p != (tACL_CONN *)NULL) {
  1830. btu_start_timer (&btm_cb.devcb.tx_power_timer, BTU_TTYPE_BTM_ACL,
  1831. BTM_DEV_REPLY_TIMEOUT);
  1832. btm_cb.devcb.p_tx_power_cmpl_cb = p_cb;
  1833. #if BLE_INCLUDED == TRUE
  1834. if (p->transport == BT_TRANSPORT_LE) {
  1835. memcpy(btm_cb.devcb.read_tx_pwr_addr, remote_bda, BD_ADDR_LEN);
  1836. ret = btsnd_hcic_ble_read_adv_chnl_tx_power();
  1837. } else
  1838. #endif
  1839. {
  1840. ret = btsnd_hcic_read_tx_power (p->hci_handle, BTM_READ_RSSI_TYPE_CUR);
  1841. }
  1842. if (!ret) {
  1843. btm_cb.devcb.p_tx_power_cmpl_cb = NULL;
  1844. btu_stop_timer (&btm_cb.devcb.tx_power_timer);
  1845. return (BTM_NO_RESOURCES);
  1846. } else {
  1847. return (BTM_CMD_STARTED);
  1848. }
  1849. }
  1850. /* If here, no BD Addr found */
  1851. return (BTM_UNKNOWN_ADDR);
  1852. }
  1853. #if (BLE_INCLUDED == TRUE)
  1854. tBTM_STATUS BTM_BleReadAdvTxPower(tBTM_CMPL_CB *p_cb)
  1855. {
  1856. BOOLEAN ret;
  1857. tBTM_TX_POWER_RESULTS result;
  1858. /* If someone already waiting on the version, do not allow another */
  1859. if (btm_cb.devcb.p_tx_power_cmpl_cb) {
  1860. result.status = BTM_BUSY;
  1861. (*p_cb)(&result);
  1862. return (BTM_BUSY);
  1863. }
  1864. btm_cb.devcb.p_tx_power_cmpl_cb = p_cb;
  1865. btu_start_timer (&btm_cb.devcb.tx_power_timer, BTU_TTYPE_BTM_ACL,
  1866. BTM_DEV_REPLY_TIMEOUT);
  1867. ret = btsnd_hcic_ble_read_adv_chnl_tx_power();
  1868. if(!ret) {
  1869. btm_cb.devcb.p_tx_power_cmpl_cb = NULL;
  1870. btu_stop_timer (&btm_cb.devcb.tx_power_timer);
  1871. result.status = BTM_NO_RESOURCES;
  1872. (*p_cb)(&result);
  1873. return (BTM_NO_RESOURCES);
  1874. } else {
  1875. return BTM_CMD_STARTED;
  1876. }
  1877. }
  1878. void BTM_BleGetWhiteListSize(uint16_t *length)
  1879. {
  1880. tBTM_BLE_CB *p_cb = &btm_cb.ble_ctr_cb;
  1881. if (p_cb->white_list_avail_size == 0) {
  1882. BTM_TRACE_WARNING("%s Whitelist full.", __func__);
  1883. }
  1884. *length = p_cb->white_list_avail_size;
  1885. return;
  1886. }
  1887. #endif ///BLE_INCLUDED == TRUE
  1888. /*******************************************************************************
  1889. **
  1890. ** Function btm_read_tx_power_complete
  1891. **
  1892. ** Description This function is called when the command complete message
  1893. ** is received from the HCI for the read tx power request.
  1894. **
  1895. ** Returns void
  1896. **
  1897. *******************************************************************************/
  1898. void btm_read_tx_power_complete (UINT8 *p, BOOLEAN is_ble)
  1899. {
  1900. tBTM_CMPL_CB *p_cb = btm_cb.devcb.p_tx_power_cmpl_cb;
  1901. tBTM_TX_POWER_RESULTS results;
  1902. UINT16 handle;
  1903. tACL_CONN *p_acl_cb = &btm_cb.acl_db[0];
  1904. UINT16 index;
  1905. BTM_TRACE_DEBUG ("btm_read_tx_power_complete\n");
  1906. btu_stop_timer (&btm_cb.devcb.tx_power_timer);
  1907. /* If there was a callback registered for read rssi, call it */
  1908. btm_cb.devcb.p_tx_power_cmpl_cb = NULL;
  1909. if (p_cb) {
  1910. STREAM_TO_UINT8 (results.hci_status, p);
  1911. if (results.hci_status == HCI_SUCCESS) {
  1912. results.status = BTM_SUCCESS;
  1913. if (!is_ble) {
  1914. STREAM_TO_UINT16 (handle, p);
  1915. STREAM_TO_UINT8 (results.tx_power, p);
  1916. /* Search through the list of active channels for the correct BD Addr */
  1917. for (index = 0; index < MAX_L2CAP_LINKS; index++, p_acl_cb++) {
  1918. if ((p_acl_cb->in_use) && (handle == p_acl_cb->hci_handle)) {
  1919. memcpy (results.rem_bda, p_acl_cb->remote_addr, BD_ADDR_LEN);
  1920. break;
  1921. }
  1922. }
  1923. }
  1924. #if BLE_INCLUDED == TRUE
  1925. else {
  1926. STREAM_TO_UINT8 (results.tx_power, p);
  1927. memcpy(results.rem_bda, btm_cb.devcb.read_tx_pwr_addr, BD_ADDR_LEN);
  1928. }
  1929. #endif
  1930. BTM_TRACE_DEBUG ("BTM TX power Complete: tx_power %d, hci status 0x%02x\n",
  1931. results.tx_power, results.hci_status);
  1932. } else {
  1933. results.status = BTM_ERR_PROCESSING;
  1934. }
  1935. (*p_cb)(&results);
  1936. }
  1937. }
  1938. /*******************************************************************************
  1939. **
  1940. ** Function btm_read_rssi_complete
  1941. **
  1942. ** Description This function is called when the command complete message
  1943. ** is received from the HCI for the read rssi request.
  1944. **
  1945. ** Returns void
  1946. **
  1947. *******************************************************************************/
  1948. void btm_read_rssi_complete (UINT8 *p)
  1949. {
  1950. tBTM_CMPL_CB *p_cb = btm_cb.devcb.p_rssi_cmpl_cb;
  1951. tBTM_RSSI_RESULTS results;
  1952. UINT16 handle;
  1953. tACL_CONN *p_acl_cb = &btm_cb.acl_db[0];
  1954. UINT16 index;
  1955. BTM_TRACE_DEBUG ("btm_read_rssi_complete\n");
  1956. btu_stop_timer (&btm_cb.devcb.rssi_timer);
  1957. /* If there was a callback registered for read rssi, call it */
  1958. btm_cb.devcb.p_rssi_cmpl_cb = NULL;
  1959. if (p_cb) {
  1960. STREAM_TO_UINT8 (results.hci_status, p);
  1961. if (results.hci_status == HCI_SUCCESS) {
  1962. results.status = BTM_SUCCESS;
  1963. STREAM_TO_UINT16 (handle, p);
  1964. STREAM_TO_UINT8 (results.rssi, p);
  1965. BTM_TRACE_DEBUG ("BTM RSSI Complete: rssi %d, hci status 0x%02x\n",
  1966. results.rssi, results.hci_status);
  1967. /* Search through the list of active channels for the correct BD Addr */
  1968. for (index = 0; index < MAX_L2CAP_LINKS; index++, p_acl_cb++) {
  1969. if ((p_acl_cb->in_use) && (handle == p_acl_cb->hci_handle)) {
  1970. memcpy (results.rem_bda, p_acl_cb->remote_addr, BD_ADDR_LEN);
  1971. break;
  1972. }
  1973. }
  1974. } else {
  1975. results.status = BTM_ERR_PROCESSING;
  1976. }
  1977. (*p_cb)(&results);
  1978. }
  1979. }
  1980. /*******************************************************************************
  1981. **
  1982. ** Function btm_read_link_quality_complete
  1983. **
  1984. ** Description This function is called when the command complete message
  1985. ** is received from the HCI for the read link quality.
  1986. **
  1987. ** Returns void
  1988. **
  1989. *******************************************************************************/
  1990. void btm_read_link_quality_complete (UINT8 *p)
  1991. {
  1992. tBTM_CMPL_CB *p_cb = btm_cb.devcb.p_lnk_qual_cmpl_cb;
  1993. tBTM_LINK_QUALITY_RESULTS results;
  1994. UINT16 handle;
  1995. tACL_CONN *p_acl_cb = &btm_cb.acl_db[0];
  1996. UINT16 index;
  1997. BTM_TRACE_DEBUG ("btm_read_link_quality_complete\n");
  1998. btu_stop_timer (&btm_cb.devcb.lnk_quality_timer);
  1999. /* If there was a callback registered for read rssi, call it */
  2000. btm_cb.devcb.p_lnk_qual_cmpl_cb = NULL;
  2001. if (p_cb) {
  2002. STREAM_TO_UINT8 (results.hci_status, p);
  2003. if (results.hci_status == HCI_SUCCESS) {
  2004. results.status = BTM_SUCCESS;
  2005. STREAM_TO_UINT16 (handle, p);
  2006. STREAM_TO_UINT8 (results.link_quality, p);
  2007. BTM_TRACE_DEBUG ("BTM Link Quality Complete: Link Quality %d, hci status 0x%02x\n",
  2008. results.link_quality, results.hci_status);
  2009. /* Search through the list of active channels for the correct BD Addr */
  2010. for (index = 0; index < MAX_L2CAP_LINKS; index++, p_acl_cb++) {
  2011. if ((p_acl_cb->in_use) && (handle == p_acl_cb->hci_handle)) {
  2012. memcpy (results.rem_bda, p_acl_cb->remote_addr, BD_ADDR_LEN);
  2013. break;
  2014. }
  2015. }
  2016. } else {
  2017. results.status = BTM_ERR_PROCESSING;
  2018. }
  2019. (*p_cb)(&results);
  2020. }
  2021. }
  2022. /*******************************************************************************
  2023. **
  2024. ** Function btm_remove_acl
  2025. **
  2026. ** Description This function is called to disconnect an ACL connection
  2027. **
  2028. ** Returns BTM_SUCCESS if successfully initiated, otherwise BTM_NO_RESOURCES.
  2029. **
  2030. *******************************************************************************/
  2031. tBTM_STATUS btm_remove_acl (BD_ADDR bd_addr, tBT_TRANSPORT transport)
  2032. {
  2033. UINT16 hci_handle = BTM_GetHCIConnHandle(bd_addr, transport);
  2034. tBTM_STATUS status = BTM_SUCCESS;
  2035. BTM_TRACE_DEBUG ("btm_remove_acl\n");
  2036. #if BTM_DISC_DURING_RS == TRUE
  2037. tBTM_SEC_DEV_REC *p_dev_rec = btm_find_dev (bd_addr);
  2038. /* Role Switch is pending, postpone until completed */
  2039. if (p_dev_rec && (p_dev_rec->rs_disc_pending == BTM_SEC_RS_PENDING)) {
  2040. p_dev_rec->rs_disc_pending = BTM_SEC_DISC_PENDING;
  2041. } else /* otherwise can disconnect right away */
  2042. #endif
  2043. {
  2044. if (hci_handle != 0xFFFF && p_dev_rec &&
  2045. p_dev_rec->sec_state != BTM_SEC_STATE_DISCONNECTING) {
  2046. if (!btsnd_hcic_disconnect (hci_handle, HCI_ERR_PEER_USER)) {
  2047. status = BTM_NO_RESOURCES;
  2048. }
  2049. } else {
  2050. status = BTM_UNKNOWN_ADDR;
  2051. }
  2052. }
  2053. return status;
  2054. }
  2055. /*******************************************************************************
  2056. **
  2057. ** Function BTM_SetTraceLevel
  2058. **
  2059. ** Description This function sets the trace level for BTM. If called with
  2060. ** a value of 0xFF, it simply returns the current trace level.
  2061. **
  2062. ** Returns The new or current trace level
  2063. **
  2064. *******************************************************************************/
  2065. UINT8 BTM_SetTraceLevel (UINT8 new_level)
  2066. {
  2067. BTM_TRACE_DEBUG ("BTM_SetTraceLevel\n");
  2068. if (new_level != 0xFF) {
  2069. btm_cb.trace_level = new_level;
  2070. }
  2071. return (btm_cb.trace_level);
  2072. }
  2073. /*******************************************************************************
  2074. **
  2075. ** Function btm_cont_rswitch
  2076. **
  2077. ** Description This function is called to continue processing an active
  2078. ** role switch. It first disables encryption if enabled and
  2079. ** EPR is not supported
  2080. **
  2081. ** Returns void
  2082. **
  2083. *******************************************************************************/
  2084. void btm_cont_rswitch (tACL_CONN *p, tBTM_SEC_DEV_REC *p_dev_rec,
  2085. UINT8 hci_status)
  2086. {
  2087. BOOLEAN sw_ok = TRUE;
  2088. BTM_TRACE_DEBUG ("btm_cont_rswitch\n");
  2089. /* Check to see if encryption needs to be turned off if pending
  2090. change of link key or role switch */
  2091. if (p->switch_role_state == BTM_ACL_SWKEY_STATE_MODE_CHANGE) {
  2092. /* Must turn off Encryption first if necessary */
  2093. /* Some devices do not support switch or change of link key while encryption is on */
  2094. if (p_dev_rec != NULL && ((p_dev_rec->sec_flags & BTM_SEC_ENCRYPTED) != 0)
  2095. && !BTM_EPR_AVAILABLE(p)) {
  2096. if (btsnd_hcic_set_conn_encrypt (p->hci_handle, FALSE)) {
  2097. p->encrypt_state = BTM_ACL_ENCRYPT_STATE_ENCRYPT_OFF;
  2098. if (p->switch_role_state == BTM_ACL_SWKEY_STATE_MODE_CHANGE) {
  2099. p->switch_role_state = BTM_ACL_SWKEY_STATE_ENCRYPTION_OFF;
  2100. }
  2101. } else {
  2102. /* Error occurred; set states back to Idle */
  2103. if (p->switch_role_state == BTM_ACL_SWKEY_STATE_MODE_CHANGE) {
  2104. sw_ok = FALSE;
  2105. }
  2106. }
  2107. } else /* Encryption not used or EPR supported, continue with switch
  2108. and/or change of link key */
  2109. {
  2110. if (p->switch_role_state == BTM_ACL_SWKEY_STATE_MODE_CHANGE) {
  2111. p->switch_role_state = BTM_ACL_SWKEY_STATE_IN_PROGRESS;
  2112. #if BTM_DISC_DURING_RS == TRUE
  2113. if (p_dev_rec) {
  2114. p_dev_rec->rs_disc_pending = BTM_SEC_RS_PENDING;
  2115. }
  2116. #endif
  2117. sw_ok = btsnd_hcic_switch_role (p->remote_addr, (UINT8)!p->link_role);
  2118. }
  2119. }
  2120. if (!sw_ok) {
  2121. p->switch_role_state = BTM_ACL_SWKEY_STATE_IDLE;
  2122. btm_acl_report_role_change(hci_status, p->remote_addr);
  2123. }
  2124. }
  2125. }
  2126. /*******************************************************************************
  2127. **
  2128. ** Function btm_acl_resubmit_page
  2129. **
  2130. ** Description send pending page request
  2131. **
  2132. *******************************************************************************/
  2133. void btm_acl_resubmit_page (void)
  2134. {
  2135. #if (SMP_INCLUDED == TRUE)
  2136. tBTM_SEC_DEV_REC *p_dev_rec;
  2137. BT_HDR *p_buf;
  2138. UINT8 *pp;
  2139. BD_ADDR bda;
  2140. BTM_TRACE_DEBUG ("btm_acl_resubmit_page\n");
  2141. /* If there were other page request schedule can start the next one */
  2142. if ((p_buf = (BT_HDR *)fixed_queue_dequeue(btm_cb.page_queue, 0)) != NULL) {
  2143. /* skip 3 (2 bytes opcode and 1 byte len) to get to the bd_addr
  2144. * for both create_conn and rmt_name */
  2145. pp = (UINT8 *)(p_buf + 1) + p_buf->offset + 3;
  2146. STREAM_TO_BDADDR (bda, pp);
  2147. p_dev_rec = btm_find_or_alloc_dev (bda);
  2148. memcpy (btm_cb.connecting_bda, p_dev_rec->bd_addr, BD_ADDR_LEN);
  2149. memcpy (btm_cb.connecting_dc, p_dev_rec->dev_class, DEV_CLASS_LEN);
  2150. btu_hcif_send_cmd (LOCAL_BR_EDR_CONTROLLER_ID, p_buf);
  2151. } else {
  2152. btm_cb.paging = FALSE;
  2153. }
  2154. #endif ///SMP_INCLUDED == TRUE
  2155. }
  2156. /*******************************************************************************
  2157. **
  2158. ** Function btm_acl_reset_paging
  2159. **
  2160. ** Description set paging to FALSE and free the page queue - called at hci_reset
  2161. **
  2162. *******************************************************************************/
  2163. void btm_acl_reset_paging (void)
  2164. {
  2165. BT_HDR *p;
  2166. BTM_TRACE_DEBUG ("btm_acl_reset_paging\n");
  2167. /* If we sent reset we are definitely not paging any more */
  2168. while ((p = (BT_HDR *)fixed_queue_dequeue(btm_cb.page_queue, 0)) != NULL) {
  2169. osi_free (p);
  2170. }
  2171. btm_cb.paging = FALSE;
  2172. }
  2173. /*******************************************************************************
  2174. **
  2175. ** Function btm_acl_paging
  2176. **
  2177. ** Description send a paging command or queue it in btm_cb
  2178. **
  2179. *******************************************************************************/
  2180. #if (SMP_INCLUDED == TRUE && CLASSIC_BT_INCLUDED == TRUE)
  2181. void btm_acl_paging (BT_HDR *p, BD_ADDR bda)
  2182. {
  2183. tBTM_SEC_DEV_REC *p_dev_rec;
  2184. BTM_TRACE_DEBUG ("btm_acl_paging discing:%d, paging:%d BDA: %06x%06x\n",
  2185. btm_cb.discing, btm_cb.paging,
  2186. (bda[0] << 16) + (bda[1] << 8) + bda[2], (bda[3] << 16) + (bda[4] << 8) + bda[5]);
  2187. if (btm_cb.discing) {
  2188. btm_cb.paging = TRUE;
  2189. fixed_queue_enqueue(btm_cb.page_queue, p, FIXED_QUEUE_MAX_TIMEOUT);
  2190. } else {
  2191. if (!BTM_ACL_IS_CONNECTED (bda)) {
  2192. BTM_TRACE_DEBUG ("connecting_bda: %06x%06x\n",
  2193. (btm_cb.connecting_bda[0] << 16) + (btm_cb.connecting_bda[1] << 8) +
  2194. btm_cb.connecting_bda[2],
  2195. (btm_cb.connecting_bda[3] << 16) + (btm_cb.connecting_bda[4] << 8) +
  2196. btm_cb.connecting_bda[5]);
  2197. if (btm_cb.paging &&
  2198. memcmp (bda, btm_cb.connecting_bda, BD_ADDR_LEN) != 0) {
  2199. fixed_queue_enqueue(btm_cb.page_queue, p, FIXED_QUEUE_MAX_TIMEOUT);
  2200. } else {
  2201. p_dev_rec = btm_find_or_alloc_dev (bda);
  2202. memcpy (btm_cb.connecting_bda, p_dev_rec->bd_addr, BD_ADDR_LEN);
  2203. memcpy (btm_cb.connecting_dc, p_dev_rec->dev_class, DEV_CLASS_LEN);
  2204. btu_hcif_send_cmd (LOCAL_BR_EDR_CONTROLLER_ID, p);
  2205. }
  2206. btm_cb.paging = TRUE;
  2207. } else { /* ACL is already up */
  2208. btu_hcif_send_cmd (LOCAL_BR_EDR_CONTROLLER_ID, p);
  2209. }
  2210. }
  2211. }
  2212. #endif ///SMP_INCLUDED == TRUE
  2213. /*******************************************************************************
  2214. **
  2215. ** Function btm_acl_notif_conn_collision
  2216. **
  2217. ** Description Send connection collision event to upper layer if registered
  2218. **
  2219. ** Returns TRUE if sent out to upper layer,
  2220. ** FALSE if no one needs the notification.
  2221. **
  2222. *******************************************************************************/
  2223. BOOLEAN btm_acl_notif_conn_collision (BD_ADDR bda)
  2224. {
  2225. tBTM_BL_EVENT_DATA evt_data;
  2226. /* Report possible collision to the upper layer. */
  2227. if (btm_cb.p_bl_changed_cb) {
  2228. BTM_TRACE_DEBUG ("btm_acl_notif_conn_collision: RemBdAddr: %02x%02x%02x%02x%02x%02x\n",
  2229. bda[0], bda[1], bda[2], bda[3], bda[4], bda[5]);
  2230. evt_data.event = BTM_BL_COLLISION_EVT;
  2231. evt_data.conn.p_bda = bda;
  2232. #if BLE_INCLUDED == TRUE
  2233. evt_data.conn.transport = BT_TRANSPORT_BR_EDR;
  2234. evt_data.conn.handle = BTM_INVALID_HCI_HANDLE;
  2235. #endif
  2236. (*btm_cb.p_bl_changed_cb)(&evt_data);
  2237. return TRUE;
  2238. } else {
  2239. return FALSE;
  2240. }
  2241. }
  2242. /*******************************************************************************
  2243. **
  2244. ** Function btm_acl_chk_peer_pkt_type_support
  2245. **
  2246. ** Description Check if peer supports requested packets
  2247. **
  2248. *******************************************************************************/
  2249. void btm_acl_chk_peer_pkt_type_support (tACL_CONN *p, UINT16 *p_pkt_type)
  2250. {
  2251. /* 3 and 5 slot packets? */
  2252. if (!HCI_3_SLOT_PACKETS_SUPPORTED(p->peer_lmp_features[HCI_EXT_FEATURES_PAGE_0])) {
  2253. *p_pkt_type &= ~(BTM_ACL_PKT_TYPES_MASK_DH3 + BTM_ACL_PKT_TYPES_MASK_DM3);
  2254. }
  2255. if (!HCI_5_SLOT_PACKETS_SUPPORTED(p->peer_lmp_features[HCI_EXT_FEATURES_PAGE_0])) {
  2256. *p_pkt_type &= ~(BTM_ACL_PKT_TYPES_MASK_DH5 + BTM_ACL_PKT_TYPES_MASK_DM5);
  2257. }
  2258. /* 2 and 3 MPS support? */
  2259. if (!HCI_EDR_ACL_2MPS_SUPPORTED(p->peer_lmp_features[HCI_EXT_FEATURES_PAGE_0])) {
  2260. /* Not supported. Add 'not_supported' mask for all 2MPS packet types */
  2261. *p_pkt_type |= (BTM_ACL_PKT_TYPES_MASK_NO_2_DH1 + BTM_ACL_PKT_TYPES_MASK_NO_2_DH3 +
  2262. BTM_ACL_PKT_TYPES_MASK_NO_2_DH5);
  2263. }
  2264. if (!HCI_EDR_ACL_3MPS_SUPPORTED(p->peer_lmp_features[HCI_EXT_FEATURES_PAGE_0])) {
  2265. /* Not supported. Add 'not_supported' mask for all 3MPS packet types */
  2266. *p_pkt_type |= (BTM_ACL_PKT_TYPES_MASK_NO_3_DH1 + BTM_ACL_PKT_TYPES_MASK_NO_3_DH3 +
  2267. BTM_ACL_PKT_TYPES_MASK_NO_3_DH5);
  2268. }
  2269. /* EDR 3 and 5 slot support? */
  2270. if (HCI_EDR_ACL_2MPS_SUPPORTED(p->peer_lmp_features[HCI_EXT_FEATURES_PAGE_0])
  2271. || HCI_EDR_ACL_3MPS_SUPPORTED(p->peer_lmp_features[HCI_EXT_FEATURES_PAGE_0])) {
  2272. if (!HCI_3_SLOT_EDR_ACL_SUPPORTED(p->peer_lmp_features[HCI_EXT_FEATURES_PAGE_0]))
  2273. /* Not supported. Add 'not_supported' mask for all 3-slot EDR packet types */
  2274. {
  2275. *p_pkt_type |= (BTM_ACL_PKT_TYPES_MASK_NO_2_DH3 + BTM_ACL_PKT_TYPES_MASK_NO_3_DH3);
  2276. }
  2277. if (!HCI_5_SLOT_EDR_ACL_SUPPORTED(p->peer_lmp_features[HCI_EXT_FEATURES_PAGE_0]))
  2278. /* Not supported. Add 'not_supported' mask for all 5-slot EDR packet types */
  2279. {
  2280. *p_pkt_type |= (BTM_ACL_PKT_TYPES_MASK_NO_2_DH5 + BTM_ACL_PKT_TYPES_MASK_NO_3_DH5);
  2281. }
  2282. }
  2283. }