Remove duplicated codes for set/unset sound for start and stop 96/276896/3
authorwn.jang <wn.jang@samsung.com>
Mon, 27 Jun 2022 11:02:45 +0000 (20:02 +0900)
committerwn.jang <wn.jang@samsung.com>
Tue, 28 Jun 2022 01:37:07 +0000 (10:37 +0900)
Change-Id: I927c0cd1ec1e022a9564a259e82a58d1d2936a11

client/stt_dbus.c
client/stt_main.h

index 0474266..79066d7 100644 (file)
@@ -45,6 +45,24 @@ extern int __stt_cb_set_volume(unsigned int uid, float volume);
 
 extern int __stt_cb_speech_status(unsigned int uid, int status);
 
+
+static char* __convert_stt_set_sound_method_into_char(stt_set_sound_method_e method)
+{
+       switch (method) {
+       case STT_SET_START_SOUND:
+               return STT_METHOD_SET_START_SOUND;
+       case STT_UNSET_START_SOUND:
+               return STT_METHOD_UNSET_START_SOUND;
+       case STT_SET_STOP_SOUND:
+               return STT_METHOD_SET_STOP_SOUND;
+       case STT_UNSET_STOP_SOUND:
+               return STT_METHOD_UNSET_STOP_SOUND;
+       default:
+               SLOG(LOG_ERROR, TAG_STTC, "Unsupported set sound method, method(%d)", method); //LCOV_EXCL_LINE
+       }
+       return NULL;
+}
+
 //LCOV_EXCL_START
 char* __stt_get_default_engine()
 {
@@ -1526,12 +1544,13 @@ int stt_dbus_request_is_recognition_type_supported(unsigned int uid, const char*
        return result;
 }
 
-int stt_dbus_request_set_start_sound(unsigned int uid, const char* file)
+static int __stt_dbus_request_set_sound(int method, unsigned int uid, const char* file)
 {
        if (NULL == file) {
                SLOG(LOG_ERROR, TAG_STTC, "Input parameter is NULL"); //LCOV_EXCL_LINE
                return STT_ERROR_INVALID_PARAMETER;
        }
+       char* method_string = __convert_stt_set_sound_method_into_char(method);
 
        DBusMessage* msg;
 
@@ -1539,13 +1558,13 @@ int stt_dbus_request_set_start_sound(unsigned int uid, const char* file)
                g_server_service_name,
                g_server_service_object,
                g_server_service_interface,
-               STT_METHOD_SET_START_SOUND);
+               method_string);
 
        if (NULL == msg) {
-               SLOG(LOG_ERROR, TAG_STTC, ">>>> stt set start sound : Fail to make message"); //LCOV_EXCL_LINE
+               SLOG(LOG_ERROR, TAG_STTC, ">>>> %s : Fail to make message", method_string); //LCOV_EXCL_LINE
                return STT_ERROR_OPERATION_FAILED;
        } else {
-               SLOG(LOG_DEBUG, TAG_STTC, ">>>> stt set start sound : uid(%u) file(%s)", uid, file);
+               SLOG(LOG_DEBUG, TAG_STTC, ">>>> %s : uid(%u) file(%s)", method_string, uid, file);
        }
 
        dbus_message_append_args(msg,
@@ -1585,9 +1604,9 @@ int stt_dbus_request_set_start_sound(unsigned int uid, const char* file)
                result_msg = NULL;
 
                if (0 == result) {
-                       SLOG(LOG_DEBUG, TAG_STTC, "<<<< stt set start sound : result = %d", result);
+                       SLOG(LOG_DEBUG, TAG_STTC, "<<<< stt %s: result = %d", method_string, result);
                } else {
-                       SLOG(LOG_ERROR, TAG_STTC, "<<<< stt set start sound : result = %d", result); //LCOV_EXCL_LINE
+                       SLOG(LOG_ERROR, TAG_STTC, "<<<< stt %s : result = %d", method_string, result); //LCOV_EXCL_LINE
                }
        } else {
                SLOG(LOG_ERROR, TAG_STTC, "<<<< Result message is NULL"); //LCOV_EXCL_LINE
@@ -1598,21 +1617,22 @@ int stt_dbus_request_set_start_sound(unsigned int uid, const char* file)
        return result;
 }
 
-int stt_dbus_request_unset_start_sound(unsigned int uid)
+static int __stt_dbus_request_unset_sound(int method, unsigned int uid)
 {
        DBusMessage* msg;
+       char* method_string = __convert_stt_set_sound_method_into_char(method);
 
        msg = dbus_message_new_method_call(
                g_server_service_name,
                g_server_service_object,
                g_server_service_interface,
-               STT_METHOD_UNSET_START_SOUND);
+               method_string);
 
        if (NULL == msg) {
-               SLOG(LOG_ERROR, TAG_STTC, ">>>> stt unset start sound : Fail to make message"); //LCOV_EXCL_LINE
+               SLOG(LOG_ERROR, TAG_STTC, ">>>> %s : Fail to make message", method_string); //LCOV_EXCL_LINE
                return STT_ERROR_OPERATION_FAILED;
        } else {
-               SLOG(LOG_DEBUG, TAG_STTC, ">>>> stt unset start sound : uid(%u)", uid);
+               SLOG(LOG_DEBUG, TAG_STTC, ">>>> %s : uid(%u)", method_string, uid);
        }
 
        dbus_message_append_args(msg,
@@ -1651,9 +1671,9 @@ int stt_dbus_request_unset_start_sound(unsigned int uid)
                result_msg = NULL;
 
                if (0 == result) {
-                       SLOG(LOG_DEBUG, TAG_STTC, "<<<< stt unset start sound : result = %d", result);
+                       SLOG(LOG_DEBUG, TAG_STTC, "<<<< %s : result = %d", method_string, result);
                } else {
-                       SLOG(LOG_ERROR, TAG_STTC, "<<<< stt unset start sound : result = %d", result); //LCOV_EXCL_LINE
+                       SLOG(LOG_ERROR, TAG_STTC, "<<<< %s : result = %d", method_string, result); //LCOV_EXCL_LINE
                }
        } else {
                SLOG(LOG_ERROR, TAG_STTC, "<<<< Result message is NULL"); //LCOV_EXCL_LINE
@@ -1664,142 +1684,44 @@ int stt_dbus_request_unset_start_sound(unsigned int uid)
        return result;
 }
 
-int stt_dbus_request_set_stop_sound(unsigned int uid, const char* file)
+int stt_dbus_request_set_start_sound(unsigned int uid, const char* file)
 {
-       if (NULL == file) {
-               SLOG(LOG_ERROR, TAG_STTC, "Input parameter is NULL"); //LCOV_EXCL_LINE
-               return STT_ERROR_INVALID_PARAMETER;
-       }
-
-       DBusMessage* msg;
-
-       msg = dbus_message_new_method_call(
-               g_server_service_name,
-               g_server_service_object,
-               g_server_service_interface,
-               STT_METHOD_SET_STOP_SOUND);
-
-       if (NULL == msg) {
-               SLOG(LOG_ERROR, TAG_STTC, ">>>> stt set stop sound : Fail to make message"); //LCOV_EXCL_LINE
-               return STT_ERROR_OPERATION_FAILED;
-       } else {
-               SLOG(LOG_DEBUG, TAG_STTC, ">>>> stt set stop sound : uid(%u) file(%s)", uid, file);
+       int ret = __stt_dbus_request_set_sound(STT_SET_START_SOUND, uid, file);
+       if (STT_ERROR_NONE != ret) {
+               SLOG(LOG_ERROR, TAG_STTC, "Fail to request set start sound, uid(%d), file(%s)", uid, file);
        }
 
-       dbus_message_append_args(msg,
-               DBUS_TYPE_UINT32, &uid,
-               DBUS_TYPE_STRING, &file,
-               DBUS_TYPE_INVALID);
-
-       DBusError err;
-       dbus_error_init(&err);
-
-       DBusMessage* result_msg;
-       int result = STT_ERROR_OPERATION_FAILED;
+       return ret;
+}
 
-       result_msg = dbus_connection_send_with_reply_and_block(g_conn_sender, msg, g_waiting_time, &err);
-       dbus_message_unref(msg);
-       msg = NULL;
-       if (dbus_error_is_set(&err)) {
-               //LCOV_EXCL_START
-               SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Send Error (%s)", err.message);
-               dbus_error_free(&err);
-               //LCOV_EXCL_STOP
+int stt_dbus_request_unset_start_sound(unsigned int uid)
+{
+       int ret = __stt_dbus_request_unset_sound(STT_UNSET_START_SOUND, uid);
+       if (STT_ERROR_NONE != ret) {
+               SLOG(LOG_ERROR, TAG_STTC, "Fail to request unset start sound, uid(%d)", uid);
        }
 
-       if (NULL != result_msg) {
-               dbus_message_get_args(result_msg, &err,
-                       DBUS_TYPE_INT32, &result,
-                       DBUS_TYPE_INVALID);
-
-               if (dbus_error_is_set(&err)) {
-                       //LCOV_EXCL_START
-                       SLOG(LOG_ERROR, TAG_STTC, "<<<< Get arguments error (%s)", err.message);
-                       dbus_error_free(&err);
-                       result = STT_ERROR_OPERATION_FAILED;
-                       //LCOV_EXCL_STOP
-               }
-               dbus_message_unref(result_msg);
-               result_msg = NULL;
+       return ret;
+}
 
-               if (0 == result) {
-                       SLOG(LOG_DEBUG, TAG_STTC, "<<<< stt set stop sound : result = %d", result);
-               } else {
-                       SLOG(LOG_ERROR, TAG_STTC, "<<<< stt set stop sound : result = %d", result); //LCOV_EXCL_LINE
-               }
-       } else {
-               SLOG(LOG_ERROR, TAG_STTC, "<<<< Result message is NULL"); //LCOV_EXCL_LINE
-               stt_dbus_reconnect();
-               result = STT_ERROR_TIMED_OUT;
+int stt_dbus_request_set_stop_sound(unsigned int uid, const char* file)
+{
+       int ret = __stt_dbus_request_set_sound(STT_SET_STOP_SOUND, uid, file);
+       if (STT_ERROR_NONE != ret) {
+               SLOG(LOG_ERROR, TAG_STTC, "Fail to request set stop sound, uid(%d), file(%s)", uid, file);
        }
 
-       return result;
+       return ret;
 }
 
 int stt_dbus_request_unset_stop_sound(unsigned int uid)
 {
-       DBusMessage* msg;
-
-       msg = dbus_message_new_method_call(
-               g_server_service_name,
-               g_server_service_object,
-               g_server_service_interface,
-               STT_METHOD_UNSET_STOP_SOUND);
-
-       if (NULL == msg) {
-               SLOG(LOG_ERROR, TAG_STTC, ">>>> stt unset stop sound : Fail to make message"); //LCOV_EXCL_LINE
-               return STT_ERROR_OPERATION_FAILED;
-       } else {
-               SLOG(LOG_DEBUG, TAG_STTC, ">>>> stt unset stop sound : uid(%u)", uid);
-       }
-
-       dbus_message_append_args(msg,
-               DBUS_TYPE_UINT32, &uid,
-               DBUS_TYPE_INVALID);
-
-       DBusError err;
-       dbus_error_init(&err);
-
-       DBusMessage* result_msg;
-       int result = STT_ERROR_OPERATION_FAILED;
-
-       result_msg = dbus_connection_send_with_reply_and_block(g_conn_sender, msg, g_waiting_time, &err);
-       dbus_message_unref(msg);
-       msg = NULL;
-       if (dbus_error_is_set(&err)) {
-               //LCOV_EXCL_START
-               SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Send Error (%s)", err.message);
-               dbus_error_free(&err);
-               //LCOV_EXCL_STOP
-       }
-
-       if (NULL != result_msg) {
-               dbus_message_get_args(result_msg, &err,
-                       DBUS_TYPE_INT32, &result,
-                       DBUS_TYPE_INVALID);
-
-               if (dbus_error_is_set(&err)) {
-                       //LCOV_EXCL_START
-                       SLOG(LOG_ERROR, TAG_STTC, "<<<< Get arguments error (%s)", err.message);
-                       dbus_error_free(&err);
-                       result = STT_ERROR_OPERATION_FAILED;
-                       //LCOV_EXCL_STOP
-               }
-               dbus_message_unref(result_msg);
-               result_msg = NULL;
-
-               if (0 == result) {
-                       SLOG(LOG_DEBUG, TAG_STTC, "<<<< stt unset stop sound : result = %d", result);
-               } else {
-                       SLOG(LOG_ERROR, TAG_STTC, "<<<< stt unset stop sound : result = %d", result); //LCOV_EXCL_LINE
-               }
-       } else {
-               SLOG(LOG_ERROR, TAG_STTC, "<<<< Result message is NULL"); //LCOV_EXCL_LINE
-               stt_dbus_reconnect();
-               result = STT_ERROR_TIMED_OUT;
+       int ret = __stt_dbus_request_unset_sound(STT_UNSET_STOP_SOUND, uid);
+       if (STT_ERROR_NONE != ret) {
+               SLOG(LOG_ERROR, TAG_STTC, "Fail to request unset stop sound, uid(%d)", uid);
        }
 
-       return result;
+       return ret;
 }
 
 static int __stt_dbus_send_message_without_reply(DBusMessage* msg, char* method)
index 77af45f..e38cca8 100644 (file)
@@ -50,6 +50,13 @@ typedef enum {
        STT_RESULT_STATE_NOT_DONE       = 1                     /**< Async state change */
 } stt_result_state_e;
 
+typedef enum {
+       STT_SET_START_SOUND,
+       STT_UNSET_START_SOUND,
+       STT_SET_STOP_SOUND,
+       STT_UNSET_STOP_SOUND,
+} stt_set_sound_method_e;
+
 #ifdef __cplusplus
 }
 #endif