commonappservices/alarmserver/Server/Source/assrvsystemstate.cpp
changeset 0 2e3d3ce01487
child 57 5e7d68cc22e0
equal deleted inserted replaced
-1:000000000000 0:2e3d3ce01487
       
     1 // Copyright (c) 2008-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 "assrvsystemstate.h"
       
    17 
       
    18 #include <startupdomaindefs.h>
       
    19 #include <ssm/ssmsubstates.hrh>
       
    20 
       
    21 // Some constants defined for ssm workaround
       
    22 const TUid KCategoryWorkAroundUID = {0x101f5027};
       
    23 const TInt KKeyWorkAroundStateChange = 301;
       
    24 const TInt KKeyWorkAroundAck = 302;
       
    25 	
       
    26 CASSrvSystemState* CASSrvSystemState::NewL()
       
    27 	{
       
    28 	CASSrvSystemState* self = new(ELeave) CASSrvSystemState();
       
    29 	CleanupStack::PushL(self);
       
    30 	self->ConstructL();
       
    31 	CleanupStack::Pop(self);
       
    32 	return self;
       
    33 	}
       
    34 
       
    35 CASSrvSystemState::~CASSrvSystemState()
       
    36 	{
       
    37 	Cancel();
       
    38 	iStateAwareSession.Close();
       
    39 	iObservers.Close();
       
    40 	
       
    41 	// close the workaround property
       
    42 	iWorkAroundProperty.Close();
       
    43 	}
       
    44 
       
    45 CASSrvSystemState::CASSrvSystemState()
       
    46 	:CActive(CActive::EPriorityStandard)
       
    47 	{
       
    48 	CActiveScheduler::Add(this);
       
    49 	}
       
    50 
       
    51 void CASSrvSystemState::ConstructL()
       
    52 	{
       
    53 	iWorkAround = (iWorkAroundProperty.Attach(KCategoryWorkAroundUID, KKeyWorkAroundStateChange) == KErrNone);
       
    54 	
       
    55 	if (iWorkAround)
       
    56 		{
       
    57 		// the workwround property has been defined so we must be in test code
       
    58 		// subscribe to changes to this property
       
    59 		iWorkAroundProperty.Subscribe(iStatus);
       
    60 		SetActive();
       
    61 		}
       
    62 	else
       
    63 		{
       
    64 		// This should be all that is in this function when we remove the workaround
       
    65 		TInt connectError = iStateAwareSession.Connect(KSM2AppServicesDomain3);
       
    66 		if (connectError == KErrNone)
       
    67 			{
       
    68 			iStateAwareSession.RequestStateNotification(iStatus);
       
    69 			SetActive();
       
    70 			}
       
    71 		}
       
    72 	}
       
    73 
       
    74 void CASSrvSystemState::RunL()
       
    75 	{
       
    76 	// If there is an error fail silently, there isn't anything useful we can do
       
    77 	// and it would be excessive to panic the server because we can't register a device wakeup
       
    78 	
       
    79 	if (iStatus == KErrNone)
       
    80 		{
       
    81 		MASSrvSystemStateObserver::TState state;
       
    82 		if (GetState(state))
       
    83 			{
       
    84 			// notify the observers
       
    85 			const TInt KObserverCount(iObservers.Count()); 
       
    86 			for (TInt i(0) ; i < KObserverCount ; ++i)
       
    87 				{
       
    88 				iObservers[i]->MHandleSystemStateChange(state);
       
    89 				}
       
    90 			}
       
    91 		
       
    92 		AcknowledgeAndRequestStateNotification();
       
    93 		}
       
    94 	}
       
    95 
       
    96 void CASSrvSystemState::DoCancel()
       
    97 	{
       
    98 	if (iWorkAround)
       
    99 		{
       
   100 		iWorkAroundProperty.Cancel();
       
   101 		}
       
   102 	else
       
   103 		{
       
   104 		iStateAwareSession.RequestStateNotificationCancel();
       
   105 		}
       
   106 	}
       
   107 
       
   108 void CASSrvSystemState::RequestNotificationL(MASSrvSystemStateObserver& aObserver)
       
   109 	{
       
   110 	User::LeaveIfError(iObservers.InsertInAddressOrder(&aObserver));
       
   111 	}
       
   112 
       
   113 void CASSrvSystemState::RequestNotificationCancel(MASSrvSystemStateObserver& aObserver)
       
   114 	{
       
   115 	TInt index = KErrNotFound;
       
   116 	const TInt error = iObservers.FindInAddressOrder(&aObserver, index);
       
   117 	
       
   118 	if	(error != KErrNotFound)
       
   119 		{
       
   120 		iObservers.Remove(index);
       
   121 		}
       
   122 	}
       
   123 
       
   124 TBool CASSrvSystemState::GetState(MASSrvSystemStateObserver::TState& aState)
       
   125 	{
       
   126 	TBool interestedInChange(EFalse);
       
   127 	TSsmState ssmState;
       
   128 	
       
   129 	if (iWorkAround)
       
   130 		{
       
   131 		TInt value;
       
   132 		iWorkAroundProperty.Get(KCategoryWorkAroundUID, KKeyWorkAroundStateChange, value);
       
   133 		ssmState.Set(value, 0x10);
       
   134 		}
       
   135 	else
       
   136 		{
       
   137 		ssmState = iStateAwareSession.State();
       
   138 		}
       
   139 		
       
   140 	if (ssmState.MainState() == ESsmShutdown && ssmState.SubState() == ESsmShutdownSubStateCritical)
       
   141 		{
       
   142 		aState = MASSrvSystemStateObserver::EShutdown;
       
   143 		interestedInChange = ETrue;
       
   144 		}
       
   145 	else if (ssmState.MainState() == ESsmNormal && ssmState.SubState() == ESsmNormalSubState)
       
   146 		{
       
   147 		aState = MASSrvSystemStateObserver::ENormal;
       
   148 		interestedInChange = ETrue;
       
   149 		}
       
   150 	
       
   151 	return interestedInChange;
       
   152 	}
       
   153 
       
   154 void CASSrvSystemState::AcknowledgeAndRequestStateNotification()
       
   155 	{
       
   156 	if (iWorkAround)
       
   157 		{
       
   158 		// Send an acknoledgement back to the test code
       
   159 		// that we have finished actioning the state notification
       
   160 		RProperty::Set(KCategoryWorkAroundUID, KKeyWorkAroundAck, 3);
       
   161 		iWorkAroundProperty.Subscribe(iStatus);
       
   162 		}
       
   163 	else
       
   164 		{
       
   165 		// Acknowledge this state change and request notification
       
   166 		// for the next state change
       
   167 		iStateAwareSession.AcknowledgeAndRequestStateNotification(KErrNone, iStatus);
       
   168 		}
       
   169 	
       
   170 	SetActive();
       
   171 	}
       
   172