1
0
mirror of https://github.com/whowechina/aic_pico.git synced 2024-11-24 05:50:11 +01:00

Better mifare auth caching

This commit is contained in:
whowechina 2024-06-19 12:01:26 +08:00
parent c85e42094f
commit d37974fe22

View File

@ -468,28 +468,34 @@ bool pn5180_mifare_auth(const uint8_t uid[4], uint8_t block_id, uint8_t key_id,
}
uint8_t ignored[16];
if (!pn5180_mifare_read(block_id & 0xfc, ignored)) {
if (!pn5180_mifare_read((block_id & 0xfc) + 1, ignored)) {
DEBUG("\nPN5180 Mifare auth check bad");
return false;
}
cache.result = true;
return true;
}
bool pn5180_mifare_read(uint8_t block_id, uint8_t block_data[16])
{
static struct {
uint8_t block_id;
uint32_t time;
uint8_t data[16];
} cache = { 0 };
} cache[3] = { 0 };
uint32_t now = time_us_32();
if ((now < cache.time + 1000000) && (block_id == 0)) {
memcpy(block_data, cache.data, 16);
cache.time = now;
if (block_id < 3) {
if ((cache[block_id].block_id == block_id) &&
(now < cache[block_id].time + 1000000)) {
memcpy(block_data, cache[block_id].data, 16);
cache[block_id].time = now;
return true;
}
}
uint8_t cmd[] = { CMD_MIFARE_READ, block_id };
pn5180_send_data(cmd, sizeof(cmd), 0);
@ -504,9 +510,10 @@ bool pn5180_mifare_read(uint8_t block_id, uint8_t block_data[16])
pn5180_read_data(block_data, 16);
if (block_id == 0) {
memcpy(cache.data, block_data, 16);
cache.time = time_us_32();
if (block_id < 3) {
memcpy(cache[block_id].data, block_data, 16);
cache[block_id].block_id = block_id;
cache[block_id].time = time_us_32();
}
return true;