Skip to content

A Hardware Hacking Tool with Web-Based CLI That Speaks Every Protocol

rework i2 slave to send random data and protect log size

Geo86117cc09f5a
diff --git a/src/Controllers/I2cController.cpp b/src/Controllers/I2cController.cpp
index 459e1708..7e0e5905 100644
--- a/src/Controllers/I2cController.cpp
+++ b/src/Controllers/I2cController.cpp
@@ -195,7 +195,7 @@ void I2cController::handleWrite(const TerminalCommand& cmd) {
}
reg = argTransformer.parseHexOrDec(args[0]);
} else {
- reg = (uint8_t)userInputManager.readValidatedUint32("Register (e.g. 0x00)", 0);
+ reg = (uint8_t)userInputManager.readValidatedHex("Register to write", 0, 0x00, 0xFF);
}
// val
@@ -207,7 +207,7 @@ void I2cController::handleWrite(const TerminalCommand& cmd) {
}
val = argTransformer.parseHexOrDec(args[1]);
} else {
- val = (uint8_t)userInputManager.readValidatedUint32("Value (e.g. 0x1E)", 0);
+ val = (uint8_t)userInputManager.readValidatedHex("Value to write", 0, 0x00, 0xFF);
}
// Ping addr
@@ -258,7 +258,7 @@ void I2cController::handleRead(const TerminalCommand& cmd) {
}
reg = argTransformer.parseHexOrDec(cmd.getArgs());
} else {
- reg = (uint8_t)userInputManager.readValidatedUint32("Register (e.g. 0x00)", 0);
+ reg = (uint8_t)userInputManager.readValidatedHex("Register to read", 0, 0x00, 0xFF);
}
// Check I2C device presence
@@ -335,6 +335,9 @@ void I2cController::handleSlave(const TerminalCommand& cmd) {
// Start slave
i2cService.clearSlaveLog();
i2cService.beginSlave(addr, sda, scl);
+ std::vector<std::string> currentLog;
+ currentLog.reserve(I2cService::SLAVE_LOG_MAX);
+ uint32_t lastLogCount = 0;
std::vector<std::string> lastLog;
while (true) {
@@ -343,18 +346,27 @@ void I2cController::handleSlave(const TerminalCommand& cmd) {
if (key == '\r' || key == '\n') break;
// Get master log from slave and display it
- auto currentLog = i2cService.getSlaveLog();
- if (currentLog.size() > lastLog.size()) {
- for (size_t i = lastLog.size(); i < currentLog.size(); ++i) {
+ currentLog = i2cService.getSlaveLog();
+ uint32_t count = i2cService.getSlaveLogCount();
+ if (count != lastLogCount) {
+ currentLog = i2cService.getSlaveLog();
+
+ uint32_t delta = count - lastLogCount;
+ if (delta > currentLog.size()) delta = currentLog.size(); // cap
+
+ // DIsplay new entries
+ size_t start = currentLog.size() - delta;
+ for (size_t i = start; i < currentLog.size(); ++i) {
terminalView.println(currentLog[i]);
}
- lastLog = currentLog;
+
+ lastLogCount = count;
}
- delay(1);
}
// Close slave
i2cService.endSlave();
+ i2cService.clearSlaveLog();
ensureConfigured();
terminalView.println("\nI2S Slave: Stopped by user.");
}
diff --git a/src/Services/I2cService.cpp b/src/Services/I2cService.cpp
index ecd59b6b..38d4d914 100644
--- a/src/Services/I2cService.cpp
+++ b/src/Services/I2cService.cpp
@@ -128,11 +128,6 @@ bool I2cService::isReadableDevice(uint8_t addr, uint8_t startReg) {
Slave
*/
-std::vector<std::string> I2cService::slaveLog;
-uint8_t I2cService::slaveResponseBuffer[16] = {};
-size_t I2cService::slaveResponseLength = 1;
-portMUX_TYPE I2cService::slaveLogMux = portMUX_INITIALIZER_UNLOCKED;
-
void I2cService::beginSlave(uint8_t address, uint8_t sda, uint8_t scl, uint32_t freq) {
Wire.end();
Wire1.end();
@@ -146,12 +141,6 @@ void I2cService::endSlave() {
Wire1.end();
}
-void I2cService::setSlaveResponse(const uint8_t* data, size_t len) {
- size_t copyLen = (len < sizeof(slaveResponseBuffer)) ? len : sizeof(slaveResponseBuffer);
- memcpy(slaveResponseBuffer, data, copyLen);
- slaveResponseLength = copyLen;
-}
-
std::vector<std::string> I2cService::getSlaveLog() {
portENTER_CRITICAL(&slaveLogMux);
std::vector<std::string> copy = slaveLog;
@@ -159,14 +148,22 @@ std::vector<std::string> I2cService::getSlaveLog() {
return copy;
}
+uint32_t I2cService::getSlaveLogCount() {
+ portENTER_CRITICAL(&slaveLogMux);
+ uint32_t v = slaveLogTotal;
+ portEXIT_CRITICAL(&slaveLogMux);
+ return v;
+}
+
void I2cService::clearSlaveLog() {
portENTER_CRITICAL(&slaveLogMux);
slaveLog.clear();
+ slaveLogWriteIdx = 0;
portEXIT_CRITICAL(&slaveLogMux);
}
void I2cService::onSlaveReceive(int len) {
- std::string entry = "Master wrote:";
+ std::string entry = "Master wrote: 0x";
while (Wire1.available()) {
uint8_t b = Wire1.read();
char hex[5];
@@ -175,16 +172,36 @@ void I2cService::onSlaveReceive(int len) {
}
portENTER_CRITICAL(&slaveLogMux);
- slaveLog.push_back(entry);
+
+ if (slaveLog.size() < SLAVE_LOG_MAX) {
+ slaveLog.push_back(entry);
+ } else {
+ slaveLog[slaveLogWriteIdx] = entry;
+ slaveLogWriteIdx = (slaveLogWriteIdx + 1) % SLAVE_LOG_MAX;
+ }
+ slaveLogTotal++;
+
portEXIT_CRITICAL(&slaveLogMux);
}
void I2cService::onSlaveRequest() {
portENTER_CRITICAL(&slaveLogMux);
- slaveLog.push_back("Master requested read");
+
+ const std::string entry = "Master requested read";
+
+ if (slaveLog.size() < SLAVE_LOG_MAX) {
+ slaveLog.push_back(entry);
+ } else {
+ slaveLog[slaveLogWriteIdx] = entry;
+ slaveLogWriteIdx = (slaveLogWriteIdx + 1) % SLAVE_LOG_MAX;
+ }
+ slaveLogTotal++;
+
portEXIT_CRITICAL(&slaveLogMux);
- Wire1.write(slaveResponseBuffer, slaveResponseLength);
+ // Send counter as data
+ uint8_t v = (uint8_t)(slaveLogTotal & 0xFF);
+ Wire1.write(&v, 1);
}
/*
diff --git a/src/Services/I2cService.h b/src/Services/I2cService.h
index ed3ba88b..a829f326 100644
--- a/src/Services/I2cService.h
+++ b/src/Services/I2cService.h
@@ -34,10 +34,11 @@ public:
bool i2cBitBangRecoverBus(uint8_t scl, uint8_t sda, uint32_t freqHz);
// Slave
+ static constexpr size_t SLAVE_LOG_MAX = 128;
void beginSlave(uint8_t address, uint8_t sda, uint8_t scl, uint32_t freq = 100000);
void endSlave();
- void setSlaveResponse(const uint8_t* data, size_t len);
std::vector<std::string> getSlaveLog();
+ uint32_t getSlaveLogCount();
void clearSlaveLog();
// Glitch
@@ -76,13 +77,13 @@ public:
private:
- static std::vector<std::string> slaveLog;
- static portMUX_TYPE slaveLogMux;
- static uint8_t slaveResponseBuffer[16];
- static size_t slaveResponseLength;
- static I2cService* activeSlaveInstance;
ExternalEEPROM eeprom;
+ bool probeReadableReg(uint8_t addr, uint8_t reg);
+
static void onSlaveReceive(int len);
static void onSlaveRequest();
- bool probeReadableReg(uint8_t addr, uint8_t reg);
+ inline static std::vector<std::string> slaveLog = {};
+ inline static size_t slaveLogWriteIdx = 0;
+ inline static uint32_t slaveLogTotal = 0;
+ inline static portMUX_TYPE slaveLogMux = portMUX_INITIALIZER_UNLOCKED;
};