27#include <libusb-1.0/libusb.h>
28#include <radio_tool/h8sx/h8sx_exception.hpp>
30#define CHECK_ERR(errstr) \
33 if (err < LIBUSB_SUCCESS) \
35 std::string e = std::string(errstr) + \
36 std::string(libusb_error_name(err)); \
37 throw radio_tool::h8sx::H8SXException(e); \
42#define PACK(__Declaration__) __Declaration__ __attribute__((__packed__))
43#elif defined(_MSC_VER)
44#define PACK(__Declaration__) __pragma(pack(push, 1)) __Declaration__ __pragma(pack(pop))
47#define BULK_EP_IN 0x82
48#define BULK_EP_OUT 0x01
49#define BUF_SIZE 64 * 1024
53 enum class H8SXCmd : uint8_t
63 DEVICE_INQUIRY = 0x20,
74 CLOCK_MODE_INQUIRY = 0x21,
79 CLOCK_MODE_SELECT = 0x11,
84 PROG_UNIT_INQUIRY = 0x27,
89 BITRATE_SELECT = 0x3F,
95 BEGIN_PROGRAMMING = 0x40,
101 USER_MAT_SELECT = 0x43,
111 USER_MAT_CHECKSUM = 0x4B,
115 PACK(
struct dev_inq_hdr_t {
123 PACK(
struct dev_sel_t {
130 PACK(
struct prog_chunk_t {
131 uint8_t cmd =
static_cast<uint8_t
>(H8SXCmd::PROGRAM_128B);
133 uint8_t data[1024] = {0};
137 PACK(
struct prog_end_t {
138 uint8_t cmd =
static_cast<uint8_t
>(H8SXCmd::PROGRAM_128B);
139 uint32_t addr = 0xffffffff;
143 PACK(
struct sum_chk_t {
153 H8SX(libusb_device_handle *device)
154 : timeout(5000), device(device) {}
156 auto Init()
const -> void;
157 auto IdentifyDevice()
const -> std::string;
158 auto Download(
const std::vector<uint8_t> &)
const -> void;
161 libusb_context *usb_ctx;
163 auto GetDeviceString(
const libusb_device_descriptor &, libusb_device_handle *)
const -> std::wstring;
164 auto Checksum(
const uint8_t *data,
const size_t len)
const -> uint8_t;
167 const uint16_t timeout;
168 libusb_device_handle *device;
169 struct dev_inq_hdr_t *dir;
171 auto CheckDevice()
const -> void;