/
BluetoothGamepadService.cpp
278 lines (238 loc) · 9.87 KB
/
BluetoothGamepadService.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
/**
* A class to communicate a BLE Gamepad device
*/
#include "MicroBit.h"
#include "pxt.h"
#include "ble/GapAdvertisingData.h"
#include "ble/GattService.h"
#include "ble/GattCharacteristic.h"
#if !CONFIG_ENABLED(MICROBIT_BLE_DEVICE_INFORMATION_SERVICE)
#include "HIDDeviceInformationService.h"
#endif
#include "BluetoothGamepadService.h"
#include "USBHID_Types.h"
namespace
{
static const uint8_t RESPONSE_HID_INFORMATION[] = {0x11, 0x01, 0x00, 0x03};
static const uint16_t uuid16_list[] = {GattService::UUID_HUMAN_INTERFACE_DEVICE_SERVICE,
GattService::UUID_DEVICE_INFORMATION_SERVICE};
/**
* Characteristic Data(Report Map)
*/
static const uint8_t REPORT_MAP[] = {
USAGE_PAGE(1), 0x01, // Generic Desktop
USAGE(1), 0x05, // Game Pad
COLLECTION(1), 0x01, // Collection: Application
COLLECTION(1), 0x00, // Collection: Physical
REPORT_ID(1), 0x01, // Report ID 1
// axes
USAGE_PAGE(1), 0x01, // Generic Desktop
USAGE(1), 0x30, // X
USAGE(1), 0x31, // Y
LOGICAL_MINIMUM(1), 0xff, // Logical Min: -1
LOGICAL_MAXIMUM(1), 0x01, // Logical Max: 1
REPORT_SIZE(1), 0x02, // 1 Bit per axis
REPORT_COUNT(1), 0x02, // 2 axes
INPUT(1), 0x02, // Data, Variable, Absolute
// buttons
USAGE_PAGE(1), 0x09, // Button
USAGE(1), 0x01, // Button A
USAGE(1), 0x02, // Button B
USAGE(1), 0x0b, // Select
USAGE(1), 0x0c, // Start
LOGICAL_MINIMUM(1), 0x00, // Logical Min: 0
LOGICAL_MAXIMUM(1), 0x01, // Logical Max: 1
REPORT_SIZE(1), 0x01, // 1 Bit per button
REPORT_COUNT(1), 0x04, // 4 Buttons
INPUT(1), 0x02, // Data, Variable, Absolute
END_COLLECTION(0),
END_COLLECTION(0),
};
static const uint8_t INPUT_DESCRIPTOR_REPORT[] = {0x01, 0x01};
static const uint8_t REPORT_MAP_EXTERNAL_REPORT[] = {0x2A, 0x19};
}
static bool isInitializedService = false;
/**
* Constructor
* @param dev BLE device
*/
BluetoothGamepadService::BluetoothGamepadService(BLEDevice *dev) : ble(*dev)
{
if (isInitializedService == false)
{
startService();
isInitializedService = true;
}
startAdvertise();
}
void BluetoothGamepadService::startService()
{
memset(inputReportData, 0, sizeof(inputReportData));
connected = false;
protocolMode = REPORT_PROTOCOL;
reportTickerIsActive = false;
ble.init();
ble.securityManager().init(true, false, SecurityManager::IO_CAPS_NONE);
#if !CONFIG_ENABLED(MICROBIT_BLE_DEVICE_INFORMATION_SERVICE)
// Device Information Service
PnPID_t pnpID;
pnpID.vendorID_source = 0x2;
pnpID.vendorID = 0x0D28;
pnpID.productID = 0x0204;
pnpID.productVersion = 0x0100;
HIDDeviceInformationService hidDeviceInformationService(ble, "BBC", "uBit", &pnpID);
#endif
// Gamepad Service
GattCharacteristic protocolModeCharacteristic(GattCharacteristic::UUID_PROTOCOL_MODE_CHAR,
&protocolMode, 1, 1,
GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE_WITHOUT_RESPONSE);
GattAttribute inputReportDescriptor(BLE_UUID_DESCRIPTOR_REPORT_REFERENCE, const_cast<uint8_t *>(INPUT_DESCRIPTOR_REPORT), 2, 2, false);
GattAttribute *inputReportDescriptors[] = { &inputReportDescriptor };
GattCharacteristic inputReportCharacteristic(GattCharacteristic::UUID_REPORT_CHAR,
inputReportData, sizeof(inputReportData), sizeof(inputReportData),
GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ |
GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY |
GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE,
inputReportDescriptors, 1);
GattAttribute reportMapExternalReportDescriptor(BLE_UUID_DESCRIPTOR_EXTERNAL_REPORT_REFERENCE, const_cast<uint8_t *>(REPORT_MAP_EXTERNAL_REPORT), 2, 2, false);
GattAttribute *reportMapDescriptors[] = { &reportMapExternalReportDescriptor };
GattCharacteristic reportMapCharacteristic(GattCharacteristic::UUID_REPORT_MAP_CHAR,
const_cast<uint8_t *>(REPORT_MAP), sizeof(REPORT_MAP), sizeof(REPORT_MAP),
GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ,
reportMapDescriptors, 1);
GattCharacteristic hidInformationCharacteristic(GattCharacteristic::UUID_HID_INFORMATION_CHAR,
const_cast<uint8_t *>(RESPONSE_HID_INFORMATION), sizeof(RESPONSE_HID_INFORMATION), sizeof(RESPONSE_HID_INFORMATION),
GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ);
GattCharacteristic hidControlPointCharacteristic(GattCharacteristic::UUID_HID_CONTROL_POINT_CHAR,
&controlPointCommand, 1, 1,
GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE_WITHOUT_RESPONSE);
GattCharacteristic *gamepadCharacteristics[]{
&reportMapCharacteristic,
&protocolModeCharacteristic,
&hidControlPointCharacteristic,
&hidInformationCharacteristic,
&inputReportCharacteristic,
};
GattService gamepadService(GattService::UUID_HUMAN_INTERFACE_DEVICE_SERVICE, gamepadCharacteristics, sizeof(gamepadCharacteristics) / sizeof(GattCharacteristic *));
ble.gattServer().addService(gamepadService);
protocolModeCharacteristic.requireSecurity(SecurityManager::SECURITY_MODE_ENCRYPTION_NO_MITM);
inputReportCharacteristic.requireSecurity(SecurityManager::SECURITY_MODE_ENCRYPTION_NO_MITM);
reportMapCharacteristic.requireSecurity(SecurityManager::SECURITY_MODE_ENCRYPTION_NO_MITM);
hidInformationCharacteristic.requireSecurity(SecurityManager::SECURITY_MODE_ENCRYPTION_NO_MITM);
hidControlPointCharacteristic.requireSecurity(SecurityManager::SECURITY_MODE_ENCRYPTION_NO_MITM);
inputReportValueHandle = inputReportCharacteristic.getValueHandle();
ble.gap().onConnection(this, &BluetoothGamepadService::onConnection);
ble.gap().onDisconnection(this, &BluetoothGamepadService::onDisconnection);
startReportTicker();
}
void BluetoothGamepadService::startAdvertise()
{
ble.gap().stopAdvertising();
ble.gap().clearAdvertisingPayload();
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::GAMEPAD);
uint16_t minInterval = Gap::MSEC_TO_GAP_DURATION_UNITS(25);
if (minInterval < 6)
{
minInterval = 6;
}
uint16_t maxInterval = minInterval * 2;
Gap::ConnectionParams_t params = {minInterval, maxInterval, 0, 3200}; // timeout in 32 seconds
ble.gap().setPreferredConnectionParams(¶ms);
ble.gap().setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
ble.gap().setAdvertisingInterval(50);
ble.gap().setAdvertisingPolicyMode(Gap::ADV_POLICY_IGNORE_WHITELIST);
ble.gap().startAdvertising();
}
void BluetoothGamepadService::startReportTicker()
{
if (reportTickerIsActive)
{
return;
}
reportTicker.attach_us(this, &BluetoothGamepadService::sendCallback, 24000);
reportTickerIsActive = true;
}
void BluetoothGamepadService::stopReportTicker()
{
if (!reportTickerIsActive)
{
return;
}
reportTicker.detach();
reportTickerIsActive = false;
}
void BluetoothGamepadService::onConnection(const Gap::ConnectionCallbackParams_t *params)
{
ble.gap().stopAdvertising();
buttonsState = 0;
connected = true;
}
void BluetoothGamepadService::onDisconnection(const Gap::DisconnectionCallbackParams_t *params)
{
connected = false;
startAdvertise();
}
/**
* Toggle the state of one button
*/
void BluetoothGamepadService::setButton(GamepadButton button, ButtonState state)
{
if (state == BUTTON_UP)
{
buttonsState &= ~(button);
}
else
{
buttonsState |= button;
}
}
void BluetoothGamepadService::sendCallback()
{
if (!connected)
{
return;
}
// buttons
inputReportData[0] = buttonsState & 0xf0;
// axis
axisX = 0;
axisY = 0;
if (buttonsState & GAMEPAD_BUTTON_LEFT)
{
axisX--;
}
if (buttonsState & GAMEPAD_BUTTON_RIGHT)
{
axisX++;
}
if (buttonsState & GAMEPAD_BUTTON_UP)
{
axisY--;
}
if (buttonsState & GAMEPAD_BUTTON_DOWN)
{
axisY++;
}
switch (axisX)
{
case -1: // left
inputReportData[0] |= 0x03;
break;
case 1: // right
inputReportData[0] |= 0x01;
break;
}
switch (axisY)
{
case -1: // up
inputReportData[0] |= 0x0c;
break;
case 1: // down
inputReportData[0] |= 0x04;
break;
}
ble.gattServer().write(inputReportValueHandle, inputReportData, 1);
}