Skip to content

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

add slave and dump for I2C

Geo82c45f3a5313
diff --git a/src/Controllers/I2cController.cpp b/src/Controllers/I2cController.cpp
index 01268b53..3a78726c 100644
--- a/src/Controllers/I2cController.cpp
+++ b/src/Controllers/I2cController.cpp
@@ -29,6 +29,14 @@ void I2cController::handleCommand(const TerminalCommand& cmd) {
handleRead(cmd);
}
+ else if (cmd.getRoot() == "dump") {
+ handleDump(cmd);
+ }
+
+ else if (cmd.getRoot() == "slave") {
+ handleSlave(cmd);
+ }
+
else if (cmd.getRoot() == "config") {
handleConfig();
}
@@ -268,6 +276,234 @@ void I2cController::handleConfig() {
terminalView.println("I2C configured.\n");
}
+/*
+Slave
+*/
+void I2cController::handleSlave(const TerminalCommand& cmd) {
+ if (!argTransformer.isValidNumber(cmd.getSubcommand())) {
+ terminalView.println("Usage: slave <addr>");
+ return;
+ }
+
+ // Parse arg
+ uint8_t addr = argTransformer.parseHexOrDec(cmd.getSubcommand());
+ uint8_t sda = state.getI2cSdaPin();
+ uint8_t scl = state.getI2cSclPin();
+
+ // Validate arg
+ if (addr < 0x08 || addr > 0x77) {
+ terminalView.println("I2C Slave: Invalid address. Must be between 0x08 and 0x77.");
+ return;
+ }
+
+ terminalView.println("I2C Slave: Listening on address 0x" + argTransformer.toHex(addr) +
+ "... Press [ENTER] to stop.\n");
+
+ // Start slave
+ i2cService.clearSlaveLog();
+ i2cService.beginSlave(addr, sda, scl);
+
+ std::vector<std::string> lastLog;
+ while (true) {
+ // Enter press
+ char key = terminalInput.readChar();
+ 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) {
+ terminalView.println(currentLog[i]);
+ }
+ lastLog = currentLog;
+ }
+ delay(1);
+ }
+
+ // Close slave
+ i2cService.endSlave();
+ ensureConfigured();
+ terminalView.println("\nI2S Slave: Stopped by user.");
+}
+
+/*
+Dump
+*/
+void I2cController::handleDump(const TerminalCommand& cmd) {
+ // Validate sub
+ if (!argTransformer.isValidNumber(cmd.getSubcommand())) {
+ terminalView.println("Usage: dump <addr> [length]");
+ return;
+ }
+
+ // Parse addr
+ uint8_t addr = argTransformer.parseHexOrDec(cmd.getSubcommand());
+ uint16_t start = 0x00;
+ uint16_t len = 256;
+
+ // Check I2C device presence
+ i2cService.beginTransmission(addr);
+ if (i2cService.endTransmission()) {
+ terminalView.println("I2C Dump: No device found at " + cmd.getSubcommand());
+ return;
+ }
+
+ // Validate and parse arg
+ auto args = argTransformer.splitArgs(cmd.getArgs());
+ if (args.size() >= 1 && argTransformer.isValidNumber(args[0])) {
+ len = argTransformer.parseHexOrDec16(args[0]);
+ if (len > 256) len = 256;
+ }
+
+ std::vector<uint8_t> values(len, 0xFF);
+ std::vector<bool> valid(len, false);
+
+ // Device registers are readable
+ if (i2cService.isReadableDevice(addr, start)) {
+ terminalView.println("I2C Dump: 0x" + argTransformer.toHex(addr) +
+ " from 0x" + argTransformer.toHex(start) +
+ " for " + std::to_string(len) + " bytes... Press [ENTER] to stop.\n");
+
+ performRegisterRead(addr, start, len, values, valid);
+
+ // Not readable
+ } else {
+ terminalView.println("I2C Dump: Device at 0x" + argTransformer.toHex(addr) +
+ " may not use standard register access — trying raw read...");
+
+ performRawRead(addr, start, len, values, valid);
+ }
+
+ // Not able to read any data
+ if (std::all_of(valid.begin(), valid.end(), [](bool b) { return !b; })) {
+ terminalView.println("I2C Dump: Unable to read any data — device NACKed or unsupported protocol.\n");
+ return;
+ }
+
+ printHexDump(start, len, values, valid);
+}
+
+void I2cController::performRegisterRead(uint8_t addr, uint16_t start, uint16_t len,
+ std::vector<uint8_t>& values, std::vector<bool>& valid) {
+ const uint8_t CHUNK_SIZE = 16;
+ int consecutiveErrors = 0;
+
+ for (uint16_t offset = 0; offset < len; offset += CHUNK_SIZE) {
+ if (consecutiveErrors >= 3) {
+ terminalView.println("I2C Dump: Aborted after 3 consecutive errors.");
+ return;
+ }
+
+ uint8_t reg = start + offset;
+ uint8_t toRead = (offset + CHUNK_SIZE <= len) ? CHUNK_SIZE : (len - offset);
+
+ // Write start register
+ i2cService.beginTransmission(addr);
+ i2cService.write(reg);
+ bool writeOk = (i2cService.endTransmission(false) == 0); // No stop
+ if (!writeOk) {
+ consecutiveErrors++;
+ continue;
+ }
+
+ // Read chunk
+ uint8_t received = i2cService.requestFrom(addr, toRead, true);
+ if (received == toRead) {
+ for (uint8_t i = 0; i < toRead; ++i) {
+ auto key = terminalInput.readChar();
+ if (key == '\r' || key == '\n') {
+ terminalView.println("I2C Dump: Cancelled by user.");
+ return;
+ }
+
+ if (i2cService.available()) {
+ values[offset + i] = i2cService.read();
+ valid[offset + i] = true;
+ }
+ }
+ consecutiveErrors = 0;
+ } else {
+ while (i2cService.available()) i2cService.read(); // Flush
+ consecutiveErrors++;
+ }
+
+ delay(1);
+ }
+}
+
+void I2cController::performRawRead(uint8_t addr, uint16_t start,
+ uint16_t len,
+ std::vector<uint8_t>& values,
+ std::vector<bool>& valid) {
+ values.assign(len, 0xFF);
+ valid.assign(len, false);
+
+ terminalView.println("I2C Dump: Trying read raw...");
+
+ // Write start register
+ i2cService.beginTransmission(addr);
+ i2cService.write(start);
+ if (i2cService.endTransmission(false) != 0) {
+ return; // NACK
+ }
+
+ // Read len from register addr
+ uint16_t received = i2cService.requestFrom(addr, (uint8_t)len, true);
+ for (uint16_t i = 0; i < received && i < len; ++i) {
+ auto key = terminalInput.readChar();
+ if (key == '\r' || key == '\n') {
+ terminalView.println("I2C Dump: Cancelled by user.");
+ return;
+ }
+ if (i2cService.available()) {
+ values[i] = i2cService.read();
+ valid[i] = true;
+ }
+ }
+
+ while (i2cService.available()) i2cService.read();
+}
+
+void I2cController::printHexDump(uint16_t start, uint16_t len,
+ const std::vector<uint8_t>& values, const std::vector<bool>& valid) {
+ for (uint16_t lineStart = 0; lineStart < len; lineStart += 16) {
+ std::string line;
+ char addrStr[8];
+ snprintf(addrStr, sizeof(addrStr), "%02X:", start + lineStart);
+ line += addrStr;
+
+ for (uint8_t i = 0; i < 16; ++i) {
+ uint16_t idx = lineStart + i;
+ if (idx < len) {
+ if (valid[idx]) {
+ char hex[4];
+ snprintf(hex, sizeof(hex), " %02X", values[idx]);
+ line += hex;
+ } else {
+ line += " ??";
+ }
+ } else {
+ line += " ";
+ }
+ }
+
+ line += " ";
+
+ for (uint8_t i = 0; i < 16; ++i) {
+ uint16_t idx = lineStart + i;
+ if (idx < len && valid[idx]) {
+ char c = values[idx];
+ line += (c >= 32 && c <= 126) ? c : '.';
+ } else {
+ line += '.';
+ }
+ }
+
+ terminalView.println(line);
+ }
+ terminalView.println("");
+}
+
/*
Help
*/
@@ -276,8 +512,10 @@ void I2cController::handleHelp() {
terminalView.println(" scan");
terminalView.println(" ping <addr>");
terminalView.println(" sniff");
+ terminalView.println(" slave <addr>");
terminalView.println(" read <addr> <reg>");
terminalView.println(" write <addr> <reg> <val>");
+ terminalView.println(" dump <addr> [len]");
terminalView.println(" config");
terminalView.println(" raw instructions, e.g: [0x13 0x4B r:8]");
}
diff --git a/src/Controllers/I2cController.h b/src/Controllers/I2cController.h
index c8af6c8f..66fb9e86 100644
--- a/src/Controllers/I2cController.h
+++ b/src/Controllers/I2cController.h
@@ -53,12 +53,21 @@ private:
// Write data to an I2C device
void handleWrite(const TerminalCommand& cmd);
+ // Emulate I2C slave device logging master command
+ void handleSlave(const TerminalCommand& cmd);
+
// Configure I2C parameters
void handleConfig();
// Show I2C help message
void handleHelp();
- // Read user input from terminal
- std::string getUserInput();
+ // Dump I2C registers content
+ void handleDump(const TerminalCommand& cmd);
+ void performRegisterRead(uint8_t addr, uint16_t, uint16_t len,
+ std::vector<uint8_t>& values, std::vector<bool>& valid);
+ void performRawRead(uint8_t addr, uint16_t, uint16_t len,
+ std::vector<uint8_t>& values, std::vector<bool>& valid);
+ void printHexDump(uint16_t, uint16_t len,
+ const std::vector<uint8_t>& values, const std::vector<bool>& valid);
};
diff --git a/src/Controllers/UtilityController.cpp b/src/Controllers/UtilityController.cpp
index 0a6e9c8b..a3e768f4 100644
--- a/src/Controllers/UtilityController.cpp
+++ b/src/Controllers/UtilityController.cpp
@@ -282,8 +282,10 @@ void UtilityController::handleHelp() {
terminalView.println(" scan - Find devices");
terminalView.println(" ping <addr> - Check ACK");
terminalView.println(" sniff - View traffic");
+ terminalView.println(" slave <addr> - Emulate I2C device");
terminalView.println(" read <addr> <reg> - Read register");
terminalView.println(" write <a> <r> <val> - Write register");
+ terminalView.println(" dump <addr> [len] - Read all registers");
terminalView.println(" config - Configure settings");
terminalView.println(" [0x13 0x4B 0x1] - Instruction syntax");
diff --git a/src/Services/I2cService.cpp b/src/Services/I2cService.cpp
index b25d0421..1985a71a 100644
--- a/src/Services/I2cService.cpp
+++ b/src/Services/I2cService.cpp
@@ -110,3 +110,78 @@ std::string I2cService::executeByteCode(const std::vector<ByteCode>& bytecodes)
return result;
}
+bool I2cService::isReadableDevice(uint8_t addr, uint8_t startReg) {
+ beginTransmission(addr);
+ write(startReg);
+ bool writeOk = (endTransmission(false) == 0);
+ if (!writeOk) return false;
+
+ uint8_t received = requestFrom(addr, 1, true);
+ if (received != 1 || !available()) return false;
+
+ read(); // read to flush
+ return true;
+}
+
+
+/*
+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();
+ Wire1.begin(address, sda, scl, freq);
+
+ Wire1.onReceive(onSlaveReceive);
+ Wire1.onRequest(onSlaveRequest);
+}
+
+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;
+ portEXIT_CRITICAL(&slaveLogMux);
+ return copy;
+}
+
+void I2cService::clearSlaveLog() {
+ portENTER_CRITICAL(&slaveLogMux);
+ slaveLog.clear();
+ portEXIT_CRITICAL(&slaveLogMux);
+}
+
+void I2cService::onSlaveReceive(int len) {
+ std::string entry = "Master wrote:";
+ while (Wire1.available()) {
+ uint8_t b = Wire1.read();
+ char hex[5];
+ snprintf(hex, sizeof(hex), " %02X", b);
+ entry += hex;
+ }
+
+ portENTER_CRITICAL(&slaveLogMux);
+ slaveLog.push_back(entry);
+ portEXIT_CRITICAL(&slaveLogMux);
+}
+
+void I2cService::onSlaveRequest() {
+ portENTER_CRITICAL(&slaveLogMux);
+ slaveLog.push_back("Master requested read");
+ portEXIT_CRITICAL(&slaveLogMux);
+
+ Wire1.write(slaveResponseBuffer, slaveResponseLength);
+}
\ No newline at end of file
diff --git a/src/Services/I2cService.h b/src/Services/I2cService.h
index 0ecd1614..2a0916fa 100644
--- a/src/Services/I2cService.h
+++ b/src/Services/I2cService.h
@@ -17,6 +17,23 @@ public:
int read();
bool available() const;
bool end() const;
+ bool isReadableDevice(uint8_t addr, uint8_t startReg);
+
+ 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();
+ void clearSlaveLog();
std::string executeByteCode(const std::vector<ByteCode>& bytecodes);
+
+private:
+ static std::vector<std::string> slaveLog;
+ static portMUX_TYPE slaveLogMux;
+ static uint8_t slaveResponseBuffer[16];
+ static size_t slaveResponseLength;
+ static I2cService* activeSlaveInstance;
+
+ static void onSlaveReceive(int len);
+ static void onSlaveRequest();
};
diff --git a/src/Services/I2sService.h b/src/Services/I2sService.h
index 6bdbfbb3..f99fe4e6 100644
--- a/src/Services/I2sService.h
+++ b/src/Services/I2sService.h
@@ -17,8 +17,7 @@ public:
void playPcm(const int16_t* data, size_t numBytes);
size_t recordSamples(int16_t* outBuffer, size_t sampleCount);
void end();
- bool isInitialized() const;
-
+ bool isInitialized() const;
private:
bool initialized = false;
i2s_port_t port = I2S_NUM_0;
diff --git a/src/Services/LedService.cpp b/src/Services/LedService.cpp
index 89537559..a151a4f9 100644
--- a/src/Services/LedService.cpp
+++ b/src/Services/LedService.cpp
@@ -3,7 +3,7 @@
#include <Enums/LedProtocolEnum.h>
#include <Enums/LedChipsetEnum.h>
-extern CFastLED FastLED; // déclarer FastLED global explicitement
+extern CFastLED FastLED; // declare FastLED as global
LedService::LedService() {}
diff --git a/src/Transformers/ArgTransformer.cpp b/src/Transformers/ArgTransformer.cpp
index a0d20ecf..c4d80f82 100644
--- a/src/Transformers/ArgTransformer.cpp
+++ b/src/Transformers/ArgTransformer.cpp
@@ -60,6 +60,11 @@ uint8_t ArgTransformer::parseHexOrDec(const std::string& str) const {
return static_cast<uint8_t>(value);
}
+uint16_t ArgTransformer::parseHexOrDec16(const std::string& str) const {
+ uint32_t value = parseHexOrDec32(str);
+ return static_cast<uint16_t>(value & 0xFFFF);
+}
+
uint32_t ArgTransformer::parseHexOrDec32(const std::string& str) const {
if (str.empty()) return 0;
@@ -83,6 +88,8 @@ uint32_t ArgTransformer::parseHexOrDec32(const std::string& str) const {
return static_cast<uint32_t>(value);
}
+
+
std::vector<std::string> ArgTransformer::splitArgs(const std::string& input) {
std::vector<std::string> result;
std::istringstream iss(input);
diff --git a/src/Transformers/ArgTransformer.h b/src/Transformers/ArgTransformer.h
index 57cd6b3b..791188cd 100644
--- a/src/Transformers/ArgTransformer.h
+++ b/src/Transformers/ArgTransformer.h
@@ -15,6 +15,7 @@ public:
std::vector<uint8_t> parseByteList(const std::string& input) const;
uint8_t parseByte(const std::string& str, int index = -1) const;
uint8_t parseHexOrDec(const std::string& s) const ;
+ uint16_t parseHexOrDec16(const std::string& str) const;
uint32_t parseHexOrDec32(const std::string& s) const ;
bool parseInt(const std::string& input, int& output);
std::vector<std::string> splitArgs(const std::string& input);