Fork of BLE_SecureHeartRate : should this 'work' i.e. require a secure connection on an nRF51-DK? Hopefully I'm just missing something obvious - or is this broken?

Dependencies:   BLE_API mbed nRF51822

Fork of BLE_SecureHeartRate by Bluetooth Low Energy

Committer:
tomg
Date:
Fri May 27 16:55:05 2016 +0000
Revision:
11:70342535895d
Parent:
10:d391a5f1df7c
use IO_CAPS_KEYBOARD_DISPLAY (still doesn't work)

Who changed what in which revision?

UserRevisionLine numberNew contents of line
sunsmile2015 0:4f367a344480 1 /* mbed Microcontroller Library
sunsmile2015 0:4f367a344480 2 * Copyright (c) 2006-2013 ARM Limited
sunsmile2015 0:4f367a344480 3 *
sunsmile2015 0:4f367a344480 4 * Licensed under the Apache License, Version 2.0 (the "License");
sunsmile2015 0:4f367a344480 5 * you may not use this file except in compliance with the License.
sunsmile2015 0:4f367a344480 6 * You may obtain a copy of the License at
sunsmile2015 0:4f367a344480 7 *
sunsmile2015 0:4f367a344480 8 * http://www.apache.org/licenses/LICENSE-2.0
sunsmile2015 0:4f367a344480 9 *
sunsmile2015 0:4f367a344480 10 * Unless required by applicable law or agreed to in writing, software
sunsmile2015 0:4f367a344480 11 * distributed under the License is distributed on an "AS IS" BASIS,
sunsmile2015 0:4f367a344480 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
sunsmile2015 0:4f367a344480 13 * See the License for the specific language governing permissions and
sunsmile2015 0:4f367a344480 14 * limitations under the License.
sunsmile2015 0:4f367a344480 15 */
sunsmile2015 0:4f367a344480 16
sunsmile2015 0:4f367a344480 17 #include "mbed.h"
rgrover1 5:ed2cb43f3589 18 #include "BLE.h"
rgrover1 3:b852bf8d6fad 19 #include "HeartRateSecService.h"
sunsmile2015 0:4f367a344480 20 #include "DeviceInformationService.h"
sunsmile2015 0:4f367a344480 21
rgrover1 6:715150f7f293 22 BLE ble;
sunsmile2015 0:4f367a344480 23 DigitalOut led1(LED1);
sunsmile2015 0:4f367a344480 24
tomg 10:d391a5f1df7c 25 const static char DEVICE_NAME[] = "UWOT";
sunsmile2015 0:4f367a344480 26 static const uint16_t uuid16_list[] = {GattService::UUID_HEART_RATE_SERVICE,
sunsmile2015 0:4f367a344480 27 GattService::UUID_DEVICE_INFORMATION_SERVICE};
sunsmile2015 0:4f367a344480 28 static volatile bool triggerSensorPolling = false;
tomg 8:32cdc49c027e 29 Gap::Handle_t connectionHandle;
sunsmile2015 0:4f367a344480 30
rgrover1 7:e8dfe1c97c71 31 void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params)
sunsmile2015 0:4f367a344480 32 {
tomg 8:32cdc49c027e 33 if (params->handle == connectionHandle) {
tomg 8:32cdc49c027e 34 printf("Disconnected from %i!\r\n", params->handle);
tomg 8:32cdc49c027e 35 connectionHandle = 0;
tomg 8:32cdc49c027e 36 } else {
tomg 8:32cdc49c027e 37 printf("Disconnected from handle %i while state claims handle %i\r\n", params->handle, connectionHandle);
tomg 8:32cdc49c027e 38 }
sunsmile2015 0:4f367a344480 39 ble.startAdvertising(); // restart advertising
sunsmile2015 0:4f367a344480 40 }
sunsmile2015 0:4f367a344480 41
sunsmile2015 0:4f367a344480 42 void periodicCallback(void)
sunsmile2015 0:4f367a344480 43 {
sunsmile2015 0:4f367a344480 44 led1 = !led1; /* Do blinky on LED1 while we're waiting for BLE events */
sunsmile2015 0:4f367a344480 45
sunsmile2015 0:4f367a344480 46 /* Note that the periodicCallback() executes in interrupt context, so it is safer to do
sunsmile2015 0:4f367a344480 47 * heavy-weight sensor polling from the main thread. */
sunsmile2015 0:4f367a344480 48 triggerSensorPolling = true;
sunsmile2015 0:4f367a344480 49 }
sunsmile2015 2:2d9d4b271af8 50
rgrover1 5:ed2cb43f3589 51 void connectionCallback(const Gap::ConnectionCallbackParams_t *params)
sunsmile2015 1:52c48814da8e 52 {
tomg 8:32cdc49c027e 53 // connection handle is:
tomg 8:32cdc49c027e 54 connectionHandle = params ? params->handle : NULL;
tomg 8:32cdc49c027e 55
tomg 8:32cdc49c027e 56 printf("Connected to %i!\r\n", connectionHandle);
sunsmile2015 1:52c48814da8e 57 }
sunsmile2015 1:52c48814da8e 58
rgrover1 5:ed2cb43f3589 59 void passkeyDisplayCallback(Gap::Handle_t handle, const SecurityManager::Passkey_t passkey)
sunsmile2015 1:52c48814da8e 60 {
sunsmile2015 1:52c48814da8e 61 printf("Input passKey: ");
rgrover1 3:b852bf8d6fad 62 for (unsigned i = 0; i < Gap::ADDR_LEN; i++) {
sunsmile2015 2:2d9d4b271af8 63 printf("%c ", passkey[i]);
sunsmile2015 2:2d9d4b271af8 64 }
sunsmile2015 2:2d9d4b271af8 65 printf("\r\n");
sunsmile2015 2:2d9d4b271af8 66 }
sunsmile2015 2:2d9d4b271af8 67
rgrover1 5:ed2cb43f3589 68 void securitySetupCompletedCallback(Gap::Handle_t handle, SecurityManager::SecurityCompletionStatus_t status)
sunsmile2015 2:2d9d4b271af8 69 {
rgrover1 5:ed2cb43f3589 70 if (status == SecurityManager::SEC_STATUS_SUCCESS) {
rgrover1 7:e8dfe1c97c71 71 printf("Security success\r\n");
sunsmile2015 2:2d9d4b271af8 72 } else {
rgrover1 7:e8dfe1c97c71 73 printf("Security failed\r\n");
sunsmile2015 1:52c48814da8e 74 }
sunsmile2015 1:52c48814da8e 75 }
sunsmile2015 1:52c48814da8e 76
sunsmile2015 0:4f367a344480 77 int main(void)
sunsmile2015 0:4f367a344480 78 {
sunsmile2015 0:4f367a344480 79 led1 = 1;
sunsmile2015 0:4f367a344480 80 Ticker ticker;
sunsmile2015 0:4f367a344480 81 ticker.attach(periodicCallback, 1); // blink LED every second
sunsmile2015 2:2d9d4b271af8 82
sunsmile2015 2:2d9d4b271af8 83 /* Initialize BLE module */
sunsmile2015 0:4f367a344480 84 ble.init();
sunsmile2015 2:2d9d4b271af8 85
sunsmile2015 2:2d9d4b271af8 86 /* Initialize BLE security */
rgrover1 3:b852bf8d6fad 87 bool enableBonding = true;
rgrover1 3:b852bf8d6fad 88 bool requireMITM = true;
tomg 10:d391a5f1df7c 89 uint8_t pass[] = {'0', '1', '2', '3', '4', '5', '6'};
tomg 11:70342535895d 90 ble.securityManager().init(enableBonding, requireMITM, SecurityManager::IO_CAPS_KEYBOARD_DISPLAY, pass);
sunsmile2015 2:2d9d4b271af8 91
sunsmile2015 2:2d9d4b271af8 92 /* Set callback functions */
rgrover1 6:715150f7f293 93 ble.gap().onConnection(connectionCallback);
rgrover1 6:715150f7f293 94 ble.gap().onDisconnection(disconnectionCallback);
rgrover1 6:715150f7f293 95 ble.securityManager().onPasskeyDisplay(passkeyDisplayCallback);
rgrover1 6:715150f7f293 96 ble.securityManager().onSecuritySetupCompleted(securitySetupCompletedCallback);
sunsmile2015 2:2d9d4b271af8 97
sunsmile2015 0:4f367a344480 98 /* Setup primary service. */
sunsmile2015 0:4f367a344480 99 uint8_t hrmCounter = 100; // init HRM to 100bps
sunsmile2015 2:2d9d4b271af8 100 HeartRateSecService hrService(ble, hrmCounter, HeartRateSecService::LOCATION_FINGER);
sunsmile2015 0:4f367a344480 101
sunsmile2015 0:4f367a344480 102 /* Setup auxiliary service. */
sunsmile2015 0:4f367a344480 103 DeviceInformationService deviceInfo(ble, "ARM", "Model1", "SN1", "hw-rev1", "fw-rev1", "soft-rev1");
sunsmile2015 0:4f367a344480 104
sunsmile2015 0:4f367a344480 105 /* Setup advertising. */
rgrover1 6:715150f7f293 106 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE);
rgrover1 6:715150f7f293 107 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS, (uint8_t *)uuid16_list, sizeof(uuid16_list));
rgrover1 6:715150f7f293 108 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::GENERIC_HEART_RATE_SENSOR);
rgrover1 6:715150f7f293 109 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME));
rgrover1 6:715150f7f293 110 ble.gap().setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
rgrover1 6:715150f7f293 111 ble.gap().setAdvertisingInterval(1000);
sunsmile2015 1:52c48814da8e 112
rgrover1 6:715150f7f293 113 ble.gap().startAdvertising();
sunsmile2015 0:4f367a344480 114
sunsmile2015 0:4f367a344480 115 // infinite loop
sunsmile2015 0:4f367a344480 116 while (1) {
sunsmile2015 0:4f367a344480 117 // check for trigger from periodicCallback()
sunsmile2015 0:4f367a344480 118 if (triggerSensorPolling && ble.getGapState().connected) {
sunsmile2015 0:4f367a344480 119 triggerSensorPolling = false;
sunsmile2015 0:4f367a344480 120
sunsmile2015 0:4f367a344480 121 // Do blocking calls or whatever is necessary for sensor polling.
sunsmile2015 0:4f367a344480 122 // In our case, we simply update the HRM measurement.
sunsmile2015 0:4f367a344480 123 hrmCounter++;
sunsmile2015 0:4f367a344480 124
sunsmile2015 0:4f367a344480 125 // 100 <= HRM bps <=175
sunsmile2015 0:4f367a344480 126 if (hrmCounter == 175) {
sunsmile2015 0:4f367a344480 127 hrmCounter = 100;
sunsmile2015 0:4f367a344480 128 }
sunsmile2015 0:4f367a344480 129
sunsmile2015 0:4f367a344480 130 // update bps
sunsmile2015 0:4f367a344480 131 hrService.updateHeartRate(hrmCounter);
tomg 8:32cdc49c027e 132
tomg 8:32cdc49c027e 133 // DEBUG output
tomg 8:32cdc49c027e 134 hrService.outputSecurityStatus(connectionHandle);
sunsmile2015 0:4f367a344480 135 } else {
sunsmile2015 0:4f367a344480 136 ble.waitForEvent(); // low power wait for event
sunsmile2015 0:4f367a344480 137 }
sunsmile2015 0:4f367a344480 138 }
rgrover1 3:b852bf8d6fad 139 }