forked from orklann/micro-bit-game-pad
-
Notifications
You must be signed in to change notification settings - Fork 1
/
HIDServiceBase.cpp
281 lines (230 loc) · 10.3 KB
/
HIDServiceBase.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
/* mbed Microcontroller Library
* Copyright (c) 2015 ARM Limited
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "mbed.h"
#include "HIDServiceBase.h"
#include "HIDDeviceInformationService.h"
#include "ble/services/BatteryService.h"
#include "ble/services/DeviceInformationService.h"
HIDServiceBase::HIDServiceBase(BLE &_ble,
report_map_t reportMap,
uint8_t reportMapSize,
report_t inputReport,
report_t outputReport,
report_t featureReport,
uint8_t inputReportLength,
uint8_t outputReportLength,
uint8_t featureReportLength,
uint8_t inputReportTickerDelay) :
ble(_ble),
connected (false),
reportMapLength(reportMapSize),
inputReport(inputReport),
outputReport(outputReport),
featureReport(featureReport),
inputReportLength(inputReportLength),
outputReportLength(outputReportLength),
featureReportLength(featureReportLength),
protocolMode(REPORT_PROTOCOL),
inputReportReferenceDescriptor(BLE_UUID_DESCRIPTOR_REPORT_REFERENCE,
(uint8_t *)&inputReportReferenceData, 2, 2),
outputReportReferenceDescriptor(BLE_UUID_DESCRIPTOR_REPORT_REFERENCE,
(uint8_t *)&outputReportReferenceData, 2, 2),
featureReportReferenceDescriptor(BLE_UUID_DESCRIPTOR_REPORT_REFERENCE,
(uint8_t *)&featureReportReferenceData, 2, 2),
protocolModeCharacteristic(GattCharacteristic::UUID_PROTOCOL_MODE_CHAR, &protocolMode, 1, 1,
GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ
| GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE_WITHOUT_RESPONSE),
inputReportCharacteristic(GattCharacteristic::UUID_REPORT_CHAR,
(uint8_t *)inputReport, inputReportLength, inputReportLength,
GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ
| GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY
| GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE,
inputReportDescriptors(), 1),
outputReportCharacteristic(GattCharacteristic::UUID_REPORT_CHAR,
(uint8_t *)outputReport, outputReportLength, outputReportLength,
GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ
| GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE_WITHOUT_RESPONSE
| GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE,
outputReportDescriptors(), 1),
featureReportCharacteristic(GattCharacteristic::UUID_REPORT_CHAR,
(uint8_t *)featureReport, featureReportLength, featureReportLength,
GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ
| GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE,
featureReportDescriptors(), 1),
/*
* We need to set reportMap content as const, in order to let the compiler put it into flash
* instead of RAM. The characteristic is read-only so it won't be written, but
* GattCharacteristic constructor takes non-const arguments only. Hence the cast.
*/
reportMapCharacteristic(GattCharacteristic::UUID_REPORT_MAP_CHAR,
const_cast<uint8_t*>(reportMap), reportMapLength, reportMapLength,
GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ),
HIDInformationCharacteristic(GattCharacteristic::UUID_HID_INFORMATION_CHAR, HIDInformation()),
HIDControlPointCharacteristic(GattCharacteristic::UUID_HID_CONTROL_POINT_CHAR,
&controlPointCommand, 1, 1,
GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE_WITHOUT_RESPONSE),
reportTickerDelay(inputReportTickerDelay),
reportTickerIsActive(false)
{
// HID Device Information Service
// Device Information Service
// Note: This HID Device Infomation Service make Micro Bit recognized as
// a Joystick by OpenEmu, But button not detected by OpenEmu
PnPID_t pnpID;
pnpID.vendorID_source = 0x2;
pnpID.vendorID = 0x0D28;
pnpID.productID = 0x0204;
pnpID.productVersion = 0x0100;
HIDDeviceInformationService hidDeviceInformationService(ble, "BBC-Gamepad", "uBit", &pnpID);
// Add Battery Service make Micro Bit show on device list on MacOS
BatteryService batteryInfo(ble, 80);
static GattCharacteristic *characteristics[] = {
&HIDInformationCharacteristic,
&reportMapCharacteristic,
&protocolModeCharacteristic,
&HIDControlPointCharacteristic,
NULL,
NULL,
NULL,
NULL,
NULL
};
unsigned int charIndex = 4;
/*
* Report characteristics are optional, and depend on the reportMap descriptor
* Note: at least one should be present, but we don't check that at the moment.
*/
if (inputReportLength)
characteristics[charIndex++] = &inputReportCharacteristic;
if (outputReportLength)
characteristics[charIndex++] = &outputReportCharacteristic;
if (featureReportLength)
characteristics[charIndex++] = &featureReportCharacteristic;
/* TODO: let children add some more characteristics, namely boot keyboard and mouse (They are
* mandatory as per HIDS spec.) Ex:
*
* addExtraCharacteristics(characteristics, int& charIndex);
*/
GattService service(GattService::UUID_HUMAN_INTERFACE_DEVICE_SERVICE,
characteristics, charIndex);
ble.addService(service);
//ble.gattServer().addService(service);
ble.gap().onConnection(this, &HIDServiceBase::onConnection);
ble.gap().onDisconnection(this, &HIDServiceBase::onDisconnection);
ble.gattServer().onDataSent(this, &HIDServiceBase::onDataSent);
/*
* Change preferred connection params, in order to optimize the notification frequency. Most
* OSes seem to respect this, even though they are not required to.
*
* Some OSes don't handle reconnection well, at the moment, so we set the maximum possible
* timeout, 32 seconds
*/
uint16_t minInterval = Gap::MSEC_TO_GAP_DURATION_UNITS(reportTickerDelay / 2);
if (minInterval < 6)
minInterval = 6;
uint16_t maxInterval = minInterval * 2;
Gap::ConnectionParams_t params = {minInterval, maxInterval, 0, 3200};
ble.gap().setPreferredConnectionParams(¶ms);
SecurityManager::SecurityMode_t securityMode = SecurityManager::SECURITY_MODE_ENCRYPTION_NO_MITM;
protocolModeCharacteristic.requireSecurity(securityMode);
reportMapCharacteristic.requireSecurity(securityMode);
inputReportCharacteristic.requireSecurity(securityMode);
outputReportCharacteristic.requireSecurity(securityMode);
featureReportCharacteristic.requireSecurity(securityMode);
HIDInformationCharacteristic.requireSecurity(securityMode);
startAdvertise();
}
void HIDServiceBase::startAdvertise()
{
ble.gap().stopAdvertising();
ble.gap().clearAdvertisingPayload();
/*
* macOS & openEmu reponse to MicroBit Gamepad buttons with these 3 lines of code
*/
bool enableBonding = true;
bool requireMITM = false;
ble.securityManager().init(enableBonding, requireMITM, SecurityManager::IO_CAPS_NONE);
ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED |
GapAdvertisingData::LE_GENERAL_DISCOVERABLE);
ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS, (uint8_t *)uuid16_list, sizeof(uuid16_list));
ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::JOYSTICK);
ble.gap().setDeviceName((const uint8_t *)"Micro Gamepad");
ble.gap().setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
ble.gap().setAdvertisingInterval(50);
//
// Note: This call make macOS discover Micro Bit
//
ble.gap().setAdvertisingPolicyMode(Gap::ADV_POLICY_IGNORE_WHITELIST);
ble.gap().startAdvertising();
}
void HIDServiceBase::startReportTicker(void) {
if (reportTickerIsActive)
return;
reportTicker.attach_us(this, &HIDServiceBase::sendCallback, reportTickerDelay * 1000);
reportTickerIsActive = true;
}
void HIDServiceBase::stopReportTicker(void) {
reportTicker.detach();
reportTickerIsActive = false;
}
void HIDServiceBase::onDataSent(unsigned count) {
//startReportTicker();
}
GattAttribute** HIDServiceBase::inputReportDescriptors() {
inputReportReferenceData.ID = 0;
inputReportReferenceData.type = INPUT_REPORT;
static GattAttribute * descs[] = {
&inputReportReferenceDescriptor,
};
return descs;
}
GattAttribute** HIDServiceBase::outputReportDescriptors() {
outputReportReferenceData.ID = 0;
outputReportReferenceData.type = OUTPUT_REPORT;
static GattAttribute * descs[] = {
&outputReportReferenceDescriptor,
};
return descs;
}
GattAttribute** HIDServiceBase::featureReportDescriptors() {
featureReportReferenceData.ID = 0;
featureReportReferenceData.type = FEATURE_REPORT;
static GattAttribute * descs[] = {
&featureReportReferenceDescriptor,
};
return descs;
}
HID_information_t* HIDServiceBase::HIDInformation() {
static HID_information_t info = {HID_VERSION_1_11, 0x00, 0x03};
return &info;
}
ble_error_t HIDServiceBase::send(const report_t report) {
return ble.gattServer().write(inputReportCharacteristic.getValueHandle(),
report,
inputReportLength);
}
ble_error_t HIDServiceBase::read(report_t report) {
// TODO. For the time being, we'll just have HID input reports...
return BLE_ERROR_NOT_IMPLEMENTED;
}
void HIDServiceBase::onConnection(const Gap::ConnectionCallbackParams_t *params)
{
this->connected = true;
}
void HIDServiceBase::onDisconnection(const Gap::DisconnectionCallbackParams_t *params)
{
this->connected = false;
}