summaryrefslogtreecommitdiff
path: root/src/FLoc_LocationManager.h
blob: 04de9d6388d46a0f7cfe815f2bd813ef6defb4e6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
//
// Open Service Platform
// Copyright (c) 2012 Samsung Electronics Co., Ltd.
//
// Licensed under the Apache License, Version 2.0 (the License);
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//

/**
 * @file	FLoc_LocationManager.h
 * @brief	This is the header file for the %_LocationManager class.
 *
 * This header file contains the declarations of the %_LocationManager class methods.
 */

#ifndef _FLOC_INTERNAL_LOCATION_MANAGER_H_
#define _FLOC_INTERNAL_LOCATION_MANAGER_H_

#include <unique_ptr.h>
#include <alarm.h>
#include <location/locations.h>
#include <FBaseColAllElementsDeleter.h>
#include <FBaseDateTime.h>
#include <FBaseRtEventDrivenThread.h>
#include <FLocLocation.h>
#include <FSysISettingEventListener.h>
#include "FLoc_ILocationManagerListener.h"
#include "FLoc_Types.h"

namespace Tizen { namespace Locations
{
class _AlarmRequestInfo;
class _LocationMonitor;
class _LocationRequestInfo;
class _SyncLocationRequestInfo;

class _LocationManager
	: public Tizen::Base::Runtime::EventDrivenThread
	, public Tizen::Base::Runtime::ITimerEventListener
	, public Tizen::System::ISettingEventListener
{
public:
	// This method adds the location request into the list of requests and requests for location updates from core location provider.
	//
	// @since 2.0
	//
	result StartLocationUpdates(LocationAccuracy accuracy, int interval, _ILocationManagerListener* pListener, RequestId& reqId);

	// This method removes the location request wrt to the reqId provided. If the list entries is zero, then stops the core location provider.
	//
	// @since 2.0
	//
	result StopLocationUpdates(RequestId reqId);

	// This method updates the requested interval from the location provider.
	//
	// @since 2.0
	//
	result ChangeUpdateInterval(RequestId reqId, int interval);

	// This method initiates the synchronous location retrieval. The location monitor instance provided is used to intimate back the location.
	//
	// @since 2.0
	//
	result RegisterLocationMonitor(_LocationMonitor* pLocationMonitor);

	// This method adds the alarm request into the list of requests and requests for alarm from the core alarm manager.
	//
	// @since 2.0
	//
	result RequestStartAlarm(int interval, _ILocationManagerListener* pListener, RequestId reqId);

	// This method removes the alarm request from the list of requests and requests the removal of alarm from the core alarm manager.
	//
	// @since 2.0
	//
	result RequestStopAlarm(RequestId reqId);

	// This method returns the stored location.
	//
	// @since 2.0
	//
	Location GetLastKnownLocation(void);

	// This method converts the horizontal accuracy in meters to the LocationAccuracy enum type.
	//
	// @since 2.0
	//
	LocationAccuracy GetAccuracyLevel(double horAcc) const;

	// This method returns the single instance of the location maanger.
	//
	// @since 2.0
	//
	static _LocationManager* GetInstance(void);

private:
	// This default constructor is intentionally declared as private to implement the Singleton semantic.
	//
	// @since 2.0
	//
	_LocationManager(void);

	// The implementation of this copy constructor is intentionally blank and declared as private to prohibit copying of objects.
	//
	// @since 2.0
	//
	_LocationManager(const _LocationManager& value);

	// This destructor is intentionally declared as private to implement the Singleton semantic.
	//
	// @since 2.0
	//
	~_LocationManager(void);

	// The implementation of this copy assignment operator is intentionally blank and declared as private to prohibit copying of objects.
	//
	// @since 2.0
	//
	_LocationManager& operator =(const _LocationManager& rhs);

	// The method adds the _LocationRequestInfo instance to the array list.
	//
	// @since 2.0
	//
	void AddToLocRequestInfoList(const _LocationRequestInfo* pLocRequestInfo);

	// The method removes the _LocationRequestInfo instance corresponding to a reqId from the array list.
	//
	// @since 2.0
	//
	void RemoveFromLocRequestInfoList(RequestId reqId);

	// The method adds the _SyncLocationRequestInfo instance to the array list.
	//
	// @since 2.0
	//
	void AddToSyncLocationRequestInfoList(_SyncLocationRequestInfo& syncLocRequestInfo);

	// The method removes the _SyncLocationRequestInfo instance from the list.
	//
	// @since 2.0
	//
	void RemoveSyncLocRetrievalRequest(_SyncLocationRequestInfo& syncLocRequestInfo);

	// The method adds the _AlarmRequestInfo instance corresponding to a reqId from the array list.
	//
	// @since 2.0
	//
	void AddToAlarmRequestInfoList(_AlarmRequestInfo& alarmRequestInfo);

	// The method removes the _AlarmRequestInfo instance from the list.
	//
	// @since 2.0
	//
	void RemoveFromAlarmRequestInfoList(RequestId reqId);

	// The method updates the minimum accuracy required.
	//
	// @since 2.2
	//
	void UpdateMinimunAccuracy(void);

	// The method starts the core location provider when the location is requested during Location manager idle state.
	//
	// @since 2.2
	//
	void InitiateLocationRequest(void);

	// The method restarts the callback timer after calculating the GCD of the timeout values requested by each location provider.
	//
	// @since 2.2
	//
	void RestartCallbackTimer(void);

	// The method is called everytime the sync retrieval timer is expired.
	//
	// @since 2.0
	//
	void HandleSyncRetrievalTimerExpiry(_SyncLocationRequestInfo& syncLocRequestInfo);

	// The method is called to send back the callbacks in case of async location updates.
	//
	// @since 2.0
	//
	void SendLocationCallbacks(void);

	// This method resets the member variables.
	//
	// @since 2.0
	//
	void Reset(void);

	// The method is wait for OnStart method.
	//
	// @since 2.0
	//
	result WaitUntilThreadStart();

	// The method calls the construct method of the event driven thread.
	//
	// @since 2.0
	//
	result Construct(void);

	// This method is gets the last known location from Native side for the given method.
	//
	// @since 2.0
	//
	Location GetLastKnownLocation(location_method_e nativeLocMethod);

	// This method returns the most recent location among available last known locations.
	//
	// @since 2.0
	//
	Location GetRecentLocationAvailable(void);

	// This method updates the timer interval for a particular request Id.
	//
	// @since 2.0
	//
	void UpdateLocRequestInterval(RequestId reqId, int interval);

	// This method gets the better location among the three location providers.
	//
	// @since 2.0
	//
	const Location* FindBestLocation(void);

	// This method gets the location from the native side depending on the location handle.
	//
	// @since 2.0
	//
	result GetLocation(location_method_e nativeLocMethod);

	// This method handles the location information obtained from core location provider/s when the location manager state is LOC_MGR_STATE_BOTH_GPS_WPS.
	//
	// @since 2.2
	//
	void HandleBothGpsWpsState(void);

	// This method handles the location information obtained from core GPS location provider when the location manager state is LOC_MGR_STATE_GPS_ONLY.
	//
	// @since 2.2
	//	
	void HandleGpsOnlyState(void);

	// This method handles the location information obtained from core WPS location provider when the location manager state is LOC_MGR_STATE_WPS_ONLY.
	//
	// @since 2.2
	//
	void HandleWpsOnlyState(void);

	// This method checks the validity of location updated by GPS core provider is valid or not.
	//
	// @since 2.2
	//
	void IsGpsLocationUpdated(bool& isTimeUpdated, bool& isAccMet);

	// This method checks the validity of location updated by WPS core provider is valid or not.
	//
	// @since 2.2
	//
	void IsWpsLocationUpdated(bool& isTimeUpdated, bool& isAccMet);	

	// This method converts the LocationAccuracy enum variable to corresponding double value.
	//
	// @since 2.2
	//
	double ConvertLocationAccuracyToDouble(LocationAccuracy locAcc);

	// This method stops if timer is running and starts the sub state timer for the given interval.
	//
	// @since 2.2
	//
	result RestartSubStateTimer(int timeout);

	// This method stops the sub state timer if it is running.
	//
	// @since 2.2
	//
	void StopSubStateTimer(void);	

	// This method handles the expiry of the sub state.
	//
	// @since 2.2
	//
	void HandleSubStateTimerExpiry(void);

	// This method handles the expiry of the location check timer.
	//
	// @since 2.2
	//
	void HandleLocationCheckTimerExpiry(void);	

	// This method Covert error code for native error.
	//
	// @since 2.1
	//
	inline double ConvertToNativeFormat(double value)
	{
		return value < 0.0 ? Tizen::Locations::NaN : value;
	}

	// @see @ref Tizen::Base::Runtime::EventDrivenThread::OnStart()
	//
	// @since 2.0
	//
	virtual bool OnStart(void);

	// @see @ref Tizen::Base::Runtime::EventDrivenThread::OnStop()
	//
	// @since 2.0
	//
	virtual void OnStop(void);

	// @see @ref Tizen::Base::Runtime::EventDrivenThread::OnUserEventReceivedN()
	//
	// @since 2.0
	//
	virtual void OnUserEventReceivedN(RequestId requestId, Tizen::Base::Collection::IList* pArgs);

	// @see @ref Tizen::Base::Runtime::ITimerEventListener::OnTimerExpired()
	//
	// @since 2.0
	//
	virtual void OnTimerExpired(Tizen::Base::Runtime::Timer& timer);

	// @see @ref Tizen::System::ISettingEventListener::OnSettingChanged()
	//
	// @since 2.1
	//
	virtual void OnSettingChanged(Tizen::Base::String& key);

	// This callback is recieved from native location manager for satellite information.
	//
	// @since 2.0
	//
	static bool SatelliteInfoUpdated(unsigned int azimuth, unsigned int elevation, unsigned int prn, int snr, bool is_active, void* user_data);

	// This method is called by the native location provider when the service state of GPS method is changed.
	//
	// @since 2.0
	//
	static void GpsServiceUpdateCallback(location_service_state_e state, void* user_data);

	// This method is called by the native location provider when the service state of WPS method is changed.
	//
	// @since 2.0
	//
	static void WpsServiceUpdateCallback(location_service_state_e state, void* user_data);

	// This method is called by the core alarm manager when the requested alarm is expired.
	//
	// @since 2.0
	//
	static int AlarmExpiryCallback(alarm_id_t alarm_id, void* user_param);

	// This method initializes the single instance of the location manager.
	//
	// @since 2.0
	//
	static void InitLocationManager(void);

	// This method is called when the location manager thread is destroyed.
	//
	// @since 2.0
	//
	static void DestroyLocationManager(void);

private:
	const static RequestId REQ_ID_START_LOC_UPDATES = 1;
	const static RequestId REQ_ID_STOP_LOC_UPDATES = 2;
	const static RequestId REQ_ID_SYNC_LOC_RETRIEVAL = 3;
	const static RequestId REQ_ID_GET_LAST_LOCATION = 4;
	const static RequestId REQ_ID_UPDATE_INTERVAL = 5;
	const static RequestId REQ_ID_START_ALARM = 6;
	const static RequestId REQ_ID_STOP_ALARM = 7;

	enum _LocationManagerState
	{
		LOC_MGR_STATE_IDLE,
		LOC_MGR_STATE_PASSIVE_IDLE,
		LOC_MGR_STATE_BOTH_GPS_WPS,
		LOC_MGR_STATE_GPS_ONLY,
		LOC_MGR_STATE_WPS_ONLY
	};

	enum _LocationManagerSubState
	{
		LM_SS_IDLE,
		LM_SS_SETTLED,
		LM_SS_SENSING,
		LM_SS_NO_FIX,
		LM_SS_BOTH_SENSING,
		LM_SS_BOTH_SETTLED_WAITING,
		LM_SS_GPS_ONLY_SETTLED,
		LM_SS_GPS_ONLY_SENSING,
		LM_SS_GPS_ONLY_NO_FIX,
		LM_SS_WPS_ONLY_LESS_ACC_SENSING,
		LM_SS_WPS_ONLY_FINE_ACC_SENSING,
		LM_SS_WPS_ONLY_SENSING,
		LM_SS_WPS_ONLY_NO_FIX
	};

	class _LocationManagerHandle
	{
	public:
		_LocationManagerHandle(void)
			: serviceState(LOCATIONS_SERVICE_DISABLED)
			, handle(null)
			, pLocation(null)
		{
			oldTimeStamp.SetValue(1970, 1, 1);
		}

		~_LocationManagerHandle(void)
		{
		}

	public:
		location_service_state_e serviceState;
		location_manager_h handle;
		Tizen::Base::DateTime oldTimeStamp;
		std::unique_ptr< Tizen::Locations::Location > pLocation;
	};

	class _LocationManagerStatus
	{
	public:
		_LocationManagerStatus(void)
			: isSubStateTimerStarted(false)
			, locMgrState(LOC_MGR_STATE_IDLE)
			, locMgrSubState(LM_SS_IDLE)
			, gpsFailCount(0)
		{
		}
		~_LocationManagerStatus(void)
		{
		}

	public:
		bool isSubStateTimerStarted;
		_LocationManagerState locMgrState;
		_LocationManagerSubState locMgrSubState;
		int gpsFailCount;
		Tizen::Base::DateTime gpsTimeAtSubstateTimerStart;
		std::unique_ptr< Tizen::Base::Runtime::Timer > pLocMgrSubStateTimer;
		_LocationManagerHandle gpsHandler;
		_LocationManagerHandle wpsHandler;
	};

	bool __gpsEnabled;
	bool __wpsEnabled;
	_LocationManagerStatus __locMgrStatus;
	LocationAccuracy __minRequestedAccuracy;
	int __timerInterval;
	int __timerTicks;
	std::unique_ptr< Tizen::Base::Collection::ArrayList, Tizen::Base::Collection::AllElementsDeleter > __pLocRequestInfoList;
	std::unique_ptr< Tizen::Base::Collection::ArrayList, Tizen::Base::Collection::AllElementsDeleter > __pSyncLocRequestInfoList;
	std::unique_ptr< Tizen::Base::Collection::ArrayList, Tizen::Base::Collection::AllElementsDeleter > __pAlarmRequestInfoList;
	std::unique_ptr< Tizen::Base::Runtime::Timer > __pLocCallbackTimer;
	std::unique_ptr< Tizen::Base::Runtime::Timer > __pLocCheckTimer;
	Tizen::Base::Runtime::Monitor* __pInitMonitor;
	static _LocationManager* __pUniqueInstance;

	friend class std::default_delete< _LocationManager >;
};      // class _LocationManager
}}  // Tizen::Locations
#endif // _FLOC_INTERNAL_LOCATION_MANAGER_H_