KeyboardStream example from BLE_HID

Dependencies:   BLE_API BLE_HID mbed nRF51822

This example is currently copied as-is from the github development repo.

Committer:
Jean-Philippe Brucker
Date:
Wed Oct 07 11:42:06 2015 +0100
Revision:
0:1484ce3a8cdf
Child:
2:a0b9f9aa0e2d
Initial commit, version 0.1

Who changed what in which revision?

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