Merge "Reset waiting flag when recognition starts by widget" into tizen
[platform/core/uifw/voice-control.git] / server / vcd_server.c
index 10cb388..65fb25b 100755 (executable)
@@ -400,7 +400,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;
                                                }
@@ -425,7 +425,7 @@ 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)
 {
        if (NULL != asr_result) {
-               SLOG(LOG_DEBUG, TAG_VCD, "[Server] ASR result - Event(%d), Text(%s)", event, 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);
        }
 
@@ -436,13 +436,14 @@ int vcd_send_nlg_result(const char* nlg_result, void *user_data)
 {
        int ret = __vcd_server_launch_manager_app();
        if (0 != ret) {
-               SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to send dialog : mgr_pid(%d), nlg_result(%s)", vcd_client_manager_get_pid(), nlg_result);
+               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);
                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)
-               SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to send dialog : mgr_pid(%d), nlg_result(%s)", vcd_client_manager_get_pid(), nlg_result);
+               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);
        return ret;
 }
 
@@ -452,7 +453,7 @@ static void* __recorder_stop(void *data)
        return NULL;
 }
 
-int vcd_send_result(vce_result_event_e event, int* result_id, int count, const char* all_result, const char* non_fixed_result, const char* nlu_result, const char* msg, void *user_data)
+int vcd_send_result(vce_result_event_e event, int* result_id, int count, const char* all_result, const char* non_fixed_result, const char* nlu_result, const char* msg, int* user_info, void *user_data)
 {
        int ret = 0;
 
@@ -466,9 +467,11 @@ int vcd_send_result(vce_result_event_e event, int* result_id, int count, const c
        vc_info_parser_unset_result(vcd_client_manager_get_exclusive());
        vcd_client_manager_set_result_text(all_result);
 
-       SLOG(LOG_INFO, TAG_VCD, "[Server] Event(%d), Text(%s) Nonfixed(%s) Msg(%s) Result count(%d)", 
+       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");
@@ -502,42 +505,6 @@ int vcd_send_result(vce_result_event_e event, int* result_id, int count, const c
                }
        }
 #if 1
-
-       int pid = vcd_client_widget_get_foreground_pid();
-       if (-1 != pid) {
-               if (NULL != all_result) {
-                       vc_info_parser_set_result(all_result, event, msg, NULL, false);
-                       bool enable = false;
-                       vcd_client_widget_get_asr_result_enabled(pid, &enable);
-                       if (true == enable) {
-                               SLOG(LOG_DEBUG, TAG_VCD, "[Server] Send ASR result to Widget client");
-                               bool is_consumed = false;
-                               if (0 != vcdc_send_asr_result(pid, VC_COMMAND_TYPE_WIDGET, &is_consumed)) {
-                                       SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to send asr result");
-                               } else {
-                                       SLOG(LOG_DEBUG, TAG_VCD, "[Server] ASR result is consumed(%d)", is_consumed);
-                                       if (true == is_consumed) {
-                                               vcdc_send_show_tooltip(pid, false);
-                                               if (-1 != vcd_client_manager_get_pid()) {
-                                                       /* Manager client is available */
-                                                       vc_info_parser_unset_result(false);
-                                                       vc_info_parser_set_result(all_result, VC_RESULT_EVENT_RESULT_SUCCESS, msg, NULL, false);
-                                                       if (0 != vcdc_send_result_to_manager(vcd_client_manager_get_pid(), VC_RESULT_TYPE_NOTIFICATION)) {
-                                                               SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Fail to send result");
-                                                       }
-                                               }
-
-                                               vcd_client_manager_set_exclusive(false);
-
-                                               vcd_config_set_service_state(VCD_STATE_READY);
-                                               vcdc_send_service_state(VCD_STATE_READY);
-                                               return VCD_ERROR_NONE;
-                                       }
-                               }
-                       }
-               }
-       }
-
        /* if nlu_result is exist, Add command handle(is_action) into result list */
        /* Normal result */
        SLOG(LOG_DEBUG, TAG_VCD, "[Server] @ Get engine result @");
@@ -554,7 +521,7 @@ int vcd_send_result(vce_result_event_e event, int* result_id, int count, const c
        }
 
        /* priority filter */
-       /* system > exclusive > foreground = widget > system_background > background */
+       /* system > exclusive > widget > foreground > system_background > background */
        int i = 0;
        int* filtered_id = (int*)calloc(count, sizeof(int));
        if (!filtered_id) {
@@ -587,6 +554,56 @@ int vcd_send_result(vce_result_event_e event, int* result_id, int count, const c
                }
        }
 
+       // ASR consume
+       if (top_priority >= VC_COMMAND_PRIORITY_WIDGET) {
+               int pid = vcd_client_widget_get_foreground_pid();
+               if (-1 != pid) {
+                       if (NULL != all_result) {
+                               vc_info_parser_set_result(all_result, event, msg, NULL, false);
+                               bool enable = false;
+                               vcd_client_widget_get_asr_result_enabled(pid, &enable);
+                               if (true == enable) {
+                                       SLOG(LOG_DEBUG, TAG_VCD, "[Server] Send ASR result to Widget client");
+                                       bool is_consumed = false;
+                                       if (NULL != user_info) {
+                                               *user_info = 0x00;
+                                       }
+                                       if (0 != vcdc_send_asr_result(pid, event, all_result, VC_COMMAND_TYPE_WIDGET, &is_consumed)) {
+                                               SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to send asr result");
+                                       } else {
+                                               SLOG(LOG_DEBUG, TAG_VCD, "[Server] ASR result is consumed(%d)", is_consumed);
+                                               if (true == is_consumed) {
+                                                       if (NULL != user_info) {
+                                                               *user_info = 0x01;
+                                                               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()) {
+                                                               /* Manager client is available */
+                                                               vc_info_parser_unset_result(false);
+                                                               vc_info_parser_set_result(all_result, VC_RESULT_EVENT_RESULT_SUCCESS, msg, NULL, false);
+                                                               if (0 != vcdc_send_result_to_manager(vcd_client_manager_get_pid(), VC_RESULT_TYPE_NOTIFICATION)) {
+                                                                       SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Fail to send result");
+                                                               }
+                                                       }
+
+                                                       vcd_client_manager_set_exclusive(false);
+
+                                                       vcd_config_set_service_state(VCD_STATE_READY);
+                                                       vcdc_send_service_state(VCD_STATE_READY);
+                                                       vc_cmd_list_destroy(vc_cmd_list, true);
+                                                       if (NULL != filtered_id) {
+                                                               free(filtered_id);
+                                                               filtered_id = NULL;
+                                                       }
+                                                       return VCD_ERROR_NONE;
+                                               }
+                                       }
+                               }
+                       }
+               }
+       }
+
        int is_action = 0;
        for (i = 0; i < filtered_count; i++) {
                SLOG(LOG_INFO, TAG_VCD, "[Server]   [%d] Filtered Result id(%d)", i, filtered_id[i]);
@@ -630,7 +647,7 @@ int vcd_send_result(vce_result_event_e event, int* result_id, int count, const c
        }
 
        if (NULL != nlu_result) {
-               SLOG(LOG_INFO, TAG_VCD, "[Server] NLU (%s)", nlu_result);
+               SECURE_SLOG(LOG_INFO, TAG_VCD, "[Server] NLU (%s)", nlu_result);
                vc_info_parser_set_nlu_result(nlu_result);
                if (0 == is_action) {
                        vc_cmd_h nlu_cmd;
@@ -664,7 +681,7 @@ int vcd_send_result(vce_result_event_e event, int* result_id, int count, const c
        if (0 == result_count) {
                /* No result */
                if (NULL != all_result) {
-                       SLOG(LOG_DEBUG, TAG_VCD, "[Server] Engine result is no command : %s", all_result);
+                       SECURE_SLOG(LOG_DEBUG, TAG_VCD, "[Server] Engine result is no command : %s", all_result);
                } else {
                        SLOG(LOG_DEBUG, TAG_VCD, "[Server] Engine result is NULL");
                }
@@ -880,8 +897,8 @@ int vcd_send_result(vce_result_event_e event, int* result_id, int count, const c
 #if 0
 static void __vcd_server_nlu_result_cb(vce_result_event_e event, const char* nlu_result, void *user_data)
 {
-       SLOG(LOG_DEBUG, TAG_VCD, "[Server] NLU result cb - event(%d)", event);
-       SLOG(LOG_DEBUG, TAG_VCD, "[Server] result (%s)", nlu_result);
+       SECURE_SLOG(LOG_DEBUG, TAG_VCD, "[Server] NLU result cb - event(%d)", event);
+       SECURE_SLOG(LOG_DEBUG, TAG_VCD, "[Server] result (%s)", nlu_result);
 
        int ret = vc_info_parser_set_nlu_result(nlu_result);
        if (0 != ret) {
@@ -1010,7 +1027,7 @@ 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;
 }
@@ -1055,14 +1072,14 @@ 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;
 }
 
 static Eina_Bool __finalize_quit_ecore_loop(void *data)
 {
-       SLOG(LOG_DEBUG, TAG_VCD, "[Server] quit ecore main loop");
+       SLOG(LOG_ERROR, TAG_VCD, "[Server] quit ecore main loop");
        ecore_main_loop_quit();
        return EINA_FALSE;
 }
@@ -1311,7 +1328,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;
 }
@@ -1340,7 +1357,7 @@ int vcd_server_mgr_finalize(int pid)
                ecore_timer_add(0, __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;
 }
@@ -1451,6 +1468,7 @@ int vcd_server_mgr_set_client_info(int pid)
 static int __start_internal_recognition()
 {
        int ret;
+       vcd_client_widget_set_waiting_for_recording(-1, false);
 
        if (0 != vcd_client_command_collect_command()) {
                SLOG(LOG_ERROR, TAG_VCD, "[Client Data ERROR] Fail to collect command");
@@ -1472,7 +1490,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()) {
@@ -1492,7 +1510,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 */
@@ -1511,7 +1529,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;
 }
@@ -1520,7 +1538,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);
        }
 
@@ -1540,8 +1558,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);
 
-       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) {
@@ -1550,8 +1569,9 @@ 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);
                                return 0;
                        }
                }
@@ -1572,6 +1592,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);
@@ -1582,6 +1604,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;
 }
 
@@ -1596,6 +1619,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 */
@@ -1612,6 +1636,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;
 }
 
@@ -1633,9 +1658,21 @@ int vcd_server_mgr_cancel()
        if (VCD_STATE_READY == state) {
                SLOG(LOG_DEBUG, TAG_VCD, "[Server] Current state is READY");
                vcd_recorder_stop();
+
+               if (false == vcd_client_manager_get_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_DEBUG, TAG_VCD, "[Server] Request tooltip hide");
+                                       ecore_timer_add(0, __vcd_request_show_tooltip, (void*)false);
+                               }
+                       }
+               }
+
                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 */
@@ -1664,6 +1701,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;
 }
 
@@ -1804,7 +1842,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;
@@ -1830,7 +1868,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;
@@ -2168,6 +2206,14 @@ int vcd_server_widget_initialize(int pid)
        return VCD_ERROR_NONE;
 }
 
+static void __vcd_server_widget_start_recording(void *data)
+{
+       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)
 {
        /* check if pid is valid */
@@ -2182,10 +2228,20 @@ int vcd_server_widget_finalize(int pid)
        }
 
        if (0 == vcd_client_get_ref_count()) {
-               SLOG(LOG_DEBUG, TAG_VCD, "[Server] connected client list is empty");
+               SLOG(LOG_ERROR, TAG_VCD, "[Server] connected client list is empty");
                ecore_timer_add(0, __finalize_quit_ecore_loop, NULL);
+               return 0;
        }
 
+       bool is_waiting = false;
+       if (0 != vcd_client_widget_get_waiting_for_recording(pid, &is_waiting)) {
+               SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Fail to get waiting to recording");
+       }
+
+       if (true == is_waiting) {
+               SLOG(LOG_INFO, 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;
 }
 
@@ -2205,6 +2261,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);