Add document in vce.h
[platform/core/uifw/voice-control.git] / server / vcd_server.c
index 6cbc2b9..a525662 100755 (executable)
@@ -39,6 +39,7 @@
 static GList *g_proc_list = NULL;
 
 static Ecore_Timer *g_restart_timer = NULL;
+static Ecore_Timer *g_check_widget_client_timer = NULL;
 
 /**
 * @brief Enumerations of send event type.
@@ -400,7 +401,7 @@ static Eina_Bool __vcd_send_selected_result(void *data)
                                                        SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Fail to send result, ret(%d)", ret);
                                                        break;
                                                } else {
-                                                       SLOG(LOG_DEBUG, TAG_VCD, "[Server] Send result : pid(%d) type(%d)", temp_cmd->pid, temp_cmd->type);
+                                                       SLOG(LOG_ERROR, TAG_VCD, "[Server] Send result : pid(%d) type(%d)", temp_cmd->pid, temp_cmd->type);
                                                        pre_pid = temp_cmd->pid;
                                                        pre_type = temp_cmd->type;
                                                }
@@ -424,12 +425,21 @@ static Eina_Bool __vcd_send_selected_result(void *data)
 
 int vcd_send_asr_result(vce_asr_result_event_e event, const char* asr_result, void *user_data)
 {
+       int ret = __vcd_server_launch_manager_app();
+       if (0 != ret) {
+               SECURE_SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to send ASR result : mgr_pid(%d), asr_result(%s)", vcd_client_manager_get_pid(), asr_result);
+               return ret;
+       }
+
        if (NULL != asr_result) {
                SECURE_SLOG(LOG_DEBUG, TAG_VCD, "[Server] ASR result - Event(%d), Text(%s)", event, asr_result);
-               vcdc_send_pre_result_to_manager(vcd_client_manager_get_pid(), event, asr_result);
+               ret = vcdc_send_pre_result_to_manager(vcd_client_manager_get_pid(), event, asr_result);
+               if (0 != ret) {
+                       SECURE_SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to send ASR result : mgr_pid(%d), asr_result(%s)", vcd_client_manager_get_pid(), asr_result);
+               }
        }
 
-       return VCD_ERROR_NONE;
+       return ret;
 }
 
 int vcd_send_nlg_result(const char* nlg_result, void *user_data)
@@ -440,6 +450,7 @@ int vcd_send_nlg_result(const char* nlg_result, void *user_data)
                return ret;
        }
 
+       SLOG(LOG_ERROR, TAG_VCD, "[Server] send dialog : mgr_pid(%d), nlg_result(%s)", vcd_client_manager_get_pid(), nlg_result);
        ret = vcdc_send_dialog(vcd_client_manager_get_pid(), -1, nlg_result, NULL, 0); //0: VC_DIALOG_END
        if (0 != ret)
                SECURE_SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to send dialog : mgr_pid(%d), nlg_result(%s)", vcd_client_manager_get_pid(), nlg_result);
@@ -469,6 +480,8 @@ int vcd_send_result(vce_result_event_e event, int* result_id, int count, const c
        SECURE_SLOG(LOG_INFO, TAG_VCD, "[Server] Event(%d), Text(%s) Nonfixed(%s) Msg(%s) Result count(%d)",
                event, all_result, non_fixed_result, msg, count);
 
+       SLOG(LOG_ERROR, TAG_VCD, "[Server] NLU result(%s)", nlu_result);
+
        if (VCD_RECOGNITION_MODE_RESTART_AFTER_REJECT == vcd_client_get_recognition_mode()) {
                if (VCE_RESULT_EVENT_REJECTED == event) {
                        SLOG(LOG_DEBUG, TAG_VCD, "[Server] Restart by no or rejected result");
@@ -572,7 +585,7 @@ int vcd_send_result(vce_result_event_e event, int* result_id, int count, const c
                                                if (true == is_consumed) {
                                                        if (NULL != user_info) {
                                                                *user_info = 0x01;
-                                                               SLOG(LOG_DEBUG, TAG_VCD, "[Server] Send whether ASR result is consumed or not (%d)", *user_info);
+                                                               SLOG(LOG_ERROR, TAG_VCD, "[Server] Send whether ASR result is consumed or not (%d)", *user_info);
                                                        }
                                                        vcdc_send_show_tooltip(pid, false);
                                                        if (-1 != vcd_client_manager_get_pid()) {
@@ -914,9 +927,14 @@ int vcd_send_error(vce_error_e error, const char* msg, void *user_data)
        char* error_msg = NULL;
        if (NULL != msg) {
                error_msg = strdup(msg);
+               if (NULL == error_msg) {
+                       return VCD_ERROR_OUT_OF_MEMORY;
+               }
        }
 
-       if (0 != vcdc_send_error_signal_to_manager(vcd_client_manager_get_pid(), error, error_msg)) {
+       int ret = VCD_ERROR_NONE;
+       ret = vcdc_send_error_signal_to_manager(vcd_client_manager_get_pid(), error, error_msg);
+       if (VCD_ERROR_NONE != ret) {
                SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to send error signal");
        }
 
@@ -925,7 +943,7 @@ int vcd_send_error(vce_error_e error, const char* msg, void *user_data)
                error_msg = NULL;
        }
 
-       return VCD_ERROR_NONE;
+       return ret;
 }
 
 /*
@@ -1024,12 +1042,12 @@ int vcd_initialize(vce_request_callback_s *callback)
        vcd_config_set_service_state(VCD_STATE_READY);
        vcdc_send_service_state(VCD_STATE_READY);
 
-       SLOG(LOG_DEBUG, TAG_VCD, "[Server SUCCESS] initialize");
+       SLOG(LOG_ERROR, TAG_VCD, "[Server SUCCESS] initialize");
 
        return 0;
 }
 
-void vcd_finalize()
+bool vcd_finalize()
 {
        GList *iter = NULL;
        if (0 < g_list_length(g_proc_list)) {
@@ -1052,14 +1070,17 @@ void vcd_finalize()
                }
                vcd_engine_recognize_cancel();
        }
+
        if (0 != vcd_recorder_destroy()) {
                SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to destroy recorder");
+               return false;
        } else {
                SLOG(LOG_DEBUG, TAG_VCD, "[Server] destroy recorder");
        }
 
        if (0 != vcd_engine_agent_release()) {
                SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to release engine");
+               return false;
        } else {
                SLOG(LOG_DEBUG, TAG_VCD, "[Server] release engine");
        }
@@ -1069,16 +1090,20 @@ void vcd_finalize()
        vcd_config_set_service_state(VCD_STATE_NONE);
        vcdc_send_service_state(VCD_STATE_NONE);
 
-       SLOG(LOG_DEBUG, TAG_VCD, "[Server] mode finalize");
+       SLOG(LOG_ERROR, TAG_VCD, "[Server] mode finalize");
 
-       return;
+       return true;
 }
 
 static Eina_Bool __finalize_quit_ecore_loop(void *data)
 {
-       SLOG(LOG_ERROR, TAG_VCD, "[Server] quit ecore main loop");
-       ecore_main_loop_quit();
-       return EINA_FALSE;
+       bool ret = vcd_finalize();
+       if (false == ret) {
+               return EINA_TRUE;
+       } else {
+               ecore_main_loop_quit();
+               return EINA_FALSE;
+       }
 }
 
 static void __read_proc()
@@ -1325,7 +1350,7 @@ int vcd_server_mgr_initialize(int pid)
        if (0 != vcdc_send_manager_pid(pid))
                SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Fail to send manager pid");
 
-       SLOG(LOG_DEBUG, TAG_VCD, "[Server Success] Manager initialize : pid(%d)", pid);
+       SLOG(LOG_ERROR, TAG_VCD, "[Server Success] Manager initialize : pid(%d)", pid);
 
        return VCD_ERROR_NONE;
 }
@@ -1351,10 +1376,10 @@ int vcd_server_mgr_finalize(int pid)
 
        if (0 == vcd_client_get_ref_count()) {
                SLOG(LOG_DEBUG, TAG_VCD, "[Server] Connected client list is empty");
-               ecore_timer_add(0, __finalize_quit_ecore_loop, NULL);
+               ecore_timer_add(0.5, __finalize_quit_ecore_loop, NULL);
        }
 
-       SLOG(LOG_DEBUG, TAG_VCD, "[Server Success] Manager Finalize : pid(%d)", pid);
+       SLOG(LOG_ERROR, TAG_VCD, "[Server Success] Manager Finalize : pid(%d)", pid);
 
        return VCD_ERROR_NONE;
 }
@@ -1462,9 +1487,48 @@ int vcd_server_mgr_set_client_info(int pid)
        return VCD_ERROR_NONE;
 }
 
+static int __reset_waiting_for_widget_recording(void)
+{
+       SLOG(LOG_ERROR, TAG_VCD, "[Server] Reet waiting for widget recording");
+       // Delete timer to check that widget client is terminated
+       if (g_check_widget_client_timer) {
+               ecore_timer_del(g_check_widget_client_timer);
+               g_check_widget_client_timer = NULL;
+       }
+       // Reset flag to wait for recording from widget client
+       vcd_client_widget_set_waiting_for_recording(-1, false);
+       return 0;
+}
+
+static Eina_Bool __send_waiting_timeout_error_to_manager(void* data)
+{
+       intptr_t ppid = (intptr_t)data;
+       int pid = (int)ppid;
+       SLOG(LOG_ERROR, TAG_VCD, "Widget client didn't send to start recording, pid(%d)", pid);
+       __reset_waiting_for_widget_recording();
+
+       vcdc_send_error_signal_to_manager(vcd_client_manager_get_pid(), VCD_ERROR_OPERATION_FAILED, "voice_engine.error.proc_fail");
+       return EINA_FALSE;
+}
+
+static int __set_waiting_for_widget_recording(int pid)
+{
+       SLOG(LOG_ERROR, TAG_VCD, "[Server] Set waiting for widget recording, pid(%d)", pid);
+
+       // Check if the app included widget client is terminated or not. If it is terminated, vcd_server_widget_finalize() function will be called
+       // In that function, it will start recording
+       intptr_t ppid = (intptr_t)pid;
+       g_check_widget_client_timer = ecore_timer_add(2.0, __send_waiting_timeout_error_to_manager, (void*)ppid);
+
+       // Set flag to wait for recording from widget client
+       vcd_client_widget_set_waiting_for_recording(pid, true);
+       return 0;
+}
+
 static int __start_internal_recognition()
 {
        int ret;
+       __reset_waiting_for_widget_recording();
 
        if (0 != vcd_client_command_collect_command()) {
                SLOG(LOG_ERROR, TAG_VCD, "[Client Data ERROR] Fail to collect command");
@@ -1486,7 +1550,7 @@ static int __start_internal_recognition()
                return VCD_ERROR_OPERATION_FAILED;
        }
 
-       SLOG(LOG_DEBUG, TAG_VCD, "[Server Success] Set command");
+       SLOG(LOG_ERROR, TAG_VCD, "[Server Success] Set command");
 
        bool stop_by_silence = true;
        if (VCD_RECOGNITION_MODE_MANUAL == vcd_client_get_recognition_mode()) {
@@ -1506,7 +1570,7 @@ static int __start_internal_recognition()
                return VCD_ERROR_OPERATION_FAILED;
        }
 
-       SLOG(LOG_DEBUG, TAG_VCD, "[Server Success] Start engine");
+       SLOG(LOG_ERROR, TAG_VCD, "[Server Success] Start engine");
 
 #if 1
        /* 5. recorder start */
@@ -1525,7 +1589,7 @@ static int __start_internal_recognition()
        vcd_config_set_service_state(VCD_STATE_RECORDING);
        vcdc_send_service_state(VCD_STATE_RECORDING);
 
-       SLOG(LOG_DEBUG, TAG_VCD, "[Server Success] Start recognition(%d)", stop_by_silence);
+       SLOG(LOG_ERROR, TAG_VCD, "[Server Success] Start recognition(%d)", stop_by_silence);
 
        return 0;
 }
@@ -1534,7 +1598,7 @@ static Eina_Bool __vcd_request_show_tooltip(void *data)
 {
        int pid = vcd_client_widget_get_foreground_pid();
        if (-1 != pid) {
-               SLOG(LOG_DEBUG, TAG_VCD, "[Server] Request tooltip show and widget command");
+               SLOG(LOG_ERROR, TAG_VCD, "[Server] Request tooltip show and widget command, show(%d)", (bool)data);
                vcdc_send_show_tooltip(pid, (bool)data);
        }
 
@@ -1554,9 +1618,9 @@ int vcd_server_mgr_start(vcd_recognition_mode_e recognition_mode, bool exclusive
                SLOG(LOG_DEBUG, TAG_VCD, "[Server] Manager is NOT available.");
                return VCD_ERROR_OPERATION_FAILED;
        }
-       vcd_client_widget_set_waiting_for_recording(-1, false);
+       __reset_waiting_for_widget_recording();
 
-       SLOG(LOG_DEBUG, TAG_VCD, "[Server] set recognition mode = %d", recognition_mode);
+       SLOG(LOG_ERROR, TAG_VCD, "[Server] set recognition mode = %d", recognition_mode);
        vcd_client_set_recognition_mode(recognition_mode);
 
        if (false == exclusive_cmd) {
@@ -1565,9 +1629,10 @@ int vcd_server_mgr_start(vcd_recognition_mode_e recognition_mode, bool exclusive
                if (1 == vcd_config_get_command_type_enabled(VC_COMMAND_TYPE_WIDGET)) {
                        int pid = vcd_client_widget_get_foreground_pid();
                        if (-1 != pid) {
-                               SLOG(LOG_INFO, TAG_VCD, "[Server] Request tooltip show and widget command");
+                               SLOG(LOG_ERROR, TAG_VCD, "[Server] Request tooltip show and widget command");
                                ecore_timer_add(0, __vcd_request_show_tooltip, (void*)true);
-                               vcd_client_widget_set_waiting_for_recording(pid, true);
+
+                               __set_waiting_for_widget_recording(pid);
                                return 0;
                        }
                }
@@ -1588,6 +1653,8 @@ int vcd_server_mgr_start(vcd_recognition_mode_e recognition_mode, bool exclusive
                }
        }
 
+       SLOG(LOG_ERROR, TAG_VCD, "[Server] start internal recognition");
+
        int ret = __start_internal_recognition();
        if (0 != ret) {
                SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to start recongition : %d", ret);
@@ -1598,6 +1665,7 @@ int vcd_server_mgr_start(vcd_recognition_mode_e recognition_mode, bool exclusive
                vcd_client_unset_exclusive_command(fg_pid);
        }
 
+       SLOG(LOG_ERROR, TAG_VCD, "[Server Success] start internal recognition");
        return VCD_ERROR_NONE;
 }
 
@@ -1612,6 +1680,7 @@ int vcd_server_mgr_stop()
                SLOG(LOG_DEBUG, TAG_VCD, "[Server] Manager is NOT available.");
                return VCD_ERROR_OPERATION_FAILED;
        }
+       SLOG(LOG_ERROR, TAG_VCD, "[Server] stop internal recognition");
 
 #if 1
        /* 2. Stop recorder */
@@ -1628,6 +1697,7 @@ int vcd_server_mgr_stop()
        vcd_config_set_service_state(VCD_STATE_PROCESSING);
        vcdc_send_service_state(VCD_STATE_PROCESSING);
 
+       SLOG(LOG_ERROR, TAG_VCD, "[Server Success] stop internal recognition");
        return VCD_ERROR_NONE;
 }
 
@@ -1663,6 +1733,7 @@ int vcd_server_mgr_cancel()
                vcdc_send_service_state(VCD_STATE_READY);
                return VCD_ERROR_NONE;
        }
+       SLOG(LOG_ERROR, TAG_VCD, "[Server] cancel internal recognition");
 
 #if 1
        /* 2. Stop recorder */
@@ -1691,6 +1762,7 @@ int vcd_server_mgr_cancel()
        vcd_config_set_service_state(VCD_STATE_READY);
        vcdc_send_service_state(VCD_STATE_READY);
 
+       SLOG(LOG_ERROR, TAG_VCD, "[Server Success] cancel internal recognition");
        return VCD_ERROR_NONE;
 }
 
@@ -1831,7 +1903,7 @@ int vcd_server_mgr_enable_command_type(int pid, int cmd_type)
        if (0 != ret) {
                SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to enable command type");
        } else {
-               SLOG(LOG_DEBUG, TAG_VCD, "[Server] Enable command type(%d)", cmd_type);
+               SLOG(LOG_ERROR, TAG_VCD, "[Server] Enable command type(%d)", cmd_type);
        }
 
        return ret;
@@ -1857,7 +1929,7 @@ int vcd_server_mgr_disable_command_type(int pid, int cmd_type)
        if (0 != ret) {
                SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to disable command type");
        } else {
-               SLOG(LOG_DEBUG, TAG_VCD, "[Server] Disable command type(%d)", cmd_type);
+               SLOG(LOG_ERROR, TAG_VCD, "[Server] Disable command type(%d)", cmd_type);
        }
 
        return ret;
@@ -1905,7 +1977,7 @@ int vcd_server_finalize(int pid)
 
        if (0 == vcd_client_get_ref_count()) {
                SLOG(LOG_DEBUG, TAG_VCD, "[Server] Connected client list is empty");
-               ecore_timer_add(0, __finalize_quit_ecore_loop, NULL);
+               ecore_timer_add(0.5, __finalize_quit_ecore_loop, NULL);
        }
 
        SLOG(LOG_DEBUG, TAG_VCD, "[Server Success] Client Finalize : pid(%d)", pid);
@@ -2197,10 +2269,11 @@ int vcd_server_widget_initialize(int pid)
 
 static void __vcd_server_widget_start_recording(void *data)
 {
+       SLOG(LOG_ERROR, TAG_VCD, "[Server INFO] start recording");
+
        if (0 != __start_internal_recognition()) {
                SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to start recongition");
        }
-       vcd_client_widget_set_waiting_for_recording(-1, false);
 }
 
 int vcd_server_widget_finalize(int pid)
@@ -2218,7 +2291,7 @@ int vcd_server_widget_finalize(int pid)
 
        if (0 == vcd_client_get_ref_count()) {
                SLOG(LOG_ERROR, TAG_VCD, "[Server] connected client list is empty");
-               ecore_timer_add(0, __finalize_quit_ecore_loop, NULL);
+               ecore_timer_add(0.5, __finalize_quit_ecore_loop, NULL);
                return 0;
        }
 
@@ -2228,7 +2301,7 @@ int vcd_server_widget_finalize(int pid)
        }
 
        if (true == is_waiting) {
-               SLOG(LOG_INFO, TAG_VCD, "[Server INFO] invoke to start recording");
+               SLOG(LOG_ERROR, TAG_VCD, "[Server INFO] invoke to start recording");
                ecore_main_loop_thread_safe_call_async(__vcd_server_widget_start_recording, NULL);
        }
        return VCD_ERROR_NONE;
@@ -2242,6 +2315,12 @@ int vcd_server_widget_start_recording(int pid, bool widget_command)
                return VCD_ERROR_INVALID_PARAMETER;
        }
 
+       bool waiting;
+       if (0 != vcd_client_widget_get_waiting_for_recording(pid, &waiting) || false == waiting) {
+               SLOG(LOG_ERROR, TAG_VCD, "[Server] Server is not waiting for recording, pid(%d), waiting(%d)", pid, waiting);
+               return 0;
+       }
+
        if (true == widget_command) {
                vcd_client_widget_set_command(pid);
                SLOG(LOG_DEBUG, TAG_VCD, "[Server] widget command is available");
@@ -2250,6 +2329,7 @@ int vcd_server_widget_start_recording(int pid, bool widget_command)
                SLOG(LOG_WARN, TAG_VCD, "[Server] widget command is NOT available");
        }
 
+       SLOG(LOG_ERROR, TAG_VCD, "[Server] start internal recongition : %d", widget_command);
        int ret = __start_internal_recognition();
        if (0 != ret) {
                SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to start recongition : %d", ret);