// ---------------------------------------------------------
// CT_LbsInstallPsyTp273::NotifyPositionUpdate
// 
// (other items were commented in a header).
// ---------------------------------------------------------
//
void CT_LbsInstallPsyTp273::NotifyPositionUpdate(
	TPositionInfoBase& aPosInfo,
    TRequestStatus& aStatus)
	{
	TInt err = KErrNone;
    
    TTime tt;
    tt.UniversalTime();
    //Request ID must be unique, use universalime as seed
    // to give a random number
    TInt64 seed = tt.Int64();
    TReal lat = 90 * Math::FRand(seed);
    TReal lon = 90 * Math::FRand(seed);
    TReal32 alt = (TReal32)(90 * Math::FRand(seed));
    TPositionInfo* position = static_cast<TPositionInfo*> (&aPosInfo);
    TUid implUid = { KPosImplementationUid };
    position->SetModuleId(implUid);

    TTime now;
    now.UniversalTime();          

    TPosition posse;
    posse.SetCoordinate(lat, lon, alt);
    posse.SetTime(now);
    position->SetPosition(posse);

    TRequestStatus* status = &aStatus;
	User::RequestComplete(status, err);
    }
/** Adjusts system time if required. The decision whether the adjustment is needed or not
is based on the following criterias:
 - satellite time must be present in the location update
 - time threshold must be exceeded
 - time from a last adjustment is greater than a defined interval.
 
@param aStatus An error code.
@param TPositionSatelliteInfo Position and time information. 
							  If clock adjustment takes place the TPosition::iTime is 
							  re-set to the satellite time.
@see CLbsAdmin
@see TPositionSatelliteInfo
*/
void CAutoClockAdjust::LocationUpdate(TInt aStatus, TPositionSatelliteInfo& aPosInfo)
	{
	LBSLOG(ELogP1, "CAutoClockAdjust::LocationUpdate()\n");
	TTimeIntervalMicroSeconds timeCorr;
	TTime sysTime;
	TInt err;
	
	// If adjustment on, no error, and satellite information present
	if ((iClockAdjustSetting == CLbsAdmin::EClockAdjustOn) && (aStatus == KErrNone) &&
	    ((aPosInfo.PositionClassType() & EPositionSatelliteInfoClass) == EPositionSatelliteInfoClass))
		{
		// Is is time do do another time adjustment?
		sysTime.UniversalTime();
		if (Abs(sysTime.MicroSecondsFrom(iLastAdjustment).Int64()) > (1000*iAdjustInterval))
			{
			const TPositionSatelliteInfo& satInfo = static_cast<const TPositionSatelliteInfo&>(aPosInfo);
			err = CalculateTimeCorrection(satInfo, timeCorr);
			if (err == KErrNone)
				{
				// Is threshold exceeded? 
				if (Abs(timeCorr.Int64()) > (1000*iAdjustThreshold))
					{
					sysTime.UniversalTime();
					sysTime += timeCorr;
					LBSLOG(ELogP9, "->S CGpsSetClockBase::SetUTCTime() ClockModule\n");
					LBSLOG5(ELogP9, "  > TTime sysTime  = %02d:%02d:%02d.%06d\n", sysTime.DateTime().Hour(), 
																				sysTime.DateTime().Minute(),
																				sysTime.DateTime().Second(),
																				sysTime.DateTime().MicroSecond());

					err = iSetClockImpl->SetUTCTime(sysTime);
					LBSLOG2(ELogP9, "  Return  = %d\n", err);

					if (err == KErrNone)
						{				
						// Sync the position time with the satellite time
						// to avoid re-adjusting the system time by the manual clock adjustment component.
						TPosition pos;
						aPosInfo.GetPosition(pos);
						pos.SetTime(aPosInfo.SatelliteTime());
						aPosInfo.SetPosition(pos);
						LBSLOG2(ELogP2, "ACTION: Clock Adjusted by %ld\n", timeCorr.Int64());
						}
					}
					
				if (err == KErrNone)
					{				
					// Remember the current time even if threshold not exceeded
					iLastAdjustment = sysTime;
					}
				else
					{
					LBSLOG_WARN2(ELogP3, "Clock Adjustment failed. Error: %d\n", err);
					}
				}
			}
		}
	}