locationmgmt/networklocationmgr/src/lbsnetlocmanrequesthandler.cpp
changeset 0 9cfd9a3ee49c
equal deleted inserted replaced
-1:000000000000 0:9cfd9a3ee49c
       
     1 // Copyright (c) 2006-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 // Includes
       
    15 // Component
       
    16 // 
       
    17 //
       
    18 
       
    19 #include "lbsnetlocmanrequesthandler.h"
       
    20 #include "lbsnetlocmanlogic.h"
       
    21 #include "lbsdevloggermacros.h"
       
    22 
       
    23 
       
    24 //****************************************************************
       
    25 // Class CNetLocManRequestHandler
       
    26 //****************************************************************
       
    27 /** Static constructor.
       
    28 @param aChannel A reference to the channel identifier
       
    29 @param aNetLocMan A reference to the parent class
       
    30 @released
       
    31 @internalComponent
       
    32 */
       
    33 CNetLocManRequestHandler* CNetLocManRequestHandler::NewL(const RLbsPositionUpdateRequests::TChannelIdentifer& aChannel, CNetLocManLogic& aNetLocMan)
       
    34 	{
       
    35 	CNetLocManRequestHandler* self = new(ELeave) CNetLocManRequestHandler (aChannel, aNetLocMan);
       
    36 
       
    37 	CleanupStack::PushL(self);
       
    38 
       
    39 	self->ConstructL();
       
    40 	CleanupStack::Pop();
       
    41 	
       
    42 	return self;
       
    43 	}
       
    44 	
       
    45 
       
    46 /** Constructor.
       
    47 @param aChannel A reference to the channel identifier
       
    48 @param aNetLocMan A reference to the parent class
       
    49 @released
       
    50 @internalComponent
       
    51 */
       
    52 CNetLocManRequestHandler::CNetLocManRequestHandler(const RLbsPositionUpdateRequests::TChannelIdentifer& aChannel, CNetLocManLogic& aNetLocMan) : 
       
    53 CActive(CActive::EPriorityStandard), iChannel(aChannel), iNetLocMan(aNetLocMan), iTimer(NULL)
       
    54 	{
       
    55 	}
       
    56 
       
    57 
       
    58 /** 
       
    59 @see CActive::DoCancel
       
    60 @released
       
    61 @internalComponent
       
    62 */
       
    63 void CNetLocManRequestHandler::DoCancel()
       
    64 	{
       
    65 	// cancel RLbsPositionUpdateRequests async request
       
    66 	iPosUpdateReq.CancelNotifyPositionUpdateRequestChange();
       
    67 	}
       
    68 
       
    69 
       
    70 /** Second phase constructor.
       
    71 @released
       
    72 @internalComponent
       
    73 */
       
    74 void CNetLocManRequestHandler::ConstructL()
       
    75 	{
       
    76 	iPosUpdateReq.OpenL(iChannel);
       
    77 	iLocationReq.OpenL(KLbsNetLocManagerUid);
       
    78 	iTimer = CLbsCallbackTimer::NewL(*this);
       
    79 	CActiveScheduler::Add(this);
       
    80 	}
       
    81 
       
    82 
       
    83 /** Destructor
       
    84 @released
       
    85 @internalComponent
       
    86 */
       
    87 CNetLocManRequestHandler::~CNetLocManRequestHandler()
       
    88 	{
       
    89 	Cancel();
       
    90 	iPosUpdateReq.Close();
       
    91 	iLocationReq.Close();
       
    92 	if(iTimer)
       
    93 		{
       
    94 		iTimer->Cancel();
       
    95 		delete iTimer;	
       
    96 		}
       
    97 	}
       
    98  
       
    99 
       
   100 /** Subscribe to the position update request property
       
   101 @released
       
   102 @internalComponent
       
   103 */
       
   104 void CNetLocManRequestHandler::StartGettingRequests()
       
   105 	{
       
   106 	LBSLOG(ELogP2, "CNetLocManRequestHandler::StartGettingRequests:");
       
   107 
       
   108 	Cancel();
       
   109 	// subscribe to property from Network Location Manager -> Network Gateway
       
   110 	iPosUpdateReq.NotifyPositionUpdateRequestChange(iStatus);
       
   111 	SetActive();	
       
   112 	}
       
   113 
       
   114 
       
   115 /** 
       
   116 @see CActive::RunError
       
   117 @param aError An error code
       
   118 @released
       
   119 @internalComponent
       
   120 */
       
   121 TInt CNetLocManRequestHandler::RunError(TInt aError)
       
   122 	{
       
   123 	// handle RunL leaving - in our case we do nothing 
       
   124 	// as this means we have had encountered a program inconsistency
       
   125 	// By doing nothing here  causes the main manager thread to exit
       
   126 	// and the Lbs root process resolves any issues - by stopping and restarting
       
   127 	// lbs components
       
   128  	return aError;
       
   129 	}
       
   130 
       
   131 
       
   132 /** Handle requests from the LocServer
       
   133 This function handles the incomming position requests from the LocServer,
       
   134 and pass them on to the NetworkGateway
       
   135 @see CActive::RunL
       
   136 @released
       
   137 @internalComponent
       
   138 */
       
   139 void CNetLocManRequestHandler::RunL()
       
   140 	{
       
   141 	TInt status = iStatus.Int();
       
   142 
       
   143 	StartGettingRequests(); // we want further location requests!
       
   144 	
       
   145 	switch (status)
       
   146 		{
       
   147 		case KErrNone:
       
   148 			{
       
   149 			TLbsPositionUpdateRequestBase updateRequest; 
       
   150 			User::LeaveIfError(iPosUpdateReq.GetPositionUpdateRequest(updateRequest));
       
   151 			
       
   152 			// when there is a new request forwarded, increase session ID by one
       
   153 			TLbsNetSessionIdInt sessionId;
       
   154 			iNetLocMan.GetSessionId(sessionId);
       
   155 			
       
   156 			switch(updateRequest.Type())
       
   157 				{
       
   158 				case TLbsPositionUpdateRequestBase::ECancel:
       
   159 					{
       
   160 					iTimer->Cancel();
       
   161 					TLbsNetLocNetworkLocationCancelMsg locCancel(sessionId, 0);
       
   162 					User::LeaveIfError(iLocationReq.SetNetworkLocationRequest(locCancel));
       
   163 					break;
       
   164 					}
       
   165 		
       
   166 				case TLbsPositionUpdateRequestBase::EUpdateRequest:
       
   167 					{
       
   168 					// only increment when new update request comes in
       
   169 					iNetLocMan.IncrementSessionId();
       
   170 					TLbsPositionUpdateRequest* request = 
       
   171 						(reinterpret_cast<TLbsPositionUpdateRequest*>(&updateRequest));
       
   172 					
       
   173 					TTime target = request->TargetTime();
       
   174 					TTime now;
       
   175 					now.UniversalTime();
       
   176 					
       
   177 					TTimeIntervalMicroSeconds timeout;
       
   178 					timeout = target.MicroSecondsFrom(now);
       
   179 					
       
   180 					if (timeout < 0)
       
   181 						timeout = 0;
       
   182 					
       
   183 					iTimer->Cancel();
       
   184 					iTimer->EventAfter(timeout ,0);
       
   185 					break;
       
   186 					}
       
   187 				}
       
   188 			break;
       
   189 			}
       
   190 		default:
       
   191 			{
       
   192 			User::LeaveIfError(status);
       
   193 			break;
       
   194 			}
       
   195 		}
       
   196 	}
       
   197 	
       
   198 
       
   199 void CNetLocManRequestHandler::OnTimerEventL(TInt /*aTimerId*/)
       
   200 	{
       
   201 	TLbsPositionUpdateRequestBase updateRequest; 
       
   202 	User::LeaveIfError(iPosUpdateReq.GetPositionUpdateRequest(updateRequest));
       
   203 
       
   204 	TLbsNetSessionIdInt sessionId;
       
   205 	iNetLocMan.GetSessionId(sessionId);
       
   206 	
       
   207 	TLbsPositionUpdateRequest* request = 
       
   208 		(reinterpret_cast<TLbsPositionUpdateRequest*>(&updateRequest));
       
   209 	
       
   210 	TLbsNetPosRequestQualityInt quality; 
       
   211 	quality.SetMaxFixTime(request->RequestQuality().MaxFixTime());
       
   212 	
       
   213 	quality.SetMinHorizontalAccuracy(request->RequestQuality().MinHorizontalAccuracy());
       
   214 	quality.SetMinVerticalAccuracy(request->RequestQuality().MinVerticalAccuracy());										
       
   215 
       
   216 	// Set device status to EDeviceActive
       
   217 	iNetLocMan.UpdateDeviceStatus(TPositionModuleStatus::EDeviceActive);
       
   218 	
       
   219 	TLbsNetLocNetworkLocationRequestMsg locRequest(sessionId, ETrue, quality);
       
   220 	User::LeaveIfError(iLocationReq.SetNetworkLocationRequest(locRequest));
       
   221 	}
       
   222 	
       
   223 	
       
   224 TInt CNetLocManRequestHandler::OnTimerError(TInt /*aTimerId*/, TInt aError)
       
   225 	{
       
   226 	return aError;
       
   227 	}
       
   228 
       
   229