Program is unlisted and not meant for wide distribution. It doesn't contain any confidential information, but could use polishing and commenting
SensorService.h@1:71bd3e921d6c, 2016-10-14 (annotated)
- Committer:
- JyriLehtinen
- Date:
- Fri Oct 14 07:57:41 2016 +0000
- Revision:
- 1:71bd3e921d6c
- Parent:
- 0:14ce95b48075
I can't rember what's done, this wasn't meant to be published;
Who changed what in which revision?
| User | Revision | Line number | New 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 | 1:71bd3e921d6c | 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, ¤tCharacteristic}; |
| 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 | 1:71bd3e921d6c | 39 | memcpy(&voltage, &TimeStamp, 2); |
| JyriLehtinen | 1:71bd3e921d6c | 40 | memcpy(&voltage+2, &volt_value, 4); |
| JyriLehtinen | 0:14ce95b48075 | 41 | uint16_t volt_handle = voltageCharacteristic.getValueAttribute().getHandle(); |
| JyriLehtinen | 1:71bd3e921d6c | 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 | 1:71bd3e921d6c | 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 | 1:71bd3e921d6c | 62 | memcpy(¤t, &TimeStamp, 2); |
| JyriLehtinen | 1:71bd3e921d6c | 63 | memcpy(¤t+2, &curr_value, 4); |
| JyriLehtinen | 0:14ce95b48075 | 64 | uint16_t curr_handle = currentCharacteristic.getValueAttribute().getHandle(); |
| JyriLehtinen | 1:71bd3e921d6c | 65 | printf("Send current handle: %d\n", curr_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 | 1:71bd3e921d6c | 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 | 1:71bd3e921d6c | 86 | if (isVoltageHandle(handle)) |
| JyriLehtinen | 1:71bd3e921d6c | 87 | { |
| JyriLehtinen | 1:71bd3e921d6c | 88 | isEnabledVoltageNotify = true; |
| JyriLehtinen | 1:71bd3e921d6c | 89 | memset(voltage,0,VOLT_DATA_LEN); |
| JyriLehtinen | 1:71bd3e921d6c | 90 | memset(pastVoltage, 0, VOLT_DATA_LEN); |
| JyriLehtinen | 1:71bd3e921d6c | 91 | return; |
| JyriLehtinen | 1:71bd3e921d6c | 92 | } |
| JyriLehtinen | 1:71bd3e921d6c | 93 | |
| JyriLehtinen | 1:71bd3e921d6c | 94 | if (isCurrentHandle(handle)) |
| JyriLehtinen | 1:71bd3e921d6c | 95 | { |
| JyriLehtinen | 1:71bd3e921d6c | 96 | isEnabledCurrentNotify = true; |
| JyriLehtinen | 1:71bd3e921d6c | 97 | memset(current,0,CURR_DATA_LEN); |
| JyriLehtinen | 1:71bd3e921d6c | 98 | memset(pastCurrent, 0, CURR_DATA_LEN); |
| JyriLehtinen | 1:71bd3e921d6c | 99 | return; |
| JyriLehtinen | 1:71bd3e921d6c | 100 | } |
| JyriLehtinen | 0:14ce95b48075 | 101 | } |
| JyriLehtinen | 0:14ce95b48075 | 102 | |
| JyriLehtinen | 0:14ce95b48075 | 103 | void disNotify (Gap::Handle_t handle) |
| JyriLehtinen | 0:14ce95b48075 | 104 | { |
| JyriLehtinen | 0:14ce95b48075 | 105 | // printf("\n\r\n\rdisNotify, Handle: 0x%02X\n", handle); |
| JyriLehtinen | 0:14ce95b48075 | 106 | if (isVoltageHandle(handle)) { isEnabledVoltageNotify = false; memset(voltage,0,VOLT_DATA_LEN); return; } |
| JyriLehtinen | 0:14ce95b48075 | 107 | if (isCurrentHandle(handle)) { isEnabledCurrentNotify = false; memset(current,0,CURR_DATA_LEN); return; } |
| JyriLehtinen | 0:14ce95b48075 | 108 | } |
| JyriLehtinen | 0:14ce95b48075 | 109 | |
| JyriLehtinen | 0:14ce95b48075 | 110 | //---- |
| JyriLehtinen | 0:14ce95b48075 | 111 | |
| JyriLehtinen | 0:14ce95b48075 | 112 | bool isVoltageNotificationEn (void) |
| JyriLehtinen | 0:14ce95b48075 | 113 | { |
| JyriLehtinen | 0:14ce95b48075 | 114 | return isEnabledVoltageNotify; |
| JyriLehtinen | 0:14ce95b48075 | 115 | } |
| JyriLehtinen | 0:14ce95b48075 | 116 | |
| JyriLehtinen | 0:14ce95b48075 | 117 | |
| JyriLehtinen | 0:14ce95b48075 | 118 | bool isVoltageHandle(Gap::Handle_t handle) |
| JyriLehtinen | 0:14ce95b48075 | 119 | { |
| JyriLehtinen | 0:14ce95b48075 | 120 | if(handle == voltageCharacteristic.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 | bool isCurrentNotificationEn (void) |
| JyriLehtinen | 0:14ce95b48075 | 126 | { |
| JyriLehtinen | 0:14ce95b48075 | 127 | return isEnabledCurrentNotify; |
| JyriLehtinen | 0:14ce95b48075 | 128 | } |
| JyriLehtinen | 0:14ce95b48075 | 129 | |
| JyriLehtinen | 0:14ce95b48075 | 130 | |
| JyriLehtinen | 0:14ce95b48075 | 131 | bool isCurrentHandle(Gap::Handle_t handle) |
| JyriLehtinen | 0:14ce95b48075 | 132 | { |
| JyriLehtinen | 0:14ce95b48075 | 133 | if(handle == currentCharacteristic.getValueAttribute().getHandle()) |
| JyriLehtinen | 0:14ce95b48075 | 134 | { return true; } |
| JyriLehtinen | 0:14ce95b48075 | 135 | return false; |
| JyriLehtinen | 0:14ce95b48075 | 136 | } |
| JyriLehtinen | 0:14ce95b48075 | 137 | |
| JyriLehtinen | 0:14ce95b48075 | 138 | protected: |
| JyriLehtinen | 0:14ce95b48075 | 139 | BLE &ble; |
| JyriLehtinen | 0:14ce95b48075 | 140 | uint8_t voltage[VOLT_DATA_LEN]; |
| JyriLehtinen | 0:14ce95b48075 | 141 | uint8_t pastVoltage[VOLT_DATA_LEN]; |
| JyriLehtinen | 0:14ce95b48075 | 142 | uint8_t current[CURR_DATA_LEN]; |
| JyriLehtinen | 0:14ce95b48075 | 143 | uint8_t pastCurrent[CURR_DATA_LEN]; |
| JyriLehtinen | 0:14ce95b48075 | 144 | |
| JyriLehtinen | 0:14ce95b48075 | 145 | GattCharacteristic voltageCharacteristic; |
| JyriLehtinen | 0:14ce95b48075 | 146 | GattCharacteristic currentCharacteristic; |
| JyriLehtinen | 0:14ce95b48075 | 147 | |
| JyriLehtinen | 0:14ce95b48075 | 148 | bool isEnabledVoltageNotify; |
| JyriLehtinen | 0:14ce95b48075 | 149 | bool isEnabledCurrentNotify; |
| JyriLehtinen | 0:14ce95b48075 | 150 | }; |
| JyriLehtinen | 0:14ce95b48075 | 151 | |
| JyriLehtinen | 0:14ce95b48075 | 152 | #endif |