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