os/kernelhwsrv/kerneltest/f32test/smassstorage/src/tstate.cpp
author sl
Tue, 10 Jun 2014 14:32:02 +0200
changeset 1 260cb5ec6c19
permissions -rw-r--r--
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".
     7 //
     8 // Initial Contributors:
     9 // Nokia Corporation - initial contribution.
    10 //
    11 // Contributors:
    12 //
    13 // Description:
    14 // Implementation of generic finite state machine state
    15 // 
    16 //
    17 
    18 /**
    19  @file
    20  @internalTechnology
    21 */
    22 
    23 #include <f32file.h>
    24 #include <testusbc.h>
    25 
    26 #include "tstate.h"
    27 #include "t_ms_main.h"
    28 #include "scsicmdbuilder.h"
    29 #include "cpropertywatch.h"
    30 
    31 _LIT(KMsFs, "MassStorageFileSystem");
    32 
    33 GLREF_D RFs fs;
    34 GLREF_D TInt removalDrvNo;
    35 GLREF_D TUint8 testLun;
    36 
    37 GLDEF_D TBuf8<KCbwLength> cbwBuf; 
    38 GLDEF_D TBuf8<KCswLength> cswBuf;
    39 
    40 GLDEF_D TInt dCBWTag = 1234567;    // arbitrary, any number would do for this test.
    41 GLDEF_D RDevTestUsbcClient usbcClient;
    42 
    43 /**
    44 Unmount FAT and mount MSFS.
    45 
    46 @param driveNumber
    47 */
    48 LOCAL_C void MountMsFs(TInt driveNumber)
    49 	{
    50 	test.Printf(_L("MountMsFs driveNumber=%d\n"), driveNumber); 
    51 
    52 	TInt err = KErrNone;
    53 	
    54 	TFileName oldFs;
    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);
    58 	if(err==KErrNone)
    59 		{
    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);
    64 		}
    65 	
    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);
    69 	}
    70 
    71 /**
    72 Unmount MSFS and mount FAT.
    73 
    74 @param driveNumber
    75 */
    76 LOCAL_C void UnmountMsFs(TInt driveNumber)
    77 	{
    78 	test.Printf(_L("UnmountMsFs driveNumber=%d\n"), driveNumber); 
    79 	TInt err = fs.DismountFileSystem(KMsFs, driveNumber);
    80 
    81 	test.Printf(_L("MSFS Dismount:%d\n"), err);
    82 	test(err == KErrNone);
    83 
    84 	err = fs.MountFileSystem(_L("FAT"), driveNumber);
    85    	test.Printf(_L("FAT Mount:    %d\n"), err);
    86 	}
    87 
    88 LOCAL_C TBool SendAndReceive(TInt aStatus = 0)
    89 	{
    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());
    95 
    96     // Read CSW
    97     test.Printf(_L("Reading CSW\n"));
    98     usbcClient.HostRead(status, EEndpoint2, cswBuf, KCswLength);
    99     User::WaitForRequest(status);
   100     test(KErrNone == status.Int());
   101 
   102     // Check dCSWTag
   103     TInt recvedCBWTag = extractInt(&cswBuf[4]);
   104     test(dCBWTag == recvedCBWTag);
   105      
   106     // Check bCSWStatus
   107     TInt bCSWStatus = cswBuf[KCswLength - 1];
   108     test.Printf(_L("CSW status: %d\n"), bCSWStatus);
   109     return(bCSWStatus == aStatus);
   110 	}
   111 	
   112 //////////////////////////////////////////////////////////////
   113 
   114 void
   115 TDisconnected::MoveTo(TInt aStateId) const
   116 	{
   117     switch (aStateId)
   118     	{
   119         case EUsbMsDriveState_Connecting:
   120             MoveToConnecting();
   121             break;
   122     	case EUsbMsDriveState_Connected:
   123     		MoveToConnected();
   124     		break;	
   125         default:
   126             test.Printf(_L("Cannot reach %d from %d\n"), GetStateId(), aStateId);
   127             test(EFalse);
   128     	}
   129 	}
   130 
   131 void
   132 TDisconnected::MoveToConnecting() const
   133 	{
   134     test.Printf(_L("Moving to connecting state\n"));
   135    	MountMsFs(removalDrvNo);	
   136 	
   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!
   141 	}
   142 
   143 void
   144 TDisconnected::MoveToConnected() const
   145 	{
   146     test.Printf(_L("Moving to connected state\n"));
   147    	MountMsFs(removalDrvNo);
   148 
   149 	BuildTestUnitReady();
   150 	createCBW(cbwBuf, ++dCBWTag, 0, 0, scsiCmdBuf, testLun);
   151 	test(SendAndReceive(1));
   152 	}
   153 
   154 //////////////////////////////////////////////////////////////
   155 
   156 void
   157 TConnecting::MoveTo(TInt aStateId) const
   158 	{
   159     switch (aStateId)
   160     	{
   161        	case EUsbMsState_Written:
   162            	MoveToWritten();
   163             break;
   164        	default:
   165            	test.Printf(_L("Cannot reach %d from %d\n"), GetStateId(), aStateId);
   166            	test(EFalse);
   167     	}
   168 	}
   169 	
   170 void TConnecting::MoveToWritten() const
   171 	{
   172 	test.Printf(_L("Moving to written state\n"));
   173 	
   174 	// Mount MS file system 
   175 	MountMsFs(removalDrvNo);
   176 	
   177 	BuildTestUnitReady();
   178 	createCBW(cbwBuf, ++dCBWTag, 0, 0, scsiCmdBuf, testLun);
   179 	test(SendAndReceive(1));
   180 
   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);
   186 		
   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());
   193     
   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());
   200 
   201     // Check CSW status
   202     // Read CSW
   203     test.Printf(_L("Reading CSW\n"));
   204     usbcClient.HostRead(status, EEndpoint2, cswBuf, KCswLength);
   205     User::WaitForRequest(status);
   206     test(KErrNone == status.Int());
   207 
   208     // Check dCSWTag
   209     TInt recvedCBWTag = extractInt(&cswBuf[4]);
   210     test(dCBWTag == recvedCBWTag);
   211      
   212     // Check bCSWStatus
   213     TInt bCSWStatus = cswBuf[KCswLength - 1];
   214     test.Printf(_L("CSW status: %d\n"), bCSWStatus);
   215     test(bCSWStatus == 0);	
   216 	}
   217 
   218 //////////////////////////////////////////////////////////////
   219 
   220 void
   221 TConnected::MoveTo(TInt aStateId) const
   222 	{
   223     switch (aStateId)
   224     	{
   225        	case EUsbMsDriveState_Active:
   226            	MoveToActive();
   227             break;
   228        	default:
   229            	test.Printf(_L("Cannot reach %d from %d\n"), GetStateId(), aStateId);
   230            	test(EFalse);
   231     	}
   232 	}
   233 
   234 void
   235 TConnected::MoveToActive() const
   236 	{
   237     test.Printf(_L("Moving to active state\n"));
   238     
   239     // send prevent medium removal using testld
   240     // 1: prevent medium removal
   241     BuildMediumRemoval(1);
   242     createCBW(cbwBuf, ++dCBWTag, 0, 0, scsiCmdBuf, testLun);
   243 
   244 	if(!SendAndReceive())
   245 		{
   246 		// Prevent Media Removal command not supported
   247 		test.Printf(_L("Prevent Media Removal command not supported, issuing read instead\n"));
   248 	
   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);
   254 			
   255 		// Send read command
   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());
   261     
   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());
   268 
   269 		// Check CSW status
   270 		// Read CSW
   271 		test.Printf(_L("Reading CSW\n"));
   272 		usbcClient.HostRead(status, EEndpoint2, cswBuf, KCswLength);
   273 		User::WaitForRequest(status);
   274 		test(KErrNone == status.Int());
   275 
   276 		// Check dCSWTag
   277 		TInt recvedCBWTag = extractInt(&cswBuf[4]);
   278 		test(dCBWTag == recvedCBWTag);
   279      
   280 		// Check bCSWStatus
   281 		TInt bCSWStatus = cswBuf[KCswLength - 1];
   282 		test.Printf(_L("CSW status: %d\n"), bCSWStatus);
   283 		test(bCSWStatus == 0);
   284 		}
   285 	}
   286 
   287 //////////////////////////////////////////////////////////////
   288 
   289 void
   290 TActive::MoveTo(TInt aStateId) const
   291 	{
   292     switch (aStateId)
   293     	{
   294        	case EUsbMsDriveState_Locked:
   295            	MoveToLocked();
   296             break;
   297     	case EUsbMsDriveState_Disconnecting:
   298     		MoveToDisconnecting();
   299     		break;
   300        	default:
   301            	test.Printf(_L("Cannot reach %d from %d\n"), GetStateId(), aStateId);
   302            	test(EFalse);
   303     	}
   304 	}
   305 
   306 void
   307 TActive::MoveToLocked() const
   308 	{
   309     test.Printf(_L("Moving to locked state\n"));
   310     // To be implemented. Wait for lock defect fix
   311 	}
   312 	
   313 void
   314 TActive::MoveToDisconnecting() const
   315 	{
   316     test.Printf(_L("Moving to disconnecting state\n"));
   317     
   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"));
   324 	
   325 	// Now the state is connected, let's move to disconnecting state
   326 	// by sending a stop unit command
   327 	
   328 	// 0: stop unit
   329 	BuildStartStopUnit(0);
   330 	createCBW(cbwBuf, ++dCBWTag, 0, 0, scsiCmdBuf, testLun);
   331 	test(SendAndReceive());
   332 	}
   333 
   334 //////////////////////////////////////////////////////////////
   335 
   336 void
   337 TLocked::MoveTo(TInt aStateId) const
   338 	{
   339     switch (aStateId)
   340     	{
   341        	case EUsbMsDriveState_Disconnecting:
   342            	MoveToDisconnecting();
   343             break;
   344        	default:
   345            	test.Printf(_L("Cannot reach %d from %d\n"), GetStateId(), aStateId);
   346            	test(EFalse);
   347     	}
   348 	}
   349 
   350 void
   351 TLocked::MoveToDisconnecting() const
   352 	{
   353     test.Printf(_L("Moving to disconnecting state\n"));
   354     // To be implemented once lock issue is resolved
   355 	}
   356 
   357 //////////////////////////////////////////////////////////////
   358 
   359 void
   360 TDisconnecting::MoveTo(TInt aStateId) const
   361 	{
   362     switch (aStateId)
   363     	{
   364        	case EUsbMsDriveState_Disconnected:
   365            	MoveToDisconnected();
   366             break;
   367        	default:
   368            	test.Printf(_L("Cannot reach %d from %d\n"), GetStateId(), aStateId);
   369            	test(EFalse);
   370     	}
   371 	}
   372 
   373 void
   374 TDisconnecting::MoveToDisconnected() const
   375 	{
   376     test.Printf(_L("Moving to disconnected state\n"));
   377     UnmountMsFs(removalDrvNo);
   378 	}
   379 
   380 //////////////////////////////////////////////////////////////
   381 
   382 void
   383 TWritten::MoveTo(TInt aStateId) const
   384 	{
   385     switch (aStateId)
   386     	{
   387        	case EUsbMsState_Read:
   388            	MoveToRead();
   389             break;
   390        	default:
   391            	test.Printf(_L("Cannot reach %d from %d\n"), GetStateId(), aStateId);
   392            	test(EFalse);
   393     	}
   394 	}
   395 
   396 void
   397 TWritten::MoveToRead() const
   398 	{
   399     test.Printf(_L("Moving to read state\n"));
   400 	
   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);
   406 		
   407     // Send read command
   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());
   413     
   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());
   420 
   421     // Check CSW status
   422     // Read CSW
   423     test.Printf(_L("Reading CSW\n"));
   424     usbcClient.HostRead(status, EEndpoint2, cswBuf, KCswLength);
   425     User::WaitForRequest(status);
   426     test(KErrNone == status.Int());
   427 
   428     // Check dCSWTag
   429     TInt recvedCBWTag = extractInt(&cswBuf[4]);
   430     test(dCBWTag == recvedCBWTag);
   431      
   432     // Check bCSWStatus
   433     TInt bCSWStatus = cswBuf[KCswLength - 1];
   434     test.Printf(_L("CSW status: %d\n"), bCSWStatus);
   435     test(bCSWStatus == 0);		
   436 	}
   437 
   438 //////////////////////////////////////////////////////////////
   439 
   440 void
   441 TRead::MoveTo(TInt aStateId) const
   442 	{
   443     switch (aStateId)
   444     	{
   445        	case EUsbMsDriveState_Disconnected:
   446            	MoveToDisconnected();
   447             break;
   448        	default:
   449            	test.Printf(_L("Cannot reach %d from %d\n"), GetStateId(), aStateId);
   450            	test(EFalse);
   451     	}
   452 	}
   453 
   454 void
   455 TRead::MoveToDisconnected() const
   456 	{
   457     test.Printf(_L("Moving to disconnected state\n"));
   458     UnmountMsFs(removalDrvNo);
   459 	}
   460