sysstatemgmt/systemstatemgr/cmd/src/cmdpoweroff.cpp
changeset 0 4e1aa6a622a0
equal deleted inserted replaced
-1:000000000000 0:4e1aa6a622a0
       
     1 // Copyright (c) 2007-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 "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 //
       
    15 
       
    16 #include <s32strm.h>
       
    17 #include <barsread2.h>
       
    18 #include <e32power.h>
       
    19 
       
    20 #include "cmdpoweroff.h"
       
    21 #include "ssmdebug.h"
       
    22 #include "ssmpanic.h"
       
    23 #include "ssmcommandparameters.h"
       
    24 
       
    25 /**
       
    26 Used to create an instance of CCmdPowerOff class from a read stream.
       
    27 CSsmCommandList::InternalizeL() uses this method to construct a command from stream.
       
    28 
       
    29 @param aReadStream Read stream containing data through which object can be created
       
    30 @return	A pointer to an object of type CCmdPowerOff.
       
    31 */
       
    32 CCmdPowerOff* CCmdPowerOff::NewL(RReadStream& aReadStream)
       
    33 	{
       
    34 	CCmdPowerOff* self = new (ELeave) CCmdPowerOff();
       
    35 	CleanupStack::PushL(self);
       
    36 	self->ConstructL(aReadStream);
       
    37 	CleanupStack::Pop(self);
       
    38 	return self;
       
    39 	}
       
    40 
       
    41 /**
       
    42 Used to create an instance of CCmdPowerOff class from given parameters.
       
    43 This method is used by SsmCommandFactory to create a command.
       
    44 
       
    45 @param aSeverity The severity of the command
       
    46 @param aState The power state to transition into
       
    47 @return	A pointer to an object of type CCmdPowerOff.
       
    48 */
       
    49 CCmdPowerOff* CCmdPowerOff::NewL(TCmdErrorSeverity aSeverity, TPowerState aState)
       
    50 	{
       
    51 	CCmdPowerOff* self = new (ELeave) CCmdPowerOff(aSeverity, aState);
       
    52 	CleanupStack::PushL(self);
       
    53 	self->ConstructL();
       
    54 	CleanupStack::Pop(self);
       
    55 	return self;
       
    56 	}
       
    57 
       
    58 /**
       
    59 Used to create an instance of CCmdPowerOff class from resource.
       
    60 
       
    61 @param aCommandParameters Object data from a resource file
       
    62 @return	A pointer to an object of type CCmdPowerOff.
       
    63 */
       
    64 CCmdPowerOff* CCmdPowerOff::NewL(TSsmCommandParameters& aCommandParameters)
       
    65 	{
       
    66 	CCmdPowerOff* self = new (ELeave) CCmdPowerOff();
       
    67 	CleanupStack::PushL(self);
       
    68 	self->ConstructL(aCommandParameters);
       
    69 	CleanupStack::Pop(self);
       
    70 	return self;
       
    71 	}
       
    72 /**
       
    73 Used to create an instance of CCmdPowerOff class from CCmdPowerOff object
       
    74 Must be used only by CLE
       
    75 @param aCmdPowerOff CCmdPowerOff reference 
       
    76 @return A pointer to an object of type CCmdPowerOff.
       
    77 */
       
    78 CCmdPowerOff* CCmdPowerOff::NewLC(const CCmdPowerOff& aCmdPowerOff)
       
    79     {
       
    80     CCmdPowerOff* self = new (ELeave) CCmdPowerOff(aCmdPowerOff.Severity(),aCmdPowerOff.PowerStateInfo());
       
    81     CleanupStack::PushL(self);
       
    82     self->ConstructL(aCmdPowerOff);
       
    83     return self;
       
    84     }
       
    85 
       
    86 void CCmdPowerOff::ConstructL(const CCmdPowerOff& aCmdPowerOff)
       
    87     {
       
    88     iConditionalResourceId = aCmdPowerOff.ConditionalInformation();
       
    89 #ifdef SYMBIAN_SSM_FLEXIBLE_MERGE
       
    90 	if (iConditionalResourceId != 0)
       
    91         {
       
    92         SetCommandResourceFileNameL(aCmdPowerOff.GetCommandResourceFileName());
       
    93         } 
       
    94 #endif 
       
    95     }
       
    96 
       
    97 /**
       
    98 Destructor
       
    99 */	
       
   100 CCmdPowerOff::~CCmdPowerOff()
       
   101 	{
       
   102 	Cancel();
       
   103 	}
       
   104 
       
   105 /**
       
   106 Initialises the BIC's active object and initiates the command
       
   107  
       
   108 @param aStatus 	the TRequestStatus of the active object calling this BIC
       
   109 */	
       
   110 void CCmdPowerOff::Execute(TRequestStatus& aStatus)
       
   111 	{
       
   112 	DEBUGVERBOSE1A("CCmdPowerOff::Execute");
       
   113 	aStatus = KRequestPending;
       
   114 	iExecuteRequest = &aStatus;
       
   115 
       
   116 	// power down and wait for a wakeup event
       
   117 	if (iState == EPwStandby)
       
   118 		{
       
   119 #ifdef TEST_SSM_GRACEFUL_SHUTDOWN
       
   120 		TInt testErr = RProperty::Set(iTestCategory, iTestKey, iState);
       
   121 		DEBUGPRINT3A("Testing TPowerState::EPwStandby(%d), Published with error(%d)", iState, testErr);
       
   122 		CompleteRequest(iStatus, KErrNone);	// For Testing complete the shutdown.
       
   123 #else
       
   124 		Power::RequestWakeupEventNotification(iStatus);
       
   125 		TInt err = Power::EnableWakeupEvents(iState);
       
   126 		if (err == KErrNone)
       
   127 			{
       
   128 			err = Power::PowerDown();
       
   129 
       
   130 			if (err != KErrNone)
       
   131 				{
       
   132 				Power::CancelWakeupEventNotification();
       
   133 				}
       
   134 			}
       
   135 #endif	//TEST_SSM_GRACEFUL_SHUTDOWN
       
   136 		}
       
   137 	else
       
   138 		{
       
   139 		CompleteRequest(iStatus, KErrNone);
       
   140 		}
       
   141 
       
   142 	SetActive();
       
   143 	}
       
   144 
       
   145 /**
       
   146 Releases resources associated with this BIC
       
   147 */	
       
   148 void CCmdPowerOff::Release()
       
   149 	{
       
   150 	delete this;
       
   151 	}
       
   152 
       
   153 /**
       
   154 Initiates a Cancel on the object. 
       
   155 */	
       
   156 void CCmdPowerOff::ExecuteCancel()
       
   157 	{
       
   158 	Cancel();
       
   159 	CompleteExecuteRequest(KErrCancel);
       
   160 	}
       
   161 
       
   162 /**
       
   163 Returns the Type of the BIC
       
   164 @return	The Type of the BIC
       
   165 */	
       
   166 TSsmCommandType CCmdPowerOff::Type() const
       
   167 	{
       
   168 	return (ESsmCmdPowerOff);
       
   169 	}
       
   170 
       
   171 /**
       
   172 Returns the Version of the BIC
       
   173 @return	The maximum supported version of the BIC
       
   174 */	
       
   175 TInt CCmdPowerOff::MaxSupportedVersion()
       
   176 	{
       
   177 	return (static_cast<TInt>(ECmdPowerOffInitialVersion));
       
   178 	}
       
   179 
       
   180 /**
       
   181 Configures the BIC using data contained in a ReadStream
       
   182 @param aReadStream 	A read stream containing BIC data
       
   183 */
       
   184 void CCmdPowerOff::InternalizeL(RReadStream& aReadStream)
       
   185 	{
       
   186 	iSeverity = static_cast<TCmdErrorSeverity>(aReadStream.ReadInt16L());
       
   187 	iState = static_cast<TPowerState>(aReadStream.ReadInt16L());
       
   188 	}
       
   189 
       
   190 /**
       
   191 Externalises the configuration of the BIC
       
   192 
       
   193 @param aWriteStream A write stream to write BIC data to
       
   194 */
       
   195 void CCmdPowerOff::ExternalizeL(RWriteStream& aWriteStream) const
       
   196 	{
       
   197 	aWriteStream.WriteInt16L(iSeverity);
       
   198 	aWriteStream.WriteInt16L(iState);
       
   199 	}
       
   200 
       
   201 /**
       
   202 Completes the tasks of the BIC
       
   203 */
       
   204 void CCmdPowerOff::RunL()
       
   205 	{
       
   206 	SSMLOGLEAVEIFERROR(iStatus.Int());
       
   207 
       
   208 	TInt err = KErrNone;
       
   209 	if (iState != EPwStandby)
       
   210 		{
       
   211 #ifdef TEST_SSM_GRACEFUL_SHUTDOWN
       
   212 		TInt testErr = RProperty::Set(iTestCategory, iTestKey, iState);
       
   213 		DEBUGPRINT3A("Testing TPowerState::!EPwStandby(%d), Published with error(%d)", iState, testErr);
       
   214 #else
       
   215 		err = Power::EnableWakeupEvents(iState);
       
   216 		if (err == KErrNone)
       
   217 			{
       
   218 			err = Power::PowerDown();	// will not return from this function on success...
       
   219 			}
       
   220 #endif	//TEST_SSM_GRACEFUL_SHUTDOWN
       
   221 		}
       
   222 
       
   223 	CompleteExecuteRequest(err);
       
   224 	}
       
   225 
       
   226 /**
       
   227 Called to handle any cleanup if RunL leaves
       
   228 @param aError The error to finish with
       
   229 @return	KErrNone
       
   230 */
       
   231 TInt CCmdPowerOff::RunError(TInt aError)
       
   232 	{
       
   233 	CompleteExecuteRequest(aError);
       
   234 	return KErrNone;
       
   235 	}
       
   236 
       
   237 /**
       
   238 Called during cancellation of the active BIC
       
   239 */
       
   240 void CCmdPowerOff::DoCancel()
       
   241 	{
       
   242 	Power::CancelWakeupEventNotification();
       
   243 	}
       
   244 
       
   245 /**
       
   246 Default constructor.
       
   247 */
       
   248 CCmdPowerOff::CCmdPowerOff()
       
   249 	{
       
   250 #ifdef TEST_SSM_GRACEFUL_SHUTDOWN
       
   251 	iTestKey = 123;
       
   252 	iTestCategory = TUid::Uid(0x456);
       
   253 #endif	//TEST_SSM_GRACEFUL_SHUTDOWN	
       
   254 	}
       
   255 
       
   256 /**
       
   257 Overloaded constructor
       
   258 @param aSeverity The severity of the command
       
   259 @param aState The power state to transition into
       
   260 */
       
   261 CCmdPowerOff::CCmdPowerOff(TCmdErrorSeverity aSeverity, TPowerState aState)
       
   262 	: CSsmCommandBase(aSeverity), iState(aState)
       
   263 	{
       
   264 #ifdef TEST_SSM_GRACEFUL_SHUTDOWN
       
   265 	iTestKey = 123;
       
   266 	iTestCategory = TUid::Uid(0x456);
       
   267 #endif	//TEST_SSM_GRACEFUL_SHUTDOWN
       
   268 	}
       
   269 
       
   270 
       
   271 /**
       
   272 Constructs the object through read stream.
       
   273 @param aReadStream A read stream containing BIC data
       
   274 */
       
   275 void CCmdPowerOff::ConstructL(RReadStream& aReadStream)
       
   276 	{
       
   277 	InternalizeL(aReadStream);
       
   278 	ConstructL();
       
   279 	}
       
   280 
       
   281 /**
       
   282 Constructs an object from resource file.
       
   283 @param aCommandParameters Object data from a resource file
       
   284 */
       
   285 void CCmdPowerOff::ConstructL(TSsmCommandParameters& aCommandParameters)
       
   286 	{
       
   287 	RResourceReader& reader = aCommandParameters.MainReader();
       
   288 	const TSsmCommandType type = static_cast<TSsmCommandType>(reader.ReadInt16L());
       
   289 	SSMLOGLEAVEIFFALSE(type == Type(), KErrNotSupported);
       
   290 	const TInt version = reader.ReadInt16L();
       
   291 	SSMLOGLEAVEIFFALSE(__COMPARE_VERSION(version, CCmdPowerOff::MaxSupportedVersion()), KErrNotSupported);
       
   292 	iSeverity = static_cast<TCmdErrorSeverity>(reader.ReadInt16L());
       
   293 	iState = static_cast<TPowerState>(reader.ReadInt16L());
       
   294 	ConstructL();
       
   295 	}
       
   296 
       
   297 void CCmdPowerOff::ConstructL()
       
   298 	{
       
   299 	ValidateL();
       
   300 	}
       
   301 
       
   302 /**
       
   303 Validates the data
       
   304 @leave KErrArgument If value of power state is not one of the value in TPowerState
       
   305 */
       
   306 void CCmdPowerOff::ValidateL()
       
   307 	{
       
   308 	if (iState!=EPwActive && iState!=EPwStandby && iState!=EPwOff &&
       
   309 		iState!=EPwRestart && iState!=EPwLimit)
       
   310 		{
       
   311 		SSMLOGLEAVE(KErrArgument);
       
   312 		}
       
   313 	}
       
   314