A Hardware Hacking Tool with Web-Based CLI That Speaks Every Protocol
C arduinobluetoothcan-busdebuggingeepromesp32flipperzerogpiohardware-hackingi2cjtagopenocdprotocolspwmradiorfidspisubghzuartwifi
create subghz analyzer
Geo321071d82eae
diff --git a/src/Managers/SubGhzAnalyzeManager.cpp b/src/Managers/SubGhzAnalyzeManager.cppnew file mode 100644index 00000000..d26ee786--- /dev/null+++ b/src/Managers/SubGhzAnalyzeManager.cpp@@ -0,0 +1,328 @@+#include "SubGhzAnalyzeManager.h"+#include <cmath>+#include <sstream>+#include <unordered_set>++std::string SubGhzAnalyzeManager::analyzeFrame(const std::vector<rmt_item32_t>& items, float tickPerUs) {+ SubGhzDetectResult r;+ if (items.empty()) { r.notes = "Empty frame"; return formatFrame(r); }++ std::vector<uint32_t> highs, lows;+ collectDurations(items, tickPerUs, highs, lows);++ float T = estimateBaseT(highs, lows);+ r.baseT_us = T;++ float ratio = 0.f;+ bool pulse = looksPulseLength(T, highs, lows, ratio);+ bool manch = looksManchester(T, highs, lows);+ bool pwm = looksPWM(T, highs, lows);++ if (manch) r.encoding = RfEncoding::Manchester;+ else if (pulse) r.encoding = RfEncoding::PulseLength;+ else if (pwm) r.encoding = RfEncoding::PWM;+ else r.encoding = RfEncoding::Unknown;++ // Bitrate+ if (r.encoding == RfEncoding::Manchester && T > 0.f) {+ r.bitrate_kbps = 1000.f / (2.f * T);+ } else if (T > 0.f) {+ r.bitrate_kbps = 1000.f / T;+ }++ // Attempt decoding+ if (r.encoding == RfEncoding::PulseLength) {+ std::string hex;+ int bits = 0;+ if (decodePT2262Like(T, items, tickPerUs, hex, bits)) {+ r.payloadHex = hex;+ r.bitCount = bits;++ // Guess protocol+ if (bits >= 20 && bits <= 36 && ratio > 2.f) {+ r.protocolGuess = "EV1527/PT2262-like";+ } else {+ r.protocolGuess = "Pulse-length (generic)";+ }+ r.confidence = clamp01(0.6f + 0.2f * (ratio > 2.5f));+ } else {+ r.protocolGuess = "Pulse-length (undecoded)";+ r.confidence = 0.35f;+ }+ } else if (r.encoding == RfEncoding::Manchester) {+ r.protocolGuess = "Manchester (generic)";+ r.confidence = 0.5f;+ } else if (r.encoding == RfEncoding::PWM) {+ r.protocolGuess = "PWM (generic)";+ r.confidence = 0.4f;+ } else {+ r.protocolGuess = "Unknown";+ r.confidence = 0.2f;+ }++ return formatFrame(r);+}++std::string SubGhzAnalyzeManager::analyzeFrequencyActivity(+ int dwellMs,+ int windowMs,+ int thresholdDbm,+ const std::function<int(int)>& measure,+ const std::function<bool()>& shouldAbort,+ float neighborLeftConf,+ float neighborRightConf+) {+ if (windowMs < 1) windowMs = 1;+ const int windows = std::max(1, dwellMs / windowMs);++ int hits = 0;+ int peakDbm = -127, minDbm = 127, maxDbm = -127;+ long long sum = 0;++ std::vector<int> samples;+ samples.reserve(windows);++ for (int i = 0; i < windows; ++i) {+ if (shouldAbort && shouldAbort()) break;+ const int v = measure(windowMs);+ samples.push_back(v);+ sum += v;+ if (v > peakDbm) peakDbm = v;+ if (v < minDbm) minDbm = v;+ if (v > maxDbm) maxDbm = v;+ if (v >= thresholdDbm) hits++;+ }++ const int used = static_cast<int>(samples.size());+ const float avgDbm = (used > 0) ? static_cast<float>(sum) / used : -127.f;++ // Variance+ double var = 0.0;+ for (int v : samples) {+ const double d = static_cast<double>(v) - static_cast<double>(avgDbm);+ var += d * d;+ }+ var /= std::max(1, used);+ const float stddevDbm = static_cast<float>(std::sqrt(var));++ const float hitRatio = (used > 0) ? static_cast<float>(hits) / used : 0.f;++ // Confidence+ const float margin = static_cast<float>(peakDbm - thresholdDbm);+ float mScore = margin / 25.f;+ if (mScore < 0.f) mScore = 0.f;+ else if (mScore > 1.f) mScore = 1.f;++ float aScore = hitRatio;+ if (aScore < 0.f) aScore = 0.f;+ else if (aScore > 1.f) aScore = 1.f;++ float conf = 0.25f + 0.55f * mScore + 0.30f * aScore;++ if (hits == 0 || peakDbm < thresholdDbm) {+ conf = 0.f; // nothing+ } else if (hitRatio < 0.10f && (margin) < 5) {+ conf *= 0.3f; // rare+ }++ // Bonus stable+ if (margin >= 15.f && hitRatio >= 0.60f && stddevDbm <= 3.0f) conf += 0.05f;++ // Boost neighboring confidence+ if (neighborLeftConf > 0.60f) conf += 0.03f;+ if (neighborRightConf > 0.60f) conf += 0.03f;+ if (neighborLeftConf > 0.80f && neighborRightConf > 0.80f) conf += 0.02f;++ if (conf < 0.f) conf = 0.f;+ else if (conf > 1.f) conf = 1.f;++ return formatFrequency(peakDbm, hits, used, avgDbm, stddevDbm, hitRatio, conf);+}++std::string SubGhzAnalyzeManager::formatFrequency(+ int peakDbm,+ int hits,+ int windows,+ float avgDbm,+ float stddevDbm,+ float hitRatio,+ float confidence+) {+ std::ostringstream oss;+ oss << "peak=" << peakDbm << " dBm"+ << " activity=" << static_cast<int>(hitRatio * 100.f) << '%'+ << " conf=" << static_cast<int>(confidence * 100.f) << '%'+ << " avg=" << static_cast<int>(std::lround(avgDbm)) << " dBm"+ << " (std=" << static_cast<int>(std::lround(stddevDbm)) << ")"+ << " hits=" << hits << '/' << windows;+ return oss.str();+}+++std::string SubGhzAnalyzeManager::formatFrame(const SubGhzDetectResult& r) const {+ auto encToStr = [](RfEncoding e){+ switch (e) {+ case RfEncoding::PulseLength: return "PulseLength";+ case RfEncoding::Manchester: return "Manchester";+ case RfEncoding::PWM: return "PWM";+ default: return "Unknown";+ }+ };++ std::ostringstream oss;+ oss << " [Frame received]\r\n"+ << " Encoding : " << encToStr(r.encoding) << "\r\n"+ << " Base T (us) : " << std::lround(r.baseT_us) << "\r\n"+ << " Bitrate (kb) : " << r.bitrate_kbps << "\r\n"+ << " Bit count : " << r.bitCount << "\r\n"+ << " Payload (hex): " << (r.payloadHex.empty() ? "-" : r.payloadHex) << "\r\n"+ << " Protocol : " << r.protocolGuess << "\r\n"+ << " Confidence : " << r.confidence << "\r\n";+ if (!r.notes.empty()) oss << "Notes : " << r.notes << "\r\n";+ return oss.str();+}++void SubGhzAnalyzeManager::collectDurations(const std::vector<rmt_item32_t>& items, float tickPerUs,+ std::vector<uint32_t>& highs, std::vector<uint32_t>& lows) {+ highs.clear(); lows.clear();+ highs.reserve(items.size());+ lows.reserve(items.size());++ for (const auto& it : items) {+ // Convert RMT ticks+ uint32_t d0 = (uint32_t)std::max(1.f, std::round(it.duration0 / tickPerUs));+ uint32_t d1 = (uint32_t)std::max(1.f, std::round(it.duration1 / tickPerUs));+ highs.push_back(d0);+ lows.push_back(d1);+ }+}++float SubGhzAnalyzeManager::median(std::vector<uint32_t> v) {+ if (v.empty()) return 0.f;+ std::sort(v.begin(), v.end());+ size_t n = v.size();+ return (n & 1) ? (float)v[n/2] : 0.5f * (v[n/2 - 1] + v[n/2]);+}++float SubGhzAnalyzeManager::estimateBaseT(const std::vector<uint32_t>& highs, const std::vector<uint32_t>& lows) {+ // Take short durations as approximation+ std::vector<uint32_t> pool;+ pool.reserve(highs.size() + lows.size());+ for (auto d : highs) if (d >= 2) pool.push_back(d);+ for (auto d : lows) if (d >= 2) pool.push_back(d);+ if (pool.size() < 8) return 0.f;++ std::sort(pool.begin(), pool.end());+ size_t m = std::max<size_t>(4, pool.size()/4);+ std::vector<uint32_t> q(pool.begin(), pool.begin() + m);+ return median(q);+}++bool SubGhzAnalyzeManager::looksManchester(float T, const std::vector<uint32_t>& highs, const std::vector<uint32_t>& lows) {+ if (T <= 0.f || highs.size() < 8) return false;+ int ok = 0, total = 0;+ float tol = T * 0.40f; // tolerance+ for (size_t i = 0; i < highs.size(); ++i) {+ total++;+ if (nearf(highs[i], T, tol) && nearf(lows[i], T, tol)) ok++;+ }+ return total >= 8 && (float)ok / total > 0.6f;+}++bool SubGhzAnalyzeManager::looksPulseLength(float T, const std::vector<uint32_t>& highs, const std::vector<uint32_t>& lows, float& ratioOut) {+ if (T <= 0.f || highs.size() < 8) { ratioOut = 0.f; return false; }++ std::vector<float> r1, r2;+ r1.reserve(highs.size()); r2.reserve(highs.size());+ for (size_t i = 0; i < highs.size(); ++i) {+ float h = (float)highs[i];+ float l = (float)lows[i];+ if (h < 1.f || l < 1.f) continue;+ r1.push_back(h / l);+ r2.push_back(l / h);+ }+ if (r1.empty() || r2.empty()) { ratioOut = 0.f; return false; }+ std::sort(r1.begin(), r1.end());+ std::sort(r2.begin(), r2.end());+ float m1 = r1[r1.size()/2];+ float m2 = r2[r2.size()/2];+ ratioOut = (m1 > m2) ? m1 : m2;++ return ratioOut > 1.8f && ratioOut < 4.2f;+}++bool SubGhzAnalyzeManager::looksPWM(float T, const std::vector<uint32_t>& highs, const std::vector<uint32_t>& lows) {+ if (T <= 0.f || highs.size() < 8) return false;++ auto medH = median(highs);+ auto medL = median(lows);+ float varH = 0.f, varL = 0.f;+ for (auto d : highs) varH += std::fabs((float)d - medH);+ for (auto d : lows) varL += std::fabs((float)d - medL);+ varH /= std::max<size_t>(1, highs.size());+ varL /= std::max<size_t>(1, lows.size());++ return (varH < 0.25f * varL) || (varL < 0.25f * varH);+}++static inline bool isShortT(float d, float T) { return std::fabs(d - T) <= (T * 0.6f); }+static inline bool isLong3T(float d, float T) { return std::fabs(d - 3*T) <= (T * 0.8f); }++bool SubGhzAnalyzeManager::decodePT2262Like(float T, const std::vector<rmt_item32_t>& items, float tickPerUs,+ std::string& hexOut, int& bitCountOut) {+ if (T <= 0.f || items.size() < 8) return false;++ std::string bits;+ bits.reserve(items.size());++ for (const auto& it : items) {+ float h = it.duration0 / tickPerUs;+ float l = it.duration1 / tickPerUs;++ // Ignore+ if (h < 2 || l < 2) continue;++ bool hS = isShortT(h, T), hL = isLong3T(h, T);+ bool lS = isShortT(l, T), lL = isLong3T(l, T);++ if (hS && lL) bits.push_back('0');+ else if (hL && lS) bits.push_back('1');+ else {+ float ehS = std::fabs(h - T), ehL = std::fabs(h - 3*T);+ float elS = std::fabs(l - T), elL = std::fabs(l - 3*T);+ if (ehS+elL < ehL+elS) bits.push_back('0');+ else bits.push_back('1');+ }+ }++ // Too short+ if ((int)bits.size() < 16) return false;++ // Truncate to multiple of 4+ bitCountOut = (int)(bits.size() & ~0x3);+ if (bitCountOut == 0) return false;++ bits.resize((size_t)bitCountOut);+ hexOut = bitsToHex(bits);+ return true;+}++bool SubGhzAnalyzeManager::nearf(float a, float b, float tol) { return std::fabs(a - b) <= tol; }++float SubGhzAnalyzeManager::clamp01(float v) {+ if (v < 0.f) return 0.f;+ if (v > 1.f) return 1.f;+ return v;+}++std::string SubGhzAnalyzeManager::bitsToHex(const std::string& bits) {+ static const char* HEX = "0123456789ABCDEF";+ std::string out;+ out.reserve(bits.size()/4);+ for (size_t i = 0; i + 3 < bits.size(); i += 4) {+ int v = ((bits[i]-'0')<<3) | ((bits[i+1]-'0')<<2) | ((bits[i+2]-'0')<<1) | (bits[i+3]-'0');+ out.push_back(HEX[v & 0xF]);+ }+ return out;+}+diff --git a/src/Managers/SubGhzAnalyzeManager.h b/src/Managers/SubGhzAnalyzeManager.hnew file mode 100644index 00000000..734c92e5--- /dev/null+++ b/src/Managers/SubGhzAnalyzeManager.h@@ -0,0 +1,92 @@+#pragma once++#include <vector>+#include <string>+#include <cstdint>+#include <algorithm>+#include <functional>++#include "Interfaces/ITerminalView.h"+#include "Interfaces/IInput.h"+#include "driver/rmt.h"++enum class RfEncoding { Unknown, PulseLength, Manchester, PWM };++struct SubGhzDetectResult {+ RfEncoding encoding = RfEncoding::Unknown;+ float baseT_us = 0.f; // estimate period (µs)+ float bitrate_kbps = 0.f;+ int bitCount = 0; // useful bits if decoded+ std::string payloadHex; // payload in hex if decoded+ std::string protocolGuess;+ float confidence = 0.f; // 0..1+ std::string notes; // free text+};++struct FrequencyActivity {+ // Inputs+ int thresholdDbm = -80;+ int windows = 0;+ int hits = 0; // number of windows with peak >= threshold+ float hitRatio = 0.f; // hits / windows++ // Stats RSSI+ int peakDbm = -127;+ int minDbm = 127;+ int maxDbm = -127;+ float avgDbm = -127.f;+ float stddevDbm = 0.f;++ // Score+ float confidence = 0.f;++ // Optional+ std::string reason;+};++class SubGhzAnalyzeManager {+public:++ // Analyse frame+ std::string analyzeFrame(const std::vector<rmt_item32_t>& items,+ float tickPerUs = 1.0f);++ // Analyze activity+ std::string analyzeFrequencyActivity(int dwellMs,+ int windowMs,+ int thresholdDbm,+ const std::function<int(int)>& measure,+ const std::function<bool()>& shouldAbort = nullptr,+ float neighborLeftConf = 0.f,+ float neighborRightConf = 0.f);+++private:+ std::string formatFrame(const SubGhzDetectResult& r) const;+ std::string formatFrequency(int peakDbm,+ int hits,+ int windows,+ float avgDbm,+ float stddevDbm,+ float hitRatio,+ float confidence);++ void collectDurations(const std::vector<rmt_item32_t>& items, float tickPerUs,+ std::vector<uint32_t>& highs, std::vector<uint32_t>& lows);+ float median(std::vector<uint32_t> v);+ float estimateBaseT(const std::vector<uint32_t>& highs, const std::vector<uint32_t>& lows);++ // Protocols prediction+ bool looksManchester(float T, const std::vector<uint32_t>& highs, const std::vector<uint32_t>& lows);+ bool looksPulseLength(float T, const std::vector<uint32_t>& highs, const std::vector<uint32_t>& lows, float& ratioOut);+ bool looksPWM(float T, const std::vector<uint32_t>& highs, const std::vector<uint32_t>& lows);++ // Decode+ bool decodePT2262Like(float T, const std::vector<rmt_item32_t>& items, float tickPerUs,+ std::string& hexOut, int& bitCountOut);++ // Utils+ std::string bitsToHex(const std::string& bits);+ float clamp01(float v);+ bool nearf(float a, float b, float tol);+};