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