btm_acl.c 97 KB

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