A Hardware Hacking Tool with Web-Based CLI That Speaks Every Protocol
C arduinobluetoothcan-busdebuggingeepromesp32flipperzerogpiohardware-hackingi2cjtagopenocdprotocolspwmradiorfidspisubghzuartwifi
fix subghz file parsing related to #43
Geo5ea9bd1eeee5
diff --git a/src/Transformers/SubGhzTransformer.cpp b/src/Transformers/SubGhzTransformer.cppindex e93fb58c..6de01466 100644--- a/src/Transformers/SubGhzTransformer.cpp+++ b/src/Transformers/SubGhzTransformer.cpp@@ -44,29 +44,27 @@ std::vector<SubGhzFileCommand> SubGhzTransformer::transformFromFileFormat(constuint32_t frequency = 0;uint16_t te = 0;- // Accumulators+ // Accumulateursstd::vector<int> bitList, bitRawList;std::vector<uint64_t> keyList;- std::vector<std::string> rawDataLines; // RAW_Data / Data_RAW+ std::vector<std::string> rawDataLines; // pour Protocol: RAW uniquement (timings signés)+ std::vector<uint8_t> binRawBytes; // pour Protocol: BinRAW (octets)- // Buffer to avoid copies- const char* p = fileContent.c_str();+ // Buffer de lecture+ const char* p = fileContent.c_str();const char* end = p + fileContent.size();while (p < end) {- // line boundaries [lineStart, lineEnd)const char* lineStart = p;while (p < end && *p != '\n') ++p;const char* lineEnd = p;- if (p < end && *p == '\n') ++p; // skip '\n'- if (lineEnd > lineStart && *(lineEnd-1) == '\r') --lineEnd; // strip '\r'+ if (p < end && *p == '\n') ++p;+ if (lineEnd > lineStart && *(lineEnd - 1) == '\r') --lineEnd;- // Search for ':'const char* colon = lineStart;while (colon < lineEnd && *colon != ':') ++colon;- if (colon == lineEnd) continue; // no "key: value"+ if (colon == lineEnd) continue;- // construct key/value (small strings)std::string key(lineStart, colon - lineStart);std::string val(colon + 1, lineEnd - (colon + 1));trim(key);@@ -78,35 +76,40 @@ std::vector<SubGhzFileCommand> SubGhzTransformer::transformFromFileFormat(const} else if (iequals(key, "Preset")) {presetStr = val;} else if (iequals(key, "Frequency")) {- uint32_t hz=0; if (parseUint32(val, hz)) frequency = hz;+ uint32_t hz = 0; if (parseUint32(val, hz)) frequency = hz;} else if (iequals(key, "TE")) {- uint16_t tev=0; if (parseUint16(val, tev)) te = tev;+ uint16_t tev = 0; if (parseUint16(val, tev)) te = tev;} else if (iequals(key, "Bit")) {- int v=0; if (parseInt(val, v)) bitList.push_back(v);+ int v = 0; if (parseInt(val, v)) bitList.push_back(v);} else if (iequals(key, "Bit_RAW")) {- int v=0; if (parseInt(val, v)) bitRawList.push_back(v);+ int v = 0; if (parseInt(val, v)) bitRawList.push_back(v);} else if (iequals(key, "Key")) {- uint64_t k=0; if (parseHexToU64(val, k)) keyList.push_back(k);- } else if (iequals(key, "RAW_Data") || iequals(key, "Data_RAW")) {- rawDataLines.push_back(val);+ uint64_t k = 0; if (parseHexToU64(val, k)) keyList.push_back(k);} else if (iequals(key, "BinRAW")) {auto bytes = parseHexByteStream(val);if (!bytes.empty()) {- SubGhzFileCommand cmd;- cmd.protocol = SubGhzProtocolEnum::BinRAW;- cmd.preset = mapPreset(presetStr);- cmd.frequency_hz = frequency;- cmd.te_us = te;- cmd.bitstream_bytes = std::move(bytes);- cmd.source_file = sourcePath;- out.emplace_back(std::move(cmd));+ binRawBytes.insert(binRawBytes.end(), bytes.begin(), bytes.end());+ }+ } else if (iequals(key, "RAW_Data") || iequals(key, "Data_RAW")) {+ // IMPORTANT :+ // - Bytes if Protocol: BinRAW+ // - Timings if Protocol: RAW+ if (iequals(protocolStr, "BinRAW")) {+ auto bytes = parseHexByteStream(val);+ if (!bytes.empty()) {+ binRawBytes.insert(binRawBytes.end(), bytes.begin(), bytes.end());+ }+ } else {+ rawDataLines.push_back(val);}}}- // Finalizer for accumulated data+ // Finalisation (ne crée plus de "compat RAW" quand Protocol != RAW)flushAccumulated(protocolStr, presetStr, frequency, te,- bitList, bitRawList, keyList, rawDataLines,+ bitList, bitRawList, keyList,+ rawDataLines, // timings pour RAW+ binRawBytes, // octets pour BinRAWout, sourcePath);return out;@@ -121,73 +124,103 @@ void SubGhzTransformer::flushAccumulated(const std::vector<int>& bitRawList,const std::vector<uint64_t>& keyList,const std::vector<std::string>& rawDataLines,+ const std::vector<uint8_t>& binRawBytes,std::vector<SubGhzFileCommand>& out,const std::string& sourcePath) {const auto protocol = detectProtocol(protocolStr);- const auto preset = mapPreset(presetStr);+ const auto preset = mapPreset(presetStr);+ // BINRAW+ if (protocol == SubGhzProtocolEnum::BinRAW) {+ if (!binRawBytes.empty()) {+ SubGhzFileCommand cmd;+ cmd.protocol = SubGhzProtocolEnum::BinRAW;+ cmd.preset = preset;+ cmd.frequency_hz = frequency;+ cmd.te_us = te;+ cmd.bitstream_bytes = binRawBytes;++ // Number of bits to send+ if (!bitList.empty()) cmd.bits = (uint16_t)std::max(0, bitList.back());+ else if (!bitRawList.empty()) cmd.bits = (uint16_t)std::max(0, bitRawList.back());+ else cmd.bits = (uint16_t)(binRawBytes.size() * 8);++ cmd.source_file = sourcePath;+ out.emplace_back(std::move(cmd));+ }+ return;+ }++ // RAWif (protocol == SubGhzProtocolEnum::RAW) {if (!rawDataLines.empty()) {SubGhzFileCommand cmd;- cmd.protocol = SubGhzProtocolEnum::RAW;- cmd.preset = preset;- cmd.frequency_hz = frequency;- cmd.te_us = te;- cmd.raw_timings = parseRawTimingsJoined(rawDataLines);- cmd.source_file = sourcePath;- if (!cmd.raw_timings.empty())+ cmd.protocol = SubGhzProtocolEnum::RAW;+ cmd.preset = preset;+ cmd.frequency_hz = frequency;+ cmd.te_us = te;+ cmd.raw_timings = parseRawTimingsJoined(rawDataLines);+ cmd.source_file = sourcePath;+ if (!cmd.raw_timings.empty()) {out.emplace_back(std::move(cmd));+ }}- return; // RAW+ return;}- if (!rawDataLines.empty()) {- // If there are RAW lines but Protocol != RAW, we can choose:- // - either ignore, or produce an additional RAW command.- // Here: we still produce a separate RAW command (useful for compatibility).- SubGhzFileCommand rc;- rc.protocol = SubGhzProtocolEnum::RAW;- rc.preset = preset;- rc.frequency_hz = frequency;- rc.te_us = te;- rc.raw_timings = parseRawTimingsJoined(rawDataLines);- rc.source_file = sourcePath;- if (!rc.raw_timings.empty()) out.emplace_back(std::move(rc));- }+ // RcSwitch+ const bool protoKeyed =+ (protocol == SubGhzProtocolEnum::RcSwitch) ||+ (protocol == SubGhzProtocolEnum::Princeton) ||+ (protocol == SubGhzProtocolEnum::Unknown);- for (int b : bitList) {- SubGhzFileCommand cmd;- cmd.protocol = (protocol==SubGhzProtocolEnum::Unknown) ? SubGhzProtocolEnum::RcSwitch : protocol;- cmd.preset = preset;- cmd.frequency_hz = frequency;- cmd.te_us = te;- cmd.bits = static_cast<uint16_t>(std::max(0, b));- cmd.key = 0; // clé non fournie ici- cmd.source_file = sourcePath;- out.emplace_back(std::move(cmd));+ if (!protoKeyed) {+ return;}- for (int b : bitRawList) {- SubGhzFileCommand cmd;- cmd.protocol = (protocol==SubGhzProtocolEnum::Unknown) ? SubGhzProtocolEnum::RcSwitch : protocol;- cmd.preset = preset;- cmd.frequency_hz = frequency;- cmd.te_us = te;- cmd.bits = static_cast<uint16_t>(std::max(0, b));- cmd.key = 0;- cmd.source_file = sourcePath;- out.emplace_back(std::move(cmd));++ // If we have keys, create one command per key+ if (!keyList.empty()) {+ uint16_t inferredBits = 0;+ if (!bitList.empty()) inferredBits = (uint16_t)std::max(0, bitList.back());+ else if (!bitRawList.empty()) inferredBits = (uint16_t)std::max(0, bitRawList.back());+ else {+ for (uint64_t k : keyList) {+ uint16_t b = 0;+ while ((k >> b) && b < 64) ++b;+ inferredBits = std::max(inferredBits, (uint16_t)std::max(1, (int)b));+ }+ }++ for (uint64_t k : keyList) {+ SubGhzFileCommand cmd;+ cmd.protocol = (protocol == SubGhzProtocolEnum::Unknown)+ ? SubGhzProtocolEnum::RcSwitch+ : protocol;+ cmd.preset = preset;+ cmd.frequency_hz = frequency;+ cmd.te_us = te;+ cmd.key = k;+ cmd.bits = inferredBits ? inferredBits : 24;+ cmd.source_file = sourcePath;+ out.emplace_back(std::move(cmd));+ }+ return;}- // Keys (hex → u64) pour RcSwitch/Princeton- for (uint64_t k : keyList) {+ // No key, but bits+ if (!bitList.empty() || !bitRawList.empty()) {+ const int b = !bitList.empty() ? bitList.back() : bitRawList.back();SubGhzFileCommand cmd;- cmd.protocol = (protocol==SubGhzProtocolEnum::Unknown) ? SubGhzProtocolEnum::RcSwitch : protocol;- cmd.preset = preset;- cmd.frequency_hz = frequency;- cmd.te_us = te;- cmd.key = k;- cmd.source_file = sourcePath;+ cmd.protocol = (protocol == SubGhzProtocolEnum::Unknown)+ ? SubGhzProtocolEnum::RcSwitch+ : protocol;+ cmd.preset = preset;+ cmd.frequency_hz = frequency;+ cmd.te_us = te;+ cmd.bits = (uint16_t)std::max(0, b);+ cmd.key = 0;+ cmd.source_file = sourcePath;out.emplace_back(std::move(cmd));}}@@ -250,7 +283,7 @@ std::vector<int32_t> SubGhzTransformer::parseRawTimingsJoined(const std::vector<}std::vector<uint8_t> SubGhzTransformer::parseHexByteStream(const std::string& hexLine) {- // Accepte "A1 b2 0C" etc.+ // Accept "A1 b2 0C" etcstd::vector<uint8_t> out;std::istringstream iss(hexLine);std::string tok;diff --git a/src/Transformers/SubGhzTransformer.h b/src/Transformers/SubGhzTransformer.hindex fcd60fe7..61b2ec71 100644--- a/src/Transformers/SubGhzTransformer.h+++ b/src/Transformers/SubGhzTransformer.h@@ -67,6 +67,7 @@ private:const std::vector<int>& bitRawList,const std::vector<uint64_t>& keyList,const std::vector<std::string>& rawDataLines,+ const std::vector<uint8_t>& binRawBytes,std::vector<SubGhzFileCommand>& out,const std::string& sourcePath);