From: Jihoon Jung Date: Mon, 2 Apr 2018 05:33:03 +0000 (+0900) Subject: Add db implementation X-Git-Tag: submit/tizen/20190131.065036~66 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=ebf8facbccc519dcd29f00c211ae1ef4100038ab;p=platform%2Fcore%2Fapi%2Fmulti-device-group.git Add db implementation Signed-off-by: Jihoon Jung --- diff --git a/CMakeLists.txt b/CMakeLists.txt index 9f51414..63e73f8 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -11,7 +11,7 @@ INCLUDE_DIRECTORIES(${CMAKE_SOURCE_DIR}/src/include) INCLUDE_DIRECTORIES(${CMAKE_SOURCE_DIR}/capi/include) MESSAGE(" - Checking...packages dependency") -SET(COMMON_DEPS glib-2.0 gio-2.0 gio-unix-2.0 dlog dbus-1 iotivity uuid capi-base-common capi-system-info vconf boost gmock) +SET(COMMON_DEPS glib-2.0 gio-2.0 gio-unix-2.0 dlog dbus-1 iotivity uuid capi-base-common capi-system-info vconf boost gmock sqlite3) IF (LINUX) PKG_CHECK_MODULES(daemon_pkgs REQUIRED ${COMMON_DEPS}) ADD_DEFINITIONS("-DLINUX") diff --git a/capi/src/companion.c b/capi/src/companion.c old mode 100644 new mode 100755 index 025d600..91dd6ab --- a/capi/src/companion.c +++ b/capi/src/companion.c @@ -663,6 +663,9 @@ EXPORT_API int companion_device_eject(companion_h handle, companion_device_s *dev = (companion_device_s *)device; companion_check_null_ret_error("dev", dev, COMP_ERROR_INVALID_PARAMETER); + companion_group_s *grp = (companion_group_s *)group; + companion_check_null_ret_error("grp", grp, COMP_ERROR_INVALID_PARAMETER); + _handle->device_eject_result_cb.result_cb = result_cb; _handle->device_eject_result_cb.user_data = user_data; @@ -671,7 +674,7 @@ EXPORT_API int companion_device_eject(companion_h handle, #if 0 group_call_device_eject_sync(group_proxy, dev->device_id, &ret, NULL, &error); #else - group_call_eject_sync(_handle->group_proxy, dev->device_id, &ret, NULL, &error); + group_call_eject_sync(_handle->group_proxy, grp->group_name, dev->device_id, &ret, NULL, &error); #endif return ret; diff --git a/capi/src/companion_gdbus.xml b/capi/src/companion_gdbus.xml index d72ef50..34705c4 100644 --- a/capi/src/companion_gdbus.xml +++ b/capi/src/companion_gdbus.xml @@ -46,6 +46,7 @@ + diff --git a/src/companion-manager/include/comp_context.h b/src/companion-manager/include/comp_context.h index e1c9c5c..81b5b11 100644 --- a/src/companion-manager/include/comp_context.h +++ b/src/companion-manager/include/comp_context.h @@ -24,6 +24,7 @@ #include #include #include +#include #ifdef __cplusplus extern "C" @@ -41,6 +42,9 @@ typedef struct { bool mot_me; /**< Indicator whether mot local comp-manager */ GList *grp_list; GList *pairwise_list; + + //sqlite3 db + sqlite3 *db; } comp_context_t; int comp_context_create(); diff --git a/src/companion-manager/include/comp_db.h b/src/companion-manager/include/comp_db.h new file mode 100755 index 0000000..1156237 --- /dev/null +++ b/src/companion-manager/include/comp_db.h @@ -0,0 +1,30 @@ +/* + * Copyright (c) 2016 Samsung Electronics Co., Ltd. All rights reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +#ifndef __COMP_DB_H__ +#define __COMP_DB_H__ + +#define COMP_DB_FILE "/opt/usr/dbspace/.comp.db" +#define COMP_DB_TABLE "comp_group_information" + +int comp_db_init(); +int comp_db_deinit(); +int comp_db_insert(char *group_name, char *device_id); +int comp_db_delete_group(char *group_name); +int comp_db_delete_device(char *group_name, char *device_id); + +#endif diff --git a/src/companion-manager/include/comp_gdbus_group.h b/src/companion-manager/include/comp_gdbus_group.h index ebb8fe0..f62c17d 100644 --- a/src/companion-manager/include/comp_gdbus_group.h +++ b/src/companion-manager/include/comp_gdbus_group.h @@ -59,7 +59,7 @@ gboolean group_device_eject(Group *group, GDBusMethodInvocation *invocation, gboolean group_invite(Group *group, GDBusMethodInvocation *invocation, gchar *group_name, gchar *uuid, gchar *pin, gpointer user_data); -gboolean group_eject(Group *group, GDBusMethodInvocation *invocation, +gboolean group_eject(Group *group, GDBusMethodInvocation *invocation, gchar *group_name, gchar *uuid, gpointer user_data); gboolean group_get_remote_device(Group *group, GDBusMethodInvocation *invocation, diff --git a/src/companion-manager/include/comp_group.h b/src/companion-manager/include/comp_group.h old mode 100644 new mode 100755 index 468c337..aea6b79 --- a/src/companion-manager/include/comp_group.h +++ b/src/companion-manager/include/comp_group.h @@ -95,8 +95,8 @@ comp_group_invite_info_t *comp_group_get_invite_info(); void comp_group_free_invite_info(); void comp_group_notify_group_invite(int result); -/* dismiss from group (async) */ -int comp_group_dismiss(gchar *uuid_dev1, const char *uuid_dev2); +/* eject from group (async) */ +int comp_group_eject(const gchar *group_name, gchar *uuid_dev1, const char *uuid_dev2); void comp_group_notify_group_dismiss(int result); int comp_group_pair_resource(char* target1, char *subject1, char *uri1, @@ -133,6 +133,7 @@ int comp_group_get_group_device_id(comp_group_t *handle); /* notify */ int comp_group_event_handler(/*callback*/); // event hadnler int comp_group_add_device_in_group(const char *group_name, const char *uuid); +int comp_group_remove_device_in_group(char *group_name, char *uuid); /* Group management */ int comp_group_request_create_group(char *uuid, char *group_name); diff --git a/src/companion-manager/include/comp_manager.h b/src/companion-manager/include/comp_manager.h index b090179..c7f0d11 100644 --- a/src/companion-manager/include/comp_manager.h +++ b/src/companion-manager/include/comp_manager.h @@ -22,5 +22,6 @@ #include #include #include +#include #endif /* __COMP_MANAGER_H__ */ diff --git a/src/companion-manager/src/comp_db.c b/src/companion-manager/src/comp_db.c new file mode 100755 index 0000000..3849dbb --- /dev/null +++ b/src/companion-manager/src/comp_db.c @@ -0,0 +1,345 @@ +/* + * Copyright (c) 2016 Samsung Electronics Co., Ltd. All rights reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +#include +#include +#include + +static bool __is_table_existing(const char *table) +{ + bool result; + char *sql; + int ret; + + comp_context_t *comp_ctx = comp_context_get_context(); + + sql = sqlite3_mprintf("SELECT count(*) FROM sqlite_master WHERE type='table' AND name ='%s';", table); + if (sql != NULL) { + sqlite3_stmt *stmt = NULL; + + ret = sqlite3_prepare_v2(comp_ctx->db, sql, strlen(sql), &stmt, NULL); + if (ret == SQLITE_OK) { + ret = sqlite3_step(stmt); + if (ret == SQLITE_ROW) { + int count; + + count = sqlite3_column_int(stmt, 0); + if (count > 0) + result = true; + else + result = false; + } else { + LOG_ERR("sqlite3_step failed, [%d:%s]", ret, sqlite3_errmsg(comp_ctx->db)); + + result = false; + } + + sqlite3_finalize(stmt); + } else { + LOG_ERR("sqlite3_prepare_v2 failed, [%d:%s]", ret, sqlite3_errmsg(comp_ctx->db)); + + result = false; + } + + sqlite3_free(sql); + } else { + LOG_ERR("sqlite3_mprintf failed"); + + result = false; + } + + return result; +} + +static int __create_table() +{ + int ret = 0; + int sql_ret; + char *sql = NULL; + char *error = NULL; + + comp_context_t *comp_ctx = comp_context_get_context(); + + if (comp_ctx->db == NULL) { + LOG_ERR("comp_ctx->db is NULL"); + return -1; + } + + sql = sqlite3_mprintf("CREATE TABLE %s(id INTEGER PRIMARY KEY, group_name TEXT, device_id TEXT);", + COMP_DB_TABLE); + + if (sql != NULL) { + sql_ret = sqlite3_exec(comp_ctx->db, sql, NULL, NULL, &error); + if (sql_ret != SQLITE_OK) { + LOG_ERR("sqlite3_exec failed, %d, %s", sql_ret, error); + ret = -1; + sqlite3_free(error); + } + sqlite3_free(sql); + } else { + LOG_ERR("sql is NULL"); + ret = -1; + } + + return ret; +} + +static void __prepare_table() +{ + if (__is_table_existing(COMP_DB_TABLE) == false) + __create_table(); +} + +static int __check_integrity() +{ + sqlite3_stmt *stmt = NULL; + int result = 0; + + comp_context_t *comp_ctx = comp_context_get_context(); + + if (comp_ctx->db == 0) { + LOG_ERR("failed to check handle"); + return -1; + } + + int ret = sqlite3_prepare_v2(comp_ctx->db, "PRAGMA integrity_check", -1, &stmt, NULL); + if (ret != SQLITE_OK && ret != SQLITE_BUSY) { + LOG_ERR("failed to check integrity: %s", sqlite3_errmsg(comp_ctx->db)); + return -1; + } else { + ret = sqlite3_step(stmt); + if (ret == SQLITE_ROW) { + const char *ret_val = (const char *)sqlite3_column_text(stmt, 0); + LOG_ERR("ret_val: %s", ret_val); + if (ret_val && strcmp(ret_val, "ok") == 0) { + result = 0; + } else { + LOG_ERR("failed to check integrity"); + result = -1; + } + } else { + result = 0; + } + } + + sqlite3_finalize(stmt); + + return result; +} + +int comp_db_init() +{ + int ret = 0; + int sql_ret; + char *error = NULL; + + comp_context_t *comp_ctx = comp_context_get_context(); + + if (comp_ctx->db == NULL) { + sql_ret = sqlite3_open_v2(COMP_DB_FILE, &(comp_ctx->db), + SQLITE_OPEN_FULLMUTEX | SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, NULL); + if (sql_ret != SQLITE_OK) { + LOG_ERR("sqlite3_open_v2 failed, (%d)", sql_ret); + goto ERR; + } + + if (__check_integrity() < 0) { + LOG_ERR("Failed to check integrity"); + unlink(COMP_DB_FILE); + comp_ctx->db = NULL; + sql_ret = sqlite3_open_v2(COMP_DB_FILE, &comp_ctx->db, SQLITE_OPEN_NOMUTEX | SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, NULL); + if (sql_ret != SQLITE_OK) { + LOG_ERR("sqlite3_open_v2 failed, (%d)", sql_ret); + goto ERR; + } + } + + /* Enable persist journal mode */ + sql_ret = sqlite3_exec(comp_ctx->db, "PRAGMA journal_mode = PERSIST", NULL, NULL, &error); + if (sql_ret != SQLITE_OK) { + LOG_ERR("Fail to change journal mode: (%d) %s", sql_ret, error); + sqlite3_free(error); + + goto ERR; + } + + __prepare_table(); + } + + if (comp_ctx->db != NULL) { + //iot activity per each column + LOG_ERR("comp db init"); + //comp_group_create(group_name); + } + + return ret; + +ERR: + if (comp_ctx->db != NULL) { + sql_ret = sqlite3_close(comp_ctx->db); + if (sql_ret == SQLITE_OK) + comp_ctx->db = NULL; + else + LOG_ERR("sqlite3_close failed, [%d]", sql_ret); + } + + return ret; +} +int comp_db_deinit() +{ + int ret = 0; + int sql_ret; + + comp_context_t *comp_ctx = comp_context_get_context(); + + if (comp_ctx->db != NULL) { + sql_ret = sqlite3_close(comp_ctx->db); + if (sql_ret != SQLITE_OK) { + LOG_ERR("sqlite3_close failed, %d", sql_ret); + return -1; + } + comp_ctx->db = NULL; + } + + return ret; +} + +int comp_db_insert(char *group_name, char *device_id) +{ + int ret = 0; + int sql_ret; + char *sql = NULL; + char *error = NULL; + + comp_context_t *comp_ctx = comp_context_get_context(); + + if (comp_ctx->db == NULL) { + LOG_ERR("mtp_ctx->db is NULL"); + return -1; + } + + sql = sqlite3_mprintf("INSERT INTO %s (group_name, device_id) values(%Q, %Q);", + COMP_DB_TABLE, + group_name, + device_id); + + if (sql != NULL) { + sql_ret = sqlite3_exec(comp_ctx->db, sql, NULL, NULL, &error); + if (sql_ret != SQLITE_OK) { + LOG_ERR("sqlite3_exec failed, %d, %s", sql_ret, error); + ret = -1; + sqlite3_free(error); + } + sqlite3_free(sql); + } else { + LOG_ERR("sql is NULL"); + ret = -1; + } + + return ret; +} + +int comp_db_delete_group(char *group_name) +{ + int ret = 0; + int sql_ret; + char *sql = NULL; + char *error = NULL; + + comp_context_t *comp_ctx = comp_context_get_context(); + + if (comp_ctx->db == NULL) { + LOG_ERR("comp_ctx->db is NULL"); + return -1; + } + + if (group_name == NULL) { + LOG_ERR("group_name is NULL"); + return -1; + } + + sql = sqlite3_mprintf("DELETE FROM %s WHERE group_name=%Q", + COMP_DB_TABLE, + group_name); + + if (sql != NULL) { + sql_ret = sqlite3_exec(comp_ctx->db, sql, NULL, NULL, &error); + if (sql_ret != SQLITE_OK) { + LOG_ERR("sqlite3_exec failed, %d, %s", sql_ret, error); + ret = -1; + sqlite3_free(error); + } + sqlite3_free(sql); + } else { + LOG_ERR("sql is NULL"); + ret = -1; + } + + return ret; +} + +int comp_db_delete_device(char *group_name, char *device_id) +{ + int ret = 0; + int sql_ret; + char *sql = NULL; + char *error = NULL; + + comp_context_t *comp_ctx = comp_context_get_context(); + + if (comp_ctx->db == NULL) { + LOG_ERR("comp_ctx->db is NULL"); + return -1; + } + + if (group_name == NULL) { + LOG_ERR("group_name is NULL"); + return -1; + } + + if (device_id == NULL) { + LOG_ERR("device_id is NULL"); + return -1; + } + + sql = sqlite3_mprintf("DELETE FROM %s WHERE group_name=%Q and device_id=%Q", + COMP_DB_TABLE, + group_name, + device_id); + + if (sql != NULL) { + sql_ret = sqlite3_exec(comp_ctx->db, sql, NULL, NULL, &error); + if (sql_ret != SQLITE_OK) { + LOG_ERR("sqlite3_exec failed, %d, %s", sql_ret, error); + ret = -1; + sqlite3_free(error); + } + sqlite3_free(sql); + } else { + LOG_ERR("sql is NULL"); + ret = -1; + } + + return ret; + +} + +int comp_db_print() +{ + return 0; +} + diff --git a/src/companion-manager/src/comp_gdbus_group.c b/src/companion-manager/src/comp_gdbus_group.c index 21ab0c6..0f992fa 100644 --- a/src/companion-manager/src/comp_gdbus_group.c +++ b/src/companion-manager/src/comp_gdbus_group.c @@ -128,7 +128,7 @@ gboolean group_device_eject(Group *group, GDBusMethodInvocation *invocation, LOG_DEBUG("group device eject called using dbus successful"); - result = comp_group_dismiss(uuid_dev1, uuid_dev2); + result = comp_group_eject(NULL, uuid_dev1, uuid_dev2); group_complete_device_eject(group, invocation, result); @@ -154,7 +154,7 @@ gboolean group_invite(Group *group, GDBusMethodInvocation *invocation, gchar *gr return TRUE; } -gboolean group_eject(Group *group, GDBusMethodInvocation *invocation, +gboolean group_eject(Group *group, GDBusMethodInvocation *invocation, gchar *group_name, gchar *uuid, gpointer user_data) { gint result = 0; @@ -165,7 +165,7 @@ gboolean group_eject(Group *group, GDBusMethodInvocation *invocation, return FALSE; } - result = comp_group_dismiss(comp_ctx->device_uuid, uuid); + result = comp_group_eject(group_name, comp_ctx->device_uuid, uuid); /* Now, for the sake of convenience, we change 'group_complete_eject' to 'group_complete_device_eject'. */ #if 0 diff --git a/src/companion-manager/src/comp_group.c b/src/companion-manager/src/comp_group.c old mode 100644 new mode 100755 index 533dce9..a09bc89 --- a/src/companion-manager/src/comp_group.c +++ b/src/companion-manager/src/comp_group.c @@ -19,6 +19,7 @@ #include #include #include +#include GList *found_group_list; GList *mot_enb_dev_list = NULL; @@ -30,6 +31,11 @@ comp_mot_device_t *my_device = NULL; int comp_group_initialize() { //Initialize memory of context + //1. group creation + //db select + //2. device creation + //db select by group + return COMP_ERROR_NONE; } @@ -70,10 +76,6 @@ int comp_group_create(const char* name) LOG_BEGIN(); - ret = comp_iot_add_resource(COMP_RESOURCE_TYPE_GROUP, name); - if (ret != COMP_ERROR_NONE) - LOG_ERR("Add resource is failed : %s", comp_log_get_error_string(ret)); - comp_context_t *comp_ctx = comp_context_get_context(); comp_check_null_ret_error("comp_ctx", comp_ctx, COMP_ERROR_INVALID_PARAMETER); GList *list = comp_ctx->grp_list; @@ -88,11 +90,17 @@ int comp_group_create(const char* name) } } + ret = comp_iot_add_resource(COMP_RESOURCE_TYPE_GROUP, name); + if (ret != COMP_ERROR_NONE) { + LOG_ERR("Add resource is failed : %s", comp_log_get_error_string(ret)); + } + comp_grp_t *grp = g_new0(comp_grp_t, 1); grp->name = strdup(name); comp_ctx->grp_list = g_list_append(comp_ctx->grp_list, grp); + comp_group_add_device_in_group(name, comp_ctx->device_uuid); comp_resource_print_list(); __print_groups_information(); @@ -479,6 +487,8 @@ int comp_group_add_device_in_group(const char *group_name, const char *uuid) } group->device_list = g_list_append(group->device_list, strdup(uuid)); + comp_db_insert(group_name, uuid); + __print_groups_information(); return 0; } @@ -547,7 +557,7 @@ void comp_group_notify_group_dismiss(int result) notify_group_device_eject_result(result); } -int comp_group_remove_device_in_group(const char *uuid) +int comp_group_remove_device_in_group(char *group_name, char *uuid) { comp_context_t *comp_ctx = comp_context_get_context(); comp_check_null_ret_error("comp_ctx", comp_ctx, -1); @@ -558,27 +568,22 @@ int comp_group_remove_device_in_group(const char *uuid) iter = comp_ctx->grp_list; while (iter != NULL) { comp_grp_t *group = (comp_grp_t *)iter->data; + if (strcmp(group_name, group->name) == 0) { + GList *device_iter = group->device_list; - /* Find the device in devices list */ - GList *device_iter = group->device_list; - while (device_iter != NULL) { - char *device_uuid = (char *)device_iter->data; - /* Lastly, we find the device in the group */ - if (strcmp(device_uuid, uuid) == 0) { - /* Remove device id in devices list */ - g_list_free_full(group->device_list, _free_device_func); - } - device_iter = g_list_next(device_iter); + device_iter = g_list_remove_all(device_iter, uuid); } + iter = g_list_next(iter); } + comp_db_delete_device(group_name, uuid); __print_groups_information(); return 0; } //dismiss from group (async) -int comp_group_dismiss(gchar *uuid_dev1, const char *uuid_dev2) +int comp_group_eject(const gchar *group_name, gchar *uuid_dev1, const gchar *uuid_dev2) { int ret; @@ -593,7 +598,7 @@ int comp_group_dismiss(gchar *uuid_dev1, const char *uuid_dev2) #else ret = agent_unpair(uuid_dev1, uuid_dev2); #endif - comp_group_remove_device_in_group(uuid_dev2); + comp_group_remove_device_in_group(group_name, uuid_dev2); LOG_END(); diff --git a/src/companion-manager/src/comp_iot.cpp b/src/companion-manager/src/comp_iot.cpp old mode 100644 new mode 100755 index 00d0174..1f50e06 --- a/src/companion-manager/src/comp_iot.cpp +++ b/src/companion-manager/src/comp_iot.cpp @@ -471,7 +471,7 @@ OCEntityHandlerResult _request_handler(std::shared_ptr reques group_name.c_str(),comp_ctx->device_uuid, uuid.c_str()); - result = comp_group_dismiss(comp_ctx->device_uuid, + result = comp_group_eject(group_name.c_str(), comp_ctx->device_uuid, uuid.c_str()); arg = g_strdup(uuid.c_str()); break; diff --git a/src/companion-manager/src/comp_manager.c b/src/companion-manager/src/comp_manager.c index ddc8c67..c094a09 100644 --- a/src/companion-manager/src/comp_manager.c +++ b/src/companion-manager/src/comp_manager.c @@ -31,13 +31,20 @@ int main(int argc, char *argv[]) goto EXIT; } - //3. iotivity iniatlize + //2. iotivity iniatlize ret = comp_iot_initialize(); if (ret != COMP_ERROR_NONE) { LOG_ERR("COMP iot initialize failed : %s", comp_log_get_error_string(ret)); goto EXIT; } + //3. Initializing Data + ret = comp_db_init(); + if (ret != COMP_ERROR_NONE) { + LOG_ERR("COMP db initialize failed : %s", comp_log_get_error_string(ret)); + goto EXIT; + } + //4. dbus interface initialize ret = comp_gdbus_init(); if (ret != COMP_ERROR_NONE) { @@ -83,6 +90,10 @@ EXIT: if (ret != COMP_ERROR_NONE) LOG_ERR("COMP group deinitialize failed : %s", comp_log_get_error_string(ret)); + ret = comp_db_deinit(); + if (ret != COMP_ERROR_NONE) + LOG_ERR("COMP db deinitialize failed : %s", comp_log_get_error_string(ret)); + ret = comp_iot_deinitialize(); if (ret != COMP_ERROR_NONE) LOG_ERR("COMP iot deinitialize failed : %s", comp_log_get_error_string(ret)); diff --git a/src/companion-manager/src/companion_gdbus.xml b/src/companion-manager/src/companion_gdbus.xml index d72ef50..34705c4 100644 --- a/src/companion-manager/src/companion_gdbus.xml +++ b/src/companion-manager/src/companion_gdbus.xml @@ -46,6 +46,7 @@ +