datasourceadaptation/gpsdatasourceadaptation/common/src/crequesthandler.cpp
changeset 0 9cfd9a3ee49c
equal deleted inserted replaced
-1:000000000000 0:9cfd9a3ee49c
       
     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 // requesthandler.cpp
       
    15 // 
       
    16 //
       
    17 
       
    18 /**
       
    19  @file
       
    20  @InternalComponent
       
    21 */
       
    22 
       
    23 #include "crequesthandler.h"
       
    24 #include "lbsdevloggermacros.h"
       
    25 #include "cpositionerq.h"
       
    26 #include "psylogging.h"
       
    27 #include "psypanic.h"
       
    28 #include "mpositioner.h"
       
    29 #include "trequestparameters.h"
       
    30 #include "tpositionercall.h"
       
    31 
       
    32 /*
       
    33 * CRequestHandler::~CRequestHandler
       
    34 * Destructor.
       
    35 */
       
    36 CRequestHandler::~CRequestHandler()
       
    37     {
       
    38 	if(iPowerStandByTimer)
       
    39 		{
       
    40 		iPowerStandByTimer->Cancel();
       
    41 		}
       
    42 
       
    43 	delete iPowerStandByTimer;
       
    44 	iPowerStandByTimer = NULL;
       
    45     }
       
    46 
       
    47 /**
       
    48 @param aModuleId Position module Id
       
    49 @param aChannel Channel identifier
       
    50 @param aCache A reference to lastknown position cache
       
    51 */
       
    52 CRequestHandler::CRequestHandler() 
       
    53 	{
       
    54 	}
       
    55 	
       
    56 void CRequestHandler::ConstructL()
       
    57 	{
       
    58 	LBSLOG(ELogP1, "CRequestHandler::ConstructL()");
       
    59 	iPowerStandByTimer = CLbsCallbackTimer::NewL(*this);
       
    60 	ResetMergedRequest();
       
    61 	}
       
    62 
       
    63 /**
       
    64 * Update the request parameters
       
    65 *	
       
    66 * @param aRequest The request to be updated
       
    67 * @param aTimeForFix The maximum fix time
       
    68 * @param aQuality The requested position info quality
       
    69 */
       
    70 void CRequestHandler::BuildUpdateRequest(TLbsLocRequestQuality& aRequest, const TTimeIntervalMicroSeconds& aTimeForFix,
       
    71 										const TPositionQuality& aQuality)
       
    72 	{
       
    73 	aRequest.SetMinHorizontalAccuracy(aQuality.HorizontalAccuracy());
       
    74 	aRequest.SetMinVerticalAccuracy(aQuality.VerticalAccuracy());
       
    75 	aRequest.SetMaxFixTime(aTimeForFix);
       
    76 	}
       
    77 
       
    78 /**
       
    79 * Compare the current and merged requests
       
    80 * 
       
    81 * @ret TBool, ETrue if the current and merged requests are not the same 
       
    82 */
       
    83 TBool CRequestHandler::MergedDifferent()
       
    84 	{
       
    85 	TBool different = EFalse;
       
    86 
       
    87 	different = iMergedRequest.RequestQuality().MinHorizontalAccuracy() != 
       
    88 				iCurrentRequest.RequestQuality().MinHorizontalAccuracy();
       
    89 	different |= iMergedRequest.RequestQuality().MinVerticalAccuracy() != 
       
    90 				iCurrentRequest.RequestQuality().MinVerticalAccuracy();
       
    91 	different |= iMergedRequest.TargetTime() != iCurrentRequest.TargetTime();
       
    92 
       
    93 	TLbsNetPosMethodInt mergedMethod;
       
    94 	TLbsNetPosMethodInt currentMethod;
       
    95 	iMergedRequest.RequestMethod().GetPosMethod(0, mergedMethod);
       
    96 	iCurrentRequest.RequestMethod().GetPosMethod(0, currentMethod);
       
    97 	different |= mergedMethod.PosMode() != currentMethod.PosMode();
       
    98 
       
    99 	return different;
       
   100 	}
       
   101 	
       
   102 
       
   103 /**
       
   104 * Cancels the location retrieval if no outstanding requests
       
   105 * Does nothing if there are outstanding requests
       
   106 * @param aError, reason for cancel
       
   107 */
       
   108 void CRequestHandler::CancelRequestL()
       
   109 	{
       
   110 	LBSLOG(ELogP1, "CRequestHandler::CancelRequestL()");
       
   111 	TActiveClients active;
       
   112 	if(!(iPositionerQ->IterETrue(active)))
       
   113 		{
       
   114 		LBSLOG(ELogP1, "No active clients. Send cancel to manager");
       
   115 		TTracking tracking;
       
   116 		IssueCancel(iPositionerQ->IterETrue(tracking));
       
   117 		ResetMergedRequest();
       
   118 		}
       
   119 	LBSLOG(ELogP1, "CPositionerQ::CancelRequestL() end");
       
   120 	}
       
   121 
       
   122 
       
   123 /**
       
   124 * Remerges any outstanding requests and Issues either new merged request if any, or issues cancel if none
       
   125 * @param aError, reason for cancel
       
   126 */
       
   127 void CRequestHandler::ReMergeWithCancelRequestL(TInt aError)
       
   128 	{
       
   129 	LBSLOG(ELogP1, "CRequestHandler::ReMergeCancelRequestL()");
       
   130 	TActiveClients active;
       
   131 	if(iPositionerQ->IterETrue(active))
       
   132 		{
       
   133 		LBSLOG(ELogP1, "Active clients");
       
   134 		SubmitRequestL();
       
   135 		}
       
   136 	else // no outstanding requests
       
   137 		{
       
   138 		LBSLOG(ELogP1, "No active clients");
       
   139 		if(aError != KErrTimedOut)// Don't pass to mgr if it's a LS timeout
       
   140 			{
       
   141 			LBSLOG(ELogP1, "Send cancel to manager");
       
   142 			TTracking tracking;
       
   143 			IssueCancel(iPositionerQ->IterETrue(tracking));
       
   144 			}
       
   145 		ResetMergedRequest();
       
   146 		}
       
   147 	LBSLOG(ELogP1, "CPositionerQ::ReMergeCancelRequestL() end");
       
   148 	}
       
   149 
       
   150 /**
       
   151 * Triggers merging of new request
       
   152 * Assumes the new request is now active so will be picked up below
       
   153 */
       
   154 void CRequestHandler::SubmitNewRequestL(MPositioner* aNewRequest)
       
   155 	{
       
   156 	LBSLOG(ELogP1, "CRequestHandler::SubmitNewRequestL()");
       
   157 	TInt newReqIndex = iPositionerQ->Index(*aNewRequest);
       
   158 		
       
   159 	TInt error = MergeNewRequest(newReqIndex);
       
   160 	if(KErrNone == error)
       
   161 		{
       
   162 		if(MergedDifferent())
       
   163 			{
       
   164 			IssueMergedRequestL();
       
   165 			
       
   166 			iPowerStandByTimer->Cancel();
       
   167 			}
       
   168 		}
       
   169 	else
       
   170 		{
       
   171 		aNewRequest->UpdateFailed(error);
       
   172 		}
       
   173 	}
       
   174 
       
   175 TInt CRequestHandler::MergeNewRequest(TInt aNewRequest)
       
   176 	{
       
   177 	LBSLOG(ELogP1, "CRequestHandler::MergeNewRequest()");
       
   178 	TBool active = (iPositionerQ->Positioner(aNewRequest)).IsActive();
       
   179 	
       
   180 	__ASSERT_DEBUG(active, User::Panic(KAdaptationPanicCategory, EPanicNewRequestInactive));
       
   181 
       
   182 	TTracking tracking;
       
   183 	TInt error = MergeRequest( (iPositionerQ->Positioner(aNewRequest)).PositionRequestParams(), 
       
   184 		iPositionerQ->IterETrue(tracking), TLbsPositionUpdateRequestBase::EPowerAdviceOn );
       
   185 
       
   186 	return error;
       
   187 	}
       
   188 
       
   189 
       
   190 
       
   191 /**
       
   192 * Triggers merging of existing requests
       
   193 */
       
   194 void CRequestHandler::SubmitRequestL()
       
   195 	{
       
   196 	LBSLOG(ELogP1, "CRequestHandler::SubmitRequestL()");
       
   197 	MergeAllRequests();
       
   198 		
       
   199 	if(MergedDifferent())
       
   200 		{
       
   201 		IssueMergedRequestL();
       
   202 		
       
   203 		iPowerStandByTimer->Cancel();
       
   204 		}
       
   205 	}
       
   206 
       
   207 /**
       
   208 * Merges all active requests
       
   209 */
       
   210 void CRequestHandler::MergeAllRequests()
       
   211 	{
       
   212 	LBSLOG(ELogP1, "CRequestHandler::MergeAllRequests()");
       
   213 	ResetMergedRequest();
       
   214 	
       
   215     TInt count = iPositionerQ->Count();
       
   216     for(TInt i=0; i<count; i++)
       
   217         {
       
   218         if((iPositionerQ->Positioner(i)).IsActive())
       
   219             {
       
   220    			TTracking tracking;
       
   221    			TInt error = MergeRequest( (iPositionerQ->Positioner(i)).PositionRequestParams(), iPositionerQ->IterETrue(tracking), TLbsPositionUpdateRequestBase::EPowerAdviceOn );
       
   222 			__ASSERT_DEBUG(KErrNone == error, User::Panic(KAdaptationPanicCategory, EPanicModeMergeFailed));
       
   223             }
       
   224         }
       
   225 	}
       
   226 
       
   227 /**
       
   228 * 
       
   229 */
       
   230 void CRequestHandler::WarmDownTimerExpired()
       
   231 	{
       
   232 	LBSLOG(ELogP1, "CRequestHandler::WarmDownTimerExpired()");
       
   233 	// Iterate positioners. If none active and no warmdown timers running, send a cancel to the manager
       
   234 	TActiveClients active;
       
   235 	TWarmingDownClients warmdown;
       
   236 	TTracking tracking;
       
   237 	
       
   238 	if(!iPositionerQ->IterETrue(active) && !iPositionerQ->IterETrue(warmdown))
       
   239 		{
       
   240 		IssueCancel(iPositionerQ->IterETrue(tracking));
       
   241 		ResetMergedRequest();
       
   242 		}
       
   243 	}
       
   244 	
       
   245 /**
       
   246 * CRequestHandler::NotifyRequestComplete
       
   247 * 
       
   248 * Notifies the request handler that a location fix has been received 
       
   249 * and recombines any outstanding requests for active positioners. This
       
   250 * is because all positioners are held in the queue not just active ones.
       
   251 */
       
   252 void CRequestHandler::NotifyRequestComplete()
       
   253 	{
       
   254 	LBSLOG(ELogP1, "CRequestHandler::NotifyRequestComplete()");
       
   255 	TActiveClients active;
       
   256 	if(!(iPositionerQ->IterETrue(active)))
       
   257 		{
       
   258 		TInactivityTimeout inactivity;
       
   259 
       
   260 		TTimeIntervalMicroSeconds32 timeout = iPositionerQ->IterGreatest(inactivity);
       
   261 		iPowerStandByTimer->Cancel();
       
   262 		iPowerStandByTimer->EventAfter(timeout, EPowerStandByTimerEvent);
       
   263 		ResetMergedRequest();
       
   264 		}
       
   265 	else
       
   266 		{
       
   267 		// If an error occurred sending a request then complete
       
   268 		// all outstanding clients with the error code
       
   269 		TRAPD(result, SubmitRequestL());
       
   270 		if(result != KErrNone)
       
   271 			{
       
   272 			TUpdateFailed call(result);
       
   273 			iPositionerQ->PositionerIterator(call);
       
   274 			}
       
   275 		}
       
   276 	}
       
   277 
       
   278 /**
       
   279 TimeOutTimerEvent handling
       
   280 */	
       
   281 void CRequestHandler::TimeOutTimerEvent()
       
   282 	{
       
   283     LBSLOG(ELogP1, "CRequestHandler::TimeOutTimerEvent()");
       
   284 
       
   285 	iPowerStandByTimer->Cancel();
       
   286 
       
   287 	TActiveClients active;
       
   288 	if(!(iPositionerQ->IterETrue(active)))
       
   289 		{
       
   290 		// if queue has no active positioners then send advice EPowerAdviceStandby
       
   291 		TTracking tracking;
       
   292 		IssueStatus(iPositionerQ->IterETrue(tracking), TLbsPositionUpdateRequestBase::EPowerAdviceStandby);
       
   293 		}
       
   294 	}
       
   295 	
       
   296 /**
       
   297 TimeOutTimer error handling
       
   298 */	
       
   299 TInt CRequestHandler::TimeOutTimerError(TInt /*aError*/)
       
   300 	{
       
   301 	return KErrNone;
       
   302 	}
       
   303 
       
   304 /**
       
   305 from MLbsCallbackTimerObserver
       
   306 
       
   307 @param aTimerId Time event ID to identify two different time events
       
   308 */
       
   309 void CRequestHandler::OnTimerEventL(TInt aTimerId)	
       
   310 	{
       
   311 	switch(aTimerId)
       
   312 		{
       
   313 	case EPowerStandByTimerEvent:
       
   314 		TimeOutTimerEvent();
       
   315 		break;
       
   316 
       
   317 	default:
       
   318 		__ASSERT_DEBUG(0, User::Panic(KAdaptationPanicCategory, KUnknownTimerEventId));
       
   319 		}
       
   320 	}
       
   321 /**
       
   322 Timer error handling
       
   323 
       
   324 @param aError Time error code
       
   325 @param aTimerId Time event ID to identify two different time events
       
   326 */	
       
   327 TInt CRequestHandler::OnTimerError(TInt aError, TInt aTimerId)
       
   328 	{
       
   329 	switch(aTimerId)
       
   330 		{
       
   331 	case EPowerStandByTimerEvent:
       
   332 		TimeOutTimerError(aError);
       
   333 		break;
       
   334 
       
   335 	default:
       
   336 		__ASSERT_DEBUG(0, User::Panic(KAdaptationPanicCategory, KUnknownTimerEventId));
       
   337 		}
       
   338 	return KErrNone; // we have handled the error locally	
       
   339 	}
       
   340 	
       
   341