|
@@ -24,6 +24,7 @@
|
24
|
24
|
|
25
|
25
|
#include "config.h"
|
26
|
26
|
#include "log.h"
|
|
27
|
+#include "main.h"
|
27
|
28
|
#include "util.h"
|
28
|
29
|
#include "ble.h"
|
29
|
30
|
|
|
@@ -104,6 +105,7 @@ static void hci_add_scan_result(bd_addr_t addr, bd_addr_type_t type, int8_t rssi
|
104
|
105
|
scans[unused].type = type;
|
105
|
106
|
scans[unused].rssi = rssi;
|
106
|
107
|
scans[unused].name[0] = '\0';
|
|
108
|
+ scans[unused].data_len = 0;
|
107
|
109
|
}
|
108
|
110
|
|
109
|
111
|
static void hci_scan_result_add_name(bd_addr_t addr, const uint8_t *data, uint8_t data_size) {
|
|
@@ -128,6 +130,28 @@ static void hci_scan_result_add_name(bd_addr_t addr, const uint8_t *data, uint8_
|
128
|
130
|
debug("no matching entry for %s to add name '%.*s' to", bd_addr_to_str(addr), data_size, data);
|
129
|
131
|
}
|
130
|
132
|
|
|
133
|
+static void hci_scan_result_add_data(bd_addr_t addr, const uint8_t *data, uint8_t data_size) {
|
|
134
|
+ for (uint i = 0; i < BLE_MAX_SCAN_RESULTS; i++) {
|
|
135
|
+ if (!scans[i].set) {
|
|
136
|
+ continue;
|
|
137
|
+ }
|
|
138
|
+ if (memcmp(addr, scans[i].addr, sizeof(bd_addr_t)) != 0) {
|
|
139
|
+ continue;
|
|
140
|
+ }
|
|
141
|
+
|
|
142
|
+ uint8_t len = data_size;
|
|
143
|
+ if (len > BLE_MAX_DATA_LENGTH) {
|
|
144
|
+ len = BLE_MAX_DATA_LENGTH;
|
|
145
|
+ }
|
|
146
|
+ memcpy(scans[i].data, data, len);
|
|
147
|
+ scans[i].data_len = len;
|
|
148
|
+ scans[i].time = to_ms_since_boot(get_absolute_time());
|
|
149
|
+ return;
|
|
150
|
+ }
|
|
151
|
+
|
|
152
|
+ debug("no matching entry for %s to add data to", bd_addr_to_str(addr));
|
|
153
|
+}
|
|
154
|
+
|
131
|
155
|
static void hci_event_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size) {
|
132
|
156
|
UNUSED(size);
|
133
|
157
|
UNUSED(channel);
|
|
@@ -189,6 +213,18 @@ static void hci_event_handler(uint8_t packet_type, uint16_t channel, uint8_t *pa
|
189
|
213
|
hci_scan_result_add_name(addr, data, data_size);
|
190
|
214
|
break;
|
191
|
215
|
|
|
216
|
+ case BLUETOOTH_DATA_TYPE_SERVICE_DATA:
|
|
217
|
+ case BLUETOOTH_DATA_TYPE_MANUFACTURER_SPECIFIC_DATA:
|
|
218
|
+ // TODO ugly
|
|
219
|
+ if (data_size == 12) {
|
|
220
|
+ // Crafty+
|
|
221
|
+ hci_scan_result_add_data(addr, data, data_size);
|
|
222
|
+ } else if (data_size == 26) {
|
|
223
|
+ // Volcano
|
|
224
|
+ hci_scan_result_add_data(addr, data, data_size);
|
|
225
|
+ }
|
|
226
|
+ break;
|
|
227
|
+
|
192
|
228
|
default:
|
193
|
229
|
//debug("Unexpected advertisement type 0x%02X from %s", data_type, bd_addr_to_str(addr));
|
194
|
230
|
//hexdump(data, data_size);
|
|
@@ -399,10 +435,13 @@ int32_t ble_get_scan_results(struct ble_scan_result *buf, uint16_t len) {
|
399
|
435
|
continue;
|
400
|
436
|
}
|
401
|
437
|
|
402
|
|
- uint32_t diff = to_ms_since_boot(get_absolute_time()) - scans[i].time;
|
403
|
|
- if (diff >= BLE_MAX_SCAN_AGE_MS) {
|
404
|
|
- //debug("removing %s due to age", bd_addr_to_str(scans[i].addr));
|
405
|
|
- scans[i].set = false;
|
|
438
|
+ // only age out entries while scanning, otherwise keep results cached
|
|
439
|
+ if (state == TC_W4_SCAN) {
|
|
440
|
+ uint32_t diff = to_ms_since_boot(get_absolute_time()) - scans[i].time;
|
|
441
|
+ if (diff >= BLE_MAX_SCAN_AGE_MS) {
|
|
442
|
+ //debug("removing %s due to age", bd_addr_to_str(scans[i].addr));
|
|
443
|
+ scans[i].set = false;
|
|
444
|
+ }
|
406
|
445
|
}
|
407
|
446
|
|
408
|
447
|
memcpy(buf + pos, scans + i, sizeof(struct ble_scan_result));
|
|
@@ -497,15 +536,13 @@ int32_t ble_read(const uint8_t *characteristic, uint8_t *buff, uint16_t buff_len
|
497
|
536
|
uint32_t start_time = to_ms_since_boot(get_absolute_time());
|
498
|
537
|
while (1) {
|
499
|
538
|
sleep_ms(1);
|
|
539
|
+ main_loop_hw();
|
500
|
540
|
|
501
|
541
|
uint32_t now = to_ms_since_boot(get_absolute_time());
|
502
|
542
|
if ((now - start_time) >= BLE_READ_TIMEOUT_MS) {
|
503
|
543
|
debug("timeout waiting for read");
|
504
|
544
|
return -3;
|
505
|
545
|
}
|
506
|
|
-#if BLE_READ_TIMEOUT_MS >= (WATCHDOG_PERIOD_MS / 2)
|
507
|
|
- watchdog_update();
|
508
|
|
-#endif
|
509
|
546
|
|
510
|
547
|
cyw43_thread_enter();
|
511
|
548
|
enum ble_state state_cached = state;
|
|
@@ -585,15 +622,13 @@ int8_t ble_write(const uint8_t *service, const uint8_t *characteristic,
|
585
|
622
|
uint32_t start_time = to_ms_since_boot(get_absolute_time());
|
586
|
623
|
while (1) {
|
587
|
624
|
sleep_ms(1);
|
|
625
|
+ main_loop_hw();
|
588
|
626
|
|
589
|
627
|
uint32_t now = to_ms_since_boot(get_absolute_time());
|
590
|
628
|
if ((now - start_time) >= BLE_SRVC_TIMEOUT_MS) {
|
591
|
629
|
debug("timeout waiting for service");
|
592
|
630
|
return -3;
|
593
|
631
|
}
|
594
|
|
-#if BLE_SRVC_TIMEOUT_MS >= (WATCHDOG_PERIOD_MS / 2)
|
595
|
|
- watchdog_update();
|
596
|
|
-#endif
|
597
|
632
|
|
598
|
633
|
cyw43_thread_enter();
|
599
|
634
|
enum ble_state state_cached = state;
|
|
@@ -604,9 +639,6 @@ int8_t ble_write(const uint8_t *service, const uint8_t *characteristic,
|
604
|
639
|
}
|
605
|
640
|
}
|
606
|
641
|
|
607
|
|
- // allow some time for service discovery
|
608
|
|
- watchdog_update();
|
609
|
|
-
|
610
|
642
|
cyw43_thread_enter();
|
611
|
643
|
}
|
612
|
644
|
|
|
@@ -654,15 +686,13 @@ int8_t ble_write(const uint8_t *service, const uint8_t *characteristic,
|
654
|
686
|
uint32_t start_time = to_ms_since_boot(get_absolute_time());
|
655
|
687
|
while (1) {
|
656
|
688
|
sleep_ms(1);
|
|
689
|
+ main_loop_hw();
|
657
|
690
|
|
658
|
691
|
uint32_t now = to_ms_since_boot(get_absolute_time());
|
659
|
692
|
if ((now - start_time) >= BLE_CHAR_TIMEOUT_MS) {
|
660
|
693
|
debug("timeout waiting for characteristic");
|
661
|
694
|
return -5;
|
662
|
695
|
}
|
663
|
|
-#if BLE_CHAR_TIMEOUT_MS >= (WATCHDOG_PERIOD_MS / 2)
|
664
|
|
- watchdog_update();
|
665
|
|
-#endif
|
666
|
696
|
|
667
|
697
|
cyw43_thread_enter();
|
668
|
698
|
enum ble_state state_cached = state;
|
|
@@ -673,9 +703,6 @@ int8_t ble_write(const uint8_t *service, const uint8_t *characteristic,
|
673
|
703
|
}
|
674
|
704
|
}
|
675
|
705
|
|
676
|
|
- // allow some time for characteristic discovery
|
677
|
|
- watchdog_update();
|
678
|
|
-
|
679
|
706
|
cyw43_thread_enter();
|
680
|
707
|
}
|
681
|
708
|
|
|
@@ -700,15 +727,13 @@ int8_t ble_write(const uint8_t *service, const uint8_t *characteristic,
|
700
|
727
|
uint32_t start_time = to_ms_since_boot(get_absolute_time());
|
701
|
728
|
while (1) {
|
702
|
729
|
sleep_ms(1);
|
|
730
|
+ main_loop_hw();
|
703
|
731
|
|
704
|
732
|
uint32_t now = to_ms_since_boot(get_absolute_time());
|
705
|
733
|
if ((now - start_time) >= BLE_WRTE_TIMEOUT_MS) {
|
706
|
734
|
debug("timeout waiting for write");
|
707
|
735
|
return -7;
|
708
|
736
|
}
|
709
|
|
-#if BLE_WRTE_TIMEOUT_MS >= (WATCHDOG_PERIOD_MS / 2)
|
710
|
|
- watchdog_update();
|
711
|
|
-#endif
|
712
|
737
|
|
713
|
738
|
cyw43_thread_enter();
|
714
|
739
|
enum ble_state state_cached = state;
|