OpenHantek
scopedevice.h
Go to the documentation of this file.
1 // SPDX-License-Identifier: GPL-2.0+
2 
3 #pragma once
4 
5 #include <QMutex>
6 #include <QObject>
7 #include <QReadWriteLock>
8 #include <QStringList>
9 
10 #ifdef Q_OS_FREEBSD
11 #include <libusb.h>
12 #else
13 #include <libusb-1.0/libusb.h>
14 #endif
15 #include <memory>
16 
17 #include "models/modelDEMO.h"
18 #include "usbdevicedefinitions.h"
19 
20 class DSOModel;
21 
22 typedef uint64_t UniqueUSBid;
23 
24 
28 const QString libUsbErrorString( int error );
29 
30 
33 class ScopeDevice : public QObject {
34  Q_OBJECT
35 
36  public:
37  explicit ScopeDevice( DSOModel *model, libusb_device *device, unsigned findIteration = 0 );
38  explicit ScopeDevice();
39  ScopeDevice( const ScopeDevice & ) = delete;
40  ~ScopeDevice();
41  bool connectDevice( QString &errorMessage );
42  void disconnectFromDevice();
43 
46  bool isConnected();
47 
49  bool isRealHW() const { return realHW; }
50  bool isDemoDevice() const { return !realHW; }
51 
53  void stopSampling() { stopTransfer = true; }
54 
55  bool hasStopped() {
56  bool stopped = stopTransfer;
57  stopTransfer = false;
58  return stopped;
59  }
60 
64  bool needsFirmware();
65 
69  inline unsigned int getFwVersion() const { return descriptor.bcdDevice; }
70 
75  inline const QString getSerialNumber() const { return serialNumber; }
76 
77 
84  QString readUSBdescriptor( libusb_device_handle *handle, uint8_t index );
85 
90  inline void setFindIteration( unsigned iteration ) { findIteration = iteration; }
91  inline unsigned getFindIteration() const { return findIteration; }
92 
100  int bulkReadMulti( unsigned char *data, unsigned length, bool captureSmallBlocks, unsigned &received,
101  int attempts = HANTEK_ATTEMPTS_MULTI );
102 
112  int controlTransfer( unsigned char type, unsigned char request, unsigned char *data, unsigned int length, int value, int index,
113  int attempts = HANTEK_ATTEMPTS );
114 
118  template < class T > inline int controlWrite( const T *command ) {
119  return controlTransfer( LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_ENDPOINT_OUT, uint8_t( command->code ),
120  const_cast< unsigned char * >( command->data() ), unsigned( command->size() ), command->value, 0,
121  HANTEK_ATTEMPTS );
122  }
123 
127  template < class T > inline int controlRead( const T *command ) {
128  return controlTransfer( LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_ENDPOINT_IN, uint8_t( command->code ),
129  const_cast< unsigned char * >( command->data() ), unsigned( command->size() ), command->value, 0,
130  HANTEK_ATTEMPTS );
131  }
132 
136  inline libusb_device *getUSBDevice() const { return device; }
137 
145  static UniqueUSBid computeUSBdeviceID( libusb_device *device );
146 
149  inline const DSOModel *getModel() const { return model; }
156  inline void overwriteInPacketLength( unsigned len ) { inPacketLength = len; }
157 
158  protected:
159  int claimInterface( const libusb_interface_descriptor *interfaceDescriptor );
160 
161  // Device model data
163 
164  // Libusb specific variables
165  struct libusb_device_descriptor descriptor;
166  libusb_device *device;
167  libusb_device_handle *handle = nullptr;
168  unsigned findIteration;
171  unsigned outPacketLength;
172  unsigned inPacketLength;
173 
174  private:
182  int bulkTransfer( unsigned char endpoint, const unsigned char *data, unsigned int length, int attempts = HANTEK_ATTEMPTS,
183  unsigned int timeout = HANTEK_TIMEOUT );
184 
190  inline int bulkWrite( const unsigned char *data, unsigned int length, int attempts = HANTEK_ATTEMPTS ) {
191  return bulkTransfer( HANTEK_EP_OUT, data, length, attempts );
192  }
193 
199  template < class T > inline int bulkRead( const T *command, int attempts = HANTEK_ATTEMPTS ) {
200  return bulkTransfer( HANTEK_EP_IN, command->data(), command->size(), attempts );
201  }
202 
203  bool realHW = true;
204  bool stopTransfer = false;
205  bool disconnected = true;
206  QString serialNumber = "0000";
207 
208  signals:
209  void deviceDisconnected();
210 };
211 
212 extern unsigned verboseLevel;
unsigned findIteration
Definition: scopedevice.h:168
#define HANTEK_EP_OUT
OUT Endpoint for bulk transfers.
Definition: usbdevicedefinitions.h:10
UniqueUSBid getUniqueUSBDeviceID() const
Definition: scopedevice.h:141
DSOModel * model
Definition: scopedevice.h:162
#define HANTEK_ATTEMPTS
The number of transfer attempts.
Definition: usbdevicedefinitions.h:7
int nInterface
Definition: scopedevice.h:170
const QString getSerialNumber() const
getSerialNumber
Definition: scopedevice.h:75
libusb_device_handle * handle
Definition: scopedevice.h:167
bool connectDevice(QString &errorMessage)
Definition: scopedevice.cpp:66
~ScopeDevice()
Definition: scopedevice.cpp:111
void setFindIteration(unsigned iteration)
Definition: scopedevice.h:90
bool hasStopped()
Definition: scopedevice.h:55
bool realHW
Definition: scopedevice.h:203
int controlWrite(const T *command)
Control write to the oscilloscope.
Definition: scopedevice.h:118
ScopeDevice()
Definition: scopedevice.cpp:63
const DSOModel * getModel() const
Get the oscilloscope model.
Definition: scopedevice.h:149
int claimInterface(const libusb_interface_descriptor *interfaceDescriptor)
Definition: scopedevice.cpp:121
unsigned verboseLevel
Definition: main.cpp:72
void deviceDisconnected()
The device has been disconnected.
int controlRead(const T *command)
Control read to the oscilloscope.
Definition: scopedevice.h:127
unsigned getFindIteration() const
Definition: scopedevice.h:91
void disconnectFromDevice()
Definition: scopedevice.cpp:144
bool isRealHW() const
Distinguish between real hw or demo device.
Definition: scopedevice.h:49
unsigned int getFwVersion() const
Definition: scopedevice.h:69
#define HANTEK_EP_IN
IN Endpoint for bulk transfers.
Definition: usbdevicedefinitions.h:11
This class handles the USB communication with an usb device that has one in and one out endpoint...
Definition: scopedevice.h:33
Describes a device This is the central class to describe a hantek compatible DSO. It contains all usb...
Definition: dsomodel.h:17
void stopSampling()
Stop a long running (interruptable) bulk transfer.
Definition: scopedevice.h:53
bool isConnected()
Check if the oscilloscope is connected.
Definition: scopedevice.cpp:167
int bulkRead(const T *command, int attempts=HANTEK_ATTEMPTS)
Bulk read from the oscilloscope.
Definition: scopedevice.h:199
unsigned outPacketLength
Packet length for the OUT endpoint.
Definition: scopedevice.h:171
libusb_device * device
The USB handle for the oscilloscope.
Definition: scopedevice.h:166
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: scopedevice.cpp:238
static UniqueUSBid computeUSBdeviceID(libusb_device *device)
Definition: scopedevice.cpp:25
bool needsFirmware()
Definition: scopedevice.cpp:170
QString serialNumber
Definition: scopedevice.h:206
struct libusb_device_descriptor descriptor
Definition: scopedevice.h:165
uint64_t UniqueUSBid
Definition: scopedevice.h:20
int bulkWrite(const unsigned char *data, unsigned int length, int attempts=HANTEK_ATTEMPTS)
Bulk write to the oscilloscope.
Definition: scopedevice.h:190
#define HANTEK_TIMEOUT
Timeout for USB transfers in ms.
Definition: usbdevicedefinitions.h:5
bool stopTransfer
Definition: scopedevice.h:204
#define HANTEK_ATTEMPTS_MULTI
The number of multi packet transfer attempts.
Definition: usbdevicedefinitions.h:8
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: scopedevice.cpp:176
void overwriteInPacketLength(unsigned len)
Definition: scopedevice.h:156
int bulkReadMulti(unsigned char *data, unsigned length, bool captureSmallBlocks, unsigned &received, int attempts=HANTEK_ATTEMPTS_MULTI)
Multi packet bulk read from the oscilloscope.
Definition: scopedevice.cpp:196
const QString libUsbErrorString(int error)
Returns string representation for libusb errors.
Definition: scopedevice.cpp:22
QString readUSBdescriptor(libusb_device_handle *handle, uint8_t index)
readUSBdescriptor
Definition: scopedevice.cpp:259
libusb_device * getUSBDevice() const
Definition: scopedevice.h:136
unsigned inPacketLength
Packet length for the IN endpoint.
Definition: scopedevice.h:172
const UniqueUSBid uniqueUSBdeviceID
Definition: scopedevice.h:169
bool isDemoDevice() const
Definition: scopedevice.h:50
bool disconnected
Definition: scopedevice.h:205