networkprotocolmodules/common/suplrrlpasn1/src/suplend.cpp
branchGCC_SURGE
changeset 49 5f20f71a57a3
parent 41 ec40843d536a
parent 45 15a2125aa2f3
equal deleted inserted replaced
41:ec40843d536a 49:5f20f71a57a3
     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 //
       
    15 
       
    16 /**
       
    17  @file
       
    18  @internalTechnology
       
    19  
       
    20 */
       
    21 
       
    22 #include "ULP.h"
       
    23 #include "ASN1TTime.h"
       
    24 #include <lbssatellite.h>
       
    25 #include <lbs/lbsgpsmeasurement.h>
       
    26 #include "suplend.h"
       
    27 #include "supldevloggermacros.h" 
       
    28 
       
    29 /**
       
    30 Static factor constructor
       
    31 */
       
    32 EXPORT_C CSuplEnd* CSuplEnd::NewL(TBool aIsOutgoingMessage)
       
    33 	{
       
    34 	SUPLLOG(ELogP1, "CSuplEnd::NewL() Begin\n");
       
    35 	CSuplEnd* self = CSuplEnd::NewLC(aIsOutgoingMessage);
       
    36 	SUPLLOG(ELogP1, "CSuplEnd::NewL() End\n");
       
    37 	CleanupStack::Pop(self);
       
    38 	return self;
       
    39 	}
       
    40 
       
    41 EXPORT_C CSuplEnd* CSuplEnd::NewLC(TBool aIsOutgoingMessage)
       
    42 	{
       
    43 	SUPLLOG(ELogP1, "CSuplEnd::NewLC() Begin\n");
       
    44 	CSuplEnd* self = new (ELeave) CSuplEnd(aIsOutgoingMessage);
       
    45 	CleanupStack::PushL(self);
       
    46 	self->ConstructL();
       
    47 	SUPLLOG(ELogP1, "CSuplEnd::NewLC() End\n");
       
    48 	return self;
       
    49 	}
       
    50 
       
    51 /** 
       
    52 Constructor 
       
    53 */
       
    54 CSuplEnd::CSuplEnd(TBool aIsOutgoingMessage)
       
    55  : CSuplMessageBase::CSuplMessageBase(ESuplEnd, aIsOutgoingMessage)
       
    56 	{
       
    57 	}
       
    58 
       
    59 
       
    60 /** 
       
    61 Second stage constructor 
       
    62 */
       
    63 void CSuplEnd::ConstructL()
       
    64 	{
       
    65 	// call the base class ConstructL() to create common data structures
       
    66 	CSuplMessageBase::ConstructL();
       
    67 
       
    68 	// message specific structures for outgoing messages.
       
    69 	if (iIsOutgoingMessage)
       
    70 		{
       
    71 		// local reference to context object
       
    72 		OSCTXT* pctxt = iControl->getCtxtPtr();
       
    73 
       
    74 		// create the SUPL POS specific data structures
       
    75 		iData->message.t =  T_UlpMessage_msSUPLEND;
       
    76 		iData->message.u.msSUPLEND = (ASN1T_SUPLEND*)rtxMemAllocZ(pctxt, sizeof(ASN1T_SUPLEND));
       
    77    		LeaveIfAllocErrorL();
       
    78 		}
       
    79 	}
       
    80 		
       
    81 /**
       
    82 Destructor
       
    83 */
       
    84 CSuplEnd::~CSuplEnd()
       
    85 	{
       
    86 	SUPLLOG(ELogP1, "CSuplEnd::~CSuplEnd() Begin\n");
       
    87 	SUPLLOG(ELogP1, "CSuplEnd::~CSuplEnd() End\n");
       
    88 	}
       
    89 	
       
    90 
       
    91 /**
       
    92 SetPosition()
       
    93 
       
    94 Set the Position parameter
       
    95 
       
    96 @param  aPosInfo, position info as provided by LBS subsystem
       
    97 @return error indication, KErrNone otherwise
       
    98 */
       
    99 EXPORT_C TInt CSuplEnd::SetPosition(const TPositionInfoBase& aPosInfo)
       
   100 	{
       
   101 	SUPLLOG(ELogP1, "CSuplEnd::SetPosition() Begin\n");
       
   102 	__ASSERT_DEBUG(iIsOutgoingMessage, User::Invariant());
       
   103 	__ASSERT_DEBUG(iData->message.u.msSUPLEND != NULL, User::Invariant());
       
   104 	
       
   105 	// flag that the optional position parameter is present
       
   106 	iData->message.u.msSUPLEND->m.positionPresent = 1;
       
   107 
       
   108 	SUPLLOG(ELogP1, "CSuplEnd::SetPosition() End (Calling PopulatePosition())\n");
       
   109 	return PopulatePosition(aPosInfo, iData->message.u.msSUPLEND->position);
       
   110 	}
       
   111 
       
   112 	
       
   113 /** 
       
   114 SetStatusCode()
       
   115 
       
   116 Set the SUPL Status Code 
       
   117 
       
   118 @param  aStatusCode, the status code for session termination
       
   119 @return error indication, KErrNone otherwise
       
   120 */
       
   121 EXPORT_C TInt CSuplEnd::SetStatusCode(TSuplStatusCode& aStatusCode)
       
   122 	{
       
   123 	SUPLLOG(ELogP1, "CSuplEnd::SetStatusCode() Begin\n");
       
   124 	SUPLLOG2(ELogP1, "    - TSuplStatusCode aStatusCode  = %d\n", aStatusCode);
       
   125 	__ASSERT_DEBUG(iIsOutgoingMessage, User::Invariant());
       
   126 	__ASSERT_DEBUG(iData->message.u.msSUPLEND != NULL, User::Invariant());
       
   127 
       
   128 	// flag that the optional status code parameter is present and set param
       
   129 	iData->message.u.msSUPLEND->m.statusCodePresent = 1;
       
   130 	iData->message.u.msSUPLEND->statusCode = aStatusCode;
       
   131 	
       
   132 	SUPLLOG(ELogP1, "CSuplEnd::SetStatusCode() End\n");
       
   133 	return KErrNone;
       
   134 	}
       
   135 
       
   136 	
       
   137 
       
   138 /** 
       
   139 SetVer()
       
   140 
       
   141 Sets the Ver parameter, a hash of the received SUPL INIT message of the
       
   142 outgoing message
       
   143 
       
   144 @param aVer, pointer to buffer containing the hash of the SUPL INIT message
       
   145 @return error indication, KErrNone otherwise
       
   146 */
       
   147 EXPORT_C TInt CSuplEnd::SetVer(const TDesC8& aVer)
       
   148 	{
       
   149 	SUPLLOG(ELogP1, "CSuplEnd::SetVer() Begin\n");
       
   150 	__ASSERT_DEBUG(iData->message.u.msSUPLEND != NULL, User::Invariant());
       
   151 
       
   152 	ASN1T_SUPLEND& suplEnd = *iData->message.u.msSUPLEND;
       
   153 	suplEnd.m.verPresent = 1;
       
   154 	suplEnd.ver.numbits = 64;
       
   155 	TPtr8 target(suplEnd.ver.data, 8);
       
   156 	target.Copy(aVer);
       
   157 
       
   158 	SUPLLOG(ELogP1, "CSuplEnd::SetVer() End\n");
       
   159 	return KErrNone;
       
   160 	}
       
   161 
       
   162 /** 
       
   163 PositionPresent()
       
   164 
       
   165 @return ETrue if the position parameter is present 
       
   166 */
       
   167 EXPORT_C TBool CSuplEnd::PositionPresent()
       
   168 	{
       
   169 	SUPLLOG(ELogP1, "CSuplEnd::PositionPresent() Begin\n");
       
   170 	__ASSERT_DEBUG(!iIsOutgoingMessage, User::Invariant());
       
   171 	__ASSERT_DEBUG(iData->message.u.msSUPLEND != NULL, User::Invariant());
       
   172 
       
   173 	if (iData->message.u.msSUPLEND->m.positionPresent != 0)
       
   174 		{
       
   175 		SUPLLOG(ELogP1, "CSuplEnd::PositionPresent(ETrue) End\n");
       
   176 		return ETrue;
       
   177 		}
       
   178 	SUPLLOG(ELogP1, "CSuplEnd::PositionPresent(EFalse) End\n");
       
   179 	return EFalse;
       
   180 	}
       
   181 
       
   182 	
       
   183 /** 
       
   184 Postion()
       
   185 
       
   186 Retrieve the received position information.
       
   187 
       
   188 @param  aPosition, on return populated with the received position information
       
   189 @return error indication, KErrNone otherwise
       
   190 */
       
   191 EXPORT_C TInt CSuplEnd::GetPosition(TPosition& aPosition)
       
   192 	{
       
   193 	SUPLLOG(ELogP1, "CSuplEnd::Position() Begin\n");
       
   194 	__ASSERT_DEBUG(!iIsOutgoingMessage, User::Invariant());
       
   195 	__ASSERT_DEBUG(iData->message.u.msSUPLEND != NULL, User::Invariant());
       
   196 	
       
   197 	if (iData->message.u.msSUPLEND->m.positionPresent != 0)
       
   198 		{
       
   199 		ASN1T_Position& receivedPos = iData->message.u.msSUPLEND->position;
       
   200 
       
   201 		TTime time(0);
       
   202 		TInt err = DecodeTime((TUint8*)receivedPos.timestamp, time);
       
   203 
       
   204 		aPosition.SetTime(time);
       
   205 		
       
   206 		TReal64 latitude  = receivedPos.positionEstimate.latitude;
       
   207 		latitude *= 90;
       
   208 		latitude /= 8388608; // 2^23
       
   209 		if (receivedPos.positionEstimate.latitudeSign == PositionEstimate_latitudeSign::south)
       
   210 			{
       
   211 			latitude *= -1;
       
   212 			}
       
   213 		TReal64 longitude = receivedPos.positionEstimate.longitude;
       
   214 		longitude *= 360;
       
   215 		longitude /= 16777216;
       
   216 		aPosition.SetCoordinate(latitude, longitude);
       
   217 		
       
   218 		// horizontal uncertainty
       
   219 		// from 3gpp ts 23.032:   uncert = C( (1+x)^k - 1 ), C = 10, x = 0.1
       
   220 		if (receivedPos.positionEstimate.m.uncertaintyPresent != 0)
       
   221 			{
       
   222 			TUint k = receivedPos.positionEstimate.uncertainty.uncertaintySemiMajor;
       
   223 			TReal uncert;
       
   224 			Math::Pow(uncert, 1.1, k);
       
   225 			uncert -= 1;
       
   226 			uncert *= 10;
       
   227 			aPosition.SetHorizontalAccuracy(uncert);
       
   228 			}
       
   229 	
       
   230 		// vertical position
       
   231 		if (receivedPos.positionEstimate.m.altitudeInfoPresent != 0)
       
   232 			{
       
   233 			// actual altitude in meters
       
   234 			TReal32 altitude = receivedPos.positionEstimate.altitudeInfo.altitude;
       
   235 			// direction
       
   236 			if (receivedPos.positionEstimate.altitudeInfo.altitudeDirection == AltitudeInfo_altitudeDirection::depth)
       
   237 				{
       
   238 				altitude *= -1;
       
   239 				}
       
   240 			aPosition.SetCoordinate(latitude, longitude, altitude);
       
   241 			
       
   242 			// altitude uncertainty 
       
   243 			// from 3gpp ts 23.032:   altUncert = C( (1+x)^k - 1 ), C = 45, x = 0.025
       
   244 			TUint kAlt = receivedPos.positionEstimate.altitudeInfo.altUncertainty;
       
   245 			TReal altUncert;
       
   246 			Math::Pow(altUncert, 1.025, kAlt);
       
   247 			altUncert -= 1;
       
   248 			altUncert *= 45;
       
   249 			aPosition.SetVerticalAccuracy(altUncert);
       
   250 			}
       
   251 		}
       
   252 	else
       
   253 		{
       
   254 		return KErrNotFound;
       
   255 		}
       
   256 	
       
   257 	SUPLLOG(ELogP1, "CSuplEnd::Position() End\n");
       
   258 	return KErrNone;
       
   259 	}
       
   260 
       
   261 	
       
   262 /** 
       
   263 StatusCodePresent()
       
   264 
       
   265 @return ETrue if the status code parameter is present 
       
   266 */
       
   267 EXPORT_C TBool CSuplEnd::StatusCodePresent()
       
   268 	{
       
   269 	SUPLLOG(ELogP1, "CSuplEnd::StatusCodePresent() Begin\n");
       
   270 	if (iData->message.u.msSUPLEND->m.statusCodePresent != 0)
       
   271 		{
       
   272 		SUPLLOG(ELogP1, "CSuplEnd::StatusCodePresent(ETrue) End\n");
       
   273 		return ETrue;
       
   274 		}
       
   275 
       
   276 	SUPLLOG(ELogP1, "CSuplEnd::StatusCodePresent(EFalse) End\n");
       
   277 	return EFalse;
       
   278 	}
       
   279 
       
   280 
       
   281 /** 
       
   282 StatusCode()
       
   283 
       
   284 @return the SUPL status code
       
   285 */
       
   286 EXPORT_C TSuplStatusCode CSuplEnd::StatusCode()
       
   287 	{
       
   288 	SUPLLOG(ELogP1, "CSuplEnd::StatusCode() Begin\n");
       
   289 	__ASSERT_DEBUG(!iIsOutgoingMessage, User::Invariant());
       
   290 	__ASSERT_DEBUG(iData->message.u.msSUPLEND != NULL, User::Invariant());
       
   291 	
       
   292 	if (iData->message.u.msSUPLEND->m.statusCodePresent != 0)
       
   293 		{
       
   294 		SUPLLOG2(ELogP1, "CSuplEnd::StatusCode() End (Status Code == %d\n", iData->message.u.msSUPLEND->statusCode);
       
   295 		return (TSuplStatusCode)iData->message.u.msSUPLEND->statusCode;
       
   296 		}
       
   297 	
       
   298 	SUPLLOG(ELogP1, "CSuplEnd::StatusCode() End (Status Code not present)\n");
       
   299 	return ESuplStatusUnspecified;
       
   300 	}
       
   301 
       
   302 
       
   303 
       
   304 TInt CSuplEnd::DecodeTime(const TUint8* aTimeString, TTime& aTimeOut)
       
   305 	{
       
   306 	SUPLLOG(ELogP1, "CSuplEnd::DecodeTime() Begin\n");
       
   307 	
       
   308 	TInt err = KErrNone;
       
   309 	
       
   310 	ASN1TUTCTime t;
       
   311 	err = t.parseString((const char*)aTimeString);
       
   312 	if (err != KErrNone)
       
   313 		{
       
   314 		return err;
       
   315 		}
       
   316 
       
   317 	TInt month = t.getMonth();
       
   318 
       
   319 	TMonth monthEnum;
       
   320 	switch (month)
       
   321 		{
       
   322 		case 1:  monthEnum = EJanuary; break;
       
   323 		case 2:  monthEnum = EFebruary; break;
       
   324 		case 3:  monthEnum = EMarch; break;
       
   325 		case 4:  monthEnum = EApril; break;
       
   326 		case 5:  monthEnum = EMay; break;
       
   327 		case 6:  monthEnum = EJune; break;
       
   328 		case 7:  monthEnum = EJuly; break;
       
   329 		case 8:  monthEnum = EAugust; break;
       
   330 		case 9:  monthEnum = ESeptember; break;
       
   331 		case 10: monthEnum = EOctober; break;
       
   332 		case 11: monthEnum = ENovember; break;
       
   333 		case 12: monthEnum = EDecember; break;
       
   334 		default:
       
   335 			__ASSERT_DEBUG(0, User::Invariant());
       
   336 			return KErrArgument;
       
   337 		}
       
   338 	
       
   339 	//Get the day value, subtract 1 from it to convert to internal day values (0-...)
       
   340 	TInt day = t.getDay() - 1;
       
   341 	
       
   342 	TDateTime dateTime(t.getYear(), monthEnum, day, t.getHour(), t.getMinute(), t.getSecond(), 0);
       
   343 	aTimeOut = dateTime;
       
   344 	
       
   345 	SUPLLOG(ELogP1, "CSuplEnd::DecodeTime() End\n");
       
   346 	return KErrNone;
       
   347 	}
       
   348 
       
   349