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