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 // TODO: false in final solution
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 {
68 LocationLogger(ILocationListener *listener = nullptr);
77 ILocationListener * const __listener;
78 void __broadcast(LocationEvent locationEvent);
81 void __startLogging();
83 void __locationRequest();
84 void __onActiveRequestSucceeded();
85 void __onActiveLocationSucceeded();
87 /* INTERNAL : COUNTERS (LIMITS) */
88 int __activeRequestAttempts;
92 bool __checkGeneralLimits();
93 bool __checkActiveLimits();
94 bool __checkActiveRequestLimits();
96 /* INTERNAL : FLAGS */
97 bool __activeRequestSucceeded;
98 bool __activeLocationSucceeded;
102 time_t __timerTimestamp;
103 TimerManager __timerManager;
104 TimerPurpose __timerPurpose;
105 void __setNextTimer();
106 void __activeRequestTimerStart();
107 void __startServiceTimerStart();
108 void __activeIntervalTimerStart();
109 void __passiveIntervalTimerStart();
110 void __timerStart(time_t minutes);
112 bool onTimerExpired(int timerId);
115 static int __dbCreateTable();
116 int __dbInsertLog(LocationEvent locationEvent);
119 static const char* __locationError2Str(int error);
120 static void __log(location_accessibility_state_e state);
122 /* LOCATION MANAGER */
123 location_manager_h __locationManager;
124 void __locationManagerCreate();
125 void __locationManagerDestroy();
126 void __locationManagerStart();
127 void __locationManagerStop();
128 location_accessibility_state_e __locationManagerGetAccessibilityState();
130 /* LOCATION MANAGER : LOCATION SERVICE STATE */
131 location_service_state_e __locationServiceState;
132 static void __locationServiceStateChangedCb(location_service_state_e state, void *userData);
133 void __locationManagerSetServiceStateChangedCb();
134 void __locationManagerUnsetServiceStateChangedCb();
136 /* LOCATION MANAGER : LOCATION METHOD SETTINGS */
137 location_method_e __locationMethod;
138 bool __locationMethodState;
139 bool __locationManagerIsEnabledMethod(location_method_e method);
140 static void __locationSettingChangedCb(location_method_e method, bool enable, void *userData);
141 void __locationManagerSetSettingChangedCb();
142 void __locationManagerUnsetSettingChangedCb();
144 /* LOCATION MANAGER : LOCATION */
145 double __locationManagerGetHorizontalAccuracy();
147 /* LOCATION MANAGER : LOCATION : SYNCHRONOUS */
148 bool __locationManagerGetLocation();
149 void __locationManagerGetLastLocation();
151 /* LOCATION MANAGER : LOCATION : ASYNCHRONOUS */
152 static void __positionUpdatedCb(double latitude, double longitude,
153 double altitude, time_t timestamp, void *userData);
154 static void __locationUpdatedCb(location_error_e error, double latitude,
155 double longitude, double altitude, time_t timestamp, double speed,
156 double direction, double climb, void *userData);
157 bool __locationManagerRequestSingleLocation();
159 }; /* class LocationLogger */
161 } /* namespace ctx */
163 #endif /* End of _CONTEXT_PLACE_RECOGNITION_LOCATION_LOGGER_H_ */