btm_acl.c 87 KB

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