-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
executable file
·164 lines (139 loc) · 4.46 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
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
var fs = require('fs');
var ndef = require('ndef');
var spawn = require('child_process').spawn;
/**
* Function to get UID number from result of reading tag.
* @param data result of read function
* @returns tag UID
*/
function getUID(data) {
var regex = /^.*? with UID ([a-f0-9]+)/; //regex expression to extract UID from string like "Found Mifare Classic 1k with UID 564ecb50."
var result = data.match(regex);
if (result != undefined && result != null) {
return result[1];
}
}
/**
* Function to read mifare classic tags
* callback(error, uid, data) if successful returns uid number and raw tag data
* callback(error, uid) when tag has no content
* @param callback function
*/
function read(callback) {
var temp = require("temp").track();
var fileName = temp.path({
suffix: '.bin'
});
var command = spawn('mifare-classic-read-ndef', ['-y', '-o', fileName]);
var result = '';
var uid = '';
var errorMessage = '';
command.on('error', function (err) {
errorMessage = "Cannot spawn program. Are you sure that libfreefare is installed?";
});
command.stdout.on('data', function (data) {
result += data.toString();
});
command.stderr.on('data', function (data) {
errorMessage += data.toString();
});
command.on('close', function (code) {
if (errorMessage.length > 0) {
try {
uid = getUID(result);
} catch (e) {
callback(errorMessage);
}
callback(errorMessage.replace(/\n$/, ""), uid);
}
else {
if (result.indexOf('Found') === -1) {
errorMessage = "No TAG found.";
}
uid = getUID(result);
if (code === 0 && errorMessage.length === 0) {
fs.readFile(fileName, function (err, data) {
callback(err, uid, data);
});
} else {
callback(errorMessage.replace(/\n$/, ""), uid);
}
fs.unlinkSync(fileName);
}
});
}
/**
* Function to format tag content
* @param callback null or error message
*/
function format(callback) {
var errorMessage = '';
var command = spawn('mifare-classic-format', ['-y']);
command.on('error', function (err) {
errorMessage = "Cannot spawn program. Are you sure that libfreefare is installed?";
});
command.stdout.on('data', function (data) {
process.stdout.write(data + "");
});
command.stderr.on('data', function (data) {
errorMessage += data;
});
command.on('close', function (code) {
if (code === 0) {
callback(null);
} else {
callback(errorMessage.replace(/\n$/, ""));
}
});
}
/**
* Function to write NDEF contant into mifare classic tag.
* @param data byte array of ndef data
* @param callback function
*/
function write(data, callback) {
var errorMessage = '';
var result = "";
var buffer = Buffer(data);
var temp = require("temp").track();
var fileName = temp.path({
suffix: '.bin'
});
fs.writeFile(fileName, buffer, function (err) {
var command = spawn('mifare-classic-write-ndef', ['-y', '-i', fileName]);
command.on('error', function (err) {
errorMessage = "Cannot spawn program. Are you sure that libfreefare is installed?";
});
if (err) {
callback(err);
}
command.stdout.on('data', function (data) {
process.stdout.write(data + "");
result += data;
});
command.stderr.on('data', function (data) {
errorMessage += data;
});
command.on('close', function (code) {
if (errorMessage.length > 0) {
callback(errorMessage);
}
else {
if (result.indexOf('Found') === -1) {
errorMessage = "No TAG found.";
}
if (code === 0 && errorMessage.length === 0) {
callback(null);
fs.unlinkSync(fileName);
} else {
callback(errorMessage.replace(/\n$/, ""));
}
}
});
});
}
module.exports = {
read: read,
write: write,
format: format
};