Skip to content

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

rework subghz sniff and replay to be more resilient to noise

Geo92ccdbb4f9fc
diff --git a/src/Controllers/SubGhzController.cpp b/src/Controllers/SubGhzController.cpp
index ce5815c0..ded2b0eb 100644
--- a/src/Controllers/SubGhzController.cpp
+++ b/src/Controllers/SubGhzController.cpp
@@ -43,15 +43,15 @@ void SubGhzController::handleSniff(const TerminalCommand&) {
break;
}
- auto raw = subGhzService.readRawPulses();
- for (auto &line : raw) {
- count++;
+ auto [line, pulseCount] = subGhzService.readRawPulses();
+ if (pulseCount > 8) { // ignore too short frames, likely noise
+ count += pulseCount;
terminalView.println(line);
}
}
subGhzService.stopRawSniffer();
- terminalView.println("\nSUBGHZ Sniff: Stopped by user. " + std::to_string(count) + " frames\n");
+ terminalView.println("\nSUBGHZ Sniff: Stopped by user. " + std::to_string(count) + " pulses\n");
}
/*
@@ -172,15 +172,15 @@ Replay
void SubGhzController::handleReplay(const TerminalCommand&) {
const float f = state.getSubGhzFrequency();
+ // Gap between each frame for emmitting
+ auto gap = userInputManager.readValidatedInt("Inter-frame gap (ms):", 100, 0, 10000);
+
// Set profile to read frames
if (!subGhzService.applySniffProfile(f)) {
terminalView.println("SUBGHZ: Not detected. Run 'config' first.");
return;
}
- // Gap between each frame for emmitting
- auto gap = userInputManager.readValidatedInt("Inter-frame gap (ms):", 100, 0, 10000);
-
// Start sniffer
if (!subGhzService.startRawSniffer(state.getSubGhzGdoPin())) {
terminalView.println("SUBGHZ: Failed to start raw sniffer.");
@@ -223,7 +223,7 @@ void SubGhzController::handleReplay(const TerminalCommand&) {
}
// Start sending frames
- terminalView.print("SUBGHZ: Replay in progress...\n");
+ terminalView.print("SUBGHZ: Replay in progress...\r\n");
bool okAll = true;
auto gdo = state.getSubGhzGdoPin();
bool confirm = true;
@@ -395,7 +395,7 @@ void SubGhzController::handleDecode(const TerminalCommand&) {
// Read and analyze frame
frame = subGhzService.readRawFrame();
- if (!frame.empty()) {
+ if (!frame.empty() && frame.size() >= 5) {
auto result = subGhzAnalyzeManager.analyzeFrame(frame);
terminalView.println(result);
}
diff --git a/src/Services/SubGhzService.cpp b/src/Services/SubGhzService.cpp
index d059ca3d..ad2c23d8 100644
--- a/src/Services/SubGhzService.cpp
+++ b/src/Services/SubGhzService.cpp
@@ -141,7 +141,7 @@ bool SubGhzService::startRawSniffer(int pin) {
// Install RMT driver
if (rmt_config(&rxconfig) != ESP_OK) return false;
- if (rmt_driver_install(rxconfig.channel, 16 * 1024, 0) != ESP_OK) return false;
+ if (rmt_driver_install(rxconfig.channel, 8192, 0) != ESP_OK) return false;
// Get ring buffer handle
if (rmt_get_ringbuf_handle(rxconfig.channel, &rb_) != ESP_OK) return false;
@@ -157,14 +157,12 @@ void SubGhzService::stopRawSniffer() {
ELECHOUSE_cc1101.setSidle();
}
-std::vector<std::string> SubGhzService::readRawPulses() {
- std::vector<std::string> result;
- if (!rb_) return result;
+std::pair<std::string, size_t> SubGhzService::readRawPulses() {
+ if (!rb_) return {"", 0};
- // Receive raw pulses from RMT
size_t rx_size = 0;
rmt_item32_t* item = (rmt_item32_t*) xRingbufferReceive(rb_, &rx_size, 0);
- if (!item) return result;
+ if (!item) return {"", 0};
size_t n = rx_size / sizeof(rmt_item32_t);
uint32_t totalDuration = 0;
@@ -173,27 +171,21 @@ std::vector<std::string> SubGhzService::readRawPulses() {
totalDuration += item[i].duration1;
}
- // Header
std::ostringstream oss;
- oss << "[raw " << n << " pulses | freq=" << mhz_ << " MHz | dur="
- << totalDuration << " ticks]\r\n";
+ oss << "[raw " << n << " pulses | freq=" << mhz_
+ << " MHz | dur=" << totalDuration << " ticks]\r\n";
- // Pulse details
int col = 0;
for (size_t i = 0; i < n; i++) {
oss << (item[i].level0 ? 'H' : 'L') << ":" << item[i].duration0
<< " | "
<< (item[i].level1 ? 'H' : 'L') << ":" << item[i].duration1
<< " ";
-
if (++col % 4 == 0) oss << "\r\n";
}
-
oss << "\n\r";
- result.push_back(oss.str());
- vRingbufferReturnItem(rb_, (void*)item);
- return result;
+ return {oss.str(), n};
}
std::vector<rmt_item32_t> SubGhzService::readRawFrame() {
@@ -211,32 +203,31 @@ std::vector<rmt_item32_t> SubGhzService::readRawFrame() {
return frame;
}
-bool SubGhzService::sendRawFrame(int pin, const std::vector<rmt_item32_t>& items, uint32_t /*tick_per_us_ignored*/) {
+bool SubGhzService::sendRawFrame(int pin, const std::vector<rmt_item32_t>& items, uint32_t tick_per_us) {
if (!isConfigured_ || items.empty()) return false;
+ // Output
+ if (!startTxBitBang()) return false;
- if (!startTxBitBang()) return false; // configure gdo0_
-
- // Stabilisation
- gpio_set_level((gpio_num_t)pin, 0);
- esp_rom_delay_us(200);
+ // tick to us
+ auto ticks_to_us = [tick_per_us](uint32_t ticks) -> uint32_t {
+ if (!tick_per_us) return 0;
+ return (ticks + (tick_per_us/2)) / tick_per_us;
+ };
+ // Bit bang seq
for (const auto& it : items) {
- // phase 0
gpio_set_level((gpio_num_t)pin, it.level0 ? 1 : 0);
- uint32_t us0 = it.duration0;
+ uint32_t us0 = ticks_to_us(it.duration0);
if (us0) esp_rom_delay_us(us0);
- // phase 1
gpio_set_level((gpio_num_t)pin, it.level1 ? 1 : 0);
- uint32_t us1 = it.duration1;
+ uint32_t us1 = ticks_to_us(it.duration1);
if (us1) esp_rom_delay_us(us1);
}
- // Stop
+ // Reset
gpio_set_level((gpio_num_t)pin, 0);
- esp_rom_delay_us(200);
-
return true;
}
diff --git a/src/Services/SubGhzService.h b/src/Services/SubGhzService.h
index 61d152b6..db719a22 100644
--- a/src/Services/SubGhzService.h
+++ b/src/Services/SubGhzService.h
@@ -35,7 +35,7 @@ public:
// RMT raw sniffer
bool startRawSniffer(int pin);
- std::vector<std::string> readRawPulses();
+ std::pair<std::string, size_t> readRawPulses();
std::vector<rmt_item32_t> readRawFrame();
void stopRawSniffer();