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
main.cpp
00001 /* mbed Microcontroller Library 00002 * Copyright (c) 2006-2013 ARM Limited 00003 * 00004 * Licensed under the Apache License, Version 2.0 (the "License"); 00005 * you may not use this file except in compliance with the License. 00006 * You may obtain a copy of the License at 00007 * 00008 * http://www.apache.org/licenses/LICENSE-2.0 00009 * 00010 * Unless required by applicable law or agreed to in writing, software 00011 * distributed under the License is distributed on an "AS IS" BASIS, 00012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 00013 * See the License for the specific language governing permissions and 00014 * limitations under the License. 00015 */ 00016 00017 #include "mbed.h" 00018 #include "BLE.h" 00019 #include "HeartRateSecService.h" 00020 #include "DeviceInformationService.h" 00021 00022 BLE ble; 00023 DigitalOut led1(LED1); 00024 00025 const static char DEVICE_NAME[] = "UWOT"; 00026 static const uint16_t uuid16_list[] = {GattService::UUID_HEART_RATE_SERVICE, 00027 GattService::UUID_DEVICE_INFORMATION_SERVICE}; 00028 static volatile bool triggerSensorPolling = false; 00029 Gap::Handle_t connectionHandle; 00030 00031 void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params) 00032 { 00033 if (params->handle == connectionHandle) { 00034 printf("Disconnected from %i!\r\n", params->handle); 00035 connectionHandle = 0; 00036 } else { 00037 printf("Disconnected from handle %i while state claims handle %i\r\n", params->handle, connectionHandle); 00038 } 00039 ble.startAdvertising(); // restart advertising 00040 } 00041 00042 void periodicCallback(void) 00043 { 00044 led1 = !led1; /* Do blinky on LED1 while we're waiting for BLE events */ 00045 00046 /* Note that the periodicCallback() executes in interrupt context, so it is safer to do 00047 * heavy-weight sensor polling from the main thread. */ 00048 triggerSensorPolling = true; 00049 } 00050 00051 void connectionCallback(const Gap::ConnectionCallbackParams_t *params) 00052 { 00053 // connection handle is: 00054 connectionHandle = params ? params->handle : NULL; 00055 00056 printf("Connected to %i!\r\n", connectionHandle); 00057 } 00058 00059 void passkeyDisplayCallback(Gap::Handle_t handle, const SecurityManager::Passkey_t passkey) 00060 { 00061 printf("Input passKey: "); 00062 for (unsigned i = 0; i < Gap::ADDR_LEN; i++) { 00063 printf("%c ", passkey[i]); 00064 } 00065 printf("\r\n"); 00066 } 00067 00068 void securitySetupCompletedCallback(Gap::Handle_t handle, SecurityManager::SecurityCompletionStatus_t status) 00069 { 00070 if (status == SecurityManager::SEC_STATUS_SUCCESS) { 00071 printf("Security success\r\n"); 00072 } else { 00073 printf("Security failed\r\n"); 00074 } 00075 } 00076 00077 int main(void) 00078 { 00079 led1 = 1; 00080 Ticker ticker; 00081 ticker.attach(periodicCallback, 1); // blink LED every second 00082 00083 /* Initialize BLE module */ 00084 ble.init(); 00085 00086 /* Initialize BLE security */ 00087 bool enableBonding = true; 00088 bool requireMITM = true; 00089 uint8_t pass[] = {'0', '1', '2', '3', '4', '5', '6'}; 00090 ble.securityManager().init(enableBonding, requireMITM, SecurityManager::IO_CAPS_KEYBOARD_DISPLAY, pass); 00091 00092 /* Set callback functions */ 00093 ble.gap().onConnection(connectionCallback); 00094 ble.gap().onDisconnection(disconnectionCallback); 00095 ble.securityManager().onPasskeyDisplay(passkeyDisplayCallback); 00096 ble.securityManager().onSecuritySetupCompleted(securitySetupCompletedCallback); 00097 00098 /* Setup primary service. */ 00099 uint8_t hrmCounter = 100; // init HRM to 100bps 00100 HeartRateSecService hrService(ble, hrmCounter, HeartRateSecService::LOCATION_FINGER ); 00101 00102 /* Setup auxiliary service. */ 00103 DeviceInformationService deviceInfo(ble, "ARM", "Model1", "SN1", "hw-rev1", "fw-rev1", "soft-rev1"); 00104 00105 /* Setup advertising. */ 00106 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE); 00107 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS, (uint8_t *)uuid16_list, sizeof(uuid16_list)); 00108 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::GENERIC_HEART_RATE_SENSOR); 00109 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME)); 00110 ble.gap().setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED); 00111 ble.gap().setAdvertisingInterval(1000); 00112 00113 ble.gap().startAdvertising(); 00114 00115 // infinite loop 00116 while (1) { 00117 // check for trigger from periodicCallback() 00118 if (triggerSensorPolling && ble.getGapState().connected) { 00119 triggerSensorPolling = false; 00120 00121 // Do blocking calls or whatever is necessary for sensor polling. 00122 // In our case, we simply update the HRM measurement. 00123 hrmCounter++; 00124 00125 // 100 <= HRM bps <=175 00126 if (hrmCounter == 175) { 00127 hrmCounter = 100; 00128 } 00129 00130 // update bps 00131 hrService.updateHeartRate(hrmCounter); 00132 00133 // DEBUG output 00134 hrService.outputSecurityStatus(connectionHandle); 00135 } else { 00136 ble.waitForEvent(); // low power wait for event 00137 } 00138 } 00139 }
Generated on Mon Aug 15 2022 20:41:50 by 1.7.2