mesh-io-generic.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899
  1. // SPDX-License-Identifier: LGPL-2.1-or-later
  2. /*
  3. *
  4. * BlueZ - Bluetooth protocol stack for Linux
  5. *
  6. * Copyright (C) 2018-2019 Intel Corporation. All rights reserved.
  7. *
  8. *
  9. */
  10. #ifdef HAVE_CONFIG_H
  11. #include <config.h>
  12. #endif
  13. #include <errno.h>
  14. #include <string.h>
  15. #include <sys/time.h>
  16. #include <ell/ell.h>
  17. #include "monitor/bt.h"
  18. #include "src/shared/hci.h"
  19. #include "lib/bluetooth.h"
  20. #include "lib/mgmt.h"
  21. #include "mesh/mesh-defs.h"
  22. #include "mesh/mesh-mgmt.h"
  23. #include "mesh/mesh-io.h"
  24. #include "mesh/mesh-io-api.h"
  25. #include "mesh/mesh-io-generic.h"
  26. struct mesh_io_private {
  27. struct bt_hci *hci;
  28. void *user_data;
  29. mesh_io_ready_func_t ready_callback;
  30. struct l_timeout *tx_timeout;
  31. struct l_queue *rx_regs;
  32. struct l_queue *tx_pkts;
  33. struct tx_pkt *tx;
  34. uint16_t index;
  35. uint16_t interval;
  36. bool sending;
  37. bool active;
  38. };
  39. struct pvt_rx_reg {
  40. mesh_io_recv_func_t cb;
  41. void *user_data;
  42. uint8_t len;
  43. uint8_t filter[0];
  44. };
  45. struct process_data {
  46. struct mesh_io_private *pvt;
  47. const uint8_t *data;
  48. uint8_t len;
  49. struct mesh_io_recv_info info;
  50. };
  51. struct tx_pkt {
  52. struct mesh_io_send_info info;
  53. bool delete;
  54. uint8_t len;
  55. uint8_t pkt[30];
  56. };
  57. struct tx_pattern {
  58. const uint8_t *data;
  59. uint8_t len;
  60. };
  61. static uint32_t get_instant(void)
  62. {
  63. struct timeval tm;
  64. uint32_t instant;
  65. gettimeofday(&tm, NULL);
  66. instant = tm.tv_sec * 1000;
  67. instant += tm.tv_usec / 1000;
  68. return instant;
  69. }
  70. static uint32_t instant_remaining_ms(uint32_t instant)
  71. {
  72. instant -= get_instant();
  73. return instant;
  74. }
  75. static void process_rx_callbacks(void *v_reg, void *v_rx)
  76. {
  77. struct pvt_rx_reg *rx_reg = v_reg;
  78. struct process_data *rx = v_rx;
  79. if (!memcmp(rx->data, rx_reg->filter, rx_reg->len))
  80. rx_reg->cb(rx_reg->user_data, &rx->info, rx->data, rx->len);
  81. }
  82. static void process_rx(struct mesh_io_private *pvt, int8_t rssi,
  83. uint32_t instant, const uint8_t *addr,
  84. const uint8_t *data, uint8_t len)
  85. {
  86. struct process_data rx = {
  87. .pvt = pvt,
  88. .data = data,
  89. .len = len,
  90. .info.instant = instant,
  91. .info.addr = addr,
  92. .info.chan = 7,
  93. .info.rssi = rssi,
  94. };
  95. l_queue_foreach(pvt->rx_regs, process_rx_callbacks, &rx);
  96. }
  97. static void event_adv_report(struct mesh_io *io, const void *buf, uint8_t size)
  98. {
  99. const struct bt_hci_evt_le_adv_report *evt = buf;
  100. const uint8_t *adv;
  101. const uint8_t *addr;
  102. uint32_t instant;
  103. uint8_t adv_len;
  104. uint16_t len = 0;
  105. int8_t rssi;
  106. if (evt->event_type != 0x03)
  107. return;
  108. instant = get_instant();
  109. adv = evt->data;
  110. adv_len = evt->data_len;
  111. addr = evt->addr;
  112. /* rssi is just beyond last byte of data */
  113. rssi = (int8_t) adv[adv_len];
  114. while (len < adv_len - 1) {
  115. uint8_t field_len = adv[0];
  116. /* Check for the end of advertising data */
  117. if (field_len == 0)
  118. break;
  119. len += field_len + 1;
  120. /* Do not continue data parsing if got incorrect length */
  121. if (len > adv_len)
  122. break;
  123. /* TODO: Create an Instant to use */
  124. process_rx(io->pvt, rssi, instant, addr, adv + 1, adv[0]);
  125. adv += field_len + 1;
  126. }
  127. }
  128. static void event_callback(const void *buf, uint8_t size, void *user_data)
  129. {
  130. uint8_t event = l_get_u8(buf);
  131. struct mesh_io *io = user_data;
  132. switch (event) {
  133. case BT_HCI_EVT_LE_ADV_REPORT:
  134. event_adv_report(io, buf + 1, size - 1);
  135. break;
  136. default:
  137. l_debug("Other Meta Evt - %d", event);
  138. }
  139. }
  140. static void local_commands_callback(const void *data, uint8_t size,
  141. void *user_data)
  142. {
  143. const struct bt_hci_rsp_read_local_commands *rsp = data;
  144. if (rsp->status)
  145. l_error("Failed to read local commands");
  146. }
  147. static void local_features_callback(const void *data, uint8_t size,
  148. void *user_data)
  149. {
  150. const struct bt_hci_rsp_read_local_features *rsp = data;
  151. if (rsp->status)
  152. l_error("Failed to read local features");
  153. }
  154. static void hci_generic_callback(const void *data, uint8_t size,
  155. void *user_data)
  156. {
  157. uint8_t status = l_get_u8(data);
  158. if (status)
  159. l_error("Failed to initialize HCI");
  160. }
  161. static void configure_hci(struct mesh_io_private *io)
  162. {
  163. struct bt_hci_cmd_le_set_scan_parameters cmd;
  164. struct bt_hci_cmd_set_event_mask cmd_sem;
  165. struct bt_hci_cmd_le_set_event_mask cmd_slem;
  166. struct bt_hci_cmd_le_set_random_address cmd_raddr;
  167. /* Set scan parameters */
  168. cmd.type = 0x00; /* Passive Scanning. No scanning PDUs shall be sent */
  169. cmd.interval = 0x0030; /* Scan Interval = N * 0.625ms */
  170. cmd.window = 0x0030; /* Scan Window = N * 0.625ms */
  171. cmd.own_addr_type = 0x00; /* Public Device Address */
  172. /* Accept all advertising packets except directed advertising packets
  173. * not addressed to this device (default).
  174. */
  175. cmd.filter_policy = 0x00;
  176. /* Set event mask
  177. *
  178. * Mask: 0x2000800002008890
  179. * Disconnection Complete
  180. * Encryption Change
  181. * Read Remote Version Information Complete
  182. * Hardware Error
  183. * Data Buffer Overflow
  184. * Encryption Key Refresh Complete
  185. * LE Meta
  186. */
  187. cmd_sem.mask[0] = 0x90;
  188. cmd_sem.mask[1] = 0x88;
  189. cmd_sem.mask[2] = 0x00;
  190. cmd_sem.mask[3] = 0x02;
  191. cmd_sem.mask[4] = 0x00;
  192. cmd_sem.mask[5] = 0x80;
  193. cmd_sem.mask[6] = 0x00;
  194. cmd_sem.mask[7] = 0x20;
  195. /* Set LE event mask
  196. *
  197. * Mask: 0x000000000000087f
  198. * LE Connection Complete
  199. * LE Advertising Report
  200. * LE Connection Update Complete
  201. * LE Read Remote Used Features Complete
  202. * LE Long Term Key Request
  203. * LE Remote Connection Parameter Request
  204. * LE Data Length Change
  205. * LE PHY Update Complete
  206. */
  207. cmd_slem.mask[0] = 0x7f;
  208. cmd_slem.mask[1] = 0x08;
  209. cmd_slem.mask[2] = 0x00;
  210. cmd_slem.mask[3] = 0x00;
  211. cmd_slem.mask[4] = 0x00;
  212. cmd_slem.mask[5] = 0x00;
  213. cmd_slem.mask[6] = 0x00;
  214. cmd_slem.mask[7] = 0x00;
  215. /* Set LE random address */
  216. l_getrandom(cmd_raddr.addr, 6);
  217. cmd_raddr.addr[5] |= 0xc0;
  218. /* TODO: Move to suitable place. Set suitable masks */
  219. /* Reset Command */
  220. bt_hci_send(io->hci, BT_HCI_CMD_RESET, NULL, 0, hci_generic_callback,
  221. NULL, NULL);
  222. /* Read local supported commands */
  223. bt_hci_send(io->hci, BT_HCI_CMD_READ_LOCAL_COMMANDS, NULL, 0,
  224. local_commands_callback, NULL, NULL);
  225. /* Read local supported features */
  226. bt_hci_send(io->hci, BT_HCI_CMD_READ_LOCAL_FEATURES, NULL, 0,
  227. local_features_callback, NULL, NULL);
  228. /* Set event mask */
  229. bt_hci_send(io->hci, BT_HCI_CMD_SET_EVENT_MASK, &cmd_sem,
  230. sizeof(cmd_sem), hci_generic_callback, NULL, NULL);
  231. /* Set LE event mask */
  232. bt_hci_send(io->hci, BT_HCI_CMD_LE_SET_EVENT_MASK, &cmd_slem,
  233. sizeof(cmd_slem), hci_generic_callback, NULL, NULL);
  234. /* Set LE random address */
  235. bt_hci_send(io->hci, BT_HCI_CMD_LE_SET_RANDOM_ADDRESS, &cmd_raddr,
  236. sizeof(cmd_raddr), hci_generic_callback, NULL, NULL);
  237. /* Scan Params */
  238. bt_hci_send(io->hci, BT_HCI_CMD_LE_SET_SCAN_PARAMETERS, &cmd,
  239. sizeof(cmd), hci_generic_callback, NULL, NULL);
  240. }
  241. static void scan_enable_rsp(const void *buf, uint8_t size,
  242. void *user_data)
  243. {
  244. uint8_t status = *((uint8_t *) buf);
  245. if (status)
  246. l_error("LE Scan enable failed (0x%02x)", status);
  247. }
  248. static void set_recv_scan_enable(const void *buf, uint8_t size,
  249. void *user_data)
  250. {
  251. struct mesh_io_private *pvt = user_data;
  252. struct bt_hci_cmd_le_set_scan_enable cmd;
  253. cmd.enable = 0x01; /* Enable scanning */
  254. cmd.filter_dup = 0x00; /* Report duplicates */
  255. bt_hci_send(pvt->hci, BT_HCI_CMD_LE_SET_SCAN_ENABLE,
  256. &cmd, sizeof(cmd), scan_enable_rsp, pvt, NULL);
  257. }
  258. static void scan_disable_rsp(const void *buf, uint8_t size,
  259. void *user_data)
  260. {
  261. struct bt_hci_cmd_le_set_scan_parameters cmd;
  262. struct mesh_io_private *pvt = user_data;
  263. uint8_t status = *((uint8_t *) buf);
  264. if (status)
  265. l_error("LE Scan disable failed (0x%02x)", status);
  266. cmd.type = pvt->active ? 0x01 : 0x00; /* Passive/Active scanning */
  267. cmd.interval = L_CPU_TO_LE16(0x0010); /* 10 ms */
  268. cmd.window = L_CPU_TO_LE16(0x0010); /* 10 ms */
  269. cmd.own_addr_type = 0x01; /* ADDR_TYPE_RANDOM */
  270. cmd.filter_policy = 0x00; /* Accept all */
  271. bt_hci_send(pvt->hci, BT_HCI_CMD_LE_SET_SCAN_PARAMETERS,
  272. &cmd, sizeof(cmd),
  273. set_recv_scan_enable, pvt, NULL);
  274. }
  275. static bool simple_match(const void *a, const void *b)
  276. {
  277. return a == b;
  278. }
  279. static bool find_by_ad_type(const void *a, const void *b)
  280. {
  281. const struct tx_pkt *tx = a;
  282. uint8_t ad_type = L_PTR_TO_UINT(b);
  283. return !ad_type || ad_type == tx->pkt[0];
  284. }
  285. static bool find_by_pattern(const void *a, const void *b)
  286. {
  287. const struct tx_pkt *tx = a;
  288. const struct tx_pattern *pattern = b;
  289. if (tx->len < pattern->len)
  290. return false;
  291. return (!memcmp(tx->pkt, pattern->data, pattern->len));
  292. }
  293. static bool find_active(const void *a, const void *b)
  294. {
  295. const struct pvt_rx_reg *rx_reg = a;
  296. /* Mesh specific AD types do *not* require active scanning,
  297. * so do not turn on Active Scanning on their account.
  298. */
  299. if (rx_reg->filter[0] < MESH_AD_TYPE_PROVISION ||
  300. rx_reg->filter[0] > MESH_AD_TYPE_BEACON)
  301. return true;
  302. return false;
  303. }
  304. static void restart_scan(struct mesh_io_private *pvt)
  305. {
  306. struct bt_hci_cmd_le_set_scan_enable cmd;
  307. if (l_queue_isempty(pvt->rx_regs))
  308. return;
  309. pvt->active = l_queue_find(pvt->rx_regs, find_active, NULL);
  310. cmd.enable = 0x00; /* Disable scanning */
  311. cmd.filter_dup = 0x00; /* Report duplicates */
  312. bt_hci_send(pvt->hci, BT_HCI_CMD_LE_SET_SCAN_ENABLE,
  313. &cmd, sizeof(cmd), scan_disable_rsp, pvt, NULL);
  314. }
  315. static void hci_init(void *user_data)
  316. {
  317. struct mesh_io *io = user_data;
  318. bool result = true;
  319. bool restarted = false;
  320. if (io->pvt->hci) {
  321. restarted = true;
  322. bt_hci_unref(io->pvt->hci);
  323. }
  324. io->pvt->hci = bt_hci_new_user_channel(io->pvt->index);
  325. if (!io->pvt->hci) {
  326. l_error("Failed to start mesh io (hci %u): %s", io->pvt->index,
  327. strerror(errno));
  328. result = false;
  329. }
  330. if (result) {
  331. configure_hci(io->pvt);
  332. bt_hci_register(io->pvt->hci, BT_HCI_EVT_LE_META_EVENT,
  333. event_callback, io, NULL);
  334. l_debug("Started mesh on hci %u", io->pvt->index);
  335. if (restarted)
  336. restart_scan(io->pvt);
  337. }
  338. if (io->pvt->ready_callback)
  339. io->pvt->ready_callback(io->pvt->user_data, result);
  340. }
  341. static void read_info(int index, void *user_data)
  342. {
  343. struct mesh_io *io = user_data;
  344. if (io->pvt->index != MGMT_INDEX_NONE &&
  345. index != io->pvt->index) {
  346. l_debug("Ignore index %d", index);
  347. return;
  348. }
  349. io->pvt->index = index;
  350. hci_init(io);
  351. }
  352. static bool dev_init(struct mesh_io *io, void *opts,
  353. mesh_io_ready_func_t cb, void *user_data)
  354. {
  355. if (!io || io->pvt)
  356. return false;
  357. io->pvt = l_new(struct mesh_io_private, 1);
  358. io->pvt->index = *(int *)opts;
  359. io->pvt->rx_regs = l_queue_new();
  360. io->pvt->tx_pkts = l_queue_new();
  361. io->pvt->ready_callback = cb;
  362. io->pvt->user_data = user_data;
  363. if (io->pvt->index == MGMT_INDEX_NONE)
  364. return mesh_mgmt_list(read_info, io);
  365. l_idle_oneshot(hci_init, io, NULL);
  366. return true;
  367. }
  368. static bool dev_destroy(struct mesh_io *io)
  369. {
  370. struct mesh_io_private *pvt = io->pvt;
  371. if (!pvt)
  372. return true;
  373. bt_hci_unref(pvt->hci);
  374. l_timeout_remove(pvt->tx_timeout);
  375. l_queue_destroy(pvt->rx_regs, l_free);
  376. l_queue_remove_if(pvt->tx_pkts, simple_match, pvt->tx);
  377. l_queue_destroy(pvt->tx_pkts, l_free);
  378. l_free(pvt->tx);
  379. l_free(pvt);
  380. io->pvt = NULL;
  381. return true;
  382. }
  383. static bool dev_caps(struct mesh_io *io, struct mesh_io_caps *caps)
  384. {
  385. struct mesh_io_private *pvt = io->pvt;
  386. if (!pvt || !caps)
  387. return false;
  388. caps->max_num_filters = 255;
  389. caps->window_accuracy = 50;
  390. return true;
  391. }
  392. static void send_cancel_done(const void *buf, uint8_t size,
  393. void *user_data)
  394. {
  395. struct mesh_io_private *pvt = user_data;
  396. struct bt_hci_cmd_le_set_random_address cmd;
  397. if (!pvt)
  398. return;
  399. pvt->sending = false;
  400. /* At end of any burst of ADVs, change random address */
  401. l_getrandom(cmd.addr, 6);
  402. cmd.addr[5] |= 0xc0;
  403. bt_hci_send(pvt->hci, BT_HCI_CMD_LE_SET_RANDOM_ADDRESS,
  404. &cmd, sizeof(cmd), NULL, NULL, NULL);
  405. }
  406. static void send_cancel(struct mesh_io_private *pvt)
  407. {
  408. struct bt_hci_cmd_le_set_adv_enable cmd;
  409. if (!pvt)
  410. return;
  411. if (!pvt->sending) {
  412. send_cancel_done(NULL, 0, pvt);
  413. return;
  414. }
  415. cmd.enable = 0x00; /* Disable advertising */
  416. bt_hci_send(pvt->hci, BT_HCI_CMD_LE_SET_ADV_ENABLE,
  417. &cmd, sizeof(cmd),
  418. send_cancel_done, pvt, NULL);
  419. }
  420. static void set_send_adv_enable(const void *buf, uint8_t size,
  421. void *user_data)
  422. {
  423. struct mesh_io_private *pvt = user_data;
  424. struct bt_hci_cmd_le_set_adv_enable cmd;
  425. if (!pvt)
  426. return;
  427. pvt->sending = true;
  428. cmd.enable = 0x01; /* Enable advertising */
  429. bt_hci_send(pvt->hci, BT_HCI_CMD_LE_SET_ADV_ENABLE,
  430. &cmd, sizeof(cmd), NULL, NULL, NULL);
  431. }
  432. static void set_send_adv_data(const void *buf, uint8_t size,
  433. void *user_data)
  434. {
  435. struct mesh_io_private *pvt = user_data;
  436. struct tx_pkt *tx;
  437. struct bt_hci_cmd_le_set_adv_data cmd;
  438. if (!pvt || !pvt->tx)
  439. return;
  440. tx = pvt->tx;
  441. if (tx->len >= sizeof(cmd.data))
  442. goto done;
  443. memset(&cmd, 0, sizeof(cmd));
  444. cmd.len = tx->len + 1;
  445. cmd.data[0] = tx->len;
  446. memcpy(cmd.data + 1, tx->pkt, tx->len);
  447. bt_hci_send(pvt->hci, BT_HCI_CMD_LE_SET_ADV_DATA,
  448. &cmd, sizeof(cmd),
  449. set_send_adv_enable, pvt, NULL);
  450. done:
  451. if (tx->delete) {
  452. l_queue_remove_if(pvt->tx_pkts, simple_match, tx);
  453. l_free(tx);
  454. }
  455. pvt->tx = NULL;
  456. }
  457. static void set_send_adv_params(const void *buf, uint8_t size,
  458. void *user_data)
  459. {
  460. struct mesh_io_private *pvt = user_data;
  461. struct bt_hci_cmd_le_set_adv_parameters cmd;
  462. uint16_t hci_interval;
  463. if (!pvt)
  464. return;
  465. hci_interval = (pvt->interval * 16) / 10;
  466. cmd.min_interval = L_CPU_TO_LE16(hci_interval);
  467. cmd.max_interval = L_CPU_TO_LE16(hci_interval);
  468. cmd.type = 0x03; /* ADV_NONCONN_IND */
  469. cmd.own_addr_type = 0x01; /* ADDR_TYPE_RANDOM */
  470. cmd.direct_addr_type = 0x00;
  471. memset(cmd.direct_addr, 0, 6);
  472. cmd.channel_map = 0x07;
  473. cmd.filter_policy = 0x03;
  474. bt_hci_send(pvt->hci, BT_HCI_CMD_LE_SET_ADV_PARAMETERS,
  475. &cmd, sizeof(cmd),
  476. set_send_adv_data, pvt, NULL);
  477. }
  478. static void send_pkt(struct mesh_io_private *pvt, struct tx_pkt *tx,
  479. uint16_t interval)
  480. {
  481. struct bt_hci_cmd_le_set_adv_enable cmd;
  482. /* Delete superseded packet in favor of new packet */
  483. if (pvt->tx && pvt->tx != tx && pvt->tx->delete) {
  484. l_queue_remove_if(pvt->tx_pkts, simple_match, pvt->tx);
  485. l_free(pvt->tx);
  486. }
  487. pvt->tx = tx;
  488. pvt->interval = interval;
  489. if (!pvt->sending) {
  490. set_send_adv_params(NULL, 0, pvt);
  491. return;
  492. }
  493. cmd.enable = 0x00; /* Disable advertising */
  494. bt_hci_send(pvt->hci, BT_HCI_CMD_LE_SET_ADV_ENABLE,
  495. &cmd, sizeof(cmd),
  496. set_send_adv_params, pvt, NULL);
  497. }
  498. static void tx_to(struct l_timeout *timeout, void *user_data)
  499. {
  500. struct mesh_io_private *pvt = user_data;
  501. struct tx_pkt *tx;
  502. uint16_t ms;
  503. uint8_t count;
  504. if (!pvt)
  505. return;
  506. tx = l_queue_pop_head(pvt->tx_pkts);
  507. if (!tx) {
  508. l_timeout_remove(timeout);
  509. pvt->tx_timeout = NULL;
  510. send_cancel(pvt);
  511. return;
  512. }
  513. if (tx->info.type == MESH_IO_TIMING_TYPE_GENERAL) {
  514. ms = tx->info.u.gen.interval;
  515. count = tx->info.u.gen.cnt;
  516. if (count != MESH_IO_TX_COUNT_UNLIMITED)
  517. tx->info.u.gen.cnt--;
  518. } else {
  519. ms = 25;
  520. count = 1;
  521. }
  522. tx->delete = !!(count == 1);
  523. send_pkt(pvt, tx, ms);
  524. if (count == 1) {
  525. /* Recalculate wakeup if we are responding to POLL */
  526. tx = l_queue_peek_head(pvt->tx_pkts);
  527. if (tx && tx->info.type == MESH_IO_TIMING_TYPE_POLL_RSP) {
  528. ms = instant_remaining_ms(tx->info.u.poll_rsp.instant +
  529. tx->info.u.poll_rsp.delay);
  530. }
  531. } else
  532. l_queue_push_tail(pvt->tx_pkts, tx);
  533. if (timeout) {
  534. pvt->tx_timeout = timeout;
  535. l_timeout_modify_ms(timeout, ms);
  536. } else
  537. pvt->tx_timeout = l_timeout_create_ms(ms, tx_to, pvt, NULL);
  538. }
  539. static void tx_worker(void *user_data)
  540. {
  541. struct mesh_io_private *pvt = user_data;
  542. struct tx_pkt *tx;
  543. uint32_t delay;
  544. tx = l_queue_peek_head(pvt->tx_pkts);
  545. if (!tx)
  546. return;
  547. switch (tx->info.type) {
  548. case MESH_IO_TIMING_TYPE_GENERAL:
  549. if (tx->info.u.gen.min_delay == tx->info.u.gen.max_delay)
  550. delay = tx->info.u.gen.min_delay;
  551. else {
  552. l_getrandom(&delay, sizeof(delay));
  553. delay %= tx->info.u.gen.max_delay -
  554. tx->info.u.gen.min_delay;
  555. delay += tx->info.u.gen.min_delay;
  556. }
  557. break;
  558. case MESH_IO_TIMING_TYPE_POLL:
  559. if (tx->info.u.poll.min_delay == tx->info.u.poll.max_delay)
  560. delay = tx->info.u.poll.min_delay;
  561. else {
  562. l_getrandom(&delay, sizeof(delay));
  563. delay %= tx->info.u.poll.max_delay -
  564. tx->info.u.poll.min_delay;
  565. delay += tx->info.u.poll.min_delay;
  566. }
  567. break;
  568. case MESH_IO_TIMING_TYPE_POLL_RSP:
  569. /* Delay until Instant + Delay */
  570. delay = instant_remaining_ms(tx->info.u.poll_rsp.instant +
  571. tx->info.u.poll_rsp.delay);
  572. if (delay > 255)
  573. delay = 0;
  574. break;
  575. default:
  576. return;
  577. }
  578. if (!delay)
  579. tx_to(pvt->tx_timeout, pvt);
  580. else if (pvt->tx_timeout)
  581. l_timeout_modify_ms(pvt->tx_timeout, delay);
  582. else
  583. pvt->tx_timeout = l_timeout_create_ms(delay, tx_to, pvt, NULL);
  584. }
  585. static bool send_tx(struct mesh_io *io, struct mesh_io_send_info *info,
  586. const uint8_t *data, uint16_t len)
  587. {
  588. struct mesh_io_private *pvt = io->pvt;
  589. struct tx_pkt *tx;
  590. bool sending = false;
  591. if (!info || !data || !len || len > sizeof(tx->pkt))
  592. return false;
  593. tx = l_new(struct tx_pkt, 1);
  594. memcpy(&tx->info, info, sizeof(tx->info));
  595. memcpy(&tx->pkt, data, len);
  596. tx->len = len;
  597. if (info->type == MESH_IO_TIMING_TYPE_POLL_RSP)
  598. l_queue_push_head(pvt->tx_pkts, tx);
  599. else {
  600. if (pvt->tx)
  601. sending = true;
  602. else
  603. sending = !l_queue_isempty(pvt->tx_pkts);
  604. l_queue_push_tail(pvt->tx_pkts, tx);
  605. /*
  606. * If transmitter is idle, send packets at least twice to
  607. * guard against in-line cancelation of HCI command chain.
  608. */
  609. if (info->type == MESH_IO_TIMING_TYPE_GENERAL && !sending &&
  610. tx->info.u.gen.cnt == 1)
  611. tx->info.u.gen.cnt++;
  612. }
  613. if (!sending) {
  614. l_timeout_remove(pvt->tx_timeout);
  615. pvt->tx_timeout = NULL;
  616. l_idle_oneshot(tx_worker, pvt, NULL);
  617. }
  618. return true;
  619. }
  620. static bool tx_cancel(struct mesh_io *io, const uint8_t *data, uint8_t len)
  621. {
  622. struct mesh_io_private *pvt = io->pvt;
  623. struct tx_pkt *tx;
  624. if (!data)
  625. return false;
  626. if (len == 1) {
  627. do {
  628. tx = l_queue_remove_if(pvt->tx_pkts, find_by_ad_type,
  629. L_UINT_TO_PTR(data[0]));
  630. l_free(tx);
  631. if (tx == pvt->tx)
  632. pvt->tx = NULL;
  633. } while (tx);
  634. } else {
  635. struct tx_pattern pattern = {
  636. .data = data,
  637. .len = len
  638. };
  639. do {
  640. tx = l_queue_remove_if(pvt->tx_pkts, find_by_pattern,
  641. &pattern);
  642. l_free(tx);
  643. if (tx == pvt->tx)
  644. pvt->tx = NULL;
  645. } while (tx);
  646. }
  647. if (l_queue_isempty(pvt->tx_pkts)) {
  648. send_cancel(pvt);
  649. l_timeout_remove(pvt->tx_timeout);
  650. pvt->tx_timeout = NULL;
  651. }
  652. return true;
  653. }
  654. static bool find_by_filter(const void *a, const void *b)
  655. {
  656. const struct pvt_rx_reg *rx_reg = a;
  657. const uint8_t *filter = b;
  658. return !memcmp(rx_reg->filter, filter, rx_reg->len);
  659. }
  660. static bool recv_register(struct mesh_io *io, const uint8_t *filter,
  661. uint8_t len, mesh_io_recv_func_t cb, void *user_data)
  662. {
  663. struct bt_hci_cmd_le_set_scan_enable cmd;
  664. struct mesh_io_private *pvt = io->pvt;
  665. struct pvt_rx_reg *rx_reg;
  666. bool already_scanning;
  667. bool active = false;
  668. if (!cb || !filter || !len)
  669. return false;
  670. rx_reg = l_queue_remove_if(pvt->rx_regs, find_by_filter, filter);
  671. l_free(rx_reg);
  672. rx_reg = l_malloc(sizeof(*rx_reg) + len);
  673. memcpy(rx_reg->filter, filter, len);
  674. rx_reg->len = len;
  675. rx_reg->cb = cb;
  676. rx_reg->user_data = user_data;
  677. already_scanning = !l_queue_isempty(pvt->rx_regs);
  678. l_queue_push_head(pvt->rx_regs, rx_reg);
  679. /* Look for any AD types requiring Active Scanning */
  680. if (l_queue_find(pvt->rx_regs, find_active, NULL))
  681. active = true;
  682. if (!already_scanning || pvt->active != active) {
  683. pvt->active = active;
  684. cmd.enable = 0x00; /* Disable scanning */
  685. cmd.filter_dup = 0x00; /* Report duplicates */
  686. bt_hci_send(pvt->hci, BT_HCI_CMD_LE_SET_SCAN_ENABLE,
  687. &cmd, sizeof(cmd), scan_disable_rsp, pvt, NULL);
  688. }
  689. return true;
  690. }
  691. static bool recv_deregister(struct mesh_io *io, const uint8_t *filter,
  692. uint8_t len)
  693. {
  694. struct bt_hci_cmd_le_set_scan_enable cmd = {0, 0};
  695. struct mesh_io_private *pvt = io->pvt;
  696. struct pvt_rx_reg *rx_reg;
  697. bool active = false;
  698. rx_reg = l_queue_remove_if(pvt->rx_regs, find_by_filter, filter);
  699. if (rx_reg)
  700. l_free(rx_reg);
  701. /* Look for any AD types requiring Active Scanning */
  702. if (l_queue_find(pvt->rx_regs, find_active, NULL))
  703. active = true;
  704. if (l_queue_isempty(pvt->rx_regs)) {
  705. bt_hci_send(pvt->hci, BT_HCI_CMD_LE_SET_SCAN_ENABLE,
  706. &cmd, sizeof(cmd), NULL, NULL, NULL);
  707. } else if (active != pvt->active) {
  708. pvt->active = active;
  709. bt_hci_send(pvt->hci, BT_HCI_CMD_LE_SET_SCAN_ENABLE,
  710. &cmd, sizeof(cmd), scan_disable_rsp, pvt, NULL);
  711. }
  712. return true;
  713. }
  714. const struct mesh_io_api mesh_io_generic = {
  715. .init = dev_init,
  716. .destroy = dev_destroy,
  717. .caps = dev_caps,
  718. .send = send_tx,
  719. .reg = recv_register,
  720. .dereg = recv_deregister,
  721. .cancel = tx_cancel,
  722. };