datasourcemodules/gpspositioningmodule/lbsagpspsy/src/responsehandler/cagpsresponsehandler.cpp
changeset 36 b47902b73a93
parent 0 9cfd9a3ee49c
equal deleted inserted replaced
35:a2efdd544abf 36:b47902b73a93
       
     1 // Copyright (c) 2008-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 // responsehandler.cpp
       
    15 // 
       
    16 //
       
    17 
       
    18 /**
       
    19  @file
       
    20  @InternalComponent
       
    21 */
       
    22 
       
    23 #include "cpositionerq.h"
       
    24 #include "cagpsresponsehandler.h"
       
    25 #include "lbsdevloggermacros.h"
       
    26 #include "psypanic.h"
       
    27 #include "utilfunctions.h"
       
    28 #include "cpositionerq.h"
       
    29 #include "crequesthandler.h"
       
    30 #include "tpositionercall.h"
       
    31 
       
    32 /*
       
    33 * CAgpsResponseHandler::NewL
       
    34 * Two-phased constructor.
       
    35 */
       
    36 CAgpsResponseHandler* CAgpsResponseHandler::NewL(CPositionerQ* aPositionerQ, CRequestHandler* aRequestHandler)
       
    37     {
       
    38     CAgpsResponseHandler* self = new( ELeave ) CAgpsResponseHandler(aPositionerQ, aRequestHandler);
       
    39     
       
    40     CleanupStack::PushL( self );
       
    41     self->ConstructL();
       
    42     CleanupStack::Pop();
       
    43 
       
    44     return self;
       
    45     }
       
    46 
       
    47 /*
       
    48 * CAgpsResponseHandler::CAgpsResponseHandler
       
    49 * C++ default constructor can NOT contain any code, that
       
    50 * might leave.
       
    51 */
       
    52 CAgpsResponseHandler::CAgpsResponseHandler(CPositionerQ* aPositionerQ, CRequestHandler* aRequestHandler)
       
    53     {
       
    54     iPositionerQ = aPositionerQ;
       
    55     iRequestHandler = aRequestHandler;
       
    56     }
       
    57 
       
    58 CAgpsResponseHandler::~CAgpsResponseHandler()
       
    59     {
       
    60     delete iAGPSDataBus;
       
    61     delete iFinalNetDataBus;
       
    62 	delete iRefDataBus;
       
    63 	}
       
    64 
       
    65 /**
       
    66 * construct the data bus by module ID and set the data bus observer
       
    67 */
       
    68 void CAgpsResponseHandler::ConstructL()
       
    69 	{
       
    70 	iAGPSDataBus = CAgpsDataBus::NewL(*this, KLbsGpsLocManagerUid);
       
    71 	iFinalNetDataBus = CFinalNetDataBus::NewL(*this);
       
    72 	iRefDataBus = CRefDataBus::NewL(*this);
       
    73 	
       
    74    	Subscribe();
       
    75 	}
       
    76 	
       
    77 void CAgpsResponseHandler::Subscribe()
       
    78 	{
       
    79 	iAGPSDataBus->Subscribe();
       
    80 	iFinalNetDataBus->Subscribe();
       
    81 	iRefDataBus->Subscribe();
       
    82 	}
       
    83 	
       
    84 /**
       
    85 Location update
       
    86 
       
    87 @param aPosition Position info from the data bus
       
    88 @param aStatus status of the bus
       
    89 @param aError error code
       
    90 @param aActualTime position update time
       
    91 */
       
    92 void CAgpsResponseHandler::DataUpdate(TPositionInfo& aPosition, TInt aStatus, TInt aError, TTime aActualTime, TUint aAttributes, TUint aMode)
       
    93 	{
       
    94 	LBSLOG(ELogP1, "CAgpsResponseHandler::DataUpdateL Start ");
       
    95 
       
    96 	Subscribe();
       
    97 	
       
    98 	if(aStatus==KErrNone)
       
    99 		{
       
   100 		TModeUpdate mode(aMode);
       
   101 		iPositionerQ->PositionerIterator(mode);
       
   102 		
       
   103 		// if this is valid data then we need to tell the positioner
       
   104 		if((KErrNone == aError))
       
   105 			{
       
   106 			LBSLOG(ELogP1, "CAgpsResponseHandler::DataUpdateL Valid ");
       
   107 			
       
   108 			TLocationUpdate call(aPosition, aError, aActualTime, aMode);
       
   109 			iPositionerQ->PositionerIterator(call);
       
   110 			
       
   111 			iRequestHandler->NotifyRequestComplete();
       
   112 			}
       
   113 		else if (KPositionCalculationFutile == aError)
       
   114 			{
       
   115 			LBSLOG(ELogP1, "CAgpsResponseHandler::DataUpdateL Futile update ");
       
   116 			if(!(aAttributes & RLbsPositionUpdates::ESelfLocateSessionInProgress))//nb: futile updates ignored during session as manager will re-publish on session complete
       
   117 				{
       
   118 				// If there's a valid FNP we should use that, otherwise use this futile update
       
   119 				TPositionInfo FnpPosInfo;
       
   120 				TTime actualTime;
       
   121 				TInt err = iFinalNetDataBus->GetLastPositionInfo(FnpPosInfo, actualTime);
       
   122 				if(err == KErrNone && actualTime > aActualTime)
       
   123 					{
       
   124 					LBSLOG(ELogP1, "CAgpsResponseHandler::DataUpdateL using FNP");
       
   125 					TLocationUpdate call(FnpPosInfo, aError, aActualTime, aMode);
       
   126 					iPositionerQ->PositionerIterator(call);
       
   127 					iRequestHandler->NotifyRequestComplete();
       
   128 					}
       
   129 				else
       
   130 					{
       
   131 					TLocationUpdate call(aPosition, aError, aActualTime, aMode);
       
   132 					iPositionerQ->PositionerIterator(call);
       
   133 					iRequestHandler->NotifyRequestComplete();
       
   134 					}
       
   135 				}
       
   136 			else
       
   137 				{
       
   138 				LBSLOG(ELogP1, "CAgpsResponseHandler::DataUpdateL Ignoring Futile update during self locate session");
       
   139 				}
       
   140 			}
       
   141 		else if (aError < 0)
       
   142 			{
       
   143 			LBSLOG2(ELogP1, "CAgpsResponseHandler::DataUpdateL -  Error: %d", aError);
       
   144 			
       
   145 			TUpdateFailed call(aError);
       
   146 			iPositionerQ->PositionerIterator(call);
       
   147 			iRequestHandler->NotifyRequestComplete();
       
   148 			}
       
   149 		else
       
   150 			{
       
   151 			// 'Futile' is the only +ve error code we currently have, so this shouldn't happen
       
   152 			__ASSERT_DEBUG(EFalse, User::Panic(KAdaptationPanicCategory, EPanicUnknownUpdateError));
       
   153 			}
       
   154 		}
       
   155 	LBSLOG(ELogP1, "CAgpsResponseHandler::DataUpdateL End ");
       
   156 	}
       
   157 
       
   158 /**
       
   159 GetLastPosition
       
   160 
       
   161 Retrieves the most recent position that's less than the supplied max age if there is one, 
       
   162 including partial positions if partial allowed
       
   163 
       
   164 @param aPos	container for the position being returned (with KErrNone return)	
       
   165 @param aOldestValidTime	 the oldest valid time for a position being returned. If there is a valid position no older than this time, it should be returned.
       
   166 @param aAllowPartial	whether partial updates should be considered
       
   167 @return ETrue if the fix fulfills the requirements, 
       
   168 */
       
   169 TBool CAgpsResponseHandler::GetLastPosition(TPositionInfoBase& aPos, TTime aOldestValidTime, TBool aAllowPartial)
       
   170 	{
       
   171 	TBool ret = EFalse;
       
   172 	TTime actualTime, mostRecent;
       
   173 	TPositionSatelliteInfo satPosInfo;
       
   174 	TPositionInfo refPosInfo, FnpPosInfo;
       
   175  	TPosition pos;
       
   176  	TPositionChoice posChoice = ENone; 	 	
       
   177  	TInt err;
       
   178  	TUint attributes;
       
   179  	TUint mode = 0;
       
   180  	
       
   181  	err = iAGPSDataBus->GetLastPositionInfo(satPosInfo, mostRecent, attributes, mode);
       
   182 	if( err == KErrNone && mostRecent >= aOldestValidTime)	// got a recent enough gps position
       
   183 		{
       
   184 		satPosInfo.GetPosition(pos);
       
   185 		if(aAllowPartial || !Partial(pos))// complete enough
       
   186 			{
       
   187 			posChoice = EAGPSPos;
       
   188 			}
       
   189 		}
       
   190 	err = iRefDataBus->GetLastPositionInfo(refPosInfo, actualTime);
       
   191 	if( err == KErrNone && actualTime >= aOldestValidTime) // got a recent enough ref position
       
   192 		{
       
   193 		refPosInfo.GetPosition(pos);
       
   194 		if(aAllowPartial || !Partial(pos))// complete enough
       
   195 			{
       
   196 			if(posChoice == ENone || actualTime > mostRecent)
       
   197 				{
       
   198 				posChoice = ERefPos;
       
   199 				}
       
   200 			}		
       
   201 		}
       
   202 	err = iFinalNetDataBus->GetLastPositionInfo(FnpPosInfo, actualTime);
       
   203 	if( err == KErrNone && actualTime >= aOldestValidTime)  // got a recent enough fnp position
       
   204 		{
       
   205 		FnpPosInfo.GetPosition(pos);
       
   206 		if(aAllowPartial || !Partial(pos))// complete enough
       
   207 			{
       
   208 			if(posChoice == ENone || actualTime > mostRecent )
       
   209 				{
       
   210 				posChoice = EFNPPos;
       
   211 				}
       
   212 			}		
       
   213 		}
       
   214 
       
   215 	TPositionInfo* chosenPosInfo(NULL);
       
   216 	
       
   217 	switch(posChoice)
       
   218 		{
       
   219 		case ENone:
       
   220 			break;	
       
   221 		case EAGPSPos:
       
   222 			{
       
   223 			chosenPosInfo = &satPosInfo;
       
   224 			break;
       
   225 			}
       
   226 		case ERefPos:
       
   227 			{
       
   228 			chosenPosInfo = &refPosInfo;
       
   229 			break;
       
   230 			}
       
   231 			
       
   232 		case EFNPPos:
       
   233 			{
       
   234 			chosenPosInfo = &FnpPosInfo;
       
   235 			break;
       
   236 			}
       
   237 		default:
       
   238 			__ASSERT_DEBUG(EFalse, User::Invariant());
       
   239 		}
       
   240 		
       
   241 	if(chosenPosInfo != NULL)
       
   242 		{
       
   243 		TRAPD(error, CopyPositionTypes(aPos, *chosenPosInfo));
       
   244 		if(!error)
       
   245 			{
       
   246 			ret = ETrue;
       
   247 			}
       
   248 		else
       
   249 			{
       
   250 			LBSLOG(ELogP1, "CAgpsResponseHandler::GetLastPosition() failed to copy position ");
       
   251 			ret = EFalse;
       
   252 			}
       
   253 		}
       
   254 	
       
   255 	return ret;
       
   256 	}
       
   257