Repository navigation
Expand file tree
/
Copy pathBleCommandChannel.cpp
More file actions
414 lines (334 loc) · 12.6 KB
/
Copy pathBleCommandChannel.cpp
File metadata and controls
414 lines (334 loc) · 12.6 KB
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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
/**
* This file is part of AgentLight.
*
* @link https://github.com/whalesky-labs/AgentLight
* @document https://github.com/whalesky-labs/AgentLight/blob/main/README.md
* @contact root@imoi.cn
* @license https://github.com/whalesky-labs/AgentLight/blob/main/LICENSE
*/
#include "agentlight/infrastructure/BleCommandChannel.h"
#include <BLE2902.h>
#include <BLEAdvertising.h>
#include <BLEDevice.h>
#include <BLEHIDDevice.h>
#include <BLEServer.h>
#include <BLEUtils.h>
namespace agentlight {
namespace {
constexpr uint8_t BLE_KEY_SIZE = 16;
constexpr uint8_t HID_COMMAND_FEATURE_REPORT_ID = 1;
constexpr uint8_t HID_COMMAND_FEATURE_REPORT_SIZE = 32;
constexpr uint8_t HID_COMMAND_MAX_TEXT_SIZE = HID_COMMAND_FEATURE_REPORT_SIZE - 1;
uint8_t hidGenericReportMap[] = {
0x06, 0x00, 0xFF, // Usage Page (Vendor Defined 0xFF00)
0x09, 0x01, // Usage (Vendor Usage 1)
0xA1, 0x01, // Collection (Application)
0x85, HID_COMMAND_FEATURE_REPORT_ID,
0x09, 0x01, // Usage (Vendor Usage 1)
0x15, 0x00, // Logical Minimum (0)
0x26, 0xFF, 0x00, // Logical Maximum (255)
0x75, 0x08, // Report Size (8)
0x95, HID_COMMAND_FEATURE_REPORT_SIZE,
0xB1, 0x02, // Feature (Data, Variable, Absolute)
0xC0, // End Collection
};
void writeHidTextReport(BLECharacteristic* characteristic, const String& text) {
if (characteristic == nullptr) {
return;
}
uint8_t report[HID_COMMAND_FEATURE_REPORT_SIZE] = {0};
const uint8_t textLength = static_cast<uint8_t>(
min(text.length(), static_cast<unsigned int>(HID_COMMAND_MAX_TEXT_SIZE)));
report[0] = textLength;
for (uint8_t index = 0; index < textLength; ++index) {
report[index + 1] = static_cast<uint8_t>(text.charAt(index));
}
characteristic->setValue(report, sizeof(report));
}
String decodeHidTextReport(const uint8_t* data, size_t length) {
if (data == nullptr || length == 0) {
return "";
}
size_t cursor = 0;
if (length > 1 && data[0] == HID_COMMAND_FEATURE_REPORT_ID) {
cursor = 1;
}
if (cursor >= length) {
return "";
}
const uint8_t textLength = data[cursor];
const size_t available = length - cursor - 1;
if (textLength == 0 || textLength > available || textLength > HID_COMMAND_MAX_TEXT_SIZE) {
return "";
}
char buffer[HID_COMMAND_MAX_TEXT_SIZE + 1] = {0};
for (uint8_t index = 0; index < textLength; ++index) {
buffer[index] = static_cast<char>(data[cursor + 1 + index]);
}
return String(buffer);
}
class RxCallbacks : public BLECharacteristicCallbacks {
public:
RxCallbacks(BleCommandChannel& channel, String (*handler)(const String& command))
: channel_(channel), handler_(handler) {}
void onWrite(BLECharacteristic* characteristic) override {
const auto value = characteristic->getValue();
if (value.length() == 0) {
return;
}
if (!channel_.acceptsCommands()) {
channel_.publish("SKIP BLE_SUSPENDED");
return;
}
const String response = handler_(String(value.c_str()));
channel_.publish(response);
}
private:
BleCommandChannel& channel_;
String (*handler_)(const String& command);
};
class HidCommandCallbacks : public BLECharacteristicCallbacks {
public:
HidCommandCallbacks(BleCommandChannel& channel, String (*handler)(const String& command))
: channel_(channel), handler_(handler) {}
void onWrite(BLECharacteristic* characteristic, esp_ble_gatts_cb_param_t* param) override {
const String command = decodeHidTextReport(param->write.value, param->write.len);
if (command.length() == 0) {
writeHidTextReport(characteristic, "ERROR HID_EMPTY_COMMAND");
return;
}
if (!channel_.acceptsCommands()) {
writeHidTextReport(characteristic, "SKIP BLE_SUSPENDED");
return;
}
const String response = handler_(command);
writeHidTextReport(characteristic, response);
}
private:
BleCommandChannel& channel_;
String (*handler_)(const String& command);
};
class PinPairingCallbacks : public BLESecurityCallbacks {
public:
explicit PinPairingCallbacks(uint32_t pairingPin) : pairingPin_(pairingPin) {}
uint32_t onPassKeyRequest() override {
Serial.print("AgentLight BLE passkey requested: ");
printPasskey(pairingPin_);
return pairingPin_;
}
void onPassKeyNotify(uint32_t passKey) override {
Serial.print("AgentLight BLE passkey: ");
printPasskey(passKey);
}
bool onSecurityRequest() override {
Serial.println("AgentLight BLE security request accepted");
return true;
}
void onAuthenticationComplete(esp_ble_auth_cmpl_t authentication) override {
Serial.print("AgentLight BLE pairing ");
Serial.println(authentication.success ? "succeeded" : "failed");
}
bool onConfirmPIN(uint32_t pin) override {
Serial.print("AgentLight BLE numeric confirmation: ");
printPasskey(pin);
return true;
}
private:
void printPasskey(uint32_t passKey) {
char buffer[7];
snprintf(buffer, sizeof(buffer), "%06lu", static_cast<unsigned long>(passKey));
Serial.println(buffer);
}
uint32_t pairingPin_;
};
} // namespace
class BleCommandChannel::ServerCallbacks : public BLEServerCallbacks {
public:
ServerCallbacks(BleCommandChannel& channel, const BleSecurityConfig& securityConfig)
: channel_(channel), securityConfig_(securityConfig) {}
void onConnect(BLEServer* server) override {
(void)server;
}
void onConnect(BLEServer* server, esp_ble_gatts_cb_param_t* param) override {
(void)server;
channel_.handleConnected(param->connect.conn_id);
if (!securityConfig_.requirePairing) {
return;
}
const esp_err_t result = esp_ble_set_encryption(
param->connect.remote_bda,
ESP_BLE_SEC_ENCRYPT_MITM);
Serial.print("AgentLight BLE encryption requested: ");
Serial.println(result == ESP_OK ? "OK" : "FAILED");
}
void onDisconnect(BLEServer* server) override {
(void)server;
channel_.handleDisconnected();
}
private:
BleCommandChannel& channel_;
BleSecurityConfig securityConfig_;
};
void BleCommandChannel::begin(
const BleCommandChannelConfig& config,
String (*handler)(const String& command),
void (*connectionHandler)(BleConnectionState state)) {
lifecycle_.begin(config.advertisement.lifecycle);
connectionHandler_ = connectionHandler;
BLEDevice::init(config.deviceName);
applySecurity(config.security);
server_ = BLEDevice::createServer();
server_->setCallbacks(new ServerCallbacks(*this, config.security));
configureHidProfile(server_, config.hidProfile, handler);
BLEService* service = server_->createService(config.serviceUuid);
txCharacteristic_ = service->createCharacteristic(
config.txUuid,
BLECharacteristic::PROPERTY_READ | BLECharacteristic::PROPERTY_NOTIFY);
BLE2902* clientConfigDescriptor = new BLE2902();
txCharacteristic_->addDescriptor(clientConfigDescriptor);
txCharacteristic_->setValue("STATUS OFF");
BLECharacteristic* rxCharacteristic = service->createCharacteristic(
config.rxUuid,
BLECharacteristic::PROPERTY_WRITE | BLECharacteristic::PROPERTY_WRITE_NR);
rxCharacteristic->setCallbacks(new RxCallbacks(*this, handler));
applyAccessPermissions(rxCharacteristic, txCharacteristic_, hidFeatureReport_, clientConfigDescriptor, config.security);
service->start();
BLEAdvertising* advertising = BLEDevice::getAdvertising();
configureAdvertising(advertising, config);
}
void BleCommandChannel::poll(uint32_t nowMs) {
applyAdvertisingAction(lifecycle_.poll(nowMs));
}
void BleCommandChannel::openManualReconnectWindow(uint32_t nowMs) {
commandsEnabled_ = true;
applyAdvertisingAction(lifecycle_.openManualReconnectWindow(nowMs));
}
void BleCommandChannel::closeManualReconnectWindow() {
applyAdvertisingAction(lifecycle_.closeManualReconnectWindow());
}
void BleCommandChannel::suspend() {
commandsEnabled_ = false;
closeManualReconnectWindow();
BLEDevice::stopAdvertising();
disconnectClient();
}
void BleCommandChannel::applySecurity(const BleSecurityConfig& securityConfig) {
if (!securityConfig.requirePairing) {
return;
}
securityCallbacks_ = new PinPairingCallbacks(securityConfig.pairingPin);
BLEDevice::setSecurityCallbacks(securityCallbacks_);
BLEDevice::setEncryptionLevel(ESP_BLE_SEC_ENCRYPT_MITM);
security_ = new BLESecurity();
security_->setStaticPIN(securityConfig.pairingPin);
security_->setCapability(ESP_IO_CAP_OUT);
security_->setKeySize(BLE_KEY_SIZE);
security_->setAuthenticationMode(ESP_LE_AUTH_REQ_SC_MITM_BOND);
security_->setInitEncryptionKey(ESP_BLE_ENC_KEY_MASK | ESP_BLE_ID_KEY_MASK);
security_->setRespEncryptionKey(ESP_BLE_ENC_KEY_MASK | ESP_BLE_ID_KEY_MASK);
}
void BleCommandChannel::applyAccessPermissions(
BLECharacteristic* rxCharacteristic,
BLECharacteristic* txCharacteristic,
BLECharacteristic* hidFeatureReport,
BLE2902* clientConfigDescriptor,
const BleSecurityConfig& securityConfig) {
if (!securityConfig.requirePairing) {
return;
}
txCharacteristic->setAccessPermissions(ESP_GATT_PERM_READ_ENC_MITM);
rxCharacteristic->setAccessPermissions(ESP_GATT_PERM_WRITE_ENC_MITM);
if (hidFeatureReport != nullptr) {
hidFeatureReport->setAccessPermissions(
ESP_GATT_PERM_READ_ENC_MITM | ESP_GATT_PERM_WRITE_ENC_MITM);
}
clientConfigDescriptor->setAccessPermissions(
ESP_GATT_PERM_READ_ENC_MITM | ESP_GATT_PERM_WRITE_ENC_MITM);
}
void BleCommandChannel::configureHidProfile(
BLEServer* server,
const BleHidProfileConfig& config,
String (*handler)(const String& command)) {
if (!config.enabled) {
return;
}
systemHidDevice_ = new BLEHIDDevice(server);
systemHidDevice_->manufacturer();
systemHidDevice_->manufacturer(config.manufacturerName);
BLECharacteristic* modelNumber = systemHidDevice_->deviceInfo()->createCharacteristic(
BLEUUID(static_cast<uint16_t>(0x2A24)),
BLECharacteristic::PROPERTY_READ);
modelNumber->setValue(config.modelNumber);
systemHidDevice_->pnp(0x02, config.vendorId, config.productId, config.productVersion);
systemHidDevice_->hidInfo(0x00, 0x00);
systemHidDevice_->reportMap(hidGenericReportMap, sizeof(hidGenericReportMap));
hidFeatureReport_ = systemHidDevice_->featureReport(HID_COMMAND_FEATURE_REPORT_ID);
hidFeatureReport_->setCallbacks(new HidCommandCallbacks(*this, handler));
writeHidTextReport(hidFeatureReport_, "STATUS OFF");
systemHidDevice_->startServices();
const uint8_t batteryLevel = config.batteryLevel > 100 ? 100 : config.batteryLevel;
systemHidDevice_->setBatteryLevel(batteryLevel);
}
void BleCommandChannel::configureAdvertising(BLEAdvertising* advertising, const BleCommandChannelConfig& config) {
BLEAdvertisementData advertisement;
advertisement.setFlags(ESP_BLE_ADV_FLAG_GEN_DISC | ESP_BLE_ADV_FLAG_BREDR_NOT_SPT);
advertisement.setName(config.advertisement.advertisedName);
advertisement.setCompleteServices(BLEUUID(config.advertisement.advertisedServiceUuid));
advertisement.setAppearance(config.advertisement.appearance);
BLEAdvertisementData scanResponse;
scanResponse.setName(config.deviceName);
advertising->setAdvertisementType(ADV_TYPE_IND);
advertising->setScanResponse(true);
advertising->setAdvertisementData(advertisement);
advertising->setScanResponseData(scanResponse);
}
void BleCommandChannel::handleConnected(uint16_t connectionId) {
connected_ = true;
connectionId_ = connectionId;
applyAdvertisingAction(lifecycle_.handleConnected());
notifyConnectionState(BleConnectionState::Connected);
}
void BleCommandChannel::handleDisconnected() {
connected_ = false;
connectionId_ = 0;
notifyConnectionState(BleConnectionState::Disconnected);
applyAdvertisingAction(lifecycle_.handleDisconnected(millis()));
}
void BleCommandChannel::applyAdvertisingAction(BleAdvertisingAction action) {
switch (action) {
case BleAdvertisingAction::StartAdvertising:
BLEDevice::startAdvertising();
return;
case BleAdvertisingAction::StopAdvertising:
BLEDevice::stopAdvertising();
return;
case BleAdvertisingAction::None:
default:
return;
}
}
void BleCommandChannel::notifyConnectionState(BleConnectionState state) {
if (connectionHandler_ != nullptr) {
connectionHandler_(state);
}
}
void BleCommandChannel::disconnectClient() {
if (!connected_ || server_ == nullptr) {
return;
}
server_->disconnect(connectionId_);
}
void BleCommandChannel::publish(const String& message) {
if (txCharacteristic_ == nullptr) {
return;
}
txCharacteristic_->setValue(message.c_str());
txCharacteristic_->notify();
}
bool BleCommandChannel::connected() const {
return connected_;
}
bool BleCommandChannel::acceptsCommands() const {
return commandsEnabled_;
}
} // namespace agentlight