Read Data from USB using Shared Chunks

This tutorial describes how to read shared chunk data from a USB connection.

Read data

To read data from the host to the device (Out) call the function RDevUsbcScClient::ReadDataNotify() to make a request to wait for data in the specified buffer and wait for it to complete.

If the buffer already has data in it, it will return immediately with the code KErrCompletion. If there is no data in the buffer an asynchronous request is set up that will complete when data is available. Note: check the return code before attempting to wait for the request to complete.

TUsbcScHdrEndpointRecord* epInfo;
TRequestStatus status;

TUsbcScChunkHeader chunkHeader(gChunk);
    
chunkHeader.GetBuffer(0, 2, epInfo);  
TInt outBuffNum = epInfo->iBufferNo;

r = gPort.ReadDataNotify(outBuffNum,status);

The first parameter required by ReadDataNotify() is the endpoint buffer number. The endpoint buffer number can be found in the alternate setting table in the chunk header. The location of the buffer can be found by looking at the buffer offset table, which is also in the chunk header.

When the request has completed, process the data in the buffer that was read from the driver.

The transfer buffer for an IN endpoint is always pointed to by the iTail member of the endpoint buffer struct SUsbcScBufferHeader. When finished processing the current chunk move iTail to point to the next transfer buffer.

iTransfer = (TUsbcScTransferHeader*) (iHeader->iTail + iBase);
... // Process data
iHeader->iTail = iTransfer->iNext;

If there is no more data to be read then close the channel and unload the driver.

Next steps

When you have finished reading and writing Close and Unload the Driver.