sipplugins/sippsystemstatemonitor/src/sipdevicestateaware.cpp
changeset 0 307788aac0a8
child 26 822e1f077722
equal deleted inserted replaced
-1:000000000000 0:307788aac0a8
       
     1 /*
       
     2 * Copyright (c) 2007 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description:  Class Implementation, aware of Device System State Changes and perform
       
    15 *                processing related to profile registration/deregistration
       
    16 *
       
    17 */
       
    18 
       
    19 
       
    20 #include "sipdevicestateaware.h"
       
    21 #include <sipsystemstateobserver.h>
       
    22 #include <ssm/ssmdomaindefs.h>
       
    23 #include <ssm/ssmstate.h>
       
    24 #include <ssm/ssmsubstates.hrh>
       
    25 #include <e32std.h> 
       
    26 
       
    27 static const TInt KMicroSecondsInSecond = 1000000;
       
    28 static const TInt KGuardTimerSeconds = 5;
       
    29 
       
    30 // -----------------------------------------------------------------------------
       
    31 // CSipDeviceStateAware::NewL
       
    32 // -----------------------------------------------------------------------------
       
    33 //
       
    34 CSipDeviceStateAware* CSipDeviceStateAware::NewL ()
       
    35     {
       
    36     CSipDeviceStateAware* self = new (ELeave) CSipDeviceStateAware();
       
    37     CleanupStack::PushL (self);
       
    38     self->ConstructL ();
       
    39     CleanupStack::Pop(self);
       
    40     return self;
       
    41     }
       
    42 
       
    43 // -----------------------------------------------------------------------------
       
    44 // CSipDeviceStateAware::ConstructL
       
    45 // -----------------------------------------------------------------------------
       
    46 //
       
    47 void CSipDeviceStateAware::ConstructL()
       
    48 	{
       
    49 	iGuardTimer = CPeriodic::NewL( EPriorityNormal );
       
    50     //SIP System connects to Domain Id GenMiddlewareDomain3    
       
    51     User::LeaveIfError(iStateAwareSession.Connect(KSM2GenMiddlewareDomain3));
       
    52     TSsmState currentState = iStateAwareSession.State();
       
    53     if(ESsmNormal == currentState.MainState())
       
    54         {
       
    55         iState = CSipSystemStateMonitor::ESystemReady;
       
    56         }
       
    57     iStateAwareSession.RequestStateNotification(iStatus);        
       
    58     SetActive();
       
    59     }
       
    60 
       
    61 // -----------------------------------------------------------------------------
       
    62 // CSipDeviceStateAware::CSipDeviceStateAware
       
    63 // -----------------------------------------------------------------------------
       
    64 //
       
    65 CSipDeviceStateAware::CSipDeviceStateAware():CActive(EPriorityStandard)
       
    66     {    
       
    67     CActiveScheduler::Add(this);
       
    68 	iState = CSipSystemStateMonitor::ESystemNotReady;
       
    69     iCount = 0;
       
    70     }
       
    71 
       
    72 // -----------------------------------------------------------------------------
       
    73 // CSipDeviceStateAware::~CSipDeviceStateAware
       
    74 // -----------------------------------------------------------------------------
       
    75 //
       
    76 CSipDeviceStateAware::~CSipDeviceStateAware ()
       
    77     {
       
    78      iGuardTimer->Cancel();
       
    79      delete iGuardTimer;
       
    80      CActive::Cancel();     
       
    81      iObservers.Close();
       
    82     }
       
    83 
       
    84 // -----------------------------------------------------------------------------
       
    85 // CSipDeviceStateAware::RunL
       
    86 // Currently this object only monitor for basically three events, Offline, online
       
    87 // and shutdown. 
       
    88 // -----------------------------------------------------------------------------
       
    89 //
       
    90 void CSipDeviceStateAware::RunL()
       
    91     {
       
    92 	TBool ackAndRequest = ETrue;
       
    93     TInt status = iStatus.Int();
       
    94     if(KErrNone == status)
       
    95         {
       
    96             TSsmState currentState = iStateAwareSession.State();
       
    97             if(ESsmNormal == currentState.MainState() )
       
    98                 {                
       
    99                 switch (currentState.SubState())
       
   100                     {
       
   101                     // Device is entering into Offline Mode. Hence as part
       
   102                     // of staged offline, SIP tries to deregister all profiles.
       
   103                     case ESsmNormalRfOffSubState:
       
   104                         {
       
   105 						if(iState == CSipSystemStateMonitor::ESystemOnline)
       
   106 							{
       
   107                        		iState = CSipSystemStateMonitor::ESystemOffline;
       
   108                        		NotifyObservers(CSipSystemStateMonitor::ESystemOffline);
       
   109 							iGuardTimer->Cancel();
       
   110 							TTimeIntervalMicroSeconds32 time( KGuardTimerSeconds * KMicroSecondsInSecond );
       
   111 							iGuardTimer->Start( time, time, TCallBack( TimerExpired, this) );                                        
       
   112 							ackAndRequest = EFalse;
       
   113 							}
       
   114                         break;
       
   115                         }
       
   116                     //Device entering into online mode, SIP tries to register all
       
   117                     //profiles with registration mode 'always on'
       
   118                     case ESsmNormalRfOnSubState:
       
   119                         {
       
   120 						if(iState == CSipSystemStateMonitor::ESystemOffline)
       
   121 							{
       
   122                        		NotifyObservers(CSipSystemStateMonitor::ESystemOnline);
       
   123 							}
       
   124                        	iState = CSipSystemStateMonitor::ESystemOnline;
       
   125                         break;
       
   126                         }
       
   127                     default:
       
   128                         {                        
       
   129                         break;
       
   130                         }
       
   131                     }//end switch                
       
   132                 } //end if
       
   133 			}//end outer if
       
   134 	if(ackAndRequest)
       
   135     	iStateAwareSession.AcknowledgeAndRequestStateNotification(KErrNone, iStatus);
       
   136 	else
       
   137 		iStateAwareSession.RequestStateNotification(iStatus);
       
   138     SetActive();
       
   139     }
       
   140 
       
   141 // -----------------------------------------------------------------------------
       
   142 // CSipDeviceStateAware::RunError
       
   143 // -----------------------------------------------------------------------------
       
   144 //     
       
   145 TInt CSipDeviceStateAware::RunError( TInt /*aError*/ )
       
   146     {
       
   147     return KErrNone; // RunL cannot leave at the moment
       
   148     }
       
   149 
       
   150 // -----------------------------------------------------------------------------
       
   151 // CSipDeviceStateAware::DoCancel
       
   152 // -----------------------------------------------------------------------------
       
   153 //
       
   154 void CSipDeviceStateAware::DoCancel()
       
   155     {
       
   156     iStateAwareSession.Close();
       
   157     }
       
   158 
       
   159 // -----------------------------------------------------------------------------
       
   160 // CSipDeviceStateAware::AddObserverL
       
   161 // -----------------------------------------------------------------------------
       
   162 //
       
   163 void CSipDeviceStateAware::AddObserverL(MSipSystemStateObserver& aObserver)
       
   164     {
       
   165     iObservers.InsertInAddressOrderL( &aObserver );
       
   166     }
       
   167 
       
   168 // -----------------------------------------------------------------------------
       
   169 // CSipDeviceStateAware::RemoveObserver
       
   170 // -----------------------------------------------------------------------------
       
   171 //
       
   172 void CSipDeviceStateAware::RemoveObserver(MSipSystemStateObserver& aObserver)
       
   173     {
       
   174     TInt index = iObservers.Find( &aObserver );
       
   175     if ( index >= 0 )
       
   176         {
       
   177         iObservers.Remove( index ); 
       
   178         } 
       
   179     }
       
   180 
       
   181 // -----------------------------------------------------------------------------
       
   182 // CSipDeviceStateAware::NotifyObservers
       
   183 // This function will notify all the observers when the state of the phone changes
       
   184 // -----------------------------------------------------------------------------
       
   185 //
       
   186 void CSipDeviceStateAware::NotifyObservers(
       
   187 				CSipSystemStateMonitor::TSystemState aState) const
       
   188     {
       
   189     for ( TInt i = iObservers.Count()-1; i >= 0; i-- )
       
   190             {
       
   191             iObservers[i]->SystemVariableUpdated( 
       
   192                     CSipSystemStateMonitor::ESystemState, 
       
   193                     0,
       
   194                     aState );
       
   195             }
       
   196     }
       
   197 
       
   198 // -----------------------------------------------------------------------------
       
   199 // CSipDeviceStateAware::TimerExpired
       
   200 // -----------------------------------------------------------------------------
       
   201 //
       
   202 TInt CSipDeviceStateAware::TimerExpired(TAny* aSelf)
       
   203     {
       
   204 	CSipDeviceStateAware* self = reinterpret_cast<CSipDeviceStateAware*>(aSelf);
       
   205     self->EventProcessingCompleted();
       
   206     return ETrue;
       
   207     }
       
   208 
       
   209 // -----------------------------------------------------------------------------
       
   210 // CSipDeviceStateAware::EventProcessingCompleted
       
   211 // -----------------------------------------------------------------------------
       
   212 //
       
   213 void CSipDeviceStateAware::EventProcessingCompleted(
       
   214         MSipSystemStateObserver& aObserver )
       
   215     {
       
   216     TInt index = iObservers.Find( &aObserver );
       
   217     if ( index >= 0 )
       
   218         iCount++;
       
   219     if( iObservers.Count() == iCount)
       
   220         {
       
   221         iGuardTimer->Cancel();
       
   222         iStateAwareSession.AcknowledgeStateNotification(KErrNone);        
       
   223         iCount = 0;        
       
   224         }	
       
   225     }
       
   226 
       
   227 // -----------------------------------------------------------------------------
       
   228 // CSipDeviceStateAware::EventProcessingCompleted
       
   229 // -----------------------------------------------------------------------------
       
   230 //
       
   231 void CSipDeviceStateAware::EventProcessingCompleted()
       
   232     {
       
   233     iGuardTimer->Cancel();
       
   234     iStateAwareSession.AcknowledgeStateNotification(KErrNone);   
       
   235     iCount = 0;
       
   236     }
       
   237