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".
8 // Initial Contributors:
9 // Nokia Corporation - initial contribution.
14 // @file testinterfacesettingbase.h
19 #include "testinterfacesettingbase.h"
20 #include "testinterfacebase.h"
21 #include "controltransferrequests.h"
24 namespace NUnitTesting_USBDI
27 CInterfaceSettingBase::CInterfaceSettingBase(const TDesC& aString)
28 : iHashEndpointFunction(EndpointNumberHash),
29 iIdRelEndpoint(EndpointIdentityRelationship),
30 iEndpoints(iHashEndpointFunction,iIdRelEndpoint)
32 // Set the name of this interface setting
33 iSettingString.Copy(aString);
34 iInterfaceInfo().iString = &iSettingString;
36 // Zero count the number of endpoints used for this setting
37 iInterfaceInfo().iTotalEndpointsUsed = 0;
41 CInterfaceSettingBase::~CInterfaceSettingBase()
45 iEndpointReaders.ResetAndDestroy();
46 iEndpointWriters.ResetAndDestroy();
50 const TDesC& CInterfaceSettingBase::Name() const
52 return iSettingString;
56 void CInterfaceSettingBase::SetClassCodeL(TUint8 aClassCode,TUint8 aSubClassCode,TUint8 aDeviceProtocol)
58 // Set class specific information
59 iInterfaceInfo().iClass = TUsbcClassInfo(aClassCode,aSubClassCode,aDeviceProtocol);
63 TInt CInterfaceSettingBase::AddEndpoint(TEndpoint& aEndpoint)
67 if(aEndpoint.iEndpointNumber == EEndpoint0)
72 if(iEndpoints.Count() < KMaxEndpointsPerClient)
74 // Set EEndpoint1, EEndpoint2, EEndpoint3, EEndpoint4 or EEndpoint5
75 iInterfaceInfo().iEndpointData[iEndpoints.Count()] = aEndpoint.iEndpointInfo;
76 iEndpoints.Insert(aEndpoint.iEndpointNumber,aEndpoint);
78 // Update the total endpoints used on this interface
79 iInterfaceInfo().iTotalEndpointsUsed = iEndpoints.Count();
90 void CInterfaceSettingBase::CreateEndpointReaderL(RDevUsbcClient& aClientDriver,TUint aEndpoint)
93 // Create the reader for this endpoint and store in the container
94 CEndpointReader* epReader = new (ELeave) CEndpointReader(aClientDriver,
95 iEndpoints.Find(static_cast<TEndpointNumber>(aEndpoint))->iEndpointNumber);
96 CleanupStack::PushL(epReader);
97 iEndpointReaders.AppendL(epReader);
98 CleanupStack::Pop(epReader);
102 void CInterfaceSettingBase::CreateEndpointWriterL(RDevUsbcClient& aClientDriver,TUint aEndpoint)
105 // Create the writer for this endpoint and store in the container
106 CEndpointWriter* epWriter = new (ELeave) CEndpointWriter(aClientDriver,
107 iEndpoints.Find(static_cast<TEndpointNumber>(aEndpoint))->iEndpointNumber);
108 CleanupStack::PushL(epWriter);
109 iEndpointWriters.AppendL(epWriter);
110 CleanupStack::Pop(epWriter);
113 void CInterfaceSettingBase::WriteSpecifiedDataToEndpointL(const TDesC8& aData,TUint16 aEndpointNumber)
116 // Access the correct writer for the given endpoint
117 // and write the data to the host
118 iEndpointWriters[aEndpointNumber-1]->Write(aData, ETrue);
121 void CInterfaceSettingBase::WriteSpecifiedDataToEndpointL(const TDesC8& aDataPattern, const TUint aNumBytes, TUint16 aEndpointNumber)
124 // Access the correct writer for the given endpoint
125 // and write the data to the host
126 iEndpointWriters[aEndpointNumber-1]->WriteUsingPatternL(aDataPattern,aNumBytes,ETrue);
129 void CInterfaceSettingBase::RepeatedWriteSpecifiedDataToEndpointL(const TDesC8& aDataPattern, TUint aNumBytesPerWrite, TUint aTotalNumBytes, TUint16 aEndpointNumber)
132 // Access the correct writer for the given endpoint
133 // and write the data to the host
134 iEndpointWriters[aEndpointNumber-1]->WriteInPartsUsingPatternL(aDataPattern,aNumBytesPerWrite,aTotalNumBytes,ETrue);
137 void CInterfaceSettingBase::WriteCachedEndpointDataToEndpointL(const TUint16 aReadEndpointNumber,TUint16 aWriteEndpointNumber)
141 iEndpointWriters[aWriteEndpointNumber-1]->Write(iEndpointReaders[aReadEndpointNumber-1]->Buffer(), ETrue);
144 void CInterfaceSettingBase::CancelWriteDataToEndpointL(TUint16 aEndpointNumber)
147 // Access the correct writer for the given endpoint
148 // and cancel any outstanding write. This will not of course work for 'synchronous' writes.
149 iEndpointWriters[aEndpointNumber-1]->Cancel();
152 void CInterfaceSettingBase::WriteSynchronousSpecifiedDataToEndpointL(const TDesC8& aDataPattern, const TUint aNumBytes, TUint16 aEndpointNumber)
155 // Access the correct writer for the given endpoint
156 // and write the data to the host
157 iEndpointWriters[aEndpointNumber-1]->WriteSynchronousUsingPatternL(aDataPattern,aNumBytes,ETrue);
160 void CInterfaceSettingBase::WriteSynchronousSpecifiedDataToAndHaltEndpointL(const TDesC8& aDataPattern, const TUint aNumBytes, TUint16 aEndpointNumber)
163 // Access the correct writer for the given endpoint
164 // and write the data to the host
165 iEndpointWriters[aEndpointNumber-1]->WriteSynchronousUsingPatternAndHaltL(aDataPattern,aNumBytes);
168 void CInterfaceSettingBase::WriteSynchronousCachedEndpointDataToEndpointL(const TUint16 aReadEndpointNumber,TUint16 aWriteEndpointNumber)
172 //Attempt to write the complete cached buffer by starting at zero and choosing the max length posible
173 WriteSynchronousCachedEndpointDataToEndpointL(aReadEndpointNumber, aWriteEndpointNumber, 0, KMaxTInt);
176 void CInterfaceSettingBase::WriteSynchronousCachedEndpointDataToEndpointL(const TUint16 aReadEndpointNumber,TUint16 aWriteEndpointNumber, TUint aStartPoint, TUint aLength)
179 // Access the correct writer for the given endpoint
180 // Access the 'source' endpoint buffer. This contains data from that endpoint's last read.
181 // Write this data to the host using 'write' endpoint.
184 TPtr8 dataPtr(iEndpointReaders[aReadEndpointNumber-1]->Buffer());
185 User::LeaveIfError(dataPtr.MaxLength() == 0);
186 if(aStartPoint+aLength>dataPtr.Length())
187 //allow use of excessive length to imply a write of all the rest of the buffer...
188 //..otherwise 'Mid' would panic!
190 aLength = dataPtr.Length() - aStartPoint;
194 // Note we need a synchronous 'Write' here
195 iEndpointWriters[aWriteEndpointNumber-1]->WriteSynchronous(iEndpointReaders[aReadEndpointNumber-1]->Buffer().Mid(aStartPoint, aLength), ETrue);
198 TBool CInterfaceSettingBase::CachedEndpointResultL(const TUint16 aEndpointNumber)
201 // Access endpoint buffer containing data from the endpoint's last read
202 // and validate with the supplied data pattern
204 return iEndpointReaders[aEndpointNumber-1]->IsValid();
207 TInt CInterfaceSettingBase::NumBytesReadSoFarL(const TUint16 aEndpointNumber)
210 // Access endpoint buffer containing data from the endpoint's last read
211 // and validate with the supplied data pattern
213 return iEndpointReaders[aEndpointNumber-1]->NumBytesReadSoFar();
216 TInt CInterfaceSettingBase::NumBytesWrittenSoFarL(const TUint16 aEndpointNumber)
219 // Access endpoint buffer containing data from the endpoint's last read
220 // and validate with the supplied data pattern
222 return iEndpointWriters[aEndpointNumber-1]->NumBytesWrittenSoFar();
225 TBool CInterfaceSettingBase::ValidateCachedEndpointDataL(const TDesC8& aDataPattern, const TUint aNumBytes, const TUint16 aEndpointNumber)
228 // Access endpoint buffer containing data from the endpoint's last read
229 // and validate with the supplied data pattern
231 return ValidateCachedEndpointDataL(aDataPattern, 0, aNumBytes, aEndpointNumber);
234 TBool CInterfaceSettingBase::ValidateCachedEndpointDataL(const TDesC8& aDataPattern, const TUint aStartPoint, const TUint aNumBytes, const TUint16 aEndpointNumber)
237 __ASSERT_DEBUG(aDataPattern.Length()!=0, User::Panic(_L("Trying to validate with ZERO LENGTH STRING"), KErrArgument));
239 //Check data in endpoint buffer
240 TPtr8 dataToValidate(iEndpointReaders[aEndpointNumber-1]->Buffer());
241 User::LeaveIfError(dataToValidate.MaxLength() == 0);
243 TUint startPoint = aStartPoint%aDataPattern.Length();
244 TUint numStartBytes = (aDataPattern.Length() - startPoint)%aDataPattern.Length();
245 numStartBytes = aNumBytes<numStartBytes?aNumBytes:numStartBytes; //never test for more than aNumBytes
246 TUint fullRepeats = (aNumBytes-numStartBytes)/aDataPattern.Length();
247 TUint startEndPoint = (fullRepeats*aDataPattern.Length()) + numStartBytes;
248 TUint numEndBytes = aNumBytes - startEndPoint;//fullRepeats*aDataPattern.Length() - numStartBytes;
251 if(dataToValidate.Left(numStartBytes).Compare(aDataPattern.Mid(startPoint, numStartBytes)) != 0)
253 RDebug::Printf("ROUND TRIP VALIDATION: Start Bytes Match Failure");
254 RDebug::Printf("ROUND TRIP VALIDATION: numStartBytes = %d", numStartBytes);
255 RDebug::Printf("Start of sent data ...");
256 RDebug::RawPrint(aDataPattern.Mid(startPoint, numStartBytes));
257 RDebug::Printf("\n");
258 RDebug::Printf("ROUND TRIP VALIDATION: Start of returned data ...");
259 RDebug::RawPrint(dataToValidate.Left(numStartBytes));
260 RDebug::Printf("\n");
266 if(dataToValidate.Mid(startEndPoint,numEndBytes).Compare(aDataPattern.Left(numEndBytes)) != 0)
268 RDebug::Printf("ROUND TRIP VALIDATION: End Bytes Match Failure");
269 RDebug::Printf("ROUND TRIP VALIDATION: startEndPoint = %d, numEndBytes = %d", startEndPoint, numEndBytes);
270 RDebug::Printf("End of sent data ...");
271 RDebug::RawPrint(aDataPattern.Left(numEndBytes));
272 RDebug::Printf("\n");
273 RDebug::Printf("ROUND TRIP VALIDATION: End of returned data ...");
274 RDebug::RawPrint(dataToValidate.Mid(startEndPoint,numEndBytes));
275 RDebug::Printf("\n");
279 for(TInt i=0; i<fullRepeats; i++)
281 if(dataToValidate.Mid(numStartBytes + i*aDataPattern.Length(),aDataPattern.Length()).Compare(aDataPattern) != 0)
283 RDebug::Printf("ROUND TRIP VALIDATION: Repeated Bytes Match Failure, Repeat %d",i);
284 RDebug::Printf("sent data middle block ...");
285 RDebug::RawPrint(aDataPattern);
286 RDebug::Printf("\n");
287 RDebug::Printf("ROUND TRIP VALIDATION: Middle block of returned data ...");
288 RDebug::RawPrint(dataToValidate.Mid(numStartBytes + i*aDataPattern.Length(),aDataPattern.Length()));
289 RDebug::Printf("\n");
290 return EFalse; //from 'for' loop
297 void CInterfaceSettingBase::ReadDataFromEndpointL(TUint aNumBytes, TUint16 aEndpointNumber)
300 // Access the correct readerer for the given endpoint
301 // and prepare to read the data to be sent by the host
302 iEndpointReaders[aEndpointNumber-1]->ReadL(aNumBytes);
305 void CInterfaceSettingBase::CancelAnyReadDataFromEndpointL(TUint16 aEndpointNumber)
308 // Access the correct writer for the given endpoint
309 // and cancel any outstanding write. This will not of course work for 'synchronous' writes.
310 iEndpointReaders[aEndpointNumber-1]->Cancel();
313 void CInterfaceSettingBase::ReadDataFromAndHaltEndpointL(TUint aNumBytes, TUint16 aEndpointNumber)
316 // Access the correct reader for the given endpoint
317 // and prepare to read the data to be sent by the host
318 iEndpointReaders[aEndpointNumber-1]->ReadAndHaltL(aNumBytes);
321 void CInterfaceSettingBase::RepeatedReadAndValidateFromEndpointL(const TDesC8& aDataPattern, TUint aNumBytesPerRead, TUint aTotalNumBytes, TUint16 aEndpointNumber)
324 // Access the correct reader for the given endpoint
325 // and prepare to read the data to be sent by the host
326 // using multiple 'Reads'
327 iEndpointReaders[aEndpointNumber-1]->RepeatedReadAndValidateL(aDataPattern, aNumBytesPerRead, aTotalNumBytes);
330 void CInterfaceSettingBase::ReadDataUntilShortFromEndpointL(TUint aNumBytes, TUint16 aEndpointNumber)
333 // Access the correct reader for the given endpoint
334 // and prepare to read the data to be sent by the host
335 iEndpointReaders[aEndpointNumber-1]->ReadUntilShortL(aNumBytes);