-
Notifications
You must be signed in to change notification settings - Fork 39
/
index.js
94 lines (77 loc) · 2.36 KB
/
index.js
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
var nfc = require('bindings')('nfc')
, events = require('events')
, ndef = require('ndef')
;
var inherits = function(target, source) {
for (var k in source.prototype) {
target.prototype[k] = source.prototype[k];
}
};
inherits(nfc.NFC, events.EventEmitter);
exports.nfc = { version : nfc.version
, NFC : nfc.NFC
};
exports.nfc.parse = function(data) {
var bytes, i, results, tlv;
results = [];
for (i = 0, bytes = data.toJSON().data; i < bytes.length; i += tlv.len) {
tlv = { type: bytes[i++] };
if ((tlv.type === 0xfe) || (i >= bytes.length)) {
results.push(tlv);
break;
}
tlv.len = bytes[i++];
if (tlv.len === 0xff) {
if ((i + 1) >= bytes.length) break;
tlv.len = bytes[i++] << 8;
tlv.len += bytes[i++];
}
if ((tlv.len > 0) && ((i + tlv.len) < bytes.length)) tlv.value = bytes.slice(i, i + tlv.len);
if ((tlv.type === 0x03) && (!!tlv.value)) tlv.ndef = ndef.decodeMessage(tlv.value);
if (!!tlv.value) tlv.value = ndef.util.bytesToHexString(tlv.value);
results.push(tlv);
}
return results;
};
exports.nfc.scan = function() {
var device, devices, i, info, j, k, kv, mod, mods, prop, props, protocol, results, speeds, v, x;
results = {};
devices = nfc.scan();
for (device in devices) if (devices.hasOwnProperty(device)) {
props = devices[device].info.split('\n');
info = {};
for (i = 0; i < props.length; i++) {
prop = props[i].trim();
if (prop === '') continue;
x = prop.indexOf(':');
if (x < 1) {
info[i] = prop;
continue;
}
k = prop.substring(0, x);
v = prop.substring(x + 1).trim();
if (v.indexOf(')') === -1) {
info[k] = v;
continue;
}
kv = v.split('), ');
mods = {};
for (j = 0; j < kv.length; j++) {
mod = kv[j].trim();
if (mod === '') continue;
x = mod.indexOf(' (');
if (x < 1) {
mods[j] = mod;
continue;
}
protocol = mod.substring(0, x);
speeds = mod.substring(x + 2).trim();
if (speeds.indexOf(')') === (speeds.length - 1)) speeds = speeds.substring(0, speeds.length - 1);
mods[protocol] = speeds.split(', ');
}
info[k] = mods;
}
results[device] = { name: devices[device].name, info: info };
}
return results;
};