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