Skip to content

Commit

Permalink
Merge pull request #26 from cifred98/main
Browse files Browse the repository at this point in the history
Fix Fw Write for SGL radios
  • Loading branch information
v0l committed Oct 5, 2023
2 parents 070f8ca + 356c68b commit 482eb6f
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 33 deletions.
6 changes: 3 additions & 3 deletions include/radio_tool/hid/tyt_hid.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -91,9 +91,9 @@ namespace radio_tool::hid

auto Setup() -> void;

auto SendCommand(const tyt::Command& cmd) -> void;
auto SendCommand(const std::vector<uint8_t>& cmd) -> void;
auto SendCommand(const std::vector<uint8_t>& cmd, const uint8_t& size, const uint8_t& fill) -> void;
auto SendCommand(const tyt::Command& cmd) -> tyt::Command;
auto SendCommand(const std::vector<uint8_t>& cmd) -> tyt::Command;
auto SendCommand(const std::vector<uint8_t>& cmd, const uint8_t& size, const uint8_t& fill) -> tyt::Command;

auto SendCommandAndOk(const tyt::Command& cmd) -> void;
auto SendCommandAndOk(const std::vector<uint8_t>& cmd) -> void;
Expand Down
8 changes: 8 additions & 0 deletions include/radio_tool/util.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,14 @@
#include <iterator>
#include <iomanip>

#if defined(_MSC_VER)
#define bswap32(x) _byteswap_ulong((x))
#define bswap16(x) _byteswap_ushort((x))
#else
#define bswap32(x) __builtin_bswap32((x))
#define bswap16(x) __builtin_bswap16((x))
#endif

namespace radio_tool
{
constexpr auto kiB = (1UL << 10);
Expand Down
7 changes: 1 addition & 6 deletions src/h8sx.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,7 @@
#include <cstring>
#include <exception>
#include <thread>

#if defined(_MSC_VER)
#define bswap32(x) _byteswap_ulong((x))
#else
#define bswap32(x) __builtin_bswap32((x))
#endif
#include "radio_tool/util.hpp"

using namespace radio_tool::h8sx;

Expand Down
29 changes: 16 additions & 13 deletions src/tyt_hid.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ auto TYTHID::Setup() -> void
libusb_close(device);
throw std::runtime_error(libusb_error_name(err));
}

/*
auto buffer = (uint8_t*)malloc(64);
auto tx = libusb_alloc_transfer(0);
libusb_fill_interrupt_transfer(
Expand All @@ -55,6 +55,7 @@ auto TYTHID::Setup() -> void
},
this, 5000);
libusb_submit_transfer(tx);
*/
}

auto TYTHID::OnTransfer(libusb_transfer* tx) -> void
Expand All @@ -79,9 +80,9 @@ auto TYTHID::OnTransfer(libusb_transfer* tx) -> void
libusb_submit_transfer(tx);
}

auto TYTHID::SendCommand(const tyt::Command& cmd) -> void
auto TYTHID::SendCommand(const tyt::Command& cmd) -> tyt::Command
{
std::vector<uint8_t> payload(std::max(42, (int)cmd.data.size()));
std::vector<uint8_t> payload((int)cmd.data.size() + 4);
std::fill(payload.begin(), payload.end(), 0x00);

auto nums = (uint16_t*)payload.data();
Expand All @@ -90,19 +91,24 @@ auto TYTHID::SendCommand(const tyt::Command& cmd) -> void
std::copy(cmd.data.begin(), cmd.data.end(), payload.begin() + 4);

InterruptWrite(TYTHID::EP_OUT, payload);
auto data = InterruptRead(TYTHID::EP_IN, 42);
auto type = ((uint16_t)data[1] << 8) | data[0];
auto len = ((uint16_t)data[3] << 8) | data[2];
return tyt::Command((tyt::CommandType)type, len,
std::vector<uint8_t>(data.begin() + 4, data.begin() + 4 + len));
}

auto TYTHID::SendCommand(const std::vector<uint8_t>& cmd) -> void
auto TYTHID::SendCommand(const std::vector<uint8_t>& cmd) -> tyt::Command
{
SendCommand(tyt::Command(tyt::CommandType::DeviceToHost, cmd.size(), cmd));
return SendCommand(tyt::Command(tyt::CommandType::HostToDevice, cmd.size(), cmd));
}

auto TYTHID::SendCommand(const std::vector<uint8_t>& cmd, const uint8_t& size, const uint8_t& fill) -> void
auto TYTHID::SendCommand(const std::vector<uint8_t>& cmd, const uint8_t& size, const uint8_t& fill) -> tyt::Command
{
auto ncmd = std::vector<uint8_t>(size, fill);
std::copy(cmd.begin(), cmd.end(), ncmd.begin());

SendCommand(ncmd);
return SendCommand(ncmd);
}

auto TYTHID::WaitForReply() -> tyt::Command
Expand Down Expand Up @@ -130,8 +136,7 @@ auto TYTHID::WaitForReply() -> tyt::Command

auto TYTHID::SendCommandAndOk(const tyt::Command& cmd) -> void
{
SendCommand(cmd);
auto ok = WaitForReply();
auto ok = SendCommand(cmd);
if (!(ok == tyt::OKResponse))
{
radio_tool::PrintHex(ok.data.begin(), ok.data.end());
Expand All @@ -141,8 +146,7 @@ auto TYTHID::SendCommandAndOk(const tyt::Command& cmd) -> void

auto TYTHID::SendCommandAndOk(const std::vector<uint8_t>& cmd) -> void
{
SendCommand(cmd);
auto ok = WaitForReply();
auto ok = SendCommand(cmd);
if (!(ok == tyt::OKResponse))
{
radio_tool::PrintHex(ok.data.begin(), ok.data.end());
Expand All @@ -152,8 +156,7 @@ auto TYTHID::SendCommandAndOk(const std::vector<uint8_t>& cmd) -> void

auto TYTHID::SendCommandAndOk(const std::vector<uint8_t>& cmd, const uint8_t& size, const uint8_t& fill) -> void
{
SendCommand(cmd, size, fill);
auto ok = WaitForReply();
auto ok = SendCommand(cmd, size, fill);
if (!(ok == tyt::OKResponse))
{
radio_tool::PrintHex(ok.data.begin(), ok.data.end());
Expand Down
22 changes: 11 additions & 11 deletions src/tyt_sgl_radio.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include <iomanip>
#include <iostream>
#include <vector>
#include "radio_tool/util.hpp"

using namespace radio_tool::radio;

Expand Down Expand Up @@ -50,8 +51,7 @@ auto TYTSGLRadio::WriteFirmware(const std::string &file) -> void
fw.Read(file);
auto config = fw.GetConfig();

device.SendCommand(hid::tyt::commands::Download);
auto rsp = device.WaitForReply();
auto rsp = device.SendCommand(hid::tyt::commands::Download);
if (std::equal(rsp.data.begin(), rsp.data.end(), hid::tyt::commands::Update.begin()))
{
device.SendCommandAndOk(hid::tyt::OK);
Expand All @@ -68,8 +68,7 @@ auto TYTSGLRadio::WriteFirmware(const std::string &file) -> void
}

// send key
device.SendCommand(std::vector<uint8_t>(config->header.model_key.begin(), config->header.model_key.end()), 0x08, 0xff);
auto rsp_key = device.WaitForReply();
auto rsp_key = device.SendCommand(std::vector<uint8_t>(config->header.model_key.begin(), config->header.model_key.end()), 0x08, 0xff);
if (!std::equal(rsp_key.data.begin(), rsp_key.data.end(), config->header.model_key.begin()))
{
auto key_rsp = std::string(rsp_key.data.begin(), rsp_key.data.end());
Expand All @@ -80,15 +79,15 @@ auto TYTSGLRadio::WriteFirmware(const std::string &file) -> void
throw new std::runtime_error(msg.str());
}

device.SendCommandAndOk(hid::tyt::commands::FlashProgram);
device.SendCommandAndOk(hid::tyt::commands::FlashProgram, 0x08, 0xff);

device.SendCommandAndOk(std::vector<uint8_t>(config->header.radio_group.begin(), config->header.radio_group.end()), 0x10, 0xff);
device.SendCommandAndOk(std::vector<uint8_t>(config->header.radio_model.begin(), config->header.radio_model.end()), 0x08, 0xff);
device.SendCommandAndOk(std::vector<uint8_t>(config->header.protocol_version.begin(), config->header.protocol_version.end()));

device.SendCommandAndOk(hid::tyt::commands::FlashErase);
device.SendCommandAndOk(hid::tyt::commands::FlashErase, 0x08, 0xff);
device.SendCommandAndOk(hid::tyt::OK);
device.SendCommandAndOk(hid::tyt::commands::Program);
device.SendCommandAndOk(hid::tyt::commands::Program, 0x08, 0xff);

constexpr auto TransferSize = 0x20u;
constexpr auto HeaderSize = 0x06u;
Expand All @@ -101,8 +100,8 @@ auto TYTSGLRadio::WriteFirmware(const std::string &file) -> void
while (address < binary.size)
{
auto transferSize = std::min(TransferSize, binary.size - address);
*(uint32_t *)buf.data() = address;
*(uint16_t *)(buf.data() + 4) = transferSize;
*(uint32_t *)buf.data() = bswap32(address);
*(uint16_t *)(buf.data() + 4) = bswap16(transferSize);

auto src = binary.data.begin() + address;
std::copy(src, src + transferSize, buf.begin() + HeaderSize);
Expand All @@ -117,11 +116,12 @@ auto TYTSGLRadio::WriteFirmware(const std::string &file) -> void

auto checksumCommand = std::vector<uint8_t>(hid::tyt::commands::End.size() + 5, 0xff);
std::copy(hid::tyt::commands::End.begin(), hid::tyt::commands::End.end(), checksumCommand.begin());
*(uint32_t *)(checksumCommand.data() + hid::tyt::commands::End.size()) = checksum(binary.data.begin() + start, binary.data.begin() + end);
*(uint32_t *)(checksumCommand.data() + hid::tyt::commands::End.size() + 1) = checksum(binary.data.begin() + start, binary.data.begin() + end);
device.SendCommandAndOk(checksumCommand);

checksumBlock++;
}
std::cerr << "Sent block " << checksumBlock << std::endl;
}
}
}

Expand Down
6 changes: 6 additions & 0 deletions src/usb_radio_factory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,12 @@ auto USBRadioFactory::ListDevices(const uint16_t &idx_offset) const -> const std
mfg = L"Yaesu";
prd = L"FT-70D";
}
// Raddioddity & others
else if (desc.idVendor == hid::TYTHID::VID && desc.idProduct == hid::TYTHID::PID)
{
mfg = L"TYT";
prd = L"SGL";
}
else
{
mfg = GetDeviceString(desc.iManufacturer, h);
Expand Down

0 comments on commit 482eb6f

Please sign in to comment.