fix svace warnings
[platform/core/location/geofence-server.git] / geofence-server / src / geofence_server_db.c
1 /* Copyright 2014 Samsung Electronics Co., Ltd All Rights Reserved
2  *
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15
16 #include <sqlite3.h>
17 #include <sys/time.h>
18 #include <db-util.h>
19 #include <gio/gio.h>
20 #include <sys/stat.h>
21 #include <string.h>
22 #include <bluetooth.h>
23 #include <wifi.h>
24 #include <tzplatform_config.h>
25 #include <sys/types.h>
26 #include <fcntl.h>
27
28 #include "debug_util.h"
29 #include "geofence_server.h"
30 #include "geofence_server_db.h"
31 #include "geofence_server_private.h"
32
33 /* dbspace path for Tizen 3.0 was changed.
34 #define GEOFENCE_SERVER_DB_FILE                ".geofence-server.db"
35 #define GEOFENCE_SERVER_DB_PATH                "/opt/dbspace/"GEOFENCE_SERVER_DB_FILE
36 */
37
38 #define GEOFENCE_DB_NAME        ".geofence-server.db"
39 #define GEOFENCE_DB_FILE        tzplatform_mkpath(TZ_USER_DB, GEOFENCE_DB_NAME)
40
41 #define MAX_DATA_NAME           20
42 #define DATA_LEN                        20
43
44 #define GEOFENCE_INVALID        0
45
46 char *menu_table[4] = { "GeoFence", "FenceGeocoordinate", "FenceGeopointWifi", "FenceBssid" };
47
48 const char *group_id = NULL;
49
50 #ifdef SUPPORT_ENCRYPTION
51 static char *password = "k1s2c3w4k5a6";
52 const char *col_latitude = "la";
53 const char *col_longitude = "lo";
54 const char *col_radius = "r";
55 #endif
56
57 typedef enum {
58     FENCE_MAIN_TABLE = 0,       /*GeoFence */
59     FENCE_GEOCOORDINATE_TAB,    /*FenceGeocoordinate */
60     FENCE_GEOPOINT_WIFI_TABLE,  /*FenceCurrentLocation */
61     FENCE_BSSID_TABLE   /*FenceBluetoothBssid */
62 } fence_table_type_e;
63
64 static struct {
65         sqlite3 *handle;
66 } db_info_s = {
67         .handle = NULL,
68 };
69
70 #define SQLITE3_RETURN(ret, msg, state) \
71         if (ret != SQLITE_OK) { \
72                 LOGI_GEOFENCE("sqlite3 Error[%d] : %s", ret, msg); \
73                 sqlite3_reset(state); \
74                 sqlite3_clear_bindings(state); \
75                 sqlite3_finalize(state); \
76                 return FENCE_ERR_SQLITE_FAIL; \
77         }
78
79 /*
80  * \note
81  * DB Table schema
82  *
83  * GeoFence
84  * +----------+-------+-------+------------+-------+-------+-----------+---------+
85  * | fence_id | name     | app_id  | geofence_type |direction |enable    |smart_assist_id|time_stamp
86  * +-------+-------+-------+-------+
87  * |   -   |   -   |   -   |   -   |
88  * +-------+-------+-------+-------+
89  * CREATE TABLE GeoFence ( fence_id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT, app_id TEXT NOT NULL, geofence_type INTEGER," \
90                 "direction INTEGER,  enable INTEGER,  smart_assist_id INTEGER, time_stamp INTEGER)";
91  *
92  *
93  * FenceGeocoordinate
94  * +----------+---------+--------+------+
95  * | fence_id | latitude     | longitude | radius
96  * +-------+---------+-----+---------+
97  * |   -   |    -    |  -  |    -    |     -    |    -    |
98  * +-------+---------+-----+---------+
99  *  CREATE TABLE FenceGeocoordinate ( fence_id INTEGER , latitude DOUBLE, longitude DOUBLE, radius DOUBLE, FOREIGN KEY(fence_id) REFERENCES GeoFence(fence_id) ON DELETE CASCADE)";
100  *
101  *
102  * FenceCurrentLocation
103  * +-----+-------+------
104  * |bssid 1|fence_id1 |...
105  * +-----+-------+------
106  * |bssid 2|fence_id1|...
107  * +-----+-------+------
108  * |bssid 3|fence_id1|...
109  * +-----+-------+------
110  * |bssid 1|fence_id2|...
111  * +-----+-------+------
112  * |bssid 2|fence_id2|...
113  * +-------+---------+-----+---------+
114  * |   -   |    -    |  -  |    -    |     -    |    -    |
115  * +-------+---------+-----+---------+
116 *CREATE TABLE FenceCurrentLocation ( fence_id INTEGER,  bssid TEXT,  FOREIGN KEY(fence_id) REFERENCES GeoFence(fence_id) ON DELETE CASCADE)";
117 */
118
119 static inline int begin_transaction(void)
120 {
121         FUNC_ENTRANCE_SERVER;
122         sqlite3_stmt *stmt;
123         int ret;
124
125         ret = sqlite3_prepare_v2(db_info_s.handle, "BEGIN TRANSACTION", -1, &stmt, NULL);
126
127         if (ret != SQLITE_OK) {
128                 LOGI_GEOFENCE("Error: %s", sqlite3_errmsg(db_info_s.handle));
129                 return FENCE_ERR_SQLITE_FAIL;
130         }
131
132         if (sqlite3_step(stmt) != SQLITE_DONE) {
133                 LOGI_GEOFENCE("Failed to do update (%s)", sqlite3_errmsg(db_info_s.handle));
134                 sqlite3_finalize(stmt);
135                 return FENCE_ERR_SQLITE_FAIL;
136         }
137
138         sqlite3_finalize(stmt);
139         return FENCE_ERR_NONE;
140 }
141
142 static inline int rollback_transaction(void)
143 {
144         FUNC_ENTRANCE_SERVER;
145         int ret;
146         sqlite3_stmt *stmt;
147
148         ret = sqlite3_prepare_v2(db_info_s.handle, "ROLLBACK TRANSACTION", -1, &stmt, NULL);
149         if (ret != SQLITE_OK) {
150                 LOGI_GEOFENCE("Error: %s", sqlite3_errmsg(db_info_s.handle));
151                 return FENCE_ERR_SQLITE_FAIL;
152         }
153
154         if (sqlite3_step(stmt) != SQLITE_DONE) {
155                 LOGI_GEOFENCE("Failed to do update (%s)", sqlite3_errmsg(db_info_s.handle));
156                 sqlite3_finalize(stmt);
157                 return FENCE_ERR_SQLITE_FAIL;
158         }
159
160         sqlite3_finalize(stmt);
161         return FENCE_ERR_NONE;
162 }
163
164 static inline int commit_transaction(void)
165 {
166         FUNC_ENTRANCE_SERVER;
167         sqlite3_stmt *stmt;
168         int ret;
169
170         ret = sqlite3_prepare_v2(db_info_s.handle, "COMMIT TRANSACTION", -1, &stmt, NULL);
171         if (ret != SQLITE_OK) {
172                 LOGI_GEOFENCE("Error: %s", sqlite3_errmsg(db_info_s.handle));
173                 return FENCE_ERR_SQLITE_FAIL;
174         }
175
176         if (sqlite3_step(stmt) != SQLITE_DONE) {
177                 LOGI_GEOFENCE("Failed to do update (%s)", sqlite3_errmsg(db_info_s.handle));
178                 sqlite3_finalize(stmt);
179                 return FENCE_ERR_SQLITE_FAIL;
180         }
181
182         sqlite3_finalize(stmt);
183         return FENCE_ERR_NONE;
184 }
185
186 static inline int __geofence_manager_db_create_places_table(void)
187 {
188         FUNC_ENTRANCE_SERVER;
189         char *err = NULL;
190         char *ddl;
191
192         ddl = sqlite3_mprintf("CREATE TABLE Places ( place_id INTEGER PRIMARY KEY AUTOINCREMENT, access_type INTEGER, place_name TEXT NOT NULL, app_id TEXT NOT NULL)");
193         if (sqlite3_exec(db_info_s.handle, ddl, NULL, NULL, &err) != SQLITE_OK) {
194                 LOGI_GEOFENCE("Failed to execute the DDL (%s)", err);
195                 sqlite3_free(ddl);
196                 return FENCE_ERR_SQLITE_FAIL;
197         }
198
199         if (sqlite3_changes(db_info_s.handle) == 0) {
200                 LOGI_GEOFENCE("No changes  to DB");
201         }
202         sqlite3_free(ddl);
203         return FENCE_ERR_NONE;
204 }
205
206 static inline int __geofence_manager_db_create_geofence_table(void)
207 {
208         FUNC_ENTRANCE_SERVER;
209         char *err = NULL;
210         char *ddl;
211
212         ddl = sqlite3_mprintf("CREATE TABLE GeoFence ( fence_id INTEGER PRIMARY KEY AUTOINCREMENT, place_id INTEGER, enable INTEGER, app_id TEXT NOT NULL, geofence_type INTEGER, access_type INTEGER, running_status INTEGER, ble_info TEXT, FOREIGN KEY(place_id) REFERENCES Places(place_id) ON DELETE CASCADE)");
213
214         if (sqlite3_exec(db_info_s.handle, ddl, NULL, NULL, &err) != SQLITE_OK) {
215                 LOGI_GEOFENCE("Failed to execute the DDL (%s)", err);
216                 sqlite3_free(ddl);
217                 return FENCE_ERR_SQLITE_FAIL;
218         }
219
220         if (sqlite3_changes(db_info_s.handle) == 0) {
221                 LOGI_GEOFENCE("No changes  to DB");
222         }
223         sqlite3_free(ddl);
224         return FENCE_ERR_NONE;
225 }
226
227 static inline int __geofence_manager_db_create_geocoordinate_table(void)
228 {
229         FUNC_ENTRANCE_SERVER;
230         char *err = NULL;
231         char *ddl;
232
233         ddl = sqlite3_mprintf("CREATE TABLE FenceGeocoordinate ( fence_id INTEGER PRIMARY KEY, latitude TEXT NOT NULL, longitude TEXT NOT NULL, radius TEXT NOT NULL, address TEXT, FOREIGN KEY(fence_id) REFERENCES GeoFence(fence_id) ON DELETE CASCADE ON UPDATE CASCADE)");
234
235         if (sqlite3_exec(db_info_s.handle, ddl, NULL, NULL, &err) != SQLITE_OK) {
236                 LOGI_GEOFENCE("Failed to execute the DDL (%s)", err);
237                 sqlite3_free(ddl);
238                 return FENCE_ERR_SQLITE_FAIL;
239         }
240
241         if (sqlite3_changes(db_info_s.handle) == 0) {
242                 LOGI_GEOFENCE("No changes to DB");
243         }
244         sqlite3_free(ddl);
245         return FENCE_ERR_NONE;
246 }
247
248 static inline int __geofence_manager_db_create_wifi_data_table(void)
249 {
250         FUNC_ENTRANCE_SERVER;
251         char *err = NULL;
252         char *ddl;
253
254         ddl = sqlite3_mprintf("CREATE TABLE FenceGeopointWifi (fence_id INTEGER, bssid TEXT, FOREIGN KEY(fence_id) REFERENCES GeoFence(fence_id) ON DELETE CASCADE ON UPDATE CASCADE)");
255
256         if (sqlite3_exec(db_info_s.handle, ddl, NULL, NULL, &err) != SQLITE_OK) {
257                 LOGI_GEOFENCE("Failed to execute the DDL (%s)", err);
258                 sqlite3_free(ddl);
259                 return FENCE_ERR_SQLITE_FAIL;
260         }
261
262         if (sqlite3_changes(db_info_s.handle) == 0) {
263                 LOGI_GEOFENCE("No changes to DB");
264         }
265         sqlite3_free(ddl);
266         return FENCE_ERR_NONE;
267 }
268
269 /* DB table for save the pair of fence id and bluetooth bssid */
270 static inline int __geofence_manager_db_create_bssid_table(void)
271 {
272         FUNC_ENTRANCE_SERVER
273         char *err = NULL;
274         char *ddl;
275
276         ddl = sqlite3_mprintf("CREATE TABLE FenceBssid (fence_id INTEGER PRIMARY KEY, bssid TEXT, ssid TEXT, FOREIGN KEY(fence_id) REFERENCES GeoFence(fence_id) ON DELETE CASCADE ON UPDATE CASCADE)");
277
278         if (sqlite3_exec(db_info_s.handle, ddl, NULL, NULL, &err) != SQLITE_OK) {
279                 LOGI_GEOFENCE("Failed to execute the DDL (%s)", err);
280                 sqlite3_free(ddl);
281                 return FENCE_ERR_SQLITE_FAIL;
282         }
283
284         if (sqlite3_changes(db_info_s.handle) == 0)
285                 LOGI_GEOFENCE("No changes to DB");
286         sqlite3_free(ddl);
287         return FENCE_ERR_NONE;
288 }
289
290 static int __geofence_manager_open_db_handle(const int open_flag)
291 {
292         LOGI_GEOFENCE("enter");
293         int ret = SQLITE_OK;
294
295         ret = db_util_open_with_options(GEOFENCE_DB_FILE, &db_info_s.handle, open_flag, NULL);
296         if (ret != SQLITE_OK) {
297                 LOGI_GEOFENCE("sqlite3_open_v2 Error[%d] : %s", ret, sqlite3_errmsg(db_info_s.handle));
298                 return FENCE_ERR_SQLITE_FAIL;
299         }
300
301         return FENCE_ERR_NONE;
302 }
303
304 static int __geofence_manager_db_get_count_by_fence_id_and_bssid(int fence_id, char *bssid, fence_table_type_e table_type, int *count)
305 {
306         FUNC_ENTRANCE_SERVER;
307         g_return_val_if_fail(bssid, FENCE_ERR_INVALID_PARAMETER);
308         sqlite3_stmt *state = NULL;
309         int ret = SQLITE_OK;
310         const char *tail = NULL;
311
312         char *query = sqlite3_mprintf("SELECT COUNT(fence_id) FROM %Q where fence_id = %d AND bssid = %Q;", menu_table[table_type], fence_id, bssid);
313
314         ret = sqlite3_prepare_v2(db_info_s.handle, query, -1, &state, &tail);
315         if (ret != SQLITE_OK) {
316                 LOGI_GEOFENCE("Error: %s", sqlite3_errmsg(db_info_s.handle));
317                 sqlite3_free(query);
318                 return FENCE_ERR_PREPARE;
319         }
320
321         ret = sqlite3_step(state);
322         if (ret != SQLITE_ROW) {
323                 LOGI_GEOFENCE("sqlite3_step Error[%d] : %s", ret, sqlite3_errmsg(db_info_s.handle));
324                 sqlite3_finalize(state);
325                 sqlite3_free(query);
326                 return FENCE_ERR_SQLITE_FAIL;
327         }
328         *count = sqlite3_column_int(state, 0);
329         sqlite3_reset(state);
330         sqlite3_finalize(state);
331         sqlite3_free(query);
332
333         return FENCE_ERR_NONE;
334 }
335
336 static int __geofence_manager_db_insert_bssid_info(const int fence_id, const char *bssid_info, const char *ssid)
337 {
338         FUNC_ENTRANCE_SERVER;
339         g_return_val_if_fail(fence_id, FENCE_ERR_INVALID_PARAMETER);
340         g_return_val_if_fail(bssid_info, FENCE_ERR_INVALID_PARAMETER);
341         sqlite3_stmt *state = NULL;
342         int ret = SQLITE_OK;
343         int index = 0;
344         int count = -1;
345         const char *tail;
346         char *bssid = NULL;
347
348         char *query = sqlite3_mprintf("INSERT INTO %Q(fence_id, bssid, ssid) VALUES (?, ?, ?)", menu_table[FENCE_BSSID_TABLE]);
349         bssid = (char *)g_malloc0(sizeof(char) * WLAN_BSSID_LEN);
350         g_strlcpy(bssid, bssid_info, WLAN_BSSID_LEN);
351         LOGI_GEOFENCE("fence_id[%d], bssid[%s], ssid[%s]", fence_id, bssid, ssid);
352
353         ret = __geofence_manager_db_get_count_by_fence_id_and_bssid(fence_id, bssid, FENCE_BSSID_TABLE, &count);
354         if (ret != FENCE_ERR_NONE) {
355                 LOGI_GEOFENCE("__geofence_manager_db_get_count_by_fence_id_and_bssid() failed. ERROR(%d)", ret);
356                 sqlite3_free(query);
357                 return ret;
358         }
359         if (count > 0) {
360                 LOGI_GEOFENCE("count = %d", count);
361                 return FENCE_ERR_NONE;
362         }
363
364         ret = sqlite3_prepare_v2(db_info_s.handle, query, -1, &state, &tail);
365         if (ret != SQLITE_OK) {
366                 LOGI_GEOFENCE("Error: %s", sqlite3_errmsg(db_info_s.handle));
367                 sqlite3_free(query);
368                 return FENCE_ERR_PREPARE;
369         }
370
371         ret = sqlite3_bind_int(state, ++index, fence_id);
372         SQLITE3_RETURN(ret, sqlite3_errmsg(db_info_s.handle), state);
373
374         ret = sqlite3_bind_text(state, ++index, bssid, -1, SQLITE_STATIC);
375         SQLITE3_RETURN(ret, sqlite3_errmsg(db_info_s.handle), state);
376
377         ret = sqlite3_bind_text(state, ++index, ssid, -1, SQLITE_STATIC);
378         SQLITE3_RETURN(ret, sqlite3_errmsg(db_info_s.handle), state);
379
380         ret = sqlite3_step(state);
381         if (ret != SQLITE_DONE) {
382                 LOGI_GEOFENCE("sqlite3_step Error[%d] : %s", ret, sqlite3_errmsg(db_info_s.handle));
383                 sqlite3_finalize(state);
384                 g_free(bssid);
385                 sqlite3_free(query);
386                 return FENCE_ERR_SQLITE_FAIL;
387         }
388
389         sqlite3_reset(state);
390         sqlite3_clear_bindings(state);
391         sqlite3_finalize(state);
392         g_free(bssid);
393         sqlite3_free(query);
394         LOGI_GEOFENCE("fence_id[%d], bssid[%s], ssid[%s] inserted db table [%s] successfully.", fence_id, bssid_info, ssid, menu_table[FENCE_BSSID_TABLE]);
395
396         return FENCE_ERR_NONE;
397 }
398
399 static int __geofence_manager_db_insert_wifi_data_info(gpointer data, gpointer user_data)
400 {
401         FUNC_ENTRANCE_SERVER;
402         g_return_val_if_fail(data, FENCE_ERR_INVALID_PARAMETER);
403         g_return_val_if_fail(user_data, FENCE_ERR_INVALID_PARAMETER);
404         int *fence_id = (int *) user_data;
405         sqlite3_stmt *state = NULL;
406         wifi_info_s *wifi_info = NULL;
407         int ret = SQLITE_OK;
408         int index = 0;
409         int count = -1;
410         const char *tail;
411         char *bssid = NULL;
412         wifi_info = (wifi_info_s *) data;
413         bssid = (char *)g_malloc0(sizeof(char) * WLAN_BSSID_LEN);
414         g_strlcpy(bssid, wifi_info->bssid, WLAN_BSSID_LEN);
415         LOGI_GEOFENCE("fence_id[%d] bssid[%s]", *fence_id, wifi_info->bssid);
416
417         char *query = sqlite3_mprintf("INSERT INTO FenceGeopointWifi(fence_id, bssid) VALUES (?, ?)");
418
419         ret = __geofence_manager_db_get_count_by_fence_id_and_bssid(*fence_id, bssid, FENCE_GEOPOINT_WIFI_TABLE, &count);
420         if (count > 0) {
421                 LOGI_GEOFENCE("count = %d", count);
422                 sqlite3_free(query);
423                 return ret;
424         }
425
426         ret = sqlite3_prepare_v2(db_info_s.handle, query, -1, &state, &tail);
427         if (ret != SQLITE_OK) {
428                 LOGI_GEOFENCE("Error: %s", sqlite3_errmsg(db_info_s.handle));
429                 sqlite3_free(query);
430                 return FENCE_ERR_PREPARE;
431         }
432
433         ret = sqlite3_bind_int(state, ++index, *fence_id);
434         SQLITE3_RETURN(ret, sqlite3_errmsg(db_info_s.handle), state);
435
436         ret = sqlite3_bind_text(state, ++index, bssid, -1, SQLITE_STATIC);
437         SQLITE3_RETURN(ret, sqlite3_errmsg(db_info_s.handle), state);
438
439         ret = sqlite3_step(state);
440         if (ret != SQLITE_DONE) {
441                 LOGI_GEOFENCE("sqlite3_step Error[%d] : %s", ret, sqlite3_errmsg(db_info_s.handle));
442                 sqlite3_finalize(state);
443                 g_free(bssid);
444                 sqlite3_free(query);
445                 return FENCE_ERR_SQLITE_FAIL;
446         }
447
448         sqlite3_reset(state);
449         sqlite3_clear_bindings(state);
450         sqlite3_finalize(state);
451         g_free(bssid);
452         sqlite3_free(query);
453
454         return FENCE_ERR_NONE;
455 }
456
457 static int __geofence_manager_delete_table(int fence_id, fence_table_type_e table_type)
458 {
459         FUNC_ENTRANCE_SERVER;
460         sqlite3_stmt *state = NULL;
461         int ret = SQLITE_OK;
462
463         char *query = sqlite3_mprintf("DELETE from %Q where fence_id = %d;", menu_table[table_type], fence_id);
464         LOGI_GEOFENCE("current fence id is [%d]", fence_id);
465         ret = sqlite3_prepare_v2(db_info_s.handle, query, -1, &state, NULL);
466         if (SQLITE_OK != ret) {
467                 LOGI_GEOFENCE("Fail to connect to table. Error[%s]", sqlite3_errmsg(db_info_s.handle));
468                 sqlite3_free(query);
469                 return FENCE_ERR_SQLITE_FAIL;
470         }
471
472         ret = sqlite3_step(state);
473         if (SQLITE_DONE != ret) {
474                 LOGI_GEOFENCE("Fail to step. Error[%d]", ret);
475                 sqlite3_finalize(state);
476                 sqlite3_free(query);
477                 return FENCE_ERR_SQLITE_FAIL;
478         }
479         sqlite3_finalize(state);
480         LOGI_GEOFENCE("fence_id[%d], deleted from db table [%s] successfully.", fence_id, menu_table[table_type]);
481         sqlite3_free(query);
482         return FENCE_ERR_NONE;
483 }
484
485 static int __geofence_manager_delete_place_table(int place_id)
486 {
487         FUNC_ENTRANCE_SERVER;
488         sqlite3_stmt *state = NULL;
489         int ret = SQLITE_OK;
490
491         char *query = sqlite3_mprintf("DELETE from Places where place_id = %d;", place_id);
492
493         LOGI_GEOFENCE("current place id is [%d]", place_id);
494         ret = sqlite3_prepare_v2(db_info_s.handle, query, -1, &state, NULL);
495         if (SQLITE_OK != ret) {
496                 LOGI_GEOFENCE("Fail to connect to table. Error[%s]", sqlite3_errmsg(db_info_s.handle));
497                 sqlite3_free(query);
498                 return FENCE_ERR_SQLITE_FAIL;
499         }
500
501         ret = sqlite3_step(state);
502         if (SQLITE_DONE != ret) {
503                 LOGI_GEOFENCE("Fail to step. Error[%d]", ret);
504                 sqlite3_finalize(state);
505                 sqlite3_free(query);
506                 return FENCE_ERR_SQLITE_FAIL;
507         }
508         sqlite3_finalize(state);
509         LOGI_GEOFENCE("place_id[%d], deleted place from db table Places successfully.", place_id);
510         sqlite3_free(query);
511         return FENCE_ERR_NONE;
512 }
513
514 static inline void __geofence_manager_db_create_table(void)
515 {
516         FUNC_ENTRANCE_SERVER;
517         int ret;
518         begin_transaction();
519
520         ret = __geofence_manager_db_create_places_table();
521         if (ret < 0) {
522                 rollback_transaction();
523                 return;
524         }
525
526         ret = __geofence_manager_db_create_geofence_table();
527         if (ret < 0) {
528                 rollback_transaction();
529                 return;
530         }
531
532         ret = __geofence_manager_db_create_geocoordinate_table();
533         if (ret < 0) {
534                 rollback_transaction();
535                 return;
536         }
537
538         ret = __geofence_manager_db_create_wifi_data_table();
539         if (ret < 0) {
540                 rollback_transaction();
541                 return;
542         }
543
544         ret = __geofence_manager_db_create_bssid_table();
545         if (ret < 0) {
546                 rollback_transaction();
547                 return;
548         }
549
550         commit_transaction();
551 }
552
553 /* Get fence id count in certain table, such as GeoFence/FenceGeocoordinate/FenceCurrentLocation */
554 static int __geofence_manager_db_get_count_of_fence_id(int fence_id, fence_table_type_e table_type, int *count)
555 {
556         sqlite3_stmt *state = NULL;
557         int ret = SQLITE_OK;
558         const char *tail = NULL;
559
560         char *query = sqlite3_mprintf("SELECT COUNT(fence_id) FROM %Q where fence_id = %d;", menu_table[table_type], fence_id);
561
562         ret = sqlite3_prepare_v2(db_info_s.handle, query, -1, &state, &tail);
563         if (ret != SQLITE_OK) {
564                 LOGI_GEOFENCE("Error: %s", sqlite3_errmsg(db_info_s.handle));
565                 sqlite3_free(query);
566                 return FENCE_ERR_PREPARE;
567         }
568
569         ret = sqlite3_step(state);
570         if (ret != SQLITE_ROW) {
571                 LOGI_GEOFENCE("sqlite3_step Error[%d] : %s", ret, sqlite3_errmsg(db_info_s.handle));
572                 sqlite3_finalize(state);
573                 sqlite3_free(query);
574                 return FENCE_ERR_SQLITE_FAIL;
575         }
576         *count = sqlite3_column_int(state, 0);
577         sqlite3_reset(state);
578         sqlite3_finalize(state);
579         sqlite3_free(query);
580         return FENCE_ERR_NONE;
581 }
582
583 static int __geofence_manager_db_enable_foreign_keys(void)
584 {
585         sqlite3_stmt *state = NULL;
586         int ret = FENCE_ERR_NONE;
587         char *query = sqlite3_mprintf("PRAGMA foreign_keys = ON;");
588
589         ret = sqlite3_prepare_v2(db_info_s.handle, query, -1, &state, NULL);
590         if (SQLITE_OK != ret) {
591                 LOGI_GEOFENCE("Fail to connect to table. Error[%s]", sqlite3_errmsg(db_info_s.handle));
592                 sqlite3_free(query);
593                 return FENCE_ERR_SQLITE_FAIL;
594         }
595
596         ret = sqlite3_step(state);
597         if (SQLITE_DONE != ret) {
598                 LOGI_GEOFENCE("Fail to step. Error[%d]", ret);
599                 sqlite3_finalize(state);
600                 sqlite3_free(query);
601                 return FENCE_ERR_SQLITE_FAIL;
602         }
603         sqlite3_reset(state);
604         sqlite3_finalize(state);
605         sqlite3_free(query);
606         return FENCE_ERR_NONE;
607 }
608
609 #ifdef SUPPORT_ENCRYPTION
610 void replaceChar(char *src, char oldChar, char newChar)
611 {
612         while (*src) {
613                 if (*src == oldChar)
614                         *src = newChar;
615                 src++;
616         }
617 }
618
619 void __geofence_manager_generate_password(char *password)
620 {
621         char *bt_address = NULL;
622         char *wifi_address = NULL;
623         char *token = NULL, *save_token = NULL;
624         int bt_temp[6] = {0}, wifi_temp[6] = {0};
625         int i = 0, fkey[6], lkey[6];
626         char s1[100], s2[100], result[200];
627         char keyword[6] = { 'b', 'w', 'd', 's', 'j', 'f' };
628         int ret = 0;
629
630         ret = bt_adapter_get_address(&bt_address);
631         if (ret != BT_ERROR_NONE) {
632                 LOGD_GEOFENCE("bt address get fail %d", ret);
633         }
634
635         ret = wifi_get_mac_address(&wifi_address);
636         if (ret != WIFI_ERROR_NONE) {
637                 LOGD_GEOFENCE("wifi address get fail %d", ret);
638         }
639
640         if (bt_address) {
641                 token = strtok_r(bt_address, ":", &save_token);
642                 i = 0;
643                 while (token) {
644                         bt_temp[i++] = atoi(token);
645                         token = strtok_r(NULL, ":", &save_token);
646                         if (i >= 6)
647                                 break;
648                 }
649         }
650
651         if (wifi_address) {
652                 token = strtok_r(wifi_address, ":", &save_token);
653                 i = 0;
654                 while (token) {
655                         wifi_temp[i++] = atoi(token);
656                         token = strtok_r(NULL, ":", &save_token);
657                         if (i >= 6)
658                                 break;
659                 }
660         }
661
662         memset((void *) s1, 0, sizeof(s1));
663         memset((void *) s2, 0, sizeof(s2));
664         memset((void *) result, 0, sizeof(result));
665
666         for (i = 0; i < 6; i++) {
667                 fkey[i] = bt_temp[i] * wifi_temp[i];
668                 lkey[i] = bt_temp[i] + wifi_temp[i];
669         }
670
671         for (i = 0; i < 6; i++) {
672                 sprintf(s1, "%s%x", s1, fkey[i]);
673                 sprintf(s2, "%s%x", s2, lkey[i]);
674                 replaceChar(s1, 0x30 + ((i * 2) % 10), keyword[i]);
675                 replaceChar(s2, 0x30 + ((i * 2 + 1) % 10), keyword[i]);
676                 LOGD_GEOFENCE("s1 %s", s1);
677                 LOGD_GEOFENCE("s2 %s", s2);
678         }
679
680         sprintf(result, "%s%s", s1, s2);
681         LOGD_GEOFENCE("result : %s", result);
682
683         password = result;
684
685         if (bt_address != NULL)
686                 free(bt_address);
687         if (wifi_address != NULL)
688                 free(wifi_address);
689 }
690 #endif
691
692 static int __check_db_file()
693 {
694         int fd = -1;
695
696     fd = open(GEOFENCE_DB_FILE, O_RDONLY);
697     if (fd < 0) {
698              LOGW_GEOFENCE("DB file(%s) is not exist.", GEOFENCE_DB_FILE);
699              return -1;
700      }
701      close(fd);
702      return 0;
703 }
704
705 /**
706  * This function in DB and create  GeoFence/FenceGeocoordinate /FenceCurrentLocation four table on DB if necessary.
707  *
708  * @param[in]    struct of  fence_point_info_s
709  * @return         FENCE_ERR_NONE on success, negative values for errors
710  */
711 int geofence_manager_db_init(void)
712 {
713         FUNC_ENTRANCE_SERVER;
714         struct stat stat;
715         int open_flag = 0;
716
717         if (__check_db_file()) {
718                 LOGW_GEOFENCE("db(%s) file doesn't exist.", GEOFENCE_DB_FILE);
719                 open_flag = SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE | SQLITE_OPEN_FULLMUTEX;
720         } else {
721                 if (lstat(GEOFENCE_DB_FILE, &stat) < 0) {
722                         LOGE_GEOFENCE("Can't get db(%s) information.", GEOFENCE_DB_FILE);
723                         return FENCE_ERR_SQLITE_FAIL;
724                 }
725                 open_flag = SQLITE_OPEN_READWRITE | SQLITE_OPEN_FULLMUTEX;
726         }
727
728         if (__geofence_manager_open_db_handle(open_flag) != FENCE_ERR_NONE) {
729                 LOGI_GEOFENCE("Fail to create db file(%s).", GEOFENCE_DB_FILE);
730                 return FENCE_ERR_SQLITE_FAIL;
731         }
732
733         if (open_flag & SQLITE_OPEN_CREATE)
734                 __geofence_manager_db_create_table();
735
736         return FENCE_ERR_NONE;
737 }
738
739 int geofence_manager_db_reset(void)
740 {
741         FUNC_ENTRANCE_SERVER;
742         sqlite3_stmt *state = NULL;
743         int idx = 0;
744         int ret = SQLITE_OK;
745         char *query = NULL;
746
747         for (idx = 0; idx < 4; idx++) {
748                 query = sqlite3_mprintf("DELETE from %Q;", menu_table[idx]);
749
750                 ret = sqlite3_prepare_v2(db_info_s.handle, query, -1, &state, NULL);
751                 if (SQLITE_OK != ret) {
752                         LOGI_GEOFENCE("Fail to connect to table. Error[%s]", sqlite3_errmsg(db_info_s.handle));
753                         sqlite3_free(query);
754                         return FENCE_ERR_SQLITE_FAIL;
755                 }
756
757                 ret = sqlite3_step(state);
758                 if (SQLITE_DONE != ret) {
759                         LOGI_GEOFENCE("Fail to step. Error[%d]", ret);
760                         sqlite3_finalize(state);
761                         sqlite3_free(query);
762                         return FENCE_ERR_SQLITE_FAIL;
763                 }
764                 sqlite3_finalize(state);
765                 sqlite3_free(query);
766         }
767         return FENCE_ERR_NONE;
768 }
769
770 int geofence_manager_set_place_info(place_info_s *place_info, int *place_id)
771 {
772         FUNC_ENTRANCE_SERVER;
773         g_return_val_if_fail(place_info, FENCE_ERR_INVALID_PARAMETER);
774         g_return_val_if_fail(place_id, FENCE_ERR_INVALID_PARAMETER);
775         sqlite3_stmt *state = NULL;
776         int ret = SQLITE_OK;
777         int index = 0;
778         const char *tail;
779         char *appid = NULL;
780         char *place_name = NULL;
781         char *query = sqlite3_mprintf("INSERT INTO Places (access_type, place_name, app_id) VALUES (?, ?, ?)");
782
783         place_name = (char *)g_malloc0(sizeof(char) * PLACE_NAME_LEN);
784         g_strlcpy(place_name, place_info->place_name, PLACE_NAME_LEN);
785         appid = (char *)g_malloc0(sizeof(char) * APP_ID_LEN);
786         g_strlcpy(appid, place_info->appid, APP_ID_LEN);
787
788         ret = sqlite3_prepare_v2(db_info_s.handle, query, -1, &state, &tail);
789         if (ret != SQLITE_OK) {
790                 LOGI_GEOFENCE("Error: %s", sqlite3_errmsg(db_info_s.handle));
791                 g_free(place_name);
792                 g_free(appid);
793                 sqlite3_free(query);
794                 return FENCE_ERR_PREPARE;
795         }
796         LOGD_GEOFENCE("appid[%s] access_type[%d] place_name[%s]", appid, place_info->access_type, place_info->place_name);
797
798         ret = sqlite3_bind_int(state, ++index, place_info->access_type);
799         SQLITE3_RETURN(ret, sqlite3_errmsg(db_info_s.handle), state);
800
801         ret = sqlite3_bind_text(state, ++index, place_name, -1, SQLITE_STATIC);
802         SQLITE3_RETURN(ret, sqlite3_errmsg(db_info_s.handle), state);
803
804         ret = sqlite3_bind_text(state, ++index, appid, -1, SQLITE_STATIC);
805         SQLITE3_RETURN(ret, sqlite3_errmsg(db_info_s.handle), state);
806
807         ret = sqlite3_step(state);
808         if (ret != SQLITE_DONE) {
809                 LOGI_GEOFENCE("sqlite3_step Error[%d] : %s", ret, sqlite3_errmsg(db_info_s.handle));
810                 sqlite3_finalize(state);
811                 g_free(place_name);
812                 g_free(appid);
813                 sqlite3_free(query);
814                 return FENCE_ERR_SQLITE_FAIL;
815         }
816         *place_id = sqlite3_last_insert_rowid(db_info_s.handle);
817         LOGI_GEOFENCE(" auto-genarated place_id[%d]", *place_id);
818         sqlite3_reset(state);
819         sqlite3_clear_bindings(state);
820         sqlite3_finalize(state);
821         g_free(place_name);
822         g_free(appid);
823         sqlite3_free(query);
824
825         if (*place_id < 1) {
826                 LOGI_GEOFENCE("TMP Invalid fence_id");
827                 *place_id = 0;
828         }
829
830         return FENCE_ERR_NONE;
831 }
832
833 int geofence_manager_set_common_info(fence_common_info_s *fence_info, int *fence_id)
834 {
835         FUNC_ENTRANCE_SERVER;
836         g_return_val_if_fail(fence_info, FENCE_ERR_INVALID_PARAMETER);
837         g_return_val_if_fail(fence_id, FENCE_ERR_INVALID_PARAMETER);
838         sqlite3_stmt *state = NULL;
839         int ret = SQLITE_OK;
840         int index = 0;
841         const char *tail;
842         char *appid = NULL;
843         char *query = sqlite3_mprintf("INSERT INTO GeoFence (place_id, enable, app_id, geofence_type, access_type, running_status) VALUES (?, ?, ?, ?, ?, ?)");
844         appid = (char *)g_malloc0(sizeof(char) * APP_ID_LEN);
845         g_strlcpy(appid, fence_info->appid, APP_ID_LEN);
846
847         ret = sqlite3_prepare_v2(db_info_s.handle, query, -1, &state, &tail);
848         if (ret != SQLITE_OK) {
849                 LOGI_GEOFENCE("Error: %s", sqlite3_errmsg(db_info_s.handle));
850                 g_free(appid);
851                 sqlite3_free(query);
852                 return FENCE_ERR_PREPARE;
853         }
854
855         LOGD_GEOFENCE("place_id[%d], enable[%d], appid[%s] geofence_type[%d] access_type[%d] running_status[%d]", fence_info->place_id, fence_info->enable, appid, fence_info->running_status, fence_info->type, fence_info->access_type, fence_info->place_id);
856
857         ret = sqlite3_bind_int(state, ++index, fence_info->place_id);
858         SQLITE3_RETURN(ret, sqlite3_errmsg(db_info_s.handle), state);
859
860         ret = sqlite3_bind_int(state, ++index, fence_info->enable);
861         SQLITE3_RETURN(ret, sqlite3_errmsg(db_info_s.handle), state);
862
863         ret = sqlite3_bind_text(state, ++index, appid, -1, SQLITE_STATIC);
864         SQLITE3_RETURN(ret, sqlite3_errmsg(db_info_s.handle), state);
865
866         ret = sqlite3_bind_int(state, ++index, fence_info->type);
867         SQLITE3_RETURN(ret, sqlite3_errmsg(db_info_s.handle), state);
868
869         ret = sqlite3_bind_int(state, ++index, fence_info->access_type);
870         SQLITE3_RETURN(ret, sqlite3_errmsg(db_info_s.handle), state);
871
872         ret = sqlite3_bind_int(state, ++index, fence_info->running_status);
873         SQLITE3_RETURN(ret, sqlite3_errmsg(db_info_s.handle), state);
874
875         ret = sqlite3_step(state);
876         if (ret != SQLITE_DONE) {
877                 LOGI_GEOFENCE("sqlite3_step Error[%d] : %s", ret, sqlite3_errmsg(db_info_s.handle));
878                 sqlite3_finalize(state);
879                 g_free(appid);
880                 sqlite3_free(query);
881                 return FENCE_ERR_SQLITE_FAIL;
882         }
883         *fence_id = sqlite3_last_insert_rowid(db_info_s.handle);
884         LOGI_GEOFENCE(" auto-genarated fence_id[%d]", *fence_id);
885         sqlite3_reset(state);
886         sqlite3_clear_bindings(state);
887         sqlite3_finalize(state);
888         g_free(appid);
889         sqlite3_free(query);
890
891         if (*fence_id < 1) {
892                 LOGI_GEOFENCE("TMP Invalid fence_id");
893                 *fence_id = 0;
894         }
895
896         return FENCE_ERR_NONE;
897 }
898
899 int geofence_manager_get_place_list_from_db(int *number_of_places, GList **places)
900 {
901         FUNC_ENTRANCE_SERVER;
902         sqlite3_stmt *state = NULL;
903         int ret = SQLITE_OK;
904         const char *tail = NULL;
905         char *query = NULL;
906         int count = 0;
907
908         query = sqlite3_mprintf("SELECT place_id, place_name, access_type, app_id FROM Places");
909
910         ret = sqlite3_prepare_v2(db_info_s.handle, query, -1, &state, &tail);
911         if (ret != SQLITE_OK) {
912                 LOGI_GEOFENCE("Error: %s", sqlite3_errmsg(db_info_s.handle));
913                 sqlite3_free(query);
914                 return FENCE_ERR_PREPARE;
915         }
916         GList *place_list = NULL;
917         int column_index = 0;
918         do {
919                 ret = sqlite3_step(state);
920
921                 if (ret != SQLITE_ROW) {
922                         LOGI_GEOFENCE("DONE...!!! : %d", ret);
923                         break;
924                 }
925                 column_index = 0;
926                 place_info_s *place = g_slice_new0(place_info_s);
927
928                 if (place == NULL)
929                         continue;
930
931                 place->place_id = sqlite3_column_int(state, column_index++);
932                 g_strlcpy(place->place_name, (char *) sqlite3_column_text(state, column_index++), PLACE_NAME_LEN);
933                 place->access_type = sqlite3_column_int(state, column_index++);
934                 g_strlcpy(place->appid, (char *) sqlite3_column_text(state, column_index++), APP_ID_LEN);
935                 place_list = g_list_append(place_list, place);
936                 count++;
937         } while (ret != SQLITE_DONE);
938
939         *places = place_list;
940         *number_of_places = count;
941
942         sqlite3_reset(state);
943         sqlite3_finalize(state);
944         sqlite3_free(query);
945         return FENCE_ERR_NONE;
946 }
947
948 int geofence_manager_get_fence_list_from_db(int *number_of_fences, GList **fences, int place_id)
949 {
950         FUNC_ENTRANCE_SERVER;
951
952         sqlite3_stmt *state = NULL;
953         int ret = SQLITE_OK;
954         const char *tail = NULL;
955         char *query = NULL;
956         int count = 0;
957
958         if (place_id == -1)
959                 query = sqlite3_mprintf("SELECT DISTINCT A.fence_id, A.app_id, A.geofence_type, A.access_type, A.place_id, B.latitude, B.longitude, B.radius, B.address, C.bssid, C.ssid FROM GeoFence A LEFT JOIN FenceGeocoordinate B ON A.fence_id = B.fence_id LEFT JOIN FenceBssid C ON A.fence_id = C.fence_id GROUP BY A.fence_id");
960         else
961                 query = sqlite3_mprintf("SELECT DISTINCT A.fence_id, A.app_id, A.geofence_type, A.access_type, A.place_id, B.latitude, B.longitude, B.radius, B.address, C.bssid, C.ssid FROM GeoFence A LEFT JOIN FenceGeocoordinate B ON A.fence_id = B.fence_id LEFT JOIN FenceBssid C ON A.fence_id = C.fence_id WHERE A.place_id = %d GROUP BY A.fence_id", place_id);
962
963         ret = sqlite3_prepare_v2(db_info_s.handle, query, -1, &state, &tail);
964         if (ret != SQLITE_OK) {
965                 LOGI_GEOFENCE("Error: %s", sqlite3_errmsg(db_info_s.handle));
966                 sqlite3_free(query);
967                 return FENCE_ERR_PREPARE;
968         }
969         GList *fence_list = NULL;
970         do {
971                 ret = sqlite3_step(state);
972
973                 if (ret != SQLITE_ROW) {
974                         LOGI_GEOFENCE("DONE...!!! : %d", ret);
975                         break;
976                 }
977                 int column_index = 0;
978
979                 geofence_info_s *fence = g_slice_new0(geofence_info_s);
980
981                 if (fence == NULL)
982                         continue;
983
984                 fence->fence_id = sqlite3_column_int(state, column_index++);
985                 g_strlcpy(fence->app_id, (char *) sqlite3_column_text(state, column_index++), APP_ID_LEN);
986                 fence->param.type = sqlite3_column_int(state, column_index++);
987                 fence->access_type = sqlite3_column_int(state, column_index++);
988                 fence->param.place_id = sqlite3_column_int(state, column_index++);
989                 char *data_name = NULL;
990
991                 data_name = (char *) sqlite3_column_text(state, column_index++);
992                 if (!data_name || !strlen(data_name))
993                         LOGI_GEOFENCE("ERROR: data_name is NULL!!!");
994                 else
995                         fence->param.latitude = atof(data_name);
996
997                 data_name = (char *) sqlite3_column_text(state, column_index++);
998                 if (!data_name || !strlen(data_name))
999                         LOGI_GEOFENCE("ERROR: data_name is NULL!!!");
1000                 else
1001                         fence->param.longitude = atof(data_name);
1002
1003                 data_name = (char *) sqlite3_column_text(state, column_index++);
1004                 if (!data_name || !strlen(data_name))
1005                         LOGI_GEOFENCE("ERROR: data_name is NULL!!!");
1006                 else
1007                         fence->param.radius = atof(data_name);
1008
1009                 g_strlcpy(fence->param.address, (char *) sqlite3_column_text(state, column_index++), ADDRESS_LEN);
1010                 g_strlcpy(fence->param.bssid, (char *) sqlite3_column_text(state, column_index++), WLAN_BSSID_LEN);
1011                 g_strlcpy(fence->param.ssid, (char *) sqlite3_column_text(state, column_index++), WLAN_BSSID_LEN);
1012                 LOGI_GEOFENCE("radius = %d, bssid = %s", fence->param.radius, fence->param.bssid);
1013                 fence_list = g_list_append(fence_list, fence);
1014                 count++;
1015         } while (ret != SQLITE_DONE);
1016
1017         *fences = fence_list;
1018         *number_of_fences = count;
1019
1020         sqlite3_reset(state);
1021         sqlite3_finalize(state);
1022         sqlite3_free(query);
1023         return FENCE_ERR_NONE;
1024 }
1025
1026 int geofence_manager_get_fenceid_list_from_db(int *number_of_fences, GList **fences, int place_id)
1027 {
1028         FUNC_ENTRANCE_SERVER;
1029         sqlite3_stmt *state = NULL;
1030         int ret = SQLITE_OK;
1031         const char *tail = NULL;
1032         char *query = NULL;
1033         int count = 0;
1034         query = sqlite3_mprintf("SELECT fence_id FROM GeoFence WHERE place_id = %d", place_id);
1035
1036         ret = sqlite3_prepare_v2(db_info_s.handle, query, -1, &state, &tail);
1037         if (ret != SQLITE_OK) {
1038                 LOGI_GEOFENCE("Error: %s", sqlite3_errmsg(db_info_s.handle));
1039                 sqlite3_free(query);
1040                 return FENCE_ERR_PREPARE;
1041         }
1042         GList *fence_list = NULL;
1043         int column_index = 0;
1044         int fence_id = 0;
1045         do {
1046                 ret = sqlite3_step(state);
1047                 if (ret != SQLITE_ROW) {
1048                         LOGI_GEOFENCE("DONE...!!! : %d", ret);
1049                         break;
1050                 }
1051                 fence_id = 0;
1052                 fence_id = sqlite3_column_int(state, column_index);
1053                 fence_list = g_list_append(fence_list, GINT_TO_POINTER(fence_id));
1054                 count++;
1055         } while (ret != SQLITE_DONE);
1056         *fences = fence_list;
1057         *number_of_fences = count;
1058
1059         sqlite3_reset(state);
1060         sqlite3_finalize(state);
1061         sqlite3_free(query);
1062         return FENCE_ERR_NONE;
1063 }
1064
1065 int geofence_manager_update_geocoordinate_info(int fence_id, geocoordinate_info_s *geocoordinate_info)
1066 {
1067         FUNC_ENTRANCE_SERVER;
1068         g_return_val_if_fail(fence_id, FENCE_ERR_INVALID_PARAMETER);
1069         g_return_val_if_fail(geocoordinate_info, FENCE_ERR_INVALID_PARAMETER);
1070         sqlite3_stmt *state = NULL;
1071         const char *tail;
1072         int ret = SQLITE_OK;
1073
1074         char *query = sqlite3_mprintf("UPDATE FenceGeocoordinate SET latitude = %lf, longitude = %lf, radius = %lf where fence_id = %d;", geocoordinate_info->latitude, geocoordinate_info->longitude, geocoordinate_info->radius, fence_id);
1075
1076         ret = sqlite3_prepare_v2(db_info_s.handle, query, -1, &state, &tail);
1077         if (ret != SQLITE_OK) {
1078                 LOGI_GEOFENCE("Error: %s", sqlite3_errmsg(db_info_s.handle));
1079                 sqlite3_free(query);
1080                 return FENCE_ERR_PREPARE;
1081         }
1082
1083         ret = sqlite3_step(state);
1084         if (ret != SQLITE_DONE) {
1085                 LOGI_GEOFENCE("sqlite3_step Error[%d] : %s", ret, sqlite3_errmsg(db_info_s.handle));
1086                 sqlite3_finalize(state);
1087                 sqlite3_free(query);
1088                 return FENCE_ERR_SQLITE_FAIL;
1089         }
1090
1091         sqlite3_finalize(state);
1092         sqlite3_free(query);
1093         LOGI_GEOFENCE("fence_id: %d has been successfully updated.", fence_id);
1094         return FENCE_ERR_NONE;
1095 }
1096
1097 int geofence_manager_update_place_info(int place_id, const char *place_info_name)
1098 {
1099         FUNC_ENTRANCE_SERVER;
1100         g_return_val_if_fail(place_id, FENCE_ERR_INVALID_PARAMETER);
1101         sqlite3_stmt *state = NULL;
1102         const char *tail;
1103         int ret = SQLITE_OK;
1104         char *place_name = NULL;
1105
1106         place_name = (char *)g_malloc0(sizeof(char) * PLACE_NAME_LEN);
1107         g_strlcpy(place_name, place_info_name, PLACE_NAME_LEN);
1108
1109         char *query = sqlite3_mprintf("UPDATE Places SET place_name = %Q where place_id = %d", place_name, place_id);
1110
1111         ret = sqlite3_prepare_v2(db_info_s.handle, query, -1, &state, &tail);
1112         if (ret != SQLITE_OK) {
1113                 LOGI_GEOFENCE("Error: %s", sqlite3_errmsg(db_info_s.handle));
1114                 sqlite3_free(query);
1115                 g_free(place_name);
1116                 return FENCE_ERR_PREPARE;
1117         }
1118
1119         ret = sqlite3_step(state);
1120         if (ret != SQLITE_DONE) {
1121                 LOGI_GEOFENCE("sqlite3_step Error[%d] : %s", ret, sqlite3_errmsg(db_info_s.handle));
1122                 sqlite3_finalize(state);
1123                 g_free(place_name);
1124                 sqlite3_free(query);
1125                 return FENCE_ERR_SQLITE_FAIL;
1126         }
1127
1128         sqlite3_finalize(state);
1129         g_free(place_name);
1130         sqlite3_free(query);
1131         LOGI_GEOFENCE("place_id: %d has been successfully updated.", place_id);
1132         return FENCE_ERR_NONE;
1133 }
1134
1135 /**
1136  * This function set geocoordinate info  in DB.
1137  *
1138  * @param[in]           fence_id
1139  * @param[out]          struct of geocoordinate_info_s
1140  * @return              FENCE_ERR_NONE on success, negative values for errors
1141  */
1142 int geofence_manager_set_geocoordinate_info(int fence_id, geocoordinate_info_s *geocoordinate_info)
1143 {
1144         FUNC_ENTRANCE_SERVER;
1145         g_return_val_if_fail(fence_id, FENCE_ERR_INVALID_PARAMETER);
1146         g_return_val_if_fail(geocoordinate_info, FENCE_ERR_INVALID_PARAMETER);
1147         sqlite3_stmt *state = NULL;
1148         int ret = SQLITE_OK;
1149         int index = 0;
1150         const char *tail;
1151         int count = -1;
1152         char data_name_lat[MAX_DATA_NAME] = { 0 };
1153         char data_name_lon[MAX_DATA_NAME] = { 0 };
1154         char data_name_rad[MAX_DATA_NAME] = { 0 };
1155         char *query = sqlite3_mprintf("INSERT INTO FenceGeocoordinate(fence_id, latitude, longitude, radius, address) VALUES (?, ?, ?, ?, ?)");
1156
1157         ret = __geofence_manager_db_get_count_of_fence_id(fence_id, FENCE_GEOCOORDINATE_TAB, &count);
1158         if (ret != FENCE_ERR_NONE) {
1159                 LOGI_GEOFENCE("Fail to get geofence_manager_db_get_count_of_fence_id [%d]", ret);
1160                 sqlite3_free(query);
1161                 return ret;
1162         } else if (count) {     /* fence id has been in FenceGeocoordinate table */
1163                 sqlite3_free(query);
1164                 return FENCE_ERR_FENCE_ID;
1165         }
1166
1167         ret = sqlite3_prepare_v2(db_info_s.handle, query, -1, &state, &tail);
1168         if (ret != SQLITE_OK) {
1169                 LOGI_GEOFENCE("Error: %s", sqlite3_errmsg(db_info_s.handle));
1170                 sqlite3_free(query);
1171                 return FENCE_ERR_PREPARE;
1172         }
1173
1174         ret = sqlite3_bind_int(state, ++index, fence_id);
1175         SQLITE3_RETURN(ret, sqlite3_errmsg(db_info_s.handle), state);
1176
1177 #ifdef SUPPORT_ENCRYPTION
1178         if (password == NULL)
1179                 __geofence_manager_generate_password(password);
1180 #endif
1181
1182         ret = snprintf(data_name_lat, MAX_DATA_NAME, "%lf", geocoordinate_info->latitude);
1183
1184         ret = sqlite3_bind_text(state, ++index, data_name_lat, -1, SQLITE_STATIC);
1185
1186         /*ret = sqlite3_bind_double (state, ++index, geocoordinate_info->latitude);*/
1187         SQLITE3_RETURN(ret, sqlite3_errmsg(db_info_s.handle), state);
1188
1189         ret = snprintf(data_name_lon, MAX_DATA_NAME, "%lf", geocoordinate_info->longitude);
1190         if (ret < 0) {
1191                 LOGD_GEOFENCE("ERROR: String will be truncated");
1192                 return FENCE_ERR_STRING_TRUNCATED;
1193         }
1194
1195         ret = sqlite3_bind_text(state, ++index, data_name_lon, -1, SQLITE_STATIC);
1196         /*ret = sqlite3_bind_double (state, ++index, geocoordinate_info->longitude);*/
1197         SQLITE3_RETURN(ret, sqlite3_errmsg(db_info_s.handle), state);
1198
1199         ret = snprintf(data_name_rad, MAX_DATA_NAME, "%lf", geocoordinate_info->radius);
1200         if (ret < 0) {
1201                 LOGD_GEOFENCE("ERROR: String will be truncated");
1202                 return FENCE_ERR_STRING_TRUNCATED;
1203         }
1204
1205         ret = sqlite3_bind_text(state, ++index, data_name_rad, -1, SQLITE_STATIC);
1206         /*ret = sqlite3_bind_double (state, ++index, geocoordinate_info->radius);*/
1207         SQLITE3_RETURN(ret, sqlite3_errmsg(db_info_s.handle), state);
1208
1209         ret = sqlite3_bind_text(state, ++index, geocoordinate_info->address, -1, SQLITE_STATIC);
1210         SQLITE3_RETURN(ret, sqlite3_errmsg(db_info_s.handle), state);
1211
1212         ret = sqlite3_step(state);
1213         if (ret != SQLITE_DONE) {
1214                 LOGI_GEOFENCE("sqlite3_step Error[%d] : %s", ret, sqlite3_errmsg(db_info_s.handle));
1215                 sqlite3_finalize(state);
1216                 return FENCE_ERR_SQLITE_FAIL;
1217         }
1218
1219         sqlite3_reset(state);
1220         sqlite3_clear_bindings(state);
1221         sqlite3_finalize(state);
1222         sqlite3_free(query);
1223
1224         return FENCE_ERR_NONE;
1225 }
1226
1227 /**
1228  * This function get geocoordinate info from DB.
1229  *
1230  * @param[in]   fence_id
1231  * @param[out]  struct of geocoordinate_info_s
1232  * @return      FENCE_ERR_NONE on success, negative values for errors
1233  */
1234 int geofence_manager_get_geocoordinate_info(int fence_id, geocoordinate_info_s **geocoordinate_info)
1235 {
1236         FUNC_ENTRANCE_SERVER;
1237         g_return_val_if_fail(fence_id, FENCE_ERR_INVALID_PARAMETER);
1238         sqlite3_stmt *state = NULL;
1239         int ret = SQLITE_OK;
1240         const char *tail = NULL;
1241         int index = 0;
1242         char *data_name = NULL;
1243         char *query = sqlite3_mprintf("SELECT * FROM FenceGeocoordinate where fence_id = %d;", fence_id);
1244
1245         LOGD_GEOFENCE("current fence id is [%d]", fence_id);
1246         ret = sqlite3_prepare_v2(db_info_s.handle, query, -1, &state, &tail);
1247         if (ret != SQLITE_OK) {
1248                 LOGI_GEOFENCE("Error: %s", sqlite3_errmsg(db_info_s.handle));
1249                 sqlite3_free(query);
1250                 return FENCE_ERR_PREPARE;
1251         }
1252
1253         ret = sqlite3_step(state);
1254         if (ret != SQLITE_ROW) {
1255                 LOGI_GEOFENCE("sqlite3_step Error[%d] : %s", ret, sqlite3_errmsg(db_info_s.handle));
1256                 sqlite3_finalize(state);
1257                 sqlite3_free(query);
1258                 return FENCE_ERR_SQLITE_FAIL;
1259         }
1260
1261         *geocoordinate_info = (geocoordinate_info_s *)g_malloc0(sizeof(geocoordinate_info_s));
1262         g_return_val_if_fail(*geocoordinate_info, FENCE_ERR_INVALID_PARAMETER);
1263
1264 #ifdef SUPPORT_ENCRYPTION
1265         if (password == NULL)
1266                 __geofence_manager_generate_password(password);
1267 #endif
1268
1269         data_name = (char *) sqlite3_column_text(state, ++index);
1270
1271         if (!data_name || !strlen(data_name)) {
1272                 LOGI_GEOFENCE("ERROR: data_name is NULL!!!");
1273         } else {
1274                 (*geocoordinate_info)->latitude = atof(data_name);
1275         }
1276
1277         data_name = (char *) sqlite3_column_text(state, ++index);
1278         if (!data_name || !strlen(data_name)) {
1279                 LOGI_GEOFENCE("ERROR: data_name is NULL!!!");
1280         } else {
1281                 (*geocoordinate_info)->longitude = atof(data_name);
1282         }
1283
1284         data_name = (char *) sqlite3_column_text(state, ++index);
1285         if (!data_name || !strlen(data_name)) {
1286                 LOGI_GEOFENCE("ERROR: data_name is NULL!!!");
1287         } else {
1288                 (*geocoordinate_info)->radius = atof(data_name);
1289         }
1290
1291         g_strlcpy((*geocoordinate_info)->address, (char *) sqlite3_column_text(state, ++index), ADDRESS_LEN);
1292
1293         sqlite3_finalize(state);
1294         sqlite3_free(query);
1295
1296         return FENCE_ERR_NONE;
1297 }
1298
1299 /**
1300  * This function get ap list  from DB.
1301  *
1302  * @param[in]   fence_id
1303  * @param[out]  ap_list
1304  * @return      FENCE_ERR_NONE on success, negative values for errors
1305  */
1306 int geofence_manager_get_ap_info(const int fence_id, GList **ap_list)
1307 {
1308         FUNC_ENTRANCE_SERVER;
1309         sqlite3_stmt *state = NULL;
1310         int ret = SQLITE_OK;
1311         const char *tail = NULL;
1312         int count = -1;
1313         int i = 0;
1314         wifi_info_s *wifi_info = NULL;
1315         const char *bssid = NULL;
1316
1317         char *query1 = sqlite3_mprintf("SELECT COUNT(bssid) FROM FenceGeopointWifi where fence_id = %d;", fence_id);
1318
1319         LOGD_GEOFENCE("current fence id is [%d]", fence_id);
1320         ret = sqlite3_prepare_v2(db_info_s.handle, query1, -1, &state, &tail);
1321         if (ret != SQLITE_OK) {
1322                 LOGI_GEOFENCE("Error: %s", sqlite3_errmsg(db_info_s.handle));
1323                 sqlite3_free(query1);
1324                 return FENCE_ERR_PREPARE;
1325         }
1326
1327         ret = sqlite3_step(state);
1328         if (ret != SQLITE_ROW) {
1329                 LOGD_GEOFENCE("Fail to get count sqlite3_step");
1330                 sqlite3_finalize(state);
1331                 sqlite3_free(query1);
1332                 return FENCE_ERR_SQLITE_FAIL;
1333         }
1334
1335         count = sqlite3_column_int(state, 0);
1336         sqlite3_reset(state);
1337         sqlite3_finalize(state);
1338         sqlite3_free(query1);
1339         if (count <= 0) {
1340                 LOGI_GEOFENCE("ERROR: count = %d", count);
1341                 return FENCE_ERR_COUNT;
1342         } else {
1343                 LOGD_GEOFENCE("count[%d]", count);
1344         }
1345
1346         char *query2 = sqlite3_mprintf("SELECT * FROM FenceGeopointWifi where fence_id = %d;", fence_id);
1347
1348         ret = sqlite3_prepare_v2(db_info_s.handle, query2, -1, &state, &tail);
1349         if (ret != SQLITE_OK) {
1350                 LOGI_GEOFENCE("Error: %s", sqlite3_errmsg(db_info_s.handle));
1351                 sqlite3_free(query2);
1352                 return FENCE_ERR_PREPARE;
1353         }
1354
1355         for (i = 0; i < count; i++) {
1356                 ret = sqlite3_step(state);
1357                 if (ret != SQLITE_ROW) {
1358                         LOGI_GEOFENCE("sqlite3_step Error[%d] : %s", ret, sqlite3_errmsg(db_info_s.handle));
1359                         break;
1360                 }
1361                 wifi_info = g_slice_new0(wifi_info_s);
1362                 g_return_val_if_fail(wifi_info, -1);
1363                 if (wifi_info) {
1364                         bssid = (const char *) sqlite3_column_text(state, 1);
1365                         g_strlcpy(wifi_info->bssid, bssid, WLAN_BSSID_LEN);
1366                         *ap_list = g_list_append(*ap_list, (gpointer) wifi_info);
1367                 }
1368         }
1369
1370         sqlite3_finalize(state);
1371         sqlite3_free(query2);
1372         return FENCE_ERR_NONE;
1373 }
1374
1375 /*This function get place info from DB.
1376  *
1377  * @param[in]   place_id
1378  * @param[out]  struct of place_info_s
1379  * @return      FENCE_ERR_NONE on success, negative values for errors
1380  */
1381 int geofence_manager_get_place_info(int place_id, place_info_s **place_info)
1382 {
1383         FUNC_ENTRANCE_SERVER;
1384         g_return_val_if_fail(place_id, FENCE_ERR_INVALID_PARAMETER);
1385         sqlite3_stmt *state = NULL;
1386         int ret = SQLITE_OK;
1387         const char *tail = NULL;
1388         int index = 0;
1389         char *data_name = NULL;
1390         char *query = sqlite3_mprintf("SELECT * FROM Places where place_id = %d;", place_id);
1391
1392         LOGD_GEOFENCE("current place id is [%d]", place_id);
1393         ret = sqlite3_prepare_v2(db_info_s.handle, query, -1, &state, &tail);
1394         if (ret != SQLITE_OK) {
1395                 LOGI_GEOFENCE("Error: %s", sqlite3_errmsg(db_info_s.handle));
1396                 sqlite3_free(query);
1397                 return FENCE_ERR_PREPARE;
1398         }
1399         ret = sqlite3_step(state);
1400         if (ret != SQLITE_ROW) {
1401                 LOGI_GEOFENCE("sqlite3_step Error[%d] : %s", ret, sqlite3_errmsg(db_info_s.handle));
1402                 sqlite3_finalize(state);
1403                 sqlite3_free(query);
1404                 return FENCE_ERR_SQLITE_FAIL;
1405         }
1406         *place_info = (place_info_s *)g_malloc0(sizeof(place_info_s));
1407         g_return_val_if_fail(*place_info, FENCE_ERR_INVALID_PARAMETER);
1408
1409         data_name = (char *)sqlite3_column_text(state, ++index);
1410         if (!data_name || !strlen(data_name)) {
1411                 LOGI_GEOFENCE("ERROR: data_name is NULL!!!");
1412         } else {
1413                 (*place_info)->access_type = atof(data_name);
1414         }
1415
1416         g_strlcpy((*place_info)->place_name, (char *)sqlite3_column_text(state, ++index), PLACE_NAME_LEN);
1417         g_strlcpy((*place_info)->appid, (char *)sqlite3_column_text(state, ++index), APP_ID_LEN);
1418         sqlite3_finalize(state);
1419         sqlite3_free(query);
1420
1421         return FENCE_ERR_NONE;
1422 }
1423
1424 /**
1425  * This function insert ap list  in DB.
1426  *
1427  * @param[in]   fence_id
1428  * @param[out]  ap_list
1429  * @return      FENCE_ERR_NONE on success, negative values for errors
1430  */
1431 int geofence_manager_set_ap_info(int fence_id, GList *ap_list)
1432 {
1433         FUNC_ENTRANCE_SERVER;
1434         g_return_val_if_fail(fence_id, FENCE_ERR_INVALID_PARAMETER);
1435         g_return_val_if_fail(ap_list, FENCE_ERR_INVALID_PARAMETER);
1436         int ret = FENCE_ERR_NONE;
1437         int count = -1;
1438
1439         ret = __geofence_manager_db_get_count_of_fence_id(fence_id, FENCE_GEOPOINT_WIFI_TABLE, &count);
1440         if (ret != FENCE_ERR_NONE) {
1441                 LOGI_GEOFENCE("Fail to get geofence_manager_db_get_count_of_fence_id [%d]", ret);
1442                 return ret;
1443         } else {
1444                 if (count) {    /* fence id has been in FenceCurrentLocation table */
1445                         LOGI_GEOFENCE("count is [%d]", count);
1446                         return FENCE_ERR_FENCE_ID;
1447                 }
1448         }
1449
1450         g_list_foreach(ap_list, (GFunc) __geofence_manager_db_insert_wifi_data_info, &fence_id);
1451
1452         return FENCE_ERR_NONE;
1453 }
1454
1455 /**
1456  * This function get bluetooth info from DB.
1457  *
1458  * @param[in]   fence_id
1459  * @param[out]  bt_info which contained bssid of bluetooth and correspond of fence_id.
1460  * @return      FENCE_ERR_NONE on success, negative values for errors
1461  */
1462 int geofence_manager_get_bssid_info(const int fence_id, bssid_info_s **bssid_info)
1463 {
1464         FUNC_ENTRANCE_SERVER;
1465         sqlite3_stmt *state = NULL;
1466         int ret = SQLITE_OK;
1467         const char *tail = NULL;
1468         int count = -1;
1469         int i = 0;
1470         bssid_info_s *bssid_info_from_db = NULL;
1471         const char *bssid = NULL;
1472         const char *ssid = NULL;
1473
1474         char *query1 = sqlite3_mprintf("SELECT COUNT(bssid) FROM %s where fence_id = %d;", menu_table[FENCE_BSSID_TABLE], fence_id);
1475
1476         LOGD_GEOFENCE("current fence id is [%d]", fence_id);
1477         ret = sqlite3_prepare_v2(db_info_s.handle, query1, -1, &state, &tail);
1478         if (ret != SQLITE_OK) {
1479                 LOGI_GEOFENCE("Error: %s", sqlite3_errmsg(db_info_s.handle));
1480                 sqlite3_free(query1);
1481                 return FENCE_ERR_PREPARE;
1482         }
1483
1484         ret = sqlite3_step(state);
1485         if (ret != SQLITE_ROW) {
1486                 LOGD_GEOFENCE("Fail to get count sqlite3_step");
1487                 sqlite3_finalize(state);
1488                 sqlite3_free(query1);
1489                 return FENCE_ERR_SQLITE_FAIL;
1490         }
1491
1492         count = sqlite3_column_int(state, 0);
1493         sqlite3_reset(state);
1494         sqlite3_finalize(state);
1495         sqlite3_free(query1);
1496         if (count <= 0) {
1497                 LOGI_GEOFENCE("ERROR: count = %d", count);
1498                 return FENCE_ERR_COUNT;
1499         } else {
1500                 LOGD_GEOFENCE("count[%d]", count);
1501         }
1502
1503         char *query2 = sqlite3_mprintf("SELECT * FROM %s where fence_id = %d;", menu_table[FENCE_BSSID_TABLE], fence_id);
1504
1505         ret = sqlite3_prepare_v2(db_info_s.handle, query2, -1, &state, &tail);
1506         if (ret != SQLITE_OK) {
1507                 LOGI_GEOFENCE("Error: %s", sqlite3_errmsg(db_info_s.handle));
1508                 sqlite3_free(query2);
1509                 return FENCE_ERR_PREPARE;
1510         }
1511
1512         /*'count' should be 1. because bluetooth bssid and fence_id matched one by one.*/
1513         for (i = 0; i < count; i++) {
1514                 ret = sqlite3_step(state);
1515                 if (ret != SQLITE_ROW) {
1516                         LOGI_GEOFENCE("sqlite3_step Error[%d] : %s", ret, sqlite3_errmsg(db_info_s.handle));
1517                         break;
1518                 }
1519                 bssid_info_from_db = g_slice_new0(bssid_info_s);
1520                 g_return_val_if_fail(bssid_info_from_db, -1);
1521                 if (bssid_info_from_db) {
1522                         bssid = (const char *)sqlite3_column_text(state, 1);
1523                         ssid = (const char *)sqlite3_column_text(state, 2);
1524                         g_strlcpy(bssid_info_from_db->bssid, bssid, WLAN_BSSID_LEN);
1525                         g_strlcpy(bssid_info_from_db->ssid, ssid, WLAN_BSSID_LEN);
1526                         *bssid_info = bssid_info_from_db;
1527                 }
1528         }
1529
1530         sqlite3_finalize(state);
1531         sqlite3_free(query2);
1532         return FENCE_ERR_NONE;
1533 }
1534
1535 int geofence_manager_update_bssid_info(const int fence_id, bssid_info_s *bssid_info)
1536 {
1537         FUNC_ENTRANCE_SERVER
1538         g_return_val_if_fail(fence_id, FENCE_ERR_INVALID_PARAMETER);
1539         g_return_val_if_fail(bssid_info, FENCE_ERR_INVALID_PARAMETER);
1540         sqlite3_stmt *state = NULL;
1541         int ret = SQLITE_OK;
1542         const char *tail;
1543         char *query = sqlite3_mprintf("UPDATE %Q SET bssid = %Q where fence_id = %d;", menu_table[FENCE_BSSID_TABLE], bssid_info->bssid, fence_id);
1544
1545         ret = sqlite3_prepare_v2(db_info_s.handle, query, -1, &state, &tail);
1546         if (ret != SQLITE_OK) {
1547                 LOGI_GEOFENCE("Error: %s", sqlite3_errmsg(db_info_s.handle));
1548                 sqlite3_free(query);
1549                 return FENCE_ERR_PREPARE;
1550         }
1551
1552         ret = sqlite3_step(state);
1553         if (ret != SQLITE_DONE) {
1554                 LOGI_GEOFENCE("sqlite3_step Error[%d] : %s", ret, sqlite3_errmsg(db_info_s.handle));
1555                 sqlite3_finalize(state);
1556                 sqlite3_free(query);
1557                 return FENCE_ERR_SQLITE_FAIL;
1558         }
1559
1560         sqlite3_finalize(state);
1561         sqlite3_free(query);
1562         LOGI_GEOFENCE("Fence_id: %d has been successfully updated.", fence_id);
1563         return FENCE_ERR_NONE;
1564 }
1565
1566 /**
1567  * This function insert bssid information in DB.
1568  *
1569  * @param[in]   fence_id
1570  * @param[in]   bssid_info which contained bssid of wifi or bluetooth for geofence.
1571  * @return      FENCE_ERR_NONE on success, negative values for errors
1572  */
1573 int geofence_manager_set_bssid_info(int fence_id, bssid_info_s *bssid_info)
1574 {
1575         FUNC_ENTRANCE_SERVER
1576         g_return_val_if_fail(fence_id, FENCE_ERR_INVALID_PARAMETER);
1577         g_return_val_if_fail(bssid_info, FENCE_ERR_INVALID_PARAMETER);
1578         int ret = FENCE_ERR_NONE;
1579         int count = -1;
1580
1581         ret = __geofence_manager_db_get_count_of_fence_id(fence_id, FENCE_BSSID_TABLE, &count);
1582         if (ret != FENCE_ERR_NONE) {
1583                 LOGI_GEOFENCE("Fail to get geofence_manager_db_get_count_of_fence_id [%d]", ret);
1584                 return ret;
1585         } else {
1586                 if (count) {    /* fence id has been in FenceBssid table */
1587                         LOGI_GEOFENCE("count is [%d]", count);
1588                         return FENCE_ERR_FENCE_ID;
1589                 }
1590         }
1591
1592         ret = __geofence_manager_db_insert_bssid_info(fence_id, bssid_info->bssid, bssid_info->ssid);
1593         if (ret != FENCE_ERR_NONE) {
1594                 LOGI_GEOFENCE("Fail to insert the bssid info");
1595                 return ret;
1596         }
1597         return FENCE_ERR_NONE;
1598 }
1599
1600 /**
1601  * This function get enable status  from DB.
1602  *
1603  * @param[in]   fence_id
1604  * @param[in]   status: 1 enbale, 0 disable.
1605  * @return      FENCE_ERR_NONE on success, negative values for errors
1606  */
1607 int geofence_manager_get_enable_status(const int fence_id, int *status)
1608 {
1609         FUNC_ENTRANCE_SERVER;
1610         sqlite3_stmt *state = NULL;
1611         int ret = SQLITE_OK;
1612         const char *tail = NULL;
1613
1614         char *query = sqlite3_mprintf("SELECT enable FROM GeoFence where fence_id = %d;", fence_id);
1615
1616         LOGD_GEOFENCE("current fence id is [%d]", fence_id);
1617         ret = sqlite3_prepare_v2(db_info_s.handle, query, -1, &state, &tail);
1618         if (ret != SQLITE_OK) {
1619                 LOGI_GEOFENCE("Error: %s", sqlite3_errmsg(db_info_s.handle));
1620                 sqlite3_free(query);
1621                 return FENCE_ERR_PREPARE;
1622         }
1623
1624         ret = sqlite3_step(state);
1625         if (ret != SQLITE_ROW) {
1626                 LOGI_GEOFENCE("sqlite3_step Error[%d] : %s", ret, sqlite3_errmsg(db_info_s.handle));
1627                 sqlite3_finalize(state);
1628                 sqlite3_free(query);
1629                 return FENCE_ERR_SQLITE_FAIL;
1630         }
1631
1632         *status = sqlite3_column_int(state, 0);
1633
1634         sqlite3_finalize(state);
1635         sqlite3_free(query);
1636         return FENCE_ERR_NONE;
1637 }
1638
1639 /**
1640  * This function set  enable  on DB.
1641  *
1642  * @param[in]   fence_id
1643  * @param[in]   status: 1 enbale, 0 disable.
1644  * @return      FENCE_ERR_NONE on success, negative values for errors
1645  */
1646 int geofence_manager_set_enable_status(int fence_id, int status)
1647 {
1648         FUNC_ENTRANCE_SERVER;
1649         sqlite3_stmt *state;
1650         int ret = SQLITE_OK;
1651         const char *tail;
1652
1653         char *query = sqlite3_mprintf("UPDATE GeoFence SET enable = %d where fence_id = %d;", status, fence_id);
1654
1655         ret = sqlite3_prepare_v2(db_info_s.handle, query, -1, &state, &tail);
1656         if (ret != SQLITE_OK) {
1657                 LOGI_GEOFENCE("Error: %s", sqlite3_errmsg(db_info_s.handle));
1658                 sqlite3_free(query);
1659                 return FENCE_ERR_PREPARE;
1660         }
1661
1662         ret = sqlite3_step(state);
1663         if (ret != SQLITE_DONE) {
1664                 LOGI_GEOFENCE("sqlite3_step Error[%d] : %s", ret, sqlite3_errmsg(db_info_s.handle));
1665                 sqlite3_finalize(state);
1666                 sqlite3_free(query);
1667                 return FENCE_ERR_SQLITE_FAIL;
1668         }
1669
1670         sqlite3_finalize(state);
1671         sqlite3_free(query);
1672         return FENCE_ERR_NONE;
1673 }
1674
1675 /**
1676  * This function get name from DB.
1677  *
1678  * @param[in]   fence_id
1679  * @param[out]  name
1680  * @return      FENCE_ERR_NONE on success, negative values for errors
1681  */
1682 int geofence_manager_get_place_name(int place_id, char **name)
1683 {
1684         FUNC_ENTRANCE_SERVER;
1685         sqlite3_stmt *state = NULL;
1686         int ret = SQLITE_OK;
1687         const char *tail = NULL;
1688         char *tmp = NULL;
1689
1690         char *query = sqlite3_mprintf("SELECT place_name FROM Places where place_id = %d;", place_id);
1691
1692         LOGD_GEOFENCE("current place id is [%d]", place_id);
1693         ret = sqlite3_prepare_v2(db_info_s.handle, query, -1, &state, &tail);
1694         if (ret != SQLITE_OK) {
1695                 LOGI_GEOFENCE("Error: %s", sqlite3_errmsg(db_info_s.handle));
1696                 sqlite3_free(query);
1697                 return FENCE_ERR_PREPARE;
1698         }
1699
1700         ret = sqlite3_step(state);
1701         if (ret != SQLITE_ROW) {
1702                 LOGI_GEOFENCE("sqlite3_step Error[%d] : %s", ret, sqlite3_errmsg(db_info_s.handle));
1703                 sqlite3_finalize(state);
1704                 sqlite3_free(query);
1705                 return FENCE_ERR_SQLITE_FAIL;
1706         }
1707
1708         tmp = (char *) sqlite3_column_text(state, 0);
1709         if (!tmp || !strlen(tmp)) {
1710                 LOGI_GEOFENCE("ERROR: name is NULL!!!");
1711         } else {
1712                 *name = g_strdup(tmp);
1713         }
1714
1715         sqlite3_finalize(state);
1716         sqlite3_free(query);
1717         return FENCE_ERR_NONE;
1718 }
1719
1720 /**
1721  * This function set name on DB.
1722  *
1723  * @param[in]   fence_id
1724  * @param[in]   name
1725  * @return      FENCE_ERR_NONE on success, negative values for errors
1726  */
1727 int geofence_manager_set_place_name(int place_id, const char *name)
1728 {
1729         FUNC_ENTRANCE_SERVER;
1730         sqlite3_stmt *state;
1731         int ret = SQLITE_OK;
1732         const char *tail;
1733
1734         char *query = sqlite3_mprintf("UPDATE Places SET place_name = %Q where place_id = %d;", name, place_id);
1735
1736         ret = sqlite3_prepare_v2(db_info_s.handle, query, -1, &state, &tail);
1737         if (ret != SQLITE_OK) {
1738                 LOGI_GEOFENCE("Error: %s", sqlite3_errmsg(db_info_s.handle));
1739                 sqlite3_free(query);
1740                 return FENCE_ERR_PREPARE;
1741         }
1742
1743         ret = sqlite3_step(state);
1744         if (ret != SQLITE_DONE) {
1745                 LOGI_GEOFENCE("sqlite3_step Error[%d] : %s", ret, sqlite3_errmsg(db_info_s.handle));
1746                 sqlite3_finalize(state);
1747                 sqlite3_free(query);
1748                 return FENCE_ERR_SQLITE_FAIL;
1749         }
1750
1751         sqlite3_finalize(state);
1752         sqlite3_free(query);
1753         return FENCE_ERR_NONE;
1754 }
1755
1756 /**
1757  * This function get appid from DB.
1758  *
1759  * @param[in]  place_id
1760  * @param[in]  appid
1761  * @return     FENCE_ERR_NONE on success, negative values for errors
1762  */
1763 int geofence_manager_get_appid_from_places(int place_id, char **appid)
1764 {
1765         FUNC_ENTRANCE_SERVER;
1766         sqlite3_stmt *state = NULL;
1767         int ret = SQLITE_OK;
1768         const char *tail = NULL;
1769         char *id = NULL;
1770
1771         char *query = sqlite3_mprintf("SELECT app_id FROM Places where place_id = %d;", place_id);
1772
1773         LOGD_GEOFENCE("current place id is [%d]", place_id);
1774         ret = sqlite3_prepare_v2(db_info_s.handle, query, -1, &state, &tail);
1775         if (ret != SQLITE_OK) {
1776                 LOGI_GEOFENCE("Error: %s", sqlite3_errmsg(db_info_s.handle));
1777                 sqlite3_free(query);
1778                 return FENCE_ERR_PREPARE;
1779         }
1780
1781         ret = sqlite3_step(state);
1782         if (ret != SQLITE_ROW) {
1783                 LOGI_GEOFENCE("sqlite3_step Error[%d] : %s", ret, sqlite3_errmsg(db_info_s.handle));
1784                 sqlite3_finalize(state);
1785                 sqlite3_free(query);
1786                 return FENCE_ERR_SQLITE_FAIL;
1787         }
1788
1789         id = (char *) sqlite3_column_text(state, 0);
1790         if (!id || !strlen(id)) {
1791                 LOGI_GEOFENCE("ERROR: appid is NULL!!!");
1792         } else {
1793                 *appid = g_strdup(id);
1794         }
1795
1796         sqlite3_finalize(state);
1797         sqlite3_free(query);
1798         return FENCE_ERR_NONE;
1799 }
1800
1801 /**
1802  * This function set appid on DB.
1803  *
1804  * @param[in]   place_id
1805  * @param[in]   appid.
1806  * @return         FENCE_ERR_NONE on success, negative values for errors
1807  */
1808 int geofence_manager_set_appid_to_places(int place_id, char *appid)
1809 {
1810         FUNC_ENTRANCE_SERVER;
1811         sqlite3_stmt *state;
1812         int ret = SQLITE_OK;
1813         const char *tail;
1814
1815         char *query = sqlite3_mprintf("UPDATE Places SET app_id = %Q where place_id = %d;", appid, place_id);
1816         ret = sqlite3_prepare_v2(db_info_s.handle, query, -1, &state, &tail);
1817         if (ret != SQLITE_OK) {
1818                 LOGI_GEOFENCE("Error: %s", sqlite3_errmsg(db_info_s.handle));
1819                 sqlite3_free(query);
1820                 return FENCE_ERR_PREPARE;
1821         }
1822
1823         ret = sqlite3_step(state);
1824         if (ret != SQLITE_DONE) {
1825                 LOGI_GEOFENCE("sqlite3_step Error[%d] : %s", ret, sqlite3_errmsg(db_info_s.handle));
1826                 sqlite3_finalize(state);
1827                 sqlite3_free(query);
1828                 return FENCE_ERR_SQLITE_FAIL;
1829         }
1830
1831         sqlite3_finalize(state);
1832         sqlite3_free(query);
1833         return FENCE_ERR_NONE;
1834 }
1835
1836 /**
1837  * This function get appid from DB.
1838  *
1839  * @param[in]   fence_id
1840  * @param[in]   appid
1841  * @return      FENCE_ERR_NONE on success, negative values for errors
1842  */
1843 int geofence_manager_get_appid_from_geofence(int fence_id, char **appid)
1844 {
1845         FUNC_ENTRANCE_SERVER;
1846         sqlite3_stmt *state = NULL;
1847         int ret = SQLITE_OK;
1848         const char *tail = NULL;
1849         char *id = NULL;
1850
1851         char *query = sqlite3_mprintf("SELECT app_id FROM GeoFence where fence_id = %d;", fence_id);
1852         LOGD_GEOFENCE("current fence id is [%d]", fence_id);
1853         ret = sqlite3_prepare_v2(db_info_s.handle, query, -1, &state, &tail);
1854         if (ret != SQLITE_OK) {
1855                 LOGI_GEOFENCE("Error: %s", sqlite3_errmsg(db_info_s.handle));
1856                 sqlite3_free(query);
1857                 return FENCE_ERR_PREPARE;
1858         }
1859
1860         ret = sqlite3_step(state);
1861         if (ret != SQLITE_ROW) {
1862                 LOGI_GEOFENCE("sqlite3_step Error[%d] : %s", ret, sqlite3_errmsg(db_info_s.handle));
1863                 sqlite3_finalize(state);
1864                 sqlite3_free(query);
1865                 return FENCE_ERR_SQLITE_FAIL;
1866         }
1867
1868         id = (char *) sqlite3_column_text(state, 0);
1869         if (!id || !strlen(id)) {
1870                 LOGI_GEOFENCE("ERROR: appid is NULL!!!");
1871         } else {
1872                 *appid = g_strdup(id);
1873         }
1874
1875         sqlite3_finalize(state);
1876         sqlite3_free(query);
1877         return FENCE_ERR_NONE;
1878 }
1879
1880 /**
1881  * This function set appid on DB.
1882  *
1883  * @param[in]   fence_id
1884  * @param[in]   appid.
1885  * @return      FENCE_ERR_NONE on success, negative values for errors
1886  */
1887 int geofence_manager_set_appid_to_geofence(int fence_id, char *appid)
1888 {
1889         FUNC_ENTRANCE_SERVER;
1890         sqlite3_stmt *state;
1891         int ret = SQLITE_OK;
1892         const char *tail;
1893
1894         char *query = sqlite3_mprintf("UPDATE GeoFence SET app_id = %Q where fence_id = %d;", appid, fence_id);
1895
1896         ret = sqlite3_prepare_v2(db_info_s.handle, query, -1, &state, &tail);
1897         if (ret != SQLITE_OK) {
1898                 LOGI_GEOFENCE("Error: %s", sqlite3_errmsg(db_info_s.handle));
1899                 sqlite3_free(query);
1900                 return FENCE_ERR_PREPARE;
1901         }
1902
1903         ret = sqlite3_step(state);
1904         if (ret != SQLITE_DONE) {
1905                 LOGI_GEOFENCE("sqlite3_step Error[%d] : %s", ret, sqlite3_errmsg(db_info_s.handle));
1906                 sqlite3_finalize(state);
1907                 sqlite3_free(query);
1908                 return FENCE_ERR_SQLITE_FAIL;
1909         }
1910
1911         sqlite3_finalize(state);
1912         sqlite3_free(query);
1913         return FENCE_ERR_NONE;
1914 }
1915
1916 /**
1917  * This function get ble info from DB.
1918  *
1919  * @param[in]   fence_id
1920  * @param[in]   ble_info
1921  * @return      FENCE_ERR_NONE on success, negative values for errors
1922  */
1923 int geofence_manager_get_ble_info_from_geofence(int fence_id, char **ble_info)
1924 {
1925         FUNC_ENTRANCE_SERVER;
1926         sqlite3_stmt *state = NULL;
1927         int ret = SQLITE_OK;
1928         const char *tail = NULL;
1929         char *info = NULL;
1930
1931         char *query = sqlite3_mprintf("SELECT ble_info FROM GeoFence where fence_id = %d;", fence_id);
1932         LOGD_GEOFENCE("current fence id is [%d]", fence_id);
1933         ret = sqlite3_prepare_v2(db_info_s.handle, query, -1, &state, &tail);
1934         if (ret != SQLITE_OK) {
1935                 LOGI_GEOFENCE("Error: %s", sqlite3_errmsg(db_info_s.handle));
1936                 sqlite3_free(query);
1937                 return FENCE_ERR_PREPARE;
1938         }
1939
1940         ret = sqlite3_step(state);
1941         if (ret != SQLITE_ROW) {
1942                 LOGI_GEOFENCE("sqlite3_step Error[%d] : %s", ret, sqlite3_errmsg(db_info_s.handle));
1943                 sqlite3_finalize(state);
1944                 sqlite3_free(query);
1945                 return FENCE_ERR_SQLITE_FAIL;
1946         }
1947
1948         info = (char *) sqlite3_column_text(state, 0);
1949         if (!info || !strlen(info)) {
1950                 LOGI_GEOFENCE("ERROR: ble info is NULL!!!");
1951         } else {
1952                 *ble_info = g_strdup(info);
1953         }
1954
1955         sqlite3_finalize(state);
1956         sqlite3_free(query);
1957         return FENCE_ERR_NONE;
1958 }
1959
1960 /**
1961  * This function set ble info on DB.
1962  *
1963  * @param[in]   fence_id
1964  * @param[in]   ble_info
1965  * @return      FENCE_ERR_NONE on success, negative values for errors
1966  */
1967 int geofence_manager_set_ble_info_to_geofence(int fence_id, char *ble_info)
1968 {
1969         FUNC_ENTRANCE_SERVER;
1970         sqlite3_stmt *state;
1971         int ret = SQLITE_OK;
1972         const char *tail;
1973
1974         char *query = sqlite3_mprintf("UPDATE GeoFence SET ble_info = %Q where fence_id = %d;", ble_info, fence_id);
1975
1976         ret = sqlite3_prepare_v2(db_info_s.handle, query, -1, &state, &tail);
1977         if (ret != SQLITE_OK) {
1978                 LOGI_GEOFENCE("Error: %s", sqlite3_errmsg(db_info_s.handle));
1979                 sqlite3_free(query);
1980                 return FENCE_ERR_PREPARE;
1981         }
1982
1983         ret = sqlite3_step(state);
1984         if (ret != SQLITE_DONE) {
1985                 LOGI_GEOFENCE("sqlite3_step Error[%d] : %s", ret, sqlite3_errmsg(db_info_s.handle));
1986                 sqlite3_finalize(state);
1987                 sqlite3_free(query);
1988                 return FENCE_ERR_SQLITE_FAIL;
1989         }
1990
1991         sqlite3_finalize(state);
1992         sqlite3_free(query);
1993         return FENCE_ERR_NONE;
1994 }
1995
1996 /**
1997  * This function get geofence type from DB.
1998  *
1999  * @param[in]   fence_id
2000  * @param[in]   geofence_type_e.
2001  * @return      FENCE_ERR_NONE on success, negative values for errors
2002  */
2003 int geofence_manager_get_geofence_type(int fence_id, geofence_type_e *fence_type)
2004 {
2005         FUNC_ENTRANCE_SERVER;
2006         sqlite3_stmt *state = NULL;
2007         int ret = SQLITE_OK;
2008         const char *tail = NULL;
2009
2010         char *query = sqlite3_mprintf("SELECT geofence_type FROM GeoFence where fence_id = %d;", fence_id);
2011
2012         LOGD_GEOFENCE("current fence id is [%d]", fence_id);
2013         ret = sqlite3_prepare_v2(db_info_s.handle, query, -1, &state, &tail);
2014         if (ret != SQLITE_OK) {
2015                 LOGI_GEOFENCE("Error: %s", sqlite3_errmsg(db_info_s.handle));
2016                 sqlite3_free(query);
2017                 return FENCE_ERR_PREPARE;
2018         }
2019
2020         ret = sqlite3_step(state);
2021         if (ret != SQLITE_ROW) {
2022                 LOGI_GEOFENCE("sqlite3_step Error[%d] : %s", ret, sqlite3_errmsg(db_info_s.handle));
2023                 sqlite3_finalize(state);
2024                 sqlite3_free(query);
2025                 return FENCE_ERR_SQLITE_FAIL;
2026         }
2027
2028         *fence_type = sqlite3_column_int(state, 0);
2029
2030         sqlite3_reset(state);
2031         sqlite3_finalize(state);
2032         sqlite3_free(query);
2033
2034         return FENCE_ERR_NONE;
2035 }
2036
2037 int geofence_manager_get_place_id(int fence_id, int *place_id)
2038 {
2039         FUNC_ENTRANCE_SERVER;
2040         sqlite3_stmt *state = NULL;
2041         int ret = SQLITE_OK;
2042         const char *tail = NULL;
2043
2044         char *query = sqlite3_mprintf("SELECT place_id FROM GeoFence where fence_id = %d;", fence_id);
2045
2046         LOGD_GEOFENCE("current fence id is [%d]", fence_id);
2047         ret = sqlite3_prepare_v2(db_info_s.handle, query, -1, &state, &tail);
2048         if (ret != SQLITE_OK) {
2049                 LOGI_GEOFENCE("Error: %s", sqlite3_errmsg(db_info_s.handle));
2050                 sqlite3_free(query);
2051                 return FENCE_ERR_PREPARE;
2052         }
2053
2054         ret = sqlite3_step(state);
2055         if (ret != SQLITE_ROW) {
2056                 LOGI_GEOFENCE("sqlite3_step Error[%d] : %s", ret, sqlite3_errmsg(db_info_s.handle));
2057                 sqlite3_finalize(state);
2058                 sqlite3_free(query);
2059                 return FENCE_ERR_SQLITE_FAIL;
2060         }
2061
2062         *place_id = sqlite3_column_int(state, 0);
2063
2064         sqlite3_reset(state);
2065         sqlite3_finalize(state);
2066         sqlite3_free(query);
2067
2068         return FENCE_ERR_NONE;
2069 }
2070
2071 /**
2072  * This function get geofence/place access type from DB.
2073  *
2074  * @param[in]   fence_id/place_id
2075  * @param[in]   access_type_e.
2076  * @return      FENCE_ERR_NONE on success, negative values for errors
2077  */
2078 int geofence_manager_get_access_type(int fence_id, int place_id, access_type_e *fence_type)
2079 {
2080         FUNC_ENTRANCE_SERVER;
2081         sqlite3_stmt *state = NULL;
2082         int ret = SQLITE_OK;
2083         const char *tail = NULL;
2084         char *query = NULL;
2085
2086         if (place_id == -1)
2087                 query = sqlite3_mprintf("SELECT access_type FROM GeoFence WHERE fence_id = %d;", fence_id);
2088         else if (fence_id == -1)
2089                 query = sqlite3_mprintf("SELECT access_type FROM Places WHERE place_id = %d", place_id);
2090
2091         LOGD_GEOFENCE("current fence id is [%d]", fence_id);
2092         LOGD_GEOFENCE("current place id is [%d]", place_id);
2093         ret = sqlite3_prepare_v2(db_info_s.handle, query, -1, &state, &tail);
2094         if (ret != SQLITE_OK) {
2095                 LOGI_GEOFENCE("Error: %s", sqlite3_errmsg(db_info_s.handle));
2096                 sqlite3_free(query);
2097                 return FENCE_ERR_PREPARE;
2098         }
2099
2100         ret = sqlite3_step(state);
2101         if (ret != SQLITE_ROW) {
2102                 LOGI_GEOFENCE("sqlite3_step Error[%d] : %s", ret, sqlite3_errmsg(db_info_s.handle));
2103                 sqlite3_finalize(state);
2104                 sqlite3_free(query);
2105                 return FENCE_ERR_SQLITE_FAIL;
2106         }
2107
2108         *fence_type = sqlite3_column_int(state, 0);
2109
2110         sqlite3_reset(state);
2111         sqlite3_finalize(state);
2112         sqlite3_free(query);
2113
2114         return FENCE_ERR_NONE;
2115 }
2116
2117 /**
2118  * This function set geofence type on DB.
2119  *
2120  * @param[in]   fence_id
2121  * @param[in]   fence_type.
2122  * @return      FENCE_ERR_NONE on success, negative values for errors
2123  */
2124 int geofence_manager_set_geofence_type(int fence_id, geofence_type_e fence_type)
2125 {
2126         FUNC_ENTRANCE_SERVER;
2127         sqlite3_stmt *state;
2128         int ret = SQLITE_OK;
2129         const char *tail;
2130
2131         char *query = sqlite3_mprintf("UPDATE GeoFence SET geofence_type = %d where fence_id = %d;", fence_type, fence_id);
2132
2133         ret = sqlite3_prepare_v2(db_info_s.handle, query, -1, &state, &tail);
2134         if (ret != SQLITE_OK) {
2135                 LOGI_GEOFENCE("Error: %s", sqlite3_errmsg(db_info_s.handle));
2136                 sqlite3_free(query);
2137                 return FENCE_ERR_PREPARE;
2138         }
2139
2140         ret = sqlite3_step(state);
2141         if (ret != SQLITE_DONE) {
2142                 LOGI_GEOFENCE("sqlite3_step Error[%d] : %s", ret, sqlite3_errmsg(db_info_s.handle));
2143                 sqlite3_finalize(state);
2144                 sqlite3_free(query);
2145                 return FENCE_ERR_SQLITE_FAIL;
2146         }
2147
2148         sqlite3_finalize(state);
2149         sqlite3_free(query);
2150         return FENCE_ERR_NONE;
2151 }
2152
2153 /**
2154  * This function get geofence place_id from DB.
2155  *
2156  * @param[in]   fence_id
2157  * @param[in]   place_id
2158  * @return      FENCE_ERR_NONE on success, negative values for errors
2159  */
2160 int geofence_manager_get_placeid_from_geofence(int fence_id, int *place_id)
2161 {
2162         FUNC_ENTRANCE_SERVER;
2163         sqlite3_stmt *state = NULL;
2164         int ret = SQLITE_OK;
2165         const char *tail = NULL;
2166
2167         char *query = sqlite3_mprintf("SELECT place_id FROM GeoFence where fence_id = %d;", fence_id);
2168         LOGD_GEOFENCE("current fence id is [%d]", fence_id);
2169         ret = sqlite3_prepare_v2(db_info_s.handle, query, -1, &state, &tail);
2170         if (ret != SQLITE_OK) {
2171                 LOGI_GEOFENCE("Error: %s", sqlite3_errmsg(db_info_s.handle));
2172                 sqlite3_free(query);
2173                 return FENCE_ERR_PREPARE;
2174         }
2175
2176         ret = sqlite3_step(state);
2177         if (ret != SQLITE_ROW) {
2178                 LOGI_GEOFENCE("sqlite3_step Error[%d] : %s", ret, sqlite3_errmsg(db_info_s.handle));
2179                 sqlite3_finalize(state);
2180                 sqlite3_free(query);
2181                 return FENCE_ERR_SQLITE_FAIL;
2182         }
2183
2184         *place_id = sqlite3_column_int(state, 0);
2185
2186         sqlite3_reset(state);
2187         sqlite3_finalize(state);
2188         sqlite3_free(query);
2189
2190         return FENCE_ERR_NONE;
2191 }
2192
2193 /**
2194  * This function get running status from DB.
2195  *
2196  * @param[in]   fence_id
2197  * @param[in]   int
2198  * @return      FENCE_ERR_NONE on success, negative values for errors
2199  */
2200 int geofence_manager_get_running_status(int fence_id, int *running_status)
2201 {
2202         FUNC_ENTRANCE_SERVER;
2203         sqlite3_stmt *state = NULL;
2204         int ret = SQLITE_OK;
2205         const char *tail = NULL;
2206
2207         char *query = sqlite3_mprintf("SELECT running_status FROM GeoFence where fence_id = %d;", fence_id);
2208
2209         LOGD_GEOFENCE("current fence id is [%d]", fence_id);
2210         ret = sqlite3_prepare_v2(db_info_s.handle, query, -1, &state, &tail);
2211         if (ret != SQLITE_OK) {
2212                 LOGI_GEOFENCE("Error: %s", sqlite3_errmsg(db_info_s.handle));
2213                 sqlite3_free(query);
2214                 return FENCE_ERR_PREPARE;
2215         }
2216
2217         ret = sqlite3_step(state);
2218         if (ret != SQLITE_ROW) {
2219                 LOGI_GEOFENCE("sqlite3_step Error[%d] : %s", ret, sqlite3_errmsg(db_info_s.handle));
2220                 sqlite3_finalize(state);
2221                 sqlite3_free(query);
2222                 return FENCE_ERR_SQLITE_FAIL;
2223         }
2224
2225         *running_status = sqlite3_column_int(state, 0);
2226
2227         sqlite3_reset(state);
2228         sqlite3_finalize(state);
2229         sqlite3_free(query);
2230
2231         return FENCE_ERR_NONE;
2232 }
2233
2234 /**
2235  * This function set running state on DB.
2236  *
2237  * @param[in]   fence_id
2238  * @param[in]   state
2239  * @return      FENCE_ERR_NONE on success, negative values for errors
2240  */
2241 int geofence_manager_set_running_status(int fence_id, int running_status)
2242 {
2243         FUNC_ENTRANCE_SERVER;
2244         sqlite3_stmt *state;
2245         int ret = SQLITE_OK;
2246         const char *tail;
2247
2248         char *query = sqlite3_mprintf("UPDATE GeoFence SET running_status = %d where fence_id = %d;", running_status, fence_id);
2249
2250         ret = sqlite3_prepare_v2(db_info_s.handle, query, -1, &state, &tail);
2251         if (ret != SQLITE_OK) {
2252                 LOGI_GEOFENCE("Error: %s", sqlite3_errmsg(db_info_s.handle));
2253                 sqlite3_free(query);
2254                 return FENCE_ERR_PREPARE;
2255         }
2256
2257         ret = sqlite3_step(state);
2258         if (ret != SQLITE_DONE) {
2259                 LOGI_GEOFENCE("sqlite3_step Error[%d] : %s", ret, sqlite3_errmsg(db_info_s.handle));
2260                 sqlite3_finalize(state);
2261                 sqlite3_free(query);
2262                 return FENCE_ERR_SQLITE_FAIL;
2263         }
2264
2265         sqlite3_finalize(state);
2266         sqlite3_free(query);
2267         return FENCE_ERR_NONE;
2268 }
2269
2270 /**
2271  * This function get direction type from DB.
2272  *
2273  * @param[in]   fence_id
2274  * @param[in]   direction
2275  * @return      FENCE_ERR_NONE on success, negative values for errors
2276  */
2277 int geofence_manager_get_direction(int fence_id, geofence_direction_e *direction)
2278 {
2279         FUNC_ENTRANCE_SERVER;
2280         sqlite3_stmt *state = NULL;
2281         int ret = SQLITE_OK;
2282         const char *tail = NULL;
2283
2284         char *query = sqlite3_mprintf("SELECT direction FROM GeoFence where fence_id = %d;", fence_id);
2285         LOGD_GEOFENCE("current fence id is [%d]", fence_id);
2286         ret = sqlite3_prepare_v2(db_info_s.handle, query, -1, &state, &tail);
2287         if (ret != SQLITE_OK) {
2288                 LOGI_GEOFENCE("Error: %s", sqlite3_errmsg(db_info_s.handle));
2289                 sqlite3_free(query);
2290                 return FENCE_ERR_PREPARE;
2291         }
2292
2293         ret = sqlite3_step(state);
2294         if (ret != SQLITE_ROW) {
2295                 LOGI_GEOFENCE("sqlite3_step Error[%d] : %s", ret, sqlite3_errmsg(db_info_s.handle));
2296                 sqlite3_finalize(state);
2297                 sqlite3_free(query);
2298                 return FENCE_ERR_SQLITE_FAIL;
2299         }
2300
2301         *direction = sqlite3_column_int(state, 0);
2302
2303         sqlite3_finalize(state);
2304         sqlite3_free(query);
2305
2306         return FENCE_ERR_NONE;
2307 }
2308
2309 /**
2310  * This function set direction type on DB.
2311  *
2312  * @param[in]   fence_id
2313  * @param[in]   direction
2314  * @return      FENCE_ERR_NONE on success, negative values for errors
2315  */
2316 int geofence_manager_set_direction(int fence_id, geofence_direction_e direction)
2317 {
2318         FUNC_ENTRANCE_SERVER;
2319         sqlite3_stmt *state;
2320         int ret = SQLITE_OK;
2321         const char *tail;
2322
2323         char *query = sqlite3_mprintf("UPDATE GeoFence SET direction = %d where fence_id = %d;", direction, fence_id);
2324
2325         ret = sqlite3_prepare_v2(db_info_s.handle, query, -1, &state, &tail);
2326         if (ret != SQLITE_OK) {
2327                 LOGI_GEOFENCE("Error: %s", sqlite3_errmsg(db_info_s.handle));
2328                 sqlite3_free(query);
2329                 return FENCE_ERR_PREPARE;
2330         }
2331
2332         ret = sqlite3_step(state);
2333         if (ret != SQLITE_DONE) {
2334                 LOGI_GEOFENCE("sqlite3_step Error[%d] : %s", ret, sqlite3_errmsg(db_info_s.handle));
2335                 sqlite3_finalize(state);
2336                 sqlite3_free(query);
2337                 return FENCE_ERR_SQLITE_FAIL;
2338         }
2339
2340         sqlite3_finalize(state);
2341         sqlite3_free(query);
2342
2343         return FENCE_ERR_NONE;
2344 }
2345
2346 /**
2347  * This function remove fence from DB.
2348  *
2349  * @param[in]    fence_id
2350  * @return       FENCE_ERR_NONE on success, negative values for errors
2351  */
2352 int geofence_manager_delete_fence_info(int fence_id)
2353 {
2354         FUNC_ENTRANCE_SERVER;
2355         g_return_val_if_fail(fence_id, FENCE_ERR_INVALID_PARAMETER);
2356         int ret = FENCE_ERR_NONE;
2357         geofence_type_e fence_type = GEOFENCE_INVALID;
2358
2359         ret = geofence_manager_get_geofence_type(fence_id, &fence_type);
2360         if (FENCE_ERR_NONE != ret) {
2361                 LOGI_GEOFENCE("Fail to geofence_manager_delete_fence_point_info");
2362                 return ret;
2363         }
2364
2365         ret = __geofence_manager_db_enable_foreign_keys();
2366         if (FENCE_ERR_NONE != ret) {
2367                 LOGI_GEOFENCE("Fail to geofence_manager_db_enable_foreign_keys");
2368                 return ret;
2369         }
2370
2371         ret = __geofence_manager_delete_table(fence_id, FENCE_MAIN_TABLE);
2372         if (FENCE_ERR_NONE != ret) {
2373                 LOGI_GEOFENCE("Fail to geofence_manager_delete_fence_point_info");
2374                 return ret;
2375         }
2376
2377         return ret;
2378 }
2379
2380 /**
2381  * This function remove place from DB.
2382  *
2383  * @param[in]      place_id
2384  * @return         FENCE_ERR_NONE on success, negative values for errors
2385  */
2386 int geofence_manager_delete_place_info(int place_id)
2387 {
2388         FUNC_ENTRANCE_SERVER;
2389         g_return_val_if_fail(place_id, FENCE_ERR_INVALID_PARAMETER);
2390         int ret = FENCE_ERR_NONE;
2391
2392         ret = __geofence_manager_db_enable_foreign_keys();
2393         if (FENCE_ERR_NONE != ret) {
2394                 LOGI_GEOFENCE("Fail to geofence_manager_db_enable_foreign_keys");
2395                 return ret;
2396         }
2397
2398         ret = __geofence_manager_delete_place_table(place_id);
2399         if (FENCE_ERR_NONE != ret) {
2400                 LOGI_GEOFENCE("Fail to geofence_manager_delete_place_info");
2401                 return ret;
2402         }
2403
2404         return ret;
2405 }
2406
2407 /**
2408  * This function close  DB handle.
2409  *
2410  * @param[in]    fence_id
2411  * @return         FENCE_ERR_NONE on success, negative values for errors
2412  */
2413 int geofence_manager_close_db(void)
2414 {
2415         FUNC_ENTRANCE_SERVER;
2416         int ret = SQLITE_OK;
2417
2418         if (db_info_s.handle == NULL) {
2419                 return FENCE_ERR_NONE;
2420         }
2421
2422         ret = db_util_close(db_info_s.handle);
2423         if (ret != SQLITE_OK) {
2424                 LOGI_GEOFENCE("Close DB ERROR!!!");
2425                 return FENCE_ERR_SQLITE_FAIL;
2426         }
2427
2428         return FENCE_ERR_NONE;
2429 }
2430
2431 /**
2432  * This function deletes all data on db.
2433  *
2434  * @return         FENCE_ERR_NONE on success, negative values for errors
2435  */
2436 int geofence_manager_reset(void)
2437 {
2438         FUNC_ENTRANCE_SERVER;
2439         sqlite3_stmt *state = NULL;
2440         int ret = SQLITE_OK;
2441
2442         ret = __geofence_manager_db_enable_foreign_keys();
2443         if (FENCE_ERR_NONE != ret) {
2444                 LOGI_GEOFENCE("Fail to geofence_manager_db_enable_foreign_keys");
2445                 return ret;
2446         }
2447
2448         char *query_two = sqlite3_mprintf("DELETE from %Q;", menu_table[FENCE_MAIN_TABLE]);
2449
2450         ret = sqlite3_prepare_v2(db_info_s.handle, query_two, -1, &state, NULL);
2451         if (SQLITE_OK != ret) {
2452                 LOGI_GEOFENCE("Fail to connect to table. Error[%s]", sqlite3_errmsg(db_info_s.handle));
2453                 sqlite3_free(query_two);
2454                 return FENCE_ERR_SQLITE_FAIL;
2455         }
2456
2457         ret = sqlite3_step(state);
2458         if (SQLITE_DONE != ret) {
2459                 LOGI_GEOFENCE("Fail to step. Error[%d]", ret);
2460                 sqlite3_finalize(state);
2461                 sqlite3_free(query_two);
2462                 return FENCE_ERR_SQLITE_FAIL;
2463         }
2464         sqlite3_reset(state);
2465         sqlite3_free(query_two);
2466
2467         char *query_three = sqlite3_mprintf("UPDATE sqlite_sequence SET seq = 0 where name = %Q;", menu_table[FENCE_MAIN_TABLE]);
2468
2469         ret = sqlite3_prepare_v2(db_info_s.handle, query_three, -1, &state, NULL);
2470         if (SQLITE_OK != ret) {
2471                 LOGI_GEOFENCE("Fail to connect to table. Error[%s]", sqlite3_errmsg(db_info_s.handle));
2472                 sqlite3_free(query_three);
2473                 return FENCE_ERR_SQLITE_FAIL;
2474         }
2475
2476         ret = sqlite3_step(state);
2477         if (SQLITE_DONE != ret) {
2478                 LOGI_GEOFENCE("Fail to step. Error[%d]", ret);
2479                 sqlite3_finalize(state);
2480                 sqlite3_free(query_three);
2481                 return FENCE_ERR_SQLITE_FAIL;
2482         }
2483
2484         sqlite3_reset(state);
2485         sqlite3_finalize(state);
2486         sqlite3_free(query_three);
2487         return FENCE_ERR_NONE;
2488 }
2489
2490 /**
2491  * This function copy source wifi info to dest wifi info.
2492  *
2493  * @param[in]    src_wifi
2494  * @param[out]  dest_wifi
2495  * @return         FENCE_ERR_NONE on success, negative values for errors
2496  */
2497 int geofence_manager_copy_wifi_info(wifi_info_s *src_wifi, wifi_info_s **dest_wifi)
2498 {
2499         FUNC_ENTRANCE_SERVER;
2500         g_return_val_if_fail(src_wifi, FENCE_ERR_INVALID_PARAMETER);
2501
2502         *dest_wifi = (wifi_info_s *)g_malloc0(sizeof(wifi_info_s));
2503         g_return_val_if_fail(*dest_wifi, -1);
2504
2505         g_strlcpy((*dest_wifi)->bssid, src_wifi->bssid, WLAN_BSSID_LEN);
2506
2507         return FENCE_ERR_NONE;
2508 }
2509
2510 /**
2511 * This function create a wifi infor .
2512 *
2513 * @param[in]    fence_id
2514 * @param[in]    bssid
2515 * @param[out]   wifi info
2516 * @return       FENCE_ERR_NONE on success, negative values for errors
2517 */
2518 int geofence_manager_create_wifi_info(int fence_id, char *bssid, wifi_info_s **new_wifi)
2519 {
2520         FUNC_ENTRANCE_SERVER;
2521         g_return_val_if_fail(fence_id >= 0, FENCE_ERR_INVALID_PARAMETER);
2522         g_return_val_if_fail(bssid, FENCE_ERR_INVALID_PARAMETER);
2523
2524         *new_wifi = (wifi_info_s *)g_malloc0(sizeof(wifi_info_s));
2525         g_strlcpy((*new_wifi)->bssid, bssid, WLAN_BSSID_LEN);
2526
2527         return FENCE_ERR_NONE;
2528 }
2529
2530 /**
2531 * This function get fence id count  by params such as app id and fence type and enable status .
2532 *
2533 * @param[in]    app_id : if app_id == NULL: ALL
2534 * @param[in]    fence_type:if GEOFENCE_TYPE_INVALID == NULL: ALL fence type
2535 * @param[in]    enable_status
2536 * @param[out]  fence id count
2537 * @return           FENCE_ERR_NONE on success, negative values for errors
2538 */
2539 int geofence_manager_get_count_by_params(const char *app_id, geofence_type_e fence_type, int *count)
2540 {
2541         FUNC_ENTRANCE_SERVER;
2542         sqlite3_stmt *state = NULL;
2543         int ret = SQLITE_OK;
2544         const char *tail = NULL;
2545         char *query = NULL;
2546
2547         if (NULL == app_id) {
2548                 if (GEOFENCE_INVALID != fence_type) {   /* app_id == NULL : All  and  GEOFENCE_TYPE_INVALID != fence_type */
2549                         query = sqlite3_mprintf("SELECT COUNT(fence_id) FROM GeoFence where geofence_type = %d ;", fence_type);
2550                 } else {
2551                         query = sqlite3_mprintf("SELECT COUNT(fence_id) FROM GeoFence ;");
2552                 }
2553         } else {                        /*app_id not NULL */
2554                 if (GEOFENCE_INVALID != fence_type) {   /* app_id not NULL   and  GEOFENCE_TYPE_INVALID != fence_type */
2555                         query = sqlite3_mprintf("SELECT COUNT(fence_id) FROM GeoFence where app_id = %Q AND geofence_type = %d ;", app_id, fence_type);
2556                 } else {
2557                         query = sqlite3_mprintf("SELECT COUNT(fence_id) FROM GeoFence where app_id = %Q ;", app_id);
2558                 }
2559         }
2560
2561         LOGI_GEOFENCE("app_id[%s] fence_type[%d] ", app_id, fence_type);
2562         ret = sqlite3_prepare_v2(db_info_s.handle, query, -1, &state, &tail);
2563         if (ret != SQLITE_OK) {
2564                 LOGI_GEOFENCE("Error: %s", sqlite3_errmsg(db_info_s.handle));
2565                 sqlite3_free(query);
2566                 return FENCE_ERR_PREPARE;
2567         }
2568
2569         ret = sqlite3_step(state);
2570         if (ret != SQLITE_ROW) {
2571                 LOGI_GEOFENCE("sqlite3_step Error[%d] : %s", ret, sqlite3_errmsg(db_info_s.handle));
2572                 sqlite3_finalize(state);
2573                 sqlite3_free(query);
2574                 return FENCE_ERR_SQLITE_FAIL;
2575         }
2576
2577         *count = sqlite3_column_int(state, 0);
2578
2579         if (*count <= 0) {
2580                 LOGI_GEOFENCE("ERROR: count = %d", *count);
2581                 return FENCE_ERR_COUNT;
2582         } else {
2583                 LOGI_GEOFENCE("count[%d]", *count);
2584         }
2585
2586         sqlite3_reset(state);
2587         sqlite3_finalize(state);
2588         sqlite3_free(query);
2589         return FENCE_ERR_NONE;
2590 }
2591
2592 /*
2593         app_id == NULL : All, geofence_type_e : INVALID - all, IN enable_status : enable, disable or both. Output : a list of geofence_id
2594 */
2595 int geofence_manager_get_fences(const char *app_id, geofence_type_e fence_type, GList **fences)
2596 {
2597         FUNC_ENTRANCE_SERVER;
2598         sqlite3_stmt *state = NULL;
2599         int ret = SQLITE_OK;
2600         const char *tail = NULL;
2601         char *query = NULL;
2602         int i = 0;
2603         int fence_id = 0;
2604         int count = -1;
2605
2606         ret = geofence_manager_get_count_by_params(app_id, fence_type, &count);
2607         if (ret != FENCE_ERR_NONE) {
2608                 LOGI_GEOFENCE("ERROR: geofence_manager_get_count_of_fences_by_app.");
2609                 return ret;
2610         }
2611
2612         if (NULL == app_id) {
2613                 if (GEOFENCE_INVALID != fence_type) {   /* app_id == NULL : All  and  GEOFENCE_TYPE_INVALID != fence_type */
2614                         query = sqlite3_mprintf("SELECT fence_id FROM GeoFence where geofence_type = %d;", fence_type);
2615                 } else {
2616                         query = sqlite3_mprintf("SELECT fence_id FROM GeoFence;");
2617                 }
2618         } else {                        /*app_id not NULL */
2619                 if (GEOFENCE_INVALID != fence_type) {   /* app_id not NULL   and  GEOFENCE_TYPE_INVALID != fence_type */
2620                         query = sqlite3_mprintf("SELECT fence_id FROM GeoFence where app_id = %Q AND geofence_type = %d ;", app_id, fence_type);
2621                 } else {
2622                         query = sqlite3_mprintf("SELECT fence_id FROM GeoFence where app_id = %Q;", app_id);
2623                 }
2624         }
2625
2626         ret = sqlite3_prepare_v2(db_info_s.handle, query, -1, &state, &tail);
2627         if (ret != SQLITE_OK) {
2628                 LOGI_GEOFENCE("Error: %s", sqlite3_errmsg(db_info_s.handle));
2629                 sqlite3_free(query);
2630                 return FENCE_ERR_PREPARE;
2631         }
2632
2633         for (i = 0; i < count; i++) {
2634                 ret = sqlite3_step(state);
2635                 if (ret != SQLITE_ROW) {
2636                         LOGI_GEOFENCE("sqlite3_step Error[%d] : %s", ret, sqlite3_errmsg(db_info_s.handle));
2637                         break;
2638                 }
2639                 fence_id = sqlite3_column_int(state, 0);
2640                 LOGI_GEOFENCE("fence id is [%d]", fence_id);
2641                 *fences = g_list_append(*fences, (gpointer) GINT_TO_POINTER(fence_id));
2642         }
2643
2644         sqlite3_reset(state);
2645         sqlite3_finalize(state);
2646         sqlite3_free(query);
2647         return FENCE_ERR_NONE;
2648 }
2649
2650 int geofence_manager_get_count_of_fences(int *count)
2651 {
2652         FUNC_ENTRANCE_SERVER;
2653         sqlite3_stmt *state = NULL;
2654         int ret = SQLITE_OK;
2655         const char *tail = NULL;
2656         char *query = sqlite3_mprintf("SELECT COUNT(fence_id) FROM GeoFence;");
2657
2658         ret = sqlite3_prepare_v2(db_info_s.handle, query, -1, &state, &tail);
2659         if (ret != SQLITE_OK) {
2660                 LOGI_GEOFENCE("Error: %s", sqlite3_errmsg(db_info_s.handle));
2661                 sqlite3_free(query);
2662                 return FENCE_ERR_PREPARE;
2663         }
2664
2665         ret = sqlite3_step(state);
2666         if (ret != SQLITE_ROW) {
2667                 LOGI_GEOFENCE("sqlite3_step Error[%d] : %s", ret, sqlite3_errmsg(db_info_s.handle));
2668                 sqlite3_finalize(state);
2669                 sqlite3_free(query);
2670                 return FENCE_ERR_SQLITE_FAIL;
2671         }
2672
2673         *count = sqlite3_column_int(state, 0);
2674
2675         if (*count < 0) {
2676                 LOGI_GEOFENCE("ERROR: count = %d", *count);
2677                 return FENCE_ERR_COUNT;
2678         } else {
2679                 LOGI_GEOFENCE("count[%d]", *count);
2680         }
2681
2682         sqlite3_reset(state);
2683         sqlite3_finalize(state);
2684         sqlite3_free(query);
2685         return FENCE_ERR_NONE;
2686 }
2687
2688 int geofence_manager_get_place_count_by_placeid(int place_id, int *count)
2689 {
2690         FUNC_ENTRANCE_SERVER;
2691         sqlite3_stmt *state = NULL;
2692         int ret = SQLITE_OK;
2693         const char *tail = NULL;
2694         char *query = sqlite3_mprintf("SELECT COUNT(place_id) FROM Places WHERE place_id=%d;", place_id);
2695
2696         ret = sqlite3_prepare_v2(db_info_s.handle, query, -1, &state, &tail);
2697         if (ret != SQLITE_OK) {
2698                 LOGI_GEOFENCE("Error: %s", sqlite3_errmsg(db_info_s.handle));
2699                 sqlite3_free(query);
2700                 return FENCE_ERR_PREPARE;
2701         }
2702
2703         ret = sqlite3_step(state);
2704         if (ret != SQLITE_ROW) {
2705                 LOGI_GEOFENCE("sqlite3_step Error[%d] : %s", ret, sqlite3_errmsg(db_info_s.handle));
2706                 sqlite3_finalize(state);
2707                 sqlite3_free(query);
2708                 return FENCE_ERR_SQLITE_FAIL;
2709         }
2710
2711         *count = sqlite3_column_int(state, 0);
2712
2713         if (*count < 0) {
2714                 LOGI_GEOFENCE("ERROR: place count = %d", *count);
2715                 return FENCE_ERR_COUNT;
2716         } else {
2717                 LOGI_GEOFENCE("place count[%d]", *count);
2718         }
2719
2720         sqlite3_reset(state);
2721         sqlite3_finalize(state);
2722         sqlite3_free(query);
2723         return FENCE_ERR_NONE;
2724 }