Program is unlisted and not meant for wide distribution. It doesn't contain any confidential information, but could use polishing and commenting

Committer:
JyriLehtinen
Date:
Thu Jul 07 11:53:23 2016 +0000
Revision:
0:14ce95b48075
Child:
1:71bd3e921d6c
Working revision of voltage and current characteristics for ST BlueMS

Who changed what in which revision?

UserRevisionLine numberNew contents of line
JyriLehtinen 0:14ce95b48075 1 #ifndef SENSOR_SERVICE_H
JyriLehtinen 0:14ce95b48075 2 #define SENSOR_SERVICE_H
JyriLehtinen 0:14ce95b48075 3
JyriLehtinen 0:14ce95b48075 4 #include "BLE.h"
JyriLehtinen 0:14ce95b48075 5 #include "UUID.h"
JyriLehtinen 0:14ce95b48075 6 #include "Utils.h"
JyriLehtinen 0:14ce95b48075 7
JyriLehtinen 0:14ce95b48075 8 const UUID::LongUUIDBytes_t SENS_SERVICE_UUID = {0x00,0x00,0x00,0x00,0x00,0x01,0x11,0xe1,0x9a,0xb4,0x00,0x02,0xa5,0xd5,0xc5,0x1b}; //Primary service
JyriLehtinen 0:14ce95b48075 9 const UUID::LongUUIDBytes_t VOLT_CHAR_UUID = {0x40,0x00,0x00,0x00,0x00,0x01,0x11,0xe1,0xac,0x36,0x00,0x02,0xa5,0xd5,0xc5,0x1b}; //Voltage characteristic
JyriLehtinen 0:14ce95b48075 10 const UUID::LongUUIDBytes_t CURR_CHAR_UUID = {0x08,0x00,0x00,0x00,0x00,0x01,0x11,0xe1,0xac,0x36,0x00,0x02,0xa5,0xd5,0xc5,0x1b}; //Current characteristic
JyriLehtinen 0:14ce95b48075 11
JyriLehtinen 0:14ce95b48075 12 #define VOLT_DATA_LEN 2+4 //TimeStamp(uint16_t) + VoltageData(uint32_t)
JyriLehtinen 0:14ce95b48075 13 #define CURR_DATA_LEN 2+4 //TimeStamp(uint16_t) + VoltageData(uint32_t)
JyriLehtinen 0:14ce95b48075 14
JyriLehtinen 0:14ce95b48075 15 class SensorService
JyriLehtinen 0:14ce95b48075 16 {
JyriLehtinen 0:14ce95b48075 17 public:
JyriLehtinen 0:14ce95b48075 18 SensorService(BLE &_ble) :
JyriLehtinen 0:14ce95b48075 19 ble(_ble),
JyriLehtinen 0:14ce95b48075 20 voltage(), //six byte array
JyriLehtinen 0:14ce95b48075 21 voltageCharacteristic(VOLT_CHAR_UUID, voltage, VOLT_DATA_LEN, VOLT_DATA_LEN, GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY),
JyriLehtinen 0:14ce95b48075 22 currentCharacteristic(CURR_CHAR_UUID, current, CURR_DATA_LEN, CURR_DATA_LEN, GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY)
JyriLehtinen 0:14ce95b48075 23 {
JyriLehtinen 0:14ce95b48075 24 static bool serviceAdded = false; //To be sure to define just once
JyriLehtinen 0:14ce95b48075 25 if(serviceAdded)
JyriLehtinen 0:14ce95b48075 26 { return; }
JyriLehtinen 0:14ce95b48075 27 GattCharacteristic *charTable[] = {&voltageCharacteristic, &currentCharacteristic};
JyriLehtinen 0:14ce95b48075 28 GattService sensorService(SENS_SERVICE_UUID, charTable, sizeof(charTable)/sizeof (GattCharacteristic *));
JyriLehtinen 0:14ce95b48075 29
JyriLehtinen 0:14ce95b48075 30 ble.addService(sensorService);
JyriLehtinen 0:14ce95b48075 31 memset(voltage, 0, VOLT_DATA_LEN);
JyriLehtinen 0:14ce95b48075 32 memset(current, 0, CURR_DATA_LEN);
JyriLehtinen 0:14ce95b48075 33 serviceAdded = true;
JyriLehtinen 0:14ce95b48075 34 }
JyriLehtinen 0:14ce95b48075 35
JyriLehtinen 0:14ce95b48075 36 //Voltage GATT server functions
JyriLehtinen 0:14ce95b48075 37 void sendVoltage (uint32_t volt_value, uint16_t TimeStamp)
JyriLehtinen 0:14ce95b48075 38 {
JyriLehtinen 0:14ce95b48075 39 STORE_LE_16(voltage, TimeStamp);
JyriLehtinen 0:14ce95b48075 40 STORE_LE_32(voltage+2, volt_value);
JyriLehtinen 0:14ce95b48075 41 uint16_t volt_handle = voltageCharacteristic.getValueAttribute().getHandle();
JyriLehtinen 0:14ce95b48075 42 //printf("Send voltage handle: %d\n", volt_handle);
JyriLehtinen 0:14ce95b48075 43 memcpy(pastVoltage, voltage, VOLT_DATA_LEN);
JyriLehtinen 0:14ce95b48075 44 ble.gattServer().write(volt_handle, voltage, VOLT_DATA_LEN, 0);
JyriLehtinen 0:14ce95b48075 45 //printf("Update\n");
JyriLehtinen 0:14ce95b48075 46 }
JyriLehtinen 0:14ce95b48075 47
JyriLehtinen 0:14ce95b48075 48 void updateVoltage(uint32_t volt_value, uint16_t TimeStamp)
JyriLehtinen 0:14ce95b48075 49 {
JyriLehtinen 0:14ce95b48075 50 if(ble.getGapState().connected && isEnabledVoltageNotify)
JyriLehtinen 0:14ce95b48075 51 {
JyriLehtinen 0:14ce95b48075 52 if(memcmp(&pastVoltage[2], &volt_value, 4) != 0)
JyriLehtinen 0:14ce95b48075 53 {
JyriLehtinen 0:14ce95b48075 54 sendVoltage(volt_value, TimeStamp);
JyriLehtinen 0:14ce95b48075 55 }
JyriLehtinen 0:14ce95b48075 56 }
JyriLehtinen 0:14ce95b48075 57 }
JyriLehtinen 0:14ce95b48075 58
JyriLehtinen 0:14ce95b48075 59 //Current GATT server functions
JyriLehtinen 0:14ce95b48075 60 void sendCurrent (uint32_t curr_value, uint16_t TimeStamp)
JyriLehtinen 0:14ce95b48075 61 {
JyriLehtinen 0:14ce95b48075 62 STORE_LE_16(current, TimeStamp);
JyriLehtinen 0:14ce95b48075 63 STORE_LE_32(current+2, curr_value);
JyriLehtinen 0:14ce95b48075 64 uint16_t curr_handle = currentCharacteristic.getValueAttribute().getHandle();
JyriLehtinen 0:14ce95b48075 65 //printf("Send voltage handle: %d\n", volt_handle);
JyriLehtinen 0:14ce95b48075 66 memcpy(pastCurrent, current, CURR_DATA_LEN);
JyriLehtinen 0:14ce95b48075 67 ble.gattServer().write(curr_handle, current, CURR_DATA_LEN, 0);
JyriLehtinen 0:14ce95b48075 68 //printf("Update\n");
JyriLehtinen 0:14ce95b48075 69 }
JyriLehtinen 0:14ce95b48075 70
JyriLehtinen 0:14ce95b48075 71 void updateCurrent(uint32_t curr_value, uint16_t TimeStamp)
JyriLehtinen 0:14ce95b48075 72 {
JyriLehtinen 0:14ce95b48075 73 if(ble.getGapState().connected && isEnabledCurrentNotify)
JyriLehtinen 0:14ce95b48075 74 {
JyriLehtinen 0:14ce95b48075 75 if(memcmp(&pastCurrent[2], &curr_value, 4) != 0)
JyriLehtinen 0:14ce95b48075 76 {
JyriLehtinen 0:14ce95b48075 77 sendCurrent(curr_value, TimeStamp);
JyriLehtinen 0:14ce95b48075 78 }
JyriLehtinen 0:14ce95b48075 79 }
JyriLehtinen 0:14ce95b48075 80 }
JyriLehtinen 0:14ce95b48075 81
JyriLehtinen 0:14ce95b48075 82
JyriLehtinen 0:14ce95b48075 83 void enNotify (Gap::Handle_t handle)
JyriLehtinen 0:14ce95b48075 84 {
JyriLehtinen 0:14ce95b48075 85 // printf("\n\r\n\renNotify, Handle: 0x%02X\n", handle);
JyriLehtinen 0:14ce95b48075 86 if (isVoltageHandle(handle)) { isEnabledVoltageNotify = true; memset(voltage,0,VOLT_DATA_LEN); return; }
JyriLehtinen 0:14ce95b48075 87 if (isCurrentHandle(handle)) { isEnabledCurrentNotify = true; memset(current,0,CURR_DATA_LEN); return; }
JyriLehtinen 0:14ce95b48075 88 }
JyriLehtinen 0:14ce95b48075 89
JyriLehtinen 0:14ce95b48075 90 void disNotify (Gap::Handle_t handle)
JyriLehtinen 0:14ce95b48075 91 {
JyriLehtinen 0:14ce95b48075 92 // printf("\n\r\n\rdisNotify, Handle: 0x%02X\n", handle);
JyriLehtinen 0:14ce95b48075 93 if (isVoltageHandle(handle)) { isEnabledVoltageNotify = false; memset(voltage,0,VOLT_DATA_LEN); return; }
JyriLehtinen 0:14ce95b48075 94 if (isCurrentHandle(handle)) { isEnabledCurrentNotify = false; memset(current,0,CURR_DATA_LEN); return; }
JyriLehtinen 0:14ce95b48075 95 }
JyriLehtinen 0:14ce95b48075 96
JyriLehtinen 0:14ce95b48075 97 //----
JyriLehtinen 0:14ce95b48075 98
JyriLehtinen 0:14ce95b48075 99 bool isVoltageNotificationEn (void)
JyriLehtinen 0:14ce95b48075 100 {
JyriLehtinen 0:14ce95b48075 101 return isEnabledVoltageNotify;
JyriLehtinen 0:14ce95b48075 102 }
JyriLehtinen 0:14ce95b48075 103
JyriLehtinen 0:14ce95b48075 104
JyriLehtinen 0:14ce95b48075 105 bool isVoltageHandle(Gap::Handle_t handle)
JyriLehtinen 0:14ce95b48075 106 {
JyriLehtinen 0:14ce95b48075 107 if(handle == voltageCharacteristic.getValueAttribute().getHandle())
JyriLehtinen 0:14ce95b48075 108 { return true; }
JyriLehtinen 0:14ce95b48075 109 return false;
JyriLehtinen 0:14ce95b48075 110 }
JyriLehtinen 0:14ce95b48075 111
JyriLehtinen 0:14ce95b48075 112 bool isCurrentNotificationEn (void)
JyriLehtinen 0:14ce95b48075 113 {
JyriLehtinen 0:14ce95b48075 114 return isEnabledCurrentNotify;
JyriLehtinen 0:14ce95b48075 115 }
JyriLehtinen 0:14ce95b48075 116
JyriLehtinen 0:14ce95b48075 117
JyriLehtinen 0:14ce95b48075 118 bool isCurrentHandle(Gap::Handle_t handle)
JyriLehtinen 0:14ce95b48075 119 {
JyriLehtinen 0:14ce95b48075 120 if(handle == currentCharacteristic.getValueAttribute().getHandle())
JyriLehtinen 0:14ce95b48075 121 { return true; }
JyriLehtinen 0:14ce95b48075 122 return false;
JyriLehtinen 0:14ce95b48075 123 }
JyriLehtinen 0:14ce95b48075 124
JyriLehtinen 0:14ce95b48075 125 protected:
JyriLehtinen 0:14ce95b48075 126 BLE &ble;
JyriLehtinen 0:14ce95b48075 127 uint8_t voltage[VOLT_DATA_LEN];
JyriLehtinen 0:14ce95b48075 128 uint8_t pastVoltage[VOLT_DATA_LEN];
JyriLehtinen 0:14ce95b48075 129 uint8_t current[CURR_DATA_LEN];
JyriLehtinen 0:14ce95b48075 130 uint8_t pastCurrent[CURR_DATA_LEN];
JyriLehtinen 0:14ce95b48075 131
JyriLehtinen 0:14ce95b48075 132 GattCharacteristic voltageCharacteristic;
JyriLehtinen 0:14ce95b48075 133 GattCharacteristic currentCharacteristic;
JyriLehtinen 0:14ce95b48075 134
JyriLehtinen 0:14ce95b48075 135 bool isEnabledVoltageNotify;
JyriLehtinen 0:14ce95b48075 136 bool isEnabledCurrentNotify;
JyriLehtinen 0:14ce95b48075 137 };
JyriLehtinen 0:14ce95b48075 138
JyriLehtinen 0:14ce95b48075 139 #endif