Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Fork of BLE_HeartRate_DELTA by
main.cpp@2:0737743120d7, 2017-03-28 (annotated)
- Committer:
- silviaChen
- Date:
- Tue Mar 28 08:21:46 2017 +0000
- Revision:
- 2:0737743120d7
- Parent:
- 1:82331af3e4c9
Add BLE bonding/pairing function
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| silviaChen | 0:c7bcc0b36b5e | 1 | /* mbed Microcontroller Library |
| silviaChen | 0:c7bcc0b36b5e | 2 | * Copyright (c) 2006-2015 ARM Limited |
| silviaChen | 0:c7bcc0b36b5e | 3 | * |
| silviaChen | 0:c7bcc0b36b5e | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| silviaChen | 0:c7bcc0b36b5e | 5 | * you may not use this file except in compliance with the License. |
| silviaChen | 0:c7bcc0b36b5e | 6 | * You may obtain a copy of the License at |
| silviaChen | 0:c7bcc0b36b5e | 7 | * |
| silviaChen | 0:c7bcc0b36b5e | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| silviaChen | 0:c7bcc0b36b5e | 9 | * |
| silviaChen | 0:c7bcc0b36b5e | 10 | * Unless required by applicable law or agreed to in writing, software |
| silviaChen | 0:c7bcc0b36b5e | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| silviaChen | 0:c7bcc0b36b5e | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| silviaChen | 0:c7bcc0b36b5e | 13 | * See the License for the specific language governing permissions and |
| silviaChen | 0:c7bcc0b36b5e | 14 | * limitations under the License. |
| silviaChen | 0:c7bcc0b36b5e | 15 | */ |
| silviaChen | 0:c7bcc0b36b5e | 16 | |
| silviaChen | 0:c7bcc0b36b5e | 17 | #include "mbed.h" |
| silviaChen | 0:c7bcc0b36b5e | 18 | #include "ble/BLE.h" |
| silviaChen | 2:0737743120d7 | 19 | #include "HeartRateSecService.h" |
| silviaChen | 0:c7bcc0b36b5e | 20 | #include "ble/services/BatteryService.h" |
| silviaChen | 0:c7bcc0b36b5e | 21 | #include "ble/services/DeviceInformationService.h" |
| silviaChen | 0:c7bcc0b36b5e | 22 | |
| silviaChen | 2:0737743120d7 | 23 | BLE ble; |
| silviaChen | 0:c7bcc0b36b5e | 24 | DigitalOut led1(LED1); |
| silviaChen | 2:0737743120d7 | 25 | Serial uart(USBTX, USBRX, 115200); |
| silviaChen | 0:c7bcc0b36b5e | 26 | |
| silviaChen | 2:0737743120d7 | 27 | const static char DEVICE_NAME[] = "DELTA_HRM_SEC"; |
| silviaChen | 0:c7bcc0b36b5e | 28 | static const uint16_t uuid16_list[] = {GattService::UUID_HEART_RATE_SERVICE, |
| silviaChen | 0:c7bcc0b36b5e | 29 | GattService::UUID_DEVICE_INFORMATION_SERVICE}; |
| silviaChen | 0:c7bcc0b36b5e | 30 | static volatile bool triggerSensorPolling = false; |
| silviaChen | 2:0737743120d7 | 31 | bool isSecuritySuccess = false; |
| silviaChen | 0:c7bcc0b36b5e | 32 | |
| silviaChen | 0:c7bcc0b36b5e | 33 | uint8_t hrmCounter = 100; // init HRM to 100bps |
| silviaChen | 0:c7bcc0b36b5e | 34 | |
| silviaChen | 2:0737743120d7 | 35 | HeartRateSecService *hrSecService; |
| silviaChen | 0:c7bcc0b36b5e | 36 | DeviceInformationService *deviceInfo; |
| silviaChen | 0:c7bcc0b36b5e | 37 | |
| silviaChen | 0:c7bcc0b36b5e | 38 | void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params) |
| silviaChen | 0:c7bcc0b36b5e | 39 | { |
| silviaChen | 2:0737743120d7 | 40 | ble.gap().startAdvertising(); // restart advertising |
| silviaChen | 0:c7bcc0b36b5e | 41 | } |
| silviaChen | 0:c7bcc0b36b5e | 42 | |
| silviaChen | 0:c7bcc0b36b5e | 43 | void periodicCallback(void) |
| silviaChen | 0:c7bcc0b36b5e | 44 | { |
| silviaChen | 0:c7bcc0b36b5e | 45 | led1 = !led1; /* Do blinky on LED1 while we're waiting for BLE events */ |
| silviaChen | 0:c7bcc0b36b5e | 46 | |
| silviaChen | 0:c7bcc0b36b5e | 47 | /* Note that the periodicCallback() executes in interrupt context, so it is safer to do |
| silviaChen | 0:c7bcc0b36b5e | 48 | * heavy-weight sensor polling from the main thread. */ |
| silviaChen | 0:c7bcc0b36b5e | 49 | triggerSensorPolling = true; |
| silviaChen | 0:c7bcc0b36b5e | 50 | } |
| silviaChen | 0:c7bcc0b36b5e | 51 | |
| silviaChen | 2:0737743120d7 | 52 | void passkeyDisplayCallback(Gap::Handle_t handle, const SecurityManager::Passkey_t passkey) |
| silviaChen | 2:0737743120d7 | 53 | { |
| silviaChen | 2:0737743120d7 | 54 | uart.printf("Input passKey: "); |
| silviaChen | 2:0737743120d7 | 55 | for (unsigned i = 0; i < Gap::ADDR_LEN; i++) { |
| silviaChen | 2:0737743120d7 | 56 | uart.printf("%c ", passkey[i]); |
| silviaChen | 2:0737743120d7 | 57 | } |
| silviaChen | 2:0737743120d7 | 58 | uart.printf("\r\n"); |
| silviaChen | 2:0737743120d7 | 59 | } |
| silviaChen | 2:0737743120d7 | 60 | |
| silviaChen | 2:0737743120d7 | 61 | void securitySetupCompletedCallback(Gap::Handle_t handle, SecurityManager::SecurityCompletionStatus_t status) |
| silviaChen | 2:0737743120d7 | 62 | { |
| silviaChen | 2:0737743120d7 | 63 | if (status == SecurityManager::SEC_STATUS_SUCCESS) { |
| silviaChen | 2:0737743120d7 | 64 | uart.printf("Security success\r\n"); |
| silviaChen | 2:0737743120d7 | 65 | isSecuritySuccess = true; |
| silviaChen | 2:0737743120d7 | 66 | } else { |
| silviaChen | 2:0737743120d7 | 67 | uart.printf("Security failed\r\n"); |
| silviaChen | 2:0737743120d7 | 68 | isSecuritySuccess = false; |
| silviaChen | 2:0737743120d7 | 69 | ble.gap().disconnect(Gap::LOCAL_HOST_TERMINATED_CONNECTION); |
| silviaChen | 2:0737743120d7 | 70 | } |
| silviaChen | 2:0737743120d7 | 71 | } |
| silviaChen | 2:0737743120d7 | 72 | |
| silviaChen | 0:c7bcc0b36b5e | 73 | void bleInitComplete(BLE::InitializationCompleteCallbackContext *params) |
| silviaChen | 0:c7bcc0b36b5e | 74 | { |
| silviaChen | 0:c7bcc0b36b5e | 75 | BLE &ble = params->ble; |
| silviaChen | 0:c7bcc0b36b5e | 76 | ble_error_t error = params->error; |
| silviaChen | 0:c7bcc0b36b5e | 77 | |
| silviaChen | 0:c7bcc0b36b5e | 78 | if (error != BLE_ERROR_NONE) { |
| silviaChen | 0:c7bcc0b36b5e | 79 | return; |
| silviaChen | 0:c7bcc0b36b5e | 80 | } |
| silviaChen | 2:0737743120d7 | 81 | |
| silviaChen | 2:0737743120d7 | 82 | /* Initialize BLE security */ |
| silviaChen | 2:0737743120d7 | 83 | bool enableBonding = true; |
| silviaChen | 2:0737743120d7 | 84 | bool requireMITM = true; //Need passkey |
| silviaChen | 2:0737743120d7 | 85 | uint8_t pKey[6] = {'0', '0', '0', '0', '0', '0'}; //set the passkey |
| silviaChen | 2:0737743120d7 | 86 | ble.securityManager().init(enableBonding, requireMITM, SecurityManager::IO_CAPS_DISPLAY_ONLY, pKey); |
| silviaChen | 0:c7bcc0b36b5e | 87 | |
| silviaChen | 0:c7bcc0b36b5e | 88 | ble.gap().onDisconnection(disconnectionCallback); |
| silviaChen | 2:0737743120d7 | 89 | ble.securityManager().onPasskeyDisplay(passkeyDisplayCallback); |
| silviaChen | 2:0737743120d7 | 90 | ble.securityManager().onSecuritySetupCompleted(securitySetupCompletedCallback); |
| silviaChen | 0:c7bcc0b36b5e | 91 | |
| silviaChen | 0:c7bcc0b36b5e | 92 | /* Setup primary service. */ |
| silviaChen | 2:0737743120d7 | 93 | hrSecService = new HeartRateSecService(ble, hrmCounter, HeartRateSecService::LOCATION_FINGER); |
| silviaChen | 0:c7bcc0b36b5e | 94 | |
| silviaChen | 0:c7bcc0b36b5e | 95 | /* Setup auxiliary service. */ |
| silviaChen | 0:c7bcc0b36b5e | 96 | deviceInfo = new DeviceInformationService(ble, "DELTA", "NQ620", "SN1", "hw-rev1", "fw-rev1", "soft-rev1"); |
| silviaChen | 0:c7bcc0b36b5e | 97 | |
| silviaChen | 0:c7bcc0b36b5e | 98 | /* Setup advertising. */ |
| silviaChen | 0:c7bcc0b36b5e | 99 | ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE); |
| silviaChen | 0:c7bcc0b36b5e | 100 | ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS, (uint8_t *)uuid16_list, sizeof(uuid16_list)); |
| silviaChen | 0:c7bcc0b36b5e | 101 | ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::GENERIC_HEART_RATE_SENSOR); |
| silviaChen | 0:c7bcc0b36b5e | 102 | ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME)); |
| silviaChen | 0:c7bcc0b36b5e | 103 | ble.gap().setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED); |
| silviaChen | 0:c7bcc0b36b5e | 104 | ble.gap().setAdvertisingInterval(1000); /* 1000ms */ |
| silviaChen | 0:c7bcc0b36b5e | 105 | ble.gap().startAdvertising(); |
| silviaChen | 0:c7bcc0b36b5e | 106 | } |
| silviaChen | 0:c7bcc0b36b5e | 107 | |
| silviaChen | 0:c7bcc0b36b5e | 108 | int main(void) |
| silviaChen | 0:c7bcc0b36b5e | 109 | { |
| silviaChen | 0:c7bcc0b36b5e | 110 | led1 = 1; |
| silviaChen | 0:c7bcc0b36b5e | 111 | Ticker ticker; |
| silviaChen | 0:c7bcc0b36b5e | 112 | ticker.attach(periodicCallback, 1); // blink LED every second |
| silviaChen | 0:c7bcc0b36b5e | 113 | |
| silviaChen | 0:c7bcc0b36b5e | 114 | ble.init(bleInitComplete); |
| silviaChen | 0:c7bcc0b36b5e | 115 | |
| silviaChen | 0:c7bcc0b36b5e | 116 | /* SpinWait for initialization to complete. This is necessary because the |
| silviaChen | 0:c7bcc0b36b5e | 117 | * BLE object is used in the main loop below. */ |
| silviaChen | 0:c7bcc0b36b5e | 118 | while (ble.hasInitialized() == false) { /* spin loop */ } |
| silviaChen | 0:c7bcc0b36b5e | 119 | |
| tsungta | 1:82331af3e4c9 | 120 | ble.setTxPower(0); |
| tsungta | 1:82331af3e4c9 | 121 | |
| silviaChen | 0:c7bcc0b36b5e | 122 | // infinite loop |
| silviaChen | 0:c7bcc0b36b5e | 123 | while (1) { |
| silviaChen | 0:c7bcc0b36b5e | 124 | // check for trigger from periodicCallback() |
| silviaChen | 2:0737743120d7 | 125 | if (triggerSensorPolling && ble.getGapState().connected && isSecuritySuccess) { |
| silviaChen | 0:c7bcc0b36b5e | 126 | triggerSensorPolling = false; |
| silviaChen | 0:c7bcc0b36b5e | 127 | |
| silviaChen | 0:c7bcc0b36b5e | 128 | // Do blocking calls or whatever is necessary for sensor polling. |
| silviaChen | 0:c7bcc0b36b5e | 129 | // In our case, we simply update the HRM measurement. |
| silviaChen | 0:c7bcc0b36b5e | 130 | hrmCounter++; |
| silviaChen | 0:c7bcc0b36b5e | 131 | if (hrmCounter == 175) { // 100 <= HRM bps <=175 |
| silviaChen | 0:c7bcc0b36b5e | 132 | hrmCounter = 100; |
| silviaChen | 0:c7bcc0b36b5e | 133 | } |
| silviaChen | 0:c7bcc0b36b5e | 134 | |
| silviaChen | 2:0737743120d7 | 135 | hrSecService->updateHeartRate(hrmCounter); |
| silviaChen | 0:c7bcc0b36b5e | 136 | } else { |
| silviaChen | 0:c7bcc0b36b5e | 137 | ble.waitForEvent(); // low power wait for event |
| silviaChen | 0:c7bcc0b36b5e | 138 | } |
| silviaChen | 0:c7bcc0b36b5e | 139 | } |
| silviaChen | 0:c7bcc0b36b5e | 140 | } |
