ble nano hid over gatt

Dependencies:   BLE_API mbed-dev nRF51822

Committer:
cho45
Date:
Mon Aug 22 13:06:35 2016 +0000
Revision:
18:5d0232c9e70e
Parent:
17:3233ee19f716
Child:
19:cd7f2fe05ae4
????????????????????????? slaveLatency ??????????

Who changed what in which revision?

UserRevisionLine numberNew contents of line
cho45 6:f1c3ea8bc850 1 #include "mbed.h"
cho45 6:f1c3ea8bc850 2 #include "BLE.h"
cho45 6:f1c3ea8bc850 3 #include "KeyboardService.h"
cho45 6:f1c3ea8bc850 4 #include "BatteryService.h"
cho45 6:f1c3ea8bc850 5 #include "DeviceInformationService.h"
cho45 15:70bf079d3ee1 6 #include "DFUService.h"
cho45 6:f1c3ea8bc850 7 #include "HIDController_BLE.h"
cho45 6:f1c3ea8bc850 8
cho45 6:f1c3ea8bc850 9 static const char MODEL_NAME[] = "keyboard";
cho45 6:f1c3ea8bc850 10 static const char SERIAL_NUMBER[] = "X00000";
cho45 6:f1c3ea8bc850 11 static const char HARDWARE_REVISION[] = "0.1";
cho45 6:f1c3ea8bc850 12 static const char FIRMWARE_REVISION[] = "0.1";
cho45 6:f1c3ea8bc850 13 static const char SOFTWARE_REVISION[] = "0.0";
cho45 6:f1c3ea8bc850 14
cho45 6:f1c3ea8bc850 15 static const uint8_t DEVICE_NAME[] = "my keyboard";
cho45 6:f1c3ea8bc850 16 static const uint8_t SHORT_DEVICE_NAME[] = "kbd1";
cho45 6:f1c3ea8bc850 17
cho45 6:f1c3ea8bc850 18 static const bool ENABLE_BONDING = true;
cho45 6:f1c3ea8bc850 19 static const bool REQUIRE_MITM = true;
cho45 6:f1c3ea8bc850 20 static const uint8_t PASSKEY[6] = {'1','2','3','4','5','6'}; // must be 6-digits number
cho45 6:f1c3ea8bc850 21
cho45 6:f1c3ea8bc850 22 static const uint16_t uuid16_list[] = {
cho45 6:f1c3ea8bc850 23 GattService::UUID_HUMAN_INTERFACE_DEVICE_SERVICE,
cho45 6:f1c3ea8bc850 24 GattService::UUID_DEVICE_INFORMATION_SERVICE,
cho45 6:f1c3ea8bc850 25 GattService::UUID_BATTERY_SERVICE
cho45 6:f1c3ea8bc850 26 };
cho45 6:f1c3ea8bc850 27
cho45 6:f1c3ea8bc850 28 static KeyboardService* keyboardService;
cho45 6:f1c3ea8bc850 29 static BatteryService* batteryService;
cho45 6:f1c3ea8bc850 30 static DeviceInformationService* deviceInformationService;
cho45 15:70bf079d3ee1 31 static DFUService* dfuService;
cho45 6:f1c3ea8bc850 32
cho45 13:b0ffdf2012b9 33 static BLEProtocol::Address_t peerAddress;
cho45 13:b0ffdf2012b9 34
cho45 6:f1c3ea8bc850 35 static void updateBatteryLevel() {
cho45 6:f1c3ea8bc850 36 if (!batteryService) return;
cho45 6:f1c3ea8bc850 37 static const float BATTERY_MAX = 2.4;
cho45 6:f1c3ea8bc850 38 static const float REFERNECE = 1.2;
cho45 6:f1c3ea8bc850 39 static const float PRESCALE = 3;
cho45 6:f1c3ea8bc850 40
cho45 6:f1c3ea8bc850 41 NRF_ADC->ENABLE = ADC_ENABLE_ENABLE_Enabled;
cho45 6:f1c3ea8bc850 42
cho45 6:f1c3ea8bc850 43 // Use internal 1.2V reference for batteryInput
cho45 6:f1c3ea8bc850 44 // 1/3 pre-scaled input and 1.2V internal band gap reference
cho45 6:f1c3ea8bc850 45 // ref. mbed-src/targets/hal/TARGET_NORDIC/TARGET_MCU_NRF51822/analogin_api.c
cho45 6:f1c3ea8bc850 46 NRF_ADC->CONFIG =
cho45 6:f1c3ea8bc850 47 (ADC_CONFIG_RES_10bit << ADC_CONFIG_RES_Pos) |
cho45 6:f1c3ea8bc850 48 // Use VDD 1/3 for input
cho45 6:f1c3ea8bc850 49 (ADC_CONFIG_INPSEL_SupplyOneThirdPrescaling << ADC_CONFIG_INPSEL_Pos) |
cho45 6:f1c3ea8bc850 50 // Use internal band gap for reference
cho45 6:f1c3ea8bc850 51 (ADC_CONFIG_REFSEL_VBG << ADC_CONFIG_REFSEL_Pos) |
cho45 6:f1c3ea8bc850 52 (ADC_CONFIG_EXTREFSEL_None << ADC_CONFIG_EXTREFSEL_Pos);
cho45 6:f1c3ea8bc850 53
cho45 6:f1c3ea8bc850 54 // Start ADC
cho45 6:f1c3ea8bc850 55 NRF_ADC->TASKS_START = 1;
cho45 6:f1c3ea8bc850 56 while (((NRF_ADC->BUSY & ADC_BUSY_BUSY_Msk) >> ADC_BUSY_BUSY_Pos) == ADC_BUSY_BUSY_Busy) {
cho45 6:f1c3ea8bc850 57 // busy loop
cho45 6:f1c3ea8bc850 58 }
cho45 6:f1c3ea8bc850 59
cho45 6:f1c3ea8bc850 60 // Read ADC result
cho45 6:f1c3ea8bc850 61 uint16_t raw10bit = static_cast<uint16_t>(NRF_ADC->RESULT);
cho45 6:f1c3ea8bc850 62 float ratio = raw10bit / static_cast<float>(1<<10);
cho45 6:f1c3ea8bc850 63
cho45 6:f1c3ea8bc850 64 float batteryVoltage = ratio * (REFERNECE * PRESCALE);
cho45 6:f1c3ea8bc850 65 float percentage = (batteryVoltage / BATTERY_MAX) * 100;
cho45 6:f1c3ea8bc850 66 if (percentage > 100) {
cho45 6:f1c3ea8bc850 67 percentage = 100;
cho45 6:f1c3ea8bc850 68 }
cho45 6:f1c3ea8bc850 69 printf("updateBatteryLevel %f V : %d/100\r\n", batteryVoltage, static_cast<uint8_t>(percentage));
cho45 6:f1c3ea8bc850 70 batteryService->updateBatteryLevel(static_cast<uint8_t>(percentage));
cho45 6:f1c3ea8bc850 71 }
cho45 6:f1c3ea8bc850 72
cho45 6:f1c3ea8bc850 73
cho45 6:f1c3ea8bc850 74 static void onConnect(const Gap::ConnectionCallbackParams_t *params) {
cho45 17:3233ee19f716 75 printf("onConnect: ");
cho45 17:3233ee19f716 76 for (unsigned i = 0; i < Gap::ADDR_LEN; i++) {
cho45 17:3233ee19f716 77 printf("%02x", params->peerAddr[i]);
cho45 17:3233ee19f716 78 }
cho45 17:3233ee19f716 79 printf("\r\n");
cho45 13:b0ffdf2012b9 80 peerAddress.type = params->peerAddrType;
cho45 13:b0ffdf2012b9 81 memcpy(peerAddress.address, params->peerAddr, Gap::ADDR_LEN);
cho45 6:f1c3ea8bc850 82 }
cho45 6:f1c3ea8bc850 83
cho45 6:f1c3ea8bc850 84 static void onDisconnect(const Gap::DisconnectionCallbackParams_t *params) {
cho45 6:f1c3ea8bc850 85 printf("onDisconnect\r\n");
cho45 6:f1c3ea8bc850 86 BLE::Instance(BLE::DEFAULT_INSTANCE).gap().startAdvertising();
cho45 6:f1c3ea8bc850 87 }
cho45 6:f1c3ea8bc850 88
cho45 6:f1c3ea8bc850 89 static void onTimeout(const Gap::TimeoutSource_t source) {
cho45 6:f1c3ea8bc850 90 printf("onTimeout\r\n");
cho45 6:f1c3ea8bc850 91 BLE::Instance(BLE::DEFAULT_INSTANCE).gap().startAdvertising();
cho45 6:f1c3ea8bc850 92 }
cho45 6:f1c3ea8bc850 93
cho45 6:f1c3ea8bc850 94 static void passkeyDisplayCallback(Gap::Handle_t handle, const SecurityManager::Passkey_t passkey) {
cho45 6:f1c3ea8bc850 95 printf("Input passKey: ");
cho45 6:f1c3ea8bc850 96 for (unsigned i = 0; i < Gap::ADDR_LEN; i++) {
cho45 6:f1c3ea8bc850 97 printf("%c", passkey[i]);
cho45 6:f1c3ea8bc850 98 }
cho45 6:f1c3ea8bc850 99 printf("\r\n");
cho45 6:f1c3ea8bc850 100 }
cho45 6:f1c3ea8bc850 101
cho45 6:f1c3ea8bc850 102 static void securitySetupCompletedCallback(Gap::Handle_t handle, SecurityManager::SecurityCompletionStatus_t status) {
cho45 6:f1c3ea8bc850 103 if (status == SecurityManager::SEC_STATUS_SUCCESS) {
cho45 6:f1c3ea8bc850 104 printf("Security success %d\r\n", status);
cho45 13:b0ffdf2012b9 105
cho45 13:b0ffdf2012b9 106 printf("Set whitelist\r\n");
cho45 13:b0ffdf2012b9 107 Gap::Whitelist_t whitelist;
cho45 13:b0ffdf2012b9 108 whitelist.size = 1;
cho45 13:b0ffdf2012b9 109 whitelist.capacity = 1;
cho45 13:b0ffdf2012b9 110 whitelist.addresses = &peerAddress;
cho45 13:b0ffdf2012b9 111
cho45 13:b0ffdf2012b9 112 BLE::Instance(BLE::DEFAULT_INSTANCE).gap().setWhitelist(whitelist);
cho45 13:b0ffdf2012b9 113 printf("Set Advertising Policy Mode\r\n");
cho45 13:b0ffdf2012b9 114 // BLE::Instance(BLE::DEFAULT_INSTANCE).gap().setAdvertisingPolicyMode(Gap::ADV_POLICY_FILTER_SCAN_REQS);
cho45 13:b0ffdf2012b9 115 // BLE::Instance(BLE::DEFAULT_INSTANCE).gap().setAdvertisingPolicyMode(Gap::ADV_POLICY_FILTER_CONN_REQS);
cho45 13:b0ffdf2012b9 116 BLE::Instance(BLE::DEFAULT_INSTANCE).gap().setAdvertisingPolicyMode(Gap::ADV_POLICY_FILTER_ALL_REQS);
cho45 13:b0ffdf2012b9 117
cho45 6:f1c3ea8bc850 118 } else {
cho45 6:f1c3ea8bc850 119 printf("Security failed %d\r\n", status);
cho45 6:f1c3ea8bc850 120 }
cho45 6:f1c3ea8bc850 121 }
cho45 6:f1c3ea8bc850 122
cho45 6:f1c3ea8bc850 123 static void securitySetupInitiatedCallback(Gap::Handle_t, bool allowBonding, bool requireMITM, SecurityManager::SecurityIOCapabilities_t iocaps) {
cho45 6:f1c3ea8bc850 124 printf("Security setup initiated\r\n");
cho45 6:f1c3ea8bc850 125 }
cho45 6:f1c3ea8bc850 126
cho45 6:f1c3ea8bc850 127 static void bleInitComplete(BLE::InitializationCompleteCallbackContext *params) {
cho45 6:f1c3ea8bc850 128 // https://developer.mbed.org/compiler/#nav:/keyboard/BLE_API/ble/blecommon.h;
cho45 6:f1c3ea8bc850 129 ble_error_t error;
cho45 6:f1c3ea8bc850 130 BLE &ble = params->ble;
cho45 16:345eebc4f259 131
cho45 16:345eebc4f259 132 /**< Minimum Connection Interval in 1.25 ms units, see BLE_GAP_CP_LIMITS.*/
cho45 18:5d0232c9e70e 133 uint16_t minConnectionInterval = Gap::MSEC_TO_GAP_DURATION_UNITS(24);
cho45 16:345eebc4f259 134 /**< Maximum Connection Interval in 1.25 ms units, see BLE_GAP_CP_LIMITS.*/
cho45 18:5d0232c9e70e 135 uint16_t maxConnectionInterval = Gap::MSEC_TO_GAP_DURATION_UNITS(44);
cho45 16:345eebc4f259 136 /**< Slave Latency in number of connection events, see BLE_GAP_CP_LIMITS.*/
cho45 18:5d0232c9e70e 137 uint16_t slaveLatency = 4;
cho45 16:345eebc4f259 138 /**< Connection Supervision Timeout in 10 ms units, see BLE_GAP_CP_LIMITS.*/
cho45 16:345eebc4f259 139 uint16_t connectionSupervisionTimeout = 32 * 100;
cho45 16:345eebc4f259 140 Gap::ConnectionParams_t connectionParams = {
cho45 16:345eebc4f259 141 minConnectionInterval,
cho45 16:345eebc4f259 142 maxConnectionInterval,
cho45 16:345eebc4f259 143 slaveLatency,
cho45 16:345eebc4f259 144 connectionSupervisionTimeout
cho45 16:345eebc4f259 145 };
cho45 16:345eebc4f259 146
cho45 6:f1c3ea8bc850 147 error = params->error;
cho45 6:f1c3ea8bc850 148 if (error != BLE_ERROR_NONE) {
cho45 6:f1c3ea8bc850 149 printf("error on ble.init() \r\n");
cho45 6:f1c3ea8bc850 150 goto return_error;
cho45 6:f1c3ea8bc850 151 }
cho45 6:f1c3ea8bc850 152
cho45 6:f1c3ea8bc850 153 ble.gap().onDisconnection(onDisconnect);
cho45 6:f1c3ea8bc850 154 ble.gap().onConnection(onConnect);
cho45 6:f1c3ea8bc850 155 ble.gap().onTimeout(onTimeout);
cho45 6:f1c3ea8bc850 156
cho45 17:3233ee19f716 157 // printf("setup ble security manager\r\n");
cho45 6:f1c3ea8bc850 158 ble.securityManager().onSecuritySetupInitiated(securitySetupInitiatedCallback);
cho45 6:f1c3ea8bc850 159 ble.securityManager().onPasskeyDisplay(passkeyDisplayCallback);
cho45 6:f1c3ea8bc850 160 ble.securityManager().onSecuritySetupCompleted(securitySetupCompletedCallback);
cho45 6:f1c3ea8bc850 161 // bonding with hard-coded passkey.
cho45 6:f1c3ea8bc850 162 error = ble.securityManager().init(ENABLE_BONDING, REQUIRE_MITM, SecurityManager::IO_CAPS_DISPLAY_ONLY, PASSKEY);
cho45 6:f1c3ea8bc850 163 if (error != BLE_ERROR_NONE) {
cho45 6:f1c3ea8bc850 164 printf("error on ble.securityManager().init()");
cho45 6:f1c3ea8bc850 165 goto return_error;
cho45 6:f1c3ea8bc850 166 }
cho45 16:345eebc4f259 167
cho45 6:f1c3ea8bc850 168
cho45 17:3233ee19f716 169 // printf("new KeyboardService\r\n");
cho45 6:f1c3ea8bc850 170 keyboardService = new KeyboardService(ble);
cho45 17:3233ee19f716 171 // printf("new DeviceInformationService\r\n");
cho45 6:f1c3ea8bc850 172 deviceInformationService = new DeviceInformationService(ble, "lowreal.net", MODEL_NAME, SERIAL_NUMBER, HARDWARE_REVISION, FIRMWARE_REVISION, SOFTWARE_REVISION);
cho45 17:3233ee19f716 173 // printf("new BatteryService\r\n");
cho45 6:f1c3ea8bc850 174 batteryService = new BatteryService(ble, 100);
cho45 16:345eebc4f259 175 /** TODO STUCK with BLE NANO
cho45 16:345eebc4f259 176 printf("new DFUService\r\n");
cho45 16:345eebc4f259 177 dfuService = new DFUService(ble);
cho45 16:345eebc4f259 178 */
cho45 16:345eebc4f259 179
cho45 6:f1c3ea8bc850 180 updateBatteryLevel();
cho45 16:345eebc4f259 181
cho45 17:3233ee19f716 182 //printf("setup connection params\r\n");
cho45 16:345eebc4f259 183
cho45 16:345eebc4f259 184 ble.gap().setPreferredConnectionParams(&connectionParams);
cho45 6:f1c3ea8bc850 185
cho45 17:3233ee19f716 186 // printf("general setup\r\n");
cho45 6:f1c3ea8bc850 187 error = ble.gap().accumulateAdvertisingPayload(
cho45 6:f1c3ea8bc850 188 GapAdvertisingData::BREDR_NOT_SUPPORTED |
cho45 6:f1c3ea8bc850 189 GapAdvertisingData::LE_GENERAL_DISCOVERABLE
cho45 6:f1c3ea8bc850 190 );
cho45 9:d1daefbf1fbd 191 // shoud be LE_LIMITED_DISCOVERABLE
cho45 9:d1daefbf1fbd 192 // error = ble.gap().accumulateAdvertisingPayload(
cho45 9:d1daefbf1fbd 193 // GapAdvertisingData::BREDR_NOT_SUPPORTED |
cho45 9:d1daefbf1fbd 194 // GapAdvertisingData::LE_LIMITED_DISCOVERABLE
cho45 9:d1daefbf1fbd 195 // );
cho45 6:f1c3ea8bc850 196 if (error != BLE_ERROR_NONE) goto return_error;
cho45 6:f1c3ea8bc850 197
cho45 17:3233ee19f716 198 // printf("set COMPLETE_LIST_16BIT_SERVICE_IDS\r\n");
cho45 6:f1c3ea8bc850 199 error = ble.gap().accumulateAdvertisingPayload(
cho45 6:f1c3ea8bc850 200 GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS,
cho45 6:f1c3ea8bc850 201 (uint8_t*)uuid16_list, sizeof(uuid16_list)
cho45 6:f1c3ea8bc850 202 );
cho45 6:f1c3ea8bc850 203 if (error != BLE_ERROR_NONE) goto return_error;
cho45 6:f1c3ea8bc850 204
cho45 17:3233ee19f716 205 // printf("set advertising\r\n");
cho45 6:f1c3ea8bc850 206 // see 5.1.2: HID over GATT Specification (pg. 25)
cho45 6:f1c3ea8bc850 207 ble.gap().setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
cho45 6:f1c3ea8bc850 208
cho45 17:3233ee19f716 209 // printf("set advertising interval\r\n");
cho45 9:d1daefbf1fbd 210 ble.gap().setAdvertisingInterval(30);
cho45 17:3233ee19f716 211 // printf("set advertising timeout\r\n");
cho45 9:d1daefbf1fbd 212 ble.gap().setAdvertisingTimeout(180);
cho45 6:f1c3ea8bc850 213
cho45 17:3233ee19f716 214 // printf("set keyboard\r\n");
cho45 6:f1c3ea8bc850 215 error = ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::KEYBOARD);
cho45 6:f1c3ea8bc850 216 if (error != BLE_ERROR_NONE) goto return_error;
cho45 6:f1c3ea8bc850 217
cho45 17:3233ee19f716 218 // printf("set complete local name\r\n");
cho45 6:f1c3ea8bc850 219 error = ble.gap().accumulateAdvertisingPayload(
cho45 6:f1c3ea8bc850 220 GapAdvertisingData::COMPLETE_LOCAL_NAME,
cho45 6:f1c3ea8bc850 221 DEVICE_NAME, sizeof(DEVICE_NAME)
cho45 6:f1c3ea8bc850 222 );
cho45 6:f1c3ea8bc850 223 if (error != BLE_ERROR_NONE) goto return_error;
cho45 6:f1c3ea8bc850 224
cho45 17:3233ee19f716 225 // printf("set device name\r\n");
cho45 6:f1c3ea8bc850 226 error = ble.gap().setDeviceName(DEVICE_NAME);
cho45 6:f1c3ea8bc850 227 if (error != BLE_ERROR_NONE) goto return_error;
cho45 6:f1c3ea8bc850 228 // ble.gap().setTxPower(-12);
cho45 6:f1c3ea8bc850 229
cho45 6:f1c3ea8bc850 230
cho45 6:f1c3ea8bc850 231 printf("advertising\r\n");
cho45 6:f1c3ea8bc850 232 error = ble.gap().startAdvertising();
cho45 6:f1c3ea8bc850 233 if (error != BLE_ERROR_NONE) goto return_error;
cho45 6:f1c3ea8bc850 234 return;
cho45 6:f1c3ea8bc850 235
cho45 6:f1c3ea8bc850 236 return_error:
cho45 6:f1c3ea8bc850 237 printf("error with %d\r\n", error);
cho45 6:f1c3ea8bc850 238 return;
cho45 6:f1c3ea8bc850 239 }
cho45 6:f1c3ea8bc850 240
cho45 6:f1c3ea8bc850 241 void HIDController::init() {
cho45 6:f1c3ea8bc850 242 // https://github.com/jpbrucker/BLE_HID/blob/master/examples/examples_common.cpp
cho45 6:f1c3ea8bc850 243 printf("ble.init\r\n");
cho45 6:f1c3ea8bc850 244
cho45 6:f1c3ea8bc850 245 BLE& ble = BLE::Instance(BLE::DEFAULT_INSTANCE);
cho45 6:f1c3ea8bc850 246 ble.init(bleInitComplete);
cho45 6:f1c3ea8bc850 247
cho45 6:f1c3ea8bc850 248 while (!ble.hasInitialized()) { }
cho45 6:f1c3ea8bc850 249
cho45 6:f1c3ea8bc850 250 printf("ble.hasIntialized\r\n");
cho45 6:f1c3ea8bc850 251 }
cho45 6:f1c3ea8bc850 252
cho45 10:1aed2481a743 253 void HIDController::waitForEvent() {
cho45 6:f1c3ea8bc850 254 BLE& ble = BLE::Instance(BLE::DEFAULT_INSTANCE);
cho45 6:f1c3ea8bc850 255 ble.waitForEvent();
cho45 6:f1c3ea8bc850 256 }
cho45 6:f1c3ea8bc850 257
cho45 6:f1c3ea8bc850 258 void HIDController::appendReportData(uint8_t key) {
cho45 6:f1c3ea8bc850 259 if (keyboardService) {
cho45 6:f1c3ea8bc850 260 keyboardService->appendReportData(key);
cho45 6:f1c3ea8bc850 261 }
cho45 6:f1c3ea8bc850 262 }
cho45 6:f1c3ea8bc850 263
cho45 6:f1c3ea8bc850 264 void HIDController::deleteReportData(uint8_t key) {
cho45 6:f1c3ea8bc850 265 if (keyboardService) {
cho45 6:f1c3ea8bc850 266 keyboardService->deleteReportData(key);
cho45 6:f1c3ea8bc850 267 }
cho45 6:f1c3ea8bc850 268 }
cho45 9:d1daefbf1fbd 269
cho45 9:d1daefbf1fbd 270 void HIDController::queueCurrentReportData() {
cho45 9:d1daefbf1fbd 271 if (keyboardService) {
cho45 9:d1daefbf1fbd 272 keyboardService->queueCurrentReportData();
cho45 9:d1daefbf1fbd 273 }
cho45 9:d1daefbf1fbd 274 }