20#include <libusb-1.0/libusb.h>
21#include <radio_tool/hid/hid.hpp>
24#include <condition_variable>
32 const std::vector<uint8_t> A = {
'A' };
33 const std::vector<uint8_t> Update = {
'#',
'U',
'P',
'D',
'A',
'T',
'E',
'?' };
34 const std::vector<uint8_t> Download = {
'D',
'O',
'W',
'N',
'L',
'O',
'A',
'D' };
35 const std::vector<uint8_t> FlashProgram = {
'F',
'-',
'P',
'R',
'O',
'G' };
36 const std::vector<uint8_t> FlashErase = {
'F',
'-',
'E',
'R',
'A',
'S',
'E' };
37 const std::vector<uint8_t> EraseROM = {
'E',
'R',
'A',
'S',
'E',
'R',
'O',
'M' };
38 const std::vector<uint8_t> FlashVersion = {
'F',
'-',
'V',
'E',
'R' };
39 const std::vector<uint8_t> FlashCompany = {
'F',
'-',
'C',
'O' };
40 const std::vector<uint8_t> FlashSerialNumber = {
'F',
'-',
'S',
'N' };
41 const std::vector<uint8_t> FlashTime = {
'F',
'-',
'T',
'I',
'M',
'E' };
42 const std::vector<uint8_t> FlashMod = {
'F',
'-',
'M',
'O',
'D' };
43 const std::vector<uint8_t> Program = {
'P',
'R',
'O',
'G',
'R',
'A',
'M' };
44 const std::vector<uint8_t> End = {
'E',
'N',
'D' };
47 enum class CommandType : uint16_t
56 Command(
const CommandType& t,
const uint16_t& l,
const std::vector<uint8_t>& d)
57 : type(t), length(l), data(d) {}
59 const CommandType type;
60 const uint16_t length;
61 const std::vector<uint8_t> data;
63 auto operator==(
const Command& other)
const ->
bool
65 return type == other.type && length == other.length && std::equal(data.begin(), data.end(), other.data.begin());
72 const Command OK =
Command(CommandType::HostToDevice, 1, commands::A);
77 const Command OKResponse =
Command(CommandType::DeviceToHost, 1, commands::A);
83 const unsigned char EP_IN = LIBUSB_ENDPOINT_IN | 0x01;
84 const unsigned char EP_OUT = LIBUSB_ENDPOINT_OUT | 0x02;
86 static const auto VID = 0x15a2;
87 static const auto PID = 0x0073;
89 TYTHID(libusb_device_handle* device)
90 :
HID(device), signalCallback(), signalReady(), tx(
nullptr) {}
95 auto SendCommand(
const std::vector<uint8_t>& cmd) -> void;
96 auto SendCommand(
const std::vector<uint8_t>& cmd,
const uint8_t& size,
const uint8_t& fill) -> void;
99 auto SendCommandAndOk(
const std::vector<uint8_t>& cmd) -> void;
100 auto SendCommandAndOk(
const std::vector<uint8_t>& cmd,
const uint8_t& size,
const uint8_t& fill) -> void;
102 auto WaitForReply()->tyt::Command;
104 auto OnTransfer(libusb_transfer* tx) -> void;
107 std::mutex signalCallback;
108 std::condition_variable signalReady;
109 struct libusb_transfer* tx;