datasourcemodules/locationapesuplpsy/src/suplpsyrequestmanager.cpp
changeset 0 9cfd9a3ee49c
equal deleted inserted replaced
-1:000000000000 0:9cfd9a3ee49c
       
     1 /*
       
     2 * Copyright (c) 2005 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:  This is SUPL PSY location request manager
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 
       
    20 // INCLUDE FILES
       
    21 #include <e32std.h>
       
    22 #include <bacntf.h>
       
    23 #include <lbs.h>      
       
    24 #include "suplpsyrequestmanager.h"
       
    25 #include "suplpsyrequestor.h"
       
    26 #include "suplpsyrequestcompletelistener.h"
       
    27 #include "suplpsylogging.h"
       
    28 #include "suplpsypanic.h"
       
    29 #include "suplpsyinfostoremanager.h"
       
    30 
       
    31 
       
    32 // LOCAL CONSTANTS AND MACROS
       
    33 
       
    34 // =========================== == LOCAL FUNCTIONS ===============================
       
    35 
       
    36 // ========================== == MEMBER FUNCTIONS ===============================
       
    37 
       
    38 // -----------------------------------------------------------------------------
       
    39 // CSuplPsyRequestManager::CSuplPsyRequestManager
       
    40 // C++default constructor can NOT contain any code, that
       
    41 // might leave.
       
    42 // -----------------------------------------------------------------------------
       
    43 //
       
    44 CSuplPsyRequestManager::CSuplPsyRequestManager(): iSuplPsyCellIdHandler( NULL ),
       
    45                                                   iSuplPsyInfoStoreManager( NULL ),
       
    46                                                   iMaxAge( 0 )
       
    47     {
       
    48     // Nothing to do here
       
    49     }
       
    50 
       
    51 
       
    52 // -----------------------------------------------------------------------------
       
    53 // CSuplPsyRequestManager::ConstructL
       
    54 // Symbian 2nd phase constructor can leave.
       
    55 // -----------------------------------------------------------------------------
       
    56 //
       
    57 void CSuplPsyRequestManager::ConstructL()
       
    58     {
       
    59     TRACESTRING( "CSuplPsyRequestManager::ConstructL start" )
       
    60 
       
    61     //Construct requestor
       
    62     iRequestor = CSuplPsyRequestor::NewL( 
       
    63         TCallBack( LocationRequestCompleteStatic, this ) );
       
    64 
       
    65     //Construct environment change notifier
       
    66     iEnvChangeNotifier = 
       
    67         CEnvironmentChangeNotifier::NewL( 
       
    68             CActive::EPriorityStandard, 
       
    69             TCallBack( NotifierCallBack, this ) );
       
    70     iSuplPsyCellIdHandler = CSuplPsyCellIdHandler::NewL( *this );
       
    71     
       
    72     iSuplPsyInfoStoreManager = CSuplPsyInfoStoreManager::NewL();
       
    73     
       
    74     iEnvChangeNotifier->Start();
       
    75     TRACESTRING( "CSuplPsyRequestManager::ConstructL end" )
       
    76     }
       
    77 
       
    78 
       
    79 // -----------------------------------------------------------------------------
       
    80 // CSuplPsyRequestManager::RegisterL
       
    81 // -----------------------------------------------------------------------------
       
    82 //
       
    83 CSuplPsyRequestManager* CSuplPsyRequestManager::RegisterL( 
       
    84         MSuplPsyRequestCompleteListener& aPositioner )
       
    85     {
       
    86     CSuplPsyRequestManager* self = reinterpret_cast < CSuplPsyRequestManager* >
       
    87         ( Dll::Tls() );
       
    88 
       
    89     if ( self == NULL )
       
    90         {
       
    91         self = new( ELeave ) CSuplPsyRequestManager;
       
    92     
       
    93         CleanupStack::PushL( self );
       
    94         self->ConstructL();
       
    95         CleanupStack::Pop( self );
       
    96 
       
    97         Dll::SetTls( self );
       
    98         }
       
    99 
       
   100     self->RegisterPositionerL( aPositioner );
       
   101     return self;
       
   102     }
       
   103 
       
   104     
       
   105 // -----------------------------------------------------------------------------
       
   106 // CSuplPsyRequestManager::~CSuplPsyRequestManager
       
   107 // Destructor
       
   108 // -----------------------------------------------------------------------------
       
   109 //
       
   110 CSuplPsyRequestManager::~CSuplPsyRequestManager()
       
   111     {
       
   112     delete iRequestor;
       
   113     
       
   114     iPositionerArray.Reset();
       
   115     iPositionerArray.Close();
       
   116 
       
   117     // Delete environment change notifier
       
   118     if ( iEnvChangeNotifier != NULL )
       
   119         {
       
   120         iEnvChangeNotifier->Cancel();
       
   121         delete iEnvChangeNotifier;
       
   122         }
       
   123 
       
   124     delete iSuplPsyCellIdHandler;
       
   125     iSuplPsyCellIdHandler = NULL;
       
   126     
       
   127     delete iSuplPsyInfoStoreManager;
       
   128     iSuplPsyInfoStoreManager = NULL;
       
   129     TRACESTRING( "CSuplPsyRequestManager:: destructed" )
       
   130     }
       
   131 
       
   132 
       
   133 // -----------------------------------------------------------------------------
       
   134 // CSuplPsyRequestManager::NotifyPositionUpdate
       
   135 // 
       
   136 // -----------------------------------------------------------------------------
       
   137 //
       
   138 void CSuplPsyRequestManager::NotifyPositionUpdate ( 
       
   139         MSuplPsyRequestCompleteListener& aPositioner )
       
   140     {
       
   141     TInt index = FindPositioner( aPositioner );
       
   142     if ( index != KErrNotFound )
       
   143         {
       
   144         iPositionerArray[index].iRequested = ETrue;
       
   145         // Issue request to get current cell id info
       
   146         iSuplPsyCellIdHandler->GetCellID( iCellIdInfo );
       
   147         }
       
   148     }
       
   149 
       
   150 
       
   151 // -----------------------------------------------------------------------------
       
   152 // CSuplPsyRequestManager::CancelNotifyPositionUpdate
       
   153 // 
       
   154 // -----------------------------------------------------------------------------
       
   155 //
       
   156 void CSuplPsyRequestManager::CancelNotifyPositionUpdate ( 
       
   157         MSuplPsyRequestCompleteListener& aPositioner )
       
   158     {
       
   159     TInt index = FindPositioner( aPositioner );
       
   160     if ( index != KErrNotFound )
       
   161         {
       
   162         iPositionerArray[index].iRequested = EFalse;
       
   163 
       
   164         if ( !IsActiveLocationRequest() )
       
   165             {
       
   166             iRequestor->Cancel();
       
   167             }
       
   168         }
       
   169     }
       
   170 
       
   171 
       
   172 
       
   173 // -----------------------------------------------------------------------------
       
   174 // CSuplPsyRequestManager::RegisterPositionerL
       
   175 // -----------------------------------------------------------------------------
       
   176 //
       
   177 void CSuplPsyRequestManager::RegisterPositionerL( 
       
   178         MSuplPsyRequestCompleteListener& aPositioner )
       
   179     {
       
   180     //Panic if the positiner has already registered
       
   181     if ( FindPositioner( aPositioner ) != KErrNotFound )
       
   182         {
       
   183         Panic( EPanicPositionerAlreadyRegistered );
       
   184         }
       
   185 
       
   186     TPositionerStatus status;
       
   187     status.iPositioner = &aPositioner;
       
   188     status.iRequested = EFalse;
       
   189     User::LeaveIfError( iPositionerArray.Append( status ) );
       
   190     }
       
   191 
       
   192 
       
   193 // -----------------------------------------------------------------------------
       
   194 // CSuplPsyRequestManager::FindPositioner
       
   195 // -----------------------------------------------------------------------------
       
   196 //
       
   197 TInt CSuplPsyRequestManager::FindPositioner( 
       
   198         MSuplPsyRequestCompleteListener& aPositioner ) const
       
   199     {
       
   200     TInt count = iPositionerArray.Count();
       
   201     for ( TInt i = 0; i < count; i++ )
       
   202         {
       
   203         if ( iPositionerArray[i].iPositioner == &aPositioner )
       
   204             {
       
   205             return i;
       
   206             }
       
   207         }
       
   208     return KErrNotFound;
       
   209     }
       
   210 
       
   211 // -----------------------------------------------------------------------------
       
   212 // CSuplPsyRequestManager::Unregister
       
   213 // -----------------------------------------------------------------------------
       
   214 //
       
   215 void CSuplPsyRequestManager::Unregister( 
       
   216         MSuplPsyRequestCompleteListener& aPositioner )
       
   217     {
       
   218     TInt index = FindPositioner( aPositioner );
       
   219     if ( index != KErrNotFound )
       
   220         {
       
   221         iPositionerArray.Remove( index );
       
   222         }
       
   223 
       
   224     //Delete this instance if no positioner is registered
       
   225     if ( iPositionerArray.Count() == 0 )
       
   226         {
       
   227         delete this;
       
   228         Dll::SetTls( NULL );
       
   229         }
       
   230     }
       
   231 
       
   232 // -----------------------------------------------------------------------------
       
   233 // CSuplPsyRequestManager::IsActiveLocationRequest
       
   234 // -----------------------------------------------------------------------------
       
   235 //
       
   236 TBool CSuplPsyRequestManager::IsActiveLocationRequest() const
       
   237     {
       
   238     TInt count = iPositionerArray.Count();
       
   239     for ( TInt i = 0; i < count; i++ )
       
   240         {
       
   241         if ( iPositionerArray[i].iRequested )
       
   242             {
       
   243             return ETrue;
       
   244             }
       
   245         }
       
   246     return EFalse;
       
   247     }
       
   248 
       
   249 // -----------------------------------------------------------------------------
       
   250 // CSuplPsyRequestManager::LocationRequestComplete
       
   251 // -----------------------------------------------------------------------------
       
   252 //
       
   253 void CSuplPsyRequestManager::LocationRequestComplete()
       
   254     {
       
   255     // Update the Cell id information from SUPL FW
       
   256     TCellIdInfo cellinfofw;
       
   257     
       
   258     TInt errCIdfw = iSuplPsyInfoStoreManager->ReadSuplFwCellIdInfo( cellinfofw );
       
   259             
       
   260     if( KErrNone == errCIdfw )
       
   261 	    {
       
   262 	    // Log supl fw cid info
       
   263     	TRACESTRING2("supl fw Cid=%d",cellinfofw.iCid )
       
   264     	TRACESTRING2("supl fw Lac=%d",cellinfofw.iLac )
       
   265     	TRACESTRING2("supl fw MCC=%d",cellinfofw.iMCC )
       
   266     	TRACESTRING2("supl fw MNC=%d",cellinfofw.iMNC )	
       
   267 	    TInt er = iSuplPsyInfoStoreManager->WriteSuplPsyCellIdInfo( cellinfofw );//Ignore the error
       
   268 	    TRACESTRING2("cenrep write error=%d",er )
       
   269 	    }
       
   270     
       
   271     
       
   272     // Update the location information
       
   273     iSuplPsyInfoStoreManager->WriteSuplPsyLocationInfo( GetPosition() );//Ignore the error 
       
   274     TInt err = iRequestor->GetRequestResult();
       
   275         
       
   276     //Complete all requests
       
   277     TInt count = iPositionerArray.Count();
       
   278     for ( TInt i = 0; i < count; i++ )
       
   279         {
       
   280         TPositionerStatus& positionerStatus = iPositionerArray[i];
       
   281         if ( positionerStatus.iRequested )
       
   282             {
       
   283             TBool completed = positionerStatus.iPositioner->RequestComplete( 
       
   284                 err,
       
   285                 iRequestor->GetLocation() );
       
   286                 
       
   287             if ( completed )
       
   288                 {
       
   289                 positionerStatus.iRequested = EFalse;
       
   290                 }
       
   291             }
       
   292         }
       
   293     }
       
   294         
       
   295 // -----------------------------------------------------------------------------
       
   296 // CSuplPsyRequestManager::LocationRequestCompleteStatic
       
   297 // -----------------------------------------------------------------------------
       
   298 //
       
   299 TInt CSuplPsyRequestManager::LocationRequestCompleteStatic( TAny* aAny )
       
   300     {
       
   301     reinterpret_cast < CSuplPsyRequestManager* >( aAny )->LocationRequestComplete();
       
   302     return KErrNone;
       
   303     }
       
   304 
       
   305 // -----------------------------------------------------------------------------
       
   306 // CSuplPsyRequestManager::IsLastPostionValid
       
   307 // -----------------------------------------------------------------------------
       
   308 //
       
   309 TBool CSuplPsyRequestManager::IsLastPostionValid( TTime aMaxAge ) const
       
   310     {
       
   311     TRACESTRING( "CSuplPsyRequestManager:: IsLastPostionValid" )
       
   312     TPosition position;
       
   313     iRequestor->GetLocation().GetPosition( position );
       
   314 
       
   315     return ( aMaxAge > 0 && position.Time() >= aMaxAge );
       
   316     }
       
   317     
       
   318 // -----------------------------------------------------------------------------
       
   319 // CSuplPsyRequestManager::GetPosition
       
   320 // -----------------------------------------------------------------------------
       
   321 const HPositionGenericInfo& CSuplPsyRequestManager::GetPosition()
       
   322     {
       
   323     return iRequestor->GetLocation();
       
   324     }
       
   325     
       
   326 
       
   327 // -----------------------------------------------------------------------------
       
   328 // CSuplPsyRequestManager::NotifierCallBack
       
   329 // -----------------------------------------------------------------------------
       
   330 TInt CSuplPsyRequestManager::NotifierCallBack( TAny* aPtr )
       
   331     {
       
   332     reinterpret_cast < CSuplPsyRequestManager* >( aPtr )->HandleEnvironmentChange();
       
   333     return KErrNone;
       
   334     }
       
   335 
       
   336 // -----------------------------------------------------------------------------
       
   337 // CSuplPsyRequestManager:HandleEnvironmentChange
       
   338 // 
       
   339 // Currently we are only interested of system time change. This function is also
       
   340 // called once immediately after enviroment change notifier object is
       
   341 // constructed.
       
   342 //
       
   343 // NOTE: This function can be used to detect changes in phone power supply
       
   344 // in case this information is needed to change the functionality depending of
       
   345 // whether external power supply is connected or not.
       
   346 //
       
   347 // ( other items were commented in a header ).
       
   348 // -----------------------------------------------------------------------------
       
   349 //
       
   350 void CSuplPsyRequestManager::HandleEnvironmentChange()
       
   351     {
       
   352     // Get reason why this function was called
       
   353     TInt envChange = iEnvChangeNotifier->Change();
       
   354 
       
   355     // If reason was cancel, or system time change flag is not on, then we
       
   356     // can return.
       
   357      if ( envChange == KErrCancel || !( envChange & EChangesSystemTime ) )
       
   358         {
       
   359         return;
       
   360         }
       
   361 
       
   362     // If system time really changed, then we set last fix time as 0
       
   363     iRequestor->ResetLastPosition();
       
   364     }
       
   365 
       
   366 // -----------------------------------------------------------------------------
       
   367 // CSuplPsyRequestManager::BasicCellIDRequestCompletedL
       
   368 // 
       
   369 // -----------------------------------------------------------------------------
       
   370 //
       
   371 void CSuplPsyRequestManager::BasicCellIDRequestCompletedL( TInt aErrorCode )
       
   372     {
       
   373     TRACESTRING( "CSuplPsyRequestManager::BasicCellIDRequestCompletedL start" )
       
   374     
       
   375     TRACESTRING2("Request Completed with Error Code: %d",aErrorCode )
       
   376         
       
   377     TBool requestSuplFw = ETrue;    
       
   378         
       
   379     if( KErrNone == aErrorCode )
       
   380 	    {
       
   381         // Log current cell id info
       
   382         TRACESTRING2("Current Cid=%d",iCellIdInfo.iCid )
       
   383         TRACESTRING2("Current Lac=%d",iCellIdInfo.iLac )
       
   384         TRACESTRING2("Current MCC=%d",iCellIdInfo.iMCC )
       
   385         TRACESTRING2("Current MNC=%d",iCellIdInfo.iMNC )
       
   386              
       
   387 	    // Get stored cell id from cenrep
       
   388 	    	   
       
   389 	    CSuplPsyCellIdHandler::TNetworkTypeInfo type = iSuplPsyCellIdHandler->NetworkType();
       
   390 	    TCellIdInfo cellinfo;
       
   391 	    TInt errCId = iSuplPsyInfoStoreManager->ReadSuplPsyCellIdInfo( cellinfo );
       
   392 	       
       
   393 	        
       
   394 	    HPositionGenericInfo* posinfo =  HPositionGenericInfo::NewLC();
       
   395 	    TInt errPosInfo = iSuplPsyInfoStoreManager->ReadSuplPsyLocationInfo( *posinfo );
       
   396 	    	    
       
   397 	    // Validity of cell id info
       
   398         TRACESTRING2("Validity of cell id info=%d",errCId )
       
   399         
       
   400         // Presence of saved location info
       
   401         TRACESTRING2("Saved Location info is present=%d",errPosInfo )
       
   402 	    
       
   403 	    if( KErrNone == errCId && KErrNone == errPosInfo)
       
   404 		    {
       
   405 		    TRACESTRING( "Valid Cell Id & Location stored info exists" )
       
   406 		    
       
   407 		    // Log saved cell id info
       
   408 	    	TRACESTRING2("Saved Cid=%d",cellinfo.iCid )
       
   409         	TRACESTRING2("Saved Lac=%d",cellinfo.iLac )
       
   410         	TRACESTRING2("Saved MCC=%d",cellinfo.iMCC )
       
   411         	TRACESTRING2("Saved MNC=%d",cellinfo.iMNC )
       
   412 		    
       
   413 		    TPosition pos;
       
   414 	        posinfo->GetPosition( pos );
       
   415 	    
       
   416 	        TRACESTRING2( "Max Age=%d",iMaxAge.Int64() )
       
   417 	        TRACESTRING2( "Fix Time=%d",pos.Time().Int64() )
       
   418 	        
       
   419 	        // Check if max age expires
       
   420 	        if( iMaxAge > 0 && pos.Time() >= iMaxAge )
       
   421 		    	{
       
   422 		        TRACESTRING( "Max age not expired" )
       
   423 		        // Check if current cell id is same as stored one.
       
   424 		        if( iCellIdInfo.iCid == cellinfo.iCid && 
       
   425 		            ( iCellIdInfo.iLac == cellinfo.iLac || 
       
   426 		            CSuplPsyCellIdHandler:: EWCDMA == iSuplPsyCellIdHandler->NetworkType() ) &&
       
   427 		            iCellIdInfo.iMCC == cellinfo.iMCC &&
       
   428 		            iCellIdInfo.iMNC == cellinfo.iMNC )
       
   429 					{
       
   430 			        TRACESTRING( "Current CID info is same as stored one" )
       
   431 			        // completes client's request with stored position info
       
   432 			        TInt count = iPositionerArray.Count();
       
   433                     for ( TInt i = 0; i < count; i++ )
       
   434                 		{
       
   435                 	    TPositionerStatus& positionerStatus = iPositionerArray[i];
       
   436                  	    if ( positionerStatus.iRequested )
       
   437                  			{
       
   438                   		    TBool completed = positionerStatus.iPositioner->RequestComplete( 
       
   439                 							  aErrorCode,*posinfo );
       
   440                    			if ( completed )
       
   441 		                		{
       
   442 		                		positionerStatus.iRequested = EFalse;
       
   443 		                		}
       
   444             				}	
       
   445 			    		}
       
   446 		    	    TRACESTRING( "Clients request completed with stored location info" )
       
   447 		    	    requestSuplFw = EFalse;
       
   448 		    		}
       
   449 				}
       
   450 		    }
       
   451 	    CleanupStack::PopAndDestroy( posinfo );
       
   452 	    }
       
   453     
       
   454     if( requestSuplFw )
       
   455 	    {
       
   456 	    TRACESTRING( "Issuing Location Request..." )
       
   457 	    //Issue location request to SUPL FW
       
   458 	    iRequestor->RequestLocation();	
       
   459 	    }
       
   460     TRACESTRING( "CSuplPsyRequestManager::BasicCellIDRequestCompletedL end" )
       
   461     }
       
   462     
       
   463 // -----------------------------------------------------------------------------
       
   464 // CSuplPsyRequestManager::RecieveMaxAge
       
   465 // 
       
   466 // -----------------------------------------------------------------------------
       
   467 //
       
   468 void CSuplPsyRequestManager::RecieveMaxAge( TTime aMaxAge )
       
   469 	{
       
   470 	iMaxAge = aMaxAge;	
       
   471 	}
       
   472 //  End of File