|
1 /* |
|
2 * Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies). |
|
3 * All rights reserved. |
|
4 * This component and the accompanying materials are made available |
|
5 * under the terms of "Eclipse Public License v1.0" |
|
6 * which accompanies this distribution, and is available |
|
7 * at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
8 * |
|
9 * Initial Contributors: |
|
10 * Nokia Corporation - initial contribution. |
|
11 * |
|
12 * Contributors: |
|
13 * |
|
14 * Description: Implementation of the GPS Watcher |
|
15 * |
|
16 */ |
|
17 |
|
18 |
|
19 // INCLUDE FILES |
|
20 #include <e32base.h> |
|
21 #include <e32svr.h> |
|
22 |
|
23 #include "GPSWatcher.h" |
|
24 #include "GPSTimeUpdater.h" |
|
25 #include "DRMClock.h" |
|
26 #include "drmlog.h" |
|
27 |
|
28 // Wait time in microseconds: wait 5 minutes |
|
29 const TInt KGpsDefaultWaitTime = 300000000; |
|
30 const TInt KMaxRetryCounter = 20; |
|
31 |
|
32 // ============================ MEMBER FUNCTIONS =============================== |
|
33 |
|
34 |
|
35 // ----------------------------------------------------------------------------- |
|
36 // CGPSWatcher::CGPSWatcher |
|
37 // C++ default constructor can NOT contain any code, that |
|
38 // might leave. |
|
39 // ----------------------------------------------------------------------------- |
|
40 // |
|
41 CGPSWatcher::CGPSWatcher( CDRMClock* aClock ) : |
|
42 CTimer(EPriorityHigh), |
|
43 iClock( aClock ), |
|
44 iRetryCounter( 0 ) |
|
45 { |
|
46 CActiveScheduler::Add(this); |
|
47 } |
|
48 |
|
49 // ----------------------------------------------------------------------------- |
|
50 // CGPSWatcher::~CGPSWatcher |
|
51 // C++ destructor |
|
52 // ----------------------------------------------------------------------------- |
|
53 // |
|
54 CGPSWatcher::~CGPSWatcher() |
|
55 { |
|
56 Cancel(); |
|
57 |
|
58 delete iTimeUpdater; |
|
59 iTimeUpdater = 0; |
|
60 |
|
61 iPosServer.Close(); |
|
62 } |
|
63 |
|
64 // ----------------------------------------------------------------------------- |
|
65 // CGPSWatcher::NewL |
|
66 // Two-phased constructor |
|
67 // ----------------------------------------------------------------------------- |
|
68 // |
|
69 CGPSWatcher* CGPSWatcher::NewL( CDRMClock* aClock ) |
|
70 { |
|
71 CGPSWatcher* self = new (ELeave) CGPSWatcher( aClock ); |
|
72 |
|
73 CleanupStack::PushL(self); |
|
74 self->ConstructL(); |
|
75 CleanupStack::Pop(self); |
|
76 |
|
77 return self; |
|
78 } |
|
79 |
|
80 // ----------------------------------------------------------------------------- |
|
81 // CGPSWatcher::ConstructL |
|
82 // Symbian 2nd phase constructor can leave. |
|
83 // ----------------------------------------------------------------------------- |
|
84 // |
|
85 void CGPSWatcher::ConstructL() |
|
86 { |
|
87 CTimer::ConstructL(); |
|
88 |
|
89 User::LeaveIfError(iPosServer.Connect()); |
|
90 |
|
91 // Immediately subscribe for module status events |
|
92 iStatusEvent.SetRequestedEvents(TPositionModuleStatusEventBase::EEventDeviceStatus); |
|
93 Subscribe(); |
|
94 |
|
95 // Check initial state |
|
96 CheckModules(); |
|
97 } |
|
98 |
|
99 // ----------------------------------------------------------------------------- |
|
100 // CGPSWatcher::Subscribe |
|
101 // Subscribe to position events |
|
102 // ----------------------------------------------------------------------------- |
|
103 // |
|
104 void CGPSWatcher::Subscribe() |
|
105 { |
|
106 iPosServer.NotifyModuleStatusEvent(iStatusEvent, iStatus); |
|
107 SetActive(); |
|
108 } |
|
109 |
|
110 // ----------------------------------------------------------------------------- |
|
111 // CGPSWatcher::RunL |
|
112 // Inherited from CActive |
|
113 // ----------------------------------------------------------------------------- |
|
114 // |
|
115 void CGPSWatcher::RunL() |
|
116 { |
|
117 // If there are errors just leave and stop watching: |
|
118 DRMLOG2(_L("CGPSWatcher::RunL: error: '%d'"), iStatus.Int()); |
|
119 |
|
120 // If we already updated the time: |
|
121 if( iTimeUpdater && iTimeUpdater->TimeReceived() ) |
|
122 { |
|
123 DRMLOG( _L("CGPSWatcher::RunL: time updater exists, time received, deleting")); |
|
124 iClock->Notify( CDRMClock::ENotifyGPSTimeReceived ); |
|
125 DRMLOG( _L("CGPSWatcher::RunL: object deleted, returning")); |
|
126 return; |
|
127 } |
|
128 |
|
129 // The server was killed: |
|
130 if( iStatus.Int() == KErrServerTerminated ) |
|
131 { |
|
132 if( iRetryCounter > KMaxRetryCounter ) |
|
133 { |
|
134 DRMLOG( _L("CGPSWatcher::RunL: Maximum retries reached, stopping watcher.") ); |
|
135 iClock->Notify( CDRMClock::ENotifyGPSTimeReceived ); |
|
136 return; |
|
137 } |
|
138 // If there are errors just leave and stop watching: |
|
139 DRMLOG2(_L("CGPSWatcher::RunL: server terminated, waiting '%d' microseconds"), KGpsDefaultWaitTime); |
|
140 After( TTimeIntervalMicroSeconds32(KGpsDefaultWaitTime) ); |
|
141 return; |
|
142 } |
|
143 |
|
144 Subscribe(); |
|
145 TPositionModuleStatusEventBase::TModuleEvent occurredEvents = iStatusEvent.OccurredEvents(); |
|
146 DRMLOG2(_L("CGPSWatcher::RunL: occurredEvents = 0x%X"), occurredEvents); |
|
147 |
|
148 |
|
149 // If time updater is not running, check module statuses |
|
150 if(!iTimeUpdater) |
|
151 CheckModules(); |
|
152 } |
|
153 |
|
154 |
|
155 // ----------------------------------------------------------------------------- |
|
156 // CGPSWatcher::DoCancel |
|
157 // Inherited from CActive |
|
158 // ----------------------------------------------------------------------------- |
|
159 // |
|
160 void CGPSWatcher::DoCancel() |
|
161 { |
|
162 iPosServer.CancelRequest(EPositionServerNotifyModuleStatusEvent); |
|
163 } |
|
164 |
|
165 // ----------------------------------------------------------------------------- |
|
166 // CGPSWatcher::RunError |
|
167 // Inherited from CActive |
|
168 // ----------------------------------------------------------------------------- |
|
169 // |
|
170 TInt CGPSWatcher::RunError( TInt /*aError*/ ) |
|
171 { |
|
172 // ignore errors |
|
173 return KErrNone; |
|
174 } |
|
175 |
|
176 // ----------------------------------------------------------------------------- |
|
177 // CGPSWatcher::CheckModules |
|
178 // Check what modules are present and act accordingly |
|
179 // ----------------------------------------------------------------------------- |
|
180 // |
|
181 void CGPSWatcher::CheckModules() |
|
182 { |
|
183 // Get number of modules |
|
184 TUint numModules; |
|
185 if(iPosServer.GetNumModules(numModules)!=KErrNone || !numModules) |
|
186 return; |
|
187 |
|
188 // Collect all active and satellite capable modules |
|
189 TFullName moduleName; |
|
190 RPointerArray<TPositionModuleInfo> satelliteModules; |
|
191 |
|
192 TPositionModuleInfo *moduleInfo(0); |
|
193 |
|
194 for(TUint i=0; i<numModules; i++) |
|
195 { |
|
196 if(!moduleInfo) |
|
197 { |
|
198 moduleInfo = new TPositionModuleInfo; |
|
199 if(!moduleInfo) |
|
200 { |
|
201 continue; |
|
202 } |
|
203 } |
|
204 |
|
205 // Get module info from the server |
|
206 if(iPosServer.GetModuleInfoByIndex(i, *moduleInfo) != KErrNone) |
|
207 { |
|
208 continue; |
|
209 } |
|
210 |
|
211 // Check if the module is internal and satellite capable |
|
212 if(! (moduleInfo->DeviceLocation() & TPositionModuleInfo::EDeviceInternal) || |
|
213 ! (moduleInfo->Capabilities() & TPositionModuleInfo::ECapabilitySatellite) ) |
|
214 { |
|
215 // Not internal or satellite capable |
|
216 continue; |
|
217 } |
|
218 |
|
219 // Get module status |
|
220 TPositionModuleStatus moduleStatus; |
|
221 if(iPosServer.GetModuleStatus(moduleStatus, moduleInfo->ModuleId()) != KErrNone) |
|
222 { |
|
223 continue; |
|
224 } |
|
225 // Check if the module is active |
|
226 if(moduleStatus.DeviceStatus() != TPositionModuleStatus::EDeviceActive) |
|
227 { |
|
228 continue; |
|
229 } |
|
230 |
|
231 moduleInfo->GetModuleName(moduleName); |
|
232 |
|
233 DRMLOG6( _L("Module %d: id=0x%08X, name = %S, cap=0x%08X, status=%d"), i, moduleInfo->ModuleId().iUid, &moduleName, moduleInfo->Capabilities(), moduleStatus.DeviceStatus()); |
|
234 |
|
235 // Active internal satellite device, try to append in the array |
|
236 if(satelliteModules.Append(moduleInfo) == KErrNone) |
|
237 { |
|
238 moduleInfo = 0; |
|
239 } |
|
240 } |
|
241 |
|
242 delete moduleInfo; |
|
243 |
|
244 if(satelliteModules.Count()) |
|
245 { |
|
246 DRMLOG( _L("Active satellite module available") ); |
|
247 // We have an active satellite module available |
|
248 TPositionModuleId moduleId = satelliteModules[0]->ModuleId(); |
|
249 |
|
250 // Just allocating/deleting the time updater will do the trick |
|
251 iTimeUpdater = CGPSTimeUpdater::New(iPosServer, moduleId, iClock); |
|
252 } |
|
253 else |
|
254 { |
|
255 DRMLOG( _L("No active satellite modules available") ); |
|
256 // Delete time updater since no active GPS anymore |
|
257 delete iTimeUpdater; iTimeUpdater = 0; |
|
258 } |
|
259 |
|
260 satelliteModules.ResetAndDestroy(); |
|
261 satelliteModules.Close(); |
|
262 } |