OpenHantek
Loading...
Searching...
No Matches
scopedevice.h
Go to the documentation of this file.
1// SPDX-License-Identifier: GPL-2.0-or-later
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 "dsomodel.h"
19
20class DSOModel;
21
22typedef uint64_t UniqueUSBid;
23
24
28const QString libUsbErrorString( int error );
29
30
33class 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() override;
41 bool connectDevice( QString &errorMessage );
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( uint8_t( LIBUSB_REQUEST_TYPE_VENDOR ) | uint8_t( LIBUSB_ENDPOINT_OUT ), uint8_t( command->code ),
120 const_cast< unsigned char * >( command->data() ), unsigned( command->size() ), command->value, 0,
122 }
123
127 template < class T > inline int controlRead( const T *command ) {
128 return controlTransfer( uint8_t( LIBUSB_REQUEST_TYPE_VENDOR ) | uint8_t( LIBUSB_ENDPOINT_IN ), uint8_t( command->code ),
129 const_cast< unsigned char * >( command->data() ), unsigned( command->size() ), command->value, 0,
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;
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:
210};
211
212extern int verboseLevel;
Describes a device This is the central class to describe a hantek compatible DSO. It contains all usb...
Definition dsomodel.h:17
bool stopTransfer
Definition scopedevice.h:204
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:199
unsigned int getFwVersion() const
Definition scopedevice.h:69
bool needsFirmware()
Definition scopedevice.cpp:173
unsigned getFindIteration() const
Definition scopedevice.h:91
int nInterface
Definition scopedevice.h:170
ScopeDevice()
Definition scopedevice.cpp:65
libusb_device_handle * handle
Definition scopedevice.h:167
void deviceDisconnected()
The device has been disconnected.
void overwriteInPacketLength(unsigned len)
Definition scopedevice.h:156
libusb_device * device
The USB handle for the oscilloscope.
Definition scopedevice.h:166
bool hasStopped()
Definition scopedevice.h:55
ScopeDevice(const ScopeDevice &)=delete
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:285
QString readUSBdescriptor(libusb_device_handle *handle, uint8_t index)
readUSBdescriptor
Definition scopedevice.cpp:312
libusb_device * getUSBDevice() const
Definition scopedevice.h:136
bool connectDevice(QString &errorMessage)
Definition scopedevice.cpp:68
ScopeDevice(DSOModel *model, libusb_device *device, unsigned findIteration=0)
Definition scopedevice.cpp:58
int claimInterface(const libusb_interface_descriptor *interfaceDescriptor)
Definition scopedevice.cpp:124
const DSOModel * getModel() const
Get the oscilloscope model.
Definition scopedevice.h:149
QString serialNumber
Definition scopedevice.h:206
void disconnectFromDevice()
Definition scopedevice.cpp:147
int controlRead(const T *command)
Control read to the oscilloscope.
Definition scopedevice.h:127
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:179
UniqueUSBid getUniqueUSBDeviceID() const
Definition scopedevice.h:141
~ScopeDevice() override
Definition scopedevice.cpp:114
int controlWrite(const T *command)
Control write to the oscilloscope.
Definition scopedevice.h:118
bool realHW
Definition scopedevice.h:203
bool isConnected()
Check if the oscilloscope is connected.
Definition scopedevice.cpp:170
unsigned inPacketLength
Packet length for the IN endpoint.
Definition scopedevice.h:172
const UniqueUSBid uniqueUSBdeviceID
Definition scopedevice.h:169
bool isRealHW() const
Distinguish between real hw or demo device.
Definition scopedevice.h:49
static UniqueUSBid computeUSBdeviceID(libusb_device *device)
Definition scopedevice.cpp:27
const QString getSerialNumber() const
getSerialNumber
Definition scopedevice.h:75
int bulkRead(const T *command, int attempts=HANTEK_ATTEMPTS)
Bulk read from the oscilloscope.
Definition scopedevice.h:199
unsigned findIteration
Definition scopedevice.h:168
void setFindIteration(unsigned iteration)
Definition scopedevice.h:90
struct libusb_device_descriptor descriptor
Definition scopedevice.h:165
int bulkWrite(const unsigned char *data, unsigned int length, int attempts=HANTEK_ATTEMPTS)
Bulk write to the oscilloscope.
Definition scopedevice.h:190
bool disconnected
Definition scopedevice.h:205
bool isDemoDevice() const
Definition scopedevice.h:50
void stopSampling()
Stop a long running (interruptible) bulk transfer.
Definition scopedevice.h:53
unsigned outPacketLength
Packet length for the OUT endpoint.
Definition scopedevice.h:171
DSOModel * model
Definition scopedevice.h:162
int verboseLevel
Definition main.cpp:62
uint64_t UniqueUSBid
Definition scopedevice.h:22
const QString libUsbErrorString(int error)
Returns string representation for libusb errors.
Definition scopedevice.cpp:24
#define HANTEK_ATTEMPTS
The number of transfer attempts.
Definition usbdevicedefinitions.h:7
#define HANTEK_TIMEOUT
Timeout for USB transfers in ms.
Definition usbdevicedefinitions.h:5
#define HANTEK_EP_IN
IN Endpoint for bulk transfers.
Definition usbdevicedefinitions.h:11
#define HANTEK_ATTEMPTS_MULTI
The number of multi packet transfer attempts.
Definition usbdevicedefinitions.h:8
#define HANTEK_EP_OUT
OUT Endpoint for bulk transfers.
Definition usbdevicedefinitions.h:10