radio_tool 0.2.1
Loading...
Searching...
No Matches
tyt_sgl_radio.cpp
1
18#include <radio_tool/radio/tyt_sgl_radio.hpp>
19#include <radio_tool/radio/radio_factory.hpp>
20#include <radio_tool/fw/tyt_fw_sgl.hpp>
21
22#include <math.h>
23#include <iomanip>
24#include <iostream>
25#include <vector>
26
27using namespace radio_tool::radio;
28
29TYTSGLRadio::TYTSGLRadio(libusb_device_handle *h) : device(h)
30{
31 device.Setup();
32}
33
34auto TYTSGLRadio::ToString() const -> const std::string
35{
36 std::stringstream out;
37
38 out << "== TYT SGL Radio Info ==" << std::endl
39 << "Radio: "
40 << "ASD" << std::endl
41 << "RTC: "
42 << "000";
43
44 return out.str();
45}
46
47auto TYTSGLRadio::WriteFirmware(const std::string &file) -> void
48{
49 fw::TYTSGLFW fw;
50 fw.Read(file);
51 auto config = fw.GetConfig();
52
53 device.SendCommand(hid::tyt::commands::Download);
54 auto rsp = device.WaitForReply();
55 if (std::equal(rsp.data.begin(), rsp.data.end(), hid::tyt::commands::Update.begin()))
56 {
57 device.SendCommandAndOk(hid::tyt::OK);
58 }
59 else
60 {
61 auto download_rsp = std::string(rsp.data.begin(), rsp.data.end());
62 auto update_rsp = std::string(hid::tyt::commands::Update.begin(), hid::tyt::commands::Update.end());
63 std::stringstream msg("Invalid response, expected '");
64 msg << update_rsp << "'"
65 << " got '" << download_rsp << "'";
66
67 throw new std::runtime_error(msg.str());
68 }
69
70 // send key
71 device.SendCommand(std::vector<uint8_t>(config->header.model_key.begin(), config->header.model_key.end()), 0x08, 0xff);
72 auto rsp_key = device.WaitForReply();
73 if (!std::equal(rsp_key.data.begin(), rsp_key.data.end(), config->header.model_key.begin()))
74 {
75 auto key_rsp = std::string(rsp_key.data.begin(), rsp_key.data.end());
76 std::stringstream msg("Invalid response, expected firmware key '");
77 msg << config->header.model_key << "'"
78 << " got '" << key_rsp << "'";
79
80 throw new std::runtime_error(msg.str());
81 }
82
83 device.SendCommandAndOk(hid::tyt::commands::FlashProgram);
84
85 device.SendCommandAndOk(std::vector<uint8_t>(config->header.radio_group.begin(), config->header.radio_group.end()), 0x10, 0xff);
86 device.SendCommandAndOk(std::vector<uint8_t>(config->header.radio_model.begin(), config->header.radio_model.end()), 0x08, 0xff);
87 device.SendCommandAndOk(std::vector<uint8_t>(config->header.protocol_version.begin(), config->header.protocol_version.end()));
88
89 device.SendCommandAndOk(hid::tyt::commands::FlashErase);
90 device.SendCommandAndOk(hid::tyt::OK);
91 device.SendCommandAndOk(hid::tyt::commands::Program);
92
93 constexpr auto TransferSize = 0x20u;
94 constexpr auto HeaderSize = 0x06u;
95 constexpr auto ChecksumBlockSize = 0x400u;
96
97 auto buf = std::vector<uint8_t>(TransferSize + HeaderSize);
98 auto binary = fw.GetDataSegments()[0];
99 auto address = 0;
100 auto checksumBlock = 0;
101 while (address < binary.size)
102 {
103 auto transferSize = std::min(TransferSize, binary.size - address);
104 *(uint32_t *)buf.data() = address;
105 *(uint16_t *)(buf.data() + 4) = transferSize;
106
107 auto src = binary.data.begin() + address;
108 std::copy(src, src + transferSize, buf.begin() + HeaderSize);
109
110 device.SendCommandAndOk(buf);
111
112 address += transferSize;
113 if (address % ChecksumBlockSize == 0 || address == binary.size)
114 {
115 auto start = ChecksumBlockSize * checksumBlock;
116 auto end = address;
117
118 auto checksumCommand = std::vector<uint8_t>(hid::tyt::commands::End.size() + 5, 0xff);
119 std::copy(hid::tyt::commands::End.begin(), hid::tyt::commands::End.end(), checksumCommand.begin());
120 *(uint32_t *)(checksumCommand.data() + hid::tyt::commands::End.size()) = checksum(binary.data.begin() + start, binary.data.begin() + end);
121 device.SendCommandAndOk(checksumCommand);
122
123 checksumBlock++;
124 }
125 }
126}
127
128auto TYTSGLRadio::checksum(std::vector<uint8_t>::const_iterator &&begin, std::vector<uint8_t>::const_iterator &&end) const -> uint32_t
129{
130 uint32_t counter = 0;
131 while (begin != end)
132 {
133 counter += *begin;
134 std::advance(begin, 1);
135 }
136 return counter;
137}
virtual auto GetDataSegments() const -> const std::vector< FirmwareSegment >
Definition: fw.hpp:111
auto Read(const std::string &file) -> void override
Definition: tyt_fw_sgl.cpp:36
auto WriteFirmware(const std::string &file) -> void override
auto ToString() const -> const std::string override