First public contribution.
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 MS drive state watcher
23 #include "t_ms_main.h"
24 #include "cpropertywatch.h"
25 #include "cstatemachine.h"
27 LOCAL_D TInt testedDriveIndex = -1;
29 ///////////////////////////////////////////////////////////////////////////
30 // class CPropertyWatch
31 ///////////////////////////////////////////////////////////////////////////
33 CPropertyWatch* CPropertyWatch::NewLC(TUsbMsDriveState_Subkey aSubkey, CPropertyHandler& aHandler)
35 CPropertyWatch* me=new(ELeave) CPropertyWatch(aHandler);
36 CleanupStack::PushL(me);
37 me->ConstructL(aSubkey);
41 CPropertyWatch::CPropertyWatch(CPropertyHandler& aHandler)
42 : CActive(0), iHandler(aHandler)
45 void CPropertyWatch::ConstructL(TUsbMsDriveState_Subkey aSubkey)
47 User::LeaveIfError(iProperty.Attach(KUsbMsDriveState_Category, aSubkey));
48 CActiveScheduler::Add(this);
49 // initial subscription and process current property value
53 CPropertyWatch::~CPropertyWatch()
59 void CPropertyWatch::DoCancel()
64 void CPropertyWatch::RunL()
66 // resubscribe before processing new value to prevent missing updates
67 iProperty.Subscribe(iStatus);
68 iHandler.HandleStatusChange(iProperty);
72 ///////////////////////////////////////////////////////////////////////////
73 // class CPropertyHandler
74 ///////////////////////////////////////////////////////////////////////////
76 CPropertyHandler::CPropertyHandler(TInt aDriveNo, CStateMachine& aSm)
82 CPropertyHandler::~CPropertyHandler()
86 ///////////////////////////////////////////////////////////////////////////
87 // class CMsDriveStatusHandler
88 ///////////////////////////////////////////////////////////////////////////
90 CMsDriveStatusHandler*
91 CMsDriveStatusHandler::NewLC(TInt aDriveNo, CStateMachine& aSm)
93 CMsDriveStatusHandler* self = new (ELeave) CMsDriveStatusHandler(aDriveNo, aSm);
94 CleanupStack::PushL(self);
98 CMsDriveStatusHandler::CMsDriveStatusHandler(TInt aDriveNo, CStateMachine& aSm)
99 : CPropertyHandler(aDriveNo, aSm)
104 CMsDriveStatusHandler::HandleStatusChange(RProperty& aProperty)
106 TInt driveStatus = -1;
107 TInt currentStatus = iStateMachine.CurrentStateId();
108 TBuf8<16> allDrivesStatus;
109 TInt ret = aProperty.Get(allDrivesStatus);
110 test.Printf(_L("Property.Get() return value: %d\n"), ret);
111 test(ret == KErrNone);
113 if (testedDriveIndex < 0)
115 for(TInt i=0; i<allDrivesStatus.Length()/2; i++)
117 if (allDrivesStatus[2*i] == iDriveNo)
119 testedDriveIndex = i;
125 if (testedDriveIndex < 0)
127 test.Printf(_L("Tested drive %d not found\n"), iDriveNo);
131 driveStatus = allDrivesStatus[2*testedDriveIndex + 1];
133 switch (currentStatus)
135 case EUsbMsDriveState_Disconnected:
136 if (iStateMachine.FromStateId() == EUsbMsDriveState_Disconnected)
138 iStateMachine.MoveTo(EUsbMsDriveState_Connected);
139 iStateMachine.SetFromStateId(EUsbMsDriveState_Disconnected);
141 else if (iStateMachine.FromStateId() == EUsbMsDriveState_Disconnecting)
143 test(driveStatus == currentStatus);
144 iStateMachine.MoveTo(EUsbMsDriveState_Connecting);
145 iStateMachine.SetFromStateId(EUsbMsDriveState_Disconnected);
147 else if (iStateMachine.FromStateId() == EUsbMsState_Read)
149 CActiveScheduler::Stop();
152 case EUsbMsDriveState_Connecting:
153 if (iStateMachine.FromStateId() == EUsbMsDriveState_Disconnected)
155 iStateMachine.MoveTo(EUsbMsState_Written);
156 iStateMachine.SetFromStateId(EUsbMsDriveState_Connecting);
159 case EUsbMsDriveState_Connected:
160 if (iStateMachine.FromStateId() == EUsbMsDriveState_Disconnected)
162 test(driveStatus == currentStatus);
163 iStateMachine.MoveTo(EUsbMsDriveState_Active);
164 iStateMachine.SetFromStateId(EUsbMsDriveState_Connected);
167 case EUsbMsDriveState_Disconnecting:
168 if (iStateMachine.FromStateId() == EUsbMsDriveState_Active)
170 test(driveStatus == currentStatus);
171 iStateMachine.MoveTo(EUsbMsDriveState_Disconnected);
172 iStateMachine.SetFromStateId(EUsbMsDriveState_Disconnecting);
176 case EUsbMsDriveState_Active:
177 if (iStateMachine.FromStateId() == EUsbMsDriveState_Connected)
179 test(driveStatus == currentStatus);
180 iStateMachine.MoveTo(EUsbMsDriveState_Disconnecting);
181 iStateMachine.SetFromStateId(EUsbMsDriveState_Active);
184 case EUsbMsDriveState_Locked:
185 iStateMachine.MoveTo(EUsbMsDriveState_Disconnecting);
188 test.Printf(_L("uninteresting drive status: %d\n"), currentStatus);
193 ///////////////////////////////////////////////////////////////////////////
194 // class CMsReadStatusHandler
195 ///////////////////////////////////////////////////////////////////////////
197 CMsReadStatusHandler*
198 CMsReadStatusHandler::NewLC(TInt aDriveNo, CStateMachine& aSm)
200 CMsReadStatusHandler* self = new (ELeave) CMsReadStatusHandler(aDriveNo, aSm);
201 CleanupStack::PushL(self);
205 CMsReadStatusHandler::CMsReadStatusHandler(TInt aDriveNo, CStateMachine& aSm)
206 : CPropertyHandler(aDriveNo, aSm)
211 CMsReadStatusHandler::HandleStatusChange(RProperty& aProperty)
213 TInt currentStatus = iStateMachine.CurrentStateId();
214 TUsbMsBytesTransferred kBytes;
216 TInt ret = aProperty.Get(kBytes);
217 test.Printf(_L("Read Property.Get() return value: %d\n"), ret);
218 test(ret == KErrNone);
220 switch (currentStatus)
222 case EUsbMsState_Read:
223 if (iStateMachine.FromStateId() == EUsbMsState_Written
224 && (1 == kBytes[testedDriveIndex]))
226 iStateMachine.MoveTo(EUsbMsDriveState_Disconnected);
227 iStateMachine.SetFromStateId(EUsbMsState_Read);
230 case EUsbMsDriveState_Active:
231 if (iStateMachine.FromStateId() == EUsbMsDriveState_Connected)
233 iStateMachine.MoveTo(EUsbMsDriveState_Disconnecting);
234 iStateMachine.SetFromStateId(EUsbMsDriveState_Active);
238 test.Printf(_L("uninteresting read status: %d\n"), currentStatus);
243 ///////////////////////////////////////////////////////////////////////////
244 // class CMsWrittenStatusHandler
245 ///////////////////////////////////////////////////////////////////////////
247 CMsWrittenStatusHandler*
248 CMsWrittenStatusHandler::NewLC(TInt aDriveNo, CStateMachine& aSm)
250 CMsWrittenStatusHandler* self = new (ELeave) CMsWrittenStatusHandler(aDriveNo, aSm);
251 CleanupStack::PushL(self);
255 CMsWrittenStatusHandler::CMsWrittenStatusHandler(TInt aDriveNo, CStateMachine& aSm)
256 : CPropertyHandler(aDriveNo, aSm)
261 CMsWrittenStatusHandler::HandleStatusChange(RProperty& aProperty)
263 TInt currentStatus = iStateMachine.CurrentStateId();
264 TUsbMsBytesTransferred kBytes;
266 TInt ret = aProperty.Get(kBytes);
267 test.Printf(_L("Written Property.Get() return value: %d\n"), ret);
268 test(ret == KErrNone);
269 test.Printf(_L("Kilobytes written: %d\n"), kBytes[testedDriveIndex]);
271 switch (currentStatus)
273 case EUsbMsState_Written:
274 if (iStateMachine.FromStateId() == EUsbMsDriveState_Connecting &&
275 kBytes[testedDriveIndex] == 1)
277 iStateMachine.MoveTo(EUsbMsState_Read);
278 iStateMachine.SetFromStateId(EUsbMsState_Written);
282 test.Printf(_L("uninteresting write status: %d\n"), currentStatus);