radio_tool 0.2.1
Loading...
Searching...
No Matches
tyt_hid.hpp
1
18#pragma once
19
20#include <libusb-1.0/libusb.h>
21#include <radio_tool/hid/hid.hpp>
22
23#include <mutex>
24#include <condition_variable>
25
26namespace radio_tool::hid
27{
28 namespace tyt
29 {
30 namespace commands
31 {
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' }; // ? write time ?
42 const std::vector<uint8_t> FlashMod = { 'F', '-', 'M', 'O', 'D' }; // ? mode ?
43 const std::vector<uint8_t> Program = { 'P', 'R', 'O', 'G', 'R', 'A', 'M' };
44 const std::vector<uint8_t> End = { 'E', 'N', 'D' };
45 };
46
47 enum class CommandType : uint16_t
48 {
49 HostToDevice = 0x01,
50 DeviceToHost = 0x03
51 };
52
53 class Command
54 {
55 public:
56 Command(const CommandType& t, const uint16_t& l, const std::vector<uint8_t>& d)
57 : type(t), length(l), data(d) {}
58
59 const CommandType type;
60 const uint16_t length;
61 const std::vector<uint8_t> data;
62
63 auto operator==(const Command& other) const -> bool
64 {
65 return type == other.type && length == other.length && std::equal(data.begin(), data.end(), other.data.begin());
66 }
67 };
68
72 const Command OK = Command(CommandType::HostToDevice, 1, commands::A);
73
77 const Command OKResponse = Command(CommandType::DeviceToHost, 1, commands::A);
78 };
79
80 class TYTHID : public HID
81 {
82 public:
83 const unsigned char EP_IN = LIBUSB_ENDPOINT_IN | 0x01;
84 const unsigned char EP_OUT = LIBUSB_ENDPOINT_OUT | 0x02;
85
86 static const auto VID = 0x15a2;
87 static const auto PID = 0x0073;
88
89 TYTHID(libusb_device_handle* device)
90 : HID(device), signalCallback(), signalReady(), tx(nullptr) {}
91
92 auto Setup() -> void;
93
94 auto SendCommand(const tyt::Command& cmd) -> void;
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;
97
98 auto SendCommandAndOk(const tyt::Command& cmd) -> 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;
101
102 auto WaitForReply()->tyt::Command;
103
104 auto OnTransfer(libusb_transfer* tx) -> void;
105
106 private:
107 std::mutex signalCallback;
108 std::condition_variable signalReady;
109 struct libusb_transfer* tx;
110 };
111}