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.
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"
36 * VC Server static variable
38 static bool g_is_engine;
40 static GList *g_proc_list = NULL;
42 static Ecore_Timer *g_restart_timer = NULL;
45 * @brief Enumerations of send event type.
48 VCD_SEND_EVENT_TYPE_TEXT, /**< send text event to vc engine*/
49 VCD_SEND_EVENT_TYPE_LIST_EVENT, /**< send list event to vc engine */
50 VCD_SEND_EVENT_TYPE_HAPTIC_EVENT /**< send haptic event to vc engine */
51 } vcd_send_event_type_e;
54 * VC Server Internal Functions
56 static Eina_Bool __stop_by_silence(void *data)
58 SLOG(LOG_DEBUG, TAG_VCD, "===== Silence Detected ");
60 vcd_server_mgr_stop();
62 SLOG(LOG_DEBUG, TAG_VCD, "=====");
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, "=====");
74 SLOG(LOG_DEBUG, TAG_VCD, " ");
78 static void __cancel_by_error(void *data)
80 SLOG(LOG_DEBUG, TAG_VCD, "===== Cancel by error");
82 vcd_server_mgr_cancel();
84 SLOG(LOG_DEBUG, TAG_VCD, "=====");
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, "=====");
112 SLOG(LOG_DEBUG, TAG_VCD, " ");
116 static int __server_recorder_callback(const void* data, const unsigned int length)
118 vcd_state_e state = vcd_config_get_service_state();
119 if (VCD_STATE_READY == state) {
120 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Ready state, but recording");
121 } else if (VCD_STATE_PROCESSING == state) {
125 vcp_speech_detect_e speech_detected = VCP_SPEECH_DETECT_NONE;
128 ret = vcd_engine_recognize_audio(data, length, &speech_detected);
132 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to set recording data to engine(%d)", ret);
133 ecore_timer_add(0, __cancel_by_interrupt, NULL);
134 /* Send error cb to manager */
135 if (VCP_ERROR_OUT_OF_NETWORK == ret) {
136 vcdc_send_error_signal_to_manager(vcd_client_manager_get_pid(), VCD_ERROR_TIMED_OUT, "Engine connection failed");
138 vcdc_send_error_signal_to_manager(vcd_client_manager_get_pid(), VCD_ERROR_OPERATION_FAILED, "Engine recognition failed");
143 if (VCP_SPEECH_DETECT_BEGIN == speech_detected) {
144 if (-1 != vcd_client_manager_get_pid()) {
145 /* Manager client is available */
146 if (0 != vcdc_send_speech_detected(vcd_client_manager_get_pid())) {
147 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Fail to send speech detected");
150 } else if (VCP_SPEECH_DETECT_END == speech_detected) {
151 if (VCD_RECOGNITION_MODE_STOP_BY_SILENCE == vcd_client_get_recognition_mode()) {
152 /* silence detected */
153 ecore_timer_add(0, __stop_by_silence, NULL);
154 } else if (VCD_RECOGNITION_MODE_RESTART_AFTER_REJECT == vcd_client_get_recognition_mode()) {
155 /* Stop engine recognition */
156 int ret = vcd_engine_recognize_stop();
158 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to stop recognition : %d", ret);
160 vcd_config_set_service_state(VCD_STATE_PROCESSING);
161 vcdc_send_service_state(VCD_STATE_PROCESSING);
163 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Stop engine only by silence");
164 } else if (VCD_RECOGNITION_MODE_RESTART_CONTINUOUSLY == vcd_client_get_recognition_mode()) {
165 /* Stop engine recognition */
166 int ret = vcd_engine_recognize_stop();
168 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to stop recognition : %d", ret);
176 void __server_recorder_interrupt_callback()
178 SLOG(LOG_DEBUG, TAG_VCD, "===== Cancel by sound interrupt");
180 ecore_timer_add(0, __cancel_by_interrupt, NULL);
182 SLOG(LOG_DEBUG, TAG_VCD, "=====");
183 SLOG(LOG_DEBUG, TAG_VCD, " ");
186 static void __config_lang_changed_cb(const char* current_lang, void* user_data)
188 SLOG(LOG_DEBUG, TAG_VCD, "===== Change language ");
190 /* Current state is recording */
191 vcd_state_e state = vcd_config_get_service_state();
192 if (VCD_STATE_RECORDING == state || VCD_STATE_PROCESSING == state) {
193 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Current state is 'Recording'. Cancel recognition");
194 vcd_server_mgr_cancel();
198 ret = vcd_engine_set_current_language(current_lang);
200 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to set language of engine : %d", ret);
203 SLOG(LOG_DEBUG, TAG_VCD, "=====");
204 SLOG(LOG_DEBUG, TAG_VCD, " ");
209 static void __config_foreground_changed_cb(int previous, int current, void* user_data)
211 SLOG(LOG_DEBUG, TAG_VCD, "===== Change foreground");
213 SLOG(LOG_DEBUG, TAG_VCD, "Foreground pid(%d)", current);
215 if (VC_NO_FOREGROUND_PID != current) {
216 /* Foreground app is changed */
217 vcd_state_e state = vcd_config_get_service_state();
218 if (VCD_STATE_RECORDING == state) {
219 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Foreground pid(%d) is changed. Cancel recognition", current);
220 ecore_timer_add(0, __cancel_by_interrupt, NULL);
224 SLOG(LOG_DEBUG, TAG_VCD, "=====");
225 SLOG(LOG_DEBUG, TAG_VCD, " ");
230 static int __vcd_activate_app_by_appcontrol(const char* appid)
233 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Invalid parameter");
234 return VCD_ERROR_INVALID_PARAMETER;
238 app_control_h app_control = NULL;
239 ret = app_control_create(&app_control);
240 if (APP_CONTROL_ERROR_NONE == ret) {
242 ret = app_control_add_extra_data(app_control, "voice_launch", "get_result");
243 if (APP_CONTROL_ERROR_NONE != ret) {
244 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Fail to add extra data, ret(%d)", ret);
245 app_control_destroy(app_control);
246 return VCD_ERROR_OPERATION_FAILED;
249 ret = app_control_set_app_id(app_control, appid);
250 if (APP_CONTROL_ERROR_NONE != ret) {
251 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Fail to set app id, ret(%d)", ret);
252 app_control_destroy(app_control);
253 return VCD_ERROR_OPERATION_FAILED;
255 // Sent launch request
256 ret = app_control_send_launch_request(app_control, NULL, NULL);
257 if (APP_CONTROL_ERROR_NONE != ret) {
258 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Fail to send launch request, ret(%d)", ret);
259 app_control_destroy(app_control);
260 return VCD_ERROR_OPERATION_FAILED;
262 // Destroy app control
263 ret = app_control_destroy(app_control);
264 if (APP_CONTROL_ERROR_NONE != ret) {
265 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Fail to destroy, ret(%d)", ret);
266 return VCD_ERROR_OPERATION_FAILED;
269 return VCD_ERROR_NONE;
272 static int __vcd_resume_app(const char* appid)
274 app_context_h app_context = NULL;
275 int ret = app_manager_get_app_context(appid, &app_context);
276 if (APP_MANAGER_ERROR_NONE != ret) {
277 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Fail to get app_context, ret(%d), appid(%s)", ret, appid);
278 return VCD_ERROR_OPERATION_FAILED;
281 ret = app_manager_resume_app(app_context);
282 if (APP_MANAGER_ERROR_NONE != ret) {
283 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Fail to resume app, ret(%d), appid(%s)", ret, appid);
284 return VCD_ERROR_OPERATION_FAILED;
286 return VCD_ERROR_NONE;
289 static bool __vcd_is_package_installed(const char* appid)
291 app_info_h app_info = NULL;
292 int ret = app_manager_get_app_info(appid, &app_info);
293 if (APP_MANAGER_ERROR_NONE != ret || NULL == app_info)
295 ret = app_info_destroy(app_info);
296 if (APP_MANAGER_ERROR_NONE != ret)
297 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Fail to destroy app_info, ret(%d)", ret);
301 static bool __vcd_launch_app(const char* result)
303 if (NULL == result) {
304 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Invalid parameter");
305 return VCD_ERROR_INVALID_PARAMETER;
308 GSList* app_list = NULL;
309 if (0 != vc_db_get_appid_list(result, &app_list)) {
310 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] result text is NULL");
311 return VCD_ERROR_INVALID_PARAMETER;
314 if (0 != g_slist_length(app_list)) {
317 vc_deactivated_app_s* temp_app = NULL;
318 iter = g_slist_nth(app_list, 0);
320 while (NULL != iter) {
321 temp_app = iter->data;
323 if (NULL != temp_app) {
324 if (NULL != temp_app->appid) {
326 bool running = false;
327 ret = app_manager_is_running(temp_app->appid, &running);
328 if (APP_MANAGER_ERROR_NONE != ret) {
329 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Fail to check running with appid(%s)", temp_app->appid);
330 free(temp_app->appid);
331 temp_app->appid = NULL;
334 return VCD_ERROR_OPERATION_FAILED;
336 if (false == running) {
337 int tmp_ret = __vcd_is_package_installed(temp_app->appid);
338 if (false == tmp_ret) {
339 SLOG(LOG_WARN, TAG_VCD, "[WARNING] app is not installed, appid(%s)", temp_app->appid);
341 ret = __vcd_activate_app_by_appcontrol(temp_app->appid);
343 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Fail to activate app");
344 free(temp_app->appid);
345 temp_app->appid = NULL;
350 SLOG(LOG_ERROR, TAG_VCD, "Launch app: appid(%s) result(%s)", temp_app->appid, result);
353 ret = __vcd_resume_app(temp_app->appid);
355 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Fail to resume app");
356 free(temp_app->appid);
357 temp_app->appid = NULL;
362 SLOG(LOG_ERROR, TAG_VCD, "Resume app: appid(%s) result(%s)", temp_app->appid, result);
364 free(temp_app->appid);
365 temp_app->appid = NULL;
370 iter = g_slist_next(iter);
375 return VCD_ERROR_NONE;
378 static Eina_Bool __vcd_send_selected_result(void *data)
380 GSList* pid_list = NULL;
381 const char* result = vcd_client_manager_get_result_text();
383 if (0 != vc_info_parser_get_result_pid_list(&pid_list, result)) {
384 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Fail to get pid list. No result");
386 if (0 < g_slist_length(pid_list)) {
388 vc_cmd_s* temp_cmd = NULL;
391 iter = g_slist_nth(pid_list, 0);
392 while (NULL != iter) {
393 temp_cmd = iter->data;
395 if (NULL != temp_cmd) {
396 /* Launch deactivated several apps that is matched with result */
397 ret = __vcd_launch_app(result);
399 SLOG(LOG_ERROR, TAG_VCD, "Fail to launch or resume app, ret(%d) result(%s)", ret, result);
401 /* send result noti */
402 ret = vcdc_send_result(temp_cmd->pid, vcd_client_manager_get_pid(), temp_cmd->type);
404 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Fail to send result, ret(%d)", ret);
407 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Send result : pid(%d) type(%d)", temp_cmd->pid, 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* __recorder_stop(void *data)
444 static void __vcd_server_result_cb(vcp_result_event_e event, int* result_id, int count, const char* all_result,
445 const char* non_fixed_result, const char* nlu_result, const char* msg, void *user_data)
447 if (VCD_STATE_PROCESSING != vcd_config_get_service_state()) {
448 if (VCD_RECOGNITION_MODE_RESTART_CONTINUOUSLY != vcd_client_get_recognition_mode()) {
449 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Current state is not 'Processing' and mode is not 'Restart continuously'");
454 vc_info_parser_unset_result(vcd_client_manager_get_exclusive());
455 vcd_client_manager_set_result_text(all_result);
457 SLOG(LOG_INFO, TAG_VCD, "[Server] Event(%d), Text(%s) Nonfixed(%s) Msg(%s) Result count(%d)",
458 event, all_result, non_fixed_result, msg, count);
460 if (VCD_RECOGNITION_MODE_RESTART_AFTER_REJECT == vcd_client_get_recognition_mode()) {
461 if (VCP_RESULT_EVENT_REJECTED == event) {
462 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Restart by no or rejected result");
463 /* If no result and restart option is ON */
464 /* Send reject message */
465 bool temp = vcd_client_manager_get_exclusive();
466 vc_info_parser_set_result(all_result, event, msg, NULL, temp);
467 if (0 != vcdc_send_result_to_manager(vcd_client_manager_get_pid(), VC_RESULT_TYPE_NOTIFICATION)) {
468 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Fail to send result");
471 g_restart_timer = ecore_timer_add(0, __restart_engine, NULL);
474 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Stop recorder due to success");
475 //vcd_recorder_stop();
476 ecore_main_loop_thread_safe_call_sync(__recorder_stop, NULL);
477 } else if (VCD_RECOGNITION_MODE_RESTART_CONTINUOUSLY == vcd_client_get_recognition_mode()) {
478 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Restart continuously");
479 /* Restart option is ON */
480 g_restart_timer = ecore_timer_add(0, __restart_engine, NULL);
481 if (VCP_RESULT_EVENT_REJECTED == event) {
482 bool temp = vcd_client_manager_get_exclusive();
483 vc_info_parser_set_result(all_result, event, msg, NULL, temp);
484 if (0 != vcdc_send_result_to_manager(vcd_client_manager_get_pid(), VC_RESULT_TYPE_NOTIFICATION)) {
485 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Fail to send result");
492 int pid = vcd_client_widget_get_foreground_pid();
494 if (NULL != all_result) {
495 vc_info_parser_set_result(all_result, event, msg, NULL, false);
497 vcd_client_widget_get_asr_result_enabled(pid, &enable);
498 if (true == enable) {
499 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Send ASR result to Widget client");
500 bool is_consumed = false;
501 if (0 != vcdc_send_asr_result(pid, VC_COMMAND_TYPE_WIDGET, &is_consumed)) {
502 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to send asr result");
504 SLOG(LOG_DEBUG, TAG_VCD, "[Server] ASR result is consumed(%d)", is_consumed);
505 if (true == is_consumed) {
506 vcdc_send_show_tooltip(pid, false);
507 if (-1 != vcd_client_manager_get_pid()) {
508 /* Manager client is available */
509 if (0 != vcdc_send_result_to_manager(vcd_client_manager_get_pid(), VC_RESULT_TYPE_NOTIFICATION)) {
510 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Fail to send result");
514 vcd_client_manager_set_exclusive(false);
522 /* if nlu_result is exist, Add command handle(is_action) into result list */
524 SLOG(LOG_DEBUG, TAG_VCD, "[Server] === Get engine result ===");
527 vc_cmd_s* temp_cmd = NULL;
528 vc_cmd_list_h vc_cmd_list = NULL;
530 if (0 != vc_cmd_list_create(&vc_cmd_list)) {
531 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Fail to create command list");
532 vcd_client_manager_set_exclusive(false);
533 vcd_config_set_service_state(VCD_STATE_READY);
534 vcdc_send_service_state(VCD_STATE_READY);
538 /* priority filter */
539 /* system > exclusive > foreground = widget > system_background > background */
541 int* filtered_id = (int*)calloc(count, sizeof(int));
542 int filtered_count = 0;
543 int top_priority = VC_COMMAND_PRIORITY_BACKGROUND;
544 for (i = 0; i < count; i++) {
545 SLOG(LOG_INFO, TAG_VCD, "[Server] [%d] Result id(%d)", i, result_id[i]);
547 if (0 > result_id[i]) {
548 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Result ID(%d) is NOT valid", result_id[i]);
552 ret = vcd_client_get_cmd_from_result_id(result_id[i], &temp_cmd);
553 if (0 == ret && NULL != temp_cmd) {
554 if (top_priority == temp_cmd->priority) {
555 filtered_id[filtered_count] = result_id[i];
557 } else if (top_priority < temp_cmd->priority) {
560 filtered_id[0] = result_id[i];
562 top_priority = temp_cmd->priority;
568 for (i = 0; i < filtered_count; i++) {
569 SLOG(LOG_INFO, TAG_VCD, "[Server] [%d] Filtered Result id(%d)", i, filtered_id[i]);
571 if (filtered_id[i] < 0) {
572 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Filtered ID(%d) is NOT valid", filtered_id[i]);
576 ret = vcd_client_get_cmd_from_result_id(filtered_id[i], &temp_cmd);
577 if (0 == ret && NULL != temp_cmd) {
578 switch (temp_cmd->format) {
579 case VC_CMD_FORMAT_FIXED:
580 case VC_CMD_FORMAT_FIXED_AND_VFIXED:
581 case VC_CMD_FORMAT_VFIXED_AND_FIXED:
582 case VC_CMD_FORMAT_PARTIAL:
583 case VC_CMD_FORMAT_FIXED_AND_NONFIXED:
584 case VC_CMD_FORMAT_NONFIXED_AND_FIXED:
586 case VC_CMD_FORMAT_ACTION:
590 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Unknown command type : %d", temp_cmd->type);
594 if (0 != vc_cmd_list_add(vc_cmd_list, (vc_cmd_h)temp_cmd)) {
595 SLOG(LOG_DEBUG, TAG_VCD, "Fail to add command to list");
596 vc_cmd_destroy((vc_cmd_h)temp_cmd);
599 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] NOT found matached result(%d)", filtered_id[i]);
603 if (NULL != filtered_id) {
608 if (NULL != nlu_result) {
609 SLOG(LOG_INFO, TAG_VCD, "[Server] NLU (%s)", nlu_result);
610 vc_info_parser_set_nlu_result(nlu_result);
611 if (0 == is_action) {
613 if (0 != vc_cmd_create(&nlu_cmd)) {
614 SLOG(LOG_ERROR, TAG_VCD, "Fail to nlu cmd create");
616 if (0 != vc_cmd_set_type(nlu_cmd, VC_COMMAND_TYPE_SYSTEM)) {
617 SLOG(LOG_ERROR, TAG_VCD, "Fail to set type");
619 if (0 != vc_cmd_set_pid(nlu_cmd, vcd_client_manager_get_pid())) {
620 SLOG(LOG_ERROR, TAG_VCD, "Fail to set pid");
622 if (0 != vc_cmd_set_format(nlu_cmd, VC_CMD_FORMAT_ACTION)) {
623 SLOG(LOG_ERROR, TAG_VCD, "Fail to set format");
625 if (0 != vc_cmd_list_add(vc_cmd_list, nlu_cmd)) {
626 SLOG(LOG_ERROR, TAG_VCD, "Fail to add nlu cmd to list");
627 vc_cmd_destroy(nlu_cmd);
633 vc_cmd_print_list(vc_cmd_list);
635 SLOG(LOG_DEBUG, TAG_VCD, "[Server] =========================");
637 int result_count = 0;
638 vc_cmd_list_get_count(vc_cmd_list, &result_count);
640 if (0 == result_count) {
642 if (NULL != all_result) {
643 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Engine result is no command : %s", all_result);
645 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Engine result is NULL");
647 bool temp = vcd_client_manager_get_exclusive();
648 vc_info_parser_set_result(all_result, event, msg, NULL, temp);
650 int pid = vcd_client_widget_get_foreground_pid();
652 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Request tooltip hide");
653 /* Send to hide tooltip */
654 vcdc_send_show_tooltip(pid, false);
657 if (-1 != vcd_client_manager_get_pid()) {
658 /* Manager client is available */
659 if (0 != vcdc_send_result_to_manager(vcd_client_manager_get_pid(), VC_RESULT_TYPE_NORMAL)) {
660 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Fail to send result");
664 vcd_client_manager_set_exclusive(false);
668 if (false == vcd_client_manager_get_exclusive()) {
669 int pid = vcd_client_widget_get_foreground_pid();
671 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Request tooltip hide");
672 vcdc_send_show_tooltip(pid, false);
675 vc_info_parser_set_result(all_result, event, msg, vc_cmd_list, false);
677 if (-1 != vcd_client_manager_get_pid()) {
678 /* Manager client is available */
679 if (0 != vcdc_send_result_to_manager(vcd_client_manager_get_pid(), VC_RESULT_TYPE_NORMAL)) {
680 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Fail to send result");
683 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Manager is NOT available. Send result to client directly");
684 /* Send result to client */
685 ecore_timer_add(0, __vcd_send_selected_result, NULL);
688 /* exclusive command */
689 vc_info_parser_set_result(all_result, event, msg, vc_cmd_list, true);
691 if (-1 != vcd_client_manager_get_pid()) {
692 /* Manager client is available */
693 if (0 != vcdc_send_result_to_manager(vcd_client_manager_get_pid(), VC_RESULT_TYPE_NORMAL)) {
694 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Fail to send result");
697 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Manager is NOT available");
700 vcd_client_manager_set_exclusive(false);
704 vc_cmd_list_destroy(vc_cmd_list, true);
710 if (NULL == result_id) {
712 if (NULL != all_result) {
713 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Engine result is no command : %s", all_result);
714 bool temp = vcd_client_manager_get_exclusive();
715 vc_info_parser_set_result(all_result, event, msg, NULL, temp);
718 int pid = vcd_client_widget_get_foreground_pid();
720 if (NULL != all_result) {
721 /* Send result text to widget */
722 vcdc_send_result(pid, vcd_client_manager_get_pid(), VC_COMMAND_TYPE_WIDGET);
725 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Request tooltip hide");
726 /* Send to hide tooltip */
727 vcdc_send_show_tooltip(pid, false);
730 if (-1 != vcd_client_manager_get_pid()) {
731 /* Manager client is available */
732 if (0 != vcdc_send_result_to_manager(vcd_client_manager_get_pid(), VC_RESULT_TYPE_NORMAL)) {
733 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Fail to send result");
737 vcd_client_manager_set_exclusive(false);
743 SLOG(LOG_DEBUG, TAG_VCD, "[Server] === Get engine result ===");
746 vc_cmd_s* temp_cmd = NULL;
747 vc_cmd_list_h vc_cmd_list = NULL;
749 if (0 != vc_cmd_list_create(&vc_cmd_list)) {
750 SLOG(LOG_ERROR, TAG_VCD, "[Server] Fail to create command list");
751 vcd_client_manager_set_exclusive(false);
752 vcd_config_set_service_state(VCD_STATE_READY);
753 vcdc_send_service_state(VCD_STATE_READY);
758 for (i = 0; i < count; i++) {
759 SLOG(LOG_DEBUG, TAG_VCD, "[Server] [%d] Result ID(%d)", i, result_id[i]);
761 if (result_id[i] < 0) {
762 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Result ID(%d) is NOT valid", result_id[i]);
766 ret = vcd_client_get_cmd_from_result_id(result_id[i], &temp_cmd);
767 if (0 == ret && NULL != temp_cmd) {
768 switch (temp_cmd->format) {
769 case VC_CMD_FORMAT_FIXED:
770 case VC_CMD_FORMAT_FIXED_AND_VFIXED:
771 case VC_CMD_FORMAT_VFIXED_AND_FIXED:
772 case VC_CMD_FORMAT_PARTIAL:
773 /* Nonfixed result is NOT valid */
775 case VC_CMD_FORMAT_FIXED_AND_NONFIXED:
776 if (NULL == temp_cmd->parameter) {
777 if (NULL != non_fixed_result) {
778 temp_cmd->parameter = strdup(non_fixed_result);
781 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Command type is NOT vaild. Parameter (%s)", temp_cmd->parameter);
784 case VC_CMD_FORMAT_NONFIXED_AND_FIXED:
785 if (NULL == temp_cmd->command) {
786 if (NULL != non_fixed_result) {
787 temp_cmd->command = strdup(non_fixed_result);
790 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Command type is NOT vaild. Command (%s)", temp_cmd->command);
795 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Unknown command type : %d", temp_cmd->type);
799 if (0 != vc_cmd_list_add(vc_cmd_list, (vc_cmd_h)temp_cmd)) {
800 SLOG(LOG_DEBUG, TAG_VCD, "Fail to add command to list");
801 vc_cmd_destroy((vc_cmd_h)temp_cmd);
804 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] NOT found matached result(%d)", result_id[i]);
808 vc_cmd_print_list(vc_cmd_list);
810 SLOG(LOG_DEBUG, TAG_VCD, "[Server] =========================");
812 int result_count = 0;
813 vc_cmd_list_get_count(vc_cmd_list, &result_count);
815 if (false == vcd_client_manager_get_exclusive()) {
816 int pid = vcd_client_widget_get_foreground_pid();
818 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Request tooltip hide");
819 vcdc_send_show_tooltip(pid, false);
822 vc_info_parser_set_result(all_result, event, msg, vc_cmd_list, false);
824 if (-1 != vcd_client_manager_get_pid()) {
825 /* Manager client is available */
826 if (0 != vcdc_send_result_to_manager(vcd_client_manager_get_pid(), VC_RESULT_TYPE_NORMAL)) {
827 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Fail to send result");
830 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Manager is NOT available. Send result to client directly");
831 /* Send result to client */
832 ecore_timer_add(0, __vcd_send_selected_result, NULL);
835 /* exclusive command */
836 vc_info_parser_set_result(all_result, event, msg, vc_cmd_list, true);
838 if (-1 != vcd_client_manager_get_pid()) {
839 /* Manager client is available */
840 if (0 != vcdc_send_result_to_manager(vcd_client_manager_get_pid(), VC_RESULT_TYPE_NORMAL)) {
841 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Fail to send result");
844 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Manager is NOT available");
847 vcd_client_manager_set_exclusive(false);
850 vc_cmd_list_destroy(vc_cmd_list, true);
857 static void __vcd_server_nlu_result_cb(vcp_result_event_e event, const char* nlu_result, void *user_data)
859 SLOG(LOG_DEBUG, TAG_VCD, "[Server] NLU result cb - event(%d)", event);
860 SLOG(LOG_DEBUG, TAG_VCD, "[Server] result (%s)", nlu_result);
862 int ret = vc_info_parser_set_nlu_result(nlu_result);
864 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to set nlu result (%d)", ret);
871 static void __vcd_server_error_cb(vcp_error_e error, const char* msg, void *user_data)
873 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Engine Error cb - reason(%d), msg(%s)", error, msg);
874 ecore_main_loop_thread_safe_call_async(__cancel_by_error, NULL);
876 char* error_msg = NULL;
877 error_msg = strdup(msg);
879 if (0 != vcdc_send_error_signal_to_manager(vcd_client_manager_get_pid(), error, error_msg)) {
880 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to send error signal");
883 if (NULL != error_msg) {
892 * vcd server Interfaces
894 static void __vcd_file_clean_up()
896 SLOG(LOG_DEBUG, TAG_VCD, "== Old file clean up == ");
899 struct dirent *dirp = NULL;
901 dp = opendir(VC_RUNTIME_INFO_ROOT);
903 SLOG(LOG_ERROR, TAG_VCD, "[File message WARN] Fail to open path : %s", VC_RUNTIME_INFO_ROOT);
907 char remove_path[256] = {0, };
912 if (!strncmp("vc_", dirp->d_name, strlen("vc_"))) {
913 memset(remove_path, 0, 256);
914 snprintf(remove_path, 256, "%s/%s", VC_RUNTIME_INFO_ROOT, dirp->d_name);
917 if (0 != remove(remove_path)) {
918 SLOG(LOG_WARN, TAG_VCD, "[File message WARN] Fail to remove file : %s", remove_path);
920 SLOG(LOG_DEBUG, TAG_VCD, "[File message] Remove file : %s", remove_path);
924 } while (NULL != dirp);
931 static int __vcd_db_clean_up()
934 int cnt = VC_COMMAND_TYPE_FOREGROUND;
936 if (VC_COMMAND_TYPE_BACKGROUND != cnt)
937 ret = vc_db_delete_commands(-1, cnt, NULL);
938 } while (VC_COMMAND_TYPE_EXCLUSIVE >= ++cnt);
947 /* Remove old file */
948 __vcd_file_clean_up();
950 /* initialize modules */
951 ret = vcd_config_initialize(__config_lang_changed_cb, __config_foreground_changed_cb, NULL);
953 SLOG(LOG_ERROR, TAG_VCD, "[Server WARNING] Fail to initialize config.");
957 ret = __vcd_db_clean_up();
959 SLOG(LOG_ERROR, TAG_VCD, "[Server WARNING] Fail to remove db data");
962 vcd_config_set_service_state(VCD_STATE_NONE);
964 //ret = vcd_engine_agent_init(__vcd_server_pre_result_cb, __vcd_server_result_cb, __vcd_server_nlu_result_cb, __vcd_server_error_cb);
965 ret = vcd_engine_agent_init(__vcd_server_asr_result_cb, __vcd_server_result_cb, __vcd_server_error_cb);
967 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to engine agent initialize : result(%d)", ret);
971 if (0 != vcd_recorder_create(__server_recorder_callback, __server_recorder_interrupt_callback)) {
972 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to create recorder");
973 return VCD_ERROR_OPERATION_FAILED;
977 ret = vcd_engine_agent_initialize_current_engine();
979 if (VCD_ERROR_ENGINE_NOT_FOUND == ret)
980 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] There is No Voice control engine");
982 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to init engine");
990 if (0 != vcd_engine_agent_load_current_engine()) {
991 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to load current engine");
992 return VCD_ERROR_OPERATION_FAILED;
995 /* Initialize manager info */
996 vcd_client_manager_unset();
998 vcd_config_set_service_state(VCD_STATE_READY);
999 vcdc_send_service_state(VCD_STATE_READY);
1001 SLOG(LOG_DEBUG, TAG_VCD, "[Server SUCCESS] initialize");
1009 if (0 < g_list_length(g_proc_list)) {
1010 iter = g_list_first(g_proc_list);
1011 while (NULL != iter) {
1012 g_proc_list = g_list_remove_link(g_proc_list, iter);
1013 iter = g_list_first(g_proc_list);
1017 if (g_restart_timer != NULL) {
1018 ecore_timer_del(g_restart_timer);
1019 g_restart_timer = NULL;
1022 vcd_state_e state = vcd_config_get_service_state();
1023 if (VCD_STATE_READY != state) {
1024 if (VCD_STATE_RECORDING == state) {
1025 vcd_recorder_stop();
1027 vcd_engine_recognize_cancel();
1029 if (0 != vcd_recorder_destroy()) {
1030 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to destroy recorder");
1032 SLOG(LOG_DEBUG, TAG_VCD, "[Server] destroy recorder");
1035 if (0 != vcd_engine_agent_release()) {
1036 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to release engine");
1038 SLOG(LOG_DEBUG, TAG_VCD, "[Server] release engine");
1041 vcd_client_manager_unset_appid();
1043 vcd_config_set_service_state(VCD_STATE_NONE);
1044 vcdc_send_service_state(VCD_STATE_NONE);
1046 SLOG(LOG_DEBUG, TAG_VCD, "[Server] mode finalize");
1051 static Eina_Bool __finalize_quit_ecore_loop(void *data)
1053 SLOG(LOG_DEBUG, TAG_VCD, "[Server] quit ecore main loop");
1054 ecore_main_loop_quit();
1058 static void __read_proc()
1061 struct dirent *dirp = NULL;
1065 if (0 < g_list_length(g_proc_list)) {
1066 iter = g_list_first(g_proc_list);
1067 while (NULL != iter) {
1068 g_proc_list = g_list_remove_link(g_proc_list, iter);
1069 iter = g_list_first(g_proc_list);
1073 dp = opendir("/proc");
1075 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Fail to open proc");
1081 tmp = atoi(dirp->d_name);
1082 if (0 >= tmp) continue;
1083 g_proc_list = g_list_append(g_proc_list, GINT_TO_POINTER(tmp));
1085 } while (NULL != dirp);
1091 static void __vcd_cleanup_client(vcd_client_type_e type)
1093 int* client_list = NULL;
1094 int client_count = 0;
1101 if (VCD_CLIENT_TYPE_NORMAL == type) {
1102 ret = vcd_client_get_list(&client_list, &client_count);
1103 } else if (VCD_CLIENT_TYPE_WIDGET == type) {
1104 ret = vcd_client_widget_get_list(&client_list, &client_count);
1105 } else if (VCD_CLIENT_TYPE_MANAGER == type) {
1106 mgr_pid = vcd_client_manager_get_pid();
1107 client_list = &mgr_pid;
1109 if (-1 == mgr_pid) {
1110 SLOG(LOG_WARN, TAG_VCD, "[WARNING] Invalid Manager pid");
1115 if (0 == ret || mgr_pid > 0) {
1116 SLOG(LOG_DEBUG, TAG_VCD, "===== Clean up %s client ", type ? (type == 1) ? "Widget" : "Manager" : "Normal");
1117 if (NULL != client_list && client_count > 0) {
1118 for (i = 0; i < client_count; i++) {
1121 for (j = 0; j < g_list_length(g_proc_list); j++) {
1122 iter = g_list_nth(g_proc_list, j);
1124 if (client_list[i] == GPOINTER_TO_INT(iter->data)) {
1125 SLOG(LOG_DEBUG, TAG_VCD, "%s pid(%d) is running", type ? (type == 1) ? "Widget" : "Manager" : "Normal", client_list[i]);
1132 if (false == exist) {
1133 SLOG(LOG_ERROR, TAG_VCD, "%s pid(%d) should be removed", type ? (type == 1) ? "Widget" : "Manager" : "Normal", client_list[i]);
1134 if (VCD_CLIENT_TYPE_NORMAL == type)
1135 vcd_server_finalize(client_list[i]);
1136 else if (VCD_CLIENT_TYPE_WIDGET == type)
1137 vcd_server_widget_finalize(client_list[i]);
1139 vcd_server_mgr_finalize(mgr_pid);
1143 SLOG(LOG_DEBUG, TAG_VCD, "=====");
1144 SLOG(LOG_DEBUG, TAG_VCD, " ");
1146 if (NULL != client_list && -1 == mgr_pid) {
1153 Eina_Bool vcd_cleanup_client_all(void *data)
1157 __vcd_cleanup_client(VCD_CLIENT_TYPE_NORMAL);
1158 __vcd_cleanup_client(VCD_CLIENT_TYPE_WIDGET);
1159 __vcd_cleanup_client(VCD_CLIENT_TYPE_MANAGER);
1162 if (0 == vcd_client_get_list(&client_list, &client_count)) {
1163 SLOG(LOG_DEBUG, TAG_VCD, "===== Clean up client ");
1164 if (NULL != client_list && client_count > 0) {
1165 for (i = 0; i < client_count; i++) {
1168 for (j = 0; j < g_list_length(g_proc_list); j++) {
1169 iter = g_list_nth(g_proc_list, j);
1171 if (client_list[i] == GPOINTER_TO_INT(iter->data)) {
1172 SLOG(LOG_DEBUG, TAG_VCD, "pid(%d) is running", client_list[i]);
1179 if (false == exist) {
1180 SLOG(LOG_ERROR, TAG_VCD, "pid(%d) should be removed", client_list[i]);
1181 vcd_server_finalize(client_list[i]);
1184 result = vcdc_send_hello(client_list[i], VCD_CLIENT_TYPE_NORMAL);
1187 SLOG(LOG_DEBUG, TAG_VCD, "[Server] pid(%d) should be removed.", client_list[i]);
1188 vcd_server_finalize(client_list[i]);
1189 } else if (-1 == result) {
1190 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Hello result has error");
1195 SLOG(LOG_DEBUG, TAG_VCD, "=====");
1196 SLOG(LOG_DEBUG, TAG_VCD, " ");
1198 if (NULL != client_list) {
1203 /* If app is in background state, app cannot response message. */
1204 if (0 == vcd_client_widget_get_list(&client_list, &client_count)) {
1205 SLOG(LOG_DEBUG, TAG_VCD, "===== Clean up widget");
1206 if (NULL != client_list && client_count > 0) {
1207 for (i = 0; i < client_count; i++) {
1210 for (j = 0; j < g_list_length(g_proc_list); j++) {
1211 iter = g_list_nth(g_proc_list, j);
1213 if (client_list[i] == GPOINTER_TO_INT(iter->data)) {
1214 SLOG(LOG_DEBUG, TAG_VCD, "widget pid(%d) is running", client_list[i]);
1221 if (false == exist) {
1222 SLOG(LOG_ERROR, TAG_VCD, "widget pid(%d) should be removed", client_list[i]);
1223 vcd_server_widget_finalize(client_list[i]);
1226 result = vcdc_send_hello(client_list[i], VCD_CLIENT_TYPE_WIDGET);
1229 SLOG(LOG_DEBUG, TAG_VCD, "[Server] widget pid(%d) should be removed.", client_list[i]);
1230 vcd_server_widget_finalize(client_list[i]);
1231 } else if (-1 == result) {
1232 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Hello result has error");
1237 SLOG(LOG_DEBUG, TAG_VCD, "=====");
1238 SLOG(LOG_DEBUG, TAG_VCD, " ");
1241 if (NULL != client_list) {
1249 int mgr_pid = vcd_client_manager_get_pid();
1251 for (j = 0; j < g_list_length(g_proc_list); j++) {
1252 iter = g_list_nth(g_proc_list, j);
1254 if (mgr_pid == GPOINTER_TO_INT(iter->data)) {
1255 SLOG(LOG_DEBUG, TAG_VCD, "manager pid(%d) is running", mgr_pid);
1262 if (false == exist) {
1263 SLOG(LOG_ERROR, TAG_VCD, "manager pid (%d) should be removed", mgr_pid);
1264 vcd_server_mgr_finalize(mgr_pid);
1271 int vcd_server_get_service_state()
1273 return vcd_config_get_service_state();
1276 int vcd_server_get_foreground()
1279 vcd_config_get_foreground(&pid);
1287 int vcd_server_mgr_initialize(int pid)
1289 if (false == g_is_engine) {
1290 if (0 != vcd_engine_agent_initialize_current_engine()) {
1291 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] No Engine");
1292 g_is_engine = false;
1293 return VCD_ERROR_ENGINE_NOT_FOUND;
1299 /* check if pid is valid */
1300 if (false == vcd_client_manager_is_valid(pid)) {
1301 SLOG(LOG_ERROR, TAG_VCD, "[Server] old manager pid(%d) be removed", vcd_client_manager_get_pid());
1302 vcd_server_mgr_cancel();
1303 vcd_client_manager_unset();
1306 /* Add client information to client manager */
1307 if (0 != vcd_client_manager_set(pid)) {
1308 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to add manager");
1309 return VCD_ERROR_OPERATION_FAILED;
1312 if (0 != vcdc_send_manager_pid(pid))
1313 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Fail to send manager pid");
1315 SLOG(LOG_DEBUG, TAG_VCD, "[Server Success] Manager initialize : pid(%d)", pid);
1317 return VCD_ERROR_NONE;
1320 int vcd_server_mgr_finalize(int pid)
1322 /* check if pid is valid */
1323 if (false == vcd_client_manager_is_valid(pid)) {
1324 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] The manager pid(%d) is NOT valid", pid);
1325 return VCD_ERROR_INVALID_PARAMETER;
1328 /* Cancel recognition */
1329 vcd_server_mgr_cancel();
1331 if (0 != vcdc_send_manager_pid(-1))
1332 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Fail to send manager pid");
1334 /* Remove manager information */
1335 if (0 != vcd_client_manager_unset()) {
1336 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Fail to delete client");
1339 if (0 == vcd_client_get_ref_count()) {
1340 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Connected client list is empty");
1341 ecore_timer_add(0, __finalize_quit_ecore_loop, NULL);
1344 SLOG(LOG_DEBUG, TAG_VCD, "[Server Success] Manager Finalize : pid(%d)", pid);
1346 return VCD_ERROR_NONE;
1349 int vcd_server_mgr_set_command(int pid)
1351 if (0 != vcd_client_manager_set_command(pid)) {
1352 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] The manager pid(%d) is NOT valid", pid);
1353 return VCD_ERROR_INVALID_PARAMETER;
1355 return VCD_ERROR_NONE;
1358 int vcd_server_mgr_unset_command(int pid)
1360 if (0 != vcd_client_manager_unset_command(pid)) {
1361 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] The manager pid(%d) is NOT valid", pid);
1362 return VCD_ERROR_INVALID_PARAMETER;
1364 return VCD_ERROR_NONE;
1367 int vcd_server_mgr_set_demandable_client(int pid)
1369 /* check if pid is valid */
1370 if (false == vcd_client_manager_is_valid(pid)) {
1371 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] The manager pid(%d) is NOT valid", pid);
1372 return VCD_ERROR_INVALID_PARAMETER;
1375 GSList* client_list = NULL;
1376 if (0 != vc_info_parser_get_demandable_clients(&client_list)) {
1377 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to get demandable client");
1378 return VCD_ERROR_OPERATION_FAILED;
1381 /* Save client list */
1382 if (0 != vcd_client_manager_set_demandable_client(pid, client_list)) {
1383 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to set demandable client list");
1384 return VCD_ERROR_OPERATION_FAILED;
1387 return VCD_ERROR_NONE;
1390 int vcd_server_mgr_set_audio_type(int pid, const char* audio_type)
1392 /* check if pid is valid */
1393 if (false == vcd_client_manager_is_valid(pid)) {
1394 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] The manager pid(%d) is NOT valid", pid);
1395 return VCD_ERROR_INVALID_PARAMETER;
1399 vcp_audio_type_e type = VCP_AUDIO_TYPE_PCM_S16_LE;
1403 ret = vcd_engine_get_audio_format(audio_type, &type, &rate, &channel);
1405 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to get audio format : %d", ret);
1406 return VCD_ERROR_OPERATION_FAILED;
1409 ret = vcd_recorder_set(audio_type, type, rate, channel);
1411 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to set audio in type : %d", ret);
1412 return VCD_ERROR_OPERATION_FAILED;
1415 return VCD_ERROR_NONE;
1418 int vcd_server_mgr_get_audio_type(int pid, char** audio_type)
1420 /* check if pid is valid */
1421 if (false == vcd_client_manager_is_valid(pid)) {
1422 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] The manager pid(%d) is NOT valid", pid);
1423 return VCD_ERROR_INVALID_PARAMETER;
1426 int ret = vcd_recorder_get(audio_type);
1428 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to get audio in type : %d", ret);
1429 return VCD_ERROR_OPERATION_FAILED;
1432 return VCD_ERROR_NONE;
1435 int vcd_server_mgr_set_client_info(int pid)
1437 /* check if pid is valid */
1438 if (false == vcd_client_manager_is_valid(pid)) {
1439 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] The manager pid(%d) is NOT valid", pid);
1440 return VCD_ERROR_INVALID_PARAMETER;
1443 int ret = vcd_client_save_client_info();
1445 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to save client info : %d", ret);
1446 return VCD_ERROR_OPERATION_FAILED;
1449 return VCD_ERROR_NONE;
1452 static int __start_internal_recognition()
1456 if (0 != vcd_client_command_collect_command()) {
1457 SLOG(LOG_ERROR, TAG_VCD, "[Client Data ERROR] Fail to collect command");
1458 /* Send error cb to manager */
1459 int pid = vcd_client_widget_get_foreground_pid();
1461 vcdc_send_error_signal_to_manager(vcd_client_manager_get_pid(), VCD_ERROR_OPERATION_FAILED, "voice_engine.error.proc_fail");
1462 return VCD_ERROR_OPERATION_FAILED;
1465 /* 3. Set command to engine */
1466 ret = vcd_engine_set_commands();
1468 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to set commands : %d", ret);
1469 /* Send error cb to manager */
1470 int pid = vcd_client_widget_get_foreground_pid();
1472 vcdc_send_error_signal_to_manager(vcd_client_manager_get_pid(), VCD_ERROR_OPERATION_FAILED, "voice_engine.error.proc_fail");
1473 return VCD_ERROR_OPERATION_FAILED;
1476 SLOG(LOG_DEBUG, TAG_VCD, "[Server Success] Set command");
1478 bool stop_by_silence = true;
1479 if (VCD_RECOGNITION_MODE_MANUAL == vcd_client_get_recognition_mode()) {
1480 stop_by_silence = false;
1483 vcd_client_manager_set_result_text(NULL);
1485 /* 4. start recognition */
1486 ret = vcd_engine_recognize_start(stop_by_silence);
1488 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to start recognition : result(%d)", ret);
1489 /* Send error cb to manager */
1490 int pid = vcd_client_widget_get_foreground_pid();
1492 vcdc_send_error_signal_to_manager(vcd_client_manager_get_pid(), VCD_ERROR_OPERATION_FAILED, "voice_engine.error.proc_fail");
1493 return VCD_ERROR_OPERATION_FAILED;
1496 SLOG(LOG_DEBUG, TAG_VCD, "[Server Success] Start engine");
1498 /* 5. recorder start */
1499 ret = vcd_recorder_start();
1501 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to start recorder : result(%d)", ret);
1502 vcd_engine_recognize_cancel();
1503 /* Send error cb to manager */
1504 int pid = vcd_client_widget_get_foreground_pid();
1506 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;
1599 /* 2. Stop recorder */
1600 vcd_recorder_stop();
1602 /* 3. Stop engine recognition */
1603 int ret = vcd_engine_recognize_stop();
1605 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to stop recognition : %d", ret);
1608 /* 4. Set original mode */
1609 vcd_config_set_service_state(VCD_STATE_PROCESSING);
1610 vcdc_send_service_state(VCD_STATE_PROCESSING);
1612 return VCD_ERROR_NONE;
1615 int vcd_server_mgr_cancel()
1617 /* 1. Check current state */
1618 vcd_state_e state = vcd_config_get_service_state();
1619 if (VCD_STATE_RECORDING != state && VCD_STATE_PROCESSING != state) {
1620 SLOG(LOG_WARN, TAG_VCD, "[Server ERROR] Current state is not recording or processing");
1621 return VCD_ERROR_INVALID_STATE;
1623 if (-1 == vcd_client_manager_get_pid()) {
1624 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Manager is NOT available.");
1625 return VCD_ERROR_OPERATION_FAILED;
1628 if (g_restart_timer != NULL) {
1629 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Delete restart engine timer");
1630 ecore_timer_del(g_restart_timer);
1631 g_restart_timer = NULL;
1634 /* 2. Stop recorder */
1635 vcd_recorder_stop();
1636 /* 3. Cancel engine */
1637 int ret = vcd_engine_recognize_cancel();
1639 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Fail to cancel : %d", ret);
1642 if (false == vcd_client_manager_get_exclusive()) {
1643 if (1 == vcd_config_get_command_type_enabled(VC_COMMAND_TYPE_WIDGET)) {
1644 int pid = vcd_client_widget_get_foreground_pid();
1646 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Request tooltip hide");
1647 ecore_timer_add(0, __vcd_request_show_tooltip, (void*)false);
1651 vcd_client_manager_set_exclusive(false);
1655 vcd_config_set_service_state(VCD_STATE_READY);
1656 vcdc_send_service_state(VCD_STATE_READY);
1658 return VCD_ERROR_NONE;
1662 int vcd_server_mgr_result_select()
1664 __vcd_send_selected_result(NULL);
1666 return VCD_ERROR_NONE;
1669 int vcd_server_mgr_set_domain(int pid, const char* domain)
1671 /* check if pid is valid */
1672 if (false == vcd_client_manager_is_valid(pid)) {
1673 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] The manager pid(%d) is NOT valid", pid);
1674 return VCD_ERROR_INVALID_PARAMETER;
1677 vcd_state_e state = vcd_config_get_service_state();
1678 if (VCD_STATE_READY != state) {
1679 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Current state is not ready");
1680 return VCD_ERROR_INVALID_STATE;
1683 /* Set domain to engine */
1684 int ret = vcd_engine_set_domain(pid, domain);
1686 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to set domain : %d", ret);
1688 SLOG(LOG_DEBUG, TAG_VCD, "[Server SUCCESS] Set domain");
1694 int vcd_server_mgr_set_private_data(int pid, const char* key, const char* data)
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 private data to engine */
1709 int ret = vcd_engine_set_private_data(pid, key, data);
1711 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to set private data : %d", ret);
1713 SLOG(LOG_DEBUG, TAG_VCD, "[Server SUCCESS] Set private data");
1719 int vcd_server_mgr_get_private_data(int pid, const char* key, 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;
1726 vcd_state_e state = vcd_config_get_service_state();
1727 if (VCD_STATE_READY != state) {
1728 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Current state is not ready");
1729 return VCD_ERROR_INVALID_STATE;
1732 /* Get private data to engine */
1733 int ret = vcd_engine_get_private_data(pid, key, data);
1735 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to get private data : %d", ret);
1737 SLOG(LOG_DEBUG, TAG_VCD, "[Server SUCCESS] Set private data");
1743 int vcd_server_mgr_do_action(int pid, int type, const char* action)
1747 /* check if pid is valid */
1748 if (false == vcd_client_manager_is_valid(pid)) {
1749 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] The manager pid(%d) is NOT valid", pid);
1750 return VCD_ERROR_INVALID_PARAMETER;
1753 vcd_state_e state = vcd_config_get_service_state();
1754 if (VCD_STATE_READY != state) {
1755 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Current state is not ready");
1756 return VCD_ERROR_INVALID_STATE;
1759 /* Reqeust do action to engine */
1760 if (VCD_SEND_EVENT_TYPE_TEXT == type)
1761 ret = vcd_engine_process_text(pid, action);
1762 else if (VCD_SEND_EVENT_TYPE_LIST_EVENT == type)
1763 ret = vcd_engine_process_list_event(pid, action);
1764 else if (VCD_SEND_EVENT_TYPE_HAPTIC_EVENT == type)
1765 ret = vcd_engine_process_haptic_event(pid, action);
1768 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to process do action : %d", ret);
1770 SLOG(LOG_DEBUG, TAG_VCD, "[Server SUCCESS] Process do action");
1776 int vcd_server_mgr_enable_command_type(int pid, int cmd_type)
1780 /* check if pid is valid */
1781 if (false == vcd_client_manager_is_valid(pid)) {
1782 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] The manager pid(%d) is NOT valid", pid);
1783 return VCD_ERROR_INVALID_PARAMETER;
1786 vcd_state_e state = vcd_config_get_service_state();
1787 if (VCD_STATE_READY != state) {
1788 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Current state is not ready");
1789 return VCD_ERROR_INVALID_STATE;
1792 ret = vcd_config_enable_command_type(cmd_type);
1794 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to enable command type");
1796 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Enable command type(%d)", cmd_type);
1802 int vcd_server_mgr_disable_command_type(int pid, int cmd_type)
1806 /* check if pid is valid */
1807 if (false == vcd_client_manager_is_valid(pid)) {
1808 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] The manager pid(%d) is NOT valid", pid);
1809 return VCD_ERROR_INVALID_PARAMETER;
1812 vcd_state_e state = vcd_config_get_service_state();
1813 if (VCD_STATE_READY != state) {
1814 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Current state is not ready");
1815 return VCD_ERROR_INVALID_STATE;
1818 ret = vcd_config_disable_command_type(cmd_type);
1820 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to disable command type");
1822 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Disable command type(%d)", cmd_type);
1829 * VC Server Functions for Client
1831 int vcd_server_initialize(int pid)
1833 if (false == g_is_engine) {
1834 if (0 != vcd_engine_agent_initialize_current_engine()) {
1835 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] No Engine");
1836 g_is_engine = false;
1837 return VCD_ERROR_ENGINE_NOT_FOUND;
1843 if (false == vcd_engine_is_available_engine()) {
1844 if (0 != vcd_engine_agent_initialize_current_engine()) {
1845 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] No Engine");
1846 return VCD_ERROR_ENGINE_NOT_FOUND;
1850 /* check if pid is valid */
1851 if (true == vcd_client_is_available(pid)) {
1852 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] The pid is already exist");
1853 return VCD_ERROR_INVALID_PARAMETER;
1856 /* Add client information to client manager */
1857 if (0 != vcd_client_add(pid)) {
1858 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to add client info");
1859 return VCD_ERROR_OPERATION_FAILED;
1862 SLOG(LOG_DEBUG, TAG_VCD, "[Server Success] Client Initialize : pid(%d)", pid);
1864 return VCD_ERROR_NONE;
1867 int vcd_server_finalize(int pid)
1869 /* check if pid is valid */
1870 if (false == vcd_client_is_available(pid)) {
1871 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] pid is NOT valid ");
1872 return VCD_ERROR_INVALID_PARAMETER;
1875 /* Remove client information */
1876 if (0 != vcd_client_delete(pid)) {
1877 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Fail to delete client");
1880 if (0 == vcd_client_get_ref_count()) {
1881 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Connected client list is empty");
1882 ecore_timer_add(0, __finalize_quit_ecore_loop, NULL);
1885 SLOG(LOG_DEBUG, TAG_VCD, "[Server Success] Client Finalize : pid(%d)", pid);
1887 return VCD_ERROR_NONE;
1890 int vcd_server_set_command(int pid, vc_cmd_type_e cmd_type)
1892 /* check if pid is valid */
1893 if (false == vcd_client_is_available(pid)) {
1894 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] pid is NOT valid ");
1895 return VCD_ERROR_INVALID_PARAMETER;
1898 if (0 != vcd_client_set_command_type(pid, cmd_type)) {
1899 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to set command type : pid(%d), cmd_type(%d)", pid, cmd_type);
1900 return VCD_ERROR_OPERATION_FAILED;
1906 int vcd_server_unset_command(int pid, vc_cmd_type_e cmd_type)
1908 /* check if pid is valid */
1909 if (false == vcd_client_is_available(pid)) {
1910 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] pid is NOT valid ");
1911 return VCD_ERROR_INVALID_PARAMETER;
1914 if (0 != vcd_client_unset_command_type(pid, cmd_type)) {
1915 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to unset command type : pid(%d), cmd_type(%d)", pid, cmd_type);
1916 return VCD_ERROR_OPERATION_FAILED;
1922 int vcd_server_set_foreground(int pid, bool value)
1924 /* check if pid is valid */
1925 if (false == vcd_client_is_available(pid) && false == vcd_client_widget_is_available(pid)) {
1926 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] pid is NOT valid ");
1927 return VCD_ERROR_INVALID_PARAMETER;
1930 if (0 != vcd_config_set_foreground(pid, value)) {
1931 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to set foreground : pid(%d), value(%d)", pid, value);
1932 return VCD_ERROR_OPERATION_FAILED;
1938 static int __vcd_server_launch_manager_app()
1941 bool running = false;
1944 ret = vcd_client_manager_get_appid(&appid);
1945 if (0 != ret || NULL == appid) {
1946 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Fail to get manager appid");
1947 return VCD_ERROR_OPERATION_FAILED;
1949 ret = app_manager_is_running(appid, &running);
1951 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Fail to check running with appid(%s)", appid);
1954 return VCD_ERROR_OPERATION_FAILED;
1956 if (false == running) {
1957 int tmp_ret = __vcd_is_package_installed(appid);
1958 if (false == tmp_ret) {
1959 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] manager app is not installed, appid(%s)", appid);
1961 ret = __vcd_activate_app_by_appcontrol(appid);
1963 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Fail to activate app");
1968 SLOG(LOG_ERROR, TAG_VCD, "Launch vc manager app: appid(%s)", appid);
1971 ret = __vcd_resume_app(appid);
1973 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Fail to resume app");
1978 SLOG(LOG_ERROR, TAG_VCD, "Resume vc manager app: appid(%s)", appid);
1984 return VCD_ERROR_NONE;
1987 int vcd_server_dialog(int pid, const char* disp_text, const char* utt_text, int continuous)
1989 /* check if pid is valid */
1990 if (false == vcd_client_is_available(pid) && false == vcd_client_widget_is_available(pid)) {
1991 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] pid is NOT valid ");
1992 return VCD_ERROR_INVALID_PARAMETER;
1995 int ret = __vcd_server_launch_manager_app();
1997 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);
2001 ret = vcdc_send_dialog(vcd_client_manager_get_pid(), pid, disp_text, utt_text, continuous);
2003 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 int vcd_server_is_system_command_valid(int pid, int* is_sys_cmd_valid)
2012 /* check if pid is valid */
2013 if (false == vcd_client_is_available(pid) && false == vcd_client_widget_is_available(pid)) {
2014 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] pid is NOT valid ");
2015 return VCD_ERROR_INVALID_PARAMETER;
2018 /* check if system command is valid */
2019 if (true == vcd_client_manager_is_system_command_valid(vcd_client_manager_get_pid()))
2020 *is_sys_cmd_valid = true;
2022 *is_sys_cmd_valid = false;
2028 int vcd_server_set_exclusive_command(int pid, bool value)
2030 /* check if pid is valid */
2031 if (false == vcd_client_is_available(pid)) {
2032 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] pid is NOT valid ");
2033 return VCD_ERROR_INVALID_PARAMETER;
2036 if (true == value) {
2037 if (0 != vcd_client_set_exclusive_command(pid)) {
2038 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to set exclusive command : pid(%d)", pid);
2039 return VCD_ERROR_OPERATION_FAILED;
2042 if (0 != vcd_client_unset_exclusive_command(pid)) {
2043 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to unset exclusive command : pid(%d)", pid);
2044 return VCD_ERROR_OPERATION_FAILED;
2051 int vcd_server_request_start(int pid, bool stop_by_silence)
2053 /* check if pid is valid */
2054 if (false == vcd_client_is_available(pid)) {
2055 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] pid(%d) is NOT forground client", pid);
2056 return VCD_ERROR_INVALID_PARAMETER;
2060 /* Check current state */
2061 vcd_state_e state = vcd_config_get_service_state();
2063 /* Service state should be ready */
2064 if (VCD_STATE_READY != state) {
2065 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Current state is not Ready : pid(%d)", pid);
2066 return VCD_ERROR_INVALID_STATE;
2069 if (-1 != vcd_client_manager_get_pid()) {
2070 /* Check current pid is valid */
2071 if (false == vcd_client_manager_check_demandable_client(pid)) {
2072 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Current client is NOT available : pid(%d)", pid);
2073 return VCD_ERROR_INVALID_PARAMETER;
2077 ret = vcd_server_mgr_start(stop_by_silence, false);
2079 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Fail to start recognition");
2080 return VCD_ERROR_INVALID_PARAMETER;
2086 int vcd_server_request_stop(int pid)
2089 /* Check current state */
2090 vcd_state_e state = vcd_config_get_service_state();
2092 /* Service state should be ready */
2093 if (VCD_STATE_RECORDING != state) {
2094 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Current state is not Recording : pid(%d)", pid);
2095 return VCD_ERROR_INVALID_STATE;
2098 if (-1 != vcd_client_manager_get_pid()) {
2099 /* Check current pid is valid */
2100 if (false == vcd_client_manager_check_demandable_client(pid)) {
2101 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Current client is NOT available : pid(%d)", pid);
2102 return VCD_ERROR_INVALID_PARAMETER;
2106 ret = vcd_server_mgr_stop();
2108 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Fail to start recognition");
2109 return VCD_ERROR_OPERATION_FAILED;
2112 return VCD_ERROR_NONE;
2115 int vcd_server_request_cancel(int pid)
2118 /* Check current state */
2119 vcd_state_e state = vcd_config_get_service_state();
2121 /* Service state should be recording or processing */
2122 if (VCD_STATE_RECORDING != state && VCD_STATE_PROCESSING != state) {
2123 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Current state is not Recording or Processing : pid(%d)", pid);
2124 return VCD_ERROR_INVALID_STATE;
2127 if (-1 != vcd_client_manager_get_pid()) {
2128 /* Check current pid is valid */
2129 if (false == vcd_client_manager_check_demandable_client(pid)) {
2130 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Current client is NOT available : pid(%d)", pid);
2131 return VCD_ERROR_INVALID_PARAMETER;
2135 ret = vcd_server_mgr_cancel();
2137 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Fail to start recognition");
2138 return VCD_ERROR_OPERATION_FAILED;
2141 return VCD_ERROR_NONE;
2146 * VC Server Functions for Widget lib
2148 int vcd_server_widget_initialize(int pid)
2150 if (false == g_is_engine) {
2151 if (0 != vcd_engine_agent_initialize_current_engine()) {
2152 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] No Engine");
2153 g_is_engine = false;
2154 return VCD_ERROR_ENGINE_NOT_FOUND;
2160 if (false == vcd_engine_is_available_engine()) {
2161 if (0 != vcd_engine_agent_initialize_current_engine()) {
2162 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] No Engine");
2163 return VCD_ERROR_ENGINE_NOT_FOUND;
2167 /* check if pid is valid */
2168 if (true == vcd_client_widget_is_available(pid)) {
2169 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] The pid is already exist");
2170 return VCD_ERROR_NONE;
2173 /* Add client information to client manager */
2174 if (0 != vcd_client_widget_add(pid)) {
2175 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to add client info");
2176 return VCD_ERROR_OPERATION_FAILED;
2179 SLOG(LOG_DEBUG, TAG_VCD, "[Server Success] Initialize widget : pid(%d)", pid);
2181 return VCD_ERROR_NONE;
2184 int vcd_server_widget_finalize(int pid)
2186 /* check if pid is valid */
2187 if (false == vcd_client_widget_is_available(pid)) {
2188 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] pid is NOT valid ");
2189 return VCD_ERROR_INVALID_PARAMETER;
2192 /* Remove client information */
2193 if (0 != vcd_client_widget_delete(pid)) {
2194 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Fail to delete client");
2197 if (0 == vcd_client_get_ref_count()) {
2198 SLOG(LOG_DEBUG, TAG_VCD, "[Server] connected client list is empty");
2199 ecore_timer_add(0, __finalize_quit_ecore_loop, NULL);
2202 return VCD_ERROR_NONE;
2205 int vcd_server_widget_start_recording(int pid, bool widget_command)
2207 /* check if pid is valid */
2208 if (false == vcd_client_widget_is_available(pid)) {
2209 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] pid is NOT valid ");
2210 return VCD_ERROR_INVALID_PARAMETER;
2213 if (true == widget_command) {
2214 vcd_client_widget_set_command(pid);
2215 SLOG(LOG_DEBUG, TAG_VCD, "[Server] widget command is available");
2217 vcd_client_widget_unset_command(pid);
2218 SLOG(LOG_WARN, TAG_VCD, "[Server] widget command is NOT available");
2221 int ret = __start_internal_recognition();
2223 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to start recongition : %d", ret);
2224 ecore_timer_add(0, __vcd_request_show_tooltip, (void*)false);
2230 int vcd_server_widget_start(int pid, bool stop_by_silence)
2232 /* check if pid is valid */
2233 int fore_pid = vcd_client_widget_get_foreground_pid();
2234 if (pid != fore_pid) {
2235 SLOG(LOG_ERROR, TAG_VCD, "[Server] pid is NOT foreground");
2236 return VCD_ERROR_INVALID_PARAMETER;
2239 /* Check current state */
2240 vcd_state_e state = vcd_config_get_service_state();
2242 /* Service state should be ready */
2243 if (VCD_STATE_READY != state) {
2244 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Current state is not Ready : pid(%d)", pid);
2245 return VCD_ERROR_INVALID_STATE;
2248 vcd_client_set_slience_detection(stop_by_silence);
2250 /* Notify show tooltip */
2251 ecore_timer_add(0, __vcd_request_show_tooltip, (void*)true);
2256 int vcd_server_widget_stop(int pid)
2258 /* check if pid is valid */
2259 int fore_pid = vcd_client_widget_get_foreground_pid();
2260 if (pid != fore_pid) {
2261 SLOG(LOG_ERROR, TAG_VCD, "[Server] pid is NOT foreground");
2262 return VCD_ERROR_INVALID_PARAMETER;
2266 /* Check current state */
2267 vcd_state_e state = vcd_config_get_service_state();
2269 /* Service state should be recording */
2270 if (VCD_STATE_RECORDING != state) {
2271 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Current service state is not Recording : pid(%d)", pid);
2272 return VCD_ERROR_INVALID_STATE;
2275 ret = vcd_server_mgr_stop();
2277 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Fail to start recognition");
2278 return VCD_ERROR_OPERATION_FAILED;
2281 return VCD_ERROR_NONE;
2284 int vcd_server_widget_cancel(int pid)
2286 /* check if pid is valid */
2287 int fore_pid = vcd_client_widget_get_foreground_pid();
2288 if (pid != fore_pid) {
2289 SLOG(LOG_ERROR, TAG_VCD, "[Server] pid is NOT foreground");
2290 return VCD_ERROR_INVALID_PARAMETER;
2294 /* Check current state */
2295 vcd_state_e state = vcd_config_get_service_state();
2297 /* Service state should be recording or processing */
2298 if (VCD_STATE_RECORDING != state && VCD_STATE_PROCESSING != state) {
2299 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Current state is not Recording or Processing : pid(%d)", pid);
2300 return VCD_ERROR_INVALID_STATE;
2303 ret = vcd_server_mgr_cancel();
2305 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Fail to cancel recognition : %d", ret);
2309 return VCD_ERROR_NONE;
2312 int vcd_server_widget_enable_asr_result(int pid, bool enable)
2315 ret = vcd_client_widget_set_asr_result_enabled(pid, enable);
2317 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Fail to enable asr result : %d", ret);