BLE lib
Fork of Nucleo_BLE_API by
services/UnicornService.h@4:f1696bf0e6c6, 2015-02-08 (annotated)
- Committer:
- vijaynvr
- Date:
- Sun Feb 08 14:26:05 2015 +0000
- Revision:
- 4:f1696bf0e6c6
working
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
vijaynvr | 4:f1696bf0e6c6 | 1 | /* mbed Microcontroller Library |
vijaynvr | 4:f1696bf0e6c6 | 2 | * Copyright (c) 2006-2013 ARM Limited |
vijaynvr | 4:f1696bf0e6c6 | 3 | * |
vijaynvr | 4:f1696bf0e6c6 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
vijaynvr | 4:f1696bf0e6c6 | 5 | * you may not use this file except in compliance with the License. |
vijaynvr | 4:f1696bf0e6c6 | 6 | * You may obtain a copy of the License at |
vijaynvr | 4:f1696bf0e6c6 | 7 | * |
vijaynvr | 4:f1696bf0e6c6 | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
vijaynvr | 4:f1696bf0e6c6 | 9 | * |
vijaynvr | 4:f1696bf0e6c6 | 10 | * Unless required by applicable law or agreed to in writing, software |
vijaynvr | 4:f1696bf0e6c6 | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
vijaynvr | 4:f1696bf0e6c6 | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
vijaynvr | 4:f1696bf0e6c6 | 13 | * See the License for the specific language governing permissions and |
vijaynvr | 4:f1696bf0e6c6 | 14 | * limitations under the License. |
vijaynvr | 4:f1696bf0e6c6 | 15 | */ |
vijaynvr | 4:f1696bf0e6c6 | 16 | |
vijaynvr | 4:f1696bf0e6c6 | 17 | #ifndef __BLE_HEART_RATE_SERVICE_H__ |
vijaynvr | 4:f1696bf0e6c6 | 18 | #define __BLE_HEART_RATE_SERVICE_H__ |
vijaynvr | 4:f1696bf0e6c6 | 19 | |
vijaynvr | 4:f1696bf0e6c6 | 20 | #include "BLEDevice.h" |
vijaynvr | 4:f1696bf0e6c6 | 21 | |
vijaynvr | 4:f1696bf0e6c6 | 22 | /* Heart Rate Service */ |
vijaynvr | 4:f1696bf0e6c6 | 23 | /* Service: https://developer.bluetooth.org/gatt/services/Pages/ServiceViewer.aspx?u=org.bluetooth.service.heart_rate.xml */ |
vijaynvr | 4:f1696bf0e6c6 | 24 | /* HRM Char: https://developer.bluetooth.org/gatt/characteristics/Pages/CharacteristicViewer.aspx?u=org.bluetooth.characteristic.heart_rate_measurement.xml */ |
vijaynvr | 4:f1696bf0e6c6 | 25 | /* Location: https://developer.bluetooth.org/gatt/characteristics/Pages/CharacteristicViewer.aspx?u=org.bluetooth.characteristic.body_sensor_location.xml */ |
vijaynvr | 4:f1696bf0e6c6 | 26 | class UnicornService { |
vijaynvr | 4:f1696bf0e6c6 | 27 | public: |
vijaynvr | 4:f1696bf0e6c6 | 28 | enum { |
vijaynvr | 4:f1696bf0e6c6 | 29 | LOCATION_OTHER = 0, |
vijaynvr | 4:f1696bf0e6c6 | 30 | LOCATION_CHEST, |
vijaynvr | 4:f1696bf0e6c6 | 31 | LOCATION_WRIST, |
vijaynvr | 4:f1696bf0e6c6 | 32 | LOCATION_FINGER, |
vijaynvr | 4:f1696bf0e6c6 | 33 | LOCATION_HAND, |
vijaynvr | 4:f1696bf0e6c6 | 34 | LOCATION_EAR_LOBE, |
vijaynvr | 4:f1696bf0e6c6 | 35 | LOCATION_FOOT, |
vijaynvr | 4:f1696bf0e6c6 | 36 | }; |
vijaynvr | 4:f1696bf0e6c6 | 37 | |
vijaynvr | 4:f1696bf0e6c6 | 38 | public: |
vijaynvr | 4:f1696bf0e6c6 | 39 | /** |
vijaynvr | 4:f1696bf0e6c6 | 40 | * Constructor. |
vijaynvr | 4:f1696bf0e6c6 | 41 | * |
vijaynvr | 4:f1696bf0e6c6 | 42 | * param[in] _ble |
vijaynvr | 4:f1696bf0e6c6 | 43 | * Reference to the underlying BLEDevice. |
vijaynvr | 4:f1696bf0e6c6 | 44 | * param[in] hrmCounter (8-bit) |
vijaynvr | 4:f1696bf0e6c6 | 45 | * initial value for the hrm counter. |
vijaynvr | 4:f1696bf0e6c6 | 46 | * param[in] location |
vijaynvr | 4:f1696bf0e6c6 | 47 | * Sensor's location. |
vijaynvr | 4:f1696bf0e6c6 | 48 | */ |
vijaynvr | 4:f1696bf0e6c6 | 49 | UnicornService(BLEDevice &_ble, uint8_t hrmCounter, uint8_t location) : |
vijaynvr | 4:f1696bf0e6c6 | 50 | ble(_ble), |
vijaynvr | 4:f1696bf0e6c6 | 51 | valueBytes(hrmCounter), |
vijaynvr | 4:f1696bf0e6c6 | 52 | hrmRate(GattCharacteristic::UUID_HEART_RATE_MEASUREMENT_CHAR, valueBytes.getPointer(), |
vijaynvr | 4:f1696bf0e6c6 | 53 | valueBytes.getNumValueBytes(), HeartRateValueBytes::MAX_VALUE_BYTES, |
vijaynvr | 4:f1696bf0e6c6 | 54 | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY), |
vijaynvr | 4:f1696bf0e6c6 | 55 | hrmLocation(GattCharacteristic::UUID_BODY_SENSOR_LOCATION_CHAR, (uint8_t *)&location, sizeof(location), sizeof(location), |
vijaynvr | 4:f1696bf0e6c6 | 56 | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ), |
vijaynvr | 4:f1696bf0e6c6 | 57 | controlPoint(GattCharacteristic::UUID_HEART_RATE_CONTROL_POINT_CHAR, (uint8_t *)&controlPointValue, |
vijaynvr | 4:f1696bf0e6c6 | 58 | sizeof(controlPointValue), sizeof(controlPointValue), GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE) { |
vijaynvr | 4:f1696bf0e6c6 | 59 | setupService(); |
vijaynvr | 4:f1696bf0e6c6 | 60 | } |
vijaynvr | 4:f1696bf0e6c6 | 61 | |
vijaynvr | 4:f1696bf0e6c6 | 62 | /** |
vijaynvr | 4:f1696bf0e6c6 | 63 | * Same constructor as above, but with a 16-bit HRM Counter value. |
vijaynvr | 4:f1696bf0e6c6 | 64 | */ |
vijaynvr | 4:f1696bf0e6c6 | 65 | UnicornService(BLEDevice &_ble, uint16_t hrmCounter, uint8_t location) : |
vijaynvr | 4:f1696bf0e6c6 | 66 | ble(_ble), |
vijaynvr | 4:f1696bf0e6c6 | 67 | valueBytes(hrmCounter), |
vijaynvr | 4:f1696bf0e6c6 | 68 | hrmRate(GattCharacteristic::UUID_HEART_RATE_MEASUREMENT_CHAR, valueBytes.getPointer(), |
vijaynvr | 4:f1696bf0e6c6 | 69 | valueBytes.getNumValueBytes(), HeartRateValueBytes::MAX_VALUE_BYTES, |
vijaynvr | 4:f1696bf0e6c6 | 70 | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY), |
vijaynvr | 4:f1696bf0e6c6 | 71 | hrmLocation(GattCharacteristic::UUID_BODY_SENSOR_LOCATION_CHAR, (uint8_t *)&location, sizeof(location), sizeof(location), |
vijaynvr | 4:f1696bf0e6c6 | 72 | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ), |
vijaynvr | 4:f1696bf0e6c6 | 73 | controlPoint(GattCharacteristic::UUID_HEART_RATE_CONTROL_POINT_CHAR, (uint8_t *)&controlPointValue, |
vijaynvr | 4:f1696bf0e6c6 | 74 | sizeof(controlPointValue), sizeof(controlPointValue), GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE) { |
vijaynvr | 4:f1696bf0e6c6 | 75 | setupService(); |
vijaynvr | 4:f1696bf0e6c6 | 76 | } |
vijaynvr | 4:f1696bf0e6c6 | 77 | |
vijaynvr | 4:f1696bf0e6c6 | 78 | /** |
vijaynvr | 4:f1696bf0e6c6 | 79 | * Set a new 8-bit value for heart rate. |
vijaynvr | 4:f1696bf0e6c6 | 80 | */ |
vijaynvr | 4:f1696bf0e6c6 | 81 | void updateHeartRate(uint8_t hrmCounter) { |
vijaynvr | 4:f1696bf0e6c6 | 82 | valueBytes.updateHeartRate(hrmCounter); |
vijaynvr | 4:f1696bf0e6c6 | 83 | ble.updateCharacteristicValue(hrmRate.getValueAttribute().getHandle(), valueBytes.getPointer(), valueBytes.getNumValueBytes()); |
vijaynvr | 4:f1696bf0e6c6 | 84 | } |
vijaynvr | 4:f1696bf0e6c6 | 85 | |
vijaynvr | 4:f1696bf0e6c6 | 86 | /** |
vijaynvr | 4:f1696bf0e6c6 | 87 | * Set a new 16-bit value for heart rate. |
vijaynvr | 4:f1696bf0e6c6 | 88 | */ |
vijaynvr | 4:f1696bf0e6c6 | 89 | void updateHeartRate(uint16_t hrmCounter) { |
vijaynvr | 4:f1696bf0e6c6 | 90 | valueBytes.updateHeartRate(hrmCounter); |
vijaynvr | 4:f1696bf0e6c6 | 91 | ble.updateCharacteristicValue(hrmRate.getValueAttribute().getHandle(), valueBytes.getPointer(), valueBytes.getNumValueBytes()); |
vijaynvr | 4:f1696bf0e6c6 | 92 | } |
vijaynvr | 4:f1696bf0e6c6 | 93 | |
vijaynvr | 4:f1696bf0e6c6 | 94 | /** |
vijaynvr | 4:f1696bf0e6c6 | 95 | * This callback allows the UART service to receive updates to the |
vijaynvr | 4:f1696bf0e6c6 | 96 | * txCharacteristic. The application should forward the call to this |
vijaynvr | 4:f1696bf0e6c6 | 97 | * function from the global onDataWritten() callback handler; or if that's |
vijaynvr | 4:f1696bf0e6c6 | 98 | * not used, this method can be used as a callback directly. |
vijaynvr | 4:f1696bf0e6c6 | 99 | */ |
vijaynvr | 4:f1696bf0e6c6 | 100 | virtual void onDataWritten(const GattCharacteristicWriteCBParams *params) { |
vijaynvr | 4:f1696bf0e6c6 | 101 | if (params->charHandle == controlPoint.getValueAttribute().getHandle()) { |
vijaynvr | 4:f1696bf0e6c6 | 102 | /* Do something here if the new value is 1; else you can override this method by |
vijaynvr | 4:f1696bf0e6c6 | 103 | * extending this class. |
vijaynvr | 4:f1696bf0e6c6 | 104 | * @NOTE: if you are extending this class, be sure to also call |
vijaynvr | 4:f1696bf0e6c6 | 105 | * ble.onDataWritten(this, &ExtendedHRService::onDataWritten); in |
vijaynvr | 4:f1696bf0e6c6 | 106 | * your constructor. |
vijaynvr | 4:f1696bf0e6c6 | 107 | */ |
vijaynvr | 4:f1696bf0e6c6 | 108 | } |
vijaynvr | 4:f1696bf0e6c6 | 109 | } |
vijaynvr | 4:f1696bf0e6c6 | 110 | |
vijaynvr | 4:f1696bf0e6c6 | 111 | private: |
vijaynvr | 4:f1696bf0e6c6 | 112 | void setupService(void) { |
vijaynvr | 4:f1696bf0e6c6 | 113 | static bool serviceAdded = false; /* We should only ever need to add the heart rate service once. */ |
vijaynvr | 4:f1696bf0e6c6 | 114 | if (serviceAdded) { |
vijaynvr | 4:f1696bf0e6c6 | 115 | return; |
vijaynvr | 4:f1696bf0e6c6 | 116 | } |
vijaynvr | 4:f1696bf0e6c6 | 117 | |
vijaynvr | 4:f1696bf0e6c6 | 118 | GattCharacteristic *charTable[] = {&hrmRate, &hrmLocation, &controlPoint}; |
vijaynvr | 4:f1696bf0e6c6 | 119 | GattService hrmService(GattService::UUID_HEART_RATE_SERVICE, charTable, sizeof(charTable) / sizeof(GattCharacteristic *)); |
vijaynvr | 4:f1696bf0e6c6 | 120 | |
vijaynvr | 4:f1696bf0e6c6 | 121 | ble.addService(hrmService); |
vijaynvr | 4:f1696bf0e6c6 | 122 | serviceAdded = true; |
vijaynvr | 4:f1696bf0e6c6 | 123 | |
vijaynvr | 4:f1696bf0e6c6 | 124 | ble.onDataWritten(this, &UnicornService::onDataWritten); |
vijaynvr | 4:f1696bf0e6c6 | 125 | } |
vijaynvr | 4:f1696bf0e6c6 | 126 | |
vijaynvr | 4:f1696bf0e6c6 | 127 | private: |
vijaynvr | 4:f1696bf0e6c6 | 128 | /* Private internal representation for the bytes used to work with the vaulue of the heart-rate characteristic. */ |
vijaynvr | 4:f1696bf0e6c6 | 129 | struct HeartRateValueBytes { |
vijaynvr | 4:f1696bf0e6c6 | 130 | static const unsigned MAX_VALUE_BYTES = 3; /* FLAGS + up to two bytes for heart-rate */ |
vijaynvr | 4:f1696bf0e6c6 | 131 | static const unsigned FLAGS_BYTE_INDEX = 0; |
vijaynvr | 4:f1696bf0e6c6 | 132 | |
vijaynvr | 4:f1696bf0e6c6 | 133 | static const unsigned VALUE_FORMAT_BITNUM = 0; |
vijaynvr | 4:f1696bf0e6c6 | 134 | static const uint8_t VALUE_FORMAT_FLAG = (1 << VALUE_FORMAT_BITNUM); |
vijaynvr | 4:f1696bf0e6c6 | 135 | |
vijaynvr | 4:f1696bf0e6c6 | 136 | HeartRateValueBytes(uint8_t hrmCounter) : valueBytes() { |
vijaynvr | 4:f1696bf0e6c6 | 137 | updateHeartRate(hrmCounter); |
vijaynvr | 4:f1696bf0e6c6 | 138 | } |
vijaynvr | 4:f1696bf0e6c6 | 139 | |
vijaynvr | 4:f1696bf0e6c6 | 140 | HeartRateValueBytes(uint16_t hrmCounter) : valueBytes() { |
vijaynvr | 4:f1696bf0e6c6 | 141 | updateHeartRate(hrmCounter); |
vijaynvr | 4:f1696bf0e6c6 | 142 | } |
vijaynvr | 4:f1696bf0e6c6 | 143 | |
vijaynvr | 4:f1696bf0e6c6 | 144 | void updateHeartRate(uint8_t hrmCounter) { |
vijaynvr | 4:f1696bf0e6c6 | 145 | valueBytes[FLAGS_BYTE_INDEX] &= ~VALUE_FORMAT_FLAG; |
vijaynvr | 4:f1696bf0e6c6 | 146 | valueBytes[FLAGS_BYTE_INDEX + 1] = hrmCounter; |
vijaynvr | 4:f1696bf0e6c6 | 147 | } |
vijaynvr | 4:f1696bf0e6c6 | 148 | |
vijaynvr | 4:f1696bf0e6c6 | 149 | void updateHeartRate(uint16_t hrmCounter) { |
vijaynvr | 4:f1696bf0e6c6 | 150 | valueBytes[FLAGS_BYTE_INDEX] |= VALUE_FORMAT_FLAG; |
vijaynvr | 4:f1696bf0e6c6 | 151 | valueBytes[FLAGS_BYTE_INDEX + 1] = (uint8_t)(hrmCounter & 0xFF); |
vijaynvr | 4:f1696bf0e6c6 | 152 | valueBytes[FLAGS_BYTE_INDEX + 2] = (uint8_t)(hrmCounter >> 8); |
vijaynvr | 4:f1696bf0e6c6 | 153 | } |
vijaynvr | 4:f1696bf0e6c6 | 154 | |
vijaynvr | 4:f1696bf0e6c6 | 155 | uint8_t *getPointer(void) { |
vijaynvr | 4:f1696bf0e6c6 | 156 | return valueBytes; |
vijaynvr | 4:f1696bf0e6c6 | 157 | } |
vijaynvr | 4:f1696bf0e6c6 | 158 | |
vijaynvr | 4:f1696bf0e6c6 | 159 | const uint8_t *getPointer(void) const { |
vijaynvr | 4:f1696bf0e6c6 | 160 | return valueBytes; |
vijaynvr | 4:f1696bf0e6c6 | 161 | } |
vijaynvr | 4:f1696bf0e6c6 | 162 | |
vijaynvr | 4:f1696bf0e6c6 | 163 | unsigned getNumValueBytes(void) const { |
vijaynvr | 4:f1696bf0e6c6 | 164 | return 1 + ((valueBytes[FLAGS_BYTE_INDEX] & VALUE_FORMAT_FLAG) ? sizeof(uint16_t) : sizeof(uint8_t)); |
vijaynvr | 4:f1696bf0e6c6 | 165 | } |
vijaynvr | 4:f1696bf0e6c6 | 166 | |
vijaynvr | 4:f1696bf0e6c6 | 167 | private: |
vijaynvr | 4:f1696bf0e6c6 | 168 | /* First byte = 8-bit values, no extra info, Second byte = uint8_t HRM value */ |
vijaynvr | 4:f1696bf0e6c6 | 169 | /* See --> https://developer.bluetooth.org/gatt/characteristics/Pages/CharacteristicViewer.aspx?u=org.bluetooth.characteristic.heart_rate_measurement.xml */ |
vijaynvr | 4:f1696bf0e6c6 | 170 | uint8_t valueBytes[MAX_VALUE_BYTES]; |
vijaynvr | 4:f1696bf0e6c6 | 171 | }; |
vijaynvr | 4:f1696bf0e6c6 | 172 | |
vijaynvr | 4:f1696bf0e6c6 | 173 | private: |
vijaynvr | 4:f1696bf0e6c6 | 174 | BLEDevice &ble; |
vijaynvr | 4:f1696bf0e6c6 | 175 | HeartRateValueBytes valueBytes; |
vijaynvr | 4:f1696bf0e6c6 | 176 | uint8_t controlPointValue; |
vijaynvr | 4:f1696bf0e6c6 | 177 | GattCharacteristic hrmRate; |
vijaynvr | 4:f1696bf0e6c6 | 178 | GattCharacteristic hrmLocation; |
vijaynvr | 4:f1696bf0e6c6 | 179 | GattCharacteristic controlPoint; |
vijaynvr | 4:f1696bf0e6c6 | 180 | }; |
vijaynvr | 4:f1696bf0e6c6 | 181 | |
vijaynvr | 4:f1696bf0e6c6 | 182 | #endif /* #ifndef __BLE_HEART_RATE_SERVICE_H__*/ |