A BLE HID controller implementation with communication over SPI
Dependencies: BLE_API BLE_HID mbed nRF51822
common.cpp@0:f21dc3a04d62, 2017-12-13 (annotated)
- Committer:
- mrhannah
- Date:
- Wed Dec 13 05:34:15 2017 +0000
- Revision:
- 0:f21dc3a04d62
Initial/(final) changes for project
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
mrhannah | 0:f21dc3a04d62 | 1 | /* mbed Microcontroller Library |
mrhannah | 0:f21dc3a04d62 | 2 | * Copyright (c) 2015 ARM Limited |
mrhannah | 0:f21dc3a04d62 | 3 | * |
mrhannah | 0:f21dc3a04d62 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
mrhannah | 0:f21dc3a04d62 | 5 | * you may not use this file except in compliance with the License. |
mrhannah | 0:f21dc3a04d62 | 6 | * You may obtain a copy of the License at |
mrhannah | 0:f21dc3a04d62 | 7 | * |
mrhannah | 0:f21dc3a04d62 | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
mrhannah | 0:f21dc3a04d62 | 9 | * |
mrhannah | 0:f21dc3a04d62 | 10 | * Unless required by applicable law or agreed to in writing, software |
mrhannah | 0:f21dc3a04d62 | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
mrhannah | 0:f21dc3a04d62 | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
mrhannah | 0:f21dc3a04d62 | 13 | * See the License for the specific language governing permissions and |
mrhannah | 0:f21dc3a04d62 | 14 | * limitations under the License. |
mrhannah | 0:f21dc3a04d62 | 15 | */ |
mrhannah | 0:f21dc3a04d62 | 16 | |
mrhannah | 0:f21dc3a04d62 | 17 | #include "ble/services/BatteryService.h" |
mrhannah | 0:f21dc3a04d62 | 18 | #include "ble/services/DeviceInformationService.h" |
mrhannah | 0:f21dc3a04d62 | 19 | |
mrhannah | 0:f21dc3a04d62 | 20 | #include "common.h" |
mrhannah | 0:f21dc3a04d62 | 21 | |
mrhannah | 0:f21dc3a04d62 | 22 | static void passkeyDisplayCallback(Gap::Handle_t handle, const SecurityManager::Passkey_t passkey) |
mrhannah | 0:f21dc3a04d62 | 23 | { |
mrhannah | 0:f21dc3a04d62 | 24 | printf("Input passKey: "); |
mrhannah | 0:f21dc3a04d62 | 25 | for (unsigned i = 0; i < Gap::ADDR_LEN; i++) { |
mrhannah | 0:f21dc3a04d62 | 26 | printf("%c", passkey[i]); |
mrhannah | 0:f21dc3a04d62 | 27 | } |
mrhannah | 0:f21dc3a04d62 | 28 | printf("\r\n"); |
mrhannah | 0:f21dc3a04d62 | 29 | } |
mrhannah | 0:f21dc3a04d62 | 30 | |
mrhannah | 0:f21dc3a04d62 | 31 | static void securitySetupCompletedCallback(Gap::Handle_t handle, SecurityManager::SecurityCompletionStatus_t status) |
mrhannah | 0:f21dc3a04d62 | 32 | { |
mrhannah | 0:f21dc3a04d62 | 33 | if (status == SecurityManager::SEC_STATUS_SUCCESS) { |
mrhannah | 0:f21dc3a04d62 | 34 | printf("Security success %d\r\n", status); |
mrhannah | 0:f21dc3a04d62 | 35 | } else { |
mrhannah | 0:f21dc3a04d62 | 36 | printf("Security failed %d\r\n", status); |
mrhannah | 0:f21dc3a04d62 | 37 | } |
mrhannah | 0:f21dc3a04d62 | 38 | } |
mrhannah | 0:f21dc3a04d62 | 39 | |
mrhannah | 0:f21dc3a04d62 | 40 | static void securitySetupInitiatedCallback(Gap::Handle_t, bool allowBonding, bool requireMITM, SecurityManager::SecurityIOCapabilities_t iocaps) |
mrhannah | 0:f21dc3a04d62 | 41 | { |
mrhannah | 0:f21dc3a04d62 | 42 | printf("Security setup initiated\r\n"); |
mrhannah | 0:f21dc3a04d62 | 43 | } |
mrhannah | 0:f21dc3a04d62 | 44 | |
mrhannah | 0:f21dc3a04d62 | 45 | void initializeSecurity(BLE &ble) |
mrhannah | 0:f21dc3a04d62 | 46 | { |
mrhannah | 0:f21dc3a04d62 | 47 | bool enableBonding = true; |
mrhannah | 0:f21dc3a04d62 | 48 | bool requireMITM = HID_SECURITY_REQUIRE_MITM; |
mrhannah | 0:f21dc3a04d62 | 49 | |
mrhannah | 0:f21dc3a04d62 | 50 | ble.securityManager().onSecuritySetupInitiated(securitySetupInitiatedCallback); |
mrhannah | 0:f21dc3a04d62 | 51 | ble.securityManager().onPasskeyDisplay(passkeyDisplayCallback); |
mrhannah | 0:f21dc3a04d62 | 52 | ble.securityManager().onSecuritySetupCompleted(securitySetupCompletedCallback); |
mrhannah | 0:f21dc3a04d62 | 53 | |
mrhannah | 0:f21dc3a04d62 | 54 | ble.securityManager().init(enableBonding, requireMITM, HID_SECURITY_IOCAPS); |
mrhannah | 0:f21dc3a04d62 | 55 | } |
mrhannah | 0:f21dc3a04d62 | 56 | |
mrhannah | 0:f21dc3a04d62 | 57 | void initializeHOGP(BLE &ble) |
mrhannah | 0:f21dc3a04d62 | 58 | { |
mrhannah | 0:f21dc3a04d62 | 59 | static const uint16_t uuid16_list[] = {GattService::UUID_HUMAN_INTERFACE_DEVICE_SERVICE, |
mrhannah | 0:f21dc3a04d62 | 60 | GattService::UUID_DEVICE_INFORMATION_SERVICE, |
mrhannah | 0:f21dc3a04d62 | 61 | GattService::UUID_BATTERY_SERVICE}; |
mrhannah | 0:f21dc3a04d62 | 62 | |
mrhannah | 0:f21dc3a04d62 | 63 | DeviceInformationService deviceInfo(ble, "ARM", "m1", "abc", "def", "ghi", "jkl"); |
mrhannah | 0:f21dc3a04d62 | 64 | |
mrhannah | 0:f21dc3a04d62 | 65 | BatteryService batteryInfo(ble, 80); |
mrhannah | 0:f21dc3a04d62 | 66 | |
mrhannah | 0:f21dc3a04d62 | 67 | ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | |
mrhannah | 0:f21dc3a04d62 | 68 | GapAdvertisingData::LE_GENERAL_DISCOVERABLE); |
mrhannah | 0:f21dc3a04d62 | 69 | ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS, |
mrhannah | 0:f21dc3a04d62 | 70 | (uint8_t *)uuid16_list, sizeof(uuid16_list)); |
mrhannah | 0:f21dc3a04d62 | 71 | |
mrhannah | 0:f21dc3a04d62 | 72 | // see 5.1.2: HID over GATT Specification (pg. 25) |
mrhannah | 0:f21dc3a04d62 | 73 | ble.gap().setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED); |
mrhannah | 0:f21dc3a04d62 | 74 | // 30ms to 50ms is recommended (5.1.2) |
mrhannah | 0:f21dc3a04d62 | 75 | ble.gap().setAdvertisingInterval(50); |
mrhannah | 0:f21dc3a04d62 | 76 | } |