A Hardware Hacking Tool with Web-Based CLI That Speaks Every Protocol
C arduinobluetoothcan-busdebuggingeepromesp32flipperzerogpiohardware-hackingi2cjtagopenocdprotocolspwmradiorfidspisubghzuartwifi
rework uart scan to detect correct baudrate
Geo8655d08321a0
diff --git a/src/Controllers/OneWireController.cpp b/src/Controllers/OneWireController.cppindex 0ed830a8..d1522412 100644--- a/src/Controllers/OneWireController.cpp+++ b/src/Controllers/OneWireController.cpp@@ -101,7 +101,7 @@ void OneWireController::handlePing() {Read*/void OneWireController::handleRead() {- terminalView.println("OneWire Read: press ENTER to stop.\n");+ terminalView.println("OneWire Read: Press ENTER to stop.\n");while (true) {auto key = terminalInput.readChar();diff --git a/src/Controllers/UartController.cpp b/src/Controllers/UartController.cppindex 9f539c10..3ace911f 100644--- a/src/Controllers/UartController.cpp+++ b/src/Controllers/UartController.cpp@@ -180,72 +180,119 @@ void UartController::handlePing() {Scan*/void UartController::handleScan() {- std::vector<int> baudrates = {9600, 19200, 38400, 57600, 115200, 230400, 460800, 921600};-terminalView.println("UART Scan: in progress... (press ENTER to cancel)");+ terminalView.println("");+ terminalView.println("[INFOS]");+ terminalView.println(" The UART scanner attempts to detect the correct baudrate");+ terminalView.println(" by iteratively switching speeds, sending predefined probes");+ terminalView.println("");+uartService.clearUartBuffer();for (int baud : baudrates) {- uartService.switchBaudrate(baud);- terminalView.println("→ Testing baudrate " + std::to_string(baud));-- std::string response;- size_t asciiCount = 0;- size_t probeIndex = 0;- unsigned long start = millis();- unsigned long lastSend = 0;-- uartService.flush();- while (millis() - start < 1000) {- // Cancel if user presses Enter- char key = terminalInput.readChar();- if (key == '\r' || key == '\n') {- terminalView.println("UART Scan: Cancelled by user.");- return;- }+ if (scanAtBaudrate(baud)) {+ state.setUartBaudRate(baud);+ uartService.switchBaudrate(baud);+ terminalView.println("");+ terminalView.println("UART Scan: Setting baudrate to UART config.");+ terminalView.println("UART Scan: Baudrate detected " + std::to_string(baud));+ terminalView.println("");+ return;+ }+ }- // Send probes to get uart data- if (probeIndex < probes.size()) {- uartService.write(probes[probeIndex]);- probeIndex++;- lastSend = millis();- }+ uartService.switchBaudrate(state.getUartBaudRate()); // restore previous+ terminalView.println("Uart Scan: No device detected.");+ terminalView.println("");+}- // Read incoming UART data- while (uartService.available() > 0) {- char c = uartService.read();- response += c;+bool UartController::scanAtBaudrate(int baud) {+ const size_t maxResponseSize = 8192;+ uartService.switchBaudrate(baud);+ terminalView.println("→ Testing baudrate " + std::to_string(baud));+ uartService.clearUartBuffer();- if (isprint(c) || isspace(c))- asciiCount++;- }+ std::string response;+ size_t asciiCount = 0;+ size_t probeIndex = 0;+ unsigned long start = millis();- float asciiRatio = response.empty() ? 0.0f : (float)asciiCount / response.size();-- // Accept short but readable responses (e.g., "AT+OK\r\n")- bool validShort = response.length() >= 10 && asciiRatio >= 0.95f;-- // Accept long and mostly printable responses- bool validLong = response.length() > 60 && asciiCount > 20 && asciiRatio >= 0.9f;-- // Baudrate detected- if (validShort || validLong) {- terminalView.println("");- terminalView.println("Preview:");- terminalView.println(response.substr(0, 100) + "...");- uartService.switchBaudrate(baud);- state.setUartBaudRate(baud);- terminalView.println("");- terminalView.println("UART Scan: Setting baudrate to UART config.");- terminalView.println("UART Scan: Baudrate detected " + std::to_string(baud));- return;- }+ while (millis() - start < 1500) {+ if (checkScanCancelled()) return false;+ sendNextProbe(probeIndex);+ updateResponse(response, asciiCount, maxResponseSize);- delay(10);+ if (isValidResponse(response, asciiCount)) {+ terminalView.println("");+ terminalView.println("Preview:");+ auto cleaned = argTransformer.filterPrintable(response.substr(0, 100));+ terminalView.println(cleaned + "...");+ return true;}++ delay(10);}- terminalView.println("Uart Scan: No device detected.");+ return false;+}++bool UartController::checkScanCancelled() {+ char key = terminalInput.readChar();+ if (key == '\r' || key == '\n') {+ terminalView.println("UART Scan: Cancelled by user.");+ return true;+ }+ return false;+}++void UartController::sendNextProbe(size_t& probeIndex) {+ if (probeIndex < probes.size()) {+ uartService.write(probes[probeIndex]);+ probeIndex++;+ }+}++void UartController::updateResponse(std::string& response, size_t& asciiCount, size_t maxSize) {+ unsigned long readStart = millis();+ const unsigned long readTimeout = 150; // ms+ while (uartService.available() > 0 && millis() - readStart < readTimeout) {+ char c = uartService.read();++ if (response.length() >= maxSize) {+ char dropped = response.front();+ if (isprint(dropped) || isspace(dropped)) asciiCount--;+ response.erase(0, 1);+ }++ if (isprint(c) || isspace(c)) asciiCount++;+ response += c;+ }+}++bool UartController::isValidResponse(const std::string& response, size_t asciiCount) {+ if (response.empty()) return false;++ float ratio = static_cast<float>(asciiCount) / response.size();+ float entropy = computeEntropy(response);++ bool plausibleLength = response.length() >= 32;+ bool readableEnough = ratio >= 0.85f;+ bool entropyOK = entropy >= 3.0f && entropy <= 7.5f;++ return plausibleLength && readableEnough && entropyOK;+}++float UartController::computeEntropy(const std::string& data) {+ std::unordered_map<char, size_t> freq;+ for (char c : data)+ freq[c]++;++ float entropy = 0.0f;+ for (auto& p : freq) {+ float prob = static_cast<float>(p.second) / data.size();+ entropy -= prob * std::log2(prob);+ }+ return entropy;}/*@@ -254,7 +301,7 @@ Configvoid UartController::handleConfig() {terminalView.println("");terminalView.println("UART Configuration:");-+uint8_t rxPin = userInputManager.readValidatedUint8("RX pin number", state.getUartRxPin());state.setUartRxPin(rxPin);diff --git a/src/Controllers/UartController.h b/src/Controllers/UartController.hindex 769438d0..7e8d4767 100644--- a/src/Controllers/UartController.h+++ b/src/Controllers/UartController.h@@ -38,9 +38,6 @@ public:// Perform a simple readvoid handleRead();- // Probe known devices- void handleScan();-// Send simple ping commandvoid handlePing();@@ -59,6 +56,27 @@ public:// Encode UART config bitsuint32_t buildUartConfig(uint8_t dataBits, char parity, uint8_t stopBits);+ // Scan to find the right baudrate+ void handleScan();++ // Scan a baudrate+ bool scanAtBaudrate(int baud);++ // Check Enter press+ bool checkScanCancelled();++ // Send next probe to get a UART response+ void sendNextProbe(size_t& probeIndex);++ // Update rolling buffer+ void updateResponse(std::string& response, size_t& asciiCount, size_t maxSize);++ // Check if data response is a valid UART data+ bool isValidResponse(const std::string& response, size_t asciiCount);++ // Check entropy to determine if a response is a valid UART response+ float computeEntropy(const std::string& data);+ITerminalView& terminalView;IInput& terminalInput;IInput& deviceInput;@@ -86,4 +104,39 @@ public:"help\r\n", "?\r\n", "\r\n","\x1B", "\x03", "\x04", "\x1A", "\x11", "\x13" // ESC, Ctrl+C/Z/D, XON/XOFF};++ std::vector<int> baudrates = {+ // Les plus standards+ 9600,+ 115200,+ 19200,+ 57600,+ 38400,++ // Baudrates spécifiques+ 4800,+ 2400,+ 14400,+ 28800,+ 76800,+ 128000,++ // Haut débit+ 230400,+ 250000,+ 460800,++ // Tres haut débit+ 921600,+ 1000000,+ 1500000,+ 2000000,+ 3000000,++ // legacy+ 1200,+ 600,+ 300+ };+};