-
Notifications
You must be signed in to change notification settings - Fork 127
/
ACR122Reader.js
235 lines (165 loc) · 5.28 KB
/
ACR122Reader.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
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
"use strict";
import Reader from './Reader';
import {
ConnectError,
DisconnectError,
TransmitError,
ControlError,
AuthenticationError,
LoadAuthenticationKeyError,
ReadError,
WriteError,
GetUIDError,
CARD_NOT_CONNECTED,
OPERATION_FAILED,
UNKNOWN_ERROR,
FAILURE,
} from './errors';
class ACR122Reader extends Reader {
async inAutoPoll() {
const payload = [
0xD4,
0x60,
0xFF, // PollNr (0xFF = Endless polling)
0x01, // Period (0x01 – 0x0F) indicates the polling period in units of 150 ms
0x00, // Type 1 0x00 = Generic passive 106 kbps (ISO/IEC14443-4A, Mifare and DEP)
];
// CMD: Direct Transmit (to inner PN532 chip InAutoPoll CMD)
const packet = Buffer.from([
0xff, // Class
0x00, // INS
0x00, // P1
0x00, // P2
payload.length, // Lc: Number of Bytes to send (Maximum 255 bytes)
...payload,
]);
// console.log(packet);
let response = null;
try {
response = await this.control(packet, 2);
this.logger.debug('response received', response);
// Red OFF Green OFF 0x00
// Red ON Green OFF 0x01
// Red OFF Green ON 0x02
// Red ON Green ON 0x03
// console.log(response.slice(1));
} catch (err) {
throw err;
}
// const statusCode = response.readUInt16BE(0);
//
// if (statusCode !== 0x9000) {
// //throw new LoadAuthenticationKeyError(OPERATION_FAILED, `Load authentication key operation failed: Status code: ${statusCode}`);
// }
}
async led(led, blinking) {
// P2: LED State Control (1 byte = 8 bits)
// format:
/*
+-----+----------------------------------+-------------------------------------+
| Bit | Item | Description |
+-----+----------------------------------+-------------------------------------+
| 0 | Final Red LED State | 1 = On; 0 = Off |
| 1 | Final Green LED State | 1 = On; 0 = Off |
| 2 | Red LED State Mask | 1 = Update the State; 0 = No change |
| 3 | Green LED State Mask | 1 = Update the State; 0 = No change |
| 4 | Initial Red LED Blinking State | 1 = On; 0 = Off |
| 5 | Initial Green LED Blinking State | 1 = On; 0 = Off |
| 6 | Red LED Blinking Mask | 1 = Blink; 0 = Not Blink |
| 7 | Green LED Blinking Mask | 1 = Blink; 0 = Not Blink |
+-----+----------------------------------+-------------------------------------+
*/
//const led = 0b00001111;
//const led = 0x50;
// Data In: Blinking Duration Control (4 bytes)
// Byte 0: T1 Duration Initial Blinking State (Unit = 100 ms)
// Byte 1: T2 Duration Toggle Blinking State (Unit = 100 ms)
// Byte 2: Number of repetition
// Byte 3: Link to Buzzer
// - 00: The buzzer will not turn on
// - 01: The buzzer will turn on during the T1 Duration
// - 02: The buzzer will turn on during the T2 Duration
// - 03: The buzzer will turn on during the T1 and T2 Duration
// const blinking = [
// 0x00,
// 0x00,
// 0x00,
// 0x00
// ];
// CMD: Bi-Color LED and Buzzer Control
const packet = Buffer.from([
0xff, // Class
0x00, // INS
0x40, // P1
led, // P2: LED State Control
0x04, // Lc
...blinking, // Data In: Blinking Duration Control (4 bytes)
]);
//console.log(packet);
let response = null;
try {
response = await this.control(packet, 2);
this.logger.debug('response received', response);
// Red OFF Green OFF 0x00
// Red ON Green OFF 0x01
// Red OFF Green ON 0x02
// Red ON Green ON 0x03
//console.log(response.slice(1));
} catch (err) {
throw err;
}
// const statusCode = response.readUInt16BE(0);
//
// if (statusCode !== 0x9000) {
// //throw new LoadAuthenticationKeyError(OPERATION_FAILED, `Load authentication key operation failed: Status code: ${statusCode}`);
// }
}
async setBuzzerOutput(enabled = true) {
// CMD: Set Buzzer Output Enable for Card Detection
const packet = Buffer.from([
0xff, // Class
0x00, // INS
0x52, // P1
enabled ? 0xff : 0x00, // P2: PollBuzzStatus
0x00, // Le
]);
//console.log(packet);
let response = null;
try {
response = await this.control(packet, 2);
this.logger.debug('response received', response);
} catch (err) {
throw err;
}
const statusCode = response.readUInt16BE(0);
if (statusCode !== 0x9000) {
//throw new LoadAuthenticationKeyError(OPERATION_FAILED, `Load authentication key operation failed: Status code: ${statusCode}`);
}
}
async setPICC(picc) {
// just enable Auto ATS Generation
// const picc = 0b01000000;
// CMD: Set PICC Operating Parameter
const packet = Buffer.from([
0xff, // Class
0x00, // INS
0x51, // P1
picc, // P2: New PICC Operating Parameter
0x00, // Le
]);
//console.log(packet);
let response = null;
try {
response = await this.control(packet, 1);
this.logger.debug('response received', response);
} catch (err) {
throw err;
}
// const statusCode = response.readUInt16BE(0);
//
// if (statusCode !== 0x9000) {
// //throw new LoadAuthenticationKeyError(OPERATION_FAILED, `Load authentication key operation failed: Status code: ${statusCode}`);
// }
}
}
export default ACR122Reader;