forked from tc-maxx/esp32-keyble
-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy patheQ3.cpp
462 lines (418 loc) · 18.7 KB
/
eQ3.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
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
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
#include <Arduino.h>
#include <string>
#include <BLEDevice.h>
#include <ctime>
#include <sstream>
#include "eQ3.h"
#include "eQ3_util.h"
using namespace std;
int _LockStatus = 0;
int _RSSI = 0;
// -----------------------------------------------------------------------------
// --[tickTask]-----------------------------------------------------------------
// -----------------------------------------------------------------------------
void tickTask(void *params) {
auto * inst = (eQ3*) params;
while(inst->onTick())
yield();
}
eQ3* cb_instance;
#define SEMAPHORE_WAIT_TIME (10000 / portTICK_PERIOD_MS)
// -----------------------------------------------------------------------------
// --[notify_func]--------------------------------------------------------------
// -----------------------------------------------------------------------------
void notify_func(BLERemoteCharacteristic* pBLERemoteCharacteristic, uint8_t* pData, size_t length, bool isNotify) {
cb_instance->onNotify(pBLERemoteCharacteristic,pData,length,isNotify);
}
// -----------------------------------------------------------------------------
// --[ctor]---------------------------------------------------------------------
// -----------------------------------------------------------------------------
// Important: Initialize BLEDevice::init(""); yourself
eQ3::eQ3(std::string ble_address, std::string user_key, unsigned char user_id) {
state.user_key = hexstring_to_string(user_key);
//Serial.println(state.user_key.length());
state.user_id = user_id;
cb_instance = this;
mutex = xSemaphoreCreateMutex();
this->address = ble_address;
// init BLE scan
bleScan = BLEDevice::getScan();
bleScan->setAdvertisedDeviceCallbacks(this);
bleScan->setActiveScan(true);
bleScan->setInterval(160);
bleScan->setWindow(200);
// TODO move this out to an extra init?
bleClient = BLEDevice::createClient();
bleClient->setClientCallbacks((BLEClientCallbacks *)this);
// pin to core 1 (where the Arduino main loop resides), priority 1
xTaskCreatePinnedToCore(&tickTask, "worker", 10000, this, 1, nullptr, 1);
}
// -----------------------------------------------------------------------------
// --[onConnect]----------------------------------------------------------------
// -----------------------------------------------------------------------------
void eQ3::onConnect(BLEClient *pClient) {
Serial.println("# Connecting...");
state.connectionState = CONNECTING;
}
// -----------------------------------------------------------------------------
// --[onDisconnect]-------------------------------------------------------------
// -----------------------------------------------------------------------------
void eQ3::onDisconnect(BLEClient *pClient) {
Serial.println("# Disconnected!");
state.connectionState = DISCONNECTED;
recvFragments.clear();
sendQueue = std::queue<eQ3Message::MessageFragment>(); // clear queue
queue.clear();
sendChar = recvChar = nullptr;
}
// -----------------------------------------------------------------------------
// --[onTick]-------------------------------------------------------------------
// -----------------------------------------------------------------------------
bool eQ3::onTick() {
// end task when disconnected?
//if (state.connectionState == DISCONNECTED)
// return false;
if (xSemaphoreTake(mutex, 0)) {
if (state.connectionState == FOUND) {
Serial.println("# Connecting in tick");
bleClient->connect(BLEAddress(address), BLE_ADDR_TYPE_PUBLIC);
} else if (state.connectionState == CONNECTING) {
Serial.println("# Connected in tick");
BLERemoteService *comm;
comm = bleClient->getService(BLEUUID(BLE_UUID_SERVICE));
sendChar = comm->getCharacteristic(BLEUUID(BLE_UUID_WRITE)); // write buffer characteristic
recvChar = comm->getCharacteristic(BLEUUID(BLE_UUID_READ)); // read buffer characteristic
//recvChar->setNotifyCallbacks((BLERemoteCharacteristicCallbacks*)this);
recvChar->registerForNotify(¬ify_func);
lastActivity = time(NULL);
state.connectionState = CONNECTED;
auto queueFunc = queue.find(CONNECTED);
if (queueFunc != queue.end()) {
queue.erase(CONNECTED);
//xSemaphoreGive(mutex); // function will take the semaphore again
queueFunc->second();
}
} else if (state.connectionState != DISCONNECTED) {
sendNextFragment();
lastActivity = time(NULL);
}
// } else {
// sendNextFragment();
// lastActivity = time(NULL);
// }
// TODO disconnect if no answer for long time?
/* if (state.connectionState >= CONNECTED && difftime(lastActivity, time(NULL)) > LOCK_TIMEOUT && sendQueue.empty()) {
Serial.println("# Lock timeout");
bleClient->disconnect();
}*/
xSemaphoreGive(mutex);
}
return true;
}
// -----------------------------------------------------------------------------
// --[onResult]-----------------------------------------------------------------
// -----------------------------------------------------------------------------
void eQ3::onResult(BLEAdvertisedDevice advertisedDevice) {
if (advertisedDevice.getAddress().toString() == address) { // TODO: Make name and address variable
Serial.print("# Found device: ");
Serial.println(advertisedDevice.getAddress().toString().c_str());
Serial.print("# RSSI: ");
Serial.println(advertisedDevice.getRSSI());
_RSSI = advertisedDevice.getRSSI();
bleScan->stop();
state.connectionState = FOUND;
}
}
// -----------------------------------------------------------------------------
// --[setOnStatusChange]--------------------------------------------------------
// -----------------------------------------------------------------------------
void eQ3::setOnStatusChange(std::function<void(LockStatus)> cb) {
xSemaphoreTake(mutex, SEMAPHORE_WAIT_TIME);
onStatusChange = cb;
xSemaphoreGive(mutex);
}
// -----------------------------------------------------------------------------
// --[exchangeNonces]-----------------------------------------------------------
// -----------------------------------------------------------------------------
void eQ3::exchangeNonces() {
state.local_session_nonce.clear();
for (int i = 0; i < 8; i++)
state.local_session_nonce.append(1,esp_random());
auto *msg = new eQ3Message::Connection_Request_Message;
Serial.println("# Nonce exchange");
sendMessage(msg);
}
// -----------------------------------------------------------------------------
// --[connect]------------------------------------------------------------------
// -----------------------------------------------------------------------------
void eQ3::connect() {
state.connectionState = SCANNING;
bleScan->start(25, nullptr, false);
Serial.println("# Searching ...");
//state.connectionState = FOUND;
//Serial.println("connecting directly...");
}
// -----------------------------------------------------------------------------
// --[sendMessage]--------------------------------------------------------------
// -----------------------------------------------------------------------------
bool eQ3::sendMessage(eQ3Message::Message *msg) {
std::string data;
if (msg->isSecure()) {
if (state.connectionState < NONCES_EXCHANGED) {
// TODO check if slot for nonces_exchanged is already set?
queue.insert(make_pair(NONCES_EXCHANGED,[this,msg]{
Serial.println("# sendMessage called again...");
sendMessage(msg);
}));
exchangeNonces();
return true;
}
string padded_data;
padded_data.append(msg->encode(&state));
int pad_to = generic_ceil(padded_data.length(), 15, 8);
if (pad_to > padded_data.length())
padded_data.append(pad_to - padded_data.length(), 0);
crypt_data(padded_data, msg->id, state.remote_session_nonce, state.local_security_counter, state.user_key);
data.append(1, msg->id);
data.append(crypt_data(padded_data, msg->id, state.remote_session_nonce, state.local_security_counter, state.user_key));
data.append(1, (char) (state.local_security_counter >> 8));
data.append(1, (char) state.local_security_counter);
data.append(compute_auth_value(padded_data, msg->id, state.remote_session_nonce, state.local_security_counter, state.user_key));
state.local_security_counter++;
} else {
if (state.connectionState < CONNECTED) {
// TODO check if slot for connected is already set?
queue.insert(make_pair(CONNECTED,[this,msg]{
sendMessage(msg);
}));
connect();
return true;
}
data.append(1, msg->id);
data.append(msg->encode(&state));
}
// fragment
int chunks = data.length() / 15;
if (data.length() % 15 > 0)
chunks += 1;
for (int i = 0; i < chunks; i++) {
eQ3Message::MessageFragment frag;
frag.data.append(1, (i ? 0 : 0x80) + (chunks - 1 - i)); // fragment status byte
frag.data.append(data.substr(i * 15, 15));
if (frag.data.length() < 16)
frag.data.append(16 - (frag.data.length() % 16), 0); // padding
sendQueue.push(frag);
}
Serial.println("# sendMessage end.");;
free(msg);
return true;
}
// -----------------------------------------------------------------------------
// --[onNotify]-----------------------------------------------------------------
// -----------------------------------------------------------------------------
void eQ3::onNotify(BLERemoteCharacteristic* pBLERemoteCharacteristic, uint8_t* pData, size_t length, bool isNotify) {
xSemaphoreTake(mutex, SEMAPHORE_WAIT_TIME);
eQ3Message::MessageFragment frag;
lastActivity = time(NULL);
frag.data = std::string((char *) pData, length);
recvFragments.push_back(frag);
Serial.print("# Fragment Data: ");
Serial.println(string_to_hex(frag.data).c_str());
if (frag.isLast()) {
if (!sendQueue.empty())
sendQueue.pop();
// concat message
std::stringstream ss;
auto msgtype = recvFragments.front().getType();
for (auto &received_fragment : recvFragments) {
ss << received_fragment.getData();
}
std::string msgdata = ss.str();
recvFragments.clear();
if (eQ3Message::Message::isTypeSecure(msgtype)) {
auto msg_security_counter = static_cast<uint16_t>(msgdata[msgdata.length() - 6]);
msg_security_counter <<= 8;
msg_security_counter += msgdata[msgdata.length() - 5];
//Serial.println((int)msg_security_counter);
if (msg_security_counter <= state.remote_security_counter) {
Serial.print("# Msg. security counter: ");
Serial.println(msg_security_counter);
Serial.print("# Security counter: ");
Serial.println(state.remote_security_counter);
Serial.println("# Falscher remote counter");
xSemaphoreGive(mutex);
return;
}
state.remote_security_counter = msg_security_counter;
string msg_auth_value = msgdata.substr(msgdata.length() - 4, 4);
Serial.print("# Auth value: ");
Serial.println(string_to_hex(msg_auth_value).c_str());
//std::string decrypted = crypt_data(msgdata.substr(0, msgdata.length() - 6), msgtype,
std::string decrypted = crypt_data(msgdata.substr(1, msgdata.length() - 7), msgtype, state.local_session_nonce, state.remote_security_counter, state.user_key);
Serial.print("# Crypted data: ");
Serial.println(string_to_hex(msgdata.substr(1, msgdata.length() - 7)).c_str());
std::string computed_auth_value = compute_auth_value(decrypted, msgtype, state.local_session_nonce, state.remote_security_counter, state.user_key);
if (msg_auth_value != computed_auth_value) {
Serial.println("# Auth value mismatch");
xSemaphoreGive(mutex);
return;
}
msgdata = decrypted;
Serial.print("# Decrypted: ");
Serial.println(string_to_hex(msgdata).c_str());
}
switch (msgtype) {
case 0: {
// fragment ack, remove first
if (!sendQueue.empty())
sendQueue.pop();
xSemaphoreGive(mutex);
return;
}
case 0x81: // answer with security
// TODO call callback to user that pairing succeeded
Serial.println("# Answer with security...");
break;
case 0x01: // answer without security
// TODO report error
Serial.println("# Answer without security...");
break;
case 0x05: {
// status changed notification
Serial.println("# Status changed notification");
// TODO request status
auto * message = new eQ3Message::StatusRequestMessage;
sendMessage(message);
break;
}
case 0x03: {
// connection info message
eQ3Message::Connection_Info_Message message;
message.data = msgdata;
state.user_id = message.getUserId();
state.remote_session_nonce = message.getRemoteSessionNonce();
assert(state.remote_session_nonce.length() == 8);
state.local_security_counter = 1;
state.remote_security_counter = 0;
state.connectionState = NONCES_EXCHANGED;
Serial.println("# Nonce exchanged");
auto queueFunc = queue.find(NONCES_EXCHANGED);
if (queueFunc != queue.end()) {
queue.erase(queueFunc);
//xSemaphoreGive(mutex); // function will take the semaphore again
queueFunc->second();
}
xSemaphoreGive(mutex);
return;
}
case 0x83: {
// status info
eQ3Message::Status_Info_Message message;
message.data = msgdata;
Serial.print("# New state: ");
Serial.println(message.getLockStatus());
_LockStatus = message.getLockStatus();
raw_data = message.data;
//onStatusChange((LockStatus)message.getLockStatus()); // BUG: löst einen Reset aus!!
break;
}
default:
/*case 0x8f: */{ // user info
Serial.println("# User info");
xSemaphoreGive(mutex);
return;
}
}
} else {
// create ack
Serial.println("# send Ack ");
eQ3Message::FragmentAckMessage ack(frag.getStatusByte());
sendQueue.push(ack);
}
xSemaphoreGive(mutex);
}
// -----------------------------------------------------------------------------
// --[pairingRequest]-----------------------------------------------------------
// -----------------------------------------------------------------------------
void eQ3::pairingRequest(std::string cardkey) {
xSemaphoreTake(mutex, SEMAPHORE_WAIT_TIME);
if (state.connectionState < NONCES_EXCHANGED) {
// TODO check if slot for nonces_exchanged is already set?
// TODO callback when pairing finished, or make blocking?
queue.insert(make_pair(NONCES_EXCHANGED,[this,cardkey]{
pairingRequest(cardkey);
}));
Serial.println("# Pairing request");
exchangeNonces();
xSemaphoreGive(mutex);
return;
}
auto *message = new eQ3Message::PairingRequestMessage();
//return concatenated_array([data.user_id], padded_array(data.encrypted_pair_key, 22, 0), integer_to_byte_array(data.security_counter, 2), data.authentication_value);
message->data.append(1, state.user_id);
Serial.print("#Message id: ");
Serial.println((int) message->id);
// enc pair key
assert(state.remote_session_nonce.length() == 8);
assert(state.user_key.length() == 16);
std::string card_key = hexstring_to_string(cardkey);
string encrypted_pair_key = crypt_data(state.user_key, 0x04, state.remote_session_nonce, state.local_security_counter, card_key);
if (encrypted_pair_key.length() < 22)
encrypted_pair_key.append(22 - encrypted_pair_key.length(), 0);
assert(encrypted_pair_key.length() == 22);
message->data.append(encrypted_pair_key);
// counter
message->data.append(1, (char) (state.local_security_counter >> 8));
message->data.append(1, (char) (state.local_security_counter));
// auth value
string extra;
extra.append(1, state.user_id);
extra.append(state.user_key);
if (extra.length() < 23)
extra.append(23 - extra.length(), 0);
assert(extra.length() == 23);
string auth_value = compute_auth_value(extra, 0x04, state.remote_session_nonce, state.local_security_counter, card_key);
message->data.append(auth_value);
assert(message->data.length() == 29);
sendMessage(message);
xSemaphoreGive(mutex);
}
// -----------------------------------------------------------------------------
// --[sendNextFragment]---------------------------------------------------------
// -----------------------------------------------------------------------------
void eQ3::sendNextFragment() {
if (sendQueue.empty())
return;
if (sendQueue.front().sent && std::difftime(sendQueue.front().timeSent, std::time(NULL)) < 5)
return;
sendQueue.front().sent = true;
Serial.print("# Sending actual fragment: ");
string data = sendQueue.front().data;
sendQueue.front().timeSent = std::time(NULL);
Serial.println(string_to_hex(data).c_str());
assert(data.length() == 16);
sendChar->writeValue((uint8_t *) (data.c_str()), 16, true);
}
void eQ3::sendCommand(CommandType command) {
xSemaphoreTake(mutex, SEMAPHORE_WAIT_TIME);
Serial.println("# Getting Semaphore for sendcommand");
auto msg = new eQ3Message::CommandMessage(command);
sendMessage(msg);
Serial.println("# Semaphore handover");
xSemaphoreGive(mutex);
}
void eQ3::unlock() {
sendCommand(UNLOCK);
}
void eQ3::lock() {
sendCommand(LOCK);
}
void eQ3::open() {
sendCommand(OPEN);
}
void eQ3::updateInfo() {
xSemaphoreTake(mutex, SEMAPHORE_WAIT_TIME);
auto * message = new eQ3Message::StatusRequestMessage;
sendMessage(message);
xSemaphoreGive(mutex);
}