Main MRPT website > C++ reference for MRPT 1.5.7
List of all members | Public Member Functions | Private Attributes
UsbInterface Class Reference

Detailed Description

An IoInterface for dealing specifically with Xsens USB devices.

Definition at line 34 of file usbinterface.h.

#include <xSens_MT4/xcommunication/include/xsens/usbinterface.h>

Inheritance diagram for UsbInterface:
Inheritance graph

Public Member Functions

 UsbInterface ()
 Default constructor, initializes all members to their default values. More...
 
 ~UsbInterface ()
 Destructor, de-initializes, frees memory allocated for buffers, etc. More...
 
XsResultValue open (const XsPortInfo &portInfo, uint32_t readBufSize=0, uint32_t writeBufSize=0)
 Open a communication channel to the given USB port name. More...
 
XsResultValue close (void)
 Close the USB communication port. More...
 
XsResultValue closeUsb (void)
 Close the USB communication port. More...
 
XsResultValue flushData (void)
 Flush all data in the buffers to and from the device. More...
 
bool isOpen (void) const
 Return whether the USB communication port is open or not. More...
 
uint8_t usbBus () const
 The USB bus number this device is on (libusb/linux only) More...
 
uint8_t usbAddress () const
 The address of the device (libusb/linux only) More...
 
XsResultValue getLastResult (void) const
 Return the error code of the last operation. More...
 
XsResultValue setTimeout (uint32_t ms)
 Set the default timeout value to use in blocking operations. More...
 
uint32_t getTimeout (void) const
 Return the current timeout value. More...
 
void setRawIo (bool enable)
 Sets the RAWIO mode of the USB interface. More...
 
bool getRawIo (void)
 Retrieves the state of the RAWIO mode of the USB interface. More...
 
virtual XsResultValue writeData (const XsByteArray &data, XsSize *written=NULL)
 Write the data to the USB port. More...
 
virtual XsResultValue readData (XsSize maxLength, XsByteArray &data)
 Read data from the USB port and put it into the data buffer. More...
 
XsResultValue writeData (XsSize length, const void *data, XsSize *written=NULL)
 Write the data to the USB port. More...
 
XsResultValue readData (XsSize maxLength, void *data, XsSize *length=NULL)
 Read data from the serial port and put it into the data buffer. More...
 
XsResultValue waitForData (XsSize maxLength, void *data, XsSize *length=NULL)
 Wait for data to arrive or a timeout to occur. More...
 
void getPortName (XsString &portname) const
 Retrieve the port name that was last successfully opened. More...
 
virtual XsResultValue waitForData (XsSize maxLength, XsByteArray &data)
 Wait for data to arrive or a timeout to occur.
More...
 
virtual XsResultValue open (const XsString &filename, bool createNew, bool readOnly)
 Open a file.
More...
 
virtual XsResultValue waitForData (XsSize maxLength, XsByteArray &data)
 Wait for data to arrive or a timeout to occur.
More...
 
virtual void cancelIo (void) const
 Cancel any pending io requests.
More...
 
virtual XsResultValue appendData (const XsByteArray &bdata)
 Write data to the end of the file.
More...
 
virtual XsResultValue closeAndDelete (void)
 Close the file and delete it.
More...
 
virtual XsResultValue create (const XsString &filename)
 Create an empty file.
More...
 
virtual XsResultValue deleteData (XsFilePos start, XsSize length)
 Delete the given data from the file.
More...
 
virtual XsResultValue find (const XsByteArray &needleV, XsFilePos &pos)
 Find a string of bytes in the file.
More...
 
virtual XsFilePos getFileSize (void) const
 Return the size of the file.
More...
 
virtual XsResultValue getName (XsString &filename) const
 Retrieve the filename that was last successfully opened.
More...
 
virtual XsFilePos getReadPosition (void) const
 Return the current read position.
More...
 
virtual XsFilePos getWritePosition (void) const
 Return the current write position.
More...
 
virtual XsResultValue insertData (XsFilePos start, const XsByteArray &data)
 Insert the given data into the file.
More...
 
virtual bool isReadOnly (void) const
 Return whether the file is readonly or not.
More...
 
virtual XsResultValue setReadPosition (XsFilePos pos)
 Set the new absolute read position.
More...
 
virtual XsResultValue setWritePosition (XsFilePos pos=-1)
 Set the new absolute write position.
More...
 

Private Attributes

UsbInterfacePrivated
 

Constructor & Destructor Documentation

◆ UsbInterface()

UsbInterface::UsbInterface ( )

◆ ~UsbInterface()

UsbInterface::~UsbInterface ( )

Destructor, de-initializes, frees memory allocated for buffers, etc.

Definition at line 417 of file usbinterface.cpp.

References closeUsb(), and d.

Member Function Documentation

◆ appendData()

XsResultValue IoInterface::appendData ( const XsByteArray bdata)
virtualinherited

Write data to the end of the file.

The function writes the given data to the file at the end. The current write position is also moved to the end of the file.

Parameters
bdataThe byte data to append to the file
Returns
XRV_OK if the write was successful

Reimplemented in IoInterfaceFile.

Definition at line 79 of file iointerface.cpp.

References void(), and XRV_INVALIDOPERATION.

◆ cancelIo()

void IoInterface::cancelIo ( void  ) const
virtualinherited

Cancel any pending io requests.

Reimplemented in SerialInterface.

Definition at line 72 of file iointerface.cpp.

◆ close()

XsResultValue UsbInterface::close ( void  )
virtual

Close the USB communication port.

Implements IoInterface.

Definition at line 434 of file usbinterface.cpp.

References closeUsb().

◆ closeAndDelete()

XsResultValue IoInterface::closeAndDelete ( void  )
virtualinherited

Close the file and delete it.

Returns
XRV_OK if the file was closed and deleted successfully

Reimplemented in IoInterfaceFile.

Definition at line 85 of file iointerface.cpp.

References XRV_INVALIDOPERATION.

◆ closeUsb()

XsResultValue UsbInterface::closeUsb ( void  )

◆ create()

XsResultValue IoInterface::create ( const XsString filename)
virtualinherited

Create an empty file.

Parameters
filenameThe desired (path+)name of the file
Returns
XRV_OK if the file was created successfully

Reimplemented in IoInterfaceFile.

Definition at line 90 of file iointerface.cpp.

References void(), and XRV_INVALIDOPERATION.

◆ deleteData()

XsResultValue IoInterface::deleteData ( XsFilePos  start,
XsSize  length 
)
virtualinherited

Delete the given data from the file.

The function erases the given data from the file at the given write position. This operation may take a while to complete, but is faster than insertData.

The write position is not changed and the read position is checked for validity upon function exit.

Parameters
startThe offset of the first byte to delete
lengthThe total number of bytes to delete
Returns
XRV_OK if the data was deleted successfully

Reimplemented in IoInterfaceFile.

Definition at line 96 of file iointerface.cpp.

References void(), and XRV_INVALIDOPERATION.

◆ find()

XsResultValue IoInterface::find ( const XsByteArray needleV,
XsFilePos pos 
)
virtualinherited

Find a string of bytes in the file.

The function searches from the current read position until the given needle is found. If the needle is not found, XsResultValue::NOT_FOUND is returned. The function will update the seek position to the first character of the found needle.

Parameters
needleVThe byte string to find.
posThe position where needleV was found. This will point to the first character of the found needleV.
Returns
XRV_OK if the data was found, XRV_ENDOFFILE if it wasn't found

Reimplemented in IoInterfaceFile.

Definition at line 103 of file iointerface.cpp.

References void(), and XRV_INVALIDOPERATION.

◆ flushData()

XsResultValue UsbInterface::flushData ( void  )
virtual

Flush all data in the buffers to and from the device.

Returns
XRV_OK if the data was flushed successfully

Implements IoInterface.

Definition at line 512 of file usbinterface.cpp.

References d, UsbInterfacePrivate::getContextManager(), UsbInterfacePrivate::m_dataInEndPoint, UsbInterfacePrivate::m_deviceHandle, UsbInterfacePrivate::m_endTime, UsbInterfacePrivate::m_lastResult, and XRV_OK.

Referenced by closeUsb(), and open().

◆ getFileSize()

XsFilePos IoInterface::getFileSize ( void  ) const
virtualinherited

Return the size of the file.

Returns
The size of the file.

Reimplemented in IoInterfaceFile.

Definition at line 110 of file iointerface.cpp.

◆ getLastResult()

XsResultValue UsbInterface::getLastResult ( void  ) const
virtual

Return the error code of the last operation.

Implements IoInterface.

Definition at line 545 of file usbinterface.cpp.

References d, and UsbInterfacePrivate::m_lastResult.

◆ getName()

XsResultValue IoInterface::getName ( XsString filename) const
virtualinherited

Retrieve the filename that was last successfully opened.

Parameters
filenameThe XsString which will contain the filename.
Returns
XRV_OK

Reimplemented in IoInterfaceFile.

Definition at line 115 of file iointerface.cpp.

References void(), and XRV_INVALIDOPERATION.

◆ getPortName()

void UsbInterface::getPortName ( XsString portname) const

Retrieve the port name that was last successfully opened.

Definition at line 1089 of file usbinterface.cpp.

References d, and UsbInterfacePrivate::m_portname.

◆ getRawIo()

bool UsbInterface::getRawIo ( void  )

Retrieves the state of the RAWIO mode of the USB interface.

Returns
true if raw IO mode is enabled
Note
Only applies to WinUSB implementations

Definition at line 931 of file usbinterface.cpp.

References d.

◆ getReadPosition()

XsFilePos IoInterface::getReadPosition ( void  ) const
virtualinherited

Return the current read position.

Returns
The current read position.

Reimplemented in IoInterfaceFile.

Definition at line 121 of file iointerface.cpp.

◆ getTimeout()

uint32_t UsbInterface::getTimeout ( void  ) const
virtual

Return the current timeout value.

Implements StreamInterface.

Definition at line 551 of file usbinterface.cpp.

References d, and UsbInterfacePrivate::m_timeout.

◆ getWritePosition()

XsFilePos IoInterface::getWritePosition ( void  ) const
virtualinherited

Return the current write position.

Returns
The current write position.

Reimplemented in IoInterfaceFile.

Definition at line 126 of file iointerface.cpp.

◆ insertData()

XsResultValue IoInterface::insertData ( XsFilePos  start,
const XsByteArray data 
)
virtualinherited

Insert the given data into the file.

The function writes the given data to the file at the current write position. This operation may take a while to complete.

The write position is placed at the end of the inserted data.

Parameters
startThe offset in the file to write the first byte
dataThe data to insert in the file
Returns
XRV_OK if the data was inserted successfully

Reimplemented in IoInterfaceFile.

Definition at line 131 of file iointerface.cpp.

References void(), and XRV_INVALIDOPERATION.

◆ isOpen()

bool UsbInterface::isOpen ( void  ) const
virtual

Return whether the USB communication port is open or not.

Implements IoInterface.

Definition at line 557 of file usbinterface.cpp.

References d, and UsbInterfacePrivate::m_deviceHandle.

Referenced by closeUsb(), open(), readData(), and writeData().

◆ isReadOnly()

bool IoInterface::isReadOnly ( void  ) const
virtualinherited

Return whether the file is readonly or not.

Returns
true if the file is readonly

Reimplemented in IoInterfaceFile.

Definition at line 138 of file iointerface.cpp.

◆ open() [1/2]

XsResultValue UsbInterface::open ( const XsPortInfo portInfo,
uint32_t  readBufSize = 0,
uint32_t  writeBufSize = 0 
)
virtual

◆ open() [2/2]

XsResultValue IoInterface::open ( const XsString filename,
bool  createNew,
bool  readOnly 
)
virtualinherited

Open a file.

Parameters
filenameThe name of the file to open
createNewWhen true, the file will be created if it doesn't exist yet
readOnlyWhen true, the file will be marked as read only for IoInterfaceFile, preventing accidental writes to the file.
Returns
XRV_OK if the file was opened successfully
See also
createFile

Reimplemented in IoInterfaceFile.

Definition at line 143 of file iointerface.cpp.

References void(), and XRV_INVALIDOPERATION.

◆ readData() [1/2]

XsResultValue UsbInterface::readData ( XsSize  maxLength,
XsByteArray data 
)
virtual

Read data from the USB port and put it into the data buffer.

This function reads up to maxLength bytes from the port (non-blocking) and puts it into the data buffer.

Parameters
maxLengthThe maximum amount of data read.
dataThe buffer that will store the received data.
Returns
XRV_OK if no error occurred. It can be that no data is available and XRV_OK will be returned. Check data.size() for the number of bytes that were read.

Implements IoInterface.

Definition at line 810 of file usbinterface.cpp.

Referenced by waitForData().

◆ readData() [2/2]

XsResultValue UsbInterface::readData ( XsSize  maxLength,
void data,
XsSize length = NULL 
)

Read data from the serial port and put it into the data buffer.

This function reads up to maxLength bytes from the USB port (non-blocking) and puts it into the data buffer.

Parameters
maxLengthThe maximum number of bytes to read.
dataPointer to a buffer that will store the received data.
lengthThe number of bytes placed into data.
Returns
XRV_OK if no error occurred. It can be that no data is available and XRV_OK will be returned. Check *length for the number of bytes that were read.

Definition at line 828 of file usbinterface.cpp.

References XsLibUsb::bulk_transfer, d, mrpt::system::os::fopen(), UsbInterfacePrivate::getContextManager(), isOpen(), JLTRACE, UsbInterfacePrivate::libusbErrorToString(), UsbInterfacePrivate::libusbErrorToXrv(), UsbInterfacePrivate::m_dataInEndPoint, UsbInterfacePrivate::m_deviceHandle, UsbInterfacePrivate::m_lastResult, UsbInterfacePrivate::UsbContext::m_libUsb, UsbInterfacePrivate::m_timeout, mrpt::system::os::memcpy(), mrpt::system::os::sprintf(), usbAddress(), usbBus(), XRV_NOPORTOPEN, XRV_OK, and XS_MAX_FILENAME_LENGTH.

◆ setRawIo()

void UsbInterface::setRawIo ( bool  enable)

Sets the RAWIO mode of the USB interface.

Note
Only applies to WinUSB implementations
Parameters
enable: If true will enable RAW IO mode

Definition at line 913 of file usbinterface.cpp.

References d, JLDEBUG, UsbInterfacePrivate::m_lastResult, void(), and XRV_OK.

◆ setReadPosition()

XsResultValue IoInterface::setReadPosition ( XsFilePos  pos)
virtualinherited

Set the new absolute read position.

The read position is checked against the filesize first.

Parameters
posThe new read position
Returns
XRV_OK if the read position was updated successfully

Reimplemented in IoInterfaceFile.

Definition at line 151 of file iointerface.cpp.

References void(), and XRV_INVALIDOPERATION.

◆ setTimeout()

XsResultValue UsbInterface::setTimeout ( uint32_t  ms)
virtual

Set the default timeout value to use in blocking operations.

This function sets the value of m_timeout. There is no infinity value. The value 0 means that the default value is used.

Parameters
msThe desired timeout in milliseconds
Returns
XRV_OK if the timeout value was successfully updated

Implements StreamInterface.

Definition at line 888 of file usbinterface.cpp.

References d, FALSE, JLDEBUG, UsbInterfacePrivate::m_lastResult, UsbInterfacePrivate::m_timeout, and XRV_OK.

Referenced by open().

◆ setWritePosition()

XsResultValue IoInterface::setWritePosition ( XsFilePos  pos = -1)
virtualinherited

Set the new absolute write position.

The write position is checked against the filesize first.

Parameters
posThe new write position
Returns
XRV_OK if the write position was updated successfully

Reimplemented in IoInterfaceFile.

Definition at line 157 of file iointerface.cpp.

References void(), and XRV_INVALIDOPERATION.

◆ usbAddress()

uint8_t UsbInterface::usbAddress ( ) const

◆ usbBus()

uint8_t UsbInterface::usbBus ( ) const

The USB bus number this device is on (libusb/linux only)

Definition at line 1061 of file usbinterface.cpp.

References d, XsLibUsb::get_bus_number, XsLibUsb::get_device, UsbInterfacePrivate::getContextManager(), UsbInterfacePrivate::m_deviceHandle, and UsbInterfacePrivate::UsbContext::m_libUsb.

Referenced by readData(), and writeData().

◆ waitForData() [1/3]

XsResultValue IoInterface::waitForData

Wait for data to arrive or a timeout to occur.

The function waits until maxLength data is available or until a timeout occurs. The function returns success if data is available or XsResultValue::TIMEOUT if a timeout occurred. A timeout value of 0 indicates that the default timeout stored in the class should be used.

Parameters
maxLengthThe maximum number of bytes to read before returning
dataThe buffer to put the read data in.
Returns
XRV_OK if maxLength bytes were read, XRV_TIMEOUT if less was read, XRV_TIMEOUTNODATA if nothing was read

Definition at line 65 of file iointerface.cpp.

◆ waitForData() [2/3]

XsResultValue UsbInterface::waitForData ( XsSize  maxLength,
void data,
XsSize length = NULL 
)

Wait for data to arrive or a timeout to occur.

The function waits until maxLength data is available or until a timeout occurs. The function returns success if data is available or XsResultValue::TIMEOUT if a timeout occurred. A timeout value of 0 indicates that the default timeout stored in the class should be used.

Parameters
maxLengthThe maximum number of bytes to wait for
dataA buffer that will be filled with the read data. It must be able to contain at least maxLength bytes.
lengthAn optional pointer to storage for the actual number of bytes read.
Returns
XRV_OK if the requested data was read

Definition at line 954 of file usbinterface.cpp.

References d, xsens::getTimeOfDay(), JLTRACE, UsbInterfacePrivate::m_lastResult, UsbInterfacePrivate::m_timeout, readData(), XRV_OK, and XRV_TIMEOUT.

◆ waitForData() [3/3]

XsResultValue IoInterface::waitForData ( XsSize  maxLength,
XsByteArray data 
)
virtualinherited

Wait for data to arrive or a timeout to occur.

The function waits until maxLength data is available or until a timeout occurs. The function returns success if data is available or XsResultValue::TIMEOUT if a timeout occurred. A timeout value of 0 indicates that the default timeout stored in the class should be used.

Parameters
maxLengthThe maximum number of bytes to read before returning
dataThe buffer to put the read data in.
Returns
XRV_OK if maxLength bytes were read, XRV_TIMEOUT if less was read, XRV_TIMEOUTNODATA if nothing was read

Reimplemented in SerialInterface.

Definition at line 65 of file iointerface.cpp.

References void(), and XRV_INVALIDOPERATION.

◆ writeData() [1/2]

XsResultValue UsbInterface::writeData ( const XsByteArray data,
XsSize written = NULL 
)
virtual

Write the data to the USB port.

The function writes the given data to the connected USB port. The default timeout is respected in this operation.

Parameters
dataThe data to be written
writtenAn optional pointer to storage for the actual number of bytes that were written
Returns
XRV_OK if the data was successfully written
See also
writeData(const XsSize, const void *, XsSize*)

Implements IoInterface.

Definition at line 990 of file usbinterface.cpp.

◆ writeData() [2/2]

XsResultValue UsbInterface::writeData ( XsSize  length,
const void data,
XsSize written = NULL 
)

Write the data to the USB port.

The function writes the given data to the connected USB port. The default timeout is respected in this operation.

Parameters
lengthThe number of bytes to write.
dataA pointer to a memory buffer that contains the bytes to send
writtenAn optional pointer to storage for the actual number of bytes that were written
Returns
XRV_OK if the data was successfully written
See also
writeData(const XsByteArray&, XsSize*)

Definition at line 1004 of file usbinterface.cpp.

References XsLibUsb::bulk_transfer, d, mrpt::system::os::fopen(), UsbInterfacePrivate::getContextManager(), isOpen(), JLALERT, UsbInterfacePrivate::libusbErrorToString(), UsbInterfacePrivate::libusbErrorToXrv(), UsbInterfacePrivate::m_dataOutEndPoint, UsbInterfacePrivate::m_deviceHandle, UsbInterfacePrivate::m_lastResult, UsbInterfacePrivate::UsbContext::m_libUsb, mrpt::system::os::sprintf(), usbAddress(), usbBus(), XRV_NOPORTOPEN, XRV_OK, and XS_MAX_FILENAME_LENGTH.

Member Data Documentation

◆ d

UsbInterfacePrivate* UsbInterface::d
private



Page generated by Doxygen 1.8.14 for MRPT 1.5.7 Git: 5902e14cc Wed Apr 24 15:04:01 2019 +0200 at lun oct 28 01:39:17 CET 2019