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