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_DEBUG, 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 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 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 ret = vcdc_send_dialog(vcd_client_manager_get_pid(), -1, nlg_result, NULL, 0); //0: VC_DIALOG_END
445 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to send dialog : mgr_pid(%d), nlg_result(%s)", vcd_client_manager_get_pid(), nlg_result);
449 static void* __recorder_stop(void *data)
455 int vcd_send_result(vce_result_event_e event, int* result_id, int count, const char* all_result, const char* non_fixed_result, const char* nlu_result, const char* msg, void *user_data)
459 if (VCD_STATE_PROCESSING != vcd_config_get_service_state()) {
460 if (VCD_RECOGNITION_MODE_RESTART_CONTINUOUSLY != vcd_client_get_recognition_mode()) {
461 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Current state is not 'Processing' and mode is not 'Restart continuously'");
462 return VCD_ERROR_INVALID_STATE;
466 vc_info_parser_unset_result(vcd_client_manager_get_exclusive());
467 vcd_client_manager_set_result_text(all_result);
469 SLOG(LOG_INFO, TAG_VCD, "[Server] Event(%d), Text(%s) Nonfixed(%s) Msg(%s) Result count(%d)",
470 event, all_result, non_fixed_result, msg, count);
472 if (VCD_RECOGNITION_MODE_RESTART_AFTER_REJECT == vcd_client_get_recognition_mode()) {
473 if (VCE_RESULT_EVENT_REJECTED == event) {
474 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Restart by no or rejected result");
475 /* If no result and restart option is ON */
476 /* Send reject message */
477 bool temp = vcd_client_manager_get_exclusive();
478 vc_info_parser_set_result(all_result, event, msg, NULL, temp);
479 ret = vcdc_send_result_to_manager(vcd_client_manager_get_pid(), VC_RESULT_TYPE_NOTIFICATION);
481 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Fail to send result");
484 g_restart_timer = ecore_timer_add(0, __restart_engine, NULL);
487 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Stop recorder due to success");
488 //vcd_recorder_stop();
489 ecore_main_loop_thread_safe_call_sync(__recorder_stop, NULL);
490 } else if (VCD_RECOGNITION_MODE_RESTART_CONTINUOUSLY == vcd_client_get_recognition_mode()) {
491 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Restart continuously");
492 /* Restart option is ON */
493 g_restart_timer = ecore_timer_add(0, __restart_engine, NULL);
494 if (VCE_RESULT_EVENT_REJECTED == event) {
495 bool temp = vcd_client_manager_get_exclusive();
496 vc_info_parser_set_result(all_result, event, msg, NULL, temp);
497 ret = vcdc_send_result_to_manager(vcd_client_manager_get_pid(), VC_RESULT_TYPE_NOTIFICATION);
499 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Fail to send result");
506 int pid = vcd_client_widget_get_foreground_pid();
508 if (NULL != all_result) {
510 vcd_client_widget_get_asr_result_enabled(pid, &enable);
511 if (true == enable) {
512 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Send ASR result to Widget client");
513 bool is_consumed = false;
514 if (0 != vcdc_send_asr_result(pid, event, all_result, VC_COMMAND_TYPE_WIDGET, &is_consumed)) {
515 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to send asr result");
517 SLOG(LOG_DEBUG, TAG_VCD, "[Server] ASR result is consumed(%d)", is_consumed);
518 if (true == is_consumed) {
519 vcdc_send_show_tooltip(pid, false);
520 if (-1 != vcd_client_manager_get_pid()) {
521 /* Manager client is available */
522 vc_info_parser_unset_result(false);
523 vc_info_parser_set_result(all_result, VC_RESULT_EVENT_RESULT_SUCCESS, msg, NULL, false);
524 if (0 != vcdc_send_result_to_manager(vcd_client_manager_get_pid(), VC_RESULT_TYPE_NOTIFICATION)) {
525 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Fail to send result");
529 vcd_client_manager_set_exclusive(false);
531 vcd_config_set_service_state(VCD_STATE_READY);
532 vcdc_send_service_state(VCD_STATE_READY);
533 return VCD_ERROR_NONE;
540 /* if nlu_result is exist, Add command handle(is_action) into result list */
542 SLOG(LOG_DEBUG, TAG_VCD, "[Server] @ Get engine result @");
544 vc_cmd_s* temp_cmd = NULL;
545 vc_cmd_list_h vc_cmd_list = NULL;
547 if (0 != vc_cmd_list_create(&vc_cmd_list)) {
548 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Fail to create command list");
549 vcd_client_manager_set_exclusive(false);
550 vcd_config_set_service_state(VCD_STATE_READY);
551 vcdc_send_service_state(VCD_STATE_READY);
552 return VCD_ERROR_NONE;
555 /* priority filter */
556 /* system > exclusive > foreground = widget > system_background > background */
558 int* filtered_id = (int*)calloc(count, sizeof(int));
560 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to allocate memory");
561 return VCD_ERROR_OUT_OF_MEMORY;
563 int filtered_count = 0;
564 int top_priority = VC_COMMAND_PRIORITY_BACKGROUND;
565 for (i = 0; i < count; i++) {
566 SLOG(LOG_INFO, TAG_VCD, "[Server] [%d] Result id(%d)", i, result_id[i]);
568 if (0 > result_id[i]) {
569 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Result ID(%d) is NOT valid", result_id[i]);
573 ret = vcd_client_get_cmd_from_result_id(result_id[i], &temp_cmd);
574 if (0 == ret && NULL != temp_cmd) {
575 if (top_priority == temp_cmd->priority) {
576 filtered_id[filtered_count] = result_id[i];
578 } else if (top_priority > temp_cmd->priority) {
579 filtered_id[0] = result_id[i];
581 top_priority = temp_cmd->priority;
584 vc_cmd_destroy((vc_cmd_h)temp_cmd);
590 for (i = 0; i < filtered_count; i++) {
591 SLOG(LOG_INFO, TAG_VCD, "[Server] [%d] Filtered Result id(%d)", i, filtered_id[i]);
593 if (filtered_id[i] < 0) {
594 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Filtered ID(%d) is NOT valid", filtered_id[i]);
598 ret = vcd_client_get_cmd_from_result_id(filtered_id[i], &temp_cmd);
599 if (0 == ret && NULL != temp_cmd) {
600 switch (temp_cmd->format) {
601 case VC_CMD_FORMAT_FIXED:
602 case VC_CMD_FORMAT_FIXED_AND_VFIXED:
603 case VC_CMD_FORMAT_VFIXED_AND_FIXED:
604 case VC_CMD_FORMAT_PARTIAL:
605 case VC_CMD_FORMAT_FIXED_AND_NONFIXED:
606 case VC_CMD_FORMAT_NONFIXED_AND_FIXED:
608 case VC_CMD_FORMAT_ACTION:
612 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Unknown command type : %d", temp_cmd->type);
616 if (0 != vc_cmd_list_add(vc_cmd_list, (vc_cmd_h)temp_cmd)) {
617 SLOG(LOG_DEBUG, TAG_VCD, "Fail to add command to list");
618 vc_cmd_destroy((vc_cmd_h)temp_cmd);
622 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] NOT found matached result(%d)", filtered_id[i]);
626 if (NULL != filtered_id) {
631 if (NULL != nlu_result) {
632 SLOG(LOG_INFO, TAG_VCD, "[Server] NLU (%s)", nlu_result);
633 vc_info_parser_set_nlu_result(nlu_result);
634 if (0 == is_action) {
636 if (0 != vc_cmd_create(&nlu_cmd)) {
637 SLOG(LOG_ERROR, TAG_VCD, "Fail to nlu cmd create");
639 if (0 != vc_cmd_set_type(nlu_cmd, VC_COMMAND_TYPE_SYSTEM)) {
640 SLOG(LOG_ERROR, TAG_VCD, "Fail to set type");
642 if (0 != vc_cmd_set_pid(nlu_cmd, vcd_client_manager_get_pid())) {
643 SLOG(LOG_ERROR, TAG_VCD, "Fail to set pid");
645 if (0 != vc_cmd_set_format(nlu_cmd, VC_CMD_FORMAT_ACTION)) {
646 SLOG(LOG_ERROR, TAG_VCD, "Fail to set format");
648 if (0 != vc_cmd_list_add(vc_cmd_list, nlu_cmd)) {
649 SLOG(LOG_ERROR, TAG_VCD, "Fail to add nlu cmd to list");
650 vc_cmd_destroy(nlu_cmd);
656 vc_cmd_print_list(vc_cmd_list);
658 SLOG(LOG_DEBUG, TAG_VCD, "[Server] @@@@");
660 int result_count = 0;
661 vc_cmd_list_get_count(vc_cmd_list, &result_count);
663 if (0 == result_count) {
665 if (NULL != all_result) {
666 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Engine result is no command : %s", all_result);
668 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Engine result is NULL");
670 bool temp = vcd_client_manager_get_exclusive();
671 vc_info_parser_set_result(all_result, event, msg, NULL, temp);
673 int pid = vcd_client_widget_get_foreground_pid();
675 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Request tooltip hide");
676 /* Send to hide tooltip */
677 vcdc_send_show_tooltip(pid, false);
680 if (-1 != vcd_client_manager_get_pid()) {
681 /* Manager client is available */
682 if (0 != vcdc_send_result_to_manager(vcd_client_manager_get_pid(), VC_RESULT_TYPE_NORMAL)) {
683 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Fail to send result");
687 vcd_client_manager_set_exclusive(false);
689 return VCD_ERROR_NONE;
691 if (false == vcd_client_manager_get_exclusive()) {
692 int pid = vcd_client_widget_get_foreground_pid();
694 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Request tooltip hide");
695 vcdc_send_show_tooltip(pid, false);
698 vc_info_parser_set_result(all_result, event, msg, vc_cmd_list, false);
700 if (-1 != vcd_client_manager_get_pid()) {
701 /* Manager client is available */
702 if (0 != vcdc_send_result_to_manager(vcd_client_manager_get_pid(), VC_RESULT_TYPE_NORMAL)) {
703 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Fail to send result");
706 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Manager is NOT available. Send result to client directly");
707 /* Send result to client */
708 ecore_timer_add(0, __vcd_send_selected_result, NULL);
711 /* exclusive command */
712 vc_info_parser_set_result(all_result, event, msg, vc_cmd_list, true);
714 if (-1 != vcd_client_manager_get_pid()) {
715 /* Manager client is available */
716 if (0 != vcdc_send_result_to_manager(vcd_client_manager_get_pid(), VC_RESULT_TYPE_NORMAL)) {
717 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Fail to send result");
720 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Manager is NOT available");
723 vcd_client_manager_set_exclusive(false);
727 vc_cmd_list_destroy(vc_cmd_list, true);
729 return VCD_ERROR_NONE;
733 if (NULL == result_id) {
735 if (NULL != all_result) {
736 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Engine result is no command : %s", all_result);
737 bool temp = vcd_client_manager_get_exclusive();
738 vc_info_parser_set_result(all_result, event, msg, NULL, temp);
741 int pid = vcd_client_widget_get_foreground_pid();
743 if (NULL != all_result) {
744 /* Send result text to widget */
745 vcdc_send_result(pid, vcd_client_manager_get_pid(), VC_COMMAND_TYPE_WIDGET);
748 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Request tooltip hide");
749 /* Send to hide tooltip */
750 vcdc_send_show_tooltip(pid, false);
753 if (-1 != vcd_client_manager_get_pid()) {
754 /* Manager client is available */
755 if (0 != vcdc_send_result_to_manager(vcd_client_manager_get_pid(), VC_RESULT_TYPE_NORMAL)) {
756 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Fail to send result");
760 vcd_client_manager_set_exclusive(false);
766 SLOG(LOG_DEBUG, TAG_VCD, "[Server] @ Get engine result @");
769 vc_cmd_s* temp_cmd = NULL;
770 vc_cmd_list_h vc_cmd_list = NULL;
772 if (0 != vc_cmd_list_create(&vc_cmd_list)) {
773 SLOG(LOG_ERROR, TAG_VCD, "[Server] Fail to create command list");
774 vcd_client_manager_set_exclusive(false);
775 vcd_config_set_service_state(VCD_STATE_READY);
776 vcdc_send_service_state(VCD_STATE_READY);
781 for (i = 0; i < count; i++) {
782 SLOG(LOG_DEBUG, TAG_VCD, "[Server] [%d] Result ID(%d)", i, result_id[i]);
784 if (result_id[i] < 0) {
785 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Result ID(%d) is NOT valid", result_id[i]);
789 ret = vcd_client_get_cmd_from_result_id(result_id[i], &temp_cmd);
790 if (0 == ret && NULL != temp_cmd) {
791 switch (temp_cmd->format) {
792 case VC_CMD_FORMAT_FIXED:
793 case VC_CMD_FORMAT_FIXED_AND_VFIXED:
794 case VC_CMD_FORMAT_VFIXED_AND_FIXED:
795 case VC_CMD_FORMAT_PARTIAL:
796 /* Nonfixed result is NOT valid */
798 case VC_CMD_FORMAT_FIXED_AND_NONFIXED:
799 if (NULL == temp_cmd->parameter) {
800 if (NULL != non_fixed_result) {
801 temp_cmd->parameter = strdup(non_fixed_result);
804 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Command type is NOT vaild. Parameter (%s)", temp_cmd->parameter);
807 case VC_CMD_FORMAT_NONFIXED_AND_FIXED:
808 if (NULL == temp_cmd->command) {
809 if (NULL != non_fixed_result) {
810 temp_cmd->command = strdup(non_fixed_result);
813 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Command type is NOT vaild. Command (%s)", temp_cmd->command);
818 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Unknown command type : %d", temp_cmd->type);
822 if (0 != vc_cmd_list_add(vc_cmd_list, (vc_cmd_h)temp_cmd)) {
823 SLOG(LOG_DEBUG, TAG_VCD, "Fail to add command to list");
824 vc_cmd_destroy((vc_cmd_h)temp_cmd);
827 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] NOT found matached result(%d)", result_id[i]);
831 vc_cmd_print_list(vc_cmd_list);
833 SLOG(LOG_DEBUG, TAG_VCD, "[Server] @@@@");
835 int result_count = 0;
836 vc_cmd_list_get_count(vc_cmd_list, &result_count);
838 if (false == vcd_client_manager_get_exclusive()) {
839 int pid = vcd_client_widget_get_foreground_pid();
841 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Request tooltip hide");
842 vcdc_send_show_tooltip(pid, false);
845 vc_info_parser_set_result(all_result, event, msg, vc_cmd_list, false);
847 if (-1 != vcd_client_manager_get_pid()) {
848 /* Manager client is available */
849 if (0 != vcdc_send_result_to_manager(vcd_client_manager_get_pid(), VC_RESULT_TYPE_NORMAL)) {
850 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Fail to send result");
853 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Manager is NOT available. Send result to client directly");
854 /* Send result to client */
855 ecore_timer_add(0, __vcd_send_selected_result, NULL);
858 /* exclusive command */
859 vc_info_parser_set_result(all_result, event, msg, vc_cmd_list, true);
861 if (-1 != vcd_client_manager_get_pid()) {
862 /* Manager client is available */
863 if (0 != vcdc_send_result_to_manager(vcd_client_manager_get_pid(), VC_RESULT_TYPE_NORMAL)) {
864 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Fail to send result");
867 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Manager is NOT available");
870 vcd_client_manager_set_exclusive(false);
873 vc_cmd_list_destroy(vc_cmd_list, true);
880 static void __vcd_server_nlu_result_cb(vce_result_event_e event, const char* nlu_result, void *user_data)
882 SLOG(LOG_DEBUG, TAG_VCD, "[Server] NLU result cb - event(%d)", event);
883 SLOG(LOG_DEBUG, TAG_VCD, "[Server] result (%s)", nlu_result);
885 int ret = vc_info_parser_set_nlu_result(nlu_result);
887 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to set nlu result (%d)", ret);
894 int vcd_send_error(vce_error_e error, const char* msg, void *user_data)
896 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Engine Error cb - reason(%d), msg(%s)", error, msg);
897 ecore_main_loop_thread_safe_call_async(__cancel_by_error, NULL);
899 char* error_msg = NULL;
901 error_msg = strdup(msg);
904 if (0 != vcdc_send_error_signal_to_manager(vcd_client_manager_get_pid(), error, error_msg)) {
905 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to send error signal");
908 if (NULL != error_msg) {
913 return VCD_ERROR_NONE;
917 * vcd server Interfaces
919 static void __vcd_file_clean_up()
921 SLOG(LOG_DEBUG, TAG_VCD, "== Old file clean up == ");
924 struct dirent *dirp = NULL;
926 dp = opendir(VC_RUNTIME_INFO_ROOT);
928 SLOG(LOG_ERROR, TAG_VCD, "[File message WARN] Fail to open path : %s", VC_RUNTIME_INFO_ROOT);
932 char remove_path[256] = {0, };
937 if (!strncmp("vc_", dirp->d_name, strlen("vc_"))) {
938 memset(remove_path, 0, 256);
939 snprintf(remove_path, 256, "%s/%s", VC_RUNTIME_INFO_ROOT, dirp->d_name);
942 if (0 != remove(remove_path)) {
943 SLOG(LOG_WARN, TAG_VCD, "[File message WARN] Fail to remove file : %s", remove_path);
945 SLOG(LOG_DEBUG, TAG_VCD, "[File message] Remove file : %s", remove_path);
949 } while (NULL != dirp);
956 static int __vcd_db_clean_up()
959 int cnt = VC_COMMAND_TYPE_FOREGROUND;
961 if (VC_COMMAND_TYPE_BACKGROUND != cnt)
962 ret = vc_db_delete_commands(-1, cnt, NULL);
963 } while (VC_COMMAND_TYPE_EXCLUSIVE >= ++cnt);
968 int vcd_initialize(vce_request_callback_s *callback)
972 /* Remove old file */
973 __vcd_file_clean_up();
975 /* initialize modules */
976 ret = vcd_config_initialize(__config_lang_changed_cb, __config_foreground_changed_cb, NULL);
978 SLOG(LOG_ERROR, TAG_VCD, "[Server WARNING] Fail to initialize config.");
982 ret = __vcd_db_clean_up();
984 SLOG(LOG_ERROR, TAG_VCD, "[Server WARNING] Fail to remove db data");
987 vcd_config_set_service_state(VCD_STATE_NONE);
989 ret = vcd_engine_agent_init();
991 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to engine agent initialize : result(%d)", ret);
995 if (0 != vcd_recorder_create(__server_recorder_callback, __server_recorder_interrupt_callback)) {
996 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to create recorder");
997 return VCD_ERROR_OPERATION_FAILED;
1001 if (0 != vcd_engine_agent_load_current_engine(callback)) {
1002 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to load current engine");
1003 return VCD_ERROR_OPERATION_FAILED;
1006 /* Initialize manager info */
1007 vcd_client_manager_unset();
1009 vcd_config_set_service_state(VCD_STATE_READY);
1010 vcdc_send_service_state(VCD_STATE_READY);
1012 SLOG(LOG_DEBUG, TAG_VCD, "[Server SUCCESS] initialize");
1020 if (0 < g_list_length(g_proc_list)) {
1021 iter = g_list_first(g_proc_list);
1022 while (NULL != iter) {
1023 g_proc_list = g_list_remove_link(g_proc_list, iter);
1024 iter = g_list_first(g_proc_list);
1028 if (g_restart_timer != NULL) {
1029 ecore_timer_del(g_restart_timer);
1030 g_restart_timer = NULL;
1033 vcd_state_e state = vcd_config_get_service_state();
1034 if (VCD_STATE_READY != state) {
1035 if (VCD_STATE_RECORDING == state) {
1036 vcd_recorder_stop();
1038 vcd_engine_recognize_cancel();
1040 if (0 != vcd_recorder_destroy()) {
1041 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to destroy recorder");
1043 SLOG(LOG_DEBUG, TAG_VCD, "[Server] destroy recorder");
1046 if (0 != vcd_engine_agent_release()) {
1047 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to release engine");
1049 SLOG(LOG_DEBUG, TAG_VCD, "[Server] release engine");
1052 vcd_client_manager_unset_appid();
1054 vcd_config_set_service_state(VCD_STATE_NONE);
1055 vcdc_send_service_state(VCD_STATE_NONE);
1057 SLOG(LOG_DEBUG, TAG_VCD, "[Server] mode finalize");
1062 static Eina_Bool __finalize_quit_ecore_loop(void *data)
1064 SLOG(LOG_DEBUG, TAG_VCD, "[Server] quit ecore main loop");
1065 ecore_main_loop_quit();
1069 static void __read_proc()
1072 struct dirent *dirp = NULL;
1076 if (0 < g_list_length(g_proc_list)) {
1077 iter = g_list_first(g_proc_list);
1078 while (NULL != iter) {
1079 g_proc_list = g_list_remove_link(g_proc_list, iter);
1080 iter = g_list_first(g_proc_list);
1084 dp = opendir("/proc");
1086 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Fail to open proc");
1092 tmp = atoi(dirp->d_name);
1093 if (0 >= tmp) continue;
1094 g_proc_list = g_list_append(g_proc_list, GINT_TO_POINTER(tmp));
1096 } while (NULL != dirp);
1102 static void __vcd_cleanup_client(vcd_client_type_e type)
1104 int* client_list = NULL;
1105 int client_count = 0;
1112 if (VCD_CLIENT_TYPE_NORMAL == type) {
1113 ret = vcd_client_get_list(&client_list, &client_count);
1114 } else if (VCD_CLIENT_TYPE_WIDGET == type) {
1115 ret = vcd_client_widget_get_list(&client_list, &client_count);
1116 } else if (VCD_CLIENT_TYPE_MANAGER == type) {
1117 mgr_pid = vcd_client_manager_get_pid();
1118 client_list = &mgr_pid;
1120 if (-1 == mgr_pid) {
1121 SLOG(LOG_WARN, TAG_VCD, "[WARNING] Invalid Manager pid");
1126 if (0 == ret || mgr_pid > 0) {
1127 SLOG(LOG_DEBUG, TAG_VCD, "@@@ Clean up %s client ", type ? (type == 1) ? "Widget" : "Manager" : "Normal");
1128 if (NULL != client_list && client_count > 0) {
1129 for (i = 0; i < client_count; i++) {
1132 for (j = 0; j < g_list_length(g_proc_list); j++) {
1133 iter = g_list_nth(g_proc_list, j);
1135 if (*(client_list + i) == GPOINTER_TO_INT(iter->data)) {
1136 SLOG(LOG_DEBUG, TAG_VCD, "%s pid(%d) is running", type ? (type == 1) ? "Widget" : "Manager" : "Normal", *(client_list + i));
1143 if (false == exist) {
1144 SLOG(LOG_ERROR, TAG_VCD, "%s pid(%d) should be removed", type ? (type == 1) ? "Widget" : "Manager" : "Normal", *(client_list + i));
1145 if (VCD_CLIENT_TYPE_NORMAL == type)
1146 vcd_server_finalize(*(client_list + i));
1147 else if (VCD_CLIENT_TYPE_WIDGET == type)
1148 vcd_server_widget_finalize(*(client_list + i));
1150 vcd_server_mgr_finalize(mgr_pid);
1154 SLOG(LOG_DEBUG, TAG_VCD, "@@@");
1156 if (NULL != client_list && -1 == mgr_pid) {
1163 Eina_Bool vcd_cleanup_client_all(void *data)
1167 __vcd_cleanup_client(VCD_CLIENT_TYPE_NORMAL);
1168 __vcd_cleanup_client(VCD_CLIENT_TYPE_WIDGET);
1169 __vcd_cleanup_client(VCD_CLIENT_TYPE_MANAGER);
1172 if (0 == vcd_client_get_list(&client_list, &client_count)) {
1173 SLOG(LOG_DEBUG, TAG_VCD, "@@@ Clean up client ");
1174 if (NULL != client_list && client_count > 0) {
1175 for (i = 0; i < client_count; i++) {
1178 for (j = 0; j < g_list_length(g_proc_list); j++) {
1179 iter = g_list_nth(g_proc_list, j);
1181 if (client_list[i] == GPOINTER_TO_INT(iter->data)) {
1182 SLOG(LOG_DEBUG, TAG_VCD, "pid(%d) is running", client_list[i]);
1189 if (false == exist) {
1190 SLOG(LOG_ERROR, TAG_VCD, "pid(%d) should be removed", client_list[i]);
1191 vcd_server_finalize(client_list[i]);
1194 result = vcdc_send_hello(client_list[i], VCD_CLIENT_TYPE_NORMAL);
1197 SLOG(LOG_DEBUG, TAG_VCD, "[Server] pid(%d) should be removed.", client_list[i]);
1198 vcd_server_finalize(client_list[i]);
1199 } else if (-1 == result) {
1200 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Hello result has error");
1205 SLOG(LOG_DEBUG, TAG_VCD, "@@@");
1207 if (NULL != client_list) {
1212 /* If app is in background state, app cannot response message. */
1213 if (0 == vcd_client_widget_get_list(&client_list, &client_count)) {
1214 SLOG(LOG_DEBUG, TAG_VCD, "@@@ Clean up widget");
1215 if (NULL != client_list && client_count > 0) {
1216 for (i = 0; i < client_count; i++) {
1219 for (j = 0; j < g_list_length(g_proc_list); j++) {
1220 iter = g_list_nth(g_proc_list, j);
1222 if (client_list[i] == GPOINTER_TO_INT(iter->data)) {
1223 SLOG(LOG_DEBUG, TAG_VCD, "widget pid(%d) is running", client_list[i]);
1230 if (false == exist) {
1231 SLOG(LOG_ERROR, TAG_VCD, "widget pid(%d) should be removed", client_list[i]);
1232 vcd_server_widget_finalize(client_list[i]);
1235 result = vcdc_send_hello(client_list[i], VCD_CLIENT_TYPE_WIDGET);
1238 SLOG(LOG_DEBUG, TAG_VCD, "[Server] widget pid(%d) should be removed.", client_list[i]);
1239 vcd_server_widget_finalize(client_list[i]);
1240 } else if (-1 == result) {
1241 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Hello result has error");
1246 SLOG(LOG_DEBUG, TAG_VCD, "@@@");
1249 if (NULL != client_list) {
1257 int mgr_pid = vcd_client_manager_get_pid();
1259 for (j = 0; j < g_list_length(g_proc_list); j++) {
1260 iter = g_list_nth(g_proc_list, j);
1262 if (mgr_pid == GPOINTER_TO_INT(iter->data)) {
1263 SLOG(LOG_DEBUG, TAG_VCD, "manager pid(%d) is running", mgr_pid);
1270 if (false == exist) {
1271 SLOG(LOG_ERROR, TAG_VCD, "manager pid (%d) should be removed", mgr_pid);
1272 vcd_server_mgr_finalize(mgr_pid);
1279 int vcd_server_get_service_state()
1281 return vcd_config_get_service_state();
1284 int vcd_server_get_foreground()
1287 vcd_config_get_foreground(&pid);
1295 int vcd_server_mgr_initialize(int pid)
1297 /* check if pid is valid */
1298 if (false == vcd_client_manager_is_valid(pid)) {
1299 SLOG(LOG_ERROR, TAG_VCD, "[Server] old manager pid(%d) be removed", vcd_client_manager_get_pid());
1300 vcd_server_mgr_cancel();
1301 vcd_client_manager_unset();
1304 /* Add client information to client manager */
1305 if (0 != vcd_client_manager_set(pid)) {
1306 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to add manager");
1307 return VCD_ERROR_OPERATION_FAILED;
1310 if (0 != vcdc_send_manager_pid(pid))
1311 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Fail to send manager pid");
1313 SLOG(LOG_DEBUG, TAG_VCD, "[Server Success] Manager initialize : pid(%d)", pid);
1315 return VCD_ERROR_NONE;
1318 int vcd_server_mgr_finalize(int pid)
1320 /* check if pid is valid */
1321 if (false == vcd_client_manager_is_valid(pid)) {
1322 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] The manager pid(%d) is NOT valid", pid);
1323 return VCD_ERROR_INVALID_PARAMETER;
1326 /* Cancel recognition */
1327 vcd_server_mgr_cancel();
1329 if (0 != vcdc_send_manager_pid(-1))
1330 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Fail to send manager pid");
1332 /* Remove manager information */
1333 if (0 != vcd_client_manager_unset()) {
1334 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Fail to delete client");
1337 if (0 == vcd_client_get_ref_count()) {
1338 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Connected client list is empty");
1339 ecore_timer_add(0, __finalize_quit_ecore_loop, NULL);
1342 SLOG(LOG_DEBUG, TAG_VCD, "[Server Success] Manager Finalize : pid(%d)", pid);
1344 return VCD_ERROR_NONE;
1347 int vcd_server_mgr_set_command(int pid)
1349 if (0 != vcd_client_manager_set_command(pid)) {
1350 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] The manager pid(%d) is NOT valid", pid);
1351 return VCD_ERROR_INVALID_PARAMETER;
1353 return VCD_ERROR_NONE;
1356 int vcd_server_mgr_unset_command(int pid)
1358 if (0 != vcd_client_manager_unset_command(pid)) {
1359 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] The manager pid(%d) is NOT valid", pid);
1360 return VCD_ERROR_INVALID_PARAMETER;
1362 return VCD_ERROR_NONE;
1365 int vcd_server_mgr_set_demandable_client(int pid)
1367 /* check if pid is valid */
1368 if (false == vcd_client_manager_is_valid(pid)) {
1369 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] The manager pid(%d) is NOT valid", pid);
1370 return VCD_ERROR_INVALID_PARAMETER;
1373 GSList* client_list = NULL;
1374 if (0 != vc_info_parser_get_demandable_clients(&client_list)) {
1375 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to get demandable client");
1376 return VCD_ERROR_OPERATION_FAILED;
1379 /* Save client list */
1380 if (0 != vcd_client_manager_set_demandable_client(pid, client_list)) {
1381 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to set demandable client list");
1382 return VCD_ERROR_OPERATION_FAILED;
1385 return VCD_ERROR_NONE;
1388 int vcd_server_mgr_set_audio_type(int pid, const char* audio_type)
1390 /* check if pid is valid */
1391 if (false == vcd_client_manager_is_valid(pid)) {
1392 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] The manager pid(%d) is NOT valid", pid);
1393 return VCD_ERROR_INVALID_PARAMETER;
1397 vce_audio_type_e type = VCE_AUDIO_TYPE_PCM_S16_LE;
1401 ret = vcd_engine_get_audio_format(audio_type, &type, &rate, &channel);
1403 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to get audio format : %d", ret);
1404 return VCD_ERROR_OPERATION_FAILED;
1407 ret = vcd_recorder_set(audio_type, type, rate, channel);
1409 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to set audio in type : %d", ret);
1410 return VCD_ERROR_OPERATION_FAILED;
1413 return VCD_ERROR_NONE;
1416 int vcd_server_mgr_get_audio_type(int pid, char** audio_type)
1418 /* check if pid is valid */
1419 if (false == vcd_client_manager_is_valid(pid)) {
1420 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] The manager pid(%d) is NOT valid", pid);
1421 return VCD_ERROR_INVALID_PARAMETER;
1424 int ret = vcd_recorder_get(audio_type);
1426 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to get audio in type : %d", ret);
1427 return VCD_ERROR_OPERATION_FAILED;
1430 return VCD_ERROR_NONE;
1433 int vcd_server_mgr_set_client_info(int pid)
1435 /* check if pid is valid */
1436 if (false == vcd_client_manager_is_valid(pid)) {
1437 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] The manager pid(%d) is NOT valid", pid);
1438 return VCD_ERROR_INVALID_PARAMETER;
1441 int ret = vcd_client_save_client_info();
1443 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to save client info : %d", ret);
1444 return VCD_ERROR_OPERATION_FAILED;
1447 return VCD_ERROR_NONE;
1450 static int __start_internal_recognition()
1454 if (0 != vcd_client_command_collect_command()) {
1455 SLOG(LOG_ERROR, TAG_VCD, "[Client Data ERROR] Fail to collect command");
1456 /* Send error cb to manager */
1457 int pid = vcd_client_widget_get_foreground_pid();
1459 vcdc_send_error_signal_to_manager(vcd_client_manager_get_pid(), VCD_ERROR_OPERATION_FAILED, "voice_engine.error.proc_fail");
1460 return VCD_ERROR_OPERATION_FAILED;
1463 /* 3. Set command to engine */
1464 ret = vcd_engine_set_commands();
1466 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to set commands : %d", ret);
1467 /* Send error cb to manager */
1468 int pid = vcd_client_widget_get_foreground_pid();
1470 vcdc_send_error_signal_to_manager(vcd_client_manager_get_pid(), VCD_ERROR_OPERATION_FAILED, "voice_engine.error.proc_fail");
1471 return VCD_ERROR_OPERATION_FAILED;
1474 SLOG(LOG_DEBUG, TAG_VCD, "[Server Success] Set command");
1476 bool stop_by_silence = true;
1477 if (VCD_RECOGNITION_MODE_MANUAL == vcd_client_get_recognition_mode()) {
1478 stop_by_silence = false;
1481 vcd_client_manager_set_result_text(NULL);
1483 /* 4. start recognition */
1484 ret = vcd_engine_recognize_start(stop_by_silence);
1486 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to start recognition : result(%d)", ret);
1487 /* Send error cb to manager */
1488 int pid = vcd_client_widget_get_foreground_pid();
1490 vcdc_send_error_signal_to_manager(vcd_client_manager_get_pid(), VCD_ERROR_OPERATION_FAILED, "voice_engine.error.proc_fail");
1491 return VCD_ERROR_OPERATION_FAILED;
1494 SLOG(LOG_DEBUG, TAG_VCD, "[Server Success] Start engine");
1497 /* 5. recorder start */
1498 ret = vcd_recorder_start();
1500 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to start recorder : result(%d)", ret);
1501 vcd_engine_recognize_cancel();
1502 /* Send error cb to manager */
1503 int pid = vcd_client_widget_get_foreground_pid();
1505 vcdc_send_error_signal_to_manager(vcd_client_manager_get_pid(), VCD_ERROR_OPERATION_FAILED, "voice_engine.error.proc_fail");
1510 vcd_config_set_service_state(VCD_STATE_RECORDING);
1511 vcdc_send_service_state(VCD_STATE_RECORDING);
1513 SLOG(LOG_DEBUG, TAG_VCD, "[Server Success] Start recognition(%d)", stop_by_silence);
1518 static Eina_Bool __vcd_request_show_tooltip(void *data)
1520 int pid = vcd_client_widget_get_foreground_pid();
1522 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Request tooltip show and widget command");
1523 vcdc_send_show_tooltip(pid, (bool)data);
1529 int vcd_server_mgr_start(vcd_recognition_mode_e recognition_mode, bool exclusive_cmd, bool start_by_client)
1531 /* 1. check current state */
1532 vcd_state_e state = vcd_config_get_service_state();
1534 if (VCD_STATE_READY != state) {
1535 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Current state is not ready");
1536 return VCD_ERROR_INVALID_STATE;
1538 if (-1 == vcd_client_manager_get_pid()) {
1539 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Manager is NOT available.");
1540 return VCD_ERROR_OPERATION_FAILED;
1543 SLOG(LOG_DEBUG, TAG_VCD, "[Server] set recognition mode = %d", recognition_mode);
1544 vcd_client_set_recognition_mode(recognition_mode);
1546 if (false == exclusive_cmd) {
1547 vcd_client_update_foreground_pid();
1548 /* Notify show tooltip */
1549 if (1 == vcd_config_get_command_type_enabled(VC_COMMAND_TYPE_WIDGET)) {
1550 int pid = vcd_client_widget_get_foreground_pid();
1552 SLOG(LOG_INFO, TAG_VCD, "[Server] Request tooltip show and widget command");
1553 ecore_timer_add(0, __vcd_request_show_tooltip, (void*)true);
1558 vcd_client_manager_set_exclusive(exclusive_cmd);
1562 if (true == start_by_client) {
1563 /* Get foreground pid */
1564 if (0 != vcd_config_get_foreground(&fg_pid)) {
1565 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to get foreground");
1568 /* Set client exclusive option */
1569 if (0 != vcd_client_set_exclusive_command(fg_pid)) {
1570 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to set exclusive command");
1574 int ret = __start_internal_recognition();
1576 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to start recongition : %d", ret);
1580 if (true == start_by_client) {
1581 vcd_client_unset_exclusive_command(fg_pid);
1584 return VCD_ERROR_NONE;
1587 int vcd_server_mgr_stop()
1589 /* 1. Check current state is recording */
1590 if (VCD_STATE_RECORDING != vcd_config_get_service_state()) {
1591 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Current state is not recording");
1592 return VCD_ERROR_INVALID_STATE;
1594 if (-1 == vcd_client_manager_get_pid()) {
1595 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Manager is NOT available.");
1596 return VCD_ERROR_OPERATION_FAILED;
1600 /* 2. Stop recorder */
1601 vcd_recorder_stop();
1604 /* 3. Stop engine recognition */
1605 int ret = vcd_engine_recognize_stop();
1607 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to stop recognition : %d", ret);
1610 /* 4. Set original mode */
1611 vcd_config_set_service_state(VCD_STATE_PROCESSING);
1612 vcdc_send_service_state(VCD_STATE_PROCESSING);
1614 return VCD_ERROR_NONE;
1617 int vcd_server_mgr_cancel()
1619 if (-1 == vcd_client_manager_get_pid()) {
1620 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Manager is NOT available.");
1621 return VCD_ERROR_OPERATION_FAILED;
1624 if (g_restart_timer != NULL) {
1625 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Delete restart engine timer");
1626 ecore_timer_del(g_restart_timer);
1627 g_restart_timer = NULL;
1630 /* 1. Check current state */
1631 vcd_state_e state = vcd_config_get_service_state();
1632 if (VCD_STATE_READY == state) {
1633 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Current state is READY");
1634 vcd_recorder_stop();
1636 if (false == vcd_client_manager_get_exclusive()) {
1637 if (1 == vcd_config_get_command_type_enabled(VC_COMMAND_TYPE_WIDGET)) {
1638 int pid = vcd_client_widget_get_foreground_pid();
1640 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Request tooltip hide");
1641 ecore_timer_add(0, __vcd_request_show_tooltip, (void*)false);
1646 vcdc_send_service_state(VCD_STATE_READY);
1647 return VCD_ERROR_NONE;
1651 /* 2. Stop recorder */
1652 vcd_recorder_stop();
1655 /* 3. Cancel engine */
1656 int ret = vcd_engine_recognize_cancel();
1658 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Fail to cancel : %d", ret);
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);
1670 vcd_client_manager_set_exclusive(false);
1674 vcd_config_set_service_state(VCD_STATE_READY);
1675 vcdc_send_service_state(VCD_STATE_READY);
1677 return VCD_ERROR_NONE;
1681 int vcd_server_mgr_result_select()
1683 __vcd_send_selected_result(NULL);
1685 return VCD_ERROR_NONE;
1688 int vcd_server_mgr_set_domain(int pid, const char* domain)
1690 /* check if pid is valid */
1691 if (false == vcd_client_manager_is_valid(pid)) {
1692 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] The manager pid(%d) is NOT valid", pid);
1693 return VCD_ERROR_INVALID_PARAMETER;
1696 vcd_state_e state = vcd_config_get_service_state();
1697 if (VCD_STATE_READY != state) {
1698 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Current state is not ready");
1699 return VCD_ERROR_INVALID_STATE;
1702 /* Set domain to engine */
1703 int ret = vcd_engine_set_domain(pid, domain);
1705 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to set domain : %d", ret);
1707 SLOG(LOG_DEBUG, TAG_VCD, "[Server SUCCESS] Set domain");
1713 int vcd_server_mgr_set_private_data(int pid, const char* key, const char* data)
1715 /* check if pid is valid */
1716 if (false == vcd_client_manager_is_valid(pid)) {
1717 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] The manager pid(%d) is NOT valid", pid);
1718 return VCD_ERROR_INVALID_PARAMETER;
1721 vcd_state_e state = vcd_config_get_service_state();
1722 if (VCD_STATE_READY != state) {
1723 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Current state is not ready");
1724 return VCD_ERROR_INVALID_STATE;
1727 /* Set private data to engine */
1728 int ret = vcd_engine_set_private_data(pid, key, data);
1730 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to set private data : %d", ret);
1732 SLOG(LOG_DEBUG, TAG_VCD, "[Server SUCCESS] Set private data");
1738 int vcd_server_mgr_get_private_data(int pid, const char* key, char** data)
1740 /* check if pid is valid */
1741 if (false == vcd_client_manager_is_valid(pid)) {
1742 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] The manager pid(%d) is NOT valid", pid);
1743 return VCD_ERROR_INVALID_PARAMETER;
1745 vcd_state_e state = vcd_config_get_service_state();
1746 if (VCD_STATE_READY != state) {
1747 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Current state is not ready");
1748 return VCD_ERROR_INVALID_STATE;
1751 /* Get private data to engine */
1752 int ret = vcd_engine_get_private_data(pid, key, data);
1754 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to get private data : %d", ret);
1756 SLOG(LOG_DEBUG, TAG_VCD, "[Server SUCCESS] Set private data");
1762 int vcd_server_mgr_do_action(int pid, int type, const char* action)
1766 /* check if pid is valid */
1767 if (false == vcd_client_manager_is_valid(pid)) {
1768 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] The manager pid(%d) is NOT valid", pid);
1769 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 /* Reqeust do action to engine */
1779 if (VCD_SEND_EVENT_TYPE_TEXT == type)
1780 ret = vcd_engine_process_text(pid, action);
1781 else if (VCD_SEND_EVENT_TYPE_LIST_EVENT == type)
1782 ret = vcd_engine_process_list_event(pid, action);
1783 else if (VCD_SEND_EVENT_TYPE_HAPTIC_EVENT == type)
1784 ret = vcd_engine_process_haptic_event(pid, action);
1787 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to process do action : %d", ret);
1789 vcd_config_set_service_state(VCD_STATE_PROCESSING);
1790 vcdc_send_service_state(VCD_STATE_PROCESSING);
1791 SLOG(LOG_DEBUG, TAG_VCD, "[Server SUCCESS] Process do action");
1797 int vcd_server_mgr_enable_command_type(int pid, int cmd_type)
1801 /* check if pid is valid */
1802 if (false == vcd_client_manager_is_valid(pid)) {
1803 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] The manager pid(%d) is NOT valid", pid);
1804 return VCD_ERROR_INVALID_PARAMETER;
1807 vcd_state_e state = vcd_config_get_service_state();
1808 if (VCD_STATE_READY != state) {
1809 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Current state is not ready");
1810 return VCD_ERROR_INVALID_STATE;
1813 ret = vcd_config_enable_command_type(cmd_type);
1815 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to enable command type");
1817 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Enable command type(%d)", cmd_type);
1823 int vcd_server_mgr_disable_command_type(int pid, int cmd_type)
1827 /* check if pid is valid */
1828 if (false == vcd_client_manager_is_valid(pid)) {
1829 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] The manager pid(%d) is NOT valid", pid);
1830 return VCD_ERROR_INVALID_PARAMETER;
1833 vcd_state_e state = vcd_config_get_service_state();
1834 if (VCD_STATE_READY != state) {
1835 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Current state is not ready");
1836 return VCD_ERROR_INVALID_STATE;
1839 ret = vcd_config_disable_command_type(cmd_type);
1841 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to disable command type");
1843 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Disable command type(%d)", cmd_type);
1850 * VC Server Functions for Client
1852 int vcd_server_initialize(int pid)
1854 if (false == vcd_engine_is_available_engine()) {
1855 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] No Engine");
1856 return VCD_ERROR_ENGINE_NOT_FOUND;
1859 /* check if pid is valid */
1860 if (true == vcd_client_is_available(pid)) {
1861 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] The pid is already exist");
1862 return VCD_ERROR_INVALID_PARAMETER;
1865 /* Add client information to client manager */
1866 if (0 != vcd_client_add(pid)) {
1867 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to add client info");
1868 return VCD_ERROR_OPERATION_FAILED;
1871 SLOG(LOG_DEBUG, TAG_VCD, "[Server Success] Client Initialize : pid(%d)", pid);
1873 return VCD_ERROR_NONE;
1876 int vcd_server_finalize(int pid)
1878 /* check if pid is valid */
1879 if (false == vcd_client_is_available(pid)) {
1880 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] pid is NOT valid ");
1881 return VCD_ERROR_INVALID_PARAMETER;
1884 /* Remove client information */
1885 if (0 != vcd_client_delete(pid)) {
1886 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Fail to delete client");
1889 if (0 == vcd_client_get_ref_count()) {
1890 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Connected client list is empty");
1891 ecore_timer_add(0, __finalize_quit_ecore_loop, NULL);
1894 SLOG(LOG_DEBUG, TAG_VCD, "[Server Success] Client Finalize : pid(%d)", pid);
1896 return VCD_ERROR_NONE;
1899 int vcd_server_set_command(int pid, vc_cmd_type_e cmd_type)
1901 /* check if pid is valid */
1902 if (false == vcd_client_is_available(pid)) {
1903 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] pid is NOT valid ");
1904 return VCD_ERROR_INVALID_PARAMETER;
1907 if (0 != vcd_client_set_command_type(pid, cmd_type)) {
1908 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to set command type : pid(%d), cmd_type(%d)", pid, cmd_type);
1909 return VCD_ERROR_OPERATION_FAILED;
1915 int vcd_server_unset_command(int pid, vc_cmd_type_e cmd_type)
1917 /* check if pid is valid */
1918 if (false == vcd_client_is_available(pid)) {
1919 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] pid is NOT valid ");
1920 return VCD_ERROR_INVALID_PARAMETER;
1923 if (0 != vcd_client_unset_command_type(pid, cmd_type)) {
1924 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to unset command type : pid(%d), cmd_type(%d)", pid, cmd_type);
1925 return VCD_ERROR_OPERATION_FAILED;
1931 int vcd_server_set_foreground(int pid, bool value)
1933 /* check if pid is valid */
1934 if (false == vcd_client_is_available(pid) && false == vcd_client_widget_is_available(pid)) {
1935 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] pid is NOT valid ");
1936 return VCD_ERROR_INVALID_PARAMETER;
1939 if (0 != vcd_config_set_foreground(pid, value)) {
1940 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to set foreground : pid(%d), value(%d)", pid, value);
1941 return VCD_ERROR_OPERATION_FAILED;
1947 static int __vcd_server_launch_manager_app()
1950 bool running = false;
1953 ret = vcd_client_manager_get_appid(&appid);
1954 if (0 != ret || NULL == appid) {
1955 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Fail to get manager appid");
1956 return VCD_ERROR_OPERATION_FAILED;
1958 ret = app_manager_is_running(appid, &running);
1960 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Fail to check running with appid(%s)", appid);
1963 return VCD_ERROR_OPERATION_FAILED;
1965 if (false == running) {
1966 int tmp_ret = __vcd_is_package_installed(appid);
1967 if (false == tmp_ret) {
1968 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] manager app is not installed, appid(%s)", appid);
1970 ret = __vcd_activate_app_by_appcontrol(appid);
1972 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Fail to activate app");
1977 SLOG(LOG_ERROR, TAG_VCD, "Launch vc manager app: appid(%s)", appid);
1980 ret = __vcd_resume_app(appid);
1982 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Fail to resume app");
1987 SLOG(LOG_ERROR, TAG_VCD, "Resume vc manager app: appid(%s)", appid);
1993 return VCD_ERROR_NONE;
1996 int vcd_server_dialog(int pid, const char* disp_text, const char* utt_text, int continuous)
1998 /* check if pid is valid */
1999 if (false == vcd_client_is_available(pid) && false == vcd_client_widget_is_available(pid)) {
2000 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] pid is NOT valid ");
2001 return VCD_ERROR_INVALID_PARAMETER;
2004 int ret = __vcd_server_launch_manager_app();
2006 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);
2010 ret = vcdc_send_dialog(vcd_client_manager_get_pid(), pid, disp_text, utt_text, continuous);
2012 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);
2019 int vcd_server_is_system_command_valid(int pid, int* is_sys_cmd_valid)
2021 /* check if pid is valid */
2022 if (false == vcd_client_is_available(pid) && false == vcd_client_widget_is_available(pid)) {
2023 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] pid is NOT valid ");
2024 return VCD_ERROR_INVALID_PARAMETER;
2027 /* check if system command is valid */
2028 if (true == vcd_client_manager_is_system_command_valid(vcd_client_manager_get_pid()))
2029 *is_sys_cmd_valid = true;
2031 *is_sys_cmd_valid = false;
2037 int vcd_server_set_exclusive_command(int pid, bool value)
2039 /* check if pid is valid */
2040 if (false == vcd_client_is_available(pid)) {
2041 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] pid is NOT valid ");
2042 return VCD_ERROR_INVALID_PARAMETER;
2045 if (true == value) {
2046 if (0 != vcd_client_set_exclusive_command(pid)) {
2047 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to set exclusive command : pid(%d)", pid);
2048 return VCD_ERROR_OPERATION_FAILED;
2051 if (0 != vcd_client_unset_exclusive_command(pid)) {
2052 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to unset exclusive command : pid(%d)", pid);
2053 return VCD_ERROR_OPERATION_FAILED;
2060 int vcd_server_request_start(int pid, bool stop_by_silence)
2062 /* check if pid is valid */
2063 if (false == vcd_client_is_available(pid)) {
2064 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] pid(%d) is NOT forground client", pid);
2065 return VCD_ERROR_INVALID_PARAMETER;
2069 /* Check current state */
2070 vcd_state_e state = vcd_config_get_service_state();
2072 /* Service state should be ready */
2073 if (VCD_STATE_READY != state) {
2074 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Current state is not Ready : pid(%d)", pid);
2075 return VCD_ERROR_INVALID_STATE;
2078 if (-1 != vcd_client_manager_get_pid()) {
2079 /* Check current pid is valid */
2080 if (false == vcd_client_manager_check_demandable_client(pid)) {
2081 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Current client is NOT available : pid(%d)", pid);
2082 return VCD_ERROR_INVALID_PARAMETER;
2086 ret = vcd_server_mgr_start(stop_by_silence, false);
2088 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Fail to start recognition");
2089 return VCD_ERROR_INVALID_PARAMETER;
2095 int vcd_server_request_stop(int pid)
2098 /* Check current state */
2099 vcd_state_e state = vcd_config_get_service_state();
2101 /* Service state should be ready */
2102 if (VCD_STATE_RECORDING != state) {
2103 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Current state is not Recording : pid(%d)", pid);
2104 return VCD_ERROR_INVALID_STATE;
2107 if (-1 != vcd_client_manager_get_pid()) {
2108 /* Check current pid is valid */
2109 if (false == vcd_client_manager_check_demandable_client(pid)) {
2110 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Current client is NOT available : pid(%d)", pid);
2111 return VCD_ERROR_INVALID_PARAMETER;
2115 ret = vcd_server_mgr_stop();
2117 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Fail to start recognition");
2118 return VCD_ERROR_OPERATION_FAILED;
2121 return VCD_ERROR_NONE;
2124 int vcd_server_request_cancel(int pid)
2127 /* Check current state */
2128 vcd_state_e state = vcd_config_get_service_state();
2130 /* Service state should be recording or processing */
2131 if (VCD_STATE_RECORDING != state && VCD_STATE_PROCESSING != state) {
2132 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Current state is not Recording or Processing : pid(%d)", pid);
2133 return VCD_ERROR_INVALID_STATE;
2136 if (-1 != vcd_client_manager_get_pid()) {
2137 /* Check current pid is valid */
2138 if (false == vcd_client_manager_check_demandable_client(pid)) {
2139 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Current client is NOT available : pid(%d)", pid);
2140 return VCD_ERROR_INVALID_PARAMETER;
2144 ret = vcd_server_mgr_cancel();
2146 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Fail to start recognition");
2147 return VCD_ERROR_OPERATION_FAILED;
2150 return VCD_ERROR_NONE;
2155 * VC Server Functions for Widget lib
2157 int vcd_server_widget_initialize(int pid)
2159 if (false == vcd_engine_is_available_engine()) {
2160 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] No Engine");
2161 return VCD_ERROR_ENGINE_NOT_FOUND;
2164 /* check if pid is valid */
2165 if (true == vcd_client_widget_is_available(pid)) {
2166 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] The pid is already exist");
2167 return VCD_ERROR_NONE;
2170 /* Add client information to client manager */
2171 if (0 != vcd_client_widget_add(pid)) {
2172 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to add client info");
2173 return VCD_ERROR_OPERATION_FAILED;
2176 SLOG(LOG_DEBUG, TAG_VCD, "[Server Success] Initialize widget : pid(%d)", pid);
2178 return VCD_ERROR_NONE;
2181 int vcd_server_widget_finalize(int pid)
2183 /* check if pid is valid */
2184 if (false == vcd_client_widget_is_available(pid)) {
2185 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] pid is NOT valid ");
2186 return VCD_ERROR_INVALID_PARAMETER;
2189 /* Remove client information */
2190 if (0 != vcd_client_widget_delete(pid)) {
2191 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Fail to delete client");
2194 if (0 == vcd_client_get_ref_count()) {
2195 SLOG(LOG_DEBUG, TAG_VCD, "[Server] connected client list is empty");
2196 ecore_timer_add(0, __finalize_quit_ecore_loop, NULL);
2199 return VCD_ERROR_NONE;
2202 int vcd_server_widget_start_recording(int pid, bool widget_command)
2204 /* check if pid is valid */
2205 if (false == vcd_client_widget_is_available(pid)) {
2206 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] pid is NOT valid ");
2207 return VCD_ERROR_INVALID_PARAMETER;
2210 if (true == widget_command) {
2211 vcd_client_widget_set_command(pid);
2212 SLOG(LOG_DEBUG, TAG_VCD, "[Server] widget command is available");
2214 vcd_client_widget_unset_command(pid);
2215 SLOG(LOG_WARN, TAG_VCD, "[Server] widget command is NOT available");
2218 int ret = __start_internal_recognition();
2220 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to start recongition : %d", ret);
2221 ecore_timer_add(0, __vcd_request_show_tooltip, (void*)false);
2227 int vcd_server_widget_start(int pid, bool stop_by_silence)
2229 /* check if pid is valid */
2230 int fore_pid = vcd_client_widget_get_foreground_pid();
2231 if (pid != fore_pid) {
2232 SLOG(LOG_ERROR, TAG_VCD, "[Server] pid is NOT foreground");
2233 return VCD_ERROR_INVALID_PARAMETER;
2236 /* Check current state */
2237 vcd_state_e state = vcd_config_get_service_state();
2239 /* Service state should be ready */
2240 if (VCD_STATE_READY != state) {
2241 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Current state is not Ready : pid(%d)", pid);
2242 return VCD_ERROR_INVALID_STATE;
2245 vcd_client_set_slience_detection(stop_by_silence);
2247 /* Notify show tooltip */
2248 ecore_timer_add(0, __vcd_request_show_tooltip, (void*)true);
2253 int vcd_server_widget_stop(int pid)
2255 /* check if pid is valid */
2256 int fore_pid = vcd_client_widget_get_foreground_pid();
2257 if (pid != fore_pid) {
2258 SLOG(LOG_ERROR, TAG_VCD, "[Server] pid is NOT foreground");
2259 return VCD_ERROR_INVALID_PARAMETER;
2263 /* Check current state */
2264 vcd_state_e state = vcd_config_get_service_state();
2266 /* Service state should be recording */
2267 if (VCD_STATE_RECORDING != state) {
2268 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Current service state is not Recording : pid(%d)", pid);
2269 return VCD_ERROR_INVALID_STATE;
2272 ret = vcd_server_mgr_stop();
2274 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Fail to start recognition");
2275 return VCD_ERROR_OPERATION_FAILED;
2278 return VCD_ERROR_NONE;
2281 int vcd_server_widget_cancel(int pid)
2283 /* check if pid is valid */
2284 int fore_pid = vcd_client_widget_get_foreground_pid();
2285 if (pid != fore_pid) {
2286 SLOG(LOG_ERROR, TAG_VCD, "[Server] pid is NOT foreground");
2287 return VCD_ERROR_INVALID_PARAMETER;
2291 /* Check current state */
2292 vcd_state_e state = vcd_config_get_service_state();
2294 /* Service state should be recording or processing */
2295 if (VCD_STATE_RECORDING != state && VCD_STATE_PROCESSING != state) {
2296 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Current state is not Recording or Processing : pid(%d)", pid);
2297 return VCD_ERROR_INVALID_STATE;
2300 ret = vcd_server_mgr_cancel();
2302 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Fail to cancel recognition : %d", ret);
2306 return VCD_ERROR_NONE;
2309 int vcd_server_widget_enable_asr_result(int pid, bool enable)
2312 ret = vcd_client_widget_set_asr_result_enabled(pid, enable);
2314 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Fail to enable asr result : %d", ret);
2320 int vcd_server_set_language(const char* language)
2322 int ret = VCD_ERROR_NONE;
2324 ret = vcd_config_set_default_language(language);
2326 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Fail to set language : %d", ret);
2330 ret = vcd_engine_set_current_language(language);
2332 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Fail to set language : %d", ret);
2339 * For engine service
2341 int vcd_get_foreach_command(vce_cmd_h vce_command, vce_command_cb callback, void* user_data)
2343 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Get foreach command");
2345 if (NULL == callback) {
2346 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] input parameter is NULL");
2347 return VCD_ERROR_INVALID_PARAMETER;
2351 ret = vcd_engine_agent_get_foreach_command(vce_command, callback, user_data);
2353 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to get foreach command : ret(%d)", ret);
2359 int vcd_get_command_count(vce_cmd_h vce_command)
2361 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Get command count");
2364 ret = vcd_engine_agent_get_command_count(vce_command);
2366 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to get command count : ret(%d)", ret);
2372 int vcd_get_audio_type(char** audio_type)
2374 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Get audio type");
2377 ret = vcd_engine_agent_get_audio_type(audio_type);
2379 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to get audio type : ret(%d)", ret);
2385 int vcd_set_private_data(const char* key, const char* data)
2387 vcd_state_e state = vcd_config_get_service_state();
2389 if (VCD_STATE_READY != state) {
2390 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Current state(%d) is NOT 'READY'", state);
2391 return VCD_ERROR_INVALID_STATE;
2394 int ret = vcd_engine_agent_set_private_data(key, data);
2396 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to set private data to the manager client : ret(%d)", ret);
2398 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Set private data to the manager client, key(%s), data(%s)", key, data);
2404 int vcd_get_private_data(const char* key, char** data)
2406 vcd_state_e state = vcd_config_get_service_state();
2408 if (VCD_STATE_READY != state) {
2409 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Current state(%d) is NOT 'READY'", state);
2410 return VCD_ERROR_INVALID_STATE;
2413 int ret = vcd_engine_agent_get_private_data(key, data);
2415 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to get private data from the manager client : ret(%d)", ret);
2417 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Get private data from the manager client, key(%s), data(%s)", key, data);
2423 int vcd_start_recording()
2425 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Start recording");
2428 ret = vcd_engine_agent_start_recording();
2430 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to start recording : ret(%d)", ret);
2436 int vcd_stop_recording()
2438 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Stop recording");
2441 ret = vcd_engine_agent_stop_recording();
2443 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to stop recording : ret(%d)", ret);
2449 int vcd_set_private_data_set_cb(vce_private_data_set_cb callback_func)
2451 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Set private data set cb");
2454 ret = vcd_engine_agent_set_private_data_set_cb(callback_func);
2456 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to set private data set cb : ret(%d)", ret);
2462 int vcd_set_private_data_requested_cb(vce_private_data_requested_cb callback_func)
2464 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Set private data requested cb");
2467 ret = vcd_engine_agent_set_private_data_requested_cb(callback_func);
2469 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to set private data requested cb : ret(%d)", ret);
2475 int vcd_set_nlu_base_info_requested_cb(vce_nlu_base_info_requested_cb callback_func)
2477 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Set nlu base info requested cb");
2480 ret = vcd_engine_agent_set_nlu_base_info_requested_cb(callback_func);
2482 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to set nlu base info requested cb : ret(%d)", ret);