1
0
mirror of synced 2024-11-24 08:00:09 +01:00

用真实 Amusement IC 的 PMm 覆盖模拟 Felica 值。

This commit is contained in:
Sucareto 2021-11-21 00:09:22 +08:00
parent d5bf4658b0
commit a48eeee48d

644
cmd.h
View File

@ -1,321 +1,323 @@
#include "FastLED.h" #include "FastLED.h"
#define NUM_LEDS 6 #define NUM_LEDS 6
CRGB leds[NUM_LEDS]; CRGB leds[NUM_LEDS];
#include <Wire.h> #include <Wire.h>
#include <PN532_I2C.h> #include <PN532_I2C.h>
#include <PN532.h> #include <PN532.h>
PN532_I2C pn532i2c(Wire); PN532_I2C pn532i2c(Wire);
PN532 nfc(pn532i2c); PN532 nfc(pn532i2c);
uint8_t cardtype, uid[4], uL; uint8_t cardtype, uid[4], uL;
uint16_t systemCode = 0xFFFF; uint16_t systemCode = 0xFFFF;
uint8_t requestCode = 0x01; uint8_t requestCode = 0x01;
typedef union { typedef union {
uint8_t block[18]; uint8_t block[18];
struct { struct {
uint8_t IDm[8]; uint8_t IDm[8];
uint8_t PMm[8]; uint8_t PMm[8];
uint16_t SystemCode; uint16_t SystemCode;
}; };
} Felica; } Felica;
Felica felica; Felica felica;
uint8_t AimeKey[6], BanaKey[6]; uint8_t AimeKey[6], BanaKey[6];
#ifdef M2F #ifdef M2F
#define M2F_B 1 //指定从mifare读取第几block当作felica的IDm和PMm #define M2F_B 1 //指定从mifare读取第几block当作felica的IDm和PMm
uint8_t MifareKey[6] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF}; uint8_t MifareKey[6] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
#endif #endif
enum { enum {
SG_NFC_CMD_GET_FW_VERSION = 0x30,//获取 FW 版本 SG_NFC_CMD_GET_FW_VERSION = 0x30,//获取 FW 版本
SG_NFC_CMD_GET_HW_VERSION = 0x32,//获取 HW 版本 SG_NFC_CMD_GET_HW_VERSION = 0x32,//获取 HW 版本
SG_RGB_CMD_GET_INFO = 0xF0,//获取 LED 信息 SG_RGB_CMD_GET_INFO = 0xF0,//获取 LED 信息
SG_NFC_CMD_RESET = 0x62,//重置读卡器 SG_NFC_CMD_RESET = 0x62,//重置读卡器
SG_RGB_CMD_SET_COLOR = 0x81,//LED 颜色设置 SG_RGB_CMD_SET_COLOR = 0x81,//LED 颜色设置
SG_RGB_CMD_RESET = 0xF5,//LED 重置 SG_RGB_CMD_RESET = 0xF5,//LED 重置
SG_NFC_CMD_RADIO_ON = 0x40,//打开读卡器 SG_NFC_CMD_RADIO_ON = 0x40,//打开读卡器
SG_NFC_CMD_RADIO_OFF = 0x41,//关闭读卡器 SG_NFC_CMD_RADIO_OFF = 0x41,//关闭读卡器
SG_NFC_CMD_POLL = 0x42,//发送卡号 SG_NFC_CMD_POLL = 0x42,//发送卡号
SG_NFC_CMD_MIFARE_SELECT_TAG = 0x43, SG_NFC_CMD_MIFARE_SELECT_TAG = 0x43,
SG_NFC_CMD_MIFARE_SET_KEY_BANA = 0x50, SG_NFC_CMD_MIFARE_SET_KEY_BANA = 0x50,
SG_NFC_CMD_MIFARE_SET_KEY_AIME = 0x54, SG_NFC_CMD_MIFARE_SET_KEY_AIME = 0x54,
SG_NFC_CMD_MIFARE_READ_BLOCK = 0x52, SG_NFC_CMD_MIFARE_READ_BLOCK = 0x52,
SG_NFC_CMD_MIFARE_AUTHENTICATE = 0x55, SG_NFC_CMD_MIFARE_AUTHENTICATE = 0x55,
SG_NFC_CMD_FELICA_ENCAP = 0x71, SG_NFC_CMD_FELICA_ENCAP = 0x71,
//FELICA_ENCAP //FELICA_ENCAP
FELICA_CMD_POLL = 0x00, FELICA_CMD_POLL = 0x00,
FELICA_CMD_GET_SYSTEM_CODE = 0x0C, FELICA_CMD_GET_SYSTEM_CODE = 0x0C,
FELICA_CMD_NDA_A4 = 0xA4, FELICA_CMD_NDA_A4 = 0xA4,
FELICA_CMD_NDA_06 = 0x06,//测试中,作用未知 FELICA_CMD_NDA_06 = 0x06,//测试中,作用未知
FELICA_CMD_NDA_08 = 0x08,//测试中,作用未知 FELICA_CMD_NDA_08 = 0x08,//测试中,作用未知
}; };
typedef union packet_req { typedef union packet_req {
uint8_t bytes[256]; uint8_t bytes[256];
struct { struct {
uint8_t frame_len; uint8_t frame_len;
uint8_t addr; uint8_t addr;
uint8_t seq_no; uint8_t seq_no;
uint8_t cmd; uint8_t cmd;
uint8_t payload_len; uint8_t payload_len;
union { union {
uint8_t key[6];// sg_nfc_req_mifare_set_key(bana or aime) uint8_t key[6];// sg_nfc_req_mifare_set_key(bana or aime)
uint8_t color_payload[3];//sg_led_req_set_color uint8_t color_payload[3];//sg_led_req_set_color
struct {// sg_nfc_cmd_mifare_read_block struct {// sg_nfc_cmd_mifare_read_block
uint8_t uid[4]; uint8_t uid[4];
uint8_t block_no; uint8_t block_no;
}; };
struct {// sg_nfc_req_felica_encap struct {// sg_nfc_req_felica_encap
uint8_t IDm[8]; uint8_t IDm[8];
uint8_t encap_len; uint8_t encap_len;
uint8_t code; uint8_t code;
uint8_t felica_payload[241]; uint8_t felica_payload[241];
}; };
}; };
}; };
} packet_req_t; } packet_req_t;
typedef union packet_res { typedef union packet_res {
uint8_t bytes[256]; uint8_t bytes[256];
struct { struct {
uint8_t frame_len; uint8_t frame_len;
uint8_t addr; uint8_t addr;
uint8_t seq_no; uint8_t seq_no;
uint8_t cmd; uint8_t cmd;
uint8_t status; uint8_t status;
uint8_t payload_len; uint8_t payload_len;
union { union {
char version[23];// sg_nfc_res_get_fw_version,sg_nfc_res_get_hw_version char version[23];// sg_nfc_res_get_fw_version,sg_nfc_res_get_hw_version
uint8_t reset_payload; //sg_led_res_reset uint8_t reset_payload; //sg_led_res_reset
uint8_t info_payload[9]; //sg_led_res_get_info uint8_t info_payload[9]; //sg_led_res_get_info
uint8_t block[16];// sg_nfc_res_mifare_read_block uint8_t block[16];// sg_nfc_res_mifare_read_block
struct {// sg_nfc_res_poll struct {// sg_nfc_res_poll
uint8_t count; uint8_t count;
uint8_t type; uint8_t type;
uint8_t id_len; uint8_t id_len;
uint8_t IDm[8];//and Aime uid uint8_t IDm[8];//and Aime uid
uint8_t PMm[8]; uint8_t PMm[8];
}; };
struct {// sg_nfc_res_felica_encap struct {// sg_nfc_res_felica_encap
uint8_t encap_len; uint8_t encap_len;
uint8_t code; uint8_t code;
uint8_t encap_IDm[8]; uint8_t encap_IDm[8];
union { union {
struct {//FELICA_CMD_POLL struct {//FELICA_CMD_POLL
uint8_t encap_PMm[8]; uint8_t encap_PMm[8];
uint8_t system_code[2]; uint8_t system_code[2];
}; };
struct {//NDA06 struct {//NDA06
uint8_t NDA06_code[3]; uint8_t NDA06_code[3];
uint8_t NDA06_IDm[8]; uint8_t NDA06_IDm[8];
uint8_t NDA06_Data[8]; uint8_t NDA06_Data[8];
}; };
uint8_t felica_payload[241]; uint8_t felica_payload[241];
}; };
}; };
}; };
}; };
} packet_res_t; } packet_res_t;
static packet_req_t req; static packet_req_t req;
static packet_res_t res; static packet_res_t res;
static void sg_res_init(uint8_t payload_len = 0) { //初始化模板 static void sg_res_init(uint8_t payload_len = 0) { //初始化模板
res.frame_len = 6 + payload_len; res.frame_len = 6 + payload_len;
res.addr = req.addr; res.addr = req.addr;
res.seq_no = req.seq_no; res.seq_no = req.seq_no;
res.cmd = req.cmd; res.cmd = req.cmd;
res.status = 0; res.status = 0;
res.payload_len = payload_len; res.payload_len = payload_len;
} }
static void sg_nfc_cmd_reset() {//重置读卡器 static void sg_nfc_cmd_reset() {//重置读卡器
nfc.begin(); nfc.begin();
nfc.setPassiveActivationRetries(0x10);//设定等待次数 nfc.setPassiveActivationRetries(0x10);//设定等待次数
nfc.SAMConfig(); nfc.SAMConfig();
if (nfc.getFirmwareVersion()) { if (nfc.getFirmwareVersion()) {
nfc.SAMConfig(); nfc.SAMConfig();
sg_res_init(); sg_res_init();
res.status = 3; res.status = 3;
return; return;
} }
fill_solid(leds, NUM_LEDS, 0xFFFF00); fill_solid(leds, NUM_LEDS, 0xFFFF00);
FastLED.show(); FastLED.show();
} }
static void sg_nfc_cmd_get_fw_version() { static void sg_nfc_cmd_get_fw_version() {
sg_res_init(23); sg_res_init(23);
// memcpy(res.version, "TN32MSEC003S F/W Ver1.2", 23); // memcpy(res.version, "TN32MSEC003S F/W Ver1.2", 23);
memcpy(res.version, "Sucareto Aime Reader FW", 23); memcpy(res.version, "Sucareto Aime Reader FW", 23);
} }
static void sg_nfc_cmd_get_hw_version() { static void sg_nfc_cmd_get_hw_version() {
sg_res_init(23); sg_res_init(23);
// memcpy(res.version, "TN32MSEC003S H/W Ver3.0", 23); // memcpy(res.version, "TN32MSEC003S H/W Ver3.0", 23);
memcpy(res.version, "Sucareto Aime Reader HW", 23); memcpy(res.version, "Sucareto Aime Reader HW", 23);
} }
static void sg_nfc_cmd_mifare_set_key() { static void sg_nfc_cmd_mifare_set_key() {
sg_res_init(); sg_res_init();
if (req.cmd == SG_NFC_CMD_MIFARE_SET_KEY_BANA) { if (req.cmd == SG_NFC_CMD_MIFARE_SET_KEY_BANA) {
memcpy(BanaKey, req.key, 6); memcpy(BanaKey, req.key, 6);
} else if (req.cmd == SG_NFC_CMD_MIFARE_SET_KEY_AIME) { } else if (req.cmd == SG_NFC_CMD_MIFARE_SET_KEY_AIME) {
memcpy(AimeKey, req.key, 6); memcpy(AimeKey, req.key, 6);
} }
} }
static void sg_led_cmd_reset() { static void sg_led_cmd_reset() {
sg_res_init(); sg_res_init();
FastLED.clear(); FastLED.clear();
FastLED.show(); FastLED.show();
} }
static void sg_led_cmd_get_info() { static void sg_led_cmd_get_info() {
sg_res_init(9); sg_res_init(9);
static uint8_t info[9] = {'1', '5', '0', '8', '4', 0xFF, 0x10, 0x00, 0x12}; static uint8_t info[9] = {'1', '5', '0', '8', '4', 0xFF, 0x10, 0x00, 0x12};
memcpy(res.info_payload, info, 9); memcpy(res.info_payload, info, 9);
} }
static void sg_led_cmd_set_color() { static void sg_led_cmd_set_color() {
uint8_t r = req.color_payload[0]; uint8_t r = req.color_payload[0];
uint8_t g = req.color_payload[1]; uint8_t g = req.color_payload[1];
uint8_t b = req.color_payload[2]; uint8_t b = req.color_payload[2];
fill_solid(leds, NUM_LEDS, CRGB(r, g, b)); fill_solid(leds, NUM_LEDS, CRGB(r, g, b));
FastLED.show(); FastLED.show();
} }
static void sg_nfc_cmd_radio_on() { static void sg_nfc_cmd_radio_on() {
cardtype = 0; cardtype = 0;
uL = 0; uL = 0;
memset(&uid, 0, 4); memset(&uid, 0, 4);
memset(&felica, 0, 16); memset(&felica, 0, 16);
sg_res_init(); sg_res_init();
//uid,uidLength,block,keyA(0) or keyB(1),Key //uid,uidLength,block,keyA(0) or keyB(1),Key
if (nfc.readPassiveTargetID(PN532_MIFARE_ISO14443A, uid, &uL) && nfc.mifareclassic_AuthenticateBlock(uid, uL, 1, 1, AimeKey)) { if (nfc.readPassiveTargetID(PN532_MIFARE_ISO14443A, uid, &uL) && nfc.mifareclassic_AuthenticateBlock(uid, uL, 1, 1, AimeKey)) {
cardtype = 0x10; cardtype = 0x10;
return; return;
} }
if (nfc.readPassiveTargetID(PN532_MIFARE_ISO14443A, uid, &uL) && nfc.mifareclassic_AuthenticateBlock(uid, uL, 1, 0, BanaKey)) { if (nfc.readPassiveTargetID(PN532_MIFARE_ISO14443A, uid, &uL) && nfc.mifareclassic_AuthenticateBlock(uid, uL, 1, 0, BanaKey)) {
cardtype = 0x10; cardtype = 0x10;
return; return;
} }
if (nfc.felica_Polling(systemCode, requestCode, felica.IDm, felica.PMm, &felica.SystemCode, 200)) { if (nfc.felica_Polling(systemCode, requestCode, felica.IDm, felica.PMm, &felica.SystemCode, 200)) {
cardtype = 0x20; cardtype = 0x20;
felica.SystemCode = felica.SystemCode >> 8 | felica.SystemCode << 8; felica.SystemCode = felica.SystemCode >> 8 | felica.SystemCode << 8;
return; return;
} }
#ifdef M2F #ifdef M2F
if (nfc.readPassiveTargetID(PN532_MIFARE_ISO14443A, uid, &uL) && nfc.mifareclassic_AuthenticateBlock(uid, uL, M2F_B, 0, MifareKey)) { if (nfc.readPassiveTargetID(PN532_MIFARE_ISO14443A, uid, &uL) && nfc.mifareclassic_AuthenticateBlock(uid, uL, M2F_B, 0, MifareKey)) {
cardtype = 0x30; cardtype = 0x30;
return; return;
} }
#endif #endif
} }
static void sg_nfc_cmd_radio_off() { static void sg_nfc_cmd_radio_off() {
sg_res_init(); sg_res_init();
} }
static void sg_nfc_cmd_poll() { //卡号发送 static void sg_nfc_cmd_poll() { //卡号发送
if (cardtype == 0x10) { if (cardtype == 0x10) {
res.count = 1; res.count = 1;
res.type = 0x10; res.type = 0x10;
res.id_len = uL; res.id_len = uL;
memcpy(res.IDm, uid, uL); memcpy(res.IDm, uid, uL);
sg_res_init(0x07); sg_res_init(0x07);
return; return;
} }
if (cardtype == 0x20) { if (cardtype == 0x20) {
sg_res_init(0x13); sg_res_init(0x13);
memcpy(res.IDm, felica.IDm, 8); memcpy(res.IDm, felica.IDm, 8);
memcpy(res.PMm, felica.PMm, 8); memcpy(res.PMm, felica.PMm, 8);
res.count = 1; res.count = 1;
res.type = 0x20; res.type = 0x20;
res.id_len = 0x10; res.id_len = 0x10;
return; return;
} }
#ifdef M2F #ifdef M2F
if (cardtype == 0x30) { if (cardtype == 0x30) {
sg_res_init(0x13); sg_res_init(0x13);
nfc.mifareclassic_ReadDataBlock(M2F_B, felica.block); nfc.mifareclassic_ReadDataBlock(M2F_B, felica.block);
memcpy(res.IDm, felica.IDm, 8); memcpy(res.IDm, felica.IDm, 8);
memcpy(res.PMm, felica.PMm, 8); uint8_t aic_PMm[8] = {0x00, 0xF1, 0x00, 0x00, 0x00, 0x01, 0x43, 0x00};
felica.block[16] = 0x00; memcpy(felica.PMm, aic_PMm, 8);
felica.block[17] = 0x03;//suica memcpy(res.PMm, felica.PMm, 8);
res.count = 1; felica.block[16] = 0x88;
res.type = 0x20; felica.block[17] = 0xB4;//Amusement IC
res.id_len = 0x10; res.count = 1;
return; res.type = 0x20;
} res.id_len = 0x10;
#endif return;
sg_res_init(1); }
res.count = 0; #endif
} sg_res_init(1);
res.count = 0;
static void sg_nfc_cmd_mifare_select_tag() { }
sg_res_init();
} static void sg_nfc_cmd_mifare_select_tag() {
sg_res_init();
static void sg_nfc_cmd_mifare_authenticate() { }
sg_res_init();
} static void sg_nfc_cmd_mifare_authenticate() {
sg_res_init();
static void sg_nfc_cmd_mifare_read_block() {//读取卡扇区数据 }
sg_res_init();
if (req.block_no > 2) { static void sg_nfc_cmd_mifare_read_block() {//读取卡扇区数据
return; sg_res_init();
} if (req.block_no > 2) {
if (nfc.mifareclassic_ReadDataBlock(req.block_no, res.block)) { return;
sg_res_init(0x10); }
return; if (nfc.mifareclassic_ReadDataBlock(req.block_no, res.block)) {
} sg_res_init(0x10);
} return;
}
static void sg_nfc_cmd_felica_encap() { }
uint8_t code = req.code;
res.code = code + 1; static void sg_nfc_cmd_felica_encap() {
switch (code) { uint8_t code = req.code;
case FELICA_CMD_POLL: res.code = code + 1;
sg_res_init(0x14); switch (code) {
memcpy(res.encap_IDm, felica.IDm, 8); case FELICA_CMD_POLL:
memcpy(res.encap_PMm, felica.PMm, 8); sg_res_init(0x14);
memcpy(res.system_code, &felica.SystemCode, 2); memcpy(res.encap_IDm, felica.IDm, 8);
break; memcpy(res.encap_PMm, felica.PMm, 8);
case FELICA_CMD_GET_SYSTEM_CODE: memcpy(res.system_code, &felica.SystemCode, 2);
sg_res_init(0x0D); break;
memcpy(res.IDm, felica.IDm, 8); case FELICA_CMD_GET_SYSTEM_CODE:
res.felica_payload[0] = 0x01;//未知 sg_res_init(0x0D);
res.felica_payload[1] = felica.block[16];//SystemCode memcpy(res.IDm, felica.IDm, 8);
res.felica_payload[2] = felica.block[17]; res.felica_payload[0] = 0x01;//未知
break; res.felica_payload[1] = felica.block[16];//SystemCode
case FELICA_CMD_NDA_A4: res.felica_payload[2] = felica.block[17];
sg_res_init(0x0B); break;
res.felica_payload[0] = 0x00; case FELICA_CMD_NDA_A4:
break; sg_res_init(0x0B);
case FELICA_CMD_NDA_06: res.felica_payload[0] = 0x00;
sg_res_init(0x1D); break;
memcpy(res.encap_IDm, felica.IDm, 8); case FELICA_CMD_NDA_06:
res.NDA06_code[0] = 0x00; sg_res_init(0x1D);
res.NDA06_code[1] = 0x00; memcpy(res.encap_IDm, felica.IDm, 8);
res.NDA06_code[2] = 0x01;//未知 res.NDA06_code[0] = 0x00;
memcpy(res.NDA06_IDm, felica.IDm, 8); res.NDA06_code[1] = 0x00;
memcpy(res.NDA06_Data, felica.PMm, 8);//填补数据用 res.NDA06_code[2] = 0x01;//未知
break; memcpy(res.NDA06_IDm, felica.IDm, 8);
case FELICA_CMD_NDA_08: memcpy(res.NDA06_Data, felica.PMm, 8);//填补数据用
sg_res_init(0x0C); break;
memcpy(res.encap_IDm, felica.IDm, 8); case FELICA_CMD_NDA_08:
res.felica_payload[0] = 0x00; sg_res_init(0x0C);
res.felica_payload[1] = 0x00; memcpy(res.encap_IDm, felica.IDm, 8);
break; res.felica_payload[0] = 0x00;
default: res.felica_payload[1] = 0x00;
sg_res_init(); break;
res.status = 1; default:
} sg_res_init();
res.encap_len = res.payload_len; res.status = 1;
} }
res.encap_len = res.payload_len;
}