Update contrib.
1 // Copyright (c) 2004-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 // Implementation of generic finite state machine state
27 #include "t_ms_main.h"
28 #include "scsicmdbuilder.h"
29 #include "cpropertywatch.h"
31 _LIT(KMsFs, "MassStorageFileSystem");
34 GLREF_D TInt removalDrvNo;
35 GLREF_D TUint8 testLun;
37 GLDEF_D TBuf8<KCbwLength> cbwBuf;
38 GLDEF_D TBuf8<KCswLength> cswBuf;
40 GLDEF_D TInt dCBWTag = 1234567; // arbitrary, any number would do for this test.
41 GLDEF_D RDevTestUsbcClient usbcClient;
44 Unmount FAT and mount MSFS.
48 LOCAL_C void MountMsFs(TInt driveNumber)
50 test.Printf(_L("MountMsFs driveNumber=%d\n"), driveNumber);
55 err = fs.FileSystemName(oldFs, driveNumber);
56 test.Printf(_L("FAT file system name %S; error code %d\n"), &oldFs, err);
57 test(err == KErrNone || err == KErrNotFound);
60 test.Printf(_L("Unmounting FAT FS %S\n"), &oldFs);
61 err = fs.DismountFileSystem(oldFs, driveNumber);
62 test.Printf(_L("%S Dismount %c: %d\n"), &oldFs,'A' + driveNumber, err);
63 test(err == KErrNone);
66 test.Printf(_L("Mounting MSFS\n"));
67 err = fs.MountFileSystem(KMsFs, driveNumber);
68 test.Printf(_L("MSFS Mount %c: %d\n"), 'A' + driveNumber, err);
72 Unmount MSFS and mount FAT.
76 LOCAL_C void UnmountMsFs(TInt driveNumber)
78 test.Printf(_L("UnmountMsFs driveNumber=%d\n"), driveNumber);
79 TInt err = fs.DismountFileSystem(KMsFs, driveNumber);
81 test.Printf(_L("MSFS Dismount:%d\n"), err);
82 test(err == KErrNone);
84 err = fs.MountFileSystem(_L("FAT"), driveNumber);
85 test.Printf(_L("FAT Mount: %d\n"), err);
88 LOCAL_C TBool SendAndReceive(TInt aStatus = 0)
90 test.Printf(_L("SendAndReceive\n"));
91 TRequestStatus status;
92 usbcClient.HostWrite(status, EEndpoint1, cbwBuf, KCbwLength);
93 User::WaitForRequest(status);
94 test(KErrNone == status.Int());
97 test.Printf(_L("Reading CSW\n"));
98 usbcClient.HostRead(status, EEndpoint2, cswBuf, KCswLength);
99 User::WaitForRequest(status);
100 test(KErrNone == status.Int());
103 TInt recvedCBWTag = extractInt(&cswBuf[4]);
104 test(dCBWTag == recvedCBWTag);
107 TInt bCSWStatus = cswBuf[KCswLength - 1];
108 test.Printf(_L("CSW status: %d\n"), bCSWStatus);
109 return(bCSWStatus == aStatus);
112 //////////////////////////////////////////////////////////////
115 TDisconnected::MoveTo(TInt aStateId) const
119 case EUsbMsDriveState_Connecting:
122 case EUsbMsDriveState_Connected:
126 test.Printf(_L("Cannot reach %d from %d\n"), GetStateId(), aStateId);
132 TDisconnected::MoveToConnecting() const
134 test.Printf(_L("Moving to connecting state\n"));
135 MountMsFs(removalDrvNo);
137 // send test unit ready message
138 BuildTestUnitReady();
139 createCBW(cbwBuf, ++dCBWTag, 0, 0, scsiCmdBuf, testLun);
140 test(SendAndReceive(1)); // 1: the unit is not ready!
144 TDisconnected::MoveToConnected() const
146 test.Printf(_L("Moving to connected state\n"));
147 MountMsFs(removalDrvNo);
149 BuildTestUnitReady();
150 createCBW(cbwBuf, ++dCBWTag, 0, 0, scsiCmdBuf, testLun);
151 test(SendAndReceive(1));
154 //////////////////////////////////////////////////////////////
157 TConnecting::MoveTo(TInt aStateId) const
161 case EUsbMsState_Written:
165 test.Printf(_L("Cannot reach %d from %d\n"), GetStateId(), aStateId);
170 void TConnecting::MoveToWritten() const
172 test.Printf(_L("Moving to written state\n"));
174 // Mount MS file system
175 MountMsFs(removalDrvNo);
177 BuildTestUnitReady();
178 createCBW(cbwBuf, ++dCBWTag, 0, 0, scsiCmdBuf, testLun);
179 test(SendAndReceive(1));
181 // Write 1k bytes using testldd.
182 // 0x2A: opcode for write (10); 10: starting sector; 2: total sectors
183 BuildReadWrite(0x2A, 10, 2);
184 // 0: indicates host writing
185 createCBW(cbwBuf, ++dCBWTag, KKiloBytes, 0, scsiCmdBuf, testLun);
187 // Send write command
188 test.Printf(_L("Sending CBW write cmd\n"));
189 TRequestStatus status;
190 usbcClient.HostWrite(status, EEndpoint1, cbwBuf, KCbwLength);
191 User::WaitForRequest(status);
192 test(KErrNone == status.Int());
194 // Write actual data. We don't care the contents.
195 TBuf8<KKiloBytes> writeBuf;
196 writeBuf.SetLength(KKiloBytes);
197 usbcClient.HostWrite(status, EEndpoint1, writeBuf, KKiloBytes);
198 User::WaitForRequest(status);
199 test(KErrNone == status.Int());
203 test.Printf(_L("Reading CSW\n"));
204 usbcClient.HostRead(status, EEndpoint2, cswBuf, KCswLength);
205 User::WaitForRequest(status);
206 test(KErrNone == status.Int());
209 TInt recvedCBWTag = extractInt(&cswBuf[4]);
210 test(dCBWTag == recvedCBWTag);
213 TInt bCSWStatus = cswBuf[KCswLength - 1];
214 test.Printf(_L("CSW status: %d\n"), bCSWStatus);
215 test(bCSWStatus == 0);
218 //////////////////////////////////////////////////////////////
221 TConnected::MoveTo(TInt aStateId) const
225 case EUsbMsDriveState_Active:
229 test.Printf(_L("Cannot reach %d from %d\n"), GetStateId(), aStateId);
235 TConnected::MoveToActive() const
237 test.Printf(_L("Moving to active state\n"));
239 // send prevent medium removal using testld
240 // 1: prevent medium removal
241 BuildMediumRemoval(1);
242 createCBW(cbwBuf, ++dCBWTag, 0, 0, scsiCmdBuf, testLun);
244 if(!SendAndReceive())
246 // Prevent Media Removal command not supported
247 test.Printf(_L("Prevent Media Removal command not supported, issuing read instead\n"));
249 // Read 1k bytes using testldd.
250 // 0x28: opcode for read (10); 10: starting sector; 2: total sectors
251 BuildReadWrite(0x28, 10, 2);
252 // 0x80: indicates host writing
253 createCBW(cbwBuf, ++dCBWTag, KKiloBytes, 0x80, scsiCmdBuf, testLun);
256 test.Printf(_L("Sending CBW read cmd\n"));
257 TRequestStatus status;
258 usbcClient.HostWrite(status, EEndpoint1, cbwBuf, KCbwLength);
259 User::WaitForRequest(status);
260 test(KErrNone == status.Int());
262 // Read actual data. We don't care the contents.
263 TBuf8<KKiloBytes> readBuf;
264 readBuf.SetLength(KKiloBytes);
265 usbcClient.HostRead(status, EEndpoint2, readBuf, KKiloBytes);
266 User::WaitForRequest(status);
267 test(KErrNone == status.Int());
271 test.Printf(_L("Reading CSW\n"));
272 usbcClient.HostRead(status, EEndpoint2, cswBuf, KCswLength);
273 User::WaitForRequest(status);
274 test(KErrNone == status.Int());
277 TInt recvedCBWTag = extractInt(&cswBuf[4]);
278 test(dCBWTag == recvedCBWTag);
281 TInt bCSWStatus = cswBuf[KCswLength - 1];
282 test.Printf(_L("CSW status: %d\n"), bCSWStatus);
283 test(bCSWStatus == 0);
287 //////////////////////////////////////////////////////////////
290 TActive::MoveTo(TInt aStateId) const
294 case EUsbMsDriveState_Locked:
297 case EUsbMsDriveState_Disconnecting:
298 MoveToDisconnecting();
301 test.Printf(_L("Cannot reach %d from %d\n"), GetStateId(), aStateId);
307 TActive::MoveToLocked() const
309 test.Printf(_L("Moving to locked state\n"));
310 // To be implemented. Wait for lock defect fix
314 TActive::MoveToDisconnecting() const
316 test.Printf(_L("Moving to disconnecting state\n"));
318 // send allow medium removal using testld
319 // 0: allow medium removal
320 BuildMediumRemoval(0);
321 createCBW(cbwBuf, ++dCBWTag, 0, 0, scsiCmdBuf, testLun);
322 if(!SendAndReceive())
323 test.Printf(_L("Prevent Media Removal command not supported, no need to allow\n"));
325 // Now the state is connected, let's move to disconnecting state
326 // by sending a stop unit command
329 BuildStartStopUnit(0);
330 createCBW(cbwBuf, ++dCBWTag, 0, 0, scsiCmdBuf, testLun);
331 test(SendAndReceive());
334 //////////////////////////////////////////////////////////////
337 TLocked::MoveTo(TInt aStateId) const
341 case EUsbMsDriveState_Disconnecting:
342 MoveToDisconnecting();
345 test.Printf(_L("Cannot reach %d from %d\n"), GetStateId(), aStateId);
351 TLocked::MoveToDisconnecting() const
353 test.Printf(_L("Moving to disconnecting state\n"));
354 // To be implemented once lock issue is resolved
357 //////////////////////////////////////////////////////////////
360 TDisconnecting::MoveTo(TInt aStateId) const
364 case EUsbMsDriveState_Disconnected:
365 MoveToDisconnected();
368 test.Printf(_L("Cannot reach %d from %d\n"), GetStateId(), aStateId);
374 TDisconnecting::MoveToDisconnected() const
376 test.Printf(_L("Moving to disconnected state\n"));
377 UnmountMsFs(removalDrvNo);
380 //////////////////////////////////////////////////////////////
383 TWritten::MoveTo(TInt aStateId) const
387 case EUsbMsState_Read:
391 test.Printf(_L("Cannot reach %d from %d\n"), GetStateId(), aStateId);
397 TWritten::MoveToRead() const
399 test.Printf(_L("Moving to read state\n"));
401 // Read 1k bytes using testldd.
402 // 0x28: opcode for read (10); 10: starting sector; 2: total sectors
403 BuildReadWrite(0x28, 10, 2);
404 // 0x80: indicates host writing
405 createCBW(cbwBuf, ++dCBWTag, KKiloBytes, 0x80, scsiCmdBuf, testLun);
408 test.Printf(_L("Sending CBW read cmd\n"));
409 TRequestStatus status;
410 usbcClient.HostWrite(status, EEndpoint1, cbwBuf, KCbwLength);
411 User::WaitForRequest(status);
412 test(KErrNone == status.Int());
414 // Read actual data. We don't care the contents.
415 TBuf8<KKiloBytes> readBuf;
416 readBuf.SetLength(KKiloBytes);
417 usbcClient.HostRead(status, EEndpoint2, readBuf, KKiloBytes);
418 User::WaitForRequest(status);
419 test(KErrNone == status.Int());
423 test.Printf(_L("Reading CSW\n"));
424 usbcClient.HostRead(status, EEndpoint2, cswBuf, KCswLength);
425 User::WaitForRequest(status);
426 test(KErrNone == status.Int());
429 TInt recvedCBWTag = extractInt(&cswBuf[4]);
430 test(dCBWTag == recvedCBWTag);
433 TInt bCSWStatus = cswBuf[KCswLength - 1];
434 test.Printf(_L("CSW status: %d\n"), bCSWStatus);
435 test(bCSWStatus == 0);
438 //////////////////////////////////////////////////////////////
441 TRead::MoveTo(TInt aStateId) const
445 case EUsbMsDriveState_Disconnected:
446 MoveToDisconnected();
449 test.Printf(_L("Cannot reach %d from %d\n"), GetStateId(), aStateId);
455 TRead::MoveToDisconnected() const
457 test.Printf(_L("Moving to disconnected state\n"));
458 UnmountMsFs(removalDrvNo);