-
Notifications
You must be signed in to change notification settings - Fork 39
/
nfc.cc
495 lines (417 loc) · 18.4 KB
/
nfc.cc
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
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <err.h>
#include <nfc/nfc.h>
#include <nan.h>
#include "mifare.h"
using namespace v8;
static const nfc_modulation nmMifare = {
NMT_ISO14443A,
NBR_106,
};
static uint8_t keys[] = {
0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xd3, 0xf7, 0xd3, 0xf7, 0xd3, 0xf7,
0xa0, 0xa1, 0xa2, 0xa3, 0xa4, 0xa5,
0xb0, 0xb1, 0xb2, 0xb3, 0xb4, 0xb5,
0x4d, 0x3a, 0x99, 0xc3, 0x51, 0xdd,
0x1a, 0x98, 0x2c, 0x7e, 0x45, 0x9a,
0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0xab, 0xcd, 0xef, 0x12, 0x34, 0x56
};
static size_t num_keys = sizeof(keys) / 6;
namespace {
class NFC: public Nan::ObjectWrap {
public:
static NAN_METHOD(New);
static NAN_METHOD(Start);
static NAN_METHOD(Stop);
void stop() {
run = false;
while(claimed);
if(pnd) {
nfc_abort_command(pnd);
nfc_close(pnd);
pnd = NULL;
}
if(context) {
nfc_exit(context);
context = NULL;
}
}
nfc_device *pnd;
nfc_target nt;
nfc_context *context;
bool run;
bool claimed;
};
class NFCCard {
public:
NFCCard() {
deviceID = name = uid = tag = error = NULL;
type = data_size = offset = 0;
data = NULL;
}
~NFCCard() {
delete deviceID;
delete name;
delete uid;
delete tag;
delete error;
if(data) delete data;
}
void AddToNodeObject(Local<Object> object) {
if(deviceID) object->Set(Nan::New("deviceID").ToLocalChecked(), Nan::New(deviceID).ToLocalChecked());
if(name) object->Set(Nan::New("name").ToLocalChecked(), Nan::New(name).ToLocalChecked());
if(uid) object->Set(Nan::New("uid").ToLocalChecked(), Nan::New(uid).ToLocalChecked());
if(type) object->Set(Nan::New("type").ToLocalChecked(), Nan::New<Int32>(type));
if(tag) object->Set(Nan::New("tag").ToLocalChecked(), Nan::New(tag).ToLocalChecked());
if(error) object->Set(Nan::New("error").ToLocalChecked(), Nan::Error(error));
if(data) object->Set(Nan::New("data").ToLocalChecked(), Nan::NewBuffer(data, data_size).ToLocalChecked());
if(offset) object->Set(Nan::New("offset").ToLocalChecked(), Nan::New<Int32>((int32_t)offset));
data = NULL; //ownership transferred to nodejs
}
void SetDeviceID(const char *deviceID) {
if(this->deviceID) delete this->deviceID;
this->deviceID = strdup(deviceID);
}
void SetName(const char *name) {
if(this->name) delete this->name;
this->name = strdup(name);
}
void SetUID(const char *uid) {
if(this->uid) delete this->uid;
this->uid = strdup(uid);
}
void SetType(int32_t type) {
this->type = type;
}
void SetTag(const char *tag) {
if(this->tag) delete this->tag;
this->tag = strdup(tag);
}
void SetError(const char *error) {
if(this->error) delete this->error;
this->error = strdup(error);
}
void SetOffset(size_t offset) {
this->offset = offset;
}
void SetData(const uint8_t *data, size_t data_size) {
if(this->data) free(this->data);
this->data_size = data_size;
this->data = (char*)malloc(data_size);
memcpy(this->data, data, data_size);
}
private:
char *deviceID;
char *name;
char *uid;
int32_t type;
char *tag;
char *error;
size_t offset;
size_t data_size;
char *data;
};
class NFCReadWorker : public Nan::AsyncProgressWorker {
public:
NFCReadWorker(NFC *baton, Local<Object>self)
: Nan::AsyncProgressWorker(new Nan::Callback(self.As<Function>())), baton(baton) {
SaveToPersistent("self", self);
baton->run = true;
}
~NFCReadWorker() {
delete callback; //For some reason HandleProgressCallback only fires while callback exists.
}
void HandleOKCallback() {
Local<Value> argv = Nan::New("stopped").ToLocalChecked();
Local<Object> self = GetFromPersistent("self").As<Object>();
Nan::MakeCallback(self, "emit", 1, &argv);
}
void HandleErrorCallback() {
Local<Value> argv[2];
argv[0] = Nan::New("error").ToLocalChecked();
argv[1] = Nan::Error(AsyncProgressWorker::ErrorMessage());
Local<Object> self = GetFromPersistent("self").As<Object>();
Nan::MakeCallback(self, "emit", 2, argv);
HandleOKCallback();
}
void Execute(const AsyncProgressWorker::ExecutionProgress& progress) {
while(baton->run && nfc_initiator_select_passive_target(baton->pnd, nmMifare, NULL, 0, &baton->nt) > 0) {
baton->claimed = true;
tag = new NFCCard();
if(baton->run) ReadTag(tag);
baton->claimed = false;
progress.Send(NULL, 0);
int timeout = 5 * 1000; //5 second timeout
while(baton->run && tag && --timeout > 0) {
usleep(1000);
}
if(timeout <= 0) {
//unresponsive VM, node was likely killed while this devide was not stopped.
baton->stop();
baton->run = false;
fprintf(stderr, "Node was stopped while some NFC devices where still started.\n");
}
}
}
#define MAX_DEVICE_COUNT 16
#define MAX_FRAME_LENGTH 264
void ReadTag(NFCCard *tag) {
unsigned long cc, n;
char *bp, result[BUFSIZ];
const char *sp;
tag->SetDeviceID(nfc_device_get_connstring(baton->pnd));
tag->SetName(nfc_device_get_name(baton->pnd));
cc = baton->nt.nti.nai.szUidLen;
if (cc > sizeof baton->nt.nti.nai.abtUid) cc = sizeof baton->nt.nti.nai.abtUid;
char uid[3 * sizeof baton->nt.nti.nai.abtUid];
bzero(uid, sizeof uid);
for (n = 0, bp = uid, sp = ""; n < cc; n++, bp += strlen(bp), sp = ":") {
snprintf(bp, sizeof uid - (bp - uid), "%s%02x", sp, baton->nt.nti.nai.abtUid[n]);
}
tag->SetUID(uid);
tag->SetType(baton->nt.nti.nai.abtAtqa[1]);
switch (baton->nt.nti.nai.abtAtqa[1]) {
case 0x04:
{
tag->SetTag("mifare-classic");
// size guessing logic from nfc-mfclassic.c
uint8_t uiBlocks = ((baton->nt.nti.nai.abtAtqa[1] & 0x02) == 0x02) ? 0xff // 4Kb
: ((baton->nt.nti.nai.btSak & 0x01) == 0x01) ? 0x13 // 320b
: 0x3f; // 1Kb/2Kb
if (nfc_device_set_property_bool(baton->pnd, NP_EASY_FRAMING, false) < 0) {
snprintf(result, sizeof result, "nfc_device_set_property_bool easyFraming=false: %s",
nfc_strerror(baton->pnd));
tag->SetError(result);
break;
}
uint8_t abtRats[2] = { 0xe0, 0x50 };
uint8_t abtRx[MAX_FRAME_LENGTH];
int res = nfc_initiator_transceive_bytes(baton->pnd, abtRats, sizeof abtRats, abtRx, sizeof abtRx, 0);
if (res > 0) {
int flip;
for (flip = 0; flip < 2; flip++) {
if (nfc_device_set_property_bool(baton->pnd, NP_ACTIVATE_FIELD, flip > 0) < 0) {
snprintf(result, sizeof result, "nfc_device_set_property_bool activateField=%s: %s",
flip > 0 ? "true" : "false", nfc_strerror(baton->pnd));
tag->SetError(result);
break;
}
}
if (flip != 2) break;
if ((res >= 10)
&& (abtRx[5] == 0xc1)
&& (abtRx[6] == 0x05)
&& (abtRx[7] == 0x2f)
&& (abtRx[8] == 0x2f)
&& ((baton->nt.nti.nai.abtAtqa[1] & 0x02) == 0x00)) uiBlocks = 0x7f;
}
if (nfc_initiator_select_passive_target(baton->pnd, nmMifare, NULL, 0, &baton->nt) <= 0) {
tag->SetError("unable to reselect tag");
break;
}
if (nfc_device_set_property_bool(baton->pnd, NP_EASY_FRAMING, true) < 0) {
snprintf(result, sizeof result, "nfc_device_set_property_bool easyFraming=false: %s",
nfc_strerror(baton->pnd));
tag->SetError(result);
break;
}
int cnt, len;
uint8_t command[MAX_FRAME_LENGTH], data[4 * 1024], *dp;
len = (uiBlocks + 1) * 16;
if (((unsigned long) len) > sizeof data) len = sizeof data;
for (cnt = uiBlocks, dp = data + len - 16;
cnt >= 0;
cnt--, dp -= 16) {
if (((cnt + 1) % (cnt < 128 ? 4 : 16)) == 0) {
size_t key_index;
struct mifare_param_auth auth_params;
for (key_index = 0; key_index < num_keys; key_index++) {
bzero(command, sizeof command);
command[0] = MC_AUTH_B;
command[1] = cnt;
bzero(&auth_params, sizeof auth_params);
memcpy(auth_params.abtKey, (uint8_t *) keys + (key_index * 6),
sizeof auth_params.abtKey);
memcpy(auth_params.abtAuthUid, baton->nt.nti.nai.abtUid + baton->nt.nti.nai.szUidLen - 4,
sizeof auth_params.abtAuthUid);
memcpy(command + 2, &auth_params, sizeof auth_params);
res = nfc_initiator_transceive_bytes(baton->pnd, command, 2 + sizeof auth_params, abtRx,
sizeof abtRx, -1);
if (res >= 0) break;
}
if (key_index >= num_keys) {
snprintf(result, sizeof result, "nfc_initiator_transceive_bytes: %s", nfc_strerror(baton->pnd));
tag->SetError(result);
break;
}
}
command[0] = MC_READ;
command[1] = cnt;
res = nfc_initiator_transceive_bytes(baton->pnd, command, 2, dp, 16, -1);
if (res >= 0) continue;
if (res != NFC_ERFTRANS) {
snprintf(result, sizeof result, "nfc_initiator_transceive_bytes: %s", nfc_strerror(baton->pnd));
tag->SetError(result);
}
break;
}
if (cnt >= 0) break;
tag->SetData(data, len);
tag->SetOffset(16 * 4);
break;
}
case 0x44:
{
tag->SetTag("mifare-ultralight");
if (nfc_device_set_property_bool(baton->pnd, NP_EASY_FRAMING, true) < 0) {
snprintf(result, sizeof result, "nfc_device_set_property_bool easyFraming=false: %s",
nfc_strerror(baton->pnd));
tag->SetError(result);
break;
}
int cnt, len, res;
uint8_t command[2], data[16 * 12], *dp;
for (n = 0, cc = 0x0f, dp = data, cnt = sizeof data, len = 0;
n < cc;
n += 4, dp += res, cnt -= res, len += res) {
command[0] = MC_READ;
command[1] = n;
res = nfc_initiator_transceive_bytes(baton->pnd, command, 2, dp, cnt, -1);
if (res >= 0) continue;
if (res != NFC_ERFTRANS) {
snprintf(result, sizeof result, "nfc_initiator_transceive_bytes: %s", nfc_strerror(baton->pnd));
tag->SetError(result);
}
break;
}
if (n < cc) break;
tag->SetData(data, len);
tag->SetOffset(16);
break;
}
default:
break;
}
}
void HandleProgressCallback(const char *_tag, size_t size) {
Nan::HandleScope scope;
Local<Object> object = Nan::New<Object>();
tag->AddToNodeObject(object);
delete tag;
tag = NULL;
Local<Value> argv[2];
argv[0] = Nan::New("read").ToLocalChecked();
argv[1] = object;
Local<Object> self = GetFromPersistent("self").As<Object>();
Nan::MakeCallback(self, "emit", 2, argv);
}
private:
NFC *baton;
NFCCard *tag;
};
NAN_METHOD(NFC::New) {
Nan::HandleScope scope;
assert(info.IsConstructCall());
NFC* self = new NFC();
self->Wrap(info.This());
info.GetReturnValue().Set(info.This());
}
NAN_METHOD(NFC::Stop) {
Nan::HandleScope scope;
NFC* nfc = ObjectWrap::Unwrap<NFC>(info.This());
nfc->stop();
info.GetReturnValue().Set(info.This());
}
NAN_METHOD(NFC::Start) {
Nan::HandleScope scope;
nfc_context *context;
nfc_init(&context);
if (context == NULL) return Nan::ThrowError("unable to init libfnc (malloc).");
nfc_device *pnd;
if (info.Length() > 0) {
if (!info[0]->IsString()) {
nfc_exit(context);
return Nan::ThrowError("deviceID parameter is not a string");
}
nfc_connstring connstring;
String::Utf8Value device(info[0]->ToString());
snprintf(connstring, sizeof connstring, "%s", *device);
pnd = nfc_open(context, connstring);
} else {
pnd = nfc_open(context, NULL);
}
if (pnd == NULL) {
nfc_exit(context);
return Nan::ThrowError("unable open NFC device");
}
char result[BUFSIZ];
if (nfc_initiator_init(pnd) < 0) {
snprintf(result, sizeof result, "nfc_initiator_init: %s", nfc_strerror(pnd));
nfc_close(pnd);
nfc_exit(context);
return Nan::ThrowError(result);
}
NFC *baton = ObjectWrap::Unwrap<NFC>(info.This());
baton->context = context;
baton->pnd = pnd;
NFCReadWorker* readWorker = new NFCReadWorker(baton, info.This());
Nan::AsyncQueueWorker(readWorker);
Local<Object> object = Nan::New<Object>();
object->Set(Nan::New("deviceID").ToLocalChecked(), Nan::New(nfc_device_get_connstring(baton->pnd)).ToLocalChecked());
object->Set(Nan::New("name").ToLocalChecked(), Nan::New(nfc_device_get_name(baton->pnd)).ToLocalChecked());
info.GetReturnValue().Set(object);
}
NAN_METHOD(Scan) {
Nan::HandleScope scope;
nfc_context *context;
nfc_init(&context);
if (context == NULL) return Nan::ThrowError("unable to init libfnc (malloc).");
Local<Object> object = Nan::New<Object>();
nfc_connstring connstrings[MAX_DEVICE_COUNT];
size_t i, n = nfc_list_devices(context, connstrings, MAX_DEVICE_COUNT);
for (i = 0; i < n; i++) {
Local<Object> entry = Nan::New<Object>();
nfc_device *pnd = nfc_open(context, connstrings[i]);
if (pnd == NULL) continue;
entry->Set(Nan::New("name").ToLocalChecked(), Nan::New(nfc_device_get_name(pnd)).ToLocalChecked());
char *info;
if (nfc_device_get_information_about(pnd, &info) >= 0) {
entry->Set(Nan::New("info").ToLocalChecked(), Nan::New(info).ToLocalChecked());
nfc_free(info);
} else {
entry->Set(Nan::New("info").ToLocalChecked(), Nan::New("").ToLocalChecked());
}
object->Set(Nan::New(nfc_device_get_connstring(pnd)).ToLocalChecked(), entry);
nfc_close(pnd);
}
nfc_exit(context);
info.GetReturnValue().Set(object);
}
NAN_METHOD(Version) {
Nan::HandleScope scope;
nfc_context *context;
nfc_init(&context);
if (context == NULL) return Nan::ThrowError("unable to init libnfc (malloc).");
Local<Object> object = Nan::New<Object>();
object->Set(Nan::New("name").ToLocalChecked(), Nan::New("libnfc").ToLocalChecked());
object->Set(Nan::New("version").ToLocalChecked(), Nan::New(nfc_version()).ToLocalChecked());
nfc_exit(context);
info.GetReturnValue().Set(object);
}
NAN_MODULE_INIT(init) {
Local<v8::FunctionTemplate> tpl = Nan::New<v8::FunctionTemplate>(NFC::New);
tpl->SetClassName(Nan::New("NFC").ToLocalChecked());
tpl->InstanceTemplate()->SetInternalFieldCount(1);
SetPrototypeMethod(tpl, "start", NFC::Start);
SetPrototypeMethod(tpl, "stop", NFC::Stop);
Nan::Export(target, "version", Version);
Nan::Export(target, "scan", Scan);
Nan::Set(target, Nan::New("NFC").ToLocalChecked(), tpl->GetFunction());
};
}
NODE_MODULE(nfc, init)