os/kernelhwsrv/kerneltest/e32test/usbho/t_usbdi/src/hostisochronoustransfers.cpp
author sl
Tue, 10 Jun 2014 14:32:02 +0200
changeset 1 260cb5ec6c19
permissions -rw-r--r--
Update contrib.
     1 // Copyright (c) 2007-2009 Nokia Corporation and/or its subsidiary(-ies).
     2 // All rights reserved.
     3 // This component and the accompanying materials are made available
     4 // under the terms of the License "Eclipse Public License v1.0"
     5 // which accompanies this distribution, and is available
     6 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
     7 //
     8 // Initial Contributors:
     9 // Nokia Corporation - initial contribution.
    10 //
    11 // Contributors:
    12 //
    13 // Description:
    14 // @file hostisochronoustransfers.cpp
    15 // @internalComponent
    16 // 
    17 //
    18 
    19 #include "hosttransfers.h"
    20 #include <e32debug.h>
    21 #include <e32test.h>
    22 
    23 extern RTest gtest;
    24 
    25 namespace NUnitTesting_USBDI
    26 	{
    27 
    28 CIsochTransfer::CIsochTransfer(RUsbPipe& aPipe,RUsbInterface& aInterface,TUint16 aMaxPacketSize,
    29 							   TInt aMaxNumPackets,MTransferObserver& aObserver,TInt aTransferId)
    30 :	CBaseTransfer(aPipe,aInterface,aObserver,aTransferId),
    31 	iTransferDescriptor(aMaxPacketSize,aMaxNumPackets),
    32 	iMaxPacketSize(aMaxPacketSize)
    33 	{
    34 	RDebug::Printf("aMaxPacketSize = %d, aMaxNumPackets = %d",aMaxPacketSize, aMaxNumPackets);
    35 	}
    36 	
    37 CIsochTransfer::~CIsochTransfer()
    38 	{
    39 	LOG_FUNC
    40 	
    41 	// Cancel the transfer
    42 
    43 	Cancel();
    44 	}	
    45 	
    46 TBool CIsochTransfer::DataPolled(TUint aPacketsToBeRead, RBuf8& aDataPolled) 
    47 	{
    48 	LOG_FUNC
    49 	TInt numOfPacketsReturned = 0;	
    50 	
    51 	TInt firstPacketIndex = 0;
    52 	TInt totalPacketsRead = 0;
    53 	TInt packetsToBeRead = aPacketsToBeRead;
    54 
    55 	TUint dataPolledBufSize = iMaxPacketSize*aPacketsToBeRead;
    56 	aDataPolled.CreateL(dataPolledBufSize);
    57 		
    58 	do {						
    59 		TPtrC8 ptrRet = iTransferDescriptor.Packets(firstPacketIndex, packetsToBeRead, numOfPacketsReturned); 
    60 		RDebug::Printf("numOfPacketsReturned = %d", numOfPacketsReturned);
    61 		RDebug::Printf("ptrRet.Length() = %d", ptrRet.Length());
    62 		firstPacketIndex = numOfPacketsReturned;
    63 		totalPacketsRead += numOfPacketsReturned;
    64 		packetsToBeRead = packetsToBeRead - numOfPacketsReturned;
    65 		RDebug::Printf("totalPacketsRead = %d", totalPacketsRead);	
    66 		RDebug::Printf("packetsToBeRead = %d", packetsToBeRead);	
    67 		aDataPolled.Append(ptrRet);		
    68 		}	while(totalPacketsRead != aPacketsToBeRead); 	
    69 		
    70 	return ETrue; 
    71 	}
    72 	
    73 	
    74 TInt CIsochTransfer::TransferInL(TInt aPacketsExpected)
    75 	{
    76 	LOG_FUNC
    77 	
    78 	// Activate the asynchronous transfer 	
    79 	RDebug::Printf("Activating isoch. in transfer");
    80 	
    81 	iTransferDescriptor.Reset();
    82 	TPacketLengths fullLengths = iTransferDescriptor.Lengths();
    83 	
    84 	for(TInt packet = 0; packet < fullLengths.MaxNumPackets(); packet++)
    85 		{
    86 		fullLengths[packet] = iMaxPacketSize;
    87 		}	
    88 
    89 	RDebug::Printf("fullLengths.MaxNumPackets() == %d",fullLengths.MaxNumPackets());
    90 	iTransferDescriptor.ReceivePackets(aPacketsExpected);
    91 		
    92 	Pipe().Transfer(iTransferDescriptor,iStatus);
    93 	SetActive();
    94 	return KErrNone;																
    95 	}
    96 		
    97 TInt CIsochTransfer::RegisterTransferDescriptor()
    98 	{
    99 	LOG_FUNC
   100 	
   101 	// Register the transfer descriptor with the interface	
   102 	TInt err(Interface().RegisterTransferDescriptor(iTransferDescriptor));
   103 	if(err != KErrNone)
   104 		{
   105 		RDebug::Printf("<Error %d> Unable to register transfer descriptor",err);
   106 		}
   107 	return err;	
   108 	}
   109 	
   110 TInt CIsochTransfer::PrepareTransfer(const TDesC8& aIsochData)
   111 	{
   112 	LOG_FUNC
   113 
   114 	//	
   115 	iTransferDescriptor.Reset();
   116 	TPacketLengths fullLengths = iTransferDescriptor.Lengths();
   117 
   118 	RDebug::Printf("fullLengths.MaxNumPackets() == %d",fullLengths.MaxNumPackets());
   119 	
   120 	//	
   121 	TInt bytesRemaining(aIsochData.Size());
   122 	TInt maxAvailablePacketSlots;
   123 	TInt startOffset(0);
   124 	TInt startPacket(0); 
   125 	
   126 	RDebug::Printf("Audio data is %d bytes",bytesRemaining);
   127 	
   128 	// Keep saving the isoch data to transfer in each packet buffer supplied 
   129 	// by the transfer descriptor
   130 
   131 	while(bytesRemaining)
   132 		{
   133 		// Request a modifiable buffer to write the isoch data to
   134 		TPtr8 packetBuffer = iTransferDescriptor.WritablePackets(bytesRemaining/iMaxPacketSize,maxAvailablePacketSlots);
   135 		TInt dataToWrite = Min(bytesRemaining,packetBuffer.MaxSize());		
   136 		
   137 		if(dataToWrite == 0)
   138 			{
   139 			RDebug::Printf("<Warning> dropping the rest of the isoch data");
   140 			break;
   141 			}
   142 		
   143 		// Validate entire buffer as it is going to be filled
   144 		
   145 		packetBuffer.SetMax();
   146 		// Calculate the number of packets to write in this buffer		
   147 		TInt maxPacket = dataToWrite / iMaxPacketSize;
   148 	
   149 		for(TInt packet = 0; packet < maxPacket; packet++)
   150 			{
   151 			fullLengths[startPacket + packet] = iMaxPacketSize;
   152 			}
   153 		
   154 		packetBuffer.Copy(aIsochData.Mid(startOffset, maxPacket * iMaxPacketSize));
   155 		iTransferDescriptor.SaveMultiple(maxPacket);
   156 		bytesRemaining -= maxPacket * iMaxPacketSize;
   157 		startOffset += maxPacket * iMaxPacketSize;
   158 		startPacket += maxPacket;
   159 		}	
   160 	return KErrNone;
   161 	}
   162 
   163 TInt CIsochTransfer::TransferOut()
   164 	{	
   165 	// Transfer the iscohronous data	
   166 	RDebug::Printf("Activating isochronous out transfer");
   167 	Pipe().Transfer(iTransferDescriptor,iStatus);
   168 	SetActive();
   169 	return KErrNone;
   170 	}
   171 	
   172 		
   173 	}