Delta
/
BLE_Secure_HeartRate_DELTA
Fork of BLE_HeartRate_DELTA by
Revision 2:0737743120d7, committed 2017-03-28
- Comitter:
- silviaChen
- Date:
- Tue Mar 28 08:21:46 2017 +0000
- Parent:
- 1:82331af3e4c9
- Commit message:
- Add BLE bonding/pairing function
Changed in this revision
HeartRateSecService.h | Show annotated file Show diff for this revision Revisions of this file |
main.cpp | Show annotated file Show diff for this revision Revisions of this file |
diff -r 82331af3e4c9 -r 0737743120d7 HeartRateSecService.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/HeartRateSecService.h Tue Mar 28 08:21:46 2017 +0000 @@ -0,0 +1,203 @@ +/* mbed Microcontroller Library + * Copyright (c) 2006-2013 ARM Limited + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef __BLE_HEART_RATE_SEC_SAMPLE_SERVICE_H__ +#define __BLE_HEART_RATE_SEC_SAMPLE_SERVICE_H__ + +#include "BLE.h" + +/** +* @class HeartRateSecService +* @brief BLE Service for HeartRate. This BLE Service contains the location of the sensor, the heartrate in beats per minute. <br> +* Service: https://developer.bluetooth.org/gatt/services/Pages/ServiceViewer.aspx?u=org.bluetooth.service.heart_rate.xml <br> +* HRM Char: https://developer.bluetooth.org/gatt/characteristics/Pages/CharacteristicViewer.aspx?u=org.bluetooth.characteristic.heart_rate_measurement.xml <br> +* Location: https://developer.bluetooth.org/gatt/characteristics/Pages/CharacteristicViewer.aspx?u=org.bluetooth.characteristic.body_sensor_location.xml +*/ +class HeartRateSecService { +public: + /** + * @enum SensorLocation + * @brief Location of HeartRate sensor on body. + */ + enum { + LOCATION_OTHER = 0, /*!< Other Location */ + LOCATION_CHEST, /*!< Chest */ + LOCATION_WRIST, /*!< Wrist */ + LOCATION_FINGER, /*!< Finger */ + LOCATION_HAND, /*!< Hand */ + LOCATION_EAR_LOBE, /*!< Earlobe */ + LOCATION_FOOT, /*!< Foot */ + }; + +public: + /** + * @brief Constructor with 8bit HRM Counter value. + * + * @param[ref] _ble + * Reference to the underlying BLEDevice. + * @param[in] hrmCounter (8-bit) + * initial value for the hrm counter. + * @param[in] location + * Sensor's location. + */ + HeartRateSecService(BLE &_ble, uint8_t hrmCounter, uint8_t location) : + ble(_ble), + valueBytes(hrmCounter), + hrmRate(GattCharacteristic::UUID_HEART_RATE_MEASUREMENT_CHAR, valueBytes.getPointer(), + valueBytes.getNumValueBytes(), HeartRateValueBytes::MAX_VALUE_BYTES, + GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY), + hrmLocation(GattCharacteristic::UUID_BODY_SENSOR_LOCATION_CHAR, &location), + controlPoint(GattCharacteristic::UUID_HEART_RATE_CONTROL_POINT_CHAR, &controlPointValue) { + setupService(); + } + + /** + * @brief Constructor with a 16-bit HRM Counter value. + * + * @param[in] _ble + * Reference to the underlying BLEDevice. + * @param[in] hrmCounter (8-bit) + * initial value for the hrm counter. + * @param[in] location + * Sensor's location. + */ + HeartRateSecService(BLE &_ble, uint16_t hrmCounter, uint8_t location) : + ble(_ble), + valueBytes(hrmCounter), + hrmRate(GattCharacteristic::UUID_HEART_RATE_MEASUREMENT_CHAR, valueBytes.getPointer(), + valueBytes.getNumValueBytes(), HeartRateValueBytes::MAX_VALUE_BYTES, + GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY), + hrmLocation(GattCharacteristic::UUID_BODY_SENSOR_LOCATION_CHAR, &location), + controlPoint(GattCharacteristic::UUID_HEART_RATE_CONTROL_POINT_CHAR, &controlPointValue) { + setupService(); + } + + /** + * @brief Set a new 8-bit value for heart rate. + * + * @param[in] hrmCounter + * HeartRate in bpm. + */ + void updateHeartRate(uint8_t hrmCounter) { + valueBytes.updateHeartRate(hrmCounter); + ble.gattServer().write(hrmRate.getValueAttribute().getHandle(), valueBytes.getPointer(), valueBytes.getNumValueBytes()); + } + + /** + * Set a new 16-bit value for heart rate. + * + * @param[in] hrmCounter + * HeartRate in bpm. + */ + void updateHeartRate(uint16_t hrmCounter) { + valueBytes.updateHeartRate(hrmCounter); + ble.gattServer().write(hrmRate.getValueAttribute().getHandle(), valueBytes.getPointer(), valueBytes.getNumValueBytes()); + } + + /** + * This callback allows the HeartRateSecService to receive updates to the + * controlPoint Characteristic. + * + * @param[in] params + * Information about the characterisitc being updated. + */ + virtual void onDataWritten(const GattWriteCallbackParams *params) { + if (params->handle == controlPoint.getValueAttribute().getHandle()) { + /* Do something here if the new value is 1; else you can override this method by + * extending this class. + * @NOTE: if you are extending this class, be sure to also call + * ble.onDataWritten(this, &ExtendedHRService::onDataWritten); in + * your constructor. + */ + } + } + +private: + void setupService(void) { + static bool serviceAdded = false; /* We should only ever need to add the heart rate service once. */ + if (serviceAdded) { + return; + } + + hrmRate.requireSecurity(SecurityManager::SECURITY_MODE_ENCRYPTION_WITH_MITM); //Need passkey + + GattCharacteristic *charTable[] = {&hrmRate, &hrmLocation, &controlPoint}; + GattService hrmService(GattService::UUID_HEART_RATE_SERVICE, charTable, sizeof(charTable) / sizeof(GattCharacteristic *)); + + ble.gattServer().addService(hrmService); + serviceAdded = true; + + ble.gattServer().onDataWritten(this, &HeartRateSecService::onDataWritten); + } + +private: + /* Private internal representation for the bytes used to work with the vaulue of the heart-rate characteristic. */ + struct HeartRateValueBytes { + static const unsigned MAX_VALUE_BYTES = 3; /* FLAGS + up to two bytes for heart-rate */ + static const unsigned FLAGS_BYTE_INDEX = 0; + + static const unsigned VALUE_FORMAT_BITNUM = 0; + static const uint8_t VALUE_FORMAT_FLAG = (1 << VALUE_FORMAT_BITNUM); + + HeartRateValueBytes(uint8_t hrmCounter) : valueBytes() { + updateHeartRate(hrmCounter); + } + + HeartRateValueBytes(uint16_t hrmCounter) : valueBytes() { + updateHeartRate(hrmCounter); + } + + void updateHeartRate(uint8_t hrmCounter) { + valueBytes[FLAGS_BYTE_INDEX] &= ~VALUE_FORMAT_FLAG; + valueBytes[FLAGS_BYTE_INDEX + 1] = hrmCounter; + } + + void updateHeartRate(uint16_t hrmCounter) { + valueBytes[FLAGS_BYTE_INDEX] |= VALUE_FORMAT_FLAG; + valueBytes[FLAGS_BYTE_INDEX + 1] = (uint8_t)(hrmCounter & 0xFF); + valueBytes[FLAGS_BYTE_INDEX + 2] = (uint8_t)(hrmCounter >> 8); + } + + uint8_t *getPointer(void) { + return valueBytes; + } + + const uint8_t *getPointer(void) const { + return valueBytes; + } + + unsigned getNumValueBytes(void) const { + return 1 + ((valueBytes[FLAGS_BYTE_INDEX] & VALUE_FORMAT_FLAG) ? sizeof(uint16_t) : sizeof(uint8_t)); + } + +private: + /* First byte = 8-bit values, no extra info, Second byte = uint8_t HRM value */ + /* See --> https://developer.bluetooth.org/gatt/characteristics/Pages/CharacteristicViewer.aspx?u=org.bluetooth.characteristic.heart_rate_measurement.xml */ + uint8_t valueBytes[MAX_VALUE_BYTES]; + }; + +private: + BLE &ble; + + HeartRateValueBytes valueBytes; + uint8_t controlPointValue; + + GattCharacteristic hrmRate; + ReadOnlyGattCharacteristic<uint8_t> hrmLocation; + WriteOnlyGattCharacteristic<uint8_t> controlPoint; +}; + +#endif /* #ifndef __BLE_HEART_RATE_SEC_SAMPLE_SERVICE_H__*/ \ No newline at end of file
diff -r 82331af3e4c9 -r 0737743120d7 main.cpp --- a/main.cpp Tue Mar 14 08:33:26 2017 +0000 +++ b/main.cpp Tue Mar 28 08:21:46 2017 +0000 @@ -16,25 +16,28 @@ #include "mbed.h" #include "ble/BLE.h" -#include "ble/services/HeartRateService.h" +#include "HeartRateSecService.h" #include "ble/services/BatteryService.h" #include "ble/services/DeviceInformationService.h" +BLE ble; DigitalOut led1(LED1); +Serial uart(USBTX, USBRX, 115200); -const static char DEVICE_NAME[] = "DELTA_HRM1"; +const static char DEVICE_NAME[] = "DELTA_HRM_SEC"; static const uint16_t uuid16_list[] = {GattService::UUID_HEART_RATE_SERVICE, GattService::UUID_DEVICE_INFORMATION_SERVICE}; static volatile bool triggerSensorPolling = false; +bool isSecuritySuccess = false; uint8_t hrmCounter = 100; // init HRM to 100bps -HeartRateService *hrService; +HeartRateSecService *hrSecService; DeviceInformationService *deviceInfo; void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params) { - BLE::Instance(BLE::DEFAULT_INSTANCE).gap().startAdvertising(); // restart advertising + ble.gap().startAdvertising(); // restart advertising } void periodicCallback(void) @@ -46,6 +49,27 @@ triggerSensorPolling = true; } +void passkeyDisplayCallback(Gap::Handle_t handle, const SecurityManager::Passkey_t passkey) +{ + uart.printf("Input passKey: "); + for (unsigned i = 0; i < Gap::ADDR_LEN; i++) { + uart.printf("%c ", passkey[i]); + } + uart.printf("\r\n"); +} + +void securitySetupCompletedCallback(Gap::Handle_t handle, SecurityManager::SecurityCompletionStatus_t status) +{ + if (status == SecurityManager::SEC_STATUS_SUCCESS) { + uart.printf("Security success\r\n"); + isSecuritySuccess = true; + } else { + uart.printf("Security failed\r\n"); + isSecuritySuccess = false; + ble.gap().disconnect(Gap::LOCAL_HOST_TERMINATED_CONNECTION); + } +} + void bleInitComplete(BLE::InitializationCompleteCallbackContext *params) { BLE &ble = params->ble; @@ -54,11 +78,19 @@ if (error != BLE_ERROR_NONE) { return; } + + /* Initialize BLE security */ + bool enableBonding = true; + bool requireMITM = true; //Need passkey + uint8_t pKey[6] = {'0', '0', '0', '0', '0', '0'}; //set the passkey + ble.securityManager().init(enableBonding, requireMITM, SecurityManager::IO_CAPS_DISPLAY_ONLY, pKey); ble.gap().onDisconnection(disconnectionCallback); + ble.securityManager().onPasskeyDisplay(passkeyDisplayCallback); + ble.securityManager().onSecuritySetupCompleted(securitySetupCompletedCallback); /* Setup primary service. */ - hrService = new HeartRateService(ble, hrmCounter, HeartRateService::LOCATION_FINGER); + hrSecService = new HeartRateSecService(ble, hrmCounter, HeartRateSecService::LOCATION_FINGER); /* Setup auxiliary service. */ deviceInfo = new DeviceInformationService(ble, "DELTA", "NQ620", "SN1", "hw-rev1", "fw-rev1", "soft-rev1"); @@ -79,7 +111,6 @@ Ticker ticker; ticker.attach(periodicCallback, 1); // blink LED every second - BLE& ble = BLE::Instance(BLE::DEFAULT_INSTANCE); ble.init(bleInitComplete); /* SpinWait for initialization to complete. This is necessary because the @@ -91,7 +122,7 @@ // infinite loop while (1) { // check for trigger from periodicCallback() - if (triggerSensorPolling && ble.getGapState().connected) { + if (triggerSensorPolling && ble.getGapState().connected && isSecuritySuccess) { triggerSensorPolling = false; // Do blocking calls or whatever is necessary for sensor polling. @@ -101,7 +132,7 @@ hrmCounter = 100; } - hrService->updateHeartRate(hrmCounter); + hrSecService->updateHeartRate(hrmCounter); } else { ble.waitForEvent(); // low power wait for event }