1
0
mirror of https://github.com/whowechina/aic_pico.git synced 2025-02-26 06:39:46 +01:00

Optimization on nfc part

This commit is contained in:
whowechina 2024-01-21 12:59:47 +08:00
parent 042738861e
commit 7be6dbd0c0
7 changed files with 49 additions and 41 deletions

Binary file not shown.

View File

@ -305,8 +305,8 @@ static void cmd_detect_card()
if (virtual_aic.enabled) { if (virtual_aic.enabled) {
printf("\nVirtual FeliCa from 15693."); printf("\nVirtual FeliCa from 15693.");
virtual_aic.active = true; virtual_aic.active = true;
memcpy(virtual_aic.idm, "\x01\x01", 2); memcpy(virtual_aic.idm, card.uid, 8);
memcpy(virtual_aic.idm + 2, card.uid, 6); virtual_aic.idm[0] = 0x01;
fake_felica_card(); fake_felica_card();
} }
break; break;

View File

@ -11,8 +11,7 @@
#include "save.h" #include "save.h"
#include "cli.h" #include "cli.h"
#include "pn532.h" #include "nfc.h"
#include "pn5180.h"
static int fps[2]; static int fps[2];
void fps_count(int core) void fps_count(int core)
@ -33,6 +32,8 @@ void fps_count(int core)
static void handle_display() static void handle_display()
{ {
printf("[NFC Module]\n");
printf(" %s\n", nfc_module_name());
printf("[Config]\n"); printf("[Config]\n");
printf(" Light: RGB-%s LED-%s\n", printf(" Light: RGB-%s LED-%s\n",
aic_cfg->light.rgb ? "ON" : "OFF", aic_cfg->light.rgb ? "ON" : "OFF",
@ -53,32 +54,11 @@ static void handle_factory_reset()
static void handle_nfc() static void handle_nfc()
{ {
bool ret; printf("NFC module: %s\n", nfc_module_name());
nfc_card_t card = nfc_detect_card();
// ret = pn532_config_rf(); printf("Card %s:", nfc_card_name(card.card_type));
// printf("RF: %d\n", ret); for (int i = 0; i < card.len; i++) {
printf(" %02x", card.uid[i]);
ret = pn532_config_sam();
printf("Sam: %d\n", ret);
uint8_t buf[32];
int len = sizeof(buf);
ret = pn532_poll_mifare(buf, &len);
printf("Mifare: %d -", len);
if (ret) {
for (int i = 0; i < len; i++) {
printf(" %02x", buf[i]);
}
}
printf("\n");
printf("Felica: ");
if (pn532_poll_felica(buf, buf + 8, buf + 16, false)) {
for (int i = 0; i < 18; i++) {
printf(" %02x%s", buf[i], (i % 8 == 7) ? "," : "");
}
} }
printf("\n"); printf("\n");
} }
@ -152,7 +132,7 @@ void commands_init()
cli_register("display", handle_display, "Display all settings."); cli_register("display", handle_display, "Display all settings.");
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 debug."); cli_register("nfc", handle_nfc, "NFC module.");
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

@ -130,10 +130,7 @@ void detect_card()
break; break;
case NFC_CARD_VICINITY: case NFC_CARD_VICINITY:
hid_cardio.current[0] = REPORT_ID_EAMU; hid_cardio.current[0] = REPORT_ID_EAMU;
// 15693 cards store uid in reverse byte order memcpy(hid_cardio.current + 1, card.uid, 8);
for (int i = 0; i < 8; i++) {
hid_cardio.current[i + 1] = card.uid[7 - i];
}
break; break;
default: default:
memset(hid_cardio.current, 0, 9); memset(hid_cardio.current, 0, 9);
@ -142,9 +139,8 @@ void detect_card()
return; return;
} }
if (card.card_type != NFC_CARD_NULL) { if (card.card_type != NFC_CARD_NONE) {
const char *card_type_str[3] = { "MIFARE", "FeliCa", "15693" }; printf("\n%s:", nfc_card_name(card.card_type));
printf("\n%s:", card_type_str[card.card_type - 1]);
for (int i = 0; i < card.len; i++) { for (int i = 0; i < card.len; i++) {
printf(" %02x", hid_cardio.current[i + 1]); printf(" %02x", hid_cardio.current[i + 1]);
} }

View File

@ -21,6 +21,32 @@ static enum {
NFC_MODULE_UNKNOWN, NFC_MODULE_UNKNOWN,
} nfc_module = NFC_MODULE_UNKNOWN; } nfc_module = NFC_MODULE_UNKNOWN;
static const char *nfc_module_names[] = {
"PN532",
"PN5180",
"Unknown",
};
const char *nfc_module_name()
{
return nfc_module_names[nfc_module];
}
static const char *nfc_card_names[] = {
"None",
"MIFARE",
"FeliCa",
"15693"
};
const char *nfc_card_name(nfc_card_type card_type)
{
if (card_type >= sizeof(nfc_card_names) / sizeof(nfc_card_names[0])) {
return "Unknown";
}
return nfc_card_names[card_type];
}
static bool null_poll_mifare(uint8_t uid[7], int *len) static bool null_poll_mifare(uint8_t uid[7], int *len)
{ {
return false; return false;
@ -121,7 +147,7 @@ nfc_card_t nfc_detect_card()
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)) {
card.card_type = NFC_CARD_NULL; card.card_type = NFC_CARD_NONE;
} }
return card; return card;

View File

@ -10,12 +10,14 @@
#include "hardware/i2c.h" #include "hardware/i2c.h"
typedef enum { typedef enum {
NFC_CARD_NULL = 0, NFC_CARD_NONE = 0,
NFC_CARD_MIFARE, NFC_CARD_MIFARE,
NFC_CARD_FELICA, NFC_CARD_FELICA,
NFC_CARD_VICINITY, NFC_CARD_VICINITY,
} nfc_card_type; } nfc_card_type;
const char *nfc_card_name(nfc_card_type card_type);
typedef void (*nfc_wait_loop_t)(); typedef void (*nfc_wait_loop_t)();
typedef struct { typedef struct {
nfc_card_type card_type; nfc_card_type card_type;
@ -31,6 +33,8 @@ typedef struct {
void nfc_init(nfc_wait_loop_t loop); void nfc_init(nfc_wait_loop_t loop);
nfc_card_t nfc_detect_card(); nfc_card_t nfc_detect_card();
const char *nfc_module_name();
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);
bool nfc_mifare_read(uint8_t block_id, uint8_t block_data[16]); bool nfc_mifare_read(uint8_t block_id, uint8_t block_data[16]);

View File

@ -361,7 +361,9 @@ bool pn5180_poll_vicinity(uint8_t uid[8])
if (len == 10) { if (len == 10) {
uint8_t id[10]; uint8_t id[10];
pn5180_read_data(id, len); pn5180_read_data(id, len);
memmove(uid, id + 2, 8); for (int i = 0; i < 8; i++) {
uid[i] = id[9 - i]; // 15693 stores id in reversed byte order
}
result = true; result = true;
} }