Add a routine to check if it is stored in the DB before the invite /
authorsaerome.kim <saerome.kim@samsung.com>
Fri, 27 Apr 2018 02:28:42 +0000 (11:28 +0900)
committersaerome.kim <saerome.kim@samsung.com>
Mon, 2 Jul 2018 10:39:37 +0000 (19:39 +0900)
eject execution.

Signed-off-by: saerome.kim <saerome.kim@samsung.com>
packaging/d2d-manager.spec [changed mode: 0755->0644]
src/mdg-manager/include/mdgd_db.h [changed mode: 0755->0644]
src/mdg-manager/src/mdgd_db.c [changed mode: 0755->0644]
src/mdg-manager/src/mdgd_gdbus.c
src/mdg-manager/src/mdgd_gdbus_group.c
src/mdg-manager/src/mdgd_iot.cpp [changed mode: 0644->0755]
src/mot-agent/ma-service-interface.c
src/mot-agent/ma-subowner.c
src/mot-agent/ma-util.c
src/mot-agent/ma-util.h

old mode 100755 (executable)
new mode 100644 (file)
index 52d06a7..f9cf423
@@ -111,6 +111,8 @@ cp %{SOURCE8} %{buildroot}%{_unitdir}/ma.service
 %postun
 rm -rf %{NETWORK_FW_DATADIR}/*.db
 rm -rf %{NETWORK_FW_DATADIR}/*.db-journal
+rm -rf /opt/usr/dbspace/.mdgd.db
+rm -rf /opt/usr/dbspace/.mdgd.db-journal
 
 %files
 %manifest %{name}.manifest
old mode 100755 (executable)
new mode 100644 (file)
index 4eb6d32..b9d7e74
@@ -36,6 +36,7 @@ int mdgd_db_device_insert(const char *device_id, const char *group_name);
 int mdgd_db_group_delete(char *group_name);
 int mdgd_db_device_delete(char *device_id, char *group_name);
 bool mdgd_check_device_exist(char *device_id);
+bool mdgd_check_group_exist(const char *group_name);
 
 #ifdef __cplusplus
 }
old mode 100755 (executable)
new mode 100644 (file)
index 22b04dd..33226c0
@@ -416,7 +416,57 @@ bool mdgd_check_device_exist(char *device_id)
 
        mdgd_context_t *mdgd_ctx = mdgd_context_get_context();
 
-       sql = sqlite3_mprintf("SELECT count(*) FROM %s WHERE device_id=%Q;", MDGD_DB_DEVICE_TABLE, device_id);
+       sql = sqlite3_mprintf("SELECT count(*) FROM %s WHERE device_id=%Q;",
+               MDGD_DB_DEVICE_TABLE, device_id);
+       if (sql != NULL) {
+               sqlite3_stmt *stmt = NULL;
+
+               ret = sqlite3_prepare_v2(mdgd_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(mdgd_ctx->db));
+
+                               result = false;
+                       }
+
+                       sqlite3_finalize(stmt);
+               } else {
+                       LOG_ERR("sqlite3_prepare_v2 failed, [%d:%s]", ret, sqlite3_errmsg(mdgd_ctx->db));
+
+                       result = false;
+               }
+
+               sqlite3_free(sql);
+       } else {
+               LOG_ERR("sqlite3_mprintf failed");
+
+               result = false;
+       }
+
+       return result;
+}
+
+bool mdgd_check_group_exist(const char *group_name)
+{
+       int ret = 0;
+       bool result = false;
+       int sql_ret;
+       char *sql = NULL;
+       char *error = NULL;
+
+       mdgd_context_t *mdgd_ctx = mdgd_context_get_context();
+
+       sql = sqlite3_mprintf("SELECT count(*) FROM %s WHERE group_name=%Q;",
+               MDGD_DB_DEVICE_TABLE, group_name);
        if (sql != NULL) {
                sqlite3_stmt *stmt = NULL;
 
index b7f3c42..bedf36f 100644 (file)
@@ -208,7 +208,7 @@ static void _app_conn_destroyed_cb(GDBusConnection *conn, const gchar *Name,
        g_free(data);
 
        if (mdgd_ctx->ref_count == 0) {
-               LOG_DEBUG("No app remaining quit comapnion-manager");
+               LOG_DEBUG("No app remaining quit mdgd");
                g_main_loop_quit(mdgd_ctx->main_loop);
        }
 
index 6f26864..9cc3bc0 100644 (file)
@@ -15,6 +15,7 @@
  *
  */
 
+#include <mdgd_db.h>
 #include <mdgd_gdbus_group.h>
 
 /* GDBUS Group Layer */
 gboolean group_create(Group *group, GDBusMethodInvocation *invocation,
        gchar *group_name, gpointer user_data)
 {
-       gint result = 0;
+       gint result = MDGD_ERROR_NONE;
 
        LOG_DEBUG("group create called using dbus successful");
 
-       result = mdgd_group_create(group_name);
-       if (result == 0)
-               mdgd_db_group_insert(group_name);
+       if (mdgd_check_group_exist(group_name))
+               LOG_DEBUG("Group already created");
+       else {
+               result = mdgd_group_create(group_name);
+               if (MDGD_ERROR_NONE == result)
+                       mdgd_db_group_insert(group_name);
+       }
 
        group_complete_create(group, invocation, result);
 
@@ -39,11 +44,11 @@ gboolean group_create(Group *group, GDBusMethodInvocation *invocation,
 gboolean group_find(Group *group, GDBusMethodInvocation *invocation, gint timeout,
        gpointer user_data)
 {
-       gint result = 0;
+       gint result = MDGD_ERROR_UNKNOWN;
 
        LOG_DEBUG("group find called using dbus successful");
 
-       mdgd_group_find(timeout);
+       result = mdgd_group_find(timeout);
 
        group_complete_find(group, invocation, result);
        return TRUE;
@@ -52,7 +57,7 @@ gboolean group_find(Group *group, GDBusMethodInvocation *invocation, gint timeou
 gboolean group_get_found_groups(Group *group, GDBusMethodInvocation *invocation,
        gpointer user_data)
 {
-       gint result = 0;
+       gint result = MDGD_ERROR_UNKNOWN;
        GVariant *group_data;
 
        LOG_DEBUG("get found groups called using dbus successful");
@@ -66,7 +71,7 @@ gboolean group_get_found_groups(Group *group, GDBusMethodInvocation *invocation,
 gboolean group_join(Group *group, GDBusMethodInvocation *invocation,
        gpointer user_data)
 {
-       gint result = 0;
+       gint result = MDGD_ERROR_UNKNOWN;
 
        LOG_DEBUG("join called using dbus successful");
 
@@ -96,7 +101,7 @@ gboolean group_delete(Group *group, GDBusMethodInvocation *invocation,
 gboolean group_device_find(Group *group, GDBusMethodInvocation *invocation,
        gint timeout, gpointer user_data)
 {
-       int result = 0;
+       int result = MDGD_ERROR_UNKNOWN;
 
        result = mdgd_group_find_mot_enabled_devices(timeout);
 
@@ -110,7 +115,7 @@ gboolean group_device_invite(Group *group, GDBusMethodInvocation *invocation,
        gchar *interface_1, int permission_1, gchar *uri_2, gchar *rt_2,
        gchar *interface_2, int permission_2, gpointer user_data)
 {
-       gint result = 0;
+       gint result = MDGD_ERROR_UNKNOWN;
 
        LOG_DEBUG("group device invite called using dbus successful");
 /*
@@ -126,7 +131,7 @@ gboolean group_device_invite(Group *group, GDBusMethodInvocation *invocation,
 gboolean group_device_eject(Group *group, GDBusMethodInvocation *invocation,
        gchar *uuid_dev1, gchar *uuid_dev2, gpointer user_data)
 {
-       gint result = 0;
+       gint result = MDGD_ERROR_UNKNOWN;
 
        LOG_DEBUG("group device eject called using dbus successful");
 
@@ -140,13 +145,17 @@ 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)
 {
-       gint result = 0;
+       gint result = MDGD_ERROR_UNKNOWN;
 
        LOG_DEBUG("group invite called using dbus successful");
 
-       result = mdgd_group_invite(group_name, uuid, pin);
-       if (result == 0)
-               mdgd_db_device_insert(uuid, group_name);
+       if (mdgd_check_device_exist(uuid)) {
+               result = MDGD_ERROR_ALREADY_REGISTERED;
+       } else {
+               result = mdgd_group_invite(group_name, uuid, pin);
+               if (MDGD_ERROR_NONE == result)
+                       mdgd_db_device_insert(uuid, group_name);
+       }
 
        /* Now, for the sake of convenience, we change 'group_complete_invite' to 'group_complete_device_invite'. */
 #if 0
@@ -161,15 +170,16 @@ gboolean group_invite(Group *group, GDBusMethodInvocation *invocation, gchar *gr
 gboolean group_eject(Group *group, GDBusMethodInvocation *invocation, gchar *group_name,
        gchar *uuid, gpointer user_data)
 {
-       gint result = 0;
+       gint result = MDGD_ERROR_UNKNOWN;
 
        mdgd_context_t *mdgd_ctx = mdgd_context_get_context();
-       if (!mdgd_ctx) {
-               LOG_ERR("ctx is null");
-               return FALSE;
-       }
+       mdgd_check_null_ret_error("mdgd_ctx", mdgd_ctx, FALSE);
 
-       result = mdgd_group_eject(group_name, mdgd_ctx->device_uuid, uuid);
+       if (!mdgd_check_device_exist(uuid)) {
+               result = MDGD_ERROR_ALREADY_REGISTERED;
+       } else {
+               result = mdgd_group_eject(group_name, mdgd_ctx->device_uuid, uuid);
+       }
 
        /* Now, for the sake of convenience, we change 'group_complete_eject' to 'group_complete_device_eject'. */
 #if 0
@@ -203,7 +213,7 @@ gboolean group_pair_resource(Group *group, GDBusMethodInvocation *invocation,
        gchar *uri_2, gchar *rt_2, gchar *interface_2, int permission_2,
        gpointer user_data)
 {
-       int ret = 0;
+       int ret = MDGD_ERROR_UNKNOWN;
 
        LOG_DEBUG("pair resource called using dbus successful");
 
@@ -219,7 +229,7 @@ gboolean group_pair_resource(Group *group, GDBusMethodInvocation *invocation,
 gboolean group_unpair_resource(Group *group, GDBusMethodInvocation *invocation,
        gchar *uuid_dev1, gchar *uuid_dev2, gpointer user_data)
 {
-       int ret = 0;
+       int ret = MDGD_ERROR_UNKNOWN;
 
        LOG_DEBUG("unpair resource called using dbus successful");
 
@@ -234,7 +244,7 @@ gboolean group_send_data(Group *group, GDBusMethodInvocation *invocation,
        gchar *uuid_dev, gchar *addr, int port, GVariant *params,
        gpointer user_data)
 {
-       int ret = 0;
+       int ret = MDGD_ERROR_UNKNOWN;
        int length = 0;
        int len;
        unsigned char *data;
@@ -268,7 +278,7 @@ gboolean group_send_data(Group *group, GDBusMethodInvocation *invocation,
 gboolean group_find_mowned_devices(Group *group,
        GDBusMethodInvocation *invocation, gint timeout, gpointer user_data)
 {
-       int ret = 0;
+       int ret = MDGD_ERROR_UNKNOWN;
 
        LOG_DEBUG("find mot owned devices called using dbus successful");
 
@@ -324,7 +334,7 @@ gboolean group_get_my_uuid(Group *group,
 gboolean group_request_create_group(Group *group, GDBusMethodInvocation
        *invocation, gchar *uuid, gchar *group_name, gpointer user_data)
 {
-       int ret = 0;
+       int ret = MDGD_ERROR_UNKNOWN;
 
        LOG_DEBUG("Request Create Group called using dbus successful");
 
@@ -338,7 +348,7 @@ gboolean group_request_create_group(Group *group, GDBusMethodInvocation
 gboolean group_request_invite(Group *group, GDBusMethodInvocation *invocation,
        gchar *uuid, gchar *group_name, gchar *target_uuid, gchar *PIN, gpointer user_data)
 {
-       int ret = 0;
+       int ret = MDGD_ERROR_UNKNOWN;
 
        LOG_DEBUG("Request Invite called using dbus successful");
 
@@ -352,7 +362,7 @@ gboolean group_request_invite(Group *group, GDBusMethodInvocation *invocation,
 gboolean group_request_eject(Group *group, GDBusMethodInvocation *invocation,
        gchar *uuid, gchar *group_name, gchar *target_uuid, gpointer user_data)
 {
-       int ret = 0;
+       int ret = MDGD_ERROR_UNKNOWN;
 
        LOG_DEBUG("Request Eject called using dbus successful");
 
@@ -366,7 +376,7 @@ gboolean group_request_eject(Group *group, GDBusMethodInvocation *invocation,
 gboolean group_request_delete_group(Group *group,
        GDBusMethodInvocation *invocation, gchar *uuid, gchar *group_name, gpointer user_data)
 {
-       int ret = 0;
+       int ret = MDGD_ERROR_UNKNOWN;
 
        LOG_DEBUG("Request Delete Group called using dbus successful");
 
@@ -381,7 +391,7 @@ gboolean group_start_invited_device_monitor(Group *group,
                                        GDBusMethodInvocation *invocation, int start,
                                        gpointer user_data)
 {
-       int ret = 0;
+       int ret = MDGD_ERROR_UNKNOWN;
 
        LOG_DEBUG("Start Myowned device monitor");
 
old mode 100644 (file)
new mode 100755 (executable)
index 7eee321..cf81b49
@@ -499,11 +499,17 @@ OCEntityHandlerResult _request_handler(std::shared_ptr<OCResourceRequest> reques
                                        case MDGD_REQ_CREATE_GROUP:
                                                LOG_DEBUG("Request create group");
 
+                                               result = MDGD_ERROR_NONE;
+
                                                if (rep.getValue("name", group_name)) {
                                                        LOG_DEBUG("group_name : %s", group_name.c_str());
-                                                       result = mdgd_group_create(group_name.c_str());
-                                                       if (result == 0)
-                                                               mdgd_db_group_insert(group_name.c_str());
+                                                       if (mdgd_check_group_exist(group_name.c_str()))
+                                                               LOG_DEBUG("Group already created");
+                                                       else {
+                                                               result = mdgd_group_create(group_name.c_str());
+                                                               if (result == 0)
+                                                                       mdgd_db_group_insert(group_name.c_str());
+                                                       }
                                                        arg = (unsigned char *) g_strdup(group_name.c_str());
                                                        arg_len = strlen(group_name.c_str());
                                                }
index d1b0266..d680a6a 100644 (file)
@@ -140,7 +140,8 @@ static gboolean _ma_dbus_handle_get_ownerid(NetMa *object,
 
        ret = ma_request_get_ownerid(service, &uuid_str);
        if (MA_ERROR_NONE != ret)
-               MA_LOGE("Failed to ma_request_get_ownerid = %d", ret);
+               MA_LOGE("Failed to ma_request_get_ownerid [%d][%s]",
+                       ret, ma_erro_to_string(ret));
 
        net_ma_complete_get_ownerid(object, invocation, uuid_str, ret);
 
@@ -162,7 +163,8 @@ static gboolean _ma_dbus_handle_disc_mot_enb_devs(NetMa *object,
 
        ret = ma_request_disc_mot_enb_devs(service, timeout);
        if (MA_ERROR_NONE != ret)
-               MA_LOGE("Failed to ma_request_disc_mot_enb_devs !");
+               MA_LOGE("Failed to ma_request_disc_mot_enb_devs [%d][%s]",
+                       ret, ma_erro_to_string(ret));
 
        net_ma_complete_disc_mot_enb_devs(object, invocation, ret);
 
@@ -182,7 +184,8 @@ static gboolean _ma_dbus_handle_disc_mowned_devs(NetMa *object,
 
        ret = ma_request_disc_owned_devs(service, timeout);
        if (MA_ERROR_NONE != ret)
-               MA_LOGE("Failed to ma_request_disc_owned_devs !");
+               MA_LOGE("Failed to ma_request_disc_owned_devs [%d][%s]",
+                       ret, ma_erro_to_string(ret));
 
        net_ma_complete_disc_mowned_devs(object, invocation, ret);
 
@@ -201,7 +204,8 @@ static gboolean _ma_dbus_handle_mot(NetMa *object,
 
        ret = ma_request_mot(service, arg_target, arg_pin);
        if (MA_ERROR_NONE != ret)
-               MA_LOGE("Failed to ma_request_disc_moted_devs !");
+               MA_LOGE("Failed to ma_request_disc_moted_devs [%d][%s]",
+                       ret, ma_erro_to_string(ret));
 
        net_ma_complete_mot(object, invocation, ret);
 
@@ -222,7 +226,8 @@ static gboolean _ma_dbus_handle_acl(NetMa *object,
        ret = ma_request_prov_acl(service, arg_target, arg_subject, arg_uri, arg_rt,
                arg_interface, arg_permission);
        if (MA_ERROR_NONE != ret)
-               MA_LOGE("Failed to ma_request_prov_acl !");
+               MA_LOGE("Failed to ma_request_prov_acl [%d][%s]",
+                       ret, ma_erro_to_string(ret));
 
        net_ma_complete_acl(object, invocation, ret);
 
@@ -241,7 +246,8 @@ static gboolean _ma_dbus_handle_cred(NetMa *object,
 
        ret = ma_request_prov_cred(service, arg_dev1, arg_dev2);
        if (MA_ERROR_NONE != ret)
-               MA_LOGE("Failed to ma_request_prov_cred !");
+               MA_LOGE("Failed to ma_request_prov_cred [%d][%s]",
+                       ret, ma_erro_to_string(ret));
 
        net_ma_complete_cred(object, invocation, ret);
 
@@ -265,7 +271,8 @@ static gboolean _ma_dbus_handle_pairwise(NetMa *object,
                                        interface_1, permission_1, target_2, subject_2, uri_2,
                                        rt_2, interface_2, permission_2);
        if (MA_ERROR_NONE != ret)
-               MA_LOGE("Failed to ma_request_pairwise = %d", ret);
+               MA_LOGE("Failed to ma_request_pairwise [%d][%s]",
+                       ret, ma_erro_to_string(ret));
 
        net_ma_complete_pairwise(object, invocation, ret);
 
@@ -283,7 +290,8 @@ static gboolean _ma_dbus_handle_unlink(NetMa *object,
 
        ret = ma_request_unlink_rsrc(service, uuid_dev1, uuid_dev2);
        if (MA_ERROR_NONE != ret)
-               MA_LOGE("Failed to ma_request_unpair = %d", ret);
+               MA_LOGE("Failed to ma_request_unpair [%d][%s]",
+                       ret, ma_erro_to_string(ret));
 
        net_ma_complete_unpair(object, invocation, ret);
 
@@ -301,7 +309,8 @@ static gboolean _ma_dbus_handle_remove_subowner(NetMa *object,
 
        ret = ma_request_remove_subowner(service, arg_target);
        if (MA_ERROR_NONE != ret)
-               MA_LOGE("Failed to ma_request_remove_subowner !");
+               MA_LOGE("Failed to ma_request_remove_subowner [%d][%s]",
+                       ret, ma_erro_to_string(ret));
 
        net_ma_complete_remove_subowner(object, invocation, ret);
 
@@ -319,7 +328,8 @@ static gboolean _ma_dbus_handle_remove_device(NetMa *object,
 
        ret = ma_request_remove_device(service, arg_target);
        if (MA_ERROR_NONE != ret)
-               MA_LOGE("Failed to ma_request_remove_subowner !");
+               MA_LOGE("Failed to ma_request_remove_subowner [%d][%s]",
+                       ret, ma_erro_to_string(ret));
 
        net_ma_complete_remove_device(object, invocation, ret);
 
@@ -344,7 +354,8 @@ static gboolean _ma_dbus_handle_pair(NetMa *object,
                                        interface_1, permission_1, target_2, subject_2, uri_2,
                                        rt_2, interface_2, permission_2);
        if (MA_ERROR_NONE != ret)
-               MA_LOGE("Failed to ma_request_pair = %d", ret);
+               MA_LOGE("Failed to ma_request_pair [%d][%s]",
+                       ret, ma_erro_to_string(ret));
 
        net_ma_complete_pair(object, invocation, ret);
 
@@ -362,7 +373,8 @@ static gboolean _ma_dbus_handle_unpair(NetMa *object,
 
        ret = ma_request_unpair(service, uuid_owner, uuid_owned);
        if (MA_ERROR_NONE != ret)
-               MA_LOGE("Failed to ma_request_unpair = %d", ret);
+               MA_LOGE("Failed to ma_request_unpair [%d][%s]",
+                       ret, ma_erro_to_string(ret));
 
        net_ma_complete_unpair(object, invocation, ret);
 
index 381454f..7833bd4 100644 (file)
@@ -401,7 +401,7 @@ static gpointer ___notify_found_devs(gpointer data)
 
        while (iter != NULL) {
                OicUuid_t *uuid = &iter->doxm->deviceID;
-               ma_check_null_ret("uuid", uuid);
+               ma_check_null_ret_error("uuid", uuid, NULL);
 
                g_variant_builder_open(&builder, G_VARIANT_TYPE_VARDICT);
 
index e276c29..f059b1a 100644 (file)
@@ -26,6 +26,7 @@
 #include <iotivity_config.h>
 #include <platform_features.h>
 
+#include "ma.h"
 #include "ma-log.h"
 #include "ma-util.h"
 
@@ -120,6 +121,33 @@ const char* ma_wifi_error_to_string(wifi_manager_error_e err)
        }
 }
 
+const char * ma_erro_to_string(ma_error_e err)
+{
+       switch (err) {
+       /* CHECK: List all enum values here */
+       CASE_TO_STR(MA_ERROR_NONE)
+       CASE_TO_STR(MA_ERROR_IO_ERROR)
+       CASE_TO_STR(MA_ERROR_INVALID_PARAMETER)
+       CASE_TO_STR(MA_ERROR_OUT_OF_MEMORY)
+       CASE_TO_STR(MA_ERROR_PERMISSION_DENIED)
+       CASE_TO_STR(MA_ERROR_NOT_SUPPORTED)
+       CASE_TO_STR(MA_ERROR_NO_DATA)
+       CASE_TO_STR(MA_ERROR_OPERATION_FAILED)
+       CASE_TO_STR(MA_ERROR_ALREADY_REGISTERED)
+       CASE_TO_STR(MA_ERROR_IN_PROGRESS)
+       CASE_TO_STR(MA_ERROR_COMM_ERROR)
+       CASE_TO_STR(MA_ERROR_RX)
+       CASE_TO_STR(MA_ERROR_TX)
+       CASE_TO_STR(MA_ERROR_PLUGIN_FAIL)
+       CASE_TO_STR(MA_ERROR_ALREADY_IN_PROGRESS)
+       CASE_TO_STR(MA_ERROR_NOT_STARTED)
+       CASE_TO_STR(MA_ERROR_ALREADY_INITIALIZED)
+       CASE_TO_STR(MA_ERROR_UNKNOWN)
+       default :
+               return "Unknown Error";
+       }
+}
+
 OicUuid_t* ma_convert_uuid(gchar *device_id)
 {
        OicUuid_t *uuid;
index 1bdf540..2ec8b81 100644 (file)
@@ -58,6 +58,7 @@ extern "C"
 
 const char* ma_ocf_error_to_string(OCStackResult err);
 const char* ma_wifi_error_to_string(wifi_manager_error_e err);
+const char * ma_erro_to_string(ma_error_e err);
 OicUuid_t* ma_convert_uuid(gchar *device_id);
 char * ma_get_readable_uuid(const OicUuid_t* uuid);
 #ifdef TEST