btm_ble_gap.cc 92 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598
  1. /******************************************************************************
  2. *
  3. * Copyright 2008-2014 Broadcom Corporation
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at:
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. *
  17. ******************************************************************************/
  18. /******************************************************************************
  19. *
  20. * This file contains functions for BLE GAP.
  21. *
  22. ******************************************************************************/
  23. #define LOG_TAG "bt_btm_ble"
  24. #include <base/bind.h>
  25. #include <base/callback.h>
  26. #include <base/strings/string_number_conversions.h>
  27. #include <stddef.h>
  28. #include <stdio.h>
  29. #include <string.h>
  30. #include <list>
  31. #include <vector>
  32. #include "bt_types.h"
  33. #include "bt_utils.h"
  34. #include "btm_ble_api.h"
  35. #include "btm_int.h"
  36. #include "btu.h"
  37. #include "device/include/controller.h"
  38. #include "gap_api.h"
  39. #include "hcimsgs.h"
  40. #include "osi/include/osi.h"
  41. #include "advertise_data_parser.h"
  42. #include "btm_ble_int.h"
  43. #include "gatt_int.h"
  44. #include "gattdefs.h"
  45. #include "l2c_int.h"
  46. #include "osi/include/log.h"
  47. #define BTM_BLE_NAME_SHORT 0x01
  48. #define BTM_BLE_NAME_CMPL 0x02
  49. #define BTM_BLE_FILTER_TARGET_UNKNOWN 0xff
  50. #define BTM_BLE_POLICY_UNKNOWN 0xff
  51. #define BTM_EXT_BLE_RMT_NAME_TIMEOUT_MS (30 * 1000)
  52. #define MIN_ADV_LENGTH 2
  53. #define BTM_VSC_CHIP_CAPABILITY_RSP_LEN 9
  54. #define BTM_VSC_CHIP_CAPABILITY_RSP_LEN_L_RELEASE \
  55. BTM_VSC_CHIP_CAPABILITY_RSP_LEN
  56. #define BTM_VSC_CHIP_CAPABILITY_RSP_LEN_M_RELEASE 15
  57. namespace {
  58. class AdvertisingCache {
  59. public:
  60. /* Set the data to |data| for device |addr_type, addr| */
  61. const std::vector<uint8_t>& Set(uint8_t addr_type, const RawAddress& addr,
  62. std::vector<uint8_t> data) {
  63. auto it = Find(addr_type, addr);
  64. if (it != items.end()) {
  65. it->data = std::move(data);
  66. return it->data;
  67. }
  68. if (items.size() > cache_max) {
  69. items.pop_back();
  70. }
  71. items.emplace_front(addr_type, addr, std::move(data));
  72. return items.front().data;
  73. }
  74. /* Append |data| for device |addr_type, addr| */
  75. const std::vector<uint8_t>& Append(uint8_t addr_type, const RawAddress& addr,
  76. std::vector<uint8_t> data) {
  77. auto it = Find(addr_type, addr);
  78. if (it != items.end()) {
  79. it->data.insert(it->data.end(), data.begin(), data.end());
  80. return it->data;
  81. }
  82. if (items.size() > cache_max) {
  83. items.pop_back();
  84. }
  85. items.emplace_front(addr_type, addr, std::move(data));
  86. return items.front().data;
  87. }
  88. /* Clear data for device |addr_type, addr| */
  89. void Clear(uint8_t addr_type, const RawAddress& addr) {
  90. auto it = Find(addr_type, addr);
  91. if (it != items.end()) {
  92. items.erase(it);
  93. }
  94. }
  95. private:
  96. struct Item {
  97. uint8_t addr_type;
  98. RawAddress addr;
  99. std::vector<uint8_t> data;
  100. Item(uint8_t addr_type, const RawAddress& addr, std::vector<uint8_t> data)
  101. : addr_type(addr_type), addr(addr), data(data) {}
  102. };
  103. std::list<Item>::iterator Find(uint8_t addr_type, const RawAddress& addr) {
  104. for (auto it = items.begin(); it != items.end(); it++) {
  105. if (it->addr_type == addr_type && it->addr == addr) {
  106. return it;
  107. }
  108. }
  109. return items.end();
  110. }
  111. /* we keep maximum 7 devices in the cache */
  112. const size_t cache_max = 7;
  113. std::list<Item> items;
  114. };
  115. /* Devices in this cache are waiting for eiter scan response, or chained packets
  116. * on secondary channel */
  117. AdvertisingCache cache;
  118. } // namespace
  119. #if (BLE_VND_INCLUDED == TRUE)
  120. static tBTM_BLE_CTRL_FEATURES_CBACK* p_ctrl_le_feature_rd_cmpl_cback = NULL;
  121. #endif
  122. /*******************************************************************************
  123. * Local functions
  124. ******************************************************************************/
  125. static void btm_ble_update_adv_flag(uint8_t flag);
  126. static void btm_ble_process_adv_pkt_cont(
  127. uint16_t evt_type, uint8_t addr_type, const RawAddress& bda,
  128. uint8_t primary_phy, uint8_t secondary_phy, uint8_t advertising_sid,
  129. int8_t tx_power, int8_t rssi, uint16_t periodic_adv_int, uint8_t data_len,
  130. uint8_t* data);
  131. static uint8_t btm_set_conn_mode_adv_init_addr(tBTM_BLE_INQ_CB* p_cb,
  132. RawAddress& p_peer_addr_ptr,
  133. tBLE_ADDR_TYPE* p_peer_addr_type,
  134. tBLE_ADDR_TYPE* p_own_addr_type);
  135. static void btm_ble_stop_observe(void);
  136. static void btm_ble_fast_adv_timer_timeout(void* data);
  137. static void btm_ble_start_slow_adv(void);
  138. static void btm_ble_inquiry_timer_gap_limited_discovery_timeout(void* data);
  139. static void btm_ble_inquiry_timer_timeout(void* data);
  140. static void btm_ble_observer_timer_timeout(void* data);
  141. #define BTM_BLE_INQ_RESULT 0x01
  142. #define BTM_BLE_OBS_RESULT 0x02
  143. bool ble_evt_type_is_connectable(uint16_t evt_type) {
  144. return evt_type & (1 << BLE_EVT_CONNECTABLE_BIT);
  145. }
  146. bool ble_evt_type_is_scannable(uint16_t evt_type) {
  147. return evt_type & (1 << BLE_EVT_SCANNABLE_BIT);
  148. }
  149. bool ble_evt_type_is_directed(uint16_t evt_type) {
  150. return evt_type & (1 << BLE_EVT_DIRECTED_BIT);
  151. }
  152. bool ble_evt_type_is_scan_resp(uint16_t evt_type) {
  153. return evt_type & (1 << BLE_EVT_SCAN_RESPONSE_BIT);
  154. }
  155. bool ble_evt_type_is_legacy(uint16_t evt_type) {
  156. return evt_type & (1 << BLE_EVT_LEGACY_BIT);
  157. }
  158. uint8_t ble_evt_type_data_status(uint16_t evt_type) {
  159. return (evt_type >> 5) & 3;
  160. }
  161. constexpr uint8_t UNSUPPORTED = 255;
  162. /* LE states combo bit to check */
  163. const uint8_t btm_le_state_combo_tbl[BTM_BLE_STATE_MAX][BTM_BLE_STATE_MAX] = {
  164. {
  165. /* single state support */
  166. HCI_LE_STATES_CONN_ADV_BIT, /* conn_adv */
  167. HCI_LE_STATES_INIT_BIT, /* init */
  168. HCI_LE_STATES_INIT_BIT, /* master */
  169. HCI_LE_STATES_SLAVE_BIT, /* slave */
  170. UNSUPPORTED, /* todo: lo du dir adv, not covered ? */
  171. HCI_LE_STATES_HI_DUTY_DIR_ADV_BIT, /* hi duty dir adv */
  172. HCI_LE_STATES_NON_CONN_ADV_BIT, /* non connectable adv */
  173. HCI_LE_STATES_PASS_SCAN_BIT, /* passive scan */
  174. HCI_LE_STATES_ACTIVE_SCAN_BIT, /* active scan */
  175. HCI_LE_STATES_SCAN_ADV_BIT /* scanable adv */
  176. },
  177. {
  178. /* conn_adv =0 */
  179. UNSUPPORTED, /* conn_adv */
  180. HCI_LE_STATES_CONN_ADV_INIT_BIT, /* init: 32 */
  181. HCI_LE_STATES_CONN_ADV_MASTER_BIT, /* master: 35 */
  182. HCI_LE_STATES_CONN_ADV_SLAVE_BIT, /* slave: 38,*/
  183. UNSUPPORTED, /* lo du dir adv */
  184. UNSUPPORTED, /* hi duty dir adv */
  185. UNSUPPORTED, /* non connectable adv */
  186. HCI_LE_STATES_CONN_ADV_PASS_SCAN_BIT, /* passive scan */
  187. HCI_LE_STATES_CONN_ADV_ACTIVE_SCAN_BIT, /* active scan */
  188. UNSUPPORTED /* scanable adv */
  189. },
  190. {
  191. /* init */
  192. HCI_LE_STATES_CONN_ADV_INIT_BIT, /* conn_adv: 32 */
  193. UNSUPPORTED, /* init */
  194. HCI_LE_STATES_INIT_MASTER_BIT, /* master 28 */
  195. HCI_LE_STATES_INIT_MASTER_SLAVE_BIT, /* slave 41 */
  196. HCI_LE_STATES_LO_DUTY_DIR_ADV_INIT_BIT, /* lo du dir adv 34 */
  197. HCI_LE_STATES_HI_DUTY_DIR_ADV_INIT_BIT, /* hi duty dir adv 33 */
  198. HCI_LE_STATES_NON_CONN_INIT_BIT, /* non connectable adv */
  199. HCI_LE_STATES_PASS_SCAN_INIT_BIT, /* passive scan */
  200. HCI_LE_STATES_ACTIVE_SCAN_INIT_BIT, /* active scan */
  201. HCI_LE_STATES_SCAN_ADV_INIT_BIT /* scanable adv */
  202. },
  203. {
  204. /* master */
  205. HCI_LE_STATES_CONN_ADV_MASTER_BIT, /* conn_adv: 35 */
  206. HCI_LE_STATES_INIT_MASTER_BIT, /* init 28 */
  207. HCI_LE_STATES_INIT_MASTER_BIT, /* master 28 */
  208. HCI_LE_STATES_CONN_ADV_INIT_BIT, /* slave: 32 */
  209. HCI_LE_STATES_LO_DUTY_DIR_ADV_MASTER_BIT, /* lo duty cycle adv 37 */
  210. HCI_LE_STATES_HI_DUTY_DIR_ADV_MASTER_BIT, /* hi duty cycle adv 36 */
  211. HCI_LE_STATES_NON_CONN_ADV_MASTER_BIT, /* non connectable adv*/
  212. HCI_LE_STATES_PASS_SCAN_MASTER_BIT, /* passive scan */
  213. HCI_LE_STATES_ACTIVE_SCAN_MASTER_BIT, /* active scan */
  214. HCI_LE_STATES_SCAN_ADV_MASTER_BIT /* scanable adv */
  215. },
  216. {
  217. /* slave */
  218. HCI_LE_STATES_CONN_ADV_SLAVE_BIT, /* conn_adv: 38,*/
  219. HCI_LE_STATES_INIT_MASTER_SLAVE_BIT, /* init 41 */
  220. HCI_LE_STATES_INIT_MASTER_SLAVE_BIT, /* master 41 */
  221. HCI_LE_STATES_CONN_ADV_SLAVE_BIT, /* slave: 38,*/
  222. HCI_LE_STATES_LO_DUTY_DIR_ADV_SLAVE_BIT, /* lo duty cycle adv 40 */
  223. HCI_LE_STATES_HI_DUTY_DIR_ADV_SLAVE_BIT, /* hi duty cycle adv 39 */
  224. HCI_LE_STATES_NON_CONN_ADV_SLAVE_BIT, /* non connectable adv */
  225. HCI_LE_STATES_PASS_SCAN_SLAVE_BIT, /* passive scan */
  226. HCI_LE_STATES_ACTIVE_SCAN_SLAVE_BIT, /* active scan */
  227. HCI_LE_STATES_SCAN_ADV_SLAVE_BIT /* scanable adv */
  228. },
  229. {
  230. /* lo duty cycle adv */
  231. UNSUPPORTED, /* conn_adv: 38,*/
  232. HCI_LE_STATES_LO_DUTY_DIR_ADV_INIT_BIT, /* init 34 */
  233. HCI_LE_STATES_LO_DUTY_DIR_ADV_MASTER_BIT, /* master 37 */
  234. HCI_LE_STATES_LO_DUTY_DIR_ADV_SLAVE_BIT, /* slave: 40 */
  235. UNSUPPORTED, /* lo duty cycle adv 40 */
  236. UNSUPPORTED, /* hi duty cycle adv 39 */
  237. UNSUPPORTED, /* non connectable adv */
  238. UNSUPPORTED, /* TODO: passive scan, not covered? */
  239. UNSUPPORTED, /* TODO: active scan, not covered? */
  240. UNSUPPORTED /* scanable adv */
  241. },
  242. {
  243. /* hi duty cycle adv */
  244. UNSUPPORTED, /* conn_adv: 38,*/
  245. HCI_LE_STATES_HI_DUTY_DIR_ADV_INIT_BIT, /* init 33 */
  246. HCI_LE_STATES_HI_DUTY_DIR_ADV_MASTER_BIT, /* master 36 */
  247. HCI_LE_STATES_HI_DUTY_DIR_ADV_SLAVE_BIT, /* slave: 39*/
  248. UNSUPPORTED, /* lo duty cycle adv 40 */
  249. UNSUPPORTED, /* hi duty cycle adv 39 */
  250. UNSUPPORTED, /* non connectable adv */
  251. HCI_LE_STATES_HI_DUTY_DIR_ADV_PASS_SCAN_BIT, /* passive scan */
  252. HCI_LE_STATES_HI_DUTY_DIR_ADV_ACTIVE_SCAN_BIT, /* active scan */
  253. UNSUPPORTED /* scanable adv */
  254. },
  255. {
  256. /* non connectable adv */
  257. UNSUPPORTED, /* conn_adv: */
  258. HCI_LE_STATES_NON_CONN_INIT_BIT, /* init */
  259. HCI_LE_STATES_NON_CONN_ADV_MASTER_BIT, /* master */
  260. HCI_LE_STATES_NON_CONN_ADV_SLAVE_BIT, /* slave: */
  261. UNSUPPORTED, /* lo duty cycle adv */
  262. UNSUPPORTED, /* hi duty cycle adv */
  263. UNSUPPORTED, /* non connectable adv */
  264. HCI_LE_STATES_NON_CONN_ADV_PASS_SCAN_BIT, /* passive scan */
  265. HCI_LE_STATES_NON_CONN_ADV_ACTIVE_SCAN_BIT, /* active scan */
  266. UNSUPPORTED /* scanable adv */
  267. },
  268. {
  269. /* passive scan */
  270. HCI_LE_STATES_CONN_ADV_PASS_SCAN_BIT, /* conn_adv: */
  271. HCI_LE_STATES_PASS_SCAN_INIT_BIT, /* init */
  272. HCI_LE_STATES_PASS_SCAN_MASTER_BIT, /* master */
  273. HCI_LE_STATES_PASS_SCAN_SLAVE_BIT, /* slave: */
  274. UNSUPPORTED, /* lo duty cycle adv */
  275. HCI_LE_STATES_HI_DUTY_DIR_ADV_PASS_SCAN_BIT, /* hi duty cycle adv */
  276. HCI_LE_STATES_NON_CONN_ADV_PASS_SCAN_BIT, /* non connectable adv */
  277. UNSUPPORTED, /* passive scan */
  278. UNSUPPORTED, /* active scan */
  279. HCI_LE_STATES_SCAN_ADV_PASS_SCAN_BIT /* scanable adv */
  280. },
  281. {
  282. /* active scan */
  283. HCI_LE_STATES_CONN_ADV_ACTIVE_SCAN_BIT, /* conn_adv: */
  284. HCI_LE_STATES_ACTIVE_SCAN_INIT_BIT, /* init */
  285. HCI_LE_STATES_ACTIVE_SCAN_MASTER_BIT, /* master */
  286. HCI_LE_STATES_ACTIVE_SCAN_SLAVE_BIT, /* slave: */
  287. UNSUPPORTED, /* lo duty cycle adv */
  288. HCI_LE_STATES_HI_DUTY_DIR_ADV_ACTIVE_SCAN_BIT, /* hi duty cycle adv */
  289. HCI_LE_STATES_NON_CONN_ADV_ACTIVE_SCAN_BIT, /* non connectable adv */
  290. UNSUPPORTED, /* TODO: passive scan */
  291. UNSUPPORTED, /* TODO: active scan */
  292. HCI_LE_STATES_SCAN_ADV_ACTIVE_SCAN_BIT /* scanable adv */
  293. },
  294. {
  295. /* scanable adv */
  296. UNSUPPORTED, /* conn_adv: */
  297. HCI_LE_STATES_SCAN_ADV_INIT_BIT, /* init */
  298. HCI_LE_STATES_SCAN_ADV_MASTER_BIT, /* master */
  299. HCI_LE_STATES_SCAN_ADV_SLAVE_BIT, /* slave: */
  300. UNSUPPORTED, /* lo duty cycle adv */
  301. UNSUPPORTED, /* hi duty cycle adv */
  302. UNSUPPORTED, /* non connectable adv */
  303. HCI_LE_STATES_SCAN_ADV_PASS_SCAN_BIT, /* passive scan */
  304. HCI_LE_STATES_SCAN_ADV_ACTIVE_SCAN_BIT, /* active scan */
  305. UNSUPPORTED /* scanable adv */
  306. }};
  307. /* check LE combo state supported */
  308. inline bool BTM_LE_STATES_SUPPORTED(const uint8_t* x, uint8_t bit_num) {
  309. uint8_t mask = 1 << (bit_num % 8);
  310. uint8_t offset = bit_num / 8;
  311. return ((x)[offset] & mask);
  312. }
  313. /*******************************************************************************
  314. *
  315. * Function BTM_BleUpdateAdvFilterPolicy
  316. *
  317. * Description This function update the filter policy of advertiser.
  318. *
  319. * Parameter adv_policy: advertising filter policy
  320. *
  321. * Return void
  322. ******************************************************************************/
  323. void BTM_BleUpdateAdvFilterPolicy(tBTM_BLE_AFP adv_policy) {
  324. tBTM_BLE_INQ_CB* p_cb = &btm_cb.ble_ctr_cb.inq_var;
  325. tBLE_ADDR_TYPE init_addr_type = BLE_ADDR_PUBLIC;
  326. RawAddress adv_address = RawAddress::kEmpty;
  327. uint8_t adv_mode = p_cb->adv_mode;
  328. BTM_TRACE_EVENT("BTM_BleUpdateAdvFilterPolicy");
  329. if (!controller_get_interface()->supports_ble()) return;
  330. if (p_cb->afp != adv_policy) {
  331. p_cb->afp = adv_policy;
  332. /* if adv active, stop and restart */
  333. btm_ble_stop_adv();
  334. if (p_cb->connectable_mode & BTM_BLE_CONNECTABLE)
  335. p_cb->evt_type = btm_set_conn_mode_adv_init_addr(
  336. p_cb, adv_address, &init_addr_type, &p_cb->adv_addr_type);
  337. btsnd_hcic_ble_write_adv_params(
  338. (uint16_t)(p_cb->adv_interval_min ? p_cb->adv_interval_min
  339. : BTM_BLE_GAP_ADV_SLOW_INT),
  340. (uint16_t)(p_cb->adv_interval_max ? p_cb->adv_interval_max
  341. : BTM_BLE_GAP_ADV_SLOW_INT),
  342. p_cb->evt_type, p_cb->adv_addr_type, init_addr_type, adv_address,
  343. p_cb->adv_chnl_map, p_cb->afp);
  344. if (adv_mode == BTM_BLE_ADV_ENABLE) btm_ble_start_adv();
  345. }
  346. }
  347. /*******************************************************************************
  348. *
  349. * Function BTM_BleObserve
  350. *
  351. * Description This procedure keep the device listening for advertising
  352. * events from a broadcast device.
  353. *
  354. * Parameters start: start or stop observe.
  355. * white_list: use white list in observer mode or not.
  356. *
  357. * Returns void
  358. *
  359. ******************************************************************************/
  360. tBTM_STATUS BTM_BleObserve(bool start, uint8_t duration,
  361. tBTM_INQ_RESULTS_CB* p_results_cb,
  362. tBTM_CMPL_CB* p_cmpl_cb) {
  363. tBTM_BLE_INQ_CB* p_inq = &btm_cb.ble_ctr_cb.inq_var;
  364. tBTM_STATUS status = BTM_WRONG_MODE;
  365. uint32_t scan_interval =
  366. !p_inq->scan_interval ? BTM_BLE_GAP_DISC_SCAN_INT : p_inq->scan_interval;
  367. uint32_t scan_window =
  368. !p_inq->scan_window ? BTM_BLE_GAP_DISC_SCAN_WIN : p_inq->scan_window;
  369. BTM_TRACE_EVENT("%s : scan_type:%d, %d, %d", __func__,
  370. btm_cb.btm_inq_vars.scan_type, p_inq->scan_interval,
  371. p_inq->scan_window);
  372. if (!controller_get_interface()->supports_ble()) return BTM_ILLEGAL_VALUE;
  373. if (start) {
  374. /* shared inquiry database, do not allow observe if any inquiry is active */
  375. if (BTM_BLE_IS_OBS_ACTIVE(btm_cb.ble_ctr_cb.scan_activity)) {
  376. BTM_TRACE_ERROR("%s Observe Already Active", __func__);
  377. return status;
  378. }
  379. btm_cb.ble_ctr_cb.p_obs_results_cb = p_results_cb;
  380. btm_cb.ble_ctr_cb.p_obs_cmpl_cb = p_cmpl_cb;
  381. status = BTM_CMD_STARTED;
  382. /* scan is not started */
  383. if (!BTM_BLE_IS_SCAN_ACTIVE(btm_cb.ble_ctr_cb.scan_activity)) {
  384. /* allow config of scan type */
  385. p_inq->scan_type = (p_inq->scan_type == BTM_BLE_SCAN_MODE_NONE)
  386. ? BTM_BLE_SCAN_MODE_ACTI
  387. : p_inq->scan_type;
  388. /* assume observe always not using white list */
  389. #if (defined BLE_PRIVACY_SPT && BLE_PRIVACY_SPT == TRUE)
  390. /* enable resolving list */
  391. btm_ble_enable_resolving_list_for_platform(BTM_BLE_RL_SCAN);
  392. #endif
  393. btm_send_hci_set_scan_params(
  394. p_inq->scan_type, (uint16_t)scan_interval, (uint16_t)scan_window,
  395. btm_cb.ble_ctr_cb.addr_mgnt_cb.own_addr_type, BTM_BLE_DEFAULT_SFP);
  396. p_inq->scan_duplicate_filter = BTM_BLE_DUPLICATE_DISABLE;
  397. status = btm_ble_start_scan();
  398. }
  399. if (status == BTM_CMD_STARTED) {
  400. btm_cb.ble_ctr_cb.scan_activity |= BTM_LE_OBSERVE_ACTIVE;
  401. if (duration != 0) {
  402. /* start observer timer */
  403. uint64_t duration_ms = duration * 1000;
  404. alarm_set_on_mloop(btm_cb.ble_ctr_cb.observer_timer, duration_ms,
  405. btm_ble_observer_timer_timeout, NULL);
  406. }
  407. }
  408. } else if (BTM_BLE_IS_OBS_ACTIVE(btm_cb.ble_ctr_cb.scan_activity)) {
  409. status = BTM_CMD_STARTED;
  410. btm_ble_stop_observe();
  411. } else {
  412. BTM_TRACE_ERROR("%s Observe not active", __func__);
  413. }
  414. return status;
  415. }
  416. #if (BLE_VND_INCLUDED == TRUE)
  417. /*******************************************************************************
  418. *
  419. * Function btm_vsc_brcm_features_complete
  420. *
  421. * Description Command Complete callback for HCI_BLE_VENDOR_CAP_OCF
  422. *
  423. * Returns void
  424. *
  425. ******************************************************************************/
  426. static void btm_ble_vendor_capability_vsc_cmpl_cback(
  427. tBTM_VSC_CMPL* p_vcs_cplt_params) {
  428. uint8_t status = 0xFF;
  429. uint8_t* p;
  430. BTM_TRACE_DEBUG("%s", __func__);
  431. /* Check status of command complete event */
  432. CHECK(p_vcs_cplt_params->opcode == HCI_BLE_VENDOR_CAP_OCF);
  433. CHECK(p_vcs_cplt_params->param_len > 0);
  434. p = p_vcs_cplt_params->p_param_buf;
  435. STREAM_TO_UINT8(status, p);
  436. if (status != HCI_SUCCESS) {
  437. BTM_TRACE_DEBUG("%s: Status = 0x%02x (0 is success)", __func__, status);
  438. return;
  439. }
  440. CHECK(p_vcs_cplt_params->param_len > BTM_VSC_CHIP_CAPABILITY_RSP_LEN);
  441. STREAM_TO_UINT8(btm_cb.cmn_ble_vsc_cb.adv_inst_max, p);
  442. STREAM_TO_UINT8(btm_cb.cmn_ble_vsc_cb.rpa_offloading, p);
  443. STREAM_TO_UINT16(btm_cb.cmn_ble_vsc_cb.tot_scan_results_strg, p);
  444. STREAM_TO_UINT8(btm_cb.cmn_ble_vsc_cb.max_irk_list_sz, p);
  445. STREAM_TO_UINT8(btm_cb.cmn_ble_vsc_cb.filter_support, p);
  446. STREAM_TO_UINT8(btm_cb.cmn_ble_vsc_cb.max_filter, p);
  447. STREAM_TO_UINT8(btm_cb.cmn_ble_vsc_cb.energy_support, p);
  448. if (p_vcs_cplt_params->param_len >
  449. BTM_VSC_CHIP_CAPABILITY_RSP_LEN_L_RELEASE) {
  450. STREAM_TO_UINT16(btm_cb.cmn_ble_vsc_cb.version_supported, p);
  451. } else {
  452. btm_cb.cmn_ble_vsc_cb.version_supported = BTM_VSC_CHIP_CAPABILITY_L_VERSION;
  453. }
  454. if (btm_cb.cmn_ble_vsc_cb.version_supported >=
  455. BTM_VSC_CHIP_CAPABILITY_M_VERSION) {
  456. CHECK(p_vcs_cplt_params->param_len >= BTM_VSC_CHIP_CAPABILITY_RSP_LEN_M_RELEASE);
  457. STREAM_TO_UINT16(btm_cb.cmn_ble_vsc_cb.total_trackable_advertisers, p);
  458. STREAM_TO_UINT8(btm_cb.cmn_ble_vsc_cb.extended_scan_support, p);
  459. STREAM_TO_UINT8(btm_cb.cmn_ble_vsc_cb.debug_logging_supported, p);
  460. }
  461. btm_cb.cmn_ble_vsc_cb.values_read = true;
  462. BTM_TRACE_DEBUG(
  463. "%s: stat=%d, irk=%d, ADV ins:%d, rpa=%d, ener=%d, ext_scan=%d", __func__,
  464. status, btm_cb.cmn_ble_vsc_cb.max_irk_list_sz,
  465. btm_cb.cmn_ble_vsc_cb.adv_inst_max, btm_cb.cmn_ble_vsc_cb.rpa_offloading,
  466. btm_cb.cmn_ble_vsc_cb.energy_support,
  467. btm_cb.cmn_ble_vsc_cb.extended_scan_support);
  468. btm_ble_adv_init();
  469. if (btm_cb.cmn_ble_vsc_cb.max_filter > 0) btm_ble_adv_filter_init();
  470. #if (BLE_PRIVACY_SPT == TRUE)
  471. /* VS capability included and non-4.2 device */
  472. if (btm_cb.cmn_ble_vsc_cb.max_irk_list_sz > 0 &&
  473. controller_get_interface()->get_ble_resolving_list_max_size() == 0)
  474. btm_ble_resolving_list_init(btm_cb.cmn_ble_vsc_cb.max_irk_list_sz);
  475. #endif /* (BLE_PRIVACY_SPT == TRUE) */
  476. if (btm_cb.cmn_ble_vsc_cb.tot_scan_results_strg > 0) btm_ble_batchscan_init();
  477. if (p_ctrl_le_feature_rd_cmpl_cback != NULL)
  478. p_ctrl_le_feature_rd_cmpl_cback(status);
  479. }
  480. #endif /* (BLE_VND_INCLUDED == TRUE) */
  481. /*******************************************************************************
  482. *
  483. * Function BTM_BleGetVendorCapabilities
  484. *
  485. * Description This function reads local LE features
  486. *
  487. * Parameters p_cmn_vsc_cb : Locala LE capability structure
  488. *
  489. * Returns void
  490. *
  491. ******************************************************************************/
  492. extern void BTM_BleGetVendorCapabilities(tBTM_BLE_VSC_CB* p_cmn_vsc_cb) {
  493. BTM_TRACE_DEBUG("BTM_BleGetVendorCapabilities");
  494. if (NULL != p_cmn_vsc_cb) {
  495. *p_cmn_vsc_cb = btm_cb.cmn_ble_vsc_cb;
  496. }
  497. }
  498. /******************************************************************************
  499. *
  500. * Function BTM_BleReadControllerFeatures
  501. *
  502. * Description Reads BLE specific controller features
  503. *
  504. * Parameters: tBTM_BLE_CTRL_FEATURES_CBACK : Callback to notify when
  505. * features are read
  506. *
  507. * Returns void
  508. *
  509. ******************************************************************************/
  510. #if (BLE_VND_INCLUDED == TRUE)
  511. extern void BTM_BleReadControllerFeatures(
  512. tBTM_BLE_CTRL_FEATURES_CBACK* p_vsc_cback) {
  513. if (btm_cb.cmn_ble_vsc_cb.values_read) return;
  514. BTM_TRACE_DEBUG("BTM_BleReadControllerFeatures");
  515. p_ctrl_le_feature_rd_cmpl_cback = p_vsc_cback;
  516. BTM_VendorSpecificCommand(HCI_BLE_VENDOR_CAP_OCF, 0, NULL,
  517. btm_ble_vendor_capability_vsc_cmpl_cback);
  518. }
  519. #else
  520. extern void BTM_BleReadControllerFeatures(
  521. UNUSED_ATTR tBTM_BLE_CTRL_FEATURES_CBACK* p_vsc_cback) {}
  522. #endif
  523. /*******************************************************************************
  524. *
  525. * Function BTM_BleEnableMixedPrivacyMode
  526. *
  527. * Description This function is called to enabled Mixed mode if privacy 1.2
  528. * is applicable in controller.
  529. *
  530. * Parameters mixed_on: mixed mode to be used or not.
  531. *
  532. * Returns void
  533. *
  534. ******************************************************************************/
  535. void BTM_BleEnableMixedPrivacyMode(bool mixed_on) {
  536. #if (BLE_PRIVACY_SPT == TRUE)
  537. btm_cb.ble_ctr_cb.mixed_mode = mixed_on;
  538. /* TODO: send VSC to enabled mixed mode */
  539. #endif
  540. }
  541. /*******************************************************************************
  542. *
  543. * Function BTM_BleConfigPrivacy
  544. *
  545. * Description This function is called to enable or disable the privacy in
  546. * LE channel of the local device.
  547. *
  548. * Parameters privacy_mode: privacy mode on or off.
  549. *
  550. * Returns bool privacy mode set success; otherwise failed.
  551. *
  552. ******************************************************************************/
  553. bool BTM_BleConfigPrivacy(bool privacy_mode) {
  554. #if (BLE_PRIVACY_SPT == TRUE)
  555. tBTM_BLE_CB* p_cb = &btm_cb.ble_ctr_cb;
  556. BTM_TRACE_EVENT("%s", __func__);
  557. /* if LE is not supported, return error */
  558. if (!controller_get_interface()->supports_ble()) return false;
  559. tGAP_BLE_ATTR_VALUE gap_ble_attr_value;
  560. gap_ble_attr_value.addr_resolution = 0;
  561. if (!privacy_mode) /* if privacy disabled, always use public address */
  562. {
  563. p_cb->addr_mgnt_cb.own_addr_type = BLE_ADDR_PUBLIC;
  564. p_cb->privacy_mode = BTM_PRIVACY_NONE;
  565. } else /* privacy is turned on*/
  566. {
  567. /* always set host random address, used when privacy 1.1 or priavcy 1.2 is
  568. * disabled */
  569. p_cb->addr_mgnt_cb.own_addr_type = BLE_ADDR_RANDOM;
  570. btm_gen_resolvable_private_addr(base::Bind(&btm_gen_resolve_paddr_low));
  571. /* 4.2 controller only allow privacy 1.2 or mixed mode, resolvable private
  572. * address in controller */
  573. if (controller_get_interface()->supports_ble_privacy()) {
  574. gap_ble_attr_value.addr_resolution = 1;
  575. /* check vendor specific capability */
  576. p_cb->privacy_mode =
  577. btm_cb.ble_ctr_cb.mixed_mode ? BTM_PRIVACY_MIXED : BTM_PRIVACY_1_2;
  578. } else /* 4.1/4.0 controller */
  579. p_cb->privacy_mode = BTM_PRIVACY_1_1;
  580. }
  581. GAP_BleAttrDBUpdate(GATT_UUID_GAP_CENTRAL_ADDR_RESOL, &gap_ble_attr_value);
  582. return true;
  583. #else
  584. return false;
  585. #endif
  586. }
  587. /*******************************************************************************
  588. *
  589. * Function BTM_BleMaxMultiAdvInstanceCount
  590. *
  591. * Description Returns max number of multi adv instances supported by
  592. * controller
  593. *
  594. * Returns Max multi adv instance count
  595. *
  596. ******************************************************************************/
  597. extern uint8_t BTM_BleMaxMultiAdvInstanceCount(void) {
  598. return btm_cb.cmn_ble_vsc_cb.adv_inst_max < BTM_BLE_MULTI_ADV_MAX
  599. ? btm_cb.cmn_ble_vsc_cb.adv_inst_max
  600. : BTM_BLE_MULTI_ADV_MAX;
  601. }
  602. /*******************************************************************************
  603. *
  604. * Function BTM_BleLocalPrivacyEnabled
  605. *
  606. * Description Checks if local device supports private address
  607. *
  608. * Returns Return true if local privacy is enabled else false
  609. *
  610. ******************************************************************************/
  611. bool BTM_BleLocalPrivacyEnabled(void) {
  612. #if (BLE_PRIVACY_SPT == TRUE)
  613. return (btm_cb.ble_ctr_cb.privacy_mode != BTM_PRIVACY_NONE);
  614. #else
  615. return false;
  616. #endif
  617. }
  618. /*******************************************************************************
  619. *
  620. * Function BTM_BleSetConnectableMode
  621. *
  622. * Description This function is called to set BLE connectable mode for a
  623. * peripheral device.
  624. *
  625. * Parameters conn_mode: directed connectable mode, or non-directed. It
  626. * can be BTM_BLE_CONNECT_EVT,
  627. * BTM_BLE_CONNECT_DIR_EVT or
  628. * BTM_BLE_CONNECT_LO_DUTY_DIR_EVT
  629. *
  630. * Returns BTM_ILLEGAL_VALUE if controller does not support BLE.
  631. * BTM_SUCCESS is status set successfully; otherwise failure.
  632. *
  633. ******************************************************************************/
  634. tBTM_STATUS BTM_BleSetConnectableMode(tBTM_BLE_CONN_MODE connectable_mode) {
  635. tBTM_BLE_INQ_CB* p_cb = &btm_cb.ble_ctr_cb.inq_var;
  636. BTM_TRACE_EVENT("%s connectable_mode = %d ", __func__, connectable_mode);
  637. if (!controller_get_interface()->supports_ble()) return BTM_ILLEGAL_VALUE;
  638. p_cb->directed_conn = connectable_mode;
  639. return btm_ble_set_connectability(p_cb->connectable_mode);
  640. }
  641. #if (BLE_PRIVACY_SPT == TRUE)
  642. static bool is_resolving_list_bit_set(void* data, void* context) {
  643. tBTM_SEC_DEV_REC* p_dev_rec = static_cast<tBTM_SEC_DEV_REC*>(data);
  644. if ((p_dev_rec->ble.in_controller_list & BTM_RESOLVING_LIST_BIT) != 0)
  645. return false;
  646. return true;
  647. }
  648. #endif
  649. /*******************************************************************************
  650. *
  651. * Function btm_set_conn_mode_adv_init_addr
  652. *
  653. * Description set initator address type and local address type based on
  654. * adv mode.
  655. *
  656. *
  657. ******************************************************************************/
  658. static uint8_t btm_set_conn_mode_adv_init_addr(
  659. tBTM_BLE_INQ_CB* p_cb, RawAddress& p_peer_addr_ptr,
  660. tBLE_ADDR_TYPE* p_peer_addr_type, tBLE_ADDR_TYPE* p_own_addr_type) {
  661. uint8_t evt_type;
  662. #if (BLE_PRIVACY_SPT == TRUE)
  663. tBTM_SEC_DEV_REC* p_dev_rec;
  664. #endif
  665. evt_type =
  666. (p_cb->connectable_mode == BTM_BLE_NON_CONNECTABLE)
  667. ? ((p_cb->scan_rsp) ? BTM_BLE_DISCOVER_EVT : BTM_BLE_NON_CONNECT_EVT)
  668. : BTM_BLE_CONNECT_EVT;
  669. if (evt_type == BTM_BLE_CONNECT_EVT) {
  670. evt_type = p_cb->directed_conn;
  671. if (p_cb->directed_conn == BTM_BLE_CONNECT_DIR_EVT ||
  672. p_cb->directed_conn == BTM_BLE_CONNECT_LO_DUTY_DIR_EVT) {
  673. #if (BLE_PRIVACY_SPT == TRUE)
  674. /* for privacy 1.2, convert peer address as static, own address set as ID
  675. * addr */
  676. if (btm_cb.ble_ctr_cb.privacy_mode == BTM_PRIVACY_1_2 ||
  677. btm_cb.ble_ctr_cb.privacy_mode == BTM_PRIVACY_MIXED) {
  678. /* only do so for bonded device */
  679. if ((p_dev_rec = btm_find_or_alloc_dev(p_cb->direct_bda.bda)) != NULL &&
  680. p_dev_rec->ble.in_controller_list & BTM_RESOLVING_LIST_BIT) {
  681. btm_ble_enable_resolving_list(BTM_BLE_RL_ADV);
  682. p_peer_addr_ptr = p_dev_rec->ble.identity_addr;
  683. *p_peer_addr_type = p_dev_rec->ble.identity_addr_type;
  684. *p_own_addr_type = BLE_ADDR_RANDOM_ID;
  685. return evt_type;
  686. }
  687. /* otherwise fall though as normal directed adv */
  688. else {
  689. btm_ble_disable_resolving_list(BTM_BLE_RL_ADV, true);
  690. }
  691. }
  692. #endif
  693. /* direct adv mode does not have privacy, if privacy is not enabled */
  694. *p_peer_addr_type = p_cb->direct_bda.type;
  695. p_peer_addr_ptr = p_cb->direct_bda.bda;
  696. return evt_type;
  697. }
  698. }
  699. /* undirect adv mode or non-connectable mode*/
  700. #if (BLE_PRIVACY_SPT == TRUE)
  701. /* when privacy 1.2 privacy only mode is used, or mixed mode */
  702. if ((btm_cb.ble_ctr_cb.privacy_mode == BTM_PRIVACY_1_2 &&
  703. p_cb->afp != AP_SCAN_CONN_ALL) ||
  704. btm_cb.ble_ctr_cb.privacy_mode == BTM_PRIVACY_MIXED) {
  705. list_node_t* n =
  706. list_foreach(btm_cb.sec_dev_rec, is_resolving_list_bit_set, NULL);
  707. if (n) {
  708. /* if enhanced privacy is required, set Identity address and matching IRK
  709. * peer */
  710. tBTM_SEC_DEV_REC* p_dev_rec =
  711. static_cast<tBTM_SEC_DEV_REC*>(list_node(n));
  712. p_peer_addr_ptr = p_dev_rec->ble.identity_addr;
  713. *p_peer_addr_type = p_dev_rec->ble.identity_addr_type;
  714. *p_own_addr_type = BLE_ADDR_RANDOM_ID;
  715. } else {
  716. /* resolving list is empty, not enabled */
  717. *p_own_addr_type = BLE_ADDR_RANDOM;
  718. }
  719. }
  720. /* privacy 1.1, or privacy 1.2, general discoverable/connectable mode, disable
  721. privacy in */
  722. /* controller fall back to host based privacy */
  723. else if (btm_cb.ble_ctr_cb.privacy_mode != BTM_PRIVACY_NONE) {
  724. *p_own_addr_type = BLE_ADDR_RANDOM;
  725. }
  726. #endif
  727. /* if no privacy,do not set any peer address,*/
  728. /* local address type go by global privacy setting */
  729. return evt_type;
  730. }
  731. /**
  732. * This function is called to set scan parameters. |cb| is called with operation
  733. * status
  734. **/
  735. void BTM_BleSetScanParams(uint32_t scan_interval, uint32_t scan_window,
  736. tBLE_SCAN_MODE scan_mode,
  737. base::Callback<void(uint8_t)> cb) {
  738. tBTM_BLE_INQ_CB* p_cb = &btm_cb.ble_ctr_cb.inq_var;
  739. uint32_t max_scan_interval;
  740. uint32_t max_scan_window;
  741. BTM_TRACE_EVENT("%s", __func__);
  742. if (!controller_get_interface()->supports_ble()) return;
  743. /* If not supporting extended scan support, use the older range for checking
  744. */
  745. if (btm_cb.cmn_ble_vsc_cb.extended_scan_support == 0) {
  746. max_scan_interval = BTM_BLE_SCAN_INT_MAX;
  747. max_scan_window = BTM_BLE_SCAN_WIN_MAX;
  748. } else {
  749. /* If supporting extended scan support, use the new extended range for
  750. * checking */
  751. max_scan_interval = BTM_BLE_EXT_SCAN_INT_MAX;
  752. max_scan_window = BTM_BLE_EXT_SCAN_WIN_MAX;
  753. }
  754. if (BTM_BLE_ISVALID_PARAM(scan_interval, BTM_BLE_SCAN_INT_MIN,
  755. max_scan_interval) &&
  756. BTM_BLE_ISVALID_PARAM(scan_window, BTM_BLE_SCAN_WIN_MIN,
  757. max_scan_window) &&
  758. (scan_mode == BTM_BLE_SCAN_MODE_ACTI ||
  759. scan_mode == BTM_BLE_SCAN_MODE_PASS)) {
  760. p_cb->scan_type = scan_mode;
  761. p_cb->scan_interval = scan_interval;
  762. p_cb->scan_window = scan_window;
  763. cb.Run(BTM_SUCCESS);
  764. } else {
  765. cb.Run(BTM_ILLEGAL_VALUE);
  766. BTM_TRACE_ERROR("Illegal params: scan_interval = %d scan_window = %d",
  767. scan_interval, scan_window);
  768. }
  769. }
  770. /*******************************************************************************
  771. *
  772. * Function BTM_BleWriteScanRsp
  773. *
  774. * Description This function is called to write LE scan response.
  775. *
  776. * Parameters: p_scan_rsp: scan response information.
  777. *
  778. * Returns void
  779. *
  780. ******************************************************************************/
  781. void BTM_BleWriteScanRsp(uint8_t* data, uint8_t length,
  782. tBTM_BLE_ADV_DATA_CMPL_CBACK* p_adv_data_cback) {
  783. BTM_TRACE_EVENT("%s: length: %d", __func__, length);
  784. if (!controller_get_interface()->supports_ble()) {
  785. p_adv_data_cback(BTM_ILLEGAL_VALUE);
  786. return;
  787. }
  788. btsnd_hcic_ble_set_scan_rsp_data(length, data);
  789. if (length != 0)
  790. btm_cb.ble_ctr_cb.inq_var.scan_rsp = true;
  791. else
  792. btm_cb.ble_ctr_cb.inq_var.scan_rsp = false;
  793. p_adv_data_cback(BTM_SUCCESS);
  794. }
  795. /*******************************************************************************
  796. *
  797. * Function BTM__BLEReadDiscoverability
  798. *
  799. * Description This function is called to read the current LE
  800. * discoverability mode of the device.
  801. *
  802. * Returns BTM_BLE_NON_DISCOVERABLE ,BTM_BLE_LIMITED_DISCOVERABLE or
  803. * BTM_BLE_GENRAL_DISCOVERABLE
  804. *
  805. ******************************************************************************/
  806. uint16_t BTM_BleReadDiscoverability() {
  807. BTM_TRACE_API("%s", __func__);
  808. return (btm_cb.ble_ctr_cb.inq_var.discoverable_mode);
  809. }
  810. /*******************************************************************************
  811. *
  812. * Function BTM__BLEReadConnectability
  813. *
  814. * Description This function is called to read the current LE
  815. * connectability mode of the device.
  816. *
  817. * Returns BTM_BLE_NON_CONNECTABLE or BTM_BLE_CONNECTABLE
  818. *
  819. ******************************************************************************/
  820. uint16_t BTM_BleReadConnectability() {
  821. BTM_TRACE_API("%s", __func__);
  822. return (btm_cb.ble_ctr_cb.inq_var.connectable_mode);
  823. }
  824. /*******************************************************************************
  825. *
  826. * Function btm_ble_select_adv_interval
  827. *
  828. * Description select adv interval based on device mode
  829. *
  830. * Returns void
  831. *
  832. ******************************************************************************/
  833. void btm_ble_select_adv_interval(tBTM_BLE_INQ_CB* p_cb, uint8_t evt_type,
  834. uint16_t* p_adv_int_min,
  835. uint16_t* p_adv_int_max) {
  836. if (p_cb->adv_interval_min && p_cb->adv_interval_max) {
  837. *p_adv_int_min = p_cb->adv_interval_min;
  838. *p_adv_int_max = p_cb->adv_interval_max;
  839. } else {
  840. switch (evt_type) {
  841. case BTM_BLE_CONNECT_EVT:
  842. case BTM_BLE_CONNECT_LO_DUTY_DIR_EVT:
  843. *p_adv_int_min = *p_adv_int_max = BTM_BLE_GAP_ADV_FAST_INT_1;
  844. break;
  845. case BTM_BLE_NON_CONNECT_EVT:
  846. case BTM_BLE_DISCOVER_EVT:
  847. *p_adv_int_min = *p_adv_int_max = BTM_BLE_GAP_ADV_FAST_INT_2;
  848. break;
  849. /* connectable directed event */
  850. case BTM_BLE_CONNECT_DIR_EVT:
  851. *p_adv_int_min = BTM_BLE_GAP_ADV_DIR_MIN_INT;
  852. *p_adv_int_max = BTM_BLE_GAP_ADV_DIR_MAX_INT;
  853. break;
  854. default:
  855. *p_adv_int_min = *p_adv_int_max = BTM_BLE_GAP_ADV_SLOW_INT;
  856. break;
  857. }
  858. }
  859. return;
  860. }
  861. /*******************************************************************************
  862. *
  863. * Function btm_ble_update_dmt_flag_bits
  864. *
  865. * Description Obtain updated adv flag value based on connect and
  866. * discoverability mode. Also, setup DMT support value in the
  867. * flag based on whether the controller supports both LE and
  868. * BR/EDR.
  869. *
  870. * Parameters: flag_value (Input / Output) - flag value
  871. * connect_mode (Input) - Connect mode value
  872. * disc_mode (Input) - discoverability mode
  873. *
  874. * Returns void
  875. *
  876. ******************************************************************************/
  877. void btm_ble_update_dmt_flag_bits(uint8_t* adv_flag_value,
  878. const uint16_t connect_mode,
  879. const uint16_t disc_mode) {
  880. /* BR/EDR non-discoverable , non-connectable */
  881. if ((disc_mode & BTM_DISCOVERABLE_MASK) == 0 &&
  882. (connect_mode & BTM_CONNECTABLE_MASK) == 0)
  883. *adv_flag_value |= BTM_BLE_BREDR_NOT_SPT;
  884. else
  885. *adv_flag_value &= ~BTM_BLE_BREDR_NOT_SPT;
  886. /* if local controller support, mark both controller and host support in flag
  887. */
  888. if (controller_get_interface()->supports_simultaneous_le_bredr())
  889. *adv_flag_value |= (BTM_BLE_DMT_CONTROLLER_SPT | BTM_BLE_DMT_HOST_SPT);
  890. else
  891. *adv_flag_value &= ~(BTM_BLE_DMT_CONTROLLER_SPT | BTM_BLE_DMT_HOST_SPT);
  892. }
  893. /*******************************************************************************
  894. *
  895. * Function btm_ble_set_adv_flag
  896. *
  897. * Description Set adv flag in adv data.
  898. *
  899. * Parameters: connect_mode (Input)- Connect mode value
  900. * disc_mode (Input) - discoverability mode
  901. *
  902. * Returns void
  903. *
  904. ******************************************************************************/
  905. void btm_ble_set_adv_flag(uint16_t connect_mode, uint16_t disc_mode) {
  906. uint8_t flag = 0, old_flag = 0;
  907. tBTM_BLE_LOCAL_ADV_DATA* p_adv_data = &btm_cb.ble_ctr_cb.inq_var.adv_data;
  908. if (p_adv_data->p_flags != NULL) flag = old_flag = *(p_adv_data->p_flags);
  909. btm_ble_update_dmt_flag_bits(&flag, connect_mode, disc_mode);
  910. LOG_DEBUG(LOG_TAG, "disc_mode %04x", disc_mode);
  911. /* update discoverable flag */
  912. if (disc_mode & BTM_BLE_LIMITED_DISCOVERABLE) {
  913. flag &= ~BTM_BLE_GEN_DISC_FLAG;
  914. flag |= BTM_BLE_LIMIT_DISC_FLAG;
  915. } else if (disc_mode & BTM_BLE_GENERAL_DISCOVERABLE) {
  916. flag |= BTM_BLE_GEN_DISC_FLAG;
  917. flag &= ~BTM_BLE_LIMIT_DISC_FLAG;
  918. } else /* remove all discoverable flags */
  919. {
  920. flag &= ~(BTM_BLE_LIMIT_DISC_FLAG | BTM_BLE_GEN_DISC_FLAG);
  921. }
  922. if (flag != old_flag) {
  923. btm_ble_update_adv_flag(flag);
  924. }
  925. }
  926. /*******************************************************************************
  927. *
  928. * Function btm_ble_set_discoverability
  929. *
  930. * Description This function is called to set BLE discoverable mode.
  931. *
  932. * Parameters: combined_mode: discoverability mode.
  933. *
  934. * Returns BTM_SUCCESS is status set successfully; otherwise failure.
  935. *
  936. ******************************************************************************/
  937. tBTM_STATUS btm_ble_set_discoverability(uint16_t combined_mode) {
  938. tBTM_LE_RANDOM_CB* p_addr_cb = &btm_cb.ble_ctr_cb.addr_mgnt_cb;
  939. tBTM_BLE_INQ_CB* p_cb = &btm_cb.ble_ctr_cb.inq_var;
  940. uint16_t mode = (combined_mode & BTM_BLE_DISCOVERABLE_MASK);
  941. uint8_t new_mode = BTM_BLE_ADV_ENABLE;
  942. uint8_t evt_type;
  943. tBTM_STATUS status = BTM_SUCCESS;
  944. RawAddress address = RawAddress::kEmpty;
  945. tBLE_ADDR_TYPE init_addr_type = BLE_ADDR_PUBLIC,
  946. own_addr_type = p_addr_cb->own_addr_type;
  947. uint16_t adv_int_min, adv_int_max;
  948. BTM_TRACE_EVENT("%s mode=0x%0x combined_mode=0x%x", __func__, mode,
  949. combined_mode);
  950. /*** Check mode parameter ***/
  951. if (mode > BTM_BLE_MAX_DISCOVERABLE) return (BTM_ILLEGAL_VALUE);
  952. p_cb->discoverable_mode = mode;
  953. evt_type = btm_set_conn_mode_adv_init_addr(p_cb, address, &init_addr_type,
  954. &own_addr_type);
  955. if (p_cb->connectable_mode == BTM_BLE_NON_CONNECTABLE &&
  956. mode == BTM_BLE_NON_DISCOVERABLE)
  957. new_mode = BTM_BLE_ADV_DISABLE;
  958. btm_ble_select_adv_interval(p_cb, evt_type, &adv_int_min, &adv_int_max);
  959. alarm_cancel(p_cb->fast_adv_timer);
  960. /* update adv params if start advertising */
  961. BTM_TRACE_EVENT("evt_type=0x%x p-cb->evt_type=0x%x ", evt_type,
  962. p_cb->evt_type);
  963. if (new_mode == BTM_BLE_ADV_ENABLE) {
  964. btm_ble_set_adv_flag(btm_cb.btm_inq_vars.connectable_mode, combined_mode);
  965. if (evt_type != p_cb->evt_type || p_cb->adv_addr_type != own_addr_type ||
  966. !p_cb->fast_adv_on) {
  967. btm_ble_stop_adv();
  968. /* update adv params */
  969. btsnd_hcic_ble_write_adv_params(adv_int_min, adv_int_max, evt_type,
  970. own_addr_type, init_addr_type, address,
  971. p_cb->adv_chnl_map, p_cb->afp);
  972. p_cb->evt_type = evt_type;
  973. p_cb->adv_addr_type = own_addr_type;
  974. }
  975. }
  976. if (status == BTM_SUCCESS && p_cb->adv_mode != new_mode) {
  977. if (new_mode == BTM_BLE_ADV_ENABLE)
  978. status = btm_ble_start_adv();
  979. else
  980. status = btm_ble_stop_adv();
  981. }
  982. if (p_cb->adv_mode == BTM_BLE_ADV_ENABLE) {
  983. p_cb->fast_adv_on = true;
  984. /* start initial GAP mode adv timer */
  985. alarm_set_on_mloop(p_cb->fast_adv_timer, BTM_BLE_GAP_FAST_ADV_TIMEOUT_MS,
  986. btm_ble_fast_adv_timer_timeout, NULL);
  987. } else {
  988. #if (BLE_PRIVACY_SPT == TRUE)
  989. btm_ble_disable_resolving_list(BTM_BLE_RL_ADV, true);
  990. #endif
  991. }
  992. /* set up stop advertising timer */
  993. if (status == BTM_SUCCESS && mode == BTM_BLE_LIMITED_DISCOVERABLE) {
  994. BTM_TRACE_EVENT("start timer for limited disc mode duration=%d ms",
  995. BTM_BLE_GAP_LIM_TIMEOUT_MS);
  996. /* start Tgap(lim_timeout) */
  997. alarm_set_on_mloop(p_cb->inquiry_timer, BTM_BLE_GAP_LIM_TIMEOUT_MS,
  998. btm_ble_inquiry_timer_gap_limited_discovery_timeout,
  999. NULL);
  1000. }
  1001. return status;
  1002. }
  1003. /*******************************************************************************
  1004. *
  1005. * Function btm_ble_set_connectability
  1006. *
  1007. * Description This function is called to set BLE connectability mode.
  1008. *
  1009. * Parameters: combined_mode: connectability mode.
  1010. *
  1011. * Returns BTM_SUCCESS is status set successfully; otherwise failure.
  1012. *
  1013. ******************************************************************************/
  1014. tBTM_STATUS btm_ble_set_connectability(uint16_t combined_mode) {
  1015. tBTM_LE_RANDOM_CB* p_addr_cb = &btm_cb.ble_ctr_cb.addr_mgnt_cb;
  1016. tBTM_BLE_INQ_CB* p_cb = &btm_cb.ble_ctr_cb.inq_var;
  1017. uint16_t mode = (combined_mode & BTM_BLE_CONNECTABLE_MASK);
  1018. uint8_t new_mode = BTM_BLE_ADV_ENABLE;
  1019. uint8_t evt_type;
  1020. tBTM_STATUS status = BTM_SUCCESS;
  1021. RawAddress address = RawAddress::kEmpty;
  1022. tBLE_ADDR_TYPE peer_addr_type = BLE_ADDR_PUBLIC,
  1023. own_addr_type = p_addr_cb->own_addr_type;
  1024. uint16_t adv_int_min, adv_int_max;
  1025. BTM_TRACE_EVENT("%s mode=0x%0x combined_mode=0x%x", __func__, mode,
  1026. combined_mode);
  1027. /*** Check mode parameter ***/
  1028. if (mode > BTM_BLE_MAX_CONNECTABLE) return (BTM_ILLEGAL_VALUE);
  1029. p_cb->connectable_mode = mode;
  1030. evt_type = btm_set_conn_mode_adv_init_addr(p_cb, address, &peer_addr_type,
  1031. &own_addr_type);
  1032. if (mode == BTM_BLE_NON_CONNECTABLE &&
  1033. p_cb->discoverable_mode == BTM_BLE_NON_DISCOVERABLE)
  1034. new_mode = BTM_BLE_ADV_DISABLE;
  1035. btm_ble_select_adv_interval(p_cb, evt_type, &adv_int_min, &adv_int_max);
  1036. alarm_cancel(p_cb->fast_adv_timer);
  1037. /* update adv params if needed */
  1038. if (new_mode == BTM_BLE_ADV_ENABLE) {
  1039. btm_ble_set_adv_flag(combined_mode, btm_cb.btm_inq_vars.discoverable_mode);
  1040. if (p_cb->evt_type != evt_type ||
  1041. p_cb->adv_addr_type != p_addr_cb->own_addr_type || !p_cb->fast_adv_on) {
  1042. btm_ble_stop_adv();
  1043. btsnd_hcic_ble_write_adv_params(adv_int_min, adv_int_max, evt_type,
  1044. own_addr_type, peer_addr_type, address,
  1045. p_cb->adv_chnl_map, p_cb->afp);
  1046. p_cb->evt_type = evt_type;
  1047. p_cb->adv_addr_type = own_addr_type;
  1048. }
  1049. }
  1050. /* update advertising mode */
  1051. if (status == BTM_SUCCESS && new_mode != p_cb->adv_mode) {
  1052. if (new_mode == BTM_BLE_ADV_ENABLE)
  1053. status = btm_ble_start_adv();
  1054. else
  1055. status = btm_ble_stop_adv();
  1056. }
  1057. if (p_cb->adv_mode == BTM_BLE_ADV_ENABLE) {
  1058. p_cb->fast_adv_on = true;
  1059. /* start initial GAP mode adv timer */
  1060. alarm_set_on_mloop(p_cb->fast_adv_timer, BTM_BLE_GAP_FAST_ADV_TIMEOUT_MS,
  1061. btm_ble_fast_adv_timer_timeout, NULL);
  1062. } else {
  1063. #if (BLE_PRIVACY_SPT == TRUE)
  1064. btm_ble_disable_resolving_list(BTM_BLE_RL_ADV, true);
  1065. #endif
  1066. }
  1067. return status;
  1068. }
  1069. void btm_send_hci_scan_enable(uint8_t enable, uint8_t filter_duplicates) {
  1070. if (controller_get_interface()->supports_ble_extended_advertising()) {
  1071. btsnd_hcic_ble_set_extended_scan_enable(enable, filter_duplicates, 0x0000,
  1072. 0x0000);
  1073. } else {
  1074. btsnd_hcic_ble_set_scan_enable(enable, filter_duplicates);
  1075. }
  1076. }
  1077. void btm_send_hci_set_scan_params(uint8_t scan_type, uint16_t scan_int,
  1078. uint16_t scan_win, uint8_t addr_type_own,
  1079. uint8_t scan_filter_policy) {
  1080. if (controller_get_interface()->supports_ble_extended_advertising()) {
  1081. scanning_phy_cfg phy_cfg;
  1082. phy_cfg.scan_type = scan_type;
  1083. phy_cfg.scan_int = scan_int;
  1084. phy_cfg.scan_win = scan_win;
  1085. btsnd_hcic_ble_set_extended_scan_params(addr_type_own, scan_filter_policy,
  1086. 1, &phy_cfg);
  1087. } else {
  1088. btsnd_hcic_ble_set_scan_params(scan_type, scan_int, scan_win, addr_type_own,
  1089. scan_filter_policy);
  1090. }
  1091. }
  1092. /*******************************************************************************
  1093. *
  1094. * Function btm_ble_start_inquiry
  1095. *
  1096. * Description This function is called to start BLE inquiry procedure.
  1097. * If the duration is zero, the periodic inquiry mode is
  1098. * cancelled.
  1099. *
  1100. * Parameters: mode - GENERAL or LIMITED inquiry
  1101. * p_inq_params - pointer to the BLE inquiry parameter.
  1102. * p_results_cb - callback returning pointer to results
  1103. * (tBTM_INQ_RESULTS)
  1104. * p_cmpl_cb - callback indicating the end of an inquiry
  1105. *
  1106. *
  1107. *
  1108. * Returns BTM_CMD_STARTED if successfully started
  1109. * BTM_NO_RESOURCES if could not allocate a message buffer
  1110. * BTM_BUSY - if an inquiry is already active
  1111. *
  1112. ******************************************************************************/
  1113. tBTM_STATUS btm_ble_start_inquiry(uint8_t mode, uint8_t duration) {
  1114. tBTM_STATUS status = BTM_CMD_STARTED;
  1115. tBTM_BLE_CB* p_ble_cb = &btm_cb.ble_ctr_cb;
  1116. tBTM_INQUIRY_VAR_ST* p_inq = &btm_cb.btm_inq_vars;
  1117. BTM_TRACE_DEBUG("btm_ble_start_inquiry: mode = %02x inq_active = 0x%02x",
  1118. mode, btm_cb.btm_inq_vars.inq_active);
  1119. /* if selective connection is active, or inquiry is already active, reject it
  1120. */
  1121. if (BTM_BLE_IS_INQ_ACTIVE(p_ble_cb->scan_activity)) {
  1122. BTM_TRACE_ERROR("LE Inquiry is active, can not start inquiry");
  1123. return (BTM_BUSY);
  1124. }
  1125. if (!BTM_BLE_IS_SCAN_ACTIVE(p_ble_cb->scan_activity)) {
  1126. btm_send_hci_set_scan_params(
  1127. BTM_BLE_SCAN_MODE_ACTI, BTM_BLE_LOW_LATENCY_SCAN_INT,
  1128. BTM_BLE_LOW_LATENCY_SCAN_WIN,
  1129. btm_cb.ble_ctr_cb.addr_mgnt_cb.own_addr_type, SP_ADV_ALL);
  1130. #if (BLE_PRIVACY_SPT == TRUE)
  1131. /* enable IRK list */
  1132. btm_ble_enable_resolving_list_for_platform(BTM_BLE_RL_SCAN);
  1133. #endif
  1134. p_ble_cb->inq_var.scan_duplicate_filter = BTM_BLE_DUPLICATE_DISABLE;
  1135. status = btm_ble_start_scan();
  1136. } else if ((p_ble_cb->inq_var.scan_interval !=
  1137. BTM_BLE_LOW_LATENCY_SCAN_INT) ||
  1138. (p_ble_cb->inq_var.scan_window != BTM_BLE_LOW_LATENCY_SCAN_WIN)) {
  1139. BTM_TRACE_DEBUG("%s, restart LE scan with low latency scan params",
  1140. __func__);
  1141. btm_send_hci_scan_enable(BTM_BLE_SCAN_DISABLE, BTM_BLE_DUPLICATE_ENABLE);
  1142. btm_send_hci_set_scan_params(
  1143. BTM_BLE_SCAN_MODE_ACTI, BTM_BLE_LOW_LATENCY_SCAN_INT,
  1144. BTM_BLE_LOW_LATENCY_SCAN_WIN,
  1145. btm_cb.ble_ctr_cb.addr_mgnt_cb.own_addr_type, SP_ADV_ALL);
  1146. btm_send_hci_scan_enable(BTM_BLE_SCAN_ENABLE, BTM_BLE_DUPLICATE_DISABLE);
  1147. }
  1148. if (status == BTM_CMD_STARTED) {
  1149. p_inq->inq_active |= mode;
  1150. p_ble_cb->scan_activity |= mode;
  1151. BTM_TRACE_DEBUG("btm_ble_start_inquiry inq_active = 0x%02x",
  1152. p_inq->inq_active);
  1153. if (duration != 0) {
  1154. /* start inquiry timer */
  1155. uint64_t duration_ms = duration * 1000;
  1156. alarm_set_on_mloop(p_ble_cb->inq_var.inquiry_timer, duration_ms,
  1157. btm_ble_inquiry_timer_timeout, NULL);
  1158. }
  1159. }
  1160. return status;
  1161. }
  1162. /*******************************************************************************
  1163. *
  1164. * Function btm_ble_read_remote_name_cmpl
  1165. *
  1166. * Description This function is called when BLE remote name is received.
  1167. *
  1168. * Returns void
  1169. *
  1170. ******************************************************************************/
  1171. void btm_ble_read_remote_name_cmpl(bool status, const RawAddress& bda,
  1172. uint16_t length, char* p_name) {
  1173. uint8_t hci_status = HCI_SUCCESS;
  1174. BD_NAME bd_name;
  1175. memset(bd_name, 0, (BD_NAME_LEN + 1));
  1176. if (length > BD_NAME_LEN) {
  1177. length = BD_NAME_LEN;
  1178. }
  1179. memcpy((uint8_t*)bd_name, p_name, length);
  1180. if ((!status) || (length == 0)) {
  1181. hci_status = HCI_ERR_HOST_TIMEOUT;
  1182. }
  1183. btm_process_remote_name(&bda, bd_name, length + 1, hci_status);
  1184. btm_sec_rmt_name_request_complete(&bda, (uint8_t*)p_name, hci_status);
  1185. }
  1186. /*******************************************************************************
  1187. *
  1188. * Function btm_ble_read_remote_name
  1189. *
  1190. * Description This function read remote LE device name using GATT read
  1191. * procedure.
  1192. *
  1193. * Parameters: None.
  1194. *
  1195. * Returns void
  1196. *
  1197. ******************************************************************************/
  1198. tBTM_STATUS btm_ble_read_remote_name(const RawAddress& remote_bda,
  1199. tBTM_CMPL_CB* p_cb) {
  1200. tBTM_INQUIRY_VAR_ST* p_inq = &btm_cb.btm_inq_vars;
  1201. if (!controller_get_interface()->supports_ble()) return BTM_ERR_PROCESSING;
  1202. tINQ_DB_ENT* p_i = btm_inq_db_find(remote_bda);
  1203. if (p_i && !ble_evt_type_is_connectable(p_i->inq_info.results.ble_evt_type)) {
  1204. BTM_TRACE_DEBUG("name request to non-connectable device failed.");
  1205. return BTM_ERR_PROCESSING;
  1206. }
  1207. /* read remote device name using GATT procedure */
  1208. if (p_inq->remname_active) return BTM_BUSY;
  1209. if (!GAP_BleReadPeerDevName(remote_bda, btm_ble_read_remote_name_cmpl))
  1210. return BTM_BUSY;
  1211. p_inq->p_remname_cmpl_cb = p_cb;
  1212. p_inq->remname_active = true;
  1213. p_inq->remname_bda = remote_bda;
  1214. alarm_set_on_mloop(p_inq->remote_name_timer, BTM_EXT_BLE_RMT_NAME_TIMEOUT_MS,
  1215. btm_inq_remote_name_timer_timeout, NULL);
  1216. return BTM_CMD_STARTED;
  1217. }
  1218. /*******************************************************************************
  1219. *
  1220. * Function btm_ble_cancel_remote_name
  1221. *
  1222. * Description This function cancel read remote LE device name.
  1223. *
  1224. * Parameters: None.
  1225. *
  1226. * Returns void
  1227. *
  1228. ******************************************************************************/
  1229. bool btm_ble_cancel_remote_name(const RawAddress& remote_bda) {
  1230. tBTM_INQUIRY_VAR_ST* p_inq = &btm_cb.btm_inq_vars;
  1231. bool status;
  1232. status = GAP_BleCancelReadPeerDevName(remote_bda);
  1233. p_inq->remname_active = false;
  1234. p_inq->remname_bda = RawAddress::kEmpty;
  1235. alarm_cancel(p_inq->remote_name_timer);
  1236. return status;
  1237. }
  1238. /*******************************************************************************
  1239. *
  1240. * Function btm_ble_update_adv_flag
  1241. *
  1242. * Description This function update the limited discoverable flag in the
  1243. * adv data.
  1244. *
  1245. * Parameters: None.
  1246. *
  1247. * Returns void
  1248. *
  1249. ******************************************************************************/
  1250. static void btm_ble_update_adv_flag(uint8_t flag) {
  1251. tBTM_BLE_LOCAL_ADV_DATA* p_adv_data = &btm_cb.ble_ctr_cb.inq_var.adv_data;
  1252. uint8_t* p;
  1253. BTM_TRACE_DEBUG("btm_ble_update_adv_flag new=0x%x", flag);
  1254. if (p_adv_data->p_flags != NULL) {
  1255. BTM_TRACE_DEBUG("btm_ble_update_adv_flag old=0x%x", *p_adv_data->p_flags);
  1256. *p_adv_data->p_flags = flag;
  1257. } else /* no FLAGS in ADV data*/
  1258. {
  1259. p = (p_adv_data->p_pad == NULL) ? p_adv_data->ad_data : p_adv_data->p_pad;
  1260. /* need 3 bytes space to stuff in the flags, if not */
  1261. /* erase all written data, just for flags */
  1262. if ((BTM_BLE_AD_DATA_LEN - (p - p_adv_data->ad_data)) < 3) {
  1263. p = p_adv_data->p_pad = p_adv_data->ad_data;
  1264. memset(p_adv_data->ad_data, 0, BTM_BLE_AD_DATA_LEN);
  1265. }
  1266. *p++ = 2;
  1267. *p++ = BTM_BLE_AD_TYPE_FLAG;
  1268. p_adv_data->p_flags = p;
  1269. *p++ = flag;
  1270. p_adv_data->p_pad = p;
  1271. }
  1272. btsnd_hcic_ble_set_adv_data(
  1273. (uint8_t)(p_adv_data->p_pad - p_adv_data->ad_data), p_adv_data->ad_data);
  1274. p_adv_data->data_mask |= BTM_BLE_AD_BIT_FLAGS;
  1275. }
  1276. /**
  1277. * Check ADV flag to make sure device is discoverable and match the search
  1278. * condition
  1279. */
  1280. uint8_t btm_ble_is_discoverable(const RawAddress& bda,
  1281. std::vector<uint8_t> const& adv_data) {
  1282. uint8_t flag = 0, rt = 0;
  1283. uint8_t data_len;
  1284. tBTM_INQ_PARMS* p_cond = &btm_cb.btm_inq_vars.inqparms;
  1285. /* for observer, always "discoverable */
  1286. if (BTM_BLE_IS_OBS_ACTIVE(btm_cb.ble_ctr_cb.scan_activity))
  1287. rt |= BTM_BLE_OBS_RESULT;
  1288. /* does not match filter condition */
  1289. if (p_cond->filter_cond_type == BTM_FILTER_COND_BD_ADDR &&
  1290. bda != p_cond->filter_cond.bdaddr_cond) {
  1291. BTM_TRACE_DEBUG("BD ADDR does not meet filter condition");
  1292. return rt;
  1293. }
  1294. if (!adv_data.empty()) {
  1295. const uint8_t* p_flag = AdvertiseDataParser::GetFieldByType(
  1296. adv_data, BTM_BLE_AD_TYPE_FLAG, &data_len);
  1297. if (p_flag != NULL && data_len != 0) {
  1298. flag = *p_flag;
  1299. if ((btm_cb.btm_inq_vars.inq_active & BTM_BLE_GENERAL_INQUIRY) &&
  1300. (flag & (BTM_BLE_LIMIT_DISC_FLAG | BTM_BLE_GEN_DISC_FLAG)) != 0) {
  1301. BTM_TRACE_DEBUG("Find Generable Discoverable device");
  1302. rt |= BTM_BLE_INQ_RESULT;
  1303. }
  1304. else if (btm_cb.btm_inq_vars.inq_active & BTM_BLE_LIMITED_INQUIRY &&
  1305. (flag & BTM_BLE_LIMIT_DISC_FLAG) != 0) {
  1306. BTM_TRACE_DEBUG("Find limited discoverable device");
  1307. rt |= BTM_BLE_INQ_RESULT;
  1308. }
  1309. }
  1310. }
  1311. return rt;
  1312. }
  1313. static void btm_ble_appearance_to_cod(uint16_t appearance, uint8_t* dev_class) {
  1314. dev_class[0] = 0;
  1315. switch (appearance) {
  1316. case BTM_BLE_APPEARANCE_GENERIC_PHONE:
  1317. dev_class[1] = BTM_COD_MAJOR_PHONE;
  1318. dev_class[2] = BTM_COD_MINOR_UNCLASSIFIED;
  1319. break;
  1320. case BTM_BLE_APPEARANCE_GENERIC_COMPUTER:
  1321. dev_class[1] = BTM_COD_MAJOR_COMPUTER;
  1322. dev_class[2] = BTM_COD_MINOR_UNCLASSIFIED;
  1323. break;
  1324. case BTM_BLE_APPEARANCE_GENERIC_REMOTE:
  1325. dev_class[1] = BTM_COD_MAJOR_PERIPHERAL;
  1326. dev_class[2] = BTM_COD_MINOR_REMOTE_CONTROL;
  1327. break;
  1328. case BTM_BLE_APPEARANCE_GENERIC_THERMOMETER:
  1329. case BTM_BLE_APPEARANCE_THERMOMETER_EAR:
  1330. dev_class[1] = BTM_COD_MAJOR_HEALTH;
  1331. dev_class[2] = BTM_COD_MINOR_THERMOMETER;
  1332. break;
  1333. case BTM_BLE_APPEARANCE_GENERIC_HEART_RATE:
  1334. case BTM_BLE_APPEARANCE_HEART_RATE_BELT:
  1335. dev_class[1] = BTM_COD_MAJOR_HEALTH;
  1336. dev_class[2] = BTM_COD_MINOR_HEART_PULSE_MONITOR;
  1337. break;
  1338. case BTM_BLE_APPEARANCE_GENERIC_BLOOD_PRESSURE:
  1339. case BTM_BLE_APPEARANCE_BLOOD_PRESSURE_ARM:
  1340. case BTM_BLE_APPEARANCE_BLOOD_PRESSURE_WRIST:
  1341. dev_class[1] = BTM_COD_MAJOR_HEALTH;
  1342. dev_class[2] = BTM_COD_MINOR_BLOOD_MONITOR;
  1343. break;
  1344. case BTM_BLE_APPEARANCE_GENERIC_PULSE_OXIMETER:
  1345. case BTM_BLE_APPEARANCE_PULSE_OXIMETER_FINGERTIP:
  1346. case BTM_BLE_APPEARANCE_PULSE_OXIMETER_WRIST:
  1347. dev_class[1] = BTM_COD_MAJOR_HEALTH;
  1348. dev_class[2] = BTM_COD_MINOR_PULSE_OXIMETER;
  1349. break;
  1350. case BTM_BLE_APPEARANCE_GENERIC_GLUCOSE:
  1351. dev_class[1] = BTM_COD_MAJOR_HEALTH;
  1352. dev_class[2] = BTM_COD_MINOR_GLUCOSE_METER;
  1353. break;
  1354. case BTM_BLE_APPEARANCE_GENERIC_WEIGHT:
  1355. dev_class[1] = BTM_COD_MAJOR_HEALTH;
  1356. dev_class[2] = BTM_COD_MINOR_WEIGHING_SCALE;
  1357. break;
  1358. case BTM_BLE_APPEARANCE_GENERIC_WALKING:
  1359. case BTM_BLE_APPEARANCE_WALKING_IN_SHOE:
  1360. case BTM_BLE_APPEARANCE_WALKING_ON_SHOE:
  1361. case BTM_BLE_APPEARANCE_WALKING_ON_HIP:
  1362. dev_class[1] = BTM_COD_MAJOR_HEALTH;
  1363. dev_class[2] = BTM_COD_MINOR_STEP_COUNTER;
  1364. break;
  1365. case BTM_BLE_APPEARANCE_GENERIC_WATCH:
  1366. case BTM_BLE_APPEARANCE_SPORTS_WATCH:
  1367. dev_class[1] = BTM_COD_MAJOR_WEARABLE;
  1368. dev_class[2] = BTM_COD_MINOR_WRIST_WATCH;
  1369. break;
  1370. case BTM_BLE_APPEARANCE_GENERIC_EYEGLASSES:
  1371. dev_class[1] = BTM_COD_MAJOR_WEARABLE;
  1372. dev_class[2] = BTM_COD_MINOR_GLASSES;
  1373. break;
  1374. case BTM_BLE_APPEARANCE_GENERIC_DISPLAY:
  1375. dev_class[1] = BTM_COD_MAJOR_IMAGING;
  1376. dev_class[2] = BTM_COD_MINOR_DISPLAY;
  1377. break;
  1378. case BTM_BLE_APPEARANCE_GENERIC_MEDIA_PLAYER:
  1379. dev_class[1] = BTM_COD_MAJOR_AUDIO;
  1380. dev_class[2] = BTM_COD_MINOR_UNCLASSIFIED;
  1381. break;
  1382. case BTM_BLE_APPEARANCE_GENERIC_BARCODE_SCANNER:
  1383. case BTM_BLE_APPEARANCE_HID_BARCODE_SCANNER:
  1384. case BTM_BLE_APPEARANCE_GENERIC_HID:
  1385. dev_class[1] = BTM_COD_MAJOR_PERIPHERAL;
  1386. dev_class[2] = BTM_COD_MINOR_UNCLASSIFIED;
  1387. break;
  1388. case BTM_BLE_APPEARANCE_HID_KEYBOARD:
  1389. dev_class[1] = BTM_COD_MAJOR_PERIPHERAL;
  1390. dev_class[2] = BTM_COD_MINOR_KEYBOARD;
  1391. break;
  1392. case BTM_BLE_APPEARANCE_HID_MOUSE:
  1393. dev_class[1] = BTM_COD_MAJOR_PERIPHERAL;
  1394. dev_class[2] = BTM_COD_MINOR_POINTING;
  1395. break;
  1396. case BTM_BLE_APPEARANCE_HID_JOYSTICK:
  1397. dev_class[1] = BTM_COD_MAJOR_PERIPHERAL;
  1398. dev_class[2] = BTM_COD_MINOR_JOYSTICK;
  1399. break;
  1400. case BTM_BLE_APPEARANCE_HID_GAMEPAD:
  1401. dev_class[1] = BTM_COD_MAJOR_PERIPHERAL;
  1402. dev_class[2] = BTM_COD_MINOR_GAMEPAD;
  1403. break;
  1404. case BTM_BLE_APPEARANCE_HID_DIGITIZER_TABLET:
  1405. dev_class[1] = BTM_COD_MAJOR_PERIPHERAL;
  1406. dev_class[2] = BTM_COD_MINOR_DIGITIZING_TABLET;
  1407. break;
  1408. case BTM_BLE_APPEARANCE_HID_CARD_READER:
  1409. dev_class[1] = BTM_COD_MAJOR_PERIPHERAL;
  1410. dev_class[2] = BTM_COD_MINOR_CARD_READER;
  1411. break;
  1412. case BTM_BLE_APPEARANCE_HID_DIGITAL_PEN:
  1413. dev_class[1] = BTM_COD_MAJOR_PERIPHERAL;
  1414. dev_class[2] = BTM_COD_MINOR_DIGITAL_PAN;
  1415. break;
  1416. case BTM_BLE_APPEARANCE_UKNOWN:
  1417. case BTM_BLE_APPEARANCE_GENERIC_CLOCK:
  1418. case BTM_BLE_APPEARANCE_GENERIC_TAG:
  1419. case BTM_BLE_APPEARANCE_GENERIC_KEYRING:
  1420. case BTM_BLE_APPEARANCE_GENERIC_CYCLING:
  1421. case BTM_BLE_APPEARANCE_CYCLING_COMPUTER:
  1422. case BTM_BLE_APPEARANCE_CYCLING_SPEED:
  1423. case BTM_BLE_APPEARANCE_CYCLING_CADENCE:
  1424. case BTM_BLE_APPEARANCE_CYCLING_POWER:
  1425. case BTM_BLE_APPEARANCE_CYCLING_SPEED_CADENCE:
  1426. case BTM_BLE_APPEARANCE_GENERIC_OUTDOOR_SPORTS:
  1427. case BTM_BLE_APPEARANCE_OUTDOOR_SPORTS_LOCATION:
  1428. case BTM_BLE_APPEARANCE_OUTDOOR_SPORTS_LOCATION_AND_NAV:
  1429. case BTM_BLE_APPEARANCE_OUTDOOR_SPORTS_LOCATION_POD:
  1430. case BTM_BLE_APPEARANCE_OUTDOOR_SPORTS_LOCATION_POD_AND_NAV:
  1431. default:
  1432. dev_class[1] = BTM_COD_MAJOR_UNCLASSIFIED;
  1433. dev_class[2] = BTM_COD_MINOR_UNCLASSIFIED;
  1434. };
  1435. }
  1436. /**
  1437. * Update adv packet information into inquiry result.
  1438. */
  1439. void btm_ble_update_inq_result(tINQ_DB_ENT* p_i, uint8_t addr_type,
  1440. const RawAddress& bda, uint16_t evt_type,
  1441. uint8_t primary_phy, uint8_t secondary_phy,
  1442. uint8_t advertising_sid, int8_t tx_power,
  1443. int8_t rssi, uint16_t periodic_adv_int,
  1444. std::vector<uint8_t> const& data) {
  1445. tBTM_INQ_RESULTS* p_cur = &p_i->inq_info.results;
  1446. uint8_t len;
  1447. tBTM_INQUIRY_VAR_ST* p_inq = &btm_cb.btm_inq_vars;
  1448. /* Save the info */
  1449. p_cur->inq_result_type = BTM_INQ_RESULT_BLE;
  1450. p_cur->ble_addr_type = addr_type;
  1451. p_cur->rssi = rssi;
  1452. p_cur->ble_primary_phy = primary_phy;
  1453. p_cur->ble_secondary_phy = secondary_phy;
  1454. p_cur->ble_advertising_sid = advertising_sid;
  1455. p_cur->ble_tx_power = tx_power;
  1456. p_cur->ble_periodic_adv_int = periodic_adv_int;
  1457. if (btm_cb.ble_ctr_cb.inq_var.scan_type == BTM_BLE_SCAN_MODE_ACTI &&
  1458. ble_evt_type_is_scannable(evt_type) &&
  1459. !ble_evt_type_is_scan_resp(evt_type)) {
  1460. p_i->scan_rsp = false;
  1461. } else
  1462. p_i->scan_rsp = true;
  1463. if (p_i->inq_count != p_inq->inq_counter)
  1464. p_cur->device_type = BT_DEVICE_TYPE_BLE;
  1465. else
  1466. p_cur->device_type |= BT_DEVICE_TYPE_BLE;
  1467. if (evt_type != BTM_BLE_SCAN_RSP_EVT) p_cur->ble_evt_type = evt_type;
  1468. p_i->inq_count = p_inq->inq_counter; /* Mark entry for current inquiry */
  1469. if (!data.empty()) {
  1470. const uint8_t* p_flag =
  1471. AdvertiseDataParser::GetFieldByType(data, BTM_BLE_AD_TYPE_FLAG, &len);
  1472. if (p_flag != NULL && len != 0) p_cur->flag = *p_flag;
  1473. }
  1474. if (!data.empty()) {
  1475. /* Check to see the BLE device has the Appearance UUID in the advertising
  1476. * data. If it does
  1477. * then try to convert the appearance value to a class of device value
  1478. * Bluedroid can use.
  1479. * Otherwise fall back to trying to infer if it is a HID device based on the
  1480. * service class.
  1481. */
  1482. const uint8_t* p_uuid16 = AdvertiseDataParser::GetFieldByType(
  1483. data, BTM_BLE_AD_TYPE_APPEARANCE, &len);
  1484. if (p_uuid16 && len == 2) {
  1485. btm_ble_appearance_to_cod((uint16_t)p_uuid16[0] | (p_uuid16[1] << 8),
  1486. p_cur->dev_class);
  1487. } else {
  1488. p_uuid16 = AdvertiseDataParser::GetFieldByType(
  1489. data, BTM_BLE_AD_TYPE_16SRV_CMPL, &len);
  1490. if (p_uuid16 != NULL) {
  1491. uint8_t i;
  1492. for (i = 0; i + 2 <= len; i = i + 2) {
  1493. /* if this BLE device support HID over LE, set HID Major in class of
  1494. * device */
  1495. if ((p_uuid16[i] | (p_uuid16[i + 1] << 8)) == UUID_SERVCLASS_LE_HID) {
  1496. p_cur->dev_class[0] = 0;
  1497. p_cur->dev_class[1] = BTM_COD_MAJOR_PERIPHERAL;
  1498. p_cur->dev_class[2] = 0;
  1499. break;
  1500. }
  1501. }
  1502. }
  1503. }
  1504. }
  1505. /* if BR/EDR not supported is not set, assume is a DUMO device */
  1506. if ((p_cur->flag & BTM_BLE_BREDR_NOT_SPT) == 0 &&
  1507. !ble_evt_type_is_directed(evt_type)) {
  1508. if (p_cur->ble_addr_type != BLE_ADDR_RANDOM) {
  1509. BTM_TRACE_DEBUG("BR/EDR NOT support bit not set, treat as DUMO");
  1510. p_cur->device_type |= BT_DEVICE_TYPE_DUMO;
  1511. } else {
  1512. BTM_TRACE_DEBUG("Random address, treating device as LE only");
  1513. }
  1514. } else {
  1515. BTM_TRACE_DEBUG("BR/EDR NOT SUPPORT bit set, LE only device");
  1516. }
  1517. }
  1518. /*******************************************************************************
  1519. *
  1520. * Function btm_clear_all_pending_le_entry
  1521. *
  1522. * Description This function is called to clear all LE pending entry in
  1523. * inquiry database.
  1524. *
  1525. * Returns void
  1526. *
  1527. ******************************************************************************/
  1528. void btm_clear_all_pending_le_entry(void) {
  1529. uint16_t xx;
  1530. tINQ_DB_ENT* p_ent = btm_cb.btm_inq_vars.inq_db;
  1531. for (xx = 0; xx < BTM_INQ_DB_SIZE; xx++, p_ent++) {
  1532. /* mark all pending LE entry as unused if an LE only device has scan
  1533. * response outstanding */
  1534. if ((p_ent->in_use) &&
  1535. (p_ent->inq_info.results.device_type == BT_DEVICE_TYPE_BLE) &&
  1536. !p_ent->scan_rsp)
  1537. p_ent->in_use = false;
  1538. }
  1539. }
  1540. void btm_ble_process_adv_addr(RawAddress& bda, uint8_t* addr_type) {
  1541. #if (BLE_PRIVACY_SPT == TRUE)
  1542. /* map address to security record */
  1543. bool match = btm_identity_addr_to_random_pseudo(&bda, addr_type, false);
  1544. VLOG(1) << __func__ << ": bda=" << bda;
  1545. /* always do RRA resolution on host */
  1546. if (!match && BTM_BLE_IS_RESOLVE_BDA(bda)) {
  1547. tBTM_SEC_DEV_REC* match_rec = btm_ble_resolve_random_addr(bda);
  1548. if (match_rec) {
  1549. match_rec->ble.active_addr_type = BTM_BLE_ADDR_RRA;
  1550. match_rec->ble.cur_rand_addr = bda;
  1551. if (btm_ble_init_pseudo_addr(match_rec, bda)) {
  1552. bda = match_rec->bd_addr;
  1553. } else {
  1554. // Assign the original address to be the current report address
  1555. bda = match_rec->ble.pseudo_addr;
  1556. *addr_type = match_rec->ble.ble_addr_type;
  1557. }
  1558. }
  1559. }
  1560. #endif
  1561. }
  1562. /**
  1563. * This function is called when extended advertising report event is received .
  1564. * It updates the inquiry database. If the inquiry database is full, the oldest
  1565. * entry is discarded.
  1566. */
  1567. void btm_ble_process_ext_adv_pkt(uint8_t data_len, uint8_t* data) {
  1568. RawAddress bda, direct_address;
  1569. uint8_t* p = data;
  1570. uint8_t addr_type, num_reports, pkt_data_len, primary_phy, secondary_phy,
  1571. advertising_sid;
  1572. int8_t rssi, tx_power;
  1573. uint16_t event_type, periodic_adv_int, direct_address_type;
  1574. /* Only process the results if the inquiry is still active */
  1575. if (!BTM_BLE_IS_SCAN_ACTIVE(btm_cb.ble_ctr_cb.scan_activity)) return;
  1576. /* Extract the number of reports in this event. */
  1577. STREAM_TO_UINT8(num_reports, p);
  1578. while (num_reports--) {
  1579. if (p > data + data_len) {
  1580. // TODO(jpawlowski): we should crash the stack here
  1581. BTM_TRACE_ERROR(
  1582. "Malformed LE Extended Advertising Report Event from controller - "
  1583. "can't loop the data");
  1584. return;
  1585. }
  1586. /* Extract inquiry results */
  1587. STREAM_TO_UINT16(event_type, p);
  1588. STREAM_TO_UINT8(addr_type, p);
  1589. STREAM_TO_BDADDR(bda, p);
  1590. STREAM_TO_UINT8(primary_phy, p);
  1591. STREAM_TO_UINT8(secondary_phy, p);
  1592. STREAM_TO_UINT8(advertising_sid, p);
  1593. STREAM_TO_INT8(tx_power, p);
  1594. STREAM_TO_INT8(rssi, p);
  1595. STREAM_TO_UINT16(periodic_adv_int, p);
  1596. STREAM_TO_UINT8(direct_address_type, p);
  1597. STREAM_TO_BDADDR(direct_address, p);
  1598. STREAM_TO_UINT8(pkt_data_len, p);
  1599. uint8_t* pkt_data = p;
  1600. p += pkt_data_len; /* Advance to the the next packet*/
  1601. if (p > data + data_len) {
  1602. LOG(ERROR) << "Invalid pkt_data_len: " << +pkt_data_len;
  1603. return;
  1604. }
  1605. if (rssi >= 21 && rssi <= 126) {
  1606. BTM_TRACE_ERROR("%s: bad rssi value in advertising report: %d", __func__,
  1607. rssi);
  1608. }
  1609. if (addr_type != BLE_ADDR_ANONYMOUS) {
  1610. btm_ble_process_adv_addr(bda, &addr_type);
  1611. }
  1612. btm_ble_process_adv_pkt_cont(event_type, addr_type, bda, primary_phy,
  1613. secondary_phy, advertising_sid, tx_power, rssi,
  1614. periodic_adv_int, pkt_data_len, pkt_data);
  1615. }
  1616. }
  1617. /**
  1618. * This function is called when advertising report event is received. It updates
  1619. * the inquiry database. If the inquiry database is full, the oldest entry is
  1620. * discarded.
  1621. */
  1622. void btm_ble_process_adv_pkt(uint8_t data_len, uint8_t* data) {
  1623. RawAddress bda;
  1624. uint8_t* p = data;
  1625. uint8_t legacy_evt_type, addr_type, num_reports, pkt_data_len;
  1626. int8_t rssi;
  1627. /* Only process the results if the inquiry is still active */
  1628. if (!BTM_BLE_IS_SCAN_ACTIVE(btm_cb.ble_ctr_cb.scan_activity)) return;
  1629. /* Extract the number of reports in this event. */
  1630. STREAM_TO_UINT8(num_reports, p);
  1631. while (num_reports--) {
  1632. if (p > data + data_len) {
  1633. // TODO(jpawlowski): we should crash the stack here
  1634. BTM_TRACE_ERROR("Malformed LE Advertising Report Event from controller");
  1635. return;
  1636. }
  1637. /* Extract inquiry results */
  1638. STREAM_TO_UINT8(legacy_evt_type, p);
  1639. STREAM_TO_UINT8(addr_type, p);
  1640. STREAM_TO_BDADDR(bda, p);
  1641. STREAM_TO_UINT8(pkt_data_len, p);
  1642. uint8_t* pkt_data = p;
  1643. p += pkt_data_len; /* Advance to the the rssi byte */
  1644. if (p > data + data_len - sizeof(rssi)) {
  1645. LOG(ERROR) << "Invalid pkt_data_len: " << +pkt_data_len;
  1646. return;
  1647. }
  1648. STREAM_TO_INT8(rssi, p);
  1649. if (rssi >= 21 && rssi <= 126) {
  1650. BTM_TRACE_ERROR("%s: bad rssi value in advertising report: ", __func__,
  1651. pkt_data_len, rssi);
  1652. }
  1653. btm_ble_process_adv_addr(bda, &addr_type);
  1654. uint16_t event_type;
  1655. if (legacy_evt_type == 0x00) { // ADV_IND;
  1656. event_type = 0x0013;
  1657. } else if (legacy_evt_type == 0x01) { // ADV_DIRECT_IND;
  1658. event_type = 0x0015;
  1659. } else if (legacy_evt_type == 0x02) { // ADV_SCAN_IND;
  1660. event_type = 0x0012;
  1661. } else if (legacy_evt_type == 0x03) { // ADV_NONCONN_IND;
  1662. event_type = 0x0010;
  1663. } else if (legacy_evt_type == 0x04) { // SCAN_RSP;
  1664. // We can't distinguish between "SCAN_RSP to an ADV_IND", and "SCAN_RSP to
  1665. // an ADV_SCAN_IND", so always return "SCAN_RSP to an ADV_IND"
  1666. event_type = 0x001B;
  1667. } else {
  1668. BTM_TRACE_ERROR(
  1669. "Malformed LE Advertising Report Event - unsupported "
  1670. "legacy_event_type 0x%02x",
  1671. legacy_evt_type);
  1672. return;
  1673. }
  1674. btm_ble_process_adv_pkt_cont(
  1675. event_type, addr_type, bda, PHY_LE_1M, PHY_LE_NO_PACKET, NO_ADI_PRESENT,
  1676. TX_POWER_NOT_PRESENT, rssi, 0x00 /* no periodic adv */, pkt_data_len,
  1677. pkt_data);
  1678. }
  1679. }
  1680. /**
  1681. * This function is called after random address resolution is done, and proceed
  1682. * to process adv packet.
  1683. */
  1684. static void btm_ble_process_adv_pkt_cont(
  1685. uint16_t evt_type, uint8_t addr_type, const RawAddress& bda,
  1686. uint8_t primary_phy, uint8_t secondary_phy, uint8_t advertising_sid,
  1687. int8_t tx_power, int8_t rssi, uint16_t periodic_adv_int, uint8_t data_len,
  1688. uint8_t* data) {
  1689. tBTM_INQUIRY_VAR_ST* p_inq = &btm_cb.btm_inq_vars;
  1690. bool update = true;
  1691. std::vector<uint8_t> tmp;
  1692. if (data_len != 0) tmp.insert(tmp.begin(), data, data + data_len);
  1693. bool is_scannable = ble_evt_type_is_scannable(evt_type);
  1694. bool is_scan_resp = ble_evt_type_is_scan_resp(evt_type);
  1695. bool is_start =
  1696. ble_evt_type_is_legacy(evt_type) && is_scannable && !is_scan_resp;
  1697. if (ble_evt_type_is_legacy(evt_type))
  1698. AdvertiseDataParser::RemoveTrailingZeros(tmp);
  1699. // We might have send scan request to this device before, but didn't get the
  1700. // response. In such case make sure data is put at start, not appended to
  1701. // already existing data.
  1702. std::vector<uint8_t> const& adv_data =
  1703. is_start ? cache.Set(addr_type, bda, std::move(tmp))
  1704. : cache.Append(addr_type, bda, std::move(tmp));
  1705. bool data_complete = (ble_evt_type_data_status(evt_type) != 0x01);
  1706. if (!data_complete) {
  1707. // If we didn't receive whole adv data yet, don't report the device.
  1708. DVLOG(1) << "Data not complete yet, waiting for more " << bda;
  1709. return;
  1710. }
  1711. bool is_active_scan =
  1712. btm_cb.ble_ctr_cb.inq_var.scan_type == BTM_BLE_SCAN_MODE_ACTI;
  1713. if (is_active_scan && is_scannable && !is_scan_resp) {
  1714. // If we didn't receive scan response yet, don't report the device.
  1715. DVLOG(1) << " Waiting for scan response " << bda;
  1716. return;
  1717. }
  1718. if (!AdvertiseDataParser::IsValid(adv_data)) {
  1719. DVLOG(1) << __func__ << "Dropping bad advertisement packet: "
  1720. << base::HexEncode(adv_data.data(), adv_data.size());
  1721. return;
  1722. }
  1723. tINQ_DB_ENT* p_i = btm_inq_db_find(bda);
  1724. /* Check if this address has already been processed for this inquiry */
  1725. if (btm_inq_find_bdaddr(bda)) {
  1726. /* never been report as an LE device */
  1727. if (p_i && (!(p_i->inq_info.results.device_type & BT_DEVICE_TYPE_BLE) ||
  1728. /* scan repsonse to be updated */
  1729. (!p_i->scan_rsp))) {
  1730. update = true;
  1731. } else if (BTM_BLE_IS_OBS_ACTIVE(btm_cb.ble_ctr_cb.scan_activity)) {
  1732. update = false;
  1733. } else {
  1734. /* if yes, skip it */
  1735. return; /* assumption: one result per event */
  1736. }
  1737. }
  1738. /* If existing entry, use that, else get a new one (possibly reusing the
  1739. * oldest) */
  1740. if (p_i == NULL) {
  1741. p_i = btm_inq_db_new(bda);
  1742. if (p_i != NULL) {
  1743. p_inq->inq_cmpl_info.num_resp++;
  1744. } else
  1745. return;
  1746. } else if (p_i->inq_count !=
  1747. p_inq->inq_counter) /* first time seen in this inquiry */
  1748. {
  1749. p_inq->inq_cmpl_info.num_resp++;
  1750. }
  1751. /* update the LE device information in inquiry database */
  1752. btm_ble_update_inq_result(p_i, addr_type, bda, evt_type, primary_phy,
  1753. secondary_phy, advertising_sid, tx_power, rssi,
  1754. periodic_adv_int, adv_data);
  1755. uint8_t result = btm_ble_is_discoverable(bda, adv_data);
  1756. if (result == 0) {
  1757. cache.Clear(addr_type, bda);
  1758. LOG_WARN(LOG_TAG,
  1759. "%s device no longer discoverable, discarding advertising packet",
  1760. __func__);
  1761. return;
  1762. }
  1763. if (!update) result &= ~BTM_BLE_INQ_RESULT;
  1764. /* If the number of responses found and limited, issue a cancel inquiry */
  1765. if (p_inq->inqparms.max_resps &&
  1766. p_inq->inq_cmpl_info.num_resp == p_inq->inqparms.max_resps) {
  1767. /* new device */
  1768. if (p_i == NULL ||
  1769. /* assume a DUMO device, BR/EDR inquiry is always active */
  1770. (p_i &&
  1771. (p_i->inq_info.results.device_type & BT_DEVICE_TYPE_BLE) ==
  1772. BT_DEVICE_TYPE_BLE &&
  1773. p_i->scan_rsp)) {
  1774. BTM_TRACE_WARNING(
  1775. "INQ RES: Extra Response Received...cancelling inquiry..");
  1776. /* if is non-periodic inquiry active, cancel now */
  1777. if ((p_inq->inq_active & BTM_BR_INQ_ACTIVE_MASK) != 0 &&
  1778. (p_inq->inq_active & BTM_PERIODIC_INQUIRY_ACTIVE) == 0)
  1779. btsnd_hcic_inq_cancel();
  1780. btm_ble_stop_inquiry();
  1781. btm_acl_update_busy_level(BTM_BLI_INQ_DONE_EVT);
  1782. }
  1783. }
  1784. tBTM_INQ_RESULTS_CB* p_inq_results_cb = p_inq->p_inq_results_cb;
  1785. if (p_inq_results_cb && (result & BTM_BLE_INQ_RESULT)) {
  1786. (p_inq_results_cb)((tBTM_INQ_RESULTS*)&p_i->inq_info.results,
  1787. const_cast<uint8_t*>(adv_data.data()), adv_data.size());
  1788. }
  1789. tBTM_INQ_RESULTS_CB* p_obs_results_cb = btm_cb.ble_ctr_cb.p_obs_results_cb;
  1790. if (p_obs_results_cb && (result & BTM_BLE_OBS_RESULT)) {
  1791. (p_obs_results_cb)((tBTM_INQ_RESULTS*)&p_i->inq_info.results,
  1792. const_cast<uint8_t*>(adv_data.data()), adv_data.size());
  1793. }
  1794. cache.Clear(addr_type, bda);
  1795. }
  1796. void btm_ble_process_phy_update_pkt(uint8_t len, uint8_t* data) {
  1797. uint8_t status, tx_phy, rx_phy;
  1798. uint16_t handle;
  1799. LOG_ASSERT(len == 5);
  1800. uint8_t* p = data;
  1801. STREAM_TO_UINT8(status, p);
  1802. STREAM_TO_UINT16(handle, p);
  1803. handle = handle & 0x0FFF;
  1804. STREAM_TO_UINT8(tx_phy, p);
  1805. STREAM_TO_UINT8(rx_phy, p);
  1806. gatt_notify_phy_updated(status, handle, tx_phy, rx_phy);
  1807. }
  1808. /*******************************************************************************
  1809. *
  1810. * Function btm_ble_start_scan
  1811. *
  1812. * Description Start the BLE scan.
  1813. *
  1814. * Returns void
  1815. *
  1816. ******************************************************************************/
  1817. tBTM_STATUS btm_ble_start_scan(void) {
  1818. tBTM_BLE_INQ_CB* p_inq = &btm_cb.ble_ctr_cb.inq_var;
  1819. /* start scan, disable duplicate filtering */
  1820. btm_send_hci_scan_enable(BTM_BLE_SCAN_ENABLE, p_inq->scan_duplicate_filter);
  1821. if (p_inq->scan_type == BTM_BLE_SCAN_MODE_ACTI)
  1822. btm_ble_set_topology_mask(BTM_BLE_STATE_ACTIVE_SCAN_BIT);
  1823. else
  1824. btm_ble_set_topology_mask(BTM_BLE_STATE_PASSIVE_SCAN_BIT);
  1825. return BTM_CMD_STARTED;
  1826. }
  1827. /*******************************************************************************
  1828. *
  1829. * Function btm_ble_stop_scan
  1830. *
  1831. * Description Stop the BLE scan.
  1832. *
  1833. * Returns void
  1834. *
  1835. ******************************************************************************/
  1836. void btm_ble_stop_scan(void) {
  1837. BTM_TRACE_EVENT("btm_ble_stop_scan ");
  1838. if (btm_cb.ble_ctr_cb.inq_var.scan_type == BTM_BLE_SCAN_MODE_ACTI)
  1839. btm_ble_clear_topology_mask(BTM_BLE_STATE_ACTIVE_SCAN_BIT);
  1840. else
  1841. btm_ble_clear_topology_mask(BTM_BLE_STATE_PASSIVE_SCAN_BIT);
  1842. /* Clear the inquiry callback if set */
  1843. btm_cb.ble_ctr_cb.inq_var.scan_type = BTM_BLE_SCAN_MODE_NONE;
  1844. /* stop discovery now */
  1845. btm_send_hci_scan_enable(BTM_BLE_SCAN_DISABLE, BTM_BLE_DUPLICATE_ENABLE);
  1846. btm_update_scanner_filter_policy(SP_ADV_ALL);
  1847. }
  1848. /*******************************************************************************
  1849. *
  1850. * Function btm_ble_stop_inquiry
  1851. *
  1852. * Description Stop the BLE Inquiry.
  1853. *
  1854. * Returns void
  1855. *
  1856. ******************************************************************************/
  1857. void btm_ble_stop_inquiry(void) {
  1858. tBTM_INQUIRY_VAR_ST* p_inq = &btm_cb.btm_inq_vars;
  1859. tBTM_BLE_CB* p_ble_cb = &btm_cb.ble_ctr_cb;
  1860. alarm_cancel(p_ble_cb->inq_var.inquiry_timer);
  1861. p_ble_cb->scan_activity &= ~BTM_BLE_INQUIRY_MASK;
  1862. /* If no more scan activity, stop LE scan now */
  1863. if (!BTM_BLE_IS_SCAN_ACTIVE(p_ble_cb->scan_activity))
  1864. btm_ble_stop_scan();
  1865. else if ((p_ble_cb->inq_var.scan_interval != BTM_BLE_LOW_LATENCY_SCAN_INT) ||
  1866. (p_ble_cb->inq_var.scan_window != BTM_BLE_LOW_LATENCY_SCAN_WIN)) {
  1867. BTM_TRACE_DEBUG("%s: setting default params for ongoing observe", __func__);
  1868. btm_ble_stop_scan();
  1869. btm_ble_start_scan();
  1870. }
  1871. /* If we have a callback registered for inquiry complete, call it */
  1872. BTM_TRACE_DEBUG("BTM Inq Compl Callback: status 0x%02x, num results %d",
  1873. p_inq->inq_cmpl_info.status, p_inq->inq_cmpl_info.num_resp);
  1874. btm_process_inq_complete(
  1875. HCI_SUCCESS, (uint8_t)(p_inq->inqparms.mode & BTM_BLE_INQUIRY_MASK));
  1876. }
  1877. /*******************************************************************************
  1878. *
  1879. * Function btm_ble_stop_observe
  1880. *
  1881. * Description Stop the BLE Observe.
  1882. *
  1883. * Returns void
  1884. *
  1885. ******************************************************************************/
  1886. static void btm_ble_stop_observe(void) {
  1887. tBTM_BLE_CB* p_ble_cb = &btm_cb.ble_ctr_cb;
  1888. tBTM_CMPL_CB* p_obs_cb = p_ble_cb->p_obs_cmpl_cb;
  1889. alarm_cancel(p_ble_cb->observer_timer);
  1890. p_ble_cb->scan_activity &= ~BTM_LE_OBSERVE_ACTIVE;
  1891. p_ble_cb->p_obs_results_cb = NULL;
  1892. p_ble_cb->p_obs_cmpl_cb = NULL;
  1893. if (!BTM_BLE_IS_SCAN_ACTIVE(p_ble_cb->scan_activity)) btm_ble_stop_scan();
  1894. if (p_obs_cb) (p_obs_cb)(&btm_cb.btm_inq_vars.inq_cmpl_info);
  1895. }
  1896. /*******************************************************************************
  1897. *
  1898. * Function btm_ble_adv_states_operation
  1899. *
  1900. * Description Set or clear adv states in topology mask
  1901. *
  1902. * Returns operation status. true if sucessful, false otherwise.
  1903. *
  1904. ******************************************************************************/
  1905. typedef bool(BTM_TOPOLOGY_FUNC_PTR)(tBTM_BLE_STATE_MASK);
  1906. static bool btm_ble_adv_states_operation(BTM_TOPOLOGY_FUNC_PTR* p_handler,
  1907. uint8_t adv_evt) {
  1908. bool rt = false;
  1909. switch (adv_evt) {
  1910. case BTM_BLE_CONNECT_EVT:
  1911. rt = (*p_handler)(BTM_BLE_STATE_CONN_ADV_BIT);
  1912. break;
  1913. case BTM_BLE_NON_CONNECT_EVT:
  1914. rt = (*p_handler)(BTM_BLE_STATE_NON_CONN_ADV_BIT);
  1915. break;
  1916. case BTM_BLE_CONNECT_DIR_EVT:
  1917. rt = (*p_handler)(BTM_BLE_STATE_HI_DUTY_DIR_ADV_BIT);
  1918. break;
  1919. case BTM_BLE_DISCOVER_EVT:
  1920. rt = (*p_handler)(BTM_BLE_STATE_SCAN_ADV_BIT);
  1921. break;
  1922. case BTM_BLE_CONNECT_LO_DUTY_DIR_EVT:
  1923. rt = (*p_handler)(BTM_BLE_STATE_LO_DUTY_DIR_ADV_BIT);
  1924. break;
  1925. default:
  1926. BTM_TRACE_ERROR("unknown adv event : %d", adv_evt);
  1927. break;
  1928. }
  1929. return rt;
  1930. }
  1931. /*******************************************************************************
  1932. *
  1933. * Function btm_ble_start_adv
  1934. *
  1935. * Description start the BLE advertising.
  1936. *
  1937. * Returns void
  1938. *
  1939. ******************************************************************************/
  1940. tBTM_STATUS btm_ble_start_adv(void) {
  1941. tBTM_BLE_INQ_CB* p_cb = &btm_cb.ble_ctr_cb.inq_var;
  1942. if (!btm_ble_adv_states_operation(btm_ble_topology_check, p_cb->evt_type))
  1943. return BTM_WRONG_MODE;
  1944. #if (BLE_PRIVACY_SPT == TRUE)
  1945. /* To relax resolving list, always have resolving list enabled, unless
  1946. * directed adv */
  1947. if (p_cb->evt_type != BTM_BLE_CONNECT_LO_DUTY_DIR_EVT &&
  1948. p_cb->evt_type != BTM_BLE_CONNECT_DIR_EVT)
  1949. /* enable resolving list is desired */
  1950. btm_ble_enable_resolving_list_for_platform(BTM_BLE_RL_ADV);
  1951. #endif
  1952. btsnd_hcic_ble_set_adv_enable(BTM_BLE_ADV_ENABLE);
  1953. p_cb->adv_mode = BTM_BLE_ADV_ENABLE;
  1954. btm_ble_adv_states_operation(btm_ble_set_topology_mask, p_cb->evt_type);
  1955. return BTM_SUCCESS;
  1956. }
  1957. /*******************************************************************************
  1958. *
  1959. * Function btm_ble_stop_adv
  1960. *
  1961. * Description Stop the BLE advertising.
  1962. *
  1963. * Returns void
  1964. *
  1965. ******************************************************************************/
  1966. tBTM_STATUS btm_ble_stop_adv(void) {
  1967. tBTM_BLE_INQ_CB* p_cb = &btm_cb.ble_ctr_cb.inq_var;
  1968. if (p_cb->adv_mode == BTM_BLE_ADV_ENABLE) {
  1969. btsnd_hcic_ble_set_adv_enable(BTM_BLE_ADV_DISABLE);
  1970. p_cb->fast_adv_on = false;
  1971. p_cb->adv_mode = BTM_BLE_ADV_DISABLE;
  1972. /* clear all adv states */
  1973. btm_ble_clear_topology_mask(BTM_BLE_STATE_ALL_ADV_MASK);
  1974. }
  1975. return BTM_SUCCESS;
  1976. }
  1977. static void btm_ble_fast_adv_timer_timeout(UNUSED_ATTR void* data) {
  1978. /* fast adv is completed, fall back to slow adv interval */
  1979. btm_ble_start_slow_adv();
  1980. }
  1981. /*******************************************************************************
  1982. *
  1983. * Function btm_ble_start_slow_adv
  1984. *
  1985. * Description Restart adv with slow adv interval
  1986. *
  1987. * Returns void
  1988. *
  1989. ******************************************************************************/
  1990. static void btm_ble_start_slow_adv(void) {
  1991. tBTM_BLE_INQ_CB* p_cb = &btm_cb.ble_ctr_cb.inq_var;
  1992. if (p_cb->adv_mode == BTM_BLE_ADV_ENABLE) {
  1993. tBTM_LE_RANDOM_CB* p_addr_cb = &btm_cb.ble_ctr_cb.addr_mgnt_cb;
  1994. RawAddress address = RawAddress::kEmpty;
  1995. tBLE_ADDR_TYPE init_addr_type = BLE_ADDR_PUBLIC;
  1996. tBLE_ADDR_TYPE own_addr_type = p_addr_cb->own_addr_type;
  1997. btm_ble_stop_adv();
  1998. p_cb->evt_type = btm_set_conn_mode_adv_init_addr(
  1999. p_cb, address, &init_addr_type, &own_addr_type);
  2000. /* slow adv mode never goes into directed adv */
  2001. btsnd_hcic_ble_write_adv_params(
  2002. BTM_BLE_GAP_ADV_SLOW_INT, BTM_BLE_GAP_ADV_SLOW_INT, p_cb->evt_type,
  2003. own_addr_type, init_addr_type, address, p_cb->adv_chnl_map, p_cb->afp);
  2004. btm_ble_start_adv();
  2005. }
  2006. }
  2007. static void btm_ble_inquiry_timer_gap_limited_discovery_timeout(
  2008. UNUSED_ATTR void* data) {
  2009. /* lim_timeout expired, limited discovery should exit now */
  2010. btm_cb.btm_inq_vars.discoverable_mode &= ~BTM_BLE_LIMITED_DISCOVERABLE;
  2011. btm_ble_set_adv_flag(btm_cb.btm_inq_vars.connectable_mode,
  2012. btm_cb.btm_inq_vars.discoverable_mode);
  2013. }
  2014. static void btm_ble_inquiry_timer_timeout(UNUSED_ATTR void* data) {
  2015. btm_ble_stop_inquiry();
  2016. }
  2017. static void btm_ble_observer_timer_timeout(UNUSED_ATTR void* data) {
  2018. btm_ble_stop_observe();
  2019. }
  2020. void btm_ble_refresh_raddr_timer_timeout(UNUSED_ATTR void* data) {
  2021. if (btm_cb.ble_ctr_cb.addr_mgnt_cb.own_addr_type == BLE_ADDR_RANDOM) {
  2022. /* refresh the random addr */
  2023. btm_gen_resolvable_private_addr(base::Bind(&btm_gen_resolve_paddr_low));
  2024. }
  2025. }
  2026. /*******************************************************************************
  2027. *
  2028. * Function btm_ble_read_remote_features_complete
  2029. *
  2030. * Description This function is called when the command complete message
  2031. * is received from the HCI for the read LE remote feature
  2032. * supported complete event.
  2033. *
  2034. * Returns void
  2035. *
  2036. ******************************************************************************/
  2037. void btm_ble_read_remote_features_complete(uint8_t* p) {
  2038. BTM_TRACE_EVENT("%s", __func__);
  2039. uint16_t handle;
  2040. uint8_t status;
  2041. STREAM_TO_UINT8(status, p);
  2042. STREAM_TO_UINT16(handle, p);
  2043. handle = handle & 0x0FFF; // only 12 bits meaningful
  2044. if (status != HCI_SUCCESS) {
  2045. BTM_TRACE_ERROR("%s: failed for handle: 0x%04d, status 0x%02x", __func__,
  2046. handle, status);
  2047. if (status != HCI_ERR_UNSUPPORTED_REM_FEATURE) return;
  2048. }
  2049. int idx = btm_handle_to_acl_index(handle);
  2050. if (idx == MAX_L2CAP_LINKS) {
  2051. BTM_TRACE_ERROR("%s: can't find acl for handle: 0x%04d", __func__, handle);
  2052. return;
  2053. }
  2054. if (status == HCI_SUCCESS) {
  2055. STREAM_TO_ARRAY(btm_cb.acl_db[idx].peer_le_features, p, BD_FEATURES_LEN);
  2056. }
  2057. btsnd_hcic_rmt_ver_req(handle);
  2058. }
  2059. /*******************************************************************************
  2060. *
  2061. * Function btm_ble_write_adv_enable_complete
  2062. *
  2063. * Description This function process the write adv enable command complete.
  2064. *
  2065. * Returns void
  2066. *
  2067. ******************************************************************************/
  2068. void btm_ble_write_adv_enable_complete(uint8_t* p) {
  2069. tBTM_BLE_INQ_CB* p_cb = &btm_cb.ble_ctr_cb.inq_var;
  2070. /* if write adv enable/disbale not succeed */
  2071. if (*p != HCI_SUCCESS) {
  2072. /* toggle back the adv mode */
  2073. p_cb->adv_mode = !p_cb->adv_mode;
  2074. }
  2075. }
  2076. /*******************************************************************************
  2077. *
  2078. * Function btm_ble_dir_adv_tout
  2079. *
  2080. * Description when directed adv time out
  2081. *
  2082. * Returns void
  2083. *
  2084. ******************************************************************************/
  2085. void btm_ble_dir_adv_tout(void) {
  2086. btm_cb.ble_ctr_cb.inq_var.adv_mode = BTM_BLE_ADV_DISABLE;
  2087. /* make device fall back into undirected adv mode by default */
  2088. btm_cb.ble_ctr_cb.inq_var.directed_conn = false;
  2089. }
  2090. /*******************************************************************************
  2091. *
  2092. * Function btm_ble_set_topology_mask
  2093. *
  2094. * Description set BLE topology mask
  2095. *
  2096. * Returns true is request is allowed, false otherwise.
  2097. *
  2098. ******************************************************************************/
  2099. bool btm_ble_set_topology_mask(tBTM_BLE_STATE_MASK request_state_mask) {
  2100. request_state_mask &= BTM_BLE_STATE_ALL_MASK;
  2101. btm_cb.ble_ctr_cb.cur_states |= (request_state_mask & BTM_BLE_STATE_ALL_MASK);
  2102. return true;
  2103. }
  2104. /*******************************************************************************
  2105. *
  2106. * Function btm_ble_clear_topology_mask
  2107. *
  2108. * Description Clear BLE topology bit mask
  2109. *
  2110. * Returns true is request is allowed, false otherwise.
  2111. *
  2112. ******************************************************************************/
  2113. bool btm_ble_clear_topology_mask(tBTM_BLE_STATE_MASK request_state_mask) {
  2114. request_state_mask &= BTM_BLE_STATE_ALL_MASK;
  2115. btm_cb.ble_ctr_cb.cur_states &= ~request_state_mask;
  2116. return true;
  2117. }
  2118. /*******************************************************************************
  2119. *
  2120. * Function btm_ble_update_link_topology_mask
  2121. *
  2122. * Description This function update the link topology mask
  2123. *
  2124. * Returns void
  2125. *
  2126. ******************************************************************************/
  2127. void btm_ble_update_link_topology_mask(uint8_t link_role, bool increase) {
  2128. btm_ble_clear_topology_mask(BTM_BLE_STATE_ALL_CONN_MASK);
  2129. if (increase)
  2130. btm_cb.ble_ctr_cb.link_count[link_role]++;
  2131. else if (btm_cb.ble_ctr_cb.link_count[link_role] > 0)
  2132. btm_cb.ble_ctr_cb.link_count[link_role]--;
  2133. if (btm_cb.ble_ctr_cb.link_count[HCI_ROLE_MASTER])
  2134. btm_ble_set_topology_mask(BTM_BLE_STATE_MASTER_BIT);
  2135. if (btm_cb.ble_ctr_cb.link_count[HCI_ROLE_SLAVE])
  2136. btm_ble_set_topology_mask(BTM_BLE_STATE_SLAVE_BIT);
  2137. if (link_role == HCI_ROLE_SLAVE && increase) {
  2138. btm_cb.ble_ctr_cb.inq_var.adv_mode = BTM_BLE_ADV_DISABLE;
  2139. /* make device fall back into undirected adv mode by default */
  2140. btm_cb.ble_ctr_cb.inq_var.directed_conn = BTM_BLE_CONNECT_EVT;
  2141. /* clear all adv states */
  2142. btm_ble_clear_topology_mask(BTM_BLE_STATE_ALL_ADV_MASK);
  2143. }
  2144. }
  2145. /*******************************************************************************
  2146. *
  2147. * Function btm_ble_update_mode_operation
  2148. *
  2149. * Description This function update the GAP role operation when a link
  2150. * status is updated.
  2151. *
  2152. * Returns void
  2153. *
  2154. ******************************************************************************/
  2155. void btm_ble_update_mode_operation(uint8_t link_role, const RawAddress* bd_addr,
  2156. uint8_t status) {
  2157. if (status == HCI_ERR_ADVERTISING_TIMEOUT) {
  2158. btm_cb.ble_ctr_cb.inq_var.adv_mode = BTM_BLE_ADV_DISABLE;
  2159. /* make device fall back into undirected adv mode by default */
  2160. btm_cb.ble_ctr_cb.inq_var.directed_conn = BTM_BLE_CONNECT_EVT;
  2161. /* clear all adv states */
  2162. btm_ble_clear_topology_mask(BTM_BLE_STATE_ALL_ADV_MASK);
  2163. }
  2164. if (btm_cb.ble_ctr_cb.inq_var.connectable_mode == BTM_BLE_CONNECTABLE) {
  2165. btm_ble_set_connectability(btm_cb.btm_inq_vars.connectable_mode |
  2166. btm_cb.ble_ctr_cb.inq_var.connectable_mode);
  2167. }
  2168. /* in case of disconnected, we must cancel bgconn and restart
  2169. in order to add back device to white list in order to reconnect */
  2170. if (bd_addr) btm_ble_bgconn_cancel_if_disconnected(*bd_addr);
  2171. /* when no connection is attempted, and controller is not rejecting last
  2172. request
  2173. due to resource limitation, start next direct connection or background
  2174. connection
  2175. now in order */
  2176. if (btm_ble_get_conn_st() == BLE_CONN_IDLE &&
  2177. status != HCI_ERR_HOST_REJECT_RESOURCES &&
  2178. status != HCI_ERR_MAX_NUM_OF_CONNECTIONS) {
  2179. btm_ble_resume_bg_conn();
  2180. }
  2181. }
  2182. /*******************************************************************************
  2183. *
  2184. * Function btm_ble_init
  2185. *
  2186. * Description Initialize the control block variable values.
  2187. *
  2188. * Returns void
  2189. *
  2190. ******************************************************************************/
  2191. void btm_ble_init(void) {
  2192. tBTM_BLE_CB* p_cb = &btm_cb.ble_ctr_cb;
  2193. BTM_TRACE_DEBUG("%s", __func__);
  2194. alarm_free(p_cb->observer_timer);
  2195. alarm_free(p_cb->inq_var.fast_adv_timer);
  2196. memset(p_cb, 0, sizeof(tBTM_BLE_CB));
  2197. memset(&(btm_cb.cmn_ble_vsc_cb), 0, sizeof(tBTM_BLE_VSC_CB));
  2198. btm_cb.cmn_ble_vsc_cb.values_read = false;
  2199. p_cb->observer_timer = alarm_new("btm_ble.observer_timer");
  2200. p_cb->cur_states = 0;
  2201. p_cb->inq_var.adv_mode = BTM_BLE_ADV_DISABLE;
  2202. p_cb->inq_var.scan_type = BTM_BLE_SCAN_MODE_NONE;
  2203. p_cb->inq_var.adv_chnl_map = BTM_BLE_DEFAULT_ADV_CHNL_MAP;
  2204. p_cb->inq_var.afp = BTM_BLE_DEFAULT_AFP;
  2205. p_cb->inq_var.sfp = BTM_BLE_DEFAULT_SFP;
  2206. p_cb->inq_var.connectable_mode = BTM_BLE_NON_CONNECTABLE;
  2207. p_cb->inq_var.discoverable_mode = BTM_BLE_NON_DISCOVERABLE;
  2208. p_cb->inq_var.fast_adv_timer = alarm_new("btm_ble_inq.fast_adv_timer");
  2209. p_cb->inq_var.inquiry_timer = alarm_new("btm_ble_inq.inquiry_timer");
  2210. /* for background connection, reset connection params to be undefined */
  2211. p_cb->scan_int = p_cb->scan_win = BTM_BLE_SCAN_PARAM_UNDEF;
  2212. p_cb->inq_var.evt_type = BTM_BLE_NON_CONNECT_EVT;
  2213. p_cb->addr_mgnt_cb.refresh_raddr_timer =
  2214. alarm_new("btm_ble_addr.refresh_raddr_timer");
  2215. #if (BLE_VND_INCLUDED == FALSE)
  2216. btm_ble_adv_filter_init();
  2217. #endif
  2218. }
  2219. /*******************************************************************************
  2220. *
  2221. * Function btm_ble_topology_check
  2222. *
  2223. * Description check to see requested state is supported. One state check
  2224. * at a time is supported
  2225. *
  2226. * Returns true is request is allowed, false otherwise.
  2227. *
  2228. ******************************************************************************/
  2229. bool btm_ble_topology_check(tBTM_BLE_STATE_MASK request_state_mask) {
  2230. bool rt = false;
  2231. uint8_t state_offset = 0;
  2232. uint16_t cur_states = btm_cb.ble_ctr_cb.cur_states;
  2233. uint8_t request_state = 0;
  2234. /* check only one bit is set and within valid range */
  2235. if (request_state_mask == BTM_BLE_STATE_INVALID ||
  2236. request_state_mask > BTM_BLE_STATE_SCAN_ADV_BIT ||
  2237. (request_state_mask & (request_state_mask - 1)) != 0) {
  2238. BTM_TRACE_ERROR("illegal state requested: %d", request_state_mask);
  2239. return rt;
  2240. }
  2241. while (request_state_mask) {
  2242. request_state_mask >>= 1;
  2243. request_state++;
  2244. }
  2245. /* check if the requested state is supported or not */
  2246. uint8_t bit_num = btm_le_state_combo_tbl[0][request_state - 1];
  2247. const uint8_t* ble_supported_states =
  2248. controller_get_interface()->get_ble_supported_states();
  2249. if (!BTM_LE_STATES_SUPPORTED(ble_supported_states, bit_num)) {
  2250. BTM_TRACE_ERROR("state requested not supported: %d", request_state);
  2251. return rt;
  2252. }
  2253. rt = true;
  2254. /* make sure currently active states are all supported in conjunction with the
  2255. requested state. If the bit in table is UNSUPPORTED, the combination is not
  2256. supported */
  2257. while (cur_states != 0) {
  2258. if (cur_states & 0x01) {
  2259. uint8_t bit_num = btm_le_state_combo_tbl[request_state][state_offset];
  2260. if (bit_num != UNSUPPORTED) {
  2261. if (!BTM_LE_STATES_SUPPORTED(ble_supported_states, bit_num)) {
  2262. rt = false;
  2263. break;
  2264. }
  2265. }
  2266. }
  2267. cur_states >>= 1;
  2268. state_offset++;
  2269. }
  2270. return rt;
  2271. }