OpenHantek
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. More...
 

Public Member Functions

 USBDevice (DSOModel *model, libusb_device *device, unsigned findIteration=0)
 
 USBDevice (const USBDevice &)=delete
 
 ~USBDevice ()
 
bool connectDevice (QString &errorMessage)
 
void disconnectFromDevice ()
 
bool isConnected ()
 Check if the oscilloscope is connected. More...
 
bool needsFirmware ()
 
unsigned int getFwVersion () const
 
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
 
UniqueUSBid getUniqueUSBDeviceID () const
 
const DSOModelgetModel () const
 Get the oscilloscope model. More...
 
void overwriteInPacketLength (unsigned len)
 

Static Public Member Functions

static UniqueUSBid computeUSBdeviceID (libusb_device *device)
 

Protected Member Functions

int claimInterface (const libusb_interface_descriptor *interfaceDescriptor)
 

Protected Attributes

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

Detailed Description

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

Constructor & Destructor Documentation

◆ USBDevice() [1/2]

USBDevice::USBDevice ( DSOModel model,
libusb_device *  device,
unsigned  findIteration = 0 
)
explicit

◆ USBDevice() [2/2]

USBDevice::USBDevice ( const USBDevice )
delete

◆ ~USBDevice()

USBDevice::~USBDevice ( )

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/received 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/received 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/received 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/received 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.

◆ claimInterface()

int USBDevice::claimInterface ( const libusb_interface_descriptor *  interfaceDescriptor)
protected

◆ computeUSBdeviceID()

UniqueUSBid USBDevice::computeUSBdeviceID ( libusb_device *  device)
static

ID built from bus, port, VID, PID and FW version

◆ connectDevice()

bool USBDevice::connectDevice ( QString &  errorMessage)

◆ controlRead()

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

Control read to the oscilloscope.

Parameters
commandBuffer for the sent/received 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/received 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/received data.
Returns
Number of sent bytes on success, libusb error code on error.

◆ deviceDisconnected

void USBDevice::deviceDisconnected ( )
signal

The device has been disconnected.

◆ disconnectFromDevice()

void USBDevice::disconnectFromDevice ( )

◆ getFindIteration()

unsigned USBDevice::getFindIteration ( ) const
inline

◆ getFwVersion()

unsigned int USBDevice::getFwVersion ( ) const
inline
Returns
Return device version as unsigned int

◆ 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()

UniqueUSBid 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 ( unsigned  len)
inline

Usually a maximum 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

Field Documentation

◆ descriptor

struct libusb_device_descriptor USBDevice::descriptor
protected

◆ device

libusb_device* USBDevice::device
protected

The USB handle for the oscilloscope.

◆ findIteration

unsigned USBDevice::findIteration
protected

◆ handle

libusb_device_handle* USBDevice::handle = nullptr
protected

◆ inPacketLength

unsigned USBDevice::inPacketLength
protected

Packet length for the IN endpoint.

◆ model

DSOModel* USBDevice::model
protected

◆ nInterface

int USBDevice::nInterface
protected

◆ outPacketLength

unsigned USBDevice::outPacketLength
protected

Packet length for the OUT endpoint.

◆ uniqueUSBdeviceID

const UniqueUSBid USBDevice::uniqueUSBdeviceID
protected

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