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 bool g_is_engine;
41 static GList *g_proc_list = NULL;
43 static Ecore_Timer *g_restart_timer = NULL;
46 * @brief Enumerations of send event type.
49 VCD_SEND_EVENT_TYPE_TEXT, /**< send text event to vc engine*/
50 VCD_SEND_EVENT_TYPE_LIST_EVENT, /**< send list event to vc engine */
51 VCD_SEND_EVENT_TYPE_HAPTIC_EVENT /**< send haptic event to vc engine */
52 } vcd_send_event_type_e;
54 static int __vcd_server_launch_manager_app();
57 * VC Server Internal Functions
59 static Eina_Bool __stop_by_silence(void *data)
61 SLOG(LOG_DEBUG, TAG_VCD, "@@@ Silence Detected ");
63 vcd_server_mgr_stop();
65 SLOG(LOG_DEBUG, TAG_VCD, "@@@");
69 static Eina_Bool __cancel_by_interrupt(void *data)
71 SLOG(LOG_DEBUG, TAG_VCD, "@@@ Cancel by interrupt");
73 vcd_server_mgr_cancel();
75 SLOG(LOG_DEBUG, TAG_VCD, "@@@");
79 static void __cancel_by_error(void *data)
81 SLOG(LOG_DEBUG, TAG_VCD, "@@@ Cancel by error");
83 vcd_server_mgr_cancel();
85 SLOG(LOG_DEBUG, TAG_VCD, "@@@");
89 static Eina_Bool __restart_engine(void *data)
91 SLOG(LOG_DEBUG, TAG_VCD, "@@@ Restart by no result");
93 g_restart_timer = NULL;
95 /* Restart recognition */
96 int ret = vcd_engine_recognize_start(true);
98 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to restart recognition : result(%d)", ret);
102 SLOG(LOG_DEBUG, TAG_VCD, "[Server Success] Start engine");
104 if (VCD_RECOGNITION_MODE_RESTART_AFTER_REJECT == vcd_client_get_recognition_mode()) {
105 vcd_config_set_service_state(VCD_STATE_RECORDING);
106 vcdc_send_service_state(VCD_STATE_RECORDING);
109 SLOG(LOG_DEBUG, TAG_VCD, "[Server Success] Restart recognition");
111 SLOG(LOG_DEBUG, TAG_VCD, "@@@");
115 static int __server_recorder_callback(const void* data, const unsigned int length)
117 vcd_state_e state = vcd_config_get_service_state();
118 if (VCD_STATE_READY == state) {
119 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Ready state, but recording");
120 } else if (VCD_STATE_PROCESSING == state) {
124 vcp_speech_detect_e speech_detected = VCP_SPEECH_DETECT_NONE;
127 ret = vcd_engine_recognize_audio(data, length, &speech_detected);
131 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to set recording data to engine(%d)", ret);
132 ecore_timer_add(0, __cancel_by_interrupt, NULL);
133 /* Send error cb to manager */
134 if (VCP_ERROR_OUT_OF_NETWORK == ret) {
135 vcdc_send_error_signal_to_manager(vcd_client_manager_get_pid(), VCD_ERROR_TIMED_OUT, "Engine connection failed");
137 vcdc_send_error_signal_to_manager(vcd_client_manager_get_pid(), VCD_ERROR_OPERATION_FAILED, "Engine recognition failed");
142 if (VCP_SPEECH_DETECT_BEGIN == speech_detected) {
143 if (-1 != vcd_client_manager_get_pid()) {
144 /* Manager client is available */
145 if (0 != vcdc_send_speech_detected(vcd_client_manager_get_pid())) {
146 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Fail to send speech detected");
149 } else if (VCP_SPEECH_DETECT_END == speech_detected) {
150 if (VCD_RECOGNITION_MODE_STOP_BY_SILENCE == vcd_client_get_recognition_mode()) {
151 /* silence detected */
152 ecore_timer_add(0, __stop_by_silence, NULL);
153 } else if (VCD_RECOGNITION_MODE_RESTART_AFTER_REJECT == vcd_client_get_recognition_mode()) {
154 /* Stop engine recognition */
155 int ret = vcd_engine_recognize_stop();
157 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to stop recognition : %d", ret);
159 vcd_config_set_service_state(VCD_STATE_PROCESSING);
160 vcdc_send_service_state(VCD_STATE_PROCESSING);
162 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Stop engine only by silence");
163 } else if (VCD_RECOGNITION_MODE_RESTART_CONTINUOUSLY == vcd_client_get_recognition_mode()) {
164 /* Stop engine recognition */
165 int ret = vcd_engine_recognize_stop();
167 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to stop recognition : %d", ret);
175 void __server_recorder_interrupt_callback()
177 SLOG(LOG_DEBUG, TAG_VCD, "@@@ Cancel by sound interrupt");
179 ecore_timer_add(0, __cancel_by_interrupt, NULL);
181 SLOG(LOG_DEBUG, TAG_VCD, "@@@");
184 static void __config_lang_changed_cb(const char* current_lang, void* user_data)
186 SLOG(LOG_DEBUG, TAG_VCD, "@@@ Change language ");
188 /* Current state is recording */
189 vcd_state_e state = vcd_config_get_service_state();
190 if (VCD_STATE_RECORDING == state || VCD_STATE_PROCESSING == state) {
191 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Current state is 'Recording'. Cancel recognition");
192 vcd_server_mgr_cancel();
196 ret = vcd_engine_set_current_language(current_lang);
198 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to set language of engine : %d", ret);
201 SLOG(LOG_DEBUG, TAG_VCD, "@@@");
206 static void __config_foreground_changed_cb(int previous, int current, void* user_data)
208 SLOG(LOG_DEBUG, TAG_VCD, "@@@ Change foreground");
210 SLOG(LOG_DEBUG, TAG_VCD, "Foreground pid(%d)", current);
212 if (VC_NO_FOREGROUND_PID != current) {
213 /* Foreground app is changed */
214 vcd_state_e state = vcd_config_get_service_state();
215 if (VCD_STATE_RECORDING == state) {
216 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Foreground pid(%d) is changed. Cancel recognition", current);
217 ecore_timer_add(0, __cancel_by_interrupt, NULL);
221 SLOG(LOG_DEBUG, TAG_VCD, "@@@");
226 static int __vcd_activate_app_by_appcontrol(const char* appid)
229 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Invalid parameter");
230 return VCD_ERROR_INVALID_PARAMETER;
234 app_control_h app_control = NULL;
235 ret = app_control_create(&app_control);
236 if (APP_CONTROL_ERROR_NONE == ret) {
238 ret = app_control_add_extra_data(app_control, "voice_launch", "get_result");
239 if (APP_CONTROL_ERROR_NONE != ret) {
240 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Fail to add extra data, ret(%d)", ret);
241 app_control_destroy(app_control);
242 return VCD_ERROR_OPERATION_FAILED;
245 ret = app_control_set_app_id(app_control, appid);
246 if (APP_CONTROL_ERROR_NONE != ret) {
247 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Fail to set app id, ret(%d)", ret);
248 app_control_destroy(app_control);
249 return VCD_ERROR_OPERATION_FAILED;
251 // Sent launch request
252 ret = app_control_send_launch_request(app_control, NULL, NULL);
253 if (APP_CONTROL_ERROR_NONE != ret) {
254 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Fail to send launch request, ret(%d)", ret);
255 app_control_destroy(app_control);
256 return VCD_ERROR_OPERATION_FAILED;
258 // Destroy app control
259 ret = app_control_destroy(app_control);
260 if (APP_CONTROL_ERROR_NONE != ret) {
261 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Fail to destroy, ret(%d)", ret);
262 return VCD_ERROR_OPERATION_FAILED;
265 return VCD_ERROR_NONE;
268 static int __vcd_resume_app(const char* appid)
270 app_context_h app_context = NULL;
271 int ret = app_manager_get_app_context(appid, &app_context);
272 if (APP_MANAGER_ERROR_NONE != ret) {
273 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Fail to get app_context, ret(%d), appid(%s)", ret, appid);
274 return VCD_ERROR_OPERATION_FAILED;
277 ret = app_manager_resume_app(app_context);
278 if (APP_MANAGER_ERROR_NONE != ret) {
279 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Fail to resume app, ret(%d), appid(%s)", ret, appid);
280 return VCD_ERROR_OPERATION_FAILED;
282 return VCD_ERROR_NONE;
285 static bool __vcd_is_package_installed(const char* appid)
287 app_info_h app_info = NULL;
288 int ret = app_manager_get_app_info(appid, &app_info);
289 if (APP_MANAGER_ERROR_NONE != ret || NULL == app_info)
291 ret = app_info_destroy(app_info);
292 if (APP_MANAGER_ERROR_NONE != ret)
293 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Fail to destroy app_info, ret(%d)", ret);
297 static bool __vcd_launch_app(const char* result)
299 if (NULL == result) {
300 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Invalid parameter");
301 return VCD_ERROR_INVALID_PARAMETER;
304 GSList* app_list = NULL;
305 if (0 != vc_db_get_appid_list(result, &app_list)) {
306 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] result text is NULL");
307 return VCD_ERROR_INVALID_PARAMETER;
310 if (0 != g_slist_length(app_list)) {
313 vc_deactivated_app_s* temp_app = NULL;
314 iter = g_slist_nth(app_list, 0);
316 while (NULL != iter) {
317 temp_app = iter->data;
319 if (NULL != temp_app) {
320 if (NULL != temp_app->appid) {
322 bool running = false;
323 ret = app_manager_is_running(temp_app->appid, &running);
324 if (APP_MANAGER_ERROR_NONE != ret) {
325 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Fail to check running with appid(%s)", temp_app->appid);
326 free(temp_app->appid);
327 temp_app->appid = NULL;
330 return VCD_ERROR_OPERATION_FAILED;
332 if (false == running) {
333 int tmp_ret = __vcd_is_package_installed(temp_app->appid);
334 if (false == tmp_ret) {
335 SLOG(LOG_WARN, TAG_VCD, "[WARNING] app is not installed, appid(%s)", temp_app->appid);
337 ret = __vcd_activate_app_by_appcontrol(temp_app->appid);
339 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Fail to activate app");
340 free(temp_app->appid);
341 temp_app->appid = NULL;
346 SLOG(LOG_ERROR, TAG_VCD, "Launch app: appid(%s) result(%s)", temp_app->appid, result);
349 ret = __vcd_resume_app(temp_app->appid);
351 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Fail to resume app");
352 free(temp_app->appid);
353 temp_app->appid = NULL;
358 SLOG(LOG_ERROR, TAG_VCD, "Resume app: appid(%s) result(%s)", temp_app->appid, result);
360 free(temp_app->appid);
361 temp_app->appid = NULL;
366 iter = g_slist_next(iter);
371 return VCD_ERROR_NONE;
374 static Eina_Bool __vcd_send_selected_result(void *data)
376 GSList* pid_list = NULL;
377 const char* result = vcd_client_manager_get_result_text();
379 if (0 != vc_info_parser_get_result_pid_list(&pid_list, result)) {
380 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Fail to get pid list. No result");
382 if (0 < g_slist_length(pid_list)) {
384 vc_cmd_s* temp_cmd = NULL;
389 iter = g_slist_nth(pid_list, 0);
390 while (NULL != iter) {
391 temp_cmd = iter->data;
393 if (NULL != temp_cmd && (pre_pid != temp_cmd->pid || pre_type == VC_COMMAND_TYPE_WIDGET || temp_cmd->type == VC_COMMAND_TYPE_WIDGET)) {
394 /* Launch deactivated several apps that is matched with result */
395 ret = __vcd_launch_app(result);
397 SLOG(LOG_ERROR, TAG_VCD, "Fail to launch or resume app, ret(%d) result(%s)", ret, result);
399 /* send result noti */
400 ret = vcdc_send_result(temp_cmd->pid, vcd_client_manager_get_pid(), temp_cmd->type);
402 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Fail to send result, ret(%d)", ret);
405 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Send result : pid(%d) type(%d)", temp_cmd->pid, temp_cmd->type);
406 pre_pid = temp_cmd->pid;
407 pre_type = temp_cmd->type;
413 pid_list = g_slist_remove_link(pid_list, iter);
414 iter = g_slist_nth(pid_list, 0);
419 if (VCD_RECOGNITION_MODE_RESTART_CONTINUOUSLY != vcd_client_get_recognition_mode()) {
420 vcd_config_set_service_state(VCD_STATE_READY);
421 vcdc_send_service_state(VCD_STATE_READY);
427 //static void __vcd_server_pre_result_cb(vcp_pre_result_event_e event, const char* pre_result, void *user_data)
428 static void __vcd_server_asr_result_cb(vcp_asr_result_event_e event, const char* asr_result, void *user_data)
430 if (NULL != asr_result) {
431 SLOG(LOG_DEBUG, TAG_VCD, "[Server] ASR result - Event(%d), Text(%s)", event, asr_result);
432 vcdc_send_pre_result_to_manager(vcd_client_manager_get_pid(), event, asr_result);
438 static void __vcd_server_nlg_result_cb(const char* nlg_result, void *user_data)
440 int ret = __vcd_server_launch_manager_app();
442 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to send dialog : mgr_pid(%d), nlg_result(%s)", vcd_client_manager_get_pid(), nlg_result);
446 ret = vcdc_send_dialog(vcd_client_manager_get_pid(), -1, nlg_result, NULL, 0); //0: VC_DIALOG_END
448 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to send dialog : mgr_pid(%d), nlg_result(%s)", vcd_client_manager_get_pid(), nlg_result);
452 static void* __recorder_stop(void *data)
458 static void __vcd_server_result_cb(vcp_result_event_e event, int* result_id, int count, const char* all_result,
459 const char* non_fixed_result, const char* nlu_result, const char* msg, void *user_data)
461 vc_info_parser_unset_result(vcd_client_manager_get_exclusive());
462 vcd_client_manager_set_result_text(all_result);
464 SLOG(LOG_INFO, TAG_VCD, "[Server] Event(%d), Text(%s) Nonfixed(%s) Msg(%s) Result count(%d)",
465 event, all_result, non_fixed_result, msg, count);
467 if (VCD_RECOGNITION_MODE_RESTART_AFTER_REJECT == vcd_client_get_recognition_mode()) {
468 if (VCP_RESULT_EVENT_REJECTED == event) {
469 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Restart by no or rejected result");
470 /* If no result and restart option is ON */
471 /* Send reject message */
472 bool temp = vcd_client_manager_get_exclusive();
473 vc_info_parser_set_result(all_result, event, msg, NULL, temp);
474 if (0 != vcdc_send_result_to_manager(vcd_client_manager_get_pid(), VC_RESULT_TYPE_NOTIFICATION)) {
475 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Fail to send result");
478 g_restart_timer = ecore_timer_add(0, __restart_engine, NULL);
481 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Stop recorder due to success");
482 //vcd_recorder_stop();
483 ecore_main_loop_thread_safe_call_sync(__recorder_stop, NULL);
484 } else if (VCD_RECOGNITION_MODE_RESTART_CONTINUOUSLY == vcd_client_get_recognition_mode()) {
485 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Restart continuously");
486 /* Restart option is ON */
487 g_restart_timer = ecore_timer_add(0, __restart_engine, NULL);
488 if (VCP_RESULT_EVENT_REJECTED == event) {
489 bool temp = vcd_client_manager_get_exclusive();
490 vc_info_parser_set_result(all_result, event, msg, NULL, temp);
491 if (0 != vcdc_send_result_to_manager(vcd_client_manager_get_pid(), VC_RESULT_TYPE_NOTIFICATION)) {
492 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Fail to send result");
499 int pid = vcd_client_widget_get_foreground_pid();
501 if (NULL != all_result) {
502 vc_info_parser_set_result(all_result, event, msg, NULL, false);
504 vcd_client_widget_get_asr_result_enabled(pid, &enable);
505 if (true == enable) {
506 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Send ASR result to Widget client");
507 bool is_consumed = false;
508 if (0 != vcdc_send_asr_result(pid, VC_COMMAND_TYPE_WIDGET, &is_consumed)) {
509 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to send asr result");
511 SLOG(LOG_DEBUG, TAG_VCD, "[Server] ASR result is consumed(%d)", is_consumed);
512 if (true == is_consumed) {
513 vcdc_send_show_tooltip(pid, false);
514 if (-1 != vcd_client_manager_get_pid()) {
515 /* Manager client is available */
516 vc_info_parser_unset_result(false);
517 vc_info_parser_set_result(all_result, VC_RESULT_EVENT_RESULT_SUCCESS, msg, NULL, false);
518 if (0 != vcdc_send_result_to_manager(vcd_client_manager_get_pid(), VC_RESULT_TYPE_NOTIFICATION)) {
519 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Fail to send result");
523 vcd_client_manager_set_exclusive(false);
525 vcd_config_set_service_state(VCD_STATE_READY);
526 vcdc_send_service_state(VCD_STATE_READY);
534 /* if nlu_result is exist, Add command handle(is_action) into result list */
536 SLOG(LOG_DEBUG, TAG_VCD, "[Server] @ Get engine result @");
539 vc_cmd_s* temp_cmd = NULL;
540 vc_cmd_list_h vc_cmd_list = NULL;
542 if (0 != vc_cmd_list_create(&vc_cmd_list)) {
543 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Fail to create command list");
544 vcd_client_manager_set_exclusive(false);
545 vcd_config_set_service_state(VCD_STATE_READY);
546 vcdc_send_service_state(VCD_STATE_READY);
550 /* priority filter */
551 /* system > exclusive > foreground = widget > system_background > background */
553 int* filtered_id = (int*)calloc(count, sizeof(int));
555 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to allocate memory");
558 int filtered_count = 0;
559 int top_priority = VC_COMMAND_PRIORITY_BACKGROUND;
560 for (i = 0; i < count; i++) {
561 SLOG(LOG_INFO, TAG_VCD, "[Server] [%d] Result id(%d)", i, result_id[i]);
563 if (0 > result_id[i]) {
564 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Result ID(%d) is NOT valid", result_id[i]);
568 ret = vcd_client_get_cmd_from_result_id(result_id[i], &temp_cmd);
569 if (0 == ret && NULL != temp_cmd) {
570 if (top_priority == temp_cmd->priority) {
571 filtered_id[filtered_count] = result_id[i];
573 } else if (top_priority < temp_cmd->priority) {
576 filtered_id[0] = result_id[i];
578 top_priority = temp_cmd->priority;
584 for (i = 0; i < filtered_count; i++) {
585 SLOG(LOG_INFO, TAG_VCD, "[Server] [%d] Filtered Result id(%d)", i, filtered_id[i]);
587 if (filtered_id[i] < 0) {
588 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Filtered ID(%d) is NOT valid", filtered_id[i]);
592 ret = vcd_client_get_cmd_from_result_id(filtered_id[i], &temp_cmd);
593 if (0 == ret && NULL != temp_cmd) {
594 switch (temp_cmd->format) {
595 case VC_CMD_FORMAT_FIXED:
596 case VC_CMD_FORMAT_FIXED_AND_VFIXED:
597 case VC_CMD_FORMAT_VFIXED_AND_FIXED:
598 case VC_CMD_FORMAT_PARTIAL:
599 case VC_CMD_FORMAT_FIXED_AND_NONFIXED:
600 case VC_CMD_FORMAT_NONFIXED_AND_FIXED:
602 case VC_CMD_FORMAT_ACTION:
606 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Unknown command type : %d", temp_cmd->type);
610 if (0 != vc_cmd_list_add(vc_cmd_list, (vc_cmd_h)temp_cmd)) {
611 SLOG(LOG_DEBUG, TAG_VCD, "Fail to add command to list");
612 vc_cmd_destroy((vc_cmd_h)temp_cmd);
615 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] NOT found matached result(%d)", filtered_id[i]);
619 if (NULL != filtered_id) {
624 if (NULL != nlu_result) {
625 SLOG(LOG_INFO, TAG_VCD, "[Server] NLU (%s)", nlu_result);
626 vc_info_parser_set_nlu_result(nlu_result);
627 if (0 == is_action) {
629 if (0 != vc_cmd_create(&nlu_cmd)) {
630 SLOG(LOG_ERROR, TAG_VCD, "Fail to nlu cmd create");
632 if (0 != vc_cmd_set_type(nlu_cmd, VC_COMMAND_TYPE_SYSTEM)) {
633 SLOG(LOG_ERROR, TAG_VCD, "Fail to set type");
635 if (0 != vc_cmd_set_pid(nlu_cmd, vcd_client_manager_get_pid())) {
636 SLOG(LOG_ERROR, TAG_VCD, "Fail to set pid");
638 if (0 != vc_cmd_set_format(nlu_cmd, VC_CMD_FORMAT_ACTION)) {
639 SLOG(LOG_ERROR, TAG_VCD, "Fail to set format");
641 if (0 != vc_cmd_list_add(vc_cmd_list, nlu_cmd)) {
642 SLOG(LOG_ERROR, TAG_VCD, "Fail to add nlu cmd to list");
643 vc_cmd_destroy(nlu_cmd);
649 vc_cmd_print_list(vc_cmd_list);
651 SLOG(LOG_DEBUG, TAG_VCD, "[Server] @@@@");
653 int result_count = 0;
654 vc_cmd_list_get_count(vc_cmd_list, &result_count);
656 if (0 == result_count) {
658 if (NULL != all_result) {
659 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Engine result is no command : %s", all_result);
661 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Engine result is NULL");
663 bool temp = vcd_client_manager_get_exclusive();
664 vc_info_parser_set_result(all_result, event, msg, NULL, temp);
666 int pid = vcd_client_widget_get_foreground_pid();
668 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Request tooltip hide");
669 /* Send to hide tooltip */
670 vcdc_send_show_tooltip(pid, false);
673 if (-1 != vcd_client_manager_get_pid()) {
674 /* Manager client is available */
675 if (0 != vcdc_send_result_to_manager(vcd_client_manager_get_pid(), VC_RESULT_TYPE_NORMAL)) {
676 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Fail to send result");
680 vcd_client_manager_set_exclusive(false);
684 if (false == vcd_client_manager_get_exclusive()) {
685 int pid = vcd_client_widget_get_foreground_pid();
687 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Request tooltip hide");
688 vcdc_send_show_tooltip(pid, false);
691 vc_info_parser_set_result(all_result, event, msg, vc_cmd_list, false);
693 if (-1 != vcd_client_manager_get_pid()) {
694 /* Manager client is available */
695 if (0 != vcdc_send_result_to_manager(vcd_client_manager_get_pid(), VC_RESULT_TYPE_NORMAL)) {
696 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Fail to send result");
699 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Manager is NOT available. Send result to client directly");
700 /* Send result to client */
701 ecore_timer_add(0, __vcd_send_selected_result, NULL);
704 /* exclusive command */
705 vc_info_parser_set_result(all_result, event, msg, vc_cmd_list, true);
707 if (-1 != vcd_client_manager_get_pid()) {
708 /* Manager client is available */
709 if (0 != vcdc_send_result_to_manager(vcd_client_manager_get_pid(), VC_RESULT_TYPE_NORMAL)) {
710 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Fail to send result");
713 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Manager is NOT available");
716 vcd_client_manager_set_exclusive(false);
720 vc_cmd_list_destroy(vc_cmd_list, true);
726 if (NULL == result_id) {
728 if (NULL != all_result) {
729 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Engine result is no command : %s", all_result);
730 bool temp = vcd_client_manager_get_exclusive();
731 vc_info_parser_set_result(all_result, event, msg, NULL, temp);
734 int pid = vcd_client_widget_get_foreground_pid();
736 if (NULL != all_result) {
737 /* Send result text to widget */
738 vcdc_send_result(pid, vcd_client_manager_get_pid(), VC_COMMAND_TYPE_WIDGET);
741 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Request tooltip hide");
742 /* Send to hide tooltip */
743 vcdc_send_show_tooltip(pid, false);
746 if (-1 != vcd_client_manager_get_pid()) {
747 /* Manager client is available */
748 if (0 != vcdc_send_result_to_manager(vcd_client_manager_get_pid(), VC_RESULT_TYPE_NORMAL)) {
749 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Fail to send result");
753 vcd_client_manager_set_exclusive(false);
759 SLOG(LOG_DEBUG, TAG_VCD, "[Server] @ Get engine result @");
762 vc_cmd_s* temp_cmd = NULL;
763 vc_cmd_list_h vc_cmd_list = NULL;
765 if (0 != vc_cmd_list_create(&vc_cmd_list)) {
766 SLOG(LOG_ERROR, TAG_VCD, "[Server] Fail to create command list");
767 vcd_client_manager_set_exclusive(false);
768 vcd_config_set_service_state(VCD_STATE_READY);
769 vcdc_send_service_state(VCD_STATE_READY);
774 for (i = 0; i < count; i++) {
775 SLOG(LOG_DEBUG, TAG_VCD, "[Server] [%d] Result ID(%d)", i, result_id[i]);
777 if (result_id[i] < 0) {
778 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Result ID(%d) is NOT valid", result_id[i]);
782 ret = vcd_client_get_cmd_from_result_id(result_id[i], &temp_cmd);
783 if (0 == ret && NULL != temp_cmd) {
784 switch (temp_cmd->format) {
785 case VC_CMD_FORMAT_FIXED:
786 case VC_CMD_FORMAT_FIXED_AND_VFIXED:
787 case VC_CMD_FORMAT_VFIXED_AND_FIXED:
788 case VC_CMD_FORMAT_PARTIAL:
789 /* Nonfixed result is NOT valid */
791 case VC_CMD_FORMAT_FIXED_AND_NONFIXED:
792 if (NULL == temp_cmd->parameter) {
793 if (NULL != non_fixed_result) {
794 temp_cmd->parameter = strdup(non_fixed_result);
797 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Command type is NOT vaild. Parameter (%s)", temp_cmd->parameter);
800 case VC_CMD_FORMAT_NONFIXED_AND_FIXED:
801 if (NULL == temp_cmd->command) {
802 if (NULL != non_fixed_result) {
803 temp_cmd->command = strdup(non_fixed_result);
806 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Command type is NOT vaild. Command (%s)", temp_cmd->command);
811 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Unknown command type : %d", temp_cmd->type);
815 if (0 != vc_cmd_list_add(vc_cmd_list, (vc_cmd_h)temp_cmd)) {
816 SLOG(LOG_DEBUG, TAG_VCD, "Fail to add command to list");
817 vc_cmd_destroy((vc_cmd_h)temp_cmd);
820 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] NOT found matached result(%d)", result_id[i]);
824 vc_cmd_print_list(vc_cmd_list);
826 SLOG(LOG_DEBUG, TAG_VCD, "[Server] @@@@");
828 int result_count = 0;
829 vc_cmd_list_get_count(vc_cmd_list, &result_count);
831 if (false == vcd_client_manager_get_exclusive()) {
832 int pid = vcd_client_widget_get_foreground_pid();
834 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Request tooltip hide");
835 vcdc_send_show_tooltip(pid, false);
838 vc_info_parser_set_result(all_result, event, msg, vc_cmd_list, false);
840 if (-1 != vcd_client_manager_get_pid()) {
841 /* Manager client is available */
842 if (0 != vcdc_send_result_to_manager(vcd_client_manager_get_pid(), VC_RESULT_TYPE_NORMAL)) {
843 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Fail to send result");
846 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Manager is NOT available. Send result to client directly");
847 /* Send result to client */
848 ecore_timer_add(0, __vcd_send_selected_result, NULL);
851 /* exclusive command */
852 vc_info_parser_set_result(all_result, event, msg, vc_cmd_list, true);
854 if (-1 != vcd_client_manager_get_pid()) {
855 /* Manager client is available */
856 if (0 != vcdc_send_result_to_manager(vcd_client_manager_get_pid(), VC_RESULT_TYPE_NORMAL)) {
857 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Fail to send result");
860 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Manager is NOT available");
863 vcd_client_manager_set_exclusive(false);
866 vc_cmd_list_destroy(vc_cmd_list, true);
873 static void __vcd_server_nlu_result_cb(vcp_result_event_e event, const char* nlu_result, void *user_data)
875 SLOG(LOG_DEBUG, TAG_VCD, "[Server] NLU result cb - event(%d)", event);
876 SLOG(LOG_DEBUG, TAG_VCD, "[Server] result (%s)", nlu_result);
878 int ret = vc_info_parser_set_nlu_result(nlu_result);
880 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to set nlu result (%d)", ret);
887 static void __vcd_server_error_cb(vcp_error_e error, const char* msg, void *user_data)
889 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Engine Error cb - reason(%d), msg(%s)", error, msg);
890 ecore_main_loop_thread_safe_call_async(__cancel_by_error, NULL);
892 char* error_msg = NULL;
894 error_msg = strdup(msg);
897 if (0 != vcdc_send_error_signal_to_manager(vcd_client_manager_get_pid(), error, error_msg)) {
898 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to send error signal");
901 if (NULL != error_msg) {
910 * vcd server Interfaces
912 static void __vcd_file_clean_up()
914 SLOG(LOG_DEBUG, TAG_VCD, "== Old file clean up == ");
917 struct dirent *dirp = NULL;
919 dp = opendir(VC_RUNTIME_INFO_ROOT);
921 SLOG(LOG_ERROR, TAG_VCD, "[File message WARN] Fail to open path : %s", VC_RUNTIME_INFO_ROOT);
925 char remove_path[256] = {0, };
930 if (!strncmp("vc_", dirp->d_name, strlen("vc_"))) {
931 memset(remove_path, 0, 256);
932 snprintf(remove_path, 256, "%s/%s", VC_RUNTIME_INFO_ROOT, dirp->d_name);
935 if (0 != remove(remove_path)) {
936 SLOG(LOG_WARN, TAG_VCD, "[File message WARN] Fail to remove file : %s", remove_path);
938 SLOG(LOG_DEBUG, TAG_VCD, "[File message] Remove file : %s", remove_path);
942 } while (NULL != dirp);
949 static int __vcd_db_clean_up()
952 int cnt = VC_COMMAND_TYPE_FOREGROUND;
954 if (VC_COMMAND_TYPE_BACKGROUND != cnt)
955 ret = vc_db_delete_commands(-1, cnt, NULL);
956 } while (VC_COMMAND_TYPE_EXCLUSIVE >= ++cnt);
965 /* Remove old file */
966 __vcd_file_clean_up();
968 /* initialize modules */
969 ret = vcd_config_initialize(__config_lang_changed_cb, __config_foreground_changed_cb, NULL);
971 SLOG(LOG_ERROR, TAG_VCD, "[Server WARNING] Fail to initialize config.");
975 ret = __vcd_db_clean_up();
977 SLOG(LOG_ERROR, TAG_VCD, "[Server WARNING] Fail to remove db data");
980 vcd_config_set_service_state(VCD_STATE_NONE);
982 //ret = vcd_engine_agent_init(__vcd_server_pre_result_cb, __vcd_server_result_cb, __vcd_server_nlu_result_cb, __vcd_server_error_cb);
983 ret = vcd_engine_agent_init(__vcd_server_asr_result_cb, __vcd_server_result_cb, __vcd_server_nlg_result_cb, __vcd_server_error_cb);
985 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to engine agent initialize : result(%d)", ret);
989 if (0 != vcd_recorder_create(__server_recorder_callback, __server_recorder_interrupt_callback)) {
990 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to create recorder");
991 return VCD_ERROR_OPERATION_FAILED;
995 ret = vcd_engine_agent_initialize_current_engine();
997 if (VCD_ERROR_ENGINE_NOT_FOUND == ret)
998 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] There is No Voice control engine");
1000 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to init engine");
1002 g_is_engine = false;
1008 if (0 != vcd_engine_agent_load_current_engine()) {
1009 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to load current engine");
1010 return VCD_ERROR_OPERATION_FAILED;
1013 /* Initialize manager info */
1014 vcd_client_manager_unset();
1016 vcd_config_set_service_state(VCD_STATE_READY);
1017 vcdc_send_service_state(VCD_STATE_READY);
1019 SLOG(LOG_DEBUG, TAG_VCD, "[Server SUCCESS] initialize");
1027 if (0 < g_list_length(g_proc_list)) {
1028 iter = g_list_first(g_proc_list);
1029 while (NULL != iter) {
1030 g_proc_list = g_list_remove_link(g_proc_list, iter);
1031 iter = g_list_first(g_proc_list);
1035 if (g_restart_timer != NULL) {
1036 ecore_timer_del(g_restart_timer);
1037 g_restart_timer = NULL;
1040 vcd_state_e state = vcd_config_get_service_state();
1041 if (VCD_STATE_READY != state) {
1042 if (VCD_STATE_RECORDING == state) {
1043 vcd_recorder_stop();
1045 vcd_engine_recognize_cancel();
1047 if (0 != vcd_recorder_destroy()) {
1048 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to destroy recorder");
1050 SLOG(LOG_DEBUG, TAG_VCD, "[Server] destroy recorder");
1053 if (0 != vcd_engine_agent_release()) {
1054 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to release engine");
1056 SLOG(LOG_DEBUG, TAG_VCD, "[Server] release engine");
1059 vcd_client_manager_unset_appid();
1061 vcd_config_set_service_state(VCD_STATE_NONE);
1062 vcdc_send_service_state(VCD_STATE_NONE);
1064 SLOG(LOG_DEBUG, TAG_VCD, "[Server] mode finalize");
1069 static Eina_Bool __finalize_quit_ecore_loop(void *data)
1071 SLOG(LOG_DEBUG, TAG_VCD, "[Server] quit ecore main loop");
1072 ecore_main_loop_quit();
1076 static void __read_proc()
1079 struct dirent *dirp = NULL;
1083 if (0 < g_list_length(g_proc_list)) {
1084 iter = g_list_first(g_proc_list);
1085 while (NULL != iter) {
1086 g_proc_list = g_list_remove_link(g_proc_list, iter);
1087 iter = g_list_first(g_proc_list);
1091 dp = opendir("/proc");
1093 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Fail to open proc");
1099 tmp = atoi(dirp->d_name);
1100 if (0 >= tmp) continue;
1101 g_proc_list = g_list_append(g_proc_list, GINT_TO_POINTER(tmp));
1103 } while (NULL != dirp);
1109 static void __vcd_cleanup_client(vcd_client_type_e type)
1111 int* client_list = NULL;
1112 int client_count = 0;
1119 if (VCD_CLIENT_TYPE_NORMAL == type) {
1120 ret = vcd_client_get_list(&client_list, &client_count);
1121 } else if (VCD_CLIENT_TYPE_WIDGET == type) {
1122 ret = vcd_client_widget_get_list(&client_list, &client_count);
1123 } else if (VCD_CLIENT_TYPE_MANAGER == type) {
1124 mgr_pid = vcd_client_manager_get_pid();
1125 client_list = &mgr_pid;
1127 if (-1 == mgr_pid) {
1128 SLOG(LOG_WARN, TAG_VCD, "[WARNING] Invalid Manager pid");
1133 if (0 == ret || mgr_pid > 0) {
1134 SLOG(LOG_DEBUG, TAG_VCD, "@@@ Clean up %s client ", type ? (type == 1) ? "Widget" : "Manager" : "Normal");
1135 if (NULL != client_list && client_count > 0) {
1136 for (i = 0; i < client_count; i++) {
1139 for (j = 0; j < g_list_length(g_proc_list); j++) {
1140 iter = g_list_nth(g_proc_list, j);
1142 if (client_list[i] == GPOINTER_TO_INT(iter->data)) {
1143 SLOG(LOG_DEBUG, TAG_VCD, "%s pid(%d) is running", type ? (type == 1) ? "Widget" : "Manager" : "Normal", client_list[i]);
1150 if (false == exist) {
1151 SLOG(LOG_ERROR, TAG_VCD, "%s pid(%d) should be removed", type ? (type == 1) ? "Widget" : "Manager" : "Normal", client_list[i]);
1152 if (VCD_CLIENT_TYPE_NORMAL == type)
1153 vcd_server_finalize(client_list[i]);
1154 else if (VCD_CLIENT_TYPE_WIDGET == type)
1155 vcd_server_widget_finalize(client_list[i]);
1157 vcd_server_mgr_finalize(mgr_pid);
1161 SLOG(LOG_DEBUG, TAG_VCD, "@@@");
1163 if (NULL != client_list && -1 == mgr_pid) {
1170 Eina_Bool vcd_cleanup_client_all(void *data)
1174 __vcd_cleanup_client(VCD_CLIENT_TYPE_NORMAL);
1175 __vcd_cleanup_client(VCD_CLIENT_TYPE_WIDGET);
1176 __vcd_cleanup_client(VCD_CLIENT_TYPE_MANAGER);
1179 if (0 == vcd_client_get_list(&client_list, &client_count)) {
1180 SLOG(LOG_DEBUG, TAG_VCD, "@@@ Clean up client ");
1181 if (NULL != client_list && client_count > 0) {
1182 for (i = 0; i < client_count; i++) {
1185 for (j = 0; j < g_list_length(g_proc_list); j++) {
1186 iter = g_list_nth(g_proc_list, j);
1188 if (client_list[i] == GPOINTER_TO_INT(iter->data)) {
1189 SLOG(LOG_DEBUG, TAG_VCD, "pid(%d) is running", client_list[i]);
1196 if (false == exist) {
1197 SLOG(LOG_ERROR, TAG_VCD, "pid(%d) should be removed", client_list[i]);
1198 vcd_server_finalize(client_list[i]);
1201 result = vcdc_send_hello(client_list[i], VCD_CLIENT_TYPE_NORMAL);
1204 SLOG(LOG_DEBUG, TAG_VCD, "[Server] pid(%d) should be removed.", client_list[i]);
1205 vcd_server_finalize(client_list[i]);
1206 } else if (-1 == result) {
1207 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Hello result has error");
1212 SLOG(LOG_DEBUG, TAG_VCD, "@@@");
1214 if (NULL != client_list) {
1219 /* If app is in background state, app cannot response message. */
1220 if (0 == vcd_client_widget_get_list(&client_list, &client_count)) {
1221 SLOG(LOG_DEBUG, TAG_VCD, "@@@ Clean up widget");
1222 if (NULL != client_list && client_count > 0) {
1223 for (i = 0; i < client_count; i++) {
1226 for (j = 0; j < g_list_length(g_proc_list); j++) {
1227 iter = g_list_nth(g_proc_list, j);
1229 if (client_list[i] == GPOINTER_TO_INT(iter->data)) {
1230 SLOG(LOG_DEBUG, TAG_VCD, "widget pid(%d) is running", client_list[i]);
1237 if (false == exist) {
1238 SLOG(LOG_ERROR, TAG_VCD, "widget pid(%d) should be removed", client_list[i]);
1239 vcd_server_widget_finalize(client_list[i]);
1242 result = vcdc_send_hello(client_list[i], VCD_CLIENT_TYPE_WIDGET);
1245 SLOG(LOG_DEBUG, TAG_VCD, "[Server] widget pid(%d) should be removed.", client_list[i]);
1246 vcd_server_widget_finalize(client_list[i]);
1247 } else if (-1 == result) {
1248 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Hello result has error");
1253 SLOG(LOG_DEBUG, TAG_VCD, "@@@");
1256 if (NULL != client_list) {
1264 int mgr_pid = vcd_client_manager_get_pid();
1266 for (j = 0; j < g_list_length(g_proc_list); j++) {
1267 iter = g_list_nth(g_proc_list, j);
1269 if (mgr_pid == GPOINTER_TO_INT(iter->data)) {
1270 SLOG(LOG_DEBUG, TAG_VCD, "manager pid(%d) is running", mgr_pid);
1277 if (false == exist) {
1278 SLOG(LOG_ERROR, TAG_VCD, "manager pid (%d) should be removed", mgr_pid);
1279 vcd_server_mgr_finalize(mgr_pid);
1286 int vcd_server_get_service_state()
1288 return vcd_config_get_service_state();
1291 int vcd_server_get_foreground()
1294 vcd_config_get_foreground(&pid);
1302 int vcd_server_mgr_initialize(int pid)
1304 if (false == g_is_engine) {
1305 if (0 != vcd_engine_agent_initialize_current_engine()) {
1306 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] No Engine");
1307 g_is_engine = false;
1308 return VCD_ERROR_ENGINE_NOT_FOUND;
1314 /* check if pid is valid */
1315 if (false == vcd_client_manager_is_valid(pid)) {
1316 SLOG(LOG_ERROR, TAG_VCD, "[Server] old manager pid(%d) be removed", vcd_client_manager_get_pid());
1317 vcd_server_mgr_cancel();
1318 vcd_client_manager_unset();
1321 /* Add client information to client manager */
1322 if (0 != vcd_client_manager_set(pid)) {
1323 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to add manager");
1324 return VCD_ERROR_OPERATION_FAILED;
1327 if (0 != vcdc_send_manager_pid(pid))
1328 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Fail to send manager pid");
1330 SLOG(LOG_DEBUG, TAG_VCD, "[Server Success] Manager initialize : pid(%d)", pid);
1332 return VCD_ERROR_NONE;
1335 int vcd_server_mgr_finalize(int pid)
1337 /* check if pid is valid */
1338 if (false == vcd_client_manager_is_valid(pid)) {
1339 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] The manager pid(%d) is NOT valid", pid);
1340 return VCD_ERROR_INVALID_PARAMETER;
1343 /* Cancel recognition */
1344 vcd_server_mgr_cancel();
1346 if (0 != vcdc_send_manager_pid(-1))
1347 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Fail to send manager pid");
1349 /* Remove manager information */
1350 if (0 != vcd_client_manager_unset()) {
1351 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Fail to delete client");
1354 if (0 == vcd_client_get_ref_count()) {
1355 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Connected client list is empty");
1356 ecore_timer_add(0, __finalize_quit_ecore_loop, NULL);
1359 SLOG(LOG_DEBUG, TAG_VCD, "[Server Success] Manager Finalize : pid(%d)", pid);
1361 return VCD_ERROR_NONE;
1364 int vcd_server_mgr_set_command(int pid)
1366 if (0 != vcd_client_manager_set_command(pid)) {
1367 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] The manager pid(%d) is NOT valid", pid);
1368 return VCD_ERROR_INVALID_PARAMETER;
1370 return VCD_ERROR_NONE;
1373 int vcd_server_mgr_unset_command(int pid)
1375 if (0 != vcd_client_manager_unset_command(pid)) {
1376 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] The manager pid(%d) is NOT valid", pid);
1377 return VCD_ERROR_INVALID_PARAMETER;
1379 return VCD_ERROR_NONE;
1382 int vcd_server_mgr_set_demandable_client(int pid)
1384 /* check if pid is valid */
1385 if (false == vcd_client_manager_is_valid(pid)) {
1386 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] The manager pid(%d) is NOT valid", pid);
1387 return VCD_ERROR_INVALID_PARAMETER;
1390 GSList* client_list = NULL;
1391 if (0 != vc_info_parser_get_demandable_clients(&client_list)) {
1392 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to get demandable client");
1393 return VCD_ERROR_OPERATION_FAILED;
1396 /* Save client list */
1397 if (0 != vcd_client_manager_set_demandable_client(pid, client_list)) {
1398 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to set demandable client list");
1399 return VCD_ERROR_OPERATION_FAILED;
1402 return VCD_ERROR_NONE;
1405 int vcd_server_mgr_set_audio_type(int pid, const char* audio_type)
1407 /* check if pid is valid */
1408 if (false == vcd_client_manager_is_valid(pid)) {
1409 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] The manager pid(%d) is NOT valid", pid);
1410 return VCD_ERROR_INVALID_PARAMETER;
1414 vcp_audio_type_e type = VCP_AUDIO_TYPE_PCM_S16_LE;
1418 ret = vcd_engine_get_audio_format(audio_type, &type, &rate, &channel);
1420 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to get audio format : %d", ret);
1421 return VCD_ERROR_OPERATION_FAILED;
1424 ret = vcd_recorder_set(audio_type, type, rate, channel);
1426 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to set audio in type : %d", ret);
1427 return VCD_ERROR_OPERATION_FAILED;
1430 return VCD_ERROR_NONE;
1433 int vcd_server_mgr_get_audio_type(int pid, char** audio_type)
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_recorder_get(audio_type);
1443 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to get audio in type : %d", ret);
1444 return VCD_ERROR_OPERATION_FAILED;
1447 return VCD_ERROR_NONE;
1450 int vcd_server_mgr_set_client_info(int pid)
1452 /* check if pid is valid */
1453 if (false == vcd_client_manager_is_valid(pid)) {
1454 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] The manager pid(%d) is NOT valid", pid);
1455 return VCD_ERROR_INVALID_PARAMETER;
1458 int ret = vcd_client_save_client_info();
1460 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to save client info : %d", ret);
1461 return VCD_ERROR_OPERATION_FAILED;
1464 return VCD_ERROR_NONE;
1467 static int __start_internal_recognition()
1471 if (0 != vcd_client_command_collect_command()) {
1472 SLOG(LOG_ERROR, TAG_VCD, "[Client Data ERROR] Fail to collect command");
1473 /* Send error cb to manager */
1474 int pid = vcd_client_widget_get_foreground_pid();
1476 vcdc_send_error_signal_to_manager(vcd_client_manager_get_pid(), VCD_ERROR_OPERATION_FAILED, "voice_engine.error.proc_fail");
1477 return VCD_ERROR_OPERATION_FAILED;
1480 /* 3. Set command to engine */
1481 ret = vcd_engine_set_commands();
1483 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to set commands : %d", ret);
1484 /* Send error cb to manager */
1485 int pid = vcd_client_widget_get_foreground_pid();
1487 vcdc_send_error_signal_to_manager(vcd_client_manager_get_pid(), VCD_ERROR_OPERATION_FAILED, "voice_engine.error.proc_fail");
1488 return VCD_ERROR_OPERATION_FAILED;
1491 SLOG(LOG_DEBUG, TAG_VCD, "[Server Success] Set command");
1493 bool stop_by_silence = true;
1494 if (VCD_RECOGNITION_MODE_MANUAL == vcd_client_get_recognition_mode()) {
1495 stop_by_silence = false;
1498 vcd_client_manager_set_result_text(NULL);
1500 /* 4. start recognition */
1501 ret = vcd_engine_recognize_start(stop_by_silence);
1503 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to start recognition : result(%d)", ret);
1504 /* Send error cb to manager */
1505 int pid = vcd_client_widget_get_foreground_pid();
1507 vcdc_send_error_signal_to_manager(vcd_client_manager_get_pid(), VCD_ERROR_OPERATION_FAILED, "voice_engine.error.proc_fail");
1508 return VCD_ERROR_OPERATION_FAILED;
1511 SLOG(LOG_DEBUG, TAG_VCD, "[Server Success] Start engine");
1514 /* 5. recorder start */
1515 ret = vcd_recorder_start();
1517 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to start recorder : result(%d)", ret);
1518 vcd_engine_recognize_cancel();
1519 /* Send error cb to manager */
1520 int pid = vcd_client_widget_get_foreground_pid();
1522 vcdc_send_error_signal_to_manager(vcd_client_manager_get_pid(), VCD_ERROR_OPERATION_FAILED, "voice_engine.error.proc_fail");
1527 vcd_config_set_service_state(VCD_STATE_RECORDING);
1528 vcdc_send_service_state(VCD_STATE_RECORDING);
1530 SLOG(LOG_DEBUG, TAG_VCD, "[Server Success] Start recognition(%d)", stop_by_silence);
1535 static Eina_Bool __vcd_request_show_tooltip(void *data)
1537 int pid = vcd_client_widget_get_foreground_pid();
1539 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Request tooltip show and widget command");
1540 vcdc_send_show_tooltip(pid, (bool)data);
1546 int vcd_server_mgr_start(vcd_recognition_mode_e recognition_mode, bool exclusive_cmd, bool start_by_client)
1548 /* 1. check current state */
1549 vcd_state_e state = vcd_config_get_service_state();
1551 if (VCD_STATE_READY != state) {
1552 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Current state is not ready");
1553 return VCD_ERROR_INVALID_STATE;
1555 if (-1 == vcd_client_manager_get_pid()) {
1556 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Manager is NOT available.");
1557 return VCD_ERROR_OPERATION_FAILED;
1560 SLOG(LOG_DEBUG, TAG_VCD, "[Server] set recognition mode = %d", recognition_mode);
1561 vcd_client_set_recognition_mode(recognition_mode);
1563 if (false == exclusive_cmd) {
1564 vcd_client_update_foreground_pid();
1565 /* Notify show tooltip */
1566 if (1 == vcd_config_get_command_type_enabled(VC_COMMAND_TYPE_WIDGET)) {
1567 int pid = vcd_client_widget_get_foreground_pid();
1569 SLOG(LOG_INFO, TAG_VCD, "[Server] Request tooltip show and widget command");
1570 ecore_timer_add(0, __vcd_request_show_tooltip, (void*)true);
1575 vcd_client_manager_set_exclusive(exclusive_cmd);
1579 if (true == start_by_client) {
1580 /* Get foreground pid */
1581 if (0 != vcd_config_get_foreground(&fg_pid)) {
1582 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to get foreground");
1585 /* Set client exclusive option */
1586 if (0 != vcd_client_set_exclusive_command(fg_pid)) {
1587 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to set exclusive command");
1591 int ret = __start_internal_recognition();
1593 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to start recongition : %d", ret);
1597 if (true == start_by_client) {
1598 vcd_client_unset_exclusive_command(fg_pid);
1601 return VCD_ERROR_NONE;
1604 int vcd_server_mgr_stop()
1606 /* 1. Check current state is recording */
1607 if (VCD_STATE_RECORDING != vcd_config_get_service_state()) {
1608 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Current state is not recording");
1609 return VCD_ERROR_INVALID_STATE;
1611 if (-1 == vcd_client_manager_get_pid()) {
1612 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Manager is NOT available.");
1613 return VCD_ERROR_OPERATION_FAILED;
1617 /* 2. Stop recorder */
1618 vcd_recorder_stop();
1621 /* 3. Stop engine recognition */
1622 int ret = vcd_engine_recognize_stop();
1624 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to stop recognition : %d", ret);
1627 /* 4. Set original mode */
1628 vcd_config_set_service_state(VCD_STATE_PROCESSING);
1629 vcdc_send_service_state(VCD_STATE_PROCESSING);
1631 return VCD_ERROR_NONE;
1634 int vcd_server_mgr_cancel()
1636 if (-1 == vcd_client_manager_get_pid()) {
1637 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Manager is NOT available.");
1638 return VCD_ERROR_OPERATION_FAILED;
1641 if (g_restart_timer != NULL) {
1642 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Delete restart engine timer");
1643 ecore_timer_del(g_restart_timer);
1644 g_restart_timer = NULL;
1647 /* 1. Check current state */
1648 vcd_state_e state = vcd_config_get_service_state();
1649 if (VCD_STATE_READY == state) {
1650 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Current state is READY");
1651 vcd_recorder_stop();
1652 vcdc_send_service_state(VCD_STATE_READY);
1653 return VCD_ERROR_NONE;
1657 /* 2. Stop recorder */
1658 vcd_recorder_stop();
1661 /* 3. Cancel engine */
1662 int ret = vcd_engine_recognize_cancel();
1664 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Fail to cancel : %d", ret);
1667 if (false == vcd_client_manager_get_exclusive()) {
1668 if (1 == vcd_config_get_command_type_enabled(VC_COMMAND_TYPE_WIDGET)) {
1669 int pid = vcd_client_widget_get_foreground_pid();
1671 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Request tooltip hide");
1672 ecore_timer_add(0, __vcd_request_show_tooltip, (void*)false);
1676 vcd_client_manager_set_exclusive(false);
1680 vcd_config_set_service_state(VCD_STATE_READY);
1681 vcdc_send_service_state(VCD_STATE_READY);
1683 return VCD_ERROR_NONE;
1687 int vcd_server_mgr_result_select()
1689 __vcd_send_selected_result(NULL);
1691 return VCD_ERROR_NONE;
1694 int vcd_server_mgr_set_domain(int pid, const char* domain)
1696 /* check if pid is valid */
1697 if (false == vcd_client_manager_is_valid(pid)) {
1698 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] The manager pid(%d) is NOT valid", pid);
1699 return VCD_ERROR_INVALID_PARAMETER;
1702 vcd_state_e state = vcd_config_get_service_state();
1703 if (VCD_STATE_READY != state) {
1704 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Current state is not ready");
1705 return VCD_ERROR_INVALID_STATE;
1708 /* Set domain to engine */
1709 int ret = vcd_engine_set_domain(pid, domain);
1711 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to set domain : %d", ret);
1713 SLOG(LOG_DEBUG, TAG_VCD, "[Server SUCCESS] Set domain");
1719 int vcd_server_mgr_set_private_data(int pid, const char* key, const char* data)
1721 /* check if pid is valid */
1722 if (false == vcd_client_manager_is_valid(pid)) {
1723 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] The manager pid(%d) is NOT valid", pid);
1724 return VCD_ERROR_INVALID_PARAMETER;
1727 vcd_state_e state = vcd_config_get_service_state();
1728 if (VCD_STATE_READY != state) {
1729 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Current state is not ready");
1730 return VCD_ERROR_INVALID_STATE;
1733 /* Set private data to engine */
1734 int ret = vcd_engine_set_private_data(pid, key, data);
1736 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to set private data : %d", ret);
1738 SLOG(LOG_DEBUG, TAG_VCD, "[Server SUCCESS] Set private data");
1744 int vcd_server_mgr_get_private_data(int pid, const char* key, char** data)
1746 /* check if pid is valid */
1747 if (false == vcd_client_manager_is_valid(pid)) {
1748 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] The manager pid(%d) is NOT valid", pid);
1749 return VCD_ERROR_INVALID_PARAMETER;
1751 vcd_state_e state = vcd_config_get_service_state();
1752 if (VCD_STATE_READY != state) {
1753 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Current state is not ready");
1754 return VCD_ERROR_INVALID_STATE;
1757 /* Get private data to engine */
1758 int ret = vcd_engine_get_private_data(pid, key, data);
1760 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to get private data : %d", ret);
1762 SLOG(LOG_DEBUG, TAG_VCD, "[Server SUCCESS] Set private data");
1768 int vcd_server_mgr_do_action(int pid, int type, const char* action)
1772 /* check if pid is valid */
1773 if (false == vcd_client_manager_is_valid(pid)) {
1774 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] The manager pid(%d) is NOT valid", pid);
1775 return VCD_ERROR_INVALID_PARAMETER;
1778 vcd_state_e state = vcd_config_get_service_state();
1779 if (VCD_STATE_READY != state) {
1780 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Current state is not ready");
1781 return VCD_ERROR_INVALID_STATE;
1784 /* Reqeust do action to engine */
1785 if (VCD_SEND_EVENT_TYPE_TEXT == type)
1786 ret = vcd_engine_process_text(pid, action);
1787 else if (VCD_SEND_EVENT_TYPE_LIST_EVENT == type)
1788 ret = vcd_engine_process_list_event(pid, action);
1789 else if (VCD_SEND_EVENT_TYPE_HAPTIC_EVENT == type)
1790 ret = vcd_engine_process_haptic_event(pid, action);
1793 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to process do action : %d", ret);
1795 vcd_config_set_service_state(VCD_STATE_PROCESSING);
1796 vcdc_send_service_state(VCD_STATE_PROCESSING);
1797 SLOG(LOG_DEBUG, TAG_VCD, "[Server SUCCESS] Process do action");
1803 int vcd_server_mgr_enable_command_type(int pid, int cmd_type)
1807 /* check if pid is valid */
1808 if (false == vcd_client_manager_is_valid(pid)) {
1809 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] The manager pid(%d) is NOT valid", pid);
1810 return VCD_ERROR_INVALID_PARAMETER;
1813 vcd_state_e state = vcd_config_get_service_state();
1814 if (VCD_STATE_READY != state) {
1815 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Current state is not ready");
1816 return VCD_ERROR_INVALID_STATE;
1819 ret = vcd_config_enable_command_type(cmd_type);
1821 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to enable command type");
1823 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Enable command type(%d)", cmd_type);
1829 int vcd_server_mgr_disable_command_type(int pid, int cmd_type)
1833 /* check if pid is valid */
1834 if (false == vcd_client_manager_is_valid(pid)) {
1835 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] The manager pid(%d) is NOT valid", pid);
1836 return VCD_ERROR_INVALID_PARAMETER;
1839 vcd_state_e state = vcd_config_get_service_state();
1840 if (VCD_STATE_READY != state) {
1841 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Current state is not ready");
1842 return VCD_ERROR_INVALID_STATE;
1845 ret = vcd_config_disable_command_type(cmd_type);
1847 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to disable command type");
1849 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Disable command type(%d)", cmd_type);
1856 * VC Server Functions for Client
1858 int vcd_server_initialize(int pid)
1860 if (false == g_is_engine) {
1861 if (0 != vcd_engine_agent_initialize_current_engine()) {
1862 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] No Engine");
1863 g_is_engine = false;
1864 return VCD_ERROR_ENGINE_NOT_FOUND;
1870 if (false == vcd_engine_is_available_engine()) {
1871 if (0 != vcd_engine_agent_initialize_current_engine()) {
1872 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] No Engine");
1873 return VCD_ERROR_ENGINE_NOT_FOUND;
1877 /* check if pid is valid */
1878 if (true == vcd_client_is_available(pid)) {
1879 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] The pid is already exist");
1880 return VCD_ERROR_INVALID_PARAMETER;
1883 /* Add client information to client manager */
1884 if (0 != vcd_client_add(pid)) {
1885 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to add client info");
1886 return VCD_ERROR_OPERATION_FAILED;
1889 SLOG(LOG_DEBUG, TAG_VCD, "[Server Success] Client Initialize : pid(%d)", pid);
1891 return VCD_ERROR_NONE;
1894 int vcd_server_finalize(int pid)
1896 /* check if pid is valid */
1897 if (false == vcd_client_is_available(pid)) {
1898 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] pid is NOT valid ");
1899 return VCD_ERROR_INVALID_PARAMETER;
1902 /* Remove client information */
1903 if (0 != vcd_client_delete(pid)) {
1904 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Fail to delete client");
1907 if (0 == vcd_client_get_ref_count()) {
1908 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Connected client list is empty");
1909 ecore_timer_add(0, __finalize_quit_ecore_loop, NULL);
1912 SLOG(LOG_DEBUG, TAG_VCD, "[Server Success] Client Finalize : pid(%d)", pid);
1914 return VCD_ERROR_NONE;
1917 int vcd_server_set_command(int pid, vc_cmd_type_e cmd_type)
1919 /* check if pid is valid */
1920 if (false == vcd_client_is_available(pid)) {
1921 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] pid is NOT valid ");
1922 return VCD_ERROR_INVALID_PARAMETER;
1925 if (0 != vcd_client_set_command_type(pid, cmd_type)) {
1926 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to set command type : pid(%d), cmd_type(%d)", pid, cmd_type);
1927 return VCD_ERROR_OPERATION_FAILED;
1933 int vcd_server_unset_command(int pid, vc_cmd_type_e cmd_type)
1935 /* check if pid is valid */
1936 if (false == vcd_client_is_available(pid)) {
1937 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] pid is NOT valid ");
1938 return VCD_ERROR_INVALID_PARAMETER;
1941 if (0 != vcd_client_unset_command_type(pid, cmd_type)) {
1942 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to unset command type : pid(%d), cmd_type(%d)", pid, cmd_type);
1943 return VCD_ERROR_OPERATION_FAILED;
1949 int vcd_server_set_foreground(int pid, bool value)
1951 /* check if pid is valid */
1952 if (false == vcd_client_is_available(pid) && false == vcd_client_widget_is_available(pid)) {
1953 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] pid is NOT valid ");
1954 return VCD_ERROR_INVALID_PARAMETER;
1957 if (0 != vcd_config_set_foreground(pid, value)) {
1958 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to set foreground : pid(%d), value(%d)", pid, value);
1959 return VCD_ERROR_OPERATION_FAILED;
1965 static int __vcd_server_launch_manager_app()
1968 bool running = false;
1971 ret = vcd_client_manager_get_appid(&appid);
1972 if (0 != ret || NULL == appid) {
1973 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Fail to get manager appid");
1974 return VCD_ERROR_OPERATION_FAILED;
1976 ret = app_manager_is_running(appid, &running);
1978 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Fail to check running with appid(%s)", appid);
1981 return VCD_ERROR_OPERATION_FAILED;
1983 if (false == running) {
1984 int tmp_ret = __vcd_is_package_installed(appid);
1985 if (false == tmp_ret) {
1986 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] manager app is not installed, appid(%s)", appid);
1988 ret = __vcd_activate_app_by_appcontrol(appid);
1990 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Fail to activate app");
1995 SLOG(LOG_ERROR, TAG_VCD, "Launch vc manager app: appid(%s)", appid);
1998 ret = __vcd_resume_app(appid);
2000 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Fail to resume app");
2005 SLOG(LOG_ERROR, TAG_VCD, "Resume vc manager app: appid(%s)", appid);
2011 return VCD_ERROR_NONE;
2014 int vcd_server_dialog(int pid, const char* disp_text, const char* utt_text, int continuous)
2016 /* check if pid is valid */
2017 if (false == vcd_client_is_available(pid) && false == vcd_client_widget_is_available(pid)) {
2018 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] pid is NOT valid ");
2019 return VCD_ERROR_INVALID_PARAMETER;
2022 int ret = __vcd_server_launch_manager_app();
2024 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);
2028 ret = vcdc_send_dialog(vcd_client_manager_get_pid(), pid, disp_text, utt_text, continuous);
2030 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 int vcd_server_is_system_command_valid(int pid, int* is_sys_cmd_valid)
2039 /* check if pid is valid */
2040 if (false == vcd_client_is_available(pid) && false == vcd_client_widget_is_available(pid)) {
2041 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] pid is NOT valid ");
2042 return VCD_ERROR_INVALID_PARAMETER;
2045 /* check if system command is valid */
2046 if (true == vcd_client_manager_is_system_command_valid(vcd_client_manager_get_pid()))
2047 *is_sys_cmd_valid = true;
2049 *is_sys_cmd_valid = false;
2055 int vcd_server_set_exclusive_command(int pid, bool value)
2057 /* check if pid is valid */
2058 if (false == vcd_client_is_available(pid)) {
2059 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] pid is NOT valid ");
2060 return VCD_ERROR_INVALID_PARAMETER;
2063 if (true == value) {
2064 if (0 != vcd_client_set_exclusive_command(pid)) {
2065 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to set exclusive command : pid(%d)", pid);
2066 return VCD_ERROR_OPERATION_FAILED;
2069 if (0 != vcd_client_unset_exclusive_command(pid)) {
2070 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to unset exclusive command : pid(%d)", pid);
2071 return VCD_ERROR_OPERATION_FAILED;
2078 int vcd_server_request_start(int pid, bool stop_by_silence)
2080 /* check if pid is valid */
2081 if (false == vcd_client_is_available(pid)) {
2082 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] pid(%d) is NOT forground client", pid);
2083 return VCD_ERROR_INVALID_PARAMETER;
2087 /* Check current state */
2088 vcd_state_e state = vcd_config_get_service_state();
2090 /* Service state should be ready */
2091 if (VCD_STATE_READY != state) {
2092 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Current state is not Ready : pid(%d)", pid);
2093 return VCD_ERROR_INVALID_STATE;
2096 if (-1 != vcd_client_manager_get_pid()) {
2097 /* Check current pid is valid */
2098 if (false == vcd_client_manager_check_demandable_client(pid)) {
2099 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Current client is NOT available : pid(%d)", pid);
2100 return VCD_ERROR_INVALID_PARAMETER;
2104 ret = vcd_server_mgr_start(stop_by_silence, false);
2106 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Fail to start recognition");
2107 return VCD_ERROR_INVALID_PARAMETER;
2113 int vcd_server_request_stop(int pid)
2116 /* Check current state */
2117 vcd_state_e state = vcd_config_get_service_state();
2119 /* Service state should be ready */
2120 if (VCD_STATE_RECORDING != state) {
2121 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Current state is not Recording : pid(%d)", pid);
2122 return VCD_ERROR_INVALID_STATE;
2125 if (-1 != vcd_client_manager_get_pid()) {
2126 /* Check current pid is valid */
2127 if (false == vcd_client_manager_check_demandable_client(pid)) {
2128 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Current client is NOT available : pid(%d)", pid);
2129 return VCD_ERROR_INVALID_PARAMETER;
2133 ret = vcd_server_mgr_stop();
2135 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Fail to start recognition");
2136 return VCD_ERROR_OPERATION_FAILED;
2139 return VCD_ERROR_NONE;
2142 int vcd_server_request_cancel(int pid)
2145 /* Check current state */
2146 vcd_state_e state = vcd_config_get_service_state();
2148 /* Service state should be recording or processing */
2149 if (VCD_STATE_RECORDING != state && VCD_STATE_PROCESSING != state) {
2150 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Current state is not Recording or Processing : pid(%d)", pid);
2151 return VCD_ERROR_INVALID_STATE;
2154 if (-1 != vcd_client_manager_get_pid()) {
2155 /* Check current pid is valid */
2156 if (false == vcd_client_manager_check_demandable_client(pid)) {
2157 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Current client is NOT available : pid(%d)", pid);
2158 return VCD_ERROR_INVALID_PARAMETER;
2162 ret = vcd_server_mgr_cancel();
2164 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Fail to start recognition");
2165 return VCD_ERROR_OPERATION_FAILED;
2168 return VCD_ERROR_NONE;
2173 * VC Server Functions for Widget lib
2175 int vcd_server_widget_initialize(int pid)
2177 if (false == g_is_engine) {
2178 if (0 != vcd_engine_agent_initialize_current_engine()) {
2179 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] No Engine");
2180 g_is_engine = false;
2181 return VCD_ERROR_ENGINE_NOT_FOUND;
2187 if (false == vcd_engine_is_available_engine()) {
2188 if (0 != vcd_engine_agent_initialize_current_engine()) {
2189 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] No Engine");
2190 return VCD_ERROR_ENGINE_NOT_FOUND;
2194 /* check if pid is valid */
2195 if (true == vcd_client_widget_is_available(pid)) {
2196 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] The pid is already exist");
2197 return VCD_ERROR_NONE;
2200 /* Add client information to client manager */
2201 if (0 != vcd_client_widget_add(pid)) {
2202 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to add client info");
2203 return VCD_ERROR_OPERATION_FAILED;
2206 SLOG(LOG_DEBUG, TAG_VCD, "[Server Success] Initialize widget : pid(%d)", pid);
2208 return VCD_ERROR_NONE;
2211 int vcd_server_widget_finalize(int pid)
2213 /* check if pid is valid */
2214 if (false == vcd_client_widget_is_available(pid)) {
2215 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] pid is NOT valid ");
2216 return VCD_ERROR_INVALID_PARAMETER;
2219 /* Remove client information */
2220 if (0 != vcd_client_widget_delete(pid)) {
2221 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Fail to delete client");
2224 if (0 == vcd_client_get_ref_count()) {
2225 SLOG(LOG_DEBUG, TAG_VCD, "[Server] connected client list is empty");
2226 ecore_timer_add(0, __finalize_quit_ecore_loop, NULL);
2229 return VCD_ERROR_NONE;
2232 int vcd_server_widget_start_recording(int pid, bool widget_command)
2234 /* check if pid is valid */
2235 if (false == vcd_client_widget_is_available(pid)) {
2236 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] pid is NOT valid ");
2237 return VCD_ERROR_INVALID_PARAMETER;
2240 if (true == widget_command) {
2241 vcd_client_widget_set_command(pid);
2242 SLOG(LOG_DEBUG, TAG_VCD, "[Server] widget command is available");
2244 vcd_client_widget_unset_command(pid);
2245 SLOG(LOG_WARN, TAG_VCD, "[Server] widget command is NOT available");
2248 int ret = __start_internal_recognition();
2250 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to start recongition : %d", ret);
2251 ecore_timer_add(0, __vcd_request_show_tooltip, (void*)false);
2257 int vcd_server_widget_start(int pid, bool stop_by_silence)
2259 /* check if pid is valid */
2260 int fore_pid = vcd_client_widget_get_foreground_pid();
2261 if (pid != fore_pid) {
2262 SLOG(LOG_ERROR, TAG_VCD, "[Server] pid is NOT foreground");
2263 return VCD_ERROR_INVALID_PARAMETER;
2266 /* Check current state */
2267 vcd_state_e state = vcd_config_get_service_state();
2269 /* Service state should be ready */
2270 if (VCD_STATE_READY != state) {
2271 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Current state is not Ready : pid(%d)", pid);
2272 return VCD_ERROR_INVALID_STATE;
2275 vcd_client_set_slience_detection(stop_by_silence);
2277 /* Notify show tooltip */
2278 ecore_timer_add(0, __vcd_request_show_tooltip, (void*)true);
2283 int vcd_server_widget_stop(int pid)
2285 /* check if pid is valid */
2286 int fore_pid = vcd_client_widget_get_foreground_pid();
2287 if (pid != fore_pid) {
2288 SLOG(LOG_ERROR, TAG_VCD, "[Server] pid is NOT foreground");
2289 return VCD_ERROR_INVALID_PARAMETER;
2293 /* Check current state */
2294 vcd_state_e state = vcd_config_get_service_state();
2296 /* Service state should be recording */
2297 if (VCD_STATE_RECORDING != state) {
2298 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Current service state is not Recording : pid(%d)", pid);
2299 return VCD_ERROR_INVALID_STATE;
2302 ret = vcd_server_mgr_stop();
2304 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Fail to start recognition");
2305 return VCD_ERROR_OPERATION_FAILED;
2308 return VCD_ERROR_NONE;
2311 int vcd_server_widget_cancel(int pid)
2313 /* check if pid is valid */
2314 int fore_pid = vcd_client_widget_get_foreground_pid();
2315 if (pid != fore_pid) {
2316 SLOG(LOG_ERROR, TAG_VCD, "[Server] pid is NOT foreground");
2317 return VCD_ERROR_INVALID_PARAMETER;
2321 /* Check current state */
2322 vcd_state_e state = vcd_config_get_service_state();
2324 /* Service state should be recording or processing */
2325 if (VCD_STATE_RECORDING != state && VCD_STATE_PROCESSING != state) {
2326 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Current state is not Recording or Processing : pid(%d)", pid);
2327 return VCD_ERROR_INVALID_STATE;
2330 ret = vcd_server_mgr_cancel();
2332 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Fail to cancel recognition : %d", ret);
2336 return VCD_ERROR_NONE;
2339 int vcd_server_widget_enable_asr_result(int pid, bool enable)
2342 ret = vcd_client_widget_set_asr_result_enabled(pid, enable);
2344 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Fail to enable asr result : %d", ret);