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);
198 return FENCE_ERR_SQLITE_FAIL;
201 if (sqlite3_changes(db_info_s.handle) == 0)
202 LOGI_GEOFENCE("No changes to DB");
204 return FENCE_ERR_NONE;
207 static inline int __geofence_manager_db_create_geofence_table(void)
209 FUNC_ENTRANCE_SERVER;
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)");
215 if (sqlite3_exec(db_info_s.handle, ddl, NULL, NULL, &err) != SQLITE_OK) {
216 LOGI_GEOFENCE("Failed to execute the DDL (%s)", err);
219 return FENCE_ERR_SQLITE_FAIL;
222 if (sqlite3_changes(db_info_s.handle) == 0)
223 LOGI_GEOFENCE("No changes to DB");
225 return FENCE_ERR_NONE;
228 static inline int __geofence_manager_db_create_geocoordinate_table(void)
230 FUNC_ENTRANCE_SERVER;
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)");
236 if (sqlite3_exec(db_info_s.handle, ddl, NULL, NULL, &err) != SQLITE_OK) {
237 LOGI_GEOFENCE("Failed to execute the DDL (%s)", err);
240 return FENCE_ERR_SQLITE_FAIL;
243 if (sqlite3_changes(db_info_s.handle) == 0)
244 LOGI_GEOFENCE("No changes to DB");
246 return FENCE_ERR_NONE;
249 static inline int __geofence_manager_db_create_wifi_data_table(void)
251 FUNC_ENTRANCE_SERVER;
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)");
257 if (sqlite3_exec(db_info_s.handle, ddl, NULL, NULL, &err) != SQLITE_OK) {
258 LOGI_GEOFENCE("Failed to execute the DDL (%s)", err);
261 return FENCE_ERR_SQLITE_FAIL;
264 if (sqlite3_changes(db_info_s.handle) == 0)
265 LOGI_GEOFENCE("No changes to DB");
267 return FENCE_ERR_NONE;
270 /* DB table for save the pair of fence id and bluetooth bssid */
271 static inline int __geofence_manager_db_create_bssid_table(void)
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)");
279 if (sqlite3_exec(db_info_s.handle, ddl, NULL, NULL, &err) != SQLITE_OK) {
280 LOGI_GEOFENCE("Failed to execute the DDL (%s)", err);
283 return FENCE_ERR_SQLITE_FAIL;
286 if (sqlite3_changes(db_info_s.handle) == 0)
287 LOGI_GEOFENCE("No changes to DB");
289 return FENCE_ERR_NONE;
292 static int __geofence_manager_open_db_handle(const int open_flag)
294 LOGI_GEOFENCE("enter");
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;
303 return FENCE_ERR_NONE;
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)
308 FUNC_ENTRANCE_SERVER;
309 g_return_val_if_fail(bssid, FENCE_ERR_INVALID_PARAMETER);
310 sqlite3_stmt *state = NULL;
312 const char *tail = NULL;
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);
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));
320 return FENCE_ERR_PREPARE;
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);
328 return FENCE_ERR_SQLITE_FAIL;
330 *count = sqlite3_column_int(state, 0);
331 sqlite3_reset(state);
332 sqlite3_finalize(state);
335 return FENCE_ERR_NONE;
338 static int __geofence_manager_db_insert_bssid_info(const int fence_id, char *bssid_info, const char *ssid)
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;
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);
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);
359 LOGI_GEOFENCE("count = %d", count);
361 return FENCE_ERR_NONE;
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));
368 return FENCE_ERR_PREPARE;
371 ret = sqlite3_bind_int(state, ++index, fence_id);
372 SQLITE3_RETURN(ret, sqlite3_errmsg(db_info_s.handle), state, query);
374 ret = sqlite3_bind_text(state, ++index, bssid_info, -1, SQLITE_STATIC);
375 SQLITE3_RETURN(ret, sqlite3_errmsg(db_info_s.handle), state, query);
377 ret = sqlite3_bind_text(state, ++index, ssid, -1, SQLITE_STATIC);
378 SQLITE3_RETURN(ret, sqlite3_errmsg(db_info_s.handle), state, query);
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);
385 return FENCE_ERR_SQLITE_FAIL;
388 sqlite3_reset(state);
389 sqlite3_clear_bindings(state);
390 sqlite3_finalize(state);
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]);
394 return FENCE_ERR_NONE;
397 static int __geofence_manager_db_insert_wifi_data_info(gpointer data, gpointer user_data)
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;
410 wifi_info = (wifi_info_s *) data;
411 LOGI_GEOFENCE("fence_id[%d] bssid[%s]", *fence_id, wifi_info->bssid);
413 char *query = sqlite3_mprintf("INSERT INTO FenceGeopointWifi(fence_id, bssid) VALUES (?, ?)");
415 ret = __geofence_manager_db_get_count_by_fence_id_and_bssid(*fence_id, wifi_info->bssid, FENCE_GEOPOINT_WIFI_TABLE, &count);
417 LOGI_GEOFENCE("count = %d", count);
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));
426 return FENCE_ERR_PREPARE;
429 ret = sqlite3_bind_int(state, ++index, *fence_id);
430 SQLITE3_RETURN(ret, sqlite3_errmsg(db_info_s.handle), state, query);
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);
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);
440 return FENCE_ERR_SQLITE_FAIL;
443 sqlite3_reset(state);
444 sqlite3_clear_bindings(state);
445 sqlite3_finalize(state);
448 return FENCE_ERR_NONE;
451 static int __geofence_manager_delete_table(int fence_id, fence_table_type_e table_type)
453 FUNC_ENTRANCE_SERVER;
454 sqlite3_stmt *state = NULL;
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));
463 return FENCE_ERR_SQLITE_FAIL;
466 ret = sqlite3_step(state);
467 if (SQLITE_DONE != ret) {
468 LOGI_GEOFENCE("Fail to step. Error[%d]", ret);
469 sqlite3_finalize(state);
471 return FENCE_ERR_SQLITE_FAIL;
473 sqlite3_finalize(state);
474 LOGI_GEOFENCE("fence_id[%d], deleted from db table [%s] successfully.", fence_id, menu_table[table_type]);
476 return FENCE_ERR_NONE;
479 static int __geofence_manager_delete_place_table(int place_id)
481 FUNC_ENTRANCE_SERVER;
482 sqlite3_stmt *state = NULL;
485 char *query = sqlite3_mprintf("DELETE from Places where place_id = %d;", place_id);
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));
492 return FENCE_ERR_SQLITE_FAIL;
495 ret = sqlite3_step(state);
496 if (SQLITE_DONE != ret) {
497 LOGI_GEOFENCE("Fail to step. Error[%d]", ret);
498 sqlite3_finalize(state);
500 return FENCE_ERR_SQLITE_FAIL;
502 sqlite3_finalize(state);
503 LOGI_GEOFENCE("place_id[%d], deleted place from db table Places successfully.", place_id);
505 return FENCE_ERR_NONE;
508 static inline void __geofence_manager_db_create_table(void)
510 FUNC_ENTRANCE_SERVER;
514 ret = __geofence_manager_db_create_places_table();
516 rollback_transaction();
520 ret = __geofence_manager_db_create_geofence_table();
522 rollback_transaction();
526 ret = __geofence_manager_db_create_geocoordinate_table();
528 rollback_transaction();
532 ret = __geofence_manager_db_create_wifi_data_table();
534 rollback_transaction();
538 ret = __geofence_manager_db_create_bssid_table();
540 rollback_transaction();
544 commit_transaction();
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)
550 sqlite3_stmt *state = NULL;
552 const char *tail = NULL;
554 char *query = sqlite3_mprintf("SELECT COUNT(fence_id) FROM %Q where fence_id = %d;", menu_table[table_type], fence_id);
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));
560 return FENCE_ERR_PREPARE;
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);
568 return FENCE_ERR_SQLITE_FAIL;
570 *count = sqlite3_column_int(state, 0);
571 sqlite3_reset(state);
572 sqlite3_finalize(state);
574 return FENCE_ERR_NONE;
577 static int __geofence_manager_db_enable_foreign_keys(void)
579 sqlite3_stmt *state = NULL;
580 int ret = FENCE_ERR_NONE;
581 char *query = sqlite3_mprintf("PRAGMA foreign_keys = ON;");
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));
587 return FENCE_ERR_SQLITE_FAIL;
590 ret = sqlite3_step(state);
591 if (SQLITE_DONE != ret) {
592 LOGI_GEOFENCE("Fail to step. Error[%d]", ret);
593 sqlite3_finalize(state);
595 return FENCE_ERR_SQLITE_FAIL;
597 sqlite3_reset(state);
598 sqlite3_finalize(state);
600 return FENCE_ERR_NONE;
603 #ifdef SUPPORT_ENCRYPTION
604 void replaceChar(char *src, char oldChar, char newChar)
613 void __geofence_manager_generate_password(char *password)
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' };
624 ret = bt_adapter_get_address(&bt_address);
625 if (ret != BT_ERROR_NONE)
626 LOGD_GEOFENCE("bt address get fail %d", ret);
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);
633 token = strtok_r(bt_address, ":", &save_token);
636 bt_temp[i++] = atoi(token);
637 token = strtok_r(NULL, ":", &save_token);
644 token = strtok_r(wifi_address, ":", &save_token);
647 wifi_temp[i++] = atoi(token);
648 token = strtok_r(NULL, ":", &save_token);
654 memset((void *) s1, 0, sizeof(s1));
655 memset((void *) s2, 0, sizeof(s2));
656 memset((void *) result, 0, sizeof(result));
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];
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);
672 sprintf(result, "%s%s", s1, s2);
673 LOGD_GEOFENCE("result : %s", result);
677 if (bt_address != NULL)
679 if (wifi_address != NULL)
684 static int __check_db_file()
688 fd = open(GEOFENCE_DB_FILE, O_RDONLY);
690 LOGW_GEOFENCE("DB file(%s) is not exist.", GEOFENCE_DB_FILE);
698 * This function in DB and create GeoFence/FenceGeocoordinate /FenceCurrentLocation four table on DB if necessary.
700 * @param[in] struct of fence_point_info_s
701 * @return FENCE_ERR_NONE on success, negative values for errors
703 int geofence_manager_db_init(void)
705 FUNC_ENTRANCE_SERVER;
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;
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;
717 open_flag = SQLITE_OPEN_READWRITE | SQLITE_OPEN_FULLMUTEX;
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;
725 if (open_flag & SQLITE_OPEN_CREATE)
726 __geofence_manager_db_create_table();
728 return FENCE_ERR_NONE;
731 int geofence_manager_db_reset(void)
733 FUNC_ENTRANCE_SERVER;
734 sqlite3_stmt *state = NULL;
739 for (idx = 0; idx < 4; idx++) {
740 query = sqlite3_mprintf("DELETE from %Q;", menu_table[idx]);
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));
746 return FENCE_ERR_SQLITE_FAIL;
749 ret = sqlite3_step(state);
750 if (SQLITE_DONE != ret) {
751 LOGI_GEOFENCE("Fail to step. Error[%d]", ret);
752 sqlite3_finalize(state);
754 return FENCE_ERR_SQLITE_FAIL;
756 sqlite3_finalize(state);
759 return FENCE_ERR_NONE;
762 int geofence_manager_set_place_info(place_info_s *place_info, int *place_id)
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;
771 char *query = sqlite3_mprintf("INSERT INTO Places (access_type, place_name, app_id) VALUES (?, ?, ?)");
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));
777 return FENCE_ERR_PREPARE;
779 LOGD_GEOFENCE("appid[%s] access_type[%d] place_name[%s]", place_info->appid, place_info->access_type, place_info->place_name);
781 ret = sqlite3_bind_int(state, ++index, place_info->access_type);
782 SQLITE3_RETURN(ret, sqlite3_errmsg(db_info_s.handle), state, query);
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);
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);
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);
795 return FENCE_ERR_SQLITE_FAIL;
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);
805 LOGI_GEOFENCE("TMP Invalid fence_id");
809 return FENCE_ERR_NONE;
812 int geofence_manager_set_common_info(fence_common_info_s *fence_info, int *fence_id)
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;
821 char *query = sqlite3_mprintf("INSERT INTO GeoFence (place_id, enable, app_id, geofence_type, access_type, running_status) VALUES (?, ?, ?, ?, ?, ?)");
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));
827 return FENCE_ERR_PREPARE;
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);
832 ret = sqlite3_bind_int(state, ++index, fence_info->place_id);
833 SQLITE3_RETURN(ret, sqlite3_errmsg(db_info_s.handle), state, query);
835 ret = sqlite3_bind_int(state, ++index, fence_info->enable);
836 SQLITE3_RETURN(ret, sqlite3_errmsg(db_info_s.handle), state, query);
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);
841 ret = sqlite3_bind_int(state, ++index, fence_info->type);
842 SQLITE3_RETURN(ret, sqlite3_errmsg(db_info_s.handle), state, query);
844 ret = sqlite3_bind_int(state, ++index, fence_info->access_type);
845 SQLITE3_RETURN(ret, sqlite3_errmsg(db_info_s.handle), state, query);
847 ret = sqlite3_bind_int(state, ++index, fence_info->running_status);
848 SQLITE3_RETURN(ret, sqlite3_errmsg(db_info_s.handle), state, query);
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);
855 return FENCE_ERR_SQLITE_FAIL;
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);
865 LOGI_GEOFENCE("TMP Invalid fence_id");
869 return FENCE_ERR_NONE;
872 int geofence_manager_get_place_list_from_db(int *number_of_places, GList **places)
874 FUNC_ENTRANCE_SERVER;
875 sqlite3_stmt *state = NULL;
877 const char *tail = NULL;
881 query = sqlite3_mprintf("SELECT place_id, place_name, access_type, app_id FROM Places");
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));
887 return FENCE_ERR_PREPARE;
889 GList *place_list = NULL;
890 int column_index = 0;
892 ret = sqlite3_step(state);
894 if (ret != SQLITE_ROW) {
895 LOGI_GEOFENCE("DONE...!!! : %d", ret);
899 place_info_s *place = g_slice_new0(place_info_s);
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);
910 } while (ret != SQLITE_DONE);
912 *places = place_list;
913 *number_of_places = count;
915 sqlite3_reset(state);
916 sqlite3_finalize(state);
918 return FENCE_ERR_NONE;
921 int geofence_manager_get_fence_list_from_db(int *number_of_fences, GList **fences, int place_id)
923 FUNC_ENTRANCE_SERVER;
925 sqlite3_stmt *state = NULL;
927 const char *tail = NULL;
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");
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);
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));
940 return FENCE_ERR_PREPARE;
942 GList *fence_list = NULL;
944 ret = sqlite3_step(state);
946 if (ret != SQLITE_ROW) {
947 LOGI_GEOFENCE("DONE...!!! : %d", ret);
950 int column_index = 0;
952 geofence_info_s *fence = g_slice_new0(geofence_info_s);
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;
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!!!");
968 fence->param.latitude = atof(data_name);
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!!!");
974 fence->param.longitude = atof(data_name);
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!!!");
980 fence->param.radius = atof(data_name);
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);
988 } while (ret != SQLITE_DONE);
990 *fences = fence_list;
991 *number_of_fences = count;
993 sqlite3_reset(state);
994 sqlite3_finalize(state);
996 return FENCE_ERR_NONE;
999 int geofence_manager_get_fenceid_list_from_db(int *number_of_fences, GList **fences, int place_id)
1001 FUNC_ENTRANCE_SERVER;
1002 sqlite3_stmt *state = NULL;
1003 int ret = SQLITE_OK;
1004 const char *tail = NULL;
1007 query = sqlite3_mprintf("SELECT fence_id FROM GeoFence WHERE place_id = %d", place_id);
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;
1015 GList *fence_list = NULL;
1016 int column_index = 0;
1019 ret = sqlite3_step(state);
1020 if (ret != SQLITE_ROW) {
1021 LOGI_GEOFENCE("DONE...!!! : %d", ret);
1025 fence_id = sqlite3_column_int(state, column_index);
1026 fence_list = g_list_append(fence_list, GINT_TO_POINTER(fence_id));
1028 } while (ret != SQLITE_DONE);
1029 *fences = fence_list;
1030 *number_of_fences = count;
1032 sqlite3_reset(state);
1033 sqlite3_finalize(state);
1034 sqlite3_free(query);
1035 return FENCE_ERR_NONE;
1038 int geofence_manager_update_geocoordinate_info(int fence_id, geocoordinate_info_s *geocoordinate_info)
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;
1045 int ret = SQLITE_OK;
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);
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;
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;
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;
1070 int geofence_manager_update_place_info(int place_id, const char *place_info_name)
1072 FUNC_ENTRANCE_SERVER;
1073 g_return_val_if_fail(place_id, FENCE_ERR_INVALID_PARAMETER);
1074 sqlite3_stmt *state = NULL;
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);
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;
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;
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;
1101 * This function set geocoordinate info in DB.
1103 * @param[in] fence_id
1104 * @param[out] struct of geocoordinate_info_s
1105 * @return FENCE_ERR_NONE on success, negative values for errors
1107 int geofence_manager_set_geocoordinate_info(int fence_id, geocoordinate_info_s *geocoordinate_info)
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;
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 (?, ?, ?, ?, ?)");
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);
1127 } else if (count) { /* fence id has been in FenceGeocoordinate table */
1128 sqlite3_free(query);
1129 return FENCE_ERR_FENCE_ID;
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;
1139 ret = sqlite3_bind_int(state, ++index, fence_id);
1140 SQLITE3_RETURN(ret, sqlite3_errmsg(db_info_s.handle), state, query);
1142 #ifdef SUPPORT_ENCRYPTION
1143 if (password == NULL)
1144 __geofence_manager_generate_password(password);
1147 ret = snprintf(data_name_lat, MAX_DATA_NAME, "%lf", geocoordinate_info->latitude);
1149 ret = sqlite3_bind_text(state, ++index, data_name_lat, -1, SQLITE_STATIC);
1151 /*ret = sqlite3_bind_double (state, ++index, geocoordinate_info->latitude);*/
1152 SQLITE3_RETURN(ret, sqlite3_errmsg(db_info_s.handle), state, query);
1154 ret = snprintf(data_name_lon, MAX_DATA_NAME, "%lf", geocoordinate_info->longitude);
1156 LOGD_GEOFENCE("ERROR: String will be truncated");
1157 sqlite3_free(query);
1158 sqlite3_finalize(state);
1159 return FENCE_ERR_STRING_TRUNCATED;
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);
1166 ret = snprintf(data_name_rad, MAX_DATA_NAME, "%lf", geocoordinate_info->radius);
1168 LOGD_GEOFENCE("ERROR: String will be truncated");
1169 sqlite3_free(query);
1170 sqlite3_finalize(state);
1171 return FENCE_ERR_STRING_TRUNCATED;
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);
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);
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;
1189 sqlite3_reset(state);
1190 sqlite3_clear_bindings(state);
1191 sqlite3_finalize(state);
1192 sqlite3_free(query);
1194 return FENCE_ERR_NONE;
1198 * This function get geocoordinate info from DB.
1200 * @param[in] fence_id
1201 * @param[out] struct of geocoordinate_info_s
1202 * @return FENCE_ERR_NONE on success, negative values for errors
1204 int geofence_manager_get_geocoordinate_info(int fence_id, geocoordinate_info_s **geocoordinate_info)
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;
1212 char *data_name = NULL;
1213 char *query = sqlite3_mprintf("SELECT * FROM FenceGeocoordinate where fence_id = %d;", fence_id);
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;
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;
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);
1236 g_return_val_if_fail(*geocoordinate_info, FENCE_ERR_INVALID_PARAMETER);
1238 #ifdef SUPPORT_ENCRYPTION
1239 if (password == NULL)
1240 __geofence_manager_generate_password(password);
1243 data_name = (char *) sqlite3_column_text(state, ++index);
1245 if (!data_name || !strlen(data_name))
1246 LOGI_GEOFENCE("ERROR: data_name is NULL!!!");
1248 (*geocoordinate_info)->latitude = atof(data_name);
1250 data_name = (char *) sqlite3_column_text(state, ++index);
1251 if (!data_name || !strlen(data_name))
1252 LOGI_GEOFENCE("ERROR: data_name is NULL!!!");
1254 (*geocoordinate_info)->longitude = atof(data_name);
1256 data_name = (char *) sqlite3_column_text(state, ++index);
1257 if (!data_name || !strlen(data_name))
1258 LOGI_GEOFENCE("ERROR: data_name is NULL!!!");
1260 (*geocoordinate_info)->radius = atof(data_name);
1262 g_strlcpy((*geocoordinate_info)->address, (char *) sqlite3_column_text(state, ++index), ADDRESS_LEN);
1264 sqlite3_finalize(state);
1265 sqlite3_free(query);
1267 return FENCE_ERR_NONE;
1271 * This function get ap list from DB.
1273 * @param[in] fence_id
1274 * @param[out] ap_list
1275 * @return FENCE_ERR_NONE on success, negative values for errors
1277 int geofence_manager_get_ap_info(const int fence_id, GList **ap_list)
1279 FUNC_ENTRANCE_SERVER;
1280 sqlite3_stmt *state = NULL;
1281 int ret = SQLITE_OK;
1282 const char *tail = NULL;
1285 wifi_info_s *wifi_info = NULL;
1286 const char *bssid = NULL;
1288 char *query1 = sqlite3_mprintf("SELECT COUNT(bssid) FROM FenceGeopointWifi where fence_id = %d;", fence_id);
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;
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;
1306 count = sqlite3_column_int(state, 0);
1307 sqlite3_reset(state);
1308 sqlite3_finalize(state);
1309 sqlite3_free(query1);
1311 LOGI_GEOFENCE("ERROR: count = %d", count);
1312 return FENCE_ERR_COUNT;
1314 LOGD_GEOFENCE("count[%d]", count);
1317 char *query2 = sqlite3_mprintf("SELECT * FROM FenceGeopointWifi where fence_id = %d;", fence_id);
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;
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));
1332 wifi_info = g_slice_new0(wifi_info_s);
1333 if (wifi_info == NULL) {
1334 sqlite3_free(query2);
1335 sqlite3_finalize(state);
1337 g_return_val_if_fail(wifi_info, -1);
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);
1345 sqlite3_finalize(state);
1346 sqlite3_free(query2);
1347 return FENCE_ERR_NONE;
1350 /*This function get place info from DB.
1352 * @param[in] place_id
1353 * @param[out] struct of place_info_s
1354 * @return FENCE_ERR_NONE on success, negative values for errors
1356 int geofence_manager_get_place_info(int place_id, place_info_s **place_info)
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;
1364 char *data_name = NULL;
1365 char *query = sqlite3_mprintf("SELECT * FROM Places where place_id = %d;", place_id);
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;
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;
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);
1386 g_return_val_if_fail(*place_info, FENCE_ERR_INTERNAL);
1388 data_name = (char *)sqlite3_column_text(state, ++index);
1389 if (!data_name || !strlen(data_name))
1390 LOGI_GEOFENCE("ERROR: data_name is NULL!!!");
1392 (*place_info)->access_type = atof(data_name);
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);
1399 return FENCE_ERR_NONE;
1403 * This function insert ap list in DB.
1405 * @param[in] fence_id
1406 * @param[out] ap_list
1407 * @return FENCE_ERR_NONE on success, negative values for errors
1409 int geofence_manager_set_ap_info(int fence_id, GList *ap_list)
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;
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);
1422 if (count) { /* fence id has been in FenceCurrentLocation table */
1423 LOGI_GEOFENCE("count is [%d]", count);
1424 return FENCE_ERR_FENCE_ID;
1428 g_list_foreach(ap_list, (GFunc) __geofence_manager_db_insert_wifi_data_info, &fence_id);
1430 return FENCE_ERR_NONE;
1434 * This function get bluetooth info from DB.
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
1440 int geofence_manager_get_bssid_info(const int fence_id, bssid_info_s **bssid_info)
1442 FUNC_ENTRANCE_SERVER;
1443 sqlite3_stmt *state = NULL;
1444 int ret = SQLITE_OK;
1445 const char *tail = NULL;
1448 bssid_info_s *bssid_info_from_db = NULL;
1449 const char *bssid = NULL;
1450 const char *ssid = NULL;
1452 char *query1 = sqlite3_mprintf("SELECT COUNT(bssid) FROM %s where fence_id = %d;", menu_table[FENCE_BSSID_TABLE], fence_id);
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;
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;
1470 count = sqlite3_column_int(state, 0);
1471 sqlite3_reset(state);
1472 sqlite3_finalize(state);
1473 sqlite3_free(query1);
1475 LOGI_GEOFENCE("ERROR: count = %d", count);
1476 return FENCE_ERR_COUNT;
1478 LOGD_GEOFENCE("count[%d]", count);
1481 char *query2 = sqlite3_mprintf("SELECT * FROM %s where fence_id = %d;", menu_table[FENCE_BSSID_TABLE], fence_id);
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;
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));
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);
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;
1512 sqlite3_finalize(state);
1513 sqlite3_free(query2);
1514 return FENCE_ERR_NONE;
1517 int geofence_manager_update_bssid_info(const int fence_id, bssid_info_s *bssid_info)
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;
1525 char *query = sqlite3_mprintf("UPDATE %Q SET bssid = %Q where fence_id = %d;", menu_table[FENCE_BSSID_TABLE], bssid_info->bssid, fence_id);
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;
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;
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;
1549 * This function insert bssid information in DB.
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
1555 int geofence_manager_set_bssid_info(int fence_id, bssid_info_s *bssid_info)
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;
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);
1568 if (count) { /* fence id has been in FenceBssid table */
1569 LOGI_GEOFENCE("count is [%d]", count);
1570 return FENCE_ERR_FENCE_ID;
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");
1579 return FENCE_ERR_NONE;
1583 * This function get enable status from DB.
1585 * @param[in] fence_id
1586 * @param[in] status: 1 enbale, 0 disable.
1587 * @return FENCE_ERR_NONE on success, negative values for errors
1589 int geofence_manager_get_enable_status(const int fence_id, int *status)
1591 FUNC_ENTRANCE_SERVER;
1592 sqlite3_stmt *state = NULL;
1593 int ret = SQLITE_OK;
1594 const char *tail = NULL;
1596 char *query = sqlite3_mprintf("SELECT enable FROM GeoFence where fence_id = %d;", fence_id);
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;
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;
1614 *status = sqlite3_column_int(state, 0);
1616 sqlite3_finalize(state);
1617 sqlite3_free(query);
1618 return FENCE_ERR_NONE;
1622 * This function set enable on DB.
1624 * @param[in] fence_id
1625 * @param[in] status: 1 enbale, 0 disable.
1626 * @return FENCE_ERR_NONE on success, negative values for errors
1628 int geofence_manager_set_enable_status(int fence_id, int status)
1630 FUNC_ENTRANCE_SERVER;
1631 sqlite3_stmt *state;
1632 int ret = SQLITE_OK;
1635 char *query = sqlite3_mprintf("UPDATE GeoFence SET enable = %d where fence_id = %d;", status, fence_id);
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;
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;
1652 sqlite3_finalize(state);
1653 sqlite3_free(query);
1654 return FENCE_ERR_NONE;
1658 * This function get name from DB.
1660 * @param[in] fence_id
1662 * @return FENCE_ERR_NONE on success, negative values for errors
1664 int geofence_manager_get_place_name(int place_id, char **name)
1666 FUNC_ENTRANCE_SERVER;
1667 sqlite3_stmt *state = NULL;
1668 int ret = SQLITE_OK;
1669 const char *tail = NULL;
1672 char *query = sqlite3_mprintf("SELECT place_name FROM Places where place_id = %d;", place_id);
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;
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;
1690 tmp = (char *) sqlite3_column_text(state, 0);
1691 if (!tmp || !strlen(tmp))
1692 LOGI_GEOFENCE("ERROR: name is NULL!!!");
1694 *name = g_strdup(tmp);
1696 sqlite3_finalize(state);
1697 sqlite3_free(query);
1698 return FENCE_ERR_NONE;
1702 * This function set name on DB.
1704 * @param[in] fence_id
1706 * @return FENCE_ERR_NONE on success, negative values for errors
1708 int geofence_manager_set_place_name(int place_id, const char *name)
1710 FUNC_ENTRANCE_SERVER;
1711 sqlite3_stmt *state;
1712 int ret = SQLITE_OK;
1715 char *query = sqlite3_mprintf("UPDATE Places SET place_name = %Q where place_id = %d;", name, place_id);
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;
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;
1732 sqlite3_finalize(state);
1733 sqlite3_free(query);
1734 return FENCE_ERR_NONE;
1738 * This function get appid from DB.
1740 * @param[in] place_id
1742 * @return FENCE_ERR_NONE on success, negative values for errors
1744 int geofence_manager_get_appid_from_places(int place_id, char **appid)
1746 FUNC_ENTRANCE_SERVER;
1747 sqlite3_stmt *state = NULL;
1748 int ret = SQLITE_OK;
1749 const char *tail = NULL;
1752 char *query = sqlite3_mprintf("SELECT app_id FROM Places where place_id = %d;", place_id);
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;
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;
1770 id = (char *) sqlite3_column_text(state, 0);
1771 if (!id || !strlen(id))
1772 LOGI_GEOFENCE("ERROR: appid is NULL!!!");
1774 *appid = g_strdup(id);
1776 sqlite3_finalize(state);
1777 sqlite3_free(query);
1778 return FENCE_ERR_NONE;
1782 * This function set appid on DB.
1784 * @param[in] place_id
1786 * @return FENCE_ERR_NONE on success, negative values for errors
1788 int geofence_manager_set_appid_to_places(int place_id, char *appid)
1790 FUNC_ENTRANCE_SERVER;
1791 sqlite3_stmt *state;
1792 int ret = SQLITE_OK;
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;
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;
1811 sqlite3_finalize(state);
1812 sqlite3_free(query);
1813 return FENCE_ERR_NONE;
1817 * This function get appid from DB.
1819 * @param[in] fence_id
1821 * @return FENCE_ERR_NONE on success, negative values for errors
1823 int geofence_manager_get_appid_from_geofence(int fence_id, char **appid)
1825 FUNC_ENTRANCE_SERVER;
1826 sqlite3_stmt *state = NULL;
1827 int ret = SQLITE_OK;
1828 const char *tail = NULL;
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;
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;
1848 id = (char *) sqlite3_column_text(state, 0);
1849 if (!id || !strlen(id))
1850 LOGI_GEOFENCE("ERROR: appid is NULL!!!");
1852 *appid = g_strdup(id);
1854 sqlite3_finalize(state);
1855 sqlite3_free(query);
1856 return FENCE_ERR_NONE;
1860 * This function set appid on DB.
1862 * @param[in] fence_id
1864 * @return FENCE_ERR_NONE on success, negative values for errors
1866 int geofence_manager_set_appid_to_geofence(int fence_id, char *appid)
1868 FUNC_ENTRANCE_SERVER;
1869 sqlite3_stmt *state;
1870 int ret = SQLITE_OK;
1873 char *query = sqlite3_mprintf("UPDATE GeoFence SET app_id = %Q where fence_id = %d;", appid, fence_id);
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;
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;
1890 sqlite3_finalize(state);
1891 sqlite3_free(query);
1892 return FENCE_ERR_NONE;
1896 * This function get ble info from DB.
1898 * @param[in] fence_id
1899 * @param[in] ble_info
1900 * @return FENCE_ERR_NONE on success, negative values for errors
1902 int geofence_manager_get_ble_info_from_geofence(int fence_id, char **ble_info)
1904 FUNC_ENTRANCE_SERVER;
1905 sqlite3_stmt *state = NULL;
1906 int ret = SQLITE_OK;
1907 const char *tail = NULL;
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;
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;
1927 info = (char *) sqlite3_column_text(state, 0);
1928 if (!info || !strlen(info))
1929 LOGI_GEOFENCE("ERROR: ble info is NULL!!!");
1931 *ble_info = g_strdup(info);
1933 sqlite3_finalize(state);
1934 sqlite3_free(query);
1935 return FENCE_ERR_NONE;
1939 * This function set ble info on DB.
1941 * @param[in] fence_id
1942 * @param[in] ble_info
1943 * @return FENCE_ERR_NONE on success, negative values for errors
1945 int geofence_manager_set_ble_info_to_geofence(int fence_id, char *ble_info)
1947 FUNC_ENTRANCE_SERVER;
1948 sqlite3_stmt *state;
1949 int ret = SQLITE_OK;
1952 char *query = sqlite3_mprintf("UPDATE GeoFence SET ble_info = %Q where fence_id = %d;", ble_info, fence_id);
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;
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;
1969 sqlite3_finalize(state);
1970 sqlite3_free(query);
1971 return FENCE_ERR_NONE;
1975 * This function get geofence type from DB.
1977 * @param[in] fence_id
1978 * @param[in] geofence_type_e.
1979 * @return FENCE_ERR_NONE on success, negative values for errors
1981 int geofence_manager_get_geofence_type(int fence_id, geofence_type_e *fence_type)
1983 FUNC_ENTRANCE_SERVER;
1984 sqlite3_stmt *state = NULL;
1985 int ret = SQLITE_OK;
1986 const char *tail = NULL;
1988 char *query = sqlite3_mprintf("SELECT geofence_type FROM GeoFence where fence_id = %d;", fence_id);
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;
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;
2006 *fence_type = sqlite3_column_int(state, 0);
2008 sqlite3_reset(state);
2009 sqlite3_finalize(state);
2010 sqlite3_free(query);
2012 return FENCE_ERR_NONE;
2015 int geofence_manager_get_place_id(int fence_id, int *place_id)
2017 FUNC_ENTRANCE_SERVER;
2018 sqlite3_stmt *state = NULL;
2019 int ret = SQLITE_OK;
2020 const char *tail = NULL;
2022 char *query = sqlite3_mprintf("SELECT place_id FROM GeoFence where fence_id = %d;", fence_id);
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;
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;
2040 *place_id = sqlite3_column_int(state, 0);
2042 sqlite3_reset(state);
2043 sqlite3_finalize(state);
2044 sqlite3_free(query);
2046 return FENCE_ERR_NONE;
2050 * This function get geofence/place access type from DB.
2052 * @param[in] fence_id/place_id
2053 * @param[in] access_type_e.
2054 * @return FENCE_ERR_NONE on success, negative values for errors
2056 int geofence_manager_get_access_type(int fence_id, int place_id, access_type_e *fence_type)
2058 FUNC_ENTRANCE_SERVER;
2059 sqlite3_stmt *state = NULL;
2060 int ret = SQLITE_OK;
2061 const char *tail = NULL;
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);
2069 LOGD_GEOFENCE("current fence id is [%d]", fence_id);
2070 LOGD_GEOFENCE("current place id is [%d]", place_id);
2073 return FENCE_ERR_INVALID_PARAMETER;
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;
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;
2090 *fence_type = sqlite3_column_int(state, 0);
2092 sqlite3_reset(state);
2093 sqlite3_finalize(state);
2094 sqlite3_free(query);
2096 return FENCE_ERR_NONE;
2100 * This function set geofence type on DB.
2102 * @param[in] fence_id
2103 * @param[in] fence_type.
2104 * @return FENCE_ERR_NONE on success, negative values for errors
2106 int geofence_manager_set_geofence_type(int fence_id, geofence_type_e fence_type)
2108 FUNC_ENTRANCE_SERVER;
2109 sqlite3_stmt *state;
2110 int ret = SQLITE_OK;
2113 char *query = sqlite3_mprintf("UPDATE GeoFence SET geofence_type = %d where fence_id = %d;", fence_type, fence_id);
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;
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;
2130 sqlite3_finalize(state);
2131 sqlite3_free(query);
2132 return FENCE_ERR_NONE;
2136 * This function get geofence place_id from DB.
2138 * @param[in] fence_id
2139 * @param[in] place_id
2140 * @return FENCE_ERR_NONE on success, negative values for errors
2142 int geofence_manager_get_placeid_from_geofence(int fence_id, int *place_id)
2144 FUNC_ENTRANCE_SERVER;
2145 sqlite3_stmt *state = NULL;
2146 int ret = SQLITE_OK;
2147 const char *tail = NULL;
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;
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;
2166 *place_id = sqlite3_column_int(state, 0);
2168 sqlite3_reset(state);
2169 sqlite3_finalize(state);
2170 sqlite3_free(query);
2172 return FENCE_ERR_NONE;
2176 * This function get running status from DB.
2178 * @param[in] fence_id
2180 * @return FENCE_ERR_NONE on success, negative values for errors
2182 int geofence_manager_get_running_status(int fence_id, int *running_status)
2184 FUNC_ENTRANCE_SERVER;
2185 sqlite3_stmt *state = NULL;
2186 int ret = SQLITE_OK;
2187 const char *tail = NULL;
2189 char *query = sqlite3_mprintf("SELECT running_status FROM GeoFence where fence_id = %d;", fence_id);
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;
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;
2207 *running_status = sqlite3_column_int(state, 0);
2209 sqlite3_reset(state);
2210 sqlite3_finalize(state);
2211 sqlite3_free(query);
2213 return FENCE_ERR_NONE;
2217 * This function set running state on DB.
2219 * @param[in] fence_id
2221 * @return FENCE_ERR_NONE on success, negative values for errors
2223 int geofence_manager_set_running_status(int fence_id, int running_status)
2225 FUNC_ENTRANCE_SERVER;
2226 sqlite3_stmt *state;
2227 int ret = SQLITE_OK;
2230 char *query = sqlite3_mprintf("UPDATE GeoFence SET running_status = %d where fence_id = %d;", running_status, fence_id);
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;
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;
2247 sqlite3_finalize(state);
2248 sqlite3_free(query);
2249 return FENCE_ERR_NONE;
2253 * This function get direction type from DB.
2255 * @param[in] fence_id
2256 * @param[in] direction
2257 * @return FENCE_ERR_NONE on success, negative values for errors
2259 int geofence_manager_get_direction(int fence_id, geofence_direction_e *direction)
2261 FUNC_ENTRANCE_SERVER;
2262 sqlite3_stmt *state = NULL;
2263 int ret = SQLITE_OK;
2264 const char *tail = NULL;
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;
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;
2283 *direction = sqlite3_column_int(state, 0);
2285 sqlite3_finalize(state);
2286 sqlite3_free(query);
2288 return FENCE_ERR_NONE;
2292 * This function set direction type on DB.
2294 * @param[in] fence_id
2295 * @param[in] direction
2296 * @return FENCE_ERR_NONE on success, negative values for errors
2298 int geofence_manager_set_direction(int fence_id, geofence_direction_e direction)
2300 FUNC_ENTRANCE_SERVER;
2301 sqlite3_stmt *state;
2302 int ret = SQLITE_OK;
2305 char *query = sqlite3_mprintf("UPDATE GeoFence SET direction = %d where fence_id = %d;", direction, fence_id);
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;
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;
2322 sqlite3_finalize(state);
2323 sqlite3_free(query);
2325 return FENCE_ERR_NONE;
2329 * This function remove fence from DB.
2331 * @param[in] fence_id
2332 * @return FENCE_ERR_NONE on success, negative values for errors
2334 int geofence_manager_delete_fence_info(int fence_id)
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;
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");
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");
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");
2363 * This function remove place from DB.
2365 * @param[in] place_id
2366 * @return FENCE_ERR_NONE on success, negative values for errors
2368 int geofence_manager_delete_place_info(int place_id)
2370 FUNC_ENTRANCE_SERVER;
2371 g_return_val_if_fail(place_id, FENCE_ERR_INVALID_PARAMETER);
2372 int ret = FENCE_ERR_NONE;
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");
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");
2390 * This function close DB handle.
2392 * @param[in] fence_id
2393 * @return FENCE_ERR_NONE on success, negative values for errors
2395 int geofence_manager_close_db(void)
2397 FUNC_ENTRANCE_SERVER;
2398 int ret = SQLITE_OK;
2400 if (db_info_s.handle == NULL)
2401 return FENCE_ERR_NONE;
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;
2409 return FENCE_ERR_NONE;
2413 * This function deletes all data on db.
2415 * @return FENCE_ERR_NONE on success, negative values for errors
2417 int geofence_manager_reset(void)
2419 FUNC_ENTRANCE_SERVER;
2420 sqlite3_stmt *state = NULL;
2421 int ret = SQLITE_OK;
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");
2429 char *query_two = sqlite3_mprintf("DELETE from %Q;", menu_table[FENCE_MAIN_TABLE]);
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;
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;
2445 sqlite3_reset(state);
2446 sqlite3_finalize(state);
2447 sqlite3_free(query_two);
2449 char *query_three = sqlite3_mprintf("UPDATE sqlite_sequence SET seq = 0 where name = %Q;", menu_table[FENCE_MAIN_TABLE]);
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;
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;
2466 sqlite3_reset(state);
2467 sqlite3_finalize(state);
2468 sqlite3_free(query_three);
2469 return FENCE_ERR_NONE;
2473 * This function copy source wifi info to dest wifi info.
2475 * @param[in] src_wifi
2476 * @param[out] dest_wifi
2477 * @return FENCE_ERR_NONE on success, negative values for errors
2479 int geofence_manager_copy_wifi_info(wifi_info_s *src_wifi, wifi_info_s **dest_wifi)
2481 FUNC_ENTRANCE_SERVER;
2482 g_return_val_if_fail(src_wifi, FENCE_ERR_INVALID_PARAMETER);
2484 *dest_wifi = (wifi_info_s *)g_malloc0(sizeof(wifi_info_s));
2485 g_return_val_if_fail(*dest_wifi, -1);
2487 g_strlcpy((*dest_wifi)->bssid, src_wifi->bssid, WLAN_BSSID_LEN);
2489 return FENCE_ERR_NONE;
2493 * This function create a wifi infor .
2495 * @param[in] fence_id
2497 * @param[out] wifi info
2498 * @return FENCE_ERR_NONE on success, negative values for errors
2500 int geofence_manager_create_wifi_info(int fence_id, char *bssid, wifi_info_s **new_wifi)
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);
2506 *new_wifi = (wifi_info_s *)g_malloc0(sizeof(wifi_info_s));
2507 g_strlcpy((*new_wifi)->bssid, bssid, WLAN_BSSID_LEN);
2509 return FENCE_ERR_NONE;
2513 * This function get fence id count by params such as app id and fence type and enable status .
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
2521 int geofence_manager_get_count_by_params(const char *app_id, geofence_type_e fence_type, int *count)
2523 FUNC_ENTRANCE_SERVER;
2524 sqlite3_stmt *state = NULL;
2525 int ret = SQLITE_OK;
2526 const char *tail = NULL;
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);
2533 query = sqlite3_mprintf("SELECT COUNT(fence_id) FROM GeoFence ;");
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);
2539 query = sqlite3_mprintf("SELECT COUNT(fence_id) FROM GeoFence where app_id = %Q ;", app_id);
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;
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;
2559 *count = sqlite3_column_int(state, 0);
2562 LOGI_GEOFENCE("ERROR: count = %d", *count);
2563 sqlite3_finalize(state);
2564 sqlite3_free(query);
2565 return FENCE_ERR_COUNT;
2567 LOGI_GEOFENCE("count[%d]", *count);
2570 sqlite3_reset(state);
2571 sqlite3_finalize(state);
2572 sqlite3_free(query);
2573 return FENCE_ERR_NONE;
2577 app_id == NULL : All, geofence_type_e : INVALID - all, IN enable_status : enable, disable or both. Output : a list of geofence_id
2579 int geofence_manager_get_fences(const char *app_id, geofence_type_e fence_type, GList **fences)
2581 FUNC_ENTRANCE_SERVER;
2582 sqlite3_stmt *state = NULL;
2583 int ret = SQLITE_OK;
2584 const char *tail = NULL;
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.");
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);
2600 query = sqlite3_mprintf("SELECT fence_id FROM GeoFence;");
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);
2606 query = sqlite3_mprintf("SELECT fence_id FROM GeoFence where app_id = %Q;", app_id);
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;
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));
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));
2628 sqlite3_reset(state);
2629 sqlite3_finalize(state);
2630 sqlite3_free(query);
2631 return FENCE_ERR_NONE;
2634 int geofence_manager_get_count_of_fences(int *count)
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;");
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;
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;
2657 *count = sqlite3_column_int(state, 0);
2660 LOGI_GEOFENCE("ERROR: count = %d", *count);
2661 sqlite3_finalize(state);
2662 sqlite3_free(query);
2663 return FENCE_ERR_COUNT;
2665 LOGI_GEOFENCE("count[%d]", *count);
2668 sqlite3_reset(state);
2669 sqlite3_finalize(state);
2670 sqlite3_free(query);
2671 return FENCE_ERR_NONE;
2674 int geofence_manager_get_place_count_by_placeid(int place_id, int *count)
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);
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;
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;
2697 *count = sqlite3_column_int(state, 0);
2700 LOGI_GEOFENCE("ERROR: place count = %d", *count);
2701 sqlite3_finalize(state);
2702 sqlite3_free(query);
2703 return FENCE_ERR_COUNT;
2705 LOGI_GEOFENCE("place count[%d]", *count);
2708 sqlite3_reset(state);
2709 sqlite3_finalize(state);
2710 sqlite3_free(query);
2711 return FENCE_ERR_NONE;