datasourcemodules/defaultpositioningmodule/src/epos_cposexternalgpsmonitor.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_defaultproxycommon.h"
       
    20 #include "epos_cposexternalgpsmonitor.h"
       
    21 #include "epos_cpospsylisthandler.h"
       
    22 #include "epos_cpospsyfixstatemanager.h"
       
    23 #include "epos_cpossingletonmanager.h"
       
    24 #include "epos_cposrequestor.h"
       
    25 #include "epos_cposconstmanager.h"
       
    26 #include "epos_defaultproxycommon.h"
       
    27 
       
    28 #include <lbs/epos_cpositioner.h>
       
    29 
       
    30 
       
    31 
       
    32 // ================= MEMBER FUNCTIONS =======================
       
    33 
       
    34 // C++ default constructor can NOT contain any code, that
       
    35 // might leave.
       
    36 //
       
    37 CPosExternalGpsMonitor::CPosExternalGpsMonitor(
       
    38         MPosModuleStatusManager& aModuleStatusManager )
       
    39     : iModuleStatusManager( aModuleStatusManager )
       
    40     {
       
    41     }
       
    42 
       
    43 // EPOC default constructor can leave.
       
    44 void CPosExternalGpsMonitor::ConstructL()
       
    45     {
       
    46     TRACESTRING( "CPosExternalGpsMonitor::ConstructL start... " )
       
    47 
       
    48     iPsyListHandler = CPosPsyListHandler::GetInstanceL();
       
    49     iPsyListHandler->AddListenerL( this );
       
    50 
       
    51     iFixStateManager = CPosPsyFixStateManager::GetInstanceL();
       
    52     iConstManager = CPosConstManager::GetInstanceL();
       
    53     
       
    54     iTimer = CPeriodic::NewL( CActive::EPriorityStandard );
       
    55 
       
    56     TRACESTRING( "CPosExternalGpsMonitor::ConstructL end" )
       
    57     }
       
    58 
       
    59 // ---------------------------------------------------------
       
    60 // CPosExternalGpsMonitor::GetInstanceL
       
    61 // ---------------------------------------------------------
       
    62 //
       
    63 CPosExternalGpsMonitor* CPosExternalGpsMonitor::GetInstanceL(
       
    64         CPositioner& aDefaultPositioner,
       
    65         MPosModuleStatusManager& aModuleStatusManager )
       
    66     {
       
    67     CPosExternalGpsMonitor* self = reinterpret_cast < CPosExternalGpsMonitor* > 
       
    68         ( CPosSingletonManager::GetObject( 
       
    69             EPosSigletonObjectExtGpsMonitorId ) );
       
    70         
       
    71     if ( !self )
       
    72         {
       
    73         //Construct a new object and store it to CPosSingletonManager
       
    74         self = new ( ELeave ) CPosExternalGpsMonitor(
       
    75             aModuleStatusManager );
       
    76         CleanupStack::PushL(self);
       
    77         self->ConstructL();
       
    78         CPosSingletonManager::SetObjectL(
       
    79             self,
       
    80             EPosSigletonObjectExtGpsMonitorId );
       
    81         self->iDefaultPositionerArray.AppendL( &aDefaultPositioner );
       
    82         self->ConstructExtGpsPsyArrayL();
       
    83         CleanupStack::Pop(self);
       
    84         }
       
    85     else
       
    86         {
       
    87         self->iDefaultPositionerArray.AppendL( &aDefaultPositioner );
       
    88         }
       
    89         
       
    90     self->iRefCount++;
       
    91     
       
    92     return self;
       
    93     }
       
    94 
       
    95 // Destructor
       
    96 CPosExternalGpsMonitor::~CPosExternalGpsMonitor()
       
    97     {
       
    98     TRACESTRING( "CPosExternalGpsMonitor::destructor start" )
       
    99 
       
   100     if ( iPsyListHandler )
       
   101         {
       
   102         iPsyListHandler->RemoveListener( this );
       
   103         iPsyListHandler->ReleaseInstance();
       
   104         }
       
   105         
       
   106     if ( iFixStateManager )
       
   107         {
       
   108         iFixStateManager->ReleaseInstance();
       
   109         }
       
   110         
       
   111     if ( iConstManager )
       
   112         {
       
   113         iConstManager->ReleaseInstance();
       
   114         }
       
   115     
       
   116     iExtGpsPsyArray.ResetAndDestroy();
       
   117     iExtGpsPsyArray.Close();
       
   118     iExtGpsPsyUsedArray.Close();
       
   119     iDefaultPositionerArray.Close();
       
   120     delete iTimer;
       
   121     TRACESTRING( "CPosExternalGpsMonitor::destructor end" )
       
   122     }
       
   123 
       
   124 // ---------------------------------------------------------
       
   125 // CPosExternalGpsMonitor::ReleaseInstance
       
   126 // ---------------------------------------------------------
       
   127 //
       
   128 void CPosExternalGpsMonitor::ReleaseInstance(
       
   129         CPositioner& aDefaultPositioner )
       
   130     {
       
   131     TInt index = iDefaultPositionerArray.Find( &aDefaultPositioner );
       
   132     if ( index != KErrNotFound )
       
   133         {
       
   134         iDefaultPositionerArray.Remove( index );
       
   135         }
       
   136         
       
   137     iRefCount--;
       
   138     if ( iRefCount == 0 )
       
   139         {
       
   140         //We shall delete the instance
       
   141         CPosSingletonManager::ReleaseObject(
       
   142             EPosSigletonObjectExtGpsMonitorId );
       
   143         }
       
   144     else
       
   145 		{
       
   146 		// Delete & reconstruct iExtGpsPsyArray
       
   147 		iExtGpsPsyArray.ResetAndDestroy();
       
   148 		iExtGpsPsyArray.Close();  
       
   149 				
       
   150 		TInt ignore = KErrNone;
       
   151 		TRAP( ignore, ConstructExtGpsPsyArrayL() ); //error ignored
       
   152 		}		    
       
   153     }
       
   154 
       
   155 // ---------------------------------------------------------
       
   156 // CPosExternalGpsMonitor::PsyUsed
       
   157 // ---------------------------------------------------------
       
   158 //
       
   159 void CPosExternalGpsMonitor::PsyUsed( TPositionModuleId aPsyId )
       
   160     {
       
   161     TRACESTRING( "CPosExternalGpsMonitor::PsyUsed" )
       
   162     TRACESTRING2( "PSY: %x", aPsyId );
       
   163 
       
   164     TInt index = GetIndexInPsyArray( aPsyId );
       
   165     //iExtGpsPsyArray and iExtGpsPsyUsedArray are saved in the same
       
   166     //order. 
       
   167     if ( index != KErrNotFound )
       
   168         {
       
   169         iExtGpsPsyUsedArray[index]++;
       
   170         }
       
   171         
       
   172     //Location request received
       
   173     iIsLoacationRequestMade = ETrue;
       
   174     
       
   175     //Update state of Ext GPS PSY used
       
   176     iIsExtGpsPsyUsed = IsExtGpsPsyInUse();
       
   177         
       
   178     //Check if we should start timer
       
   179     if ( iExtGpsPsyArray.Count() > 0 &&
       
   180         !iPsyListHandler->IsFirstGpsPsyExternal() && 
       
   181         !IsExtGpsPsyInUse() )
       
   182         {
       
   183         //We start timer if there are GPS PSY availabe, but
       
   184         //the first GPS PSY in the list is not located on external
       
   185         //device. 
       
   186         //If a external GPS PSY is already used, then we don't
       
   187         //start the timer
       
   188         
       
   189         //Start timer if it's not started yet
       
   190         StartTimer();
       
   191         }
       
   192     }
       
   193 
       
   194 // ---------------------------------------------------------
       
   195 // CPosExternalGpsMonitor::PsyNotUsed
       
   196 // ---------------------------------------------------------
       
   197 //
       
   198 void CPosExternalGpsMonitor::PsyNotUsed( TPositionModuleId aPsyId )
       
   199     {
       
   200     TRACESTRING( "CPosExternalGpsMonitor::PsyNotUsed" )
       
   201     TRACESTRING2( "PSY: %x", aPsyId );
       
   202 
       
   203     TInt index = GetIndexInPsyArray( aPsyId );
       
   204     //iExtGpsPsyArray and iExtGpsPsyUsedArray are saved in the same
       
   205     //order. 
       
   206     if ( index != KErrNotFound )
       
   207         {
       
   208         if ( iExtGpsPsyUsedArray[index] > 0 )
       
   209             {
       
   210             iExtGpsPsyUsedArray[index]--;
       
   211             }
       
   212         }
       
   213     }
       
   214 
       
   215 // ---------------------------------------------------------
       
   216 // CPosExternalGpsMonitor::ConstructExtGpsPsyArrayL
       
   217 // ---------------------------------------------------------
       
   218 //
       
   219 void CPosExternalGpsMonitor::ConstructExtGpsPsyArrayL()
       
   220     {
       
   221     TRACESTRING( "CPosExternalGpsMonitor::ConstructExtGpsPsyArrayL" )
       
   222 
       
   223     RArray< TPositionModuleId > extGpsPsyIdArray;
       
   224     CleanupClosePushL( extGpsPsyIdArray );
       
   225     iPsyListHandler->GetExtGpsPsyListL( extGpsPsyIdArray );
       
   226     
       
   227     TInt i;
       
   228     
       
   229     //Remove those modules that are not in the settings
       
   230     TInt positionerCount = iExtGpsPsyArray.Count();
       
   231     
       
   232     //Reserve enough space for iExtGpsPsyUsedArray first
       
   233     iExtGpsPsyUsedArray.ReserveL( positionerCount );
       
   234     
       
   235     for ( i = positionerCount-1; i >= 0 ; i-- )
       
   236         {
       
   237         TInt index = extGpsPsyIdArray.Find(
       
   238             iExtGpsPsyArray[i]->ModuleId() );
       
   239         if ( index == KErrNotFound )
       
   240             {
       
   241             delete iExtGpsPsyArray[i];
       
   242             iExtGpsPsyArray.Remove( i );
       
   243             iExtGpsPsyUsedArray.Remove( i );
       
   244             }
       
   245         else
       
   246             {
       
   247             extGpsPsyIdArray.Remove( index );
       
   248             }
       
   249         }
       
   250         
       
   251     //Add those PSYs that are not in the PSY array        
       
   252     TInt count = extGpsPsyIdArray.Count();
       
   253     
       
   254     if ( iDefaultPositionerArray.Count() > 0 )
       
   255         {
       
   256         for ( i = 0; i < count; i++ )
       
   257             {
       
   258             //Add this module to the list
       
   259             CPosRequestor* requestor = CPosRequestor::NewL(
       
   260                 *iDefaultPositionerArray[0],
       
   261                 extGpsPsyIdArray[i],
       
   262                 *this,
       
   263                 *iFixStateManager,
       
   264                 iModuleStatusManager );
       
   265             CleanupStack::PushL( requestor );
       
   266             
       
   267             //Notify fix state manager that a PSY is loaded
       
   268             iFixStateManager->PsyLoadedL( 
       
   269                 extGpsPsyIdArray[i] );
       
   270                 
       
   271             //Add this requestor in the array
       
   272             User::LeaveIfError( iExtGpsPsyArray.Append( requestor ) );
       
   273             
       
   274             //This function won't fail, since we have enough space already
       
   275             iExtGpsPsyUsedArray.Append( 0 ); 
       
   276             
       
   277             CleanupStack::Pop( requestor );
       
   278             }
       
   279         }
       
   280     CleanupStack::PopAndDestroy( &extGpsPsyIdArray );
       
   281     }
       
   282 
       
   283 // ---------------------------------------------------------
       
   284 // CPosExternalGpsMonitor::LocationRequestCompleted
       
   285 // ---------------------------------------------------------
       
   286 //
       
   287 void CPosExternalGpsMonitor::LocationRequestCompleted( 
       
   288             TPositionModuleId aModuleId,
       
   289             TInt aErr, 
       
   290             const TPositionInfoBase& /*aPosInfo*/,
       
   291             TBool /*aIsPosInfoUpToDate*/ )
       
   292     {
       
   293     TRACESTRING( "CPosExternalGpsMonitor::LocationRequestCompleted" )
       
   294     TRACESTRING2( "PSY: %x", aModuleId )
       
   295 
       
   296     iFixStateManager->SetExtGpsPsyFixState( 
       
   297         aModuleId,
       
   298         aErr );
       
   299     }
       
   300 
       
   301 // ---------------------------------------------------------
       
   302 // CPosExternalGpsMonitor::ExtPsyCheckingTimeoutCallback
       
   303 // ---------------------------------------------------------
       
   304 //
       
   305 TInt CPosExternalGpsMonitor::ExtPsyCheckingTimeoutCallback( TAny* aAny )
       
   306     {
       
   307     TRACESTRING( "CPosExternalGpsMonitor::ExtPsyCheckingTimeoutCallback" )
       
   308 
       
   309     reinterpret_cast < CPosExternalGpsMonitor* > ( aAny ) ->
       
   310         ExtPsyCheckingTimeout();
       
   311     return KErrNone;
       
   312     }
       
   313         
       
   314 // ---------------------------------------------------------
       
   315 // CPosExternalGpsMonitor::ExtPsyCheckingTimeout
       
   316 // ---------------------------------------------------------
       
   317 //
       
   318 void CPosExternalGpsMonitor::ExtPsyCheckingTimeout()
       
   319     {
       
   320     if ( !iIsLoacationRequestMade )
       
   321         {
       
   322         //Stop timer and cancel all location requests
       
   323         iTimer->Cancel();
       
   324         CancelAllLocationRequests();
       
   325         }
       
   326     else
       
   327         {
       
   328         //If there is location request during this period, but 
       
   329         //external GPS has not been tried, then we make location
       
   330         //request to external GPS PSYs.
       
   331         if ( !iIsExtGpsPsyUsed )
       
   332             {
       
   333             MakeLocationRequests();
       
   334             }
       
   335         else
       
   336             {
       
   337             //If external GPS PSY has been used during this period,
       
   338             //We don't need to make location request. All outgoing
       
   339             //location request will be canceled.
       
   340             CancelAllLocationRequests();
       
   341             }
       
   342         }
       
   343     iIsLoacationRequestMade = EFalse;
       
   344     }
       
   345 
       
   346 // ---------------------------------------------------------
       
   347 // CPosExternalGpsMonitor::PsyListChanged
       
   348 // ---------------------------------------------------------
       
   349 //
       
   350 void CPosExternalGpsMonitor::PsyListChanged( 
       
   351             const TPosPsyListChangeEvent& aEvent )
       
   352     {
       
   353     TRACESTRING( "CPosExternalGpsMonitor::PsyListChanged" )
       
   354 
       
   355     if ( aEvent.iType == EPosPsyListChangeEventPriorityChanged )
       
   356         {
       
   357         //do nothing
       
   358         return;
       
   359         }
       
   360         
       
   361     if ( aEvent.iType == EPosPsyListChangeEventPsyDeleted )
       
   362         {
       
   363         CancelLocationRequest( aEvent.iPsyId );
       
   364         }
       
   365         
       
   366     //Reconstruct Ext GPS PSY array
       
   367     TInt ignore = KErrNone;
       
   368     TRAP( ignore, ConstructExtGpsPsyArrayL() ); //error ignored
       
   369     }
       
   370 
       
   371 // ---------------------------------------------------------
       
   372 // CPosExternalGpsMonitor::MakeLocationRequests
       
   373 // ---------------------------------------------------------
       
   374 //
       
   375 void CPosExternalGpsMonitor::MakeLocationRequests()
       
   376     {
       
   377     TRACESTRING( "CPosExternalGpsMonitor::MakeLocationRequests" )
       
   378 
       
   379     TInt count = iExtGpsPsyArray.Count();
       
   380     for ( TInt i = 0; i < count; i++ )
       
   381         {
       
   382         //If location request is not started
       
   383         if ( !iExtGpsPsyArray[i]->IsActive() )
       
   384             {
       
   385             TInt ignore = KErrNone;
       
   386             TRAP( ignore, 
       
   387                 iExtGpsPsyArray[i]->MakeLocationRequestL(iPosInfo) );
       
   388             TRACESTRING2( "PSY: %x", iExtGpsPsyArray[i]->ModuleId() )
       
   389             }
       
   390         }
       
   391     }
       
   392 
       
   393 // ---------------------------------------------------------
       
   394 // CPosExternalGpsMonitor::CancelAllLocationRequests
       
   395 // ---------------------------------------------------------
       
   396 //
       
   397 void CPosExternalGpsMonitor::CancelAllLocationRequests()
       
   398     {
       
   399     TInt count = iExtGpsPsyArray.Count();
       
   400     for ( TInt i = 0; i < count; i++ )
       
   401         {
       
   402         iExtGpsPsyArray[i]->Cancel();
       
   403         }
       
   404     }
       
   405 
       
   406 // ---------------------------------------------------------
       
   407 // CPosExternalGpsMonitor::CancelLocationRequest
       
   408 // ---------------------------------------------------------
       
   409 //
       
   410 void CPosExternalGpsMonitor::CancelLocationRequest( TPositionModuleId aPsyId )
       
   411     {
       
   412     TInt count = iExtGpsPsyArray.Count();
       
   413     for ( TInt i = 0; i < count; i++ )
       
   414         {
       
   415         if ( iExtGpsPsyArray[i]->ModuleId() == aPsyId )
       
   416             {
       
   417             iExtGpsPsyArray[i]->Cancel();
       
   418             }
       
   419         }
       
   420     }
       
   421 
       
   422 // ---------------------------------------------------------
       
   423 // CPosExternalGpsMonitor::GetIndexInPsyArray
       
   424 // ---------------------------------------------------------
       
   425 //
       
   426 TInt CPosExternalGpsMonitor::GetIndexInPsyArray( TPositionModuleId aModuleId ) const
       
   427     {
       
   428     TInt count = iExtGpsPsyArray.Count();
       
   429     for ( TInt i = 0; i < count; i++ )
       
   430         {
       
   431         if ( iExtGpsPsyArray[i]->ModuleId() == aModuleId )
       
   432             {
       
   433             return i;
       
   434             }
       
   435         }
       
   436     return KErrNotFound;
       
   437     }
       
   438 
       
   439 // ---------------------------------------------------------
       
   440 // CPosExternalGpsMonitor::StartTimer
       
   441 // ---------------------------------------------------------
       
   442 //
       
   443 void CPosExternalGpsMonitor::StartTimer()
       
   444     {
       
   445     if ( !iTimer->IsActive() )
       
   446         {
       
   447         iTimer->Start(
       
   448             iConstManager->GetExternalGpsCheckingTimeoutValue().Int64(),
       
   449             iConstManager->GetExternalGpsCheckingTimeoutValue().Int64(),
       
   450             TCallBack( ExtPsyCheckingTimeoutCallback, this ) );
       
   451             
       
   452         iIsLoacationRequestMade = EFalse;
       
   453         iIsExtGpsPsyUsed = IsExtGpsPsyInUse();
       
   454         }
       
   455     }
       
   456 
       
   457 // ---------------------------------------------------------
       
   458 // CPosExternalGpsMonitor::IsExtGpsPsyInUse
       
   459 // ---------------------------------------------------------
       
   460 //
       
   461 TBool CPosExternalGpsMonitor::IsExtGpsPsyInUse() const
       
   462     {
       
   463     TInt count = iExtGpsPsyUsedArray.Count();
       
   464     for ( TInt i = 0; i < count; i++ )
       
   465         {
       
   466         if ( iExtGpsPsyUsedArray[i] > 0 )
       
   467             {
       
   468             return ETrue;
       
   469             }
       
   470         }
       
   471     return EFalse;
       
   472     }
       
   473 
       
   474 //  End of File