back up of work during May 2019

Dependencies:   microbit

Committer:
tht216
Date:
Wed Jun 05 15:21:14 2019 +0000
Branch:
class_implmentation
Revision:
6:f372773ad32f
Parent:
1:c840c2b6f490
TODO:; 1. multi keypresses; 2. integration

Who changed what in which revision?

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