Move .so namelinks to the devel package
[platform/core/context/context-provider.git] / src / my-place / visit-detector / LocationLogger.h
1 /*
2  * Copyright (c) 2016 Samsung Electronics Co., Ltd.
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 #ifndef _CONTEXT_PLACE_RECOGNITION_LOCATION_LOGGER_H_
18 #define _CONTEXT_PLACE_RECOGNITION_LOCATION_LOGGER_H_
19
20 #include <locations.h>
21 #include <TimerManager.h>
22 #include "VisitListenerIface.h"
23 #include "LocationListenerIface.h"
24
25 /* Database usage flag */
26 #define LOCATION_LOGGER_DATABASE false
27
28 /* Locations measure method */
29 #define LOCATION_LOGGER_METHOD LOCATIONS_METHOD_HYBRID
30
31 /* TIMEOUTS: Location active measure request timeout (in seconds). */
32 #define LOCATION_LOGGER_ACTIVE_REQUEST_TIMEOUT_SECONDS 100
33
34 /* TIMEOUTS: Location service start timeout (in minutes). */
35 #define LOCATION_LOGGER_SERVICE_START_TIMEOUT_MINUTES 2
36
37 /* FREQUENCIES/INTERVALS: "Active" measure attempts frequency (in minutes) */
38 #define LOCATION_LOGGER_ACTIVE_INTERVAL_MINUTES 5
39
40 /* FREQUENCIES/INTERVALS: "Passive" measure attempts frequency (in minutes) */
41 #define LOCATION_LOGGER_PASSIVE_INTERVAL_MINUTES 30
42
43 /* ATTEMTS LIMITS: "Active" request attempts limit (must be <= than active location attempts) */
44 #define LOCATION_LOGGER_MAX_ACTIVE_REQUEST_ATTEMPTS 0
45
46 /* ATTEMTS LIMITS: "Active" measures attempts limit (must be <= than all attempts limit) */
47 #define LOCATION_LOGGER_MAX_ACTIVE_LOCATION_ATTEMPTS 2
48
49 /* ATTEMTS LIMITS: All attempts ("active" + "passive") limit */
50 #define LOCATION_LOGGER_MAX_LOCATION_ATTEMPTS 3
51
52 /* LOCATION LIMIT: Location count limit per visit */
53 #define LOCATION_LOGGER_MAX_LOCATION_COUNT 3
54
55 namespace ctx {
56
57         enum TimerPurpose {
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
63         };
64
65         class LocationLogger : public ITimerListener, public IVisitListener {
66
67         private:
68                 /* INPUT */
69                 void onVisitStart();
70                 void onVisitEnd();
71
72                 /* OUTPUT */
73                 ILocationListener * const __listener;
74                 void __broadcast(LocationEvent locationEvent);
75
76                 /* INTERNAL */
77                 void __startLogging();
78                 void __stopLogging();
79                 void __locationRequest();
80                 void __onActiveRequestSucceeded();
81                 void __onActiveLocationSucceeded();
82
83                 /* INTERNAL : COUNTERS (LIMITS) */
84                 int __activeRequestAttempts;
85                 int __activeAttempts;
86                 int __allAttempts;
87                 int __locationCount;
88                 bool __checkGeneralLimits();
89                 bool __checkActiveLimits();
90                 bool __checkActiveRequestLimits();
91
92                 /* INTERNAL : FLAGS */
93                 bool __activeRequestSucceeded;
94                 bool __activeLocationSucceeded;
95
96                 /* TIMER */
97                 int __timerId;
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);
107                 void __timerStop();
108                 bool onTimerExpired(int timerId);
109
110                 /* DATABASE */
111                 static int __dbCreateTable();
112                 int __dbInsertLog(LocationEvent locationEvent);
113
114                 /* DEBUG */
115                 static const char* __locationError2Str(int error);
116                 static void __log(location_accessibility_state_e state);
117
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();
125
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();
131
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();
139
140                 /* LOCATION MANAGER : LOCATION */
141                 double __locationManagerGetHorizontalAccuracy();
142
143                 /* LOCATION MANAGER : LOCATION : SYNCHRONOUS */
144                 bool __locationManagerGetLocation();
145                 void __locationManagerGetLastLocation();
146
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();
154
155                 LocationLogger(const LocationLogger&) = delete;
156                 LocationLogger& operator=(const LocationLogger&) = delete;
157
158         public:
159                 LocationLogger(ILocationListener *listener = nullptr);
160                 ~LocationLogger();
161
162         };      /* class LocationLogger */
163
164 }       /* namespace ctx */
165
166 #endif /* End of _CONTEXT_PLACE_RECOGNITION_LOCATION_LOGGER_H_ */