[my-place] DEBUG_MODE simplification.
[platform/core/context/context-provider.git] / src / my-place / utils / UserPlacesTypes.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_USER_PLACES_TYPES_H_
18 #define _CONTEXT_PLACE_RECOGNITION_USER_PLACES_TYPES_H_
19
20 #include <memory>
21 #include <vector>
22 #include <map>
23 #include <unordered_map>
24 #include <unordered_set>
25 #include <string>
26 #include <ctime>
27 #include <MyPlaceTypes.h>
28
29 // Database
30 #define VISIT_TABLE                            "Log_Myplace_Visit"
31 #define VISIT_COLUMN_START_TIME                "start_time"
32 #define VISIT_COLUMN_END_TIME                  "end_time"
33 #define VISIT_COLUMN_WIFI_APS                  "wifi_aps"
34 #define VISIT_COLUMN_CATEGORY                  "category"
35 #define VISIT_COLUMN_LOCATION_VALID            "location_valid"
36 #define VISIT_COLUMN_LOCATION_LATITUDE         "location_latitude"
37 #define VISIT_COLUMN_LOCATION_LONGITUDE        "location_longitude"
38 #define VISIT_COLUMN_LOCATION_ACCURACY         "location_accuracy"
39 #define VISIT_COLUMN_CATEG_HOME                "categ_home"
40 #define VISIT_COLUMN_CATEG_WORK                "categ_work"
41 #define VISIT_COLUMN_CATEG_OTHER               "categ_other"
42
43 #define WIFI_APS_MAP_TABLE                     "Log_Myplace_WifiAPsMap"
44 #define WIFI_APS_MAP_COLUMN_MAC                "mac"
45 #define WIFI_APS_MAP_COLUMN_NETWORK_NAME       "network_name"
46 #define WIFI_APS_MAP_COLUMN_INSERT_TIME        "insert_time"
47
48 #define PLACE_TABLE                            "Log_Myplace_Place"
49 #define PLACE_COLUMN_CATEG_ID                  "categ_id"
50 #define PLACE_COLUMN_CATEG_CONFIDENCE          "categ_confidence"
51 #define PLACE_COLUMN_NAME                      "name"
52 #define PLACE_COLUMN_LOCATION_VALID            "location_valid"
53 #define PLACE_COLUMN_LOCATION_LATITUDE         "location_latitude"
54 #define PLACE_COLUMN_LOCATION_LONGITUDE        "location_longitude"
55 #define PLACE_COLUMN_LOCATION_ACCURACY         "location_accuracy"
56 #define PLACE_COLUMN_WIFI_APS                  "wifi_aps"
57 #define PLACE_COLUMN_CREATE_DATE               "create_date"
58
59 #define WIFI_TABLE                             "Log_Myplace_Wifi"
60 #define WIFI_COLUMN_TIMESTAMP                  "timestamp"
61 #define WIFI_COLUMN_BSSID                      "bssid"
62 #define WIFI_COLUMN_ESSID                      "essid"
63
64 #define LOCATION_TABLE                         "Log_Myplace_Location"
65 #define LOCATION_COLUMN_LATITUDE               "latitude"
66 #define LOCATION_COLUMN_LONGITUDE              "longitude"
67 #define LOCATION_COLUMN_ACCURACY               "accuracy"
68 #define LOCATION_COLUMN_TIMESTAMP              "timestamp"
69
70 #define MYPLACE_SETTINGS_TABLE                 "Myplace_Settings"
71 #define MYPLACE_SETTINGS_COLUMN_KEY            "key"
72 #define MYPLACE_SETTINGS_COLUMN_VALUE          "value"
73 #define MYPLACE_SETTING_KEY_USER_CONSENT       PLACE_DETECTION_REQUEST_CONSENT
74
75 enum PlaceRecogMode {
76         PLACE_RECOG_HIGH_ACCURACY_MODE = 0,
77         PLACE_RECOG_LOW_POWER_MODE = 1
78 };
79
80 namespace ctx {
81
82         /*
83          * type for numerical computations
84          */
85         typedef double num_t;
86
87         /*
88          * mac address
89          */
90         class Mac {
91
92         public:
93                 const static size_t MAC_SIZE = 6;  // number of bytes for mac address.
94                 unsigned char c[MAC_SIZE];
95
96                 Mac() {};
97                 Mac(const std::string &str);
98                 Mac(const char *str);
99                 operator std::string() const;
100
101         };      /* class Mac */
102
103         std::istream &operator>>(std::istream &input, ctx::Mac &mac);
104         std::ostream &operator<<(std::ostream &output, const ctx::Mac &mac);
105         bool operator==(const ctx::Mac &m1, const ctx::Mac &m2);
106         bool operator!=(const ctx::Mac &m1, const ctx::Mac &m2);
107         bool operator<(const ctx::Mac &m1, const ctx::Mac &m2);
108         bool operator>(const ctx::Mac &m1, const ctx::Mac &m2);
109
110 }       /* namespace ctx */
111
112 namespace std {
113
114         template <> struct hash<ctx::Mac> {
115                 size_t operator()(const ctx::Mac & m) const {
116                         size_t h = 1;
117                         for (size_t i = 0; i < ctx::Mac::MAC_SIZE; i++) {
118                                 h = h * 37 + m.c[i];
119                         }
120                         return h;
121                 }
122         };
123
124 }       /* namespace std */
125
126 namespace ctx {
127
128         typedef float share_t;
129         typedef int count_t;
130
131         typedef std::unordered_map<ctx::Mac, ctx::count_t> Macs2Counts;
132         typedef std::unordered_map<ctx::Mac, ctx::share_t> Macs2Shares;
133
134         typedef std::unordered_set<ctx::Mac> MacSet;
135
136         std::istream &operator>>(std::istream &input, ctx::MacSet &macSet);
137         std::ostream &operator<<(std::ostream &output, const ctx::MacSet &macSet);
138         ctx::MacSet macSetFromString(const std::string &str);
139
140         std::shared_ptr<MacSet> macSetsUnion(const std::vector<std::shared_ptr<MacSet>> &macSets);
141
142         struct Interval {
143                 time_t start;
144                 time_t end;
145
146                 Interval(time_t start, time_t end);
147         };
148
149 }       /* namespace ctx */
150
151 namespace std {
152
153         template <> struct hash<ctx::Interval> {
154                 size_t operator()(const ctx::Interval & interval) const {
155                         return interval.end * interval.start;
156                 }
157         };
158
159 }       /* namespace std */
160
161 namespace ctx {
162
163         /*
164          * fully describes interval data after the interval is finished
165          */
166         struct Frame {
167                 Interval interval;
168                 count_t numberOfTimestamps;
169                 Macs2Counts macs2Counts;
170
171                 Frame(Interval interval_) : interval(interval_), numberOfTimestamps(0) {};
172         };
173
174         /*
175          * mac address + its timestamp
176          */
177         struct MacEvent {
178                 time_t timestamp;
179                 Mac mac;
180                 std::string networkName;
181
182                 MacEvent(time_t timestamp_, Mac mac_, std::string networkName_ = "")
183                         : timestamp(timestamp_)
184                         , mac(mac_)
185                         , networkName(networkName_) {}
186         };
187
188         typedef std::map<int, num_t> Categs; // scores of categories
189
190         /*
191          * location + timestamp + method
192          */
193         struct LocationEvent {
194                 Location coordinates;
195                 time_t timestamp;
196
197                 LocationEvent(double latitude_, double longitude_, double accuracy_, time_t timestamp_) :
198                         coordinates(latitude_, longitude_, accuracy_),
199                         timestamp(timestamp_) {}
200
201                 void log();
202
203         };      /* struct LocationEvent */
204
205         struct Visit {
206                 Interval interval;
207                 std::shared_ptr<MacSet> macSet;
208                 Categs categs;
209                 bool locationValid;
210                 Location location; // makes sense if locationValid == true;
211
212                 Visit(Interval interval_, std::shared_ptr<MacSet> macSet_ = std::make_shared<MacSet>(), Categs categs_ = Categs()) :
213                         interval(interval_),
214                         macSet(macSet_),
215                         categs(categs_),
216                         locationValid(false) {}
217                 void setLocation(Location location);
218                 void printShort2Stream(std::ostream &out) const;
219
220         };      /* struct Visit */
221
222         bool operator==(const Visit &v1, const Visit &v2);
223         typedef std::vector<Visit> Visits;
224         typedef std::vector<MacEvent> MacEvents; // used to store current interval logs
225
226         std::shared_ptr<MacSet> macSetFromMacs2Counts(const Macs2Counts &macs2Counts);
227
228 }       /* namespace ctx */
229
230 #endif /* End of _CONTEXT_PLACE_RECOGNITION_USER_PLACES_TYPES_H_ */