We are making a bluetooth application for a vehicle.
Dependencies: BLE_API X_NUCLEO_IDB0XA1 mbed
Fork of BLE_HeartRate_IDB0XA1 by
bike_service.h@30:243e095a69d9, 2017-07-19 (annotated)
- Committer:
- bobbaddeley
- Date:
- Wed Jul 19 14:38:04 2017 +0000
- Revision:
- 30:243e095a69d9
- Parent:
- 29:e7d4922a4620
Getting alarms to work
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
bobbaddeley | 23:a31b178e2263 | 1 | /* Bike Service |
bobbaddeley | 23:a31b178e2263 | 2 | */ |
bobbaddeley | 23:a31b178e2263 | 3 | |
bobbaddeley | 23:a31b178e2263 | 4 | #ifndef __BLE_BIKE_SERVICE_H__ |
bobbaddeley | 23:a31b178e2263 | 5 | #define __BLE_BIKE_SERVICE_H__ |
bobbaddeley | 23:a31b178e2263 | 6 | |
bobbaddeley | 23:a31b178e2263 | 7 | #include "ble/BLE.h" |
bobbaddeley | 23:a31b178e2263 | 8 | |
bobbaddeley | 23:a31b178e2263 | 9 | /** |
bobbaddeley | 23:a31b178e2263 | 10 | * @class BikeService |
bobbaddeley | 23:a31b178e2263 | 11 | * @brief BLE Service for Heck Bike. |
bobbaddeley | 23:a31b178e2263 | 12 | */ |
bobbaddeley | 23:a31b178e2263 | 13 | class BikeService { |
bobbaddeley | 23:a31b178e2263 | 14 | public: |
bobbaddeley | 23:a31b178e2263 | 15 | /** |
bobbaddeley | 23:a31b178e2263 | 16 | * @brief Constructor with 8-bit HRM Counter value. |
bobbaddeley | 23:a31b178e2263 | 17 | * |
bobbaddeley | 23:a31b178e2263 | 18 | * @param[ref] _ble |
bobbaddeley | 23:a31b178e2263 | 19 | * Reference to the underlying BLE. |
bobbaddeley | 24:d352571b1c1f | 20 | */ |
bobbaddeley | 23:a31b178e2263 | 21 | BikeService(BLE &_ble) : |
bobbaddeley | 23:a31b178e2263 | 22 | ble(_ble), |
bobbaddeley | 24:d352571b1c1f | 23 | inputBytes(0), |
bobbaddeley | 24:d352571b1c1f | 24 | outputBytes(0), |
bobbaddeley | 24:d352571b1c1f | 25 | timestampBytes(), |
bobbaddeley | 24:d352571b1c1f | 26 | alarmBytes(0), |
bobbaddeley | 30:243e095a69d9 | 27 | input_characteristic(0x8EC6, inputBytes.getPointer(), |
bobbaddeley | 24:d352571b1c1f | 28 | inputBytes.getNumValueBytes(), inputBytes.getNumValueBytes(), |
bobbaddeley | 23:a31b178e2263 | 29 | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY), |
bobbaddeley | 30:243e095a69d9 | 30 | output_characteristic(0x8EC7, outputBytes.getPointer(), |
bobbaddeley | 24:d352571b1c1f | 31 | outputBytes.getNumValueBytes(), outputBytes.getNumValueBytes(), |
bobbaddeley | 24:d352571b1c1f | 32 | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY), |
bobbaddeley | 30:243e095a69d9 | 33 | timestamp_characteristic(0x8EC8, timestampBytes.getPointer(), |
bobbaddeley | 25:cb54da929d9a | 34 | timestampBytes.getNumValueBytes(), timestampBytes.getNumValueBytes(), |
bobbaddeley | 25:cb54da929d9a | 35 | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY), |
bobbaddeley | 30:243e095a69d9 | 36 | log_characteristic(0x8EC9,0), |
bobbaddeley | 30:243e095a69d9 | 37 | alarmcode_characteristic(0x8ECA,alarmBytes.getPointer(), |
bobbaddeley | 24:d352571b1c1f | 38 | alarmBytes.getNumValueBytes(), alarmBytes.getNumValueBytes(), |
bobbaddeley | 25:cb54da929d9a | 39 | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE){ |
bobbaddeley | 23:a31b178e2263 | 40 | setupService(); |
bobbaddeley | 23:a31b178e2263 | 41 | } |
bobbaddeley | 24:d352571b1c1f | 42 | |
bobbaddeley | 23:a31b178e2263 | 43 | /** |
bobbaddeley | 24:d352571b1c1f | 44 | * @brief Set the inputs. |
bobbaddeley | 23:a31b178e2263 | 45 | * |
bobbaddeley | 24:d352571b1c1f | 46 | * @param[in] value |
bobbaddeley | 24:d352571b1c1f | 47 | * The inputs, with one bit for each input. |
bobbaddeley | 23:a31b178e2263 | 48 | */ |
bobbaddeley | 24:d352571b1c1f | 49 | void updateInputs(uint8_t value) { |
bobbaddeley | 24:d352571b1c1f | 50 | inputBytes.updateValue(value); |
bobbaddeley | 30:243e095a69d9 | 51 | ble.gattServer().write(input_characteristic.getValueHandle(), inputBytes.getPointer(), inputBytes.getNumValueBytes()); |
bobbaddeley | 23:a31b178e2263 | 52 | } |
bobbaddeley | 24:d352571b1c1f | 53 | |
bobbaddeley | 23:a31b178e2263 | 54 | /** |
bobbaddeley | 24:d352571b1c1f | 55 | * @brief Set the outputs. |
bobbaddeley | 23:a31b178e2263 | 56 | * |
bobbaddeley | 24:d352571b1c1f | 57 | * @param[in] value |
bobbaddeley | 24:d352571b1c1f | 58 | * The outputs, with one bit for each output. |
bobbaddeley | 23:a31b178e2263 | 59 | */ |
bobbaddeley | 24:d352571b1c1f | 60 | void updateOutputs(uint8_t value) { |
bobbaddeley | 24:d352571b1c1f | 61 | outputBytes.updateValue(value); |
bobbaddeley | 30:243e095a69d9 | 62 | ble.gattServer().write(output_characteristic.getValueHandle(), outputBytes.getPointer(), 1); |
bobbaddeley | 24:d352571b1c1f | 63 | } |
bobbaddeley | 24:d352571b1c1f | 64 | |
bobbaddeley | 24:d352571b1c1f | 65 | /** |
bobbaddeley | 24:d352571b1c1f | 66 | * @brief Set the alarm code. |
bobbaddeley | 24:d352571b1c1f | 67 | * |
bobbaddeley | 24:d352571b1c1f | 68 | * @param[in] value |
bobbaddeley | 24:d352571b1c1f | 69 | * The alarm code, with one byte for each output. |
bobbaddeley | 24:d352571b1c1f | 70 | */ |
bobbaddeley | 24:d352571b1c1f | 71 | void updateAlarmCode(uint8_t value) { |
bobbaddeley | 24:d352571b1c1f | 72 | alarmBytes.updateAlarm(value); |
bobbaddeley | 30:243e095a69d9 | 73 | ble.gattServer().write(alarmcode_characteristic.getValueHandle(), alarmBytes.getPointer(), 1); |
bobbaddeley | 23:a31b178e2263 | 74 | } |
bobbaddeley | 25:cb54da929d9a | 75 | |
bobbaddeley | 25:cb54da929d9a | 76 | void updateTimestamp() { |
bobbaddeley | 30:243e095a69d9 | 77 | ble.gattServer().write(timestamp_characteristic.getValueHandle(), timestampBytes.getPointer(), 1); |
bobbaddeley | 25:cb54da929d9a | 78 | } |
bobbaddeley | 23:a31b178e2263 | 79 | |
bobbaddeley | 23:a31b178e2263 | 80 | /** |
bobbaddeley | 23:a31b178e2263 | 81 | * This callback allows the heart rate service to receive updates to the |
bobbaddeley | 23:a31b178e2263 | 82 | * controlPoint characteristic. |
bobbaddeley | 23:a31b178e2263 | 83 | * |
bobbaddeley | 23:a31b178e2263 | 84 | * @param[in] params |
bobbaddeley | 23:a31b178e2263 | 85 | * Information about the characterisitc being updated. |
bobbaddeley | 23:a31b178e2263 | 86 | */ |
bobbaddeley | 23:a31b178e2263 | 87 | virtual void onDataWritten(const GattWriteCallbackParams *params) { |
bobbaddeley | 30:243e095a69d9 | 88 | if (params->handle == output_characteristic.getValueAttribute().getHandle()) { |
bobbaddeley | 24:d352571b1c1f | 89 | updateOutputs(*(params->data)); |
bobbaddeley | 24:d352571b1c1f | 90 | } |
bobbaddeley | 30:243e095a69d9 | 91 | else if (params->handle == alarmcode_characteristic.getValueAttribute().getHandle()) { |
bobbaddeley | 24:d352571b1c1f | 92 | updateAlarmCode(*(params->data)); |
bobbaddeley | 23:a31b178e2263 | 93 | } |
bobbaddeley | 23:a31b178e2263 | 94 | } |
bobbaddeley | 23:a31b178e2263 | 95 | |
bobbaddeley | 23:a31b178e2263 | 96 | protected: |
bobbaddeley | 23:a31b178e2263 | 97 | void setupService(void) { |
bobbaddeley | 30:243e095a69d9 | 98 | GattCharacteristic *charTable[] = {&input_characteristic, &output_characteristic, ×tamp_characteristic, &log_characteristic, &alarmcode_characteristic}; |
bobbaddeley | 23:a31b178e2263 | 99 | GattService bikeService(0x8EC5, charTable, sizeof(charTable) / sizeof(GattCharacteristic *)); |
bobbaddeley | 23:a31b178e2263 | 100 | |
bobbaddeley | 23:a31b178e2263 | 101 | ble.addService(bikeService); |
bobbaddeley | 23:a31b178e2263 | 102 | ble.onDataWritten(this, &BikeService::onDataWritten); |
bobbaddeley | 23:a31b178e2263 | 103 | } |
bobbaddeley | 23:a31b178e2263 | 104 | |
bobbaddeley | 23:a31b178e2263 | 105 | protected: |
bobbaddeley | 24:d352571b1c1f | 106 | /* Private internal representation for the bytes used to work with the value of the input and output characteristic. */ |
bobbaddeley | 24:d352571b1c1f | 107 | struct ValueBytes { |
bobbaddeley | 23:a31b178e2263 | 108 | |
bobbaddeley | 24:d352571b1c1f | 109 | ValueBytes(uint8_t value) : valueBytes() { |
bobbaddeley | 24:d352571b1c1f | 110 | updateValue(value); |
bobbaddeley | 23:a31b178e2263 | 111 | } |
bobbaddeley | 23:a31b178e2263 | 112 | |
bobbaddeley | 24:d352571b1c1f | 113 | void updateValue(uint8_t value) { |
bobbaddeley | 24:d352571b1c1f | 114 | valueBytes[0] = value; |
bobbaddeley | 23:a31b178e2263 | 115 | } |
bobbaddeley | 24:d352571b1c1f | 116 | |
bobbaddeley | 23:a31b178e2263 | 117 | uint8_t *getPointer(void) { |
bobbaddeley | 23:a31b178e2263 | 118 | return valueBytes; |
bobbaddeley | 23:a31b178e2263 | 119 | } |
bobbaddeley | 30:243e095a69d9 | 120 | |
bobbaddeley | 30:243e095a69d9 | 121 | uint8_t getValue(){ |
bobbaddeley | 30:243e095a69d9 | 122 | return valueBytes[0]; |
bobbaddeley | 30:243e095a69d9 | 123 | } |
bobbaddeley | 23:a31b178e2263 | 124 | |
bobbaddeley | 23:a31b178e2263 | 125 | const uint8_t *getPointer(void) const { |
bobbaddeley | 23:a31b178e2263 | 126 | return valueBytes; |
bobbaddeley | 23:a31b178e2263 | 127 | } |
bobbaddeley | 23:a31b178e2263 | 128 | |
bobbaddeley | 23:a31b178e2263 | 129 | unsigned getNumValueBytes(void) const { |
bobbaddeley | 24:d352571b1c1f | 130 | return 1; |
bobbaddeley | 24:d352571b1c1f | 131 | } |
bobbaddeley | 24:d352571b1c1f | 132 | |
bobbaddeley | 24:d352571b1c1f | 133 | private: |
bobbaddeley | 24:d352571b1c1f | 134 | uint8_t valueBytes[1]; |
bobbaddeley | 24:d352571b1c1f | 135 | }; |
bobbaddeley | 24:d352571b1c1f | 136 | |
bobbaddeley | 24:d352571b1c1f | 137 | protected: |
bobbaddeley | 24:d352571b1c1f | 138 | /* Private internal representation for the bytes used to work with the value of the input and output characteristic. */ |
bobbaddeley | 24:d352571b1c1f | 139 | struct TimestampBytes { |
bobbaddeley | 24:d352571b1c1f | 140 | |
bobbaddeley | 24:d352571b1c1f | 141 | TimestampBytes(void): timestampBytes(){ |
bobbaddeley | 24:d352571b1c1f | 142 | updateTimestamp(time(NULL)); |
bobbaddeley | 24:d352571b1c1f | 143 | } |
bobbaddeley | 24:d352571b1c1f | 144 | |
bobbaddeley | 24:d352571b1c1f | 145 | TimestampBytes(time_t value) : timestampBytes() { |
bobbaddeley | 24:d352571b1c1f | 146 | updateTimestamp(value); |
bobbaddeley | 24:d352571b1c1f | 147 | } |
bobbaddeley | 24:d352571b1c1f | 148 | |
bobbaddeley | 24:d352571b1c1f | 149 | void updateTimestamp(time_t value) { |
bobbaddeley | 24:d352571b1c1f | 150 | timestampBytes = value; |
bobbaddeley | 24:d352571b1c1f | 151 | } |
bobbaddeley | 24:d352571b1c1f | 152 | |
bobbaddeley | 24:d352571b1c1f | 153 | uint8_t *getPointer(void) { |
bobbaddeley | 24:d352571b1c1f | 154 | return NULL;//×tampBytes;//TODO MAKE THIS WORK |
bobbaddeley | 24:d352571b1c1f | 155 | } |
bobbaddeley | 24:d352571b1c1f | 156 | |
bobbaddeley | 24:d352571b1c1f | 157 | const uint8_t *getPointer(void) const { |
bobbaddeley | 24:d352571b1c1f | 158 | return NULL;//×tampBytes;//TODO MAKE THIS WORK |
bobbaddeley | 24:d352571b1c1f | 159 | } |
bobbaddeley | 24:d352571b1c1f | 160 | |
bobbaddeley | 24:d352571b1c1f | 161 | unsigned getNumValueBytes(void) const { |
bobbaddeley | 24:d352571b1c1f | 162 | return sizeof(timestampBytes); |
bobbaddeley | 23:a31b178e2263 | 163 | } |
bobbaddeley | 23:a31b178e2263 | 164 | |
bobbaddeley | 23:a31b178e2263 | 165 | private: |
bobbaddeley | 24:d352571b1c1f | 166 | time_t timestampBytes; |
bobbaddeley | 23:a31b178e2263 | 167 | }; |
bobbaddeley | 24:d352571b1c1f | 168 | |
bobbaddeley | 24:d352571b1c1f | 169 | protected: |
bobbaddeley | 24:d352571b1c1f | 170 | /* Private internal representation for the bytes used to work with the value of the input and output characteristic. */ |
bobbaddeley | 24:d352571b1c1f | 171 | struct AlarmBytes { |
bobbaddeley | 23:a31b178e2263 | 172 | |
bobbaddeley | 24:d352571b1c1f | 173 | AlarmBytes(void): alarmBytes(){ |
bobbaddeley | 24:d352571b1c1f | 174 | //fill this in with the default alarm code |
bobbaddeley | 24:d352571b1c1f | 175 | } |
bobbaddeley | 24:d352571b1c1f | 176 | |
bobbaddeley | 24:d352571b1c1f | 177 | AlarmBytes(uint8_t value) : alarmBytes() { |
bobbaddeley | 24:d352571b1c1f | 178 | updateAlarm(value); |
bobbaddeley | 24:d352571b1c1f | 179 | } |
bobbaddeley | 24:d352571b1c1f | 180 | |
bobbaddeley | 24:d352571b1c1f | 181 | void updateAlarm(uint8_t value) { |
bobbaddeley | 24:d352571b1c1f | 182 | alarmBytes[0] = value; |
bobbaddeley | 24:d352571b1c1f | 183 | } |
bobbaddeley | 24:d352571b1c1f | 184 | |
bobbaddeley | 24:d352571b1c1f | 185 | uint8_t *getPointer(void) { |
bobbaddeley | 24:d352571b1c1f | 186 | return alarmBytes; |
bobbaddeley | 24:d352571b1c1f | 187 | } |
bobbaddeley | 24:d352571b1c1f | 188 | |
bobbaddeley | 24:d352571b1c1f | 189 | const uint8_t *getPointer(void) const { |
bobbaddeley | 24:d352571b1c1f | 190 | return alarmBytes; |
bobbaddeley | 24:d352571b1c1f | 191 | } |
bobbaddeley | 24:d352571b1c1f | 192 | |
bobbaddeley | 24:d352571b1c1f | 193 | unsigned getNumValueBytes(void) const { |
bobbaddeley | 24:d352571b1c1f | 194 | return 5; |
bobbaddeley | 24:d352571b1c1f | 195 | } |
bobbaddeley | 24:d352571b1c1f | 196 | |
bobbaddeley | 24:d352571b1c1f | 197 | private: |
bobbaddeley | 24:d352571b1c1f | 198 | uint8_t alarmBytes[5]; |
bobbaddeley | 24:d352571b1c1f | 199 | }; |
bobbaddeley | 24:d352571b1c1f | 200 | |
bobbaddeley | 23:a31b178e2263 | 201 | protected: |
bobbaddeley | 23:a31b178e2263 | 202 | BLE &ble; |
bobbaddeley | 23:a31b178e2263 | 203 | |
Technicus | 29:e7d4922a4620 | 204 | ValueBytes inputBytes; |
Technicus | 29:e7d4922a4620 | 205 | ValueBytes outputBytes; |
Technicus | 29:e7d4922a4620 | 206 | TimestampBytes timestampBytes; |
Technicus | 29:e7d4922a4620 | 207 | AlarmBytes alarmBytes; |
bobbaddeley | 23:a31b178e2263 | 208 | |
bobbaddeley | 30:243e095a69d9 | 209 | GattCharacteristic input_characteristic; |
bobbaddeley | 30:243e095a69d9 | 210 | GattCharacteristic output_characteristic; |
bobbaddeley | 30:243e095a69d9 | 211 | GattCharacteristic timestamp_characteristic; |
bobbaddeley | 30:243e095a69d9 | 212 | GattCharacteristic log_characteristic; |
bobbaddeley | 30:243e095a69d9 | 213 | GattCharacteristic alarmcode_characteristic; |
bobbaddeley | 23:a31b178e2263 | 214 | }; |
bobbaddeley | 23:a31b178e2263 | 215 | |
bobbaddeley | 23:a31b178e2263 | 216 | #endif /* #ifndef __BLE_BIKE_SERVICE_H__*/ |