[my-place] Refactoring: Tizen C++ convention - File names chenge.
[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 // TODO: false in final solution
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         public:
68                 LocationLogger(ILocationListener *listener = nullptr);
69                 ~LocationLogger();
70
71         private:
72                 /* INPUT */
73                 void onVisitStart();
74                 void onVisitEnd();
75
76                 /* OUTPUT */
77                 ILocationListener * const __listener;
78                 void __broadcast(LocationEvent locationEvent);
79
80                 /* INTERNAL */
81                 void __startLogging();
82                 void __stopLogging();
83                 void __locationRequest();
84                 void __onActiveRequestSucceeded();
85                 void __onActiveLocationSucceeded();
86
87                 /* INTERNAL : COUNTERS (LIMITS) */
88                 int __activeRequestAttempts;
89                 int __activeAttempts;
90                 int __allAttempts;
91                 int __locationCount;
92                 bool __checkGeneralLimits();
93                 bool __checkActiveLimits();
94                 bool __checkActiveRequestLimits();
95
96                 /* INTERNAL : FLAGS */
97                 bool __activeRequestSucceeded;
98                 bool __activeLocationSucceeded;
99
100                 /* TIMER */
101                 int __timerId;
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);
111                 void __timerStop();
112                 bool onTimerExpired(int timerId);
113
114                 /* DATABASE */
115                 static int __dbCreateTable();
116                 int __dbInsertLog(LocationEvent locationEvent);
117
118                 /* DEBUG */
119                 static const char* __locationError2Str(int error);
120                 static void __log(location_accessibility_state_e state);
121
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();
129
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();
135
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();
143
144                 /* LOCATION MANAGER : LOCATION */
145                 double __locationManagerGetHorizontalAccuracy();
146
147                 /* LOCATION MANAGER : LOCATION : SYNCHRONOUS */
148                 bool __locationManagerGetLocation();
149                 void __locationManagerGetLastLocation();
150
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();
158
159         };      /* class LocationLogger */
160
161 }       /* namespace ctx */
162
163 #endif /* End of _CONTEXT_PLACE_RECOGNITION_LOCATION_LOGGER_H_ */