|
@@ -28,10 +28,10 @@
|
28
|
28
|
#include "util.h"
|
29
|
29
|
#include "ble.h"
|
30
|
30
|
|
31
|
|
-#define BLE_READ_TIMEOUT_MS (2 * 500)
|
32
|
|
-#define BLE_SRVC_TIMEOUT_MS (2 * 500)
|
33
|
|
-#define BLE_CHAR_TIMEOUT_MS (2 * 2000)
|
34
|
|
-#define BLE_WRTE_TIMEOUT_MS (2 * 500)
|
|
31
|
+#define BLE_READ_TIMEOUT_MS (3 * 500)
|
|
32
|
+#define BLE_SRVC_TIMEOUT_MS (3 * 500)
|
|
33
|
+#define BLE_CHAR_TIMEOUT_MS (3 * 2000)
|
|
34
|
+#define BLE_WRTE_TIMEOUT_MS (3 * 500)
|
35
|
35
|
#define BLE_MAX_SCAN_AGE_MS (10 * 1000)
|
36
|
36
|
#define BLE_MAX_SERVICES 8
|
37
|
37
|
#define BLE_MAX_CHARACTERISTICS 8
|
|
@@ -576,16 +576,7 @@ int32_t ble_read(const uint8_t *characteristic, uint8_t *buff, uint16_t buff_len
|
576
|
576
|
return read_len;
|
577
|
577
|
}
|
578
|
578
|
|
579
|
|
-int8_t ble_write(const uint8_t *service, const uint8_t *characteristic,
|
580
|
|
- uint8_t *buff, uint16_t buff_len) {
|
581
|
|
- cyw43_thread_enter();
|
582
|
|
-
|
583
|
|
- if (state != TC_READY) {
|
584
|
|
- cyw43_thread_exit();
|
585
|
|
- debug("invalid state for write (%d)", state);
|
586
|
|
- return -1;
|
587
|
|
- }
|
588
|
|
-
|
|
579
|
+static int discover_service(const uint8_t *service) {
|
589
|
580
|
// check if service has already been discovered
|
590
|
581
|
int srvc = -1, free_srvc = -1;
|
591
|
582
|
for (int i = 0; i < BLE_MAX_SERVICES; i++) {
|
|
@@ -652,6 +643,10 @@ int8_t ble_write(const uint8_t *service, const uint8_t *characteristic,
|
652
|
643
|
cyw43_thread_enter();
|
653
|
644
|
}
|
654
|
645
|
|
|
646
|
+ return srvc;
|
|
647
|
+}
|
|
648
|
+
|
|
649
|
+static int discover_characteristic(int srvc, const uint8_t *characteristic) {
|
655
|
650
|
// check if characteristic has already been discovered
|
656
|
651
|
int ch = -1, free_ch = -1;
|
657
|
652
|
for (int i = 0; i < BLE_MAX_CHARACTERISTICS; i++) {
|
|
@@ -719,6 +714,31 @@ int8_t ble_write(const uint8_t *service, const uint8_t *characteristic,
|
719
|
714
|
cyw43_thread_enter();
|
720
|
715
|
}
|
721
|
716
|
|
|
717
|
+ return ch;
|
|
718
|
+}
|
|
719
|
+
|
|
720
|
+int8_t ble_write(const uint8_t *service, const uint8_t *characteristic,
|
|
721
|
+ const uint8_t *buff, uint16_t buff_len) {
|
|
722
|
+ cyw43_thread_enter();
|
|
723
|
+
|
|
724
|
+ if (state != TC_READY) {
|
|
725
|
+ cyw43_thread_exit();
|
|
726
|
+ debug("invalid state for write (%d)", state);
|
|
727
|
+ return -1;
|
|
728
|
+ }
|
|
729
|
+
|
|
730
|
+ int srvc = discover_service(service);
|
|
731
|
+ if (srvc < 0) {
|
|
732
|
+ debug("error discovering service (%d)", srvc);
|
|
733
|
+ return srvc;
|
|
734
|
+ }
|
|
735
|
+
|
|
736
|
+ int ch = discover_characteristic(srvc, characteristic);
|
|
737
|
+ if (ch < 0) {
|
|
738
|
+ debug("error discovering characteristic (%d)", ch);
|
|
739
|
+ return ch;
|
|
740
|
+ }
|
|
741
|
+
|
722
|
742
|
if (buff_len > BLE_MAX_VALUE_LEN) {
|
723
|
743
|
buff_len = BLE_MAX_VALUE_LEN;
|
724
|
744
|
}
|
|
@@ -768,3 +788,28 @@ int8_t ble_write(const uint8_t *service, const uint8_t *characteristic,
|
768
|
788
|
cyw43_thread_exit();
|
769
|
789
|
return ret;
|
770
|
790
|
}
|
|
791
|
+
|
|
792
|
+int8_t ble_discover(const uint8_t *service, const uint8_t *characteristic) {
|
|
793
|
+ cyw43_thread_enter();
|
|
794
|
+
|
|
795
|
+ if (state != TC_READY) {
|
|
796
|
+ cyw43_thread_exit();
|
|
797
|
+ debug("invalid state for discovery (%d)", state);
|
|
798
|
+ return -1;
|
|
799
|
+ }
|
|
800
|
+
|
|
801
|
+ int srvc = discover_service(service);
|
|
802
|
+ if (srvc < 0) {
|
|
803
|
+ debug("error discovering service (%d)", srvc);
|
|
804
|
+ return srvc;
|
|
805
|
+ }
|
|
806
|
+
|
|
807
|
+ int ch = discover_characteristic(srvc, characteristic);
|
|
808
|
+ if (ch < 0) {
|
|
809
|
+ debug("error discovering characteristic (%d)", ch);
|
|
810
|
+ return ch;
|
|
811
|
+ }
|
|
812
|
+
|
|
813
|
+ cyw43_thread_exit();
|
|
814
|
+ return 0;
|
|
815
|
+}
|