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")
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;
#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;
<arg type="i" name="result" direction="out"/>
</method>
<method name="Eject">
+ <arg type="s" name="group_name" direction="in"/>
<arg type="s" name="uuid" direction="in"/>
<arg type="i" name="result" direction="out" />
</method>
#include <gio/gio.h>
#include <companion_gdbus.h>
#include <sys/types.h>
+#include <sqlite3.h>
#ifdef __cplusplus
extern "C"
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();
--- /dev/null
+/*
+ * 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
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,
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,
/* 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);
#include <comp_group.h>
#include <comp_resource.h>
#include <comp_gdbus.h>
+#include <comp_db.h>
#endif /* __COMP_MANAGER_H__ */
--- /dev/null
+/*
+ * 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 <comp_db.h>
+#include <comp_context.h>
+#include <comp_group.h>
+
+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;
+}
+
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);
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;
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
#include <comp_group.h>
#include <comp_mot_agent.h>
#include <comp_gdbus_group.h>
+#include <comp_db.h>
GList *found_group_list;
GList *mot_enb_dev_list = 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;
}
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;
}
}
+ 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();
}
group->device_list = g_list_append(group->device_list, strdup(uuid));
+ comp_db_insert(group_name, uuid);
+
__print_groups_information();
return 0;
}
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);
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;
#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();
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;
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) {
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));
<arg type="i" name="result" direction="out"/>
</method>
<method name="Eject">
+ <arg type="s" name="group_name" direction="in"/>
<arg type="s" name="uuid" direction="in"/>
<arg type="i" name="result" direction="out" />
</method>