ST / Mbed OS Node_BLE_Sensors_Device

Dependencies:   X_NUCLEO_IKS01A2

Fork of Node_BLE_Switch_Device by ST

Bluetooth Low Energy enabled device with environmental and inertial features, i.e. "Pressure", "Humidity", "Temperature", "Accelerometer", "Gyroscope", and "Magnetometer", plus "Switch" feature, compatible with BlueST Protocol.

Revision:
4:a10e2c94c8c8
Parent:
0:6b34c49b5285
Child:
5:445024130101
--- a/source/CustomService.h	Thu May 10 11:59:17 2018 +0000
+++ b/source/CustomService.h	Wed May 16 17:20:48 2018 +0000
@@ -43,12 +43,20 @@
 
 #include "ble_utils.h"
 
-#define STATE_DATA_LENGTH   (sizeof(uint16_t) + sizeof(uint8_t))
-#define COMMAND_DATA_LENGTH (sizeof(uint8_t))
-#define MAX_DATA_LENGTH     (STATE_DATA_LENGTH)
+#define TIMESTAMP_LENGTH    (sizeof(uint16_t))
+#define STATE_LENGTH        (sizeof(uint8_t))
+#define COMMAND_LENGTH      (sizeof(uint8_t))
+#define PRESSURE_LENGTH     (sizeof(uint32_t))
+#define HUMIDITY_LENGTH     (sizeof(uint16_t))
+#define TEMPERATURE_LENGTH  (sizeof(uint16_t))
 
-const UUID::LongUUIDBytes_t CUSTOM_SWITCH_SERVICE_UUID        = {0x00,0x00,0x00,0x00,0x00,0x01,0x11,0xe1,0xac,0x36,0x00,0x02,0xa5,0xd5,0xc5,0x1b};
-const UUID::LongUUIDBytes_t CUSTOM_SWITCH_CHARACTERISTIC_UUID = {0x20,0x00,0x00,0x00,0x00,0x01,0x11,0xe1,0xac,0x36,0x00,0x02,0xa5,0xd5,0xc5,0x1b};
+#define STATE_DATA_LENGTH   (TIMESTAMP_LENGTH + STATE_LENGTH)
+#define COMMAND_DATA_LENGTH (COMMAND_LENGTH)
+#define SENSORS_DATA_LENGTH (TIMESTAMP_LENGTH + PRESSURE_LENGTH + HUMIDITY_LENGTH + TEMPERATURE_LENGTH)
+
+const UUID::LongUUIDBytes_t CUSTOM_SERVICE_UUID                = {0x00,0x00,0x00,0x00,0x00,0x01,0x11,0xe1,0xac,0x36,0x00,0x02,0xa5,0xd5,0xc5,0x1b};
+const UUID::LongUUIDBytes_t CUSTOM_SWITCH_CHARACTERISTIC_UUID  = {0x20,0x00,0x00,0x00,0x00,0x01,0x11,0xe1,0xac,0x36,0x00,0x02,0xa5,0xd5,0xc5,0x1b};
+const UUID::LongUUIDBytes_t CUSTOM_SENSORS_CHARACTERISTIC_UUID = {0x00,0x1c,0x00,0x00,0x00,0x01,0x11,0xe1,0xac,0x36,0x00,0x02,0xa5,0xd5,0xc5,0x1b};
 
 class CustomService {
 public:
@@ -62,31 +70,51 @@
 
     CustomService(BLEDevice &_ble) :
         ble(_ble),
-        state_command(CUSTOM_SWITCH_CHARACTERISTIC_UUID, packed_state_command, MAX_DATA_LENGTH, MAX_DATA_LENGTH,
-            /*GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ | */GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY)
+        state_command(CUSTOM_SWITCH_CHARACTERISTIC_UUID, packed_state_command, STATE_DATA_LENGTH, STATE_DATA_LENGTH,
+            /*GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ | */GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY),
+        sensors(CUSTOM_SENSORS_CHARACTERISTIC_UUID, packed_sensors, SENSORS_DATA_LENGTH, SENSORS_DATA_LENGTH,
+            GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY)
     {
-        GattCharacteristic *char_table[] = {&state_command};
-        GattService switch_service(CUSTOM_SWITCH_SERVICE_UUID, char_table, sizeof(char_table) / sizeof(GattCharacteristic *));
+        GattCharacteristic *char_table[] = {&state_command, &sensors};
+        GattService switch_service(CUSTOM_SERVICE_UUID, char_table, sizeof(char_table) / sizeof(GattCharacteristic *));
         ble.addService(switch_service);
-        memset (packed_state_command, 0, MAX_DATA_LENGTH);
+        memset (packed_state_command, 0, STATE_DATA_LENGTH);
+        memset (packed_sensors, 0, SENSORS_DATA_LENGTH);
     }
 
     void send_state(uint16_t time_stamp, uint8_t current_state) {
-        memset (packed_state_command, 0, MAX_DATA_LENGTH);
+        memset (packed_state_command, 0, STATE_DATA_LENGTH);
         STORE_LE_16(packed_state_command, time_stamp);
-        packed_state_command[2] = current_state;
-        ble.gattServer().write(state_command.getValueAttribute().getHandle(), (uint8_t *) &packed_state_command, STATE_DATA_LENGTH, 0);
+        packed_state_command[TIMESTAMP_LENGTH] = current_state;
+        ble.gattServer().write(get_state_command_handle(), (uint8_t *) &packed_state_command, STATE_DATA_LENGTH, 0);
     }
 
-    GattAttribute::Handle_t getValueHandle() const
+    void send_sensors(uint16_t time_stamp, uint32_t pressure, uint16_t humidity, uint16_t temperature) {
+        memset (packed_sensors, 0, SENSORS_DATA_LENGTH);
+        int p = 0;
+        STORE_LE_16(&packed_sensors[p], time_stamp);
+        STORE_LE_32(&packed_sensors[p += TIMESTAMP_LENGTH], pressure);
+        STORE_LE_16(&packed_sensors[p += PRESSURE_LENGTH], humidity);
+        STORE_LE_16(&packed_sensors[p += HUMIDITY_LENGTH], temperature);
+        ble.gattServer().write(get_sensors_handle(), (uint8_t *) &packed_sensors, SENSORS_DATA_LENGTH, 0);
+    }
+
+    GattAttribute::Handle_t get_state_command_handle() const
     {
         return state_command.getValueAttribute().getHandle();
     }
 
+    GattAttribute::Handle_t get_sensors_handle() const
+    {
+        return sensors.getValueAttribute().getHandle();
+    }
+
 private:
     BLEDevice &ble;
     GattCharacteristic state_command;
-    uint8_t packed_state_command[MAX_DATA_LENGTH];
+    GattCharacteristic sensors;
+    uint8_t packed_state_command[STATE_DATA_LENGTH];
+    uint8_t packed_sensors[SENSORS_DATA_LENGTH];
 };
 
 #endif /* #ifndef __BLE_CUSTOM_SERVICE_H__ */