Browse Source

associate ble notifications with characteristics

Thomas Buck 11 months ago
parent
commit
a1d8ad6002
2 changed files with 42 additions and 12 deletions
  1. 1
    1
      include/ble.h
  2. 41
    11
      src/ble.c

+ 1
- 1
include/ble.h View File

63
 int8_t ble_notification_disable(const uint8_t *service, const uint8_t *characteristic);
63
 int8_t ble_notification_disable(const uint8_t *service, const uint8_t *characteristic);
64
 int8_t ble_notification_enable(const uint8_t *service, const uint8_t *characteristic);
64
 int8_t ble_notification_enable(const uint8_t *service, const uint8_t *characteristic);
65
 bool ble_notification_ready(void);
65
 bool ble_notification_ready(void);
66
-uint16_t ble_notification_get(uint8_t *buff, uint16_t buff_len);
66
+uint16_t ble_notification_get(uint8_t *buff, uint16_t buff_len, uint8_t *characteristic);
67
 
67
 
68
 #endif // __BLE_H__
68
 #endif // __BLE_H__

+ 41
- 11
src/ble.c View File

73
 
73
 
74
 static uint16_t read_len = 0;
74
 static uint16_t read_len = 0;
75
 static uint8_t data_buff[BLE_MAX_VALUE_LEN] = {0};
75
 static uint8_t data_buff[BLE_MAX_VALUE_LEN] = {0};
76
+static uint16_t value_handle = 0;
76
 
77
 
77
 static struct ble_service services[BLE_MAX_SERVICES] = {0};
78
 static struct ble_service services[BLE_MAX_SERVICES] = {0};
78
 static uint8_t service_idx = 0;
79
 static uint8_t service_idx = 0;
219
 
220
 
220
             case BLUETOOTH_DATA_TYPE_SERVICE_DATA:
221
             case BLUETOOTH_DATA_TYPE_SERVICE_DATA:
221
             case BLUETOOTH_DATA_TYPE_MANUFACTURER_SPECIFIC_DATA:
222
             case BLUETOOTH_DATA_TYPE_MANUFACTURER_SPECIFIC_DATA:
222
-                // TODO ugly
223
-                if (data_size == 12) {
224
-                    // Crafty+
225
-                    hci_scan_result_add_data(addr, data, data_size);
226
-                } else if (data_size == 26) {
227
-                    // Volcano
228
-                    hci_scan_result_add_data(addr, data, data_size);
229
-                }
223
+                hci_scan_result_add_data(addr, data, data_size);
230
                 break;
224
                 break;
231
 
225
 
232
             default:
226
             default:
343
 
337
 
344
         uint16_t value_length = gatt_event_notification_get_value_length(packet);
338
         uint16_t value_length = gatt_event_notification_get_value_length(packet);
345
         const uint8_t *value = gatt_event_notification_get_value(packet);
339
         const uint8_t *value = gatt_event_notification_get_value(packet);
340
+        value_handle = gatt_event_notification_get_value_handle(packet);
346
         if ((read_len + value_length) <= BLE_MAX_VALUE_LEN) {
341
         if ((read_len + value_length) <= BLE_MAX_VALUE_LEN) {
347
             memcpy(data_buff + read_len, value, value_length);
342
             memcpy(data_buff + read_len, value, value_length);
348
             read_len += value_length;
343
             read_len += value_length;
951
 
946
 
952
 }
947
 }
953
 
948
 
954
-uint16_t ble_notification_get(uint8_t *buff, uint16_t buff_len) {
949
+uint16_t ble_notification_get(uint8_t *buff, uint16_t buff_len, uint8_t *characteristic) {
950
+    if ((buff == NULL) || (characteristic == NULL) || (buff_len <= 0)) {
951
+        debug("invalid params");
952
+        return -1;
953
+    }
954
+
955
     cyw43_thread_enter();
955
     cyw43_thread_enter();
956
 
956
 
957
     if (state != TC_READY) {
957
     if (state != TC_READY) {
958
         cyw43_thread_exit();
958
         cyw43_thread_exit();
959
         debug("invalid state for notify (%d)", state);
959
         debug("invalid state for notify (%d)", state);
960
-        return -1;
960
+        return -2;
961
+    }
962
+
963
+    if (read_len <= 0) {
964
+        debug("no data available");
965
+        cyw43_thread_exit();
966
+        return -3;
961
     }
967
     }
962
 
968
 
963
     if (read_len > buff_len) {
969
     if (read_len > buff_len) {
964
         debug("buffer too short (%d < %d)", buff_len, read_len);
970
         debug("buffer too short (%d < %d)", buff_len, read_len);
965
         cyw43_thread_exit();
971
         cyw43_thread_exit();
966
-        return -2;
972
+        return -4;
967
     }
973
     }
968
 
974
 
969
     memcpy(buff, data_buff, read_len);
975
     memcpy(buff, data_buff, read_len);
970
 
976
 
977
+    bool found = false;
978
+    for (int i = 0; i < BLE_MAX_SERVICES; i++) {
979
+        if (!services[i].set) {
980
+            continue;
981
+        }
982
+
983
+        for (int j = 0; j < BLE_MAX_CHARACTERISTICS; j++) {
984
+            if (!services[i].chars[j].set) {
985
+                continue;
986
+            }
987
+
988
+            if (services[i].chars[j].c.value_handle == value_handle) {
989
+                memcpy(characteristic, services[i].chars[j].c.uuid128, 16);
990
+                found = true;
991
+                break;
992
+            }
993
+        }
994
+    }
995
+
996
+    if (!found) {
997
+        debug("can not find characteristic for value handle 0x%04X", value_handle);
998
+        memset(characteristic, 0, 16);
999
+    }
1000
+
971
     uint16_t tmp = read_len;
1001
     uint16_t tmp = read_len;
972
     read_len = 0;
1002
     read_len = 0;
973
 
1003
 

Loading…
Cancel
Save