resourcemgmt/powerandmemorynotificationservice/src/shutdownsrv.cpp
changeset 0 4e1aa6a622a0
equal deleted inserted replaced
-1:000000000000 0:4e1aa6a622a0
       
     1 // Copyright (c) 1997-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 #ifndef SYMBIAN_ENABLE_SPLIT_HEADERS
       
    17 #include "shutdownsrv.h"
       
    18 #else //SYMBIAN_ENABLE_SPLIT_HEADERS
       
    19 #include "shutdownsess.h"
       
    20 #endif //SYMBIAN_ENABLE_SPLIT_HEADERS
       
    21 #include "savenotf.h"
       
    22 #include "savepriv.h"
       
    23 #include "shutdowntimer.h"
       
    24 #include <hal.h>
       
    25 #include <f32file.h>
       
    26 #include <bautils.h>
       
    27 
       
    28 #ifdef SYMBIAN_SSM_GRACEFUL_SHUTDOWN
       
    29 #include <s32mem.h>
       
    30 #include <ssm/ssmpatchableconstants.h>
       
    31 #endif
       
    32 
       
    33 //The function used to start the profiler, while profiling the shutdown.
       
    34 #ifdef __PROFILE_SHUTDOWN
       
    35 _LIT(KProfilerCmd,"start -noui");
       
    36 void StartProfiler()
       
    37 	{
       
    38 	RProcess p;
       
    39 	TInt r=p.Create(KProfilerName,KProfilerCmd);
       
    40 	if (r==KErrNone)
       
    41 		{
       
    42 		p.Resume();
       
    43 		p.Close();
       
    44 		}
       
    45 	}
       
    46 #endif
       
    47 
       
    48 //The function used to unload the profiler, while profiling the shutdown.
       
    49 #ifdef __PROFILE_SHUTDOWN
       
    50 void UnloadProfiler()
       
    51 	{
       
    52 	TFullName name;
       
    53 	TFindServer findSvr(KProfilerName);
       
    54 	Profiler::Unload();
       
    55 	while(findSvr.Next(name) == KErrNone)
       
    56 		{
       
    57 		User::After(1000000);
       
    58 		findSvr.Find(KProfilerName);
       
    59 		}
       
    60 	}
       
    61 #endif
       
    62 
       
    63 //
       
    64 // class CServShutdownServer
       
    65 //
       
    66 
       
    67 /**
       
    68 Standard phase-one factory method for creating CServShutdownServer instances.
       
    69 @return A newly-constructed shutdown server object.
       
    70 @leave Some system-wide error codes including KErrNoMemory.
       
    71 */
       
    72 EXPORT_C CServShutdownServer* CServShutdownServer::NewL()
       
    73 	{ // static
       
    74 	CServShutdownServer* self=new(ELeave) CServShutdownServer(CActive::EPriorityStandard);
       
    75 	return self;
       
    76 	}
       
    77 
       
    78 /**
       
    79 Destructor.
       
    80 Releases CShutdownTimer object if it has been allocated.
       
    81 */
       
    82 EXPORT_C CServShutdownServer::~CServShutdownServer()
       
    83 	{
       
    84 	delete iShutdownTimer;
       
    85 	}
       
    86 
       
    87 /**
       
    88 This method iterates through all session instances and checks that all they have pending
       
    89 requests (which means that all clients have completed their powerdown related processing
       
    90 and re-registered itself back to the shutdown server).. If that's true (all sessions have
       
    91 pending requests) and iPowerOff flag is nonzero, the method will call SwitchOff() - the
       
    92 power will be switched off. 
       
    93 @see CServShutdownServer::SwitchOff()
       
    94 @see CServShutdownSession::HasPendingRequest()
       
    95 */
       
    96 EXPORT_C void CServShutdownServer::HandlePowerNotifRequest(const RThread& /*aClient*/)
       
    97 	{
       
    98 	// if all sessions have a pending request then they've all saved their data and we can switch off
       
    99 	TBool completed=ETrue;
       
   100 	TDblQueIter<CSession2> iter(iSessionIter);
       
   101 	iter.SetToFirst();
       
   102 
       
   103 	for (CSession2* session=iter++; session!=NULL; session=iter++)
       
   104 		{
       
   105 		if (!static_cast<CServShutdownSession*>(session)->HasPendingRequest())
       
   106 			{
       
   107 			completed=EFalse;
       
   108 			break;
       
   109 			}
       
   110 		}
       
   111 	if (completed && iPowerOff)
       
   112 		{
       
   113 		SwitchOff();
       
   114 		}
       
   115 	}
       
   116 
       
   117 /**
       
   118 This method will call SwitchOff(), if there are no sessions, so - no clients.
       
   119 If there are registered clients (sessions), the method will call CServShutdownSession::NotifySave()
       
   120 for each of them.
       
   121 If the timer object has been initialised, it calculates the shutdown timeout value 
       
   122 and adds the timer to the active scheduler.
       
   123 @param aSaveType The action, which will be given to the clients, when calling their 
       
   124                  MSaveObserver::SaveL() implementations.
       
   125 @see CServShutdownServer::SwitchOff()
       
   126 @see CServShutdownSession::NotifySave()
       
   127 */
       
   128 EXPORT_C void CServShutdownServer::NotifySave(MSaveObserver::TSaveType aSaveType)
       
   129 	{
       
   130 	TDblQueIter<CSession2> iter(iSessionIter);
       
   131 	iter.SetToFirst();
       
   132 	CSession2* p=iter++;
       
   133 	if (p==NULL)
       
   134 		{
       
   135 		if (iPowerOff)
       
   136 			{
       
   137 			SwitchOff();
       
   138 			}
       
   139 		}
       
   140 	else
       
   141 		{
       
   142 		TInt numClients = 0;
       
   143 
       
   144 		iter.SetToFirst();
       
   145 	    for (CSession2* session=iter++; session!=NULL; session=iter++)
       
   146 			{
       
   147 			static_cast<CServShutdownSession*>(session)->NotifySave(aSaveType);
       
   148 			numClients++;  
       
   149 			}
       
   150 			
       
   151 		if(iShutdownTimer && !iShutdownTimer->IsActive())
       
   152 			{
       
   153 			iShutdownTimer->Start(numClients);
       
   154 			}
       
   155 		}
       
   156 	}
       
   157 
       
   158 /**
       
   159 @return Non-zero If the powerdown sequence has been initiated.
       
   160 */
       
   161 EXPORT_C TBool CServShutdownServer::IsPowerOff() const
       
   162 	{
       
   163 	return iPowerOff;
       
   164 	}
       
   165 
       
   166 /**
       
   167 Cancels any power off request.
       
   168 */
       
   169 EXPORT_C void CServShutdownServer::CancelPowerOff()
       
   170 	{
       
   171 	iPowerOff=EFalse;
       
   172 	
       
   173 	// Cancel the timer.
       
   174 	if (iShutdownTimer)
       
   175 		{
       
   176 		iShutdownTimer->Cancel();
       
   177 		}
       
   178 	}
       
   179 
       
   180 /**
       
   181 @param aPriority The active object priority.
       
   182 */
       
   183 EXPORT_C CServShutdownServer::CServShutdownServer(TInt aPriority):
       
   184 	CServer2(aPriority), iShutdownTimer(0)
       
   185 	{
       
   186     }
       
   187 
       
   188 /**
       
   189 Completes the server construction by adding the server to the active scheduler and initializing the
       
   190 CShutdownTimer object if applicable.
       
   191 @leave See CServer2::StartL() leave error codes.
       
   192 @see CServer2::StartL()
       
   193 @panic KErrNotSupported Incorrect patchable variables configuration.
       
   194 */
       
   195 EXPORT_C void CServShutdownServer::ConstructL()
       
   196 	{
       
   197 	iShutdownTimer = CShutdownTimer::NewL(*this);
       
   198 	#ifdef TEST_SHUTDOWN_SERVER 
       
   199 	StartL(__TEST_SHUTDOWN_SERVER_NAME);
       
   200 	#else
       
   201 	StartL(__SHUTDOWN_SERVER_NAME);
       
   202 	#endif
       
   203 	}
       
   204 
       
   205 /**
       
   206 This method switches off the power using the Power API.
       
   207 If there is a defined SYSLIBS_TEST macro, the method does nothing.
       
   208 */
       
   209 void CServShutdownServer::DoSwitchOff()
       
   210 	{
       
   211 	//Finalize the drives before shutting down
       
   212 	RFs fs;
       
   213 	TInt r=fs.Connect(); 
       
   214 	if (r==KErrNone) 
       
   215 		{
       
   216 		//Ignore the error code, as it is not a critical call
       
   217 		r=fs.FinaliseDrives();
       
   218 		fs.Close();
       
   219 		}
       
   220 #ifdef 	SYSLIBS_TEST 
       
   221 		// Test mode, only prints debug message.
       
   222 		RDebug::Printf("CServShutdownServer::SwitchOff() gets run in SYSLIBS_TEST mode.\n");
       
   223 #else 	//SYSLIBS_TEST
       
   224 	// restart or standby/shutdown using Power API 
       
   225 	if (Power::EnableWakeupEvents(iPowerEvent) == KErrNone)
       
   226 		{
       
   227 		// Prepare to wake up if power event is standby
       
   228 		TRequestStatus s;
       
   229 		Power::RequestWakeupEventNotification(s);
       
   230 		Power::PowerDown(); // if event is Restart, this function should never return
       
   231 		User::WaitForRequest(s);
       
   232 		}
       
   233 #endif //SYSLIBS_TEST
       
   234 	} 
       
   235 void CServShutdownServer::SwitchOff()
       
   236 	{
       
   237 	// If the timer has applied, cancel any outstanding requests, 
       
   238 	// no matter SwitchOff() has been triggered by the timer or the shutdown server.
       
   239 	if (iShutdownTimer)
       
   240 		{
       
   241 		iShutdownTimer->Cancel();
       
   242 		}
       
   243 
       
   244 	if (iPowerOff)
       
   245 		{
       
   246 #ifdef __PROFILE_SHUTDOWN
       
   247 		UnloadProfiler();
       
   248 #endif //__PROFILE_SHUTDOWN
       
   249 		}
       
   250 	//SSM shutdown is used only when patchable constant KSsmGracefulShutdown is true 
       
   251 #ifdef SYMBIAN_SSM_GRACEFUL_SHUTDOWN
       
   252 	if (iPowerOff && !IsSsmGracefulShutdown())
       
   253 #else
       
   254 	if (iPowerOff)
       
   255 #endif
       
   256 		{
       
   257 		//Shutdown using old shutdownsrv, not SSM
       
   258 		DoSwitchOff();
       
   259 		}
       
   260 	iPowerOff = EFalse;
       
   261 	}
       
   262 
       
   263 /**
       
   264 This method creates a new server side session object.
       
   265 @param aVersion Shutdown server version number.
       
   266 @return A pointer to the created session object.
       
   267 @leave KErrNotSupported Unknown shutdown server version
       
   268 @leave Some system-wide error codes including KErrNoMemory.
       
   269 */
       
   270 CSession2* CServShutdownServer::NewSessionL(const TVersion& aVersion,const RMessage2& /*aMessage*/) const
       
   271 	{
       
   272 	TVersion v(KShutdownMajorVN,KShutdownMinorVN,KShutdownBuildVN);
       
   273 	if (!User::QueryVersionSupported(v,aVersion))
       
   274 		User::Leave(KErrNotSupported);
       
   275 
       
   276 	CSession2* pSession = CServShutdownSession::NewL();
       
   277 	return pSession;
       
   278 	}
       
   279 
       
   280 /**
       
   281 This method has to be called, when the registered clients have to be notified that a 
       
   282 particular action has to be done, such as MSaveObserver::ESaveData, MSaveObserver::ESaveAll, 
       
   283 MSaveObserver::EReleaseRAM,...
       
   284 If this is a beginning of a powerdown sequence, the method will store the locales and the HAL
       
   285 properties.
       
   286 If the requested action is not MSaveObserver::ESaveNone, the method will call 
       
   287 CServShutdownServer::NotifySave().
       
   288 @param aAction The type of the requested action
       
   289 @param aPowerOff If it is non-zero, this is the beginning of a powerdown sequence.
       
   290 @param aEvent The type of the powerdown event (power off or restart)
       
   291 @leave KErrNotSupported Leaves if aEvent is invalid  
       
   292 @see CServShutdownServer::NotifySave()
       
   293 @see TPowerState
       
   294 */
       
   295 EXPORT_C void CServShutdownServer::HandleShutdownEventL(MSaveObserver::TSaveType aAction,TBool aPowerOff, TPowerState aEvent)
       
   296 	{
       
   297 	if( aPowerOff )
       
   298 		{
       
   299 		__ASSERT_ALWAYS((aEvent>EPwActive)&&(aEvent<EPwLimit), User::Leave(KErrNotSupported));
       
   300 #ifdef __PROFILE_SHUTDOWN
       
   301 		StartProfiler();
       
   302 #endif
       
   303 
       
   304 #ifdef SYMBIAN_SSM_GRACEFUL_SHUTDOWN
       
   305 		if (!IsSsmGracefulShutdown())
       
   306 			{
       
   307 			// Save the state of the HAL
       
   308 			// The state of the locale should be saved before the HAL
       
   309 			BaflUtils::PersistLocale();
       
   310 			BaflUtils::PersistHAL();
       
   311 			}
       
   312 #else
       
   313 		// Save the state of the HAL
       
   314 		// The state of the locale should be saved before the HAL
       
   315 		BaflUtils::PersistLocale();
       
   316 		BaflUtils::PersistHAL();
       
   317 #endif
       
   318 		}
       
   319 
       
   320 	iPowerOff=aPowerOff;
       
   321 	iPowerEvent = aEvent;
       
   322 	// add LAF setting for powering down screen (& keyboard??)
       
   323 	if (aAction != MSaveObserver::ESaveNone)
       
   324 		{
       
   325 		NotifySave(aAction);
       
   326 		}
       
   327 	}
       
   328 
       
   329 /**
       
   330 This method returns an information about the shutdown status.
       
   331 @param aPowerOff An output parameter, where iPowerOff value will be stored. 
       
   332                  It will be non-zero, if a powerdown sequence has been initiated.
       
   333 @param aAllSessionsHavePendingRequest An output parameter. It will be non-zero, if
       
   334                  all clients has pending requests.
       
   335 */
       
   336 EXPORT_C void CServShutdownServer::GetShutdownState(TBool& aPowerOff, TBool& aAllSessionsHavePendingRequest) const
       
   337 	{
       
   338 	aAllSessionsHavePendingRequest = AllSessionsHavePendingRequest();
       
   339 	aPowerOff = iPowerOff;
       
   340 	}
       
   341 
       
   342 /**
       
   343 This method creates an array of CArrayFix<TThreadId> type and appends to it the
       
   344 thread id-s of the all registered clients.
       
   345 The created CArrayFix<TThreadId> instance will be pushed on the cleanup stack.
       
   346 @return A pointer to a CArrayFix<TThreadId> array with the client thread id-s.
       
   347 @leave Some system-wide error codes including KErrNoMemory.
       
   348 */
       
   349 EXPORT_C CArrayFix<TThreadId>* CServShutdownServer::ClientArrayLC()
       
   350 	{
       
   351 	CArrayFix<TThreadId>* clientArray=new(ELeave) CArrayFixFlat<TThreadId>(2);
       
   352 	CleanupStack::PushL(clientArray);
       
   353 	TDblQueIter<CSession2> iter(iSessionIter);
       
   354 	iter.SetToFirst();
       
   355 	TKeyArrayFix key(0,ECmpTInt);
       
   356     for (CSession2* session=iter++; session!=NULL; session=iter++)
       
   357 		{
       
   358 		TThreadId id=static_cast<CServShutdownSession*>(session)->ClientThreadId();
       
   359 		TInt pos;
       
   360 		if (clientArray->Find(id,key,pos)!=0)
       
   361 			{
       
   362 			clientArray->AppendL(id);
       
   363 			}
       
   364 		}
       
   365 	return clientArray;
       
   366 	
       
   367 	
       
   368 	}
       
   369 
       
   370 #ifdef SYMBIAN_SSM_GRACEFUL_SHUTDOWN
       
   371 /**
       
   372 This method will write thread id-s of all clients that are registered for shutdown notification in to a streem.
       
   373 @param aMessage consists of buffer, clientside array count and server side array count 
       
   374 */
       
   375 void CServShutdownServer::ClientArrayL(const RMessage2& aMessage)
       
   376 	{
       
   377 	const TInt arrayItemCount = ClientArrayCount();
       
   378 	//write all registered client array in a buffer only when client side 
       
   379 	//array count is equal to server side array count
       
   380 	if(arrayItemCount == aMessage.Int1())
       
   381 		{
       
   382 		const TInt sizeRequired = arrayItemCount * sizeof(TThreadId);
       
   383 		CBufFlat* const buf=CBufFlat::NewL(sizeRequired);
       
   384 		CleanupStack::PushL(buf);
       
   385 		RBufWriteStream writeStream(*buf);
       
   386 		CleanupClosePushL(writeStream); 
       
   387 		TDblQueIter<CSession2> iter(iSessionIter);
       
   388 		iter.SetToFirst();
       
   389     	for (CSession2* session=iter++; session!=NULL; session=iter++)
       
   390 			{
       
   391 			TThreadId id=static_cast<CServShutdownSession*>(session)->ClientThreadId();
       
   392 			//Thread id (which is TUint64) is broken in to two TUint32 and written as RWriteStream doesnt 
       
   393 			//support TUint64. Client API will recreate TUint64 value from these TUint32 values.
       
   394 			if(id.Id())
       
   395 				{
       
   396 				//RWriteStream there is no API to writes a TUint64 value as a 64 bit value to stream
       
   397 				writeStream.WriteUint32L(I64HIGH(id.Id()));
       
   398 				writeStream.WriteUint32L(I64LOW(id.Id()));
       
   399 				}
       
   400 			}
       
   401 		writeStream.CommitL();
       
   402 		aMessage.WriteL(0, buf->Ptr(0));
       
   403 		CleanupStack::PopAndDestroy(2,buf);	//writeStream, buf 
       
   404 		}
       
   405 	else
       
   406 		{
       
   407 		aMessage.Write(2, TPckg<TInt>(arrayItemCount));
       
   408 		}	
       
   409 	
       
   410 	}
       
   411 		
       
   412 /**
       
   413 This method will return the number of client that are registered for Shutdown notification with ShutDown server.
       
   414 @return Number of registered clients.
       
   415 */
       
   416 TInt CServShutdownServer::ClientArrayCount()
       
   417 	{
       
   418 	TDblQueIter<CSession2> iter(iSessionIter);
       
   419 	iter.SetToFirst();
       
   420 	TInt count =0;
       
   421     for (CSession2* session=iter++; session!=NULL; session=iter++)
       
   422 		{
       
   423 		TThreadId id=static_cast<CServShutdownSession*>(session)->ClientThreadId();
       
   424 		//Clients which are not registered for notification will not have RMessage in the session and hence 
       
   425 		//thread id will be NULL(e.g.CLafShutdownEventObserverAdaptor). Avoid such clients. 		
       
   426 		if(id.Id())
       
   427 			{
       
   428 			++count;
       
   429 			}
       
   430 		}
       
   431 	return count;
       
   432 	}
       
   433 #endif	//SYMBIAN_SSM_GRACEFUL_SHUTDOWN	
       
   434 /**
       
   435 @return Non-zero, if all registered clients have pending requests.
       
   436 */
       
   437 TBool CServShutdownServer::AllSessionsHavePendingRequest() const
       
   438 	{
       
   439 	TBool ret=ETrue;
       
   440 	TDblQueIter<CSession2> iter(iSessionIter);
       
   441 	iter.SetToFirst();
       
   442 	for (CSession2* session=iter++; session!=NULL; session=iter++)
       
   443 		{
       
   444 		const CServShutdownSession* mySession=static_cast<CServShutdownSession*>(session);
       
   445 		if (!mySession->HasPendingRequest())
       
   446 			{
       
   447 			ret=EFalse;
       
   448 			break;
       
   449 			}
       
   450 		}
       
   451 	return ret;
       
   452 	}
       
   453 
       
   454 
       
   455 /**
       
   456 @param aId Client's thread id.
       
   457 @return Non-zero if the client with this thread id has no pending request.
       
   458 */
       
   459 EXPORT_C TBool CServShutdownServer::IsClientHung(TThreadId aId) const
       
   460 	{
       
   461 	TBool ret=EFalse;
       
   462 	TDblQueIter<CSession2> iter(iSessionIter);
       
   463 	iter.SetToFirst();
       
   464 	for (CSession2* session=iter++; session!=NULL; session=iter++)
       
   465 		{
       
   466 		const CServShutdownSession* mySession=static_cast<CServShutdownSession*>(session);
       
   467 
       
   468 		if (mySession->ClientThreadId()==aId)
       
   469 			{
       
   470 			if (!mySession->HasPendingRequest())
       
   471 				{
       
   472 				ret=ETrue;
       
   473 				break;
       
   474 				}
       
   475 			}
       
   476 		}
       
   477 	return ret;
       
   478 	}
       
   479 
       
   480 //
       
   481 // class CServShutdownSession
       
   482 //
       
   483 
       
   484 /**
       
   485 */
       
   486 EXPORT_C CServShutdownSession::CServShutdownSession()
       
   487 	: CSession2(),
       
   488 	iCurrentEvent(-1), iOutstandingEvent(-1)
       
   489 	{}
       
   490 
       
   491 /**
       
   492 */
       
   493 EXPORT_C CServShutdownSession::~CServShutdownSession()
       
   494 	{
       
   495 	}
       
   496 
       
   497 /**
       
   498 Standard phase-one factor method for creating CServShutdownSession instances.
       
   499 @return A pointer to the created CServShutdownSession instance.
       
   500 @leave KErrNoMemory Not enough memory to complete the operation.
       
   501 */
       
   502 CServShutdownSession* CServShutdownSession::NewL()
       
   503 	{
       
   504 	return new (ELeave) CServShutdownSession;
       
   505 	}
       
   506 
       
   507 /**
       
   508 @return Non-zero, if the client has a pending request.
       
   509 */
       
   510 TBool CServShutdownSession::HasPendingRequest() const
       
   511 	{
       
   512 	return !iPtr.IsNull();
       
   513 	}
       
   514 
       
   515 /**
       
   516 This method will complete the pending asychronous client request, effectivelly notifying it
       
   517 about the action, which the client has to do.
       
   518 @param aSaveType The type of the requested save action.
       
   519 */
       
   520 void CServShutdownSession::NotifySave(MSaveObserver::TSaveType aSaveType)
       
   521 	{
       
   522 	const TInt saveType=(TInt)aSaveType;
       
   523 	if (HasPendingRequest())
       
   524 		{
       
   525 		iPtr.Complete(saveType);
       
   526 		iCurrentEvent=saveType;
       
   527 		}
       
   528 	else
       
   529 		{
       
   530 		if (iCurrentEvent==(TInt)MSaveObserver::ESaveAll || 
       
   531 			(iCurrentEvent==(TInt)MSaveObserver::ESaveQuick && 
       
   532 			 aSaveType==MSaveObserver::ESaveData))
       
   533             {
       
   534 			iOutstandingEvent=saveType;
       
   535 			}
       
   536 		}
       
   537 	}
       
   538 
       
   539 /**
       
   540 This method dispatches all client requests to the appropriate method calls.
       
   541 @param aMessage The client's message
       
   542 @param aCompleteRequest An output parameter. If zero, the client request
       
   543                 will be completed later.
       
   544 */
       
   545 void CServShutdownSession::DoServiceL(const RMessage2& aMessage, TBool& aCompleteRequest)
       
   546 	{
       
   547 	switch (aMessage.Function())
       
   548 		{
       
   549 	case TSaveOpCodeNotify:
       
   550 		RequestNotifyPowerDown(aMessage);
       
   551 		// don't complete async message yet
       
   552 		aCompleteRequest=EFalse;
       
   553 		break;
       
   554 	case TSaveOpCodeNotifyCancel:
       
   555 		RequestNotifyPowerDownCancel();
       
   556 		break;
       
   557 	case TSaveOpCodeHandleError:
       
   558 		User::Leave(KErrNotSupported);
       
   559 		break;
       
   560 	case TSaveOpCodePowerOff:
       
   561 		PowerOffL(aMessage);
       
   562 		break;
       
   563 	case TSaveOpCodeQueryPowerState:
       
   564 		PowerStateL(aMessage);
       
   565 		break;
       
   566 #ifdef SYMBIAN_SSM_GRACEFUL_SHUTDOWN
       
   567 	case EEventObsAdaptHandleShutdown:
       
   568 	case EEventObsAdaptClientArrayCount:
       
   569 	case EEventObsAdaptClientArray:
       
   570 	case EEventObsAdaptIsClientHung:
       
   571 	case EEventObsAdaptGetShutdownState:
       
   572 		{
       
   573 		if (IsSsmGracefulShutdown())	// SSM should be used for device shutdown
       
   574 			{
       
   575 			switch(aMessage.Function())
       
   576 				{
       
   577 				case EEventObsAdaptHandleShutdown:
       
   578 					HandleShutdownEventL(aMessage);
       
   579 					break;
       
   580 				case EEventObsAdaptClientArrayCount:
       
   581 					ClientArrayCount(aMessage);
       
   582 					break;
       
   583 				case EEventObsAdaptClientArray:
       
   584 					ClientArrayL(aMessage);
       
   585 					break;
       
   586 				case EEventObsAdaptIsClientHung:
       
   587 					IsClientHung(aMessage);
       
   588 					break;
       
   589 				case EEventObsAdaptGetShutdownState:
       
   590 					GetShutdownState(aMessage);
       
   591 					break;			
       
   592 				}
       
   593 			}
       
   594 		break;	
       
   595 		}
       
   596 #endif // SYMBIAN_SSM_GRACEFUL_SHUTDOWN
       
   597 	default:
       
   598 		User::Leave(KErrNotSupported);
       
   599 		break;
       
   600 		}
       
   601 	}
       
   602 
       
   603 #ifdef SYMBIAN_SSM_GRACEFUL_SHUTDOWN
       
   604 
       
   605 void CServShutdownSession::HandleShutdownEventL(const RMessage2& aMessage)
       
   606 	{
       
   607 	MSaveObserver::TSaveType action = static_cast <MSaveObserver::TSaveType> (aMessage.Int0());
       
   608 	TBool powerOff = static_cast <TBool> (aMessage.Int1());
       
   609 	TPowerState powerEvent = static_cast <TPowerState> (aMessage.Int2());
       
   610 
       
   611 	CServShutdownServer* server = static_cast<CServShutdownServer*>(const_cast<CServer2*>(Server()));
       
   612 	server->HandleShutdownEventL(action, powerOff, powerEvent);
       
   613 	}
       
   614 
       
   615 void CServShutdownSession::ClientArrayCount(const RMessage2& aMessage) const
       
   616 	{
       
   617 	CServShutdownServer* server = static_cast<CServShutdownServer*>(const_cast<CServer2*>(Server()));
       
   618 	const TInt count = server->ClientArrayCount();
       
   619 	aMessage.Write(0, TPckg<TInt>(count));
       
   620 	}
       
   621 /*
       
   622 This function will write all registered client array in a buffer only when client side array count is equal to server side array count
       
   623 else aMessage will contain an empty buffer and server side array count .
       
   624 @param aMessage consists of buffer, clientside array count and server side array count 
       
   625 */
       
   626 void CServShutdownSession::ClientArrayL(const RMessage2& aMessage)
       
   627 	{
       
   628 	CServShutdownServer* server = static_cast<CServShutdownServer*>(const_cast<CServer2*>(Server()));
       
   629 	server->ClientArrayL(aMessage);
       
   630 	}
       
   631 
       
   632 void CServShutdownSession::IsClientHung(const RMessage2& aMessage) const
       
   633 	{
       
   634 	CServShutdownServer* server = static_cast<CServShutdownServer*>(const_cast<CServer2*>(Server()));
       
   635 	TThreadId threadId = static_cast <TThreadId>(aMessage.Int0());
       
   636 	TBool clientHung = server->IsClientHung(threadId);
       
   637 
       
   638 	aMessage.Write(1, TPckg<TBool>(clientHung));
       
   639 	}
       
   640 
       
   641 void CServShutdownSession::GetShutdownState(const RMessage2& aMessage) const
       
   642 	{
       
   643 	TBool powerOff;
       
   644 	TBool allSessionsHavePendingRequest;
       
   645 
       
   646 	CServShutdownServer* server = static_cast<CServShutdownServer*>(const_cast<CServer2*>(Server()));
       
   647 	server->GetShutdownState(powerOff, allSessionsHavePendingRequest);
       
   648 	aMessage.Write(0, TPckg<TBool>(powerOff));
       
   649 	aMessage.Write(1, TPckg<TBool>(allSessionsHavePendingRequest));
       
   650 	}
       
   651 
       
   652 #endif 	//SYMBIAN_SSM_GRACEFUL_SHUTDOWN
       
   653 
       
   654 /**
       
   655 Handles the servicing of client requests passed to the shutdown server.
       
   656 @param	aMessage The message containing the client request.
       
   657 */
       
   658 EXPORT_C void CServShutdownSession::ServiceL(const RMessage2& aMessage)
       
   659 	{
       
   660 	TBool aCompleteRequest=ETrue;
       
   661 	TRAPD(error, DoServiceL(aMessage, aCompleteRequest));
       
   662 	if (aCompleteRequest)
       
   663 		{
       
   664 		aMessage.Complete(error);
       
   665 		}
       
   666 	}
       
   667 
       
   668 /**
       
   669 This method processes a client-side registration request. It is an asynchronous request,
       
   670 which will be completed later, when powerdown/low memory event occurs.
       
   671 @param	aMessage The message containing the client request.
       
   672 */
       
   673 void CServShutdownSession::RequestNotifyPowerDown(const RMessage2& aMessage)
       
   674 	{
       
   675 	iCurrentEvent=-1;
       
   676 	iPtr = aMessage;
       
   677 
       
   678 	if (iOutstandingEvent!=-1)
       
   679 		{
       
   680 		NotifySave((MSaveObserver::TSaveType)iOutstandingEvent);
       
   681 		iOutstandingEvent=-1;
       
   682 		}
       
   683 	//The thread variable is just a dummy variable(input for CServShutdownServer::HandlePowerNotifRequest)
       
   684 	//and is not used inside the function at all, it is there to preserve BC.
       
   685 	RThread thread;
       
   686 	static_cast<CServShutdownServer*>(const_cast<CServer2*>(Server()))->HandlePowerNotifRequest(thread);
       
   687 	}
       
   688 
       
   689 /**
       
   690 This method cancels the client registration, completing the requests with 
       
   691 KErrCancel error code.
       
   692 */
       
   693 void CServShutdownSession::RequestNotifyPowerDownCancel()
       
   694 	{
       
   695 	if (HasPendingRequest())
       
   696 		iPtr.Complete(KErrCancel);
       
   697 	}
       
   698 
       
   699 /**
       
   700 @return The client's thread id.
       
   701 */
       
   702 TThreadId CServShutdownSession::ClientThreadId() const
       
   703     {
       
   704     TThreadId id = NULL;
       
   705     RThread clientThread;
       
   706     //Clients which are not registered for notification will not have RMessage in the session and hence 
       
   707 	//thread id will be NULL(e.g.CLafShutdownEventObserverAdaptor). Avoid such clients. 
       
   708     if(!iPtr.IsNull() && KErrNone == iPtr.Client(clientThread))
       
   709     	{	
       
   710 		id = clientThread.Id();	
       
   711     	}
       
   712     clientThread.Close();	
       
   713     return id;
       
   714     }
       
   715 
       
   716 
       
   717 /**
       
   718 This method should be used only with  SYSLIBS_TEST macro defined and can be used to 
       
   719 initiate a powerdown sequence.
       
   720 Without SYSLIBS_TEST macro defined the method will panic the client with 
       
   721 KErrNotSupported error code.
       
   722 @param	aMessage The message containing the client request.
       
   723 */
       
   724 #ifdef SYSLIBS_TEST
       
   725 void CServShutdownSession::PowerOffL(const RMessage2& aMessage)
       
   726     {
       
   727 	MSaveObserver::TSaveType action = static_cast <MSaveObserver::TSaveType> (aMessage.Int0());
       
   728     TBool powerOff = static_cast <TBool> (aMessage.Int1());
       
   729 	CServShutdownServer* server = static_cast<CServShutdownServer*>(const_cast<CServer2*>(Server()));
       
   730     server->HandleShutdownEventL(action, powerOff);
       
   731     }
       
   732 #else
       
   733 void CServShutdownSession::PowerOffL(const RMessage2& aMessage)
       
   734     {
       
   735     aMessage.Panic(__SHUTDOWN_SERVER_NAME, KErrNotSupported);
       
   736     }
       
   737 #endif//SYSLIBS_TEST
       
   738 
       
   739 /**
       
   740 This method should be used only with SYSLIBS_TEST macro defined and can be used to 
       
   741 get the power state of the server.
       
   742 Without SYSLIBS_TEST macro defined the method will panic the client with 
       
   743 KErrNotSupported error code.
       
   744 @param	aMessage The message containing the client request.
       
   745 */
       
   746 #ifdef SYSLIBS_TEST
       
   747 void CServShutdownSession::PowerStateL(const RMessage2& aMessage) const
       
   748     {
       
   749     CServShutdownServer* server = static_cast<CServShutdownServer*>(const_cast<CServer2*>(Server()));
       
   750     TBool powerOff = server->IsPowerOff();
       
   751     TPckg<TBool> powerOffPckg(powerOff);
       
   752     aMessage.WriteL(0,powerOffPckg);
       
   753     }
       
   754 #else
       
   755 void CServShutdownSession::PowerStateL(const RMessage2& aMessage) const
       
   756     {
       
   757     aMessage.Panic(__SHUTDOWN_SERVER_NAME, KErrNotSupported);
       
   758     }
       
   759 #endif//SYSLIBS_TEST
       
   760