1 /* Copyright 2014 Samsung Electronics Co., Ltd All Rights Reserved
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
7 * http://www.apache.org/licenses/LICENSE-2.0
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.
22 #include <bluetooth.h>
23 #include <wifi-manager.h>
24 #include <tzplatform_config.h>
25 #include <sys/types.h>
28 #include "debug_util.h"
29 #include "geofence_server.h"
30 #include "geofence_server_db.h"
31 #include "geofence_server_private.h"
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
38 #define GEOFENCE_DB_NAME ".geofence-server.db"
39 #define GEOFENCE_DB_FILE tzplatform_mkpath(TZ_USER_DB, GEOFENCE_DB_NAME)
41 #define MAX_DATA_NAME 20
44 #define GEOFENCE_INVALID 0
46 char *menu_table[4] = { "GeoFence", "FenceGeocoordinate", "FenceGeopointWifi", "FenceBssid" };
48 const char *group_id = NULL;
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";
58 FENCE_MAIN_TABLE = 0, /*GeoFence */
59 FENCE_GEOCOORDINATE_TAB, /*FenceGeocoordinate */
60 FENCE_GEOPOINT_WIFI_TABLE, /*FenceCurrentLocation */
61 FENCE_BSSID_TABLE /*FenceBluetoothBssid */
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; \
85 * +----------+-------+-------+------------+-------+-------+-----------+---------+
86 * | fence_id | name | app_id | geofence_type |direction |enable |smart_assist_id|time_stamp
87 * +-------+-------+-------+-------+
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)";
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)";
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)";
120 static inline int begin_transaction(void)
122 FUNC_ENTRANCE_SERVER;
126 ret = sqlite3_prepare_v2(db_info_s.handle, "BEGIN TRANSACTION", -1, &stmt, NULL);
128 if (ret != SQLITE_OK) {
129 LOGI_GEOFENCE("Error: %s", sqlite3_errmsg(db_info_s.handle));
130 return FENCE_ERR_SQLITE_FAIL;
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;
139 sqlite3_finalize(stmt);
140 return FENCE_ERR_NONE;
143 static inline int rollback_transaction(void)
145 FUNC_ENTRANCE_SERVER;
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;
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;
161 sqlite3_finalize(stmt);
162 return FENCE_ERR_NONE;
165 static inline int commit_transaction(void)
167 FUNC_ENTRANCE_SERVER;
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;
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;
183 sqlite3_finalize(stmt);
184 return FENCE_ERR_NONE;
187 static inline int __geofence_manager_db_create_places_table(void)
189 FUNC_ENTRANCE_SERVER;
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);
197 return FENCE_ERR_SQLITE_FAIL;
200 if (sqlite3_changes(db_info_s.handle) == 0)
201 LOGI_GEOFENCE("No changes to DB");
203 return FENCE_ERR_NONE;
206 static inline int __geofence_manager_db_create_geofence_table(void)
208 FUNC_ENTRANCE_SERVER;
212 ddl = sqlite3_mprintf("CREATE TABLE GeoFence ( fence_id INTEGER PRIMARY KEY AUTOINCREMENT, place_id INTEGER, enable INTEGER, app_id TEXT NOT NULL, geofence_type INTEGER, access_type INTEGER, running_status INTEGER, ble_info TEXT, FOREIGN KEY(place_id) REFERENCES Places(place_id) ON DELETE CASCADE)");
214 if (sqlite3_exec(db_info_s.handle, ddl, NULL, NULL, &err) != SQLITE_OK) {
215 LOGI_GEOFENCE("Failed to execute the DDL (%s)", err);
217 return FENCE_ERR_SQLITE_FAIL;
220 if (sqlite3_changes(db_info_s.handle) == 0)
221 LOGI_GEOFENCE("No changes to DB");
223 return FENCE_ERR_NONE;
226 static inline int __geofence_manager_db_create_geocoordinate_table(void)
228 FUNC_ENTRANCE_SERVER;
232 ddl = sqlite3_mprintf("CREATE TABLE FenceGeocoordinate ( fence_id INTEGER PRIMARY KEY, latitude TEXT NOT NULL, longitude TEXT NOT NULL, radius TEXT NOT NULL, address TEXT, FOREIGN KEY(fence_id) REFERENCES GeoFence(fence_id) ON DELETE CASCADE ON UPDATE CASCADE)");
234 if (sqlite3_exec(db_info_s.handle, ddl, NULL, NULL, &err) != SQLITE_OK) {
235 LOGI_GEOFENCE("Failed to execute the DDL (%s)", err);
237 return FENCE_ERR_SQLITE_FAIL;
240 if (sqlite3_changes(db_info_s.handle) == 0)
241 LOGI_GEOFENCE("No changes to DB");
243 return FENCE_ERR_NONE;
246 static inline int __geofence_manager_db_create_wifi_data_table(void)
248 FUNC_ENTRANCE_SERVER;
252 ddl = sqlite3_mprintf("CREATE TABLE FenceGeopointWifi (fence_id INTEGER, bssid TEXT, FOREIGN KEY(fence_id) REFERENCES GeoFence(fence_id) ON DELETE CASCADE ON UPDATE CASCADE)");
254 if (sqlite3_exec(db_info_s.handle, ddl, NULL, NULL, &err) != SQLITE_OK) {
255 LOGI_GEOFENCE("Failed to execute the DDL (%s)", err);
257 return FENCE_ERR_SQLITE_FAIL;
260 if (sqlite3_changes(db_info_s.handle) == 0)
261 LOGI_GEOFENCE("No changes to DB");
263 return FENCE_ERR_NONE;
266 /* DB table for save the pair of fence id and bluetooth bssid */
267 static inline int __geofence_manager_db_create_bssid_table(void)
273 ddl = sqlite3_mprintf("CREATE TABLE FenceBssid (fence_id INTEGER PRIMARY KEY, bssid TEXT, ssid TEXT, FOREIGN KEY(fence_id) REFERENCES GeoFence(fence_id) ON DELETE CASCADE ON UPDATE CASCADE)");
275 if (sqlite3_exec(db_info_s.handle, ddl, NULL, NULL, &err) != SQLITE_OK) {
276 LOGI_GEOFENCE("Failed to execute the DDL (%s)", err);
278 return FENCE_ERR_SQLITE_FAIL;
281 if (sqlite3_changes(db_info_s.handle) == 0)
282 LOGI_GEOFENCE("No changes to DB");
284 return FENCE_ERR_NONE;
287 static int __geofence_manager_open_db_handle(const int open_flag)
289 LOGI_GEOFENCE("enter");
292 ret = db_util_open_with_options(GEOFENCE_DB_FILE, &db_info_s.handle, open_flag, NULL);
293 if (ret != SQLITE_OK) {
294 LOGI_GEOFENCE("sqlite3_open_v2 Error[%d] : %s", ret, sqlite3_errmsg(db_info_s.handle));
295 return FENCE_ERR_SQLITE_FAIL;
298 return FENCE_ERR_NONE;
301 static int __geofence_manager_db_get_count_by_fence_id_and_bssid(int fence_id, char *bssid, fence_table_type_e table_type, int *count)
303 FUNC_ENTRANCE_SERVER;
304 g_return_val_if_fail(bssid, FENCE_ERR_INVALID_PARAMETER);
305 sqlite3_stmt *state = NULL;
307 const char *tail = NULL;
309 char *query = sqlite3_mprintf("SELECT COUNT(fence_id) FROM %Q where fence_id = %d AND bssid = %Q;", menu_table[table_type], fence_id, bssid);
311 ret = sqlite3_prepare_v2(db_info_s.handle, query, -1, &state, &tail);
312 if (ret != SQLITE_OK) {
313 LOGI_GEOFENCE("Error: %s", sqlite3_errmsg(db_info_s.handle));
315 return FENCE_ERR_PREPARE;
318 ret = sqlite3_step(state);
319 if (ret != SQLITE_ROW) {
320 LOGI_GEOFENCE("sqlite3_step Error[%d] : %s", ret, sqlite3_errmsg(db_info_s.handle));
321 sqlite3_finalize(state);
323 return FENCE_ERR_SQLITE_FAIL;
325 *count = sqlite3_column_int(state, 0);
326 sqlite3_reset(state);
327 sqlite3_finalize(state);
330 return FENCE_ERR_NONE;
333 static int __geofence_manager_db_insert_bssid_info(const int fence_id, char *bssid_info, const char *ssid)
335 FUNC_ENTRANCE_SERVER;
336 g_return_val_if_fail(fence_id, FENCE_ERR_INVALID_PARAMETER);
337 g_return_val_if_fail(bssid_info, FENCE_ERR_INVALID_PARAMETER);
338 sqlite3_stmt *state = NULL;
344 char *query = sqlite3_mprintf("INSERT INTO %Q(fence_id, bssid, ssid) VALUES (?, ?, ?)", menu_table[FENCE_BSSID_TABLE]);
345 LOGI_GEOFENCE("fence_id[%d], bssid[%s], ssid[%s]", fence_id, bssid_info, ssid);
347 ret = __geofence_manager_db_get_count_by_fence_id_and_bssid(fence_id, bssid_info, FENCE_BSSID_TABLE, &count);
348 if (ret != FENCE_ERR_NONE) {
349 LOGI_GEOFENCE("__geofence_manager_db_get_count_by_fence_id_and_bssid() failed. ERROR(%d)", ret);
354 LOGI_GEOFENCE("count = %d", count);
356 return FENCE_ERR_NONE;
359 ret = sqlite3_prepare_v2(db_info_s.handle, query, -1, &state, &tail);
360 if (ret != SQLITE_OK) {
361 LOGI_GEOFENCE("Error: %s", sqlite3_errmsg(db_info_s.handle));
363 return FENCE_ERR_PREPARE;
366 ret = sqlite3_bind_int(state, ++index, fence_id);
367 SQLITE3_RETURN(ret, sqlite3_errmsg(db_info_s.handle), state, query);
369 ret = sqlite3_bind_text(state, ++index, bssid_info, -1, SQLITE_STATIC);
370 SQLITE3_RETURN(ret, sqlite3_errmsg(db_info_s.handle), state, query);
372 ret = sqlite3_bind_text(state, ++index, ssid, -1, SQLITE_STATIC);
373 SQLITE3_RETURN(ret, sqlite3_errmsg(db_info_s.handle), state, query);
375 ret = sqlite3_step(state);
376 if (ret != SQLITE_DONE) {
377 LOGI_GEOFENCE("sqlite3_step Error[%d] : %s", ret, sqlite3_errmsg(db_info_s.handle));
378 sqlite3_finalize(state);
380 return FENCE_ERR_SQLITE_FAIL;
383 sqlite3_reset(state);
384 sqlite3_clear_bindings(state);
385 sqlite3_finalize(state);
387 LOGI_GEOFENCE("fence_id[%d], bssid[%s], ssid[%s] inserted db table [%s] successfully.", fence_id, bssid_info, ssid, menu_table[FENCE_BSSID_TABLE]);
389 return FENCE_ERR_NONE;
392 static int __geofence_manager_db_insert_wifi_data_info(gpointer data, gpointer user_data)
394 FUNC_ENTRANCE_SERVER;
395 g_return_val_if_fail(data, FENCE_ERR_INVALID_PARAMETER);
396 g_return_val_if_fail(user_data, FENCE_ERR_INVALID_PARAMETER);
397 int *fence_id = (int *) user_data;
398 sqlite3_stmt *state = NULL;
399 wifi_info_s *wifi_info = NULL;
405 wifi_info = (wifi_info_s *) data;
406 LOGI_GEOFENCE("fence_id[%d] bssid[%s]", *fence_id, wifi_info->bssid);
408 char *query = sqlite3_mprintf("INSERT INTO FenceGeopointWifi(fence_id, bssid) VALUES (?, ?)");
410 ret = __geofence_manager_db_get_count_by_fence_id_and_bssid(*fence_id, wifi_info->bssid, FENCE_GEOPOINT_WIFI_TABLE, &count);
412 LOGI_GEOFENCE("count = %d", count);
417 ret = sqlite3_prepare_v2(db_info_s.handle, query, -1, &state, &tail);
418 if (ret != SQLITE_OK) {
419 LOGI_GEOFENCE("Error: %s", sqlite3_errmsg(db_info_s.handle));
421 return FENCE_ERR_PREPARE;
424 ret = sqlite3_bind_int(state, ++index, *fence_id);
425 SQLITE3_RETURN(ret, sqlite3_errmsg(db_info_s.handle), state, query);
427 ret = sqlite3_bind_text(state, ++index, wifi_info->bssid, -1, SQLITE_STATIC);
428 SQLITE3_RETURN(ret, sqlite3_errmsg(db_info_s.handle), state, query);
430 ret = sqlite3_step(state);
431 if (ret != SQLITE_DONE) {
432 LOGI_GEOFENCE("sqlite3_step Error[%d] : %s", ret, sqlite3_errmsg(db_info_s.handle));
433 sqlite3_finalize(state);
435 return FENCE_ERR_SQLITE_FAIL;
438 sqlite3_reset(state);
439 sqlite3_clear_bindings(state);
440 sqlite3_finalize(state);
443 return FENCE_ERR_NONE;
446 static int __geofence_manager_delete_table(int fence_id, fence_table_type_e table_type)
448 FUNC_ENTRANCE_SERVER;
449 sqlite3_stmt *state = NULL;
452 char *query = sqlite3_mprintf("DELETE from %Q where fence_id = %d;", menu_table[table_type], fence_id);
453 LOGI_GEOFENCE("current fence id is [%d]", fence_id);
454 ret = sqlite3_prepare_v2(db_info_s.handle, query, -1, &state, NULL);
455 if (SQLITE_OK != ret) {
456 LOGI_GEOFENCE("Fail to connect to table. Error[%s]", sqlite3_errmsg(db_info_s.handle));
458 return FENCE_ERR_SQLITE_FAIL;
461 ret = sqlite3_step(state);
462 if (SQLITE_DONE != ret) {
463 LOGI_GEOFENCE("Fail to step. Error[%d]", ret);
464 sqlite3_finalize(state);
466 return FENCE_ERR_SQLITE_FAIL;
468 sqlite3_finalize(state);
469 LOGI_GEOFENCE("fence_id[%d], deleted from db table [%s] successfully.", fence_id, menu_table[table_type]);
471 return FENCE_ERR_NONE;
474 static int __geofence_manager_delete_place_table(int place_id)
476 FUNC_ENTRANCE_SERVER;
477 sqlite3_stmt *state = NULL;
480 char *query = sqlite3_mprintf("DELETE from Places where place_id = %d;", place_id);
482 LOGI_GEOFENCE("current place id is [%d]", place_id);
483 ret = sqlite3_prepare_v2(db_info_s.handle, query, -1, &state, NULL);
484 if (SQLITE_OK != ret) {
485 LOGI_GEOFENCE("Fail to connect to table. Error[%s]", sqlite3_errmsg(db_info_s.handle));
487 return FENCE_ERR_SQLITE_FAIL;
490 ret = sqlite3_step(state);
491 if (SQLITE_DONE != ret) {
492 LOGI_GEOFENCE("Fail to step. Error[%d]", ret);
493 sqlite3_finalize(state);
495 return FENCE_ERR_SQLITE_FAIL;
497 sqlite3_finalize(state);
498 LOGI_GEOFENCE("place_id[%d], deleted place from db table Places successfully.", place_id);
500 return FENCE_ERR_NONE;
503 static inline void __geofence_manager_db_create_table(void)
505 FUNC_ENTRANCE_SERVER;
509 ret = __geofence_manager_db_create_places_table();
511 rollback_transaction();
515 ret = __geofence_manager_db_create_geofence_table();
517 rollback_transaction();
521 ret = __geofence_manager_db_create_geocoordinate_table();
523 rollback_transaction();
527 ret = __geofence_manager_db_create_wifi_data_table();
529 rollback_transaction();
533 ret = __geofence_manager_db_create_bssid_table();
535 rollback_transaction();
539 commit_transaction();
542 /* Get fence id count in certain table, such as GeoFence/FenceGeocoordinate/FenceCurrentLocation */
543 static int __geofence_manager_db_get_count_of_fence_id(int fence_id, fence_table_type_e table_type, int *count)
545 sqlite3_stmt *state = NULL;
547 const char *tail = NULL;
549 char *query = sqlite3_mprintf("SELECT COUNT(fence_id) FROM %Q where fence_id = %d;", menu_table[table_type], fence_id);
551 ret = sqlite3_prepare_v2(db_info_s.handle, query, -1, &state, &tail);
552 if (ret != SQLITE_OK) {
553 LOGI_GEOFENCE("Error: %s", sqlite3_errmsg(db_info_s.handle));
555 return FENCE_ERR_PREPARE;
558 ret = sqlite3_step(state);
559 if (ret != SQLITE_ROW) {
560 LOGI_GEOFENCE("sqlite3_step Error[%d] : %s", ret, sqlite3_errmsg(db_info_s.handle));
561 sqlite3_finalize(state);
563 return FENCE_ERR_SQLITE_FAIL;
565 *count = sqlite3_column_int(state, 0);
566 sqlite3_reset(state);
567 sqlite3_finalize(state);
569 return FENCE_ERR_NONE;
572 static int __geofence_manager_db_enable_foreign_keys(void)
574 sqlite3_stmt *state = NULL;
575 int ret = FENCE_ERR_NONE;
576 char *query = sqlite3_mprintf("PRAGMA foreign_keys = ON;");
578 ret = sqlite3_prepare_v2(db_info_s.handle, query, -1, &state, NULL);
579 if (SQLITE_OK != ret) {
580 LOGI_GEOFENCE("Fail to connect to table. Error[%s]", sqlite3_errmsg(db_info_s.handle));
582 return FENCE_ERR_SQLITE_FAIL;
585 ret = sqlite3_step(state);
586 if (SQLITE_DONE != ret) {
587 LOGI_GEOFENCE("Fail to step. Error[%d]", ret);
588 sqlite3_finalize(state);
590 return FENCE_ERR_SQLITE_FAIL;
592 sqlite3_reset(state);
593 sqlite3_finalize(state);
595 return FENCE_ERR_NONE;
598 #ifdef SUPPORT_ENCRYPTION
599 void replaceChar(char *src, char oldChar, char newChar)
608 void __geofence_manager_generate_password(char *password)
610 char *bt_address = NULL;
611 char *wifi_address = NULL;
612 char *token = NULL, *save_token = NULL;
613 int bt_temp[6] = {0}, wifi_temp[6] = {0};
614 int i = 0, fkey[6], lkey[6];
615 char s1[100], s2[100], result[200];
616 char keyword[6] = { 'b', 'w', 'd', 's', 'j', 'f' };
619 ret = bt_adapter_get_address(&bt_address);
620 if (ret != BT_ERROR_NONE)
621 LOGD_GEOFENCE("bt address get fail %d", ret);
623 ret = wifi_manager_get_mac_address(&wifi_address);
624 if (ret != WIFI_MANAGER_ERROR_NONE)
625 LOGD_GEOFENCE("wifi address get fail %d", ret);
628 token = strtok_r(bt_address, ":", &save_token);
631 bt_temp[i++] = atoi(token);
632 token = strtok_r(NULL, ":", &save_token);
639 token = strtok_r(wifi_address, ":", &save_token);
642 wifi_temp[i++] = atoi(token);
643 token = strtok_r(NULL, ":", &save_token);
649 memset((void *) s1, 0, sizeof(s1));
650 memset((void *) s2, 0, sizeof(s2));
651 memset((void *) result, 0, sizeof(result));
653 for (i = 0; i < 6; i++) {
654 fkey[i] = bt_temp[i] * wifi_temp[i];
655 lkey[i] = bt_temp[i] + wifi_temp[i];
658 for (i = 0; i < 6; i++) {
659 sprintf(s1, "%s%x", s1, fkey[i]);
660 sprintf(s2, "%s%x", s2, lkey[i]);
661 replaceChar(s1, 0x30 + ((i * 2) % 10), keyword[i]);
662 replaceChar(s2, 0x30 + ((i * 2 + 1) % 10), keyword[i]);
663 LOGD_GEOFENCE("s1 %s", s1);
664 LOGD_GEOFENCE("s2 %s", s2);
667 sprintf(result, "%s%s", s1, s2);
668 LOGD_GEOFENCE("result : %s", result);
672 if (bt_address != NULL)
674 if (wifi_address != NULL)
679 static int __check_db_file()
683 fd = open(GEOFENCE_DB_FILE, O_RDONLY);
685 LOGW_GEOFENCE("DB file(%s) is not exist.", GEOFENCE_DB_FILE);
693 * This function in DB and create GeoFence/FenceGeocoordinate /FenceCurrentLocation four table on DB if necessary.
695 * @param[in] struct of fence_point_info_s
696 * @return FENCE_ERR_NONE on success, negative values for errors
698 int geofence_manager_db_init(void)
700 FUNC_ENTRANCE_SERVER;
704 if (__check_db_file()) {
705 LOGW_GEOFENCE("db(%s) file doesn't exist.", GEOFENCE_DB_FILE);
706 open_flag = SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE | SQLITE_OPEN_FULLMUTEX;
708 if (lstat(GEOFENCE_DB_FILE, &stat) < 0) {
709 LOGE_GEOFENCE("Can't get db(%s) information.", GEOFENCE_DB_FILE);
710 return FENCE_ERR_SQLITE_FAIL;
712 open_flag = SQLITE_OPEN_READWRITE | SQLITE_OPEN_FULLMUTEX;
715 if (__geofence_manager_open_db_handle(open_flag) != FENCE_ERR_NONE) {
716 LOGI_GEOFENCE("Fail to create db file(%s).", GEOFENCE_DB_FILE);
717 return FENCE_ERR_SQLITE_FAIL;
720 if (open_flag & SQLITE_OPEN_CREATE)
721 __geofence_manager_db_create_table();
723 return FENCE_ERR_NONE;
726 int geofence_manager_db_reset(void)
728 FUNC_ENTRANCE_SERVER;
729 sqlite3_stmt *state = NULL;
734 for (idx = 0; idx < 4; idx++) {
735 query = sqlite3_mprintf("DELETE from %Q;", menu_table[idx]);
737 ret = sqlite3_prepare_v2(db_info_s.handle, query, -1, &state, NULL);
738 if (SQLITE_OK != ret) {
739 LOGI_GEOFENCE("Fail to connect to table. Error[%s]", sqlite3_errmsg(db_info_s.handle));
741 return FENCE_ERR_SQLITE_FAIL;
744 ret = sqlite3_step(state);
745 if (SQLITE_DONE != ret) {
746 LOGI_GEOFENCE("Fail to step. Error[%d]", ret);
747 sqlite3_finalize(state);
749 return FENCE_ERR_SQLITE_FAIL;
751 sqlite3_finalize(state);
754 return FENCE_ERR_NONE;
757 int geofence_manager_set_place_info(place_info_s *place_info, int *place_id)
759 FUNC_ENTRANCE_SERVER;
760 g_return_val_if_fail(place_info, FENCE_ERR_INVALID_PARAMETER);
761 g_return_val_if_fail(place_id, FENCE_ERR_INVALID_PARAMETER);
762 sqlite3_stmt *state = NULL;
766 char *query = sqlite3_mprintf("INSERT INTO Places (access_type, place_name, app_id) VALUES (?, ?, ?)");
768 ret = sqlite3_prepare_v2(db_info_s.handle, query, -1, &state, &tail);
769 if (ret != SQLITE_OK) {
770 LOGI_GEOFENCE("Error: %s", sqlite3_errmsg(db_info_s.handle));
772 return FENCE_ERR_PREPARE;
774 LOGD_GEOFENCE("appid[%s] access_type[%d] place_name[%s]", place_info->appid, place_info->access_type, place_info->place_name);
776 ret = sqlite3_bind_int(state, ++index, place_info->access_type);
777 SQLITE3_RETURN(ret, sqlite3_errmsg(db_info_s.handle), state, query);
779 ret = sqlite3_bind_text(state, ++index, place_info->place_name, -1, SQLITE_STATIC);
780 SQLITE3_RETURN(ret, sqlite3_errmsg(db_info_s.handle), state, query);
782 ret = sqlite3_bind_text(state, ++index, place_info->appid, -1, SQLITE_STATIC);
783 SQLITE3_RETURN(ret, sqlite3_errmsg(db_info_s.handle), state, query);
785 ret = sqlite3_step(state);
786 if (ret != SQLITE_DONE) {
787 LOGI_GEOFENCE("sqlite3_step Error[%d] : %s", ret, sqlite3_errmsg(db_info_s.handle));
788 sqlite3_finalize(state);
790 return FENCE_ERR_SQLITE_FAIL;
792 *place_id = sqlite3_last_insert_rowid(db_info_s.handle);
793 LOGI_GEOFENCE(" auto-genarated place_id[%d]", *place_id);
794 sqlite3_reset(state);
795 sqlite3_clear_bindings(state);
796 sqlite3_finalize(state);
800 LOGI_GEOFENCE("TMP Invalid fence_id");
804 return FENCE_ERR_NONE;
807 int geofence_manager_set_common_info(fence_common_info_s *fence_info, int *fence_id)
809 FUNC_ENTRANCE_SERVER;
810 g_return_val_if_fail(fence_info, FENCE_ERR_INVALID_PARAMETER);
811 g_return_val_if_fail(fence_id, FENCE_ERR_INVALID_PARAMETER);
812 sqlite3_stmt *state = NULL;
816 char *query = sqlite3_mprintf("INSERT INTO GeoFence (place_id, enable, app_id, geofence_type, access_type, running_status) VALUES (?, ?, ?, ?, ?, ?)");
818 ret = sqlite3_prepare_v2(db_info_s.handle, query, -1, &state, &tail);
819 if (ret != SQLITE_OK) {
820 LOGI_GEOFENCE("Error: %s", sqlite3_errmsg(db_info_s.handle));
822 return FENCE_ERR_PREPARE;
825 LOGD_GEOFENCE("place_id[%d], enable[%d], appid[%s] geofence_type[%d] access_type[%d] running_status[%d]", fence_info->place_id, fence_info->enable, fence_info->appid, fence_info->type, fence_info->access_type, fence_info->running_status);
827 ret = sqlite3_bind_int(state, ++index, fence_info->place_id);
828 SQLITE3_RETURN(ret, sqlite3_errmsg(db_info_s.handle), state, query);
830 ret = sqlite3_bind_int(state, ++index, fence_info->enable);
831 SQLITE3_RETURN(ret, sqlite3_errmsg(db_info_s.handle), state, query);
833 ret = sqlite3_bind_text(state, ++index, fence_info->appid, -1, SQLITE_STATIC);
834 SQLITE3_RETURN(ret, sqlite3_errmsg(db_info_s.handle), state, query);
836 ret = sqlite3_bind_int(state, ++index, fence_info->type);
837 SQLITE3_RETURN(ret, sqlite3_errmsg(db_info_s.handle), state, query);
839 ret = sqlite3_bind_int(state, ++index, fence_info->access_type);
840 SQLITE3_RETURN(ret, sqlite3_errmsg(db_info_s.handle), state, query);
842 ret = sqlite3_bind_int(state, ++index, fence_info->running_status);
843 SQLITE3_RETURN(ret, sqlite3_errmsg(db_info_s.handle), state, query);
845 ret = sqlite3_step(state);
846 if (ret != SQLITE_DONE) {
847 LOGI_GEOFENCE("sqlite3_step Error[%d] : %s", ret, sqlite3_errmsg(db_info_s.handle));
848 sqlite3_finalize(state);
850 return FENCE_ERR_SQLITE_FAIL;
852 *fence_id = sqlite3_last_insert_rowid(db_info_s.handle);
853 LOGI_GEOFENCE(" auto-genarated fence_id[%d]", *fence_id);
854 sqlite3_reset(state);
855 sqlite3_clear_bindings(state);
856 sqlite3_finalize(state);
860 LOGI_GEOFENCE("TMP Invalid fence_id");
864 return FENCE_ERR_NONE;
867 int geofence_manager_get_place_list_from_db(int *number_of_places, GList **places)
869 FUNC_ENTRANCE_SERVER;
870 sqlite3_stmt *state = NULL;
872 const char *tail = NULL;
876 query = sqlite3_mprintf("SELECT place_id, place_name, access_type, app_id FROM Places");
878 ret = sqlite3_prepare_v2(db_info_s.handle, query, -1, &state, &tail);
879 if (ret != SQLITE_OK) {
880 LOGI_GEOFENCE("Error: %s", sqlite3_errmsg(db_info_s.handle));
882 return FENCE_ERR_PREPARE;
884 GList *place_list = NULL;
885 int column_index = 0;
887 ret = sqlite3_step(state);
889 if (ret != SQLITE_ROW) {
890 LOGI_GEOFENCE("DONE...!!! : %d", ret);
894 place_info_s *place = g_slice_new0(place_info_s);
899 place->place_id = sqlite3_column_int(state, column_index++);
900 g_strlcpy(place->place_name, (char *) sqlite3_column_text(state, column_index++), PLACE_NAME_LEN);
901 place->access_type = sqlite3_column_int(state, column_index++);
902 g_strlcpy(place->appid, (char *) sqlite3_column_text(state, column_index++), APP_ID_LEN);
903 place_list = g_list_append(place_list, place);
905 } while (ret != SQLITE_DONE);
907 *places = place_list;
908 *number_of_places = count;
910 sqlite3_reset(state);
911 sqlite3_finalize(state);
913 return FENCE_ERR_NONE;
916 int geofence_manager_get_fence_list_from_db(int *number_of_fences, GList **fences, int place_id)
918 FUNC_ENTRANCE_SERVER;
920 sqlite3_stmt *state = NULL;
922 const char *tail = NULL;
927 query = sqlite3_mprintf("SELECT DISTINCT A.fence_id, A.app_id, A.geofence_type, A.access_type, A.place_id, B.latitude, B.longitude, B.radius, B.address, C.bssid, C.ssid FROM GeoFence A LEFT JOIN FenceGeocoordinate B ON A.fence_id = B.fence_id LEFT JOIN FenceBssid C ON A.fence_id = C.fence_id GROUP BY A.fence_id");
929 query = sqlite3_mprintf("SELECT DISTINCT A.fence_id, A.app_id, A.geofence_type, A.access_type, A.place_id, B.latitude, B.longitude, B.radius, B.address, C.bssid, C.ssid FROM GeoFence A LEFT JOIN FenceGeocoordinate B ON A.fence_id = B.fence_id LEFT JOIN FenceBssid C ON A.fence_id = C.fence_id WHERE A.place_id = %d GROUP BY A.fence_id", place_id);
931 ret = sqlite3_prepare_v2(db_info_s.handle, query, -1, &state, &tail);
932 if (ret != SQLITE_OK) {
933 LOGI_GEOFENCE("Error: %s", sqlite3_errmsg(db_info_s.handle));
935 return FENCE_ERR_PREPARE;
937 GList *fence_list = NULL;
939 ret = sqlite3_step(state);
941 if (ret != SQLITE_ROW) {
942 LOGI_GEOFENCE("DONE...!!! : %d", ret);
945 int column_index = 0;
947 geofence_info_s *fence = g_slice_new0(geofence_info_s);
952 fence->fence_id = sqlite3_column_int(state, column_index++);
953 g_strlcpy(fence->app_id, (char *) sqlite3_column_text(state, column_index++), APP_ID_LEN);
954 fence->param.type = sqlite3_column_int(state, column_index++);
955 fence->access_type = sqlite3_column_int(state, column_index++);
956 fence->param.place_id = sqlite3_column_int(state, column_index++);
957 char *data_name = NULL;
959 data_name = (char *) sqlite3_column_text(state, column_index++);
960 if (!data_name || !strlen(data_name))
961 LOGI_GEOFENCE("ERROR: data_name is NULL!!!");
963 fence->param.latitude = atof(data_name);
965 data_name = (char *) sqlite3_column_text(state, column_index++);
966 if (!data_name || !strlen(data_name))
967 LOGI_GEOFENCE("ERROR: data_name is NULL!!!");
969 fence->param.longitude = atof(data_name);
971 data_name = (char *) sqlite3_column_text(state, column_index++);
972 if (!data_name || !strlen(data_name))
973 LOGI_GEOFENCE("ERROR: data_name is NULL!!!");
975 fence->param.radius = atof(data_name);
977 g_strlcpy(fence->param.address, (char *) sqlite3_column_text(state, column_index++), ADDRESS_LEN);
978 g_strlcpy(fence->param.bssid, (char *) sqlite3_column_text(state, column_index++), WLAN_BSSID_LEN);
979 g_strlcpy(fence->param.ssid, (char *) sqlite3_column_text(state, column_index++), WLAN_BSSID_LEN);
980 LOGI_GEOFENCE("radius = %d, bssid = %s", fence->param.radius, fence->param.bssid);
981 fence_list = g_list_append(fence_list, fence);
983 } while (ret != SQLITE_DONE);
985 *fences = fence_list;
986 *number_of_fences = count;
988 sqlite3_reset(state);
989 sqlite3_finalize(state);
991 return FENCE_ERR_NONE;
994 int geofence_manager_get_fenceid_list_from_db(int *number_of_fences, GList **fences, int place_id)
996 FUNC_ENTRANCE_SERVER;
997 sqlite3_stmt *state = NULL;
999 const char *tail = NULL;
1002 query = sqlite3_mprintf("SELECT fence_id FROM GeoFence WHERE place_id = %d", place_id);
1004 ret = sqlite3_prepare_v2(db_info_s.handle, query, -1, &state, &tail);
1005 if (ret != SQLITE_OK) {
1006 LOGI_GEOFENCE("Error: %s", sqlite3_errmsg(db_info_s.handle));
1007 sqlite3_free(query);
1008 return FENCE_ERR_PREPARE;
1010 GList *fence_list = NULL;
1011 int column_index = 0;
1014 ret = sqlite3_step(state);
1015 if (ret != SQLITE_ROW) {
1016 LOGI_GEOFENCE("DONE...!!! : %d", ret);
1020 fence_id = sqlite3_column_int(state, column_index);
1021 fence_list = g_list_append(fence_list, GINT_TO_POINTER(fence_id));
1023 } while (ret != SQLITE_DONE);
1024 *fences = fence_list;
1025 *number_of_fences = count;
1027 sqlite3_reset(state);
1028 sqlite3_finalize(state);
1029 sqlite3_free(query);
1030 return FENCE_ERR_NONE;
1033 int geofence_manager_update_geocoordinate_info(int fence_id, geocoordinate_info_s *geocoordinate_info)
1035 FUNC_ENTRANCE_SERVER;
1036 g_return_val_if_fail(fence_id, FENCE_ERR_INVALID_PARAMETER);
1037 g_return_val_if_fail(geocoordinate_info, FENCE_ERR_INVALID_PARAMETER);
1038 sqlite3_stmt *state = NULL;
1040 int ret = SQLITE_OK;
1042 char *query = sqlite3_mprintf("UPDATE FenceGeocoordinate SET latitude = %lf, longitude = %lf, radius = %lf where fence_id = %d;", geocoordinate_info->latitude, geocoordinate_info->longitude, geocoordinate_info->radius, fence_id);
1044 ret = sqlite3_prepare_v2(db_info_s.handle, query, -1, &state, &tail);
1045 if (ret != SQLITE_OK) {
1046 LOGI_GEOFENCE("Error: %s", sqlite3_errmsg(db_info_s.handle));
1047 sqlite3_free(query);
1048 return FENCE_ERR_PREPARE;
1051 ret = sqlite3_step(state);
1052 if (ret != SQLITE_DONE) {
1053 LOGI_GEOFENCE("sqlite3_step Error[%d] : %s", ret, sqlite3_errmsg(db_info_s.handle));
1054 sqlite3_finalize(state);
1055 sqlite3_free(query);
1056 return FENCE_ERR_SQLITE_FAIL;
1059 sqlite3_finalize(state);
1060 sqlite3_free(query);
1061 LOGI_GEOFENCE("fence_id: %d has been successfully updated.", fence_id);
1062 return FENCE_ERR_NONE;
1065 int geofence_manager_update_place_info(int place_id, const char *place_info_name)
1067 FUNC_ENTRANCE_SERVER;
1068 g_return_val_if_fail(place_id, FENCE_ERR_INVALID_PARAMETER);
1069 sqlite3_stmt *state = NULL;
1071 int ret = SQLITE_OK;
1072 char *query = sqlite3_mprintf("UPDATE Places SET place_name = %Q where place_id = %d", place_info_name, place_id);
1074 ret = sqlite3_prepare_v2(db_info_s.handle, query, -1, &state, &tail);
1075 if (ret != SQLITE_OK) {
1076 LOGI_GEOFENCE("Error: %s", sqlite3_errmsg(db_info_s.handle));
1077 sqlite3_free(query);
1078 return FENCE_ERR_PREPARE;
1081 ret = sqlite3_step(state);
1082 if (ret != SQLITE_DONE) {
1083 LOGI_GEOFENCE("sqlite3_step Error[%d] : %s", ret, sqlite3_errmsg(db_info_s.handle));
1084 sqlite3_finalize(state);
1085 sqlite3_free(query);
1086 return FENCE_ERR_SQLITE_FAIL;
1089 sqlite3_finalize(state);
1090 sqlite3_free(query);
1091 LOGI_GEOFENCE("place_id: %d has been successfully updated.", place_id);
1092 return FENCE_ERR_NONE;
1096 * This function set geocoordinate info in DB.
1098 * @param[in] fence_id
1099 * @param[out] struct of geocoordinate_info_s
1100 * @return FENCE_ERR_NONE on success, negative values for errors
1102 int geofence_manager_set_geocoordinate_info(int fence_id, geocoordinate_info_s *geocoordinate_info)
1104 FUNC_ENTRANCE_SERVER;
1105 g_return_val_if_fail(fence_id, FENCE_ERR_INVALID_PARAMETER);
1106 g_return_val_if_fail(geocoordinate_info, FENCE_ERR_INVALID_PARAMETER);
1107 sqlite3_stmt *state = NULL;
1108 int ret = SQLITE_OK;
1112 char data_name_lat[MAX_DATA_NAME] = { 0 };
1113 char data_name_lon[MAX_DATA_NAME] = { 0 };
1114 char data_name_rad[MAX_DATA_NAME] = { 0 };
1115 char *query = sqlite3_mprintf("INSERT INTO FenceGeocoordinate(fence_id, latitude, longitude, radius, address) VALUES (?, ?, ?, ?, ?)");
1117 ret = __geofence_manager_db_get_count_of_fence_id(fence_id, FENCE_GEOCOORDINATE_TAB, &count);
1118 if (ret != FENCE_ERR_NONE) {
1119 LOGI_GEOFENCE("Fail to get geofence_manager_db_get_count_of_fence_id [%d]", ret);
1120 sqlite3_free(query);
1122 } else if (count) { /* fence id has been in FenceGeocoordinate table */
1123 sqlite3_free(query);
1124 return FENCE_ERR_FENCE_ID;
1127 ret = sqlite3_prepare_v2(db_info_s.handle, query, -1, &state, &tail);
1128 if (ret != SQLITE_OK) {
1129 LOGI_GEOFENCE("Error: %s", sqlite3_errmsg(db_info_s.handle));
1130 sqlite3_free(query);
1131 return FENCE_ERR_PREPARE;
1134 ret = sqlite3_bind_int(state, ++index, fence_id);
1135 SQLITE3_RETURN(ret, sqlite3_errmsg(db_info_s.handle), state, query);
1137 #ifdef SUPPORT_ENCRYPTION
1138 if (password == NULL)
1139 __geofence_manager_generate_password(password);
1142 ret = snprintf(data_name_lat, MAX_DATA_NAME, "%lf", geocoordinate_info->latitude);
1144 ret = sqlite3_bind_text(state, ++index, data_name_lat, -1, SQLITE_STATIC);
1146 /*ret = sqlite3_bind_double (state, ++index, geocoordinate_info->latitude);*/
1147 SQLITE3_RETURN(ret, sqlite3_errmsg(db_info_s.handle), state, query);
1149 ret = snprintf(data_name_lon, MAX_DATA_NAME, "%lf", geocoordinate_info->longitude);
1151 LOGD_GEOFENCE("ERROR: String will be truncated");
1152 sqlite3_free(query);
1153 sqlite3_finalize(state);
1154 return FENCE_ERR_STRING_TRUNCATED;
1157 ret = sqlite3_bind_text(state, ++index, data_name_lon, -1, SQLITE_STATIC);
1158 /*ret = sqlite3_bind_double (state, ++index, geocoordinate_info->longitude);*/
1159 SQLITE3_RETURN(ret, sqlite3_errmsg(db_info_s.handle), state, query);
1161 ret = snprintf(data_name_rad, MAX_DATA_NAME, "%lf", geocoordinate_info->radius);
1163 LOGD_GEOFENCE("ERROR: String will be truncated");
1164 sqlite3_free(query);
1165 sqlite3_finalize(state);
1166 return FENCE_ERR_STRING_TRUNCATED;
1169 ret = sqlite3_bind_text(state, ++index, data_name_rad, -1, SQLITE_STATIC);
1170 /*ret = sqlite3_bind_double (state, ++index, geocoordinate_info->radius);*/
1171 SQLITE3_RETURN(ret, sqlite3_errmsg(db_info_s.handle), state, query);
1173 ret = sqlite3_bind_text(state, ++index, geocoordinate_info->address, -1, SQLITE_STATIC);
1174 SQLITE3_RETURN(ret, sqlite3_errmsg(db_info_s.handle), state, query);
1176 ret = sqlite3_step(state);
1177 if (ret != SQLITE_DONE) {
1178 LOGI_GEOFENCE("sqlite3_step Error[%d] : %s", ret, sqlite3_errmsg(db_info_s.handle));
1179 sqlite3_free(query);
1180 sqlite3_finalize(state);
1181 return FENCE_ERR_SQLITE_FAIL;
1184 sqlite3_reset(state);
1185 sqlite3_clear_bindings(state);
1186 sqlite3_finalize(state);
1187 sqlite3_free(query);
1189 return FENCE_ERR_NONE;
1193 * This function get geocoordinate info from DB.
1195 * @param[in] fence_id
1196 * @param[out] struct of geocoordinate_info_s
1197 * @return FENCE_ERR_NONE on success, negative values for errors
1199 int geofence_manager_get_geocoordinate_info(int fence_id, geocoordinate_info_s **geocoordinate_info)
1201 FUNC_ENTRANCE_SERVER;
1202 g_return_val_if_fail(fence_id, FENCE_ERR_INVALID_PARAMETER);
1203 sqlite3_stmt *state = NULL;
1204 int ret = SQLITE_OK;
1205 const char *tail = NULL;
1207 char *data_name = NULL;
1208 char *query = sqlite3_mprintf("SELECT * FROM FenceGeocoordinate where fence_id = %d;", fence_id);
1210 LOGD_GEOFENCE("current fence id is [%d]", fence_id);
1211 ret = sqlite3_prepare_v2(db_info_s.handle, query, -1, &state, &tail);
1212 if (ret != SQLITE_OK) {
1213 LOGI_GEOFENCE("Error: %s", sqlite3_errmsg(db_info_s.handle));
1214 sqlite3_free(query);
1215 return FENCE_ERR_PREPARE;
1218 ret = sqlite3_step(state);
1219 if (ret != SQLITE_ROW) {
1220 LOGI_GEOFENCE("sqlite3_step Error[%d] : %s", ret, sqlite3_errmsg(db_info_s.handle));
1221 sqlite3_finalize(state);
1222 sqlite3_free(query);
1223 return FENCE_ERR_SQLITE_FAIL;
1226 *geocoordinate_info = (geocoordinate_info_s *)g_malloc0(sizeof(geocoordinate_info_s));
1227 if (*geocoordinate_info == NULL) {
1228 sqlite3_finalize(state);
1229 sqlite3_free(query);
1231 g_return_val_if_fail(*geocoordinate_info, FENCE_ERR_INVALID_PARAMETER);
1233 #ifdef SUPPORT_ENCRYPTION
1234 if (password == NULL)
1235 __geofence_manager_generate_password(password);
1238 data_name = (char *) sqlite3_column_text(state, ++index);
1240 if (!data_name || !strlen(data_name))
1241 LOGI_GEOFENCE("ERROR: data_name is NULL!!!");
1243 (*geocoordinate_info)->latitude = atof(data_name);
1245 data_name = (char *) sqlite3_column_text(state, ++index);
1246 if (!data_name || !strlen(data_name))
1247 LOGI_GEOFENCE("ERROR: data_name is NULL!!!");
1249 (*geocoordinate_info)->longitude = atof(data_name);
1251 data_name = (char *) sqlite3_column_text(state, ++index);
1252 if (!data_name || !strlen(data_name))
1253 LOGI_GEOFENCE("ERROR: data_name is NULL!!!");
1255 (*geocoordinate_info)->radius = atof(data_name);
1257 g_strlcpy((*geocoordinate_info)->address, (char *) sqlite3_column_text(state, ++index), ADDRESS_LEN);
1259 sqlite3_finalize(state);
1260 sqlite3_free(query);
1262 return FENCE_ERR_NONE;
1266 * This function get ap list from DB.
1268 * @param[in] fence_id
1269 * @param[out] ap_list
1270 * @return FENCE_ERR_NONE on success, negative values for errors
1272 int geofence_manager_get_ap_info(const int fence_id, GList **ap_list)
1274 FUNC_ENTRANCE_SERVER;
1275 sqlite3_stmt *state = NULL;
1276 int ret = SQLITE_OK;
1277 const char *tail = NULL;
1280 wifi_info_s *wifi_info = NULL;
1281 const char *bssid = NULL;
1283 char *query1 = sqlite3_mprintf("SELECT COUNT(bssid) FROM FenceGeopointWifi where fence_id = %d;", fence_id);
1285 LOGD_GEOFENCE("current fence id is [%d]", fence_id);
1286 ret = sqlite3_prepare_v2(db_info_s.handle, query1, -1, &state, &tail);
1287 if (ret != SQLITE_OK) {
1288 LOGI_GEOFENCE("Error: %s", sqlite3_errmsg(db_info_s.handle));
1289 sqlite3_free(query1);
1290 return FENCE_ERR_PREPARE;
1293 ret = sqlite3_step(state);
1294 if (ret != SQLITE_ROW) {
1295 LOGD_GEOFENCE("Fail to get count sqlite3_step");
1296 sqlite3_finalize(state);
1297 sqlite3_free(query1);
1298 return FENCE_ERR_SQLITE_FAIL;
1301 count = sqlite3_column_int(state, 0);
1302 sqlite3_reset(state);
1303 sqlite3_finalize(state);
1304 sqlite3_free(query1);
1306 LOGI_GEOFENCE("ERROR: count = %d", count);
1307 return FENCE_ERR_COUNT;
1309 LOGD_GEOFENCE("count[%d]", count);
1312 char *query2 = sqlite3_mprintf("SELECT * FROM FenceGeopointWifi where fence_id = %d;", fence_id);
1314 ret = sqlite3_prepare_v2(db_info_s.handle, query2, -1, &state, &tail);
1315 if (ret != SQLITE_OK) {
1316 LOGI_GEOFENCE("Error: %s", sqlite3_errmsg(db_info_s.handle));
1317 sqlite3_free(query2);
1318 return FENCE_ERR_PREPARE;
1321 for (i = 0; i < count; i++) {
1322 ret = sqlite3_step(state);
1323 if (ret != SQLITE_ROW) {
1324 LOGI_GEOFENCE("sqlite3_step Error[%d] : %s", ret, sqlite3_errmsg(db_info_s.handle));
1327 wifi_info = g_slice_new0(wifi_info_s);
1328 if (wifi_info == NULL) {
1329 sqlite3_free(query2);
1330 sqlite3_finalize(state);
1332 g_return_val_if_fail(wifi_info, -1);
1334 bssid = (const char *) sqlite3_column_text(state, 1);
1335 g_strlcpy(wifi_info->bssid, bssid, WLAN_BSSID_LEN);
1336 *ap_list = g_list_append(*ap_list, (gpointer) wifi_info);
1340 sqlite3_finalize(state);
1341 sqlite3_free(query2);
1342 return FENCE_ERR_NONE;
1345 /*This function get place info from DB.
1347 * @param[in] place_id
1348 * @param[out] struct of place_info_s
1349 * @return FENCE_ERR_NONE on success, negative values for errors
1351 int geofence_manager_get_place_info(int place_id, place_info_s **place_info)
1353 FUNC_ENTRANCE_SERVER;
1354 g_return_val_if_fail(place_id, FENCE_ERR_INVALID_PARAMETER);
1355 sqlite3_stmt *state = NULL;
1356 int ret = SQLITE_OK;
1357 const char *tail = NULL;
1359 char *data_name = NULL;
1360 char *query = sqlite3_mprintf("SELECT * FROM Places where place_id = %d;", place_id);
1362 LOGD_GEOFENCE("current place id is [%d]", place_id);
1363 ret = sqlite3_prepare_v2(db_info_s.handle, query, -1, &state, &tail);
1364 if (ret != SQLITE_OK) {
1365 LOGI_GEOFENCE("Error: %s", sqlite3_errmsg(db_info_s.handle));
1366 sqlite3_free(query);
1367 return FENCE_ERR_PREPARE;
1369 ret = sqlite3_step(state);
1370 if (ret != SQLITE_ROW) {
1371 LOGI_GEOFENCE("sqlite3_step Error[%d] : %s", ret, sqlite3_errmsg(db_info_s.handle));
1372 sqlite3_finalize(state);
1373 sqlite3_free(query);
1374 return FENCE_ERR_SQLITE_FAIL;
1376 *place_info = (place_info_s *)g_malloc0(sizeof(place_info_s));
1377 if (*place_info == NULL) {
1378 sqlite3_free(query);
1379 sqlite3_finalize(state);
1381 g_return_val_if_fail(*place_info, FENCE_ERR_INTERNAL);
1383 data_name = (char *)sqlite3_column_text(state, ++index);
1384 if (!data_name || !strlen(data_name))
1385 LOGI_GEOFENCE("ERROR: data_name is NULL!!!");
1387 (*place_info)->access_type = atof(data_name);
1389 g_strlcpy((*place_info)->place_name, (char *)sqlite3_column_text(state, ++index), PLACE_NAME_LEN);
1390 g_strlcpy((*place_info)->appid, (char *)sqlite3_column_text(state, ++index), APP_ID_LEN);
1391 sqlite3_finalize(state);
1392 sqlite3_free(query);
1394 return FENCE_ERR_NONE;
1398 * This function insert ap list in DB.
1400 * @param[in] fence_id
1401 * @param[out] ap_list
1402 * @return FENCE_ERR_NONE on success, negative values for errors
1404 int geofence_manager_set_ap_info(int fence_id, GList *ap_list)
1406 FUNC_ENTRANCE_SERVER;
1407 g_return_val_if_fail(fence_id, FENCE_ERR_INVALID_PARAMETER);
1408 g_return_val_if_fail(ap_list, FENCE_ERR_INVALID_PARAMETER);
1409 int ret = FENCE_ERR_NONE;
1412 ret = __geofence_manager_db_get_count_of_fence_id(fence_id, FENCE_GEOPOINT_WIFI_TABLE, &count);
1413 if (ret != FENCE_ERR_NONE) {
1414 LOGI_GEOFENCE("Fail to get geofence_manager_db_get_count_of_fence_id [%d]", ret);
1417 if (count) { /* fence id has been in FenceCurrentLocation table */
1418 LOGI_GEOFENCE("count is [%d]", count);
1419 return FENCE_ERR_FENCE_ID;
1423 g_list_foreach(ap_list, (GFunc) __geofence_manager_db_insert_wifi_data_info, &fence_id);
1425 return FENCE_ERR_NONE;
1429 * This function get bluetooth info from DB.
1431 * @param[in] fence_id
1432 * @param[out] bt_info which contained bssid of bluetooth and correspond of fence_id.
1433 * @return FENCE_ERR_NONE on success, negative values for errors
1435 int geofence_manager_get_bssid_info(const int fence_id, bssid_info_s **bssid_info)
1437 FUNC_ENTRANCE_SERVER;
1438 sqlite3_stmt *state = NULL;
1439 int ret = SQLITE_OK;
1440 const char *tail = NULL;
1443 bssid_info_s *bssid_info_from_db = NULL;
1444 const char *bssid = NULL;
1445 const char *ssid = NULL;
1447 char *query1 = sqlite3_mprintf("SELECT COUNT(bssid) FROM %s where fence_id = %d;", menu_table[FENCE_BSSID_TABLE], fence_id);
1449 LOGD_GEOFENCE("current fence id is [%d]", fence_id);
1450 ret = sqlite3_prepare_v2(db_info_s.handle, query1, -1, &state, &tail);
1451 if (ret != SQLITE_OK) {
1452 LOGI_GEOFENCE("Error: %s", sqlite3_errmsg(db_info_s.handle));
1453 sqlite3_free(query1);
1454 return FENCE_ERR_PREPARE;
1457 ret = sqlite3_step(state);
1458 if (ret != SQLITE_ROW) {
1459 LOGD_GEOFENCE("Fail to get count sqlite3_step");
1460 sqlite3_finalize(state);
1461 sqlite3_free(query1);
1462 return FENCE_ERR_SQLITE_FAIL;
1465 count = sqlite3_column_int(state, 0);
1466 sqlite3_reset(state);
1467 sqlite3_finalize(state);
1468 sqlite3_free(query1);
1470 LOGI_GEOFENCE("ERROR: count = %d", count);
1471 return FENCE_ERR_COUNT;
1473 LOGD_GEOFENCE("count[%d]", count);
1476 char *query2 = sqlite3_mprintf("SELECT * FROM %s where fence_id = %d;", menu_table[FENCE_BSSID_TABLE], fence_id);
1478 ret = sqlite3_prepare_v2(db_info_s.handle, query2, -1, &state, &tail);
1479 if (ret != SQLITE_OK) {
1480 LOGI_GEOFENCE("Error: %s", sqlite3_errmsg(db_info_s.handle));
1481 sqlite3_free(query2);
1482 return FENCE_ERR_PREPARE;
1485 /*'count' should be 1. because bluetooth bssid and fence_id matched one by one.*/
1486 for (i = 0; i < count; i++) {
1487 ret = sqlite3_step(state);
1488 if (ret != SQLITE_ROW) {
1489 LOGI_GEOFENCE("sqlite3_step Error[%d] : %s", ret, sqlite3_errmsg(db_info_s.handle));
1492 bssid_info_from_db = g_slice_new0(bssid_info_s);
1493 if (bssid_info_from_db == NULL) {
1494 sqlite3_free(query2);
1495 sqlite3_finalize(state);
1497 g_return_val_if_fail(bssid_info_from_db, -1);
1498 if (bssid_info_from_db) {
1499 bssid = (const char *)sqlite3_column_text(state, 1);
1500 ssid = (const char *)sqlite3_column_text(state, 2);
1501 g_strlcpy(bssid_info_from_db->bssid, bssid, WLAN_BSSID_LEN);
1502 g_strlcpy(bssid_info_from_db->ssid, ssid, WLAN_BSSID_LEN);
1503 *bssid_info = bssid_info_from_db;
1507 sqlite3_finalize(state);
1508 sqlite3_free(query2);
1509 return FENCE_ERR_NONE;
1512 int geofence_manager_update_bssid_info(const int fence_id, bssid_info_s *bssid_info)
1514 FUNC_ENTRANCE_SERVER
1515 g_return_val_if_fail(fence_id, FENCE_ERR_INVALID_PARAMETER);
1516 g_return_val_if_fail(bssid_info, FENCE_ERR_INVALID_PARAMETER);
1517 sqlite3_stmt *state = NULL;
1518 int ret = SQLITE_OK;
1520 char *query = sqlite3_mprintf("UPDATE %Q SET bssid = %Q where fence_id = %d;", menu_table[FENCE_BSSID_TABLE], bssid_info->bssid, fence_id);
1522 ret = sqlite3_prepare_v2(db_info_s.handle, query, -1, &state, &tail);
1523 if (ret != SQLITE_OK) {
1524 LOGI_GEOFENCE("Error: %s", sqlite3_errmsg(db_info_s.handle));
1525 sqlite3_free(query);
1526 return FENCE_ERR_PREPARE;
1529 ret = sqlite3_step(state);
1530 if (ret != SQLITE_DONE) {
1531 LOGI_GEOFENCE("sqlite3_step Error[%d] : %s", ret, sqlite3_errmsg(db_info_s.handle));
1532 sqlite3_finalize(state);
1533 sqlite3_free(query);
1534 return FENCE_ERR_SQLITE_FAIL;
1537 sqlite3_finalize(state);
1538 sqlite3_free(query);
1539 LOGI_GEOFENCE("Fence_id: %d has been successfully updated.", fence_id);
1540 return FENCE_ERR_NONE;
1544 * This function insert bssid information in DB.
1546 * @param[in] fence_id
1547 * @param[in] bssid_info which contained bssid of wifi or bluetooth for geofence.
1548 * @return FENCE_ERR_NONE on success, negative values for errors
1550 int geofence_manager_set_bssid_info(int fence_id, bssid_info_s *bssid_info)
1552 FUNC_ENTRANCE_SERVER
1553 g_return_val_if_fail(fence_id, FENCE_ERR_INVALID_PARAMETER);
1554 g_return_val_if_fail(bssid_info, FENCE_ERR_INVALID_PARAMETER);
1555 int ret = FENCE_ERR_NONE;
1558 ret = __geofence_manager_db_get_count_of_fence_id(fence_id, FENCE_BSSID_TABLE, &count);
1559 if (ret != FENCE_ERR_NONE) {
1560 LOGI_GEOFENCE("Fail to get geofence_manager_db_get_count_of_fence_id [%d]", ret);
1563 if (count) { /* fence id has been in FenceBssid table */
1564 LOGI_GEOFENCE("count is [%d]", count);
1565 return FENCE_ERR_FENCE_ID;
1569 ret = __geofence_manager_db_insert_bssid_info(fence_id, bssid_info->bssid, bssid_info->ssid);
1570 if (ret != FENCE_ERR_NONE) {
1571 LOGI_GEOFENCE("Fail to insert the bssid info");
1574 return FENCE_ERR_NONE;
1578 * This function get enable status from DB.
1580 * @param[in] fence_id
1581 * @param[in] status: 1 enbale, 0 disable.
1582 * @return FENCE_ERR_NONE on success, negative values for errors
1584 int geofence_manager_get_enable_status(const int fence_id, int *status)
1586 FUNC_ENTRANCE_SERVER;
1587 sqlite3_stmt *state = NULL;
1588 int ret = SQLITE_OK;
1589 const char *tail = NULL;
1591 char *query = sqlite3_mprintf("SELECT enable FROM GeoFence where fence_id = %d;", fence_id);
1593 LOGD_GEOFENCE("current fence id is [%d]", fence_id);
1594 ret = sqlite3_prepare_v2(db_info_s.handle, query, -1, &state, &tail);
1595 if (ret != SQLITE_OK) {
1596 LOGI_GEOFENCE("Error: %s", sqlite3_errmsg(db_info_s.handle));
1597 sqlite3_free(query);
1598 return FENCE_ERR_PREPARE;
1601 ret = sqlite3_step(state);
1602 if (ret != SQLITE_ROW) {
1603 LOGI_GEOFENCE("sqlite3_step Error[%d] : %s", ret, sqlite3_errmsg(db_info_s.handle));
1604 sqlite3_finalize(state);
1605 sqlite3_free(query);
1606 return FENCE_ERR_SQLITE_FAIL;
1609 *status = sqlite3_column_int(state, 0);
1611 sqlite3_finalize(state);
1612 sqlite3_free(query);
1613 return FENCE_ERR_NONE;
1617 * This function set enable on DB.
1619 * @param[in] fence_id
1620 * @param[in] status: 1 enbale, 0 disable.
1621 * @return FENCE_ERR_NONE on success, negative values for errors
1623 int geofence_manager_set_enable_status(int fence_id, int status)
1625 FUNC_ENTRANCE_SERVER;
1626 sqlite3_stmt *state;
1627 int ret = SQLITE_OK;
1630 char *query = sqlite3_mprintf("UPDATE GeoFence SET enable = %d where fence_id = %d;", status, fence_id);
1632 ret = sqlite3_prepare_v2(db_info_s.handle, query, -1, &state, &tail);
1633 if (ret != SQLITE_OK) {
1634 LOGI_GEOFENCE("Error: %s", sqlite3_errmsg(db_info_s.handle));
1635 sqlite3_free(query);
1636 return FENCE_ERR_PREPARE;
1639 ret = sqlite3_step(state);
1640 if (ret != SQLITE_DONE) {
1641 LOGI_GEOFENCE("sqlite3_step Error[%d] : %s", ret, sqlite3_errmsg(db_info_s.handle));
1642 sqlite3_finalize(state);
1643 sqlite3_free(query);
1644 return FENCE_ERR_SQLITE_FAIL;
1647 sqlite3_finalize(state);
1648 sqlite3_free(query);
1649 return FENCE_ERR_NONE;
1653 * This function get name from DB.
1655 * @param[in] fence_id
1657 * @return FENCE_ERR_NONE on success, negative values for errors
1659 int geofence_manager_get_place_name(int place_id, char **name)
1661 FUNC_ENTRANCE_SERVER;
1662 sqlite3_stmt *state = NULL;
1663 int ret = SQLITE_OK;
1664 const char *tail = NULL;
1667 char *query = sqlite3_mprintf("SELECT place_name FROM Places where place_id = %d;", place_id);
1669 LOGD_GEOFENCE("current place id is [%d]", place_id);
1670 ret = sqlite3_prepare_v2(db_info_s.handle, query, -1, &state, &tail);
1671 if (ret != SQLITE_OK) {
1672 LOGI_GEOFENCE("Error: %s", sqlite3_errmsg(db_info_s.handle));
1673 sqlite3_free(query);
1674 return FENCE_ERR_PREPARE;
1677 ret = sqlite3_step(state);
1678 if (ret != SQLITE_ROW) {
1679 LOGI_GEOFENCE("sqlite3_step Error[%d] : %s", ret, sqlite3_errmsg(db_info_s.handle));
1680 sqlite3_finalize(state);
1681 sqlite3_free(query);
1682 return FENCE_ERR_SQLITE_FAIL;
1685 tmp = (char *) sqlite3_column_text(state, 0);
1686 if (!tmp || !strlen(tmp))
1687 LOGI_GEOFENCE("ERROR: name is NULL!!!");
1689 *name = g_strdup(tmp);
1691 sqlite3_finalize(state);
1692 sqlite3_free(query);
1693 return FENCE_ERR_NONE;
1697 * This function set name on DB.
1699 * @param[in] fence_id
1701 * @return FENCE_ERR_NONE on success, negative values for errors
1703 int geofence_manager_set_place_name(int place_id, const char *name)
1705 FUNC_ENTRANCE_SERVER;
1706 sqlite3_stmt *state;
1707 int ret = SQLITE_OK;
1710 char *query = sqlite3_mprintf("UPDATE Places SET place_name = %Q where place_id = %d;", name, place_id);
1712 ret = sqlite3_prepare_v2(db_info_s.handle, query, -1, &state, &tail);
1713 if (ret != SQLITE_OK) {
1714 LOGI_GEOFENCE("Error: %s", sqlite3_errmsg(db_info_s.handle));
1715 sqlite3_free(query);
1716 return FENCE_ERR_PREPARE;
1719 ret = sqlite3_step(state);
1720 if (ret != SQLITE_DONE) {
1721 LOGI_GEOFENCE("sqlite3_step Error[%d] : %s", ret, sqlite3_errmsg(db_info_s.handle));
1722 sqlite3_finalize(state);
1723 sqlite3_free(query);
1724 return FENCE_ERR_SQLITE_FAIL;
1727 sqlite3_finalize(state);
1728 sqlite3_free(query);
1729 return FENCE_ERR_NONE;
1733 * This function get appid from DB.
1735 * @param[in] place_id
1737 * @return FENCE_ERR_NONE on success, negative values for errors
1739 int geofence_manager_get_appid_from_places(int place_id, char **appid)
1741 FUNC_ENTRANCE_SERVER;
1742 sqlite3_stmt *state = NULL;
1743 int ret = SQLITE_OK;
1744 const char *tail = NULL;
1747 char *query = sqlite3_mprintf("SELECT app_id FROM Places where place_id = %d;", place_id);
1749 LOGD_GEOFENCE("current place id is [%d]", place_id);
1750 ret = sqlite3_prepare_v2(db_info_s.handle, query, -1, &state, &tail);
1751 if (ret != SQLITE_OK) {
1752 LOGI_GEOFENCE("Error: %s", sqlite3_errmsg(db_info_s.handle));
1753 sqlite3_free(query);
1754 return FENCE_ERR_PREPARE;
1757 ret = sqlite3_step(state);
1758 if (ret != SQLITE_ROW) {
1759 LOGI_GEOFENCE("sqlite3_step Error[%d] : %s", ret, sqlite3_errmsg(db_info_s.handle));
1760 sqlite3_finalize(state);
1761 sqlite3_free(query);
1762 return FENCE_ERR_SQLITE_FAIL;
1765 id = (char *) sqlite3_column_text(state, 0);
1766 if (!id || !strlen(id))
1767 LOGI_GEOFENCE("ERROR: appid is NULL!!!");
1769 *appid = g_strdup(id);
1771 sqlite3_finalize(state);
1772 sqlite3_free(query);
1773 return FENCE_ERR_NONE;
1777 * This function set appid on DB.
1779 * @param[in] place_id
1781 * @return FENCE_ERR_NONE on success, negative values for errors
1783 int geofence_manager_set_appid_to_places(int place_id, char *appid)
1785 FUNC_ENTRANCE_SERVER;
1786 sqlite3_stmt *state;
1787 int ret = SQLITE_OK;
1790 char *query = sqlite3_mprintf("UPDATE Places SET app_id = %Q where place_id = %d;", appid, place_id);
1791 ret = sqlite3_prepare_v2(db_info_s.handle, query, -1, &state, &tail);
1792 if (ret != SQLITE_OK) {
1793 LOGI_GEOFENCE("Error: %s", sqlite3_errmsg(db_info_s.handle));
1794 sqlite3_free(query);
1795 return FENCE_ERR_PREPARE;
1798 ret = sqlite3_step(state);
1799 if (ret != SQLITE_DONE) {
1800 LOGI_GEOFENCE("sqlite3_step Error[%d] : %s", ret, sqlite3_errmsg(db_info_s.handle));
1801 sqlite3_finalize(state);
1802 sqlite3_free(query);
1803 return FENCE_ERR_SQLITE_FAIL;
1806 sqlite3_finalize(state);
1807 sqlite3_free(query);
1808 return FENCE_ERR_NONE;
1812 * This function get appid from DB.
1814 * @param[in] fence_id
1816 * @return FENCE_ERR_NONE on success, negative values for errors
1818 int geofence_manager_get_appid_from_geofence(int fence_id, char **appid)
1820 FUNC_ENTRANCE_SERVER;
1821 sqlite3_stmt *state = NULL;
1822 int ret = SQLITE_OK;
1823 const char *tail = NULL;
1826 char *query = sqlite3_mprintf("SELECT app_id FROM GeoFence where fence_id = %d;", fence_id);
1827 LOGD_GEOFENCE("current fence id is [%d]", fence_id);
1828 ret = sqlite3_prepare_v2(db_info_s.handle, query, -1, &state, &tail);
1829 if (ret != SQLITE_OK) {
1830 LOGI_GEOFENCE("Error: %s", sqlite3_errmsg(db_info_s.handle));
1831 sqlite3_free(query);
1832 return FENCE_ERR_PREPARE;
1835 ret = sqlite3_step(state);
1836 if (ret != SQLITE_ROW) {
1837 LOGI_GEOFENCE("sqlite3_step Error[%d] : %s", ret, sqlite3_errmsg(db_info_s.handle));
1838 sqlite3_finalize(state);
1839 sqlite3_free(query);
1840 return FENCE_ERR_SQLITE_FAIL;
1843 id = (char *) sqlite3_column_text(state, 0);
1844 if (!id || !strlen(id))
1845 LOGI_GEOFENCE("ERROR: appid is NULL!!!");
1847 *appid = g_strdup(id);
1849 sqlite3_finalize(state);
1850 sqlite3_free(query);
1851 return FENCE_ERR_NONE;
1855 * This function set appid on DB.
1857 * @param[in] fence_id
1859 * @return FENCE_ERR_NONE on success, negative values for errors
1861 int geofence_manager_set_appid_to_geofence(int fence_id, char *appid)
1863 FUNC_ENTRANCE_SERVER;
1864 sqlite3_stmt *state;
1865 int ret = SQLITE_OK;
1868 char *query = sqlite3_mprintf("UPDATE GeoFence SET app_id = %Q where fence_id = %d;", appid, fence_id);
1870 ret = sqlite3_prepare_v2(db_info_s.handle, query, -1, &state, &tail);
1871 if (ret != SQLITE_OK) {
1872 LOGI_GEOFENCE("Error: %s", sqlite3_errmsg(db_info_s.handle));
1873 sqlite3_free(query);
1874 return FENCE_ERR_PREPARE;
1877 ret = sqlite3_step(state);
1878 if (ret != SQLITE_DONE) {
1879 LOGI_GEOFENCE("sqlite3_step Error[%d] : %s", ret, sqlite3_errmsg(db_info_s.handle));
1880 sqlite3_finalize(state);
1881 sqlite3_free(query);
1882 return FENCE_ERR_SQLITE_FAIL;
1885 sqlite3_finalize(state);
1886 sqlite3_free(query);
1887 return FENCE_ERR_NONE;
1891 * This function get ble info from DB.
1893 * @param[in] fence_id
1894 * @param[in] ble_info
1895 * @return FENCE_ERR_NONE on success, negative values for errors
1897 int geofence_manager_get_ble_info_from_geofence(int fence_id, char **ble_info)
1899 FUNC_ENTRANCE_SERVER;
1900 sqlite3_stmt *state = NULL;
1901 int ret = SQLITE_OK;
1902 const char *tail = NULL;
1905 char *query = sqlite3_mprintf("SELECT ble_info FROM GeoFence where fence_id = %d;", fence_id);
1906 LOGD_GEOFENCE("current fence id is [%d]", fence_id);
1907 ret = sqlite3_prepare_v2(db_info_s.handle, query, -1, &state, &tail);
1908 if (ret != SQLITE_OK) {
1909 LOGI_GEOFENCE("Error: %s", sqlite3_errmsg(db_info_s.handle));
1910 sqlite3_free(query);
1911 return FENCE_ERR_PREPARE;
1914 ret = sqlite3_step(state);
1915 if (ret != SQLITE_ROW) {
1916 LOGI_GEOFENCE("sqlite3_step Error[%d] : %s", ret, sqlite3_errmsg(db_info_s.handle));
1917 sqlite3_finalize(state);
1918 sqlite3_free(query);
1919 return FENCE_ERR_SQLITE_FAIL;
1922 info = (char *) sqlite3_column_text(state, 0);
1923 if (!info || !strlen(info))
1924 LOGI_GEOFENCE("ERROR: ble info is NULL!!!");
1926 *ble_info = g_strdup(info);
1928 sqlite3_finalize(state);
1929 sqlite3_free(query);
1930 return FENCE_ERR_NONE;
1934 * This function set ble info on DB.
1936 * @param[in] fence_id
1937 * @param[in] ble_info
1938 * @return FENCE_ERR_NONE on success, negative values for errors
1940 int geofence_manager_set_ble_info_to_geofence(int fence_id, char *ble_info)
1942 FUNC_ENTRANCE_SERVER;
1943 sqlite3_stmt *state;
1944 int ret = SQLITE_OK;
1947 char *query = sqlite3_mprintf("UPDATE GeoFence SET ble_info = %Q where fence_id = %d;", ble_info, fence_id);
1949 ret = sqlite3_prepare_v2(db_info_s.handle, query, -1, &state, &tail);
1950 if (ret != SQLITE_OK) {
1951 LOGI_GEOFENCE("Error: %s", sqlite3_errmsg(db_info_s.handle));
1952 sqlite3_free(query);
1953 return FENCE_ERR_PREPARE;
1956 ret = sqlite3_step(state);
1957 if (ret != SQLITE_DONE) {
1958 LOGI_GEOFENCE("sqlite3_step Error[%d] : %s", ret, sqlite3_errmsg(db_info_s.handle));
1959 sqlite3_finalize(state);
1960 sqlite3_free(query);
1961 return FENCE_ERR_SQLITE_FAIL;
1964 sqlite3_finalize(state);
1965 sqlite3_free(query);
1966 return FENCE_ERR_NONE;
1970 * This function get geofence type from DB.
1972 * @param[in] fence_id
1973 * @param[in] geofence_type_e.
1974 * @return FENCE_ERR_NONE on success, negative values for errors
1976 int geofence_manager_get_geofence_type(int fence_id, geofence_type_e *fence_type)
1978 FUNC_ENTRANCE_SERVER;
1979 sqlite3_stmt *state = NULL;
1980 int ret = SQLITE_OK;
1981 const char *tail = NULL;
1983 char *query = sqlite3_mprintf("SELECT geofence_type FROM GeoFence where fence_id = %d;", fence_id);
1985 LOGD_GEOFENCE("current fence id is [%d]", fence_id);
1986 ret = sqlite3_prepare_v2(db_info_s.handle, query, -1, &state, &tail);
1987 if (ret != SQLITE_OK) {
1988 LOGI_GEOFENCE("Error: %s", sqlite3_errmsg(db_info_s.handle));
1989 sqlite3_free(query);
1990 return FENCE_ERR_PREPARE;
1993 ret = sqlite3_step(state);
1994 if (ret != SQLITE_ROW) {
1995 LOGI_GEOFENCE("sqlite3_step Error[%d] : %s", ret, sqlite3_errmsg(db_info_s.handle));
1996 sqlite3_finalize(state);
1997 sqlite3_free(query);
1998 return FENCE_ERR_SQLITE_FAIL;
2001 *fence_type = sqlite3_column_int(state, 0);
2003 sqlite3_reset(state);
2004 sqlite3_finalize(state);
2005 sqlite3_free(query);
2007 return FENCE_ERR_NONE;
2010 int geofence_manager_get_place_id(int fence_id, int *place_id)
2012 FUNC_ENTRANCE_SERVER;
2013 sqlite3_stmt *state = NULL;
2014 int ret = SQLITE_OK;
2015 const char *tail = NULL;
2017 char *query = sqlite3_mprintf("SELECT place_id FROM GeoFence where fence_id = %d;", fence_id);
2019 LOGD_GEOFENCE("current fence id is [%d]", fence_id);
2020 ret = sqlite3_prepare_v2(db_info_s.handle, query, -1, &state, &tail);
2021 if (ret != SQLITE_OK) {
2022 LOGI_GEOFENCE("Error: %s", sqlite3_errmsg(db_info_s.handle));
2023 sqlite3_free(query);
2024 return FENCE_ERR_PREPARE;
2027 ret = sqlite3_step(state);
2028 if (ret != SQLITE_ROW) {
2029 LOGI_GEOFENCE("sqlite3_step Error[%d] : %s", ret, sqlite3_errmsg(db_info_s.handle));
2030 sqlite3_finalize(state);
2031 sqlite3_free(query);
2032 return FENCE_ERR_SQLITE_FAIL;
2035 *place_id = sqlite3_column_int(state, 0);
2037 sqlite3_reset(state);
2038 sqlite3_finalize(state);
2039 sqlite3_free(query);
2041 return FENCE_ERR_NONE;
2045 * This function get geofence/place access type from DB.
2047 * @param[in] fence_id/place_id
2048 * @param[in] access_type_e.
2049 * @return FENCE_ERR_NONE on success, negative values for errors
2051 int geofence_manager_get_access_type(int fence_id, int place_id, access_type_e *fence_type)
2053 FUNC_ENTRANCE_SERVER;
2054 sqlite3_stmt *state = NULL;
2055 int ret = SQLITE_OK;
2056 const char *tail = NULL;
2060 query = sqlite3_mprintf("SELECT access_type FROM GeoFence WHERE fence_id = %d;", fence_id);
2061 else if (fence_id == -1)
2062 query = sqlite3_mprintf("SELECT access_type FROM Places WHERE place_id = %d", place_id);
2064 LOGD_GEOFENCE("current fence id is [%d]", fence_id);
2065 LOGD_GEOFENCE("current place id is [%d]", place_id);
2068 return FENCE_ERR_INVALID_PARAMETER;
2070 ret = sqlite3_prepare_v2(db_info_s.handle, query, -1, &state, &tail);
2071 if (ret != SQLITE_OK) {
2072 LOGI_GEOFENCE("Error: %s", sqlite3_errmsg(db_info_s.handle));
2073 sqlite3_free(query);
2074 return FENCE_ERR_PREPARE;
2077 ret = sqlite3_step(state);
2078 if (ret != SQLITE_ROW) {
2079 LOGI_GEOFENCE("sqlite3_step Error[%d] : %s", ret, sqlite3_errmsg(db_info_s.handle));
2080 sqlite3_finalize(state);
2081 sqlite3_free(query);
2082 return FENCE_ERR_SQLITE_FAIL;
2085 *fence_type = sqlite3_column_int(state, 0);
2087 sqlite3_reset(state);
2088 sqlite3_finalize(state);
2089 sqlite3_free(query);
2091 return FENCE_ERR_NONE;
2095 * This function set geofence type on DB.
2097 * @param[in] fence_id
2098 * @param[in] fence_type.
2099 * @return FENCE_ERR_NONE on success, negative values for errors
2101 int geofence_manager_set_geofence_type(int fence_id, geofence_type_e fence_type)
2103 FUNC_ENTRANCE_SERVER;
2104 sqlite3_stmt *state;
2105 int ret = SQLITE_OK;
2108 char *query = sqlite3_mprintf("UPDATE GeoFence SET geofence_type = %d where fence_id = %d;", fence_type, fence_id);
2110 ret = sqlite3_prepare_v2(db_info_s.handle, query, -1, &state, &tail);
2111 if (ret != SQLITE_OK) {
2112 LOGI_GEOFENCE("Error: %s", sqlite3_errmsg(db_info_s.handle));
2113 sqlite3_free(query);
2114 return FENCE_ERR_PREPARE;
2117 ret = sqlite3_step(state);
2118 if (ret != SQLITE_DONE) {
2119 LOGI_GEOFENCE("sqlite3_step Error[%d] : %s", ret, sqlite3_errmsg(db_info_s.handle));
2120 sqlite3_finalize(state);
2121 sqlite3_free(query);
2122 return FENCE_ERR_SQLITE_FAIL;
2125 sqlite3_finalize(state);
2126 sqlite3_free(query);
2127 return FENCE_ERR_NONE;
2131 * This function get geofence place_id from DB.
2133 * @param[in] fence_id
2134 * @param[in] place_id
2135 * @return FENCE_ERR_NONE on success, negative values for errors
2137 int geofence_manager_get_placeid_from_geofence(int fence_id, int *place_id)
2139 FUNC_ENTRANCE_SERVER;
2140 sqlite3_stmt *state = NULL;
2141 int ret = SQLITE_OK;
2142 const char *tail = NULL;
2144 char *query = sqlite3_mprintf("SELECT place_id FROM GeoFence where fence_id = %d;", fence_id);
2145 LOGD_GEOFENCE("current fence id is [%d]", fence_id);
2146 ret = sqlite3_prepare_v2(db_info_s.handle, query, -1, &state, &tail);
2147 if (ret != SQLITE_OK) {
2148 LOGI_GEOFENCE("Error: %s", sqlite3_errmsg(db_info_s.handle));
2149 sqlite3_free(query);
2150 return FENCE_ERR_PREPARE;
2153 ret = sqlite3_step(state);
2154 if (ret != SQLITE_ROW) {
2155 LOGI_GEOFENCE("sqlite3_step Error[%d] : %s", ret, sqlite3_errmsg(db_info_s.handle));
2156 sqlite3_finalize(state);
2157 sqlite3_free(query);
2158 return FENCE_ERR_SQLITE_FAIL;
2161 *place_id = sqlite3_column_int(state, 0);
2163 sqlite3_reset(state);
2164 sqlite3_finalize(state);
2165 sqlite3_free(query);
2167 return FENCE_ERR_NONE;
2171 * This function get running status from DB.
2173 * @param[in] fence_id
2175 * @return FENCE_ERR_NONE on success, negative values for errors
2177 int geofence_manager_get_running_status(int fence_id, int *running_status)
2179 FUNC_ENTRANCE_SERVER;
2180 sqlite3_stmt *state = NULL;
2181 int ret = SQLITE_OK;
2182 const char *tail = NULL;
2184 char *query = sqlite3_mprintf("SELECT running_status FROM GeoFence where fence_id = %d;", fence_id);
2186 LOGD_GEOFENCE("current fence id is [%d]", fence_id);
2187 ret = sqlite3_prepare_v2(db_info_s.handle, query, -1, &state, &tail);
2188 if (ret != SQLITE_OK) {
2189 LOGI_GEOFENCE("Error: %s", sqlite3_errmsg(db_info_s.handle));
2190 sqlite3_free(query);
2191 return FENCE_ERR_PREPARE;
2194 ret = sqlite3_step(state);
2195 if (ret != SQLITE_ROW) {
2196 LOGI_GEOFENCE("sqlite3_step Error[%d] : %s", ret, sqlite3_errmsg(db_info_s.handle));
2197 sqlite3_finalize(state);
2198 sqlite3_free(query);
2199 return FENCE_ERR_SQLITE_FAIL;
2202 *running_status = sqlite3_column_int(state, 0);
2204 sqlite3_reset(state);
2205 sqlite3_finalize(state);
2206 sqlite3_free(query);
2208 return FENCE_ERR_NONE;
2212 * This function set running state on DB.
2214 * @param[in] fence_id
2216 * @return FENCE_ERR_NONE on success, negative values for errors
2218 int geofence_manager_set_running_status(int fence_id, int running_status)
2220 FUNC_ENTRANCE_SERVER;
2221 sqlite3_stmt *state;
2222 int ret = SQLITE_OK;
2225 char *query = sqlite3_mprintf("UPDATE GeoFence SET running_status = %d where fence_id = %d;", running_status, fence_id);
2227 ret = sqlite3_prepare_v2(db_info_s.handle, query, -1, &state, &tail);
2228 if (ret != SQLITE_OK) {
2229 LOGI_GEOFENCE("Error: %s", sqlite3_errmsg(db_info_s.handle));
2230 sqlite3_free(query);
2231 return FENCE_ERR_PREPARE;
2234 ret = sqlite3_step(state);
2235 if (ret != SQLITE_DONE) {
2236 LOGI_GEOFENCE("sqlite3_step Error[%d] : %s", ret, sqlite3_errmsg(db_info_s.handle));
2237 sqlite3_finalize(state);
2238 sqlite3_free(query);
2239 return FENCE_ERR_SQLITE_FAIL;
2242 sqlite3_finalize(state);
2243 sqlite3_free(query);
2244 return FENCE_ERR_NONE;
2248 * This function get direction type from DB.
2250 * @param[in] fence_id
2251 * @param[in] direction
2252 * @return FENCE_ERR_NONE on success, negative values for errors
2254 int geofence_manager_get_direction(int fence_id, geofence_direction_e *direction)
2256 FUNC_ENTRANCE_SERVER;
2257 sqlite3_stmt *state = NULL;
2258 int ret = SQLITE_OK;
2259 const char *tail = NULL;
2261 char *query = sqlite3_mprintf("SELECT direction FROM GeoFence where fence_id = %d;", fence_id);
2262 LOGD_GEOFENCE("current fence id is [%d]", fence_id);
2263 ret = sqlite3_prepare_v2(db_info_s.handle, query, -1, &state, &tail);
2264 if (ret != SQLITE_OK) {
2265 LOGI_GEOFENCE("Error: %s", sqlite3_errmsg(db_info_s.handle));
2266 sqlite3_free(query);
2267 return FENCE_ERR_PREPARE;
2270 ret = sqlite3_step(state);
2271 if (ret != SQLITE_ROW) {
2272 LOGI_GEOFENCE("sqlite3_step Error[%d] : %s", ret, sqlite3_errmsg(db_info_s.handle));
2273 sqlite3_finalize(state);
2274 sqlite3_free(query);
2275 return FENCE_ERR_SQLITE_FAIL;
2278 *direction = sqlite3_column_int(state, 0);
2280 sqlite3_finalize(state);
2281 sqlite3_free(query);
2283 return FENCE_ERR_NONE;
2287 * This function set direction type on DB.
2289 * @param[in] fence_id
2290 * @param[in] direction
2291 * @return FENCE_ERR_NONE on success, negative values for errors
2293 int geofence_manager_set_direction(int fence_id, geofence_direction_e direction)
2295 FUNC_ENTRANCE_SERVER;
2296 sqlite3_stmt *state;
2297 int ret = SQLITE_OK;
2300 char *query = sqlite3_mprintf("UPDATE GeoFence SET direction = %d where fence_id = %d;", direction, fence_id);
2302 ret = sqlite3_prepare_v2(db_info_s.handle, query, -1, &state, &tail);
2303 if (ret != SQLITE_OK) {
2304 LOGI_GEOFENCE("Error: %s", sqlite3_errmsg(db_info_s.handle));
2305 sqlite3_free(query);
2306 return FENCE_ERR_PREPARE;
2309 ret = sqlite3_step(state);
2310 if (ret != SQLITE_DONE) {
2311 LOGI_GEOFENCE("sqlite3_step Error[%d] : %s", ret, sqlite3_errmsg(db_info_s.handle));
2312 sqlite3_finalize(state);
2313 sqlite3_free(query);
2314 return FENCE_ERR_SQLITE_FAIL;
2317 sqlite3_finalize(state);
2318 sqlite3_free(query);
2320 return FENCE_ERR_NONE;
2324 * This function remove fence from DB.
2326 * @param[in] fence_id
2327 * @return FENCE_ERR_NONE on success, negative values for errors
2329 int geofence_manager_delete_fence_info(int fence_id)
2331 FUNC_ENTRANCE_SERVER;
2332 g_return_val_if_fail(fence_id, FENCE_ERR_INVALID_PARAMETER);
2333 int ret = FENCE_ERR_NONE;
2334 geofence_type_e fence_type = GEOFENCE_INVALID;
2336 ret = geofence_manager_get_geofence_type(fence_id, &fence_type);
2337 if (FENCE_ERR_NONE != ret) {
2338 LOGI_GEOFENCE("Fail to geofence_manager_delete_fence_point_info");
2342 ret = __geofence_manager_db_enable_foreign_keys();
2343 if (FENCE_ERR_NONE != ret) {
2344 LOGI_GEOFENCE("Fail to geofence_manager_db_enable_foreign_keys");
2348 ret = __geofence_manager_delete_table(fence_id, FENCE_MAIN_TABLE);
2349 if (FENCE_ERR_NONE != ret) {
2350 LOGI_GEOFENCE("Fail to geofence_manager_delete_fence_point_info");
2358 * This function remove place from DB.
2360 * @param[in] place_id
2361 * @return FENCE_ERR_NONE on success, negative values for errors
2363 int geofence_manager_delete_place_info(int place_id)
2365 FUNC_ENTRANCE_SERVER;
2366 g_return_val_if_fail(place_id, FENCE_ERR_INVALID_PARAMETER);
2367 int ret = FENCE_ERR_NONE;
2369 ret = __geofence_manager_db_enable_foreign_keys();
2370 if (FENCE_ERR_NONE != ret) {
2371 LOGI_GEOFENCE("Fail to geofence_manager_db_enable_foreign_keys");
2375 ret = __geofence_manager_delete_place_table(place_id);
2376 if (FENCE_ERR_NONE != ret) {
2377 LOGI_GEOFENCE("Fail to geofence_manager_delete_place_info");
2385 * This function close DB handle.
2387 * @param[in] fence_id
2388 * @return FENCE_ERR_NONE on success, negative values for errors
2390 int geofence_manager_close_db(void)
2392 FUNC_ENTRANCE_SERVER;
2393 int ret = SQLITE_OK;
2395 if (db_info_s.handle == NULL)
2396 return FENCE_ERR_NONE;
2398 ret = db_util_close(db_info_s.handle);
2399 if (ret != SQLITE_OK) {
2400 LOGI_GEOFENCE("Close DB ERROR!!!");
2401 return FENCE_ERR_SQLITE_FAIL;
2404 return FENCE_ERR_NONE;
2408 * This function deletes all data on db.
2410 * @return FENCE_ERR_NONE on success, negative values for errors
2412 int geofence_manager_reset(void)
2414 FUNC_ENTRANCE_SERVER;
2415 sqlite3_stmt *state = NULL;
2416 int ret = SQLITE_OK;
2418 ret = __geofence_manager_db_enable_foreign_keys();
2419 if (FENCE_ERR_NONE != ret) {
2420 LOGI_GEOFENCE("Fail to geofence_manager_db_enable_foreign_keys");
2424 char *query_two = sqlite3_mprintf("DELETE from %Q;", menu_table[FENCE_MAIN_TABLE]);
2426 ret = sqlite3_prepare_v2(db_info_s.handle, query_two, -1, &state, NULL);
2427 if (SQLITE_OK != ret) {
2428 LOGI_GEOFENCE("Fail to connect to table. Error[%s]", sqlite3_errmsg(db_info_s.handle));
2429 sqlite3_free(query_two);
2430 return FENCE_ERR_SQLITE_FAIL;
2433 ret = sqlite3_step(state);
2434 if (SQLITE_DONE != ret) {
2435 LOGI_GEOFENCE("Fail to step. Error[%d]", ret);
2436 sqlite3_finalize(state);
2437 sqlite3_free(query_two);
2438 return FENCE_ERR_SQLITE_FAIL;
2440 sqlite3_reset(state);
2441 sqlite3_finalize(state);
2442 sqlite3_free(query_two);
2444 char *query_three = sqlite3_mprintf("UPDATE sqlite_sequence SET seq = 0 where name = %Q;", menu_table[FENCE_MAIN_TABLE]);
2446 ret = sqlite3_prepare_v2(db_info_s.handle, query_three, -1, &state, NULL);
2447 if (SQLITE_OK != ret) {
2448 LOGI_GEOFENCE("Fail to connect to table. Error[%s]", sqlite3_errmsg(db_info_s.handle));
2449 sqlite3_free(query_three);
2450 return FENCE_ERR_SQLITE_FAIL;
2453 ret = sqlite3_step(state);
2454 if (SQLITE_DONE != ret) {
2455 LOGI_GEOFENCE("Fail to step. Error[%d]", ret);
2456 sqlite3_finalize(state);
2457 sqlite3_free(query_three);
2458 return FENCE_ERR_SQLITE_FAIL;
2461 sqlite3_reset(state);
2462 sqlite3_finalize(state);
2463 sqlite3_free(query_three);
2464 return FENCE_ERR_NONE;
2468 * This function copy source wifi info to dest wifi info.
2470 * @param[in] src_wifi
2471 * @param[out] dest_wifi
2472 * @return FENCE_ERR_NONE on success, negative values for errors
2474 int geofence_manager_copy_wifi_info(wifi_info_s *src_wifi, wifi_info_s **dest_wifi)
2476 FUNC_ENTRANCE_SERVER;
2477 g_return_val_if_fail(src_wifi, FENCE_ERR_INVALID_PARAMETER);
2479 *dest_wifi = (wifi_info_s *)g_malloc0(sizeof(wifi_info_s));
2480 g_return_val_if_fail(*dest_wifi, -1);
2482 g_strlcpy((*dest_wifi)->bssid, src_wifi->bssid, WLAN_BSSID_LEN);
2484 return FENCE_ERR_NONE;
2488 * This function create a wifi infor .
2490 * @param[in] fence_id
2492 * @param[out] wifi info
2493 * @return FENCE_ERR_NONE on success, negative values for errors
2495 int geofence_manager_create_wifi_info(int fence_id, char *bssid, wifi_info_s **new_wifi)
2497 FUNC_ENTRANCE_SERVER;
2498 g_return_val_if_fail(fence_id >= 0, FENCE_ERR_INVALID_PARAMETER);
2499 g_return_val_if_fail(bssid, FENCE_ERR_INVALID_PARAMETER);
2501 *new_wifi = (wifi_info_s *)g_malloc0(sizeof(wifi_info_s));
2502 g_strlcpy((*new_wifi)->bssid, bssid, WLAN_BSSID_LEN);
2504 return FENCE_ERR_NONE;
2508 * This function get fence id count by params such as app id and fence type and enable status .
2510 * @param[in] app_id : if app_id == NULL: ALL
2511 * @param[in] fence_type:if GEOFENCE_TYPE_INVALID == NULL: ALL fence type
2512 * @param[in] enable_status
2513 * @param[out] fence id count
2514 * @return FENCE_ERR_NONE on success, negative values for errors
2516 int geofence_manager_get_count_by_params(const char *app_id, geofence_type_e fence_type, int *count)
2518 FUNC_ENTRANCE_SERVER;
2519 sqlite3_stmt *state = NULL;
2520 int ret = SQLITE_OK;
2521 const char *tail = NULL;
2524 if (NULL == app_id) {
2525 if (GEOFENCE_INVALID != fence_type) { /* app_id == NULL : All and GEOFENCE_TYPE_INVALID != fence_type */
2526 query = sqlite3_mprintf("SELECT COUNT(fence_id) FROM GeoFence where geofence_type = %d ;", fence_type);
2528 query = sqlite3_mprintf("SELECT COUNT(fence_id) FROM GeoFence ;");
2530 } else { /*app_id not NULL */
2531 if (GEOFENCE_INVALID != fence_type) { /* app_id not NULL and GEOFENCE_TYPE_INVALID != fence_type */
2532 query = sqlite3_mprintf("SELECT COUNT(fence_id) FROM GeoFence where app_id = %Q AND geofence_type = %d ;", app_id, fence_type);
2534 query = sqlite3_mprintf("SELECT COUNT(fence_id) FROM GeoFence where app_id = %Q ;", app_id);
2538 LOGI_GEOFENCE("app_id[%s] fence_type[%d] ", app_id, fence_type);
2539 ret = sqlite3_prepare_v2(db_info_s.handle, query, -1, &state, &tail);
2540 if (ret != SQLITE_OK) {
2541 LOGI_GEOFENCE("Error: %s", sqlite3_errmsg(db_info_s.handle));
2542 sqlite3_free(query);
2543 return FENCE_ERR_PREPARE;
2546 ret = sqlite3_step(state);
2547 if (ret != SQLITE_ROW) {
2548 LOGI_GEOFENCE("sqlite3_step Error[%d] : %s", ret, sqlite3_errmsg(db_info_s.handle));
2549 sqlite3_finalize(state);
2550 sqlite3_free(query);
2551 return FENCE_ERR_SQLITE_FAIL;
2554 *count = sqlite3_column_int(state, 0);
2557 LOGI_GEOFENCE("ERROR: count = %d", *count);
2558 sqlite3_finalize(state);
2559 sqlite3_free(query);
2560 return FENCE_ERR_COUNT;
2562 LOGI_GEOFENCE("count[%d]", *count);
2565 sqlite3_reset(state);
2566 sqlite3_finalize(state);
2567 sqlite3_free(query);
2568 return FENCE_ERR_NONE;
2572 app_id == NULL : All, geofence_type_e : INVALID - all, IN enable_status : enable, disable or both. Output : a list of geofence_id
2574 int geofence_manager_get_fences(const char *app_id, geofence_type_e fence_type, GList **fences)
2576 FUNC_ENTRANCE_SERVER;
2577 sqlite3_stmt *state = NULL;
2578 int ret = SQLITE_OK;
2579 const char *tail = NULL;
2585 ret = geofence_manager_get_count_by_params(app_id, fence_type, &count);
2586 if (ret != FENCE_ERR_NONE) {
2587 LOGI_GEOFENCE("ERROR: geofence_manager_get_count_of_fences_by_app.");
2591 if (NULL == app_id) {
2592 if (GEOFENCE_INVALID != fence_type) { /* app_id == NULL : All and GEOFENCE_TYPE_INVALID != fence_type */
2593 query = sqlite3_mprintf("SELECT fence_id FROM GeoFence where geofence_type = %d;", fence_type);
2595 query = sqlite3_mprintf("SELECT fence_id FROM GeoFence;");
2597 } else { /*app_id not NULL */
2598 if (GEOFENCE_INVALID != fence_type) { /* app_id not NULL and GEOFENCE_TYPE_INVALID != fence_type */
2599 query = sqlite3_mprintf("SELECT fence_id FROM GeoFence where app_id = %Q AND geofence_type = %d ;", app_id, fence_type);
2601 query = sqlite3_mprintf("SELECT fence_id FROM GeoFence where app_id = %Q;", app_id);
2605 ret = sqlite3_prepare_v2(db_info_s.handle, query, -1, &state, &tail);
2606 if (ret != SQLITE_OK) {
2607 LOGI_GEOFENCE("Error: %s", sqlite3_errmsg(db_info_s.handle));
2608 sqlite3_free(query);
2609 return FENCE_ERR_PREPARE;
2612 for (i = 0; i < count; i++) {
2613 ret = sqlite3_step(state);
2614 if (ret != SQLITE_ROW) {
2615 LOGI_GEOFENCE("sqlite3_step Error[%d] : %s", ret, sqlite3_errmsg(db_info_s.handle));
2618 fence_id = sqlite3_column_int(state, 0);
2619 LOGI_GEOFENCE("fence id is [%d]", fence_id);
2620 *fences = g_list_append(*fences, (gpointer) GINT_TO_POINTER(fence_id));
2623 sqlite3_reset(state);
2624 sqlite3_finalize(state);
2625 sqlite3_free(query);
2626 return FENCE_ERR_NONE;
2629 int geofence_manager_get_count_of_fences(int *count)
2631 FUNC_ENTRANCE_SERVER;
2632 sqlite3_stmt *state = NULL;
2633 int ret = SQLITE_OK;
2634 const char *tail = NULL;
2635 char *query = sqlite3_mprintf("SELECT COUNT(fence_id) FROM GeoFence;");
2637 ret = sqlite3_prepare_v2(db_info_s.handle, query, -1, &state, &tail);
2638 if (ret != SQLITE_OK) {
2639 LOGI_GEOFENCE("Error: %s", sqlite3_errmsg(db_info_s.handle));
2640 sqlite3_free(query);
2641 return FENCE_ERR_PREPARE;
2644 ret = sqlite3_step(state);
2645 if (ret != SQLITE_ROW) {
2646 LOGI_GEOFENCE("sqlite3_step Error[%d] : %s", ret, sqlite3_errmsg(db_info_s.handle));
2647 sqlite3_finalize(state);
2648 sqlite3_free(query);
2649 return FENCE_ERR_SQLITE_FAIL;
2652 *count = sqlite3_column_int(state, 0);
2655 LOGI_GEOFENCE("ERROR: count = %d", *count);
2656 sqlite3_finalize(state);
2657 sqlite3_free(query);
2658 return FENCE_ERR_COUNT;
2660 LOGI_GEOFENCE("count[%d]", *count);
2663 sqlite3_reset(state);
2664 sqlite3_finalize(state);
2665 sqlite3_free(query);
2666 return FENCE_ERR_NONE;
2669 int geofence_manager_get_place_count_by_placeid(int place_id, int *count)
2671 FUNC_ENTRANCE_SERVER;
2672 sqlite3_stmt *state = NULL;
2673 int ret = SQLITE_OK;
2674 const char *tail = NULL;
2675 char *query = sqlite3_mprintf("SELECT COUNT(place_id) FROM Places WHERE place_id=%d;", place_id);
2677 ret = sqlite3_prepare_v2(db_info_s.handle, query, -1, &state, &tail);
2678 if (ret != SQLITE_OK) {
2679 LOGI_GEOFENCE("Error: %s", sqlite3_errmsg(db_info_s.handle));
2680 sqlite3_free(query);
2681 return FENCE_ERR_PREPARE;
2684 ret = sqlite3_step(state);
2685 if (ret != SQLITE_ROW) {
2686 LOGI_GEOFENCE("sqlite3_step Error[%d] : %s", ret, sqlite3_errmsg(db_info_s.handle));
2687 sqlite3_finalize(state);
2688 sqlite3_free(query);
2689 return FENCE_ERR_SQLITE_FAIL;
2692 *count = sqlite3_column_int(state, 0);
2695 LOGI_GEOFENCE("ERROR: place count = %d", *count);
2696 sqlite3_finalize(state);
2697 sqlite3_free(query);
2698 return FENCE_ERR_COUNT;
2700 LOGI_GEOFENCE("place count[%d]", *count);
2703 sqlite3_reset(state);
2704 sqlite3_finalize(state);
2705 sqlite3_free(query);
2706 return FENCE_ERR_NONE;