ble_advertiser_hci_interface.cc 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792
  1. /******************************************************************************
  2. *
  3. * Copyright 2016 The Android Open Source Project
  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. #include "ble_advertiser_hci_interface.h"
  19. #include "btm_api.h"
  20. #include "btm_ble_api.h"
  21. #include "btm_int_types.h"
  22. #include "device/include/controller.h"
  23. #include "hcidefs.h"
  24. #include "log/log.h"
  25. #include <queue>
  26. #include <utility>
  27. #include <base/bind.h>
  28. #include <base/callback.h>
  29. #include <base/location.h>
  30. #include <base/logging.h>
  31. #define BTM_BLE_MULTI_ADV_SET_RANDOM_ADDR_LEN 8
  32. #define BTM_BLE_MULTI_ADV_ENB_LEN 3
  33. #define BTM_BLE_MULTI_ADV_SET_PARAM_LEN 24
  34. #define BTM_BLE_AD_DATA_LEN 31
  35. #define BTM_BLE_MULTI_ADV_WRITE_DATA_LEN (BTM_BLE_AD_DATA_LEN + 3)
  36. #define HCIC_PARAM_SIZE_WRITE_ADV_ENABLE 1
  37. #define HCIC_PARAM_SIZE_WRITE_RANDOM_ADDR_CMD 6
  38. #define HCIC_PARAM_SIZE_BLE_WRITE_ADV_PARAMS 15
  39. #define HCIC_PARAM_SIZE_BLE_WRITE_SCAN_RSP 31
  40. #define HCIC_PARAM_SIZE_BLE_WRITE_ADV_DATA 31
  41. using status_cb = BleAdvertiserHciInterface::status_cb;
  42. using hci_cmd_cb = base::OnceCallback<void(
  43. uint8_t* /* return_parameters */, uint16_t /* return_parameters_length*/)>;
  44. extern void btu_hcif_send_cmd_with_cb(const base::Location& posted_from,
  45. uint16_t opcode, uint8_t* params,
  46. uint8_t params_len, hci_cmd_cb cb);
  47. namespace {
  48. BleAdvertiserHciInterface* instance = nullptr;
  49. void btm_ble_multi_adv_vsc_cmpl_cback(uint8_t expected_opcode,
  50. status_cb command_complete,
  51. uint8_t* param, uint16_t param_len) {
  52. uint8_t status, subcode;
  53. // All multi-adv commands respond with status and inst_id.
  54. LOG_ASSERT(param_len == 2) << "Received bad response length to multi-adv VSC";
  55. STREAM_TO_UINT8(status, param);
  56. STREAM_TO_UINT8(subcode, param);
  57. VLOG(1) << "subcode = " << +subcode << ", status: " << +status;
  58. if (expected_opcode != subcode) {
  59. LOG(ERROR) << "unexpected VSC cmpl, expect: " << +subcode
  60. << " get: " << +expected_opcode;
  61. return;
  62. }
  63. command_complete.Run(status);
  64. }
  65. void parameters_response_parser(BleAdvertiserHciInterface::parameters_cb cb,
  66. uint8_t* ret_params, uint16_t ret_params_len) {
  67. uint8_t status;
  68. int8_t tx_power;
  69. uint8_t* pp = ret_params;
  70. STREAM_TO_UINT8(status, pp);
  71. STREAM_TO_INT8(tx_power, pp);
  72. cb.Run(status, tx_power);
  73. }
  74. void known_tx_pwr(BleAdvertiserHciInterface::parameters_cb cb, int8_t tx_power,
  75. uint8_t status) {
  76. cb.Run(status, tx_power);
  77. }
  78. class BleAdvertiserVscHciInterfaceImpl : public BleAdvertiserHciInterface {
  79. void SendAdvCmd(const base::Location& posted_from, uint8_t param_len,
  80. uint8_t* param_buf, status_cb command_complete) {
  81. btu_hcif_send_cmd_with_cb(posted_from, HCI_BLE_MULTI_ADV_OCF, param_buf,
  82. param_len,
  83. base::Bind(&btm_ble_multi_adv_vsc_cmpl_cback,
  84. param_buf[0], command_complete));
  85. }
  86. void ReadInstanceCount(
  87. base::Callback<void(uint8_t /* inst_cnt*/)> cb) override {
  88. cb.Run(BTM_BleMaxMultiAdvInstanceCount());
  89. }
  90. void SetAdvertisingEventObserver(
  91. AdvertisingEventObserver* observer) override {
  92. this->advertising_event_observer = observer;
  93. }
  94. void SetParameters(uint8_t handle, uint16_t properties, uint32_t adv_int_min,
  95. uint32_t adv_int_max, uint8_t channel_map,
  96. uint8_t own_address_type, const RawAddress& own_address,
  97. uint8_t peer_address_type, const RawAddress& peer_address,
  98. uint8_t filter_policy, int8_t tx_power,
  99. uint8_t primary_phy, uint8_t secondary_max_skip,
  100. uint8_t secondary_phy, uint8_t advertising_sid,
  101. uint8_t scan_request_notify_enable,
  102. parameters_cb command_complete) override {
  103. VLOG(1) << __func__;
  104. uint8_t param[BTM_BLE_MULTI_ADV_SET_PARAM_LEN];
  105. memset(param, 0, BTM_BLE_MULTI_ADV_SET_PARAM_LEN);
  106. uint8_t* pp = param;
  107. UINT8_TO_STREAM(pp, BTM_BLE_MULTI_ADV_SET_PARAM);
  108. UINT16_TO_STREAM(pp, adv_int_min);
  109. UINT16_TO_STREAM(pp, adv_int_max);
  110. if (properties == 0x0013) {
  111. UINT8_TO_STREAM(pp, 0x00); // ADV_IND
  112. } else if (properties == 0x0012) {
  113. UINT8_TO_STREAM(pp, 0x02); // ADV_SCAN_IND
  114. } else if (properties == 0x0010) {
  115. UINT8_TO_STREAM(pp, 0x03); // ADV_NONCONN_IND
  116. } else {
  117. LOG(ERROR) << "Unsupported advertisement type selected:" << std::hex
  118. << properties;
  119. command_complete.Run(HCI_ERR_ILLEGAL_PARAMETER_FMT, 0);
  120. return;
  121. }
  122. UINT8_TO_STREAM(pp, own_address_type);
  123. BDADDR_TO_STREAM(pp, own_address);
  124. UINT8_TO_STREAM(pp, peer_address_type);
  125. BDADDR_TO_STREAM(pp, peer_address);
  126. UINT8_TO_STREAM(pp, channel_map);
  127. UINT8_TO_STREAM(pp, filter_policy);
  128. UINT8_TO_STREAM(pp, handle);
  129. INT8_TO_STREAM(pp, tx_power);
  130. SendAdvCmd(
  131. FROM_HERE, BTM_BLE_MULTI_ADV_SET_PARAM_LEN, param,
  132. base::Bind(&known_tx_pwr, std::move(command_complete), tx_power));
  133. }
  134. void SetAdvertisingData(uint8_t handle, uint8_t operation,
  135. uint8_t fragment_preference, uint8_t data_length,
  136. uint8_t* data, status_cb command_complete) override {
  137. VLOG(1) << __func__;
  138. uint8_t param[BTM_BLE_MULTI_ADV_WRITE_DATA_LEN];
  139. memset(param, 0, BTM_BLE_MULTI_ADV_WRITE_DATA_LEN);
  140. if (data_length > BTM_BLE_AD_DATA_LEN) {
  141. android_errorWriteLog(0x534e4554, "121145627");
  142. LOG(ERROR) << __func__
  143. << ": data_length=" << static_cast<int>(data_length)
  144. << ", is longer than size limit " << BTM_BLE_AD_DATA_LEN;
  145. data_length = BTM_BLE_AD_DATA_LEN;
  146. }
  147. uint8_t* pp = param;
  148. UINT8_TO_STREAM(pp, BTM_BLE_MULTI_ADV_WRITE_ADV_DATA);
  149. UINT8_TO_STREAM(pp, data_length);
  150. ARRAY_TO_STREAM(pp, data, data_length);
  151. param[BTM_BLE_MULTI_ADV_WRITE_DATA_LEN - 1] = handle;
  152. SendAdvCmd(FROM_HERE, (uint8_t)BTM_BLE_MULTI_ADV_WRITE_DATA_LEN, param,
  153. command_complete);
  154. }
  155. void SetScanResponseData(uint8_t handle, uint8_t operation,
  156. uint8_t fragment_preference,
  157. uint8_t scan_response_data_length,
  158. uint8_t* scan_response_data,
  159. status_cb command_complete) override {
  160. VLOG(1) << __func__;
  161. uint8_t param[BTM_BLE_MULTI_ADV_WRITE_DATA_LEN];
  162. memset(param, 0, BTM_BLE_MULTI_ADV_WRITE_DATA_LEN);
  163. if (scan_response_data_length > BTM_BLE_AD_DATA_LEN) {
  164. android_errorWriteLog(0x534e4554, "121145627");
  165. LOG(ERROR) << __func__ << ": scan_response_data_length="
  166. << static_cast<int>(scan_response_data_length)
  167. << ", is longer than size limit " << BTM_BLE_AD_DATA_LEN;
  168. scan_response_data_length = BTM_BLE_AD_DATA_LEN;
  169. }
  170. uint8_t* pp = param;
  171. UINT8_TO_STREAM(pp, BTM_BLE_MULTI_ADV_WRITE_SCAN_RSP_DATA);
  172. UINT8_TO_STREAM(pp, scan_response_data_length);
  173. ARRAY_TO_STREAM(pp, scan_response_data, scan_response_data_length);
  174. param[BTM_BLE_MULTI_ADV_WRITE_DATA_LEN - 1] = handle;
  175. SendAdvCmd(FROM_HERE, (uint8_t)BTM_BLE_MULTI_ADV_WRITE_DATA_LEN, param,
  176. command_complete);
  177. }
  178. void SetRandomAddress(uint8_t handle, const RawAddress& random_address,
  179. status_cb command_complete) override {
  180. VLOG(1) << __func__;
  181. uint8_t param[BTM_BLE_MULTI_ADV_SET_RANDOM_ADDR_LEN];
  182. memset(param, 0, BTM_BLE_MULTI_ADV_SET_RANDOM_ADDR_LEN);
  183. uint8_t* pp = param;
  184. UINT8_TO_STREAM(pp, BTM_BLE_MULTI_ADV_SET_RANDOM_ADDR);
  185. BDADDR_TO_STREAM(pp, random_address);
  186. UINT8_TO_STREAM(pp, handle);
  187. SendAdvCmd(FROM_HERE, (uint8_t)BTM_BLE_MULTI_ADV_SET_RANDOM_ADDR_LEN, param,
  188. command_complete);
  189. }
  190. void Enable(uint8_t enable, std::vector<SetEnableData> sets,
  191. status_cb command_complete) override {
  192. VLOG(1) << __func__;
  193. if (sets.size() != 1) {
  194. LOG(ERROR) << "Trying to enable multiple sets in VSC implemenetation!";
  195. command_complete.Run(HCI_ERR_ILLEGAL_PARAMETER_FMT);
  196. return;
  197. }
  198. SetEnableData& set = sets[0];
  199. if (set.max_extended_advertising_events) {
  200. command_complete.Run(HCI_ERR_ILLEGAL_PARAMETER_FMT);
  201. return;
  202. }
  203. uint8_t param[BTM_BLE_MULTI_ADV_ENB_LEN];
  204. memset(param, 0, BTM_BLE_MULTI_ADV_ENB_LEN);
  205. uint8_t* pp = param;
  206. UINT8_TO_STREAM(pp, BTM_BLE_MULTI_ADV_ENB);
  207. UINT8_TO_STREAM(pp, enable);
  208. UINT8_TO_STREAM(pp, set.handle);
  209. SendAdvCmd(FROM_HERE, (uint8_t)BTM_BLE_MULTI_ADV_ENB_LEN, param,
  210. command_complete);
  211. }
  212. void SetPeriodicAdvertisingParameters(uint8_t, uint16_t, uint16_t, uint16_t,
  213. status_cb command_complete) override {
  214. LOG(INFO) << __func__ << " VSC can't do periodic advertising";
  215. command_complete.Run(HCI_ERR_ILLEGAL_COMMAND);
  216. }
  217. void SetPeriodicAdvertisingData(uint8_t, uint8_t, uint8_t, uint8_t*,
  218. status_cb command_complete) override {
  219. LOG(INFO) << __func__ << " VSC can't do periodic advertising";
  220. command_complete.Run(HCI_ERR_ILLEGAL_COMMAND);
  221. }
  222. void SetPeriodicAdvertisingEnable(uint8_t, uint8_t,
  223. status_cb command_complete) override {
  224. LOG(INFO) << __func__ << " VSC can't do periodic advertising";
  225. command_complete.Run(HCI_ERR_ILLEGAL_COMMAND);
  226. }
  227. bool QuirkAdvertiserZeroHandle() override {
  228. // Android BT HCI Requirements version 0.96 and below specify that handle 0
  229. // is equal to standard HCI interface, and should be accessed using non-VSC
  230. // commands.
  231. LOG(INFO) << "QuirkAdvertiserZeroHandle in use";
  232. return true;
  233. }
  234. void RemoveAdvertisingSet(uint8_t handle,
  235. status_cb command_complete) override {
  236. // VSC Advertising don't have remove method.
  237. command_complete.Run(0);
  238. }
  239. public:
  240. static void VendorSpecificEventCback(uint8_t length, uint8_t* p) {
  241. VLOG(1) << __func__;
  242. LOG_ASSERT(p);
  243. uint8_t sub_event, adv_inst, change_reason;
  244. uint16_t conn_handle;
  245. STREAM_TO_UINT8(sub_event, p);
  246. length--;
  247. if (sub_event != HCI_VSE_SUBCODE_BLE_MULTI_ADV_ST_CHG || length != 4) {
  248. return;
  249. }
  250. STREAM_TO_UINT8(adv_inst, p);
  251. STREAM_TO_UINT8(change_reason, p);
  252. STREAM_TO_UINT16(conn_handle, p);
  253. AdvertisingEventObserver* observer =
  254. ((BleAdvertiserVscHciInterfaceImpl*)BleAdvertiserHciInterface::Get())
  255. ->advertising_event_observer;
  256. if (observer)
  257. observer->OnAdvertisingSetTerminated(change_reason, adv_inst, conn_handle,
  258. 0x00);
  259. }
  260. private:
  261. AdvertisingEventObserver* advertising_event_observer = nullptr;
  262. };
  263. void adv_cmd_cmpl_cback(status_cb cb, uint8_t* return_parameters,
  264. uint16_t return_parameters_length) {
  265. uint8_t status = *return_parameters;
  266. cb.Run(status);
  267. }
  268. class BleAdvertiserLegacyHciInterfaceImpl : public BleAdvertiserHciInterface {
  269. void SendAdvCmd(const base::Location& posted_from, uint16_t opcode,
  270. uint8_t* param_buf, uint8_t param_buf_len,
  271. status_cb command_complete) {
  272. btu_hcif_send_cmd_with_cb(
  273. posted_from, opcode, param_buf, param_buf_len,
  274. base::Bind(&adv_cmd_cmpl_cback, command_complete));
  275. }
  276. void ReadInstanceCount(
  277. base::Callback<void(uint8_t /* inst_cnt*/)> cb) override {
  278. cb.Run(1);
  279. }
  280. void SetAdvertisingEventObserver(
  281. AdvertisingEventObserver* observer) override {
  282. this->advertising_event_observer = observer;
  283. }
  284. void SetParameters(uint8_t handle, uint16_t properties, uint32_t adv_int_min,
  285. uint32_t adv_int_max, uint8_t channel_map,
  286. uint8_t own_address_type,
  287. const RawAddress& /* own_address */,
  288. uint8_t peer_address_type, const RawAddress& peer_address,
  289. uint8_t filter_policy, int8_t tx_power,
  290. uint8_t primary_phy, uint8_t secondary_max_skip,
  291. uint8_t secondary_phy, uint8_t advertising_sid,
  292. uint8_t scan_request_notify_enable,
  293. parameters_cb command_complete) override {
  294. VLOG(1) << __func__;
  295. uint8_t param[HCIC_PARAM_SIZE_BLE_WRITE_ADV_PARAMS];
  296. uint8_t* pp = param;
  297. UINT16_TO_STREAM(pp, adv_int_min);
  298. UINT16_TO_STREAM(pp, adv_int_max);
  299. if (properties == 0x0013) {
  300. UINT8_TO_STREAM(pp, 0x00); // ADV_IND
  301. } else if (properties == 0x0012) {
  302. UINT8_TO_STREAM(pp, 0x02); // ADV_SCAN_IND
  303. } else if (properties == 0x0010) {
  304. UINT8_TO_STREAM(pp, 0x03); // ADV_NONCONN_IND
  305. } else {
  306. LOG(ERROR) << "Unsupported advertisement type selected:" << std::hex
  307. << properties;
  308. command_complete.Run(HCI_ERR_ILLEGAL_PARAMETER_FMT, 0);
  309. return;
  310. }
  311. UINT8_TO_STREAM(pp, own_address_type);
  312. UINT8_TO_STREAM(pp, peer_address_type);
  313. BDADDR_TO_STREAM(pp, peer_address);
  314. UINT8_TO_STREAM(pp, channel_map);
  315. UINT8_TO_STREAM(pp, filter_policy);
  316. SendAdvCmd(
  317. FROM_HERE, HCI_BLE_WRITE_ADV_PARAMS, param,
  318. HCIC_PARAM_SIZE_BLE_WRITE_ADV_PARAMS,
  319. base::Bind(&known_tx_pwr, std::move(command_complete), (int8_t)0));
  320. }
  321. void SetAdvertisingData(uint8_t handle, uint8_t operation,
  322. uint8_t fragment_preference, uint8_t data_length,
  323. uint8_t* data, status_cb command_complete) override {
  324. VLOG(1) << __func__;
  325. uint8_t param[HCIC_PARAM_SIZE_BLE_WRITE_ADV_DATA + 1];
  326. if (data_length > HCIC_PARAM_SIZE_BLE_WRITE_ADV_DATA) {
  327. android_errorWriteLog(0x534e4554, "121145627");
  328. LOG(ERROR) << __func__
  329. << ": data_length=" << static_cast<int>(data_length)
  330. << ", is longer than size limit "
  331. << HCIC_PARAM_SIZE_BLE_WRITE_ADV_DATA;
  332. data_length = HCIC_PARAM_SIZE_BLE_WRITE_ADV_DATA;
  333. }
  334. uint8_t* pp = param;
  335. memset(pp, 0, HCIC_PARAM_SIZE_BLE_WRITE_ADV_DATA + 1);
  336. UINT8_TO_STREAM(pp, data_length);
  337. ARRAY_TO_STREAM(pp, data, data_length);
  338. SendAdvCmd(FROM_HERE, HCI_BLE_WRITE_ADV_DATA, param,
  339. HCIC_PARAM_SIZE_BLE_WRITE_ADV_DATA + 1, command_complete);
  340. }
  341. void SetScanResponseData(uint8_t handle, uint8_t operation,
  342. uint8_t fragment_preference,
  343. uint8_t scan_response_data_length,
  344. uint8_t* scan_response_data,
  345. status_cb command_complete) override {
  346. VLOG(1) << __func__;
  347. uint8_t param[HCIC_PARAM_SIZE_BLE_WRITE_ADV_DATA + 1];
  348. if (scan_response_data_length > HCIC_PARAM_SIZE_BLE_WRITE_ADV_DATA) {
  349. android_errorWriteLog(0x534e4554, "121145627");
  350. LOG(ERROR) << __func__ << ": scan_response_data_length="
  351. << static_cast<int>(scan_response_data_length)
  352. << ", is longer than size limit "
  353. << HCIC_PARAM_SIZE_BLE_WRITE_ADV_DATA;
  354. scan_response_data_length = HCIC_PARAM_SIZE_BLE_WRITE_ADV_DATA;
  355. }
  356. uint8_t* pp = param;
  357. memset(pp, 0, HCIC_PARAM_SIZE_BLE_WRITE_ADV_DATA + 1);
  358. UINT8_TO_STREAM(pp, scan_response_data_length);
  359. ARRAY_TO_STREAM(pp, scan_response_data, scan_response_data_length);
  360. SendAdvCmd(FROM_HERE, HCI_BLE_WRITE_SCAN_RSP_DATA, param,
  361. HCIC_PARAM_SIZE_BLE_WRITE_ADV_DATA + 1, command_complete);
  362. }
  363. void SetRandomAddress(uint8_t handle, const RawAddress& random_address,
  364. status_cb command_complete) override {
  365. VLOG(1) << __func__;
  366. uint8_t param[HCIC_PARAM_SIZE_WRITE_RANDOM_ADDR_CMD];
  367. uint8_t* pp = param;
  368. BDADDR_TO_STREAM(pp, random_address);
  369. SendAdvCmd(FROM_HERE, HCI_BLE_WRITE_RANDOM_ADDR, param,
  370. HCIC_PARAM_SIZE_WRITE_RANDOM_ADDR_CMD, command_complete);
  371. }
  372. void Enable(uint8_t enable, std::vector<SetEnableData> sets,
  373. status_cb command_complete) override {
  374. VLOG(1) << __func__;
  375. if (sets.size() != 1) {
  376. LOG(ERROR) << "Trying to enable multiple sets in legacy implemenetation!";
  377. command_complete.Run(HCI_ERR_ILLEGAL_PARAMETER_FMT);
  378. return;
  379. }
  380. SetEnableData& set = sets[0];
  381. if (set.max_extended_advertising_events) {
  382. command_complete.Run(HCI_ERR_ILLEGAL_PARAMETER_FMT);
  383. return;
  384. }
  385. uint8_t param[HCIC_PARAM_SIZE_WRITE_ADV_ENABLE];
  386. uint8_t* pp = param;
  387. UINT8_TO_STREAM(pp, enable);
  388. SendAdvCmd(FROM_HERE, HCI_BLE_WRITE_ADV_ENABLE, param,
  389. HCIC_PARAM_SIZE_WRITE_ADV_ENABLE, command_complete);
  390. }
  391. void SetPeriodicAdvertisingParameters(uint8_t, uint16_t, uint16_t, uint16_t,
  392. status_cb command_complete) override {
  393. LOG(INFO) << __func__ << "Legacy can't do periodic advertising";
  394. command_complete.Run(HCI_ERR_ILLEGAL_COMMAND);
  395. }
  396. void SetPeriodicAdvertisingData(uint8_t, uint8_t, uint8_t, uint8_t*,
  397. status_cb command_complete) override {
  398. LOG(INFO) << __func__ << "Legacy can't do periodic advertising";
  399. command_complete.Run(HCI_ERR_ILLEGAL_COMMAND);
  400. }
  401. void SetPeriodicAdvertisingEnable(uint8_t, uint8_t,
  402. status_cb command_complete) override {
  403. LOG(INFO) << __func__ << "Legacy can't do periodic advertising";
  404. command_complete.Run(HCI_ERR_ILLEGAL_COMMAND);
  405. }
  406. void RemoveAdvertisingSet(uint8_t handle,
  407. status_cb command_complete) override {
  408. // Legacy Advertising don't have remove method.
  409. command_complete.Run(0);
  410. }
  411. public:
  412. void OnAdvertisingSetTerminated(uint8_t status, uint16_t connection_handle) {
  413. VLOG(1) << __func__;
  414. AdvertisingEventObserver* observer = this->advertising_event_observer;
  415. if (observer)
  416. observer->OnAdvertisingSetTerminated(status, 0 /*advertising_handle*/,
  417. connection_handle, 0);
  418. }
  419. private:
  420. AdvertisingEventObserver* advertising_event_observer = nullptr;
  421. };
  422. class BleAdvertiserHciExtendedImpl : public BleAdvertiserHciInterface {
  423. void SendAdvCmd(const base::Location& posted_from, uint16_t opcode,
  424. uint8_t* param_buf, uint8_t param_buf_len,
  425. status_cb command_complete) {
  426. btu_hcif_send_cmd_with_cb(
  427. posted_from, opcode, param_buf, param_buf_len,
  428. base::Bind(&adv_cmd_cmpl_cback, command_complete));
  429. }
  430. void ReadInstanceCount(
  431. base::Callback<void(uint8_t /* inst_cnt*/)> cb) override {
  432. cb.Run(controller_get_interface()
  433. ->get_ble_number_of_supported_advertising_sets());
  434. }
  435. void SetAdvertisingEventObserver(
  436. AdvertisingEventObserver* observer) override {
  437. this->advertising_event_observer = observer;
  438. }
  439. void SetParameters(uint8_t handle, uint16_t properties, uint32_t adv_int_min,
  440. uint32_t adv_int_max, uint8_t channel_map,
  441. uint8_t own_address_type,
  442. const RawAddress& /* own_address */,
  443. uint8_t peer_address_type, const RawAddress& peer_address,
  444. uint8_t filter_policy, int8_t tx_power,
  445. uint8_t primary_phy, uint8_t secondary_max_skip,
  446. uint8_t secondary_phy, uint8_t advertising_sid,
  447. uint8_t scan_request_notify_enable,
  448. parameters_cb command_complete) override {
  449. VLOG(1) << __func__;
  450. const uint16_t HCI_LE_SET_EXT_ADVERTISING_PARAM_LEN = 25;
  451. uint8_t param[HCI_LE_SET_EXT_ADVERTISING_PARAM_LEN];
  452. memset(param, 0, HCI_LE_SET_EXT_ADVERTISING_PARAM_LEN);
  453. uint8_t* pp = param;
  454. UINT8_TO_STREAM(pp, handle);
  455. UINT16_TO_STREAM(pp, properties);
  456. UINT24_TO_STREAM(pp, adv_int_min);
  457. UINT24_TO_STREAM(pp, adv_int_max);
  458. UINT8_TO_STREAM(pp, channel_map);
  459. UINT8_TO_STREAM(pp, own_address_type);
  460. UINT8_TO_STREAM(pp, peer_address_type);
  461. BDADDR_TO_STREAM(pp, peer_address);
  462. UINT8_TO_STREAM(pp, filter_policy);
  463. INT8_TO_STREAM(pp, tx_power);
  464. UINT8_TO_STREAM(pp, primary_phy);
  465. UINT8_TO_STREAM(pp, secondary_max_skip);
  466. UINT8_TO_STREAM(pp, secondary_phy);
  467. UINT8_TO_STREAM(pp, advertising_sid);
  468. UINT8_TO_STREAM(pp, scan_request_notify_enable);
  469. btu_hcif_send_cmd_with_cb(
  470. FROM_HERE, HCI_LE_SET_EXT_ADVERTISING_PARAM, param,
  471. HCI_LE_SET_EXT_ADVERTISING_PARAM_LEN,
  472. base::Bind(parameters_response_parser, std::move(command_complete)));
  473. }
  474. void SetAdvertisingData(uint8_t handle, uint8_t operation,
  475. uint8_t fragment_preference, uint8_t data_length,
  476. uint8_t* data, status_cb command_complete) override {
  477. VLOG(1) << __func__;
  478. const uint16_t cmd_length = 4 + data_length;
  479. uint8_t param[cmd_length];
  480. memset(param, 0, cmd_length);
  481. uint8_t* pp = param;
  482. UINT8_TO_STREAM(pp, handle);
  483. UINT8_TO_STREAM(pp, operation);
  484. UINT8_TO_STREAM(pp, fragment_preference);
  485. UINT8_TO_STREAM(pp, data_length);
  486. ARRAY_TO_STREAM(pp, data, data_length);
  487. SendAdvCmd(FROM_HERE, HCI_LE_SET_EXT_ADVERTISING_DATA, param, cmd_length,
  488. command_complete);
  489. }
  490. void SetScanResponseData(uint8_t handle, uint8_t operation,
  491. uint8_t fragment_preference,
  492. uint8_t scan_response_data_length,
  493. uint8_t* scan_response_data,
  494. status_cb command_complete) override {
  495. VLOG(1) << __func__;
  496. const uint16_t cmd_length = 4 + scan_response_data_length;
  497. uint8_t param[cmd_length];
  498. memset(param, 0, cmd_length);
  499. uint8_t* pp = param;
  500. UINT8_TO_STREAM(pp, handle);
  501. UINT8_TO_STREAM(pp, operation);
  502. UINT8_TO_STREAM(pp, fragment_preference);
  503. UINT8_TO_STREAM(pp, scan_response_data_length);
  504. ARRAY_TO_STREAM(pp, scan_response_data, scan_response_data_length);
  505. SendAdvCmd(FROM_HERE, HCI_LE_SET_EXT_ADVERTISING_SCAN_RESP, param,
  506. cmd_length, command_complete);
  507. }
  508. void SetRandomAddress(uint8_t handle, const RawAddress& random_address,
  509. status_cb command_complete) override {
  510. VLOG(1) << __func__;
  511. const int LE_SET_ADVERTISING_SET_RANDOM_ADDRESS_LEN = 7;
  512. uint8_t param[LE_SET_ADVERTISING_SET_RANDOM_ADDRESS_LEN];
  513. memset(param, 0, LE_SET_ADVERTISING_SET_RANDOM_ADDRESS_LEN);
  514. uint8_t* pp = param;
  515. UINT8_TO_STREAM(pp, handle);
  516. BDADDR_TO_STREAM(pp, random_address);
  517. SendAdvCmd(FROM_HERE, HCI_LE_SET_EXT_ADVERTISING_RANDOM_ADDRESS, param,
  518. LE_SET_ADVERTISING_SET_RANDOM_ADDRESS_LEN, command_complete);
  519. }
  520. void Enable(uint8_t enable, std::vector<SetEnableData> sets,
  521. status_cb command_complete) override {
  522. VLOG(1) << __func__;
  523. /* cmd_length = header_size + num_of_of_advertiser * size_per_advertiser */
  524. const uint16_t cmd_length = 2 + sets.size() * 4;
  525. uint8_t param[cmd_length];
  526. memset(param, 0, cmd_length);
  527. uint8_t* pp = param;
  528. UINT8_TO_STREAM(pp, enable);
  529. UINT8_TO_STREAM(pp, sets.size());
  530. for (const SetEnableData& set : sets) {
  531. UINT8_TO_STREAM(pp, set.handle);
  532. UINT16_TO_STREAM(pp, set.duration);
  533. UINT8_TO_STREAM(pp, set.max_extended_advertising_events);
  534. }
  535. SendAdvCmd(FROM_HERE, HCI_LE_SET_EXT_ADVERTISING_ENABLE, param, cmd_length,
  536. command_complete);
  537. }
  538. void SetPeriodicAdvertisingParameters(uint8_t handle,
  539. uint16_t periodic_adv_int_min,
  540. uint16_t periodic_adv_int_max,
  541. uint16_t periodic_properties,
  542. status_cb command_complete) override {
  543. VLOG(1) << __func__;
  544. const uint16_t HCI_LE_SET_PRIODIC_ADVERTISING_PARAM_LEN = 7;
  545. uint8_t param[HCI_LE_SET_PRIODIC_ADVERTISING_PARAM_LEN];
  546. memset(param, 0, HCI_LE_SET_PRIODIC_ADVERTISING_PARAM_LEN);
  547. uint8_t* pp = param;
  548. UINT8_TO_STREAM(pp, handle);
  549. UINT16_TO_STREAM(pp, periodic_adv_int_min);
  550. UINT16_TO_STREAM(pp, periodic_adv_int_max);
  551. UINT16_TO_STREAM(pp, periodic_properties);
  552. SendAdvCmd(FROM_HERE, HCI_LE_SET_PERIODIC_ADVERTISING_PARAM, param,
  553. HCI_LE_SET_PRIODIC_ADVERTISING_PARAM_LEN, command_complete);
  554. }
  555. void SetPeriodicAdvertisingData(uint8_t handle, uint8_t operation,
  556. uint8_t adv_data_length, uint8_t* adv_data,
  557. status_cb command_complete) override {
  558. VLOG(1) << __func__;
  559. const uint16_t HCI_LE_SET_PRIODIC_ADVERTISING_DATA_LEN =
  560. 3 + adv_data_length;
  561. uint8_t param[HCI_LE_SET_PRIODIC_ADVERTISING_DATA_LEN];
  562. memset(param, 0, HCI_LE_SET_PRIODIC_ADVERTISING_DATA_LEN);
  563. uint8_t* pp = param;
  564. UINT8_TO_STREAM(pp, handle);
  565. UINT8_TO_STREAM(pp, operation);
  566. UINT8_TO_STREAM(pp, adv_data_length);
  567. ARRAY_TO_STREAM(pp, adv_data, adv_data_length);
  568. SendAdvCmd(FROM_HERE, HCI_LE_SET_PERIODIC_ADVERTISING_DATA, param,
  569. HCI_LE_SET_PRIODIC_ADVERTISING_DATA_LEN, command_complete);
  570. }
  571. void SetPeriodicAdvertisingEnable(uint8_t enable, uint8_t handle,
  572. status_cb command_complete) override {
  573. VLOG(1) << __func__;
  574. const uint16_t HCI_LE_ENABLE_PRIODIC_ADVERTISEMENT_LEN = 2;
  575. uint8_t param[HCI_LE_ENABLE_PRIODIC_ADVERTISEMENT_LEN];
  576. memset(param, 0, HCI_LE_ENABLE_PRIODIC_ADVERTISEMENT_LEN);
  577. uint8_t* pp = param;
  578. UINT8_TO_STREAM(pp, enable);
  579. UINT8_TO_STREAM(pp, handle);
  580. SendAdvCmd(FROM_HERE, HCI_LE_SET_PERIODIC_ADVERTISING_ENABLE, param,
  581. HCI_LE_ENABLE_PRIODIC_ADVERTISEMENT_LEN, command_complete);
  582. }
  583. void RemoveAdvertisingSet(uint8_t handle,
  584. status_cb command_complete) override {
  585. VLOG(1) << __func__;
  586. const uint16_t cmd_length = 1;
  587. uint8_t param[cmd_length];
  588. memset(param, 0, cmd_length);
  589. uint8_t* pp = param;
  590. UINT8_TO_STREAM(pp, handle);
  591. SendAdvCmd(FROM_HERE, HCI_LE_REMOVE_ADVERTISING_SET, param, cmd_length,
  592. command_complete);
  593. }
  594. public:
  595. void OnAdvertisingSetTerminated(uint8_t length, uint8_t* p) {
  596. VLOG(1) << __func__;
  597. LOG_ASSERT(p);
  598. uint8_t status, advertising_handle, num_completed_extended_adv_events;
  599. uint16_t conn_handle;
  600. STREAM_TO_UINT8(status, p);
  601. STREAM_TO_UINT8(advertising_handle, p);
  602. STREAM_TO_UINT16(conn_handle, p);
  603. STREAM_TO_UINT8(num_completed_extended_adv_events, p);
  604. conn_handle = conn_handle & 0x0FFF; // only 12 bits meaningful
  605. AdvertisingEventObserver* observer = this->advertising_event_observer;
  606. if (observer)
  607. observer->OnAdvertisingSetTerminated(status, advertising_handle,
  608. conn_handle,
  609. num_completed_extended_adv_events);
  610. }
  611. private:
  612. AdvertisingEventObserver* advertising_event_observer = nullptr;
  613. };
  614. } // namespace
  615. void btm_le_on_advertising_set_terminated(uint8_t* p, uint16_t length) {
  616. if (BleAdvertiserHciInterface::Get()) {
  617. ((BleAdvertiserHciExtendedImpl*)BleAdvertiserHciInterface::Get())
  618. ->OnAdvertisingSetTerminated(length, p);
  619. }
  620. }
  621. bool legacy_advertising_in_use = false;
  622. void btm_ble_advertiser_notify_terminated_legacy(uint8_t status,
  623. uint16_t connection_handle) {
  624. if (BleAdvertiserHciInterface::Get() && legacy_advertising_in_use) {
  625. ((BleAdvertiserLegacyHciInterfaceImpl*)BleAdvertiserHciInterface::Get())
  626. ->OnAdvertisingSetTerminated(status, connection_handle);
  627. }
  628. }
  629. void BleAdvertiserHciInterface::Initialize() {
  630. VLOG(1) << __func__;
  631. LOG_ASSERT(instance == nullptr) << "Was already initialized.";
  632. if (controller_get_interface()->supports_ble_extended_advertising()) {
  633. LOG(INFO) << "Extended advertising will be in use";
  634. instance = new BleAdvertiserHciExtendedImpl();
  635. } else if (BTM_BleMaxMultiAdvInstanceCount()) {
  636. LOG(INFO) << "VSC advertising will be in use";
  637. instance = new BleAdvertiserVscHciInterfaceImpl();
  638. BTM_RegisterForVSEvents(
  639. BleAdvertiserVscHciInterfaceImpl::VendorSpecificEventCback, true);
  640. } else {
  641. LOG(INFO) << "Legacy advertising will be in use";
  642. instance = new BleAdvertiserLegacyHciInterfaceImpl();
  643. legacy_advertising_in_use = true;
  644. }
  645. }
  646. BleAdvertiserHciInterface* BleAdvertiserHciInterface::Get() { return instance; }
  647. void BleAdvertiserHciInterface::CleanUp() {
  648. VLOG(1) << __func__;
  649. if (BTM_BleMaxMultiAdvInstanceCount()) {
  650. BTM_RegisterForVSEvents(
  651. BleAdvertiserVscHciInterfaceImpl::VendorSpecificEventCback, false);
  652. }
  653. delete instance;
  654. instance = nullptr;
  655. }