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