mirror of
https://gitlab.com/fabinfra/fabhardware/fabreader.git
synced 2025-03-13 07:01:44 +01:00
Added: new NFC
This commit is contained in:
parent
a8e4d70401
commit
c45ef8ff6b
3
src/FabReader_v2/.vscode/settings.json
vendored
3
src/FabReader_v2/.vscode/settings.json
vendored
@ -9,6 +9,7 @@
|
|||||||
"utility": "cpp",
|
"utility": "cpp",
|
||||||
"string": "cpp",
|
"string": "cpp",
|
||||||
"functional": "cpp",
|
"functional": "cpp",
|
||||||
"*.desfire": "cpp"
|
"*.desfire": "cpp",
|
||||||
|
"*.old": "cpp"
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -1,4 +1,4 @@
|
|||||||
#include "NFC.h"
|
#include "nfc.h"
|
||||||
#include <SPI.h>
|
#include <SPI.h>
|
||||||
#include <PubSubClient.h>
|
#include <PubSubClient.h>
|
||||||
#include <DESFire.h>
|
#include <DESFire.h>
|
||||||
@ -13,140 +13,120 @@ NFC::NFC(int pin_ss, int pin_rst)
|
|||||||
rfid->PCD_DumpVersionToSerial();
|
rfid->PCD_DumpVersionToSerial();
|
||||||
}
|
}
|
||||||
|
|
||||||
bool NFC::requestCard()
|
|
||||||
{
|
|
||||||
byte bufferATQA[2];
|
|
||||||
byte bufferSize = sizeof(bufferATQA);
|
|
||||||
rfid->PCD_WriteRegister(rfid->TxModeReg, 0x00);
|
|
||||||
rfid->PCD_WriteRegister(rfid->RxModeReg, 0x00);
|
|
||||||
rfid->PCD_WriteRegister(rfid->ModWidthReg, 0x26);
|
|
||||||
|
|
||||||
MFRC522::StatusCode status = rfid->PICC_RequestA(bufferATQA, &bufferSize);
|
|
||||||
Serial.printf("PICC_RequestA: 0x%02x\n", status);
|
|
||||||
return status == MFRC522::STATUS_OK;
|
|
||||||
}
|
|
||||||
|
|
||||||
MFRC522::Uid NFC::getUID()
|
MFRC522::Uid NFC::getUID()
|
||||||
{
|
{
|
||||||
return uid;
|
return uid;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool NFC::selectCard()
|
|
||||||
{
|
|
||||||
MFRC522::StatusCode status = rfid->PICC_Select(&uid);
|
|
||||||
Serial.printf("PICC_Select: 0x%02x\n", status);
|
|
||||||
if(status != MFRC522::STATUS_OK)
|
|
||||||
{
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
if(rfid->uid.sak != 0x20)
|
|
||||||
{
|
|
||||||
Serial.printf("SAK Incorret: 0x%02x\n", rfid->uid.sak);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
byte ats_buffer[16];
|
|
||||||
byte ats_buffer_len = 16;
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
status = rfid->PICC_RequestATS(ats_buffer, &ats_buffer_len);
|
|
||||||
if (status != MFRC522::STATUS_OK)
|
|
||||||
{
|
|
||||||
Serial.printf("PICC_RequestATS: 0x%02x\n", status);
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
status = rfid->PICC_ProtocolAndParameterSelection(0x00, 0x11);
|
|
||||||
if (status != MFRC522::STATUS_OK)
|
|
||||||
{
|
|
||||||
Serial.printf("PICC_ProtocolAndParameterSelection: 0x%02x\n", status);
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
cardSelected = true;
|
|
||||||
uid = rfid->uid;
|
|
||||||
Serial.println("Card Selected");
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
bool NFC::deselectCard()
|
|
||||||
{
|
|
||||||
cardSelected = false;
|
|
||||||
Serial.println("Card Deselected");
|
|
||||||
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
bool NFC::hasCardSelected()
|
bool NFC::hasCardSelected()
|
||||||
{
|
{
|
||||||
return cardSelected;
|
return cardSelected;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool NFC::isCardLost()
|
bool NFC::testNFC()
|
||||||
{
|
{
|
||||||
uint8_t control = 0x00;
|
return rfid->PCD_PerformSelfTest();
|
||||||
for(int i=0; i<3; i++)
|
}
|
||||||
{
|
|
||||||
if( !rfid->PICC_IsNewCardPresent() )
|
|
||||||
{
|
|
||||||
if(rfid->PICC_ReadCardSerial())
|
|
||||||
{
|
|
||||||
control |= 0x16;
|
|
||||||
}
|
|
||||||
if(rfid->PICC_ReadCardSerial())
|
|
||||||
{
|
|
||||||
control |= 0x16;
|
|
||||||
}
|
|
||||||
control += 0x1;
|
|
||||||
}
|
|
||||||
control += 0x4;
|
|
||||||
}
|
|
||||||
|
|
||||||
Serial.printf("Control: %d\n", control);
|
bool NFC::checkforCard()
|
||||||
|
{
|
||||||
if(!(control == 13 || control == 14))
|
// RequestA
|
||||||
{
|
if ( ! rfid->PICC_IsNewCardPresent())
|
||||||
Serial.println("Card Lost");
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
{
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// PICC_Select ???
|
||||||
|
if ( ! rfid->PICC_ReadCardSerial())
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check for DESFire
|
||||||
|
if (rfid->uid.sak != 0x20)
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool NFC::connecttoCard()
|
||||||
|
{
|
||||||
|
// RATS
|
||||||
|
byte ats[16];
|
||||||
|
byte atsLength = 16;
|
||||||
|
MFRC522::StatusCode state = rfid->PICC_RequestATS(ats, &atsLength);
|
||||||
|
if (state != MFRC522::STATUS_OK) {
|
||||||
|
Serial.println(F("Failed ATS"));
|
||||||
|
Serial.println(state);
|
||||||
|
rfid->PICC_HaltA();
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// PPS
|
||||||
|
state = rfid->PICC_ProtocolAndParameterSelection(0x00, 0x11, 0x00);
|
||||||
|
if (state != MFRC522::STATUS_OK)
|
||||||
|
{
|
||||||
|
Serial.println("Failed PPS");
|
||||||
|
Serial.println(state);
|
||||||
|
rfid->PICC_HaltA();
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
cardSelected = true;
|
||||||
|
uid = rfid->uid;
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool NFC::disconnectCard()
|
||||||
|
{
|
||||||
|
MFRC522::StatusCode state = rfid->PICC_HaltA();
|
||||||
|
if (state != MFRC522::STATUS_OK)
|
||||||
|
{
|
||||||
|
Serial.println(F("Failed PICC_HaltA"));
|
||||||
|
Serial.println(state);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool NFC::testCard()
|
||||||
|
{
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
MFRC522::StatusCode NFC::Transceive(byte* command, byte command_len, byte* response, byte* response_len)
|
MFRC522::StatusCode NFC::Transceive(byte* command, byte command_len, byte* response, byte* response_len)
|
||||||
{
|
{
|
||||||
byte buffer[63 + 4];
|
MFRC522::StatusCode state;
|
||||||
byte buffer_len = command_len + 4;
|
byte request_buffer[APDU_BUFFER_SIZE + 4]; // APDU Size + PCB + CID + 2x CRC
|
||||||
MFRC522::StatusCode status;
|
byte request_buffer_size = command_len + 4;
|
||||||
|
|
||||||
buffer[0] = 0x0A;
|
request_buffer[0] = pcb;
|
||||||
buffer[1] = 0x00;
|
request_buffer[1] = cid;
|
||||||
memcpy(&buffer[2], command, command_len);
|
|
||||||
|
|
||||||
status = rfid->PCD_CalculateCRC(buffer, buffer_len - 2, &buffer[buffer_len - 2]);
|
memcpy(&request_buffer[2], command, command_len);
|
||||||
if (status != MFRC522::STATUS_OK)
|
|
||||||
|
// Update the PCB
|
||||||
|
if (pcb == 0x0A)
|
||||||
|
pcb = 0x0B;
|
||||||
|
else
|
||||||
|
pcb = 0x0A;
|
||||||
|
|
||||||
|
// Calculate CRC_A
|
||||||
|
state = rfid->PCD_CalculateCRC(request_buffer, request_buffer_size, &request_buffer[request_buffer_size - 2]);
|
||||||
|
if (state != MFRC522::STATUS_OK)
|
||||||
{
|
{
|
||||||
Serial.printf("PCD_CalculateCRC: 0x%02x\n", status);
|
return state;
|
||||||
return status;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Serial.println(buffer_len);
|
state = rfid->PCD_TransceiveData(request_buffer, request_buffer_size, response, response_len);
|
||||||
printbytes(buffer, buffer_len);
|
if (state != MFRC522::STATUS_OK)
|
||||||
|
|
||||||
status = rfid->PCD_TransceiveData(buffer, buffer_len, response, response_len, NULL, 0, true);
|
|
||||||
if (status != MFRC522::STATUS_OK)
|
|
||||||
{
|
{
|
||||||
Serial.printf("PCD_TransceiveData: 0x%02x\n", status);
|
return state;
|
||||||
return status;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return status;
|
return state;
|
||||||
}
|
}
|
@ -2,7 +2,7 @@
|
|||||||
#define NFC_H
|
#define NFC_H
|
||||||
|
|
||||||
#include <PubSubClient.h>
|
#include <PubSubClient.h>
|
||||||
#include <DESFIre.h>
|
#include <DESFire.h>
|
||||||
#define APDU_BUFFER_SIZE 256
|
#define APDU_BUFFER_SIZE 256
|
||||||
|
|
||||||
class NFC
|
class NFC
|
||||||
@ -10,15 +10,18 @@ class NFC
|
|||||||
private:
|
private:
|
||||||
|
|
||||||
MFRC522::Uid uid;
|
MFRC522::Uid uid;
|
||||||
|
byte pcb = 0x0A;
|
||||||
|
byte cid = 0x00;
|
||||||
bool cardSelected = false;
|
bool cardSelected = false;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
DESFire* rfid;
|
DESFire* rfid;
|
||||||
NFC(int pin_ss, int pin_rst);
|
NFC(int pin_ss, int pin_rst);
|
||||||
bool requestCard();
|
bool testNFC();
|
||||||
bool selectCard();
|
bool checkforCard();
|
||||||
bool deselectCard();
|
bool connecttoCard();
|
||||||
bool isCardLost();
|
bool disconnectCard();
|
||||||
|
bool testCard();
|
||||||
bool hasCardSelected();
|
bool hasCardSelected();
|
||||||
MFRC522::Uid getUID();
|
MFRC522::Uid getUID();
|
||||||
|
|
||||||
|
@ -1,110 +1,110 @@
|
|||||||
#include "OTAProxy.h"
|
// #include "OTAProxy.h"
|
||||||
#include "NFC.h"
|
// #include "NFC.h"
|
||||||
#include <PubSubClient.h>
|
// #include <PubSubClient.h>
|
||||||
|
|
||||||
OTAProxy::OTAProxy(PubSubClient* mqttClient, NFC* nfc, int id)
|
// OTAProxy::OTAProxy(PubSubClient* mqttClient, NFC* nfc, int id)
|
||||||
{
|
// {
|
||||||
this->mqtt = mqttClient;
|
// this->mqtt = mqttClient;
|
||||||
this->nfc = nfc;
|
// this->nfc = nfc;
|
||||||
this->id = id;
|
// this->id = id;
|
||||||
}
|
// }
|
||||||
|
|
||||||
bool OTAProxy::hasActiveOTA()
|
// bool OTAProxy::hasActiveOTA()
|
||||||
{
|
// {
|
||||||
return activeOTA;
|
// return activeOTA;
|
||||||
}
|
// }
|
||||||
|
|
||||||
void OTAProxy::startOTA()
|
// void OTAProxy::startOTA()
|
||||||
{
|
// {
|
||||||
if(!(nfc->selectCard()))
|
// if(!(nfc->selectCard()))
|
||||||
{
|
// {
|
||||||
return;
|
// return;
|
||||||
}
|
// }
|
||||||
activeOTA = true;
|
// activeOTA = true;
|
||||||
|
|
||||||
char topic[] = "fabreader/00000/startOTA";
|
// char topic[] = "fabreader/00000/startOTA";
|
||||||
sprintf(topic, "fabreader/%05d/startOTA", id);
|
// sprintf(topic, "fabreader/%05d/startOTA", id);
|
||||||
MFRC522::Uid uid = nfc->getUID();
|
// MFRC522::Uid uid = nfc->getUID();
|
||||||
mqtt->publish(topic, uid.uidByte, uid.size);
|
// mqtt->publish(topic, uid.uidByte, uid.size);
|
||||||
|
|
||||||
Serial.println("Start OTA");
|
// Serial.println("Start OTA");
|
||||||
}
|
// }
|
||||||
|
|
||||||
void OTAProxy::continueOTA(char* topic, byte* payload, unsigned int length)
|
// void OTAProxy::continueOTA(char* topic, byte* payload, unsigned int length)
|
||||||
{
|
// {
|
||||||
char topic_requestOTA[] = "fabreader/00000/requestOTA";
|
// char topic_requestOTA[] = "fabreader/00000/requestOTA";
|
||||||
sprintf(topic_requestOTA, "fabreader/%05d/requestOTA", id);
|
// sprintf(topic_requestOTA, "fabreader/%05d/requestOTA", id);
|
||||||
|
|
||||||
char topic_responseOTA[] = "fabreader/00000/responseOTA";
|
// char topic_responseOTA[] = "fabreader/00000/responseOTA";
|
||||||
sprintf(topic_responseOTA, "fabreader/%05d/responseOTA", id);
|
// sprintf(topic_responseOTA, "fabreader/%05d/responseOTA", id);
|
||||||
|
|
||||||
char topic_stopOTA[] = "fabreader/00000/stopOTA";
|
// char topic_stopOTA[] = "fabreader/00000/stopOTA";
|
||||||
sprintf(topic_stopOTA, "fabreader/%05d/stopOTA", id);
|
// sprintf(topic_stopOTA, "fabreader/%05d/stopOTA", id);
|
||||||
|
|
||||||
char topic_restartOTA[] = "fabreader/00000/restartOTA";
|
// char topic_restartOTA[] = "fabreader/00000/restartOTA";
|
||||||
sprintf(topic_restartOTA, "fabreader/%05d/restartOTA", id);
|
// sprintf(topic_restartOTA, "fabreader/%05d/restartOTA", id);
|
||||||
|
|
||||||
if(!strcmp(topic, topic_requestOTA))
|
// if(!strcmp(topic, topic_requestOTA))
|
||||||
{
|
// {
|
||||||
Serial.println("Request OTA");
|
// Serial.println("Request OTA");
|
||||||
byte response[APDU_BUFFER_SIZE] = {0};
|
// byte response[APDU_BUFFER_SIZE] = {0};
|
||||||
byte response_len;
|
// byte response_len;
|
||||||
|
|
||||||
MFRC522::StatusCode status;
|
// MFRC522::StatusCode status;
|
||||||
while (true)
|
// while (true)
|
||||||
{
|
// {
|
||||||
if(nfc->isCardLost())
|
// if(nfc->isCardLost())
|
||||||
{
|
// {
|
||||||
Serial.println("Card Lost");
|
// Serial.println("Card Lost");
|
||||||
return;
|
// return;
|
||||||
}
|
// }
|
||||||
Serial.println("Run Transceive");
|
// Serial.println("Run Transceive");
|
||||||
status = nfc->Transceive(payload, length, response, &response_len);
|
// status = nfc->Transceive(payload, length, response, &response_len);
|
||||||
Serial.printf("PICC_Tranceive: 0x%02x\n", status);
|
// Serial.printf("PICC_Tranceive: 0x%02x\n", status);
|
||||||
if(status == MFRC522::STATUS_OK)
|
// if(status == MFRC522::STATUS_OK)
|
||||||
{
|
// {
|
||||||
break;
|
// break;
|
||||||
}
|
// }
|
||||||
}
|
// }
|
||||||
|
|
||||||
for(int i = 0; i < response_len; i++)
|
// for(int i = 0; i < response_len; i++)
|
||||||
{
|
// {
|
||||||
char hexCar[2];
|
// char hexCar[2];
|
||||||
|
|
||||||
sprintf(hexCar, "%02X", response[i]);
|
// sprintf(hexCar, "%02X", response[i]);
|
||||||
Serial.print(hexCar);
|
// Serial.print(hexCar);
|
||||||
}
|
// }
|
||||||
Serial.println();
|
// Serial.println();
|
||||||
|
|
||||||
mqtt->publish(topic_responseOTA, response, response_len);
|
// mqtt->publish(topic_responseOTA, response, response_len);
|
||||||
Serial.println("Response OTA");
|
// Serial.println("Response OTA");
|
||||||
}
|
// }
|
||||||
else if(!strcmp(topic, topic_stopOTA))
|
// else if(!strcmp(topic, topic_stopOTA))
|
||||||
{
|
// {
|
||||||
Serial.println("Stop OTA");
|
// Serial.println("Stop OTA");
|
||||||
while(!(nfc->deselectCard()));
|
// while(!(nfc->deselectCard()));
|
||||||
activeOTA = false;
|
// activeOTA = false;
|
||||||
}
|
// }
|
||||||
else if(!strcmp(topic, topic_restartOTA))
|
// else if(!strcmp(topic, topic_restartOTA))
|
||||||
{
|
// {
|
||||||
Serial.println("Restart OTA");
|
// Serial.println("Restart OTA");
|
||||||
while(!(nfc->deselectCard()));
|
// while(!(nfc->deselectCard()));
|
||||||
// if(nfc->hasNewCard())
|
// // if(nfc->hasNewCard())
|
||||||
// {
|
// // {
|
||||||
// startOTA();
|
// // startOTA();
|
||||||
// }
|
// // }
|
||||||
}
|
// }
|
||||||
}
|
// }
|
||||||
|
|
||||||
void OTAProxy::cancelOTA()
|
// void OTAProxy::cancelOTA()
|
||||||
{
|
// {
|
||||||
char topic_cancelOTA[] = "fabreader/00000/cancelOTA";
|
// char topic_cancelOTA[] = "fabreader/00000/cancelOTA";
|
||||||
sprintf(topic_cancelOTA, "fabreader/%05d/cancelOTA", id);
|
// sprintf(topic_cancelOTA, "fabreader/%05d/cancelOTA", id);
|
||||||
MFRC522::Uid uid = nfc->getUID();
|
// MFRC522::Uid uid = nfc->getUID();
|
||||||
mqtt->publish(topic_cancelOTA, uid.uidByte, uid.size);
|
// mqtt->publish(topic_cancelOTA, uid.uidByte, uid.size);
|
||||||
|
|
||||||
while(!(nfc->deselectCard()));
|
// while(!(nfc->deselectCard()));
|
||||||
activeOTA = false;
|
// activeOTA = false;
|
||||||
|
|
||||||
Serial.println("Cancel OTA");
|
// Serial.println("Cancel OTA");
|
||||||
}
|
// }
|
@ -62,6 +62,7 @@ void bytes2chars(byte* array, byte array_len, char* str, bool msb)
|
|||||||
|
|
||||||
void printbytes(byte* array, byte array_len)
|
void printbytes(byte* array, byte array_len)
|
||||||
{
|
{
|
||||||
|
Serial.print("0x");
|
||||||
for(int i = 0; i < array_len; i++)
|
for(int i = 0; i < array_len; i++)
|
||||||
{
|
{
|
||||||
Serial.printf("%02x", array[i]);
|
Serial.printf("%02x", array[i]);
|
||||||
|
@ -1,104 +1,146 @@
|
|||||||
|
/*
|
||||||
|
* --------------------------------------------------------------------------------------------------------------------
|
||||||
|
* Example sketch/program showing how to read data from a PICC to serial.
|
||||||
|
* --------------------------------------------------------------------------------------------------------------------
|
||||||
|
* This is a MFRC522 library example; for further details and other examples see: https://github.com/miguelbalboa/rfid
|
||||||
|
*
|
||||||
|
* Example sketch/program showing how to read data from a PICC (that is: a RFID Tag or Card) using a MFRC522 based RFID
|
||||||
|
* Reader on the Arduino SPI interface.
|
||||||
|
*
|
||||||
|
* When the Arduino and the MFRC522 module are connected (see the pin layout below), load this sketch into Arduino IDE
|
||||||
|
* then verify/compile and upload it. To see the output: use Tools, Serial Monitor of the IDE (hit Ctrl+Shft+M). When
|
||||||
|
* you present a PICC (that is: a RFID Tag or Card) at reading distance of the MFRC522 Reader/PCD, the serial output
|
||||||
|
* will show the ID/UID, type and any data blocks it can read. Note: you may see "Timeout in communication" messages
|
||||||
|
* when removing the PICC from reading distance too early.
|
||||||
|
*
|
||||||
|
* If your reader supports it, this sketch/program will read all the PICCs presented (that is: multiple tag reading).
|
||||||
|
* So if you stack two or more PICCs on top of each other and present them to the reader, it will first output all
|
||||||
|
* details of the first and then the next PICC. Note that this may take some time as all data blocks are dumped, so
|
||||||
|
* keep the PICCs at reading distance until complete.
|
||||||
|
*
|
||||||
|
* @license Released into the public domain.
|
||||||
|
*
|
||||||
|
* Typical pin layout used:
|
||||||
|
* -----------------------------------------------------------------------------------------
|
||||||
|
* MFRC522 Arduino Arduino Arduino Arduino Arduino
|
||||||
|
* Reader/PCD Uno/101 Mega Nano v3 Leonardo/Micro Pro Micro
|
||||||
|
* Signal Pin Pin Pin Pin Pin Pin
|
||||||
|
* -----------------------------------------------------------------------------------------
|
||||||
|
* RST/Reset RST 9 5 D9 RESET/ICSP-5 RST
|
||||||
|
* SPI SS SDA(SS) 10 53 D10 10 10
|
||||||
|
* SPI MOSI MOSI 11 / ICSP-4 51 D11 ICSP-4 16
|
||||||
|
* SPI MISO MISO 12 / ICSP-1 50 D12 ICSP-1 14
|
||||||
|
* SPI SCK SCK 13 / ICSP-3 52 D13 ICSP-3 15
|
||||||
|
*/
|
||||||
|
|
||||||
#include <SPI.h>
|
#include <SPI.h>
|
||||||
#include <MFRC522.h>
|
#include <MFRC522.h>
|
||||||
#include <Desfire.h>
|
#include <Desfire.h>
|
||||||
|
|
||||||
|
#define RST_PIN 9 // Configurable, see typical pin layout above
|
||||||
|
#define SS_PIN 10 // Configurable, see typical pin layout above
|
||||||
|
|
||||||
#include "Pins.h"
|
#include "Pins.h"
|
||||||
|
|
||||||
DESFire mfrc522(PIN_RFID_SPI_SS, PIN_RFID_RST); // Create MFRC522 instance
|
DESFire mfrc522(PIN_RFID_SPI_SS, PIN_RFID_RST); // Create MFRC522 instance
|
||||||
|
|
||||||
void setup()
|
void setup() {
|
||||||
{
|
|
||||||
Serial.begin(115200); // Initialize serial communications with the PC
|
Serial.begin(115200); // Initialize serial communications with the PC
|
||||||
while (!Serial); // Do nothing if no serial port is opened (added for Arduinos based on ATMEGA32U4)
|
while (!Serial); // Do nothing if no serial port is opened (added for Arduinos based on ATMEGA32U4)
|
||||||
SPI.begin(); // Init SPI bus
|
SPI.begin(); // Init SPI bus
|
||||||
mfrc522.PCD_Init(); // Init MFRC522
|
mfrc522.PCD_Init(); // Init MFRC522
|
||||||
mfrc522.PCD_DumpVersionToSerial(); // Show details of PCD - MFRC522 Card Reader details
|
mfrc522.PCD_DumpVersionToSerial(); // Show details of PCD - MFRC522 Card Reader details
|
||||||
|
mfrc522.PCD_PerformSelfTest();
|
||||||
Serial.println(F("Scan PICC to see UID, SAK, type, and data blocks..."));
|
Serial.println(F("Scan PICC to see UID, SAK, type, and data blocks..."));
|
||||||
}
|
}
|
||||||
|
|
||||||
void printBuffer(byte* buffer, byte buffer_len)
|
void loop() {
|
||||||
{
|
// Look for new cards
|
||||||
Serial.printf("Len: %02d Buffer: 0x", buffer_len);
|
if ( ! mfrc522.PICC_IsNewCardPresent()) {
|
||||||
for(int i = 0; i < buffer_len; i++)
|
|
||||||
{
|
|
||||||
Serial.printf("%02x", buffer[i]);
|
|
||||||
}
|
|
||||||
Serial.println();
|
|
||||||
}
|
|
||||||
|
|
||||||
void loop()
|
|
||||||
{
|
|
||||||
// RequestA
|
|
||||||
if ( ! mfrc522.PICC_IsNewCardPresent())
|
|
||||||
{
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// PICC_Select ???
|
// Select one of the cards
|
||||||
if ( ! mfrc522.PICC_ReadCardSerial())
|
if ( ! mfrc522.PICC_ReadCardSerial()) {
|
||||||
{
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check for DESFire
|
if (mfrc522.uid.sak != 0x20) {
|
||||||
if (mfrc522.uid.sak != 0x20)
|
|
||||||
{
|
|
||||||
// Dump debug info about the card; PICC_HaltA() is automatically called
|
// Dump debug info about the card; PICC_HaltA() is automatically called
|
||||||
mfrc522.PICC_DumpToSerial(&(mfrc522.uid));
|
mfrc522.PICC_DumpToSerial(&(mfrc522.uid));
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
Serial.println("New Card");
|
// Show an extra line
|
||||||
MFRC522::StatusCode state;
|
Serial.println();
|
||||||
|
|
||||||
// RATS
|
|
||||||
byte ats[16];
|
|
||||||
byte atsLength = 16;
|
|
||||||
state = mfrc522.PICC_RequestATS(ats, &atsLength);
|
|
||||||
if (state != MFRC522::STATUS_OK) {
|
|
||||||
Serial.println(F("Failed to get ATS!"));
|
|
||||||
Serial.println(state);
|
|
||||||
mfrc522.PICC_HaltA();
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
// PPS
|
|
||||||
state = mfrc522.PICC_ProtocolAndParameterSelection(0x00, 0x11, 0x00);
|
|
||||||
if (state != MFRC522::STATUS_OK)
|
|
||||||
{
|
|
||||||
Serial.println(F("Failed to perform protocol and parameter selection (PPS)!"));
|
|
||||||
Serial.println(state);
|
|
||||||
mfrc522.PICC_HaltA();
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
Serial.println("Complete ISO 14443 Activation");
|
|
||||||
|
|
||||||
DESFire::mifare_desfire_tag tag;
|
DESFire::mifare_desfire_tag tag;
|
||||||
|
DESFire::StatusCode response;
|
||||||
|
|
||||||
tag.pcb = 0x0A;
|
tag.pcb = 0x0A;
|
||||||
tag.cid = 0x00;
|
tag.cid = 0x00;
|
||||||
|
memset(tag.selected_application, 0, 3);
|
||||||
|
|
||||||
DESFire::StatusCode des_state;
|
// Make sure none DESFire status codes have DESFireStatus code to OK
|
||||||
byte buffer[64] = {0x5A, 0x00, 0x00, 0x03, 0x42, 0x41, 0x46, 0x00};
|
response.desfire = DESFire::MF_OPERATION_OK;
|
||||||
byte bufferSize = 8;
|
|
||||||
|
|
||||||
byte response_buffer[64];
|
byte ats[16];
|
||||||
byte response_len;
|
byte atsLength = 16;
|
||||||
|
response.mfrc522 = mfrc522.PICC_RequestATS(ats, &atsLength);
|
||||||
|
if ( ! mfrc522.IsStatusCodeOK(response)) {
|
||||||
|
Serial.println(F("Failed to get ATS!"));
|
||||||
|
Serial.println(mfrc522.GetStatusCodeName(response));
|
||||||
|
Serial.println(response.mfrc522);
|
||||||
|
|
||||||
des_state = mfrc522.MIFARE_BlockExchangeWithData(&tag, 0x90, buffer, &bufferSize, response_buffer, &response_len);
|
|
||||||
state = des_state.mfrc522;
|
|
||||||
if (state != MFRC522::STATUS_OK)
|
|
||||||
{
|
|
||||||
Serial.println("Data Exchange failed");
|
|
||||||
Serial.println(state);
|
|
||||||
}
|
|
||||||
Serial.println("Data Exchange complete");
|
|
||||||
printBuffer(response_buffer, response_len);
|
|
||||||
|
|
||||||
// Deselect
|
|
||||||
// Missing
|
|
||||||
|
|
||||||
// HaltA
|
|
||||||
mfrc522.PICC_HaltA();
|
mfrc522.PICC_HaltA();
|
||||||
Serial.println("Stop Connection");
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// TODO: Should do checks but since I know my DESFire allows and requires PPS...
|
||||||
|
// PPS1 is ommitted and, therefore, 0x00 is used (106kBd)
|
||||||
|
response.mfrc522 = mfrc522.PICC_ProtocolAndParameterSelection(0x00, 0x11);
|
||||||
|
if ( ! mfrc522.IsStatusCodeOK(response)) {
|
||||||
|
Serial.println(F("Failed to perform protocol and parameter selection (PPS)!"));
|
||||||
|
Serial.println(mfrc522.GetStatusCodeName(response));
|
||||||
|
mfrc522.PICC_HaltA();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// MIFARE DESFire should respond to a GetVersion command
|
||||||
|
DESFire::MIFARE_DESFIRE_Version_t desfireVersion;
|
||||||
|
response = mfrc522.MIFARE_DESFIRE_GetVersion(&tag, &desfireVersion);
|
||||||
|
if ( ! mfrc522.IsStatusCodeOK(response)) {
|
||||||
|
Serial.println(F("Failed to get a response for GetVersion!"));
|
||||||
|
Serial.println(mfrc522.GetStatusCodeName(response));
|
||||||
|
mfrc522.PICC_HaltA();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Dump MIFARE DESFire version information.
|
||||||
|
// NOTE: KEEP YOUR CARD CLOSE TO THE READER!
|
||||||
|
// This method takes some time and the card will be read
|
||||||
|
// once output ends! If you remove the card too fast
|
||||||
|
// a timeout will occur!
|
||||||
|
mfrc522.PICC_DumpMifareDesfireVersion(&tag, &desfireVersion);
|
||||||
|
|
||||||
|
mfrc522.PICC_DumpMifareDesfireMasterKey(&tag);
|
||||||
|
|
||||||
|
DESFire::mifare_desfire_aid_t aids[MIFARE_MAX_APPLICATION_COUNT];
|
||||||
|
byte applicationCount = 0;
|
||||||
|
response = mfrc522.MIFARE_DESFIRE_GetApplicationIds(&tag, aids, &applicationCount);
|
||||||
|
if ( ! mfrc522.IsStatusCodeOK(response)) {
|
||||||
|
Serial.println(F("Failed to get application IDs!"));
|
||||||
|
Serial.println(mfrc522.GetStatusCodeName(response));
|
||||||
|
mfrc522.PICC_HaltA();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Dump all applications
|
||||||
|
for (byte aidIndex = 0; aidIndex < applicationCount; aidIndex++) {
|
||||||
|
mfrc522.PICC_DumpMifareDesfireApplication(&tag, &(aids[aidIndex]));
|
||||||
|
}
|
||||||
|
|
||||||
|
// Call PICC_HaltA()
|
||||||
|
mfrc522.PICC_HaltA();
|
||||||
|
Serial.println();
|
||||||
}
|
}
|
49
src/FabReader_v2/src/main.cpp.2
Normal file
49
src/FabReader_v2/src/main.cpp.2
Normal file
@ -0,0 +1,49 @@
|
|||||||
|
#include <SPI.h>
|
||||||
|
#include <MFRC522.h>
|
||||||
|
#include <Desfire.h>
|
||||||
|
|
||||||
|
#include "nfc.h"
|
||||||
|
#include "pins.h"
|
||||||
|
#include "helpers.h"
|
||||||
|
|
||||||
|
NFC* nfc;
|
||||||
|
|
||||||
|
void setup()
|
||||||
|
{
|
||||||
|
Serial.begin(115200); // Initialize serial communications with the PC
|
||||||
|
while (!Serial); // Do nothing if no serial port is opened (added for Arduinos based on ATMEGA32U4)
|
||||||
|
|
||||||
|
nfc = new NFC(PIN_RFID_SPI_SS, PIN_RFID_RST);
|
||||||
|
}
|
||||||
|
|
||||||
|
void loop()
|
||||||
|
{
|
||||||
|
if(nfc->checkforCard())
|
||||||
|
{
|
||||||
|
Serial.println("Card detected");
|
||||||
|
if(nfc->connecttoCard())
|
||||||
|
{
|
||||||
|
Serial.println("Card connected");
|
||||||
|
|
||||||
|
MFRC522::StatusCode state;
|
||||||
|
|
||||||
|
byte request_buffer[APDU_BUFFER_SIZE] = {0x5A, 0x00, 0x00, 0x03, 0x42, 0x41, 0x46, 0x00};
|
||||||
|
byte request_buffer_size = 8;
|
||||||
|
byte response_buffer[APDU_BUFFER_SIZE];
|
||||||
|
byte response_buffer_size;
|
||||||
|
printbytes(request_buffer, request_buffer_size);
|
||||||
|
|
||||||
|
state = nfc->Transceive(request_buffer, request_buffer_size, response_buffer, &response_buffer_size);
|
||||||
|
if (state != MFRC522::STATUS_OK)
|
||||||
|
{
|
||||||
|
Serial.println("Data Exchange failed");
|
||||||
|
Serial.println(state);
|
||||||
|
}
|
||||||
|
Serial.println("Data Exchange complete");
|
||||||
|
printbytes(response_buffer, response_buffer_size);
|
||||||
|
|
||||||
|
nfc->disconnectCard();
|
||||||
|
Serial.println("Card disconnected");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -1,145 +0,0 @@
|
|||||||
/*
|
|
||||||
* --------------------------------------------------------------------------------------------------------------------
|
|
||||||
* Example sketch/program showing how to read data from a PICC to serial.
|
|
||||||
* --------------------------------------------------------------------------------------------------------------------
|
|
||||||
* This is a MFRC522 library example; for further details and other examples see: https://github.com/miguelbalboa/rfid
|
|
||||||
*
|
|
||||||
* Example sketch/program showing how to read data from a PICC (that is: a RFID Tag or Card) using a MFRC522 based RFID
|
|
||||||
* Reader on the Arduino SPI interface.
|
|
||||||
*
|
|
||||||
* When the Arduino and the MFRC522 module are connected (see the pin layout below), load this sketch into Arduino IDE
|
|
||||||
* then verify/compile and upload it. To see the output: use Tools, Serial Monitor of the IDE (hit Ctrl+Shft+M). When
|
|
||||||
* you present a PICC (that is: a RFID Tag or Card) at reading distance of the MFRC522 Reader/PCD, the serial output
|
|
||||||
* will show the ID/UID, type and any data blocks it can read. Note: you may see "Timeout in communication" messages
|
|
||||||
* when removing the PICC from reading distance too early.
|
|
||||||
*
|
|
||||||
* If your reader supports it, this sketch/program will read all the PICCs presented (that is: multiple tag reading).
|
|
||||||
* So if you stack two or more PICCs on top of each other and present them to the reader, it will first output all
|
|
||||||
* details of the first and then the next PICC. Note that this may take some time as all data blocks are dumped, so
|
|
||||||
* keep the PICCs at reading distance until complete.
|
|
||||||
*
|
|
||||||
* @license Released into the public domain.
|
|
||||||
*
|
|
||||||
* Typical pin layout used:
|
|
||||||
* -----------------------------------------------------------------------------------------
|
|
||||||
* MFRC522 Arduino Arduino Arduino Arduino Arduino
|
|
||||||
* Reader/PCD Uno/101 Mega Nano v3 Leonardo/Micro Pro Micro
|
|
||||||
* Signal Pin Pin Pin Pin Pin Pin
|
|
||||||
* -----------------------------------------------------------------------------------------
|
|
||||||
* RST/Reset RST 9 5 D9 RESET/ICSP-5 RST
|
|
||||||
* SPI SS SDA(SS) 10 53 D10 10 10
|
|
||||||
* SPI MOSI MOSI 11 / ICSP-4 51 D11 ICSP-4 16
|
|
||||||
* SPI MISO MISO 12 / ICSP-1 50 D12 ICSP-1 14
|
|
||||||
* SPI SCK SCK 13 / ICSP-3 52 D13 ICSP-3 15
|
|
||||||
*/
|
|
||||||
|
|
||||||
#include <SPI.h>
|
|
||||||
#include <MFRC522.h>
|
|
||||||
#include <Desfire.h>
|
|
||||||
|
|
||||||
#define RST_PIN 9 // Configurable, see typical pin layout above
|
|
||||||
#define SS_PIN 10 // Configurable, see typical pin layout above
|
|
||||||
|
|
||||||
#include "Pins.h"
|
|
||||||
|
|
||||||
DESFire mfrc522(PIN_RFID_SPI_SS, PIN_RFID_RST); // Create MFRC522 instance
|
|
||||||
|
|
||||||
void setup() {
|
|
||||||
Serial.begin(115200); // Initialize serial communications with the PC
|
|
||||||
while (!Serial); // Do nothing if no serial port is opened (added for Arduinos based on ATMEGA32U4)
|
|
||||||
SPI.begin(); // Init SPI bus
|
|
||||||
mfrc522.PCD_Init(); // Init MFRC522
|
|
||||||
mfrc522.PCD_DumpVersionToSerial(); // Show details of PCD - MFRC522 Card Reader details
|
|
||||||
Serial.println(F("Scan PICC to see UID, SAK, type, and data blocks..."));
|
|
||||||
}
|
|
||||||
|
|
||||||
void loop() {
|
|
||||||
// Look for new cards
|
|
||||||
if ( ! mfrc522.PICC_IsNewCardPresent()) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Select one of the cards
|
|
||||||
if ( ! mfrc522.PICC_ReadCardSerial()) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (mfrc522.uid.sak != 0x20) {
|
|
||||||
// Dump debug info about the card; PICC_HaltA() is automatically called
|
|
||||||
mfrc522.PICC_DumpToSerial(&(mfrc522.uid));
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Show an extra line
|
|
||||||
Serial.println();
|
|
||||||
|
|
||||||
DESFire::mifare_desfire_tag tag;
|
|
||||||
DESFire::StatusCode response;
|
|
||||||
|
|
||||||
tag.pcb = 0x0A;
|
|
||||||
tag.cid = 0x00;
|
|
||||||
memset(tag.selected_application, 0, 3);
|
|
||||||
|
|
||||||
// Make sure none DESFire status codes have DESFireStatus code to OK
|
|
||||||
response.desfire = DESFire::MF_OPERATION_OK;
|
|
||||||
|
|
||||||
byte ats[16];
|
|
||||||
byte atsLength = 16;
|
|
||||||
response.mfrc522 = mfrc522.PICC_RequestATS(ats, &atsLength);
|
|
||||||
if ( ! mfrc522.IsStatusCodeOK(response)) {
|
|
||||||
Serial.println(F("Failed to get ATS!"));
|
|
||||||
Serial.println(mfrc522.GetStatusCodeName(response));
|
|
||||||
Serial.println(response.mfrc522);
|
|
||||||
|
|
||||||
mfrc522.PICC_HaltA();
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
// TODO: Should do checks but since I know my DESFire allows and requires PPS...
|
|
||||||
// PPS1 is ommitted and, therefore, 0x00 is used (106kBd)
|
|
||||||
response.mfrc522 = mfrc522.PICC_ProtocolAndParameterSelection(0x00, 0x11);
|
|
||||||
if ( ! mfrc522.IsStatusCodeOK(response)) {
|
|
||||||
Serial.println(F("Failed to perform protocol and parameter selection (PPS)!"));
|
|
||||||
Serial.println(mfrc522.GetStatusCodeName(response));
|
|
||||||
mfrc522.PICC_HaltA();
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
// MIFARE DESFire should respond to a GetVersion command
|
|
||||||
DESFire::MIFARE_DESFIRE_Version_t desfireVersion;
|
|
||||||
response = mfrc522.MIFARE_DESFIRE_GetVersion(&tag, &desfireVersion);
|
|
||||||
if ( ! mfrc522.IsStatusCodeOK(response)) {
|
|
||||||
Serial.println(F("Failed to get a response for GetVersion!"));
|
|
||||||
Serial.println(mfrc522.GetStatusCodeName(response));
|
|
||||||
mfrc522.PICC_HaltA();
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Dump MIFARE DESFire version information.
|
|
||||||
// NOTE: KEEP YOUR CARD CLOSE TO THE READER!
|
|
||||||
// This method takes some time and the card will be read
|
|
||||||
// once output ends! If you remove the card too fast
|
|
||||||
// a timeout will occur!
|
|
||||||
mfrc522.PICC_DumpMifareDesfireVersion(&tag, &desfireVersion);
|
|
||||||
|
|
||||||
mfrc522.PICC_DumpMifareDesfireMasterKey(&tag);
|
|
||||||
|
|
||||||
DESFire::mifare_desfire_aid_t aids[MIFARE_MAX_APPLICATION_COUNT];
|
|
||||||
byte applicationCount = 0;
|
|
||||||
response = mfrc522.MIFARE_DESFIRE_GetApplicationIds(&tag, aids, &applicationCount);
|
|
||||||
if ( ! mfrc522.IsStatusCodeOK(response)) {
|
|
||||||
Serial.println(F("Failed to get application IDs!"));
|
|
||||||
Serial.println(mfrc522.GetStatusCodeName(response));
|
|
||||||
mfrc522.PICC_HaltA();
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Dump all applications
|
|
||||||
for (byte aidIndex = 0; aidIndex < applicationCount; aidIndex++) {
|
|
||||||
mfrc522.PICC_DumpMifareDesfireApplication(&tag, &(aids[aidIndex]));
|
|
||||||
}
|
|
||||||
|
|
||||||
// Call PICC_HaltA()
|
|
||||||
mfrc522.PICC_HaltA();
|
|
||||||
Serial.println();
|
|
||||||
}
|
|
Loading…
x
Reference in New Issue
Block a user