forked from QHPaeek/Arduino-Aime-Reader
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAime_Reader.h
More file actions
603 lines (563 loc) · 15.1 KB
/
Aime_Reader.h
File metadata and controls
603 lines (563 loc) · 15.1 KB
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
#include <string.h>
#if defined(__AVR_ATmega32U4__)
#pragma message "当前的开发板是 ATmega32U4"
#define SerialDevice SerialUSB
#define NUM_LEDS 11
#define LED_PIN A3
#define BOARD_VISION 1
#include "lib/WS2812_FastLed.h"
#define PN532_SPI_SS 10
#elif defined(ARDUINO_SAMD_ZERO)
#pragma message "当前的开发板是SAMD_ZERO"
#define SerialDevice SerialUSB
#define NUM_LEDS 11
#define LED_PIN A3
#define BOARD_VISION 2
#include "lib/WS2812_FastLed.h"
#define PN532_SPI_SS 10
#elif defined(ESP8266)
#pragma message "当前的开发板是 ESP8266"
#define SerialDevice Serial
#define NUM_LEDS 11
//#define LED_PIN D5 //NodeMCU 1.0(ESP12E-Mod)
#define LED_PIN 14 //Generic ESP8266 Module
#define BOARD_VISION 3
#include "lib/WS2812_FastLed.h"
#elif defined(ESP32)
#pragma message "当前的开发板是 ESP32"
#define SerialDevice Serial
#define NUM_LEDS 11
#define LED_PIN 1
#define BOARD_VISION 4
#define SDA_Pin 1 // select ESP32 I2C pins
#define SCL_Pin 2
#include "lib/WS2812_FastLed.h"
//#define PN532_SPI_SS 5
#elif defined(AIR001xx)
#pragma message "当前的开发板是 AIR001"
#define SerialDevice Serial
//LED灯的个数
#define NUM_LEDS 11
//LED引脚为PA7,不支持更改,不需要定义
#define BOARD_VISION 5
#include "lib/WS2812_Air001.h"
#elif defined(ARDUINO_GENERIC_F103C8TX) || defined(ARDUINO_GENERIC_F103CBTX) || defined(ARDUINO_GENERIC_F103CBUX)
#pragma message "当前的开发板是 STM32F103C8/CB"
#define SerialDevice Serial
#define BOARD_VISION 6
//#define SOFT_PWM
#include "lib/LED_PWM.h"
#define PN532_UART
//#include <SoftwareSerial.h>
//SoftwareSerial UART(PB7, PB6);
HardwareSerial Serial1(PB7, PB6);
#elif defined(ARDUINO_GENERIC_F072C8TX)
#pragma message "当前的开发板是 STM32F072C8"
//Generic STM32F1 series
#define SerialDevice Serial
#define BOARD_VISION 7
#define LED_PIN_RED PB1
#define LED_PIN_GREEN PB_11
#define LED_PIN_BLUE PB_10
#include "lib/LED_analogwrite.h"
#define PN532_UART
HardwareSerial Serial1(PB_7, PB_6);
#elif defined(ARDUINO_ARCH_RP2040)
#pragma message "当前的开发板是 RP2040"
#define SerialDevice Serial
#define BOARD_VISION 8
//#define LED_PIN D5
#elif defined(__AVR_ATmega328P__)
#pragma message "当前的开发板是 ATmega328P"
#define SerialDevice Serial
#define NUM_LEDS 11
#define LED_PIN 13
#define BOARD_VISION 9
#include "lib/WS2812_FastLed.h"
#elif defined(ESP32S3)
#pragma message "当前的开发板是 ESP32S3"
#define SerialDevice Serial
#define NUM_LEDS 11
//#define LED_PIN D5 //NodeMCU 1.0(ESP12E-Mod)
#define LED_PIN 0 //Generic ESP8266 Module
#define BOARD_VISION 10
#define SDA_Pin 1 // select ESP32 I2C pins
#define SCL_Pin 2
#include "lib/WS2812_FastLed.h"
#elif defined(_BOARD_GENERIC_STM32F103C_H_)
#pragma message "当前的开发板是 STM32F103C6"
#define SerialDevice Serial
#define BOARD_VISION 11
#define LED_PIN_RED PB1
#define LED_PIN_GREEN PB11
#define LED_PIN_BLUE PB10
#include "lib/LED_analogwrite.h"
#define PN532_UART
#else
#error "未经测试的开发板,请检查串口和针脚定义"
#endif
#if defined(PN532_SPI_SS)
#pragma message "使用 SPI 连接 PN532"
#include <SPI.h>
#include <PN532_SPI.h>
PN532_SPI pn532(SPI, PN532_SPI_SS);
#elif defined(PN532_UART)
#pragma message "使用 UART 连接 PN532"
#include <PN532_HSU.h>
PN532_HSU pn532(Serial1);
#elif defined(PN532_SWUART)
#pragma message "使用 SWUART 连接 PN532"
#include <PN532_SWHSU.h>
PN532_SWHSU pn532(UART);
#else
#pragma message "使用 I2C 连接 PN532"
#include <Wire.h>
#include <PN532_I2C.h>
PN532_I2C pn532(Wire);
#endif
#include "PN532.h"
PN532 nfc(pn532);
uint8_t KeyA[6], KeyB[6];
#include <EEPROM.h>
//EEPROM
//1:系统设置,第0位保留,第1位是否开启高波特率,第二位是否开启LED,第三位是否启用AIC卡号映射
//2:LED亮度
//3:固件版本号
//4-11:被映射AIC卡号IDM
//12-22:目标卡号
uint8_t system_setting[3] = {0};
uint8_t mapped_card_IDm[8] = {0};
uint8_t default_system_setting[3] = {0b10000110,128,7};
//LED灯颜色缓冲区,每次循环根据缓冲区颜色刷一次灯。
uint8_t LED_buffer[3] = {0};
enum {
CMD_GET_FW_VERSION = 0x30,
CMD_GET_HW_VERSION = 0x32,
// Card read
CMD_START_POLLING = 0x40,
CMD_STOP_POLLING = 0x41,
CMD_CARD_DETECT = 0x42,
CMD_CARD_SELECT = 0x43,
CMD_CARD_HALT = 0x44,
// MIFARE
CMD_MIFARE_KEY_SET_A = 0x50,
CMD_MIFARE_AUTHORIZE_A = 0x51,
CMD_MIFARE_READ = 0x52,
CMD_MIFARE_WRITE = 0x53,
CMD_MIFARE_KEY_SET_B = 0x54,
CMD_MIFARE_AUTHORIZE_B = 0x55,
// Boot,update
CMD_TO_UPDATER_MODE = 0x60,
CMD_SEND_HEX_DATA = 0x61,
CMD_TO_NORMAL_MODE = 0x62,
CMD_SEND_BINDATA_INIT = 0x63,
CMD_SEND_BINDATA_EXEC = 0x64,
// FeliCa
CMD_FELICA_PUSH = 0x70,
CMD_FELICA_THROUGH = 0x71,
CMD_FELICA_THROUGH_POLL = 0x00,
CMD_FELICA_THROUGH_READ = 0x06,
CMD_FELICA_THROUGH_WRITE = 0x08,
CMD_FELICA_THROUGH_GET_SYSTEM_CODE = 0x0C,
CMD_FELICA_THROUGH_NDA_A4 = 0xA4,
// LED board
CMD_EXT_BOARD_LED = 0x80,
CMD_EXT_BOARD_LED_RGB = 0x81,
CMD_EXT_BOARD_LED_RGB_UNKNOWN = 0x82, // 未知
CMD_EXT_BOARD_INFO = 0xf0,
CMD_EXT_FIRM_SUM = 0xf2,
CMD_EXT_SEND_HEX_DATA = 0xf3,
CMD_EXT_TO_BOOT_MODE = 0xf4,
CMD_EXT_TO_NORMAL_MODE = 0xf5,
//读卡器上位机功能
CMD_READ_EEPROM = 0xf6,
CMD_WRITE_EEPROM = 0xf7,
CMD_SW_READTEST_MODE = 0xf8,
};
enum { // 未确认效果
ERROR_NONE = 0,
ERROR_NFCRW_INIT_ERROR = 1,
ERROR_NFCRW_FIRMWARE_UP_TO_DATE = 3,
ERROR_NFCRW_ACCESS_ERROR = 4,
ERROR_CARD_DETECT_TIMEOUT = 5,
ERROR_CARD_DETECT_ERROR = 32,
ERROR_FELICA_ERROR = 33,
};
struct mifare_card {
bool enable;
uint8_t block0[16]= {0xED,0x88,0xA1,0x5F,0x9B,0x88,0x04,0x00,0xC8,0x50,0x00,0x20,0x00,0x00,0x00,0x16};
uint8_t block1[16]= {0};
uint8_t block2[16]= {0};
uint8_t block3[16]= {0x57,0x43,0x43,0x46,0x76,0x32,0x70,0xF8,0x78,0x11,0x57,0x43,0x43,0x46,0x76,0x32};
};
struct mifare_card card_reflect;
typedef union {
uint8_t bytes[128];
struct {
uint8_t frame_len;
uint8_t addr;
uint8_t seq_no;
uint8_t cmd;
uint8_t payload_len;
union {
uint8_t key[6]; // CMD_MIFARE_KEY_SET
uint8_t color_payload[3]; // CMD_EXT_BOARD_LED_RGB
struct {
uint8_t eeprom_data[2]; //系统内部设置
uint8_t mapped_IDm[8];
uint8_t target_accesscode[10];
};
struct { // CMD_CARD_SELECT,AUTHORIZE,READ
uint8_t uid[4];
uint8_t block_no;
};
struct { // CMD_FELICA_THROUGH
uint8_t encap_IDm[8];
uint8_t encap_len;
uint8_t encap_code;
union {
struct { // CMD_FELICA_THROUGH_POLL
uint8_t poll_systemCode[2];
uint8_t poll_requestCode;
uint8_t poll_timeout;
};
struct { // CMD_FELICA_THROUGH_READ,WRITE,NDA_A4
uint8_t RW_IDm[8];
uint8_t numService;
uint8_t serviceCodeList[2];
uint8_t numBlock;
uint8_t blockList[1][2]; // CMD_FELICA_THROUGH_READ
uint8_t blockData[16]; // CMD_FELICA_THROUGH_WRITE
};
uint8_t felica_payload[1];
};
};
};
};
} packet_request_t;
typedef union {
uint8_t bytes[128];
struct {
uint8_t frame_len;
uint8_t addr;
uint8_t seq_no;
uint8_t cmd;
uint8_t status;
uint8_t payload_len;
union {
uint8_t version[1]; // CMD_GET_FW_VERSION,CMD_GET_HW_VERSION,CMD_EXT_BOARD_INFO
uint8_t block[16]; // CMD_MIFARE_READ
struct{
uint8_t eeprom_data[3];
uint8_t board_vision;
};
struct { // CMD_CARD_DETECT
uint8_t count;
uint8_t type;
uint8_t id_len;
union {
uint8_t mifare_uid[4];
struct {
uint8_t IDm[8];
uint8_t PMm[8];
};
};
};
struct { // CMD_FELICA_THROUGH
uint8_t encap_len;
uint8_t encap_code;
uint8_t encap_IDm[8];
union {
struct { // FELICA_CMD_POLL
uint8_t poll_PMm[8];
uint8_t poll_systemCode[2];
};
struct {
uint8_t RW_status[2];
uint8_t numBlock;
uint8_t blockData[1][1][16];
};
uint8_t felica_payload[1];
};
};
};
};
} packet_response_t;
packet_request_t req;
packet_response_t res;
uint8_t len, r, checksum;
bool escape = false;
void LED_refresh()
{
if (!(system_setting[0] & 0b100)){
LED_show(0,0,0);
}
else{
LED_show((uint8_t)LED_buffer[0],(uint8_t)LED_buffer[1],(uint8_t)LED_buffer[2]);
}
}
uint8_t packet_read() {
while (SerialDevice.available()) {
r = SerialDevice.read();
if (r == 0xE0) {
req.frame_len = 0xFF;
continue;
}
if (req.frame_len == 0xFF) {
req.frame_len = r;
len = 0;
checksum = r;
continue;
}
if (r == 0xD0) {
escape = true;
continue;
}
if (escape) {
r++;
escape = false;
}
req.bytes[++len] = r;
if (len == req.frame_len && checksum == r) {
return req.cmd;
}
checksum += r;
}
return 0;
}
void packet_write() {
uint8_t checksum = 0, len = 0;
if (res.cmd == 0) {
return;
}
SerialDevice.write(0xE0);
while (len <= res.frame_len) {
uint8_t w;
if (len == res.frame_len) {
w = checksum;
} else {
w = res.bytes[len];
checksum += w;
}
if (w == 0xE0 || w == 0xD0) {
SerialDevice.write(0xD0);
SerialDevice.write(--w);
} else {
SerialDevice.write(w);
}
len++;
}
res.cmd = 0;
}
void res_init(uint8_t payload_len = 0) {
res.frame_len = 6 + payload_len;
res.addr = req.addr;
res.seq_no = req.seq_no;
res.cmd = req.cmd;
res.status = ERROR_NONE;
res.payload_len = payload_len;
}
void sys_to_normal_mode() {
res_init();
if (nfc.getFirmwareVersion()) {
res.status = ERROR_NFCRW_FIRMWARE_UP_TO_DATE;
} else {
res.status = ERROR_NFCRW_INIT_ERROR;
LED_buffer[0] = system_setting[1];
LED_buffer[1] = 0;
LED_buffer[2] = 0;
}
}
void sys_get_fw_version() {
if(system_setting[0] & 0b10){
const char fw_version[2] = "\x94";
res_init(sizeof(fw_version) - 1);
memcpy(res.version, fw_version, res.payload_len);
}
else{
const char fw_version[24] = "TN32MSEC003S F/W Ver1.2";
res_init(sizeof(fw_version) - 1);
memcpy(res.version, fw_version, res.payload_len);
}
}
void sys_get_hw_version() {
if(system_setting[0] & 0b10){
const char hw_version[10] = "837-15396";
res_init(sizeof(hw_version) - 1);
memcpy(res.version, hw_version, res.payload_len);
}
else{
const char hw_version[24] = "TN32MSEC003S H/W Ver3.0";
res_init(sizeof(hw_version) - 1);
memcpy(res.version, hw_version, res.payload_len);
}
}
void sys_get_led_info() {
if(system_setting[0] & 0b10){
const char led_info[13] = "000-00000\xFF\x11\x40";
res_init(sizeof(led_info) - 1);
memcpy(res.version, led_info, res.payload_len);
}
else{
const char led_info[10] = "15084\xFF\x10\x00\x12";
res_init(sizeof(led_info) - 1);
memcpy(res.version, led_info, res.payload_len);
}
}
void nfc_start_polling() {
res_init();
nfc.setRFField(0x00, 0x01);
}
void nfc_stop_polling() {
res_init();
nfc.setRFField(0x00, 0x00);
}
void nfc_card_detect() {
uint16_t SystemCode;
uint8_t bufferLength;
if (nfc.readPassiveTargetID(PN532_MIFARE_ISO14443A, res.mifare_uid, &res.id_len) && nfc.getBuffer(&bufferLength)[4] == 0x08) { // Only read cards with sak=0x08
res_init(0x07);
res.count = 1;
res.type = 0x10;
} else if (nfc.felica_Polling(0xFFFF, 0x00, res.IDm, res.PMm, &SystemCode, 50) == 1) {
if(system_setting[0] & 0b1000)
{
bool card_judge = true;
for(uint8_t i = 0;i<8;i++)
{
if(res.IDm[i] != mapped_card_IDm[i])
{
card_judge = false;
break;
}
}
if(card_judge)
{
card_reflect.enable = true;
res_init(0x07);
res.count = 1;
res.type = 0x10;
return;
}
else{
res_init(0x13);
res.count = 1;
res.type = 0x20;
res.id_len = 0x10;
return;
}
}
else{
res_init(0x13);
res.count = 1;
res.type = 0x20;
res.id_len = 0x10;
}
} else {
res_init(1);
res.count = 0;
res.status = ERROR_NONE;
}
}
void nfc_mifare_authorize_a() {
res_init();
if ((!nfc.mifareclassic_AuthenticateBlock(req.uid, 4, req.block_no, 0, KeyA)) && (!card_reflect.enable)) {
res.status = ERROR_NFCRW_ACCESS_ERROR;
}
}
void nfc_mifare_authorize_b() {
res_init();
if ((!nfc.mifareclassic_AuthenticateBlock(req.uid, 4, req.block_no, 1, KeyB)) && (!card_reflect.enable)) {
res.status = ERROR_NFCRW_ACCESS_ERROR;
}
}
void nfc_mifare_read() {
res_init(0x10);
if(card_reflect.enable){
switch(req.block_no){
case 0:
memcpy(res.block,card_reflect.block0,16);
break;
case 1:
memcpy(res.block,card_reflect.block1,16);
break;
case 2:
memcpy(res.block,card_reflect.block2,16);
card_reflect.enable = false;
break;
case 3:
memcpy(res.block,card_reflect.block3,16);
break;
default:
res_init();
res.status = ERROR_CARD_DETECT_TIMEOUT;
break;
}
return;
}
else if (!nfc.mifareclassic_ReadDataBlock(req.block_no, res.block)) {
res_init();
res.status = ERROR_CARD_DETECT_TIMEOUT;
}
}
void nfc_felica_through() {
uint16_t SystemCode;
if (nfc.felica_Polling(0xFFFF, 0x01, res.encap_IDm, res.poll_PMm, &SystemCode, 50) == 1) {
SystemCode = SystemCode >> 8 | SystemCode << 8;
} else {
res_init();
res.status = ERROR_FELICA_ERROR;
return;
}
uint8_t code = req.encap_code;
res.encap_code = code + 1;
switch (code) {
case CMD_FELICA_THROUGH_POLL:
{
res_init(0x14);
res.poll_systemCode[0] = SystemCode;
res.poll_systemCode[1] = SystemCode >> 8;
}
break;
case CMD_FELICA_THROUGH_GET_SYSTEM_CODE:
{
res_init(0x0D);
res.felica_payload[0] = 0x01;
res.felica_payload[1] = SystemCode;
res.felica_payload[2] = SystemCode >> 8;
}
break;
case CMD_FELICA_THROUGH_NDA_A4:
{
res_init(0x0B);
res.felica_payload[0] = 0x00;
}
break;
case CMD_FELICA_THROUGH_READ:
{
uint16_t serviceCodeList[1] = { (uint16_t)(req.serviceCodeList[1] << 8 | req.serviceCodeList[0]) };
for (uint8_t i = 0; i < req.numBlock; i++) {
uint16_t blockList[1] = { (uint16_t)(req.blockList[i][0] << 8 | req.blockList[i][1]) };
if (nfc.felica_ReadWithoutEncryption(1, serviceCodeList, 1, blockList, res.blockData[i]) != 1) {
memset(res.blockData[i], 0, 16); // dummy data
}
}
res.RW_status[0] = 0;
res.RW_status[1] = 0;
res.numBlock = req.numBlock;
res_init(0x0D + req.numBlock * 16);
}
break;
case CMD_FELICA_THROUGH_WRITE:
{
res_init(0x0C); // WriteWithoutEncryption,ignore
res.RW_status[0] = 0;
res.RW_status[1] = 0;
}
break;
default:
res_init();
res.status = ERROR_FELICA_ERROR;
}
res.encap_len = res.payload_len;
}