BLE heartRate demo with security

Dependencies:   BLE_API mbed nRF51822

Committer:
sunsmile2015
Date:
Wed Jun 03 09:11:21 2015 +0000
Revision:
2:2d9d4b271af8
Parent:
1:52c48814da8e
1.add comment; 2.add heartRate service with security; 3.print passkey as ASCII

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"
sunsmile2015 0:4f367a344480 18 #include "BLEDevice.h"
sunsmile2015 2:2d9d4b271af8 19 #include "HeartRateSecSampleSrv.h"
sunsmile2015 0:4f367a344480 20 #include "DeviceInformationService.h"
sunsmile2015 0:4f367a344480 21
sunsmile2015 0:4f367a344480 22 /* Enable the following if you need to throttle the connection interval. This has
sunsmile2015 0:4f367a344480 23 * the effect of reducing energy consumption after a connection is made;
sunsmile2015 0:4f367a344480 24 * particularly for applications where the central may want a fast connection
sunsmile2015 0:4f367a344480 25 * interval.*/
sunsmile2015 0:4f367a344480 26 #define UPDATE_PARAMS_FOR_LONGER_CONNECTION_INTERVAL 0
sunsmile2015 0:4f367a344480 27
sunsmile2015 0:4f367a344480 28 BLEDevice ble;
sunsmile2015 0:4f367a344480 29 DigitalOut led1(LED1);
sunsmile2015 0:4f367a344480 30
sunsmile2015 2:2d9d4b271af8 31 const static char DEVICE_NAME[] = "HRM_SEC";
sunsmile2015 0:4f367a344480 32 static const uint16_t uuid16_list[] = {GattService::UUID_HEART_RATE_SERVICE,
sunsmile2015 0:4f367a344480 33 GattService::UUID_DEVICE_INFORMATION_SERVICE};
sunsmile2015 0:4f367a344480 34 static volatile bool triggerSensorPolling = false;
sunsmile2015 0:4f367a344480 35
sunsmile2015 0:4f367a344480 36 void disconnectionCallback(Gap::Handle_t handle, Gap::DisconnectionReason_t reason)
sunsmile2015 0:4f367a344480 37 {
sunsmile2015 2:2d9d4b271af8 38 printf("Disconnected!\r\n");
sunsmile2015 0:4f367a344480 39 ble.startAdvertising(); // restart advertising
sunsmile2015 0:4f367a344480 40 }
sunsmile2015 0:4f367a344480 41
sunsmile2015 2:2d9d4b271af8 42
sunsmile2015 0:4f367a344480 43 void periodicCallback(void)
sunsmile2015 0:4f367a344480 44 {
sunsmile2015 0:4f367a344480 45 led1 = !led1; /* Do blinky on LED1 while we're waiting for BLE events */
sunsmile2015 0:4f367a344480 46
sunsmile2015 0:4f367a344480 47 /* Note that the periodicCallback() executes in interrupt context, so it is safer to do
sunsmile2015 0:4f367a344480 48 * heavy-weight sensor polling from the main thread. */
sunsmile2015 0:4f367a344480 49 triggerSensorPolling = true;
sunsmile2015 0:4f367a344480 50 }
sunsmile2015 2:2d9d4b271af8 51
sunsmile2015 1:52c48814da8e 52
sunsmile2015 1:52c48814da8e 53 void connectionCallback(Gap::Handle_t handle, Gap::addr_type_t peerAddrType, const Gap::Address_t peerAddr,
sunsmile2015 1:52c48814da8e 54 Gap::addr_type_t ownAddrType, const Gap::Address_t ownAddr,
sunsmile2015 1:52c48814da8e 55 const Gap::ConnectionParams_t * pParam)
sunsmile2015 1:52c48814da8e 56 {
sunsmile2015 2:2d9d4b271af8 57 printf("Connected!\r\n");
sunsmile2015 1:52c48814da8e 58 }
sunsmile2015 1:52c48814da8e 59
sunsmile2015 2:2d9d4b271af8 60
sunsmile2015 1:52c48814da8e 61 void passkeyDisplayCallback(Gap::Handle_t handle, const Gap::Passkey_t passkey)
sunsmile2015 1:52c48814da8e 62 {
sunsmile2015 1:52c48814da8e 63 uint8_t i;
sunsmile2015 1:52c48814da8e 64
sunsmile2015 1:52c48814da8e 65 printf("Input passKey: ");
sunsmile2015 1:52c48814da8e 66 for(i = 0; i < 6; i++) {
sunsmile2015 2:2d9d4b271af8 67 printf("%c ", passkey[i]);
sunsmile2015 2:2d9d4b271af8 68 }
sunsmile2015 2:2d9d4b271af8 69 printf("\r\n");
sunsmile2015 2:2d9d4b271af8 70 }
sunsmile2015 2:2d9d4b271af8 71
sunsmile2015 2:2d9d4b271af8 72
sunsmile2015 2:2d9d4b271af8 73 void securitySetupCompletedCallback(Gap::Handle_t handle, Gap::SecurityCompletionStatus_t status)
sunsmile2015 2:2d9d4b271af8 74 {
sunsmile2015 2:2d9d4b271af8 75 if(!status) {
sunsmile2015 2:2d9d4b271af8 76 printf("Security success\r\n", status);
sunsmile2015 2:2d9d4b271af8 77 } else {
sunsmile2015 2:2d9d4b271af8 78 printf("Security failed\r\n", status);
sunsmile2015 1:52c48814da8e 79 }
sunsmile2015 1:52c48814da8e 80 }
sunsmile2015 1:52c48814da8e 81
sunsmile2015 0:4f367a344480 82
sunsmile2015 0:4f367a344480 83 int main(void)
sunsmile2015 0:4f367a344480 84 {
sunsmile2015 0:4f367a344480 85 led1 = 1;
sunsmile2015 0:4f367a344480 86 Ticker ticker;
sunsmile2015 0:4f367a344480 87 ticker.attach(periodicCallback, 1); // blink LED every second
sunsmile2015 2:2d9d4b271af8 88 printf("Started\r\n");
sunsmile2015 2:2d9d4b271af8 89
sunsmile2015 2:2d9d4b271af8 90 /* Initialize BLE module */
sunsmile2015 0:4f367a344480 91 ble.init();
sunsmile2015 2:2d9d4b271af8 92
sunsmile2015 2:2d9d4b271af8 93 /* Initialize BLE security */
sunsmile2015 1:52c48814da8e 94 ble.initializeSecurity(true, true, Gap::IO_CAPS_DISPLAY_ONLY, NULL);
sunsmile2015 2:2d9d4b271af8 95
sunsmile2015 2:2d9d4b271af8 96 /* Set callback functions */
sunsmile2015 1:52c48814da8e 97 ble.onConnection(connectionCallback);
sunsmile2015 0:4f367a344480 98 ble.onDisconnection(disconnectionCallback);
sunsmile2015 1:52c48814da8e 99 ble.onPasskeyDisplay(passkeyDisplayCallback);
sunsmile2015 2:2d9d4b271af8 100 ble.onSecuritySetupCompleted(securitySetupCompletedCallback);
sunsmile2015 2:2d9d4b271af8 101
sunsmile2015 0:4f367a344480 102 /* Setup primary service. */
sunsmile2015 0:4f367a344480 103 uint8_t hrmCounter = 100; // init HRM to 100bps
sunsmile2015 2:2d9d4b271af8 104 HeartRateSecService hrService(ble, hrmCounter, HeartRateSecService::LOCATION_FINGER);
sunsmile2015 0:4f367a344480 105
sunsmile2015 0:4f367a344480 106 /* Setup auxiliary service. */
sunsmile2015 0:4f367a344480 107 DeviceInformationService deviceInfo(ble, "ARM", "Model1", "SN1", "hw-rev1", "fw-rev1", "soft-rev1");
sunsmile2015 0:4f367a344480 108
sunsmile2015 0:4f367a344480 109 /* Setup advertising. */
sunsmile2015 0:4f367a344480 110 ble.accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE);
sunsmile2015 0:4f367a344480 111 ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS, (uint8_t *)uuid16_list, sizeof(uuid16_list));
sunsmile2015 0:4f367a344480 112 ble.accumulateAdvertisingPayload(GapAdvertisingData::GENERIC_HEART_RATE_SENSOR);
sunsmile2015 0:4f367a344480 113 ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME));
sunsmile2015 0:4f367a344480 114 ble.setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
sunsmile2015 0:4f367a344480 115 ble.setAdvertisingInterval(1000);
sunsmile2015 1:52c48814da8e 116
sunsmile2015 0:4f367a344480 117 ble.startAdvertising();
sunsmile2015 0:4f367a344480 118
sunsmile2015 0:4f367a344480 119 // infinite loop
sunsmile2015 0:4f367a344480 120 while (1) {
sunsmile2015 0:4f367a344480 121 // check for trigger from periodicCallback()
sunsmile2015 0:4f367a344480 122 if (triggerSensorPolling && ble.getGapState().connected) {
sunsmile2015 0:4f367a344480 123 triggerSensorPolling = false;
sunsmile2015 0:4f367a344480 124
sunsmile2015 0:4f367a344480 125 // Do blocking calls or whatever is necessary for sensor polling.
sunsmile2015 0:4f367a344480 126 // In our case, we simply update the HRM measurement.
sunsmile2015 0:4f367a344480 127 hrmCounter++;
sunsmile2015 0:4f367a344480 128
sunsmile2015 0:4f367a344480 129 // 100 <= HRM bps <=175
sunsmile2015 0:4f367a344480 130 if (hrmCounter == 175) {
sunsmile2015 0:4f367a344480 131 hrmCounter = 100;
sunsmile2015 0:4f367a344480 132 }
sunsmile2015 0:4f367a344480 133
sunsmile2015 0:4f367a344480 134 // update bps
sunsmile2015 0:4f367a344480 135 hrService.updateHeartRate(hrmCounter);
sunsmile2015 0:4f367a344480 136 } else {
sunsmile2015 0:4f367a344480 137 ble.waitForEvent(); // low power wait for event
sunsmile2015 0:4f367a344480 138 }
sunsmile2015 0:4f367a344480 139 }
sunsmile2015 0:4f367a344480 140 }