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.
Dependencies: BLE_API mbed-dev nRF51822
HIDController_BLE.cpp@40:364deaa190fe, 2016-08-26 (annotated)
- Committer:
- cho45
- Date:
- Fri Aug 26 16:03:18 2016 +0000
- Revision:
- 40:364deaa190fe
- Parent:
- 39:b7889285c9ef
- Child:
- 42:2c3be8694896
timeout ???????????????
Who changed what in which revision?
| User | Revision | Line number | New 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 | 27:7370b8994603 | 34 | |
| cho45 | 27:7370b8994603 | 35 | static volatile Status_t controllerStatus; |
| cho45 | 13:b0ffdf2012b9 | 36 | |
| cho45 |
6:f1c3ea8bc850 | 37 | static void onConnect(const Gap::ConnectionCallbackParams_t *params) { |
| cho45 | 13:b0ffdf2012b9 | 38 | peerAddress.type = params->peerAddrType; |
| cho45 | 13:b0ffdf2012b9 | 39 | memcpy(peerAddress.address, params->peerAddr, Gap::ADDR_LEN); |
| cho45 | 34:7da766a8aa96 | 40 | // TODO whitelist に peerAddr が含まれていない場合 securitySetupCompletedCallback を待ってから CONNECTED にすべき |
| cho45 | 29:ec548c473d50 | 41 | controllerStatus = CONNECTED; |
| cho45 |
6:f1c3ea8bc850 | 42 | } |
| cho45 |
6:f1c3ea8bc850 | 43 | |
| cho45 |
6:f1c3ea8bc850 | 44 | static void onDisconnect(const Gap::DisconnectionCallbackParams_t *params) { |
| cho45 | 27:7370b8994603 | 45 | controllerStatus = DISCONNECTED; |
| cho45 | 39:b7889285c9ef | 46 | BLE& ble = BLE::Instance(BLE::DEFAULT_INSTANCE); |
| cho45 | 39:b7889285c9ef | 47 | ble.gap().setAdvertisingInterval(20); |
| cho45 | 39:b7889285c9ef | 48 | // printf("set advertising timeout\r\n"); |
| cho45 | 39:b7889285c9ef | 49 | ble.gap().setAdvertisingTimeout(30); |
| cho45 | 39:b7889285c9ef | 50 | ble.gap().startAdvertising(); |
| cho45 |
6:f1c3ea8bc850 | 51 | } |
| cho45 |
6:f1c3ea8bc850 | 52 | |
| cho45 |
6:f1c3ea8bc850 | 53 | static void onTimeout(const Gap::TimeoutSource_t source) { |
| cho45 | 32:6c0f43fda460 | 54 | controllerStatus = TIMEOUT; |
| 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 | 29:ec548c473d50 | 58 | /* |
| cho45 |
6:f1c3ea8bc850 | 59 | printf("Input passKey: "); |
| cho45 |
6:f1c3ea8bc850 | 60 | for (unsigned i = 0; i < Gap::ADDR_LEN; i++) { |
| cho45 |
6:f1c3ea8bc850 | 61 | printf("%c", passkey[i]); |
| cho45 |
6:f1c3ea8bc850 | 62 | } |
| cho45 |
6:f1c3ea8bc850 | 63 | printf("\r\n"); |
| cho45 | 29:ec548c473d50 | 64 | */ |
| cho45 |
6:f1c3ea8bc850 | 65 | } |
| cho45 |
6:f1c3ea8bc850 | 66 | |
| cho45 |
6:f1c3ea8bc850 | 67 | static void securitySetupCompletedCallback(Gap::Handle_t handle, SecurityManager::SecurityCompletionStatus_t status) { |
| cho45 |
6:f1c3ea8bc850 | 68 | if (status == SecurityManager::SEC_STATUS_SUCCESS) { |
| cho45 | 29:ec548c473d50 | 69 | /* |
| cho45 |
6:f1c3ea8bc850 | 70 | printf("Security success %d\r\n", status); |
| cho45 | 13:b0ffdf2012b9 | 71 | printf("Set whitelist\r\n"); |
| cho45 | 29:ec548c473d50 | 72 | */ |
| cho45 | 13:b0ffdf2012b9 | 73 | Gap::Whitelist_t whitelist; |
| cho45 | 13:b0ffdf2012b9 | 74 | whitelist.size = 1; |
| cho45 | 13:b0ffdf2012b9 | 75 | whitelist.capacity = 1; |
| cho45 | 13:b0ffdf2012b9 | 76 | whitelist.addresses = &peerAddress; |
| cho45 | 13:b0ffdf2012b9 | 77 | |
| cho45 | 13:b0ffdf2012b9 | 78 | BLE::Instance(BLE::DEFAULT_INSTANCE).gap().setWhitelist(whitelist); |
| cho45 | 29:ec548c473d50 | 79 | //printf("Set Advertising Policy Mode\r\n"); |
| cho45 | 13:b0ffdf2012b9 | 80 | // BLE::Instance(BLE::DEFAULT_INSTANCE).gap().setAdvertisingPolicyMode(Gap::ADV_POLICY_FILTER_SCAN_REQS); |
| cho45 | 13:b0ffdf2012b9 | 81 | // BLE::Instance(BLE::DEFAULT_INSTANCE).gap().setAdvertisingPolicyMode(Gap::ADV_POLICY_FILTER_CONN_REQS); |
| cho45 | 13:b0ffdf2012b9 | 82 | BLE::Instance(BLE::DEFAULT_INSTANCE).gap().setAdvertisingPolicyMode(Gap::ADV_POLICY_FILTER_ALL_REQS); |
| cho45 |
6:f1c3ea8bc850 | 83 | } else { |
| cho45 | 29:ec548c473d50 | 84 | //printf("Security failed %d\r\n", status); |
| cho45 |
6:f1c3ea8bc850 | 85 | } |
| cho45 |
6:f1c3ea8bc850 | 86 | } |
| cho45 |
6:f1c3ea8bc850 | 87 | |
| cho45 |
6:f1c3ea8bc850 | 88 | static void securitySetupInitiatedCallback(Gap::Handle_t, bool allowBonding, bool requireMITM, SecurityManager::SecurityIOCapabilities_t iocaps) { |
| cho45 | 29:ec548c473d50 | 89 | // printf("Security setup initiated\r\n"); |
| cho45 |
6:f1c3ea8bc850 | 90 | } |
| cho45 |
6:f1c3ea8bc850 | 91 | |
| cho45 |
6:f1c3ea8bc850 | 92 | static void bleInitComplete(BLE::InitializationCompleteCallbackContext *params) { |
| cho45 |
6:f1c3ea8bc850 | 93 | // https://developer.mbed.org/compiler/#nav:/keyboard/BLE_API/ble/blecommon.h; |
| cho45 |
6:f1c3ea8bc850 | 94 | ble_error_t error; |
| cho45 |
6:f1c3ea8bc850 | 95 | BLE &ble = params->ble; |
| cho45 | 23:b31957ce64e9 | 96 | |
| cho45 | 27:7370b8994603 | 97 | controllerStatus = DISCONNECTED; |
| cho45 | 16:345eebc4f259 | 98 | |
| cho45 | 16:345eebc4f259 | 99 | /**< Minimum Connection Interval in 1.25 ms units, see BLE_GAP_CP_LIMITS.*/ |
| cho45 | 29:ec548c473d50 | 100 | uint16_t minConnectionInterval = Gap::MSEC_TO_GAP_DURATION_UNITS(20); |
| cho45 | 16:345eebc4f259 | 101 | /**< Maximum Connection Interval in 1.25 ms units, see BLE_GAP_CP_LIMITS.*/ |
| cho45 | 29:ec548c473d50 | 102 | uint16_t maxConnectionInterval = Gap::MSEC_TO_GAP_DURATION_UNITS(40); |
| cho45 | 16:345eebc4f259 | 103 | /**< Slave Latency in number of connection events, see BLE_GAP_CP_LIMITS.*/ |
| cho45 | 29:ec548c473d50 | 104 | uint16_t slaveLatency = 4; |
| cho45 | 16:345eebc4f259 | 105 | /**< Connection Supervision Timeout in 10 ms units, see BLE_GAP_CP_LIMITS.*/ |
| cho45 | 16:345eebc4f259 | 106 | uint16_t connectionSupervisionTimeout = 32 * 100; |
| cho45 | 16:345eebc4f259 | 107 | Gap::ConnectionParams_t connectionParams = { |
| cho45 | 16:345eebc4f259 | 108 | minConnectionInterval, |
| cho45 | 16:345eebc4f259 | 109 | maxConnectionInterval, |
| cho45 | 16:345eebc4f259 | 110 | slaveLatency, |
| cho45 | 16:345eebc4f259 | 111 | connectionSupervisionTimeout |
| cho45 | 16:345eebc4f259 | 112 | }; |
| cho45 | 16:345eebc4f259 | 113 | |
| cho45 |
6:f1c3ea8bc850 | 114 | error = params->error; |
| cho45 |
6:f1c3ea8bc850 | 115 | if (error != BLE_ERROR_NONE) { |
| cho45 |
6:f1c3ea8bc850 | 116 | printf("error on ble.init() \r\n"); |
| cho45 |
6:f1c3ea8bc850 | 117 | goto return_error; |
| cho45 |
6:f1c3ea8bc850 | 118 | } |
| cho45 |
6:f1c3ea8bc850 | 119 | |
| cho45 |
6:f1c3ea8bc850 | 120 | ble.gap().onDisconnection(onDisconnect); |
| cho45 |
6:f1c3ea8bc850 | 121 | ble.gap().onConnection(onConnect); |
| cho45 |
6:f1c3ea8bc850 | 122 | ble.gap().onTimeout(onTimeout); |
| cho45 |
6:f1c3ea8bc850 | 123 | |
| cho45 | 17:3233ee19f716 | 124 | // printf("setup ble security manager\r\n"); |
| cho45 |
6:f1c3ea8bc850 | 125 | ble.securityManager().onSecuritySetupInitiated(securitySetupInitiatedCallback); |
| cho45 |
6:f1c3ea8bc850 | 126 | ble.securityManager().onPasskeyDisplay(passkeyDisplayCallback); |
| cho45 |
6:f1c3ea8bc850 | 127 | ble.securityManager().onSecuritySetupCompleted(securitySetupCompletedCallback); |
| cho45 |
6:f1c3ea8bc850 | 128 | // bonding with hard-coded passkey. |
| cho45 |
6:f1c3ea8bc850 | 129 | error = ble.securityManager().init(ENABLE_BONDING, REQUIRE_MITM, SecurityManager::IO_CAPS_DISPLAY_ONLY, PASSKEY); |
| cho45 |
6:f1c3ea8bc850 | 130 | if (error != BLE_ERROR_NONE) { |
| cho45 |
6:f1c3ea8bc850 | 131 | printf("error on ble.securityManager().init()"); |
| cho45 |
6:f1c3ea8bc850 | 132 | goto return_error; |
| cho45 |
6:f1c3ea8bc850 | 133 | } |
| cho45 | 16:345eebc4f259 | 134 | |
| cho45 |
6:f1c3ea8bc850 | 135 | |
| cho45 | 17:3233ee19f716 | 136 | // printf("new KeyboardService\r\n"); |
| cho45 |
6:f1c3ea8bc850 | 137 | keyboardService = new KeyboardService(ble); |
| cho45 | 17:3233ee19f716 | 138 | // printf("new DeviceInformationService\r\n"); |
| cho45 |
6:f1c3ea8bc850 | 139 | deviceInformationService = new DeviceInformationService(ble, "lowreal.net", MODEL_NAME, SERIAL_NUMBER, HARDWARE_REVISION, FIRMWARE_REVISION, SOFTWARE_REVISION); |
| cho45 | 17:3233ee19f716 | 140 | // printf("new BatteryService\r\n"); |
| cho45 |
6:f1c3ea8bc850 | 141 | batteryService = new BatteryService(ble, 100); |
| cho45 | 37:4ce71fa47fc3 | 142 | /** TODO |
| cho45 | 16:345eebc4f259 | 143 | printf("new DFUService\r\n"); |
| cho45 | 16:345eebc4f259 | 144 | dfuService = new DFUService(ble); |
| cho45 | 16:345eebc4f259 | 145 | */ |
| cho45 | 16:345eebc4f259 | 146 | |
| cho45 | 17:3233ee19f716 | 147 | //printf("setup connection params\r\n"); |
| cho45 | 16:345eebc4f259 | 148 | |
| cho45 | 16:345eebc4f259 | 149 | ble.gap().setPreferredConnectionParams(&connectionParams); |
| cho45 |
6:f1c3ea8bc850 | 150 | |
| cho45 | 17:3233ee19f716 | 151 | // printf("general setup\r\n"); |
| cho45 |
6:f1c3ea8bc850 | 152 | error = ble.gap().accumulateAdvertisingPayload( |
| cho45 |
6:f1c3ea8bc850 | 153 | GapAdvertisingData::BREDR_NOT_SUPPORTED | |
| cho45 |
6:f1c3ea8bc850 | 154 | GapAdvertisingData::LE_GENERAL_DISCOVERABLE |
| cho45 |
6:f1c3ea8bc850 | 155 | ); |
| cho45 |
9:d1daefbf1fbd | 156 | // shoud be LE_LIMITED_DISCOVERABLE |
| cho45 |
9:d1daefbf1fbd | 157 | // error = ble.gap().accumulateAdvertisingPayload( |
| cho45 |
9:d1daefbf1fbd | 158 | // GapAdvertisingData::BREDR_NOT_SUPPORTED | |
| cho45 |
9:d1daefbf1fbd | 159 | // GapAdvertisingData::LE_LIMITED_DISCOVERABLE |
| cho45 |
9:d1daefbf1fbd | 160 | // ); |
| cho45 |
6:f1c3ea8bc850 | 161 | if (error != BLE_ERROR_NONE) goto return_error; |
| cho45 |
6:f1c3ea8bc850 | 162 | |
| cho45 | 17:3233ee19f716 | 163 | // printf("set COMPLETE_LIST_16BIT_SERVICE_IDS\r\n"); |
| cho45 |
6:f1c3ea8bc850 | 164 | error = ble.gap().accumulateAdvertisingPayload( |
| cho45 |
6:f1c3ea8bc850 | 165 | GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS, |
| cho45 |
6:f1c3ea8bc850 | 166 | (uint8_t*)uuid16_list, sizeof(uuid16_list) |
| cho45 |
6:f1c3ea8bc850 | 167 | ); |
| cho45 |
6:f1c3ea8bc850 | 168 | if (error != BLE_ERROR_NONE) goto return_error; |
| cho45 |
6:f1c3ea8bc850 | 169 | |
| cho45 | 17:3233ee19f716 | 170 | // printf("set advertising\r\n"); |
| cho45 |
6:f1c3ea8bc850 | 171 | // see 5.1.2: HID over GATT Specification (pg. 25) |
| cho45 |
6:f1c3ea8bc850 | 172 | ble.gap().setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED); |
| cho45 |
6:f1c3ea8bc850 | 173 | |
| cho45 | 17:3233ee19f716 | 174 | // printf("set advertising interval\r\n"); |
| cho45 | 28:1f843a3daab0 | 175 | ble.gap().setAdvertisingInterval(20); |
| cho45 | 17:3233ee19f716 | 176 | // printf("set advertising timeout\r\n"); |
| cho45 | 38:115875b8cb6c | 177 | ble.gap().setAdvertisingTimeout(30); |
| cho45 | 28:1f843a3daab0 | 178 | |
| cho45 | 28:1f843a3daab0 | 179 | /* |
| cho45 | 28:1f843a3daab0 | 180 | ble.gap().setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_DIRECTED); |
| cho45 | 28:1f843a3daab0 | 181 | ble.gap().setAdvertisingTimeout(1.28); |
| cho45 | 28:1f843a3daab0 | 182 | */ |
| cho45 |
6:f1c3ea8bc850 | 183 | |
| cho45 | 17:3233ee19f716 | 184 | // printf("set keyboard\r\n"); |
| cho45 |
6:f1c3ea8bc850 | 185 | error = ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::KEYBOARD); |
| cho45 |
6:f1c3ea8bc850 | 186 | if (error != BLE_ERROR_NONE) goto return_error; |
| cho45 |
6:f1c3ea8bc850 | 187 | |
| cho45 | 17:3233ee19f716 | 188 | // printf("set complete local name\r\n"); |
| cho45 |
6:f1c3ea8bc850 | 189 | error = ble.gap().accumulateAdvertisingPayload( |
| cho45 |
6:f1c3ea8bc850 | 190 | GapAdvertisingData::COMPLETE_LOCAL_NAME, |
| cho45 |
6:f1c3ea8bc850 | 191 | DEVICE_NAME, sizeof(DEVICE_NAME) |
| cho45 |
6:f1c3ea8bc850 | 192 | ); |
| cho45 |
6:f1c3ea8bc850 | 193 | if (error != BLE_ERROR_NONE) goto return_error; |
| cho45 |
6:f1c3ea8bc850 | 194 | |
| cho45 | 17:3233ee19f716 | 195 | // printf("set device name\r\n"); |
| cho45 |
6:f1c3ea8bc850 | 196 | error = ble.gap().setDeviceName(DEVICE_NAME); |
| cho45 |
6:f1c3ea8bc850 | 197 | if (error != BLE_ERROR_NONE) goto return_error; |
| cho45 | 22:a78f0a91280a | 198 | /* (Valid values are -40, -20, -16, -12, -8, -4, 0, 4) */ |
| cho45 | 19:cd7f2fe05ae4 | 199 | ble.gap().setTxPower(0); |
| cho45 |
6:f1c3ea8bc850 | 200 | |
| cho45 | 32:6c0f43fda460 | 201 | ble.gap().setAdvertisingPolicyMode(Gap::ADV_POLICY_IGNORE_WHITELIST); |
| cho45 | 23:b31957ce64e9 | 202 | |
| cho45 | 32:6c0f43fda460 | 203 | // printf("advertising\r\n"); |
| cho45 |
6:f1c3ea8bc850 | 204 | error = ble.gap().startAdvertising(); |
| cho45 |
6:f1c3ea8bc850 | 205 | if (error != BLE_ERROR_NONE) goto return_error; |
| cho45 | 32:6c0f43fda460 | 206 | controllerStatus = ADVERTISING; |
| cho45 |
6:f1c3ea8bc850 | 207 | return; |
| cho45 |
6:f1c3ea8bc850 | 208 | |
| cho45 |
6:f1c3ea8bc850 | 209 | return_error: |
| cho45 |
6:f1c3ea8bc850 | 210 | printf("error with %d\r\n", error); |
| cho45 |
6:f1c3ea8bc850 | 211 | return; |
| cho45 |
6:f1c3ea8bc850 | 212 | } |
| cho45 |
6:f1c3ea8bc850 | 213 | |
| cho45 | 20:d8840ac38434 | 214 | bool HIDController::connected() { |
| cho45 | 27:7370b8994603 | 215 | return controllerStatus == CONNECTED; |
| cho45 | 27:7370b8994603 | 216 | } |
| cho45 | 27:7370b8994603 | 217 | |
| cho45 | 27:7370b8994603 | 218 | Status_t HIDController::status() { |
| cho45 | 27:7370b8994603 | 219 | return controllerStatus; |
| cho45 | 20:d8840ac38434 | 220 | } |
| cho45 | 20:d8840ac38434 | 221 | |
| cho45 | 32:6c0f43fda460 | 222 | const char* HIDController::statusString() { |
| cho45 | 32:6c0f43fda460 | 223 | static const char* disconnected = "disconnected"; |
| cho45 | 32:6c0f43fda460 | 224 | static const char* connecting = "connecting"; |
| cho45 | 32:6c0f43fda460 | 225 | static const char* connected = "connected"; |
| cho45 | 32:6c0f43fda460 | 226 | static const char* timeout = "timeout"; |
| cho45 | 32:6c0f43fda460 | 227 | static const char* advertising = "advertising"; |
| cho45 | 32:6c0f43fda460 | 228 | static const char* unknown = "unknown"; |
| cho45 | 32:6c0f43fda460 | 229 | |
| cho45 | 32:6c0f43fda460 | 230 | return controllerStatus == DISCONNECTED ? disconnected: |
| cho45 | 32:6c0f43fda460 | 231 | controllerStatus == CONNECTING ? connecting: |
| cho45 | 32:6c0f43fda460 | 232 | controllerStatus == CONNECTED ? connected: |
| cho45 | 32:6c0f43fda460 | 233 | controllerStatus == TIMEOUT ? timeout: |
| cho45 | 32:6c0f43fda460 | 234 | controllerStatus == ADVERTISING ? advertising: |
| cho45 | 32:6c0f43fda460 | 235 | unknown; |
| cho45 | 32:6c0f43fda460 | 236 | } |
| cho45 | 32:6c0f43fda460 | 237 | |
| cho45 |
6:f1c3ea8bc850 | 238 | void HIDController::init() { |
| cho45 |
6:f1c3ea8bc850 | 239 | // https://github.com/jpbrucker/BLE_HID/blob/master/examples/examples_common.cpp |
| cho45 |
6:f1c3ea8bc850 | 240 | printf("ble.init\r\n"); |
| cho45 |
6:f1c3ea8bc850 | 241 | |
| cho45 |
6:f1c3ea8bc850 | 242 | BLE& ble = BLE::Instance(BLE::DEFAULT_INSTANCE); |
| cho45 |
6:f1c3ea8bc850 | 243 | ble.init(bleInitComplete); |
| cho45 |
6:f1c3ea8bc850 | 244 | |
| cho45 |
6:f1c3ea8bc850 | 245 | while (!ble.hasInitialized()) { } |
| cho45 |
6:f1c3ea8bc850 | 246 | printf("ble.hasIntialized\r\n"); |
| cho45 |
6:f1c3ea8bc850 | 247 | } |
| cho45 |
6:f1c3ea8bc850 | 248 | |
| cho45 | 22:a78f0a91280a | 249 | |
| cho45 |
10:1aed2481a743 | 250 | void HIDController::waitForEvent() { |
| cho45 |
6:f1c3ea8bc850 | 251 | BLE& ble = BLE::Instance(BLE::DEFAULT_INSTANCE); |
| cho45 | 21:d801c32231b0 | 252 | keyboardService->stopReportTicker(); |
| cho45 |
6:f1c3ea8bc850 | 253 | ble.waitForEvent(); |
| cho45 |
6:f1c3ea8bc850 | 254 | } |
| cho45 |
6:f1c3ea8bc850 | 255 | |
| cho45 |
6:f1c3ea8bc850 | 256 | void HIDController::appendReportData(uint8_t key) { |
| cho45 |
6:f1c3ea8bc850 | 257 | if (keyboardService) { |
| cho45 |
6:f1c3ea8bc850 | 258 | keyboardService->appendReportData(key); |
| cho45 |
6:f1c3ea8bc850 | 259 | } |
| cho45 |
6:f1c3ea8bc850 | 260 | } |
| cho45 |
6:f1c3ea8bc850 | 261 | |
| cho45 |
6:f1c3ea8bc850 | 262 | void HIDController::deleteReportData(uint8_t key) { |
| cho45 |
6:f1c3ea8bc850 | 263 | if (keyboardService) { |
| cho45 |
6:f1c3ea8bc850 | 264 | keyboardService->deleteReportData(key); |
| cho45 |
6:f1c3ea8bc850 | 265 | } |
| cho45 |
6:f1c3ea8bc850 | 266 | } |
| cho45 |
9:d1daefbf1fbd | 267 | |
| cho45 |
9:d1daefbf1fbd | 268 | void HIDController::queueCurrentReportData() { |
| cho45 | 29:ec548c473d50 | 269 | if (!connected()) return; |
| cho45 |
9:d1daefbf1fbd | 270 | if (keyboardService) { |
| cho45 | 40:364deaa190fe | 271 | // printf("Q\r\n"); |
| cho45 |
9:d1daefbf1fbd | 272 | keyboardService->queueCurrentReportData(); |
| cho45 |
9:d1daefbf1fbd | 273 | } |
| cho45 |
9:d1daefbf1fbd | 274 | } |
| cho45 | 37:4ce71fa47fc3 | 275 | |
| cho45 | 37:4ce71fa47fc3 | 276 | void HIDController::updateBatteryLevel(uint8_t percentage) { |
| cho45 | 37:4ce71fa47fc3 | 277 | if (!batteryService) return; |
| cho45 | 37:4ce71fa47fc3 | 278 | batteryService->updateBatteryLevel(percentage); |
| cho45 | 37:4ce71fa47fc3 | 279 | } |
| cho45 | 37:4ce71fa47fc3 | 280 | |
| cho45 | 40:364deaa190fe | 281 | void HIDController::initializeConnection(bool ignoreWhiteList) { |
| cho45 | 38:115875b8cb6c | 282 | ble_error_t error; |
| cho45 | 38:115875b8cb6c | 283 | BLE& ble = BLE::Instance(BLE::DEFAULT_INSTANCE); |
| cho45 | 39:b7889285c9ef | 284 | ble.gap().setAdvertisingInterval(20); |
| cho45 | 39:b7889285c9ef | 285 | ble.gap().setAdvertisingTimeout(30); |
| cho45 | 40:364deaa190fe | 286 | if (ignoreWhiteList) { |
| cho45 | 40:364deaa190fe | 287 | ble.gap().setAdvertisingPolicyMode(Gap::ADV_POLICY_IGNORE_WHITELIST); |
| cho45 | 40:364deaa190fe | 288 | } |
| cho45 | 38:115875b8cb6c | 289 | |
| cho45 | 38:115875b8cb6c | 290 | // printf("advertising\r\n"); |
| cho45 | 38:115875b8cb6c | 291 | error = ble.gap().startAdvertising(); |
| cho45 | 38:115875b8cb6c | 292 | if (error != BLE_ERROR_NONE) goto return_error; |
| cho45 | 38:115875b8cb6c | 293 | controllerStatus = ADVERTISING; |
| cho45 | 38:115875b8cb6c | 294 | return; |
| cho45 | 38:115875b8cb6c | 295 | |
| cho45 | 38:115875b8cb6c | 296 | return_error: |
| cho45 | 38:115875b8cb6c | 297 | printf("error with %d\r\n", error); |
| cho45 | 38:115875b8cb6c | 298 | return; |
| cho45 | 38:115875b8cb6c | 299 | } |