os/kernelhwsrv/kerneltest/f32test/smassstorage/src/cpropertywatch.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 MS drive state watcher
    15 // 
    16 //
    17 
    18 /**
    19  @file
    20  @internalTechnology
    21 */
    22 
    23 #include "t_ms_main.h"
    24 #include "cpropertywatch.h"
    25 #include "cstatemachine.h"
    26 
    27 LOCAL_D TInt testedDriveIndex = -1;
    28  
    29 ///////////////////////////////////////////////////////////////////////////
    30 //	class CPropertyWatch
    31 ///////////////////////////////////////////////////////////////////////////
    32 
    33 CPropertyWatch* CPropertyWatch::NewLC(TUsbMsDriveState_Subkey aSubkey, CPropertyHandler& aHandler)
    34 	{
    35 	CPropertyWatch* me=new(ELeave) CPropertyWatch(aHandler);
    36 	CleanupStack::PushL(me);
    37 	me->ConstructL(aSubkey);
    38 	return me;
    39 	}
    40 
    41 CPropertyWatch::CPropertyWatch(CPropertyHandler& aHandler)
    42 	: CActive(0), iHandler(aHandler)
    43 	{}
    44 
    45 void CPropertyWatch::ConstructL(TUsbMsDriveState_Subkey aSubkey)
    46 	{
    47 	User::LeaveIfError(iProperty.Attach(KUsbMsDriveState_Category, aSubkey));
    48 	CActiveScheduler::Add(this);
    49 	// initial subscription and process current property value
    50 	RunL();
    51 	}
    52 
    53 CPropertyWatch::~CPropertyWatch()
    54 	{
    55 	Cancel();
    56 	iProperty.Close();
    57 	}
    58 
    59 void CPropertyWatch::DoCancel()
    60 	{
    61 	iProperty.Cancel();
    62 	}
    63 
    64 void CPropertyWatch::RunL()
    65 	{
    66 	// resubscribe before processing new value to prevent missing updates
    67 	iProperty.Subscribe(iStatus);
    68 	iHandler.HandleStatusChange(iProperty);
    69 	SetActive();
    70 	}
    71 
    72 ///////////////////////////////////////////////////////////////////////////
    73 //	class CPropertyHandler
    74 ///////////////////////////////////////////////////////////////////////////
    75 
    76 CPropertyHandler::CPropertyHandler(TInt aDriveNo, CStateMachine& aSm)
    77  	: iDriveNo(aDriveNo)
    78  	, iStateMachine(aSm)
    79 	{
    80 	}
    81 	
    82 CPropertyHandler::~CPropertyHandler()
    83 	{
    84 	}
    85 	
    86 ///////////////////////////////////////////////////////////////////////////
    87 //	class CMsDriveStatusHandler
    88 ///////////////////////////////////////////////////////////////////////////
    89 
    90 CMsDriveStatusHandler*
    91 CMsDriveStatusHandler::NewLC(TInt aDriveNo, CStateMachine& aSm)
    92 	{
    93 	CMsDriveStatusHandler* self = new (ELeave) CMsDriveStatusHandler(aDriveNo, aSm);
    94 	CleanupStack::PushL(self);
    95 	return self;
    96 	};
    97 
    98 CMsDriveStatusHandler::CMsDriveStatusHandler(TInt aDriveNo, CStateMachine& aSm)
    99 	: CPropertyHandler(aDriveNo, aSm)
   100 	{
   101 	}
   102 	
   103 void
   104 CMsDriveStatusHandler::HandleStatusChange(RProperty& aProperty)
   105 	{
   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);
   112 
   113 	if (testedDriveIndex < 0)
   114 		{
   115 		for(TInt i=0; i<allDrivesStatus.Length()/2; i++)
   116 			{
   117 			if (allDrivesStatus[2*i] == iDriveNo)
   118 				{
   119 				testedDriveIndex = i;
   120 				break;
   121 				}
   122 			}
   123 		}
   124 			
   125 	if (testedDriveIndex < 0)
   126 		{
   127 		test.Printf(_L("Tested drive %d not found\n"), iDriveNo);
   128 		test(EFalse);
   129 		}
   130 
   131 	driveStatus = allDrivesStatus[2*testedDriveIndex + 1]; 
   132 		
   133     switch (currentStatus)
   134     	{
   135     	case EUsbMsDriveState_Disconnected:
   136     		if (iStateMachine.FromStateId() == EUsbMsDriveState_Disconnected)
   137     			{
   138     			iStateMachine.MoveTo(EUsbMsDriveState_Connected);
   139     			iStateMachine.SetFromStateId(EUsbMsDriveState_Disconnected);
   140     			}
   141     		else if (iStateMachine.FromStateId() == EUsbMsDriveState_Disconnecting)
   142     			{
   143     			test(driveStatus == currentStatus);
   144     			iStateMachine.MoveTo(EUsbMsDriveState_Connecting);
   145     			iStateMachine.SetFromStateId(EUsbMsDriveState_Disconnected);
   146     			}
   147     		else if (iStateMachine.FromStateId() == EUsbMsState_Read)
   148     			{
   149     			CActiveScheduler::Stop();
   150     			}
   151     		break;
   152     	case EUsbMsDriveState_Connecting:
   153     		if (iStateMachine.FromStateId() == EUsbMsDriveState_Disconnected)
   154     			{
   155     			iStateMachine.MoveTo(EUsbMsState_Written);
   156     			iStateMachine.SetFromStateId(EUsbMsDriveState_Connecting);
   157     			}
   158     		break;
   159     	case EUsbMsDriveState_Connected:
   160     		if (iStateMachine.FromStateId() == EUsbMsDriveState_Disconnected)
   161     			{
   162     			test(driveStatus == currentStatus);
   163     			iStateMachine.MoveTo(EUsbMsDriveState_Active);
   164     			iStateMachine.SetFromStateId(EUsbMsDriveState_Connected);
   165     			}
   166     		break;
   167     	case EUsbMsDriveState_Disconnecting:
   168     		if (iStateMachine.FromStateId() == EUsbMsDriveState_Active)
   169     			{
   170     			test(driveStatus == currentStatus);
   171     			iStateMachine.MoveTo(EUsbMsDriveState_Disconnected);
   172     			iStateMachine.SetFromStateId(EUsbMsDriveState_Disconnecting);
   173     			}
   174     		
   175     		break;
   176     	case EUsbMsDriveState_Active:
   177     		if (iStateMachine.FromStateId() == EUsbMsDriveState_Connected)
   178     			{
   179     			test(driveStatus == currentStatus);
   180     			iStateMachine.MoveTo(EUsbMsDriveState_Disconnecting);
   181     			iStateMachine.SetFromStateId(EUsbMsDriveState_Active);
   182     			}
   183     		break;
   184     	case EUsbMsDriveState_Locked:
   185     		iStateMachine.MoveTo(EUsbMsDriveState_Disconnecting);
   186     		break;
   187     	default:
   188     		test.Printf(_L("uninteresting drive status: %d\n"), currentStatus);
   189     		break;
   190     	}
   191 	}
   192 	
   193 ///////////////////////////////////////////////////////////////////////////
   194 //	class CMsReadStatusHandler
   195 ///////////////////////////////////////////////////////////////////////////
   196 
   197 CMsReadStatusHandler*
   198 CMsReadStatusHandler::NewLC(TInt aDriveNo, CStateMachine& aSm)
   199 	{
   200 	CMsReadStatusHandler* self = new (ELeave) CMsReadStatusHandler(aDriveNo, aSm);
   201 	CleanupStack::PushL(self);
   202 	return self;
   203 	};
   204 	
   205 CMsReadStatusHandler::CMsReadStatusHandler(TInt aDriveNo, CStateMachine& aSm)
   206 	: CPropertyHandler(aDriveNo, aSm)
   207 	{
   208 	}
   209 	
   210 void
   211 CMsReadStatusHandler::HandleStatusChange(RProperty& aProperty)
   212 	{
   213     TInt currentStatus = iStateMachine.CurrentStateId();
   214 	TUsbMsBytesTransferred kBytes;
   215 	
   216 	TInt ret = aProperty.Get(kBytes);
   217 	test.Printf(_L("Read Property.Get() return value: %d\n"), ret); 
   218 	test(ret == KErrNone);
   219 	
   220     switch (currentStatus)
   221     	{
   222     	case EUsbMsState_Read:
   223     	    if (iStateMachine.FromStateId() == EUsbMsState_Written
   224     	    	&& (1 == kBytes[testedDriveIndex]))
   225     			{
   226     			iStateMachine.MoveTo(EUsbMsDriveState_Disconnected);
   227     			iStateMachine.SetFromStateId(EUsbMsState_Read);
   228     			}
   229     		break;
   230 		case EUsbMsDriveState_Active:
   231     		if (iStateMachine.FromStateId() == EUsbMsDriveState_Connected)
   232     			{
   233     			iStateMachine.MoveTo(EUsbMsDriveState_Disconnecting);
   234     			iStateMachine.SetFromStateId(EUsbMsDriveState_Active);
   235     			}
   236     		break;
   237     	default:
   238     		test.Printf(_L("uninteresting read status: %d\n"), currentStatus);
   239     		break;
   240     	}
   241 	}
   242 
   243 ///////////////////////////////////////////////////////////////////////////
   244 //	class CMsWrittenStatusHandler
   245 ///////////////////////////////////////////////////////////////////////////
   246 
   247 CMsWrittenStatusHandler*
   248 CMsWrittenStatusHandler::NewLC(TInt aDriveNo, CStateMachine& aSm)
   249 	{
   250 	CMsWrittenStatusHandler* self = new (ELeave) CMsWrittenStatusHandler(aDriveNo, aSm);
   251 	CleanupStack::PushL(self);
   252 	return self;
   253 	};
   254 
   255 CMsWrittenStatusHandler::CMsWrittenStatusHandler(TInt aDriveNo, CStateMachine& aSm)
   256 	: CPropertyHandler(aDriveNo, aSm)
   257 	{
   258 	}
   259 	
   260 void
   261 CMsWrittenStatusHandler::HandleStatusChange(RProperty& aProperty)
   262 	{
   263     TInt currentStatus = iStateMachine.CurrentStateId();
   264 	TUsbMsBytesTransferred kBytes;
   265 	
   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]);
   270 	
   271     switch (currentStatus)
   272     	{
   273     	case EUsbMsState_Written:
   274     		if (iStateMachine.FromStateId() == EUsbMsDriveState_Connecting &&
   275 				kBytes[testedDriveIndex] == 1)
   276 				{				
   277     			iStateMachine.MoveTo(EUsbMsState_Read);
   278     			iStateMachine.SetFromStateId(EUsbMsState_Written);
   279     			}
   280     		break;
   281     	default:
   282     		test.Printf(_L("uninteresting write status: %d\n"), currentStatus);
   283     		break;
   284     	}
   285 	}