|
@@ -20,12 +20,17 @@
|
20
|
20
|
*/
|
21
|
21
|
|
22
|
22
|
#include "pico/cyw43_arch.h"
|
|
23
|
+#include "hardware/watchdog.h"
|
23
|
24
|
|
24
|
25
|
#include "config.h"
|
25
|
26
|
#include "log.h"
|
26
|
27
|
#include "util.h"
|
27
|
28
|
#include "ble.h"
|
28
|
29
|
|
|
30
|
+#define BLE_READ_TIMEOUT_MS 500
|
|
31
|
+#define BLE_SRVC_TIMEOUT_MS 500
|
|
32
|
+#define BLE_CHAR_TIMEOUT_MS 2000
|
|
33
|
+#define BLE_WRTE_TIMEOUT_MS 500
|
29
|
34
|
#define BLE_MAX_SCAN_AGE_MS (10 * 1000)
|
30
|
35
|
#define BLE_MAX_SERVICES 8
|
31
|
36
|
#define BLE_MAX_CHARACTERISTICS 8
|
|
@@ -485,10 +490,18 @@ int32_t ble_read(const uint8_t *characteristic, uint8_t *buff, uint16_t buff_len
|
485
|
490
|
read_len = 0;
|
486
|
491
|
cyw43_thread_exit();
|
487
|
492
|
|
|
493
|
+ uint32_t start_time = to_ms_since_boot(get_absolute_time());
|
488
|
494
|
while (1) {
|
489
|
495
|
sleep_ms(1);
|
490
|
496
|
|
491
|
|
- // TODO timeout
|
|
497
|
+ uint32_t now = to_ms_since_boot(get_absolute_time());
|
|
498
|
+ if ((now - start_time) >= BLE_READ_TIMEOUT_MS) {
|
|
499
|
+ debug("timeout waiting for read");
|
|
500
|
+ return -3;
|
|
501
|
+ }
|
|
502
|
+#if BLE_READ_TIMEOUT_MS >= (WATCHDOG_PERIOD_MS / 2)
|
|
503
|
+ watchdog_update();
|
|
504
|
+#endif
|
492
|
505
|
|
493
|
506
|
cyw43_thread_enter();
|
494
|
507
|
enum ble_state state_cached = state;
|
|
@@ -506,7 +519,7 @@ int32_t ble_read(const uint8_t *characteristic, uint8_t *buff, uint16_t buff_len
|
506
|
519
|
if (read_len > buff_len) {
|
507
|
520
|
debug("buffer too short (%d < %d)", buff_len, read_len);
|
508
|
521
|
cyw43_thread_exit();
|
509
|
|
- return -3;
|
|
522
|
+ return -4;
|
510
|
523
|
}
|
511
|
524
|
|
512
|
525
|
memcpy(buff, data_buff, read_len);
|
|
@@ -566,10 +579,19 @@ int8_t ble_write(const uint8_t *service, const uint8_t *characteristic,
|
566
|
579
|
cyw43_thread_exit();
|
567
|
580
|
|
568
|
581
|
debug("waiting for service discovery");
|
|
582
|
+
|
|
583
|
+ uint32_t start_time = to_ms_since_boot(get_absolute_time());
|
569
|
584
|
while (1) {
|
570
|
585
|
sleep_ms(1);
|
571
|
586
|
|
572
|
|
- // TODO timeout
|
|
587
|
+ uint32_t now = to_ms_since_boot(get_absolute_time());
|
|
588
|
+ if ((now - start_time) >= BLE_SRVC_TIMEOUT_MS) {
|
|
589
|
+ debug("timeout waiting for service");
|
|
590
|
+ return -3;
|
|
591
|
+ }
|
|
592
|
+#if BLE_SRVC_TIMEOUT_MS >= (WATCHDOG_PERIOD_MS / 2)
|
|
593
|
+ watchdog_update();
|
|
594
|
+#endif
|
573
|
595
|
|
574
|
596
|
cyw43_thread_enter();
|
575
|
597
|
enum ble_state state_cached = state;
|
|
@@ -580,6 +602,11 @@ int8_t ble_write(const uint8_t *service, const uint8_t *characteristic,
|
580
|
602
|
break;
|
581
|
603
|
}
|
582
|
604
|
}
|
|
605
|
+
|
|
606
|
+ // allow some time for service discovery
|
|
607
|
+ watchdog_update();
|
|
608
|
+
|
|
609
|
+ cyw43_thread_enter();
|
583
|
610
|
}
|
584
|
611
|
|
585
|
612
|
// check if characteristic has already been discovered
|
|
@@ -616,7 +643,7 @@ int8_t ble_write(const uint8_t *service, const uint8_t *characteristic,
|
616
|
643
|
if (r != ERROR_CODE_SUCCESS) {
|
617
|
644
|
cyw43_thread_exit();
|
618
|
645
|
debug("gatt characteristic discovery failed %d", r);
|
619
|
|
- return -3;
|
|
646
|
+ return -4;
|
620
|
647
|
}
|
621
|
648
|
|
622
|
649
|
state = TC_W4_CHARACTERISTIC;
|
|
@@ -624,10 +651,19 @@ int8_t ble_write(const uint8_t *service, const uint8_t *characteristic,
|
624
|
651
|
cyw43_thread_exit();
|
625
|
652
|
|
626
|
653
|
debug("waiting for characteristic discovery");
|
|
654
|
+
|
|
655
|
+ uint32_t start_time = to_ms_since_boot(get_absolute_time());
|
627
|
656
|
while (1) {
|
628
|
657
|
sleep_ms(1);
|
629
|
658
|
|
630
|
|
- // TODO timeout
|
|
659
|
+ uint32_t now = to_ms_since_boot(get_absolute_time());
|
|
660
|
+ if ((now - start_time) >= BLE_CHAR_TIMEOUT_MS) {
|
|
661
|
+ debug("timeout waiting for characteristic");
|
|
662
|
+ return -5;
|
|
663
|
+ }
|
|
664
|
+#if BLE_CHAR_TIMEOUT_MS >= (WATCHDOG_PERIOD_MS / 2)
|
|
665
|
+ watchdog_update();
|
|
666
|
+#endif
|
631
|
667
|
|
632
|
668
|
cyw43_thread_enter();
|
633
|
669
|
enum ble_state state_cached = state;
|
|
@@ -638,6 +674,11 @@ int8_t ble_write(const uint8_t *service, const uint8_t *characteristic,
|
638
|
674
|
break;
|
639
|
675
|
}
|
640
|
676
|
}
|
|
677
|
+
|
|
678
|
+ // allow some time for characteristic discovery
|
|
679
|
+ watchdog_update();
|
|
680
|
+
|
|
681
|
+ cyw43_thread_enter();
|
641
|
682
|
}
|
642
|
683
|
|
643
|
684
|
if (buff_len > BLE_MAX_VALUE_LEN) {
|
|
@@ -652,17 +693,26 @@ int8_t ble_write(const uint8_t *service, const uint8_t *characteristic,
|
652
|
693
|
if (r != ERROR_CODE_SUCCESS) {
|
653
|
694
|
cyw43_thread_exit();
|
654
|
695
|
debug("gatt write failed %d", r);
|
655
|
|
- return -4;
|
|
696
|
+ return -6;
|
656
|
697
|
}
|
657
|
698
|
|
658
|
699
|
state = TC_W4_WRITE;
|
659
|
700
|
cyw43_thread_exit();
|
660
|
701
|
|
661
|
702
|
debug("waiting for write");
|
|
703
|
+
|
|
704
|
+ uint32_t start_time = to_ms_since_boot(get_absolute_time());
|
662
|
705
|
while (1) {
|
663
|
706
|
sleep_ms(1);
|
664
|
707
|
|
665
|
|
- // TODO timeout
|
|
708
|
+ uint32_t now = to_ms_since_boot(get_absolute_time());
|
|
709
|
+ if ((now - start_time) >= BLE_WRTE_TIMEOUT_MS) {
|
|
710
|
+ debug("timeout waiting for write");
|
|
711
|
+ return -7;
|
|
712
|
+ }
|
|
713
|
+#if BLE_WRTE_TIMEOUT_MS >= (WATCHDOG_PERIOD_MS / 2)
|
|
714
|
+ watchdog_update();
|
|
715
|
+#endif
|
666
|
716
|
|
667
|
717
|
cyw43_thread_enter();
|
668
|
718
|
enum ble_state state_cached = state;
|
|
@@ -676,7 +726,7 @@ int8_t ble_write(const uint8_t *service, const uint8_t *characteristic,
|
676
|
726
|
|
677
|
727
|
cyw43_thread_enter();
|
678
|
728
|
|
679
|
|
- int8_t ret = (state == TC_WRITE_COMPLETE) ? 0 : -1;
|
|
729
|
+ int8_t ret = (state == TC_WRITE_COMPLETE) ? 0 : -8;
|
680
|
730
|
state = TC_READY;
|
681
|
731
|
|
682
|
732
|
cyw43_thread_exit();
|