1
0
mirror of https://github.com/whowechina/aic_pico.git synced 2024-11-27 23:10:48 +01:00

Can turn off virtual AIC option on PN532

This commit is contained in:
whowechina 2024-03-10 22:45:20 +08:00
parent 6a64d5fab1
commit 0c698adafa
13 changed files with 169 additions and 98 deletions

Binary file not shown.

View File

@ -12,11 +12,11 @@
#include "hardware/gpio.h" #include "hardware/gpio.h"
#include "hardware/i2c.h" #include "hardware/i2c.h"
#include "config.h"
#include "nfc.h" #include "nfc.h"
#include "aime.h" #include "aime.h"
#define FRAME_TIMEOUT 200000 #define AIME_EXPIRE_TIME 5000000ULL
enum { enum {
CMD_GET_FW_VERSION = 0x30, CMD_GET_FW_VERSION = 0x30,
CMD_GET_HW_VERSION = 0x32, CMD_GET_HW_VERSION = 0x32,
@ -74,12 +74,11 @@ const char *led_info[] = { "15084\xFF\x10\x00\x12", "000-00000\xFF\x11\x40" };
static int baudrate_mode = 0; static int baudrate_mode = 0;
static struct { static struct {
bool enabled; // feature enabled
bool active; // currently active bool active; // currently active
uint8_t idm[8]; uint8_t idm[8];
const uint8_t pmm[8]; const uint8_t pmm[8];
const uint8_t syscode[2]; const uint8_t syscode[2];
} virtual_aic = { true, false, "", "\x00\xf1\x00\x00\x00\x01\x43\x00", "\x88\xb4" }; } virtual_aic = { false, "", "\x00\xf1\x00\x00\x00\x01\x43\x00", "\x88\xb4" };
static void putc_trap(uint8_t byte) static void putc_trap(uint8_t byte)
{ {
@ -92,11 +91,6 @@ void aime_set_baudrate(int mode)
baudrate_mode = (mode == 0) ? 0 : 1; baudrate_mode = (mode == 0) ? 0 : 1;
} }
void aime_set_virtual_aic(bool enable)
{
virtual_aic.enabled = enable;
}
void aime_init(aime_putc_func putc_func) void aime_init(aime_putc_func putc_func)
{ {
aime_putc = putc_func; aime_putc = putc_func;
@ -207,7 +201,7 @@ static void cmd_key_set(uint8_t key[6])
static void cmd_set_polling(bool enabled) static void cmd_set_polling(bool enabled)
{ {
//pn532_set_rf_field(0, enabled ? 1 : 0); nfc_rf_field(enabled);
send_simple_response(STATUS_OK); send_simple_response(STATUS_OK);
} }
@ -219,7 +213,6 @@ typedef struct __attribute__((packed)) {
struct { struct {
uint8_t idm[8]; uint8_t idm[8];
uint8_t pmm[8]; uint8_t pmm[8];
uint8_t syscode[2];
}; };
uint8_t uid[6]; uint8_t uid[6];
}; };
@ -230,11 +223,16 @@ static void handle_mifare_card(const uint8_t *uid, int len)
card_info_t *card = (card_info_t *) response.payload; card_info_t *card = (card_info_t *) response.payload;
build_response(len > 4 ? 10 : 7); build_response(len > 4 ? 10 : 7);
card->count = 1; card->count = 1;
card->type = 0x10; card->type = 0x10;
card->id_len = len; card->id_len = len;
memcpy(card->uid, uid, len); memcpy(card->uid, uid, len);
printf("\nMIFARE Card:");
for (int i = 0; i < len; i++) {
printf(" %02x", uid[i]);
}
} }
static void handle_felica_card(const uint8_t idm[8], const uint8_t pmm[8]) static void handle_felica_card(const uint8_t idm[8], const uint8_t pmm[8])
@ -273,10 +271,11 @@ static void handle_no_card()
static void cmd_detect_card() static void cmd_detect_card()
{ {
nfc_card_t card = nfc_detect_card(); nfc_card_t card = nfc_detect_card();
display_card(&card);
switch (card.card_type) { switch (card.card_type) {
case NFC_CARD_MIFARE: case NFC_CARD_MIFARE:
if (virtual_aic.enabled) { if (aic_cfg->virtual_aic) {
printf("\nVirtual FeliCa from MIFARE."); printf("\nVirtual FeliCa from MIFARE.");
virtual_aic.active = true; virtual_aic.active = true;
memcpy(virtual_aic.idm, "\x01\x01", 2); memcpy(virtual_aic.idm, "\x01\x01", 2);
@ -292,7 +291,7 @@ static void cmd_detect_card()
} }
break; break;
case NFC_CARD_FELICA: case NFC_CARD_FELICA:
if (virtual_aic.enabled) { if (aic_cfg->virtual_aic) {
printf("\nVirtual FeliCa from FeliCa."); printf("\nVirtual FeliCa from FeliCa.");
virtual_aic.active = true; virtual_aic.active = true;
memcpy(virtual_aic.idm, card.uid, 8); memcpy(virtual_aic.idm, card.uid, 8);
@ -302,7 +301,7 @@ static void cmd_detect_card()
} }
break; break;
case NFC_CARD_VICINITY: case NFC_CARD_VICINITY:
if (virtual_aic.enabled) { if (aic_cfg->virtual_aic) {
printf("\nVirtual FeliCa from 15693."); printf("\nVirtual FeliCa from 15693.");
virtual_aic.active = true; virtual_aic.active = true;
memcpy(virtual_aic.idm, card.uid, 8); memcpy(virtual_aic.idm, card.uid, 8);
@ -320,22 +319,19 @@ static void cmd_detect_card()
static void cmd_card_select() static void cmd_card_select()
{ {
printf("CARD SELECT %d\n", request.payload_len);
send_simple_response(STATUS_OK); send_simple_response(STATUS_OK);
} }
static void cmd_mifare_auth(int type) static void cmd_mifare_auth(int type)
{ {
printf("MIFARE AUTH\n"); const uint8_t *key = mifare_keys[type];
nfc_mifare_auth(request.mifare.uid, request.mifare.block_id, type, mifare_keys[type]); nfc_mifare_auth(request.mifare.uid, request.mifare.block_id,
type, key);
send_simple_response(STATUS_OK); send_simple_response(STATUS_OK);
} }
static void cmd_mifare_read() static void cmd_mifare_read()
{ {
printf("MIFARE READ %02x %02x %02x %02x %02x\n", request.mifare.block_id,
request.mifare.uid[0], request.mifare.uid[1], request.mifare.uid[2],
request.mifare.uid[3]);
build_response(16); build_response(16);
memset(response.payload, 0, 16); memset(response.payload, 0, 16);
nfc_mifare_read(request.mifare.block_id, response.payload); nfc_mifare_read(request.mifare.block_id, response.payload);
@ -344,7 +340,6 @@ static void cmd_mifare_read()
static void cmd_mifare_halt() static void cmd_mifare_halt()
{ {
printf("MIFARE HALT\n");
send_simple_response(STATUS_OK); send_simple_response(STATUS_OK);
} }
@ -394,7 +389,7 @@ static int cmd_felica_read()
uint8_t *req_data = request.felica.data; uint8_t *req_data = request.felica.data;
if (req_data[8] != 1) { if (req_data[8] != 1) {
printf("Felica Encap READ Error: service_num != 1\n"); printf("\nFelica Encap READ Error: service_num != 1");
return -1; return -1;
} }
@ -416,10 +411,11 @@ static int cmd_felica_read()
for (int i = 0; i < block_num; i++) { for (int i = 0; i < block_num; i++) {
uint16_t block_id = (block[i * 2 + 1] << 8) | block[i * 2 + 2]; uint16_t block_id = (block[i * 2 + 1] << 8) | block[i * 2 + 2];
if (block_id == 0x8082) { if (virtual_aic.active) {
memcpy(read_resp->block_data[i], felica_resp->idm, 8); if (block_id == 0x8082) {
} memcpy(read_resp->block_data[i], virtual_aic.idm, 8);
if (!virtual_aic.active) { }
} else {
nfc_felica_read_wo_encrypt(svc_code, block_id, read_resp->block_data[i]); nfc_felica_read_wo_encrypt(svc_code, block_id, read_resp->block_data[i]);
} }
} }
@ -436,7 +432,7 @@ static int cmd_felica_write()
uint8_t *req_data = request.felica.data; uint8_t *req_data = request.felica.data;
if (req_data[8] != 1) { if (req_data[8] != 1) {
printf("Felica Encap Write Error: service_num != 1\n"); printf("\nFelica Encap Write Error: service_num != 1");
return -1; return -1;
} }
@ -466,24 +462,16 @@ static int cmd_felica_write()
static void cmd_felica() static void cmd_felica()
{ {
nfc_card_t card = nfc_detect_card(); nfc_card_t card = nfc_poll_felica();
if (!virtual_aic.active) { if ((card.card_type != NFC_CARD_FELICA) && (!virtual_aic.active)) {
if (card.card_type != NFC_CARD_FELICA) { send_simple_response(STATUS_FELICA_ERROR);
send_simple_response(STATUS_FELICA_ERROR); return;
return;
}
} }
uint8_t felica_code = request.felica.code; uint8_t felica_op = request.felica.code;
/*
printf("Felica (%02x):", felica_code);
for (int i = 0; i < request.payload_len; i++) {
printf(" %02x", request.payload[i]);
}
printf("\n");
*/
int datalen = -1; int datalen = -1;
switch (felica_code) { switch (felica_op) {
case CMD_FELICA_OP_GET_SYSTEM_CODE: case CMD_FELICA_OP_GET_SYSTEM_CODE:
datalen = cmd_felica_get_syscode(&card); datalen = cmd_felica_get_syscode(&card);
break; break;
@ -497,7 +485,7 @@ static void cmd_felica()
datalen = cmd_felica_write(); datalen = cmd_felica_write();
break; break;
default: default:
printf("Unknown code %d\n", felica_code); printf("\nUnknown code %d", felica_op);
break; break;
} }
@ -509,7 +497,7 @@ static void cmd_felica()
felica_resp_t *felica_resp = (felica_resp_t *) response.payload; felica_resp_t *felica_resp = (felica_resp_t *) response.payload;
build_response(sizeof(*felica_resp) + datalen); build_response(sizeof(*felica_resp) + datalen);
felica_resp->len = response.payload_len; felica_resp->len = response.payload_len;
felica_resp->code = felica_code + 1; felica_resp->code = felica_op + 1;
if (virtual_aic.active) { if (virtual_aic.active) {
memcpy(felica_resp->idm, virtual_aic.idm, 8); memcpy(felica_resp->idm, virtual_aic.idm, 8);
} else { } else {
@ -601,16 +589,18 @@ static void aime_handle_frame()
break; break;
default: default:
printf("Unknown command: %02x [", request.cmd); printf("\nUnknown command: %02x [", request.cmd);
for (int i = 0; i < request.len; i++) { for (int i = 0; i < request.len; i++) {
printf(" %02x", request.raw[i]); printf(" %02x", request.raw[i]);
} }
printf("]\n"); printf("]");
send_simple_response(STATUS_OK); send_simple_response(STATUS_OK);
break; break;
} }
} }
static uint64_t expire_time;
bool aime_feed(int c) bool aime_feed(int c)
{ {
if (c == 0xe0) { if (c == 0xe0) {
@ -640,6 +630,7 @@ bool aime_feed(int c)
if (req_ctx.check_sum == c) { if (req_ctx.check_sum == c) {
aime_handle_frame(); aime_handle_frame();
req_ctx.active = false; req_ctx.active = false;
expire_time = time_us_64() + AIME_EXPIRE_TIME;
} }
return true; return true;
} }
@ -651,6 +642,11 @@ bool aime_feed(int c)
return true; return true;
} }
uint64_t aime_expire_time()
{
return expire_time;
}
uint32_t aime_led_color() uint32_t aime_led_color()
{ {
return led_color; return led_color;

View File

@ -12,6 +12,9 @@ typedef void (*aime_putc_func)(uint8_t byte);
void aime_init(aime_putc_func putc_func); void aime_init(aime_putc_func putc_func);
bool aime_feed(int c); bool aime_feed(int c);
/* aime activity expires at this time */
uint64_t aime_expire_time();
uint32_t aime_led_color(); uint32_t aime_led_color();
// mode 0 or 1 // mode 0 or 1

View File

@ -8,7 +8,7 @@
#define I2C_PORT i2c0 #define I2C_PORT i2c0
#define I2C_SCL 21 #define I2C_SCL 21
#define I2C_SDA 20 #define I2C_SDA 20
#define I2C_FREQ 733*1000 #define I2C_FREQ 433*1000
#define RGB_PIN 12 #define RGB_PIN 12
#define RGB_ORDER GRB // or RGB #define RGB_ORDER GRB // or RGB

View File

@ -39,6 +39,7 @@ static void handle_display()
aic_cfg->light.rgb ? "ON" : "OFF", aic_cfg->light.rgb ? "ON" : "OFF",
aic_cfg->light.led ? "ON" : "OFF"); aic_cfg->light.led ? "ON" : "OFF");
printf(" Level: [%d ~ %d]\n", aic_cfg->light.min, aic_cfg->light.max); printf(" Level: [%d ~ %d]\n", aic_cfg->light.min, aic_cfg->light.max);
printf(" Virtual AIC: %s\n", aic_cfg->virtual_aic ? "ON" : "OFF");
} }
static void handle_save() static void handle_save()
@ -63,6 +64,27 @@ static void handle_nfc()
printf("\n"); printf("\n");
} }
static void handle_virtual(int argc, char *argv[])
{
const char *usage = "Usage: virtual <on|off>\n";
if (argc != 1) {
printf("%s", usage);
return;
}
const char *commands[] = { "on", "off" };
int match = cli_match_prefix(commands, 2, argv[0]);
if (match < 0) {
printf("%s", usage);
return;
}
aic_cfg->virtual_aic = (match == 0);
config_changed();
}
static void handle_light(int argc, char *argv[]) static void handle_light(int argc, char *argv[])
{ {
const char *usage = "Usage: light <rgb|led|both|off>\n"; const char *usage = "Usage: light <rgb|led|both|off>\n";
@ -133,6 +155,7 @@ void commands_init()
cli_register("save", handle_save, "Save config to flash."); cli_register("save", handle_save, "Save config to flash.");
cli_register("factory", handle_factory_reset, "Reset everything to default."); cli_register("factory", handle_factory_reset, "Reset everything to default.");
cli_register("nfc", handle_nfc, "NFC module."); cli_register("nfc", handle_nfc, "NFC module.");
cli_register("virtual", handle_virtual, "Virtual AIC card.");
cli_register("light", handle_light, "Turn on/off lights."); cli_register("light", handle_light, "Turn on/off lights.");
cli_register("level", handle_level, "Set light level."); cli_register("level", handle_level, "Set light level.");
} }

View File

@ -12,7 +12,8 @@
aic_cfg_t *aic_cfg; aic_cfg_t *aic_cfg;
static aic_cfg_t default_cfg = { static aic_cfg_t default_cfg = {
.light = { .min = 0, .max = 128, .rgb = true, .led = true } .light = { .min = 0, .max = 128, .rgb = true, .led = true },
.virtual_aic = true,
}; };
aic_runtime_t *aic_runtime; aic_runtime_t *aic_runtime;

View File

@ -16,6 +16,7 @@ typedef struct __attribute__((packed)) {
bool rgb; bool rgb;
bool led; bool led;
} light; } light;
bool virtual_aic;
uint32_t reserved; uint32_t reserved;
} aic_cfg_t; } aic_cfg_t;

View File

@ -158,7 +158,7 @@ static void rainbow_fade()
last = now; last = now;
if (curr_speed > RAINBOW_MIN_SPEED) { if (curr_speed > RAINBOW_MIN_SPEED) {
curr_speed = curr_speed * 95 / 100; curr_speed = curr_speed * 90 / 100;
} }
if (curr_level > aic_cfg->light.min) { if (curr_level > aic_cfg->light.min) {
curr_level -= (curr_level - aic_cfg->light.min) / 10 + 1; curr_level -= (curr_level - aic_cfg->light.min) / 10 + 1;

View File

@ -107,46 +107,60 @@ static void core1_loop()
} }
} }
void detect_card() static void update_cardio(nfc_card_t *card)
{ {
static nfc_card_t old_card = { 0 }; switch (card->card_type) {
nfc_card_t card = nfc_detect_card();
switch (card.card_type) {
case NFC_CARD_MIFARE: case NFC_CARD_MIFARE:
hid_cardio.current[0] = REPORT_ID_EAMU; hid_cardio.current[0] = REPORT_ID_EAMU;
hid_cardio.current[1] = 0xe0; hid_cardio.current[1] = 0xe0;
hid_cardio.current[2] = 0x04; hid_cardio.current[2] = 0x04;
if (card.len == 4) { if (card->len == 4) {
memcpy(hid_cardio.current + 3, card.uid, 4); memcpy(hid_cardio.current + 3, card->uid, 4);
memcpy(hid_cardio.current + 7, card.uid, 2); memcpy(hid_cardio.current + 7, card->uid, 2);
} else if (card.len == 7) { } else if (card->len == 7) {
memcpy(hid_cardio.current + 3, card.uid + 1, 6); memcpy(hid_cardio.current + 3, card->uid + 1, 6);
} }
break; break;
case NFC_CARD_FELICA: case NFC_CARD_FELICA:
hid_cardio.current[0] = REPORT_ID_FELICA; hid_cardio.current[0] = REPORT_ID_FELICA;
memcpy(hid_cardio.current + 1, card.uid, 8); memcpy(hid_cardio.current + 1, card->uid, 8);
break; break;
case NFC_CARD_VICINITY: case NFC_CARD_VICINITY:
hid_cardio.current[0] = REPORT_ID_EAMU; hid_cardio.current[0] = REPORT_ID_EAMU;
memcpy(hid_cardio.current + 1, card.uid, 8); memcpy(hid_cardio.current + 1, card->uid, 8);
break; break;
default: default:
memset(hid_cardio.current, 0, 9); memset(hid_cardio.current, 0, 9);
return;
} }
if (memcmp(&old_card, &card, sizeof(card)) == 0) {
printf(" -> CardIO ");
for (int i = 1; i < 9; i++) {
printf("%02X", hid_cardio.current[i]);
}
}
void detect_card()
{
if (time_us_64() < aime_expire_time()) {
return; return;
} }
if (card.card_type != NFC_CARD_NONE) { nfc_rf_field(true);
printf("\n%s:", nfc_card_name(card.card_type));
for (int i = 0; i < card.len; i++) { static nfc_card_t old_card = { 0 };
printf(" %02x", hid_cardio.current[i + 1]); nfc_card_t card = nfc_detect_card();
}
nfc_rf_field(false);
if (memcmp(&old_card, &card, sizeof(old_card)) == 0) {
return;
} }
old_card = card; old_card = card;
display_card(&card);
update_cardio(&card);
} }
const int aime_intf = 1; const int aime_intf = 1;

View File

@ -62,14 +62,19 @@ static bool null_poll_vicinity(uint8_t uid[8])
return false; return false;
} }
static void null_rf_field(bool on)
{
}
struct { struct {
bool (*poll_mifare)(uint8_t uid[7], int *len); bool (*poll_mifare)(uint8_t uid[7], int *len);
bool (*poll_felica)(uint8_t uid[8], uint8_t pmm[8], uint8_t syscode[2], bool from_cache); bool (*poll_felica)(uint8_t uid[8], uint8_t pmm[8], uint8_t syscode[2], bool from_cache);
bool (*poll_vicinity)(uint8_t uid[8]); bool (*poll_vicinity)(uint8_t uid[8]);
void (*rf_field)(bool on);
} api[3] = { } api[3] = {
{ pn532_poll_mifare, pn532_poll_felica, null_poll_vicinity,}, { pn532_poll_mifare, pn532_poll_felica, null_poll_vicinity, pn532_rf_field},
{ pn5180_poll_mifare, pn5180_poll_felica, pn5180_poll_vicinity,}, { pn5180_poll_mifare, pn5180_poll_felica, pn5180_poll_vicinity, null_rf_field},
{ null_poll_mifare, null_poll_felica, null_poll_vicinity,}, { null_poll_mifare, null_poll_felica, null_poll_vicinity, null_rf_field},
}; };
void nfc_init(nfc_wait_loop_t loop) void nfc_init(nfc_wait_loop_t loop)
@ -83,13 +88,6 @@ void nfc_init(nfc_wait_loop_t loop)
} }
} }
static void nfc_config_sam()
{
if (nfc_module == NFC_MODULE_PN532) {
pn532_config_sam();
}
}
static bool nfc_detect_mifare(nfc_card_t *card) static bool nfc_detect_mifare(nfc_card_t *card)
{ {
uint8_t id[20] = { 0 }; uint8_t id[20] = { 0 };
@ -138,12 +136,15 @@ static bool nfc_detect_vicinity(nfc_card_t *card)
return true; return true;
} }
void nfc_rf_field(bool on)
{
api[nfc_module].rf_field(on);
}
nfc_card_t nfc_detect_card() nfc_card_t nfc_detect_card()
{ {
nfc_card_t card = { 0 }; nfc_card_t card = { 0 };
nfc_config_sam();
if (!nfc_detect_mifare(&card) && if (!nfc_detect_mifare(&card) &&
!nfc_detect_felica(&card) && !nfc_detect_felica(&card) &&
!nfc_detect_vicinity(&card)) { !nfc_detect_vicinity(&card)) {
@ -153,6 +154,25 @@ nfc_card_t nfc_detect_card()
return card; return card;
} }
nfc_card_t nfc_poll_felica()
{
nfc_card_t card = { 0 };
if (!nfc_detect_felica(&card)) {
card.card_type = NFC_CARD_NONE;
}
return card;
}
void display_card(const nfc_card_t *card)
{
if (card->card_type != NFC_CARD_NONE) {
printf("\n%s:", nfc_card_name(card->card_type));
for (int i = 0; i < card->len; i++) {
printf(" %02X", card->uid[i]);
}
}
}
bool nfc_mifare_auth(const uint8_t uid[4], uint8_t block_id, uint8_t key_id, const uint8_t *key) bool nfc_mifare_auth(const uint8_t uid[4], uint8_t block_id, uint8_t key_id, const uint8_t *key)
{ {
if (nfc_module == NFC_MODULE_PN532) { if (nfc_module == NFC_MODULE_PN532) {

View File

@ -31,7 +31,12 @@ typedef struct {
} nfc_card_t; } nfc_card_t;
void nfc_init(nfc_wait_loop_t loop); void nfc_init(nfc_wait_loop_t loop);
void nfc_rf_field(bool on);
nfc_card_t nfc_detect_card(); nfc_card_t nfc_detect_card();
void display_card(const nfc_card_t *card);
nfc_card_t nfc_poll_felica();
const char *nfc_module_name(); const char *nfc_module_name();

View File

@ -294,8 +294,7 @@ bool pn532_config_sam()
return pn532_read_response(0x14, &resp, 1) == 0; return pn532_read_response(0x14, &resp, 1) == 0;
} }
static bool pn532_set_rf_field(uint8_t auto_rf, uint8_t on_off)
bool pn532_set_rf_field(uint8_t auto_rf, uint8_t on_off)
{ {
uint8_t param[] = { 1, auto_rf | on_off }; uint8_t param[] = { 1, auto_rf | on_off };
pn532_write_command(0x32, param, 2); pn532_write_command(0x32, param, 2);
@ -304,6 +303,14 @@ bool pn532_set_rf_field(uint8_t auto_rf, uint8_t on_off)
return pn532_read_response(0x32, &resp, 1) >= 0; return pn532_read_response(0x32, &resp, 1) >= 0;
} }
void pn532_rf_field(bool on)
{
pn532_set_rf_field(0x00, on ? 0x01 : 0x00);
if (on) {
pn532_config_sam();
}
}
static uint8_t readbuf[255]; static uint8_t readbuf[255];
bool pn532_poll_mifare(uint8_t uid[7], int *len) bool pn532_poll_mifare(uint8_t uid[7], int *len)
@ -371,20 +378,21 @@ bool pn532_poll_felica(uint8_t uid[8], uint8_t pmm[8], uint8_t syscode[2], bool
bool pn532_mifare_auth(const uint8_t uid[4], uint8_t block_id, uint8_t key_id, const uint8_t *key) bool pn532_mifare_auth(const uint8_t uid[4], uint8_t block_id, uint8_t key_id, const uint8_t *key)
{ {
uint8_t param[] = { 1, key_id ? 1 : 0, block_id, uint8_t param[] = {
key[0], key[1], key[2], key[3], key[4], key[5], 1, key_id ? 0x61 : 0x60, block_id,
uid[0], uid[1], uid[2], uid[3] }; key[0], key[1], key[2], key[3], key[4], key[5],
uid[0], uid[1], uid[2], uid[3]
};
int ret = pn532_write_command(0x40, param, sizeof(param)); int ret = pn532_write_command(0x40, param, sizeof(param));
if (ret < 0) { if (ret < 0) {
printf("Failed mifare auth command\n"); printf("\nPN532 failed mifare auth command");
return false; return false;
} }
int result = pn532_read_response(0x40, readbuf, sizeof(readbuf)); int result = pn532_read_response(0x40, readbuf, sizeof(readbuf));
if (readbuf[0] != 0) { if (readbuf[0] != 0) {
printf("PN532 Mifare AUTH failed %d %02x\n", result, readbuf[0]); printf("\nPN532 Mifare AUTH failed %d %02x", result, readbuf[0]);
return false; return false;
} }
return true; return true;
} }
@ -394,14 +402,14 @@ bool pn532_mifare_read(uint8_t block_id, uint8_t block_data[16])
int ret = pn532_write_command(0x40, param, sizeof(param)); int ret = pn532_write_command(0x40, param, sizeof(param));
if (ret < 0) { if (ret < 0) {
printf("Failed mifare read command\n"); printf("\nPN532 failed mifare read command");
return false; return false;
} }
int result = pn532_read_response(0x40, readbuf, sizeof(readbuf)); int result = pn532_read_response(0x40, readbuf, sizeof(readbuf));
if (readbuf[0] != 0 || result != 17) { if (readbuf[0] != 0 || result != 17) {
printf("PN532 Mifare READ failed %d %02x\n", result, readbuf[0]); printf("\nPN532 Mifare READ failed %d %02x", result, readbuf[0]);
return false; return false;
} }
@ -423,7 +431,7 @@ int pn532_felica_command(uint8_t cmd, const uint8_t *param, uint8_t param_len, u
int ret = pn532_write_command(0x40, cmd_buf, sizeof(cmd_buf)); int ret = pn532_write_command(0x40, cmd_buf, sizeof(cmd_buf));
if (ret < 0) { if (ret < 0) {
printf("Failed send felica command\n"); printf("\nFailed send felica command");
return -1; return -1;
} }
@ -448,13 +456,13 @@ bool pn532_felica_read_wo_encrypt(uint16_t svc_code, uint16_t block_id, uint8_t
int result = pn532_felica_command(0x06, param, sizeof(param), readbuf); int result = pn532_felica_command(0x06, param, sizeof(param), readbuf);
if (result != 12 + 16 || readbuf[9] != 0 || readbuf[10] != 0) { if (result != 12 + 16 || readbuf[9] != 0 || readbuf[10] != 0) {
printf("PN532 Felica READ read failed %d %02x %02x\n", //printf("\nPN532 Felica READ read failed %d %02x %02x",
result, readbuf[9], readbuf[10]); // result, readbuf[9], readbuf[10]);
for (int i = 0; i < result; i++) { for (int i = 0; i < result; i++) {
printf(" %02x", readbuf[i]); printf(" %02x", readbuf[i]);
} }
printf("\n"); memset(block_data, 0, 16);
return false; return true; // we fake the result when it fails
} }
const uint8_t *result_data = readbuf + 12; const uint8_t *result_data = readbuf + 12;
@ -471,13 +479,13 @@ bool pn532_felica_write_wo_encrypt(uint16_t svc_code, uint16_t block_id, const u
int result = pn532_felica_command(0x08, param, sizeof(param), readbuf); int result = pn532_felica_command(0x08, param, sizeof(param), readbuf);
if (result < 0) { if (result < 0) {
printf("PN532 Felica WRITE failed %d\n", result); printf("\nPN532 Felica WRITE failed %d", result);
return false; return false;
} }
printf("\nPN532 Felica WRITE success ");
for (int i = 0; i < result; i++) { for (int i = 0; i < result; i++) {
printf(" %02x", readbuf[i]); printf(" %02x", readbuf[i]);
} }
printf("\n");
return false; return false;
} }

View File

@ -23,7 +23,7 @@ uint32_t pn532_firmware_ver();
bool pn532_config_sam(); bool pn532_config_sam();
bool pn532_config_rf(); bool pn532_config_rf();
bool pn532_set_rf_field(uint8_t auto_rf, uint8_t on_off); void pn532_rf_field(bool on);
bool pn532_poll_mifare(uint8_t uid[7], int *len); bool pn532_poll_mifare(uint8_t uid[7], int *len);
bool pn532_poll_felica(uint8_t uid[8], uint8_t pmm[8], uint8_t syscode[2], bool from_cache); bool pn532_poll_felica(uint8_t uid[8], uint8_t pmm[8], uint8_t syscode[2], bool from_cache);