datasourcemodules/defaultpositioningmodule/src/epos_cpospsyfixstatemanager.cpp
changeset 0 9cfd9a3ee49c
child 52 29dbbeac905d
equal deleted inserted replaced
-1:000000000000 0:9cfd9a3ee49c
       
     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 
       
    17 
       
    18 // INCLUDE FILES
       
    19 #include "epos_cpospsyfixstatemanager.h"
       
    20 #include "epos_cpospsylisthandler.h"
       
    21 #include "epos_cpossingletonmanager.h"
       
    22 #include "epos_cposfixstatetimer.h"
       
    23 #include "epos_defaultproxycommon.h"
       
    24 #include "epos_cposconstmanager.h"
       
    25 #include "epos_mpospsyfixstatelistener.h"
       
    26 
       
    27 // ================= MEMBER FUNCTIONS =======================
       
    28 
       
    29 // C++ default constructor can NOT contain any code, that
       
    30 // might leave.
       
    31 //
       
    32 CPosPsyFixStateManager::CPosPsyFixStateManager()
       
    33     {
       
    34     }
       
    35 
       
    36 // EPOC default constructor can leave.
       
    37 void CPosPsyFixStateManager::ConstructL()
       
    38     {
       
    39     TRACESTRING( "CPosPsyFixStateManager::ConstructL start... " )
       
    40 
       
    41     iConstManager = CPosConstManager::GetInstanceL();
       
    42 
       
    43     TRACESTRING( "CPosPsyFixStateManager::ConstructL end " )
       
    44 
       
    45     }
       
    46 
       
    47 // ---------------------------------------------------------
       
    48 // CPosPsyFixStateManager::GetInstanceL
       
    49 // ---------------------------------------------------------
       
    50 //
       
    51 CPosPsyFixStateManager* CPosPsyFixStateManager::GetInstanceL()
       
    52     {
       
    53     CPosPsyFixStateManager* self = reinterpret_cast < CPosPsyFixStateManager* > 
       
    54         ( CPosSingletonManager::GetObject( 
       
    55             EPosSigletonObjectIdPsyFixStateManager ) );
       
    56         
       
    57     if ( !self )
       
    58         {
       
    59         //Construct a new object and store it to CPosSingletonManager
       
    60         self = new ( ELeave ) CPosPsyFixStateManager;
       
    61         CleanupStack::PushL(self);
       
    62         self->ConstructL();
       
    63         CPosSingletonManager::SetObjectL(
       
    64             self,
       
    65             EPosSigletonObjectIdPsyFixStateManager );
       
    66         CleanupStack::Pop(self);
       
    67         }
       
    68         
       
    69     self->iRefCount++;
       
    70     
       
    71     return self;
       
    72     }
       
    73 
       
    74 // Destructor
       
    75 CPosPsyFixStateManager::~CPosPsyFixStateManager()
       
    76     {
       
    77     TRACESTRING( "CPosPsyFixStateManager::destructor start... " )
       
    78 
       
    79     if ( iConstManager )
       
    80         {
       
    81         iConstManager->ReleaseInstance();
       
    82         }
       
    83         
       
    84     //Clear fix state timer
       
    85     TInt count = iPsyStateArray.Count();
       
    86     for ( TInt i=0; i<count; i++)
       
    87         {
       
    88         delete iPsyStateArray[i].iTimer;
       
    89         }
       
    90         
       
    91     iPsyStateArray.Close();
       
    92     iListenerArray.Close();
       
    93 
       
    94     TRACESTRING( "CPosPsyFixStateManager::destructor end... " )
       
    95     }
       
    96 
       
    97 // ---------------------------------------------------------
       
    98 // CPosPsyFixStateManager::ReleaseInstance
       
    99 // ---------------------------------------------------------
       
   100 //
       
   101 void CPosPsyFixStateManager::ReleaseInstance()
       
   102     {
       
   103     iRefCount--;
       
   104     if ( iRefCount == 0 )
       
   105         {
       
   106         //We shall delete the instance
       
   107         CPosSingletonManager::ReleaseObject(
       
   108             EPosSigletonObjectIdPsyFixStateManager );
       
   109         }
       
   110     }
       
   111 
       
   112 // ---------------------------------------------------------
       
   113 // CPosPsyFixStateManager::AddListenerL
       
   114 // ---------------------------------------------------------
       
   115 //
       
   116 void CPosPsyFixStateManager::AddListenerL( MPosPsyFixStateListener* aListener )
       
   117     {
       
   118     User::LeaveIfError( iListenerArray.Append( aListener ) );
       
   119     }
       
   120         
       
   121 // ---------------------------------------------------------
       
   122 // CPosPsyFixStateManager::RemoveListener
       
   123 // ---------------------------------------------------------
       
   124 //
       
   125 void CPosPsyFixStateManager::RemoveListener( MPosPsyFixStateListener* aListener )
       
   126     {
       
   127     TInt count = iListenerArray.Count();
       
   128     for ( TInt i=count -1; i>=0; i-- )
       
   129         {
       
   130         if( iListenerArray[i] == aListener )
       
   131             {
       
   132             iListenerArray.Remove( i );
       
   133             }
       
   134         }
       
   135     }
       
   136 
       
   137 // ---------------------------------------------------------
       
   138 // CPosPsyFixStateManager::PsyLoadedL
       
   139 // ---------------------------------------------------------
       
   140 //
       
   141 void CPosPsyFixStateManager::PsyLoadedL(
       
   142             TPositionModuleId aPsyId )
       
   143     {
       
   144     TRACESTRING( "CPosPsyFixStateManager::PsyLoadedL" )
       
   145     TRACESTRING2( "PSY: %x", aPsyId )    
       
   146     if ( PsyIndex( aPsyId ) == KErrNotFound )
       
   147         {
       
   148         //If PSY is not in the PSY state array, add PSY to 
       
   149         //the array and set the fix state as unkown.
       
   150         CPosFixStateTimer* timer = CPosFixStateTimer::NewL(
       
   151             aPsyId, 
       
   152             *this );
       
   153         CleanupStack::PushL( timer );
       
   154         
       
   155         TPsyAndFixState fixAndState;
       
   156         fixAndState.iPsyId = aPsyId;
       
   157         fixAndState.iPsyState = EPsyFixStateUnknown;
       
   158         fixAndState.iLocationRequestCount = 0;
       
   159         fixAndState.iTimer = timer;
       
   160         User::LeaveIfError( 
       
   161             iPsyStateArray.Append( fixAndState ) );
       
   162             
       
   163         CleanupStack::Pop( timer );
       
   164         }
       
   165     }
       
   166         
       
   167 // ---------------------------------------------------------
       
   168 // CPosPsyFixStateManager::GetPsyFixState
       
   169 // ---------------------------------------------------------
       
   170 //
       
   171 CPosPsyFixStateManager::TPsyFixState CPosPsyFixStateManager::GetPsyFixState(
       
   172             TPositionModuleId aPsyId )
       
   173     {
       
   174     TInt index = PsyIndex( aPsyId );
       
   175     if ( index != KErrNotFound )
       
   176         {
       
   177         return iPsyStateArray[index].iPsyState;
       
   178         }
       
   179         
       
   180     //If PSY has not been loaded before, return fix state
       
   181     //as unknown.
       
   182     return EPsyFixStateUnknown;
       
   183     }
       
   184 
       
   185 // ---------------------------------------------------------
       
   186 // CPosPsyFixStateManager::PsyUsed
       
   187 // ---------------------------------------------------------
       
   188 //
       
   189 void CPosPsyFixStateManager::PsyUsed( 
       
   190         TPositionModuleId aPsyId,
       
   191         TPositionModuleStatus::TDeviceStatus aPreviousDeviceStatus )
       
   192     {
       
   193     TRACESTRING( "CPosPsyFixStateManager::PsyUsed" )
       
   194     TRACESTRING2( "PSY: %x", aPsyId )
       
   195     TRACESTRING2( "Device status: %d", aPreviousDeviceStatus)
       
   196 
       
   197     TInt index = PsyIndex( aPsyId );
       
   198     __ASSERT_DEBUG( index != KErrNotFound,
       
   199         DefaultProxyPanic( EDefaultProxyPanic_PsyNotFoundInStateManager ) );
       
   200     
       
   201     if( index != KErrNotFound )
       
   202         {
       
   203         TPsyAndFixState& fixState = iPsyStateArray[index];
       
   204         //If PSY has not been used before, then start timeshift timer
       
   205         if( fixState.iLocationRequestCount == 0 )
       
   206             {
       
   207             StartTimeshiftTimer( fixState, aPreviousDeviceStatus );
       
   208             }
       
   209             
       
   210         //Cancel unknown timer
       
   211         fixState.iTimer->CancelUnknownTimer();
       
   212             
       
   213         fixState.iLocationRequestCount++;
       
   214         }
       
   215     }
       
   216 
       
   217 
       
   218 // ---------------------------------------------------------
       
   219 // CPosPsyFixStateManager::SetPsyFixState
       
   220 // ---------------------------------------------------------
       
   221 //
       
   222 void CPosPsyFixStateManager::SetPsyFixState( 
       
   223             TPositionModuleId aPsyId, 
       
   224             TInt aLRErr  )
       
   225     {
       
   226     TRACESTRING( "CPosPsyFixStateManager::SetPsyFixState" )
       
   227     TRACESTRING2( "PSY: %x", aPsyId )
       
   228     TRACESTRING2( "Err: %d", aLRErr )
       
   229 
       
   230     TInt index = PsyIndex( aPsyId );
       
   231     if ( index != KErrNotFound )
       
   232         {
       
   233         TPsyAndFixState& fixState = iPsyStateArray[index];
       
   234 
       
   235         TPsyFixState previousFixState = fixState.iPsyState;
       
   236              
       
   237         switch ( aLRErr )
       
   238             {
       
   239             case KErrNone:
       
   240                 //Location request completed successfully.
       
   241                 //Stop timeshift timer
       
   242                 fixState.iPsyState = EPsyFixStateYes;
       
   243                 fixState.iTimer->CancelTimeshiftTimer();
       
   244                 break;
       
   245             case KPositionPartialUpdate:
       
   246             case KErrPositionBufferOverflow:
       
   247             case KErrCancel:
       
   248                 //State not changed.
       
   249                 //Leave timeshift timer running
       
   250                 break;
       
   251             default:
       
   252                 //Stop timeshift timer
       
   253                 fixState.iPsyState = EPsyFixStateNo;
       
   254                 fixState.iTimer->CancelTimeshiftTimer();
       
   255                 break;
       
   256             }
       
   257 
       
   258         //Notify liseners
       
   259         NotifyListener( aPsyId, previousFixState );
       
   260         
       
   261 
       
   262         //If the decrease the LR count for this PSY
       
   263         if( fixState.iLocationRequestCount > 0 )
       
   264             {
       
   265             fixState.iLocationRequestCount--;
       
   266 
       
   267             //Check if the PSY is not used anymore
       
   268             if ( fixState.iLocationRequestCount == 0 )
       
   269                 {
       
   270                 //if the PSY is not used anymore, start
       
   271                 //unknown timer
       
   272                 fixState.iTimer->StartUnknownTimer(
       
   273                     iConstManager->GetUnusedTimeoutValue() );
       
   274                 }
       
   275             }
       
   276                 
       
   277         }
       
   278     //If the PSY has not been loaded, we don't do anything.
       
   279     }
       
   280 
       
   281 // ---------------------------------------------------------
       
   282 // CPosPsyFixStateManager::SetExtGpsPsyFixState
       
   283 // ---------------------------------------------------------
       
   284 //
       
   285 void CPosPsyFixStateManager::SetExtGpsPsyFixState(
       
   286             TPositionModuleId aPsyId, 
       
   287             TInt aLRErr )
       
   288     {
       
   289     TRACESTRING( "CPosPsyFixStateManager::SetExtGpsPsyFixState" )
       
   290     TRACESTRING2( "PSY: %x", aPsyId )
       
   291     TRACESTRING2( "Err: %d", aLRErr )
       
   292 
       
   293     TInt index = PsyIndex( aPsyId );
       
   294     if ( index != KErrNotFound )
       
   295         {
       
   296         if ( aLRErr == KErrNone )
       
   297             {
       
   298             TPsyFixState previousFixState = iPsyStateArray[index].iPsyState;
       
   299             iPsyStateArray[index].iPsyState = EPsyFixStateYes;
       
   300             NotifyListener( aPsyId, previousFixState );
       
   301             }
       
   302         //otherwise the state will not be changed.
       
   303         }
       
   304     }
       
   305 
       
   306 // ---------------------------------------------------------
       
   307 // CPosPsyFixStateManager::TimeshiftTimerTimeout
       
   308 // ---------------------------------------------------------
       
   309 //
       
   310 void CPosPsyFixStateManager::TimeshiftTimerTimeout( 
       
   311             TPositionModuleId aPsyId )
       
   312     {
       
   313     TRACESTRING( "CPosPsyFixStateManager::TimeshiftTimerTimeout" )
       
   314     TRACESTRING2( "PSY: %x", aPsyId )
       
   315 
       
   316     TInt index = PsyIndex( aPsyId );
       
   317     if ( index != KErrNotFound )
       
   318         {
       
   319         TPsyFixState previousFixState = iPsyStateArray[index].iPsyState;
       
   320 
       
   321         iPsyStateArray[index].iPsyState = EPsyFixStateNo;
       
   322         
       
   323         TRACESTRING2( "PSY fix sate: %d", 
       
   324             iPsyStateArray[index].iPsyState );
       
   325             
       
   326         //Notify listner that PSY fix state is changed
       
   327         NotifyListener( aPsyId, previousFixState );
       
   328         }
       
   329     }
       
   330             
       
   331 // ---------------------------------------------------------
       
   332 // CPosPsyFixStateManager::UnknownTimerTimeout
       
   333 // ---------------------------------------------------------
       
   334 //
       
   335 void CPosPsyFixStateManager::UnknownTimerTimeout(
       
   336             TPositionModuleId aPsyId )
       
   337     {
       
   338     TRACESTRING( "CPosPsyFixStateManager::UnknownTimerTimeout" )
       
   339     TRACESTRING2( "PSY: %x", aPsyId )
       
   340 
       
   341     TInt index = PsyIndex( aPsyId );
       
   342     if ( index != KErrNotFound )
       
   343         {
       
   344         TPsyFixState previousFixState = iPsyStateArray[index].iPsyState;
       
   345         
       
   346         //Cancel timeshift timer
       
   347         iPsyStateArray[index].iTimer->CancelTimeshiftTimer();
       
   348 
       
   349         iPsyStateArray[index].iPsyState = EPsyFixStateUnknown;
       
   350         
       
   351         TRACESTRING2( "PSY fix sate: %d", 
       
   352             iPsyStateArray[index].iPsyState );
       
   353             
       
   354         //Notify listner that PSY fix state is changed
       
   355         NotifyListener( aPsyId, previousFixState );
       
   356         }
       
   357     }
       
   358 
       
   359 // ---------------------------------------------------------
       
   360 // CPosPsyFixStateManager::PsyIndex
       
   361 // ---------------------------------------------------------
       
   362 //
       
   363 TInt CPosPsyFixStateManager::PsyIndex(
       
   364             TPositionModuleId aPsyId )
       
   365     {
       
   366     TInt count = iPsyStateArray.Count();
       
   367     for ( TInt i = 0; i < count; i++ )
       
   368         {
       
   369         if ( iPsyStateArray[i].iPsyId == aPsyId )
       
   370             {
       
   371             return i;
       
   372             }
       
   373         }
       
   374     return KErrNotFound;
       
   375     }
       
   376 
       
   377 // ---------------------------------------------------------
       
   378 // CPosPsyFixStateManager::StartTimeshiftTimer
       
   379 // ---------------------------------------------------------
       
   380 //
       
   381 void CPosPsyFixStateManager::StartTimeshiftTimer(
       
   382             TPsyAndFixState& aFixState,
       
   383             TPositionModuleStatus::TDeviceStatus aPreviousDeviceStatus )
       
   384     {
       
   385     TTimeIntervalMicroSeconds ttff;
       
   386     TTimeIntervalMicroSeconds ttnf;
       
   387     CPosPsyListHandler* listHandler = NULL;
       
   388     TInt ignore = KErrNone;
       
   389     TRAP( ignore, listHandler = 
       
   390             CPosPsyListHandler::GetInstanceL() );
       
   391                 
       
   392     __ASSERT_DEBUG( listHandler != NULL,  
       
   393         DefaultProxyPanic( 
       
   394             EDefaultProxyPanic_GlobalObjectNotConstructed ) );
       
   395         
       
   396     listHandler->GetTtffAndTtnf(
       
   397         aFixState.iPsyId,
       
   398         ttff,
       
   399         ttnf );
       
   400             
       
   401     listHandler->ReleaseInstance();
       
   402     //Start timeshift timer
       
   403     aFixState.iTimer->StartTimeshiftTimer(
       
   404         iConstManager->GetTimeshiftValue(
       
   405             ttff,
       
   406             ttnf,
       
   407             aPreviousDeviceStatus ) );
       
   408     }
       
   409 
       
   410 // ---------------------------------------------------------
       
   411 // CPosPsyFixStateManager::NotifyListener
       
   412 // ---------------------------------------------------------
       
   413 //
       
   414 void CPosPsyFixStateManager::NotifyListener(
       
   415             TPositionModuleId aPsyId,
       
   416             TPsyFixState aPreviousFixState )
       
   417     {
       
   418     TInt index = PsyIndex( aPsyId );
       
   419     if ( index != KErrNotFound )
       
   420         {
       
   421         TPsyFixState newState = iPsyStateArray[index].iPsyState;
       
   422         if ( newState != aPreviousFixState )
       
   423             {
       
   424             TInt count = iListenerArray.Count();
       
   425             for ( TInt i = count-1; i >= 0; i-- )
       
   426                 {
       
   427                 iListenerArray[i]->PsyFixStateChanged(
       
   428                     aPsyId,
       
   429                     newState );
       
   430                 }
       
   431             }
       
   432         }
       
   433     }
       
   434 
       
   435 //  End of File