Skip to content

Commit

Permalink
Merge pull request #344 from rennancockles/dev
Browse files Browse the repository at this point in the history
rfid scan mode and write custom uid
  • Loading branch information
pr3y authored Oct 16, 2024
2 parents 61982f9 + 56bd04a commit 5886967
Show file tree
Hide file tree
Showing 3 changed files with 116 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/core/menu_items/RFIDMenu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ void RFIDMenu::optionsMenu() {
options = {
{"Read tag", [=]() { TagOMatic(); }},
{"Read 125kHz", [=]() { RFID125(); }},
{"Scan tags", [=]() { TagOMatic(TagOMatic::SCAN_MODE); }},
{"Load file", [=]() { TagOMatic(TagOMatic::LOAD_MODE); }},
{"Erase data", [=]() { TagOMatic(TagOMatic::ERASE_MODE); }},
{"Write NDEF", [=]() { TagOMatic(TagOMatic::WRITE_NDEF_MODE); }},
Expand Down
106 changes: 106 additions & 0 deletions src/modules/rfid/tag_o_matic.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#include "PN532.h"

#define NDEF_DATA_SIZE 100
#define SCAN_DUMP_SIZE 5


TagOMatic::TagOMatic() {
Expand Down Expand Up @@ -77,12 +78,18 @@ void TagOMatic::loop() {
case READ_MODE:
read_card();
break;
case SCAN_MODE:
scan_cards();
break;
case LOAD_MODE:
load_file();
break;
case CLONE_MODE:
clone_card();
break;
case CUSTOM_UID_MODE:
write_custom_uid();
break;
case WRITE_MODE:
write_data();
break;
Expand All @@ -104,10 +111,12 @@ void TagOMatic::select_state() {
options = {};
if (_read_uid) {
options.push_back({"Clone UID", [=]() { set_state(CLONE_MODE); }});
options.push_back({"Custom UID", [=]() { set_state(CUSTOM_UID_MODE); }});
options.push_back({"Write data", [=]() { set_state(WRITE_MODE); }});
options.push_back({"Save file", [=]() { set_state(SAVE_MODE); }});
}
options.push_back({"Read tag", [=]() { set_state(READ_MODE); }});
options.push_back({"Scan tags", [=]() { set_state(SCAN_MODE); }});
options.push_back({"Load file", [=]() { set_state(LOAD_MODE); }});
options.push_back({"Write NDEF", [=]() { set_state(WRITE_NDEF_MODE); }});
options.push_back({"Erase tag", [=]() { set_state(ERASE_MODE); }});
Expand All @@ -118,11 +127,20 @@ void TagOMatic::select_state() {
void TagOMatic::set_state(RFID_State state) {
current_state = state;
display_banner();
if (_scanned_set.size()>0) {
save_scan_result();
_scanned_set.clear();
_scanned_tags.clear();
}
switch (state) {
case READ_MODE:
case LOAD_MODE:
_read_uid = false;
break;
case SCAN_MODE:
_scanned_set.clear();
_scanned_tags.clear();
break;
case CLONE_MODE:
padprintln("New UID: " + _rfid->printableUID.uid);
padprintln("SAK: " + _rfid->printableUID.sak);
Expand All @@ -138,6 +156,7 @@ void TagOMatic::set_state(RFID_State state) {
break;
case SAVE_MODE:
case ERASE_MODE:
case CUSTOM_UID_MODE:
break;
}
delay(300);
Expand All @@ -151,6 +170,10 @@ void TagOMatic::display_banner() {
padprintln(" READ MODE");
padprintln(" ---------");
break;
case SCAN_MODE:
padprintln(" SCAN MODE");
padprintln(" ---------");
break;
case LOAD_MODE:
padprintln(" LOAD MODE");
padprintln(" ---------");
Expand All @@ -159,6 +182,10 @@ void TagOMatic::display_banner() {
padprintln(" CLONE MODE");
padprintln(" ----------");
break;
case CUSTOM_UID_MODE:
padprintln(" CUSTOM UID MODE");
padprintln(" ---------------");
break;
case ERASE_MODE:
padprintln(" ERASE MODE");
padprintln(" ----------");
Expand Down Expand Up @@ -208,6 +235,13 @@ void TagOMatic::dump_ndef_details() {
padprintln("Payload size: " + String(_rfid->ndefMessage.payloadSize) + " bytes");
}

void TagOMatic::dump_scan_results() {
for (int i = _scanned_tags.size(); i > 0; i--) {
if (_scanned_tags.size() > SCAN_DUMP_SIZE && i <= _scanned_tags.size()-SCAN_DUMP_SIZE) return;
padprintln(String(i) + ": " + _scanned_tags[i-1]);
}
}

void TagOMatic::read_card() {
if (_rfid->read() != RFIDInterface::SUCCESS) return;

Expand All @@ -218,6 +252,21 @@ void TagOMatic::read_card() {
delay(500);
}

void TagOMatic::scan_cards() {
if (_rfid->read() != RFIDInterface::SUCCESS) return;

if (_scanned_set.find(_rfid->printableUID.uid) == _scanned_set.end()) {
Serial.println("New tag found: " + _rfid->printableUID.uid);
_scanned_set.insert(_rfid->printableUID.uid);
_scanned_tags.push_back(_rfid->printableUID.uid);
}

display_banner();
dump_scan_results();

delay(200);
}

void TagOMatic::clone_card() {
int result = _rfid->clone();

Expand All @@ -243,6 +292,33 @@ void TagOMatic::clone_card() {
set_state(READ_MODE);
}

void TagOMatic::write_custom_uid() {
String custom_uid = keyboard("", _rfid->uid.size * 2, "UID (hex):");

custom_uid.trim();
custom_uid.replace(" ", "");
custom_uid.toUpperCase();

display_banner();

if (custom_uid.length() != _rfid->uid.size * 2) {
displayError("Invalid UID.");
delay(1000);
set_state(READ_MODE);
return;
}

_rfid->printableUID.uid = "";
for (size_t i = 0; i < custom_uid.length(); i += 2) {
_rfid->uid.uidByte[i / 2] = strtoul(custom_uid.substring(i, i + 2).c_str(), NULL, 16);
_rfid->printableUID.uid += custom_uid.substring(i, i + 2) + " ";
}
_rfid->printableUID.uid.trim();

delay(200);
set_state(CLONE_MODE);
}

void TagOMatic::erase_card() {
int result = _rfid->erase();

Expand Down Expand Up @@ -415,3 +491,33 @@ void TagOMatic::save_file() {
delay(1000);
set_state(READ_MODE);
}

void TagOMatic::save_scan_result() {
FS *fs;
if(!getFsStorage(fs)) return;

String filename = "scan_result";

if (!(*fs).exists("/BruceRFID")) (*fs).mkdir("/BruceRFID");
if (!(*fs).exists("/BruceRFID/Scans")) (*fs).mkdir("/BruceRFID/Scans");
if ((*fs).exists("/BruceRFID/Scans/" + filename + ".rfidscan")) {
int i = 1;
filename += "_";
while((*fs).exists("/BruceRFID/Scans/" + filename + String(i) + ".rfidscan")) i++;
filename += String(i);
}
File file = (*fs).open("/BruceRFID/Scans/"+ filename + ".rfidscan", FILE_WRITE);

if(!file) {
return;
}

file.println("Filetype: Bruce RFID Scan Result");
for (String uid : _scanned_tags) {
file.println(uid);
}

file.close();
delay(100);
return;
}
9 changes: 9 additions & 0 deletions src/modules/rfid/tag_o_matic.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,16 @@
#define __TAG_O_MATIC_H__

#include "RFIDInterface.h"
#include <set>


class TagOMatic {
public:
enum RFID_State {
READ_MODE,
SCAN_MODE,
CLONE_MODE,
CUSTOM_UID_MODE,
WRITE_MODE,
WRITE_NDEF_MODE,
ERASE_MODE,
Expand Down Expand Up @@ -45,13 +48,16 @@ class TagOMatic {
bool _read_uid = false;
bool _ndef_created = false;
RFID_State current_state;
std::set<String> _scanned_set;
std::vector<String> _scanned_tags;

/////////////////////////////////////////////////////////////////////////////////////
// Display functions
/////////////////////////////////////////////////////////////////////////////////////
void display_banner();
void dump_card_details();
void dump_ndef_details();
void dump_scan_results();

/////////////////////////////////////////////////////////////////////////////////////
// State management
Expand All @@ -63,11 +69,14 @@ class TagOMatic {
// Operations
/////////////////////////////////////////////////////////////////////////////////////
void read_card();
void scan_cards();
void write_custom_uid();
void clone_card();
void erase_card();
void write_data();
void write_ndef_data();
void save_file();
void save_scan_result();
void load_file();

/////////////////////////////////////////////////////////////////////////////////////
Expand Down

0 comments on commit 5886967

Please sign in to comment.