RFID: Implement tag programming
The software needs tag data to be able to send programming requests, so go through the active tags and extract the data in the new Com structs. programTag() handles tag wakeup and selection, so might fail if the requested tag is on another reader. This allows chaining them together and not keeping track of which tag which reader sees. Add an assert in case the communication buffer would get too small to send all active tags as the protocol does not define a multi-message response.
This commit is contained in:
parent
b5b5268b79
commit
38e4982aac
2 changed files with 51 additions and 0 deletions
|
@ -15,6 +15,7 @@
|
||||||
#include <MFRC522DriverPinSimple.h>
|
#include <MFRC522DriverPinSimple.h>
|
||||||
#include <MFRC522DriverSPI.h>
|
#include <MFRC522DriverSPI.h>
|
||||||
|
|
||||||
|
#include "Com.h"
|
||||||
#include "IDs.h"
|
#include "IDs.h"
|
||||||
#include "Pinout.h"
|
#include "Pinout.h"
|
||||||
|
|
||||||
|
@ -50,6 +51,21 @@ public:
|
||||||
* @return 0 if no change, ID if new tag detected, ID & sign bit if the tag left.
|
* @return 0 if no change, ID if new tag detected, ID & sign bit if the tag left.
|
||||||
*/
|
*/
|
||||||
int8_t checkTags();
|
int8_t checkTags();
|
||||||
|
|
||||||
|
/***
|
||||||
|
* Populate an already allocated array of sufficient size to fit
|
||||||
|
* maxTags number of TagInfoRecords.
|
||||||
|
* @param tagData Pointer to an array of size maxTags*sizeof(Com::TagInfoRecord)
|
||||||
|
* @return Number of tags actually put in the array.
|
||||||
|
*/
|
||||||
|
uint8_t gatherTagInfo(Com::TagInfoRecord* tagData);
|
||||||
|
|
||||||
|
/***
|
||||||
|
* Change the ID stored in a tag so that it matches tagToProgram.
|
||||||
|
* @param tagToProgram Pointer to an existing struct with the data to program.
|
||||||
|
* @return True if the programming succeeded.
|
||||||
|
*/
|
||||||
|
bool programTag(Com::TagInfoRecord* tagToProgram);
|
||||||
private:
|
private:
|
||||||
static const byte tagIdBlock = 0x11;
|
static const byte tagIdBlock = 0x11;
|
||||||
/* Used for "encrypted" communication with the tags. */
|
/* Used for "encrypted" communication with the tags. */
|
||||||
|
|
35
src/RFID.cpp
35
src/RFID.cpp
|
@ -4,6 +4,9 @@
|
||||||
|
|
||||||
#include "RFID.h"
|
#include "RFID.h"
|
||||||
|
|
||||||
|
static_assert(RFID::maxTags * sizeof(Com::TagInfoRecord) <= 62,
|
||||||
|
"RFID::maxTags too high, protocol cannot support it.\n");
|
||||||
|
|
||||||
MFRC522Constants::MIFARE_Key RFID::defaultKey = {0xFF,0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
|
MFRC522Constants::MIFARE_Key RFID::defaultKey = {0xFF,0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
|
||||||
int RFID::currentActiveTags = 0;
|
int RFID::currentActiveTags = 0;
|
||||||
|
|
||||||
|
@ -68,6 +71,38 @@ int8_t RFID::checkTags() {
|
||||||
return newTag->tag_ID;
|
return newTag->tag_ID;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
uint8_t RFID::gatherTagInfo(Com::TagInfoRecord *tagData) {
|
||||||
|
int tagCount = 0;
|
||||||
|
for (uidNode* node = activeTags; node; node = node->next) {
|
||||||
|
// The protocol does not support UIDs other than 4 bytes, so assume it
|
||||||
|
// is the case here directly.
|
||||||
|
tagData[tagCount].uid = *(uint32_t*)(node->uid.uidByte);
|
||||||
|
tagData[tagCount].figId = node->tag_ID;
|
||||||
|
tagCount++;
|
||||||
|
}
|
||||||
|
return tagCount;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool RFID::programTag(Com::TagInfoRecord *tagToProgram) {
|
||||||
|
|
||||||
|
byte comSize = sizeof(comData);
|
||||||
|
|
||||||
|
mfrc.uid.size = 4;
|
||||||
|
*(uint32_t*)(mfrc.uid.uidByte) = tagToProgram->uid;
|
||||||
|
mfrc.PICC_WakeupA(comData, &comSize);
|
||||||
|
if (mfrc.PICC_Select(&mfrc.uid, mfrc.uid.size) != MFRC522Constants::STATUS_OK) {
|
||||||
|
Serial.println("Failed to wakeup for programming. Is tag still there ?\n");
|
||||||
|
mfrc.PICC_HaltA();
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!writeBlock(mfrc.uid, tagIdBlock, tagToProgram->figId)) {
|
||||||
|
Serial.println("Failed to program tag.");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
uidNode* RFID::addActiveTag(const MFRC522Constants::Uid &newTag) {
|
uidNode* RFID::addActiveTag(const MFRC522Constants::Uid &newTag) {
|
||||||
if (!nextFreeTagSlot) {
|
if (!nextFreeTagSlot) {
|
||||||
return nullptr;
|
return nullptr;
|
||||||
|
|
Loading…
Add table
Reference in a new issue