locationtriggering/ltstrategyengine/src/lbtlocationrequestor.cpp
changeset 56 4e949f03ecc5
equal deleted inserted replaced
-1:000000000000 56:4e949f03ecc5
       
     1 /*
       
     2 * Copyright (c) 2006, 2007 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description:  Implementation of Location Requestor
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 
       
    20 #include "lbtlogger.h"
       
    21 #include "lbtlocationrequestor.h"
       
    22 #include "lbtpsychangelistner.h"
       
    23 
       
    24 _LIT( KServer, "LocationTriggering" );
       
    25 
       
    26 // CONSTANTS
       
    27 const TInt64 KMaxUpdateAgeForNpp( 7200000000 ); // 120 minutes or 2 hours
       
    28 
       
    29 
       
    30 // ======== MEMBER FUNCTIONS ========
       
    31 
       
    32 // -----------------------------------------------------------------------------
       
    33 // CLbtLocationRequestor::NewL
       
    34 // CLbtLocationRequestor instantiation method.
       
    35 // -----------------------------------------------------------------------------
       
    36 //
       
    37 CLbtLocationRequestor* CLbtLocationRequestor::NewL( )
       
    38     {
       
    39     CLbtLocationRequestor* self = new ( ELeave ) CLbtLocationRequestor;
       
    40     CleanupStack::PushL( self );
       
    41     self->ConstructL( );
       
    42     CleanupStack::Pop();
       
    43     return self;
       
    44     }
       
    45     
       
    46 
       
    47 // -----------------------------------------------------------------------------
       
    48 // CLbtLocationRequestor::~CLbtLocationRequestor
       
    49 // Destructor.
       
    50 // -----------------------------------------------------------------------------
       
    51 //
       
    52 CLbtLocationRequestor::~CLbtLocationRequestor()
       
    53     {
       
    54     if( iValidNppHandle )
       
    55     	{
       
    56     	iNppPositioner.Close();
       
    57     	}    
       
    58     delete iNbpChangeListener;
       
    59     iPositioner.Close();
       
    60     iPosServer.Close();    
       
    61     }
       
    62 	
       
    63 	
       
    64 // -----------------------------------------------------------------------------
       
    65 // CLbtLocationRequestor::CurrentLocation
       
    66 // Method used by CLbtStrategyEngine to obtain position information
       
    67 // -----------------------------------------------------------------------------
       
    68 //
       
    69 void CLbtLocationRequestor::CurrentLocation( TPositionInfo& aPosInfo, 
       
    70 											  TRequestStatus& aStatus, 
       
    71 											  TPositionModuleId aPosID,
       
    72 											  TTimeIntervalMicroSeconds aTimeOut )
       
    73     {
       
    74     FUNC_ENTER("CLbtLocationRequestor::CurrentLocation");
       
    75     if( aPosID != iPosID )
       
    76         {
       
    77         iPositioner.Close();
       
    78         TRAPD( err, OpenConnectionL( aPosID ) );
       
    79         if( KErrNone != err )
       
    80             {
       
    81             TRequestStatus* lStatus = &aStatus;
       
    82             User::RequestComplete( lStatus, err );
       
    83             return;
       
    84             }
       
    85         }
       
    86     
       
    87     // Setting update options
       
    88     TPositionUpdateOptions updateOptions;
       
    89     updateOptions.SetUpdateTimeOut( aTimeOut );
       
    90     iPositioner.SetUpdateOptions( updateOptions );
       
    91     
       
    92     iPositioner.NotifyPositionUpdate( aPosInfo, aStatus );
       
    93     }
       
    94 
       
    95 // -----------------------------------------------------------------------------
       
    96 // CLbtLocationRequestor::CurrentLocationFromNpp
       
    97 // Method used by CLbtStrategyEngine to obtain position information
       
    98 // -----------------------------------------------------------------------------
       
    99 //
       
   100 void CLbtLocationRequestor::CurrentLocationFromNpp( TPositionInfo& aPosInfo, 
       
   101                                                     TRequestStatus& aStatus,
       
   102                                                     TTimeIntervalMicroSeconds /*aTimeOut*/ )
       
   103     {
       
   104     FUNC_ENTER("CLbtLocationRequestor::CurrentLocationFromNpp");
       
   105     LOG("CLbtLocationRequestor::CurrentLocationFromNpp");
       
   106     if( !iValidNppHandle )
       
   107         {
       
   108         TRequestStatus* statusPtr = &aStatus;
       
   109         User::RequestComplete( statusPtr,KErrNotFound );
       
   110         return;
       
   111         }
       
   112     
       
   113     // Setting update options
       
   114     TPositionUpdateOptions updateOptions;
       
   115     updateOptions.SetMaxUpdateAge( TTimeIntervalMicroSeconds(KMaxUpdateAgeForNpp) );
       
   116     updateOptions.SetUpdateTimeOut( 30000000 );
       
   117     iNppPositioner.SetUpdateOptions( updateOptions );
       
   118     
       
   119     iNppPositioner.NotifyPositionUpdate( aPosInfo, aStatus );
       
   120     }
       
   121 	
       
   122 
       
   123 // -----------------------------------------------------------------------------
       
   124 // CLbtLocationRequestor::GetModuleInfo
       
   125 // Retrieves information about the positioning technology being used
       
   126 // -----------------------------------------------------------------------------
       
   127 //
       
   128 void CLbtLocationRequestor::GetModuleInfo( TPositionModuleId aPosID )
       
   129     {
       
   130     TPositionModuleInfo moduleInfo;
       
   131     iPosServer.GetModuleInfoById( aPosID, moduleInfo );
       
   132     TPositionModuleInfo::ECapabilitySatellite & moduleInfo.Capabilities()?
       
   133         iGPSUsed = ETrue : iGPSUsed = EFalse;
       
   134     }
       
   135 
       
   136 
       
   137 // -----------------------------------------------------------------------------
       
   138 // CLbtLocationRequestor::GPSUsed
       
   139 // Determines if GPS method is used to retrieve location information
       
   140 // -----------------------------------------------------------------------------
       
   141 //
       
   142 TBool CLbtLocationRequestor::GPSUsed() const
       
   143     {
       
   144     return iGPSUsed;
       
   145     }
       
   146     
       
   147     
       
   148 // -----------------------------------------------------------------------------
       
   149 // CLbtLocationRequestor::Cancel
       
   150 // Cancels all outstanding requests to the location server. This method 
       
   151 // -----------------------------------------------------------------------------
       
   152 //
       
   153 void CLbtLocationRequestor::Cancel()
       
   154     {
       
   155     iPositioner.CancelRequest( EPositionerNotifyPositionUpdate );
       
   156     }
       
   157 
       
   158 // -----------------------------------------------------------------------------
       
   159 // CLbtLocationRequestor::CancelFixFromNpp
       
   160 // Cancels all outstanding requests to the location server. This method 
       
   161 // -----------------------------------------------------------------------------
       
   162 //
       
   163 void CLbtLocationRequestor::CancelFixFromNpp()
       
   164     {
       
   165     if( iValidNppHandle )
       
   166     	{
       
   167     	iNppPositioner.CancelRequest( EPositionerNotifyPositionUpdate );
       
   168     	}    
       
   169     }
       
   170 	
       
   171 
       
   172 // -----------------------------------------------------------------------------
       
   173 // CLbtLocationRequestor::CLbtLocationRequestor
       
   174 // Default constructor.
       
   175 // -----------------------------------------------------------------------------
       
   176 //
       
   177 CLbtLocationRequestor::CLbtLocationRequestor()
       
   178     {
       
   179     }
       
   180 	
       
   181 
       
   182 // -----------------------------------------------------------------------------
       
   183 // CLbtLocationRequestor::ConstructL
       
   184 // Symbian 2nd phase constructor.
       
   185 // -----------------------------------------------------------------------------
       
   186 //
       
   187 void CLbtLocationRequestor::ConstructL()
       
   188     {
       
   189     User::LeaveIfError( iPosServer.Connect() );
       
   190     
       
   191     iPosID = KPositionNullModuleId;
       
   192     
       
   193     // TODO: Read this from CenRep
       
   194     TPositionModuleId nppPsyID = { 0x10206915 };
       
   195     TInt error = iNppPositioner.Open( iPosServer,nppPsyID ) ;
       
   196     
       
   197     if( error == KErrNone )
       
   198         {
       
   199         LOG("NBP Session Opened");
       
   200         iNbpChangeListener = NULL;
       
   201         User::LeaveIfError( iNppPositioner.SetRequestor( 
       
   202                                             CRequestor::ERequestorService,
       
   203                                             CRequestor::EFormatApplication,
       
   204                                             KServer ) );
       
   205         iValidNppHandle = ETrue;
       
   206         }
       
   207     else
       
   208         {
       
   209         iValidNppHandle = EFalse;
       
   210         iNbpChangeListener = CLbtPsyChangeListner::NewL(*this);
       
   211         iNbpChangeListener->StartToListen( nppPsyID );
       
   212         }
       
   213     }
       
   214 
       
   215 
       
   216 // -----------------------------------------------------------------------------
       
   217 // CLbtLocationRequestor::OpenConnectionL
       
   218 // Resets the position method used for acquiring location information. This is 
       
   219 // done only when the module ID specified differs from the the existing ID. 
       
   220 // -----------------------------------------------------------------------------
       
   221 //
       
   222 void CLbtLocationRequestor::OpenConnectionL( TPositionModuleId aPosID )
       
   223     {
       
   224     FUNC_ENTER("CLbtLocationRequestor::OpenConnectionL");  	
       
   225     
       
   226     if( aPosID != iPosID )
       
   227         {
       
   228         iPosID = aPosID;
       
   229         TUid defaultID = { 0XFFFFFFFF };
       
   230         if( iPosID == defaultID )
       
   231             {
       
   232             User::LeaveIfError( iPositioner.Open( iPosServer) );
       
   233             }
       
   234         else
       
   235             {
       
   236             User::LeaveIfError( iPositioner.Open( iPosServer, aPosID ) );
       
   237             }        
       
   238 
       
   239         User::LeaveIfError( iPositioner.SetRequestor( 
       
   240                                     CRequestor::ERequestorService,
       
   241                                     CRequestor::EFormatApplication,
       
   242                                     KServer ) );
       
   243         }
       
   244     }
       
   245 
       
   246 // -----------------------------------------------------------------------------
       
   247 // CLbtLocationRequestor::HandlePsyChangeEvent
       
   248 // -----------------------------------------------------------------------------
       
   249 //
       
   250 void CLbtLocationRequestor::HandlePsyChangeEvent( TPositionModuleId aModuleId )
       
   251 	{
       
   252 	FUNC_ENTER("CLbtLocationRequestor::HandlePsyChangeEvent");
       
   253 	// TODO: Read this from CenRep
       
   254 	TPositionModuleId nppPsyID = { 0x10206915 };
       
   255 	
       
   256 	if( aModuleId == nppPsyID )
       
   257 		{
       
   258 		TInt error = iNppPositioner.Open( iPosServer,nppPsyID ) ;
       
   259     
       
   260 	    if( error == KErrNone )
       
   261 	        {
       
   262 	        LOG("PSY Change Listener: NBP Session Opened");
       
   263 	        delete iNbpChangeListener;
       
   264 	        iNbpChangeListener = NULL;
       
   265 	        TInt error = iNppPositioner.SetRequestor( 
       
   266 	                                            CRequestor::ERequestorService,
       
   267 	                                            CRequestor::EFormatApplication,
       
   268 	                                            KServer );
       
   269 	        if( error == KErrNone )
       
   270 	            {
       
   271 	            iValidNppHandle = ETrue;
       
   272 	            }
       
   273 	        else
       
   274 	            {
       
   275 	            iValidNppHandle = EFalse;
       
   276 	            }
       
   277 	        }	    
       
   278 		}
       
   279 	}
       
   280 
       
   281 // -----------------------------------------------------------------------------
       
   282 // CLbtLocationRequestor::GetPositionModuleId
       
   283 // 
       
   284 // -----------------------------------------------------------------------------
       
   285 //
       
   286 TPositionModuleId CLbtLocationRequestor::GetPositionModuleId()
       
   287     {
       
   288     return iPosID;
       
   289     }
       
   290 
       
   291 // end of file