2 * Copyright (c) 2016 Samsung Electronics Co., Ltd.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 #ifndef _CONTEXT_PLACE_RECOGNITION_LOCATION_LOGGER_H_
18 #define _CONTEXT_PLACE_RECOGNITION_LOCATION_LOGGER_H_
20 #include <locations.h>
21 #include <TimerManager.h>
22 #include "VisitListenerIface.h"
23 #include "LocationListenerIface.h"
25 /* Database usage flag */
26 #define LOCATION_LOGGER_DATABASE false
28 /* Locations measure method */
29 #define LOCATION_LOGGER_METHOD LOCATIONS_METHOD_HYBRID
31 /* TIMEOUTS: Location active measure request timeout (in seconds). */
32 #define LOCATION_LOGGER_ACTIVE_REQUEST_TIMEOUT_SECONDS 100
34 /* TIMEOUTS: Location service start timeout (in minutes). */
35 #define LOCATION_LOGGER_SERVICE_START_TIMEOUT_MINUTES 2
37 /* FREQUENCIES/INTERVALS: "Active" measure attempts frequency (in minutes) */
38 #define LOCATION_LOGGER_ACTIVE_INTERVAL_MINUTES 5
40 /* FREQUENCIES/INTERVALS: "Passive" measure attempts frequency (in minutes) */
41 #define LOCATION_LOGGER_PASSIVE_INTERVAL_MINUTES 30
43 /* ATTEMTS LIMITS: "Active" request attempts limit (must be <= than active location attempts) */
44 #define LOCATION_LOGGER_MAX_ACTIVE_REQUEST_ATTEMPTS 0
46 /* ATTEMTS LIMITS: "Active" measures attempts limit (must be <= than all attempts limit) */
47 #define LOCATION_LOGGER_MAX_ACTIVE_LOCATION_ATTEMPTS 2
49 /* ATTEMTS LIMITS: All attempts ("active" + "passive") limit */
50 #define LOCATION_LOGGER_MAX_LOCATION_ATTEMPTS 3
52 /* LOCATION LIMIT: Location count limit per visit */
53 #define LOCATION_LOGGER_MAX_LOCATION_COUNT 3
58 LOCATION_LOGGER_WAITING_FOR_ACTIVE_REQUEST = 0,
59 LOCATION_LOGGER_WAITING_FOR_SERVICE_START = 1,
60 LOCATION_LOGGER_WAITING_FOR_LOCATION_METHOD_SETTING_ON = 2,
61 LOCATION_LOGGER_WAITING_FOR_ACTIVE_INTERVAL = 3,
62 LOCATION_LOGGER_WAITING_FOR_PASSIVE_INTERVAL = 4
65 class LocationLogger : public ITimerListener, public IVisitListener {
73 ILocationListener * const __listener;
74 void __broadcast(LocationEvent locationEvent);
77 void __startLogging();
79 void __locationRequest();
80 void __onActiveRequestSucceeded();
81 void __onActiveLocationSucceeded();
83 /* INTERNAL : COUNTERS (LIMITS) */
84 int __activeRequestAttempts;
88 bool __checkGeneralLimits();
89 bool __checkActiveLimits();
90 bool __checkActiveRequestLimits();
92 /* INTERNAL : FLAGS */
93 bool __activeRequestSucceeded;
94 bool __activeLocationSucceeded;
98 time_t __timerTimestamp;
99 TimerManager __timerManager;
100 TimerPurpose __timerPurpose;
101 void __setNextTimer();
102 void __activeRequestTimerStart();
103 void __startServiceTimerStart();
104 void __activeIntervalTimerStart();
105 void __passiveIntervalTimerStart();
106 void __timerStart(time_t minutes);
108 bool onTimerExpired(int timerId);
111 static int __dbCreateTable();
112 int __dbInsertLog(LocationEvent locationEvent);
115 static const char* __locationError2Str(int error);
116 static void __log(location_accessibility_state_e state);
118 /* LOCATION MANAGER */
119 location_manager_h __locationManager;
120 void __locationManagerCreate();
121 void __locationManagerDestroy();
122 void __locationManagerStart();
123 void __locationManagerStop();
124 location_accessibility_state_e __locationManagerGetAccessibilityState();
126 /* LOCATION MANAGER : LOCATION SERVICE STATE */
127 location_service_state_e __locationServiceState;
128 static void __locationServiceStateChangedCb(location_service_state_e state, void *userData);
129 void __locationManagerSetServiceStateChangedCb();
130 void __locationManagerUnsetServiceStateChangedCb();
132 /* LOCATION MANAGER : LOCATION METHOD SETTINGS */
133 location_method_e __locationMethod;
134 bool __locationMethodState;
135 bool __locationManagerIsEnabledMethod(location_method_e method);
136 static void __locationSettingChangedCb(location_method_e method, bool enable, void *userData);
137 void __locationManagerSetSettingChangedCb();
138 void __locationManagerUnsetSettingChangedCb();
140 /* LOCATION MANAGER : LOCATION */
141 double __locationManagerGetHorizontalAccuracy();
143 /* LOCATION MANAGER : LOCATION : SYNCHRONOUS */
144 bool __locationManagerGetLocation();
145 void __locationManagerGetLastLocation();
147 /* LOCATION MANAGER : LOCATION : ASYNCHRONOUS */
148 static void __positionUpdatedCb(double latitude, double longitude,
149 double altitude, time_t timestamp, void *userData);
150 static void __locationUpdatedCb(location_error_e error, double latitude,
151 double longitude, double altitude, time_t timestamp, double speed,
152 double direction, double climb, void *userData);
153 bool __locationManagerRequestSingleLocation();
155 LocationLogger(const LocationLogger&) = delete;
156 LocationLogger& operator=(const LocationLogger&) = delete;
159 LocationLogger(ILocationListener *listener = nullptr);
162 }; /* class LocationLogger */
164 } /* namespace ctx */
166 #endif /* End of _CONTEXT_PLACE_RECOGNITION_LOCATION_LOGGER_H_ */