btm_acl.cc 94 KB

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