Bluetooth BLE HID Keyboard for the AlterErgo device, based on Seeed Studio Tiny BLE.

Dependencies:   BLE_API BLE_HID mbed nRF51822

Fork of BLENano_HID by Yuuichi Akagawa

Committer:
shervinemami
Date:
Sun Aug 26 10:19:01 2018 +0000
Revision:
1:c6659c8882c9
Parent:
0:3435302551b3
Initial working version of keyboard-only BLE HID

Who changed what in which revision?

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