S&B Volcano vaporizer remote control with Pi Pico W
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ble.c 27KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976
  1. /*
  2. * ble.c
  3. *
  4. * https://github.com/raspberrypi/pico-examples/blob/master/pico_w/bt/standalone/client.c
  5. * https://vanhunteradams.com/Pico/BLE/BTStack_HCI.html
  6. *
  7. * Copyright (c) 2023 Thomas Buck (thomas@xythobuz.de)
  8. *
  9. * This program is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation, either version 3 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * See <http://www.gnu.org/licenses/>.
  20. */
  21. #include "pico/cyw43_arch.h"
  22. #include "hardware/watchdog.h"
  23. #include "config.h"
  24. #include "log.h"
  25. #include "main.h"
  26. #include "util.h"
  27. #include "ble.h"
  28. #define BLE_READ_TIMEOUT_MS (3 * 500)
  29. #define BLE_SRVC_TIMEOUT_MS (3 * 500)
  30. #define BLE_CHAR_TIMEOUT_MS (3 * 2000)
  31. #define BLE_WRTE_TIMEOUT_MS (3 * 500)
  32. #define BLE_NOTY_TIMEOUT_MS (3 * 500)
  33. #define BLE_MAX_SCAN_AGE_MS (10 * 1000)
  34. #define BLE_MAX_SERVICES 8
  35. #define BLE_MAX_CHARACTERISTICS 8
  36. enum ble_state {
  37. TC_OFF = 0,
  38. TC_IDLE,
  39. TC_W4_SCAN,
  40. TC_W4_CONNECT,
  41. TC_READY,
  42. TC_W4_READ,
  43. TC_READ_COMPLETE,
  44. TC_W4_SERVICE,
  45. TC_W4_CHARACTERISTIC,
  46. TC_W4_WRITE,
  47. TC_WRITE_COMPLETE,
  48. TC_W4_NOTIFY_ENABLE,
  49. TC_NOTIFY_ENABLED,
  50. };
  51. struct ble_characteristic {
  52. bool set;
  53. gatt_client_characteristic_t c;
  54. gatt_client_notification_t n;
  55. };
  56. struct ble_service {
  57. bool set;
  58. gatt_client_service_t service;
  59. struct ble_characteristic chars[BLE_MAX_CHARACTERISTICS];
  60. };
  61. static btstack_packet_callback_registration_t hci_event_callback_registration;
  62. static hci_con_handle_t connection_handle;
  63. static enum ble_state state = TC_OFF;
  64. static struct ble_scan_result scans[BLE_MAX_SCAN_RESULTS] = {0};
  65. static uint16_t read_len = 0;
  66. static uint8_t data_buff[BLE_MAX_VALUE_LEN] = {0};
  67. static struct ble_service services[BLE_MAX_SERVICES] = {0};
  68. static uint8_t service_idx = 0;
  69. static uint8_t characteristic_idx = 0;
  70. static void hci_add_scan_result(bd_addr_t addr, bd_addr_type_t type, int8_t rssi) {
  71. int unused = -1;
  72. for (uint i = 0; i < BLE_MAX_SCAN_RESULTS; i++) {
  73. if (!scans[i].set) {
  74. if (unused < 0) {
  75. unused = i;
  76. }
  77. continue;
  78. }
  79. if (memcmp(addr, scans[i].addr, sizeof(bd_addr_t)) == 0) {
  80. // already in list, just update changing values
  81. scans[i].time = to_ms_since_boot(get_absolute_time());
  82. scans[i].rssi = rssi;
  83. return;
  84. }
  85. }
  86. if (unused < 0) {
  87. debug("no space in scan results for %s", bd_addr_to_str(addr));
  88. return;
  89. }
  90. debug("new device with addr %s", bd_addr_to_str(addr));
  91. scans[unused].set = true;
  92. scans[unused].time = to_ms_since_boot(get_absolute_time());
  93. memcpy(scans[unused].addr, addr, sizeof(bd_addr_t));
  94. scans[unused].type = type;
  95. scans[unused].rssi = rssi;
  96. scans[unused].name[0] = '\0';
  97. scans[unused].data_len = 0;
  98. }
  99. static void hci_scan_result_add_name(bd_addr_t addr, const uint8_t *data, uint8_t data_size) {
  100. for (uint i = 0; i < BLE_MAX_SCAN_RESULTS; i++) {
  101. if (!scans[i].set) {
  102. continue;
  103. }
  104. if (memcmp(addr, scans[i].addr, sizeof(bd_addr_t)) != 0) {
  105. continue;
  106. }
  107. uint8_t len = data_size;
  108. if (len > BLE_MAX_NAME_LENGTH) {
  109. len = BLE_MAX_NAME_LENGTH;
  110. }
  111. memcpy(scans[i].name, data, len);
  112. scans[i].name[len] = '\0';
  113. scans[i].time = to_ms_since_boot(get_absolute_time());
  114. return;
  115. }
  116. debug("no matching entry for %s to add name '%.*s' to", bd_addr_to_str(addr), data_size, data);
  117. }
  118. static void hci_scan_result_add_data(bd_addr_t addr, const uint8_t *data, uint8_t data_size) {
  119. for (uint i = 0; i < BLE_MAX_SCAN_RESULTS; i++) {
  120. if (!scans[i].set) {
  121. continue;
  122. }
  123. if (memcmp(addr, scans[i].addr, sizeof(bd_addr_t)) != 0) {
  124. continue;
  125. }
  126. uint8_t len = data_size;
  127. if (len > BLE_MAX_DATA_LENGTH) {
  128. len = BLE_MAX_DATA_LENGTH;
  129. }
  130. memcpy(scans[i].data, data, len);
  131. scans[i].data_len = len;
  132. scans[i].time = to_ms_since_boot(get_absolute_time());
  133. return;
  134. }
  135. debug("no matching entry for %s to add data to", bd_addr_to_str(addr));
  136. }
  137. static void hci_event_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size) {
  138. UNUSED(size);
  139. UNUSED(channel);
  140. //debug("type=0x%02X size=%d", packet_type, size);
  141. //hexdump(packet, size);
  142. if (packet_type != HCI_EVENT_PACKET) {
  143. //debug("unexpected packet 0x%02X", packet_type);
  144. return;
  145. }
  146. switch (hci_event_packet_get_type(packet)) {
  147. case BTSTACK_EVENT_STATE:
  148. if (btstack_event_state_get_state(packet) == HCI_STATE_WORKING) {
  149. bd_addr_t local_addr;
  150. gap_local_bd_addr(local_addr);
  151. debug("BTstack up with %s", bd_addr_to_str(local_addr));
  152. state = TC_IDLE;
  153. } else {
  154. debug("BTstack down (%d)", btstack_event_state_get_state(packet));
  155. state = TC_OFF;
  156. }
  157. break;
  158. case GAP_EVENT_ADVERTISING_REPORT: {
  159. if (state != TC_W4_SCAN) {
  160. debug("scan result in invalid state %d", state);
  161. return;
  162. }
  163. bd_addr_t addr;
  164. gap_event_advertising_report_get_address(packet, addr);
  165. bd_addr_type_t type;
  166. type = gap_event_advertising_report_get_address_type(packet);
  167. int8_t rssi;
  168. rssi = (int8_t)gap_event_advertising_report_get_rssi(packet);
  169. // add data received so far
  170. hci_add_scan_result(addr, type, rssi);
  171. // get advertisement from report event
  172. const uint8_t *adv_data = gap_event_advertising_report_get_data(packet);
  173. uint8_t adv_len = gap_event_advertising_report_get_data_length(packet);
  174. // iterate over advertisement data
  175. ad_context_t context;
  176. for (ad_iterator_init(&context, adv_len, adv_data);
  177. ad_iterator_has_more(&context);
  178. ad_iterator_next(&context)) {
  179. uint8_t data_type = ad_iterator_get_data_type(&context);
  180. uint8_t data_size = ad_iterator_get_data_len(&context);
  181. const uint8_t *data = ad_iterator_get_data(&context);
  182. switch (data_type) {
  183. case BLUETOOTH_DATA_TYPE_SHORTENED_LOCAL_NAME:
  184. case BLUETOOTH_DATA_TYPE_COMPLETE_LOCAL_NAME:
  185. hci_scan_result_add_name(addr, data, data_size);
  186. break;
  187. case BLUETOOTH_DATA_TYPE_SERVICE_DATA:
  188. case BLUETOOTH_DATA_TYPE_MANUFACTURER_SPECIFIC_DATA:
  189. // TODO ugly
  190. if (data_size == 12) {
  191. // Crafty+
  192. hci_scan_result_add_data(addr, data, data_size);
  193. } else if (data_size == 26) {
  194. // Volcano
  195. hci_scan_result_add_data(addr, data, data_size);
  196. }
  197. break;
  198. default:
  199. //debug("Unexpected advertisement type 0x%02X from %s", data_type, bd_addr_to_str(addr));
  200. //hexdump(data, data_size);
  201. break;
  202. }
  203. }
  204. break;
  205. }
  206. case HCI_EVENT_LE_META:
  207. switch (hci_event_le_meta_get_subevent_code(packet)) {
  208. case HCI_SUBEVENT_LE_CONNECTION_COMPLETE:
  209. if (state != TC_W4_CONNECT) {
  210. return;
  211. }
  212. debug("connection complete");
  213. connection_handle = hci_subevent_le_connection_complete_get_connection_handle(packet);
  214. state = TC_READY;
  215. break;
  216. default:
  217. //debug("unexpected LE meta event 0x%02X", hci_event_le_meta_get_subevent_code(packet));
  218. break;
  219. }
  220. break;
  221. case HCI_EVENT_DISCONNECTION_COMPLETE:
  222. debug("disconnected");
  223. connection_handle = HCI_CON_HANDLE_INVALID;
  224. state = TC_IDLE;
  225. break;
  226. case GATT_EVENT_CHARACTERISTIC_VALUE_QUERY_RESULT:
  227. if (state != TC_W4_READ) {
  228. debug("gatt value query result in invalid state %d", state);
  229. return;
  230. }
  231. uint16_t len = gatt_event_characteristic_value_query_result_get_value_length(packet);
  232. if ((read_len + len) > BLE_MAX_VALUE_LEN) {
  233. debug("not enough space for value (%d + %d > %d)", read_len, len, BLE_MAX_VALUE_LEN);
  234. return;
  235. }
  236. memcpy(data_buff + read_len,
  237. gatt_event_characteristic_value_query_result_get_value(packet),
  238. len);
  239. read_len += len;
  240. break;
  241. case GATT_EVENT_SERVICE_QUERY_RESULT:
  242. if (state != TC_W4_SERVICE) {
  243. debug("gatt service query result in invalid state %d", state);
  244. return;
  245. }
  246. gatt_event_service_query_result_get_service(packet, &services[service_idx].service);
  247. //debug("got service %s result", uuid128_to_str(services[service_idx].service.uuid128));
  248. break;
  249. case GATT_EVENT_CHARACTERISTIC_QUERY_RESULT:
  250. if (state != TC_W4_CHARACTERISTIC) {
  251. debug("gatt characteristic query result in invalid state %d", state);
  252. return;
  253. }
  254. gatt_event_characteristic_query_result_get_characteristic(packet, &services[service_idx].chars[characteristic_idx].c);
  255. //debug("got characteristic %s result", uuid128_to_str(services[service_idx].chars[characteristic_idx].c.uuid128));
  256. break;
  257. case GATT_EVENT_QUERY_COMPLETE: {
  258. uint8_t att_status = gatt_event_query_complete_get_att_status(packet);
  259. if (att_status != ATT_ERROR_SUCCESS){
  260. debug("query result has ATT Error 0x%02x in %d", att_status, state);
  261. state = TC_READY;
  262. break;
  263. }
  264. switch (state) {
  265. case TC_W4_READ:
  266. state = TC_READ_COMPLETE;
  267. break;
  268. case TC_W4_SERVICE:
  269. //debug("service %s complete", uuid128_to_str(services[service_idx].service.uuid128));
  270. state = TC_READY;
  271. break;
  272. case TC_W4_CHARACTERISTIC:
  273. //debug("characteristic %s complete", uuid128_to_str(services[service_idx].chars[characteristic_idx].c.uuid128));
  274. state = TC_READY;
  275. break;
  276. case TC_W4_WRITE:
  277. //debug("write complete");
  278. state = TC_WRITE_COMPLETE;
  279. break;
  280. case TC_W4_NOTIFY_ENABLE:
  281. //debug("notify enable complete");
  282. state = TC_NOTIFY_ENABLED;
  283. break;
  284. default:
  285. debug("gatt query complete in invalid state %d", state);
  286. break;
  287. }
  288. break;
  289. }
  290. case GATT_EVENT_NOTIFICATION: {
  291. if ((state != TC_READY) && (state != TC_WRITE_COMPLETE)) {
  292. debug("gatt notification in invalid state %d", state);
  293. return;
  294. }
  295. uint16_t value_length = gatt_event_notification_get_value_length(packet);
  296. const uint8_t *value = gatt_event_notification_get_value(packet);
  297. if ((read_len + value_length) <= BLE_MAX_VALUE_LEN) {
  298. memcpy(data_buff + read_len, value, value_length);
  299. read_len += value_length;
  300. }
  301. break;
  302. }
  303. default:
  304. //debug("unexpected event 0x%02X", hci_event_packet_get_type(packet));
  305. break;
  306. }
  307. }
  308. void ble_init(void) {
  309. cyw43_thread_enter();
  310. state = TC_OFF;
  311. for (uint i = 0; i < BLE_MAX_SCAN_RESULTS; i++) {
  312. scans[i].set = false;
  313. }
  314. for (uint i = 0; i < BLE_MAX_SERVICES; i++) {
  315. services[i].set = false;
  316. for (uint j = 0; j < BLE_MAX_CHARACTERISTICS; j++) {
  317. services[i].chars[j].set = false;
  318. }
  319. }
  320. cyw43_thread_exit();
  321. l2cap_init();
  322. sm_init();
  323. sm_set_io_capabilities(IO_CAPABILITY_NO_INPUT_NO_OUTPUT);
  324. gatt_client_init();
  325. hci_event_callback_registration.callback = &hci_event_handler;
  326. hci_add_event_handler(&hci_event_callback_registration);
  327. hci_power_control(HCI_POWER_ON);
  328. }
  329. bool ble_is_ready(void) {
  330. cyw43_thread_enter();
  331. bool v = (state != TC_OFF);
  332. cyw43_thread_exit();
  333. return v;
  334. }
  335. void ble_scan(enum ble_scan_mode mode) {
  336. cyw43_thread_enter();
  337. if (state == TC_OFF) {
  338. cyw43_thread_exit();
  339. return;
  340. }
  341. switch (mode) {
  342. case BLE_SCAN_OFF:
  343. debug("stopping BLE scan");
  344. gap_stop_scan();
  345. state = TC_IDLE;
  346. break;
  347. case BLE_SCAN_ON:
  348. debug("starting BLE scan");
  349. state = TC_W4_SCAN;
  350. gap_set_scan_parameters(1, 0x0030, 0x0030);
  351. gap_start_scan();
  352. break;
  353. case BLE_SCAN_TOGGLE:
  354. switch (state) {
  355. case TC_W4_SCAN:
  356. cyw43_thread_exit();
  357. ble_scan(0);
  358. return;
  359. case TC_IDLE:
  360. cyw43_thread_exit();
  361. ble_scan(1);
  362. return;
  363. default:
  364. debug("invalid state %d", state);
  365. break;
  366. }
  367. break;
  368. default:
  369. debug("invalid mode %d", mode);
  370. break;
  371. }
  372. cyw43_thread_exit();
  373. }
  374. int32_t ble_get_scan_results(struct ble_scan_result *buf, uint16_t len) {
  375. if (!buf || (len <= 0)) {
  376. return -1;
  377. }
  378. cyw43_thread_enter();
  379. if (state == TC_OFF) {
  380. cyw43_thread_exit();
  381. return -1;
  382. }
  383. uint16_t pos = 0;
  384. for (uint16_t i = 0; i < BLE_MAX_SCAN_RESULTS; i++) {
  385. if (!scans[i].set) {
  386. continue;
  387. }
  388. // only age out entries while scanning, otherwise keep results cached
  389. if (state == TC_W4_SCAN) {
  390. uint32_t diff = to_ms_since_boot(get_absolute_time()) - scans[i].time;
  391. if (diff >= BLE_MAX_SCAN_AGE_MS) {
  392. //debug("removing %s due to age", bd_addr_to_str(scans[i].addr));
  393. scans[i].set = false;
  394. }
  395. }
  396. memcpy(buf + pos, scans + i, sizeof(struct ble_scan_result));
  397. pos++;
  398. if (pos >= len) {
  399. break;
  400. }
  401. }
  402. cyw43_thread_exit();
  403. return pos;
  404. }
  405. void ble_connect(bd_addr_t addr, bd_addr_type_t type) {
  406. cyw43_thread_enter();
  407. switch (state) {
  408. case TC_W4_SCAN:
  409. cyw43_thread_exit();
  410. ble_scan(0);
  411. cyw43_thread_enter();
  412. break;
  413. case TC_READY:
  414. gap_disconnect(connection_handle);
  415. break;
  416. case TC_IDLE:
  417. break;
  418. default:
  419. debug("invalid state for connect %d", state);
  420. cyw43_thread_exit();
  421. return;
  422. }
  423. debug("connecting to %s", bd_addr_to_str(addr));
  424. state = TC_W4_CONNECT;
  425. gap_connect(addr, type);
  426. cyw43_thread_exit();
  427. }
  428. bool ble_is_connected(void) {
  429. cyw43_thread_enter();
  430. bool v = (state == TC_READY)
  431. || (state == TC_W4_READ)
  432. || (state == TC_READ_COMPLETE)
  433. || (state == TC_W4_SERVICE)
  434. || (state == TC_W4_CHARACTERISTIC)
  435. || (state == TC_W4_WRITE)
  436. || (state == TC_WRITE_COMPLETE);
  437. cyw43_thread_exit();
  438. return v;
  439. }
  440. void ble_disconnect(void) {
  441. cyw43_thread_enter();
  442. if (state == TC_READY) {
  443. debug("disconnecting");
  444. gap_disconnect(connection_handle);
  445. } else {
  446. debug("invalid state for disconnect %d", state);
  447. }
  448. cyw43_thread_exit();
  449. }
  450. int32_t ble_read(const uint8_t *characteristic, uint8_t *buff, uint16_t buff_len) {
  451. cyw43_thread_enter();
  452. if (state != TC_READY) {
  453. cyw43_thread_exit();
  454. debug("invalid state for read (%d)", state);
  455. return -1;
  456. }
  457. uint8_t r = gatt_client_read_value_of_characteristics_by_uuid128(hci_event_handler,
  458. connection_handle,
  459. 0x0001, 0xFFFF,
  460. characteristic);
  461. if (r != ERROR_CODE_SUCCESS) {
  462. cyw43_thread_exit();
  463. debug("gatt read failed %d", r);
  464. return -2;
  465. }
  466. state = TC_W4_READ;
  467. read_len = 0;
  468. cyw43_thread_exit();
  469. uint32_t start_time = to_ms_since_boot(get_absolute_time());
  470. while (1) {
  471. sleep_ms(1);
  472. main_loop_hw();
  473. uint32_t now = to_ms_since_boot(get_absolute_time());
  474. if ((now - start_time) >= BLE_READ_TIMEOUT_MS) {
  475. debug("timeout waiting for read");
  476. cyw43_thread_enter();
  477. state = TC_READY;
  478. cyw43_thread_exit();
  479. return -3;
  480. }
  481. cyw43_thread_enter();
  482. enum ble_state state_cached = state;
  483. cyw43_thread_exit();
  484. if (state_cached == TC_READ_COMPLETE) {
  485. break;
  486. }
  487. }
  488. cyw43_thread_enter();
  489. state = TC_READY;
  490. if (read_len > buff_len) {
  491. debug("buffer too short (%d < %d)", buff_len, read_len);
  492. cyw43_thread_exit();
  493. return -4;
  494. }
  495. memcpy(buff, data_buff, read_len);
  496. uint16_t tmp = read_len;
  497. read_len = 0;
  498. cyw43_thread_exit();
  499. return tmp;
  500. }
  501. static int discover_service(const uint8_t *service) {
  502. // check if service has already been discovered
  503. int srvc = -1, free_srvc = -1;
  504. for (int i = 0; i < BLE_MAX_SERVICES; i++) {
  505. if (!services[i].set) {
  506. if (free_srvc < 0) {
  507. free_srvc = i;
  508. }
  509. continue;
  510. }
  511. if (memcmp(services[i].service.uuid128, service, 16) == 0) {
  512. srvc = i;
  513. break;
  514. }
  515. }
  516. // if this service has not been discovered yet, add it
  517. if (srvc < 0) {
  518. if (free_srvc < 0) {
  519. debug("no space left for BLE service. overwriting.");
  520. free_srvc = 0;
  521. }
  522. srvc = free_srvc;
  523. services[srvc].set = true;
  524. debug("discovering service %s at %d", uuid128_to_str(service), srvc);
  525. uint8_t r = gatt_client_discover_primary_services_by_uuid128(hci_event_handler,
  526. connection_handle,
  527. service);
  528. if (r != ERROR_CODE_SUCCESS) {
  529. cyw43_thread_exit();
  530. debug("gatt service discovery failed %d", r);
  531. return -2;
  532. }
  533. state = TC_W4_SERVICE;
  534. service_idx = srvc;
  535. cyw43_thread_exit();
  536. uint32_t start_time = to_ms_since_boot(get_absolute_time());
  537. while (1) {
  538. sleep_ms(1);
  539. main_loop_hw();
  540. uint32_t now = to_ms_since_boot(get_absolute_time());
  541. if ((now - start_time) >= BLE_SRVC_TIMEOUT_MS) {
  542. debug("timeout waiting for service");
  543. cyw43_thread_enter();
  544. state = TC_READY;
  545. cyw43_thread_exit();
  546. return -3;
  547. }
  548. cyw43_thread_enter();
  549. enum ble_state state_cached = state;
  550. cyw43_thread_exit();
  551. if (state_cached == TC_READY) {
  552. break;
  553. }
  554. }
  555. cyw43_thread_enter();
  556. }
  557. return srvc;
  558. }
  559. static int discover_characteristic(int srvc, const uint8_t *characteristic) {
  560. // check if characteristic has already been discovered
  561. int ch = -1, free_ch = -1;
  562. for (int i = 0; i < BLE_MAX_CHARACTERISTICS; i++) {
  563. if (!services[srvc].chars[i].set) {
  564. if (free_ch < 0) {
  565. free_ch = i;
  566. }
  567. continue;
  568. }
  569. if (memcmp(services[srvc].chars[i].c.uuid128, characteristic, 16) == 0) {
  570. ch = i;
  571. break;
  572. }
  573. }
  574. // if this characteristic has not been discovered yet, add it
  575. if (ch < 0) {
  576. if (free_ch < 0) {
  577. debug("no space left for BLE characteristic. overwriting.");
  578. free_ch = 0;
  579. }
  580. ch = free_ch;
  581. services[srvc].chars[ch].set = true;
  582. debug("discovering characteristic %s at %d", uuid128_to_str(characteristic), ch);
  583. uint8_t r = gatt_client_discover_characteristics_for_service_by_uuid128(hci_event_handler,
  584. connection_handle,
  585. &services[srvc].service,
  586. characteristic);
  587. if (r != ERROR_CODE_SUCCESS) {
  588. cyw43_thread_exit();
  589. debug("gatt characteristic discovery failed %d", r);
  590. return -4;
  591. }
  592. state = TC_W4_CHARACTERISTIC;
  593. characteristic_idx = ch;
  594. cyw43_thread_exit();
  595. uint32_t start_time = to_ms_since_boot(get_absolute_time());
  596. while (1) {
  597. sleep_ms(1);
  598. main_loop_hw();
  599. uint32_t now = to_ms_since_boot(get_absolute_time());
  600. if ((now - start_time) >= BLE_CHAR_TIMEOUT_MS) {
  601. debug("timeout waiting for characteristic");
  602. cyw43_thread_enter();
  603. state = TC_READY;
  604. cyw43_thread_exit();
  605. return -5;
  606. }
  607. cyw43_thread_enter();
  608. enum ble_state state_cached = state;
  609. cyw43_thread_exit();
  610. if (state_cached == TC_READY) {
  611. break;
  612. }
  613. }
  614. cyw43_thread_enter();
  615. }
  616. return ch;
  617. }
  618. int8_t ble_write(const uint8_t *service, const uint8_t *characteristic,
  619. const uint8_t *buff, uint16_t buff_len) {
  620. cyw43_thread_enter();
  621. if (state != TC_READY) {
  622. cyw43_thread_exit();
  623. debug("invalid state for write (%d)", state);
  624. return -1;
  625. }
  626. int srvc = discover_service(service);
  627. if (srvc < 0) {
  628. debug("error discovering service (%d)", srvc);
  629. return srvc;
  630. }
  631. int ch = discover_characteristic(srvc, characteristic);
  632. if (ch < 0) {
  633. debug("error discovering characteristic (%d)", ch);
  634. return ch;
  635. }
  636. if (buff_len > BLE_MAX_VALUE_LEN) {
  637. buff_len = BLE_MAX_VALUE_LEN;
  638. }
  639. memcpy(data_buff, buff, buff_len);
  640. uint8_t r = gatt_client_write_value_of_characteristic(hci_event_handler,
  641. connection_handle,
  642. services[srvc].chars[ch].c.value_handle,
  643. buff_len, data_buff);
  644. if (r != ERROR_CODE_SUCCESS) {
  645. cyw43_thread_exit();
  646. debug("gatt write failed %d", r);
  647. return -6;
  648. }
  649. state = TC_W4_WRITE;
  650. cyw43_thread_exit();
  651. uint32_t start_time = to_ms_since_boot(get_absolute_time());
  652. while (1) {
  653. sleep_ms(1);
  654. main_loop_hw();
  655. uint32_t now = to_ms_since_boot(get_absolute_time());
  656. if ((now - start_time) >= BLE_WRTE_TIMEOUT_MS) {
  657. debug("timeout waiting for write");
  658. cyw43_thread_enter();
  659. state = TC_READY;
  660. cyw43_thread_exit();
  661. return -7;
  662. }
  663. cyw43_thread_enter();
  664. enum ble_state state_cached = state;
  665. cyw43_thread_exit();
  666. if ((state_cached == TC_WRITE_COMPLETE) || (state_cached == TC_READY)) {
  667. break;
  668. }
  669. }
  670. cyw43_thread_enter();
  671. int8_t ret = (state == TC_WRITE_COMPLETE) ? 0 : -8;
  672. state = TC_READY;
  673. cyw43_thread_exit();
  674. return ret;
  675. }
  676. int8_t ble_discover(const uint8_t *service, const uint8_t *characteristic) {
  677. cyw43_thread_enter();
  678. if (state != TC_READY) {
  679. cyw43_thread_exit();
  680. debug("invalid state for discovery (%d)", state);
  681. return -1;
  682. }
  683. int srvc = discover_service(service);
  684. if (srvc < 0) {
  685. debug("error discovering service (%d)", srvc);
  686. return srvc;
  687. }
  688. int ch = discover_characteristic(srvc, characteristic);
  689. if (ch < 0) {
  690. debug("error discovering characteristic (%d)", ch);
  691. return ch;
  692. }
  693. cyw43_thread_exit();
  694. return 0;
  695. }
  696. int8_t ble_notification_disable(const uint8_t *service, const uint8_t *characteristic) {
  697. cyw43_thread_enter();
  698. if (state != TC_READY) {
  699. cyw43_thread_exit();
  700. debug("invalid state for notify (%d)", state);
  701. return -1;
  702. }
  703. int srvc = discover_service(service);
  704. if (srvc < 0) {
  705. debug("error discovering service (%d)", srvc);
  706. return srvc;
  707. }
  708. int ch = discover_characteristic(srvc, characteristic);
  709. if (ch < 0) {
  710. debug("error discovering characteristic (%d)", ch);
  711. return ch;
  712. }
  713. gatt_client_stop_listening_for_characteristic_value_updates(&services[srvc].chars[ch].n);
  714. cyw43_thread_exit();
  715. return 0;
  716. }
  717. int8_t ble_notification_enable(const uint8_t *service, const uint8_t *characteristic) {
  718. cyw43_thread_enter();
  719. if (state != TC_READY) {
  720. cyw43_thread_exit();
  721. debug("invalid state for notify (%d)", state);
  722. return -1;
  723. }
  724. int srvc = discover_service(service);
  725. if (srvc < 0) {
  726. debug("error discovering service (%d)", srvc);
  727. return srvc;
  728. }
  729. int ch = discover_characteristic(srvc, characteristic);
  730. if (ch < 0) {
  731. debug("error discovering characteristic (%d)", ch);
  732. return ch;
  733. }
  734. gatt_client_listen_for_characteristic_value_updates(&services[srvc].chars[ch].n,
  735. hci_event_handler,
  736. connection_handle,
  737. &services[srvc].chars[ch].c);
  738. gatt_client_write_client_characteristic_configuration(hci_event_handler,
  739. connection_handle,
  740. &services[srvc].chars[ch].c,
  741. GATT_CLIENT_CHARACTERISTICS_CONFIGURATION_NOTIFICATION);
  742. state = TC_W4_NOTIFY_ENABLE;
  743. cyw43_thread_exit();
  744. uint32_t start_time = to_ms_since_boot(get_absolute_time());
  745. while (1) {
  746. sleep_ms(1);
  747. main_loop_hw();
  748. uint32_t now = to_ms_since_boot(get_absolute_time());
  749. if ((now - start_time) >= BLE_NOTY_TIMEOUT_MS) {
  750. debug("timeout waiting for notify enable");
  751. cyw43_thread_enter();
  752. state = TC_READY;
  753. cyw43_thread_exit();
  754. return -6;
  755. }
  756. cyw43_thread_enter();
  757. enum ble_state state_cached = state;
  758. cyw43_thread_exit();
  759. if ((state_cached == TC_NOTIFY_ENABLED) || (state_cached == TC_READY)) {
  760. break;
  761. }
  762. }
  763. cyw43_thread_enter();
  764. int8_t ret = (state == TC_NOTIFY_ENABLED) ? 0 : -7;
  765. state = TC_READY;
  766. cyw43_thread_exit();
  767. return ret;
  768. }
  769. bool ble_notification_ready(void) {
  770. cyw43_thread_enter();
  771. if (state != TC_READY) {
  772. cyw43_thread_exit();
  773. debug("invalid state for notify (%d)", state);
  774. return -1;
  775. }
  776. uint16_t tmp = read_len;
  777. cyw43_thread_exit();
  778. return (tmp > 0);
  779. }
  780. uint16_t ble_notification_get(uint8_t *buff, uint16_t buff_len) {
  781. cyw43_thread_enter();
  782. if (state != TC_READY) {
  783. cyw43_thread_exit();
  784. debug("invalid state for notify (%d)", state);
  785. return -1;
  786. }
  787. if (read_len > buff_len) {
  788. debug("buffer too short (%d < %d)", buff_len, read_len);
  789. cyw43_thread_exit();
  790. return -2;
  791. }
  792. memcpy(buff, data_buff, read_len);
  793. uint16_t tmp = read_len;
  794. read_len = 0;
  795. cyw43_thread_exit();
  796. return tmp;
  797. }