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