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;
44 * @brief Enumerations of send event type.
47 VCD_SEND_EVENT_TYPE_TEXT, /**< send text event to vc engine*/
48 VCD_SEND_EVENT_TYPE_LIST_EVENT, /**< send list event to vc engine */
49 VCD_SEND_EVENT_TYPE_HAPTIC_EVENT /**< send haptic event to vc engine */
50 } vcd_send_event_type_e;
52 static int __vcd_server_launch_manager_app();
55 * VC Server Internal Functions
57 static Eina_Bool __stop_by_silence(void *data)
59 SLOG(LOG_DEBUG, TAG_VCD, "@@@ Silence Detected ");
61 vcd_server_mgr_stop();
63 SLOG(LOG_DEBUG, TAG_VCD, "@@@");
67 static Eina_Bool __cancel_by_interrupt(void *data)
69 SLOG(LOG_DEBUG, TAG_VCD, "@@@ Cancel by interrupt");
71 vcd_server_mgr_cancel();
73 SLOG(LOG_DEBUG, TAG_VCD, "@@@");
77 static void __cancel_by_error(void *data)
79 SLOG(LOG_DEBUG, TAG_VCD, "@@@ Cancel by error");
81 vcd_server_mgr_cancel();
83 SLOG(LOG_DEBUG, TAG_VCD, "@@@");
87 static Eina_Bool __restart_engine(void *data)
89 SLOG(LOG_DEBUG, TAG_VCD, "@@@ Restart by no result");
91 g_restart_timer = NULL;
93 /* Restart recognition */
94 int ret = vcd_engine_recognize_start(true);
96 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to restart recognition : result(%d)", ret);
100 SLOG(LOG_DEBUG, TAG_VCD, "[Server Success] Start engine");
102 if (VCD_RECOGNITION_MODE_RESTART_AFTER_REJECT == vcd_client_get_recognition_mode()) {
103 vcd_config_set_service_state(VCD_STATE_RECORDING);
104 vcdc_send_service_state(VCD_STATE_RECORDING);
107 SLOG(LOG_DEBUG, TAG_VCD, "[Server Success] Restart recognition");
109 SLOG(LOG_DEBUG, TAG_VCD, "@@@");
113 static int __server_recorder_callback(const void* data, const unsigned int length)
115 vcd_state_e state = vcd_config_get_service_state();
116 if (VCD_STATE_READY == state) {
117 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Ready state, but recording");
118 } else if (VCD_STATE_PROCESSING == state) {
122 vce_speech_detect_e speech_detected = VCE_SPEECH_DETECT_NONE;
125 ret = vcd_engine_recognize_audio(data, length, &speech_detected);
129 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to set recording data to engine(%d)", ret);
130 ecore_timer_add(0, __cancel_by_interrupt, NULL);
131 /* Send error cb to manager */
132 if (VCE_ERROR_OUT_OF_NETWORK == ret) {
133 vcdc_send_error_signal_to_manager(vcd_client_manager_get_pid(), VCD_ERROR_TIMED_OUT, "Engine connection failed");
135 vcdc_send_error_signal_to_manager(vcd_client_manager_get_pid(), VCD_ERROR_OPERATION_FAILED, "Engine recognition failed");
140 if (VCE_SPEECH_DETECT_BEGIN == speech_detected) {
141 if (-1 != vcd_client_manager_get_pid()) {
142 /* Manager client is available */
143 if (0 != vcdc_send_speech_detected(vcd_client_manager_get_pid())) {
144 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Fail to send speech detected");
147 } else if (VCE_SPEECH_DETECT_END == speech_detected) {
148 if (VCD_RECOGNITION_MODE_STOP_BY_SILENCE == vcd_client_get_recognition_mode()) {
149 /* silence detected */
150 ecore_timer_add(0, __stop_by_silence, NULL);
151 } else if (VCD_RECOGNITION_MODE_RESTART_AFTER_REJECT == vcd_client_get_recognition_mode()) {
152 /* Stop engine recognition */
153 int ret = vcd_engine_recognize_stop();
155 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to stop recognition : %d", ret);
157 vcd_config_set_service_state(VCD_STATE_PROCESSING);
158 vcdc_send_service_state(VCD_STATE_PROCESSING);
160 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Stop engine only by silence");
161 } else if (VCD_RECOGNITION_MODE_RESTART_CONTINUOUSLY == vcd_client_get_recognition_mode()) {
162 /* Stop engine recognition */
163 int ret = vcd_engine_recognize_stop();
165 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to stop recognition : %d", ret);
173 void __server_recorder_interrupt_callback()
175 SLOG(LOG_DEBUG, TAG_VCD, "@@@ Cancel by sound interrupt");
177 ecore_timer_add(0, __cancel_by_interrupt, NULL);
179 SLOG(LOG_DEBUG, TAG_VCD, "@@@");
182 static void __config_lang_changed_cb(const char* current_lang, void* user_data)
184 SLOG(LOG_DEBUG, TAG_VCD, "@@@ Change language ");
186 /* Current state is recording */
187 vcd_state_e state = vcd_config_get_service_state();
188 if (VCD_STATE_RECORDING == state || VCD_STATE_PROCESSING == state) {
189 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Current state is 'Recording'. Cancel recognition");
190 vcd_server_mgr_cancel();
194 ret = vcd_engine_set_current_language(current_lang);
196 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to set language of engine : %d", ret);
199 SLOG(LOG_DEBUG, TAG_VCD, "@@@");
204 static void __config_foreground_changed_cb(int previous, int current, void* user_data)
206 SLOG(LOG_DEBUG, TAG_VCD, "@@@ Change foreground");
208 SLOG(LOG_DEBUG, TAG_VCD, "Foreground pid(%d)", current);
210 if (VC_NO_FOREGROUND_PID != current) {
211 /* Foreground app is changed */
212 vcd_state_e state = vcd_config_get_service_state();
213 if (VCD_STATE_RECORDING == state) {
214 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Foreground pid(%d) is changed. Cancel recognition", current);
215 ecore_timer_add(0, __cancel_by_interrupt, NULL);
219 SLOG(LOG_DEBUG, TAG_VCD, "@@@");
224 static int __vcd_activate_app_by_appcontrol(const char* appid)
227 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Invalid parameter");
228 return VCD_ERROR_INVALID_PARAMETER;
232 app_control_h app_control = NULL;
233 ret = app_control_create(&app_control);
234 if (APP_CONTROL_ERROR_NONE == ret) {
236 ret = app_control_add_extra_data(app_control, "voice_launch", "get_result");
237 if (APP_CONTROL_ERROR_NONE != ret) {
238 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Fail to add extra data, ret(%d)", ret);
239 app_control_destroy(app_control);
240 return VCD_ERROR_OPERATION_FAILED;
243 ret = app_control_set_app_id(app_control, appid);
244 if (APP_CONTROL_ERROR_NONE != ret) {
245 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Fail to set app id, ret(%d)", ret);
246 app_control_destroy(app_control);
247 return VCD_ERROR_OPERATION_FAILED;
249 // Sent launch request
250 ret = app_control_send_launch_request(app_control, NULL, NULL);
251 if (APP_CONTROL_ERROR_NONE != ret) {
252 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Fail to send launch request, ret(%d)", ret);
253 app_control_destroy(app_control);
254 return VCD_ERROR_OPERATION_FAILED;
256 // Destroy app control
257 ret = app_control_destroy(app_control);
258 if (APP_CONTROL_ERROR_NONE != ret) {
259 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Fail to destroy, ret(%d)", ret);
260 return VCD_ERROR_OPERATION_FAILED;
263 return VCD_ERROR_NONE;
266 static int __vcd_resume_app(const char* appid)
268 app_context_h app_context = NULL;
269 int ret = app_manager_get_app_context(appid, &app_context);
270 if (APP_MANAGER_ERROR_NONE != ret) {
271 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Fail to get app_context, ret(%d), appid(%s)", ret, appid);
272 return VCD_ERROR_OPERATION_FAILED;
275 ret = app_manager_resume_app(app_context);
276 if (APP_MANAGER_ERROR_NONE != ret) {
277 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Fail to resume app, ret(%d), appid(%s)", ret, appid);
278 return VCD_ERROR_OPERATION_FAILED;
280 return VCD_ERROR_NONE;
283 static bool __vcd_is_package_installed(const char* appid)
285 app_info_h app_info = NULL;
286 int ret = app_manager_get_app_info(appid, &app_info);
287 if (APP_MANAGER_ERROR_NONE != ret || NULL == app_info)
289 ret = app_info_destroy(app_info);
290 if (APP_MANAGER_ERROR_NONE != ret)
291 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Fail to destroy app_info, ret(%d)", ret);
295 static bool __vcd_launch_app(const char* result)
297 if (NULL == result) {
298 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Invalid parameter");
299 return VCD_ERROR_INVALID_PARAMETER;
302 GSList* app_list = NULL;
303 if (0 != vc_db_get_appid_list(result, &app_list)) {
304 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] result text is NULL");
305 return VCD_ERROR_INVALID_PARAMETER;
308 if (0 != g_slist_length(app_list)) {
311 vc_deactivated_app_s* temp_app = NULL;
312 iter = g_slist_nth(app_list, 0);
314 while (NULL != iter) {
315 temp_app = iter->data;
317 if (NULL != temp_app) {
318 if (NULL != temp_app->appid) {
320 bool running = false;
321 ret = app_manager_is_running(temp_app->appid, &running);
322 if (APP_MANAGER_ERROR_NONE != ret) {
323 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Fail to check running with appid(%s)", temp_app->appid);
324 free(temp_app->appid);
325 temp_app->appid = NULL;
328 return VCD_ERROR_OPERATION_FAILED;
330 if (false == running) {
331 int tmp_ret = __vcd_is_package_installed(temp_app->appid);
332 if (false == tmp_ret) {
333 SLOG(LOG_WARN, TAG_VCD, "[WARNING] app is not installed, appid(%s)", temp_app->appid);
335 ret = __vcd_activate_app_by_appcontrol(temp_app->appid);
337 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Fail to activate app");
338 free(temp_app->appid);
339 temp_app->appid = NULL;
344 SLOG(LOG_ERROR, TAG_VCD, "Launch app: appid(%s) result(%s)", temp_app->appid, result);
347 ret = __vcd_resume_app(temp_app->appid);
349 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Fail to resume app");
350 free(temp_app->appid);
351 temp_app->appid = NULL;
356 SLOG(LOG_ERROR, TAG_VCD, "Resume app: appid(%s) result(%s)", temp_app->appid, result);
358 free(temp_app->appid);
359 temp_app->appid = NULL;
364 iter = g_slist_next(iter);
369 return VCD_ERROR_NONE;
372 static Eina_Bool __vcd_send_selected_result(void *data)
374 GSList* pid_list = NULL;
375 const char* result = vcd_client_manager_get_result_text();
377 if (0 != vc_info_parser_get_result_pid_list(&pid_list, result)) {
378 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Fail to get pid list. No result");
380 if (0 < g_slist_length(pid_list)) {
382 vc_cmd_s* temp_cmd = NULL;
387 iter = g_slist_nth(pid_list, 0);
388 while (NULL != iter) {
389 temp_cmd = iter->data;
391 if (NULL != temp_cmd && (pre_pid != temp_cmd->pid || pre_type == VC_COMMAND_TYPE_WIDGET || temp_cmd->type == VC_COMMAND_TYPE_WIDGET)) {
392 /* Launch deactivated several apps that is matched with result */
393 ret = __vcd_launch_app(result);
395 SLOG(LOG_ERROR, TAG_VCD, "Fail to launch or resume app, ret(%d) result(%s)", ret, result);
397 /* send result noti */
398 ret = vcdc_send_result(temp_cmd->pid, vcd_client_manager_get_pid(), temp_cmd->type);
400 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Fail to send result, ret(%d)", ret);
403 SLOG(LOG_ERROR, TAG_VCD, "[Server] Send result : pid(%d) type(%d)", temp_cmd->pid, temp_cmd->type);
404 pre_pid = temp_cmd->pid;
405 pre_type = temp_cmd->type;
411 pid_list = g_slist_remove_link(pid_list, iter);
412 iter = g_slist_nth(pid_list, 0);
417 if (VCD_RECOGNITION_MODE_RESTART_CONTINUOUSLY != vcd_client_get_recognition_mode()) {
418 vcd_config_set_service_state(VCD_STATE_READY);
419 vcdc_send_service_state(VCD_STATE_READY);
425 int vcd_send_asr_result(vce_asr_result_event_e event, const char* asr_result, void *user_data)
427 if (NULL != asr_result) {
428 SECURE_SLOG(LOG_DEBUG, TAG_VCD, "[Server] ASR result - Event(%d), Text(%s)", event, asr_result);
429 vcdc_send_pre_result_to_manager(vcd_client_manager_get_pid(), event, asr_result);
432 return VCD_ERROR_NONE;
435 int vcd_send_nlg_result(const char* nlg_result, void *user_data)
437 int ret = __vcd_server_launch_manager_app();
439 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);
443 SLOG(LOG_ERROR, TAG_VCD, "[Server] send dialog : mgr_pid(%d), nlg_result(%s)", vcd_client_manager_get_pid(), nlg_result);
444 ret = vcdc_send_dialog(vcd_client_manager_get_pid(), -1, nlg_result, NULL, 0); //0: VC_DIALOG_END
446 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);
450 static void* __recorder_stop(void *data)
456 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)
460 if (VCD_STATE_PROCESSING != vcd_config_get_service_state()) {
461 if (VCD_RECOGNITION_MODE_RESTART_CONTINUOUSLY != vcd_client_get_recognition_mode()) {
462 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Current state is not 'Processing' and mode is not 'Restart continuously'");
463 return VCD_ERROR_INVALID_STATE;
467 vc_info_parser_unset_result(vcd_client_manager_get_exclusive());
468 vcd_client_manager_set_result_text(all_result);
470 SECURE_SLOG(LOG_INFO, TAG_VCD, "[Server] Event(%d), Text(%s) Nonfixed(%s) Msg(%s) Result count(%d)",
471 event, all_result, non_fixed_result, msg, count);
473 SLOG(LOG_ERROR, TAG_VCD, "[Server] NLU result(%s)", nlu_result);
475 if (VCD_RECOGNITION_MODE_RESTART_AFTER_REJECT == vcd_client_get_recognition_mode()) {
476 if (VCE_RESULT_EVENT_REJECTED == event) {
477 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Restart by no or rejected result");
478 /* If no result and restart option is ON */
479 /* Send reject message */
480 bool temp = vcd_client_manager_get_exclusive();
481 vc_info_parser_set_result(all_result, event, msg, NULL, temp);
482 ret = vcdc_send_result_to_manager(vcd_client_manager_get_pid(), VC_RESULT_TYPE_NOTIFICATION);
484 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Fail to send result");
487 g_restart_timer = ecore_timer_add(0, __restart_engine, NULL);
490 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Stop recorder due to success");
491 //vcd_recorder_stop();
492 ecore_main_loop_thread_safe_call_sync(__recorder_stop, NULL);
493 } else if (VCD_RECOGNITION_MODE_RESTART_CONTINUOUSLY == vcd_client_get_recognition_mode()) {
494 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Restart continuously");
495 /* Restart option is ON */
496 g_restart_timer = ecore_timer_add(0, __restart_engine, NULL);
497 if (VCE_RESULT_EVENT_REJECTED == event) {
498 bool temp = vcd_client_manager_get_exclusive();
499 vc_info_parser_set_result(all_result, event, msg, NULL, temp);
500 ret = vcdc_send_result_to_manager(vcd_client_manager_get_pid(), VC_RESULT_TYPE_NOTIFICATION);
502 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Fail to send result");
508 /* if nlu_result is exist, Add command handle(is_action) into result list */
510 SLOG(LOG_DEBUG, TAG_VCD, "[Server] @ Get engine result @");
512 vc_cmd_s* temp_cmd = NULL;
513 vc_cmd_list_h vc_cmd_list = NULL;
515 if (0 != vc_cmd_list_create(&vc_cmd_list)) {
516 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Fail to create command list");
517 vcd_client_manager_set_exclusive(false);
518 vcd_config_set_service_state(VCD_STATE_READY);
519 vcdc_send_service_state(VCD_STATE_READY);
520 return VCD_ERROR_NONE;
523 /* priority filter */
524 /* system > exclusive > widget > foreground > system_background > background */
526 int* filtered_id = (int*)calloc(count, sizeof(int));
528 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to allocate memory");
529 return VCD_ERROR_OUT_OF_MEMORY;
531 int filtered_count = 0;
532 int top_priority = VC_COMMAND_PRIORITY_BACKGROUND;
533 for (i = 0; i < count; i++) {
534 SLOG(LOG_INFO, TAG_VCD, "[Server] [%d] Result id(%d)", i, result_id[i]);
536 if (0 > result_id[i]) {
537 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Result ID(%d) is NOT valid", result_id[i]);
541 ret = vcd_client_get_cmd_from_result_id(result_id[i], &temp_cmd);
542 if (0 == ret && NULL != temp_cmd) {
543 if (top_priority == temp_cmd->priority) {
544 filtered_id[filtered_count] = result_id[i];
546 } else if (top_priority > temp_cmd->priority) {
547 filtered_id[0] = result_id[i];
549 top_priority = temp_cmd->priority;
552 vc_cmd_destroy((vc_cmd_h)temp_cmd);
558 if (top_priority >= VC_COMMAND_PRIORITY_WIDGET) {
559 int pid = vcd_client_widget_get_foreground_pid();
561 if (NULL != all_result) {
562 vc_info_parser_set_result(all_result, event, msg, NULL, false);
564 vcd_client_widget_get_asr_result_enabled(pid, &enable);
565 if (true == enable) {
566 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Send ASR result to Widget client");
567 bool is_consumed = false;
568 if (NULL != user_info) {
571 if (0 != vcdc_send_asr_result(pid, event, all_result, VC_COMMAND_TYPE_WIDGET, &is_consumed)) {
572 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to send asr result");
574 SLOG(LOG_DEBUG, TAG_VCD, "[Server] ASR result is consumed(%d)", is_consumed);
575 if (true == is_consumed) {
576 if (NULL != user_info) {
578 SLOG(LOG_ERROR, TAG_VCD, "[Server] Send whether ASR result is consumed or not (%d)", *user_info);
580 vcdc_send_show_tooltip(pid, false);
581 if (-1 != vcd_client_manager_get_pid()) {
582 /* Manager client is available */
583 vc_info_parser_unset_result(false);
584 vc_info_parser_set_result(all_result, VC_RESULT_EVENT_RESULT_SUCCESS, msg, NULL, false);
585 if (0 != vcdc_send_result_to_manager(vcd_client_manager_get_pid(), VC_RESULT_TYPE_NOTIFICATION)) {
586 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Fail to send result");
590 vcd_client_manager_set_exclusive(false);
592 vcd_config_set_service_state(VCD_STATE_READY);
593 vcdc_send_service_state(VCD_STATE_READY);
594 vc_cmd_list_destroy(vc_cmd_list, true);
595 if (NULL != filtered_id) {
599 return VCD_ERROR_NONE;
608 for (i = 0; i < filtered_count; i++) {
609 SLOG(LOG_INFO, TAG_VCD, "[Server] [%d] Filtered Result id(%d)", i, filtered_id[i]);
611 if (filtered_id[i] < 0) {
612 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Filtered ID(%d) is NOT valid", filtered_id[i]);
616 ret = vcd_client_get_cmd_from_result_id(filtered_id[i], &temp_cmd);
617 if (0 == ret && NULL != temp_cmd) {
618 switch (temp_cmd->format) {
619 case VC_CMD_FORMAT_FIXED:
620 case VC_CMD_FORMAT_FIXED_AND_VFIXED:
621 case VC_CMD_FORMAT_VFIXED_AND_FIXED:
622 case VC_CMD_FORMAT_PARTIAL:
623 case VC_CMD_FORMAT_FIXED_AND_NONFIXED:
624 case VC_CMD_FORMAT_NONFIXED_AND_FIXED:
626 case VC_CMD_FORMAT_ACTION:
630 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Unknown command type : %d", temp_cmd->type);
634 if (0 != vc_cmd_list_add(vc_cmd_list, (vc_cmd_h)temp_cmd)) {
635 SLOG(LOG_DEBUG, TAG_VCD, "Fail to add command to list");
636 vc_cmd_destroy((vc_cmd_h)temp_cmd);
640 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] NOT found matached result(%d)", filtered_id[i]);
644 if (NULL != filtered_id) {
649 if (NULL != nlu_result) {
650 SECURE_SLOG(LOG_INFO, TAG_VCD, "[Server] NLU (%s)", nlu_result);
651 vc_info_parser_set_nlu_result(nlu_result);
652 if (0 == is_action) {
654 if (0 != vc_cmd_create(&nlu_cmd)) {
655 SLOG(LOG_ERROR, TAG_VCD, "Fail to nlu cmd create");
657 if (0 != vc_cmd_set_type(nlu_cmd, VC_COMMAND_TYPE_SYSTEM)) {
658 SLOG(LOG_ERROR, TAG_VCD, "Fail to set type");
660 if (0 != vc_cmd_set_pid(nlu_cmd, vcd_client_manager_get_pid())) {
661 SLOG(LOG_ERROR, TAG_VCD, "Fail to set pid");
663 if (0 != vc_cmd_set_format(nlu_cmd, VC_CMD_FORMAT_ACTION)) {
664 SLOG(LOG_ERROR, TAG_VCD, "Fail to set format");
666 if (0 != vc_cmd_list_add(vc_cmd_list, nlu_cmd)) {
667 SLOG(LOG_ERROR, TAG_VCD, "Fail to add nlu cmd to list");
668 vc_cmd_destroy(nlu_cmd);
674 vc_cmd_print_list(vc_cmd_list);
676 SLOG(LOG_DEBUG, TAG_VCD, "[Server] @@@@");
678 int result_count = 0;
679 vc_cmd_list_get_count(vc_cmd_list, &result_count);
681 if (0 == result_count) {
683 if (NULL != all_result) {
684 SECURE_SLOG(LOG_DEBUG, TAG_VCD, "[Server] Engine result is no command : %s", all_result);
686 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Engine result is NULL");
688 bool temp = vcd_client_manager_get_exclusive();
689 vc_info_parser_set_result(all_result, event, msg, NULL, temp);
691 int pid = vcd_client_widget_get_foreground_pid();
693 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Request tooltip hide");
694 /* Send to hide tooltip */
695 vcdc_send_show_tooltip(pid, false);
698 if (-1 != vcd_client_manager_get_pid()) {
699 /* Manager client is available */
700 if (0 != vcdc_send_result_to_manager(vcd_client_manager_get_pid(), VC_RESULT_TYPE_NORMAL)) {
701 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Fail to send result");
705 vcd_client_manager_set_exclusive(false);
707 return VCD_ERROR_NONE;
709 if (false == vcd_client_manager_get_exclusive()) {
710 int pid = vcd_client_widget_get_foreground_pid();
712 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Request tooltip hide");
713 vcdc_send_show_tooltip(pid, false);
716 vc_info_parser_set_result(all_result, event, msg, vc_cmd_list, false);
718 if (-1 != vcd_client_manager_get_pid()) {
719 /* Manager client is available */
720 if (0 != vcdc_send_result_to_manager(vcd_client_manager_get_pid(), VC_RESULT_TYPE_NORMAL)) {
721 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Fail to send result");
724 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Manager is NOT available. Send result to client directly");
725 /* Send result to client */
726 ecore_timer_add(0, __vcd_send_selected_result, NULL);
729 /* exclusive command */
730 vc_info_parser_set_result(all_result, event, msg, vc_cmd_list, true);
732 if (-1 != vcd_client_manager_get_pid()) {
733 /* Manager client is available */
734 if (0 != vcdc_send_result_to_manager(vcd_client_manager_get_pid(), VC_RESULT_TYPE_NORMAL)) {
735 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Fail to send result");
738 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Manager is NOT available");
741 vcd_client_manager_set_exclusive(false);
745 vc_cmd_list_destroy(vc_cmd_list, true);
747 return VCD_ERROR_NONE;
751 if (NULL == result_id) {
753 if (NULL != all_result) {
754 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Engine result is no command : %s", all_result);
755 bool temp = vcd_client_manager_get_exclusive();
756 vc_info_parser_set_result(all_result, event, msg, NULL, temp);
759 int pid = vcd_client_widget_get_foreground_pid();
761 if (NULL != all_result) {
762 /* Send result text to widget */
763 vcdc_send_result(pid, vcd_client_manager_get_pid(), VC_COMMAND_TYPE_WIDGET);
766 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Request tooltip hide");
767 /* Send to hide tooltip */
768 vcdc_send_show_tooltip(pid, false);
771 if (-1 != vcd_client_manager_get_pid()) {
772 /* Manager client is available */
773 if (0 != vcdc_send_result_to_manager(vcd_client_manager_get_pid(), VC_RESULT_TYPE_NORMAL)) {
774 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Fail to send result");
778 vcd_client_manager_set_exclusive(false);
784 SLOG(LOG_DEBUG, TAG_VCD, "[Server] @ Get engine result @");
787 vc_cmd_s* temp_cmd = NULL;
788 vc_cmd_list_h vc_cmd_list = NULL;
790 if (0 != vc_cmd_list_create(&vc_cmd_list)) {
791 SLOG(LOG_ERROR, TAG_VCD, "[Server] Fail to create command list");
792 vcd_client_manager_set_exclusive(false);
793 vcd_config_set_service_state(VCD_STATE_READY);
794 vcdc_send_service_state(VCD_STATE_READY);
799 for (i = 0; i < count; i++) {
800 SLOG(LOG_DEBUG, TAG_VCD, "[Server] [%d] Result ID(%d)", i, result_id[i]);
802 if (result_id[i] < 0) {
803 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Result ID(%d) is NOT valid", result_id[i]);
807 ret = vcd_client_get_cmd_from_result_id(result_id[i], &temp_cmd);
808 if (0 == ret && NULL != temp_cmd) {
809 switch (temp_cmd->format) {
810 case VC_CMD_FORMAT_FIXED:
811 case VC_CMD_FORMAT_FIXED_AND_VFIXED:
812 case VC_CMD_FORMAT_VFIXED_AND_FIXED:
813 case VC_CMD_FORMAT_PARTIAL:
814 /* Nonfixed result is NOT valid */
816 case VC_CMD_FORMAT_FIXED_AND_NONFIXED:
817 if (NULL == temp_cmd->parameter) {
818 if (NULL != non_fixed_result) {
819 temp_cmd->parameter = strdup(non_fixed_result);
822 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Command type is NOT vaild. Parameter (%s)", temp_cmd->parameter);
825 case VC_CMD_FORMAT_NONFIXED_AND_FIXED:
826 if (NULL == temp_cmd->command) {
827 if (NULL != non_fixed_result) {
828 temp_cmd->command = strdup(non_fixed_result);
831 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Command type is NOT vaild. Command (%s)", temp_cmd->command);
836 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Unknown command type : %d", temp_cmd->type);
840 if (0 != vc_cmd_list_add(vc_cmd_list, (vc_cmd_h)temp_cmd)) {
841 SLOG(LOG_DEBUG, TAG_VCD, "Fail to add command to list");
842 vc_cmd_destroy((vc_cmd_h)temp_cmd);
845 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] NOT found matached result(%d)", result_id[i]);
849 vc_cmd_print_list(vc_cmd_list);
851 SLOG(LOG_DEBUG, TAG_VCD, "[Server] @@@@");
853 int result_count = 0;
854 vc_cmd_list_get_count(vc_cmd_list, &result_count);
856 if (false == vcd_client_manager_get_exclusive()) {
857 int pid = vcd_client_widget_get_foreground_pid();
859 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Request tooltip hide");
860 vcdc_send_show_tooltip(pid, false);
863 vc_info_parser_set_result(all_result, event, msg, vc_cmd_list, false);
865 if (-1 != vcd_client_manager_get_pid()) {
866 /* Manager client is available */
867 if (0 != vcdc_send_result_to_manager(vcd_client_manager_get_pid(), VC_RESULT_TYPE_NORMAL)) {
868 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Fail to send result");
871 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Manager is NOT available. Send result to client directly");
872 /* Send result to client */
873 ecore_timer_add(0, __vcd_send_selected_result, NULL);
876 /* exclusive command */
877 vc_info_parser_set_result(all_result, event, msg, vc_cmd_list, true);
879 if (-1 != vcd_client_manager_get_pid()) {
880 /* Manager client is available */
881 if (0 != vcdc_send_result_to_manager(vcd_client_manager_get_pid(), VC_RESULT_TYPE_NORMAL)) {
882 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Fail to send result");
885 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Manager is NOT available");
888 vcd_client_manager_set_exclusive(false);
891 vc_cmd_list_destroy(vc_cmd_list, true);
898 static void __vcd_server_nlu_result_cb(vce_result_event_e event, const char* nlu_result, void *user_data)
900 SECURE_SLOG(LOG_DEBUG, TAG_VCD, "[Server] NLU result cb - event(%d)", event);
901 SECURE_SLOG(LOG_DEBUG, TAG_VCD, "[Server] result (%s)", nlu_result);
903 int ret = vc_info_parser_set_nlu_result(nlu_result);
905 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to set nlu result (%d)", ret);
912 int vcd_send_error(vce_error_e error, const char* msg, void *user_data)
914 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Engine Error cb - reason(%d), msg(%s)", error, msg);
915 ecore_main_loop_thread_safe_call_async(__cancel_by_error, NULL);
917 char* error_msg = NULL;
919 error_msg = strdup(msg);
922 if (0 != vcdc_send_error_signal_to_manager(vcd_client_manager_get_pid(), error, error_msg)) {
923 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to send error signal");
926 if (NULL != error_msg) {
931 return VCD_ERROR_NONE;
935 * vcd server Interfaces
937 static void __vcd_file_clean_up()
939 SLOG(LOG_DEBUG, TAG_VCD, "== Old file clean up == ");
942 struct dirent *dirp = NULL;
944 dp = opendir(VC_RUNTIME_INFO_ROOT);
946 SLOG(LOG_ERROR, TAG_VCD, "[File message WARN] Fail to open path : %s", VC_RUNTIME_INFO_ROOT);
950 char remove_path[256] = {0, };
955 if (!strncmp("vc_", dirp->d_name, strlen("vc_"))) {
956 memset(remove_path, 0, 256);
957 snprintf(remove_path, 256, "%s/%s", VC_RUNTIME_INFO_ROOT, dirp->d_name);
960 if (0 != remove(remove_path)) {
961 SLOG(LOG_WARN, TAG_VCD, "[File message WARN] Fail to remove file : %s", remove_path);
963 SLOG(LOG_DEBUG, TAG_VCD, "[File message] Remove file : %s", remove_path);
967 } while (NULL != dirp);
974 static int __vcd_db_clean_up()
977 int cnt = VC_COMMAND_TYPE_FOREGROUND;
979 if (VC_COMMAND_TYPE_BACKGROUND != cnt)
980 ret = vc_db_delete_commands(-1, cnt, NULL);
981 } while (VC_COMMAND_TYPE_EXCLUSIVE >= ++cnt);
986 int vcd_initialize(vce_request_callback_s *callback)
990 /* Remove old file */
991 __vcd_file_clean_up();
993 /* initialize modules */
994 ret = vcd_config_initialize(__config_lang_changed_cb, __config_foreground_changed_cb, NULL);
996 SLOG(LOG_ERROR, TAG_VCD, "[Server WARNING] Fail to initialize config.");
1000 ret = __vcd_db_clean_up();
1002 SLOG(LOG_ERROR, TAG_VCD, "[Server WARNING] Fail to remove db data");
1005 vcd_config_set_service_state(VCD_STATE_NONE);
1007 ret = vcd_engine_agent_init();
1009 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to engine agent initialize : result(%d)", ret);
1013 if (0 != vcd_recorder_create(__server_recorder_callback, __server_recorder_interrupt_callback)) {
1014 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to create recorder");
1015 return VCD_ERROR_OPERATION_FAILED;
1019 if (0 != vcd_engine_agent_load_current_engine(callback)) {
1020 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to load current engine");
1021 return VCD_ERROR_OPERATION_FAILED;
1024 /* Initialize manager info */
1025 vcd_client_manager_unset();
1027 vcd_config_set_service_state(VCD_STATE_READY);
1028 vcdc_send_service_state(VCD_STATE_READY);
1030 SLOG(LOG_ERROR, TAG_VCD, "[Server SUCCESS] initialize");
1038 if (0 < g_list_length(g_proc_list)) {
1039 iter = g_list_first(g_proc_list);
1040 while (NULL != iter) {
1041 g_proc_list = g_list_remove_link(g_proc_list, iter);
1042 iter = g_list_first(g_proc_list);
1046 if (g_restart_timer != NULL) {
1047 ecore_timer_del(g_restart_timer);
1048 g_restart_timer = NULL;
1051 vcd_state_e state = vcd_config_get_service_state();
1052 if (VCD_STATE_READY != state) {
1053 if (VCD_STATE_RECORDING == state) {
1054 vcd_recorder_stop();
1056 vcd_engine_recognize_cancel();
1058 if (0 != vcd_recorder_destroy()) {
1059 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to destroy recorder");
1061 SLOG(LOG_DEBUG, TAG_VCD, "[Server] destroy recorder");
1064 if (0 != vcd_engine_agent_release()) {
1065 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to release engine");
1067 SLOG(LOG_DEBUG, TAG_VCD, "[Server] release engine");
1070 vcd_client_manager_unset_appid();
1072 vcd_config_set_service_state(VCD_STATE_NONE);
1073 vcdc_send_service_state(VCD_STATE_NONE);
1075 SLOG(LOG_ERROR, TAG_VCD, "[Server] mode finalize");
1080 static Eina_Bool __finalize_quit_ecore_loop(void *data)
1082 SLOG(LOG_ERROR, TAG_VCD, "[Server] quit ecore main loop");
1083 ecore_main_loop_quit();
1087 static void __read_proc()
1090 struct dirent *dirp = NULL;
1094 if (0 < g_list_length(g_proc_list)) {
1095 iter = g_list_first(g_proc_list);
1096 while (NULL != iter) {
1097 g_proc_list = g_list_remove_link(g_proc_list, iter);
1098 iter = g_list_first(g_proc_list);
1102 dp = opendir("/proc");
1104 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Fail to open proc");
1110 tmp = atoi(dirp->d_name);
1111 if (0 >= tmp) continue;
1112 g_proc_list = g_list_append(g_proc_list, GINT_TO_POINTER(tmp));
1114 } while (NULL != dirp);
1120 static void __vcd_cleanup_client(vcd_client_type_e type)
1122 int* client_list = NULL;
1123 int client_count = 0;
1130 if (VCD_CLIENT_TYPE_NORMAL == type) {
1131 ret = vcd_client_get_list(&client_list, &client_count);
1132 } else if (VCD_CLIENT_TYPE_WIDGET == type) {
1133 ret = vcd_client_widget_get_list(&client_list, &client_count);
1134 } else if (VCD_CLIENT_TYPE_MANAGER == type) {
1135 mgr_pid = vcd_client_manager_get_pid();
1136 client_list = &mgr_pid;
1138 if (-1 == mgr_pid) {
1139 SLOG(LOG_WARN, TAG_VCD, "[WARNING] Invalid Manager pid");
1144 if (0 == ret || mgr_pid > 0) {
1145 SLOG(LOG_DEBUG, TAG_VCD, "@@@ Clean up %s client ", type ? (type == 1) ? "Widget" : "Manager" : "Normal");
1146 if (NULL != client_list && client_count > 0) {
1147 for (i = 0; i < client_count; i++) {
1150 for (j = 0; j < g_list_length(g_proc_list); j++) {
1151 iter = g_list_nth(g_proc_list, j);
1153 if (*(client_list + i) == GPOINTER_TO_INT(iter->data)) {
1154 SLOG(LOG_DEBUG, TAG_VCD, "%s pid(%d) is running", type ? (type == 1) ? "Widget" : "Manager" : "Normal", *(client_list + i));
1161 if (false == exist) {
1162 SLOG(LOG_ERROR, TAG_VCD, "%s pid(%d) should be removed", type ? (type == 1) ? "Widget" : "Manager" : "Normal", *(client_list + i));
1163 if (VCD_CLIENT_TYPE_NORMAL == type)
1164 vcd_server_finalize(*(client_list + i));
1165 else if (VCD_CLIENT_TYPE_WIDGET == type)
1166 vcd_server_widget_finalize(*(client_list + i));
1168 vcd_server_mgr_finalize(mgr_pid);
1172 SLOG(LOG_DEBUG, TAG_VCD, "@@@");
1174 if (NULL != client_list && -1 == mgr_pid) {
1181 Eina_Bool vcd_cleanup_client_all(void *data)
1185 __vcd_cleanup_client(VCD_CLIENT_TYPE_NORMAL);
1186 __vcd_cleanup_client(VCD_CLIENT_TYPE_WIDGET);
1187 __vcd_cleanup_client(VCD_CLIENT_TYPE_MANAGER);
1190 if (0 == vcd_client_get_list(&client_list, &client_count)) {
1191 SLOG(LOG_DEBUG, TAG_VCD, "@@@ Clean up client ");
1192 if (NULL != client_list && client_count > 0) {
1193 for (i = 0; i < client_count; i++) {
1196 for (j = 0; j < g_list_length(g_proc_list); j++) {
1197 iter = g_list_nth(g_proc_list, j);
1199 if (client_list[i] == GPOINTER_TO_INT(iter->data)) {
1200 SLOG(LOG_DEBUG, TAG_VCD, "pid(%d) is running", client_list[i]);
1207 if (false == exist) {
1208 SLOG(LOG_ERROR, TAG_VCD, "pid(%d) should be removed", client_list[i]);
1209 vcd_server_finalize(client_list[i]);
1212 result = vcdc_send_hello(client_list[i], VCD_CLIENT_TYPE_NORMAL);
1215 SLOG(LOG_DEBUG, TAG_VCD, "[Server] pid(%d) should be removed.", client_list[i]);
1216 vcd_server_finalize(client_list[i]);
1217 } else if (-1 == result) {
1218 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Hello result has error");
1223 SLOG(LOG_DEBUG, TAG_VCD, "@@@");
1225 if (NULL != client_list) {
1230 /* If app is in background state, app cannot response message. */
1231 if (0 == vcd_client_widget_get_list(&client_list, &client_count)) {
1232 SLOG(LOG_DEBUG, TAG_VCD, "@@@ Clean up widget");
1233 if (NULL != client_list && client_count > 0) {
1234 for (i = 0; i < client_count; i++) {
1237 for (j = 0; j < g_list_length(g_proc_list); j++) {
1238 iter = g_list_nth(g_proc_list, j);
1240 if (client_list[i] == GPOINTER_TO_INT(iter->data)) {
1241 SLOG(LOG_DEBUG, TAG_VCD, "widget pid(%d) is running", client_list[i]);
1248 if (false == exist) {
1249 SLOG(LOG_ERROR, TAG_VCD, "widget pid(%d) should be removed", client_list[i]);
1250 vcd_server_widget_finalize(client_list[i]);
1253 result = vcdc_send_hello(client_list[i], VCD_CLIENT_TYPE_WIDGET);
1256 SLOG(LOG_DEBUG, TAG_VCD, "[Server] widget pid(%d) should be removed.", client_list[i]);
1257 vcd_server_widget_finalize(client_list[i]);
1258 } else if (-1 == result) {
1259 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Hello result has error");
1264 SLOG(LOG_DEBUG, TAG_VCD, "@@@");
1267 if (NULL != client_list) {
1275 int mgr_pid = vcd_client_manager_get_pid();
1277 for (j = 0; j < g_list_length(g_proc_list); j++) {
1278 iter = g_list_nth(g_proc_list, j);
1280 if (mgr_pid == GPOINTER_TO_INT(iter->data)) {
1281 SLOG(LOG_DEBUG, TAG_VCD, "manager pid(%d) is running", mgr_pid);
1288 if (false == exist) {
1289 SLOG(LOG_ERROR, TAG_VCD, "manager pid (%d) should be removed", mgr_pid);
1290 vcd_server_mgr_finalize(mgr_pid);
1297 int vcd_server_get_service_state()
1299 return vcd_config_get_service_state();
1302 int vcd_server_get_foreground()
1305 vcd_config_get_foreground(&pid);
1313 int vcd_server_mgr_initialize(int pid)
1315 /* check if pid is valid */
1316 if (false == vcd_client_manager_is_valid(pid)) {
1317 SLOG(LOG_ERROR, TAG_VCD, "[Server] old manager pid(%d) be removed", vcd_client_manager_get_pid());
1318 vcd_server_mgr_cancel();
1319 vcd_client_manager_unset();
1322 /* Add client information to client manager */
1323 if (0 != vcd_client_manager_set(pid)) {
1324 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to add manager");
1325 return VCD_ERROR_OPERATION_FAILED;
1328 if (0 != vcdc_send_manager_pid(pid))
1329 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Fail to send manager pid");
1331 SLOG(LOG_ERROR, TAG_VCD, "[Server Success] Manager initialize : pid(%d)", pid);
1333 return VCD_ERROR_NONE;
1336 int vcd_server_mgr_finalize(int pid)
1338 /* check if pid is valid */
1339 if (false == vcd_client_manager_is_valid(pid)) {
1340 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] The manager pid(%d) is NOT valid", pid);
1341 return VCD_ERROR_INVALID_PARAMETER;
1344 /* Cancel recognition */
1345 vcd_server_mgr_cancel();
1347 if (0 != vcdc_send_manager_pid(-1))
1348 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Fail to send manager pid");
1350 /* Remove manager information */
1351 if (0 != vcd_client_manager_unset()) {
1352 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Fail to delete client");
1355 if (0 == vcd_client_get_ref_count()) {
1356 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Connected client list is empty");
1357 ecore_timer_add(0, __finalize_quit_ecore_loop, NULL);
1360 SLOG(LOG_ERROR, TAG_VCD, "[Server Success] Manager Finalize : pid(%d)", pid);
1362 return VCD_ERROR_NONE;
1365 int vcd_server_mgr_set_command(int pid)
1367 if (0 != vcd_client_manager_set_command(pid)) {
1368 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] The manager pid(%d) is NOT valid", pid);
1369 return VCD_ERROR_INVALID_PARAMETER;
1371 return VCD_ERROR_NONE;
1374 int vcd_server_mgr_unset_command(int pid)
1376 if (0 != vcd_client_manager_unset_command(pid)) {
1377 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] The manager pid(%d) is NOT valid", pid);
1378 return VCD_ERROR_INVALID_PARAMETER;
1380 return VCD_ERROR_NONE;
1383 int vcd_server_mgr_set_demandable_client(int pid)
1385 /* check if pid is valid */
1386 if (false == vcd_client_manager_is_valid(pid)) {
1387 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] The manager pid(%d) is NOT valid", pid);
1388 return VCD_ERROR_INVALID_PARAMETER;
1391 GSList* client_list = NULL;
1392 if (0 != vc_info_parser_get_demandable_clients(&client_list)) {
1393 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to get demandable client");
1394 return VCD_ERROR_OPERATION_FAILED;
1397 /* Save client list */
1398 if (0 != vcd_client_manager_set_demandable_client(pid, client_list)) {
1399 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to set demandable client list");
1400 return VCD_ERROR_OPERATION_FAILED;
1403 return VCD_ERROR_NONE;
1406 int vcd_server_mgr_set_audio_type(int pid, const char* audio_type)
1408 /* check if pid is valid */
1409 if (false == vcd_client_manager_is_valid(pid)) {
1410 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] The manager pid(%d) is NOT valid", pid);
1411 return VCD_ERROR_INVALID_PARAMETER;
1415 vce_audio_type_e type = VCE_AUDIO_TYPE_PCM_S16_LE;
1419 ret = vcd_engine_get_audio_format(audio_type, &type, &rate, &channel);
1421 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to get audio format : %d", ret);
1422 return VCD_ERROR_OPERATION_FAILED;
1425 ret = vcd_recorder_set(audio_type, type, rate, channel);
1427 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to set audio in type : %d", ret);
1428 return VCD_ERROR_OPERATION_FAILED;
1431 return VCD_ERROR_NONE;
1434 int vcd_server_mgr_get_audio_type(int pid, char** audio_type)
1436 /* check if pid is valid */
1437 if (false == vcd_client_manager_is_valid(pid)) {
1438 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] The manager pid(%d) is NOT valid", pid);
1439 return VCD_ERROR_INVALID_PARAMETER;
1442 int ret = vcd_recorder_get(audio_type);
1444 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to get audio in type : %d", ret);
1445 return VCD_ERROR_OPERATION_FAILED;
1448 return VCD_ERROR_NONE;
1451 int vcd_server_mgr_set_client_info(int pid)
1453 /* check if pid is valid */
1454 if (false == vcd_client_manager_is_valid(pid)) {
1455 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] The manager pid(%d) is NOT valid", pid);
1456 return VCD_ERROR_INVALID_PARAMETER;
1459 int ret = vcd_client_save_client_info();
1461 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to save client info : %d", ret);
1462 return VCD_ERROR_OPERATION_FAILED;
1465 return VCD_ERROR_NONE;
1468 static int __start_internal_recognition()
1472 if (0 != vcd_client_command_collect_command()) {
1473 SLOG(LOG_ERROR, TAG_VCD, "[Client Data ERROR] Fail to collect command");
1474 /* Send error cb to manager */
1475 int pid = vcd_client_widget_get_foreground_pid();
1477 vcdc_send_error_signal_to_manager(vcd_client_manager_get_pid(), VCD_ERROR_OPERATION_FAILED, "voice_engine.error.proc_fail");
1478 return VCD_ERROR_OPERATION_FAILED;
1481 /* 3. Set command to engine */
1482 ret = vcd_engine_set_commands();
1484 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to set commands : %d", ret);
1485 /* Send error cb to manager */
1486 int pid = vcd_client_widget_get_foreground_pid();
1488 vcdc_send_error_signal_to_manager(vcd_client_manager_get_pid(), VCD_ERROR_OPERATION_FAILED, "voice_engine.error.proc_fail");
1489 return VCD_ERROR_OPERATION_FAILED;
1492 SLOG(LOG_ERROR, TAG_VCD, "[Server Success] Set command");
1494 bool stop_by_silence = true;
1495 if (VCD_RECOGNITION_MODE_MANUAL == vcd_client_get_recognition_mode()) {
1496 stop_by_silence = false;
1499 vcd_client_manager_set_result_text(NULL);
1501 /* 4. start recognition */
1502 ret = vcd_engine_recognize_start(stop_by_silence);
1504 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to start recognition : result(%d)", ret);
1505 /* Send error cb to manager */
1506 int pid = vcd_client_widget_get_foreground_pid();
1508 vcdc_send_error_signal_to_manager(vcd_client_manager_get_pid(), VCD_ERROR_OPERATION_FAILED, "voice_engine.error.proc_fail");
1509 return VCD_ERROR_OPERATION_FAILED;
1512 SLOG(LOG_ERROR, TAG_VCD, "[Server Success] Start engine");
1515 /* 5. recorder start */
1516 ret = vcd_recorder_start();
1518 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to start recorder : result(%d)", ret);
1519 vcd_engine_recognize_cancel();
1520 /* Send error cb to manager */
1521 int pid = vcd_client_widget_get_foreground_pid();
1523 vcdc_send_error_signal_to_manager(vcd_client_manager_get_pid(), VCD_ERROR_OPERATION_FAILED, "voice_engine.error.proc_fail");
1528 vcd_config_set_service_state(VCD_STATE_RECORDING);
1529 vcdc_send_service_state(VCD_STATE_RECORDING);
1531 SLOG(LOG_ERROR, TAG_VCD, "[Server Success] Start recognition(%d)", stop_by_silence);
1536 static Eina_Bool __vcd_request_show_tooltip(void *data)
1538 int pid = vcd_client_widget_get_foreground_pid();
1540 SLOG(LOG_ERROR, TAG_VCD, "[Server] Request tooltip show and widget command, show(%d)", (bool)data);
1541 vcdc_send_show_tooltip(pid, (bool)data);
1547 int vcd_server_mgr_start(vcd_recognition_mode_e recognition_mode, bool exclusive_cmd, bool start_by_client)
1549 /* 1. check current state */
1550 vcd_state_e state = vcd_config_get_service_state();
1552 if (VCD_STATE_READY != state) {
1553 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Current state is not ready");
1554 return VCD_ERROR_INVALID_STATE;
1556 if (-1 == vcd_client_manager_get_pid()) {
1557 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Manager is NOT available.");
1558 return VCD_ERROR_OPERATION_FAILED;
1560 vcd_client_widget_set_waiting_for_recording(-1, false);
1562 SLOG(LOG_ERROR, TAG_VCD, "[Server] set recognition mode = %d", recognition_mode);
1563 vcd_client_set_recognition_mode(recognition_mode);
1565 if (false == exclusive_cmd) {
1566 vcd_client_update_foreground_pid();
1567 /* Notify show tooltip */
1568 if (1 == vcd_config_get_command_type_enabled(VC_COMMAND_TYPE_WIDGET)) {
1569 int pid = vcd_client_widget_get_foreground_pid();
1571 SLOG(LOG_ERROR, TAG_VCD, "[Server] Request tooltip show and widget command");
1572 ecore_timer_add(0, __vcd_request_show_tooltip, (void*)true);
1573 vcd_client_widget_set_waiting_for_recording(pid, true);
1578 vcd_client_manager_set_exclusive(exclusive_cmd);
1582 if (true == start_by_client) {
1583 /* Get foreground pid */
1584 if (0 != vcd_config_get_foreground(&fg_pid)) {
1585 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to get foreground");
1588 /* Set client exclusive option */
1589 if (0 != vcd_client_set_exclusive_command(fg_pid)) {
1590 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to set exclusive command");
1594 SLOG(LOG_ERROR, TAG_VCD, "[Server] start internal recognition");
1596 int ret = __start_internal_recognition();
1598 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to start recongition : %d", ret);
1602 if (true == start_by_client) {
1603 vcd_client_unset_exclusive_command(fg_pid);
1606 SLOG(LOG_ERROR, TAG_VCD, "[Server Success] start internal recognition");
1607 return VCD_ERROR_NONE;
1610 int vcd_server_mgr_stop()
1612 /* 1. Check current state is recording */
1613 if (VCD_STATE_RECORDING != vcd_config_get_service_state()) {
1614 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Current state is not recording");
1615 return VCD_ERROR_INVALID_STATE;
1617 if (-1 == vcd_client_manager_get_pid()) {
1618 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Manager is NOT available.");
1619 return VCD_ERROR_OPERATION_FAILED;
1621 SLOG(LOG_ERROR, TAG_VCD, "[Server] stop internal recognition");
1624 /* 2. Stop recorder */
1625 vcd_recorder_stop();
1628 /* 3. Stop engine recognition */
1629 int ret = vcd_engine_recognize_stop();
1631 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to stop recognition : %d", ret);
1634 /* 4. Set original mode */
1635 vcd_config_set_service_state(VCD_STATE_PROCESSING);
1636 vcdc_send_service_state(VCD_STATE_PROCESSING);
1638 SLOG(LOG_ERROR, TAG_VCD, "[Server Success] stop internal recognition");
1639 return VCD_ERROR_NONE;
1642 int vcd_server_mgr_cancel()
1644 if (-1 == vcd_client_manager_get_pid()) {
1645 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Manager is NOT available.");
1646 return VCD_ERROR_OPERATION_FAILED;
1649 if (g_restart_timer != NULL) {
1650 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Delete restart engine timer");
1651 ecore_timer_del(g_restart_timer);
1652 g_restart_timer = NULL;
1655 /* 1. Check current state */
1656 vcd_state_e state = vcd_config_get_service_state();
1657 if (VCD_STATE_READY == state) {
1658 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Current state is READY");
1659 vcd_recorder_stop();
1661 if (false == vcd_client_manager_get_exclusive()) {
1662 if (1 == vcd_config_get_command_type_enabled(VC_COMMAND_TYPE_WIDGET)) {
1663 int pid = vcd_client_widget_get_foreground_pid();
1665 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Request tooltip hide");
1666 ecore_timer_add(0, __vcd_request_show_tooltip, (void*)false);
1671 vcdc_send_service_state(VCD_STATE_READY);
1672 return VCD_ERROR_NONE;
1674 SLOG(LOG_ERROR, TAG_VCD, "[Server] cancel internal recognition");
1677 /* 2. Stop recorder */
1678 vcd_recorder_stop();
1681 /* 3. Cancel engine */
1682 int ret = vcd_engine_recognize_cancel();
1684 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Fail to cancel : %d", ret);
1687 if (false == vcd_client_manager_get_exclusive()) {
1688 if (1 == vcd_config_get_command_type_enabled(VC_COMMAND_TYPE_WIDGET)) {
1689 int pid = vcd_client_widget_get_foreground_pid();
1691 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Request tooltip hide");
1692 ecore_timer_add(0, __vcd_request_show_tooltip, (void*)false);
1696 vcd_client_manager_set_exclusive(false);
1700 vcd_config_set_service_state(VCD_STATE_READY);
1701 vcdc_send_service_state(VCD_STATE_READY);
1703 SLOG(LOG_ERROR, TAG_VCD, "[Server Success] cancel internal recognition");
1704 return VCD_ERROR_NONE;
1708 int vcd_server_mgr_result_select()
1710 __vcd_send_selected_result(NULL);
1712 return VCD_ERROR_NONE;
1715 int vcd_server_mgr_set_domain(int pid, const char* domain)
1717 /* check if pid is valid */
1718 if (false == vcd_client_manager_is_valid(pid)) {
1719 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] The manager pid(%d) is NOT valid", pid);
1720 return VCD_ERROR_INVALID_PARAMETER;
1723 vcd_state_e state = vcd_config_get_service_state();
1724 if (VCD_STATE_READY != state) {
1725 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Current state is not ready");
1726 return VCD_ERROR_INVALID_STATE;
1729 /* Set domain to engine */
1730 int ret = vcd_engine_set_domain(pid, domain);
1732 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to set domain : %d", ret);
1734 SLOG(LOG_DEBUG, TAG_VCD, "[Server SUCCESS] Set domain");
1740 int vcd_server_mgr_set_private_data(int pid, const char* key, const char* data)
1742 /* check if pid is valid */
1743 if (false == vcd_client_manager_is_valid(pid)) {
1744 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] The manager pid(%d) is NOT valid", pid);
1745 return VCD_ERROR_INVALID_PARAMETER;
1748 vcd_state_e state = vcd_config_get_service_state();
1749 if (VCD_STATE_READY != state) {
1750 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Current state is not ready");
1751 return VCD_ERROR_INVALID_STATE;
1754 /* Set private data to engine */
1755 int ret = vcd_engine_set_private_data(pid, key, data);
1757 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to set private data : %d", ret);
1759 SLOG(LOG_DEBUG, TAG_VCD, "[Server SUCCESS] Set private data");
1765 int vcd_server_mgr_get_private_data(int pid, const char* key, char** data)
1767 /* check if pid is valid */
1768 if (false == vcd_client_manager_is_valid(pid)) {
1769 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] The manager pid(%d) is NOT valid", pid);
1770 return VCD_ERROR_INVALID_PARAMETER;
1772 vcd_state_e state = vcd_config_get_service_state();
1773 if (VCD_STATE_READY != state) {
1774 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Current state is not ready");
1775 return VCD_ERROR_INVALID_STATE;
1778 /* Get private data to engine */
1779 int ret = vcd_engine_get_private_data(pid, key, data);
1781 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to get private data : %d", ret);
1783 SLOG(LOG_DEBUG, TAG_VCD, "[Server SUCCESS] Set private data");
1789 int vcd_server_mgr_do_action(int pid, int type, const char* action)
1793 /* check if pid is valid */
1794 if (false == vcd_client_manager_is_valid(pid)) {
1795 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] The manager pid(%d) is NOT valid", pid);
1796 return VCD_ERROR_INVALID_PARAMETER;
1799 vcd_state_e state = vcd_config_get_service_state();
1800 if (VCD_STATE_READY != state) {
1801 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Current state is not ready");
1802 return VCD_ERROR_INVALID_STATE;
1805 /* Reqeust do action to engine */
1806 if (VCD_SEND_EVENT_TYPE_TEXT == type)
1807 ret = vcd_engine_process_text(pid, action);
1808 else if (VCD_SEND_EVENT_TYPE_LIST_EVENT == type)
1809 ret = vcd_engine_process_list_event(pid, action);
1810 else if (VCD_SEND_EVENT_TYPE_HAPTIC_EVENT == type)
1811 ret = vcd_engine_process_haptic_event(pid, action);
1814 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to process do action : %d", ret);
1816 vcd_config_set_service_state(VCD_STATE_PROCESSING);
1817 vcdc_send_service_state(VCD_STATE_PROCESSING);
1818 SLOG(LOG_DEBUG, TAG_VCD, "[Server SUCCESS] Process do action");
1824 int vcd_server_mgr_enable_command_type(int pid, int cmd_type)
1828 /* check if pid is valid */
1829 if (false == vcd_client_manager_is_valid(pid)) {
1830 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] The manager pid(%d) is NOT valid", pid);
1831 return VCD_ERROR_INVALID_PARAMETER;
1834 vcd_state_e state = vcd_config_get_service_state();
1835 if (VCD_STATE_READY != state) {
1836 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Current state is not ready");
1837 return VCD_ERROR_INVALID_STATE;
1840 ret = vcd_config_enable_command_type(cmd_type);
1842 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to enable command type");
1844 SLOG(LOG_ERROR, TAG_VCD, "[Server] Enable command type(%d)", cmd_type);
1850 int vcd_server_mgr_disable_command_type(int pid, int cmd_type)
1854 /* check if pid is valid */
1855 if (false == vcd_client_manager_is_valid(pid)) {
1856 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] The manager pid(%d) is NOT valid", pid);
1857 return VCD_ERROR_INVALID_PARAMETER;
1860 vcd_state_e state = vcd_config_get_service_state();
1861 if (VCD_STATE_READY != state) {
1862 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Current state is not ready");
1863 return VCD_ERROR_INVALID_STATE;
1866 ret = vcd_config_disable_command_type(cmd_type);
1868 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to disable command type");
1870 SLOG(LOG_ERROR, TAG_VCD, "[Server] Disable command type(%d)", cmd_type);
1877 * VC Server Functions for Client
1879 int vcd_server_initialize(int pid)
1881 if (false == vcd_engine_is_available_engine()) {
1882 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] No Engine");
1883 return VCD_ERROR_ENGINE_NOT_FOUND;
1886 /* check if pid is valid */
1887 if (true == vcd_client_is_available(pid)) {
1888 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] The pid is already exist");
1889 return VCD_ERROR_INVALID_PARAMETER;
1892 /* Add client information to client manager */
1893 if (0 != vcd_client_add(pid)) {
1894 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to add client info");
1895 return VCD_ERROR_OPERATION_FAILED;
1898 SLOG(LOG_DEBUG, TAG_VCD, "[Server Success] Client Initialize : pid(%d)", pid);
1900 return VCD_ERROR_NONE;
1903 int vcd_server_finalize(int pid)
1905 /* check if pid is valid */
1906 if (false == vcd_client_is_available(pid)) {
1907 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] pid is NOT valid ");
1908 return VCD_ERROR_INVALID_PARAMETER;
1911 /* Remove client information */
1912 if (0 != vcd_client_delete(pid)) {
1913 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Fail to delete client");
1916 if (0 == vcd_client_get_ref_count()) {
1917 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Connected client list is empty");
1918 ecore_timer_add(0, __finalize_quit_ecore_loop, NULL);
1921 SLOG(LOG_DEBUG, TAG_VCD, "[Server Success] Client Finalize : pid(%d)", pid);
1923 return VCD_ERROR_NONE;
1926 int vcd_server_set_command(int pid, vc_cmd_type_e cmd_type)
1928 /* check if pid is valid */
1929 if (false == vcd_client_is_available(pid)) {
1930 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] pid is NOT valid ");
1931 return VCD_ERROR_INVALID_PARAMETER;
1934 if (0 != vcd_client_set_command_type(pid, cmd_type)) {
1935 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to set command type : pid(%d), cmd_type(%d)", pid, cmd_type);
1936 return VCD_ERROR_OPERATION_FAILED;
1942 int vcd_server_unset_command(int pid, vc_cmd_type_e cmd_type)
1944 /* check if pid is valid */
1945 if (false == vcd_client_is_available(pid)) {
1946 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] pid is NOT valid ");
1947 return VCD_ERROR_INVALID_PARAMETER;
1950 if (0 != vcd_client_unset_command_type(pid, cmd_type)) {
1951 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to unset command type : pid(%d), cmd_type(%d)", pid, cmd_type);
1952 return VCD_ERROR_OPERATION_FAILED;
1958 int vcd_server_set_foreground(int pid, bool value)
1960 /* check if pid is valid */
1961 if (false == vcd_client_is_available(pid) && false == vcd_client_widget_is_available(pid)) {
1962 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] pid is NOT valid ");
1963 return VCD_ERROR_INVALID_PARAMETER;
1966 if (0 != vcd_config_set_foreground(pid, value)) {
1967 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to set foreground : pid(%d), value(%d)", pid, value);
1968 return VCD_ERROR_OPERATION_FAILED;
1974 static int __vcd_server_launch_manager_app()
1977 bool running = false;
1980 ret = vcd_client_manager_get_appid(&appid);
1981 if (0 != ret || NULL == appid) {
1982 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Fail to get manager appid");
1983 return VCD_ERROR_OPERATION_FAILED;
1985 ret = app_manager_is_running(appid, &running);
1987 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Fail to check running with appid(%s)", appid);
1990 return VCD_ERROR_OPERATION_FAILED;
1992 if (false == running) {
1993 int tmp_ret = __vcd_is_package_installed(appid);
1994 if (false == tmp_ret) {
1995 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] manager app is not installed, appid(%s)", appid);
1997 ret = __vcd_activate_app_by_appcontrol(appid);
1999 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Fail to activate app");
2004 SLOG(LOG_ERROR, TAG_VCD, "Launch vc manager app: appid(%s)", appid);
2007 ret = __vcd_resume_app(appid);
2009 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Fail to resume app");
2014 SLOG(LOG_ERROR, TAG_VCD, "Resume vc manager app: appid(%s)", appid);
2020 return VCD_ERROR_NONE;
2023 int vcd_server_dialog(int pid, const char* disp_text, const char* utt_text, int continuous)
2025 /* check if pid is valid */
2026 if (false == vcd_client_is_available(pid) && false == vcd_client_widget_is_available(pid)) {
2027 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] pid is NOT valid ");
2028 return VCD_ERROR_INVALID_PARAMETER;
2031 int ret = __vcd_server_launch_manager_app();
2033 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);
2037 ret = vcdc_send_dialog(vcd_client_manager_get_pid(), pid, disp_text, utt_text, continuous);
2039 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);
2046 int vcd_server_is_system_command_valid(int pid, int* is_sys_cmd_valid)
2048 /* check if pid is valid */
2049 if (false == vcd_client_is_available(pid) && false == vcd_client_widget_is_available(pid)) {
2050 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] pid is NOT valid ");
2051 return VCD_ERROR_INVALID_PARAMETER;
2054 /* check if system command is valid */
2055 if (true == vcd_client_manager_is_system_command_valid(vcd_client_manager_get_pid()))
2056 *is_sys_cmd_valid = true;
2058 *is_sys_cmd_valid = false;
2064 int vcd_server_set_exclusive_command(int pid, bool value)
2066 /* check if pid is valid */
2067 if (false == vcd_client_is_available(pid)) {
2068 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] pid is NOT valid ");
2069 return VCD_ERROR_INVALID_PARAMETER;
2072 if (true == value) {
2073 if (0 != vcd_client_set_exclusive_command(pid)) {
2074 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to set exclusive command : pid(%d)", pid);
2075 return VCD_ERROR_OPERATION_FAILED;
2078 if (0 != vcd_client_unset_exclusive_command(pid)) {
2079 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to unset exclusive command : pid(%d)", pid);
2080 return VCD_ERROR_OPERATION_FAILED;
2087 int vcd_server_request_start(int pid, bool stop_by_silence)
2089 /* check if pid is valid */
2090 if (false == vcd_client_is_available(pid)) {
2091 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] pid(%d) is NOT forground client", pid);
2092 return VCD_ERROR_INVALID_PARAMETER;
2096 /* Check current state */
2097 vcd_state_e state = vcd_config_get_service_state();
2099 /* Service state should be ready */
2100 if (VCD_STATE_READY != state) {
2101 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Current state is not Ready : pid(%d)", pid);
2102 return VCD_ERROR_INVALID_STATE;
2105 if (-1 != vcd_client_manager_get_pid()) {
2106 /* Check current pid is valid */
2107 if (false == vcd_client_manager_check_demandable_client(pid)) {
2108 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Current client is NOT available : pid(%d)", pid);
2109 return VCD_ERROR_INVALID_PARAMETER;
2113 ret = vcd_server_mgr_start(stop_by_silence, false);
2115 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Fail to start recognition");
2116 return VCD_ERROR_INVALID_PARAMETER;
2122 int vcd_server_request_stop(int pid)
2125 /* Check current state */
2126 vcd_state_e state = vcd_config_get_service_state();
2128 /* Service state should be ready */
2129 if (VCD_STATE_RECORDING != state) {
2130 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Current state is not Recording : pid(%d)", pid);
2131 return VCD_ERROR_INVALID_STATE;
2134 if (-1 != vcd_client_manager_get_pid()) {
2135 /* Check current pid is valid */
2136 if (false == vcd_client_manager_check_demandable_client(pid)) {
2137 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Current client is NOT available : pid(%d)", pid);
2138 return VCD_ERROR_INVALID_PARAMETER;
2142 ret = vcd_server_mgr_stop();
2144 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Fail to start recognition");
2145 return VCD_ERROR_OPERATION_FAILED;
2148 return VCD_ERROR_NONE;
2151 int vcd_server_request_cancel(int pid)
2154 /* Check current state */
2155 vcd_state_e state = vcd_config_get_service_state();
2157 /* Service state should be recording or processing */
2158 if (VCD_STATE_RECORDING != state && VCD_STATE_PROCESSING != state) {
2159 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Current state is not Recording or Processing : pid(%d)", pid);
2160 return VCD_ERROR_INVALID_STATE;
2163 if (-1 != vcd_client_manager_get_pid()) {
2164 /* Check current pid is valid */
2165 if (false == vcd_client_manager_check_demandable_client(pid)) {
2166 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Current client is NOT available : pid(%d)", pid);
2167 return VCD_ERROR_INVALID_PARAMETER;
2171 ret = vcd_server_mgr_cancel();
2173 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Fail to start recognition");
2174 return VCD_ERROR_OPERATION_FAILED;
2177 return VCD_ERROR_NONE;
2182 * VC Server Functions for Widget lib
2184 int vcd_server_widget_initialize(int pid)
2186 if (false == vcd_engine_is_available_engine()) {
2187 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] No Engine");
2188 return VCD_ERROR_ENGINE_NOT_FOUND;
2191 /* check if pid is valid */
2192 if (true == vcd_client_widget_is_available(pid)) {
2193 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] The pid is already exist");
2194 return VCD_ERROR_NONE;
2197 /* Add client information to client manager */
2198 if (0 != vcd_client_widget_add(pid)) {
2199 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to add client info");
2200 return VCD_ERROR_OPERATION_FAILED;
2203 SLOG(LOG_DEBUG, TAG_VCD, "[Server Success] Initialize widget : pid(%d)", pid);
2205 return VCD_ERROR_NONE;
2208 static void __vcd_server_widget_start_recording(void *data)
2210 if (0 != __start_internal_recognition()) {
2211 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to start recongition");
2213 vcd_client_widget_set_waiting_for_recording(-1, false);
2216 int vcd_server_widget_finalize(int pid)
2218 /* check if pid is valid */
2219 if (false == vcd_client_widget_is_available(pid)) {
2220 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] pid is NOT valid ");
2221 return VCD_ERROR_INVALID_PARAMETER;
2224 /* Remove client information */
2225 if (0 != vcd_client_widget_delete(pid)) {
2226 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Fail to delete client");
2229 if (0 == vcd_client_get_ref_count()) {
2230 SLOG(LOG_ERROR, TAG_VCD, "[Server] connected client list is empty");
2231 ecore_timer_add(0, __finalize_quit_ecore_loop, NULL);
2235 bool is_waiting = false;
2236 if (0 != vcd_client_widget_get_waiting_for_recording(pid, &is_waiting)) {
2237 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Fail to get waiting to recording");
2240 if (true == is_waiting) {
2241 SLOG(LOG_INFO, TAG_VCD, "[Server INFO] invoke to start recording");
2242 ecore_main_loop_thread_safe_call_async(__vcd_server_widget_start_recording, NULL);
2244 return VCD_ERROR_NONE;
2247 int vcd_server_widget_start_recording(int pid, bool widget_command)
2249 /* check if pid is valid */
2250 if (false == vcd_client_widget_is_available(pid)) {
2251 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] pid is NOT valid ");
2252 return VCD_ERROR_INVALID_PARAMETER;
2255 if (true == widget_command) {
2256 vcd_client_widget_set_command(pid);
2257 SLOG(LOG_DEBUG, TAG_VCD, "[Server] widget command is available");
2259 vcd_client_widget_unset_command(pid);
2260 SLOG(LOG_WARN, TAG_VCD, "[Server] widget command is NOT available");
2263 SLOG(LOG_ERROR, TAG_VCD, "[Server] start internal recongition : %d", widget_command);
2264 int ret = __start_internal_recognition();
2266 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to start recongition : %d", ret);
2267 ecore_timer_add(0, __vcd_request_show_tooltip, (void*)false);
2273 int vcd_server_widget_start(int pid, bool stop_by_silence)
2275 /* check if pid is valid */
2276 int fore_pid = vcd_client_widget_get_foreground_pid();
2277 if (pid != fore_pid) {
2278 SLOG(LOG_ERROR, TAG_VCD, "[Server] pid is NOT foreground");
2279 return VCD_ERROR_INVALID_PARAMETER;
2282 /* Check current state */
2283 vcd_state_e state = vcd_config_get_service_state();
2285 /* Service state should be ready */
2286 if (VCD_STATE_READY != state) {
2287 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Current state is not Ready : pid(%d)", pid);
2288 return VCD_ERROR_INVALID_STATE;
2291 vcd_client_set_slience_detection(stop_by_silence);
2293 /* Notify show tooltip */
2294 ecore_timer_add(0, __vcd_request_show_tooltip, (void*)true);
2299 int vcd_server_widget_stop(int pid)
2301 /* check if pid is valid */
2302 int fore_pid = vcd_client_widget_get_foreground_pid();
2303 if (pid != fore_pid) {
2304 SLOG(LOG_ERROR, TAG_VCD, "[Server] pid is NOT foreground");
2305 return VCD_ERROR_INVALID_PARAMETER;
2309 /* Check current state */
2310 vcd_state_e state = vcd_config_get_service_state();
2312 /* Service state should be recording */
2313 if (VCD_STATE_RECORDING != state) {
2314 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Current service state is not Recording : pid(%d)", pid);
2315 return VCD_ERROR_INVALID_STATE;
2318 ret = vcd_server_mgr_stop();
2320 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Fail to start recognition");
2321 return VCD_ERROR_OPERATION_FAILED;
2324 return VCD_ERROR_NONE;
2327 int vcd_server_widget_cancel(int pid)
2329 /* check if pid is valid */
2330 int fore_pid = vcd_client_widget_get_foreground_pid();
2331 if (pid != fore_pid) {
2332 SLOG(LOG_ERROR, TAG_VCD, "[Server] pid is NOT foreground");
2333 return VCD_ERROR_INVALID_PARAMETER;
2337 /* Check current state */
2338 vcd_state_e state = vcd_config_get_service_state();
2340 /* Service state should be recording or processing */
2341 if (VCD_STATE_RECORDING != state && VCD_STATE_PROCESSING != state) {
2342 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Current state is not Recording or Processing : pid(%d)", pid);
2343 return VCD_ERROR_INVALID_STATE;
2346 ret = vcd_server_mgr_cancel();
2348 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Fail to cancel recognition : %d", ret);
2352 return VCD_ERROR_NONE;
2355 int vcd_server_widget_enable_asr_result(int pid, bool enable)
2358 ret = vcd_client_widget_set_asr_result_enabled(pid, enable);
2360 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Fail to enable asr result : %d", ret);
2366 int vcd_server_set_language(const char* language)
2368 int ret = VCD_ERROR_NONE;
2370 ret = vcd_config_set_default_language(language);
2372 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Fail to set language : %d", ret);
2376 ret = vcd_engine_set_current_language(language);
2378 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Fail to set language : %d", ret);
2385 * For engine service
2387 int vcd_get_foreach_command(vce_cmd_h vce_command, vce_command_cb callback, void* user_data)
2389 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Get foreach command");
2391 if (NULL == callback) {
2392 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] input parameter is NULL");
2393 return VCD_ERROR_INVALID_PARAMETER;
2397 ret = vcd_engine_agent_get_foreach_command(vce_command, callback, user_data);
2399 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to get foreach command : ret(%d)", ret);
2405 int vcd_get_command_count(vce_cmd_h vce_command)
2407 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Get command count");
2410 ret = vcd_engine_agent_get_command_count(vce_command);
2412 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to get command count : ret(%d)", ret);
2418 int vcd_get_audio_type(char** audio_type)
2420 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Get audio type");
2423 ret = vcd_engine_agent_get_audio_type(audio_type);
2425 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to get audio type : ret(%d)", ret);
2431 int vcd_set_private_data(const char* key, const char* data)
2433 vcd_state_e state = vcd_config_get_service_state();
2435 if (VCD_STATE_READY != state) {
2436 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Current state(%d) is NOT 'READY'", state);
2437 return VCD_ERROR_INVALID_STATE;
2440 int ret = vcd_engine_agent_set_private_data(key, data);
2442 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to set private data to the manager client : ret(%d)", ret);
2444 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Set private data to the manager client, key(%s), data(%s)", key, data);
2450 int vcd_get_private_data(const char* key, char** data)
2452 vcd_state_e state = vcd_config_get_service_state();
2454 if (VCD_STATE_READY != state) {
2455 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Current state(%d) is NOT 'READY'", state);
2456 return VCD_ERROR_INVALID_STATE;
2459 int ret = vcd_engine_agent_get_private_data(key, data);
2461 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to get private data from the manager client : ret(%d)", ret);
2463 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Get private data from the manager client, key(%s), data(%s)", key, data);
2469 int vcd_start_recording()
2471 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Start recording");
2474 ret = vcd_engine_agent_start_recording();
2476 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to start recording : ret(%d)", ret);
2482 int vcd_stop_recording()
2484 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Stop recording");
2487 ret = vcd_engine_agent_stop_recording();
2489 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to stop recording : ret(%d)", ret);
2495 int vcd_set_private_data_set_cb(vce_private_data_set_cb callback_func)
2497 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Set private data set cb");
2500 ret = vcd_engine_agent_set_private_data_set_cb(callback_func);
2502 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to set private data set cb : ret(%d)", ret);
2508 int vcd_set_private_data_requested_cb(vce_private_data_requested_cb callback_func)
2510 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Set private data requested cb");
2513 ret = vcd_engine_agent_set_private_data_requested_cb(callback_func);
2515 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to set private data requested cb : ret(%d)", ret);
2521 int vcd_set_nlu_base_info_requested_cb(vce_nlu_base_info_requested_cb callback_func)
2523 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Set nlu base info requested cb");
2526 ret = vcd_engine_agent_set_nlu_base_info_requested_cb(callback_func);
2528 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to set nlu base info requested cb : ret(%d)", ret);