A simple demo around security APIs. Submitted by Xiao Sun.

Dependencies:   BLE_API mbed nRF51822

Fork of BLE_HeartRate_AddSec by xiao sun

Committer:
rgrover1
Date:
Tue Sep 29 12:05:32 2015 +0000
Revision:
7:e8dfe1c97c71
Parent:
6:715150f7f293
updating underlying libraries.

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
sunsmile2015 2:2d9d4b271af8 25 const static char DEVICE_NAME[] = "HRM_SEC";
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;
sunsmile2015 0:4f367a344480 29
rgrover1 7:e8dfe1c97c71 30 void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params)
sunsmile2015 0:4f367a344480 31 {
sunsmile2015 2:2d9d4b271af8 32 printf("Disconnected!\r\n");
sunsmile2015 0:4f367a344480 33 ble.startAdvertising(); // restart advertising
sunsmile2015 0:4f367a344480 34 }
sunsmile2015 0:4f367a344480 35
sunsmile2015 0:4f367a344480 36 void periodicCallback(void)
sunsmile2015 0:4f367a344480 37 {
sunsmile2015 0:4f367a344480 38 led1 = !led1; /* Do blinky on LED1 while we're waiting for BLE events */
sunsmile2015 0:4f367a344480 39
sunsmile2015 0:4f367a344480 40 /* Note that the periodicCallback() executes in interrupt context, so it is safer to do
sunsmile2015 0:4f367a344480 41 * heavy-weight sensor polling from the main thread. */
sunsmile2015 0:4f367a344480 42 triggerSensorPolling = true;
sunsmile2015 0:4f367a344480 43 }
sunsmile2015 2:2d9d4b271af8 44
rgrover1 5:ed2cb43f3589 45 void connectionCallback(const Gap::ConnectionCallbackParams_t *params)
sunsmile2015 1:52c48814da8e 46 {
sunsmile2015 2:2d9d4b271af8 47 printf("Connected!\r\n");
sunsmile2015 1:52c48814da8e 48 }
sunsmile2015 1:52c48814da8e 49
rgrover1 5:ed2cb43f3589 50 void passkeyDisplayCallback(Gap::Handle_t handle, const SecurityManager::Passkey_t passkey)
sunsmile2015 1:52c48814da8e 51 {
sunsmile2015 1:52c48814da8e 52 printf("Input passKey: ");
rgrover1 3:b852bf8d6fad 53 for (unsigned i = 0; i < Gap::ADDR_LEN; i++) {
sunsmile2015 2:2d9d4b271af8 54 printf("%c ", passkey[i]);
sunsmile2015 2:2d9d4b271af8 55 }
sunsmile2015 2:2d9d4b271af8 56 printf("\r\n");
sunsmile2015 2:2d9d4b271af8 57 }
sunsmile2015 2:2d9d4b271af8 58
rgrover1 5:ed2cb43f3589 59 void securitySetupCompletedCallback(Gap::Handle_t handle, SecurityManager::SecurityCompletionStatus_t status)
sunsmile2015 2:2d9d4b271af8 60 {
rgrover1 5:ed2cb43f3589 61 if (status == SecurityManager::SEC_STATUS_SUCCESS) {
rgrover1 7:e8dfe1c97c71 62 printf("Security success\r\n");
sunsmile2015 2:2d9d4b271af8 63 } else {
rgrover1 7:e8dfe1c97c71 64 printf("Security failed\r\n");
sunsmile2015 1:52c48814da8e 65 }
sunsmile2015 1:52c48814da8e 66 }
sunsmile2015 1:52c48814da8e 67
sunsmile2015 0:4f367a344480 68 int main(void)
sunsmile2015 0:4f367a344480 69 {
sunsmile2015 0:4f367a344480 70 led1 = 1;
sunsmile2015 0:4f367a344480 71 Ticker ticker;
sunsmile2015 0:4f367a344480 72 ticker.attach(periodicCallback, 1); // blink LED every second
sunsmile2015 2:2d9d4b271af8 73
sunsmile2015 2:2d9d4b271af8 74 /* Initialize BLE module */
sunsmile2015 0:4f367a344480 75 ble.init();
sunsmile2015 2:2d9d4b271af8 76
sunsmile2015 2:2d9d4b271af8 77 /* Initialize BLE security */
rgrover1 3:b852bf8d6fad 78 bool enableBonding = true;
rgrover1 3:b852bf8d6fad 79 bool requireMITM = true;
rgrover1 6:715150f7f293 80 ble.securityManager().init(enableBonding, requireMITM, SecurityManager::IO_CAPS_DISPLAY_ONLY);
sunsmile2015 2:2d9d4b271af8 81
sunsmile2015 2:2d9d4b271af8 82 /* Set callback functions */
rgrover1 6:715150f7f293 83 ble.gap().onConnection(connectionCallback);
rgrover1 6:715150f7f293 84 ble.gap().onDisconnection(disconnectionCallback);
rgrover1 6:715150f7f293 85 ble.securityManager().onPasskeyDisplay(passkeyDisplayCallback);
rgrover1 6:715150f7f293 86 ble.securityManager().onSecuritySetupCompleted(securitySetupCompletedCallback);
sunsmile2015 2:2d9d4b271af8 87
sunsmile2015 0:4f367a344480 88 /* Setup primary service. */
sunsmile2015 0:4f367a344480 89 uint8_t hrmCounter = 100; // init HRM to 100bps
sunsmile2015 2:2d9d4b271af8 90 HeartRateSecService hrService(ble, hrmCounter, HeartRateSecService::LOCATION_FINGER);
sunsmile2015 0:4f367a344480 91
sunsmile2015 0:4f367a344480 92 /* Setup auxiliary service. */
sunsmile2015 0:4f367a344480 93 DeviceInformationService deviceInfo(ble, "ARM", "Model1", "SN1", "hw-rev1", "fw-rev1", "soft-rev1");
sunsmile2015 0:4f367a344480 94
sunsmile2015 0:4f367a344480 95 /* Setup advertising. */
rgrover1 6:715150f7f293 96 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE);
rgrover1 6:715150f7f293 97 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS, (uint8_t *)uuid16_list, sizeof(uuid16_list));
rgrover1 6:715150f7f293 98 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::GENERIC_HEART_RATE_SENSOR);
rgrover1 6:715150f7f293 99 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME));
rgrover1 6:715150f7f293 100 ble.gap().setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
rgrover1 6:715150f7f293 101 ble.gap().setAdvertisingInterval(1000);
sunsmile2015 1:52c48814da8e 102
rgrover1 6:715150f7f293 103 ble.gap().startAdvertising();
sunsmile2015 0:4f367a344480 104
sunsmile2015 0:4f367a344480 105 // infinite loop
sunsmile2015 0:4f367a344480 106 while (1) {
sunsmile2015 0:4f367a344480 107 // check for trigger from periodicCallback()
sunsmile2015 0:4f367a344480 108 if (triggerSensorPolling && ble.getGapState().connected) {
sunsmile2015 0:4f367a344480 109 triggerSensorPolling = false;
sunsmile2015 0:4f367a344480 110
sunsmile2015 0:4f367a344480 111 // Do blocking calls or whatever is necessary for sensor polling.
sunsmile2015 0:4f367a344480 112 // In our case, we simply update the HRM measurement.
sunsmile2015 0:4f367a344480 113 hrmCounter++;
sunsmile2015 0:4f367a344480 114
sunsmile2015 0:4f367a344480 115 // 100 <= HRM bps <=175
sunsmile2015 0:4f367a344480 116 if (hrmCounter == 175) {
sunsmile2015 0:4f367a344480 117 hrmCounter = 100;
sunsmile2015 0:4f367a344480 118 }
sunsmile2015 0:4f367a344480 119
sunsmile2015 0:4f367a344480 120 // update bps
sunsmile2015 0:4f367a344480 121 hrService.updateHeartRate(hrmCounter);
sunsmile2015 0:4f367a344480 122 } else {
sunsmile2015 0:4f367a344480 123 ble.waitForEvent(); // low power wait for event
sunsmile2015 0:4f367a344480 124 }
sunsmile2015 0:4f367a344480 125 }
rgrover1 3:b852bf8d6fad 126 }