1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/os/kernelhwsrv/kerneltest/f32test/smassstorage/src/cpropertywatch.cpp Fri Jun 15 03:10:57 2012 +0200
1.3 @@ -0,0 +1,285 @@
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 MS drive state watcher
1.18 +//
1.19 +//
1.20 +
1.21 +/**
1.22 + @file
1.23 + @internalTechnology
1.24 +*/
1.25 +
1.26 +#include "t_ms_main.h"
1.27 +#include "cpropertywatch.h"
1.28 +#include "cstatemachine.h"
1.29 +
1.30 +LOCAL_D TInt testedDriveIndex = -1;
1.31 +
1.32 +///////////////////////////////////////////////////////////////////////////
1.33 +// class CPropertyWatch
1.34 +///////////////////////////////////////////////////////////////////////////
1.35 +
1.36 +CPropertyWatch* CPropertyWatch::NewLC(TUsbMsDriveState_Subkey aSubkey, CPropertyHandler& aHandler)
1.37 + {
1.38 + CPropertyWatch* me=new(ELeave) CPropertyWatch(aHandler);
1.39 + CleanupStack::PushL(me);
1.40 + me->ConstructL(aSubkey);
1.41 + return me;
1.42 + }
1.43 +
1.44 +CPropertyWatch::CPropertyWatch(CPropertyHandler& aHandler)
1.45 + : CActive(0), iHandler(aHandler)
1.46 + {}
1.47 +
1.48 +void CPropertyWatch::ConstructL(TUsbMsDriveState_Subkey aSubkey)
1.49 + {
1.50 + User::LeaveIfError(iProperty.Attach(KUsbMsDriveState_Category, aSubkey));
1.51 + CActiveScheduler::Add(this);
1.52 + // initial subscription and process current property value
1.53 + RunL();
1.54 + }
1.55 +
1.56 +CPropertyWatch::~CPropertyWatch()
1.57 + {
1.58 + Cancel();
1.59 + iProperty.Close();
1.60 + }
1.61 +
1.62 +void CPropertyWatch::DoCancel()
1.63 + {
1.64 + iProperty.Cancel();
1.65 + }
1.66 +
1.67 +void CPropertyWatch::RunL()
1.68 + {
1.69 + // resubscribe before processing new value to prevent missing updates
1.70 + iProperty.Subscribe(iStatus);
1.71 + iHandler.HandleStatusChange(iProperty);
1.72 + SetActive();
1.73 + }
1.74 +
1.75 +///////////////////////////////////////////////////////////////////////////
1.76 +// class CPropertyHandler
1.77 +///////////////////////////////////////////////////////////////////////////
1.78 +
1.79 +CPropertyHandler::CPropertyHandler(TInt aDriveNo, CStateMachine& aSm)
1.80 + : iDriveNo(aDriveNo)
1.81 + , iStateMachine(aSm)
1.82 + {
1.83 + }
1.84 +
1.85 +CPropertyHandler::~CPropertyHandler()
1.86 + {
1.87 + }
1.88 +
1.89 +///////////////////////////////////////////////////////////////////////////
1.90 +// class CMsDriveStatusHandler
1.91 +///////////////////////////////////////////////////////////////////////////
1.92 +
1.93 +CMsDriveStatusHandler*
1.94 +CMsDriveStatusHandler::NewLC(TInt aDriveNo, CStateMachine& aSm)
1.95 + {
1.96 + CMsDriveStatusHandler* self = new (ELeave) CMsDriveStatusHandler(aDriveNo, aSm);
1.97 + CleanupStack::PushL(self);
1.98 + return self;
1.99 + };
1.100 +
1.101 +CMsDriveStatusHandler::CMsDriveStatusHandler(TInt aDriveNo, CStateMachine& aSm)
1.102 + : CPropertyHandler(aDriveNo, aSm)
1.103 + {
1.104 + }
1.105 +
1.106 +void
1.107 +CMsDriveStatusHandler::HandleStatusChange(RProperty& aProperty)
1.108 + {
1.109 + TInt driveStatus = -1;
1.110 + TInt currentStatus = iStateMachine.CurrentStateId();
1.111 + TBuf8<16> allDrivesStatus;
1.112 + TInt ret = aProperty.Get(allDrivesStatus);
1.113 + test.Printf(_L("Property.Get() return value: %d\n"), ret);
1.114 + test(ret == KErrNone);
1.115 +
1.116 + if (testedDriveIndex < 0)
1.117 + {
1.118 + for(TInt i=0; i<allDrivesStatus.Length()/2; i++)
1.119 + {
1.120 + if (allDrivesStatus[2*i] == iDriveNo)
1.121 + {
1.122 + testedDriveIndex = i;
1.123 + break;
1.124 + }
1.125 + }
1.126 + }
1.127 +
1.128 + if (testedDriveIndex < 0)
1.129 + {
1.130 + test.Printf(_L("Tested drive %d not found\n"), iDriveNo);
1.131 + test(EFalse);
1.132 + }
1.133 +
1.134 + driveStatus = allDrivesStatus[2*testedDriveIndex + 1];
1.135 +
1.136 + switch (currentStatus)
1.137 + {
1.138 + case EUsbMsDriveState_Disconnected:
1.139 + if (iStateMachine.FromStateId() == EUsbMsDriveState_Disconnected)
1.140 + {
1.141 + iStateMachine.MoveTo(EUsbMsDriveState_Connected);
1.142 + iStateMachine.SetFromStateId(EUsbMsDriveState_Disconnected);
1.143 + }
1.144 + else if (iStateMachine.FromStateId() == EUsbMsDriveState_Disconnecting)
1.145 + {
1.146 + test(driveStatus == currentStatus);
1.147 + iStateMachine.MoveTo(EUsbMsDriveState_Connecting);
1.148 + iStateMachine.SetFromStateId(EUsbMsDriveState_Disconnected);
1.149 + }
1.150 + else if (iStateMachine.FromStateId() == EUsbMsState_Read)
1.151 + {
1.152 + CActiveScheduler::Stop();
1.153 + }
1.154 + break;
1.155 + case EUsbMsDriveState_Connecting:
1.156 + if (iStateMachine.FromStateId() == EUsbMsDriveState_Disconnected)
1.157 + {
1.158 + iStateMachine.MoveTo(EUsbMsState_Written);
1.159 + iStateMachine.SetFromStateId(EUsbMsDriveState_Connecting);
1.160 + }
1.161 + break;
1.162 + case EUsbMsDriveState_Connected:
1.163 + if (iStateMachine.FromStateId() == EUsbMsDriveState_Disconnected)
1.164 + {
1.165 + test(driveStatus == currentStatus);
1.166 + iStateMachine.MoveTo(EUsbMsDriveState_Active);
1.167 + iStateMachine.SetFromStateId(EUsbMsDriveState_Connected);
1.168 + }
1.169 + break;
1.170 + case EUsbMsDriveState_Disconnecting:
1.171 + if (iStateMachine.FromStateId() == EUsbMsDriveState_Active)
1.172 + {
1.173 + test(driveStatus == currentStatus);
1.174 + iStateMachine.MoveTo(EUsbMsDriveState_Disconnected);
1.175 + iStateMachine.SetFromStateId(EUsbMsDriveState_Disconnecting);
1.176 + }
1.177 +
1.178 + break;
1.179 + case EUsbMsDriveState_Active:
1.180 + if (iStateMachine.FromStateId() == EUsbMsDriveState_Connected)
1.181 + {
1.182 + test(driveStatus == currentStatus);
1.183 + iStateMachine.MoveTo(EUsbMsDriveState_Disconnecting);
1.184 + iStateMachine.SetFromStateId(EUsbMsDriveState_Active);
1.185 + }
1.186 + break;
1.187 + case EUsbMsDriveState_Locked:
1.188 + iStateMachine.MoveTo(EUsbMsDriveState_Disconnecting);
1.189 + break;
1.190 + default:
1.191 + test.Printf(_L("uninteresting drive status: %d\n"), currentStatus);
1.192 + break;
1.193 + }
1.194 + }
1.195 +
1.196 +///////////////////////////////////////////////////////////////////////////
1.197 +// class CMsReadStatusHandler
1.198 +///////////////////////////////////////////////////////////////////////////
1.199 +
1.200 +CMsReadStatusHandler*
1.201 +CMsReadStatusHandler::NewLC(TInt aDriveNo, CStateMachine& aSm)
1.202 + {
1.203 + CMsReadStatusHandler* self = new (ELeave) CMsReadStatusHandler(aDriveNo, aSm);
1.204 + CleanupStack::PushL(self);
1.205 + return self;
1.206 + };
1.207 +
1.208 +CMsReadStatusHandler::CMsReadStatusHandler(TInt aDriveNo, CStateMachine& aSm)
1.209 + : CPropertyHandler(aDriveNo, aSm)
1.210 + {
1.211 + }
1.212 +
1.213 +void
1.214 +CMsReadStatusHandler::HandleStatusChange(RProperty& aProperty)
1.215 + {
1.216 + TInt currentStatus = iStateMachine.CurrentStateId();
1.217 + TUsbMsBytesTransferred kBytes;
1.218 +
1.219 + TInt ret = aProperty.Get(kBytes);
1.220 + test.Printf(_L("Read Property.Get() return value: %d\n"), ret);
1.221 + test(ret == KErrNone);
1.222 +
1.223 + switch (currentStatus)
1.224 + {
1.225 + case EUsbMsState_Read:
1.226 + if (iStateMachine.FromStateId() == EUsbMsState_Written
1.227 + && (1 == kBytes[testedDriveIndex]))
1.228 + {
1.229 + iStateMachine.MoveTo(EUsbMsDriveState_Disconnected);
1.230 + iStateMachine.SetFromStateId(EUsbMsState_Read);
1.231 + }
1.232 + break;
1.233 + case EUsbMsDriveState_Active:
1.234 + if (iStateMachine.FromStateId() == EUsbMsDriveState_Connected)
1.235 + {
1.236 + iStateMachine.MoveTo(EUsbMsDriveState_Disconnecting);
1.237 + iStateMachine.SetFromStateId(EUsbMsDriveState_Active);
1.238 + }
1.239 + break;
1.240 + default:
1.241 + test.Printf(_L("uninteresting read status: %d\n"), currentStatus);
1.242 + break;
1.243 + }
1.244 + }
1.245 +
1.246 +///////////////////////////////////////////////////////////////////////////
1.247 +// class CMsWrittenStatusHandler
1.248 +///////////////////////////////////////////////////////////////////////////
1.249 +
1.250 +CMsWrittenStatusHandler*
1.251 +CMsWrittenStatusHandler::NewLC(TInt aDriveNo, CStateMachine& aSm)
1.252 + {
1.253 + CMsWrittenStatusHandler* self = new (ELeave) CMsWrittenStatusHandler(aDriveNo, aSm);
1.254 + CleanupStack::PushL(self);
1.255 + return self;
1.256 + };
1.257 +
1.258 +CMsWrittenStatusHandler::CMsWrittenStatusHandler(TInt aDriveNo, CStateMachine& aSm)
1.259 + : CPropertyHandler(aDriveNo, aSm)
1.260 + {
1.261 + }
1.262 +
1.263 +void
1.264 +CMsWrittenStatusHandler::HandleStatusChange(RProperty& aProperty)
1.265 + {
1.266 + TInt currentStatus = iStateMachine.CurrentStateId();
1.267 + TUsbMsBytesTransferred kBytes;
1.268 +
1.269 + TInt ret = aProperty.Get(kBytes);
1.270 + test.Printf(_L("Written Property.Get() return value: %d\n"), ret);
1.271 + test(ret == KErrNone);
1.272 + test.Printf(_L("Kilobytes written: %d\n"), kBytes[testedDriveIndex]);
1.273 +
1.274 + switch (currentStatus)
1.275 + {
1.276 + case EUsbMsState_Written:
1.277 + if (iStateMachine.FromStateId() == EUsbMsDriveState_Connecting &&
1.278 + kBytes[testedDriveIndex] == 1)
1.279 + {
1.280 + iStateMachine.MoveTo(EUsbMsState_Read);
1.281 + iStateMachine.SetFromStateId(EUsbMsState_Written);
1.282 + }
1.283 + break;
1.284 + default:
1.285 + test.Printf(_L("uninteresting write status: %d\n"), currentStatus);
1.286 + break;
1.287 + }
1.288 + }