|
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 // utility fucntions, and odds and sods for various bits of the location server |
|
15 // |
|
16 // |
|
17 |
|
18 /** |
|
19 @file |
|
20 @internalComponent |
|
21 @deprecated |
|
22 */ |
|
23 |
|
24 #include <lbssatellite.h> |
|
25 #include "utilfunctions.h" |
|
26 #include "nmeafunctions.h" |
|
27 |
|
28 /** |
|
29 Deep copy of position info data by type |
|
30 |
|
31 @param aTo the destination position info object reference |
|
32 @param aFrom the source position info object reference |
|
33 */ |
|
34 TInt CopyPositionTypes(TPositionInfoBase& aTo, const TPositionInfoBase& aFrom) |
|
35 { |
|
36 // check for self assignment |
|
37 if(&aTo == &aFrom) |
|
38 { |
|
39 return KErrNone; |
|
40 } |
|
41 |
|
42 return CopyPositionTypes2(aTo,aFrom); |
|
43 } |
|
44 |
|
45 /** |
|
46 Deep copy of position info data by type - |
|
47 allows for source position to be smaller than destination position |
|
48 |
|
49 @param aTo the destination position info object reference |
|
50 @param aFrom the source position info object reference |
|
51 */ |
|
52 TInt CopyPositionTypes2(TPositionInfoBase& aTo, const TPositionInfoBase& aFrom) |
|
53 { |
|
54 const TUint32 toClassType = aTo.PositionClassType(); |
|
55 const TUint toClassSize = aTo.PositionClassSize(); |
|
56 const TUint32 fromClassType = aFrom.PositionClassType(); |
|
57 const TUint fromClassSize = aFrom.PositionClassSize(); |
|
58 |
|
59 // check for self assignment |
|
60 if(&aTo == &aFrom) |
|
61 { |
|
62 return KErrNone; // copy is implicit and already done :) |
|
63 } |
|
64 |
|
65 // check we are not assigning base types |
|
66 if(toClassSize == sizeof(TPositionInfoBase) |
|
67 || fromClassSize == sizeof(TPositionInfoBase) |
|
68 || toClassType == EPositionInfoUnknownClass // this the type of a TPositionInfoBase |
|
69 || fromClassType == EPositionInfoUnknownClass) |
|
70 { |
|
71 return KErrArgument; // bad types - trying to copy between base types |
|
72 } |
|
73 // check the aTo type |
|
74 TInt typeError = SupportedType(toClassType, toClassSize); |
|
75 if(typeError != KErrNone) |
|
76 { |
|
77 return typeError; |
|
78 } // i.e. KErrNotSupported |
|
79 // check the aFromType |
|
80 typeError = SupportedType(fromClassType, fromClassSize); |
|
81 if(typeError != KErrNone) |
|
82 { |
|
83 return typeError; |
|
84 } // i.e. KErrNotSupported |
|
85 |
|
86 if(toClassType & EPositionGenericInfoClass) |
|
87 { |
|
88 return CopyGenericPositionType(aTo, aFrom); |
|
89 } |
|
90 else |
|
91 { |
|
92 // Ok now we know we can copy these things from one to the other |
|
93 // so here goes - the catch is we cannot overwrite the type and size |
|
94 // of the destination, so we need a bit of pointer math. |
|
95 // NB this relies on the being a class inheritance such that all of the |
|
96 // types are derived from TPositionInfoBase first. Or the math goes |
|
97 // haywire. This also implies a whole slew of assumptions about the ram |
|
98 // layout of these classes.... |
|
99 TUint8* baseToAddr = reinterpret_cast<TUint8*>(&aTo)+sizeof(TPositionClassTypeBase); |
|
100 const TUint8* const baseFromAddr = reinterpret_cast<const TUint8*>(&aFrom)+sizeof(TPositionClassTypeBase); |
|
101 TUint32 dataLength = toClassSize-sizeof(TPositionClassTypeBase); // we copy only this many bytes form the source |
|
102 TUint8* endAddr = Mem::Copy(baseToAddr, baseFromAddr, dataLength); |
|
103 // Sanity check the Mem::Copy() - just in case... |
|
104 if(endAddr != baseToAddr+dataLength) |
|
105 { |
|
106 return KErrGeneral; // Or KErrNoMemory? |
|
107 } |
|
108 else |
|
109 { |
|
110 return KErrNone; |
|
111 } |
|
112 } |
|
113 } |
|
114 |
|
115 TInt CopyGenericPositionType(TPositionInfoBase& aTo, const TPositionInfoBase& aFrom) |
|
116 { |
|
117 TInt error; |
|
118 HPositionGenericInfo* posInfo = static_cast<HPositionGenericInfo*>(&aTo); |
|
119 |
|
120 // Have we got satellite info? |
|
121 if(aFrom.PositionClassType() & EPositionSatelliteInfoClass) |
|
122 { |
|
123 const TPositionSatelliteInfo* satInfo = static_cast<const TPositionSatelliteInfo*>(&aFrom); |
|
124 // copy any requested satellite info: |
|
125 if ( posInfo->IsRequestedField(EPositionFieldSatelliteNumInView) ) |
|
126 { |
|
127 error = posInfo->SetValue(EPositionFieldSatelliteNumInView,static_cast<TInt8>(satInfo->NumSatellitesInView())); |
|
128 if(error != KErrNone) |
|
129 { |
|
130 return error; |
|
131 } |
|
132 } |
|
133 |
|
134 if ( posInfo->IsRequestedField(EPositionFieldSatelliteNumUsed) ) |
|
135 { |
|
136 error = posInfo->SetValue(EPositionFieldSatelliteNumUsed, static_cast<TInt8>(satInfo->NumSatellitesUsed())); |
|
137 if(error != KErrNone) |
|
138 { |
|
139 return error; |
|
140 } |
|
141 } |
|
142 |
|
143 if ( posInfo->IsRequestedField(EPositionFieldSatelliteTime) ) |
|
144 { |
|
145 error = posInfo->SetValue(EPositionFieldSatelliteTime, satInfo->SatelliteTime()); |
|
146 if(error != KErrNone) |
|
147 { |
|
148 return error; |
|
149 } |
|
150 } |
|
151 |
|
152 if ( posInfo->IsRequestedField(EPositionFieldSatelliteHorizontalDoP) ) |
|
153 { |
|
154 error = posInfo->SetValue(EPositionFieldSatelliteHorizontalDoP, static_cast<TReal32>(satInfo->HorizontalDoP())); |
|
155 if(error != KErrNone) |
|
156 { |
|
157 return error; |
|
158 } |
|
159 } |
|
160 |
|
161 if ( posInfo->IsRequestedField(EPositionFieldSatelliteVerticalDoP) ) |
|
162 { |
|
163 error = posInfo->SetValue(EPositionFieldSatelliteVerticalDoP, static_cast<TReal32>(satInfo->VerticalDoP())); |
|
164 if(error != KErrNone) |
|
165 { |
|
166 return error; |
|
167 } |
|
168 } |
|
169 |
|
170 if ( posInfo->IsRequestedField(EPositionFieldSatelliteTimeDoP) ) |
|
171 { |
|
172 error = posInfo->SetValue(EPositionFieldSatelliteTimeDoP, static_cast<TReal32>(satInfo->TimeDoP())); |
|
173 if(error != KErrNone) |
|
174 { |
|
175 return error; |
|
176 } |
|
177 } |
|
178 |
|
179 if ( posInfo->IsRequestedField(EPositionFieldSatellitePositionDoP) ) |
|
180 { |
|
181 TReal64 pdop; |
|
182 TReal64 hdop = satInfo->HorizontalDoP(); |
|
183 TReal64 vdop = satInfo->VerticalDoP(); |
|
184 if(!Math::IsNaN(hdop) && !Math::IsNaN(vdop)) |
|
185 { |
|
186 Math::Sqrt(pdop, hdop*hdop + vdop*vdop); |
|
187 } |
|
188 else |
|
189 { |
|
190 TRealX nan; |
|
191 nan.SetNaN(); |
|
192 pdop = nan; |
|
193 } |
|
194 error = posInfo->SetValue(EPositionFieldSatellitePositionDoP, static_cast<TReal32>(pdop)); |
|
195 if(error != KErrNone) |
|
196 { |
|
197 return error; |
|
198 } |
|
199 } |
|
200 } |
|
201 // Have we got course info? |
|
202 if(aFrom.PositionClassType() & EPositionCourseInfoClass) |
|
203 { |
|
204 // copy any requested course info: |
|
205 const TPositionCourseInfo* courseInfo = static_cast<const TPositionCourseInfo*>(&aFrom); |
|
206 TCourse course; |
|
207 courseInfo->GetCourse(course); |
|
208 |
|
209 if ( posInfo->IsRequestedField(EPositionFieldHorizontalSpeed) ) |
|
210 { |
|
211 error = posInfo->SetValue(EPositionFieldHorizontalSpeed, static_cast<TReal32>(course.Speed())); |
|
212 if(error != KErrNone) |
|
213 { |
|
214 return error; |
|
215 } |
|
216 } |
|
217 if ( posInfo->IsRequestedField(EPositionFieldHorizontalSpeedError) ) |
|
218 { |
|
219 error = posInfo->SetValue(EPositionFieldHorizontalSpeedError, static_cast<TReal32>(course.SpeedAccuracy())); |
|
220 if(error != KErrNone) |
|
221 { |
|
222 return error; |
|
223 } |
|
224 } |
|
225 if ( posInfo->IsRequestedField(EPositionFieldVerticalSpeed) ) |
|
226 { |
|
227 error = posInfo->SetValue(EPositionFieldVerticalSpeed, static_cast<TReal32>(course.VerticalSpeed())); |
|
228 if(error != KErrNone) |
|
229 { |
|
230 return error; |
|
231 } |
|
232 } |
|
233 if ( posInfo->IsRequestedField(EPositionFieldVerticalSpeedError) ) |
|
234 { |
|
235 error = posInfo->SetValue(EPositionFieldVerticalSpeedError, static_cast<TReal32>(course.VerticalSpeedAccuracy())); |
|
236 if(error != KErrNone) |
|
237 { |
|
238 return error; |
|
239 } |
|
240 } |
|
241 if ( posInfo->IsRequestedField(EPositionFieldTrueCourse) ) |
|
242 { |
|
243 error = posInfo->SetValue(EPositionFieldTrueCourse, static_cast<TReal32>(course.Course())); |
|
244 if(error != KErrNone) |
|
245 { |
|
246 return error; |
|
247 } |
|
248 } |
|
249 if ( posInfo->IsRequestedField(EPositionFieldTrueCourseError) ) |
|
250 { |
|
251 error = posInfo->SetValue(EPositionFieldTrueCourseError, static_cast<TReal32>(course.CourseAccuracy())); |
|
252 if(error != KErrNone) |
|
253 { |
|
254 return error; |
|
255 } |
|
256 } |
|
257 if ( posInfo->IsRequestedField(EPositionFieldHeading) ) |
|
258 { |
|
259 error = posInfo->SetValue(EPositionFieldHeading, static_cast<TReal32>(course.Heading())); |
|
260 if(error != KErrNone) |
|
261 { |
|
262 return error; |
|
263 } |
|
264 } |
|
265 if ( posInfo->IsRequestedField(EPositionFieldHeadingError) ) |
|
266 { |
|
267 error = posInfo->SetValue(EPositionFieldHeadingError, static_cast<TReal32>(course.HeadingAccuracy())); |
|
268 if(error != KErrNone) |
|
269 { |
|
270 return error; |
|
271 } |
|
272 } |
|
273 } |
|
274 // Have we got extended satellite info? |
|
275 if(aFrom.PositionClassType() & EPositionExtendedSatelliteInfoClass) |
|
276 { |
|
277 // copy any requested course info: |
|
278 const TPositionExtendedSatelliteInfo* extendedSatInfo = static_cast<const TPositionExtendedSatelliteInfo*>(&aFrom); |
|
279 |
|
280 if ( posInfo->IsRequestedField(EPositionFieldMagneticCourse) ) |
|
281 { |
|
282 error = posInfo->SetValue(EPositionFieldMagneticCourse, static_cast<TReal32>(extendedSatInfo->CourseOverGroundMagnetic())); |
|
283 if(error != KErrNone) |
|
284 { |
|
285 return error; |
|
286 } |
|
287 } |
|
288 if ( posInfo->IsRequestedField(EPositionFieldMagneticCourseError) ) |
|
289 { |
|
290 TCourse course; |
|
291 extendedSatInfo->GetCourse(course); |
|
292 error = posInfo->SetValue(EPositionFieldMagneticCourseError, static_cast<TReal32>(course.CourseAccuracy())); |
|
293 if(error != KErrNone) |
|
294 { |
|
295 return error; |
|
296 } |
|
297 } |
|
298 if ( posInfo->IsRequestedField(EPositionFieldMagneticHeading) ) |
|
299 { |
|
300 TCourse course; |
|
301 extendedSatInfo->GetCourse(course); |
|
302 error = posInfo->SetValue(EPositionFieldMagneticHeading, static_cast<TReal32>(course.Heading() - extendedSatInfo->MagneticVariation())); |
|
303 if(error != KErrNone) |
|
304 { |
|
305 return error; |
|
306 } |
|
307 } |
|
308 if ( posInfo->IsRequestedField(EPositionFieldMagneticHeadingError) ) |
|
309 { |
|
310 TCourse course; |
|
311 extendedSatInfo->GetCourse(course); |
|
312 error = posInfo->SetValue(EPositionFieldMagneticHeadingError, static_cast<TReal32>(course.HeadingAccuracy())); |
|
313 if(error != KErrNone) |
|
314 { |
|
315 return error; |
|
316 } |
|
317 } |
|
318 if ( posInfo->IsRequestedField(EPositionFieldSatelliteSeaLevelAltitude) ) |
|
319 { |
|
320 TPosition pos; |
|
321 extendedSatInfo->GetPosition(pos); |
|
322 error = posInfo->SetValue(EPositionFieldSatelliteSeaLevelAltitude, static_cast<TReal32>(pos.Altitude() + extendedSatInfo->GeoidalSeparation())); |
|
323 if(error != KErrNone) |
|
324 { |
|
325 return error; |
|
326 } |
|
327 } |
|
328 if ( posInfo->IsRequestedField(EPositionFieldSatelliteGeoidalSeparation) ) |
|
329 { |
|
330 error = posInfo->SetValue(EPositionFieldSatelliteGeoidalSeparation, static_cast<TReal32>(extendedSatInfo->GeoidalSeparation())); |
|
331 if(error != KErrNone) |
|
332 { |
|
333 return error; |
|
334 } |
|
335 } |
|
336 if ( posInfo->IsRequestedField(EPositionFieldNMEASentences) ) |
|
337 { |
|
338 TBuf8<128> buffer; |
|
339 TUint8 sentences = 0; |
|
340 CreateGga(buffer, *extendedSatInfo); |
|
341 error = posInfo->SetValue(EPositionFieldNMEASentencesStart+sentences, buffer); |
|
342 if(error != KErrNone) |
|
343 { |
|
344 return error; |
|
345 } |
|
346 ++sentences; |
|
347 CreateGll(buffer, *extendedSatInfo); |
|
348 error = posInfo->SetValue(EPositionFieldNMEASentencesStart+sentences, buffer); |
|
349 if(error != KErrNone) |
|
350 { |
|
351 return error; |
|
352 } |
|
353 ++sentences; |
|
354 CreateGsa(buffer, *extendedSatInfo); |
|
355 error = posInfo->SetValue(EPositionFieldNMEASentencesStart+sentences, buffer); |
|
356 if(error != KErrNone) |
|
357 { |
|
358 return error; |
|
359 } |
|
360 ++sentences; |
|
361 CreateGst(buffer, *extendedSatInfo); |
|
362 error = posInfo->SetValue(EPositionFieldNMEASentencesStart+sentences, buffer); |
|
363 if(error != KErrNone) |
|
364 { |
|
365 return error; |
|
366 } |
|
367 ++sentences; |
|
368 // we need at least a GSV, and after the first one we'll know exactly how many are needed |
|
369 TInt totalGsvSentences = 1; |
|
370 TInt lastGsvSentence = 0; |
|
371 while(lastGsvSentence < totalGsvSentences) |
|
372 { |
|
373 CreateGsv(buffer, *extendedSatInfo, totalGsvSentences, lastGsvSentence); |
|
374 error = posInfo->SetValue(EPositionFieldNMEASentencesStart+sentences, buffer); |
|
375 if(error != KErrNone) |
|
376 { |
|
377 return error; |
|
378 } |
|
379 ++sentences; |
|
380 } |
|
381 CreateRmc(buffer, *extendedSatInfo); |
|
382 error = posInfo->SetValue(EPositionFieldNMEASentencesStart+sentences, buffer); |
|
383 if(error != KErrNone) |
|
384 { |
|
385 return error; |
|
386 } |
|
387 ++sentences; |
|
388 CreateVtg(buffer, *extendedSatInfo); |
|
389 error = posInfo->SetValue(EPositionFieldNMEASentencesStart+sentences, buffer); |
|
390 if(error != KErrNone) |
|
391 { |
|
392 return error; |
|
393 } |
|
394 ++sentences; |
|
395 error = posInfo->SetValue(EPositionFieldNMEASentences, sentences); |
|
396 if(error != KErrNone) |
|
397 { |
|
398 return error; |
|
399 } |
|
400 |
|
401 } |
|
402 } |
|
403 |
|
404 //copy position info |
|
405 __ASSERT_DEBUG( (aTo.PositionClassType() & EPositionInfoClass) && (aFrom.PositionClassType() & EPositionInfoClass), User::Invariant()); |
|
406 |
|
407 TPositionInfo* posInfoTo = static_cast<TPositionInfo*>(&aTo); |
|
408 const TPositionInfo* posInfoFrom = static_cast<const TPositionInfo*>(&aFrom); |
|
409 TPosition posFrom; |
|
410 posInfoFrom->GetPosition(posFrom); |
|
411 |
|
412 posInfoTo->SetPosition(posFrom); |
|
413 posInfoTo->SetModuleId(posInfoFrom->ModuleId()); |
|
414 posInfoTo->SetPositionMode(posInfoFrom->PositionMode()); |
|
415 posInfoTo->SetPositionModeReason(posInfoFrom->PositionModeReason()); |
|
416 posInfoTo->SetUpdateType(posInfoFrom->UpdateType()); |
|
417 |
|
418 return KErrNone; |
|
419 } |
|
420 /** |
|
421 Check the size for supported position info type |
|
422 |
|
423 @param aType the position info type |
|
424 @param aSize the size of specified type |
|
425 @return Symbian standard error code |
|
426 */ |
|
427 TInt SupportedType(const TUint32& aType, const TInt& aSize) |
|
428 { |
|
429 if(aType==EPositionInfoClass) |
|
430 { |
|
431 if(aSize!=sizeof(TPositionInfo)) |
|
432 { |
|
433 return KErrNotSupported; // something weird. Type ok but the size is wrong |
|
434 } |
|
435 } |
|
436 else if(aType==(EPositionInfoClass|EPositionCourseInfoClass)) |
|
437 { |
|
438 if(aSize!=sizeof(TPositionCourseInfo)) |
|
439 { |
|
440 return KErrNotSupported; // something weird. Type ok but the size is wrong |
|
441 } |
|
442 } |
|
443 else if(aType==(EPositionInfoClass|EPositionCourseInfoClass|EPositionSatelliteInfoClass)) |
|
444 { |
|
445 if(aSize!=sizeof(TPositionSatelliteInfo)) |
|
446 { |
|
447 return KErrNotSupported; // something weird. Type ok but the size is wrong |
|
448 } |
|
449 } |
|
450 else if(aType==(EPositionInfoClass|EPositionCourseInfoClass|EPositionSatelliteInfoClass|EPositionExtendedSatelliteInfoClass)) |
|
451 { |
|
452 if(aSize!=sizeof(TPositionExtendedSatelliteInfo)) |
|
453 { |
|
454 return KErrNotSupported; // something weird. Type ok but the size is wrong |
|
455 } |
|
456 } |
|
457 else if(aType == (EPositionInfoClass|EPositionGenericInfoClass)) |
|
458 { |
|
459 if(aSize < sizeof(HPositionGenericInfo)) |
|
460 { |
|
461 return KErrNotSupported; // something weird. Type ok but the size is wrong |
|
462 } |
|
463 } |
|
464 else // no other types supported |
|
465 { |
|
466 return KErrNotSupported; |
|
467 } |
|
468 // othwerwise the size and type are what we expected |
|
469 return KErrNone; |
|
470 } |
|
471 |
|
472 |
|
473 TBool Partial(const TPosition& aPos) |
|
474 { |
|
475 TBool partial = EFalse; |
|
476 TReal64 lat = aPos.Latitude(); |
|
477 TReal64 lng = aPos.Longitude(); |
|
478 if(Math::IsNaN(lat) || Math::IsNaN(lng)) |
|
479 { |
|
480 partial = ETrue; |
|
481 } |
|
482 return partial; |
|
483 } |
|
484 |
|
485 // |