Fix PN532 transport and NFC tag handling.
Tighten PN532 framing and NFC access locking so scan, read, write, and raw operations behave reliably on hardware, and correct Classic versus Type 2 tag classification so common cards hit the right code paths. Refresh the embedded web assets and launcher files to match the corrected firmware behavior. Made-with: Cursor
This commit is contained in:
@@ -9,6 +9,7 @@
|
||||
static const char *TAG = "nfc_deep";
|
||||
|
||||
#define MAX_UL_PAGES 240
|
||||
#define DEFAULT_KEY_COUNT (sizeof(k_default_keys) / sizeof(k_default_keys[0]))
|
||||
|
||||
static const uint8_t k_default_keys[][6] = {
|
||||
{0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF},
|
||||
@@ -37,7 +38,7 @@ static void key_to_hex(const uint8_t k[6], char *out13)
|
||||
static bool mfc_sector_layout(const nfc_tag_info_t *tag, int sector, int *first_block, int *num_blocks,
|
||||
uint8_t *trailer_block)
|
||||
{
|
||||
bool is4k = (tag->sak == 0x19);
|
||||
bool is4k = nfc_tag_is_mifare_classic_4k(tag);
|
||||
if (!is4k) {
|
||||
if (sector < 0 || sector > 15) {
|
||||
return false;
|
||||
@@ -65,6 +66,9 @@ static bool mfc_sector_layout(const nfc_tag_info_t *tag, int sector, int *first_
|
||||
|
||||
static bool try_sector(nfc_tag_info_t *tag, int sector, cJSON *sec_out)
|
||||
{
|
||||
if (!tag || !sec_out) {
|
||||
return false;
|
||||
}
|
||||
int fb = 0;
|
||||
int nb = 0;
|
||||
uint8_t trailer = 0;
|
||||
@@ -76,16 +80,19 @@ static bool try_sector(nfc_tag_info_t *tag, int sector, cJSON *sec_out)
|
||||
cJSON_AddNumberToObject(sec_out, "trailerBlock", trailer);
|
||||
cJSON_AddNumberToObject(sec_out, "blockCount", nb);
|
||||
|
||||
for (size_t ki = 0; ki < sizeof(k_default_keys) / 6; ki++) {
|
||||
for (size_t ki = 0; ki < DEFAULT_KEY_COUNT; ki++) {
|
||||
nfc_mifare_key_t key;
|
||||
memcpy(key.key, k_default_keys[ki], 6);
|
||||
key.key_b = false;
|
||||
if (nfc_mifare_authenticate_block(tag, trailer, &key) == ESP_OK) {
|
||||
cJSON *blocks = cJSON_CreateArray();
|
||||
if (!blocks) {
|
||||
return false;
|
||||
}
|
||||
cJSON_AddStringToObject(sec_out, "keyType", "A");
|
||||
char kh[16];
|
||||
key_to_hex(key.key, kh);
|
||||
cJSON_AddStringToObject(sec_out, "keyHex", kh);
|
||||
cJSON *blocks = cJSON_CreateArray();
|
||||
for (int b = 0; b < nb; b++) {
|
||||
uint8_t bn = (uint8_t)(fb + b);
|
||||
uint8_t blk[NFC_BLOCK_LEN];
|
||||
@@ -102,11 +109,14 @@ static bool try_sector(nfc_tag_info_t *tag, int sector, cJSON *sec_out)
|
||||
}
|
||||
key.key_b = true;
|
||||
if (nfc_mifare_authenticate_block(tag, trailer, &key) == ESP_OK) {
|
||||
cJSON *blocks = cJSON_CreateArray();
|
||||
if (!blocks) {
|
||||
return false;
|
||||
}
|
||||
cJSON_AddStringToObject(sec_out, "keyType", "B");
|
||||
char kh[16];
|
||||
key_to_hex(key.key, kh);
|
||||
cJSON_AddStringToObject(sec_out, "keyHex", kh);
|
||||
cJSON *blocks = cJSON_CreateArray();
|
||||
for (int b = 0; b < nb; b++) {
|
||||
uint8_t bn = (uint8_t)(fb + b);
|
||||
uint8_t blk[NFC_BLOCK_LEN];
|
||||
@@ -128,10 +138,19 @@ static bool try_sector(nfc_tag_info_t *tag, int sector, cJSON *sec_out)
|
||||
|
||||
static void add_mifare_classic(nfc_tag_info_t *tag, cJSON *root)
|
||||
{
|
||||
int sectors = (tag->sak == 0x19) ? 40 : 16;
|
||||
if (!tag || !root) {
|
||||
return;
|
||||
}
|
||||
int sectors = nfc_tag_is_mifare_classic_4k(tag) ? 40 : 16;
|
||||
cJSON *arr = cJSON_CreateArray();
|
||||
if (!arr) {
|
||||
return;
|
||||
}
|
||||
for (int s = 0; s < sectors; s++) {
|
||||
cJSON *sec = cJSON_CreateObject();
|
||||
if (!sec) {
|
||||
break;
|
||||
}
|
||||
(void)try_sector(tag, s, sec);
|
||||
cJSON_AddItemToArray(arr, sec);
|
||||
}
|
||||
@@ -140,8 +159,14 @@ static void add_mifare_classic(nfc_tag_info_t *tag, cJSON *root)
|
||||
|
||||
static void add_ultralight(nfc_tag_info_t *tag, cJSON *root)
|
||||
{
|
||||
if (!root) {
|
||||
return;
|
||||
}
|
||||
(void)tag;
|
||||
cJSON *pages = cJSON_CreateArray();
|
||||
if (!pages) {
|
||||
return;
|
||||
}
|
||||
uint8_t buf[4];
|
||||
for (int p = 0; p < MAX_UL_PAGES; p++) {
|
||||
if (nfc_ultralight_read_page((uint8_t)p, buf) != ESP_OK) {
|
||||
@@ -156,26 +181,24 @@ static void add_ultralight(nfc_tag_info_t *tag, cJSON *root)
|
||||
|
||||
cJSON *nfc_tag_deep_profile(nfc_tag_info_t *tag)
|
||||
{
|
||||
if (!tag) {
|
||||
return NULL;
|
||||
}
|
||||
cJSON *o = cJSON_CreateObject();
|
||||
if (!o) {
|
||||
return NULL;
|
||||
}
|
||||
cJSON_AddNumberToObject(o, "capturedMs", (double)(esp_timer_get_time() / 1000));
|
||||
cJSON_AddItemToObject(o, "tag", nfc_tag_to_json(tag));
|
||||
|
||||
uint8_t gs[32];
|
||||
size_t gl = 0;
|
||||
if (pn532_get_general_status(gs, sizeof(gs), &gl) == ESP_OK && gl > 0) {
|
||||
cJSON *g = cJSON_CreateArray();
|
||||
for (size_t i = 0; i < gl; i++) {
|
||||
cJSON_AddItemToArray(g, cJSON_CreateNumber(gs[i]));
|
||||
}
|
||||
cJSON_AddItemToObject(o, "pn532GeneralStatus", g);
|
||||
cJSON *tag_json = nfc_tag_to_json(tag);
|
||||
if (!tag_json) {
|
||||
cJSON_Delete(o);
|
||||
return NULL;
|
||||
}
|
||||
cJSON_AddItemToObject(o, "tag", tag_json);
|
||||
|
||||
if (tag->type_hint == 1) {
|
||||
if (nfc_tag_is_mifare_classic(tag)) {
|
||||
add_mifare_classic(tag, o);
|
||||
} else if (tag->type_hint == 2) {
|
||||
} else if (nfc_tag_is_type2(tag)) {
|
||||
add_ultralight(tag, o);
|
||||
} else {
|
||||
cJSON_AddStringToObject(
|
||||
|
||||
Reference in New Issue
Block a user