2019-02-07 23:06:54 +00:00
|
|
|
/*
|
2019-10-27 10:13:24 +00:00
|
|
|
xsns_40_pn532.ino - Support for PN532 (HSU) NFC Tag Reader on Tasmota
|
2019-02-07 23:06:54 +00:00
|
|
|
|
2021-01-01 12:44:04 +00:00
|
|
|
Copyright (C) 2021 Andre Thomas and Theo Arends
|
2019-02-07 23:06:54 +00:00
|
|
|
|
|
|
|
This program is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
2022-10-25 18:16:23 +01:00
|
|
|
#define USE_PN532_HSU // Add support for PN532 using HSU (Serial) interface (+1k8 code, 140 bytes mem)
|
|
|
|
#define USE_PN532_DATA_FUNCTION // Add sensor40 command support for erase, setting data block content (+1k7 code, 388 bytes mem)
|
2019-02-07 23:06:54 +00:00
|
|
|
|
|
|
|
#ifdef USE_PN532_HSU
|
2020-12-01 13:37:05 +00:00
|
|
|
/*********************************************************************************************\
|
|
|
|
* PN532 - 13.56 MHz RFID and NFC reader
|
|
|
|
\*********************************************************************************************/
|
2019-02-07 23:06:54 +00:00
|
|
|
|
|
|
|
#define XSNS_40 40
|
|
|
|
|
|
|
|
#include <TasmotaSerial.h>
|
|
|
|
|
|
|
|
TasmotaSerial *PN532_Serial;
|
|
|
|
|
|
|
|
#define PN532_INVALID_ACK -1
|
|
|
|
#define PN532_TIMEOUT -2
|
|
|
|
#define PN532_INVALID_FRAME -3
|
|
|
|
#define PN532_NO_SPACE -4
|
|
|
|
|
|
|
|
#define PN532_PREAMBLE 0x00
|
|
|
|
#define PN532_STARTCODE1 0x00
|
|
|
|
#define PN532_STARTCODE2 0xFF
|
|
|
|
#define PN532_POSTAMBLE 0x00
|
|
|
|
|
|
|
|
#define PN532_HOSTTOPN532 0xD4
|
|
|
|
#define PN532_PN532TOHOST 0xD5
|
|
|
|
|
|
|
|
#define PN532_ACK_WAIT_TIME 0x0A
|
|
|
|
|
|
|
|
#define PN532_COMMAND_GETFIRMWAREVERSION 0x02
|
|
|
|
#define PN532_COMMAND_SAMCONFIGURATION 0x14
|
|
|
|
#define PN532_COMMAND_RFCONFIGURATION 0x32
|
|
|
|
#define PN532_COMMAND_INDATAEXCHANGE 0x40
|
2022-10-25 18:16:23 +01:00
|
|
|
#define PN532_COMMAND_INCOMMUNICATETHRU 0x42
|
2019-02-07 23:06:54 +00:00
|
|
|
#define PN532_COMMAND_INLISTPASSIVETARGET 0x4A
|
2022-10-25 18:16:23 +01:00
|
|
|
#define PN532_COMMAND_INRELEASE 0x52
|
|
|
|
#define PN532_COMMAND_INSELECT 0x54
|
2019-02-07 23:06:54 +00:00
|
|
|
#define PN532_MIFARE_ISO14443A 0x00
|
|
|
|
#define MIFARE_CMD_READ 0x30
|
|
|
|
#define MIFARE_CMD_AUTH_A 0x60
|
|
|
|
#define MIFARE_CMD_AUTH_B 0x61
|
|
|
|
#define MIFARE_CMD_WRITE 0xA0
|
|
|
|
|
2022-10-25 18:16:23 +01:00
|
|
|
#define NTAG21X_CMD_GET_VERSION 0x60
|
2022-10-26 22:48:22 +01:00
|
|
|
#define NTAG2XX_CMD_READ 0x30
|
2022-10-25 18:16:23 +01:00
|
|
|
#define NTAG21X_CMD_FAST_READ 0x3A
|
|
|
|
#define NTAG21X_CMD_PWD_AUTH 0x1B
|
2022-10-26 22:48:22 +01:00
|
|
|
#define NTAG2XX_CMD_WRITE 0xA2
|
2022-10-25 18:16:23 +01:00
|
|
|
|
|
|
|
const struct {
|
|
|
|
uint8_t version[6];
|
|
|
|
uint8_t confPage;
|
|
|
|
} NTAG[] PROGMEM ={
|
|
|
|
{.version={0x04, 0x02, 0x01, 0x00, 0x0f, 0x03},.confPage=0x29}, /* NTAG213 */
|
|
|
|
{.version={0x04, 0x02, 0x01, 0x00, 0x11, 0x03},.confPage=0x83}, /* NTAG215 */
|
|
|
|
{.version={0x04, 0x02, 0x01, 0x00, 0x13, 0x03},.confPage=0xe3}, /* NTAG216 */
|
|
|
|
{.version={0x04, 0x05, 0x02, 0x02, 0x13, 0x03},.confPage=0xe3}, /* NT3H2111 */
|
|
|
|
{.version={0x04, 0x05, 0x02, 0x02, 0x15, 0x03},.confPage=0xe3}, /* NT3H2211 */
|
|
|
|
};
|
|
|
|
#define NTAG_CNT (sizeof(NTAG)/7) // num records in NTAG array
|
|
|
|
|
2020-12-01 15:07:40 +00:00
|
|
|
struct PN532 {
|
|
|
|
char uids[21]; // Number of bytes in the UID. 4, 7 or 10
|
|
|
|
uint8_t packetbuffer[64]; // Global buffer used to store packet
|
|
|
|
uint8_t command = 0; // Carry command code between functions
|
|
|
|
uint8_t scantimer = 0; // Prevent multiple successful reads within 2 second window
|
|
|
|
bool present = false; // Maintain detection flag
|
2022-10-25 22:15:04 +01:00
|
|
|
uint16_t atqa;
|
2022-10-27 17:23:11 +01:00
|
|
|
#ifdef USE_PN532_DATA_FUNCTION
|
2022-10-25 22:15:04 +01:00
|
|
|
uint8_t newdata[32];
|
2020-12-01 15:07:40 +00:00
|
|
|
uint8_t function = 0;
|
2022-10-25 18:16:23 +01:00
|
|
|
uint32_t pwd_auth=0x64636261;
|
|
|
|
uint16_t pwd_pack=0x6665;
|
|
|
|
uint32_t pwd_auth_new;
|
|
|
|
uint16_t pwd_pack_new;
|
2020-12-01 15:07:40 +00:00
|
|
|
#endif // USE_PN532_DATA_FUNCTION
|
|
|
|
} Pn532;
|
2019-02-07 23:06:54 +00:00
|
|
|
|
2020-12-01 15:07:40 +00:00
|
|
|
void PN532_Init(void) {
|
2020-04-27 15:55:56 +01:00
|
|
|
if (PinUsed(GPIO_PN532_RXD) && PinUsed(GPIO_PN532_TXD)) {
|
2020-04-27 15:47:29 +01:00
|
|
|
PN532_Serial = new TasmotaSerial(Pin(GPIO_PN532_RXD), Pin(GPIO_PN532_TXD), 1);
|
2019-02-07 23:06:54 +00:00
|
|
|
if (PN532_Serial->begin(115200)) {
|
|
|
|
if (PN532_Serial->hardwareSerial()) { ClaimSerial(); }
|
|
|
|
PN532_wakeup();
|
|
|
|
uint32_t ver = PN532_getFirmwareVersion();
|
|
|
|
if (ver) {
|
2020-12-01 15:07:40 +00:00
|
|
|
uint8_t empty_uid[4] = { 0 };
|
|
|
|
ToHex_P((unsigned char*)empty_uid, sizeof(empty_uid), Pn532.uids, sizeof(Pn532.uids));
|
2019-02-07 23:06:54 +00:00
|
|
|
PN532_setPassiveActivationRetries(0xFF);
|
|
|
|
PN532_SAMConfig();
|
2021-01-23 16:10:06 +00:00
|
|
|
AddLog(LOG_LEVEL_INFO,"NFC: PN532 NFC Reader detected v%u.%u",(ver>>16) & 0xFF, (ver>>8) & 0xFF);
|
2020-12-01 15:07:40 +00:00
|
|
|
Pn532.present = true;
|
2019-02-07 23:06:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-01 15:07:40 +00:00
|
|
|
int8_t PN532_receive(uint8_t *buf, int len, uint16_t timeout) {
|
2019-02-07 23:06:54 +00:00
|
|
|
int read_bytes = 0;
|
|
|
|
int ret;
|
|
|
|
unsigned long start_millis;
|
|
|
|
while (read_bytes < len) {
|
|
|
|
start_millis = millis();
|
|
|
|
do {
|
|
|
|
ret = PN532_Serial->read();
|
|
|
|
if (ret >= 0) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} while((timeout == 0) || ((millis()- start_millis ) < timeout));
|
2019-02-08 14:35:28 +00:00
|
|
|
|
2019-02-07 23:06:54 +00:00
|
|
|
if (ret < 0) {
|
|
|
|
if (read_bytes) {
|
|
|
|
return read_bytes;
|
|
|
|
} else {
|
|
|
|
return PN532_TIMEOUT;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
buf[read_bytes] = (uint8_t)ret;
|
|
|
|
read_bytes++;
|
|
|
|
}
|
|
|
|
return read_bytes;
|
|
|
|
}
|
|
|
|
|
2020-12-01 15:07:40 +00:00
|
|
|
int8_t PN532_readAckFrame(void) {
|
2019-02-07 23:06:54 +00:00
|
|
|
const uint8_t PN532_ACK[] = {0, 0, 0xFF, 0, 0xFF, 0};
|
|
|
|
uint8_t ackBuf[sizeof(PN532_ACK)];
|
2019-02-08 14:35:28 +00:00
|
|
|
|
2019-02-07 23:06:54 +00:00
|
|
|
if (PN532_receive(ackBuf, sizeof(PN532_ACK), PN532_ACK_WAIT_TIME) <= 0) {
|
|
|
|
return PN532_TIMEOUT;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (memcmp(&ackBuf, &PN532_ACK, sizeof(PN532_ACK))) {
|
|
|
|
return PN532_INVALID_ACK;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-12-01 15:07:40 +00:00
|
|
|
int8_t PN532_writeCommand(const uint8_t *header, uint8_t hlen, const uint8_t *body = 0, uint8_t blen = 0) {
|
2019-02-07 23:06:54 +00:00
|
|
|
// Clear the serial buffer just in case
|
2019-02-08 14:35:28 +00:00
|
|
|
PN532_Serial->flush();
|
2019-02-07 23:06:54 +00:00
|
|
|
|
2020-12-01 15:07:40 +00:00
|
|
|
Pn532.command = header[0];
|
2019-02-07 23:06:54 +00:00
|
|
|
PN532_Serial->write((uint8_t)PN532_PREAMBLE);
|
|
|
|
PN532_Serial->write((uint8_t)PN532_STARTCODE1);
|
|
|
|
PN532_Serial->write(PN532_STARTCODE2);
|
|
|
|
|
|
|
|
uint8_t length = hlen + blen + 1; // length of data field: TFI + DATA
|
|
|
|
PN532_Serial->write(length);
|
|
|
|
PN532_Serial->write(~length + 1); // checksum of length
|
|
|
|
|
|
|
|
PN532_Serial->write(PN532_HOSTTOPN532);
|
|
|
|
uint8_t sum = PN532_HOSTTOPN532; // sum of TFI + DATA
|
|
|
|
|
|
|
|
PN532_Serial->write(header, hlen);
|
2019-06-30 15:44:36 +01:00
|
|
|
for (uint32_t i = 0; i < hlen; i++) {
|
2019-02-07 23:06:54 +00:00
|
|
|
sum += header[i];
|
|
|
|
}
|
|
|
|
|
|
|
|
PN532_Serial->write(body, blen);
|
2019-06-30 15:44:36 +01:00
|
|
|
for (uint32_t i = 0; i < blen; i++) {
|
2019-02-07 23:06:54 +00:00
|
|
|
sum += body[i];
|
|
|
|
}
|
|
|
|
|
|
|
|
uint8_t checksum = ~sum + 1; // checksum of TFI + DATA
|
|
|
|
PN532_Serial->write(checksum);
|
|
|
|
PN532_Serial->write((uint8_t)PN532_POSTAMBLE);
|
2019-02-08 14:35:28 +00:00
|
|
|
|
2019-02-07 23:06:54 +00:00
|
|
|
return PN532_readAckFrame();
|
|
|
|
}
|
|
|
|
|
2020-12-01 15:07:40 +00:00
|
|
|
int16_t PN532_readResponse(uint8_t buf[], uint8_t len, uint16_t timeout = 50) {
|
2019-02-07 23:06:54 +00:00
|
|
|
uint8_t tmp[3];
|
|
|
|
|
|
|
|
// Read preamble and start code
|
|
|
|
if (PN532_receive(tmp, 3, timeout)<=0) {
|
|
|
|
return PN532_TIMEOUT;
|
|
|
|
}
|
|
|
|
if (0 != tmp[0] || 0!= tmp[1] || 0xFF != tmp[2]) {
|
|
|
|
return PN532_INVALID_FRAME;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get length of data to be received
|
|
|
|
uint8_t length[2];
|
|
|
|
if (PN532_receive(length, 2, timeout) <= 0) {
|
|
|
|
return PN532_TIMEOUT;
|
|
|
|
}
|
|
|
|
// Validate that frame is valid
|
|
|
|
if (0 != (uint8_t)(length[0] + length[1])) {
|
|
|
|
return PN532_INVALID_FRAME;
|
|
|
|
}
|
|
|
|
length[0] -= 2;
|
2020-12-01 15:07:40 +00:00
|
|
|
if (length[0] > len) { // If this happens, then Pn532.packetbuffer is not large enough
|
2019-02-07 23:06:54 +00:00
|
|
|
return PN532_NO_SPACE;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the command byte
|
2020-12-01 15:07:40 +00:00
|
|
|
uint8_t cmd = Pn532.command + 1;
|
2019-02-07 23:06:54 +00:00
|
|
|
if (PN532_receive(tmp, 2, timeout) <= 0) { // Time out while receiving
|
|
|
|
return PN532_TIMEOUT;
|
|
|
|
}
|
|
|
|
if (PN532_PN532TOHOST != tmp[0] || cmd != tmp[1]) { // Invalid frame received
|
|
|
|
return PN532_INVALID_FRAME;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (PN532_receive(buf, length[0], timeout) != length[0]) { // Timed out
|
|
|
|
return PN532_TIMEOUT;
|
|
|
|
}
|
2019-02-08 14:35:28 +00:00
|
|
|
|
2019-02-07 23:06:54 +00:00
|
|
|
uint8_t sum = PN532_PN532TOHOST + cmd;
|
2019-06-30 15:44:36 +01:00
|
|
|
for (uint32_t i=0; i<length[0]; i++) {
|
2019-02-07 23:06:54 +00:00
|
|
|
sum += buf[i];
|
|
|
|
}
|
|
|
|
|
|
|
|
// Checksum & postamble
|
|
|
|
if (PN532_receive(tmp, 2, timeout) <= 0) {
|
|
|
|
return PN532_TIMEOUT;
|
|
|
|
}
|
|
|
|
if (0 != (uint8_t)(sum + tmp[0]) || 0 != tmp[1]) { // Checksum fail, so frame must be invalid
|
|
|
|
return PN532_INVALID_FRAME;
|
|
|
|
}
|
|
|
|
|
|
|
|
return length[0];
|
|
|
|
}
|
|
|
|
|
2020-12-01 15:07:40 +00:00
|
|
|
uint32_t PN532_getFirmwareVersion(void) {
|
2019-02-07 23:06:54 +00:00
|
|
|
uint32_t response;
|
|
|
|
|
2020-12-01 15:07:40 +00:00
|
|
|
Pn532.packetbuffer[0] = PN532_COMMAND_GETFIRMWAREVERSION;
|
2019-02-07 23:06:54 +00:00
|
|
|
|
2020-12-01 15:07:40 +00:00
|
|
|
if (PN532_writeCommand(Pn532.packetbuffer, 1)) {
|
2019-02-07 23:06:54 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Read data packet
|
2020-12-01 15:07:40 +00:00
|
|
|
int16_t status = PN532_readResponse(Pn532.packetbuffer, sizeof(Pn532.packetbuffer));
|
2019-02-07 23:06:54 +00:00
|
|
|
if (0 > status) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-12-01 15:07:40 +00:00
|
|
|
response = Pn532.packetbuffer[0];
|
2019-02-07 23:06:54 +00:00
|
|
|
response <<= 8;
|
2020-12-01 15:07:40 +00:00
|
|
|
response |= Pn532.packetbuffer[1];
|
2019-02-07 23:06:54 +00:00
|
|
|
response <<= 8;
|
2020-12-01 15:07:40 +00:00
|
|
|
response |= Pn532.packetbuffer[2];
|
2019-02-07 23:06:54 +00:00
|
|
|
response <<= 8;
|
2020-12-01 15:07:40 +00:00
|
|
|
response |= Pn532.packetbuffer[3];
|
2019-02-07 23:06:54 +00:00
|
|
|
|
|
|
|
return response;
|
|
|
|
}
|
|
|
|
|
2020-12-01 15:07:40 +00:00
|
|
|
void PN532_wakeup(void) {
|
2019-02-07 23:06:54 +00:00
|
|
|
uint8_t wakeup[5] = {0x55, 0x55, 0, 0, 0 };
|
|
|
|
PN532_Serial->write(wakeup,sizeof(wakeup));
|
|
|
|
|
|
|
|
// Flush the serial buffer just in case there's garbage in there
|
2019-02-08 14:35:28 +00:00
|
|
|
PN532_Serial->flush();
|
2019-02-07 23:06:54 +00:00
|
|
|
}
|
|
|
|
|
2020-12-01 15:07:40 +00:00
|
|
|
bool PN532_readPassiveTargetID(uint8_t cardbaudrate, uint8_t *uid, uint8_t *uidLength, uint16_t timeout = 50) {
|
|
|
|
Pn532.packetbuffer[0] = PN532_COMMAND_INLISTPASSIVETARGET;
|
|
|
|
Pn532.packetbuffer[1] = 1; // max 1 cards at once (we can set this to 2 later)
|
|
|
|
Pn532.packetbuffer[2] = cardbaudrate;
|
|
|
|
if (PN532_writeCommand(Pn532.packetbuffer, 3)) {
|
2019-02-07 23:06:54 +00:00
|
|
|
return 0x0; // command failed
|
|
|
|
}
|
|
|
|
// read data packet
|
2020-12-01 15:07:40 +00:00
|
|
|
if (PN532_readResponse(Pn532.packetbuffer, sizeof(Pn532.packetbuffer), timeout) < 0) {
|
2019-02-07 23:06:54 +00:00
|
|
|
return 0x0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Check some basic stuff
|
|
|
|
b0 Tags Found
|
|
|
|
b1 Tag Number (only one used in this example)
|
|
|
|
b2..3 SENS_RES
|
|
|
|
b4 SEL_RES
|
|
|
|
b5 NFCID Length
|
|
|
|
b6..NFCIDLen NFCID
|
|
|
|
*/
|
|
|
|
|
2020-12-01 15:07:40 +00:00
|
|
|
if (Pn532.packetbuffer[0] != 1) {
|
2019-02-07 23:06:54 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2022-10-25 18:16:23 +01:00
|
|
|
Pn532.atqa = Pn532.packetbuffer[2];
|
|
|
|
Pn532.atqa <<= 8;
|
|
|
|
Pn532.atqa |= Pn532.packetbuffer[3];
|
2019-02-07 23:06:54 +00:00
|
|
|
|
|
|
|
/* Card appears to be Mifare Classic */
|
2020-12-01 15:07:40 +00:00
|
|
|
*uidLength = Pn532.packetbuffer[5];
|
2019-02-07 23:06:54 +00:00
|
|
|
|
2020-12-01 15:07:40 +00:00
|
|
|
for (uint32_t i = 0; i < Pn532.packetbuffer[5]; i++) {
|
|
|
|
uid[i] = Pn532.packetbuffer[6 + i];
|
2019-02-07 23:06:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2020-12-01 15:07:40 +00:00
|
|
|
bool PN532_setPassiveActivationRetries(uint8_t maxRetries) {
|
|
|
|
Pn532.packetbuffer[0] = PN532_COMMAND_RFCONFIGURATION;
|
|
|
|
Pn532.packetbuffer[1] = 5; // Config item 5 (MaxRetries)
|
|
|
|
Pn532.packetbuffer[2] = 0xFF; // MxRtyATR (default = 0xFF)
|
|
|
|
Pn532.packetbuffer[3] = 0x01; // MxRtyPSL (default = 0x01)
|
|
|
|
Pn532.packetbuffer[4] = maxRetries;
|
|
|
|
if (PN532_writeCommand(Pn532.packetbuffer, 5)) {
|
2019-02-07 23:06:54 +00:00
|
|
|
return 0; // no ACK
|
|
|
|
}
|
2020-12-01 15:07:40 +00:00
|
|
|
return (0 < PN532_readResponse(Pn532.packetbuffer, sizeof(Pn532.packetbuffer)));
|
2019-02-07 23:06:54 +00:00
|
|
|
}
|
|
|
|
|
2020-12-01 15:07:40 +00:00
|
|
|
bool PN532_SAMConfig(void) {
|
|
|
|
Pn532.packetbuffer[0] = PN532_COMMAND_SAMCONFIGURATION;
|
|
|
|
Pn532.packetbuffer[1] = 0x01; // normal mode
|
|
|
|
Pn532.packetbuffer[2] = 0x14; // timeout 50ms * 20 = 1 second
|
|
|
|
Pn532.packetbuffer[3] = 0x00; // we don't need the external IRQ pin
|
|
|
|
if (PN532_writeCommand(Pn532.packetbuffer, 4)) {
|
2019-02-07 23:06:54 +00:00
|
|
|
return false;
|
|
|
|
}
|
2020-12-01 15:07:40 +00:00
|
|
|
return (0 < PN532_readResponse(Pn532.packetbuffer, sizeof(Pn532.packetbuffer)));
|
2019-02-07 23:06:54 +00:00
|
|
|
}
|
|
|
|
|
2022-10-26 22:48:22 +01:00
|
|
|
/* void PN532_inSelect(void) {
|
2022-10-25 18:16:23 +01:00
|
|
|
Pn532.packetbuffer[0] = PN532_COMMAND_INSELECT;
|
|
|
|
Pn532.packetbuffer[1] = 1;
|
|
|
|
if (PN532_writeCommand(Pn532.packetbuffer, 2)) {
|
|
|
|
return ;
|
|
|
|
}
|
|
|
|
int16_t res = PN532_readResponse(Pn532.packetbuffer, sizeof(Pn532.packetbuffer));
|
2022-10-26 22:48:22 +01:00
|
|
|
} */
|
2022-10-25 18:16:23 +01:00
|
|
|
|
2019-02-07 23:06:54 +00:00
|
|
|
#ifdef USE_PN532_DATA_FUNCTION
|
|
|
|
|
2022-10-27 17:23:11 +01:00
|
|
|
void PN532_inRelease(void) {
|
|
|
|
Pn532.packetbuffer[0] = PN532_COMMAND_INRELEASE;
|
|
|
|
Pn532.packetbuffer[1] = 1;
|
|
|
|
if (PN532_writeCommand(Pn532.packetbuffer, 2)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
PN532_readResponse(Pn532.packetbuffer, sizeof(Pn532.packetbuffer));
|
|
|
|
}
|
|
|
|
|
2020-12-01 15:07:40 +00:00
|
|
|
uint8_t mifareclassic_AuthenticateBlock (uint8_t *uid, uint8_t uidLen, uint32_t blockNumber, uint8_t keyNumber, uint8_t *keyData) {
|
|
|
|
uint8_t i;
|
|
|
|
uint8_t _key[6];
|
|
|
|
uint8_t _uid[7];
|
|
|
|
uint8_t _uidLen;
|
|
|
|
|
|
|
|
// Hang on to the key and uid data
|
|
|
|
memcpy(&_key, keyData, 6);
|
|
|
|
memcpy(&_uid, uid, uidLen);
|
|
|
|
_uidLen = uidLen;
|
|
|
|
|
|
|
|
// Prepare the authentication command //
|
|
|
|
Pn532.packetbuffer[0] = PN532_COMMAND_INDATAEXCHANGE; /* Data Exchange Header */
|
|
|
|
Pn532.packetbuffer[1] = 1; /* Max card numbers */
|
|
|
|
Pn532.packetbuffer[2] = (keyNumber) ? MIFARE_CMD_AUTH_B : MIFARE_CMD_AUTH_A;
|
|
|
|
Pn532.packetbuffer[3] = blockNumber; /* Block Number (1K = 0..63, 4K = 0..255 */
|
|
|
|
memcpy(&Pn532.packetbuffer[4], &_key, 6);
|
|
|
|
for (i = 0; i < _uidLen; i++) {
|
|
|
|
Pn532.packetbuffer[10 + i] = _uid[i]; /* 4 bytes card ID */
|
|
|
|
}
|
2019-02-07 23:06:54 +00:00
|
|
|
|
2020-12-01 15:07:40 +00:00
|
|
|
if (PN532_writeCommand(Pn532.packetbuffer, 10 + _uidLen)) { return 0; }
|
2019-02-07 23:06:54 +00:00
|
|
|
|
2020-12-01 15:07:40 +00:00
|
|
|
// Read the response packet
|
|
|
|
PN532_readResponse(Pn532.packetbuffer, sizeof(Pn532.packetbuffer));
|
2019-02-07 23:06:54 +00:00
|
|
|
|
2020-12-01 15:07:40 +00:00
|
|
|
// Check if the response is valid and we are authenticated???
|
|
|
|
// for an auth success it should be bytes 5-7: 0xD5 0x41 0x00
|
|
|
|
// Mifare auth error is technically byte 7: 0x14 but anything other and 0x00 is not good
|
|
|
|
if (Pn532.packetbuffer[0] != 0x00) {
|
|
|
|
// Authentification failed
|
|
|
|
return 0;
|
|
|
|
}
|
2019-02-07 23:06:54 +00:00
|
|
|
|
2020-12-01 15:07:40 +00:00
|
|
|
return 1;
|
2019-02-07 23:06:54 +00:00
|
|
|
}
|
|
|
|
|
2020-12-01 15:07:40 +00:00
|
|
|
uint8_t mifareclassic_ReadDataBlock (uint8_t blockNumber, uint8_t *data) {
|
|
|
|
/* Prepare the command */
|
2022-10-25 22:50:07 +01:00
|
|
|
for (uint8_t i = 0; i < 2; i++) {
|
|
|
|
Pn532.packetbuffer[0] = PN532_COMMAND_INDATAEXCHANGE;
|
|
|
|
Pn532.packetbuffer[1] = 1; /* Card number */
|
|
|
|
Pn532.packetbuffer[2] = MIFARE_CMD_READ; /* Mifare Read command = 0x30 */
|
|
|
|
Pn532.packetbuffer[3] = blockNumber + i; /* Block Number (0..63 for 1K, 0..255 for 4K) */
|
|
|
|
|
|
|
|
/* Send the command */
|
|
|
|
if (PN532_writeCommand(Pn532.packetbuffer, 4)) {
|
|
|
|
return 0;
|
|
|
|
}
|
2019-02-07 23:06:54 +00:00
|
|
|
|
2022-10-25 22:50:07 +01:00
|
|
|
/* Read the response packet */
|
|
|
|
PN532_readResponse(Pn532.packetbuffer, sizeof(Pn532.packetbuffer));
|
2019-02-07 23:06:54 +00:00
|
|
|
|
2022-10-25 22:50:07 +01:00
|
|
|
/* If byte 8 isn't 0x00 we probably have an error */
|
|
|
|
if (Pn532.packetbuffer[0] != 0x00) {
|
|
|
|
return 0;
|
|
|
|
}
|
2019-02-07 23:06:54 +00:00
|
|
|
|
2022-10-25 22:50:07 +01:00
|
|
|
/* Copy the 16 data bytes to the output buffer */
|
|
|
|
/* Block content starts at byte 9 of a valid response */
|
|
|
|
memcpy (&data[i<<4], &Pn532.packetbuffer[1], 16);
|
2020-12-01 15:07:40 +00:00
|
|
|
}
|
|
|
|
return 1;
|
2019-02-07 23:06:54 +00:00
|
|
|
}
|
|
|
|
|
2022-10-25 23:29:10 +01:00
|
|
|
bool mifareclassic_WriteDataBlock (uint8_t blockNumber, uint8_t *data) {
|
2020-12-01 15:07:40 +00:00
|
|
|
/* Prepare the first command */
|
2022-10-25 22:50:07 +01:00
|
|
|
for (uint8_t i = 0; i < 2; i++) {
|
|
|
|
Pn532.packetbuffer[0] = PN532_COMMAND_INDATAEXCHANGE;
|
|
|
|
Pn532.packetbuffer[1] = 1; /* Card number */
|
|
|
|
Pn532.packetbuffer[2] = MIFARE_CMD_WRITE; /* Mifare Write command = 0xA0 */
|
|
|
|
Pn532.packetbuffer[3] = blockNumber + i; /* Block Number (0..63 for 1K, 0..255 for 4K) */
|
|
|
|
memcpy(&Pn532.packetbuffer[4], &data[i<<4], 16); /* Data Payload */
|
2019-02-07 23:06:54 +00:00
|
|
|
|
2020-12-01 15:07:40 +00:00
|
|
|
/* Send the command */
|
2022-10-25 22:50:07 +01:00
|
|
|
if (PN532_writeCommand(Pn532.packetbuffer, 20)) {
|
2022-10-25 23:29:10 +01:00
|
|
|
return false;
|
2022-10-25 22:50:07 +01:00
|
|
|
}
|
2020-12-01 15:07:40 +00:00
|
|
|
/* Read the response packet */
|
2022-10-26 22:48:22 +01:00
|
|
|
if (PN532_readResponse(Pn532.packetbuffer, sizeof(Pn532.packetbuffer)) < 1) {
|
2022-10-25 23:29:10 +01:00
|
|
|
return false;
|
2022-10-25 22:50:07 +01:00
|
|
|
}
|
|
|
|
}
|
2022-10-25 23:29:10 +01:00
|
|
|
return true;
|
2019-02-07 23:06:54 +00:00
|
|
|
}
|
|
|
|
|
2022-10-25 18:16:23 +01:00
|
|
|
uint8_t ntag21x_probe (void) {
|
|
|
|
uint8_t result=0;
|
|
|
|
|
|
|
|
Pn532.packetbuffer[0] = PN532_COMMAND_INCOMMUNICATETHRU;
|
|
|
|
Pn532.packetbuffer[1] = NTAG21X_CMD_GET_VERSION;
|
|
|
|
|
|
|
|
if (PN532_writeCommand(Pn532.packetbuffer, 2)) {
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (PN532_readResponse(Pn532.packetbuffer, sizeof(Pn532.packetbuffer))<9){
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Pn532.packetbuffer[3] != 4) { // not NTAG type
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (uint8_t i=0; i<NTAG_CNT; i++) {
|
2022-10-26 22:48:22 +01:00
|
|
|
if (0 == memcmp_P(&Pn532.packetbuffer[3],&NTAG[i].version[0],6)) {
|
|
|
|
memcpy_P(&result,&NTAG[i].confPage,sizeof(result));
|
|
|
|
}
|
2022-10-25 18:16:23 +01:00
|
|
|
}
|
|
|
|
return result; //Return configuration page address
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ntag21x_auth(void) {
|
2022-10-26 22:48:22 +01:00
|
|
|
|
2022-10-25 18:16:23 +01:00
|
|
|
Pn532.packetbuffer[0] = PN532_COMMAND_INCOMMUNICATETHRU;
|
|
|
|
Pn532.packetbuffer[1] = NTAG21X_CMD_PWD_AUTH;
|
|
|
|
memcpy(&Pn532.packetbuffer[2],&Pn532.pwd_auth,4);
|
|
|
|
|
|
|
|
if (PN532_writeCommand(Pn532.packetbuffer, 6)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if ((PN532_readResponse(Pn532.packetbuffer, sizeof(Pn532.packetbuffer)))<3){
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (Pn532.packetbuffer[0]) { //PN532 return any error
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return memcmp(&Pn532.packetbuffer[1],&Pn532.pwd_pack,2)==0;
|
|
|
|
}
|
|
|
|
|
2022-10-26 22:48:22 +01:00
|
|
|
bool ntag2xx_read16 (const uint8_t page, char *out) {
|
|
|
|
|
|
|
|
Pn532.packetbuffer[0] = PN532_COMMAND_INCOMMUNICATETHRU;
|
|
|
|
Pn532.packetbuffer[1] = NTAG2XX_CMD_READ;
|
|
|
|
Pn532.packetbuffer[2] = page;
|
|
|
|
if (PN532_writeCommand(Pn532.packetbuffer, 3)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if ((PN532_readResponse(Pn532.packetbuffer, sizeof(Pn532.packetbuffer)))<17){
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (Pn532.packetbuffer[0]!=0) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
memcpy(out,&Pn532.packetbuffer[1],16);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ntag2xx_read32 (const uint8_t page, char *out) { /* Size of oun must be >= 33 */
|
|
|
|
|
|
|
|
if (!ntag2xx_read16(page, out)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (!ntag2xx_read16(page + 4, &out[16])) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
out[32]=0;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ntag2xx_write4(uint8_t page, char *in) {
|
|
|
|
|
2022-10-26 17:21:50 +01:00
|
|
|
Pn532.packetbuffer[0] = PN532_COMMAND_INCOMMUNICATETHRU;
|
2022-10-26 22:48:22 +01:00
|
|
|
Pn532.packetbuffer[1] = NTAG2XX_CMD_WRITE;
|
|
|
|
Pn532.packetbuffer[2] = page; // first page
|
|
|
|
memcpy(&Pn532.packetbuffer[3],in,4);
|
2022-10-25 18:16:23 +01:00
|
|
|
|
2022-10-26 22:48:22 +01:00
|
|
|
if (PN532_writeCommand(Pn532.packetbuffer, 7)) {
|
2022-10-26 17:21:50 +01:00
|
|
|
return false;
|
|
|
|
}
|
2022-10-26 22:48:22 +01:00
|
|
|
|
|
|
|
if (PN532_readResponse(Pn532.packetbuffer, sizeof(Pn532.packetbuffer)) < 1) {
|
2022-10-26 17:21:50 +01:00
|
|
|
return false;
|
|
|
|
}
|
2022-10-26 22:48:22 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ntag2xx_writeN(uint8_t page, uint8_t numPages, char *in) {
|
|
|
|
|
|
|
|
for (uint8_t i = 0; i < numPages; i++) {
|
|
|
|
if (!ntag2xx_write4(page +i, &in[i << 2])) {
|
2022-10-26 17:21:50 +01:00
|
|
|
return false;
|
|
|
|
}
|
2022-10-25 18:16:23 +01:00
|
|
|
}
|
2022-10-26 22:48:22 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ntag21x_set_password(uint8_t confPage, bool unsetPasswd) {
|
|
|
|
char card_datas[16];
|
|
|
|
|
|
|
|
if (ntag2xx_read16(confPage, card_datas)) {
|
|
|
|
if (unsetPasswd) {
|
|
|
|
card_datas[3]=0xFF;
|
|
|
|
return ntag2xx_writeN(confPage,1,card_datas);
|
|
|
|
}
|
|
|
|
card_datas[3]=0; // Set AUTH0 for protect all pages
|
|
|
|
card_datas[4] |= 0x80; // Set PROT flag for read and write access is protected by the password verification
|
|
|
|
memcpy(&card_datas[8],&Pn532.pwd_auth_new, 4); // New password
|
|
|
|
memcpy(&card_datas[12],&Pn532.pwd_pack_new, 2); // New password ack
|
|
|
|
return ntag2xx_writeN(confPage,4,card_datas);
|
|
|
|
}
|
|
|
|
return false;
|
2022-10-25 18:16:23 +01:00
|
|
|
}
|
|
|
|
|
2020-12-01 15:07:40 +00:00
|
|
|
#endif // USE_PN532_DATA_FUNCTION
|
2019-02-07 23:06:54 +00:00
|
|
|
|
2020-12-01 15:07:40 +00:00
|
|
|
void PN532_ScanForTag(void) {
|
2019-02-07 23:06:54 +00:00
|
|
|
uint8_t uid[] = { 0, 0, 0, 0, 0, 0, 0 };
|
|
|
|
uint8_t uid_len = 0;
|
|
|
|
if (PN532_readPassiveTargetID(PN532_MIFARE_ISO14443A, uid, &uid_len)) {
|
2020-12-01 15:07:40 +00:00
|
|
|
ToHex_P((unsigned char*)uid, uid_len, Pn532.uids, sizeof(Pn532.uids));
|
2019-02-08 14:35:28 +00:00
|
|
|
|
|
|
|
#ifdef USE_PN532_DATA_FUNCTION
|
2022-10-25 18:16:23 +01:00
|
|
|
bool success = false;
|
|
|
|
char card_datas[34]={0};
|
|
|
|
enum {NOPWD, PWD_NONE, PWD_OK, PWD_NOK} str_pwd=NOPWD;
|
|
|
|
|
|
|
|
if (Pn532.atqa == 0x44) {
|
|
|
|
uint8_t confPage=0;
|
|
|
|
uint8_t nuid[] = { 0, 0, 0, 0, 0, 0, 0 };
|
|
|
|
uint8_t nuid_len = 0;
|
|
|
|
if ((confPage=ntag21x_probe())>0) {
|
|
|
|
/* NTAG EV1 found*/
|
|
|
|
str_pwd=PWD_NONE;
|
2022-10-26 22:48:22 +01:00
|
|
|
if (!ntag2xx_read32(4, card_datas)) {
|
2022-10-25 18:16:23 +01:00
|
|
|
if (PN532_readPassiveTargetID(PN532_MIFARE_ISO14443A, nuid, &nuid_len)) {
|
|
|
|
if (memcmp(uid, nuid, sizeof(uid))==0) {
|
2022-10-26 22:48:22 +01:00
|
|
|
if (ntag21x_auth()) {
|
|
|
|
str_pwd=PWD_OK;
|
|
|
|
if (Pn532.function == 3) { /* new password */
|
|
|
|
success = ntag21x_set_password(confPage, false);
|
|
|
|
}
|
|
|
|
if (Pn532.function == 4) { /* clear password */
|
|
|
|
success = ntag21x_set_password(confPage, true);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
str_pwd=PWD_NOK;
|
2022-10-25 23:29:10 +01:00
|
|
|
}
|
2022-10-26 22:48:22 +01:00
|
|
|
if (!ntag2xx_read32(4, card_datas)) card_datas[0]=0;
|
2022-10-25 18:16:23 +01:00
|
|
|
}
|
|
|
|
}
|
2022-10-26 22:48:22 +01:00
|
|
|
} else {
|
|
|
|
if (Pn532.function == 3) { /* new password */
|
|
|
|
success = ntag21x_set_password(confPage, false);
|
|
|
|
}
|
|
|
|
}
|
2022-10-25 18:16:23 +01:00
|
|
|
} else {
|
|
|
|
if (PN532_readPassiveTargetID(PN532_MIFARE_ISO14443A, nuid, &nuid_len)) {
|
|
|
|
if (memcmp(uid, nuid, sizeof(uid))==0) {
|
2022-10-26 22:48:22 +01:00
|
|
|
if (!ntag2xx_read32(4, card_datas)) card_datas[0]=0;
|
2022-10-25 18:16:23 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-10-26 22:48:22 +01:00
|
|
|
if ((Pn532.function == 1) || (Pn532.function == 2)) {
|
|
|
|
success = ntag2xx_writeN(4, 8, (char *)Pn532.newdata);
|
|
|
|
if (!ntag2xx_read32(4, card_datas)) card_datas[0]=0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (uid_len == 4) { // Lets try to read blocks 1 & 2 of the mifare classic card for more information
|
2020-12-01 15:07:40 +00:00
|
|
|
uint8_t keyuniversal[6] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
|
|
|
|
if (mifareclassic_AuthenticateBlock (uid, uid_len, 1, 1, keyuniversal)) {
|
2022-10-26 22:48:22 +01:00
|
|
|
if ((Pn532.function == 1) || (Pn532.function == 2)) {
|
|
|
|
success=mifareclassic_WriteDataBlock(1, Pn532.newdata);
|
2019-02-07 23:06:54 +00:00
|
|
|
}
|
2022-10-25 21:14:27 +01:00
|
|
|
if (mifareclassic_ReadDataBlock(1, (uint8_t *)card_datas)) {
|
2022-10-25 22:50:07 +01:00
|
|
|
for (uint32_t i = 0; i < 32;i++) {
|
2022-10-25 21:14:27 +01:00
|
|
|
if (!isprint(card_datas[i])) {
|
|
|
|
// do not output non-printable characters to the console
|
|
|
|
card_datas[i] = 0;
|
|
|
|
}
|
|
|
|
}
|
2022-10-25 22:50:07 +01:00
|
|
|
card_datas[32] = 0;
|
2022-10-25 21:14:27 +01:00
|
|
|
} else {
|
|
|
|
card_datas[0] = 0;
|
|
|
|
}
|
2020-12-01 15:07:40 +00:00
|
|
|
} else {
|
|
|
|
sprintf_P(card_datas, PSTR("AUTHFAIL"));
|
2019-02-07 23:06:54 +00:00
|
|
|
}
|
2020-12-01 15:07:40 +00:00
|
|
|
}
|
|
|
|
switch (Pn532.function) {
|
|
|
|
case 0x01:
|
2022-10-26 22:48:22 +01:00
|
|
|
if (success) {
|
|
|
|
AddLog(LOG_LEVEL_INFO, PSTR("NFC: PN532 - Erase success"));
|
|
|
|
} else {
|
|
|
|
AddLog(LOG_LEVEL_INFO, PSTR("NFC: PN532 - Erase fail - exiting erase mode"));
|
2020-12-01 15:07:40 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 0x02:
|
2022-10-26 22:48:22 +01:00
|
|
|
if (success) {
|
|
|
|
AddLog(LOG_LEVEL_INFO, PSTR("NFC: PN532 - Data write successful"));
|
|
|
|
} else{
|
|
|
|
AddLog(LOG_LEVEL_INFO, PSTR("NFC: PN532 - Write failed - exiting set mode"));
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 0x03:
|
|
|
|
if (success) {
|
|
|
|
AddLog(LOG_LEVEL_INFO, PSTR("NFC: PN532 - Set password successful"));
|
|
|
|
} else{
|
|
|
|
AddLog(LOG_LEVEL_INFO, PSTR("NFC: PN532 - Set password failed - exiting set mode"));
|
2020-12-01 15:07:40 +00:00
|
|
|
}
|
2022-10-26 22:48:22 +01:00
|
|
|
break;
|
|
|
|
case 0x04:
|
|
|
|
if (success) {
|
|
|
|
AddLog(LOG_LEVEL_INFO, PSTR("NFC: PN532 - Unset password successful"));
|
|
|
|
} else{
|
|
|
|
AddLog(LOG_LEVEL_INFO, PSTR("NFC: PN532 - Unset password failed - exiting set mode"));
|
|
|
|
}
|
|
|
|
break;
|
2020-12-01 15:07:40 +00:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
Pn532.function = 0;
|
2022-10-25 18:16:23 +01:00
|
|
|
ResponseTime_P(PSTR(",\"PN532\":{\"UID\":\"%s\",\"" D_JSON_DATA "\":\"%s\""), Pn532.uids, card_datas);
|
|
|
|
if (str_pwd == PWD_NONE) {
|
|
|
|
ResponseAppend_P(PSTR(",\"Auth\":\"None\""));
|
|
|
|
} else
|
|
|
|
if (str_pwd == PWD_OK) {
|
|
|
|
ResponseAppend_P(PSTR(",\"Auth\":\"Ok\""));
|
|
|
|
} else
|
|
|
|
if (str_pwd == PWD_NOK) {
|
|
|
|
ResponseAppend_P(PSTR(",\"Auth\":\"NOk\""));
|
|
|
|
}
|
|
|
|
ResponseAppend_P(PSTR("}}"));
|
2022-10-27 17:23:11 +01:00
|
|
|
PN532_inRelease();
|
2019-02-07 23:06:54 +00:00
|
|
|
#else
|
2020-12-01 15:07:40 +00:00
|
|
|
ResponseTime_P(PSTR(",\"PN532\":{\"UID\":\"%s\"}}"), Pn532.uids);
|
|
|
|
#endif // USE_PN532_DATA_FUNCTION
|
|
|
|
MqttPublishTeleSensor();
|
2019-02-07 23:06:54 +00:00
|
|
|
|
2020-12-01 15:07:40 +00:00
|
|
|
Pn532.scantimer = 7; // Ignore tags found for two seconds
|
2019-02-07 23:06:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef USE_PN532_DATA_FUNCTION
|
|
|
|
|
2020-12-01 15:07:40 +00:00
|
|
|
bool PN532_Command(void) {
|
2019-02-07 23:06:54 +00:00
|
|
|
bool serviced = true;
|
|
|
|
uint8_t paramcount = 0;
|
|
|
|
if (XdrvMailbox.data_len > 0) {
|
|
|
|
paramcount=1;
|
|
|
|
} else {
|
|
|
|
serviced = false;
|
|
|
|
return serviced;
|
|
|
|
}
|
2021-01-30 11:48:44 +00:00
|
|
|
char argument[XdrvMailbox.data_len];
|
2022-10-25 18:16:23 +01:00
|
|
|
ArgV(argument, 1);
|
|
|
|
UpperCase(argument,argument);
|
2022-10-25 22:50:07 +01:00
|
|
|
if (!strncmp_P(argument,PSTR("ERASE"),5)) {
|
2022-10-26 22:48:22 +01:00
|
|
|
memset(Pn532.newdata,0,sizeof(Pn532.newdata));
|
2020-12-01 15:07:40 +00:00
|
|
|
Pn532.function = 1; // Block 1 of next card/tag will be reset to 0x00...
|
2022-10-27 17:23:11 +01:00
|
|
|
AddLog(LOG_LEVEL_INFO, PSTR("NFC: PN532 - Next scanned tag data block will be erased"));
|
2022-10-25 22:56:44 +01:00
|
|
|
ResponseTime_P(PSTR(",\"PN532\":{\"COMMAND\":\"ERASE\"}}"));
|
2019-02-07 23:06:54 +00:00
|
|
|
return serviced;
|
2022-10-25 18:16:23 +01:00
|
|
|
} else
|
2022-10-25 22:50:07 +01:00
|
|
|
if (!strncmp_P(argument,PSTR("WRITE"),5)) {
|
2022-10-25 18:16:23 +01:00
|
|
|
if (ArgC() > 1) {
|
2019-02-07 23:06:54 +00:00
|
|
|
if (XdrvMailbox.data[XdrvMailbox.data_len-1] == ',') {
|
|
|
|
serviced = false;
|
|
|
|
return serviced;
|
|
|
|
}
|
2021-01-30 11:48:44 +00:00
|
|
|
ArgV(argument, 2);
|
2022-10-25 20:38:12 +01:00
|
|
|
strncpy((char *)Pn532.newdata,argument,sizeof(Pn532.newdata));
|
2022-10-25 22:50:07 +01:00
|
|
|
if (strlen(argument)>32) argument[32]=0;
|
2020-12-01 15:07:40 +00:00
|
|
|
Pn532.function = 2;
|
2022-10-26 22:48:22 +01:00
|
|
|
AddLog(LOG_LEVEL_INFO, PSTR("NFC: PN532 - Next scanned tag data block 1 will be set to '%s'"), argument);
|
2022-10-25 22:56:44 +01:00
|
|
|
ResponseTime_P(PSTR(",\"PN532\":{\"COMMAND\":\"WRITE\"}}"));
|
2019-02-07 23:06:54 +00:00
|
|
|
return serviced;
|
|
|
|
}
|
2022-10-25 18:16:23 +01:00
|
|
|
} else
|
|
|
|
if (!strncmp_P(argument,PSTR("AUTH"),4)) {
|
|
|
|
if (ArgC() > 1) {
|
|
|
|
Pn532.pwd_auth=strtoul(ArgV(argument,2),nullptr,0);
|
2022-10-26 22:48:22 +01:00
|
|
|
ResponseTime_P(PSTR(",\"PN532\":{\"COMMAND\":\"AUTH\"}}"));
|
2022-10-25 18:16:23 +01:00
|
|
|
}
|
|
|
|
if (ArgC() > 2) {
|
|
|
|
Pn532.pwd_pack=strtoul(ArgV(argument,3),nullptr,0);
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
} else
|
|
|
|
if (!strncmp_P(argument,PSTR("SET_PWD"),7)) {
|
2022-10-26 22:48:22 +01:00
|
|
|
AddLog(LOG_LEVEL_INFO, PSTR("NFC: PN532 - Next scanned tag will set password"));
|
2022-10-25 18:16:23 +01:00
|
|
|
ResponseTime_P(PSTR(",\"PN532\":{\"COMMAND\":\"SET_PWD\"}}"));
|
|
|
|
Pn532.pwd_auth_new=Pn532.pwd_auth;
|
|
|
|
Pn532.pwd_pack_new=Pn532.pwd_pack;
|
|
|
|
if (ArgC() > 1) {
|
|
|
|
Pn532.pwd_auth_new=strtoul(ArgV(argument,2),nullptr,0);
|
|
|
|
}
|
|
|
|
if (ArgC() > 2) {
|
|
|
|
Pn532.pwd_pack_new=strtoul(ArgV(argument,3),nullptr,0);
|
|
|
|
}
|
|
|
|
Pn532.function = 3;
|
|
|
|
return true;
|
|
|
|
} else
|
|
|
|
if (!strncmp_P(argument,PSTR("UNSET_PWD"),9)) {
|
2022-10-26 22:48:22 +01:00
|
|
|
AddLog(LOG_LEVEL_INFO, PSTR("NFC: PN532 - Next scanned tag will unset password"));
|
|
|
|
ResponseTime_P(PSTR(",\"PN532\":{\"COMMAND\":\"UNSET_PWD\"}}"));
|
|
|
|
Pn532.function = 4;
|
2022-10-25 18:16:23 +01:00
|
|
|
return true;
|
2019-02-07 23:06:54 +00:00
|
|
|
}
|
2020-07-16 16:50:26 +01:00
|
|
|
return false;
|
2019-02-07 23:06:54 +00:00
|
|
|
}
|
|
|
|
|
2020-12-01 15:07:40 +00:00
|
|
|
#endif // USE_PN532_DATA_FUNCTION
|
|
|
|
|
|
|
|
#ifdef USE_WEBSERVER
|
|
|
|
void PN532_Show(void) {
|
2020-12-02 17:01:38 +00:00
|
|
|
WSContentSend_PD(PSTR("{s}PN532 UID{m}%s{e}"), Pn532.uids);
|
2020-12-01 15:07:40 +00:00
|
|
|
}
|
|
|
|
#endif // USE_WEBSERVER
|
2019-02-07 23:06:54 +00:00
|
|
|
|
2020-12-01 15:07:40 +00:00
|
|
|
/*********************************************************************************************\
|
|
|
|
* Interface
|
|
|
|
\*********************************************************************************************/
|
|
|
|
|
|
|
|
bool Xsns40(uint8_t function) {
|
2019-02-07 23:06:54 +00:00
|
|
|
bool result = false;
|
2019-02-08 14:35:28 +00:00
|
|
|
|
2020-12-01 15:07:40 +00:00
|
|
|
if (FUNC_INIT == function) {
|
|
|
|
PN532_Init();
|
|
|
|
}
|
|
|
|
else if (Pn532.present) {
|
|
|
|
switch (function) {
|
|
|
|
case FUNC_EVERY_250_MSECOND:
|
|
|
|
if (Pn532.scantimer > 0) {
|
|
|
|
Pn532.scantimer--;
|
|
|
|
} else {
|
|
|
|
PN532_ScanForTag();
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
#ifdef USE_WEBSERVER
|
|
|
|
case FUNC_WEB_SENSOR:
|
|
|
|
PN532_Show();
|
|
|
|
break;
|
|
|
|
#endif // USE_WEBSERVER
|
2019-02-08 14:35:28 +00:00
|
|
|
#ifdef USE_PN532_DATA_FUNCTION
|
2020-12-01 15:07:40 +00:00
|
|
|
case FUNC_COMMAND_SENSOR:
|
|
|
|
if (XSNS_40 == XdrvMailbox.index) {
|
|
|
|
result = PN532_Command();
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
#endif // USE_PN532_DATA_FUNCTION
|
|
|
|
}
|
2019-02-07 23:06:54 +00:00
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2020-12-01 15:07:40 +00:00
|
|
|
#endif // USE_PN532_HSU
|