diff --git a/include/radio_tool/hid/tyt_hid.hpp b/include/radio_tool/hid/tyt_hid.hpp index 31dbe8d..fe35249 100644 --- a/include/radio_tool/hid/tyt_hid.hpp +++ b/include/radio_tool/hid/tyt_hid.hpp @@ -91,9 +91,9 @@ namespace radio_tool::hid auto Setup() -> void; - auto SendCommand(const tyt::Command& cmd) -> void; - auto SendCommand(const std::vector& cmd) -> void; - auto SendCommand(const std::vector& cmd, const uint8_t& size, const uint8_t& fill) -> void; + auto SendCommand(const tyt::Command& cmd) -> tyt::Command; + auto SendCommand(const std::vector& cmd) -> tyt::Command; + auto SendCommand(const std::vector& cmd, const uint8_t& size, const uint8_t& fill) -> tyt::Command; auto SendCommandAndOk(const tyt::Command& cmd) -> void; auto SendCommandAndOk(const std::vector& cmd) -> void; diff --git a/src/tyt_hid.cpp b/src/tyt_hid.cpp index 6946ac9..2654cdf 100644 --- a/src/tyt_hid.cpp +++ b/src/tyt_hid.cpp @@ -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( @@ -55,6 +55,7 @@ auto TYTHID::Setup() -> void }, this, 5000); libusb_submit_transfer(tx); + */ } auto TYTHID::OnTransfer(libusb_transfer* tx) -> void @@ -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 payload(std::max(42, (int)cmd.data.size())); + std::vector payload((int)cmd.data.size() + 4); std::fill(payload.begin(), payload.end(), 0x00); auto nums = (uint16_t*)payload.data(); @@ -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(data.begin() + 4, data.begin() + 4 + len)); } -auto TYTHID::SendCommand(const std::vector& cmd) -> void +auto TYTHID::SendCommand(const std::vector& 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& cmd, const uint8_t& size, const uint8_t& fill) -> void +auto TYTHID::SendCommand(const std::vector& cmd, const uint8_t& size, const uint8_t& fill) -> tyt::Command { auto ncmd = std::vector(size, fill); std::copy(cmd.begin(), cmd.end(), ncmd.begin()); - SendCommand(ncmd); + return SendCommand(ncmd); } auto TYTHID::WaitForReply() -> tyt::Command @@ -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()); @@ -141,8 +146,7 @@ auto TYTHID::SendCommandAndOk(const tyt::Command& cmd) -> void auto TYTHID::SendCommandAndOk(const std::vector& cmd) -> void { - SendCommand(cmd); - auto ok = WaitForReply(); + auto ok = SendCommand(cmd); if (!(ok == tyt::OKResponse)) { radio_tool::PrintHex(ok.data.begin(), ok.data.end()); @@ -152,8 +156,7 @@ auto TYTHID::SendCommandAndOk(const std::vector& cmd) -> void auto TYTHID::SendCommandAndOk(const std::vector& 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()); diff --git a/src/tyt_sgl_radio.cpp b/src/tyt_sgl_radio.cpp index 0609009..836b8a5 100644 --- a/src/tyt_sgl_radio.cpp +++ b/src/tyt_sgl_radio.cpp @@ -23,6 +23,7 @@ #include #include #include +#include "radio_tool/util.hpp" using namespace radio_tool::radio; @@ -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); @@ -68,8 +68,7 @@ auto TYTSGLRadio::WriteFirmware(const std::string &file) -> void } // send key - device.SendCommand(std::vector(config->header.model_key.begin(), config->header.model_key.end()), 0x08, 0xff); - auto rsp_key = device.WaitForReply(); + auto rsp_key = device.SendCommand(std::vector(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()); @@ -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(config->header.radio_group.begin(), config->header.radio_group.end()), 0x10, 0xff); device.SendCommandAndOk(std::vector(config->header.radio_model.begin(), config->header.radio_model.end()), 0x08, 0xff); device.SendCommandAndOk(std::vector(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; @@ -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); @@ -117,11 +116,12 @@ auto TYTSGLRadio::WriteFirmware(const std::string &file) -> void auto checksumCommand = std::vector(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; + } } } diff --git a/src/usb_radio_factory.cpp b/src/usb_radio_factory.cpp index 884ff92..a4050f8 100644 --- a/src/usb_radio_factory.cpp +++ b/src/usb_radio_factory.cpp @@ -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);