radio_tool 0.2.1
Loading...
Searching...
No Matches
tyt_fw_sgl.hpp
1
18#pragma once
19
20#include <radio_tool/fw/fw.hpp>
21#include <radio_tool/fw/cipher/sgl.hpp>
22
23#include <fstream>
24#include <cstring>
25#include <sstream>
26#include <iomanip>
27
28namespace radio_tool::fw
29{
30 constexpr auto NotAscii = [](unsigned char ch) {
31 return !(ch >= ' ' && ch <= '~');
32 };
33
35 {
36 public:
38 const uint16_t& sgl_version,
39 const uint32_t& len,
40 const std::string& group,
41 const std::string& model,
42 const std::string& version,
43 const std::string& key,
44 const uint8_t& binary_offset,
45 const uint16_t& header2_offset)
46 : sgl_version(sgl_version),
47 length(len),
48 binary_offset(binary_offset),
49 header2_offset(header2_offset),
50 radio_group(group.begin(), std::find_if(group.begin(), group.end(), NotAscii)),
51 radio_model(model.begin(), std::find_if(model.begin(), model.end(), NotAscii)),
52 protocol_version(version),
53 model_key(key)
54 {
55 if (sgl_version > 0x100) {
56 throw std::runtime_error("Version max is 0x100");
57 }
58 if (binary_offset > 0x80) {
59 throw std::runtime_error("Binary offset max is 0x80");
60 }
61 if (header2_offset < 0x1e || header2_offset > 0x100) {
62 throw std::runtime_error("Header 2 offset must be between 0x1e <-> 0x100");
63 }
64 }
65
66 auto ToString() const->std::string;
67
68 auto Serialize(bool encrypt = true) const->std::vector<uint8_t>;
69
73 auto AsNew(const uint32_t& binary_len) const -> const SGLHeader;
74
78 auto IsCompatible(const SGLHeader& other) const -> bool;
79
80 const uint16_t sgl_version;
81 const uint32_t length;
82 const uint8_t binary_offset;
83 const uint16_t header2_offset;
84 const std::string radio_group; //BF-DMR = 0x10
85 const std::string radio_model; //1801 = 0x08
86 const std::string protocol_version; //V1.00.1 = 0x08
87 const std::string model_key; //DV01xxxx = 0x08
88 };
89
94 {
95 public:
96 TYTSGLRadioConfig(const std::string& model, const SGLHeader& header, const uint8_t* cipher, const uint32_t& cipher_l, const uint16_t& xor_offset)
98 {
99 }
100
104 const std::string radio_model;
105
110
114 const uint8_t* cipher;
115
119 const uint32_t cipher_len;
120
124 const uint16_t xor_offset;
125 };
126
127 namespace tyt::config::sgl
128 {
129 const std::vector<uint8_t> Magic = { 'S', 'G', 'L', '!' };
130
131 const std::vector<TYTSGLRadioConfig> All = {
132 TYTSGLRadioConfig("GD77", SGLHeader(1, 0, "SG-MD-760", "MD-760", "V1.00.01", "DV01xxx", 0x00, 0xff), fw::cipher::sgl, fw::cipher::sgl_length, 0x807),
133 TYTSGLRadioConfig("GD77S", SGLHeader(1, 0, "SG-MD-730", "MD-730", "V1.00.01", "DV02xxx", 0x00, 0xff), fw::cipher::sgl, fw::cipher::sgl_length, 0x2a8e),
134 TYTSGLRadioConfig("BF5R", SGLHeader(1, 0, "BF-5R", "BF-5R", "V1.00.01", "DV02xxx", 0x00, 0xff), fw::cipher::sgl, fw::cipher::sgl_length, 0x306e),
135 TYTSGLRadioConfig("DM1801", SGLHeader(1, 0, "BF-DMR", "1801", "V1.00.01", "DV03xxx", 0x00, 0xff), fw::cipher::sgl, fw::cipher::sgl_length, 0x2c7c),
136 };
137 }
138
140 {
141 public:
142 TYTSGLFW() : FirmwareSupport(), config(nullptr) {}
143
144 auto Read(const std::string& file) -> void override;
145 auto Write(const std::string& file) -> void override;
146 auto ToString() const->std::string override;
147 auto Decrypt() -> void override;
148 auto Encrypt() -> void override;
149 auto SetRadioModel(const std::string&) -> void override;
150 auto IsCompatible(const FirmwareSupport* Other) const -> bool override;
151
152 auto GetConfig() const -> const TYTSGLRadioConfig* {
153 return config;
154 }
155
159 auto GetRadioModel() const -> const std::string override;
160
164 static auto SupportsFirmwareFile(const std::string& file) -> bool;
165
169 static auto SupportsRadioModel(const std::string& model) -> bool;
170
174 static auto ReadHeader(const std::string& file) -> const SGLHeader;
175
179 static auto Create() -> std::unique_ptr<FirmwareSupport>
180 {
181 return std::make_unique<TYTSGLFW>();
182 }
183
184 private:
185 const TYTSGLRadioConfig* config;
186 };
187
188} // namespace radio_tool::fw
auto IsCompatible(const SGLHeader &other) const -> bool
Definition: tyt_fw_sgl.cpp:347
auto AsNew(const uint32_t &binary_len) const -> const SGLHeader
Definition: tyt_fw_sgl.cpp:265
auto SetRadioModel(const std::string &) -> void override
Definition: tyt_fw_sgl.cpp:194
static auto SupportsRadioModel(const std::string &model) -> bool
Definition: tyt_fw_sgl.cpp:174
auto IsCompatible(const FirmwareSupport *Other) const -> bool override
Definition: tyt_fw_sgl.cpp:235
auto GetRadioModel() const -> const std::string override
Definition: tyt_fw_sgl.cpp:186
auto Decrypt() -> void override
Definition: tyt_fw_sgl.cpp:206
static auto Create() -> std::unique_ptr< FirmwareSupport >
Definition: tyt_fw_sgl.hpp:179
auto Read(const std::string &file) -> void override
Definition: tyt_fw_sgl.cpp:36
static auto ReadHeader(const std::string &file) -> const SGLHeader
Definition: tyt_fw_sgl.cpp:107
auto Write(const std::string &file) -> void override
Definition: tyt_fw_sgl.cpp:68
static auto SupportsFirmwareFile(const std::string &file) -> bool
Definition: tyt_fw_sgl.cpp:159
auto ToString() const -> std::string override
Definition: tyt_fw_sgl.cpp:87
auto Encrypt() -> void override
Definition: tyt_fw_sgl.cpp:216