-
Notifications
You must be signed in to change notification settings - Fork 2
/
identity-service-file-json.cpp
610 lines (566 loc) · 17.5 KB
/
identity-service-file-json.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
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
#ifdef _MSC_VER
#define _CRT_SECURE_NO_WARNINGS
#pragma warning(disable : 4996)
#endif
#include <fstream>
#include <regex>
#include <sstream>
#ifdef __clang__
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wexpansion-to-defined"
#endif
#include "rapidjson/reader.h"
#include "rapidjson/filereadstream.h"
#include "rapidjson/error/en.h"
#ifdef __clang__
#pragma clang diagnostic pop
#endif
#include "identity-service-file-json.h"
#include "utilstring.h"
#include "errlist.h"
// end-device identity right flags
#define IDENTITY_FLAG_CAN_CONTROL_SERVICE 1
/**
* JSON attribute names
*/
#define ATTRS_COUNT 14
static const char *ATTR_NAMES[ATTRS_COUNT] = {
"addr", // 0 network address (hex string, 4 bytes)
"activation", // 1 ABP or OTAA
"deveui", // 2 device identifier (hex string, 8 bytes)
"nwkSKey", // 3 shared session key (hex string, 16 bytes)
"appSKey", // 4 private key (hex string, 16 bytes)
"class", // 5 A, B or C
"version", // 6 LoraWAN version
"appeui", // 7 OTAA application identifier (JoinEUI) (hex string, 8 bytes)
"appKey", // 8 Application key (hex string, 16 bytes)
"nwkKey", // 9 Network key (hex string, 16 bytes)
"devNonce", // 10 device identifier (hex string, 8 bytes)
"joinNonce", // 11 device identifier (hex string, 8 bytes)
"name", // 12 added for search
// not copied to the storage
"flags" // 13 if bit 0 is set it means allow control network service
};
static const char *ACTIVATION_NAMES[2] = {
"ABP",
"OTAA"
};
static int getAttrByName(
const char *name
)
{
int r = -1;
for (int i = 0; i < ATTRS_COUNT; i++) {
if (strcmp(ATTR_NAMES[i], name) == 0)
return i;
}
return r;
}
static ACTIVATION getActivationByName(
const char *name
)
{
for (int i = 0; i < 2; i++) {
if (strcmp(ACTIVATION_NAMES[i], name) == 0)
return (ACTIVATION) i;
}
// default ABP
return ABP;
}
static std::string getActivationName(
ACTIVATION value
)
{
if (value > OTAA)
value = ABP;
return ACTIVATION_NAMES[value];
}
JsonFileIdentityService::JsonFileIdentityService()
: path(""), errCode(0), errMessage(""), maxDevNwkAddr(0)
{
}
JsonFileIdentityService::~JsonFileIdentityService()
{
done();
}
/**
*
* Loads NetworkIdentities
* [
* {
* "addr": "network address (hex string, 4 bytes)"
* "eui": "device identifier (hex string, 8 bytes)",
* "nwkSKey": "shared session key (hex string, 16 bytes)",
* "appSKey": "private key (hex string, 16 bytes)"
* },
* ..
* ]
*/
class NetworkIdentityHandler : public rapidjson::BaseReaderHandler<rapidjson::UTF8<>, NetworkIdentityHandler> {
private:
JsonFileIdentityService *service;
bool isNetworkIdentity;
int idx;
DevAddr k;
DEVICEID v;
uint32_t flags;
public:
NetworkIdentityHandler(JsonFileIdentityService *svc)
: service(svc), isNetworkIdentity(false), idx(-1)
{
memset(&k.devaddr, 0, sizeof(DEVADDR));
memset(&v, 0, sizeof(DEVICEID));
if (service) {
// service->maxDevNwkAddr = 0;
// service->clear();
}
flags = 0;
}
void resetEntry() {
isNetworkIdentity = true;
memset(&k.devaddr, '\0', sizeof(DEVADDR));
memset(&v, '\0', sizeof(DEVICEID));
v.version.major = 1;
flags = 0;
}
bool Uint(unsigned u) {
switch(idx) {
case 13:
flags = u;
break;
}
return true;
}
bool String(
const char* str,
rapidjson::SizeType length,
bool copy
)
{
std::string s;
/*
* 0- addr 1- activation 2- eui 3- nwkSKey 4- appSKey 5- class 6- version, 7- name
*/
switch(idx) {
case 0:
string2DEVADDR(k.devaddr, str);
{
uint32_t na = k.getNwkAddr();
if (na > service->maxDevNwkAddr) {
// update helper data to remember last assigned address
service->maxDevNwkAddr = na;
}
}
break;
case 1:
v.activation = getActivationByName(str);
break;
case 2:
string2DEVEUI(v.devEUI, str);
break;
case 3:
s = hex2string(str);
string2KEY(v.nwkSKey, s);
break;
case 4:
s = hex2string(str);
string2KEY(v.appSKey, s);
break;
case 5:
v.deviceclass = string2deviceclass(str);
break;
case 6:
v.version = string2LORAWAN_VERSION(str);
break;
case 7:
string2DEVEUI(v.appEUI, str);
break;
case 8:
s = hex2string(str);
string2KEY(v.appKey, s);
break;
case 9:
s = hex2string(str);
string2KEY(v.nwkKey, s);
break;
case 10:
v.devNonce = string2DEVNONCE(str);
break;
case 11:
string2JOINNONCE(v.joinNonce, str);
break;
case 12:
string2DEVICENAME(v.name, str);
break;
default:
break;
}
return true;
}
bool StartObject() {
resetEntry();
return true;
}
bool Key(const char* str, rapidjson::SizeType length, bool copy) {
idx = getAttrByName(str);
return true;
}
bool EndObject(rapidjson::SizeType memberCount)
{
isNetworkIdentity = false;
if (k.empty()) {
// OTAA unassigned address, get a new one
NetworkIdentity identity(v);
int r = service->next(identity);
if (r)
return false;
memmove(&k.devaddr, &identity.devaddr, sizeof(DEVADDR));
}
service->put(k.devaddr, v);
service->setRightsMask(k.devaddr, flags);
return true;
}
bool StartArray() {
return true;
}
bool EndArray(rapidjson::SizeType elementCount) {
return true;
}
};
void JsonFileIdentityService::clear()
{
storage.clear();
maxDevNwkAddr = 0;
}
int JsonFileIdentityService::load()
{
clear();
NetworkIdentityHandler handler(this);
rapidjson::Reader reader;
FILE* fp = fopen(path.c_str(), "rb");
if (!fp)
return ERR_CODE_INVALID_JSON;
char readBuffer[4096];
rapidjson::FileReadStream istrm(fp, readBuffer, sizeof(readBuffer));
rapidjson::ParseResult r = reader.Parse<rapidjson::kParseCommentsFlag>(istrm, handler);
if (r.IsError()) {
errCode = r.Code();
std::stringstream ss;
ss << rapidjson::GetParseError_En(r.Code()) << " at " << r.Offset();
errMessage = ss.str();
} else {
errCode = 0;
errMessage = "";
}
fclose(fp);
return r.IsError() ? ERR_CODE_INVALID_JSON : 0;
}
int JsonFileIdentityService::save()
{
std::fstream os;
os.open(path.c_str(), std::ios::out);
os << "[" << std::endl;
bool addSeparator(false);
for (std::map<DEVADDRINT, DEVICEID>::const_iterator it = storage.begin(); it != storage.end(); it++) {
if (addSeparator)
os << ",";
uint32_t addrRightsMask = getRightsMask((DEVADDR &) (it->first.a));
os << std::endl << "{\""
<< ATTR_NAMES[0] << "\": \"" << DEVADDRINT2string(it->first) << "\",\""
<< ATTR_NAMES[1] << "\": \"" << getActivationName(it->second.activation) << "\",\""
<< ATTR_NAMES[2] << "\": \"" << DEVEUI2string(it->second.devEUI) << "\",\""
<< ATTR_NAMES[3] << "\": \"" << KEY2string(it->second.nwkSKey) << "\",\""
<< ATTR_NAMES[4] << "\": \"" << KEY2string(it->second.appSKey) << "\",\""
<< ATTR_NAMES[5] << "\": \"" << deviceclass2string(it->second.deviceclass) << "\",\""
<< ATTR_NAMES[6] << "\": \"" << LORAWAN_VERSION2string(it->second.version) << "\",\""
<< ATTR_NAMES[7] << "\": \"" << DEVEUI2string(it->second.appEUI) << "\",\""
<< ATTR_NAMES[8] << "\": \"" << KEY2string(it->second.appKey) << "\",\""
<< ATTR_NAMES[9] << "\": \"" << KEY2string(it->second.nwkKey) << "\",\""
<< ATTR_NAMES[10] << "\": \"" << DEVNONCE2string(it->second.devNonce) << "\",\""
<< ATTR_NAMES[11] << "\": \"" << JOINNONCE2string(it->second.joinNonce) << "\",\""
<< ATTR_NAMES[12] << "\": \"" << DEVICENAME2string(it->second.name) << "\"";
if (addrRightsMask) {
os << ",\"" << ATTR_NAMES[8] << "\": " << addrRightsMask;
}
os << "}";
addSeparator = true;
}
os << "]" << std::endl;
int r = os.bad() ? ERR_CODE_OPEN_DEVICE : 0;
os.close();
return r;
}
/**
* get device identifier by network address. Return 0 if success, retval = EUI and keys
* @param retval device identifier
* @param devaddr network address
* @return LORA_OK- success
*/
int JsonFileIdentityService::get(DeviceId &retval, DEVADDR &devaddr)
{
mutexMap.lock();
std::map<DEVADDRINT, DEVICEID>::const_iterator it(storage.find(DEVADDRINT(devaddr)));
if (it == storage.end()) {
mutexMap.unlock();
return ERR_CODE_DEVICE_ADDRESS_NOTFOUND;
}
retval.set(it->second);
mutexMap.unlock();
return 0;
}
/**
* get network identity(with address) by network address. Return 0 if success, retval = EUI and keys
* @param retval network identity(with address)
* @param eui device EUI
* @return LORA_OK- success
*/
int JsonFileIdentityService::getNetworkIdentity(
NetworkIdentity &retval,
const DEVEUI &eui
) {
mutexMap.lock();
for (std::map<DEVADDRINT, DEVICEID>::const_iterator it(storage.begin()); it != storage.end(); it++) {
if (memcmp(&it->second.devEUI, &eui, sizeof(DEVEUI)) == 0) {
retval.set(it->first, it->second);
mutexMap.unlock();
return 0;
}
}
mutexMap.unlock();
return ERR_CODE_DEVICE_ADDRESS_NOTFOUND;
}
// List entries
void JsonFileIdentityService::list(
std::vector<NetworkIdentity> &retval,
size_t offset,
size_t size
) {
size_t c = -1;
if (size == 0)
size = SIZE_MAX;
for (std::map<DEVADDRINT, DEVICEID>::const_iterator it(storage.begin()); it != storage.end(); it++) {
c++;
if (c < offset)
continue;
if (c >= size)
break;
NetworkIdentity v(it->first, it->second);
retval.push_back(v);
}
}
// Entries count
size_t JsonFileIdentityService::size()
{
return storage.size();
}
void JsonFileIdentityService::put(
DEVADDR &devaddr,
DEVICEID &id
)
{
mutexMap.lock();
storage[devaddr] = id;
mutexMap.unlock();
DevAddr a(devaddr);
uint32_t na = a.getNwkAddr();
if (na > maxDevNwkAddr) {
maxDevNwkAddr = na;
}
}
void JsonFileIdentityService::rm(
DEVADDR &addr
)
{
mutexMap.lock();
storage.erase(addr);
mutexMap.unlock();
}
int JsonFileIdentityService::init(
const std::string &option,
void *data
)
{
path = option;
return load();
}
void JsonFileIdentityService::flush()
{
save();
}
void JsonFileIdentityService::done()
{
}
int JsonFileIdentityService::parseIdentifiers(
std::vector<TDEVEUI> &retval,
const std::vector<std::string> &list,
bool useRegex
) {
for (std::vector<std::string>::const_iterator it(list.begin()); it != list.end(); it++) {
if (isHex(*it)) {
// identifier itself
TDEVEUI v(*it);
for (std::map<DEVADDRINT, DEVICEID, DEVADDRINTCompare>::const_iterator dit(storage.begin()); dit != storage.end(); dit++) {
if (memcmp(v.eui, dit->second.devEUI, sizeof(DEVEUI)) == 0) {
retval.push_back(v);
break;
}
}
} else {
// can contain regex "*"
try {
std::string re;
if (useRegex)
re = *it;
else
re = replaceAll(replaceAll(*it, "*", ".*"), "?", ".");
std::regex rex(re, std::regex_constants::grep);
for (std::map<DEVADDRINT, DEVICEID, DEVADDRINTCompare>::const_iterator dit(storage.begin()); dit != storage.end(); dit++) {
std::string s2 = DEVEUI2string(dit->second.devEUI);
if (std::regex_search(s2, rex))
retval.push_back(TDEVEUI(dit->second.devEUI));
}
}
catch (const std::regex_error&) {
return ERR_CODE_INVALID_REGEX;
}
}
}
return 0;
}
int JsonFileIdentityService::parseNames(
std::vector<TDEVEUI> &retval,
const std::vector<std::string> &list,
bool useRegex
) {
for (std::vector<std::string>::const_iterator it(list.begin()); it != list.end(); it++) {
try {
std::string re;
if (useRegex)
re = *it;
else
re = replaceAll(replaceAll(*it, "*", ".*"), "?", ".");
std::regex rex(re, std::regex_constants::grep);
for (std::map<DEVADDRINT, DEVICEID, DEVADDRINTCompare>::const_iterator dit(storage.begin()); dit != storage.end(); dit++) {
std::string s2 = DEVICENAME2string(dit->second.name);
if (std::regex_search(s2, rex))
retval.push_back(TDEVEUI(dit->second.devEUI));
}
}
catch (const std::regex_error&) {
return ERR_CODE_INVALID_REGEX;
}
}
return 0;
}
std::string JsonFileIdentityService::toJsonString()
{
std::stringstream ss;
ss << "[";
bool needComma = false;
for (std::map<DEVADDRINT, DEVICEID, DEVADDRINTCompare>::const_iterator dit(storage.begin()); dit != storage.end(); dit++) {
if (needComma)
ss << ", " << std::endl;
else
needComma = true;
ss << "{"
<< "\"" << ATTR_NAMES[0] << "\":\"" << DEVADDRINT2string(dit->first) << "\", "
<< "\"" << ATTR_NAMES[1] << "\":\"" << getActivationName(dit->second.activation) << "\", "
<< "\"" << ATTR_NAMES[2] << "\":\"" << DEVEUI2string(dit->second.devEUI) << "\", "
<< "\"" << ATTR_NAMES[3] << "\":\"" << KEY2string(dit->second.nwkSKey) << "\", "
<< "\"" << ATTR_NAMES[4] << "\":\"" << KEY2string(dit->second.appSKey) << "\", "
<< "\"" << ATTR_NAMES[5] << "\":\"" << deviceclass2string(dit->second.deviceclass) << "\", "
<< "\"" << ATTR_NAMES[6] << "\":\"" << LORAWAN_VERSION2string(dit->second.version) << "\", "
<< "\"" << ATTR_NAMES[7] << "\":\"" << DEVEUI2string(dit->second.appEUI) << "\", "
<< "\"" << ATTR_NAMES[8] << "\":\"" << KEY2string(dit->second.appKey) << "\", "
<< "\"" << ATTR_NAMES[9] << "\":\"" << KEY2string(dit->second.nwkKey) << "\", "
<< "\"" << ATTR_NAMES[10] << "\":\"" << DEVNONCE2string(dit->second.devNonce) << "\", "
<< "\"" << ATTR_NAMES[11] << "\":\"" << JOINNONCE2string(dit->second.joinNonce) << "\", "
<< "\"" << ATTR_NAMES[12] << "\":\"" << DEVICENAME2string(dit->second.name) << "\"";
uint32_t rightsMask = getRightsMask((DEVADDR &) (dit->first.a));
if (rightsMask)
ss << ",\"" << ATTR_NAMES[8] << "\": " << rightsMask;
ss << "}";
}
ss << "]";
return ss.str();
}
uint32_t JsonFileIdentityService::getRightsMask(
const DEVADDR &addr
)
{
std::map<DEVADDRINT, uint32_t>::const_iterator it(rightsMask.find(DEVADDRINT(addr)));
if (it == rightsMask.end())
return 0;
uint32_t r = it->second;
return r;
}
void JsonFileIdentityService::setRightsMask
(
const DEVADDR &addr,
uint32_t value
)
{
if (value)
rightsMask[DEVADDRINT(addr)] = value;
else {
// remove if exists
std::map<DEVADDRINT, uint32_t>::iterator it(rightsMask.find(DEVADDRINT(addr)));
if (it != rightsMask.end())
rightsMask.erase(it);
}
}
bool JsonFileIdentityService::canControlService
(
const DEVADDR &addr
)
{
return getRightsMask(addr) & IDENTITY_FLAG_CAN_CONTROL_SERVICE;
}
/**
* Return next network address if available
* @return 0- success, ERR_ADDR_SPACE_FULL- no address available
*/
int JsonFileIdentityService::next(
NetworkIdentity &retval
)
{
DevAddr nextAddr(netid, maxDevNwkAddr);
if (nextAddr.increment()) // if reach last address
return nextBruteForce(retval); // try harder
DEVADDRINT dai;
nextAddr.get(dai);
std::map<DEVADDRINT, DEVICEID>::const_iterator it = storage.find(dai);
if (it != storage.end())
return nextBruteForce(retval);
nextAddr.get(retval.devaddr);
return 0;
}
/**
* Return next available network address
* @return 0- success, ERR_ADDR_SPACE_FULL- no address available
*/
int JsonFileIdentityService::nextBruteForce(
NetworkIdentity &retval
)
{
int r = ERR_CODE_ADDR_SPACE_FULL;
DevAddr nextAddr(netid, false);
while(true) {
DEVADDRINT dai;
nextAddr.get(dai);
std::map<DEVADDRINT, DEVICEID>::const_iterator it = storage.find(dai);
if (it == storage.end()) {
// not used. This is first free address. Return it
nextAddr.get(retval.devaddr);
r = LORA_OK;
break;
}
// go to next address
r = nextAddr.increment();
if (r) {
// full of space
return r;
}
}
return r;
}