2 * Copyright (c) 2011-2015 Samsung Electronics Co., Ltd All Rights Reserved
4 * Licensed under the Apache License, Version 2.0 (the License);
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an AS IS BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 #include <app_control.h>
18 #include <app_manager.h>
20 #include <sound_manager.h>
22 #include "vc_cmd_db.h"
23 #include "vc_info_parser.h"
25 #include "vcd_server.h"
26 #include "vcd_client_data.h"
28 #include "vcd_engine_agent.h"
29 #include "vcd_config.h"
30 #include "vcd_recorder.h"
33 #include "voice_control_command_expand.h"
34 #include "voice_control_common.h"
37 * VC Server static variable
39 static GList *g_proc_list = NULL;
41 static Ecore_Timer *g_restart_timer = NULL;
42 static Ecore_Timer *g_check_widget_client_timer = NULL;
45 * @brief Enumerations of send event type.
48 VCD_SEND_EVENT_TYPE_TEXT, /**< send text event to vc engine*/
49 VCD_SEND_EVENT_TYPE_LIST_EVENT, /**< send list event to vc engine */
50 VCD_SEND_EVENT_TYPE_HAPTIC_EVENT /**< send haptic event to vc engine */
51 } vcd_send_event_type_e;
53 static int __vcd_server_launch_manager_app();
56 * VC Server Internal Functions
58 static Eina_Bool __stop_by_silence(void *data)
60 SLOG(LOG_DEBUG, TAG_VCD, "@@@ Silence Detected ");
62 vcd_server_mgr_stop();
64 SLOG(LOG_DEBUG, TAG_VCD, "@@@");
68 static Eina_Bool __cancel_by_interrupt(void *data)
70 SLOG(LOG_DEBUG, TAG_VCD, "@@@ Cancel by interrupt");
72 vcd_server_mgr_cancel();
74 SLOG(LOG_DEBUG, TAG_VCD, "@@@");
78 static void __cancel_by_error(void *data)
80 SLOG(LOG_DEBUG, TAG_VCD, "@@@ Cancel by error");
82 vcd_server_mgr_cancel();
84 SLOG(LOG_DEBUG, TAG_VCD, "@@@");
88 static Eina_Bool __restart_engine(void *data)
90 SLOG(LOG_DEBUG, TAG_VCD, "@@@ Restart by no result");
92 g_restart_timer = NULL;
94 /* Restart recognition */
95 int ret = vcd_engine_recognize_start(true);
97 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to restart recognition : result(%d)", ret);
101 SLOG(LOG_DEBUG, TAG_VCD, "[Server Success] Start engine");
103 if (VCD_RECOGNITION_MODE_RESTART_AFTER_REJECT == vcd_client_get_recognition_mode()) {
104 vcd_config_set_service_state(VCD_STATE_RECORDING);
105 vcdc_send_service_state(VCD_STATE_RECORDING);
108 SLOG(LOG_DEBUG, TAG_VCD, "[Server Success] Restart recognition");
110 SLOG(LOG_DEBUG, TAG_VCD, "@@@");
114 static int __server_recorder_callback(const void* data, const unsigned int length)
116 vcd_state_e state = vcd_config_get_service_state();
117 if (VCD_STATE_READY == state) {
118 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Ready state, but recording");
119 } else if (VCD_STATE_PROCESSING == state) {
123 vce_speech_detect_e speech_detected = VCE_SPEECH_DETECT_NONE;
126 ret = vcd_engine_recognize_audio(data, length, &speech_detected);
130 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to set recording data to engine(%d)", ret);
131 ecore_timer_add(0, __cancel_by_interrupt, NULL);
132 /* Send error cb to manager */
133 if (VCE_ERROR_OUT_OF_NETWORK == ret) {
134 vcdc_send_error_signal_to_manager(vcd_client_manager_get_pid(), VCD_ERROR_TIMED_OUT, "Engine connection failed");
136 vcdc_send_error_signal_to_manager(vcd_client_manager_get_pid(), VCD_ERROR_OPERATION_FAILED, "Engine recognition failed");
141 if (VCE_SPEECH_DETECT_BEGIN == speech_detected) {
142 if (-1 != vcd_client_manager_get_pid()) {
143 /* Manager client is available */
144 if (0 != vcdc_send_speech_detected(vcd_client_manager_get_pid())) {
145 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Fail to send speech detected");
148 } else if (VCE_SPEECH_DETECT_END == speech_detected) {
149 if (VCD_RECOGNITION_MODE_STOP_BY_SILENCE == vcd_client_get_recognition_mode()) {
150 /* silence detected */
151 ecore_timer_add(0, __stop_by_silence, NULL);
152 } else if (VCD_RECOGNITION_MODE_RESTART_AFTER_REJECT == vcd_client_get_recognition_mode()) {
153 /* Stop engine recognition */
154 int ret = vcd_engine_recognize_stop();
156 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to stop recognition : %d", ret);
158 vcd_config_set_service_state(VCD_STATE_PROCESSING);
159 vcdc_send_service_state(VCD_STATE_PROCESSING);
161 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Stop engine only by silence");
162 } else if (VCD_RECOGNITION_MODE_RESTART_CONTINUOUSLY == vcd_client_get_recognition_mode()) {
163 /* Stop engine recognition */
164 int ret = vcd_engine_recognize_stop();
166 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to stop recognition : %d", ret);
174 void __server_recorder_interrupt_callback()
176 SLOG(LOG_DEBUG, TAG_VCD, "@@@ Cancel by sound interrupt");
178 ecore_timer_add(0, __cancel_by_interrupt, NULL);
180 SLOG(LOG_DEBUG, TAG_VCD, "@@@");
183 static void __config_lang_changed_cb(const char* current_lang, void* user_data)
185 SLOG(LOG_DEBUG, TAG_VCD, "@@@ Change language ");
187 /* Current state is recording */
188 vcd_state_e state = vcd_config_get_service_state();
189 if (VCD_STATE_RECORDING == state || VCD_STATE_PROCESSING == state) {
190 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Current state is 'Recording'. Cancel recognition");
191 vcd_server_mgr_cancel();
195 ret = vcd_engine_set_current_language(current_lang);
197 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to set language of engine : %d", ret);
200 SLOG(LOG_DEBUG, TAG_VCD, "@@@");
205 static void __config_foreground_changed_cb(int previous, int current, void* user_data)
207 SLOG(LOG_DEBUG, TAG_VCD, "@@@ Change foreground");
209 SLOG(LOG_DEBUG, TAG_VCD, "Foreground pid(%d)", current);
211 if (VC_NO_FOREGROUND_PID != current) {
212 /* Foreground app is changed */
213 vcd_state_e state = vcd_config_get_service_state();
214 if (VCD_STATE_RECORDING == state) {
215 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Foreground pid(%d) is changed. Cancel recognition", current);
216 ecore_timer_add(0, __cancel_by_interrupt, NULL);
220 SLOG(LOG_DEBUG, TAG_VCD, "@@@");
225 static int __vcd_activate_app_by_appcontrol(const char* appid)
228 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Invalid parameter");
229 return VCD_ERROR_INVALID_PARAMETER;
233 app_control_h app_control = NULL;
234 ret = app_control_create(&app_control);
235 if (APP_CONTROL_ERROR_NONE == ret) {
237 ret = app_control_add_extra_data(app_control, "voice_launch", "get_result");
238 if (APP_CONTROL_ERROR_NONE != ret) {
239 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Fail to add extra data, ret(%d)", ret);
240 app_control_destroy(app_control);
241 return VCD_ERROR_OPERATION_FAILED;
244 ret = app_control_set_app_id(app_control, appid);
245 if (APP_CONTROL_ERROR_NONE != ret) {
246 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Fail to set app id, ret(%d)", ret);
247 app_control_destroy(app_control);
248 return VCD_ERROR_OPERATION_FAILED;
250 // Sent launch request
251 ret = app_control_send_launch_request(app_control, NULL, NULL);
252 if (APP_CONTROL_ERROR_NONE != ret) {
253 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Fail to send launch request, ret(%d)", ret);
254 app_control_destroy(app_control);
255 return VCD_ERROR_OPERATION_FAILED;
257 // Destroy app control
258 ret = app_control_destroy(app_control);
259 if (APP_CONTROL_ERROR_NONE != ret) {
260 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Fail to destroy, ret(%d)", ret);
261 return VCD_ERROR_OPERATION_FAILED;
264 return VCD_ERROR_NONE;
267 static int __vcd_resume_app(const char* appid)
269 app_context_h app_context = NULL;
270 int ret = app_manager_get_app_context(appid, &app_context);
271 if (APP_MANAGER_ERROR_NONE != ret) {
272 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Fail to get app_context, ret(%d), appid(%s)", ret, appid);
273 return VCD_ERROR_OPERATION_FAILED;
276 ret = app_manager_resume_app(app_context);
277 if (APP_MANAGER_ERROR_NONE != ret) {
278 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Fail to resume app, ret(%d), appid(%s)", ret, appid);
279 return VCD_ERROR_OPERATION_FAILED;
281 return VCD_ERROR_NONE;
284 static bool __vcd_is_package_installed(const char* appid)
286 app_info_h app_info = NULL;
287 int ret = app_manager_get_app_info(appid, &app_info);
288 if (APP_MANAGER_ERROR_NONE != ret || NULL == app_info)
290 ret = app_info_destroy(app_info);
291 if (APP_MANAGER_ERROR_NONE != ret)
292 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Fail to destroy app_info, ret(%d)", ret);
296 static bool __vcd_launch_app(const char* result)
298 if (NULL == result) {
299 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Invalid parameter");
300 return VCD_ERROR_INVALID_PARAMETER;
303 GSList* app_list = NULL;
304 if (0 != vc_db_get_appid_list(result, &app_list)) {
305 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] result text is NULL");
306 return VCD_ERROR_INVALID_PARAMETER;
309 if (0 != g_slist_length(app_list)) {
312 vc_deactivated_app_s* temp_app = NULL;
313 iter = g_slist_nth(app_list, 0);
315 while (NULL != iter) {
316 temp_app = iter->data;
318 if (NULL != temp_app) {
319 if (NULL != temp_app->appid) {
321 bool running = false;
322 ret = app_manager_is_running(temp_app->appid, &running);
323 if (APP_MANAGER_ERROR_NONE != ret) {
324 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Fail to check running with appid(%s)", temp_app->appid);
325 free(temp_app->appid);
326 temp_app->appid = NULL;
329 return VCD_ERROR_OPERATION_FAILED;
331 if (false == running) {
332 int tmp_ret = __vcd_is_package_installed(temp_app->appid);
333 if (false == tmp_ret) {
334 SLOG(LOG_WARN, TAG_VCD, "[WARNING] app is not installed, appid(%s)", temp_app->appid);
336 ret = __vcd_activate_app_by_appcontrol(temp_app->appid);
338 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Fail to activate app");
339 free(temp_app->appid);
340 temp_app->appid = NULL;
345 SLOG(LOG_ERROR, TAG_VCD, "Launch app: appid(%s) result(%s)", temp_app->appid, result);
348 ret = __vcd_resume_app(temp_app->appid);
350 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Fail to resume app");
351 free(temp_app->appid);
352 temp_app->appid = NULL;
357 SLOG(LOG_ERROR, TAG_VCD, "Resume app: appid(%s) result(%s)", temp_app->appid, result);
359 free(temp_app->appid);
360 temp_app->appid = NULL;
365 iter = g_slist_next(iter);
370 return VCD_ERROR_NONE;
373 static Eina_Bool __vcd_send_selected_result(void *data)
375 GSList* pid_list = NULL;
376 const char* result = vcd_client_manager_get_result_text();
378 if (0 != vc_info_parser_get_result_pid_list(&pid_list, result)) {
379 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Fail to get pid list. No result");
381 if (0 < g_slist_length(pid_list)) {
383 vc_cmd_s* temp_cmd = NULL;
388 iter = g_slist_nth(pid_list, 0);
389 while (NULL != iter) {
390 temp_cmd = iter->data;
392 if (NULL != temp_cmd && (pre_pid != temp_cmd->pid || pre_type == VC_COMMAND_TYPE_WIDGET || temp_cmd->type == VC_COMMAND_TYPE_WIDGET)) {
393 /* Launch deactivated several apps that is matched with result */
394 ret = __vcd_launch_app(result);
396 SLOG(LOG_ERROR, TAG_VCD, "Fail to launch or resume app, ret(%d) result(%s)", ret, result);
398 /* send result noti */
399 ret = vcdc_send_result(temp_cmd->pid, vcd_client_manager_get_pid(), temp_cmd->type);
401 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Fail to send result, ret(%d)", ret);
404 SLOG(LOG_ERROR, TAG_VCD, "[Server] Send result : pid(%d) type(%d)", temp_cmd->pid, temp_cmd->type);
405 pre_pid = temp_cmd->pid;
406 pre_type = temp_cmd->type;
412 pid_list = g_slist_remove_link(pid_list, iter);
413 iter = g_slist_nth(pid_list, 0);
418 if (VCD_RECOGNITION_MODE_RESTART_CONTINUOUSLY != vcd_client_get_recognition_mode()) {
419 vcd_config_set_service_state(VCD_STATE_READY);
420 vcdc_send_service_state(VCD_STATE_READY);
426 int vcd_send_asr_result(vce_asr_result_event_e event, const char* asr_result, void *user_data)
428 if (NULL != asr_result) {
429 SECURE_SLOG(LOG_DEBUG, TAG_VCD, "[Server] ASR result - Event(%d), Text(%s)", event, asr_result);
430 vcdc_send_pre_result_to_manager(vcd_client_manager_get_pid(), event, asr_result);
433 return VCD_ERROR_NONE;
436 int vcd_send_nlg_result(const char* nlg_result, void *user_data)
438 int ret = __vcd_server_launch_manager_app();
440 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);
444 SLOG(LOG_ERROR, TAG_VCD, "[Server] send dialog : mgr_pid(%d), nlg_result(%s)", vcd_client_manager_get_pid(), nlg_result);
445 ret = vcdc_send_dialog(vcd_client_manager_get_pid(), -1, nlg_result, NULL, 0); //0: VC_DIALOG_END
447 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);
451 static void* __recorder_stop(void *data)
457 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)
461 if (VCD_STATE_PROCESSING != vcd_config_get_service_state()) {
462 if (VCD_RECOGNITION_MODE_RESTART_CONTINUOUSLY != vcd_client_get_recognition_mode()) {
463 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Current state is not 'Processing' and mode is not 'Restart continuously'");
464 return VCD_ERROR_INVALID_STATE;
468 vc_info_parser_unset_result(vcd_client_manager_get_exclusive());
469 vcd_client_manager_set_result_text(all_result);
471 SECURE_SLOG(LOG_INFO, TAG_VCD, "[Server] Event(%d), Text(%s) Nonfixed(%s) Msg(%s) Result count(%d)",
472 event, all_result, non_fixed_result, msg, count);
474 SLOG(LOG_ERROR, TAG_VCD, "[Server] NLU result(%s)", nlu_result);
476 if (VCD_RECOGNITION_MODE_RESTART_AFTER_REJECT == vcd_client_get_recognition_mode()) {
477 if (VCE_RESULT_EVENT_REJECTED == event) {
478 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Restart by no or rejected result");
479 /* If no result and restart option is ON */
480 /* Send reject message */
481 bool temp = vcd_client_manager_get_exclusive();
482 vc_info_parser_set_result(all_result, event, msg, NULL, temp);
483 ret = vcdc_send_result_to_manager(vcd_client_manager_get_pid(), VC_RESULT_TYPE_NOTIFICATION);
485 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Fail to send result");
488 g_restart_timer = ecore_timer_add(0, __restart_engine, NULL);
491 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Stop recorder due to success");
492 //vcd_recorder_stop();
493 ecore_main_loop_thread_safe_call_sync(__recorder_stop, NULL);
494 } else if (VCD_RECOGNITION_MODE_RESTART_CONTINUOUSLY == vcd_client_get_recognition_mode()) {
495 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Restart continuously");
496 /* Restart option is ON */
497 g_restart_timer = ecore_timer_add(0, __restart_engine, NULL);
498 if (VCE_RESULT_EVENT_REJECTED == event) {
499 bool temp = vcd_client_manager_get_exclusive();
500 vc_info_parser_set_result(all_result, event, msg, NULL, temp);
501 ret = vcdc_send_result_to_manager(vcd_client_manager_get_pid(), VC_RESULT_TYPE_NOTIFICATION);
503 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Fail to send result");
509 /* if nlu_result is exist, Add command handle(is_action) into result list */
511 SLOG(LOG_DEBUG, TAG_VCD, "[Server] @ Get engine result @");
513 vc_cmd_s* temp_cmd = NULL;
514 vc_cmd_list_h vc_cmd_list = NULL;
516 if (0 != vc_cmd_list_create(&vc_cmd_list)) {
517 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Fail to create command list");
518 vcd_client_manager_set_exclusive(false);
519 vcd_config_set_service_state(VCD_STATE_READY);
520 vcdc_send_service_state(VCD_STATE_READY);
521 return VCD_ERROR_NONE;
524 /* priority filter */
525 /* system > exclusive > widget > foreground > system_background > background */
527 int* filtered_id = (int*)calloc(count, sizeof(int));
529 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to allocate memory");
530 return VCD_ERROR_OUT_OF_MEMORY;
532 int filtered_count = 0;
533 int top_priority = VC_COMMAND_PRIORITY_BACKGROUND;
534 for (i = 0; i < count; i++) {
535 SLOG(LOG_INFO, TAG_VCD, "[Server] [%d] Result id(%d)", i, result_id[i]);
537 if (0 > result_id[i]) {
538 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Result ID(%d) is NOT valid", result_id[i]);
542 ret = vcd_client_get_cmd_from_result_id(result_id[i], &temp_cmd);
543 if (0 == ret && NULL != temp_cmd) {
544 if (top_priority == temp_cmd->priority) {
545 filtered_id[filtered_count] = result_id[i];
547 } else if (top_priority > temp_cmd->priority) {
548 filtered_id[0] = result_id[i];
550 top_priority = temp_cmd->priority;
553 vc_cmd_destroy((vc_cmd_h)temp_cmd);
559 if (top_priority >= VC_COMMAND_PRIORITY_WIDGET) {
560 int pid = vcd_client_widget_get_foreground_pid();
562 if (NULL != all_result) {
563 vc_info_parser_set_result(all_result, event, msg, NULL, false);
565 vcd_client_widget_get_asr_result_enabled(pid, &enable);
566 if (true == enable) {
567 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Send ASR result to Widget client");
568 bool is_consumed = false;
569 if (NULL != user_info) {
572 if (0 != vcdc_send_asr_result(pid, event, all_result, VC_COMMAND_TYPE_WIDGET, &is_consumed)) {
573 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to send asr result");
575 SLOG(LOG_DEBUG, TAG_VCD, "[Server] ASR result is consumed(%d)", is_consumed);
576 if (true == is_consumed) {
577 if (NULL != user_info) {
579 SLOG(LOG_ERROR, TAG_VCD, "[Server] Send whether ASR result is consumed or not (%d)", *user_info);
581 vcdc_send_show_tooltip(pid, false);
582 if (-1 != vcd_client_manager_get_pid()) {
583 /* Manager client is available */
584 vc_info_parser_unset_result(false);
585 vc_info_parser_set_result(all_result, VC_RESULT_EVENT_RESULT_SUCCESS, msg, NULL, false);
586 if (0 != vcdc_send_result_to_manager(vcd_client_manager_get_pid(), VC_RESULT_TYPE_NOTIFICATION)) {
587 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Fail to send result");
591 vcd_client_manager_set_exclusive(false);
593 vcd_config_set_service_state(VCD_STATE_READY);
594 vcdc_send_service_state(VCD_STATE_READY);
595 vc_cmd_list_destroy(vc_cmd_list, true);
596 if (NULL != filtered_id) {
600 return VCD_ERROR_NONE;
609 for (i = 0; i < filtered_count; i++) {
610 SLOG(LOG_INFO, TAG_VCD, "[Server] [%d] Filtered Result id(%d)", i, filtered_id[i]);
612 if (filtered_id[i] < 0) {
613 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Filtered ID(%d) is NOT valid", filtered_id[i]);
617 ret = vcd_client_get_cmd_from_result_id(filtered_id[i], &temp_cmd);
618 if (0 == ret && NULL != temp_cmd) {
619 switch (temp_cmd->format) {
620 case VC_CMD_FORMAT_FIXED:
621 case VC_CMD_FORMAT_FIXED_AND_VFIXED:
622 case VC_CMD_FORMAT_VFIXED_AND_FIXED:
623 case VC_CMD_FORMAT_PARTIAL:
624 case VC_CMD_FORMAT_FIXED_AND_NONFIXED:
625 case VC_CMD_FORMAT_NONFIXED_AND_FIXED:
627 case VC_CMD_FORMAT_ACTION:
631 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Unknown command type : %d", temp_cmd->type);
635 if (0 != vc_cmd_list_add(vc_cmd_list, (vc_cmd_h)temp_cmd)) {
636 SLOG(LOG_DEBUG, TAG_VCD, "Fail to add command to list");
637 vc_cmd_destroy((vc_cmd_h)temp_cmd);
641 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] NOT found matached result(%d)", filtered_id[i]);
645 if (NULL != filtered_id) {
650 if (NULL != nlu_result) {
651 SECURE_SLOG(LOG_INFO, TAG_VCD, "[Server] NLU (%s)", nlu_result);
652 vc_info_parser_set_nlu_result(nlu_result);
653 if (0 == is_action) {
655 if (0 != vc_cmd_create(&nlu_cmd)) {
656 SLOG(LOG_ERROR, TAG_VCD, "Fail to nlu cmd create");
658 if (0 != vc_cmd_set_type(nlu_cmd, VC_COMMAND_TYPE_SYSTEM)) {
659 SLOG(LOG_ERROR, TAG_VCD, "Fail to set type");
661 if (0 != vc_cmd_set_pid(nlu_cmd, vcd_client_manager_get_pid())) {
662 SLOG(LOG_ERROR, TAG_VCD, "Fail to set pid");
664 if (0 != vc_cmd_set_format(nlu_cmd, VC_CMD_FORMAT_ACTION)) {
665 SLOG(LOG_ERROR, TAG_VCD, "Fail to set format");
667 if (0 != vc_cmd_list_add(vc_cmd_list, nlu_cmd)) {
668 SLOG(LOG_ERROR, TAG_VCD, "Fail to add nlu cmd to list");
669 vc_cmd_destroy(nlu_cmd);
675 vc_cmd_print_list(vc_cmd_list);
677 SLOG(LOG_DEBUG, TAG_VCD, "[Server] @@@@");
679 int result_count = 0;
680 vc_cmd_list_get_count(vc_cmd_list, &result_count);
682 if (0 == result_count) {
684 if (NULL != all_result) {
685 SECURE_SLOG(LOG_DEBUG, TAG_VCD, "[Server] Engine result is no command : %s", all_result);
687 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Engine result is NULL");
689 bool temp = vcd_client_manager_get_exclusive();
690 vc_info_parser_set_result(all_result, event, msg, NULL, temp);
692 int pid = vcd_client_widget_get_foreground_pid();
694 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Request tooltip hide");
695 /* Send to hide tooltip */
696 vcdc_send_show_tooltip(pid, false);
699 if (-1 != vcd_client_manager_get_pid()) {
700 /* Manager client is available */
701 if (0 != vcdc_send_result_to_manager(vcd_client_manager_get_pid(), VC_RESULT_TYPE_NORMAL)) {
702 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Fail to send result");
706 vcd_client_manager_set_exclusive(false);
708 return VCD_ERROR_NONE;
710 if (false == vcd_client_manager_get_exclusive()) {
711 int pid = vcd_client_widget_get_foreground_pid();
713 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Request tooltip hide");
714 vcdc_send_show_tooltip(pid, false);
717 vc_info_parser_set_result(all_result, event, msg, vc_cmd_list, false);
719 if (-1 != vcd_client_manager_get_pid()) {
720 /* Manager client is available */
721 if (0 != vcdc_send_result_to_manager(vcd_client_manager_get_pid(), VC_RESULT_TYPE_NORMAL)) {
722 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Fail to send result");
725 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Manager is NOT available. Send result to client directly");
726 /* Send result to client */
727 ecore_timer_add(0, __vcd_send_selected_result, NULL);
730 /* exclusive command */
731 vc_info_parser_set_result(all_result, event, msg, vc_cmd_list, true);
733 if (-1 != vcd_client_manager_get_pid()) {
734 /* Manager client is available */
735 if (0 != vcdc_send_result_to_manager(vcd_client_manager_get_pid(), VC_RESULT_TYPE_NORMAL)) {
736 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Fail to send result");
739 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Manager is NOT available");
742 vcd_client_manager_set_exclusive(false);
746 vc_cmd_list_destroy(vc_cmd_list, true);
748 return VCD_ERROR_NONE;
752 if (NULL == result_id) {
754 if (NULL != all_result) {
755 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Engine result is no command : %s", all_result);
756 bool temp = vcd_client_manager_get_exclusive();
757 vc_info_parser_set_result(all_result, event, msg, NULL, temp);
760 int pid = vcd_client_widget_get_foreground_pid();
762 if (NULL != all_result) {
763 /* Send result text to widget */
764 vcdc_send_result(pid, vcd_client_manager_get_pid(), VC_COMMAND_TYPE_WIDGET);
767 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Request tooltip hide");
768 /* Send to hide tooltip */
769 vcdc_send_show_tooltip(pid, false);
772 if (-1 != vcd_client_manager_get_pid()) {
773 /* Manager client is available */
774 if (0 != vcdc_send_result_to_manager(vcd_client_manager_get_pid(), VC_RESULT_TYPE_NORMAL)) {
775 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Fail to send result");
779 vcd_client_manager_set_exclusive(false);
785 SLOG(LOG_DEBUG, TAG_VCD, "[Server] @ Get engine result @");
788 vc_cmd_s* temp_cmd = NULL;
789 vc_cmd_list_h vc_cmd_list = NULL;
791 if (0 != vc_cmd_list_create(&vc_cmd_list)) {
792 SLOG(LOG_ERROR, TAG_VCD, "[Server] Fail to create command list");
793 vcd_client_manager_set_exclusive(false);
794 vcd_config_set_service_state(VCD_STATE_READY);
795 vcdc_send_service_state(VCD_STATE_READY);
800 for (i = 0; i < count; i++) {
801 SLOG(LOG_DEBUG, TAG_VCD, "[Server] [%d] Result ID(%d)", i, result_id[i]);
803 if (result_id[i] < 0) {
804 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Result ID(%d) is NOT valid", result_id[i]);
808 ret = vcd_client_get_cmd_from_result_id(result_id[i], &temp_cmd);
809 if (0 == ret && NULL != temp_cmd) {
810 switch (temp_cmd->format) {
811 case VC_CMD_FORMAT_FIXED:
812 case VC_CMD_FORMAT_FIXED_AND_VFIXED:
813 case VC_CMD_FORMAT_VFIXED_AND_FIXED:
814 case VC_CMD_FORMAT_PARTIAL:
815 /* Nonfixed result is NOT valid */
817 case VC_CMD_FORMAT_FIXED_AND_NONFIXED:
818 if (NULL == temp_cmd->parameter) {
819 if (NULL != non_fixed_result) {
820 temp_cmd->parameter = strdup(non_fixed_result);
823 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Command type is NOT vaild. Parameter (%s)", temp_cmd->parameter);
826 case VC_CMD_FORMAT_NONFIXED_AND_FIXED:
827 if (NULL == temp_cmd->command) {
828 if (NULL != non_fixed_result) {
829 temp_cmd->command = strdup(non_fixed_result);
832 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Command type is NOT vaild. Command (%s)", temp_cmd->command);
837 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Unknown command type : %d", temp_cmd->type);
841 if (0 != vc_cmd_list_add(vc_cmd_list, (vc_cmd_h)temp_cmd)) {
842 SLOG(LOG_DEBUG, TAG_VCD, "Fail to add command to list");
843 vc_cmd_destroy((vc_cmd_h)temp_cmd);
846 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] NOT found matached result(%d)", result_id[i]);
850 vc_cmd_print_list(vc_cmd_list);
852 SLOG(LOG_DEBUG, TAG_VCD, "[Server] @@@@");
854 int result_count = 0;
855 vc_cmd_list_get_count(vc_cmd_list, &result_count);
857 if (false == vcd_client_manager_get_exclusive()) {
858 int pid = vcd_client_widget_get_foreground_pid();
860 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Request tooltip hide");
861 vcdc_send_show_tooltip(pid, false);
864 vc_info_parser_set_result(all_result, event, msg, vc_cmd_list, false);
866 if (-1 != vcd_client_manager_get_pid()) {
867 /* Manager client is available */
868 if (0 != vcdc_send_result_to_manager(vcd_client_manager_get_pid(), VC_RESULT_TYPE_NORMAL)) {
869 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Fail to send result");
872 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Manager is NOT available. Send result to client directly");
873 /* Send result to client */
874 ecore_timer_add(0, __vcd_send_selected_result, NULL);
877 /* exclusive command */
878 vc_info_parser_set_result(all_result, event, msg, vc_cmd_list, true);
880 if (-1 != vcd_client_manager_get_pid()) {
881 /* Manager client is available */
882 if (0 != vcdc_send_result_to_manager(vcd_client_manager_get_pid(), VC_RESULT_TYPE_NORMAL)) {
883 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Fail to send result");
886 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Manager is NOT available");
889 vcd_client_manager_set_exclusive(false);
892 vc_cmd_list_destroy(vc_cmd_list, true);
899 static void __vcd_server_nlu_result_cb(vce_result_event_e event, const char* nlu_result, void *user_data)
901 SECURE_SLOG(LOG_DEBUG, TAG_VCD, "[Server] NLU result cb - event(%d)", event);
902 SECURE_SLOG(LOG_DEBUG, TAG_VCD, "[Server] result (%s)", nlu_result);
904 int ret = vc_info_parser_set_nlu_result(nlu_result);
906 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to set nlu result (%d)", ret);
913 int vcd_send_error(vce_error_e error, const char* msg, void *user_data)
915 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Engine Error cb - reason(%d), msg(%s)", error, msg);
916 ecore_main_loop_thread_safe_call_async(__cancel_by_error, NULL);
918 char* error_msg = NULL;
920 error_msg = strdup(msg);
923 if (0 != vcdc_send_error_signal_to_manager(vcd_client_manager_get_pid(), error, error_msg)) {
924 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to send error signal");
927 if (NULL != error_msg) {
932 return VCD_ERROR_NONE;
936 * vcd server Interfaces
938 static void __vcd_file_clean_up()
940 SLOG(LOG_DEBUG, TAG_VCD, "== Old file clean up == ");
943 struct dirent *dirp = NULL;
945 dp = opendir(VC_RUNTIME_INFO_ROOT);
947 SLOG(LOG_ERROR, TAG_VCD, "[File message WARN] Fail to open path : %s", VC_RUNTIME_INFO_ROOT);
951 char remove_path[256] = {0, };
956 if (!strncmp("vc_", dirp->d_name, strlen("vc_"))) {
957 memset(remove_path, 0, 256);
958 snprintf(remove_path, 256, "%s/%s", VC_RUNTIME_INFO_ROOT, dirp->d_name);
961 if (0 != remove(remove_path)) {
962 SLOG(LOG_WARN, TAG_VCD, "[File message WARN] Fail to remove file : %s", remove_path);
964 SLOG(LOG_DEBUG, TAG_VCD, "[File message] Remove file : %s", remove_path);
968 } while (NULL != dirp);
975 static int __vcd_db_clean_up()
978 int cnt = VC_COMMAND_TYPE_FOREGROUND;
980 if (VC_COMMAND_TYPE_BACKGROUND != cnt)
981 ret = vc_db_delete_commands(-1, cnt, NULL);
982 } while (VC_COMMAND_TYPE_EXCLUSIVE >= ++cnt);
987 int vcd_initialize(vce_request_callback_s *callback)
991 /* Remove old file */
992 __vcd_file_clean_up();
994 /* initialize modules */
995 ret = vcd_config_initialize(__config_lang_changed_cb, __config_foreground_changed_cb, NULL);
997 SLOG(LOG_ERROR, TAG_VCD, "[Server WARNING] Fail to initialize config.");
1000 /* Remove db data */
1001 ret = __vcd_db_clean_up();
1003 SLOG(LOG_ERROR, TAG_VCD, "[Server WARNING] Fail to remove db data");
1006 vcd_config_set_service_state(VCD_STATE_NONE);
1008 ret = vcd_engine_agent_init();
1010 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to engine agent initialize : result(%d)", ret);
1014 if (0 != vcd_recorder_create(__server_recorder_callback, __server_recorder_interrupt_callback)) {
1015 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to create recorder");
1016 return VCD_ERROR_OPERATION_FAILED;
1020 if (0 != vcd_engine_agent_load_current_engine(callback)) {
1021 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to load current engine");
1022 return VCD_ERROR_OPERATION_FAILED;
1025 /* Initialize manager info */
1026 vcd_client_manager_unset();
1028 vcd_config_set_service_state(VCD_STATE_READY);
1029 vcdc_send_service_state(VCD_STATE_READY);
1031 SLOG(LOG_ERROR, TAG_VCD, "[Server SUCCESS] initialize");
1039 if (0 < g_list_length(g_proc_list)) {
1040 iter = g_list_first(g_proc_list);
1041 while (NULL != iter) {
1042 g_proc_list = g_list_remove_link(g_proc_list, iter);
1043 iter = g_list_first(g_proc_list);
1047 if (g_restart_timer != NULL) {
1048 ecore_timer_del(g_restart_timer);
1049 g_restart_timer = NULL;
1052 vcd_state_e state = vcd_config_get_service_state();
1053 if (VCD_STATE_READY != state) {
1054 if (VCD_STATE_RECORDING == state) {
1055 vcd_recorder_stop();
1057 vcd_engine_recognize_cancel();
1060 if (0 != vcd_recorder_destroy()) {
1061 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to destroy recorder");
1064 SLOG(LOG_DEBUG, TAG_VCD, "[Server] destroy recorder");
1067 if (0 != vcd_engine_agent_release()) {
1068 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to release engine");
1071 SLOG(LOG_DEBUG, TAG_VCD, "[Server] release engine");
1074 vcd_client_manager_unset_appid();
1076 vcd_config_set_service_state(VCD_STATE_NONE);
1077 vcdc_send_service_state(VCD_STATE_NONE);
1079 SLOG(LOG_ERROR, TAG_VCD, "[Server] mode finalize");
1084 static Eina_Bool __finalize_quit_ecore_loop(void *data)
1086 bool ret = vcd_finalize();
1090 ecore_main_loop_quit();
1095 static void __read_proc()
1098 struct dirent *dirp = NULL;
1102 if (0 < g_list_length(g_proc_list)) {
1103 iter = g_list_first(g_proc_list);
1104 while (NULL != iter) {
1105 g_proc_list = g_list_remove_link(g_proc_list, iter);
1106 iter = g_list_first(g_proc_list);
1110 dp = opendir("/proc");
1112 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Fail to open proc");
1118 tmp = atoi(dirp->d_name);
1119 if (0 >= tmp) continue;
1120 g_proc_list = g_list_append(g_proc_list, GINT_TO_POINTER(tmp));
1122 } while (NULL != dirp);
1128 static void __vcd_cleanup_client(vcd_client_type_e type)
1130 int* client_list = NULL;
1131 int client_count = 0;
1138 if (VCD_CLIENT_TYPE_NORMAL == type) {
1139 ret = vcd_client_get_list(&client_list, &client_count);
1140 } else if (VCD_CLIENT_TYPE_WIDGET == type) {
1141 ret = vcd_client_widget_get_list(&client_list, &client_count);
1142 } else if (VCD_CLIENT_TYPE_MANAGER == type) {
1143 mgr_pid = vcd_client_manager_get_pid();
1144 client_list = &mgr_pid;
1146 if (-1 == mgr_pid) {
1147 SLOG(LOG_WARN, TAG_VCD, "[WARNING] Invalid Manager pid");
1152 if (0 == ret || mgr_pid > 0) {
1153 SLOG(LOG_DEBUG, TAG_VCD, "@@@ Clean up %s client ", type ? (type == 1) ? "Widget" : "Manager" : "Normal");
1154 if (NULL != client_list && client_count > 0) {
1155 for (i = 0; i < client_count; i++) {
1158 for (j = 0; j < g_list_length(g_proc_list); j++) {
1159 iter = g_list_nth(g_proc_list, j);
1161 if (*(client_list + i) == GPOINTER_TO_INT(iter->data)) {
1162 SLOG(LOG_DEBUG, TAG_VCD, "%s pid(%d) is running", type ? (type == 1) ? "Widget" : "Manager" : "Normal", *(client_list + i));
1169 if (false == exist) {
1170 SLOG(LOG_ERROR, TAG_VCD, "%s pid(%d) should be removed", type ? (type == 1) ? "Widget" : "Manager" : "Normal", *(client_list + i));
1171 if (VCD_CLIENT_TYPE_NORMAL == type)
1172 vcd_server_finalize(*(client_list + i));
1173 else if (VCD_CLIENT_TYPE_WIDGET == type)
1174 vcd_server_widget_finalize(*(client_list + i));
1176 vcd_server_mgr_finalize(mgr_pid);
1180 SLOG(LOG_DEBUG, TAG_VCD, "@@@");
1182 if (NULL != client_list && -1 == mgr_pid) {
1189 Eina_Bool vcd_cleanup_client_all(void *data)
1193 __vcd_cleanup_client(VCD_CLIENT_TYPE_NORMAL);
1194 __vcd_cleanup_client(VCD_CLIENT_TYPE_WIDGET);
1195 __vcd_cleanup_client(VCD_CLIENT_TYPE_MANAGER);
1198 if (0 == vcd_client_get_list(&client_list, &client_count)) {
1199 SLOG(LOG_DEBUG, TAG_VCD, "@@@ Clean up client ");
1200 if (NULL != client_list && client_count > 0) {
1201 for (i = 0; i < client_count; i++) {
1204 for (j = 0; j < g_list_length(g_proc_list); j++) {
1205 iter = g_list_nth(g_proc_list, j);
1207 if (client_list[i] == GPOINTER_TO_INT(iter->data)) {
1208 SLOG(LOG_DEBUG, TAG_VCD, "pid(%d) is running", client_list[i]);
1215 if (false == exist) {
1216 SLOG(LOG_ERROR, TAG_VCD, "pid(%d) should be removed", client_list[i]);
1217 vcd_server_finalize(client_list[i]);
1220 result = vcdc_send_hello(client_list[i], VCD_CLIENT_TYPE_NORMAL);
1223 SLOG(LOG_DEBUG, TAG_VCD, "[Server] pid(%d) should be removed.", client_list[i]);
1224 vcd_server_finalize(client_list[i]);
1225 } else if (-1 == result) {
1226 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Hello result has error");
1231 SLOG(LOG_DEBUG, TAG_VCD, "@@@");
1233 if (NULL != client_list) {
1238 /* If app is in background state, app cannot response message. */
1239 if (0 == vcd_client_widget_get_list(&client_list, &client_count)) {
1240 SLOG(LOG_DEBUG, TAG_VCD, "@@@ Clean up widget");
1241 if (NULL != client_list && client_count > 0) {
1242 for (i = 0; i < client_count; i++) {
1245 for (j = 0; j < g_list_length(g_proc_list); j++) {
1246 iter = g_list_nth(g_proc_list, j);
1248 if (client_list[i] == GPOINTER_TO_INT(iter->data)) {
1249 SLOG(LOG_DEBUG, TAG_VCD, "widget pid(%d) is running", client_list[i]);
1256 if (false == exist) {
1257 SLOG(LOG_ERROR, TAG_VCD, "widget pid(%d) should be removed", client_list[i]);
1258 vcd_server_widget_finalize(client_list[i]);
1261 result = vcdc_send_hello(client_list[i], VCD_CLIENT_TYPE_WIDGET);
1264 SLOG(LOG_DEBUG, TAG_VCD, "[Server] widget pid(%d) should be removed.", client_list[i]);
1265 vcd_server_widget_finalize(client_list[i]);
1266 } else if (-1 == result) {
1267 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Hello result has error");
1272 SLOG(LOG_DEBUG, TAG_VCD, "@@@");
1275 if (NULL != client_list) {
1283 int mgr_pid = vcd_client_manager_get_pid();
1285 for (j = 0; j < g_list_length(g_proc_list); j++) {
1286 iter = g_list_nth(g_proc_list, j);
1288 if (mgr_pid == GPOINTER_TO_INT(iter->data)) {
1289 SLOG(LOG_DEBUG, TAG_VCD, "manager pid(%d) is running", mgr_pid);
1296 if (false == exist) {
1297 SLOG(LOG_ERROR, TAG_VCD, "manager pid (%d) should be removed", mgr_pid);
1298 vcd_server_mgr_finalize(mgr_pid);
1305 int vcd_server_get_service_state()
1307 return vcd_config_get_service_state();
1310 int vcd_server_get_foreground()
1313 vcd_config_get_foreground(&pid);
1321 int vcd_server_mgr_initialize(int pid)
1323 /* check if pid is valid */
1324 if (false == vcd_client_manager_is_valid(pid)) {
1325 SLOG(LOG_ERROR, TAG_VCD, "[Server] old manager pid(%d) be removed", vcd_client_manager_get_pid());
1326 vcd_server_mgr_cancel();
1327 vcd_client_manager_unset();
1330 /* Add client information to client manager */
1331 if (0 != vcd_client_manager_set(pid)) {
1332 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to add manager");
1333 return VCD_ERROR_OPERATION_FAILED;
1336 if (0 != vcdc_send_manager_pid(pid))
1337 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Fail to send manager pid");
1339 SLOG(LOG_ERROR, TAG_VCD, "[Server Success] Manager initialize : pid(%d)", pid);
1341 return VCD_ERROR_NONE;
1344 int vcd_server_mgr_finalize(int pid)
1346 /* check if pid is valid */
1347 if (false == vcd_client_manager_is_valid(pid)) {
1348 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] The manager pid(%d) is NOT valid", pid);
1349 return VCD_ERROR_INVALID_PARAMETER;
1352 /* Cancel recognition */
1353 vcd_server_mgr_cancel();
1355 if (0 != vcdc_send_manager_pid(-1))
1356 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Fail to send manager pid");
1358 /* Remove manager information */
1359 if (0 != vcd_client_manager_unset()) {
1360 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Fail to delete client");
1363 if (0 == vcd_client_get_ref_count()) {
1364 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Connected client list is empty");
1365 ecore_timer_add(0.5, __finalize_quit_ecore_loop, NULL);
1368 SLOG(LOG_ERROR, TAG_VCD, "[Server Success] Manager Finalize : pid(%d)", pid);
1370 return VCD_ERROR_NONE;
1373 int vcd_server_mgr_set_command(int pid)
1375 if (0 != vcd_client_manager_set_command(pid)) {
1376 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] The manager pid(%d) is NOT valid", pid);
1377 return VCD_ERROR_INVALID_PARAMETER;
1379 return VCD_ERROR_NONE;
1382 int vcd_server_mgr_unset_command(int pid)
1384 if (0 != vcd_client_manager_unset_command(pid)) {
1385 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] The manager pid(%d) is NOT valid", pid);
1386 return VCD_ERROR_INVALID_PARAMETER;
1388 return VCD_ERROR_NONE;
1391 int vcd_server_mgr_set_demandable_client(int pid)
1393 /* check if pid is valid */
1394 if (false == vcd_client_manager_is_valid(pid)) {
1395 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] The manager pid(%d) is NOT valid", pid);
1396 return VCD_ERROR_INVALID_PARAMETER;
1399 GSList* client_list = NULL;
1400 if (0 != vc_info_parser_get_demandable_clients(&client_list)) {
1401 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to get demandable client");
1402 return VCD_ERROR_OPERATION_FAILED;
1405 /* Save client list */
1406 if (0 != vcd_client_manager_set_demandable_client(pid, client_list)) {
1407 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to set demandable client list");
1408 return VCD_ERROR_OPERATION_FAILED;
1411 return VCD_ERROR_NONE;
1414 int vcd_server_mgr_set_audio_type(int pid, const char* audio_type)
1416 /* check if pid is valid */
1417 if (false == vcd_client_manager_is_valid(pid)) {
1418 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] The manager pid(%d) is NOT valid", pid);
1419 return VCD_ERROR_INVALID_PARAMETER;
1423 vce_audio_type_e type = VCE_AUDIO_TYPE_PCM_S16_LE;
1427 ret = vcd_engine_get_audio_format(audio_type, &type, &rate, &channel);
1429 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to get audio format : %d", ret);
1430 return VCD_ERROR_OPERATION_FAILED;
1433 ret = vcd_recorder_set(audio_type, type, rate, channel);
1435 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to set audio in type : %d", ret);
1436 return VCD_ERROR_OPERATION_FAILED;
1439 return VCD_ERROR_NONE;
1442 int vcd_server_mgr_get_audio_type(int pid, char** audio_type)
1444 /* check if pid is valid */
1445 if (false == vcd_client_manager_is_valid(pid)) {
1446 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] The manager pid(%d) is NOT valid", pid);
1447 return VCD_ERROR_INVALID_PARAMETER;
1450 int ret = vcd_recorder_get(audio_type);
1452 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to get audio in type : %d", ret);
1453 return VCD_ERROR_OPERATION_FAILED;
1456 return VCD_ERROR_NONE;
1459 int vcd_server_mgr_set_client_info(int pid)
1461 /* check if pid is valid */
1462 if (false == vcd_client_manager_is_valid(pid)) {
1463 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] The manager pid(%d) is NOT valid", pid);
1464 return VCD_ERROR_INVALID_PARAMETER;
1467 int ret = vcd_client_save_client_info();
1469 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to save client info : %d", ret);
1470 return VCD_ERROR_OPERATION_FAILED;
1473 return VCD_ERROR_NONE;
1476 static int __reset_waiting_for_widget_recording(void)
1478 SLOG(LOG_ERROR, TAG_VCD, "[Server] Reet waiting for widget recording");
1479 // Delete timer to check that widget client is terminated
1480 if (g_check_widget_client_timer) {
1481 ecore_timer_del(g_check_widget_client_timer);
1482 g_check_widget_client_timer = NULL;
1484 // Reset flag to wait for recording from widget client
1485 vcd_client_widget_set_waiting_for_recording(-1, false);
1489 static Eina_Bool __send_waiting_timeout_error_to_manager(void* data)
1491 intptr_t ppid = (intptr_t)data;
1492 int pid = (int)ppid;
1493 SLOG(LOG_ERROR, TAG_VCD, "Widget client didn't send to start recording, pid(%d)", pid);
1494 __reset_waiting_for_widget_recording();
1496 vcdc_send_error_signal_to_manager(vcd_client_manager_get_pid(), VCD_ERROR_OPERATION_FAILED, "voice_engine.error.proc_fail");
1500 static int __set_waiting_for_widget_recording(int pid)
1502 SLOG(LOG_ERROR, TAG_VCD, "[Server] Set waiting for widget recording, pid(%d)", pid);
1504 // Check if the app included widget client is terminated or not. If it is terminated, vcd_server_widget_finalize() function will be called
1505 // In that function, it will start recording
1506 intptr_t ppid = (intptr_t)pid;
1507 g_check_widget_client_timer = ecore_timer_add(2.0, __send_waiting_timeout_error_to_manager, (void*)ppid);
1509 // Set flag to wait for recording from widget client
1510 vcd_client_widget_set_waiting_for_recording(pid, true);
1514 static int __start_internal_recognition()
1517 __reset_waiting_for_widget_recording();
1519 if (0 != vcd_client_command_collect_command()) {
1520 SLOG(LOG_ERROR, TAG_VCD, "[Client Data ERROR] Fail to collect command");
1521 /* Send error cb to manager */
1522 int pid = vcd_client_widget_get_foreground_pid();
1524 vcdc_send_error_signal_to_manager(vcd_client_manager_get_pid(), VCD_ERROR_OPERATION_FAILED, "voice_engine.error.proc_fail");
1525 return VCD_ERROR_OPERATION_FAILED;
1528 /* 3. Set command to engine */
1529 ret = vcd_engine_set_commands();
1531 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to set commands : %d", ret);
1532 /* Send error cb to manager */
1533 int pid = vcd_client_widget_get_foreground_pid();
1535 vcdc_send_error_signal_to_manager(vcd_client_manager_get_pid(), VCD_ERROR_OPERATION_FAILED, "voice_engine.error.proc_fail");
1536 return VCD_ERROR_OPERATION_FAILED;
1539 SLOG(LOG_ERROR, TAG_VCD, "[Server Success] Set command");
1541 bool stop_by_silence = true;
1542 if (VCD_RECOGNITION_MODE_MANUAL == vcd_client_get_recognition_mode()) {
1543 stop_by_silence = false;
1546 vcd_client_manager_set_result_text(NULL);
1548 /* 4. start recognition */
1549 ret = vcd_engine_recognize_start(stop_by_silence);
1551 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to start recognition : result(%d)", ret);
1552 /* Send error cb to manager */
1553 int pid = vcd_client_widget_get_foreground_pid();
1555 vcdc_send_error_signal_to_manager(vcd_client_manager_get_pid(), VCD_ERROR_OPERATION_FAILED, "voice_engine.error.proc_fail");
1556 return VCD_ERROR_OPERATION_FAILED;
1559 SLOG(LOG_ERROR, TAG_VCD, "[Server Success] Start engine");
1562 /* 5. recorder start */
1563 ret = vcd_recorder_start();
1565 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to start recorder : result(%d)", ret);
1566 vcd_engine_recognize_cancel();
1567 /* Send error cb to manager */
1568 int pid = vcd_client_widget_get_foreground_pid();
1570 vcdc_send_error_signal_to_manager(vcd_client_manager_get_pid(), VCD_ERROR_OPERATION_FAILED, "voice_engine.error.proc_fail");
1575 vcd_config_set_service_state(VCD_STATE_RECORDING);
1576 vcdc_send_service_state(VCD_STATE_RECORDING);
1578 SLOG(LOG_ERROR, TAG_VCD, "[Server Success] Start recognition(%d)", stop_by_silence);
1583 static Eina_Bool __vcd_request_show_tooltip(void *data)
1585 int pid = vcd_client_widget_get_foreground_pid();
1587 SLOG(LOG_ERROR, TAG_VCD, "[Server] Request tooltip show and widget command, show(%d)", (bool)data);
1588 vcdc_send_show_tooltip(pid, (bool)data);
1594 int vcd_server_mgr_start(vcd_recognition_mode_e recognition_mode, bool exclusive_cmd, bool start_by_client)
1596 /* 1. check current state */
1597 vcd_state_e state = vcd_config_get_service_state();
1599 if (VCD_STATE_READY != state) {
1600 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Current state is not ready");
1601 return VCD_ERROR_INVALID_STATE;
1603 if (-1 == vcd_client_manager_get_pid()) {
1604 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Manager is NOT available.");
1605 return VCD_ERROR_OPERATION_FAILED;
1607 __reset_waiting_for_widget_recording();
1609 SLOG(LOG_ERROR, TAG_VCD, "[Server] set recognition mode = %d", recognition_mode);
1610 vcd_client_set_recognition_mode(recognition_mode);
1612 if (false == exclusive_cmd) {
1613 vcd_client_update_foreground_pid();
1614 /* Notify show tooltip */
1615 if (1 == vcd_config_get_command_type_enabled(VC_COMMAND_TYPE_WIDGET)) {
1616 int pid = vcd_client_widget_get_foreground_pid();
1618 SLOG(LOG_ERROR, TAG_VCD, "[Server] Request tooltip show and widget command");
1619 ecore_timer_add(0, __vcd_request_show_tooltip, (void*)true);
1621 __set_waiting_for_widget_recording(pid);
1626 vcd_client_manager_set_exclusive(exclusive_cmd);
1630 if (true == start_by_client) {
1631 /* Get foreground pid */
1632 if (0 != vcd_config_get_foreground(&fg_pid)) {
1633 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to get foreground");
1636 /* Set client exclusive option */
1637 if (0 != vcd_client_set_exclusive_command(fg_pid)) {
1638 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to set exclusive command");
1642 SLOG(LOG_ERROR, TAG_VCD, "[Server] start internal recognition");
1644 int ret = __start_internal_recognition();
1646 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to start recongition : %d", ret);
1650 if (true == start_by_client) {
1651 vcd_client_unset_exclusive_command(fg_pid);
1654 SLOG(LOG_ERROR, TAG_VCD, "[Server Success] start internal recognition");
1655 return VCD_ERROR_NONE;
1658 int vcd_server_mgr_stop()
1660 /* 1. Check current state is recording */
1661 if (VCD_STATE_RECORDING != vcd_config_get_service_state()) {
1662 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Current state is not recording");
1663 return VCD_ERROR_INVALID_STATE;
1665 if (-1 == vcd_client_manager_get_pid()) {
1666 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Manager is NOT available.");
1667 return VCD_ERROR_OPERATION_FAILED;
1669 SLOG(LOG_ERROR, TAG_VCD, "[Server] stop internal recognition");
1672 /* 2. Stop recorder */
1673 vcd_recorder_stop();
1676 /* 3. Stop engine recognition */
1677 int ret = vcd_engine_recognize_stop();
1679 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to stop recognition : %d", ret);
1682 /* 4. Set original mode */
1683 vcd_config_set_service_state(VCD_STATE_PROCESSING);
1684 vcdc_send_service_state(VCD_STATE_PROCESSING);
1686 SLOG(LOG_ERROR, TAG_VCD, "[Server Success] stop internal recognition");
1687 return VCD_ERROR_NONE;
1690 int vcd_server_mgr_cancel()
1692 if (-1 == vcd_client_manager_get_pid()) {
1693 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Manager is NOT available.");
1694 return VCD_ERROR_OPERATION_FAILED;
1697 if (g_restart_timer != NULL) {
1698 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Delete restart engine timer");
1699 ecore_timer_del(g_restart_timer);
1700 g_restart_timer = NULL;
1703 /* 1. Check current state */
1704 vcd_state_e state = vcd_config_get_service_state();
1705 if (VCD_STATE_READY == state) {
1706 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Current state is READY");
1707 vcd_recorder_stop();
1709 if (false == vcd_client_manager_get_exclusive()) {
1710 if (1 == vcd_config_get_command_type_enabled(VC_COMMAND_TYPE_WIDGET)) {
1711 int pid = vcd_client_widget_get_foreground_pid();
1713 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Request tooltip hide");
1714 ecore_timer_add(0, __vcd_request_show_tooltip, (void*)false);
1719 vcdc_send_service_state(VCD_STATE_READY);
1720 return VCD_ERROR_NONE;
1722 SLOG(LOG_ERROR, TAG_VCD, "[Server] cancel internal recognition");
1725 /* 2. Stop recorder */
1726 vcd_recorder_stop();
1729 /* 3. Cancel engine */
1730 int ret = vcd_engine_recognize_cancel();
1732 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Fail to cancel : %d", ret);
1735 if (false == vcd_client_manager_get_exclusive()) {
1736 if (1 == vcd_config_get_command_type_enabled(VC_COMMAND_TYPE_WIDGET)) {
1737 int pid = vcd_client_widget_get_foreground_pid();
1739 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Request tooltip hide");
1740 ecore_timer_add(0, __vcd_request_show_tooltip, (void*)false);
1744 vcd_client_manager_set_exclusive(false);
1748 vcd_config_set_service_state(VCD_STATE_READY);
1749 vcdc_send_service_state(VCD_STATE_READY);
1751 SLOG(LOG_ERROR, TAG_VCD, "[Server Success] cancel internal recognition");
1752 return VCD_ERROR_NONE;
1756 int vcd_server_mgr_result_select()
1758 __vcd_send_selected_result(NULL);
1760 return VCD_ERROR_NONE;
1763 int vcd_server_mgr_set_domain(int pid, const char* domain)
1765 /* check if pid is valid */
1766 if (false == vcd_client_manager_is_valid(pid)) {
1767 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] The manager pid(%d) is NOT valid", pid);
1768 return VCD_ERROR_INVALID_PARAMETER;
1771 vcd_state_e state = vcd_config_get_service_state();
1772 if (VCD_STATE_READY != state) {
1773 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Current state is not ready");
1774 return VCD_ERROR_INVALID_STATE;
1777 /* Set domain to engine */
1778 int ret = vcd_engine_set_domain(pid, domain);
1780 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to set domain : %d", ret);
1782 SLOG(LOG_DEBUG, TAG_VCD, "[Server SUCCESS] Set domain");
1788 int vcd_server_mgr_set_private_data(int pid, const char* key, const char* data)
1790 /* check if pid is valid */
1791 if (false == vcd_client_manager_is_valid(pid)) {
1792 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] The manager pid(%d) is NOT valid", pid);
1793 return VCD_ERROR_INVALID_PARAMETER;
1796 vcd_state_e state = vcd_config_get_service_state();
1797 if (VCD_STATE_READY != state) {
1798 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Current state is not ready");
1799 return VCD_ERROR_INVALID_STATE;
1802 /* Set private data to engine */
1803 int ret = vcd_engine_set_private_data(pid, key, data);
1805 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to set private data : %d", ret);
1807 SLOG(LOG_DEBUG, TAG_VCD, "[Server SUCCESS] Set private data");
1813 int vcd_server_mgr_get_private_data(int pid, const char* key, char** data)
1815 /* check if pid is valid */
1816 if (false == vcd_client_manager_is_valid(pid)) {
1817 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] The manager pid(%d) is NOT valid", pid);
1818 return VCD_ERROR_INVALID_PARAMETER;
1820 vcd_state_e state = vcd_config_get_service_state();
1821 if (VCD_STATE_READY != state) {
1822 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Current state is not ready");
1823 return VCD_ERROR_INVALID_STATE;
1826 /* Get private data to engine */
1827 int ret = vcd_engine_get_private_data(pid, key, data);
1829 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to get private data : %d", ret);
1831 SLOG(LOG_DEBUG, TAG_VCD, "[Server SUCCESS] Set private data");
1837 int vcd_server_mgr_do_action(int pid, int type, const char* action)
1841 /* check if pid is valid */
1842 if (false == vcd_client_manager_is_valid(pid)) {
1843 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] The manager pid(%d) is NOT valid", pid);
1844 return VCD_ERROR_INVALID_PARAMETER;
1847 vcd_state_e state = vcd_config_get_service_state();
1848 if (VCD_STATE_READY != state) {
1849 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Current state is not ready");
1850 return VCD_ERROR_INVALID_STATE;
1853 /* Reqeust do action to engine */
1854 if (VCD_SEND_EVENT_TYPE_TEXT == type)
1855 ret = vcd_engine_process_text(pid, action);
1856 else if (VCD_SEND_EVENT_TYPE_LIST_EVENT == type)
1857 ret = vcd_engine_process_list_event(pid, action);
1858 else if (VCD_SEND_EVENT_TYPE_HAPTIC_EVENT == type)
1859 ret = vcd_engine_process_haptic_event(pid, action);
1862 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to process do action : %d", ret);
1864 vcd_config_set_service_state(VCD_STATE_PROCESSING);
1865 vcdc_send_service_state(VCD_STATE_PROCESSING);
1866 SLOG(LOG_DEBUG, TAG_VCD, "[Server SUCCESS] Process do action");
1872 int vcd_server_mgr_enable_command_type(int pid, int cmd_type)
1876 /* check if pid is valid */
1877 if (false == vcd_client_manager_is_valid(pid)) {
1878 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] The manager pid(%d) is NOT valid", pid);
1879 return VCD_ERROR_INVALID_PARAMETER;
1882 vcd_state_e state = vcd_config_get_service_state();
1883 if (VCD_STATE_READY != state) {
1884 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Current state is not ready");
1885 return VCD_ERROR_INVALID_STATE;
1888 ret = vcd_config_enable_command_type(cmd_type);
1890 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to enable command type");
1892 SLOG(LOG_ERROR, TAG_VCD, "[Server] Enable command type(%d)", cmd_type);
1898 int vcd_server_mgr_disable_command_type(int pid, int cmd_type)
1902 /* check if pid is valid */
1903 if (false == vcd_client_manager_is_valid(pid)) {
1904 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] The manager pid(%d) is NOT valid", pid);
1905 return VCD_ERROR_INVALID_PARAMETER;
1908 vcd_state_e state = vcd_config_get_service_state();
1909 if (VCD_STATE_READY != state) {
1910 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Current state is not ready");
1911 return VCD_ERROR_INVALID_STATE;
1914 ret = vcd_config_disable_command_type(cmd_type);
1916 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to disable command type");
1918 SLOG(LOG_ERROR, TAG_VCD, "[Server] Disable command type(%d)", cmd_type);
1925 * VC Server Functions for Client
1927 int vcd_server_initialize(int pid)
1929 if (false == vcd_engine_is_available_engine()) {
1930 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] No Engine");
1931 return VCD_ERROR_ENGINE_NOT_FOUND;
1934 /* check if pid is valid */
1935 if (true == vcd_client_is_available(pid)) {
1936 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] The pid is already exist");
1937 return VCD_ERROR_INVALID_PARAMETER;
1940 /* Add client information to client manager */
1941 if (0 != vcd_client_add(pid)) {
1942 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to add client info");
1943 return VCD_ERROR_OPERATION_FAILED;
1946 SLOG(LOG_DEBUG, TAG_VCD, "[Server Success] Client Initialize : pid(%d)", pid);
1948 return VCD_ERROR_NONE;
1951 int vcd_server_finalize(int pid)
1953 /* check if pid is valid */
1954 if (false == vcd_client_is_available(pid)) {
1955 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] pid is NOT valid ");
1956 return VCD_ERROR_INVALID_PARAMETER;
1959 /* Remove client information */
1960 if (0 != vcd_client_delete(pid)) {
1961 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Fail to delete client");
1964 if (0 == vcd_client_get_ref_count()) {
1965 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Connected client list is empty");
1966 ecore_timer_add(0.5, __finalize_quit_ecore_loop, NULL);
1969 SLOG(LOG_DEBUG, TAG_VCD, "[Server Success] Client Finalize : pid(%d)", pid);
1971 return VCD_ERROR_NONE;
1974 int vcd_server_set_command(int pid, vc_cmd_type_e cmd_type)
1976 /* check if pid is valid */
1977 if (false == vcd_client_is_available(pid)) {
1978 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] pid is NOT valid ");
1979 return VCD_ERROR_INVALID_PARAMETER;
1982 if (0 != vcd_client_set_command_type(pid, cmd_type)) {
1983 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to set command type : pid(%d), cmd_type(%d)", pid, cmd_type);
1984 return VCD_ERROR_OPERATION_FAILED;
1990 int vcd_server_unset_command(int pid, vc_cmd_type_e cmd_type)
1992 /* check if pid is valid */
1993 if (false == vcd_client_is_available(pid)) {
1994 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] pid is NOT valid ");
1995 return VCD_ERROR_INVALID_PARAMETER;
1998 if (0 != vcd_client_unset_command_type(pid, cmd_type)) {
1999 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to unset command type : pid(%d), cmd_type(%d)", pid, cmd_type);
2000 return VCD_ERROR_OPERATION_FAILED;
2006 int vcd_server_set_foreground(int pid, bool value)
2008 /* check if pid is valid */
2009 if (false == vcd_client_is_available(pid) && false == vcd_client_widget_is_available(pid)) {
2010 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] pid is NOT valid ");
2011 return VCD_ERROR_INVALID_PARAMETER;
2014 if (0 != vcd_config_set_foreground(pid, value)) {
2015 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to set foreground : pid(%d), value(%d)", pid, value);
2016 return VCD_ERROR_OPERATION_FAILED;
2022 static int __vcd_server_launch_manager_app()
2025 bool running = false;
2028 ret = vcd_client_manager_get_appid(&appid);
2029 if (0 != ret || NULL == appid) {
2030 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Fail to get manager appid");
2031 return VCD_ERROR_OPERATION_FAILED;
2033 ret = app_manager_is_running(appid, &running);
2035 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Fail to check running with appid(%s)", appid);
2038 return VCD_ERROR_OPERATION_FAILED;
2040 if (false == running) {
2041 int tmp_ret = __vcd_is_package_installed(appid);
2042 if (false == tmp_ret) {
2043 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] manager app is not installed, appid(%s)", appid);
2045 ret = __vcd_activate_app_by_appcontrol(appid);
2047 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Fail to activate app");
2052 SLOG(LOG_ERROR, TAG_VCD, "Launch vc manager app: appid(%s)", appid);
2055 ret = __vcd_resume_app(appid);
2057 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Fail to resume app");
2062 SLOG(LOG_ERROR, TAG_VCD, "Resume vc manager app: appid(%s)", appid);
2068 return VCD_ERROR_NONE;
2071 int vcd_server_dialog(int pid, const char* disp_text, const char* utt_text, int continuous)
2073 /* check if pid is valid */
2074 if (false == vcd_client_is_available(pid) && false == vcd_client_widget_is_available(pid)) {
2075 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] pid is NOT valid ");
2076 return VCD_ERROR_INVALID_PARAMETER;
2079 int ret = __vcd_server_launch_manager_app();
2081 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to send dialog : mgr_pid(%d), pid(%d), disp_text(%s), utt_text(%s), continue(%d)", vcd_client_manager_get_pid(), pid, disp_text, utt_text, continuous);
2085 ret = vcdc_send_dialog(vcd_client_manager_get_pid(), pid, disp_text, utt_text, continuous);
2087 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to send dialog : mgr_pid(%d), pid(%d), disp_text(%s), utt_text(%s), continue(%d)", vcd_client_manager_get_pid(), pid, disp_text, utt_text, continuous);
2094 int vcd_server_is_system_command_valid(int pid, int* is_sys_cmd_valid)
2096 /* check if pid is valid */
2097 if (false == vcd_client_is_available(pid) && false == vcd_client_widget_is_available(pid)) {
2098 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] pid is NOT valid ");
2099 return VCD_ERROR_INVALID_PARAMETER;
2102 /* check if system command is valid */
2103 if (true == vcd_client_manager_is_system_command_valid(vcd_client_manager_get_pid()))
2104 *is_sys_cmd_valid = true;
2106 *is_sys_cmd_valid = false;
2112 int vcd_server_set_exclusive_command(int pid, bool value)
2114 /* check if pid is valid */
2115 if (false == vcd_client_is_available(pid)) {
2116 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] pid is NOT valid ");
2117 return VCD_ERROR_INVALID_PARAMETER;
2120 if (true == value) {
2121 if (0 != vcd_client_set_exclusive_command(pid)) {
2122 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to set exclusive command : pid(%d)", pid);
2123 return VCD_ERROR_OPERATION_FAILED;
2126 if (0 != vcd_client_unset_exclusive_command(pid)) {
2127 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to unset exclusive command : pid(%d)", pid);
2128 return VCD_ERROR_OPERATION_FAILED;
2135 int vcd_server_request_start(int pid, bool stop_by_silence)
2137 /* check if pid is valid */
2138 if (false == vcd_client_is_available(pid)) {
2139 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] pid(%d) is NOT forground client", pid);
2140 return VCD_ERROR_INVALID_PARAMETER;
2144 /* Check current state */
2145 vcd_state_e state = vcd_config_get_service_state();
2147 /* Service state should be ready */
2148 if (VCD_STATE_READY != state) {
2149 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Current state is not Ready : pid(%d)", pid);
2150 return VCD_ERROR_INVALID_STATE;
2153 if (-1 != vcd_client_manager_get_pid()) {
2154 /* Check current pid is valid */
2155 if (false == vcd_client_manager_check_demandable_client(pid)) {
2156 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Current client is NOT available : pid(%d)", pid);
2157 return VCD_ERROR_INVALID_PARAMETER;
2161 ret = vcd_server_mgr_start(stop_by_silence, false);
2163 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Fail to start recognition");
2164 return VCD_ERROR_INVALID_PARAMETER;
2170 int vcd_server_request_stop(int pid)
2173 /* Check current state */
2174 vcd_state_e state = vcd_config_get_service_state();
2176 /* Service state should be ready */
2177 if (VCD_STATE_RECORDING != state) {
2178 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Current state is not Recording : pid(%d)", pid);
2179 return VCD_ERROR_INVALID_STATE;
2182 if (-1 != vcd_client_manager_get_pid()) {
2183 /* Check current pid is valid */
2184 if (false == vcd_client_manager_check_demandable_client(pid)) {
2185 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Current client is NOT available : pid(%d)", pid);
2186 return VCD_ERROR_INVALID_PARAMETER;
2190 ret = vcd_server_mgr_stop();
2192 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Fail to start recognition");
2193 return VCD_ERROR_OPERATION_FAILED;
2196 return VCD_ERROR_NONE;
2199 int vcd_server_request_cancel(int pid)
2202 /* Check current state */
2203 vcd_state_e state = vcd_config_get_service_state();
2205 /* Service state should be recording or processing */
2206 if (VCD_STATE_RECORDING != state && VCD_STATE_PROCESSING != state) {
2207 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Current state is not Recording or Processing : pid(%d)", pid);
2208 return VCD_ERROR_INVALID_STATE;
2211 if (-1 != vcd_client_manager_get_pid()) {
2212 /* Check current pid is valid */
2213 if (false == vcd_client_manager_check_demandable_client(pid)) {
2214 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Current client is NOT available : pid(%d)", pid);
2215 return VCD_ERROR_INVALID_PARAMETER;
2219 ret = vcd_server_mgr_cancel();
2221 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Fail to start recognition");
2222 return VCD_ERROR_OPERATION_FAILED;
2225 return VCD_ERROR_NONE;
2230 * VC Server Functions for Widget lib
2232 int vcd_server_widget_initialize(int pid)
2234 if (false == vcd_engine_is_available_engine()) {
2235 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] No Engine");
2236 return VCD_ERROR_ENGINE_NOT_FOUND;
2239 /* check if pid is valid */
2240 if (true == vcd_client_widget_is_available(pid)) {
2241 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] The pid is already exist");
2242 return VCD_ERROR_NONE;
2245 /* Add client information to client manager */
2246 if (0 != vcd_client_widget_add(pid)) {
2247 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to add client info");
2248 return VCD_ERROR_OPERATION_FAILED;
2251 SLOG(LOG_DEBUG, TAG_VCD, "[Server Success] Initialize widget : pid(%d)", pid);
2253 return VCD_ERROR_NONE;
2256 static void __vcd_server_widget_start_recording(void *data)
2258 SLOG(LOG_ERROR, TAG_VCD, "[Server INFO] start recording");
2260 if (0 != __start_internal_recognition()) {
2261 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to start recongition");
2265 int vcd_server_widget_finalize(int pid)
2267 /* check if pid is valid */
2268 if (false == vcd_client_widget_is_available(pid)) {
2269 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] pid is NOT valid ");
2270 return VCD_ERROR_INVALID_PARAMETER;
2273 /* Remove client information */
2274 if (0 != vcd_client_widget_delete(pid)) {
2275 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Fail to delete client");
2278 if (0 == vcd_client_get_ref_count()) {
2279 SLOG(LOG_ERROR, TAG_VCD, "[Server] connected client list is empty");
2280 ecore_timer_add(0.5, __finalize_quit_ecore_loop, NULL);
2284 bool is_waiting = false;
2285 if (0 != vcd_client_widget_get_waiting_for_recording(pid, &is_waiting)) {
2286 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Fail to get waiting to recording");
2289 if (true == is_waiting) {
2290 SLOG(LOG_ERROR, TAG_VCD, "[Server INFO] invoke to start recording");
2291 ecore_main_loop_thread_safe_call_async(__vcd_server_widget_start_recording, NULL);
2293 return VCD_ERROR_NONE;
2296 int vcd_server_widget_start_recording(int pid, bool widget_command)
2298 /* check if pid is valid */
2299 if (false == vcd_client_widget_is_available(pid)) {
2300 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] pid is NOT valid ");
2301 return VCD_ERROR_INVALID_PARAMETER;
2305 if (0 != vcd_client_widget_get_waiting_for_recording(pid, &waiting) || false == waiting) {
2306 SLOG(LOG_ERROR, TAG_VCD, "[Server] Server is not waiting for recording, pid(%d), waiting(%d)", pid, waiting);
2310 if (true == widget_command) {
2311 vcd_client_widget_set_command(pid);
2312 SLOG(LOG_DEBUG, TAG_VCD, "[Server] widget command is available");
2314 vcd_client_widget_unset_command(pid);
2315 SLOG(LOG_WARN, TAG_VCD, "[Server] widget command is NOT available");
2318 SLOG(LOG_ERROR, TAG_VCD, "[Server] start internal recongition : %d", widget_command);
2319 int ret = __start_internal_recognition();
2321 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to start recongition : %d", ret);
2322 ecore_timer_add(0, __vcd_request_show_tooltip, (void*)false);
2328 int vcd_server_widget_start(int pid, bool stop_by_silence)
2330 /* check if pid is valid */
2331 int fore_pid = vcd_client_widget_get_foreground_pid();
2332 if (pid != fore_pid) {
2333 SLOG(LOG_ERROR, TAG_VCD, "[Server] pid is NOT foreground");
2334 return VCD_ERROR_INVALID_PARAMETER;
2337 /* Check current state */
2338 vcd_state_e state = vcd_config_get_service_state();
2340 /* Service state should be ready */
2341 if (VCD_STATE_READY != state) {
2342 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Current state is not Ready : pid(%d)", pid);
2343 return VCD_ERROR_INVALID_STATE;
2346 vcd_client_set_slience_detection(stop_by_silence);
2348 /* Notify show tooltip */
2349 ecore_timer_add(0, __vcd_request_show_tooltip, (void*)true);
2354 int vcd_server_widget_stop(int pid)
2356 /* check if pid is valid */
2357 int fore_pid = vcd_client_widget_get_foreground_pid();
2358 if (pid != fore_pid) {
2359 SLOG(LOG_ERROR, TAG_VCD, "[Server] pid is NOT foreground");
2360 return VCD_ERROR_INVALID_PARAMETER;
2364 /* Check current state */
2365 vcd_state_e state = vcd_config_get_service_state();
2367 /* Service state should be recording */
2368 if (VCD_STATE_RECORDING != state) {
2369 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Current service state is not Recording : pid(%d)", pid);
2370 return VCD_ERROR_INVALID_STATE;
2373 ret = vcd_server_mgr_stop();
2375 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Fail to start recognition");
2376 return VCD_ERROR_OPERATION_FAILED;
2379 return VCD_ERROR_NONE;
2382 int vcd_server_widget_cancel(int pid)
2384 /* check if pid is valid */
2385 int fore_pid = vcd_client_widget_get_foreground_pid();
2386 if (pid != fore_pid) {
2387 SLOG(LOG_ERROR, TAG_VCD, "[Server] pid is NOT foreground");
2388 return VCD_ERROR_INVALID_PARAMETER;
2392 /* Check current state */
2393 vcd_state_e state = vcd_config_get_service_state();
2395 /* Service state should be recording or processing */
2396 if (VCD_STATE_RECORDING != state && VCD_STATE_PROCESSING != state) {
2397 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Current state is not Recording or Processing : pid(%d)", pid);
2398 return VCD_ERROR_INVALID_STATE;
2401 ret = vcd_server_mgr_cancel();
2403 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Fail to cancel recognition : %d", ret);
2407 return VCD_ERROR_NONE;
2410 int vcd_server_widget_enable_asr_result(int pid, bool enable)
2413 ret = vcd_client_widget_set_asr_result_enabled(pid, enable);
2415 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Fail to enable asr result : %d", ret);
2421 int vcd_server_set_language(const char* language)
2423 int ret = VCD_ERROR_NONE;
2425 ret = vcd_config_set_default_language(language);
2427 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Fail to set language : %d", ret);
2431 ret = vcd_engine_set_current_language(language);
2433 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Fail to set language : %d", ret);
2440 * For engine service
2442 int vcd_get_foreach_command(vce_cmd_h vce_command, vce_command_cb callback, void* user_data)
2444 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Get foreach command");
2446 if (NULL == callback) {
2447 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] input parameter is NULL");
2448 return VCD_ERROR_INVALID_PARAMETER;
2452 ret = vcd_engine_agent_get_foreach_command(vce_command, callback, user_data);
2454 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to get foreach command : ret(%d)", ret);
2460 int vcd_get_command_count(vce_cmd_h vce_command)
2462 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Get command count");
2465 ret = vcd_engine_agent_get_command_count(vce_command);
2467 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to get command count : ret(%d)", ret);
2473 int vcd_get_audio_type(char** audio_type)
2475 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Get audio type");
2478 ret = vcd_engine_agent_get_audio_type(audio_type);
2480 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to get audio type : ret(%d)", ret);
2486 int vcd_set_private_data(const char* key, const char* data)
2488 vcd_state_e state = vcd_config_get_service_state();
2490 if (VCD_STATE_READY != state) {
2491 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Current state(%d) is NOT 'READY'", state);
2492 return VCD_ERROR_INVALID_STATE;
2495 int ret = vcd_engine_agent_set_private_data(key, data);
2497 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to set private data to the manager client : ret(%d)", ret);
2499 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Set private data to the manager client, key(%s), data(%s)", key, data);
2505 int vcd_get_private_data(const char* key, char** data)
2507 vcd_state_e state = vcd_config_get_service_state();
2509 if (VCD_STATE_READY != state) {
2510 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Current state(%d) is NOT 'READY'", state);
2511 return VCD_ERROR_INVALID_STATE;
2514 int ret = vcd_engine_agent_get_private_data(key, data);
2516 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to get private data from the manager client : ret(%d)", ret);
2518 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Get private data from the manager client, key(%s), data(%s)", key, data);
2524 int vcd_start_recording()
2526 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Start recording");
2529 ret = vcd_engine_agent_start_recording();
2531 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to start recording : ret(%d)", ret);
2537 int vcd_stop_recording()
2539 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Stop recording");
2542 ret = vcd_engine_agent_stop_recording();
2544 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to stop recording : ret(%d)", ret);
2550 int vcd_set_private_data_set_cb(vce_private_data_set_cb callback_func)
2552 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Set private data set cb");
2555 ret = vcd_engine_agent_set_private_data_set_cb(callback_func);
2557 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to set private data set cb : ret(%d)", ret);
2563 int vcd_set_private_data_requested_cb(vce_private_data_requested_cb callback_func)
2565 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Set private data requested cb");
2568 ret = vcd_engine_agent_set_private_data_requested_cb(callback_func);
2570 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to set private data requested cb : ret(%d)", ret);
2576 int vcd_set_nlu_base_info_requested_cb(vce_nlu_base_info_requested_cb callback_func)
2578 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Set nlu base info requested cb");
2581 ret = vcd_engine_agent_set_nlu_base_info_requested_cb(callback_func);
2583 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to set nlu base info requested cb : ret(%d)", ret);