OpenHantek
usbdevice.h
1 // SPDX-License-Identifier: GPL-2.0+
2 
3 #pragma once
4 
5 #include <QObject>
6 #include <QStringList>
7 #include <libusb-1.0/libusb.h>
8 #include <memory>
9 
10 #include "usbdevicedefinitions.h"
11 
12 class DSOModel;
13 
14 typedef unsigned long UniqueUSBid;
15 
16 
20 QString libUsbErrorString(int error);
21 
24 class USBDevice : public QObject {
25  Q_OBJECT
26 
27  public:
28  explicit USBDevice(DSOModel* model, libusb_device *device, unsigned findIteration = 0);
29  USBDevice(const USBDevice&) = delete;
30  ~USBDevice();
31  bool connectDevice(QString &errorMessage);
32  void disconnectFromDevice();
33 
36  bool isConnected();
37 
41  bool needsFirmware();
42 
47  inline void setFindIteration(unsigned iteration) { findIteration = iteration; }
48  inline unsigned getFindIteration() const { return findIteration; }
49 
58  int bulkTransfer(unsigned char endpoint, const unsigned char *data, unsigned int length, int attempts = HANTEK_ATTEMPTS,
59  unsigned int timeout = HANTEK_TIMEOUT);
60 
66  inline int bulkWrite(const unsigned char *data, unsigned int length, int attempts = HANTEK_ATTEMPTS) {
67  return bulkTransfer(HANTEK_EP_OUT, data, length, attempts);
68  }
69 
75  template<class T>
76  inline int bulkRead(const T *command, int attempts = HANTEK_ATTEMPTS) {
77  return bulkTransfer(HANTEK_EP_IN, command->data(), command->size(), attempts);
78  }
79 
85  int bulkReadMulti(unsigned char *data, unsigned length, int attempts = HANTEK_ATTEMPTS_MULTI);
86 
96  int controlTransfer(unsigned char type, unsigned char request, unsigned char *data, unsigned int length, int value,
97  int index, int attempts = HANTEK_ATTEMPTS);
98 
102  template<class T>
103  inline int controlWrite(const T *command) {
104  return controlTransfer(LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_ENDPOINT_OUT, (uint8_t)command->code,
105  (unsigned char *)command->data(), (unsigned)command->size(), command->value, 0,
106  HANTEK_ATTEMPTS);
107  }
108 
112  template<class T>
113  inline int controlRead(const T *command) {
114  return controlTransfer(LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_ENDPOINT_IN, (uint8_t)command->code,
115  (unsigned char *)command->data(), (unsigned)command->size(), command->value, 0,
116  HANTEK_ATTEMPTS);
117  }
118 
122  inline libusb_device *getRawDevice() const { return device; }
123 
127  inline unsigned long getUniqueUSBDeviceID() const { return uniqueUSBdeviceID; }
135  static UniqueUSBid computeUSBdeviceID(libusb_device *device);
136 
139  inline const DSOModel *getModel() const { return model; }
146  inline void overwriteInPacketLength(int len) { inPacketLength = len; }
147  protected:
148  int claimInterface(const libusb_interface_descriptor *interfaceDescriptor, int endpointOut, int endPointIn);
149 
150  // Device model data
151  DSOModel* model;
152 
153  // Libusb specific variables
154  struct libusb_device_descriptor descriptor;
155  libusb_device *device;
156  libusb_device_handle *handle = nullptr;
157  unsigned findIteration;
158  const unsigned long uniqueUSBdeviceID;
159  int interface;
162  signals:
163  void deviceDisconnected();
164 };
int inPacketLength
Packet length for the IN endpoint.
Definition: usbdevice.h:161
libusb_device * device
The USB handle for the oscilloscope.
Definition: usbdevice.h:155
libusb_device * getRawDevice() const
Definition: usbdevice.h:122
const DSOModel * getModel() const
Get the oscilloscope model.
Definition: usbdevice.h:139
int bulkRead(const T *command, int attempts=HANTEK_ATTEMPTS)
Bulk read from the oscilloscope.
Definition: usbdevice.h:76
int controlRead(const T *command)
Control read to the oscilloscope.
Definition: usbdevice.h:113
Describes a device This is the central class to describe a hantek compatible DSO. It contains all usb...
Definition: dsomodel.h:17
void overwriteInPacketLength(int len)
Definition: usbdevice.h:146
int controlTransfer(unsigned char type, unsigned char request, unsigned char *data, unsigned int length, int value, int index, int attempts=HANTEK_ATTEMPTS)
Control transfer to the oscilloscope.
Definition: usbdevice.cpp:192
int controlWrite(const T *command)
Control write to the oscilloscope.
Definition: usbdevice.h:103
void setFindIteration(unsigned iteration)
Definition: usbdevice.h:47
int outPacketLength
Packet length for the OUT endpoint.
Definition: usbdevice.h:160
int bulkWrite(const unsigned char *data, unsigned int length, int attempts=HANTEK_ATTEMPTS)
Bulk write to the oscilloscope.
Definition: usbdevice.h:66
int bulkReadMulti(unsigned char *data, unsigned length, int attempts=HANTEK_ATTEMPTS_MULTI)
Multi packet bulk read from the oscilloscope.
Definition: usbdevice.cpp:174
bool needsFirmware()
Definition: usbdevice.cpp:153
int bulkTransfer(unsigned char endpoint, const unsigned char *data, unsigned int length, int attempts=HANTEK_ATTEMPTS, unsigned int timeout=HANTEK_TIMEOUT)
Bulk transfer to/from the oscilloscope.
Definition: usbdevice.cpp:157
This class handles the USB communication with an usb device that has one in and one out endpoint...
Definition: usbdevice.h:24
unsigned long getUniqueUSBDeviceID() const
Definition: usbdevice.h:127
static UniqueUSBid computeUSBdeviceID(libusb_device *device)
Definition: usbdevice.cpp:48
bool isConnected()
Check if the oscilloscope is connected.
Definition: usbdevice.cpp:151
void deviceDisconnected()
The device has been disconnected.