ble nano hid over gatt

Dependencies:   BLE_API mbed-dev nRF51822

Committer:
cho45
Date:
Wed Aug 31 12:31:14 2016 +0000
Revision:
59:2d6c0bff2151
Parent:
58:64df960619ce
Child:
60:b899414e1d34
??????

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 42:2c3be8694896 3
cho45 42:2c3be8694896 4 #include "config.h"
cho45 6:f1c3ea8bc850 5 #include "KeyboardService.h"
cho45 6:f1c3ea8bc850 6 #include "BatteryService.h"
cho45 6:f1c3ea8bc850 7 #include "DeviceInformationService.h"
cho45 15:70bf079d3ee1 8 #include "DFUService.h"
cho45 6:f1c3ea8bc850 9 #include "HIDController_BLE.h"
cho45 6:f1c3ea8bc850 10
cho45 59:2d6c0bff2151 11 static const char MANUFACTURERERS_NAME[] = "lowreal.net";
cho45 47:5bf2ae8cc710 12 static const char MODEL_NAME[] = "keble";
cho45 6:f1c3ea8bc850 13 static const char SERIAL_NUMBER[] = "X00000";
cho45 6:f1c3ea8bc850 14 static const char HARDWARE_REVISION[] = "0.1";
cho45 6:f1c3ea8bc850 15 static const char FIRMWARE_REVISION[] = "0.1";
cho45 6:f1c3ea8bc850 16 static const char SOFTWARE_REVISION[] = "0.0";
cho45 6:f1c3ea8bc850 17
cho45 47:5bf2ae8cc710 18 static const uint8_t DEVICE_NAME[] = "Keble";
cho45 6:f1c3ea8bc850 19
cho45 6:f1c3ea8bc850 20 static const bool ENABLE_BONDING = true;
cho45 6:f1c3ea8bc850 21 static const bool REQUIRE_MITM = true;
cho45 6:f1c3ea8bc850 22 static const uint8_t PASSKEY[6] = {'1','2','3','4','5','6'}; // must be 6-digits number
cho45 6:f1c3ea8bc850 23
cho45 6:f1c3ea8bc850 24 static const uint16_t uuid16_list[] = {
cho45 6:f1c3ea8bc850 25 GattService::UUID_HUMAN_INTERFACE_DEVICE_SERVICE,
cho45 6:f1c3ea8bc850 26 GattService::UUID_DEVICE_INFORMATION_SERVICE,
cho45 6:f1c3ea8bc850 27 GattService::UUID_BATTERY_SERVICE
cho45 6:f1c3ea8bc850 28 };
cho45 6:f1c3ea8bc850 29
cho45 6:f1c3ea8bc850 30 static KeyboardService* keyboardService;
cho45 6:f1c3ea8bc850 31 static BatteryService* batteryService;
cho45 6:f1c3ea8bc850 32 static DeviceInformationService* deviceInformationService;
cho45 48:d6938de02f62 33 // static DFUService* dfuService;
cho45 6:f1c3ea8bc850 34
cho45 59:2d6c0bff2151 35
cho45 13:b0ffdf2012b9 36 static BLEProtocol::Address_t peerAddress;
cho45 27:7370b8994603 37
cho45 27:7370b8994603 38 static volatile Status_t controllerStatus;
cho45 13:b0ffdf2012b9 39
cho45 6:f1c3ea8bc850 40 static void onConnect(const Gap::ConnectionCallbackParams_t *params) {
cho45 42:2c3be8694896 41 peerAddress.type = params->peerAddrType;
cho45 42:2c3be8694896 42 memcpy(peerAddress.address, params->peerAddr, Gap::ADDR_LEN);
cho45 42:2c3be8694896 43 // TODO whitelist に peerAddr が含まれていない場合 securitySetupCompletedCallback を待ってから CONNECTED にすべき
cho45 29:ec548c473d50 44 controllerStatus = CONNECTED;
cho45 6:f1c3ea8bc850 45 }
cho45 6:f1c3ea8bc850 46
cho45 6:f1c3ea8bc850 47 static void onDisconnect(const Gap::DisconnectionCallbackParams_t *params) {
cho45 27:7370b8994603 48 controllerStatus = DISCONNECTED;
cho45 42:2c3be8694896 49 DEBUG_PRINTF_BLE_INTERRUPT("onDisconnect\r\n");
cho45 6:f1c3ea8bc850 50 }
cho45 6:f1c3ea8bc850 51
cho45 6:f1c3ea8bc850 52 static void onTimeout(const Gap::TimeoutSource_t source) {
cho45 32:6c0f43fda460 53 controllerStatus = TIMEOUT;
cho45 42:2c3be8694896 54 DEBUG_PRINTF_BLE_INTERRUPT("onTimeout\r\n");
cho45 6:f1c3ea8bc850 55 }
cho45 6:f1c3ea8bc850 56
cho45 6:f1c3ea8bc850 57 static void passkeyDisplayCallback(Gap::Handle_t handle, const SecurityManager::Passkey_t passkey) {
cho45 42:2c3be8694896 58 DEBUG_PRINTF_BLE_INTERRUPT("Input passKey: ");
cho45 6:f1c3ea8bc850 59 for (unsigned i = 0; i < Gap::ADDR_LEN; i++) {
cho45 42:2c3be8694896 60 DEBUG_PRINTF_BLE_INTERRUPT("%c", passkey[i]);
cho45 6:f1c3ea8bc850 61 }
cho45 42:2c3be8694896 62 DEBUG_PRINTF_BLE_INTERRUPT("\r\n");
cho45 6:f1c3ea8bc850 63 }
cho45 6:f1c3ea8bc850 64
cho45 6:f1c3ea8bc850 65 static void securitySetupCompletedCallback(Gap::Handle_t handle, SecurityManager::SecurityCompletionStatus_t status) {
cho45 6:f1c3ea8bc850 66 if (status == SecurityManager::SEC_STATUS_SUCCESS) {
cho45 42:2c3be8694896 67 DEBUG_PRINTF_BLE_INTERRUPT("Security success %d\r\n", status);
cho45 42:2c3be8694896 68 DEBUG_PRINTF_BLE_INTERRUPT("Set whitelist\r\n");
cho45 13:b0ffdf2012b9 69 Gap::Whitelist_t whitelist;
cho45 13:b0ffdf2012b9 70 whitelist.size = 1;
cho45 13:b0ffdf2012b9 71 whitelist.capacity = 1;
cho45 13:b0ffdf2012b9 72 whitelist.addresses = &peerAddress;
cho45 13:b0ffdf2012b9 73
cho45 13:b0ffdf2012b9 74 BLE::Instance(BLE::DEFAULT_INSTANCE).gap().setWhitelist(whitelist);
cho45 42:2c3be8694896 75 DEBUG_PRINTF_BLE_INTERRUPT("Set Advertising Policy Mode\r\n");
cho45 13:b0ffdf2012b9 76 // BLE::Instance(BLE::DEFAULT_INSTANCE).gap().setAdvertisingPolicyMode(Gap::ADV_POLICY_FILTER_SCAN_REQS);
cho45 13:b0ffdf2012b9 77 // BLE::Instance(BLE::DEFAULT_INSTANCE).gap().setAdvertisingPolicyMode(Gap::ADV_POLICY_FILTER_CONN_REQS);
cho45 13:b0ffdf2012b9 78 BLE::Instance(BLE::DEFAULT_INSTANCE).gap().setAdvertisingPolicyMode(Gap::ADV_POLICY_FILTER_ALL_REQS);
cho45 6:f1c3ea8bc850 79 } else {
cho45 42:2c3be8694896 80 DEBUG_PRINTF_BLE_INTERRUPT("Security failed %d\r\n", status);
cho45 6:f1c3ea8bc850 81 }
cho45 6:f1c3ea8bc850 82 }
cho45 6:f1c3ea8bc850 83
cho45 6:f1c3ea8bc850 84 static void securitySetupInitiatedCallback(Gap::Handle_t, bool allowBonding, bool requireMITM, SecurityManager::SecurityIOCapabilities_t iocaps) {
cho45 42:2c3be8694896 85 DEBUG_PRINTF_BLE_INTERRUPT("Security setup initiated\r\n");
cho45 6:f1c3ea8bc850 86 }
cho45 6:f1c3ea8bc850 87
cho45 6:f1c3ea8bc850 88 static void bleInitComplete(BLE::InitializationCompleteCallbackContext *params) {
cho45 6:f1c3ea8bc850 89 // https://developer.mbed.org/compiler/#nav:/keyboard/BLE_API/ble/blecommon.h;
cho45 6:f1c3ea8bc850 90 ble_error_t error;
cho45 6:f1c3ea8bc850 91 BLE &ble = params->ble;
cho45 23:b31957ce64e9 92
cho45 27:7370b8994603 93 controllerStatus = DISCONNECTED;
cho45 16:345eebc4f259 94
cho45 16:345eebc4f259 95 /**< Minimum Connection Interval in 1.25 ms units, see BLE_GAP_CP_LIMITS.*/
cho45 29:ec548c473d50 96 uint16_t minConnectionInterval = Gap::MSEC_TO_GAP_DURATION_UNITS(20);
cho45 16:345eebc4f259 97 /**< Maximum Connection Interval in 1.25 ms units, see BLE_GAP_CP_LIMITS.*/
cho45 29:ec548c473d50 98 uint16_t maxConnectionInterval = Gap::MSEC_TO_GAP_DURATION_UNITS(40);
cho45 16:345eebc4f259 99 /**< Slave Latency in number of connection events, see BLE_GAP_CP_LIMITS.*/
cho45 50:c1382a0ff066 100 uint16_t slaveLatency = 10;
cho45 16:345eebc4f259 101 /**< Connection Supervision Timeout in 10 ms units, see BLE_GAP_CP_LIMITS.*/
cho45 16:345eebc4f259 102 uint16_t connectionSupervisionTimeout = 32 * 100;
cho45 16:345eebc4f259 103 Gap::ConnectionParams_t connectionParams = {
cho45 16:345eebc4f259 104 minConnectionInterval,
cho45 16:345eebc4f259 105 maxConnectionInterval,
cho45 16:345eebc4f259 106 slaveLatency,
cho45 16:345eebc4f259 107 connectionSupervisionTimeout
cho45 16:345eebc4f259 108 };
cho45 16:345eebc4f259 109
cho45 6:f1c3ea8bc850 110 error = params->error;
cho45 6:f1c3ea8bc850 111 if (error != BLE_ERROR_NONE) {
cho45 42:2c3be8694896 112 DEBUG_PRINTF_BLE("error on ble.init() \r\n");
cho45 6:f1c3ea8bc850 113 goto return_error;
cho45 6:f1c3ea8bc850 114 }
cho45 6:f1c3ea8bc850 115
cho45 6:f1c3ea8bc850 116 ble.gap().onDisconnection(onDisconnect);
cho45 6:f1c3ea8bc850 117 ble.gap().onConnection(onConnect);
cho45 6:f1c3ea8bc850 118 ble.gap().onTimeout(onTimeout);
cho45 6:f1c3ea8bc850 119
cho45 42:2c3be8694896 120 // DEBUG_PRINTF_BLE("setup ble security manager\r\n");
cho45 6:f1c3ea8bc850 121 ble.securityManager().onSecuritySetupInitiated(securitySetupInitiatedCallback);
cho45 6:f1c3ea8bc850 122 ble.securityManager().onPasskeyDisplay(passkeyDisplayCallback);
cho45 6:f1c3ea8bc850 123 ble.securityManager().onSecuritySetupCompleted(securitySetupCompletedCallback);
cho45 6:f1c3ea8bc850 124 // bonding with hard-coded passkey.
cho45 57:ae521136842c 125 error = ble.securityManager().init(ENABLE_BONDING, REQUIRE_MITM, SecurityManager::IO_CAPS_DISPLAY_ONLY, PASSKEY);
cho45 6:f1c3ea8bc850 126 if (error != BLE_ERROR_NONE) {
cho45 42:2c3be8694896 127 DEBUG_PRINTF_BLE("error on ble.securityManager().init()");
cho45 6:f1c3ea8bc850 128 goto return_error;
cho45 6:f1c3ea8bc850 129 }
cho45 16:345eebc4f259 130
cho45 6:f1c3ea8bc850 131
cho45 42:2c3be8694896 132 // DEBUG_PRINTF_BLE("new KeyboardService\r\n");
cho45 6:f1c3ea8bc850 133 keyboardService = new KeyboardService(ble);
cho45 42:2c3be8694896 134 // DEBUG_PRINTF_BLE("new DeviceInformationService\r\n");
cho45 59:2d6c0bff2151 135 deviceInformationService = new DeviceInformationService(ble, MANUFACTURERERS_NAME, MODEL_NAME, SERIAL_NUMBER, HARDWARE_REVISION, FIRMWARE_REVISION, SOFTWARE_REVISION);
cho45 42:2c3be8694896 136 // DEBUG_PRINTF_BLE("new BatteryService\r\n");
cho45 6:f1c3ea8bc850 137 batteryService = new BatteryService(ble, 100);
cho45 37:4ce71fa47fc3 138 /** TODO
cho45 42:2c3be8694896 139 DEBUG_PRINTF_BLE("new DFUService\r\n");
cho45 16:345eebc4f259 140 dfuService = new DFUService(ble);
cho45 16:345eebc4f259 141 */
cho45 16:345eebc4f259 142
cho45 42:2c3be8694896 143 //DEBUG_PRINTF_BLE("setup connection params\r\n");
cho45 16:345eebc4f259 144
cho45 16:345eebc4f259 145 ble.gap().setPreferredConnectionParams(&connectionParams);
cho45 6:f1c3ea8bc850 146
cho45 42:2c3be8694896 147 // DEBUG_PRINTF_BLE("general setup\r\n");
cho45 6:f1c3ea8bc850 148 error = ble.gap().accumulateAdvertisingPayload(
cho45 6:f1c3ea8bc850 149 GapAdvertisingData::BREDR_NOT_SUPPORTED |
cho45 6:f1c3ea8bc850 150 GapAdvertisingData::LE_GENERAL_DISCOVERABLE
cho45 6:f1c3ea8bc850 151 );
cho45 9:d1daefbf1fbd 152 // shoud be LE_LIMITED_DISCOVERABLE
cho45 9:d1daefbf1fbd 153 // error = ble.gap().accumulateAdvertisingPayload(
cho45 9:d1daefbf1fbd 154 // GapAdvertisingData::BREDR_NOT_SUPPORTED |
cho45 9:d1daefbf1fbd 155 // GapAdvertisingData::LE_LIMITED_DISCOVERABLE
cho45 9:d1daefbf1fbd 156 // );
cho45 6:f1c3ea8bc850 157 if (error != BLE_ERROR_NONE) goto return_error;
cho45 6:f1c3ea8bc850 158
cho45 42:2c3be8694896 159 // DEBUG_PRINTF_BLE("set COMPLETE_LIST_16BIT_SERVICE_IDS\r\n");
cho45 6:f1c3ea8bc850 160 error = ble.gap().accumulateAdvertisingPayload(
cho45 6:f1c3ea8bc850 161 GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS,
cho45 6:f1c3ea8bc850 162 (uint8_t*)uuid16_list, sizeof(uuid16_list)
cho45 6:f1c3ea8bc850 163 );
cho45 6:f1c3ea8bc850 164 if (error != BLE_ERROR_NONE) goto return_error;
cho45 6:f1c3ea8bc850 165
cho45 42:2c3be8694896 166 // DEBUG_PRINTF_BLE("set advertising\r\n");
cho45 6:f1c3ea8bc850 167 // see 5.1.2: HID over GATT Specification (pg. 25)
cho45 6:f1c3ea8bc850 168 ble.gap().setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
cho45 6:f1c3ea8bc850 169
cho45 42:2c3be8694896 170 // DEBUG_PRINTF_BLE("set advertising interval\r\n");
cho45 28:1f843a3daab0 171 ble.gap().setAdvertisingInterval(20);
cho45 42:2c3be8694896 172 // DEBUG_PRINTF_BLE("set advertising timeout\r\n");
cho45 38:115875b8cb6c 173 ble.gap().setAdvertisingTimeout(30);
cho45 28:1f843a3daab0 174
cho45 28:1f843a3daab0 175 /*
cho45 28:1f843a3daab0 176 ble.gap().setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_DIRECTED);
cho45 28:1f843a3daab0 177 ble.gap().setAdvertisingTimeout(1.28);
cho45 28:1f843a3daab0 178 */
cho45 6:f1c3ea8bc850 179
cho45 42:2c3be8694896 180 // DEBUG_PRINTF_BLE("set keyboard\r\n");
cho45 6:f1c3ea8bc850 181 error = ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::KEYBOARD);
cho45 6:f1c3ea8bc850 182 if (error != BLE_ERROR_NONE) goto return_error;
cho45 6:f1c3ea8bc850 183
cho45 42:2c3be8694896 184 // DEBUG_PRINTF_BLE("set complete local name\r\n");
cho45 6:f1c3ea8bc850 185 error = ble.gap().accumulateAdvertisingPayload(
cho45 6:f1c3ea8bc850 186 GapAdvertisingData::COMPLETE_LOCAL_NAME,
cho45 6:f1c3ea8bc850 187 DEVICE_NAME, sizeof(DEVICE_NAME)
cho45 6:f1c3ea8bc850 188 );
cho45 6:f1c3ea8bc850 189 if (error != BLE_ERROR_NONE) goto return_error;
cho45 6:f1c3ea8bc850 190
cho45 42:2c3be8694896 191 // DEBUG_PRINTF_BLE("set device name\r\n");
cho45 6:f1c3ea8bc850 192 error = ble.gap().setDeviceName(DEVICE_NAME);
cho45 6:f1c3ea8bc850 193 if (error != BLE_ERROR_NONE) goto return_error;
cho45 22:a78f0a91280a 194 /* (Valid values are -40, -20, -16, -12, -8, -4, 0, 4) */
cho45 19:cd7f2fe05ae4 195 ble.gap().setTxPower(0);
cho45 6:f1c3ea8bc850 196
cho45 32:6c0f43fda460 197 ble.gap().setAdvertisingPolicyMode(Gap::ADV_POLICY_IGNORE_WHITELIST);
cho45 23:b31957ce64e9 198
cho45 42:2c3be8694896 199 // DEBUG_PRINTF_BLE("advertising\r\n");
cho45 6:f1c3ea8bc850 200 error = ble.gap().startAdvertising();
cho45 6:f1c3ea8bc850 201 if (error != BLE_ERROR_NONE) goto return_error;
cho45 32:6c0f43fda460 202 controllerStatus = ADVERTISING;
cho45 6:f1c3ea8bc850 203 return;
cho45 6:f1c3ea8bc850 204
cho45 6:f1c3ea8bc850 205 return_error:
cho45 42:2c3be8694896 206 DEBUG_PRINTF_BLE("error with %d\r\n", error);
cho45 6:f1c3ea8bc850 207 return;
cho45 6:f1c3ea8bc850 208 }
cho45 6:f1c3ea8bc850 209
cho45 20:d8840ac38434 210 bool HIDController::connected() {
cho45 27:7370b8994603 211 return controllerStatus == CONNECTED;
cho45 27:7370b8994603 212 }
cho45 27:7370b8994603 213
cho45 27:7370b8994603 214 Status_t HIDController::status() {
cho45 27:7370b8994603 215 return controllerStatus;
cho45 20:d8840ac38434 216 }
cho45 20:d8840ac38434 217
cho45 32:6c0f43fda460 218 const char* HIDController::statusString() {
cho45 59:2d6c0bff2151 219 static const char* const disconnected = "disconnected";
cho45 59:2d6c0bff2151 220 static const char* const connecting = "connecting";
cho45 59:2d6c0bff2151 221 static const char* const connected = "connected";
cho45 59:2d6c0bff2151 222 static const char* const timeout = "timeout";
cho45 59:2d6c0bff2151 223 static const char* const advertising = "advertising";
cho45 59:2d6c0bff2151 224 static const char* const unknown = "unknown";
cho45 32:6c0f43fda460 225
cho45 32:6c0f43fda460 226 return controllerStatus == DISCONNECTED ? disconnected:
cho45 32:6c0f43fda460 227 controllerStatus == CONNECTING ? connecting:
cho45 32:6c0f43fda460 228 controllerStatus == CONNECTED ? connected:
cho45 32:6c0f43fda460 229 controllerStatus == TIMEOUT ? timeout:
cho45 32:6c0f43fda460 230 controllerStatus == ADVERTISING ? advertising:
cho45 32:6c0f43fda460 231 unknown;
cho45 32:6c0f43fda460 232 }
cho45 32:6c0f43fda460 233
cho45 6:f1c3ea8bc850 234 void HIDController::init() {
cho45 6:f1c3ea8bc850 235 // https://github.com/jpbrucker/BLE_HID/blob/master/examples/examples_common.cpp
cho45 42:2c3be8694896 236 DEBUG_PRINTF_BLE("ble.init\r\n");
cho45 6:f1c3ea8bc850 237
cho45 6:f1c3ea8bc850 238 BLE& ble = BLE::Instance(BLE::DEFAULT_INSTANCE);
cho45 6:f1c3ea8bc850 239 ble.init(bleInitComplete);
cho45 46:b8f4c050739a 240
cho45 46:b8f4c050739a 241 // copied from https://github.com/lancaster-university/microbit-dal/commit/3c314794f07e5ac91331c9e9849475375708ec89
cho45 46:b8f4c050739a 242 // configure the stack to hold on to CPU during critical timing events.
cho45 46:b8f4c050739a 243 // mbed-classic performs __disabe_irq calls in its timers, which can cause MIC failures
cho45 46:b8f4c050739a 244 // on secure BLE channels.
cho45 46:b8f4c050739a 245 ble_common_opt_radio_cpu_mutex_t opt;
cho45 46:b8f4c050739a 246 opt.enable = 1;
cho45 46:b8f4c050739a 247 sd_ble_opt_set(BLE_COMMON_OPT_RADIO_CPU_MUTEX, (const ble_opt_t *)&opt);
cho45 46:b8f4c050739a 248
cho45 6:f1c3ea8bc850 249 while (!ble.hasInitialized()) { }
cho45 42:2c3be8694896 250 DEBUG_PRINTF_BLE("ble.hasIntialized\r\n");
cho45 6:f1c3ea8bc850 251 }
cho45 6:f1c3ea8bc850 252
cho45 22:a78f0a91280a 253
cho45 10:1aed2481a743 254 void HIDController::waitForEvent() {
cho45 6:f1c3ea8bc850 255 BLE& ble = BLE::Instance(BLE::DEFAULT_INSTANCE);
cho45 45:f4be69c936f6 256 keyboardService->processSend();
cho45 58:64df960619ce 257 if (DEBUG_BLE_INTERRUPT) {
cho45 58:64df960619ce 258 ble.waitForEvent();
cho45 58:64df960619ce 259 } else {
cho45 58:64df960619ce 260 // disable internal HFCLK RC Clock surely. It consume 1mA constantly
cho45 58:64df960619ce 261 // TWI / SPI / UART must be disabled and boot without debug mode
cho45 58:64df960619ce 262 while (NRF_UART0->EVENTS_TXDRDY != 1);
cho45 58:64df960619ce 263
cho45 58:64df960619ce 264 const uint32_t tx = NRF_UART0->PSELTXD;
cho45 58:64df960619ce 265
cho45 58:64df960619ce 266 NRF_UART0->TASKS_STOPTX = 1;
cho45 58:64df960619ce 267 NRF_UART0->ENABLE = (UART_ENABLE_ENABLE_Disabled << UART_ENABLE_ENABLE_Pos);
cho45 58:64df960619ce 268
cho45 58:64df960619ce 269 ble.waitForEvent();
cho45 58:64df960619ce 270
cho45 58:64df960619ce 271 NRF_UART0->ENABLE = (UART_ENABLE_ENABLE_Enabled << UART_ENABLE_ENABLE_Pos);
cho45 58:64df960619ce 272 NRF_UART0->TASKS_STARTTX = 1;
cho45 58:64df960619ce 273 // dummy send to wakeup...
cho45 58:64df960619ce 274 NRF_UART0->PSELTXD = 0xFFFFFFFF;
cho45 58:64df960619ce 275 NRF_UART0->EVENTS_TXDRDY = 0;
cho45 58:64df960619ce 276 NRF_UART0->TXD = 0;
cho45 58:64df960619ce 277 while (NRF_UART0->EVENTS_TXDRDY != 1);
cho45 58:64df960619ce 278 NRF_UART0->PSELTXD = tx;
cho45 58:64df960619ce 279 }
cho45 6:f1c3ea8bc850 280 }
cho45 6:f1c3ea8bc850 281
cho45 48:d6938de02f62 282 void HIDController::appendReportData(const uint8_t key) {
cho45 6:f1c3ea8bc850 283 if (keyboardService) {
cho45 6:f1c3ea8bc850 284 keyboardService->appendReportData(key);
cho45 6:f1c3ea8bc850 285 }
cho45 6:f1c3ea8bc850 286 }
cho45 6:f1c3ea8bc850 287
cho45 48:d6938de02f62 288 void HIDController::deleteReportData(const uint8_t key) {
cho45 6:f1c3ea8bc850 289 if (keyboardService) {
cho45 6:f1c3ea8bc850 290 keyboardService->deleteReportData(key);
cho45 6:f1c3ea8bc850 291 }
cho45 6:f1c3ea8bc850 292 }
cho45 9:d1daefbf1fbd 293
cho45 9:d1daefbf1fbd 294 void HIDController::queueCurrentReportData() {
cho45 29:ec548c473d50 295 if (!connected()) return;
cho45 9:d1daefbf1fbd 296 if (keyboardService) {
cho45 42:2c3be8694896 297 DEBUG_PRINTF_BLE("Q\r\n");
cho45 9:d1daefbf1fbd 298 keyboardService->queueCurrentReportData();
cho45 9:d1daefbf1fbd 299 }
cho45 9:d1daefbf1fbd 300 }
cho45 37:4ce71fa47fc3 301
cho45 58:64df960619ce 302 void HIDController::updateBatteryLevel(const uint8_t percentage, const uint16_t voltage) {
cho45 37:4ce71fa47fc3 303 if (!batteryService) return;
cho45 58:64df960619ce 304 batteryService->updateBatteryLevel(percentage, voltage);
cho45 37:4ce71fa47fc3 305 }
cho45 37:4ce71fa47fc3 306
cho45 48:d6938de02f62 307 void HIDController::initializeConnection(const bool ignoreWhiteList = false) {
cho45 38:115875b8cb6c 308 ble_error_t error;
cho45 38:115875b8cb6c 309 BLE& ble = BLE::Instance(BLE::DEFAULT_INSTANCE);
cho45 39:b7889285c9ef 310 ble.gap().setAdvertisingInterval(20);
cho45 39:b7889285c9ef 311 ble.gap().setAdvertisingTimeout(30);
cho45 40:364deaa190fe 312 if (ignoreWhiteList) {
cho45 40:364deaa190fe 313 ble.gap().setAdvertisingPolicyMode(Gap::ADV_POLICY_IGNORE_WHITELIST);
cho45 40:364deaa190fe 314 }
cho45 38:115875b8cb6c 315
cho45 42:2c3be8694896 316 // DEBUG_PRINTF_BLE("advertising\r\n");
cho45 38:115875b8cb6c 317 error = ble.gap().startAdvertising();
cho45 38:115875b8cb6c 318 if (error != BLE_ERROR_NONE) goto return_error;
cho45 38:115875b8cb6c 319 controllerStatus = ADVERTISING;
cho45 38:115875b8cb6c 320 return;
cho45 38:115875b8cb6c 321
cho45 38:115875b8cb6c 322 return_error:
cho45 42:2c3be8694896 323 DEBUG_PRINTF_BLE("error with %d\r\n", error);
cho45 38:115875b8cb6c 324 return;
cho45 42:2c3be8694896 325 }