Based on the example USB HID keyboard https://developer.mbed.org/users/jbru/code/BLE_HID_KeyboardStreamDemo/

Dependencies:   BLE_API mbed nRF51822

Fork of HID-kb by Microbug

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers examples_common.cpp Source File

examples_common.cpp

00001 /* mbed Microcontroller Library
00002  * Copyright (c) 2015 ARM Limited
00003  *
00004  * Licensed under the Apache License, Version 2.0 (the "License");
00005  * you may not use this file except in compliance with the License.
00006  * You may obtain a copy of the License at
00007  *
00008  *     http://www.apache.org/licenses/LICENSE-2.0
00009  *
00010  * Unless required by applicable law or agreed to in writing, software
00011  * distributed under the License is distributed on an "AS IS" BASIS,
00012  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013  * See the License for the specific language governing permissions and
00014  * limitations under the License.
00015  */
00016 
00017 #include "ble/services/BatteryService.h"
00018 
00019 #include "HIDDeviceInformationService.h"
00020 
00021 #include "examples_common.h"
00022 
00023 static void passkeyDisplayCallback(Gap::Handle_t handle, const SecurityManager::Passkey_t passkey)
00024 {
00025     printf("Input passKey: ");
00026     for (unsigned i = 0; i < Gap::ADDR_LEN; i++) {
00027         printf("%c", passkey[i]);
00028     }
00029     printf("\r\n");
00030 }
00031 
00032 static void securitySetupCompletedCallback(Gap::Handle_t handle, SecurityManager::SecurityCompletionStatus_t status)
00033 {
00034     if (status == SecurityManager::SEC_STATUS_SUCCESS) {
00035         printf("Security success %d\r\n", status);
00036     } else {
00037         printf("Security failed %d\r\n", status);
00038     }
00039 }
00040 
00041 static void securitySetupInitiatedCallback(Gap::Handle_t, bool allowBonding, bool requireMITM, SecurityManager::SecurityIOCapabilities_t iocaps)
00042 {
00043     printf("Security setup initiated\r\n");
00044 }
00045 
00046 void initializeSecurity(BLE &ble)
00047 {
00048     bool enableBonding = true;
00049     bool requireMITM = HID_SECURITY_REQUIRE_MITM;
00050 
00051     ble.securityManager().onSecuritySetupInitiated(securitySetupInitiatedCallback);
00052     ble.securityManager().onPasskeyDisplay(passkeyDisplayCallback);
00053     ble.securityManager().onSecuritySetupCompleted(securitySetupCompletedCallback);
00054 
00055     ble.securityManager().init(enableBonding, requireMITM, HID_SECURITY_IOCAPS);
00056 }
00057 
00058 void initializeHOGP(BLE &ble)
00059 {
00060     static const uint16_t uuid16_list[] =  {GattService::UUID_HUMAN_INTERFACE_DEVICE_SERVICE,
00061         GattService::UUID_DEVICE_INFORMATION_SERVICE,
00062         GattService::UUID_BATTERY_SERVICE};
00063 
00064     PnPID_t pnpID;
00065     pnpID.vendorID_source = 0x2; // from the USB Implementer's Forum
00066     pnpID.vendorID = 0x0D28; // NXP
00067     pnpID.productID = 0x0204; // CMSIS-DAP (well, it's a keyboard but oh well)
00068     pnpID.productVersion = 0x0100; // v1.0
00069     HIDDeviceInformationService deviceInfo(ble, "ARM", "m1", "abc", "def", "ghi", "jkl", &pnpID);
00070 
00071     BatteryService batteryInfo(ble, 80);
00072 
00073     ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED |
00074             GapAdvertisingData::LE_GENERAL_DISCOVERABLE);
00075     ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS,
00076             (uint8_t *)uuid16_list, sizeof(uuid16_list));
00077 
00078     // see 5.1.2: HID over GATT Specification (pg. 25)
00079     ble.gap().setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
00080     // 30ms to 50ms is recommended (5.1.2)
00081     ble.gap().setAdvertisingInterval(50);
00082 }
00083