OpenHantek
Signals | Public Member Functions | Static Public Member Functions | Protected Member Functions | Protected Attributes
USBDevice Class Reference

This class handles the USB communication with an usb device that has one in and one out endpoint. More...

#include <usbdevice.h>

Inheritance diagram for USBDevice:

Signals

void deviceDisconnected ()
 The device has been disconnected.
 

Public Member Functions

 USBDevice (DSOModel *model, libusb_device *device, unsigned findIteration=0)
 
 USBDevice (const USBDevice &)=delete
 
bool connectDevice (QString &errorMessage)
 
void disconnectFromDevice ()
 
bool isConnected ()
 Check if the oscilloscope is connected. More...
 
bool needsFirmware ()
 
void setFindIteration (unsigned iteration)
 
unsigned getFindIteration () const
 
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. More...
 
int bulkWrite (const unsigned char *data, unsigned int length, int attempts=HANTEK_ATTEMPTS)
 Bulk write to the oscilloscope. More...
 
template<class T >
int bulkRead (const T *command, int attempts=HANTEK_ATTEMPTS)
 Bulk read from the oscilloscope. More...
 
int bulkReadMulti (unsigned char *data, unsigned length, int attempts=HANTEK_ATTEMPTS_MULTI)
 Multi packet bulk read from the oscilloscope. More...
 
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. More...
 
template<class T >
int controlWrite (const T *command)
 Control write to the oscilloscope. More...
 
template<class T >
int controlRead (const T *command)
 Control read to the oscilloscope. More...
 
libusb_device * getRawDevice () const
 
unsigned long getUniqueUSBDeviceID () const
 
const DSOModelgetModel () const
 Get the oscilloscope model. More...
 
void overwriteInPacketLength (int len)
 

Static Public Member Functions

static UniqueUSBid computeUSBdeviceID (libusb_device *device)
 

Protected Member Functions

int claimInterface (const libusb_interface_descriptor *interfaceDescriptor, int endpointOut, int endPointIn)
 

Protected Attributes

DSOModelmodel
 
struct libusb_device_descriptor descriptor
 
libusb_device * device
 The USB handle for the oscilloscope.
 
libusb_device_handle * handle = nullptr
 
unsigned findIteration
 
const unsigned long uniqueUSBdeviceID
 
int interface
 
int outPacketLength
 Packet length for the OUT endpoint.
 
int inPacketLength
 Packet length for the IN endpoint.
 

Detailed Description

This class handles the USB communication with an usb device that has one in and one out endpoint.

Member Function Documentation

◆ bulkRead()

template<class T >
int USBDevice::bulkRead ( const T *  command,
int  attempts = HANTEK_ATTEMPTS 
)
inline

Bulk read from the oscilloscope.

Parameters
dataBuffer for the sent/recieved data.
lengthThe length of the packet.
attemptsThe number of attempts, that are done on timeouts.
Returns
Number of received bytes on success, libusb error code on error.

◆ bulkReadMulti()

int USBDevice::bulkReadMulti ( unsigned char *  data,
unsigned  length,
int  attempts = HANTEK_ATTEMPTS_MULTI 
)

Multi packet bulk read from the oscilloscope.

Parameters
dataBuffer for the sent/recieved data.
lengthThe length of data contained in the packets.
attemptsThe number of attempts, that are done on timeouts.
Returns
Number of received bytes on success, libusb error code on error.

◆ bulkTransfer()

int USBDevice::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.

Parameters
endpointEndpoint number, also sets the direction of the transfer.
dataBuffer for the sent/recieved data.
lengthThe length of the packet.
attemptsThe number of attempts, that are done on timeouts.
timeoutThe timeout in ms.
Returns
Number of transferred bytes on success, libusb error code on error.

◆ bulkWrite()

int USBDevice::bulkWrite ( const unsigned char *  data,
unsigned int  length,
int  attempts = HANTEK_ATTEMPTS 
)
inline

Bulk write to the oscilloscope.

Parameters
dataBuffer for the sent/recieved data.
lengthThe length of the packet.
attemptsThe number of attempts, that are done on timeouts.
Returns
Number of sent bytes on success, libusb error code on error.

◆ computeUSBdeviceID()

UniqueUSBid USBDevice::computeUSBdeviceID ( libusb_device *  device)
static

The USB bus is organised in a tree hierarchy. A device is connected to a port on a bus device, which is connected to a port on another bus device etc up to the root usb device.

The USB 3.0 standard allows up to 7 levels with 256 devices on each level (1 Byte). We generate a unique number for the connected device.

◆ controlRead()

template<class T >
int USBDevice::controlRead ( const T *  command)
inline

Control read to the oscilloscope.

Parameters
commandBuffer for the sent/recieved data.
Returns
Number of received bytes on success, libusb error code on error.

◆ controlTransfer()

int USBDevice::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.

Parameters
typeThe request type, also sets the direction of the transfer.
requestThe request field of the packet.
dataBuffer for the sent/recieved data.
lengthThe length field of the packet.
valueThe value field of the packet.
indexThe index field of the packet.
attemptsThe number of attempts, that are done on timeouts.
Returns
Number of transferred bytes on success, libusb error code on error.

◆ controlWrite()

template<class T >
int USBDevice::controlWrite ( const T *  command)
inline

Control write to the oscilloscope.

Parameters
commandBuffer for the sent/recieved data.
Returns
Number of sent bytes on success, libusb error code on error.

◆ getModel()

const DSOModel* USBDevice::getModel ( ) const
inline

Get the oscilloscope model.

Returns
The ::Model of the connected Hantek DSO.

◆ getRawDevice()

libusb_device* USBDevice::getRawDevice ( ) const
inline
Returns
Returns the raw libusb device

◆ getUniqueUSBDeviceID()

unsigned long USBDevice::getUniqueUSBDeviceID ( ) const
inline
Returns
Return the unique usb device id USBDevice::computeUSBdeviceID().

◆ isConnected()

bool USBDevice::isConnected ( )

Check if the oscilloscope is connected.

Returns
true, if a connection is up.

◆ needsFirmware()

bool USBDevice::needsFirmware ( )
Returns
Return true if this device needs a firmware first

◆ overwriteInPacketLength()

void USBDevice::overwriteInPacketLength ( int  len)
inline

Usually a maximaum packet length for in and outgoing packets is determined by the underlying implementation and usb specification. E.g. the roll buffer mode uses the maximum in length for transfer. Some devices do not support that much data though and need an artification restriction.

◆ setFindIteration()

void USBDevice::setFindIteration ( unsigned  iteration)
inline

Keep track of the find iteration on which this device was found

Parameters
iterationThe new iteration value

The documentation for this class was generated from the following files: