locationtriggering/ltstrategyengine/src/lbtcellsupervisor.cpp
changeset 0 667063e416a2
child 7 19bff11d6c18
equal deleted inserted replaced
-1:000000000000 0:667063e416a2
       
     1 /*
       
     2 * Copyright (c) 2008 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:  Implementation of cell supervisor class
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 
       
    20 #include <lbttriggerconditionarea.h>
       
    21 #include <lbtgeohybrid.h>
       
    22 
       
    23 #include "lbtcellsupervisor.h"
       
    24 #include "lbtlogger.h"
       
    25 #include "flogger.h"
       
    26 
       
    27 // CONSTANTS
       
    28 
       
    29 static const TInt KCellHysterisis = 2;
       
    30 
       
    31 
       
    32 // ======== MEMBER FUNCTIONS ========
       
    33 
       
    34 // --------------------------------------------------------------------------
       
    35 // CLbtCellSupervisor::NewL
       
    36 // CLbtCellSupervisor instantiation method
       
    37 // --------------------------------------------------------------------------
       
    38 //
       
    39 CLbtCellSupervisor* CLbtCellSupervisor::NewL( TAny* aConstructionParameters, 
       
    40                                               CLbtTriggerView* aView,
       
    41                                               MLbtStrategyEngineObsrvr* aObserver,
       
    42                                               CLbtCellChangeHandler* aCellChangeHandler)
       
    43     {
       
    44     CLbtCellSupervisor* self = new ( ELeave ) CLbtCellSupervisor();
       
    45     CleanupStack::PushL( self );
       
    46     self->ConstructL( aConstructionParameters, aView,aObserver,aCellChangeHandler );
       
    47     CleanupStack::Pop();
       
    48     return self;
       
    49     }
       
    50 
       
    51 
       
    52 // -----------------------------------------------------------------------------
       
    53 // CLbtCellSupervisor::~CLbtCellSupervisor
       
    54 // Default destructor
       
    55 // -----------------------------------------------------------------------------
       
    56 //
       
    57 CLbtCellSupervisor::~CLbtCellSupervisor()
       
    58     {
       
    59     // This is just done as a precautionary measure!
       
    60     StopSupervision();
       
    61     
       
    62     iRecentlyFiredTriggerArray.Close();
       
    63     delete iCellSupervisorTimer;
       
    64     }	
       
    65 
       
    66 // -----------------------------------------------------------------------------
       
    67 // CLbtCellSupervisor::CLbtCellSupervisor
       
    68 // Default destructor
       
    69 // -----------------------------------------------------------------------------
       
    70 //
       
    71 CLbtCellSupervisor::CLbtCellSupervisor()
       
    72     {
       
    73     
       
    74     }
       
    75     
       
    76 // -----------------------------------------------------------------------------
       
    77 // CLbtCellSupervisor::StartSupervision
       
    78 // 
       
    79 // -----------------------------------------------------------------------------
       
    80 //
       
    81 void CLbtCellSupervisor::StartSupervision()
       
    82     {
       
    83     FUNC_ENTER("CLbtCellSupervisor::StartSupervision");
       
    84     iCellChangeHandler->SetObserver( this );
       
    85 	}
       
    86 
       
    87     
       
    88 // -----------------------------------------------------------------------------
       
    89 // CLbtCellSupervisor::StopSupervision
       
    90 // -----------------------------------------------------------------------------
       
    91 //
       
    92 void CLbtCellSupervisor::StopSupervision()
       
    93     {
       
    94     FUNC_ENTER("CLbtCellSupervisor::StopSupervision");
       
    95     iCellChangeHandler->Remove( this );
       
    96     iCellSupervisorTimer->Cancel();
       
    97     }
       
    98     
       
    99 
       
   100 // -----------------------------------------------------------------------------
       
   101 // CLbtCellSupervisor::SettingsChanged
       
   102 // -----------------------------------------------------------------------------
       
   103 //
       
   104 void CLbtCellSupervisor::SettingsChanged()
       
   105     {
       
   106     iEngObserver->RetrieveTriggeringSettings( iSettings );
       
   107     }
       
   108 
       
   109 
       
   110 // -----------------------------------------------------------------------------
       
   111 // CLbtCellSupervisor::Resume
       
   112 // -----------------------------------------------------------------------------
       
   113 //
       
   114 void CLbtCellSupervisor::Resume()
       
   115     {
       
   116     iRecentlyFiredTriggerArray.Reset();
       
   117     FUNC_ENTER("CLbtCellSupervisor::Resume");
       
   118     // Iterate through view and all recently fired triggers triggers
       
   119     if( iView->CellTriggerCount() )
       
   120         {
       
   121         iViewIterator.Reset();
       
   122         
       
   123         CLbtContainerTriggerEntry* conTrigger;
       
   124         
       
   125         TBool isPresent = ETrue;
       
   126         while( isPresent )
       
   127             {
       
   128             conTrigger = NULL;
       
   129             TRAP_IGNORE( isPresent = iViewIterator.NextL( conTrigger ) );
       
   130             if( conTrigger )
       
   131                 {
       
   132                 // Fetch strategy data.
       
   133                 TStrategyData strategyData = conTrigger->StrategyData();
       
   134                 
       
   135                 // If the fired time is non-zero, add it to recently fired trigger list
       
   136                 if( strategyData.iFireTime )
       
   137                     {
       
   138                     iRecentlyFiredTriggerArray.Append( conTrigger );
       
   139                     }
       
   140                 }
       
   141             }
       
   142         }
       
   143       
       
   144     iCellChangeHandler->SetObserver( this );
       
   145     }
       
   146 
       
   147 // -----------------------------------------------------------------------------
       
   148 // CLbtCellSupervisor::Reset
       
   149 // -----------------------------------------------------------------------------
       
   150 //
       
   151 void CLbtCellSupervisor::Reset()
       
   152     {
       
   153     // TODO: Check if this needed.
       
   154     }
       
   155 
       
   156 
       
   157 // -----------------------------------------------------------------------------
       
   158 // CLbtCellSupervisor::ConstructL
       
   159 // Symbian 2nd phase constructor.
       
   160 // -----------------------------------------------------------------------------
       
   161 //
       
   162 void CLbtCellSupervisor::ConstructL( TAny* /*aConstructionParameters*/,
       
   163                                      CLbtTriggerView* aView,
       
   164                                      MLbtStrategyEngineObsrvr* aObserver,
       
   165                                      CLbtCellChangeHandler* aCellChangeHandler )
       
   166     {
       
   167     iEngObserver = aObserver;
       
   168 	//iEngObserver->RetrieveTriggeringSettings( iSettings );
       
   169 	iView = aView;
       
   170 	
       
   171 	iCellChangeHandler = aCellChangeHandler;
       
   172 	iViewIterator = iView->BeginL( CLbtGeoAreaBase::ECellular,CLbtGeoAreaBase::EHybrid );
       
   173 	iCellSupervisorTimer = CLbtCellSupervisorTimer::NewL( *this );
       
   174 	}
       
   175 	
       
   176 		
       
   177 // ----------------------------------------------------------------------------
       
   178 // CLbtCellSupervisor::HandleCellChangeEvent
       
   179 // ----------------------------------------------------------------------------
       
   180 //
       
   181 void CLbtCellSupervisor::HandleCellChangeEvent(const TInt aStatus,
       
   182                                                const RMobilePhone::TMobilePhoneNetworkInfoV1& aNwInfo,
       
   183                                                const RMobilePhone::TMobilePhoneLocationAreaV1& aLocArea)
       
   184     {    
       
   185     FUNC_ENTER("CLbtCellSupervisor::HandleCellChangeEvent");
       
   186     if( KErrNone == aStatus )
       
   187 	    {
       
   188 	    // validate network info
       
   189 		if (aNwInfo.iMode == RMobilePhone::ENetworkModeGsm ||
       
   190             aNwInfo.iMode == RMobilePhone::ENetworkModeWcdma)
       
   191             {
       
   192             TLex lex( aNwInfo.iCountryCode );
       
   193             lex.Val( iCurrentCountryCode );
       
   194             if( iCurrentCountryCode )
       
   195                 {
       
   196                 lex.Assign( aNwInfo.iNetworkId );
       
   197                 lex.Val( iCurrentNetworkCode );
       
   198                 if( iCurrentNetworkCode )
       
   199                     {
       
   200                     if( aLocArea.iLocationAreaCode && aLocArea.iCellId )
       
   201                         {
       
   202                         iNwInfo = aNwInfo;
       
   203 		                iLocArea = aLocArea;
       
   204 		                LOG1("Current cellid:%d", iLocArea.iCellId);
       
   205 		                TRAP_IGNORE(EvaluateTriggersL());
       
   206                         }
       
   207                     }
       
   208                 }
       
   209             }
       
   210 		}
       
   211     }
       
   212     
       
   213     
       
   214 // ----------------------------------------------------------------------------
       
   215 // CLbtCellSupervisor::EvaluateTriggersL
       
   216 // ----------------------------------------------------------------------------
       
   217 //
       
   218 void CLbtCellSupervisor::EvaluateTriggersL()
       
   219     {
       
   220     FUNC_ENTER("CLbtCellSupervisor::EvaluateTriggersL");
       
   221     CLbtContainerTriggerEntry* conTrigger;
       
   222     
       
   223     iViewIterator.Reset();
       
   224     
       
   225     while( iViewIterator.NextL( conTrigger ) )
       
   226         {
       
   227         if( conTrigger )    
       
   228             {
       
   229             CLbtTriggerEntry* trigger = conTrigger->TriggerEntry();
       
   230             if( CLbtTriggerConditionBase::ETriggerConditionArea == 
       
   231                                                 trigger->GetCondition()->Type() )
       
   232                 {
       
   233                 // Area type trigger
       
   234                 CLbtTriggerConditionArea* triggerConArea = 
       
   235                                         static_cast< CLbtTriggerConditionArea* >
       
   236                                                 ( trigger->GetCondition() );
       
   237                    
       
   238                 if( triggerConArea->TriggerArea()->Type() == CLbtGeoAreaBase::ECellular )
       
   239                     {
       
   240                     EvalCellularTrigger( conTrigger );
       
   241                     }
       
   242                 else if( triggerConArea->TriggerArea()->Type() == CLbtGeoAreaBase::EHybrid )
       
   243                     {
       
   244                     EvalHybridTrigger( conTrigger );
       
   245                     }
       
   246                 }
       
   247             }
       
   248         }
       
   249     
       
   250     EstimateSleepInterval();    
       
   251     }
       
   252 
       
   253 
       
   254 // ----------------------------------------------------------------------------
       
   255 // CLbtCellSupervisor::EvalCellularTrigger
       
   256 // ----------------------------------------------------------------------------
       
   257 //
       
   258 void CLbtCellSupervisor::EvalCellularTrigger( CLbtContainerTriggerEntry* aTrigger )
       
   259     {
       
   260     FUNC_ENTER("CLbtCellSupervisor::EvalCellularTrigger");    
       
   261     
       
   262     CLbtTriggerConditionArea* trgCondition = 
       
   263                               static_cast< CLbtTriggerConditionArea* >
       
   264                               ( ( aTrigger->TriggerEntry() )->GetCondition() );
       
   265                 
       
   266     CLbtGeoCell* cellularArea = static_cast< CLbtGeoCell* >
       
   267                                                ( trgCondition->TriggerArea() );
       
   268 
       
   269 
       
   270     // Fetch strategy data.
       
   271     TStrategyData strategyData = aTrigger->StrategyData();
       
   272     
       
   273     // Calculate remaining rearm time
       
   274     TInt remRearmTime = CalculateRearmTime( strategyData,aTrigger->TriggerEntry()->TimeToRearm());
       
   275     
       
   276     // If rearm time is not expired, trigger is not supervised.
       
   277     if( remRearmTime > 0 )
       
   278         {
       
   279         return;        
       
   280         }
       
   281     	
       
   282     LOGSTRING("Trigger:\"%S\" ", &(aTrigger->TriggerEntry()->Name()));
       
   283     LOG1("CID:%d", cellularArea->CellId());
       
   284 
       
   285     // ENTRY type trigger
       
   286     if( CLbtTriggerConditionArea::EFireOnEnter == trgCondition->Direction() )
       
   287         {
       
   288         TLbtTriggerDataMask dataMask = 0;
       
   289         if( CheckForCurrentCGIMatch( cellularArea ) )
       
   290             {
       
   291             if( EActive == strategyData.iActivityStatus )
       
   292                 {
       
   293                 if( aTrigger->ExtendedTriggerInfo()->IsTriggerFireOnCreation() )
       
   294                     {
       
   295                     LOG("TriggerFireOnCreation");
       
   296                     HandleTriggerFired(aTrigger,strategyData,dataMask);                         
       
   297                     }
       
   298                 else
       
   299                     {
       
   300                     strategyData.iActivityStatus = EInactive;
       
   301                     aTrigger->ExtendedTriggerInfo()->SetTriggerFireOnCreation( ETrue );
       
   302                     dataMask |= CLbtContainerTriggerEntry::EContainerAttributeIsTriggerFireOnCreation;
       
   303                     }
       
   304                 }
       
   305             }
       
   306         else
       
   307         	{
       
   308             LOG("Check Visited Cell Count");
       
   309             
       
   310             if( VisitedCellCount( strategyData) <KCellHysterisis )
       
   311               	{
       
   312                 LOG("visit count < Hysteresis");
       
   313                 
       
   314               	if( iLocArea.iCellId && !FindCellId( strategyData,iLocArea.iCellId )  )
       
   315                		{
       
   316                		AddCellId( strategyData,iLocArea.iCellId );
       
   317                		
       
   318                	    if( VisitedCellCount( strategyData) >= KCellHysterisis )
       
   319                         {
       
   320 			            LOG("ACTIVATING TRIGGER !!!!!");		            
       
   321                         strategyData.iActivityStatus = EActive;
       
   322                         }
       
   323                     }
       
   324                 }
       
   325              }
       
   326         
       
   327         if(dataMask)
       
   328         	{
       
   329         	TPositionInfo dummy;  
       
   330 	        // TODO : Remove dummy position info
       
   331 	        iView->UpdateTriggerInfo( CLbtGeoAreaBase::ECellular,dummy, aTrigger, dataMask );
       
   332         	}        
       
   333         }
       
   334     
       
   335     // EXIT Trigger    
       
   336     else
       
   337         {
       
   338         
       
   339         }
       
   340     aTrigger->SetStrategyData( strategyData );    
       
   341     }
       
   342 
       
   343 
       
   344 // ----------------------------------------------------------------------------
       
   345 // CLbtCellSupervisor::EvalHybridTrigger
       
   346 // ----------------------------------------------------------------------------
       
   347 //
       
   348 void CLbtCellSupervisor::EvalHybridTrigger( CLbtContainerTriggerEntry* aTrigger )
       
   349     {
       
   350     FUNC_ENTER("CLbtCellSupervisor::EvalHybridTrigger");    
       
   351         
       
   352     CLbtTriggerConditionArea* trgCondition = 
       
   353                               static_cast< CLbtTriggerConditionArea* >
       
   354                               ( ( aTrigger->TriggerEntry() )->GetCondition() );
       
   355                 
       
   356     CLbtGeoHybrid* hybridArea = static_cast< CLbtGeoHybrid* >
       
   357                                                ( trgCondition->TriggerArea() );
       
   358                                                
       
   359     // Fetch strategy data.
       
   360     TStrategyData strategyData = aTrigger->StrategyData();
       
   361    
       
   362     // Calculate remaining rearm time
       
   363     TInt remRearmTime = CalculateRearmTime( strategyData,aTrigger->TriggerEntry()->TimeToRearm());
       
   364    
       
   365     // If rearm time is not expired, skip trigger supervision.
       
   366     if( remRearmTime > 0 )
       
   367        {
       
   368        return;        
       
   369        }
       
   370     
       
   371     RPointerArray<CLbtGeoAreaBase> hybridArray = hybridArea->HybridArea();
       
   372     TBool cellMatch = EFalse;
       
   373     for( TInt i=0;i<hybridArray.Count();i++ )
       
   374         {
       
   375         if( hybridArray[i]->Type() == CLbtGeoAreaBase::ECellular )
       
   376             {
       
   377             CLbtGeoCell* cellularArea = static_cast<CLbtGeoCell*> ( hybridArray[i] );
       
   378             if( CheckForCurrentCGIMatch( cellularArea ) )
       
   379                 {
       
   380                 cellMatch = ETrue;
       
   381                 break;
       
   382                 }
       
   383             }
       
   384         }
       
   385     
       
   386     // ENTRY type trigger
       
   387     if( CLbtTriggerConditionArea::EFireOnEnter == trgCondition->Direction() )
       
   388         {
       
   389         TLbtTriggerDataMask dataMask = 0;
       
   390         if( cellMatch )
       
   391             {
       
   392             if( EActive == strategyData.iActivityStatus )
       
   393                 {
       
   394                 if( aTrigger->ExtendedTriggerInfo()->IsTriggerFireOnCreation() )
       
   395                     {
       
   396                     LOG("TriggerFireOnCreation");
       
   397                     HandleTriggerFired(aTrigger,strategyData,dataMask);                         
       
   398                     }
       
   399                 else
       
   400                     {
       
   401                     strategyData.iActivityStatus = EInactive;
       
   402                     aTrigger->ExtendedTriggerInfo()->SetTriggerFireOnCreation( ETrue );
       
   403                     dataMask |= CLbtContainerTriggerEntry::EContainerAttributeIsTriggerFireOnCreation;
       
   404                     }
       
   405                 }
       
   406             }
       
   407         else
       
   408             {
       
   409             LOG("Check Visited Cell Count");
       
   410             
       
   411             if( VisitedCellCount( strategyData) <KCellHysterisis )
       
   412                 {
       
   413                 LOG("visit count < Hysteresis");
       
   414                 
       
   415                 if( iLocArea.iCellId && !FindCellId( strategyData,iLocArea.iCellId )  )
       
   416                     {
       
   417                     AddCellId( strategyData,iLocArea.iCellId );
       
   418                     
       
   419                     if( VisitedCellCount( strategyData) >= KCellHysterisis )
       
   420                         {
       
   421                         LOG("ACTIVATING TRIGGER !!!!!");                    
       
   422                         strategyData.iActivityStatus = EActive;
       
   423                         }
       
   424                     }
       
   425                 }
       
   426              }
       
   427         
       
   428         if(dataMask)
       
   429             {
       
   430             TPositionInfo dummy;  
       
   431             // TODO : Remove dummy position info
       
   432             iView->UpdateTriggerInfo( CLbtGeoAreaBase::ECellular,dummy, aTrigger, dataMask );
       
   433             }        
       
   434         }
       
   435     
       
   436     // EXIT Trigger    
       
   437     else
       
   438         {
       
   439         
       
   440         }
       
   441     aTrigger->SetStrategyData( strategyData );
       
   442     }
       
   443 
       
   444 
       
   445 // ----------------------------------------------------------------------------
       
   446 // CLbtCellSupervisor::CheckForCurrentCGIMatch
       
   447 // ----------------------------------------------------------------------------
       
   448 //
       
   449 TBool CLbtCellSupervisor::CheckForCurrentCGIMatch( CLbtGeoCell* aCellularArea )
       
   450     {
       
   451     TInt trCountryCode,trNetworkCode;
       
   452     
       
   453     TLex lex;
       
   454     lex.Assign( aCellularArea->NetworkCountryCode() );
       
   455     lex.Val( trCountryCode );
       
   456     lex.Assign( aCellularArea->NetworkIdentityCode() );
       
   457     lex.Val( trNetworkCode );
       
   458     
       
   459     if( aCellularArea->NetworkType() == iNwInfo.iMode &&
       
   460         iCurrentCountryCode == trCountryCode  && 
       
   461         iCurrentNetworkCode == trNetworkCode &&
       
   462         aCellularArea->LocationAreaCode() == iLocArea.iLocationAreaCode &&
       
   463         aCellularArea->CellId() == iLocArea.iCellId )
       
   464         {
       
   465         return ETrue;
       
   466         }
       
   467     return EFalse;
       
   468     }
       
   469 
       
   470 // ----------------------------------------------------------------------------
       
   471 // CLbtCellSupervisor::CalculateRearmTime
       
   472 // ----------------------------------------------------------------------------
       
   473 //
       
   474 TInt CLbtCellSupervisor::CalculateRearmTime( TStrategyData& aStrategyData, TInt aTimeToRearm )
       
   475     {
       
   476     TInt remRearmTime = 0;
       
   477     if( aStrategyData.iFireTime )
       
   478        {
       
   479        TTime triggerFiredTime( aStrategyData.iFireTime );
       
   480 
       
   481        TTime currentTime;
       
   482        currentTime.UniversalTime();
       
   483        
       
   484        TTimeIntervalSeconds interval;
       
   485        currentTime.SecondsFrom( triggerFiredTime, interval );    
       
   486            
       
   487        remRearmTime = aTimeToRearm - interval.Int();
       
   488        if( remRearmTime <= 0 )
       
   489            {
       
   490            aStrategyData.iFireTime = 0;        
       
   491            }
       
   492        }
       
   493     return remRearmTime;
       
   494     }
       
   495 
       
   496 
       
   497 // ----------------------------------------------------------------------------
       
   498 // CLbtCellSupervisor::HandleTriggerFired
       
   499 // ----------------------------------------------------------------------------
       
   500 //
       
   501 void CLbtCellSupervisor::HandleTriggerFired( CLbtContainerTriggerEntry* aTrigger, TStrategyData& aStrategyData, 
       
   502                                                         TLbtTriggerDataMask& aDataMask )
       
   503 	{
       
   504 	FUNC_ENTER("CLbtCellSupervisor::HandleTriggerFired");
       
   505 	aTrigger->ExtendedTriggerInfo()->SetTriggerFiredState( ETrue );
       
   506                             
       
   507     TLbtTriggerFireInfo triggerFireInfo;
       
   508     triggerFireInfo.iAreaType = CLbtGeoAreaBase::ECellular;
       
   509     triggerFireInfo.iTriggerId = aTrigger->TriggerEntry()->Id();
       
   510     LOG("Trigger fired:");
       
   511     LOG1("%d",aTrigger->TriggerEntry()->Id());                        
       
   512     aTrigger->ExtendedTriggerInfo()->SetFiredInfo( triggerFireInfo );
       
   513      
       
   514     // Time at which trigger fired
       
   515     TTime triggerFiredTime;
       
   516     triggerFiredTime.UniversalTime();
       
   517     
       
   518     aDataMask |= CLbtContainerTriggerEntry::EContainerAttributeIsFired | 
       
   519 	    		 CLbtContainerTriggerEntry::EContainerAttributeFireInfo;
       
   520     
       
   521     aStrategyData.iFireTime =  triggerFiredTime.Int64();
       
   522         
       
   523     // Set trigger to inactive state
       
   524     aStrategyData.iActivityStatus = EInactive;
       
   525     ClearVisitedCellInfo( aStrategyData );
       
   526     
       
   527     // Append this trigger to recently fired trigger
       
   528     iRecentlyFiredTriggerArray.Append( aTrigger );
       
   529 	}
       
   530 
       
   531 
       
   532 
       
   533 // ----------------------------------------------------------------------------
       
   534 // CLbtCellSupervisor::EstimateSleepInterval
       
   535 // ----------------------------------------------------------------------------
       
   536 //    
       
   537 void CLbtCellSupervisor::EstimateSleepInterval()    
       
   538     {
       
   539     FUNC_ENTER("CLbtCellSupervisor::EstimateSleepInterval");
       
   540     TInt minimumSleep = 0;
       
   541     
       
   542     TTime currentTime;
       
   543     currentTime.UniversalTime();
       
   544     
       
   545     for( TInt i=0;i<iRecentlyFiredTriggerArray.Count();i++ )
       
   546         {
       
   547         TInt remRearmTime =0;
       
   548         
       
   549         TStrategyData strategyData = iRecentlyFiredTriggerArray[i]->StrategyData();
       
   550         
       
   551         if( strategyData.iFireTime )
       
   552             {
       
   553             TTime firedTime( strategyData.iFireTime );
       
   554             TTimeIntervalSeconds interval;
       
   555             currentTime.SecondsFrom( firedTime,interval );
       
   556              
       
   557             remRearmTime = iRecentlyFiredTriggerArray[i]->TriggerEntry()->TimeToRearm() - interval.Int();
       
   558             }
       
   559         
       
   560         if( !minimumSleep || minimumSleep > remRearmTime )
       
   561             {
       
   562             minimumSleep = remRearmTime;
       
   563             }
       
   564         }
       
   565     
       
   566     LOG1("Sleep interval:%d",minimumSleep);    
       
   567     if( minimumSleep > 0 )
       
   568         {
       
   569         if( iCellSupervisorTimer->IsActive() )    
       
   570             {
       
   571             if( minimumSleep < iCellSupervisorTimer->RemainingTime() )
       
   572                 {
       
   573                 iCellSupervisorTimer->InterruptSleep( minimumSleep );
       
   574                 }
       
   575             }
       
   576         else
       
   577             {
       
   578             iCellSupervisorTimer->SetTimer( minimumSleep );
       
   579             }
       
   580         }
       
   581     }
       
   582 
       
   583 // ----------------------------------------------------------------------------
       
   584 // CLbtCellSupervisor::TimeOut
       
   585 // ----------------------------------------------------------------------------
       
   586 //    
       
   587 void CLbtCellSupervisor::TimeOut()
       
   588     {
       
   589     FUNC_ENTER("CLbtCellSupervisor::TimeOut");
       
   590     RPointerArray<CLbtContainerTriggerEntry> activatedTriggers;
       
   591     
       
   592     TTime currentTime;
       
   593     currentTime.UniversalTime();
       
   594     
       
   595     for( TInt i=0;i<iRecentlyFiredTriggerArray.Count();i++ )
       
   596         {
       
   597         TInt remRearmTime = 0;
       
   598         
       
   599         TStrategyData strategyData = iRecentlyFiredTriggerArray[i]->StrategyData();
       
   600         
       
   601         if( strategyData.iFireTime )
       
   602             {
       
   603             TTime firedTime( strategyData.iFireTime );
       
   604             TTimeIntervalSeconds interval;
       
   605             currentTime.SecondsFrom( firedTime,interval );
       
   606              
       
   607             remRearmTime = iRecentlyFiredTriggerArray[i]->TriggerEntry()->TimeToRearm() - interval.Int();
       
   608             
       
   609             if( remRearmTime <= 0 )
       
   610                 {
       
   611                 strategyData.iFireTime = 0;
       
   612                 iRecentlyFiredTriggerArray[i]->SetStrategyData( strategyData );
       
   613                 activatedTriggers.Append( iRecentlyFiredTriggerArray[i] );
       
   614                 iRecentlyFiredTriggerArray.Remove(i);
       
   615                 }
       
   616             }
       
   617             
       
   618         }
       
   619     // Supervise recently activated for current cell. 
       
   620     if( activatedTriggers.Count() )   
       
   621         {
       
   622         EvaluateActivatedTriggers( activatedTriggers ); 
       
   623         }
       
   624     activatedTriggers.Close();   
       
   625     
       
   626     EstimateSleepInterval();
       
   627     }
       
   628 
       
   629 // ----------------------------------------------------------------------------
       
   630 // CLbtCellSupervisor::EvaluateActivatedTriggers
       
   631 // ----------------------------------------------------------------------------
       
   632 //
       
   633 void CLbtCellSupervisor::EvaluateActivatedTriggers( RPointerArray<CLbtContainerTriggerEntry>& 
       
   634                                                     aActivatedTriggers )
       
   635     {
       
   636     for( TInt i=0;i<aActivatedTriggers.Count();i++ )
       
   637         {
       
   638         CLbtContainerTriggerEntry* conTrigger = aActivatedTriggers[i];
       
   639         
       
   640         CLbtTriggerEntry* trigger = conTrigger->TriggerEntry();
       
   641         if( CLbtTriggerConditionBase::ETriggerConditionArea == 
       
   642                                             trigger->GetCondition()->Type() )
       
   643             {
       
   644             // Area type trigger
       
   645             CLbtTriggerConditionArea* triggerConArea = 
       
   646                                     static_cast< CLbtTriggerConditionArea* >
       
   647                                             ( trigger->GetCondition() );
       
   648                
       
   649             if( triggerConArea->TriggerArea()->Type() == CLbtGeoAreaBase::ECellular )
       
   650                 {
       
   651                 EvalCellularTrigger( conTrigger );
       
   652                 }
       
   653             else if( triggerConArea->TriggerArea()->Type() == CLbtGeoAreaBase::EHybrid )
       
   654                 {
       
   655                 EvalHybridTrigger( conTrigger );
       
   656                 }
       
   657             }
       
   658         }
       
   659     // These are recently fired triggers, hysterisis condition will surely fail for these triggers.
       
   660     // This evaluation is just to keep track of visited cell.
       
   661     }    
       
   662 
       
   663 // ----------------------------------------------------------------------------
       
   664 // CLbtCellSupervisor::VisitedCellCount
       
   665 // ----------------------------------------------------------------------------
       
   666 //
       
   667 TInt CLbtCellSupervisor::VisitedCellCount( TStrategyData& aStrategyData )
       
   668     {
       
   669     TInt count = 0;
       
   670     
       
   671     for( TInt i=0;i<KVisitedCellSize;i++ )
       
   672         {
       
   673         if( aStrategyData.iVisitedCell[i] )
       
   674             {
       
   675             count++;
       
   676             }
       
   677         }
       
   678     return count;    
       
   679     }
       
   680     
       
   681 // ----------------------------------------------------------------------------
       
   682 // CLbtCellSupervisor::FindCellId
       
   683 // ----------------------------------------------------------------------------
       
   684 //
       
   685 TBool CLbtCellSupervisor::FindCellId( TStrategyData& aStrategyData,TInt aCellId )
       
   686     {
       
   687     for( TInt i=0;i<KVisitedCellSize;i++ )
       
   688         {
       
   689         if( aStrategyData.iVisitedCell[i] == aCellId )
       
   690             {
       
   691             return ETrue;
       
   692             }
       
   693         }
       
   694     return EFalse;    
       
   695     }
       
   696 
       
   697 // ----------------------------------------------------------------------------
       
   698 // CLbtCellSupervisor::AddCellId
       
   699 // ----------------------------------------------------------------------------
       
   700 //
       
   701 void CLbtCellSupervisor::AddCellId( TStrategyData& aStrategyData,TInt aCellId )
       
   702     {
       
   703     for( TInt i=0;i<KVisitedCellSize;i++ )
       
   704         {
       
   705         if( aStrategyData.iVisitedCell[i] == 0 )
       
   706             {
       
   707             aStrategyData.iVisitedCell[i] = aCellId;
       
   708             return;
       
   709             }
       
   710         }
       
   711     }
       
   712 // ----------------------------------------------------------------------------
       
   713 // CLbtCellSupervisor::ClearVisitedCellInfo
       
   714 // ----------------------------------------------------------------------------
       
   715 //
       
   716 void CLbtCellSupervisor::ClearVisitedCellInfo( TStrategyData& aStrategyData )
       
   717 	{ 
       
   718 	for( TInt i=0;i<KVisitedCellSize;i++ )
       
   719         {
       
   720         aStrategyData.iVisitedCell[i] = 0;
       
   721         }	
       
   722     }       
       
   723 // end of file
       
   724