|
1 /* |
|
2 * Name : Position.cpp |
|
3 * Description : Position helper class |
|
4 * Project : This file is part of OpenMAR, an Open Mobile Augmented Reality browser |
|
5 * Website : http://OpenMAR.org |
|
6 * |
|
7 * Copyright (c) 2010 David Caabeiro |
|
8 * |
|
9 * All rights reserved. This program and the accompanying materials are made available |
|
10 * under the terms of the Eclipse Public License v1.0 which accompanies this |
|
11 * distribution, and is available at http://www.eclipse.org/legal/epl-v10.html |
|
12 * |
|
13 */ |
|
14 |
|
15 #include "Position.h" |
|
16 |
|
17 #include "Logger.h" |
|
18 |
|
19 CPosition* CPosition::NewL(MPositionObserver& aObserver) |
|
20 { |
|
21 CPosition* self = new(ELeave) CPosition(aObserver); |
|
22 CleanupStack::PushL(self); |
|
23 self->ConstructL(); |
|
24 CleanupStack::Pop(self); |
|
25 |
|
26 return self; |
|
27 } |
|
28 |
|
29 CPosition::~CPosition() |
|
30 { |
|
31 Cancel(); |
|
32 |
|
33 iPositioner.Close(); |
|
34 iServer.Close(); |
|
35 } |
|
36 |
|
37 CPosition::CPosition(MPositionObserver& aObserver) |
|
38 : CActive(CActive::EPriorityStandard), iObserver(aObserver), iState(EUnknown) |
|
39 { |
|
40 CActiveScheduler::Add(this); |
|
41 } |
|
42 |
|
43 void CPosition::ConstructL() |
|
44 { |
|
45 User::LeaveIfError(iServer.Connect()); |
|
46 User::LeaveIfError(iPositioner.Open(iServer)); |
|
47 |
|
48 _LIT(KRequestor, "OpenMAR" ); |
|
49 User::LeaveIfError(iPositioner.SetRequestor(CRequestor::ERequestorService, CRequestor::EFormatApplication, KRequestor)); |
|
50 |
|
51 const TInt KSecond = 1000000; |
|
52 TPositionUpdateOptions updateOptions; |
|
53 updateOptions.SetUpdateInterval(TTimeIntervalMicroSeconds(10 * KSecond)); |
|
54 updateOptions.SetUpdateTimeOut(TTimeIntervalMicroSeconds(30 * KSecond)); |
|
55 updateOptions.SetMaxUpdateAge(TTimeIntervalMicroSeconds(1 * KSecond)); |
|
56 updateOptions.SetAcceptPartialUpdates(EFalse); |
|
57 |
|
58 User::LeaveIfError(iPositioner.SetUpdateOptions(updateOptions)); |
|
59 } |
|
60 |
|
61 void CPosition::Request() |
|
62 { |
|
63 if (IsActive()) |
|
64 Cancel(); |
|
65 |
|
66 iPositioner.GetLastKnownPosition(iPositionInfo, iStatus); |
|
67 SetActive(); |
|
68 |
|
69 iState = ECache; |
|
70 } |
|
71 |
|
72 void CPosition::RunL() |
|
73 { |
|
74 TPosition position; |
|
75 iPositionInfo.GetPosition(position); |
|
76 |
|
77 TTime now; |
|
78 now.UniversalTime(); |
|
79 |
|
80 TTimeIntervalSeconds interval = 0; |
|
81 now.SecondsFrom(position.Time(), interval); |
|
82 |
|
83 LOGARG("Interval between retrieved position and current time: %d secs", interval.Int()); |
|
84 |
|
85 // Compare that retrieved data is not outdated |
|
86 if (iStatus == KErrNone && interval.Int() < 300) |
|
87 { |
|
88 iObserver.PositionUpdateL(iStatus.Int(), position); |
|
89 } |
|
90 else if (iStatus == KErrTimedOut) |
|
91 { |
|
92 iObserver.PositionUpdateL(iStatus.Int(), position); |
|
93 } |
|
94 else |
|
95 { |
|
96 iPositioner.NotifyPositionUpdate(iPositionInfo, iStatus); |
|
97 SetActive(); |
|
98 |
|
99 iState = EGps; |
|
100 } |
|
101 } |
|
102 |
|
103 void CPosition::DoCancel() |
|
104 { |
|
105 switch (iState) |
|
106 { |
|
107 case ECache: |
|
108 iPositioner.CancelRequest(EPositionerGetLastKnownPosition); |
|
109 break; |
|
110 |
|
111 case EGps: |
|
112 iPositioner.CancelRequest(EPositionerNotifyPositionUpdate); |
|
113 break; |
|
114 |
|
115 default: |
|
116 break; |
|
117 }; |
|
118 } |
|
119 |
|
120 TInt CPosition::RunError(TInt aError) |
|
121 { |
|
122 return KErrNone; |
|
123 } |