libdrmconf 0.12.1
A library to program DMR radios.
Loading...
Searching...
No Matches
dfu_libusb.hh
1#ifndef DFU_LIBUSB_HH
2#define DFU_LIBUSB_HH
3
4#include <QObject>
5#include <libusb.h>
6#include "errorstack.hh"
7#include "radiointerface.hh"
8
16class DFUDevice: public QObject
17{
18 Q_OBJECT
19
20private:
22 struct __attribute__((packed)) status_t {
23 unsigned status : 8;
24 unsigned poll_timeout : 24;
25 unsigned state : 8;
26 unsigned string_index : 8;
27 };
28
29public:
32 {
33 public:
35 Descriptor(const USBDeviceInfo &info, uint8_t bus, uint8_t device);
36 };
37
38public:
40 DFUDevice(const USBDeviceDescriptor &descr, const ErrorStack &err=ErrorStack(), QObject *parent=nullptr);
42 virtual ~DFUDevice();
43
45 bool isOpen() const;
47 void close();
48
50 int download(unsigned block, uint8_t *data, unsigned len, const ErrorStack &err=ErrorStack());
52 int upload(unsigned block, uint8_t *data, unsigned len, const ErrorStack &err=ErrorStack());
53
54public:
56 static QList<USBDeviceDescriptor> detect(uint16_t vid, uint16_t pid);
57
58protected:
60 int detach(int timeout, const ErrorStack &err=ErrorStack());
62 int get_status(const ErrorStack &err=ErrorStack());
64 int clear_status(const ErrorStack &err=ErrorStack());
66 int get_state(int &pstate, const ErrorStack &err=ErrorStack());
68 int abort(const ErrorStack &err=ErrorStack());
70 int wait_idle(const ErrorStack &err=ErrorStack());
71
72protected:
74 libusb_context *_ctx;
76 libusb_device_handle *_dev;
78 status_t _status;
79};
80
81
88{
89public:
92 DFUSEDevice(const USBDeviceDescriptor &descr, const ErrorStack &err=ErrorStack(), uint16_t blocksize=32, QObject *parent=nullptr);
93
95 void close();
96
98 uint16_t blocksize() const;
99
102 bool setAddress(uint32_t address, const ErrorStack &err=ErrorStack());
105 bool readBlock(unsigned block, uint8_t *data, const ErrorStack &err=ErrorStack());
108 bool writeBlock(unsigned block, const uint8_t *data, const ErrorStack &err=ErrorStack());
111 bool erasePage(uint32_t address, const ErrorStack &err=ErrorStack());
113 bool eraseAll(const ErrorStack &err=ErrorStack());
115 bool releaseReadLock(const ErrorStack &err=ErrorStack());
117 bool leaveDFU(const ErrorStack &err=ErrorStack());
118
119protected:
121 uint16_t _blocksize;
122};
123
124
125#endif // DFU_LIBUSB_HH
Specialization to address a DFU device uniquely.
Definition dfu_libusb.hh:32
Descriptor(const USBDeviceInfo &info, uint8_t bus, uint8_t device)
Constructor from interface info, bus number and device address.
Definition dfu_libusb.cc:39
This class implements DFU protocol to access radios.
Definition dfu_libusb.hh:17
int get_status(const ErrorStack &err=ErrorStack())
Internal used function to read the current status.
Definition dfu_libusb.cc:234
libusb_context * _ctx
USB context.
Definition dfu_libusb.hh:74
DFUDevice(const USBDeviceDescriptor &descr, const ErrorStack &err=ErrorStack(), QObject *parent=nullptr)
Opens a connection to the USB-DFU device at vendor vid and product pid.
Definition dfu_libusb.cc:49
int get_state(int &pstate, const ErrorStack &err=ErrorStack())
Internal used function to read the state.
Definition dfu_libusb.cc:258
int wait_idle(const ErrorStack &err=ErrorStack())
Internal used function to busy-wait for a response from the device.
Definition dfu_libusb.cc:286
status_t _status
Device status.
Definition dfu_libusb.hh:78
int detach(int timeout, const ErrorStack &err=ErrorStack())
Internal used function to detach the device.
Definition dfu_libusb.cc:222
int download(unsigned block, uint8_t *data, unsigned len, const ErrorStack &err=ErrorStack())
Downloads some data to the device.
Definition dfu_libusb.cc:196
int abort(const ErrorStack &err=ErrorStack())
Internal used function to abort the current operation.
Definition dfu_libusb.cc:273
bool isOpen() const
Returns true if the DFU device interface is open.
Definition dfu_libusb.cc:178
static QList< USBDeviceDescriptor > detect(uint16_t vid, uint16_t pid)
Finds all DFU interfaces with the specified VID/PID combination.
Definition dfu_libusb.cc:137
int clear_status(const ErrorStack &err=ErrorStack())
Internal used function to clear the status.
Definition dfu_libusb.cc:246
virtual ~DFUDevice()
Destructor.
Definition dfu_libusb.cc:132
libusb_device_handle * _dev
USB device object.
Definition dfu_libusb.hh:76
int upload(unsigned block, uint8_t *data, unsigned len, const ErrorStack &err=ErrorStack())
Uploads some data from the device.
Definition dfu_libusb.cc:209
void close()
Closes the DFU interface.
Definition dfu_libusb.cc:183
Implements the ST MCU extensions for the DFU protocol, aka DfuSe.
Definition dfu_libusb.hh:88
void close()
Closes the connection.
Definition dfu_libusb.cc:333
bool writeBlock(unsigned block, const uint8_t *data, const ErrorStack &err=ErrorStack())
Writes a block of data to the device.
Definition dfu_libusb.cc:367
bool leaveDFU(const ErrorStack &err=ErrorStack())
Leaves the DFU mode, may boot into the application code.
Definition dfu_libusb.cc:430
uint16_t _blocksize
Holds the block size in bytes.
Definition dfu_libusb.hh:121
bool erasePage(uint32_t address, const ErrorStack &err=ErrorStack())
Erases an entire page of memory at the specified address.
Definition dfu_libusb.cc:380
bool setAddress(uint32_t address, const ErrorStack &err=ErrorStack())
Sets the read/write reference address.
Definition dfu_libusb.cc:344
bool readBlock(unsigned block, uint8_t *data, const ErrorStack &err=ErrorStack())
Reads a block of data from the device.
Definition dfu_libusb.cc:362
uint16_t blocksize() const
Returns the blocksize in bytes.
Definition dfu_libusb.cc:339
bool releaseReadLock(const ErrorStack &err=ErrorStack())
Releases the read lock.
Definition dfu_libusb.cc:414
DFUSEDevice(const USBDeviceDescriptor &descr, const ErrorStack &err=ErrorStack(), uint16_t blocksize=32, QObject *parent=nullptr)
Constructor, also connects to the specified VID/PID device found first.
Definition dfu_libusb.cc:326
bool eraseAll(const ErrorStack &err=ErrorStack())
Erases the entire memory.
Definition dfu_libusb.cc:398
Implements a stack of error messages to provide a pretty formatted error traceback.
Definition errorstack.hh:41
Base class for all radio interface descriptors representing a unique interface to a connected radio.
Definition usbdevice.hh:197
const QVariant & device() const
Returns the device information identifying the interface uniquely.
Definition usbdevice.cc:290
Generic information about a possible radio interface.
Definition usbdevice.hh:121