Skip to content

Commit

Permalink
Merge pull request #18 from toniebox-reverse-engineering/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
SciLor authored Jul 18, 2021
2 parents b934943 + 5603eae commit 52639e6
Show file tree
Hide file tree
Showing 9 changed files with 216 additions and 7 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
ConfigStatic.h
.vscode/*
15 changes: 15 additions & 0 deletions BoxCLI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ void BoxCLI::begin() {
cmdRFID.setDescription(" Access RFID");
cmdRFID.addFlagArg("u/id");
cmdRFID.addFlagArg("r/ead");
cmdRFID.addFlagArg("m/emory");
cmdRFID.addFlagArg("d/ump");
cmdRFID.addFlagArg("o/verwrite");

cmdLoad = cli.addCmd("load");
cmdLoad.setDescription(" Shows the current load of all threads");
Expand Down Expand Up @@ -280,6 +283,18 @@ void BoxCLI::execRFID() {
Log.error("No tag in place, last known is shown");
}
Box.boxRFID.logUID();
} else if (c.getArg("memory").isSet()) {
Box.boxRFID.loop();
if (!Box.boxRFID.tagActive) {
Log.error("No tag in place");
}
Box.boxRFID.logTagMemory();
} else if (c.getArg("dump").isSet()) {
Box.boxRFID.loop();
if (!Box.boxRFID.tagActive) {
Log.error("No tag in place");
}
Box.boxRFID.dumpTagMemory(c.getArg("overwrite").isSet());
} else {
Log.error("Nothing to do...");
}
Expand Down
27 changes: 24 additions & 3 deletions BoxConfig.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,11 @@ String BoxConfig::getAsJson() {
JsonObject logDoc = doc.createNestedObject("log");
ConfigLog* logCfg = &_config.log;
logDoc["sdLog"] = logCfg->sdLog;

/*
JsonObject miscDoc = doc.createNestedObject("misc");
ConfigMisc* miscCfg = &_config.misc;
miscDoc["autodump"] = miscCfg->autodump;
*/
serializeJson(doc, json);
return json;
}
Expand Down Expand Up @@ -110,17 +114,31 @@ bool BoxConfig::setFromJson(String json) {
ConfigLog* logCfg = &_config.log;
logCfg->sdLog = logDoc["sdLog"].as<bool>();

/*
JsonObject miscDoc = doc["misc"];
ConfigMisc* miscCfg = &_config.misc;
miscCfg->autodump = logDoc["autodump"].as<bool>();
*/

// Convert old config version to latest one.
if (_config.version != CONFIG_ACTIVE_VERSION) {
switch (_config.version) {
case 2:
batteryCfg->criticalAdc = batteryDoc["minimalAdc"].as<uint16_t>();
batteryCfg->lowAdc = batteryCfg->criticalAdc + 100;
_config.version = CONFIG_ACTIVE_VERSION;
_config.version = 3;
write();
break;

/*
case 3:
miscCfg->autodump = false;
_config.version = 4;
write();
break;
*/
default:
_initializeConfig();
write();
break;
}
}
Expand Down Expand Up @@ -153,4 +171,7 @@ void BoxConfig::_initializeConfig() {

ConfigLog* log = &_config.log;
log->sdLog = false;

ConfigMisc* misc = &_config.misc;
misc->autodump = false;
}
21 changes: 19 additions & 2 deletions BoxEvents.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -259,14 +259,30 @@ void BoxEvents::handleTagEvent(BoxRFID::TAG_EVENT event) {
uid[0], uid[1], uid[2], uid[3], uid[4], uid[5], uid[6], uid[7]
);

//Needs tooo much heap memory?!
//if(/*Config.get()->misc.autodump*/) {
if (Box.boxRFID.dumpTagMemory(false)) {
//Box.boxDAC.beep();
//Box.boxLEDs.setActiveAnimationByIteration(BoxLEDs::ANIMATION_TYPE::BLINK, BoxLEDs::CRGB::Yellow, 5);
//Box.boxLEDs.waitForAnimationToFinish();
}
//}

DirFs dir;
char* rcontent = "/rCONTENT";
if (!dir.openDir(rcontent)){
if (!dir.openDir(rcontent)) {
Log.info("Creating missing dir %s...", rcontent);
if (!FatFs.mkdir(rcontent)) {
Log.info("...failed!");
}
}
char* rdump = "/rDUMP";
if (!dir.openDir(rdump)) {
Log.info("Creating missing dir %s...", rdump);
if (!FatFs.mkdir(rdump)) {
Log.info("...failed!");
}
}
if (!dir.openDir((char*)path)) {
Log.info("Creating missing dir %s...", path);
if (!FatFs.mkdir((char*)path)) {
Expand All @@ -290,10 +306,11 @@ void BoxEvents::handleTagEvent(BoxRFID::TAG_EVENT event) {
path,
dir.fileName()
);
free(path);
Box.boxDAC.playFile((const char*)filepath);
free(filepath);
}
}
free(path);
}
break;
case BoxRFID::TAG_EVENT::TAG_REMOVED:
Expand Down
124 changes: 123 additions & 1 deletion BoxRFID.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -360,6 +360,45 @@ void BoxRFID::sendRawSPI(uint8_t* buffer, uint8_t length, bool continuedSend) {

spiDisable();
}
BoxRFID::ISO15693_RESULT BoxRFID::ISO15693_readSingleBlock(uint8_t blockId, uint8_t* blockData) {
uint8_t offset = 0;

trfBuffer[offset++] = 0x02; // ISO15693 flags - ISO15693_REQ_DATARATE_HIGH
trfBuffer[offset++] = 0x20; // Read Single BLock
/*
bool withUid = true;
if (withUid) {
trfBuffer[0] = trfBuffer[0] || 0x20 || 0x10; // ISO15693_REQ_DATARATE_HIGH || ISO15693_REQ_ADDRESS || ISO15693_REQ_OPTION
for (uint8_t i= 0; i<8; i++) {
trfBuffer[offset++] = tagUid[i];
}
}*/
trfBuffer[offset++] = blockId; // BlockId

trfStatus = sendDataTag(&trfBuffer[0], offset);
if (trfStatus == TRF_STATUS::RX_COMPLETE) { // If data has been received
if (trfBuffer[0] == 0x00) { // Confirm "no error" in response flags byte
if (trfRxLength == 5) {
// data Starts at the 2rd received byte, length = 4
for (uint8_t i=0; i<4; i++) {
blockData[i] = trfBuffer[i+1];
}
return ISO15693_RESULT::READ_SINGLE_BLOCK_VALID_RESPONSE;
} else {
Log.error("Received invalid answer. Length should be %i but is %i", 5, trfRxLength);
for (uint8_t i=0; i<trfRxLength; i++) {
Log.printf(" %x", trfBuffer[i]);
}
Log.println();
}
} else {
Log.error("Error flag=%X while reading", trfStatus);
}
} else {
Log.error("Unexpected TRF_STATUS=%X for read single block with id %i", trfStatus, blockId);
}
return ISO15693_RESULT::READ_SINGLE_BLOCK_INVALID_RESPONSE; //TODO
}

BoxRFID::ISO15693_RESULT BoxRFID::ISO15693_sendSingleSlotInventory(uint8_t* uid) {
uint8_t g_ui8TagDetectedCount;
Expand Down Expand Up @@ -637,6 +676,9 @@ void BoxRFID::initRFID() {
}

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

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

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

Expand All @@ -673,4 +715,84 @@ void BoxRFID::logUID() {
uint8_t uid[24];
getUID(uid);
Log.info("RFID UID: %s", uid);
}

uint8_t BoxRFID::readBlocks(uint8_t* data, uint8_t maxBytes) {
BoxRFID::ISO15693_RESULT result;
uint8_t bytesRead = 0;

resetRFID();
initRFID();
writeRegister(REGISTER::CHIP_STATUS_CONTROL, 0b00100001); //turnRfOn();
Box.delayTask(20); //not 1 ms?!

for (uint8_t i=0; i<maxBytes/4; i++) {
result = ISO15693_readSingleBlock(i, &data[i*4]);
if (result != ISO15693_RESULT::READ_SINGLE_BLOCK_VALID_RESPONSE)
break;
bytesRead += 4;
reinitRFID();
}

writeRegister(REGISTER::CHIP_STATUS_CONTROL, 0b00000001); //turnRfOff();
return bytesRead;
}
void BoxRFID::logTagMemory() {
uint8_t data[32];
uint8_t bytesRead;
bytesRead = Box.boxRFID.readBlocks(data, 32);
if (bytesRead == 32) {
Log.disableNewline(true);
Log.info("Reading %i bytes of memory:");
for (uint8_t i = 0; i < bytesRead; i++) {
Log.printf(" %x", data[i]);
}
Log.println();
Log.disableNewline(false);
} else {
Log.error("Expected 32 blocks but got %i...", bytesRead);
}
}

bool BoxRFID::dumpTagMemory(bool overwrite) {
FileFs dumpFile;
uint8_t data[32];
uint8_t bytesRead;
char* path = "rDUMP/0123456789ABCDEF";
sprintf(
(char *)path,
"rDUMP/%02x%02x%02x%02x%02x%02x%02x%02x",
tagUid[7], tagUid[6], tagUid[5], tagUid[4], tagUid[3], tagUid[2], tagUid[1], tagUid[0]
);
if (!overwrite && dumpFile.open((char*)path, FA_OPEN_EXISTING | FA_READ)) {
dumpFile.close();
Log.info("Dump %s exists, skip...", path);
return false;
}

bytesRead = Box.boxRFID.readBlocks(data, 32);
if (bytesRead == 32) {
Log.disableNewline(true);
Log.info("Reading %i bytes of memory:");
for (uint8_t i = 0; i < bytesRead; i++) {
Log.printf(" %x", data[i]);
}
Log.disableNewline(false);
Log.println();
uint8_t mode = FA_CREATE_NEW | FA_WRITE;
if (overwrite)
mode = FA_CREATE_ALWAYS | FA_WRITE;

if (dumpFile.open((char *)path, mode)) {
dumpFile.write(data, bytesRead);
dumpFile.close();
Log.info("Wrote dump to %s", path);
return true;
} else {
Log.error("Could not open %s for writing", path);
}
} else {
Log.error("Expected 32 blocks but got %i...", bytesRead);
}
return false;
}
12 changes: 11 additions & 1 deletion BoxRFID.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ class BoxRFID : public EnhancedThread {
uint8_t tagUid[8];
bool tagActive;

uint8_t readBlocks(uint8_t* data, uint8_t maxBlocks);
void logTagMemory();
bool dumpTagMemory(bool overwrite);

private:
enum class REG_CMD_WORD_BITS : uint8_t {
COMMAND_B7 = 0b10000000,
Expand Down Expand Up @@ -121,7 +125,11 @@ class BoxRFID : public EnhancedThread {

SET_PASSWORD_NO_RESPONSE = 0x30,
SET_PASSWORD_CORRECT = 0x31,
SET_PASSWORD_INCORRECT = 0x32
SET_PASSWORD_INCORRECT = 0x32,

READ_SINGLE_BLOCK_NO_RESPONSE = 0x40,
READ_SINGLE_BLOCK_VALID_RESPONSE = 0x41,
READ_SINGLE_BLOCK_INVALID_RESPONSE = 0x42,
};

void
Expand Down Expand Up @@ -168,6 +176,7 @@ class BoxRFID : public EnhancedThread {
ISO15693_RESULT ISO15693_sendSingleSlotInventory(uint8_t* uid);
ISO15693_RESULT ISO15693_getRandomSlixL(uint8_t* random);
ISO15693_RESULT ISO15693_setPassSlixL(uint8_t pass_id, uint32_t password);
ISO15693_RESULT ISO15693_readSingleBlock(uint8_t blockId, uint8_t* blockData);

void reinitRFID();

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

TRF_STATUS sendDataTag(uint8_t *sendBuffer, uint8_t sendLen);
TRF_STATUS sendDataTag(uint8_t *sendBuffer, uint8_t sendLen, uint8_t txTimeout, uint8_t rxTimeout);
};

#endif
2 changes: 2 additions & 0 deletions BoxTonies.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,10 @@ bool BoxTonies::loadTonieByUid(uint8_t uid[8]) {

memcpy(currentUid, uid, 8);
if (loadTonieByPath(path)) {
free(path);
return true;
}
free(path);
return false;
}

Expand Down
5 changes: 5 additions & 0 deletions ConfigStructures.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,17 @@ typedef struct {
bool sdLog;
} ConfigLog;

typedef struct {
bool autodump;
} ConfigMisc;

typedef struct {
uint8_t version;
ConfigBattery battery;
ConfigButtonEars buttonEars;
ConfigWifi wifi;
ConfigLog log;
ConfigMisc misc;
} ConfigStruct;

#endif
16 changes: 16 additions & 0 deletions WrapperWebServer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,22 @@ void WrapperWebServer::handleAjax(void) {
sampleMemory(6);
_server->send(200, "text/json", json);
return;
} else if (sub.equals("memory")) {
StaticJsonDocument<512> memoryContent; //Size from https://arduinojson.org/v6/assistant/
uint8_t data[32];
uint8_t bytesRead;
bytesRead = Box.boxRFID.readBlocks(data, 32);
if (bytesRead == 32) {
for (uint8_t i=0; i<bytesRead; i++) {
memoryContent[i] = data[i];
}
size_t len = measureJson(memoryContent)+1;
char json[len];
serializeJson(memoryContent, json, len);
sampleMemory(6);
_server->send(200, "text/json", json);
return;
}
}
} else if (cmd.equals("cli")) {
_server->setContentLength(CONTENT_LENGTH_UNKNOWN); // the payload can go on forever
Expand Down

0 comments on commit 52639e6

Please sign in to comment.