1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/os/kernelhwsrv/kerneltest/f32test/smassstorage/src/tstate.cpp Fri Jun 15 03:10:57 2012 +0200
1.3 @@ -0,0 +1,460 @@
1.4 +// Copyright (c) 2004-2009 Nokia Corporation and/or its subsidiary(-ies).
1.5 +// All rights reserved.
1.6 +// This component and the accompanying materials are made available
1.7 +// under the terms of the License "Eclipse Public License v1.0"
1.8 +// which accompanies this distribution, and is available
1.9 +// at the URL "http://www.eclipse.org/legal/epl-v10.html".
1.10 +//
1.11 +// Initial Contributors:
1.12 +// Nokia Corporation - initial contribution.
1.13 +//
1.14 +// Contributors:
1.15 +//
1.16 +// Description:
1.17 +// Implementation of generic finite state machine state
1.18 +//
1.19 +//
1.20 +
1.21 +/**
1.22 + @file
1.23 + @internalTechnology
1.24 +*/
1.25 +
1.26 +#include <f32file.h>
1.27 +#include <testusbc.h>
1.28 +
1.29 +#include "tstate.h"
1.30 +#include "t_ms_main.h"
1.31 +#include "scsicmdbuilder.h"
1.32 +#include "cpropertywatch.h"
1.33 +
1.34 +_LIT(KMsFs, "MassStorageFileSystem");
1.35 +
1.36 +GLREF_D RFs fs;
1.37 +GLREF_D TInt removalDrvNo;
1.38 +GLREF_D TUint8 testLun;
1.39 +
1.40 +GLDEF_D TBuf8<KCbwLength> cbwBuf;
1.41 +GLDEF_D TBuf8<KCswLength> cswBuf;
1.42 +
1.43 +GLDEF_D TInt dCBWTag = 1234567; // arbitrary, any number would do for this test.
1.44 +GLDEF_D RDevTestUsbcClient usbcClient;
1.45 +
1.46 +/**
1.47 +Unmount FAT and mount MSFS.
1.48 +
1.49 +@param driveNumber
1.50 +*/
1.51 +LOCAL_C void MountMsFs(TInt driveNumber)
1.52 + {
1.53 + test.Printf(_L("MountMsFs driveNumber=%d\n"), driveNumber);
1.54 +
1.55 + TInt err = KErrNone;
1.56 +
1.57 + TFileName oldFs;
1.58 + err = fs.FileSystemName(oldFs, driveNumber);
1.59 + test.Printf(_L("FAT file system name %S; error code %d\n"), &oldFs, err);
1.60 + test(err == KErrNone || err == KErrNotFound);
1.61 + if(err==KErrNone)
1.62 + {
1.63 + test.Printf(_L("Unmounting FAT FS %S\n"), &oldFs);
1.64 + err = fs.DismountFileSystem(oldFs, driveNumber);
1.65 + test.Printf(_L("%S Dismount %c: %d\n"), &oldFs,'A' + driveNumber, err);
1.66 + test(err == KErrNone);
1.67 + }
1.68 +
1.69 + test.Printf(_L("Mounting MSFS\n"));
1.70 + err = fs.MountFileSystem(KMsFs, driveNumber);
1.71 + test.Printf(_L("MSFS Mount %c: %d\n"), 'A' + driveNumber, err);
1.72 + }
1.73 +
1.74 +/**
1.75 +Unmount MSFS and mount FAT.
1.76 +
1.77 +@param driveNumber
1.78 +*/
1.79 +LOCAL_C void UnmountMsFs(TInt driveNumber)
1.80 + {
1.81 + test.Printf(_L("UnmountMsFs driveNumber=%d\n"), driveNumber);
1.82 + TInt err = fs.DismountFileSystem(KMsFs, driveNumber);
1.83 +
1.84 + test.Printf(_L("MSFS Dismount:%d\n"), err);
1.85 + test(err == KErrNone);
1.86 +
1.87 + err = fs.MountFileSystem(_L("FAT"), driveNumber);
1.88 + test.Printf(_L("FAT Mount: %d\n"), err);
1.89 + }
1.90 +
1.91 +LOCAL_C TBool SendAndReceive(TInt aStatus = 0)
1.92 + {
1.93 + test.Printf(_L("SendAndReceive\n"));
1.94 + TRequestStatus status;
1.95 + usbcClient.HostWrite(status, EEndpoint1, cbwBuf, KCbwLength);
1.96 + User::WaitForRequest(status);
1.97 + test(KErrNone == status.Int());
1.98 +
1.99 + // Read CSW
1.100 + test.Printf(_L("Reading CSW\n"));
1.101 + usbcClient.HostRead(status, EEndpoint2, cswBuf, KCswLength);
1.102 + User::WaitForRequest(status);
1.103 + test(KErrNone == status.Int());
1.104 +
1.105 + // Check dCSWTag
1.106 + TInt recvedCBWTag = extractInt(&cswBuf[4]);
1.107 + test(dCBWTag == recvedCBWTag);
1.108 +
1.109 + // Check bCSWStatus
1.110 + TInt bCSWStatus = cswBuf[KCswLength - 1];
1.111 + test.Printf(_L("CSW status: %d\n"), bCSWStatus);
1.112 + return(bCSWStatus == aStatus);
1.113 + }
1.114 +
1.115 +//////////////////////////////////////////////////////////////
1.116 +
1.117 +void
1.118 +TDisconnected::MoveTo(TInt aStateId) const
1.119 + {
1.120 + switch (aStateId)
1.121 + {
1.122 + case EUsbMsDriveState_Connecting:
1.123 + MoveToConnecting();
1.124 + break;
1.125 + case EUsbMsDriveState_Connected:
1.126 + MoveToConnected();
1.127 + break;
1.128 + default:
1.129 + test.Printf(_L("Cannot reach %d from %d\n"), GetStateId(), aStateId);
1.130 + test(EFalse);
1.131 + }
1.132 + }
1.133 +
1.134 +void
1.135 +TDisconnected::MoveToConnecting() const
1.136 + {
1.137 + test.Printf(_L("Moving to connecting state\n"));
1.138 + MountMsFs(removalDrvNo);
1.139 +
1.140 + // send test unit ready message
1.141 + BuildTestUnitReady();
1.142 + createCBW(cbwBuf, ++dCBWTag, 0, 0, scsiCmdBuf, testLun);
1.143 + test(SendAndReceive(1)); // 1: the unit is not ready!
1.144 + }
1.145 +
1.146 +void
1.147 +TDisconnected::MoveToConnected() const
1.148 + {
1.149 + test.Printf(_L("Moving to connected state\n"));
1.150 + MountMsFs(removalDrvNo);
1.151 +
1.152 + BuildTestUnitReady();
1.153 + createCBW(cbwBuf, ++dCBWTag, 0, 0, scsiCmdBuf, testLun);
1.154 + test(SendAndReceive(1));
1.155 + }
1.156 +
1.157 +//////////////////////////////////////////////////////////////
1.158 +
1.159 +void
1.160 +TConnecting::MoveTo(TInt aStateId) const
1.161 + {
1.162 + switch (aStateId)
1.163 + {
1.164 + case EUsbMsState_Written:
1.165 + MoveToWritten();
1.166 + break;
1.167 + default:
1.168 + test.Printf(_L("Cannot reach %d from %d\n"), GetStateId(), aStateId);
1.169 + test(EFalse);
1.170 + }
1.171 + }
1.172 +
1.173 +void TConnecting::MoveToWritten() const
1.174 + {
1.175 + test.Printf(_L("Moving to written state\n"));
1.176 +
1.177 + // Mount MS file system
1.178 + MountMsFs(removalDrvNo);
1.179 +
1.180 + BuildTestUnitReady();
1.181 + createCBW(cbwBuf, ++dCBWTag, 0, 0, scsiCmdBuf, testLun);
1.182 + test(SendAndReceive(1));
1.183 +
1.184 + // Write 1k bytes using testldd.
1.185 + // 0x2A: opcode for write (10); 10: starting sector; 2: total sectors
1.186 + BuildReadWrite(0x2A, 10, 2);
1.187 + // 0: indicates host writing
1.188 + createCBW(cbwBuf, ++dCBWTag, KKiloBytes, 0, scsiCmdBuf, testLun);
1.189 +
1.190 + // Send write command
1.191 + test.Printf(_L("Sending CBW write cmd\n"));
1.192 + TRequestStatus status;
1.193 + usbcClient.HostWrite(status, EEndpoint1, cbwBuf, KCbwLength);
1.194 + User::WaitForRequest(status);
1.195 + test(KErrNone == status.Int());
1.196 +
1.197 + // Write actual data. We don't care the contents.
1.198 + TBuf8<KKiloBytes> writeBuf;
1.199 + writeBuf.SetLength(KKiloBytes);
1.200 + usbcClient.HostWrite(status, EEndpoint1, writeBuf, KKiloBytes);
1.201 + User::WaitForRequest(status);
1.202 + test(KErrNone == status.Int());
1.203 +
1.204 + // Check CSW status
1.205 + // Read CSW
1.206 + test.Printf(_L("Reading CSW\n"));
1.207 + usbcClient.HostRead(status, EEndpoint2, cswBuf, KCswLength);
1.208 + User::WaitForRequest(status);
1.209 + test(KErrNone == status.Int());
1.210 +
1.211 + // Check dCSWTag
1.212 + TInt recvedCBWTag = extractInt(&cswBuf[4]);
1.213 + test(dCBWTag == recvedCBWTag);
1.214 +
1.215 + // Check bCSWStatus
1.216 + TInt bCSWStatus = cswBuf[KCswLength - 1];
1.217 + test.Printf(_L("CSW status: %d\n"), bCSWStatus);
1.218 + test(bCSWStatus == 0);
1.219 + }
1.220 +
1.221 +//////////////////////////////////////////////////////////////
1.222 +
1.223 +void
1.224 +TConnected::MoveTo(TInt aStateId) const
1.225 + {
1.226 + switch (aStateId)
1.227 + {
1.228 + case EUsbMsDriveState_Active:
1.229 + MoveToActive();
1.230 + break;
1.231 + default:
1.232 + test.Printf(_L("Cannot reach %d from %d\n"), GetStateId(), aStateId);
1.233 + test(EFalse);
1.234 + }
1.235 + }
1.236 +
1.237 +void
1.238 +TConnected::MoveToActive() const
1.239 + {
1.240 + test.Printf(_L("Moving to active state\n"));
1.241 +
1.242 + // send prevent medium removal using testld
1.243 + // 1: prevent medium removal
1.244 + BuildMediumRemoval(1);
1.245 + createCBW(cbwBuf, ++dCBWTag, 0, 0, scsiCmdBuf, testLun);
1.246 +
1.247 + if(!SendAndReceive())
1.248 + {
1.249 + // Prevent Media Removal command not supported
1.250 + test.Printf(_L("Prevent Media Removal command not supported, issuing read instead\n"));
1.251 +
1.252 + // Read 1k bytes using testldd.
1.253 + // 0x28: opcode for read (10); 10: starting sector; 2: total sectors
1.254 + BuildReadWrite(0x28, 10, 2);
1.255 + // 0x80: indicates host writing
1.256 + createCBW(cbwBuf, ++dCBWTag, KKiloBytes, 0x80, scsiCmdBuf, testLun);
1.257 +
1.258 + // Send read command
1.259 + test.Printf(_L("Sending CBW read cmd\n"));
1.260 + TRequestStatus status;
1.261 + usbcClient.HostWrite(status, EEndpoint1, cbwBuf, KCbwLength);
1.262 + User::WaitForRequest(status);
1.263 + test(KErrNone == status.Int());
1.264 +
1.265 + // Read actual data. We don't care the contents.
1.266 + TBuf8<KKiloBytes> readBuf;
1.267 + readBuf.SetLength(KKiloBytes);
1.268 + usbcClient.HostRead(status, EEndpoint2, readBuf, KKiloBytes);
1.269 + User::WaitForRequest(status);
1.270 + test(KErrNone == status.Int());
1.271 +
1.272 + // Check CSW status
1.273 + // Read CSW
1.274 + test.Printf(_L("Reading CSW\n"));
1.275 + usbcClient.HostRead(status, EEndpoint2, cswBuf, KCswLength);
1.276 + User::WaitForRequest(status);
1.277 + test(KErrNone == status.Int());
1.278 +
1.279 + // Check dCSWTag
1.280 + TInt recvedCBWTag = extractInt(&cswBuf[4]);
1.281 + test(dCBWTag == recvedCBWTag);
1.282 +
1.283 + // Check bCSWStatus
1.284 + TInt bCSWStatus = cswBuf[KCswLength - 1];
1.285 + test.Printf(_L("CSW status: %d\n"), bCSWStatus);
1.286 + test(bCSWStatus == 0);
1.287 + }
1.288 + }
1.289 +
1.290 +//////////////////////////////////////////////////////////////
1.291 +
1.292 +void
1.293 +TActive::MoveTo(TInt aStateId) const
1.294 + {
1.295 + switch (aStateId)
1.296 + {
1.297 + case EUsbMsDriveState_Locked:
1.298 + MoveToLocked();
1.299 + break;
1.300 + case EUsbMsDriveState_Disconnecting:
1.301 + MoveToDisconnecting();
1.302 + break;
1.303 + default:
1.304 + test.Printf(_L("Cannot reach %d from %d\n"), GetStateId(), aStateId);
1.305 + test(EFalse);
1.306 + }
1.307 + }
1.308 +
1.309 +void
1.310 +TActive::MoveToLocked() const
1.311 + {
1.312 + test.Printf(_L("Moving to locked state\n"));
1.313 + // To be implemented. Wait for lock defect fix
1.314 + }
1.315 +
1.316 +void
1.317 +TActive::MoveToDisconnecting() const
1.318 + {
1.319 + test.Printf(_L("Moving to disconnecting state\n"));
1.320 +
1.321 + // send allow medium removal using testld
1.322 + // 0: allow medium removal
1.323 + BuildMediumRemoval(0);
1.324 + createCBW(cbwBuf, ++dCBWTag, 0, 0, scsiCmdBuf, testLun);
1.325 + if(!SendAndReceive())
1.326 + test.Printf(_L("Prevent Media Removal command not supported, no need to allow\n"));
1.327 +
1.328 + // Now the state is connected, let's move to disconnecting state
1.329 + // by sending a stop unit command
1.330 +
1.331 + // 0: stop unit
1.332 + BuildStartStopUnit(0);
1.333 + createCBW(cbwBuf, ++dCBWTag, 0, 0, scsiCmdBuf, testLun);
1.334 + test(SendAndReceive());
1.335 + }
1.336 +
1.337 +//////////////////////////////////////////////////////////////
1.338 +
1.339 +void
1.340 +TLocked::MoveTo(TInt aStateId) const
1.341 + {
1.342 + switch (aStateId)
1.343 + {
1.344 + case EUsbMsDriveState_Disconnecting:
1.345 + MoveToDisconnecting();
1.346 + break;
1.347 + default:
1.348 + test.Printf(_L("Cannot reach %d from %d\n"), GetStateId(), aStateId);
1.349 + test(EFalse);
1.350 + }
1.351 + }
1.352 +
1.353 +void
1.354 +TLocked::MoveToDisconnecting() const
1.355 + {
1.356 + test.Printf(_L("Moving to disconnecting state\n"));
1.357 + // To be implemented once lock issue is resolved
1.358 + }
1.359 +
1.360 +//////////////////////////////////////////////////////////////
1.361 +
1.362 +void
1.363 +TDisconnecting::MoveTo(TInt aStateId) const
1.364 + {
1.365 + switch (aStateId)
1.366 + {
1.367 + case EUsbMsDriveState_Disconnected:
1.368 + MoveToDisconnected();
1.369 + break;
1.370 + default:
1.371 + test.Printf(_L("Cannot reach %d from %d\n"), GetStateId(), aStateId);
1.372 + test(EFalse);
1.373 + }
1.374 + }
1.375 +
1.376 +void
1.377 +TDisconnecting::MoveToDisconnected() const
1.378 + {
1.379 + test.Printf(_L("Moving to disconnected state\n"));
1.380 + UnmountMsFs(removalDrvNo);
1.381 + }
1.382 +
1.383 +//////////////////////////////////////////////////////////////
1.384 +
1.385 +void
1.386 +TWritten::MoveTo(TInt aStateId) const
1.387 + {
1.388 + switch (aStateId)
1.389 + {
1.390 + case EUsbMsState_Read:
1.391 + MoveToRead();
1.392 + break;
1.393 + default:
1.394 + test.Printf(_L("Cannot reach %d from %d\n"), GetStateId(), aStateId);
1.395 + test(EFalse);
1.396 + }
1.397 + }
1.398 +
1.399 +void
1.400 +TWritten::MoveToRead() const
1.401 + {
1.402 + test.Printf(_L("Moving to read state\n"));
1.403 +
1.404 + // Read 1k bytes using testldd.
1.405 + // 0x28: opcode for read (10); 10: starting sector; 2: total sectors
1.406 + BuildReadWrite(0x28, 10, 2);
1.407 + // 0x80: indicates host writing
1.408 + createCBW(cbwBuf, ++dCBWTag, KKiloBytes, 0x80, scsiCmdBuf, testLun);
1.409 +
1.410 + // Send read command
1.411 + test.Printf(_L("Sending CBW read cmd\n"));
1.412 + TRequestStatus status;
1.413 + usbcClient.HostWrite(status, EEndpoint1, cbwBuf, KCbwLength);
1.414 + User::WaitForRequest(status);
1.415 + test(KErrNone == status.Int());
1.416 +
1.417 + // Read actual data. We don't care the contents.
1.418 + TBuf8<KKiloBytes> readBuf;
1.419 + readBuf.SetLength(KKiloBytes);
1.420 + usbcClient.HostRead(status, EEndpoint2, readBuf, KKiloBytes);
1.421 + User::WaitForRequest(status);
1.422 + test(KErrNone == status.Int());
1.423 +
1.424 + // Check CSW status
1.425 + // Read CSW
1.426 + test.Printf(_L("Reading CSW\n"));
1.427 + usbcClient.HostRead(status, EEndpoint2, cswBuf, KCswLength);
1.428 + User::WaitForRequest(status);
1.429 + test(KErrNone == status.Int());
1.430 +
1.431 + // Check dCSWTag
1.432 + TInt recvedCBWTag = extractInt(&cswBuf[4]);
1.433 + test(dCBWTag == recvedCBWTag);
1.434 +
1.435 + // Check bCSWStatus
1.436 + TInt bCSWStatus = cswBuf[KCswLength - 1];
1.437 + test.Printf(_L("CSW status: %d\n"), bCSWStatus);
1.438 + test(bCSWStatus == 0);
1.439 + }
1.440 +
1.441 +//////////////////////////////////////////////////////////////
1.442 +
1.443 +void
1.444 +TRead::MoveTo(TInt aStateId) const
1.445 + {
1.446 + switch (aStateId)
1.447 + {
1.448 + case EUsbMsDriveState_Disconnected:
1.449 + MoveToDisconnected();
1.450 + break;
1.451 + default:
1.452 + test.Printf(_L("Cannot reach %d from %d\n"), GetStateId(), aStateId);
1.453 + test(EFalse);
1.454 + }
1.455 + }
1.456 +
1.457 +void
1.458 +TRead::MoveToDisconnected() const
1.459 + {
1.460 + test.Printf(_L("Moving to disconnected state\n"));
1.461 + UnmountMsFs(removalDrvNo);
1.462 + }
1.463 +