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