Skip to content

Commit 52639e6

Browse files
authored
Merge pull request #18 from toniebox-reverse-engineering/develop
Develop
2 parents b934943 + 5603eae commit 52639e6

File tree

9 files changed

+216
-7
lines changed

9 files changed

+216
-7
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
ConfigStatic.h
2+
.vscode/*

BoxCLI.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,9 @@ void BoxCLI::begin() {
3434
cmdRFID.setDescription(" Access RFID");
3535
cmdRFID.addFlagArg("u/id");
3636
cmdRFID.addFlagArg("r/ead");
37+
cmdRFID.addFlagArg("m/emory");
38+
cmdRFID.addFlagArg("d/ump");
39+
cmdRFID.addFlagArg("o/verwrite");
3740

3841
cmdLoad = cli.addCmd("load");
3942
cmdLoad.setDescription(" Shows the current load of all threads");
@@ -280,6 +283,18 @@ void BoxCLI::execRFID() {
280283
Log.error("No tag in place, last known is shown");
281284
}
282285
Box.boxRFID.logUID();
286+
} else if (c.getArg("memory").isSet()) {
287+
Box.boxRFID.loop();
288+
if (!Box.boxRFID.tagActive) {
289+
Log.error("No tag in place");
290+
}
291+
Box.boxRFID.logTagMemory();
292+
} else if (c.getArg("dump").isSet()) {
293+
Box.boxRFID.loop();
294+
if (!Box.boxRFID.tagActive) {
295+
Log.error("No tag in place");
296+
}
297+
Box.boxRFID.dumpTagMemory(c.getArg("overwrite").isSet());
283298
} else {
284299
Log.error("Nothing to do...");
285300
}

BoxConfig.cpp

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,11 @@ String BoxConfig::getAsJson() {
7373
JsonObject logDoc = doc.createNestedObject("log");
7474
ConfigLog* logCfg = &_config.log;
7575
logDoc["sdLog"] = logCfg->sdLog;
76-
76+
/*
77+
JsonObject miscDoc = doc.createNestedObject("misc");
78+
ConfigMisc* miscCfg = &_config.misc;
79+
miscDoc["autodump"] = miscCfg->autodump;
80+
*/
7781
serializeJson(doc, json);
7882
return json;
7983
}
@@ -110,17 +114,31 @@ bool BoxConfig::setFromJson(String json) {
110114
ConfigLog* logCfg = &_config.log;
111115
logCfg->sdLog = logDoc["sdLog"].as<bool>();
112116

117+
/*
118+
JsonObject miscDoc = doc["misc"];
119+
ConfigMisc* miscCfg = &_config.misc;
120+
miscCfg->autodump = logDoc["autodump"].as<bool>();
121+
*/
122+
113123
// Convert old config version to latest one.
114124
if (_config.version != CONFIG_ACTIVE_VERSION) {
115125
switch (_config.version) {
116126
case 2:
117127
batteryCfg->criticalAdc = batteryDoc["minimalAdc"].as<uint16_t>();
118128
batteryCfg->lowAdc = batteryCfg->criticalAdc + 100;
119-
_config.version = CONFIG_ACTIVE_VERSION;
129+
_config.version = 3;
130+
write();
120131
break;
121-
132+
/*
133+
case 3:
134+
miscCfg->autodump = false;
135+
_config.version = 4;
136+
write();
137+
break;
138+
*/
122139
default:
123140
_initializeConfig();
141+
write();
124142
break;
125143
}
126144
}
@@ -153,4 +171,7 @@ void BoxConfig::_initializeConfig() {
153171

154172
ConfigLog* log = &_config.log;
155173
log->sdLog = false;
174+
175+
ConfigMisc* misc = &_config.misc;
176+
misc->autodump = false;
156177
}

BoxEvents.cpp

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -259,14 +259,30 @@ void BoxEvents::handleTagEvent(BoxRFID::TAG_EVENT event) {
259259
uid[0], uid[1], uid[2], uid[3], uid[4], uid[5], uid[6], uid[7]
260260
);
261261

262+
//Needs tooo much heap memory?!
263+
//if(/*Config.get()->misc.autodump*/) {
264+
if (Box.boxRFID.dumpTagMemory(false)) {
265+
//Box.boxDAC.beep();
266+
//Box.boxLEDs.setActiveAnimationByIteration(BoxLEDs::ANIMATION_TYPE::BLINK, BoxLEDs::CRGB::Yellow, 5);
267+
//Box.boxLEDs.waitForAnimationToFinish();
268+
}
269+
//}
270+
262271
DirFs dir;
263272
char* rcontent = "/rCONTENT";
264-
if (!dir.openDir(rcontent)){
273+
if (!dir.openDir(rcontent)) {
265274
Log.info("Creating missing dir %s...", rcontent);
266275
if (!FatFs.mkdir(rcontent)) {
267276
Log.info("...failed!");
268277
}
269278
}
279+
char* rdump = "/rDUMP";
280+
if (!dir.openDir(rdump)) {
281+
Log.info("Creating missing dir %s...", rdump);
282+
if (!FatFs.mkdir(rdump)) {
283+
Log.info("...failed!");
284+
}
285+
}
270286
if (!dir.openDir((char*)path)) {
271287
Log.info("Creating missing dir %s...", path);
272288
if (!FatFs.mkdir((char*)path)) {
@@ -290,10 +306,11 @@ void BoxEvents::handleTagEvent(BoxRFID::TAG_EVENT event) {
290306
path,
291307
dir.fileName()
292308
);
293-
free(path);
294309
Box.boxDAC.playFile((const char*)filepath);
310+
free(filepath);
295311
}
296312
}
313+
free(path);
297314
}
298315
break;
299316
case BoxRFID::TAG_EVENT::TAG_REMOVED:

BoxRFID.cpp

Lines changed: 123 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -360,6 +360,45 @@ void BoxRFID::sendRawSPI(uint8_t* buffer, uint8_t length, bool continuedSend) {
360360

361361
spiDisable();
362362
}
363+
BoxRFID::ISO15693_RESULT BoxRFID::ISO15693_readSingleBlock(uint8_t blockId, uint8_t* blockData) {
364+
uint8_t offset = 0;
365+
366+
trfBuffer[offset++] = 0x02; // ISO15693 flags - ISO15693_REQ_DATARATE_HIGH
367+
trfBuffer[offset++] = 0x20; // Read Single BLock
368+
/*
369+
bool withUid = true;
370+
if (withUid) {
371+
trfBuffer[0] = trfBuffer[0] || 0x20 || 0x10; // ISO15693_REQ_DATARATE_HIGH || ISO15693_REQ_ADDRESS || ISO15693_REQ_OPTION
372+
for (uint8_t i= 0; i<8; i++) {
373+
trfBuffer[offset++] = tagUid[i];
374+
}
375+
}*/
376+
trfBuffer[offset++] = blockId; // BlockId
377+
378+
trfStatus = sendDataTag(&trfBuffer[0], offset);
379+
if (trfStatus == TRF_STATUS::RX_COMPLETE) { // If data has been received
380+
if (trfBuffer[0] == 0x00) { // Confirm "no error" in response flags byte
381+
if (trfRxLength == 5) {
382+
// data Starts at the 2rd received byte, length = 4
383+
for (uint8_t i=0; i<4; i++) {
384+
blockData[i] = trfBuffer[i+1];
385+
}
386+
return ISO15693_RESULT::READ_SINGLE_BLOCK_VALID_RESPONSE;
387+
} else {
388+
Log.error("Received invalid answer. Length should be %i but is %i", 5, trfRxLength);
389+
for (uint8_t i=0; i<trfRxLength; i++) {
390+
Log.printf(" %x", trfBuffer[i]);
391+
}
392+
Log.println();
393+
}
394+
} else {
395+
Log.error("Error flag=%X while reading", trfStatus);
396+
}
397+
} else {
398+
Log.error("Unexpected TRF_STATUS=%X for read single block with id %i", trfStatus, blockId);
399+
}
400+
return ISO15693_RESULT::READ_SINGLE_BLOCK_INVALID_RESPONSE; //TODO
401+
}
363402

364403
BoxRFID::ISO15693_RESULT BoxRFID::ISO15693_sendSingleSlotInventory(uint8_t* uid) {
365404
uint8_t g_ui8TagDetectedCount;
@@ -637,6 +676,9 @@ void BoxRFID::initRFID() {
637676
}
638677

639678
BoxRFID::TRF_STATUS BoxRFID::sendDataTag(uint8_t *sendBuffer, uint8_t sendLen) {
679+
sendDataTag(sendBuffer, sendLen, 15, 15); //15, 5 vs. 15, 15 (longer timeout for set password)
680+
}
681+
BoxRFID::TRF_STATUS BoxRFID::sendDataTag(uint8_t *sendBuffer, uint8_t sendLen, uint8_t txTimeout, uint8_t rxTimeout) {
640682
uint8_t buffer[sendLen+5];
641683
memcpy(&buffer[5], sendBuffer, sendLen);
642684

@@ -656,7 +698,7 @@ BoxRFID::TRF_STATUS BoxRFID::sendDataTag(uint8_t *sendBuffer, uint8_t sendLen) {
656698
Log.println();*/
657699

658700
sendRaw(&buffer[0], sendLen+5);
659-
TRF_STATUS status = waitRxData(15, 15); //15, 5 vs. 15, 15 (longer timeout for set password)
701+
TRF_STATUS status = waitRxData(txTimeout, rxTimeout);
660702
return status;
661703
}
662704

@@ -673,4 +715,84 @@ void BoxRFID::logUID() {
673715
uint8_t uid[24];
674716
getUID(uid);
675717
Log.info("RFID UID: %s", uid);
718+
}
719+
720+
uint8_t BoxRFID::readBlocks(uint8_t* data, uint8_t maxBytes) {
721+
BoxRFID::ISO15693_RESULT result;
722+
uint8_t bytesRead = 0;
723+
724+
resetRFID();
725+
initRFID();
726+
writeRegister(REGISTER::CHIP_STATUS_CONTROL, 0b00100001); //turnRfOn();
727+
Box.delayTask(20); //not 1 ms?!
728+
729+
for (uint8_t i=0; i<maxBytes/4; i++) {
730+
result = ISO15693_readSingleBlock(i, &data[i*4]);
731+
if (result != ISO15693_RESULT::READ_SINGLE_BLOCK_VALID_RESPONSE)
732+
break;
733+
bytesRead += 4;
734+
reinitRFID();
735+
}
736+
737+
writeRegister(REGISTER::CHIP_STATUS_CONTROL, 0b00000001); //turnRfOff();
738+
return bytesRead;
739+
}
740+
void BoxRFID::logTagMemory() {
741+
uint8_t data[32];
742+
uint8_t bytesRead;
743+
bytesRead = Box.boxRFID.readBlocks(data, 32);
744+
if (bytesRead == 32) {
745+
Log.disableNewline(true);
746+
Log.info("Reading %i bytes of memory:");
747+
for (uint8_t i = 0; i < bytesRead; i++) {
748+
Log.printf(" %x", data[i]);
749+
}
750+
Log.println();
751+
Log.disableNewline(false);
752+
} else {
753+
Log.error("Expected 32 blocks but got %i...", bytesRead);
754+
}
755+
}
756+
757+
bool BoxRFID::dumpTagMemory(bool overwrite) {
758+
FileFs dumpFile;
759+
uint8_t data[32];
760+
uint8_t bytesRead;
761+
char* path = "rDUMP/0123456789ABCDEF";
762+
sprintf(
763+
(char *)path,
764+
"rDUMP/%02x%02x%02x%02x%02x%02x%02x%02x",
765+
tagUid[7], tagUid[6], tagUid[5], tagUid[4], tagUid[3], tagUid[2], tagUid[1], tagUid[0]
766+
);
767+
if (!overwrite && dumpFile.open((char*)path, FA_OPEN_EXISTING | FA_READ)) {
768+
dumpFile.close();
769+
Log.info("Dump %s exists, skip...", path);
770+
return false;
771+
}
772+
773+
bytesRead = Box.boxRFID.readBlocks(data, 32);
774+
if (bytesRead == 32) {
775+
Log.disableNewline(true);
776+
Log.info("Reading %i bytes of memory:");
777+
for (uint8_t i = 0; i < bytesRead; i++) {
778+
Log.printf(" %x", data[i]);
779+
}
780+
Log.disableNewline(false);
781+
Log.println();
782+
uint8_t mode = FA_CREATE_NEW | FA_WRITE;
783+
if (overwrite)
784+
mode = FA_CREATE_ALWAYS | FA_WRITE;
785+
786+
if (dumpFile.open((char *)path, mode)) {
787+
dumpFile.write(data, bytesRead);
788+
dumpFile.close();
789+
Log.info("Wrote dump to %s", path);
790+
return true;
791+
} else {
792+
Log.error("Could not open %s for writing", path);
793+
}
794+
} else {
795+
Log.error("Expected 32 blocks but got %i...", bytesRead);
796+
}
797+
return false;
676798
}

BoxRFID.h

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,10 @@ class BoxRFID : public EnhancedThread {
3030
uint8_t tagUid[8];
3131
bool tagActive;
3232

33+
uint8_t readBlocks(uint8_t* data, uint8_t maxBlocks);
34+
void logTagMemory();
35+
bool dumpTagMemory(bool overwrite);
36+
3337
private:
3438
enum class REG_CMD_WORD_BITS : uint8_t {
3539
COMMAND_B7 = 0b10000000,
@@ -121,7 +125,11 @@ class BoxRFID : public EnhancedThread {
121125

122126
SET_PASSWORD_NO_RESPONSE = 0x30,
123127
SET_PASSWORD_CORRECT = 0x31,
124-
SET_PASSWORD_INCORRECT = 0x32
128+
SET_PASSWORD_INCORRECT = 0x32,
129+
130+
READ_SINGLE_BLOCK_NO_RESPONSE = 0x40,
131+
READ_SINGLE_BLOCK_VALID_RESPONSE = 0x41,
132+
READ_SINGLE_BLOCK_INVALID_RESPONSE = 0x42,
125133
};
126134

127135
void
@@ -168,6 +176,7 @@ class BoxRFID : public EnhancedThread {
168176
ISO15693_RESULT ISO15693_sendSingleSlotInventory(uint8_t* uid);
169177
ISO15693_RESULT ISO15693_getRandomSlixL(uint8_t* random);
170178
ISO15693_RESULT ISO15693_setPassSlixL(uint8_t pass_id, uint32_t password);
179+
ISO15693_RESULT ISO15693_readSingleBlock(uint8_t blockId, uint8_t* blockData);
171180

172181
void reinitRFID();
173182

@@ -180,6 +189,7 @@ class BoxRFID : public EnhancedThread {
180189
void initRFID();
181190

182191
TRF_STATUS sendDataTag(uint8_t *sendBuffer, uint8_t sendLen);
192+
TRF_STATUS sendDataTag(uint8_t *sendBuffer, uint8_t sendLen, uint8_t txTimeout, uint8_t rxTimeout);
183193
};
184194

185195
#endif

BoxTonies.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,10 @@ bool BoxTonies::loadTonieByUid(uint8_t uid[8]) {
1212

1313
memcpy(currentUid, uid, 8);
1414
if (loadTonieByPath(path)) {
15+
free(path);
1516
return true;
1617
}
18+
free(path);
1719
return false;
1820
}
1921

ConfigStructures.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,12 +39,17 @@ typedef struct {
3939
bool sdLog;
4040
} ConfigLog;
4141

42+
typedef struct {
43+
bool autodump;
44+
} ConfigMisc;
45+
4246
typedef struct {
4347
uint8_t version;
4448
ConfigBattery battery;
4549
ConfigButtonEars buttonEars;
4650
ConfigWifi wifi;
4751
ConfigLog log;
52+
ConfigMisc misc;
4853
} ConfigStruct;
4954

5055
#endif

WrapperWebServer.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -273,6 +273,22 @@ void WrapperWebServer::handleAjax(void) {
273273
sampleMemory(6);
274274
_server->send(200, "text/json", json);
275275
return;
276+
} else if (sub.equals("memory")) {
277+
StaticJsonDocument<512> memoryContent; //Size from https://arduinojson.org/v6/assistant/
278+
uint8_t data[32];
279+
uint8_t bytesRead;
280+
bytesRead = Box.boxRFID.readBlocks(data, 32);
281+
if (bytesRead == 32) {
282+
for (uint8_t i=0; i<bytesRead; i++) {
283+
memoryContent[i] = data[i];
284+
}
285+
size_t len = measureJson(memoryContent)+1;
286+
char json[len];
287+
serializeJson(memoryContent, json, len);
288+
sampleMemory(6);
289+
_server->send(200, "text/json", json);
290+
return;
291+
}
276292
}
277293
} else if (cmd.equals("cli")) {
278294
_server->setContentLength(CONTENT_LENGTH_UNKNOWN); // the payload can go on forever

0 commit comments

Comments
 (0)