Software APIs
Public Types | Public Member Functions | Static Public Member Functions | Protected Types | Protected Member Functions | Protected Attributes | Static Protected Attributes
USBDevStream Class Referenceabstract

Public Types

enum  StreamType {
  StreamType_Serial ,
  StreamType_Bulk ,
  StreamType_Interrupt ,
  StreamType_Isochronous ,
  StreamType_Control
}
 Stream implementations.
 

Public Member Functions

 USBDevStream (unsigned id, uint32_t num_bytes, bool retrieve, bool check, bool send, bool verbose)
 
void SetClosing (bool closing)
 Record whether or not the stream is in the process of closing (shutting down or suspending). More...
 
bool CanSchedule () const
 Indicate whether an additional I/O transfer may safely be scheduled on this stream, eg. More...
 
virtual void Stop ()=0
 Finalize the stream, prior to shutting down.
 
virtual void Pause ()=0
 Pause the stream, prior to suspending the device.
 
virtual bool Resume ()=0
 Resume streaming.
 
unsigned ID () const
 Return the Stream IDentifier of this stream.
 
std::string PrefixID ()
 Return a Stream IDentifier prefix suitable for logging/reporting.
 
virtual std::string Report (bool status=false, bool verbose=false) const =0
 Return a summary report of the stream settings or status. More...
 
void SetProperties (unsigned id, bool retrieve, bool check, bool send)
 Set Stream IDentifier and flags.
 
virtual bool Service ()
 Service this stream. More...
 
virtual bool Completed () const
 Indicates whether this stream has completed its transfer. More...
 
uint32_t TransferBytes () const
 Returns the total number of bytes to be transferred by this stream. More...
 
uint32_t BytesRecvd () const
 Returns a count of the number of bytes received from the device. More...
 
uint32_t BytesSent () const
 Returns a count of the number of bytes sent to the device. More...
 

Static Public Member Functions

static const char * StreamTypeName (StreamType type)
 Return the textual name of the given stream type. More...
 

Protected Types

enum  sig_state_t {
  kSigStateStart = 0 ,
  kSigStateCheckHead ,
  kSigStateSkipBody ,
  kSigStateCheckTail ,
  kSigStateReceived
}
 States in reception of signature.
 

Protected Member Functions

bool ProvisionSpace (uint8_t **space, uint32_t len)
 Provision the given number of bytes of contiguous space, and optionally a pointer to the start of the free space. More...
 
uint32_t SpaceAvailable (uint8_t **space)
 Returns the amount of contiguous free space available in the buffer, and optionally a pointer to the start of the free space. More...
 
bool AddData (const uint8_t *data, uint32_t len)
 Add the specified number of bytes to the circular buffer; if data is NULL then the bytes shall already be present in the buffer and copying is not performed. More...
 
bool CommitData (uint32_t len)
 Record that the specified number of bytes have been added to the circular buffer. More...
 
void ClearBuffer ()
 CLear the circular buffer by removing all of its contained data bytes. More...
 
void SigReset ()
 Reset the signature detection; Isochronous streams including a new signature at the start of each packet transferred.
 
bool SigReceived () const
 Has valid signature been received on this stream? Note that there may be some additional validity checks required for specific= transfer types. More...
 
void SigProcess (const usbdev_stream_sig_t &sig)
 Collect stream flags from the supplied signature. More...
 
uint32_t SigDetect (usbdev_stream_sig_t *sig, const uint8_t *sp, uint32_t nrecv)
 Detect and parse stream/packet signature, returning a count of the number of bytes to be discarded from the start of this data. More...
 
void SigReport (const usbdev_stream_sig_t &sig)
 Diagnostic utility function to report the contents of a stream/packet signature. More...
 
void GenerateData (uint8_t *dp, uint32_t len)
 Generate a sequence of bytes as if we'd received them correctly from the device.
 
bool ProcessData (uint8_t *dp, uint32_t len)
 Process the given sequence of bytes according to the current stream state.
 
uint32_t DataAvailable (uint8_t **data)
 Return the number of contiguous bytes of data available in the stream buffer, and a pointer to the first byte of data. More...
 
bool DiscardData (uint32_t len)
 Update the stream buffer to indicate that data has been discarded (removed from the buffer but not sent to the USB device). More...
 
bool ConsumeData (uint32_t len)
 Update the stream buffer to indicate that data has been consumed. More...
 
uint8_t sig_read8 (size_t offset)
 
uint16_t sig_read16 (size_t offset)
 
uint32_t sig_read32 (size_t offset)
 

Protected Attributes

unsigned id_
 Stream IDentifier.
 
bool closing_
 Is the stream being closed?
 
sig_state_t sig_recvd_
 Have we received the stream signature yet?
 
unsigned sig_cnt_
 
bool retrieve_
 Retrieve IN data for this stream?
 
bool check_
 Check the received data against expectations?
 
bool send_
 Send OUT data for this stream?
 
bool verbose_
 Verbose reporting?
 
uint32_t bytes_recvd_
 Total number of bytes received.
 
uint32_t bytes_sent_
 Total number of bytes sent.
 
uint8_t tst_lfsr_
 Device-side LFSR; byte stream expected from usbdev_stream_test.
 
uint8_t dpi_lfsr_
 Host/DPI-side LFSR.
 
uint32_t transfer_bytes_
 Number of bytes to be transferred.
 
struct {
uint32_t wr_idx
 Offset at which to write the next received data (IN from device).
 
uint32_t rd_idx
 Offset of next byte to be read from the buffer (OUT to device).
 
uint32_t end_idx
 Offset beyond used portion of the buffer; for packet-based transmission we wrap before the end of the circular buffer if a maximum-length packet does not fit.
 
uint8_t data [kBufferSize]
 Circular buffer of data being transferred from input to output port.
 
buf_
 Circular buffer of streamed data.
 

Static Protected Attributes

static constexpr uint32_t kBufferSize = 0x10000U
 Size of circular buffer used for streaming.
 

Detailed Description

Definition at line 51 of file usbdev_stream.h.

Member Function Documentation

◆ AddData()

bool USBDevStream::AddData ( const uint8_t *  data,
uint32_t  len 
)
protected

Add the specified number of bytes to the circular buffer; if data is NULL then the bytes shall already be present in the buffer and copying is not performed.

Parameters
dataThe data to be added to the buffer, or NULL
lenThe number of bytes to be added.
Returns
The success of the operation.

Definition at line 239 of file usbdev_stream.cc.

◆ BytesRecvd()

uint32_t USBDevStream::BytesRecvd ( ) const
inline

Returns a count of the number of bytes received from the device.

Returns
Number of bytes received.

Definition at line 149 of file usbdev_stream.h.

◆ BytesSent()

uint32_t USBDevStream::BytesSent ( ) const
inline

Returns a count of the number of bytes sent to the device.

Returns
Number of bytes sent.

Definition at line 155 of file usbdev_stream.h.

◆ CanSchedule()

bool USBDevStream::CanSchedule ( ) const
inline

Indicate whether an additional I/O transfer may safely be scheduled on this stream, eg.

it is not suspending/shutting down.

Returns
true iff another I/O transfer may be scheduled.

Definition at line 82 of file usbdev_stream.h.

◆ ClearBuffer()

void USBDevStream::ClearBuffer ( )
protected

CLear the circular buffer by removing all of its contained data bytes.

Note: this is achieved by advancing the read index to match the current write index, so all committed/added data is discarded, by any write data that has only been provisioned at this point does remain valid and may still be committed subsequently.

Definition at line 376 of file usbdev_stream.cc.

◆ CommitData()

bool USBDevStream::CommitData ( uint32_t  len)
inlineprotected

Record that the specified number of bytes have been added to the circular buffer.

The space must already have provisioned and the data bytes shall already be in the buffer.

Definition at line 214 of file usbdev_stream.h.

◆ Completed()

bool USBDevStream::Completed ( ) const
virtual

Indicates whether this stream has completed its transfer.

Returns
true iff this stream has nothing more to do.

Reimplemented in USBDevIso.

Definition at line 194 of file usbdev_stream.cc.

◆ ConsumeData()

bool USBDevStream::ConsumeData ( uint32_t  len)
protected

Update the stream buffer to indicate that data has been consumed.

Parameters
lenNumber of bytes of data consumed.
Returns
true iff the buffer was successfully updated.

Definition at line 367 of file usbdev_stream.cc.

◆ DataAvailable()

uint32_t USBDevStream::DataAvailable ( uint8_t **  data)
protected

Return the number of contiguous bytes of data available in the stream buffer, and a pointer to the first byte of data.

This may be fewer than the total number of bytes in the buffer, if the data wraps at the end of the circular buffer.

Parameters
dataReceives the pointer to the first data byte.
Returns
The number of contiguous data bytes available.

Definition at line 329 of file usbdev_stream.cc.

◆ DiscardData()

bool USBDevStream::DiscardData ( uint32_t  len)
protected

Update the stream buffer to indicate that data has been discarded (removed from the buffer but not sent to the USB device).

Parameters
lenNumber of bytes of data consumed.
Returns
true iff the buffer was successfully updated.

Definition at line 348 of file usbdev_stream.cc.

◆ ProvisionSpace()

bool USBDevStream::ProvisionSpace ( uint8_t **  space,
uint32_t  len 
)
protected

Provision the given number of bytes of contiguous space, and optionally a pointer to the start of the free space.

Parameters
spaceReceives the pointer to the start of the free space, or NULL iff pointer not required.
lenAmount of space (in bytes) to provision.
Returns
true iff the operation was successful.

Definition at line 198 of file usbdev_stream.cc.

◆ Report()

virtual std::string USBDevStream::Report ( bool  status = false,
bool  verbose = false 
) const
pure virtual

Return a summary report of the stream settings or status.

Parameters
statusIndicates whether settings or status requested.
verbosetrue iff a more verbose report is required.
Returns
Status report.

Implemented in USBDevSerial, USBDevIso, and USBDevInt.

◆ Service()

bool USBDevStream::Service ( )
virtual

Service this stream.

Returns
true iff test should continue, false indicates error.

Reimplemented in USBDevSerial, USBDevIso, and USBDevInt.

Definition at line 386 of file usbdev_stream.cc.

◆ SetClosing()

void USBDevStream::SetClosing ( bool  closing)
inline

Record whether or not the stream is in the process of closing (shutting down or suspending).

Parameters
closingWhether or not the stream is now closing.

Definition at line 75 of file usbdev_stream.h.

◆ SigDetect()

uint32_t USBDevStream::SigDetect ( usbdev_stream_sig_t *  sig,
const uint8_t *  sp,
uint32_t  nrecv 
)
protected

Detect and parse stream/packet signature, returning a count of the number of bytes to be discarded from the start of this data.

Usually this is zero, but if a valid stream signature is required, bytes must be discarded until the signature is received.

Definition at line 65 of file usbdev_stream.cc.

◆ SigProcess()

void USBDevStream::SigProcess ( const usbdev_stream_sig_t &  sig)
protected

Collect stream flags from the supplied signature.

Parameters
sigThe latest stream signature received.

Definition at line 159 of file usbdev_stream.cc.

◆ SigReceived()

bool USBDevStream::SigReceived ( ) const
inlineprotected

Has valid signature been received on this stream? Note that there may be some additional validity checks required for specific= transfer types.

Returns
true iff a signature has been received and detected.

Definition at line 251 of file usbdev_stream.h.

◆ SigReport()

void USBDevStream::SigReport ( const usbdev_stream_sig_t &  sig)
protected

Diagnostic utility function to report the contents of a stream/packet signature.

Parameters
sigStream signature to be reported.

Definition at line 174 of file usbdev_stream.cc.

◆ SpaceAvailable()

uint32_t USBDevStream::SpaceAvailable ( uint8_t **  space)
protected

Returns the amount of contiguous free space available in the buffer, and optionally a pointer to the start of the free space.

Parameters
spaceReceives the pointer to the start of the free space, or NULL iff pointer not required.
Returns
The contiguous free space available (in bytes).

Definition at line 220 of file usbdev_stream.cc.

◆ StreamTypeName()

static const char* USBDevStream::StreamTypeName ( StreamType  type)
inlinestatic

Return the textual name of the given stream type.

Parameters
typeStream type.
Returns
name of stream type.

Definition at line 162 of file usbdev_stream.h.

◆ TransferBytes()

uint32_t USBDevStream::TransferBytes ( ) const
inline

Returns the total number of bytes to be transferred by this stream.

Parameters
Numberof bytes to be transferred.

Definition at line 143 of file usbdev_stream.h.