Heart Rate Monitor with security API for Delta BLE platform
Fork of BLE_HeartRate_DELTA by
Diff: main.cpp
- Revision:
- 2:0737743120d7
- Parent:
- 1:82331af3e4c9
--- 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 }