A Hardware Hacking Tool with Web-Based CLI That Speaks Every Protocol
C arduinobluetoothcan-busdebuggingeepromesp32flipperzerogpiohardware-hackingi2cjtagopenocdprotocolspwmradiorfidspisubghzuartwifi
improve hduart implementation
Geo6af17bf4d0df
diff --git a/src/Controllers/HdUartController.cpp b/src/Controllers/HdUartController.cppindex 20e3cdc3..a11d9995 100644--- a/src/Controllers/HdUartController.cpp+++ b/src/Controllers/HdUartController.cpp@@ -33,12 +33,28 @@ Bridge mode read/write*/void HdUartController::handleBridge() {terminalView.println("HDUART Bridge: In progress... Press any ESP32 button to stop.");++ std::string echoBuffer;+while (true) {- if (hdUartService.available()) {- terminalView.print(std::string(1, hdUartService.read()));+ // Receive UART+ while (hdUartService.available()) {+ char incoming = hdUartService.read();+ if (!echoBuffer.empty() && incoming == echoBuffer.front()) {+ echoBuffer.erase(0, 1); // Filter for echo line+ } else {+ terminalView.print(std::string(1, incoming));+ }}++ // Terminal inputchar c = terminalInput.readChar();- if (c != KEY_NONE) hdUartService.write(c);+ if (c != KEY_NONE) {+ hdUartService.write(c);+ echoBuffer += c; // On s'attend à le recevoir en écho+ }++ // Device inputc = deviceInput.readChar();if (c != KEY_NONE) {terminalView.println("\nHDUART Bridge: Stopped by user.");@@ -74,15 +90,11 @@ void HdUartController::handleConfig() {bool inverted = userInputManager.readYesNo("Inverted?", state.isHdUartInverted());state.setHdUartInverted(inverted);- uint32_t config = buildUartConfig(dataBits, parity, stopBits);- state.setHdUartConfig(config);-- hdUartService.configure(baud, config, pin, inverted);+ hdUartService.configure(baud, dataBits, parity, stopBits, pin, inverted);terminalView.println("HDUART configuration applied.\n");}-/*Help*/@@ -92,19 +104,6 @@ void HdUartController::handleHelp() {" config Set TX/RX pin, baud etc.\n");}-uint32_t HdUartController::buildUartConfig(uint8_t bits, char parity, uint8_t stop) {- uint32_t conf = SERIAL_8N1;- if (bits == 5) conf = (stop == 2) ? SERIAL_5N2 : SERIAL_5N1;- else if (bits == 6) conf = (stop == 2) ? SERIAL_6N2 : SERIAL_6N1;- else if (bits == 7) conf = (stop == 2) ? SERIAL_7N2 : SERIAL_7N1;- else if (bits == 8) conf = (stop == 2) ? SERIAL_8N2 : SERIAL_8N1;-- if (parity == 'E') conf |= 0x02;- else if (parity == 'O') conf |= 0x01;-- return conf;-}-void HdUartController::ensureConfigured() {if (!configured) {handleConfig();diff --git a/src/Controllers/HdUartController.h b/src/Controllers/HdUartController.hindex 30c068ff..bde47c97 100644--- a/src/Controllers/HdUartController.h+++ b/src/Controllers/HdUartController.h@@ -42,7 +42,4 @@ private:// HDURT Available commandsvoid handleHelp();-- // Package configuration- uint32_t buildUartConfig(uint8_t dataBits, char parity, uint8_t stopBits);};diff --git a/src/Services/HdUartService.cpp b/src/Services/HdUartService.cppindex 391b7fee..659ca49d 100644--- a/src/Services/HdUartService.cpp+++ b/src/Services/HdUartService.cpp@@ -1,45 +1,55 @@#include "HdUartService.h"-void HdUartService::configure(unsigned long baud, uint32_t config, uint8_t pin, bool inverted) {- ioPin = pin;- baudRate = baud;- serialConfig = config;- isInverted = inverted;-- Serial1.end();- Serial1.begin(baudRate, serialConfig, ioPin, ioPin, isInverted); // meme pin RX/TX-}+void HdUartService::configure(unsigned long baud, uint8_t dataBits, char parity, uint8_t stopBits, uint8_t ioPin, bool inverted) {+ // Build config from raw values+ uart_config_t uart_config = buildUartConfig(baud, dataBits, parity, stopBits);-void HdUartService::switchToWrite() {- pinMode(ioPin, OUTPUT);- delayMicroseconds(10);-}+ // Configure GPIO in open drain mode+ gpio_config_t gpioConf = {+ .pin_bit_mask = (1ULL << ioPin),+ .mode = GPIO_MODE_INPUT_OUTPUT_OD,+ .pull_up_en = GPIO_PULLUP_DISABLE,+ .pull_down_en = GPIO_PULLDOWN_DISABLE,+ .intr_type = GPIO_INTR_DISABLE+ };+ gpio_config(&gpioConf);++ // Inverted+ if (isInverted) {+ uart_set_line_inverse(HD_UART_PORT, UART_SIGNAL_TXD_INV | UART_SIGNAL_RXD_INV);+ } else {+ uart_set_line_inverse(HD_UART_PORT, UART_SIGNAL_INV_DISABLE);+ }-void HdUartService::switchToRead() {- pinMode(ioPin, INPUT);- delayMicroseconds(10);+ // Apply UART config+ uart_driver_install(HD_UART_PORT, UART_RX_BUFFER_SIZE, 0, 0, NULL, 0);+ uart_param_config(HD_UART_PORT, &uart_config);++ // Route UART signals to shared pin+ esp_rom_gpio_connect_out_signal(ioPin, UART_PERIPH_SIGNAL(HD_UART_PORT, SOC_UART_TX_PIN_IDX), false, false);+ esp_rom_gpio_connect_in_signal(ioPin, UART_PERIPH_SIGNAL(HD_UART_PORT, SOC_UART_RX_PIN_IDX), false);}void HdUartService::write(uint8_t data) {- switchToWrite();- Serial1.write(data);- Serial1.flush();- switchToRead();+ uart_write_bytes(HD_UART_PORT, &data, 1);+ uart_wait_tx_done(HD_UART_PORT, pdMS_TO_TICKS(100));}void HdUartService::write(const std::string& str) {- switchToWrite();- Serial1.write(reinterpret_cast<const uint8_t*>(str.c_str()), str.length());- Serial1.flush();- switchToRead();+ uart_write_bytes(HD_UART_PORT, str.c_str(), str.length());+ uart_wait_tx_done(HD_UART_PORT, pdMS_TO_TICKS(100));}bool HdUartService::available() const {- return Serial1.available();+ size_t len = 0;+ uart_get_buffered_data_len(HD_UART_PORT, &len);+ return len > 0;}char HdUartService::read() {- return Serial1.read();+ uint8_t c;+ int len = uart_read_bytes(HD_UART_PORT, &c, 1, pdMS_TO_TICKS(10));+ return (len == 1) ? static_cast<char>(c) : '\0';}std::string HdUartService::executeByteCode(const std::vector<ByteCode>& bytecodes) {@@ -60,11 +70,10 @@ std::string HdUartService::executeByteCode(const std::vector<ByteCode>& bytecodestart = millis();while (received < code.getRepeat() && (millis() - start < timeout)) {if (available()) {- char c = read();- result += c;- received++;+ result += read();+ ++received;} else {- delay(10);+ delay(1);}}break;@@ -84,3 +93,40 @@ std::string HdUartService::executeByteCode(const std::vector<ByteCode>& bytecodereturn result;}++uart_config_t HdUartService::buildUartConfig(unsigned long baud, uint8_t bits, char parity, uint8_t stop) {+ uart_word_length_t dataBits;+ uart_parity_t parityMode;+ uart_stop_bits_t stopBits;++ // Data bits+ switch (bits) {+ case 5: dataBits = UART_DATA_5_BITS; break;+ case 6: dataBits = UART_DATA_6_BITS; break;+ case 7: dataBits = UART_DATA_7_BITS; break;+ case 8:+ default: dataBits = UART_DATA_8_BITS; break;+ }++ // Parity+ switch (parity) {+ case 'E': parityMode = UART_PARITY_EVEN; break;+ case 'O': parityMode = UART_PARITY_ODD; break;+ default: parityMode = UART_PARITY_DISABLE; break;+ }++ // Stop bits+ stopBits = (stop == 2) ? UART_STOP_BITS_2 : UART_STOP_BITS_1;++ // Assemble+ uart_config_t config = {+ .baud_rate = static_cast<int>(baud),+ .data_bits = dataBits,+ .parity = parityMode,+ .stop_bits = stopBits,+ .flow_ctrl = UART_HW_FLOWCTRL_DISABLE,+ .source_clk = UART_SCLK_APB+ };++ return config;+}diff --git a/src/Services/HdUartService.h b/src/Services/HdUartService.hindex 53bed065..52cdfe59 100644--- a/src/Services/HdUartService.h+++ b/src/Services/HdUartService.h@@ -1,11 +1,20 @@#pragma once#include <vector>-#include "Models/ByteCode.h"#include <Arduino.h>+#include "driver/uart.h"+#include "driver/gpio.h"+#include "esp_rom_gpio.h"+#include "hal/uart_types.h"+#include "soc/uart_periph.h"+#include "Models/ByteCode.h"++#define HD_UART_PORT UART_NUM_1+#define UART_RX_BUFFER_SIZE 256+class HdUartService {public:- void configure(unsigned long baud, uint32_t config, uint8_t pin, bool inverted);+ void configure(unsigned long baud, uint8_t dataBits, char parity, uint8_t stopBits, uint8_t ioPin, bool inverted);void write(uint8_t data);void write(const std::string& str);bool available() const;@@ -13,6 +22,7 @@ public:std::string readLine();std::string executeByteCode(const std::vector<ByteCode>& bytecodes);void flush();+ uart_config_t buildUartConfig(unsigned long baud, uint8_t bits, char parity, uint8_t stop);private:uint8_t ioPin;@@ -20,6 +30,4 @@ private:uint32_t serialConfig;bool isInverted;- void switchToRead();- void switchToWrite();};