Extract dbus message return for reducing duplicated codes 11/277311/1
authorwn.jang <wn.jang@samsung.com>
Tue, 5 Jul 2022 04:38:40 +0000 (13:38 +0900)
committerwn.jang <wn.jang@samsung.com>
Tue, 5 Jul 2022 04:38:40 +0000 (13:38 +0900)
Change-Id: I31a7bdbf6433ef768e4fb65c2a82eee5c14cdfd8

server/sttd_dbus_server.c

index f7a3953..3846c03 100644 (file)
@@ -111,35 +111,19 @@ int sttd_dbus_server_initialize(DBusConnection* conn, DBusMessage* msg)
        return 0;
 }
 
-int sttd_dbus_server_finalize(DBusConnection* conn, DBusMessage* msg)
+static void __sttd_dbus_server_send_method_return(DBusConnection* conn, DBusMessage* msg, int ret)
 {
-       DBusError err;
-       dbus_error_init(&err);
-
-       unsigned int uid;
-       int ret = STTD_ERROR_OPERATION_FAILED;
-       dbus_message_get_args(msg, &err, DBUS_TYPE_UINT32, &uid, DBUS_TYPE_INVALID);
-
-       SLOG(LOG_DEBUG, TAG_STTD, ">>>>> STT Finalize");
-
-       if (dbus_error_is_set(&err)) {
-               SLOG(LOG_ERROR, TAG_STTD, "[IN ERROR] stt finalize : get arguments error (%s)", err.message);
-               dbus_error_free(&err);
-               ret = STTD_ERROR_OPERATION_FAILED;
-       } else {
-               SLOG(LOG_DEBUG, TAG_STTD, "[IN] stt finalize : uid(%u)", uid);
-               ret =  sttd_server_finalize(uid);
-       }
-
        DBusMessage* reply;
-
        reply = dbus_message_new_method_return(msg);
 
        if (NULL != reply) {
-               dbus_message_append_args(reply, DBUS_TYPE_INT32, &ret, DBUS_TYPE_INVALID);
+               /* Append result and language */
+               dbus_message_append_args(reply,
+                       DBUS_TYPE_INT32, &ret,
+                       DBUS_TYPE_INVALID);
 
                if (0 == ret) {
-                       SLOG(LOG_DEBUG, TAG_STTD, "[OUT SUCCESS] Result(%d)", ret);
+                       SLOG(LOG_INFO, TAG_STTD, "[OUT SUCCESS] Result(%d)", ret);
                } else {
                        SLOG(LOG_ERROR, TAG_STTD, "[OUT ERROR] Result(%d)", ret);
                }
@@ -153,6 +137,30 @@ int sttd_dbus_server_finalize(DBusConnection* conn, DBusMessage* msg)
        } else {
                SLOG(LOG_ERROR, TAG_STTD, "[OUT ERROR] Fail to create reply message!!");
        }
+       return;
+}
+
+int sttd_dbus_server_finalize(DBusConnection* conn, DBusMessage* msg)
+{
+       DBusError err;
+       dbus_error_init(&err);
+
+       unsigned int uid;
+       int ret = STTD_ERROR_OPERATION_FAILED;
+       dbus_message_get_args(msg, &err, DBUS_TYPE_UINT32, &uid, DBUS_TYPE_INVALID);
+
+       SLOG(LOG_DEBUG, TAG_STTD, ">>>>> STT Finalize");
+
+       if (dbus_error_is_set(&err)) {
+               SLOG(LOG_ERROR, TAG_STTD, "[IN ERROR] stt finalize : get arguments error (%s)", err.message);
+               dbus_error_free(&err);
+               ret = STTD_ERROR_OPERATION_FAILED;
+       } else {
+               SLOG(LOG_DEBUG, TAG_STTD, "[IN] stt finalize : uid(%u)", uid);
+               ret = sttd_server_finalize(uid);
+       }
+
+       __sttd_dbus_server_send_method_return(conn, msg, ret);
 
        SLOG(LOG_DEBUG, TAG_STTD, "<<<<<");
        SLOG(LOG_DEBUG, TAG_STTD, "  ");
@@ -606,29 +614,7 @@ int sttd_dbus_server_set_private_data(DBusConnection* conn, DBusMessage* msg)
                ret = sttd_server_set_private_data(uid, key, data);
        }
 
-       DBusMessage* reply;
-       reply = dbus_message_new_method_return(msg);
-
-       if (NULL != reply) {
-               dbus_message_append_args(reply,
-                       DBUS_TYPE_INT32, &ret,
-                       DBUS_TYPE_INVALID);
-
-               if (0 == ret) {
-                       SLOG(LOG_DEBUG, TAG_STTD, "[OUT SUCCESS] Result(%d)", ret);
-               } else {
-                       SLOG(LOG_ERROR, TAG_STTD, "[OUT ERROR] Result(%d)", ret);
-               }
-
-               if (!dbus_connection_send(conn, reply, NULL)) {
-                       SLOG(LOG_ERROR, TAG_STTD, "[OUT ERROR] Out Of Memory!");
-               }
-
-               dbus_connection_flush(conn);
-               dbus_message_unref(reply);
-       } else {
-               SLOG(LOG_ERROR, TAG_STTD, "[OUT ERROR] Fail to create reply message!!");
-       }
+       __sttd_dbus_server_send_method_return(conn, msg, ret);
 
        SLOG(LOG_DEBUG, TAG_STTD, "<<<<<");
        SLOG(LOG_DEBUG, TAG_STTD, "  ");
@@ -772,30 +758,7 @@ int sttd_dbus_server_set_start_sound(DBusConnection* conn, DBusMessage* msg)
                ret = sttd_server_set_start_sound(uid, file);
        }
 
-       DBusMessage* reply;
-       reply = dbus_message_new_method_return(msg);
-
-       if (NULL != reply) {
-               /* Append result and language */
-               dbus_message_append_args(reply,
-                       DBUS_TYPE_INT32, &ret,
-                       DBUS_TYPE_INVALID);
-
-               if (0 == ret) {
-                       SLOG(LOG_DEBUG, TAG_STTD, "[OUT SUCCESS] Result(%d)", ret);
-               } else {
-                       SLOG(LOG_ERROR, TAG_STTD, "[OUT ERROR] Result(%d)", ret);
-               }
-
-               if (!dbus_connection_send(conn, reply, NULL)) {
-                       SLOG(LOG_ERROR, TAG_STTD, "[OUT ERROR] Out Of Memory!");
-               }
-
-               dbus_connection_flush(conn);
-               dbus_message_unref(reply);
-       } else {
-               SLOG(LOG_ERROR, TAG_STTD, "[OUT ERROR] Fail to create reply message!!");
-       }
+       __sttd_dbus_server_send_method_return(conn, msg, ret);
 
        SLOG(LOG_DEBUG, TAG_STTD, "<<<<<");
        SLOG(LOG_DEBUG, TAG_STTD, "  ");
@@ -824,30 +787,7 @@ int sttd_dbus_server_unset_start_sound(DBusConnection* conn, DBusMessage* msg)
                ret = sttd_server_set_start_sound(uid, NULL);
        }
 
-       DBusMessage* reply;
-       reply = dbus_message_new_method_return(msg);
-
-       if (NULL != reply) {
-               /* Append result and language */
-               dbus_message_append_args(reply,
-                       DBUS_TYPE_INT32, &ret,
-                       DBUS_TYPE_INVALID);
-
-               if (0 == ret) {
-                       SLOG(LOG_DEBUG, TAG_STTD, "[OUT SUCCESS] Result(%d)", ret);
-               } else {
-                       SLOG(LOG_ERROR, TAG_STTD, "[OUT ERROR] Result(%d)", ret);
-               }
-
-               if (!dbus_connection_send(conn, reply, NULL)) {
-                       SLOG(LOG_ERROR, TAG_STTD, "[OUT ERROR] Out Of Memory!");
-               }
-
-               dbus_connection_flush(conn);
-               dbus_message_unref(reply);
-       } else {
-               SLOG(LOG_ERROR, TAG_STTD, "[OUT ERROR] Fail to create reply message!!");
-       }
+       __sttd_dbus_server_send_method_return(conn, msg, ret);
 
        SLOG(LOG_DEBUG, TAG_STTD, "<<<<<");
        SLOG(LOG_DEBUG, TAG_STTD, "  ");
@@ -877,30 +817,7 @@ int sttd_dbus_server_set_stop_sound(DBusConnection* conn, DBusMessage* msg)
                ret = sttd_server_set_stop_sound(uid, file);
        }
 
-       DBusMessage* reply;
-       reply = dbus_message_new_method_return(msg);
-
-       if (NULL != reply) {
-               /* Append result and language */
-               dbus_message_append_args(reply,
-                       DBUS_TYPE_INT32, &ret,
-                       DBUS_TYPE_INVALID);
-
-               if (0 == ret) {
-                       SECURE_SLOG(LOG_DEBUG, TAG_STTD, "[OUT SUCCESS] Result(%d)", ret);
-               } else {
-                       SLOG(LOG_ERROR, TAG_STTD, "[OUT ERROR] Result(%d)", ret);
-               }
-
-               if (!dbus_connection_send(conn, reply, NULL)) {
-                       SLOG(LOG_ERROR, TAG_STTD, "[OUT ERROR] Out Of Memory!");
-               }
-
-               dbus_connection_flush(conn);
-               dbus_message_unref(reply);
-       } else {
-               SLOG(LOG_ERROR, TAG_STTD, "[OUT ERROR] Fail to create reply message!!");
-       }
+       __sttd_dbus_server_send_method_return(conn, msg, ret);
 
        SLOG(LOG_DEBUG, TAG_STTD, "<<<<<");
        SLOG(LOG_DEBUG, TAG_STTD, "  ");
@@ -929,30 +846,7 @@ int sttd_dbus_server_unset_stop_sound(DBusConnection* conn, DBusMessage* msg)
                ret = sttd_server_set_stop_sound(uid, NULL);
        }
 
-       DBusMessage* reply;
-       reply = dbus_message_new_method_return(msg);
-
-       if (NULL != reply) {
-               /* Append result and language */
-               dbus_message_append_args(reply,
-                       DBUS_TYPE_INT32, &ret,
-                       DBUS_TYPE_INVALID);
-
-               if (0 == ret) {
-                       SLOG(LOG_DEBUG, TAG_STTD, "[OUT SUCCESS] Result(%d)", ret);
-               } else {
-                       SLOG(LOG_ERROR, TAG_STTD, "[OUT ERROR] Result(%d)", ret);
-               }
-
-               if (!dbus_connection_send(conn, reply, NULL)) {
-                       SLOG(LOG_ERROR, TAG_STTD, "[OUT ERROR] Out Of Memory!");
-               }
-
-               dbus_connection_flush(conn);
-               dbus_message_unref(reply);
-       } else {
-               SLOG(LOG_ERROR, TAG_STTD, "[OUT ERROR] Fail to create reply message!!");
-       }
+       __sttd_dbus_server_send_method_return(conn, msg, ret);
 
        SLOG(LOG_DEBUG, TAG_STTD, "<<<<<");
        SLOG(LOG_DEBUG, TAG_STTD, "  ");
@@ -1003,30 +897,6 @@ int sttd_dbus_server_start(DBusConnection* conn, DBusMessage* msg)
                }
        }
 
-#if 0
-       DBusMessage* reply;
-       reply = dbus_message_new_method_return(msg);
-
-       if (NULL != reply) {
-               dbus_message_append_args(reply, DBUS_TYPE_INT32, &ret, DBUS_TYPE_INVALID);
-
-               if (0 <= ret) {
-                       SLOG(LOG_DEBUG, TAG_STTD, "[OUT SUCCESS] Result(%d)", ret);
-               } else {
-                       SLOG(LOG_ERROR, TAG_STTD, "[OUT ERROR] Result(%d)", ret);
-               }
-
-               if (!dbus_connection_send(conn, reply, NULL)) {
-                       SLOG(LOG_ERROR, TAG_STTD, "[OUT ERROR] Out Of Memory!");
-               }
-
-               dbus_connection_flush(conn);
-               dbus_message_unref(reply);
-       } else {
-               SLOG(LOG_ERROR, TAG_STTD, "[OUT ERROR] Fail to create reply message!!");
-       }
-#endif
-
        SLOG(LOG_DEBUG, TAG_STTD, "<<<<<");
        SLOG(LOG_DEBUG, TAG_STTD, "  ");
 
@@ -1061,29 +931,6 @@ int sttd_dbus_server_stop(DBusConnection* conn, DBusMessage* msg)
                        SLOG(LOG_ERROR, TAG_STTD, "[ERROR] Fail to send error signal");
                }
        }
-#if 0
-       DBusMessage* reply;
-       reply = dbus_message_new_method_return(msg);
-
-       if (NULL != reply) {
-               dbus_message_append_args(reply, DBUS_TYPE_INT32, &ret, DBUS_TYPE_INVALID);
-
-               if (0 <= ret) {
-                       SLOG(LOG_DEBUG, TAG_STTD, "[OUT SUCCESS] Result(%d)", ret);
-               } else {
-                       SLOG(LOG_ERROR, TAG_STTD, "[OUT ERROR] Result(%d)", ret);
-               }
-
-               if (!dbus_connection_send(conn, reply, NULL)) {
-                       SLOG(LOG_ERROR, TAG_STTD, "[OUT ERROR] Out Of Memory!");
-               }
-
-               dbus_connection_flush(conn);
-               dbus_message_unref(reply);
-       } else {
-               SLOG(LOG_ERROR, TAG_STTD, "[OUT ERROR] Fail to create reply message!!");
-       }
-#endif
 
        SLOG(LOG_DEBUG, TAG_STTD, "<<<<<");
        SLOG(LOG_DEBUG, TAG_STTD, "  ");
@@ -1119,29 +966,7 @@ int sttd_dbus_server_cancel(DBusConnection* conn, DBusMessage* msg)
                        SLOG(LOG_ERROR, TAG_STTD, "[ERROR] Fail to send error signal");
                }
        }
-#if 0
-       DBusMessage* reply;
-       reply = dbus_message_new_method_return(msg);
 
-       if (NULL != reply) {
-               dbus_message_append_args(reply, DBUS_TYPE_INT32, &ret, DBUS_TYPE_INVALID);
-
-               if (0 == ret) {
-                       SLOG(LOG_DEBUG, TAG_STTD, "[OUT SUCCESS] Result(%d)", ret);
-               } else {
-                       SLOG(LOG_ERROR, TAG_STTD, "[OUT ERROR] Result(%d)", ret);
-               }
-
-               if (!dbus_connection_send(conn, reply, NULL)) {
-                       SLOG(LOG_ERROR, TAG_STTD, "[OUT ERROR] Out Of Memory!");
-               }
-
-               dbus_connection_flush(conn);
-               dbus_message_unref(reply);
-       } else {
-               SLOG(LOG_ERROR, TAG_STTD, "[OUT ERROR] Fail to create reply message!!");
-       }
-#endif
        SLOG(LOG_DEBUG, TAG_STTD, "<<<<<");
        SLOG(LOG_DEBUG, TAG_STTD, "  ");