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);
644 bool temp = vcd_client_manager_get_exclusive();
645 vc_info_parser_set_result(all_result, event, msg, NULL, temp);
648 int pid = vcd_client_widget_get_foreground_pid();
650 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Request tooltip hide");
651 /* Send to hide tooltip */
652 vcdc_send_show_tooltip(pid, false);
655 if (-1 != vcd_client_manager_get_pid()) {
656 /* Manager client is available */
657 if (0 != vcdc_send_result_to_manager(vcd_client_manager_get_pid(), VC_RESULT_TYPE_NORMAL)) {
658 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Fail to send result");
662 vcd_client_manager_set_exclusive(false);
666 if (false == vcd_client_manager_get_exclusive()) {
667 int pid = vcd_client_widget_get_foreground_pid();
669 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Request tooltip hide");
670 vcdc_send_show_tooltip(pid, false);
673 vc_info_parser_set_result(all_result, event, msg, vc_cmd_list, false);
675 if (-1 != vcd_client_manager_get_pid()) {
676 /* Manager client is available */
677 if (0 != vcdc_send_result_to_manager(vcd_client_manager_get_pid(), VC_RESULT_TYPE_NORMAL)) {
678 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Fail to send result");
681 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Manager is NOT available. Send result to client directly");
682 /* Send result to client */
683 ecore_timer_add(0, __vcd_send_selected_result, NULL);
686 /* exclusive command */
687 vc_info_parser_set_result(all_result, event, msg, vc_cmd_list, true);
689 if (-1 != vcd_client_manager_get_pid()) {
690 /* Manager client is available */
691 if (0 != vcdc_send_result_to_manager(vcd_client_manager_get_pid(), VC_RESULT_TYPE_NORMAL)) {
692 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Fail to send result");
695 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Manager is NOT available");
698 vcd_client_manager_set_exclusive(false);
702 vc_cmd_list_destroy(vc_cmd_list, true);
708 if (NULL == result_id) {
710 if (NULL != all_result) {
711 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Engine result is no command : %s", all_result);
712 bool temp = vcd_client_manager_get_exclusive();
713 vc_info_parser_set_result(all_result, event, msg, NULL, temp);
716 int pid = vcd_client_widget_get_foreground_pid();
718 if (NULL != all_result) {
719 /* Send result text to widget */
720 vcdc_send_result(pid, vcd_client_manager_get_pid(), VC_COMMAND_TYPE_WIDGET);
723 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Request tooltip hide");
724 /* Send to hide tooltip */
725 vcdc_send_show_tooltip(pid, false);
728 if (-1 != vcd_client_manager_get_pid()) {
729 /* Manager client is available */
730 if (0 != vcdc_send_result_to_manager(vcd_client_manager_get_pid(), VC_RESULT_TYPE_NORMAL)) {
731 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Fail to send result");
735 vcd_client_manager_set_exclusive(false);
741 SLOG(LOG_DEBUG, TAG_VCD, "[Server] === Get engine result ===");
744 vc_cmd_s* temp_cmd = NULL;
745 vc_cmd_list_h vc_cmd_list = NULL;
747 if (0 != vc_cmd_list_create(&vc_cmd_list)) {
748 SLOG(LOG_ERROR, TAG_VCD, "[Server] Fail to create command list");
749 vcd_client_manager_set_exclusive(false);
750 vcd_config_set_service_state(VCD_STATE_READY);
751 vcdc_send_service_state(VCD_STATE_READY);
756 for (i = 0; i < count; i++) {
757 SLOG(LOG_DEBUG, TAG_VCD, "[Server] [%d] Result ID(%d)", i, result_id[i]);
759 if (result_id[i] < 0) {
760 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Result ID(%d) is NOT valid", result_id[i]);
764 ret = vcd_client_get_cmd_from_result_id(result_id[i], &temp_cmd);
765 if (0 == ret && NULL != temp_cmd) {
766 switch (temp_cmd->format) {
767 case VC_CMD_FORMAT_FIXED:
768 case VC_CMD_FORMAT_FIXED_AND_VFIXED:
769 case VC_CMD_FORMAT_VFIXED_AND_FIXED:
770 case VC_CMD_FORMAT_PARTIAL:
771 /* Nonfixed result is NOT valid */
773 case VC_CMD_FORMAT_FIXED_AND_NONFIXED:
774 if (NULL == temp_cmd->parameter) {
775 if (NULL != non_fixed_result) {
776 temp_cmd->parameter = strdup(non_fixed_result);
779 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Command type is NOT vaild. Parameter (%s)", temp_cmd->parameter);
782 case VC_CMD_FORMAT_NONFIXED_AND_FIXED:
783 if (NULL == temp_cmd->command) {
784 if (NULL != non_fixed_result) {
785 temp_cmd->command = strdup(non_fixed_result);
788 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Command type is NOT vaild. Command (%s)", temp_cmd->command);
793 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Unknown command type : %d", temp_cmd->type);
797 if (0 != vc_cmd_list_add(vc_cmd_list, (vc_cmd_h)temp_cmd)) {
798 SLOG(LOG_DEBUG, TAG_VCD, "Fail to add command to list");
799 vc_cmd_destroy((vc_cmd_h)temp_cmd);
802 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] NOT found matached result(%d)", result_id[i]);
806 vc_cmd_print_list(vc_cmd_list);
808 SLOG(LOG_DEBUG, TAG_VCD, "[Server] =========================");
810 int result_count = 0;
811 vc_cmd_list_get_count(vc_cmd_list, &result_count);
813 if (false == vcd_client_manager_get_exclusive()) {
814 int pid = vcd_client_widget_get_foreground_pid();
816 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Request tooltip hide");
817 vcdc_send_show_tooltip(pid, false);
820 vc_info_parser_set_result(all_result, event, msg, vc_cmd_list, false);
822 if (-1 != vcd_client_manager_get_pid()) {
823 /* Manager client is available */
824 if (0 != vcdc_send_result_to_manager(vcd_client_manager_get_pid(), VC_RESULT_TYPE_NORMAL)) {
825 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Fail to send result");
828 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Manager is NOT available. Send result to client directly");
829 /* Send result to client */
830 ecore_timer_add(0, __vcd_send_selected_result, NULL);
833 /* exclusive command */
834 vc_info_parser_set_result(all_result, event, msg, vc_cmd_list, true);
836 if (-1 != vcd_client_manager_get_pid()) {
837 /* Manager client is available */
838 if (0 != vcdc_send_result_to_manager(vcd_client_manager_get_pid(), VC_RESULT_TYPE_NORMAL)) {
839 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Fail to send result");
842 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Manager is NOT available");
845 vcd_client_manager_set_exclusive(false);
848 vc_cmd_list_destroy(vc_cmd_list, true);
855 static void __vcd_server_nlu_result_cb(vcp_result_event_e event, const char* nlu_result, void *user_data)
857 SLOG(LOG_DEBUG, TAG_VCD, "[Server] NLU result cb - event(%d)", event);
858 SLOG(LOG_DEBUG, TAG_VCD, "[Server] result (%s)", nlu_result);
860 int ret = vc_info_parser_set_nlu_result(nlu_result);
862 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to set nlu result (%d)", ret);
869 static void __vcd_server_error_cb(vcp_error_e error, const char* msg, void *user_data)
871 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Engine Error cb - reason(%d), msg(%s)", error, msg);
872 ecore_main_loop_thread_safe_call_async(__cancel_by_error, NULL);
874 char* error_msg = NULL;
875 error_msg = strdup(msg);
877 if (0 != vcdc_send_error_signal_to_manager(vcd_client_manager_get_pid(), error, error_msg)) {
878 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to send error signal");
881 if (NULL != error_msg) {
890 * vcd server Interfaces
892 static void __vcd_file_clean_up()
894 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, };
909 ret = readdir_r(dp, &entry, &dirp);
911 SLOG(LOG_ERROR, TAG_VCD, "[File ERROR] Fail to read directory");
916 if (!strncmp("vc_", dirp->d_name, strlen("vc_"))) {
917 memset(remove_path, 0, 256);
918 snprintf(remove_path, 256, "%s/%s", VC_RUNTIME_INFO_ROOT, dirp->d_name);
921 if (0 != remove(remove_path)) {
922 SLOG(LOG_WARN, TAG_VCD, "[File message WARN] Fail to remove file : %s", remove_path);
924 SLOG(LOG_DEBUG, TAG_VCD, "[File message] Remove file : %s", remove_path);
928 } while (NULL != dirp);
935 static int __vcd_db_clean_up()
938 int cnt = VC_COMMAND_TYPE_FOREGROUND;
940 if (VC_COMMAND_TYPE_BACKGROUND != cnt)
941 ret = vc_db_delete_commands(-1, cnt, NULL);
942 } while (VC_COMMAND_TYPE_EXCLUSIVE >= ++cnt);
951 /* Remove old file */
952 __vcd_file_clean_up();
954 /* initialize modules */
955 ret = vcd_config_initialize(__config_lang_changed_cb, __config_foreground_changed_cb, NULL);
957 SLOG(LOG_ERROR, TAG_VCD, "[Server WARNING] Fail to initialize config.");
961 ret = __vcd_db_clean_up();
963 SLOG(LOG_ERROR, TAG_VCD, "[Server WARNING] Fail to remove db data");
966 vcd_config_set_service_state(VCD_STATE_NONE);
968 //ret = vcd_engine_agent_init(__vcd_server_pre_result_cb, __vcd_server_result_cb, __vcd_server_nlu_result_cb, __vcd_server_error_cb);
969 ret = vcd_engine_agent_init(__vcd_server_asr_result_cb, __vcd_server_result_cb, __vcd_server_error_cb);
971 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to engine agent initialize : result(%d)", ret);
975 if (0 != vcd_recorder_create(__server_recorder_callback, __server_recorder_interrupt_callback)) {
976 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to create recorder");
977 return VCD_ERROR_OPERATION_FAILED;
981 ret = vcd_engine_agent_initialize_current_engine();
983 if (VCD_ERROR_ENGINE_NOT_FOUND == ret)
984 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] There is No Voice control engine");
986 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to init engine");
994 if (0 != vcd_engine_agent_load_current_engine()) {
995 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to load current engine");
996 return VCD_ERROR_OPERATION_FAILED;
999 /* Initialize manager info */
1000 vcd_client_manager_unset();
1002 vcd_config_set_service_state(VCD_STATE_READY);
1003 vcdc_send_service_state(VCD_STATE_READY);
1005 SLOG(LOG_DEBUG, TAG_VCD, "[Server SUCCESS] initialize");
1013 if (0 < g_list_length(g_proc_list)) {
1014 iter = g_list_first(g_proc_list);
1015 while (NULL != iter) {
1016 g_proc_list = g_list_remove_link(g_proc_list, iter);
1017 iter = g_list_first(g_proc_list);
1021 if (g_restart_timer != NULL) {
1022 ecore_timer_del(g_restart_timer);
1023 g_restart_timer = NULL;
1026 vcd_state_e state = vcd_config_get_service_state();
1027 if (VCD_STATE_READY != state) {
1028 if (VCD_STATE_RECORDING == state) {
1029 vcd_recorder_stop();
1031 vcd_engine_recognize_cancel();
1033 if (0 != vcd_recorder_destroy()) {
1034 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to destroy recorder");
1036 SLOG(LOG_DEBUG, TAG_VCD, "[Server] destroy recorder");
1039 if (0 != vcd_engine_agent_release()) {
1040 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to release engine");
1042 SLOG(LOG_DEBUG, TAG_VCD, "[Server] release engine");
1045 vcd_client_manager_unset_appid();
1047 vcd_config_set_service_state(VCD_STATE_NONE);
1048 vcdc_send_service_state(VCD_STATE_NONE);
1050 SLOG(LOG_DEBUG, TAG_VCD, "[Server] mode finalize");
1055 static Eina_Bool __finalize_quit_ecore_loop(void *data)
1057 SLOG(LOG_DEBUG, TAG_VCD, "[Server] quit ecore main loop");
1058 ecore_main_loop_quit();
1062 static void __read_proc()
1065 struct dirent entry;
1066 struct dirent *dirp = NULL;
1071 if (0 < g_list_length(g_proc_list)) {
1072 iter = g_list_first(g_proc_list);
1073 while (NULL != iter) {
1074 g_proc_list = g_list_remove_link(g_proc_list, iter);
1075 iter = g_list_first(g_proc_list);
1079 dp = opendir("/proc");
1081 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Fail to open proc");
1084 ret = readdir_r(dp, &entry, &dirp);
1086 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Fail to readdir");
1091 tmp = atoi(dirp->d_name);
1092 if (0 >= tmp) continue;
1093 g_proc_list = g_list_append(g_proc_list, GINT_TO_POINTER(tmp));
1095 } while (NULL != dirp);
1101 static void __vcd_cleanup_client(vcd_client_type_e type)
1103 int* client_list = NULL;
1104 int client_count = 0;
1111 if (VCD_CLIENT_TYPE_NORMAL == type) {
1112 ret = vcd_client_get_list(&client_list, &client_count);
1113 } else if (VCD_CLIENT_TYPE_WIDGET == type) {
1114 ret = vcd_client_widget_get_list(&client_list, &client_count);
1115 } else if (VCD_CLIENT_TYPE_MANAGER == type) {
1116 mgr_pid = vcd_client_manager_get_pid();
1117 client_list = &mgr_pid;
1119 if (-1 == mgr_pid) {
1120 SLOG(LOG_WARN, TAG_VCD, "[WARNING] Invalid Manager pid");
1125 if (0 == ret || mgr_pid > 0) {
1126 SLOG(LOG_DEBUG, TAG_VCD, "===== Clean up %s client ", type ? (type == 1) ? "Widget" : "Manager" : "Normal");
1127 if (NULL != client_list && client_count > 0) {
1128 for (i = 0; i < client_count; i++) {
1131 for (j = 0; j < g_list_length(g_proc_list); j++) {
1132 iter = g_list_nth(g_proc_list, j);
1134 if (client_list[i] == GPOINTER_TO_INT(iter->data)) {
1135 SLOG(LOG_DEBUG, TAG_VCD, "%s pid(%d) is running", type ? (type == 1) ? "Widget" : "Manager" : "Normal", client_list[i]);
1142 if (false == exist) {
1143 SLOG(LOG_ERROR, TAG_VCD, "%s pid(%d) should be removed", type ? (type == 1) ? "Widget" : "Manager" : "Normal", client_list[i]);
1144 if (VCD_CLIENT_TYPE_NORMAL == type)
1145 vcd_server_finalize(client_list[i]);
1146 else if (VCD_CLIENT_TYPE_WIDGET == type)
1147 vcd_server_widget_finalize(client_list[i]);
1149 vcd_server_mgr_finalize(mgr_pid);
1153 SLOG(LOG_DEBUG, TAG_VCD, "=====");
1154 SLOG(LOG_DEBUG, TAG_VCD, " ");
1156 if (NULL != client_list && -1 == mgr_pid) {
1163 Eina_Bool vcd_cleanup_client_all(void *data)
1167 __vcd_cleanup_client(VCD_CLIENT_TYPE_NORMAL);
1168 __vcd_cleanup_client(VCD_CLIENT_TYPE_WIDGET);
1169 __vcd_cleanup_client(VCD_CLIENT_TYPE_MANAGER);
1172 if (0 == vcd_client_get_list(&client_list, &client_count)) {
1173 SLOG(LOG_DEBUG, TAG_VCD, "===== Clean up client ");
1174 if (NULL != client_list && client_count > 0) {
1175 for (i = 0; i < client_count; i++) {
1178 for (j = 0; j < g_list_length(g_proc_list); j++) {
1179 iter = g_list_nth(g_proc_list, j);
1181 if (client_list[i] == GPOINTER_TO_INT(iter->data)) {
1182 SLOG(LOG_DEBUG, TAG_VCD, "pid(%d) is running", client_list[i]);
1189 if (false == exist) {
1190 SLOG(LOG_ERROR, TAG_VCD, "pid(%d) should be removed", client_list[i]);
1191 vcd_server_finalize(client_list[i]);
1194 result = vcdc_send_hello(client_list[i], VCD_CLIENT_TYPE_NORMAL);
1197 SLOG(LOG_DEBUG, TAG_VCD, "[Server] pid(%d) should be removed.", client_list[i]);
1198 vcd_server_finalize(client_list[i]);
1199 } else if (-1 == result) {
1200 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Hello result has error");
1205 SLOG(LOG_DEBUG, TAG_VCD, "=====");
1206 SLOG(LOG_DEBUG, TAG_VCD, " ");
1208 if (NULL != client_list) {
1213 /* If app is in background state, app cannot response message. */
1214 if (0 == vcd_client_widget_get_list(&client_list, &client_count)) {
1215 SLOG(LOG_DEBUG, TAG_VCD, "===== Clean up widget");
1216 if (NULL != client_list && client_count > 0) {
1217 for (i = 0; i < client_count; i++) {
1220 for (j = 0; j < g_list_length(g_proc_list); j++) {
1221 iter = g_list_nth(g_proc_list, j);
1223 if (client_list[i] == GPOINTER_TO_INT(iter->data)) {
1224 SLOG(LOG_DEBUG, TAG_VCD, "widget pid(%d) is running", client_list[i]);
1231 if (false == exist) {
1232 SLOG(LOG_ERROR, TAG_VCD, "widget pid(%d) should be removed", client_list[i]);
1233 vcd_server_widget_finalize(client_list[i]);
1236 result = vcdc_send_hello(client_list[i], VCD_CLIENT_TYPE_WIDGET);
1239 SLOG(LOG_DEBUG, TAG_VCD, "[Server] widget pid(%d) should be removed.", client_list[i]);
1240 vcd_server_widget_finalize(client_list[i]);
1241 } else if (-1 == result) {
1242 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Hello result has error");
1247 SLOG(LOG_DEBUG, TAG_VCD, "=====");
1248 SLOG(LOG_DEBUG, TAG_VCD, " ");
1251 if (NULL != client_list) {
1259 int mgr_pid = vcd_client_manager_get_pid();
1261 for (j = 0; j < g_list_length(g_proc_list); j++) {
1262 iter = g_list_nth(g_proc_list, j);
1264 if (mgr_pid == GPOINTER_TO_INT(iter->data)) {
1265 SLOG(LOG_DEBUG, TAG_VCD, "manager pid(%d) is running", mgr_pid);
1272 if (false == exist) {
1273 SLOG(LOG_ERROR, TAG_VCD, "manager pid (%d) should be removed", mgr_pid);
1274 vcd_server_mgr_finalize(mgr_pid);
1281 int vcd_server_get_service_state()
1283 return vcd_config_get_service_state();
1286 int vcd_server_get_foreground()
1289 vcd_config_get_foreground(&pid);
1297 int vcd_server_mgr_initialize(int pid)
1299 if (false == g_is_engine) {
1300 if (0 != vcd_engine_agent_initialize_current_engine()) {
1301 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] No Engine");
1302 g_is_engine = false;
1303 return VCD_ERROR_ENGINE_NOT_FOUND;
1309 /* check if pid is valid */
1310 if (false == vcd_client_manager_is_valid(pid)) {
1311 SLOG(LOG_ERROR, TAG_VCD, "[Server] old manager pid(%d) be removed", vcd_client_manager_get_pid());
1312 vcd_server_mgr_cancel();
1313 vcd_client_manager_unset();
1316 /* Add client information to client manager */
1317 if (0 != vcd_client_manager_set(pid)) {
1318 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to add manager");
1319 return VCD_ERROR_OPERATION_FAILED;
1322 if (0 != vcdc_send_manager_pid(pid))
1323 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Fail to send manager pid");
1325 SLOG(LOG_DEBUG, TAG_VCD, "[Server Success] Manager initialize : pid(%d)", pid);
1327 return VCD_ERROR_NONE;
1330 int vcd_server_mgr_finalize(int pid)
1332 /* check if pid is valid */
1333 if (false == vcd_client_manager_is_valid(pid)) {
1334 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] The manager pid(%d) is NOT valid", pid);
1335 return VCD_ERROR_INVALID_PARAMETER;
1338 /* Cancel recognition */
1339 vcd_server_mgr_cancel();
1341 if (0 != vcdc_send_manager_pid(-1))
1342 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Fail to send manager pid");
1344 /* Remove manager information */
1345 if (0 != vcd_client_manager_unset()) {
1346 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Fail to delete client");
1349 if (0 == vcd_client_get_ref_count()) {
1350 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Connected client list is empty");
1351 ecore_timer_add(0, __finalize_quit_ecore_loop, NULL);
1354 SLOG(LOG_DEBUG, TAG_VCD, "[Server Success] Manager Finalize : pid(%d)", pid);
1356 return VCD_ERROR_NONE;
1359 int vcd_server_mgr_set_command(int pid)
1361 if (0 != vcd_client_manager_set_command(pid)) {
1362 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] The manager pid(%d) is NOT valid", pid);
1363 return VCD_ERROR_INVALID_PARAMETER;
1365 return VCD_ERROR_NONE;
1368 int vcd_server_mgr_unset_command(int pid)
1370 if (0 != vcd_client_manager_unset_command(pid)) {
1371 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] The manager pid(%d) is NOT valid", pid);
1372 return VCD_ERROR_INVALID_PARAMETER;
1374 return VCD_ERROR_NONE;
1377 int vcd_server_mgr_set_demandable_client(int pid)
1379 /* check if pid is valid */
1380 if (false == vcd_client_manager_is_valid(pid)) {
1381 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] The manager pid(%d) is NOT valid", pid);
1382 return VCD_ERROR_INVALID_PARAMETER;
1385 GSList* client_list = NULL;
1386 if (0 != vc_info_parser_get_demandable_clients(&client_list)) {
1387 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to get demandable client");
1388 return VCD_ERROR_OPERATION_FAILED;
1391 /* Save client list */
1392 if (0 != vcd_client_manager_set_demandable_client(pid, client_list)) {
1393 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to set demandable client list");
1394 return VCD_ERROR_OPERATION_FAILED;
1397 return VCD_ERROR_NONE;
1400 int vcd_server_mgr_set_audio_type(int pid, const char* audio_type)
1402 /* check if pid is valid */
1403 if (false == vcd_client_manager_is_valid(pid)) {
1404 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] The manager pid(%d) is NOT valid", pid);
1405 return VCD_ERROR_INVALID_PARAMETER;
1409 vcp_audio_type_e type = VCP_AUDIO_TYPE_PCM_S16_LE;
1413 ret = vcd_engine_get_audio_format(audio_type, &type, &rate, &channel);
1415 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to get audio format : %d", ret);
1416 return VCD_ERROR_OPERATION_FAILED;
1419 ret = vcd_recorder_set(audio_type, type, rate, channel);
1421 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to set audio in type : %d", ret);
1422 return VCD_ERROR_OPERATION_FAILED;
1425 return VCD_ERROR_NONE;
1428 int vcd_server_mgr_get_audio_type(int pid, char** audio_type)
1430 /* check if pid is valid */
1431 if (false == vcd_client_manager_is_valid(pid)) {
1432 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] The manager pid(%d) is NOT valid", pid);
1433 return VCD_ERROR_INVALID_PARAMETER;
1436 int ret = vcd_recorder_get(audio_type);
1438 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to get audio in type : %d", ret);
1439 return VCD_ERROR_OPERATION_FAILED;
1442 return VCD_ERROR_NONE;
1445 int vcd_server_mgr_set_client_info(int pid)
1447 /* check if pid is valid */
1448 if (false == vcd_client_manager_is_valid(pid)) {
1449 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] The manager pid(%d) is NOT valid", pid);
1450 return VCD_ERROR_INVALID_PARAMETER;
1453 int ret = vcd_client_save_client_info();
1455 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to save client info : %d", ret);
1456 return VCD_ERROR_OPERATION_FAILED;
1459 return VCD_ERROR_NONE;
1462 static int __start_internal_recognition()
1466 /* 2. Get commands */
1467 ret = vcd_client_command_collect_command();
1469 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to collect command : %d", ret);
1470 return VCD_ERROR_OPERATION_FAILED;
1473 ret = vcd_client_get_length();
1475 SLOG(LOG_WARN, TAG_VCD, "[Server WARNIING] No current command : %d", ret);
1476 return VCD_ERROR_OPERATION_FAILED;
1479 /* 3. Set command to engine */
1480 ret = vcd_engine_set_commands();
1482 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to collect command : %d", ret);
1483 return VCD_ERROR_OPERATION_FAILED;
1486 SLOG(LOG_DEBUG, TAG_VCD, "[Server Success] Set command");
1488 bool stop_by_silence = true;
1489 if (VCD_RECOGNITION_MODE_MANUAL == vcd_client_get_recognition_mode()) {
1490 stop_by_silence = false;
1493 vcd_client_manager_set_result_text(NULL);
1495 /* 4. start recognition */
1496 ret = vcd_engine_recognize_start(stop_by_silence);
1498 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to start recognition : result(%d)", ret);
1499 return VCD_ERROR_OPERATION_FAILED;
1502 SLOG(LOG_DEBUG, TAG_VCD, "[Server Success] Start engine");
1504 /* 5. recorder start */
1505 ret = vcd_recorder_start();
1507 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to start recorder : result(%d)", ret);
1508 vcd_engine_recognize_cancel();
1512 vcd_config_set_service_state(VCD_STATE_RECORDING);
1513 vcdc_send_service_state(VCD_STATE_RECORDING);
1515 SLOG(LOG_DEBUG, TAG_VCD, "[Server Success] Start recognition(%d)", stop_by_silence);
1520 static Eina_Bool __vcd_request_show_tooltip(void *data)
1522 int pid = vcd_client_widget_get_foreground_pid();
1524 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Request tooltip show and widget command");
1525 vcdc_send_show_tooltip(pid, (bool)data);
1531 int vcd_server_mgr_start(vcd_recognition_mode_e recognition_mode, bool exclusive_cmd, bool start_by_client)
1533 /* 1. check current state */
1534 vcd_state_e state = vcd_config_get_service_state();
1536 if (VCD_STATE_READY != state) {
1537 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Current state is not ready");
1538 return VCD_ERROR_INVALID_STATE;
1540 if (-1 == vcd_client_manager_get_pid()) {
1541 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Manager is NOT available.");
1542 return VCD_ERROR_OPERATION_FAILED;
1545 SLOG(LOG_DEBUG, TAG_VCD, "[Server] set recognition mode = %d", recognition_mode);
1546 vcd_client_set_recognition_mode(recognition_mode);
1548 if (false == exclusive_cmd) {
1549 /* Notify show tooltip */
1550 int pid = vcd_client_widget_get_foreground_pid();
1552 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Request tooltip show and widget command");
1553 ecore_timer_add(0, __vcd_request_show_tooltip, (void*)true);
1557 vcd_client_manager_set_exclusive(exclusive_cmd);
1561 if (true == start_by_client) {
1562 /* Get foreground pid */
1563 if (0 != vcd_config_get_foreground(&fg_pid)) {
1564 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to get foreground");
1567 /* Set client exclusive option */
1568 if (0 != vcd_client_set_exclusive_command(fg_pid)) {
1569 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to set exclusive command");
1573 int ret = __start_internal_recognition();
1575 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to start recongition : %d", ret);
1579 if (true == start_by_client) {
1580 vcd_client_unset_exclusive_command(fg_pid);
1583 return VCD_ERROR_NONE;
1586 int vcd_server_mgr_stop()
1588 /* 1. Check current state is recording */
1589 if (VCD_STATE_RECORDING != vcd_config_get_service_state()) {
1590 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Current state is not recording");
1591 return VCD_ERROR_INVALID_STATE;
1593 if (-1 == vcd_client_manager_get_pid()) {
1594 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Manager is NOT available.");
1595 return VCD_ERROR_OPERATION_FAILED;
1598 /* 2. Stop recorder */
1599 vcd_recorder_stop();
1601 /* 3. Stop engine recognition */
1602 int ret = vcd_engine_recognize_stop();
1604 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to stop recognition : %d", ret);
1607 /* 4. Set original mode */
1608 vcd_config_set_service_state(VCD_STATE_PROCESSING);
1609 vcdc_send_service_state(VCD_STATE_PROCESSING);
1611 return VCD_ERROR_NONE;
1614 int vcd_server_mgr_cancel()
1616 /* 1. Check current state */
1617 vcd_state_e state = vcd_config_get_service_state();
1618 if (VCD_STATE_RECORDING != state && VCD_STATE_PROCESSING != state) {
1619 SLOG(LOG_WARN, TAG_VCD, "[Server ERROR] Current state is not recording or processing");
1620 return VCD_ERROR_INVALID_STATE;
1622 if (-1 == vcd_client_manager_get_pid()) {
1623 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Manager is NOT available.");
1624 return VCD_ERROR_OPERATION_FAILED;
1627 if (g_restart_timer != NULL) {
1628 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Delete restart engine timer");
1629 ecore_timer_del(g_restart_timer);
1630 g_restart_timer = NULL;
1633 /* 2. Stop recorder */
1634 vcd_recorder_stop();
1635 /* 3. Cancel engine */
1636 int ret = vcd_engine_recognize_cancel();
1638 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Fail to cancel : %d", ret);
1641 if (false == vcd_client_manager_get_exclusive()) {
1642 int pid = vcd_client_widget_get_foreground_pid();
1644 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Request tooltip hide");
1645 ecore_timer_add(0, __vcd_request_show_tooltip, (void*)false);
1648 vcd_client_manager_set_exclusive(false);
1652 vcd_config_set_service_state(VCD_STATE_READY);
1653 vcdc_send_service_state(VCD_STATE_READY);
1655 return VCD_ERROR_NONE;
1659 int vcd_server_mgr_result_select()
1661 __vcd_send_selected_result(NULL);
1663 return VCD_ERROR_NONE;
1666 int vcd_server_mgr_set_domain(int pid, const char* domain)
1668 /* check if pid is valid */
1669 if (false == vcd_client_manager_is_valid(pid)) {
1670 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] The manager pid(%d) is NOT valid", pid);
1671 return VCD_ERROR_INVALID_PARAMETER;
1674 vcd_state_e state = vcd_config_get_service_state();
1675 if (VCD_STATE_READY != state) {
1676 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Current state is not ready");
1677 return VCD_ERROR_INVALID_STATE;
1680 /* Set domain to engine */
1681 int ret = vcd_engine_set_domain(pid, domain);
1683 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to set domain : %d", ret);
1685 SLOG(LOG_DEBUG, TAG_VCD, "[Server SUCCESS] Set domain");
1691 int vcd_server_mgr_set_private_data(int pid, const char* key, const char* data)
1693 /* check if pid is valid */
1694 if (false == vcd_client_manager_is_valid(pid)) {
1695 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] The manager pid(%d) is NOT valid", pid);
1696 return VCD_ERROR_INVALID_PARAMETER;
1699 vcd_state_e state = vcd_config_get_service_state();
1700 if (VCD_STATE_READY != state) {
1701 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Current state is not ready");
1702 return VCD_ERROR_INVALID_STATE;
1705 /* Set private data to engine */
1706 int ret = vcd_engine_set_private_data(pid, key, data);
1708 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to set private data : %d", ret);
1710 SLOG(LOG_DEBUG, TAG_VCD, "[Server SUCCESS] Set private data");
1716 int vcd_server_mgr_get_private_data(int pid, const char* key, char** data)
1718 /* check if pid is valid */
1719 if (false == vcd_client_manager_is_valid(pid)) {
1720 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] The manager pid(%d) is NOT valid", pid);
1721 return VCD_ERROR_INVALID_PARAMETER;
1723 vcd_state_e state = vcd_config_get_service_state();
1724 if (VCD_STATE_READY != state) {
1725 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Current state is not ready");
1726 return VCD_ERROR_INVALID_STATE;
1729 /* Get private data to engine */
1730 int ret = vcd_engine_get_private_data(pid, key, data);
1732 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to get private data : %d", ret);
1734 SLOG(LOG_DEBUG, TAG_VCD, "[Server SUCCESS] Set private data");
1740 int vcd_server_mgr_do_action(int pid, int type, const char* action)
1744 /* check if pid is valid */
1745 if (false == vcd_client_manager_is_valid(pid)) {
1746 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] The manager pid(%d) is NOT valid", pid);
1747 return VCD_ERROR_INVALID_PARAMETER;
1750 vcd_state_e state = vcd_config_get_service_state();
1751 if (VCD_STATE_READY != state) {
1752 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Current state is not ready");
1753 return VCD_ERROR_INVALID_STATE;
1756 /* Reqeust do action to engine */
1757 if (VCD_SEND_EVENT_TYPE_TEXT == type)
1758 ret = vcd_engine_process_text(pid, action);
1759 else if (VCD_SEND_EVENT_TYPE_LIST_EVENT == type)
1760 ret = vcd_engine_process_list_event(pid, action);
1761 else if (VCD_SEND_EVENT_TYPE_HAPTIC_EVENT == type)
1762 ret = vcd_engine_process_haptic_event(pid, action);
1765 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to process do action : %d", ret);
1767 SLOG(LOG_DEBUG, TAG_VCD, "[Server SUCCESS] Process do action");
1774 * VC Server Functions for Client
1776 int vcd_server_initialize(int pid)
1778 if (false == g_is_engine) {
1779 if (0 != vcd_engine_agent_initialize_current_engine()) {
1780 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] No Engine");
1781 g_is_engine = false;
1782 return VCD_ERROR_ENGINE_NOT_FOUND;
1788 if (false == vcd_engine_is_available_engine()) {
1789 if (0 != vcd_engine_agent_initialize_current_engine()) {
1790 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] No Engine");
1791 return VCD_ERROR_ENGINE_NOT_FOUND;
1795 /* check if pid is valid */
1796 if (true == vcd_client_is_available(pid)) {
1797 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] The pid is already exist");
1798 return VCD_ERROR_INVALID_PARAMETER;
1801 /* Add client information to client manager */
1802 if (0 != vcd_client_add(pid)) {
1803 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to add client info");
1804 return VCD_ERROR_OPERATION_FAILED;
1807 SLOG(LOG_DEBUG, TAG_VCD, "[Server Success] Client Initialize : pid(%d)", pid);
1809 return VCD_ERROR_NONE;
1812 int vcd_server_finalize(int pid)
1814 /* check if pid is valid */
1815 if (false == vcd_client_is_available(pid)) {
1816 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] pid is NOT valid ");
1817 return VCD_ERROR_INVALID_PARAMETER;
1820 /* Remove client information */
1821 if (0 != vcd_client_delete(pid)) {
1822 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Fail to delete client");
1825 if (0 == vcd_client_get_ref_count()) {
1826 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Connected client list is empty");
1827 ecore_timer_add(0, __finalize_quit_ecore_loop, NULL);
1830 SLOG(LOG_DEBUG, TAG_VCD, "[Server Success] Client Finalize : pid(%d)", pid);
1832 return VCD_ERROR_NONE;
1835 int vcd_server_set_command(int pid, vc_cmd_type_e cmd_type)
1837 /* check if pid is valid */
1838 if (false == vcd_client_is_available(pid)) {
1839 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] pid is NOT valid ");
1840 return VCD_ERROR_INVALID_PARAMETER;
1843 if (0 != vcd_client_set_command_type(pid, cmd_type)) {
1844 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to set command type : pid(%d), cmd_type(%d)", pid, cmd_type);
1845 return VCD_ERROR_OPERATION_FAILED;
1851 int vcd_server_unset_command(int pid, vc_cmd_type_e cmd_type)
1853 /* check if pid is valid */
1854 if (false == vcd_client_is_available(pid)) {
1855 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] pid is NOT valid ");
1856 return VCD_ERROR_INVALID_PARAMETER;
1859 if (0 != vcd_client_unset_command_type(pid, cmd_type)) {
1860 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to unset command type : pid(%d), cmd_type(%d)", pid, cmd_type);
1861 return VCD_ERROR_OPERATION_FAILED;
1867 int vcd_server_set_foreground(int pid, bool value)
1869 /* check if pid is valid */
1870 if (false == vcd_client_is_available(pid) && false == vcd_client_widget_is_available(pid)) {
1871 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] pid is NOT valid ");
1872 return VCD_ERROR_INVALID_PARAMETER;
1875 if (0 != vcd_config_set_foreground(pid, value)) {
1876 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to set foreground : pid(%d), value(%d)", pid, value);
1877 return VCD_ERROR_OPERATION_FAILED;
1883 static int __vcd_server_launch_manager_app()
1886 bool running = false;
1889 ret = vcd_client_manager_get_appid(&appid);
1890 if (0 != ret || NULL == appid) {
1891 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Fail to get manager appid");
1892 return VCD_ERROR_OPERATION_FAILED;
1894 ret = app_manager_is_running(appid, &running);
1896 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Fail to check running with appid(%s)", appid);
1899 return VCD_ERROR_OPERATION_FAILED;
1901 if (false == running) {
1902 int tmp_ret = __vcd_is_package_installed(appid);
1903 if (false == tmp_ret) {
1904 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] manager app is not installed, appid(%s)", appid);
1906 ret = __vcd_activate_app_by_appcontrol(appid);
1908 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Fail to activate app");
1913 SLOG(LOG_ERROR, TAG_VCD, "Launch vc manager app: appid(%s)", appid);
1916 ret = __vcd_resume_app(appid);
1918 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Fail to resume app");
1923 SLOG(LOG_ERROR, TAG_VCD, "Resume vc manager app: appid(%s)", appid);
1929 return VCD_ERROR_NONE;
1932 int vcd_server_dialog(int pid, const char* disp_text, const char* utt_text, int continuous)
1934 /* check if pid is valid */
1935 if (false == vcd_client_is_available(pid) && false == vcd_client_widget_is_available(pid)) {
1936 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] pid is NOT valid ");
1937 return VCD_ERROR_INVALID_PARAMETER;
1940 int ret = __vcd_server_launch_manager_app();
1942 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);
1946 ret = vcdc_send_dialog(vcd_client_manager_get_pid(), pid, disp_text, utt_text, continuous);
1948 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);
1955 int vcd_server_is_system_command_valid(int pid, int* is_sys_cmd_valid)
1957 /* check if pid is valid */
1958 if (false == vcd_client_is_available(pid) && false == vcd_client_widget_is_available(pid)) {
1959 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] pid is NOT valid ");
1960 return VCD_ERROR_INVALID_PARAMETER;
1963 /* check if system command is valid */
1964 if (true == vcd_client_manager_is_system_command_valid(vcd_client_manager_get_pid()))
1965 *is_sys_cmd_valid = true;
1967 *is_sys_cmd_valid = false;
1973 int vcd_server_set_exclusive_command(int pid, bool value)
1975 /* check if pid is valid */
1976 if (false == vcd_client_is_available(pid)) {
1977 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] pid is NOT valid ");
1978 return VCD_ERROR_INVALID_PARAMETER;
1981 if (true == value) {
1982 if (0 != vcd_client_set_exclusive_command(pid)) {
1983 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to set exclusive command : pid(%d)", pid);
1984 return VCD_ERROR_OPERATION_FAILED;
1987 if (0 != vcd_client_unset_exclusive_command(pid)) {
1988 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to unset exclusive command : pid(%d)", pid);
1989 return VCD_ERROR_OPERATION_FAILED;
1996 int vcd_server_request_start(int pid, bool stop_by_silence)
1998 /* check if pid is valid */
1999 if (false == vcd_client_is_available(pid)) {
2000 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] pid(%d) is NOT forground client", pid);
2001 return VCD_ERROR_INVALID_PARAMETER;
2005 /* Check current state */
2006 vcd_state_e state = vcd_config_get_service_state();
2008 /* Service state should be ready */
2009 if (VCD_STATE_READY != state) {
2010 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Current state is not Ready : pid(%d)", pid);
2011 return VCD_ERROR_INVALID_STATE;
2014 if (-1 != vcd_client_manager_get_pid()) {
2015 /* Check current pid is valid */
2016 if (false == vcd_client_manager_check_demandable_client(pid)) {
2017 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Current client is NOT available : pid(%d)", pid);
2018 return VCD_ERROR_INVALID_PARAMETER;
2022 ret = vcd_server_mgr_start(stop_by_silence, false);
2024 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Fail to start recognition");
2025 return VCD_ERROR_INVALID_PARAMETER;
2031 int vcd_server_request_stop(int pid)
2034 /* Check current state */
2035 vcd_state_e state = vcd_config_get_service_state();
2037 /* Service state should be ready */
2038 if (VCD_STATE_RECORDING != state) {
2039 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Current state is not Recording : pid(%d)", pid);
2040 return VCD_ERROR_INVALID_STATE;
2043 if (-1 != vcd_client_manager_get_pid()) {
2044 /* Check current pid is valid */
2045 if (false == vcd_client_manager_check_demandable_client(pid)) {
2046 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Current client is NOT available : pid(%d)", pid);
2047 return VCD_ERROR_INVALID_PARAMETER;
2051 ret = vcd_server_mgr_stop();
2053 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Fail to start recognition");
2054 return VCD_ERROR_OPERATION_FAILED;
2057 return VCD_ERROR_NONE;
2060 int vcd_server_request_cancel(int pid)
2063 /* Check current state */
2064 vcd_state_e state = vcd_config_get_service_state();
2066 /* Service state should be recording or processing */
2067 if (VCD_STATE_RECORDING != state && VCD_STATE_PROCESSING != state) {
2068 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Current state is not Recording or Processing : pid(%d)", pid);
2069 return VCD_ERROR_INVALID_STATE;
2072 if (-1 != vcd_client_manager_get_pid()) {
2073 /* Check current pid is valid */
2074 if (false == vcd_client_manager_check_demandable_client(pid)) {
2075 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Current client is NOT available : pid(%d)", pid);
2076 return VCD_ERROR_INVALID_PARAMETER;
2080 ret = vcd_server_mgr_cancel();
2082 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Fail to start recognition");
2083 return VCD_ERROR_OPERATION_FAILED;
2086 return VCD_ERROR_NONE;
2091 * VC Server Functions for Widget lib
2093 int vcd_server_widget_initialize(int pid)
2095 if (false == g_is_engine) {
2096 if (0 != vcd_engine_agent_initialize_current_engine()) {
2097 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] No Engine");
2098 g_is_engine = false;
2099 return VCD_ERROR_ENGINE_NOT_FOUND;
2105 if (false == vcd_engine_is_available_engine()) {
2106 if (0 != vcd_engine_agent_initialize_current_engine()) {
2107 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] No Engine");
2108 return VCD_ERROR_ENGINE_NOT_FOUND;
2112 /* check if pid is valid */
2113 if (true == vcd_client_widget_is_available(pid)) {
2114 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] The pid is already exist");
2115 return VCD_ERROR_INVALID_PARAMETER;
2118 /* Add client information to client manager */
2119 if (0 != vcd_client_widget_add(pid)) {
2120 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to add client info");
2121 return VCD_ERROR_OPERATION_FAILED;
2124 SLOG(LOG_DEBUG, TAG_VCD, "[Server Success] Initialize widget : pid(%d)", pid);
2126 return VCD_ERROR_NONE;
2129 int vcd_server_widget_finalize(int pid)
2131 /* check if pid is valid */
2132 if (false == vcd_client_widget_is_available(pid)) {
2133 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] pid is NOT valid ");
2134 return VCD_ERROR_INVALID_PARAMETER;
2137 /* Remove client information */
2138 if (0 != vcd_client_widget_delete(pid)) {
2139 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Fail to delete client");
2142 if (0 == vcd_client_get_ref_count()) {
2143 SLOG(LOG_DEBUG, TAG_VCD, "[Server] connected client list is empty");
2144 ecore_timer_add(0, __finalize_quit_ecore_loop, NULL);
2147 return VCD_ERROR_NONE;
2150 int vcd_server_widget_start_recording(int pid, bool widget_command)
2152 /* check if pid is valid */
2153 if (false == vcd_client_widget_is_available(pid)) {
2154 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] pid is NOT valid ");
2155 return VCD_ERROR_INVALID_PARAMETER;
2158 if (true == widget_command) {
2159 vcd_client_widget_set_command(pid);
2160 SLOG(LOG_DEBUG, TAG_VCD, "[Server] widget command is available");
2162 vcd_client_widget_unset_command(pid);
2163 SLOG(LOG_WARN, TAG_VCD, "[Server] widget command is NOT available");
2166 int ret = __start_internal_recognition();
2168 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to start recongition : %d", ret);
2169 ecore_timer_add(0, __vcd_request_show_tooltip, (void*)false);
2175 int vcd_server_widget_start(int pid, bool stop_by_silence)
2177 /* check if pid is valid */
2178 int fore_pid = vcd_client_widget_get_foreground_pid();
2179 if (pid != fore_pid) {
2180 SLOG(LOG_ERROR, TAG_VCD, "[Server] pid is NOT foreground");
2181 return VCD_ERROR_INVALID_PARAMETER;
2184 /* Check current state */
2185 vcd_state_e state = vcd_config_get_service_state();
2187 /* Service state should be ready */
2188 if (VCD_STATE_READY != state) {
2189 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Current state is not Ready : pid(%d)", pid);
2190 return VCD_ERROR_INVALID_STATE;
2193 vcd_client_set_slience_detection(stop_by_silence);
2195 /* Notify show tooltip */
2196 ecore_timer_add(0, __vcd_request_show_tooltip, (void*)true);
2201 int vcd_server_widget_stop(int pid)
2203 /* check if pid is valid */
2204 int fore_pid = vcd_client_widget_get_foreground_pid();
2205 if (pid != fore_pid) {
2206 SLOG(LOG_ERROR, TAG_VCD, "[Server] pid is NOT foreground");
2207 return VCD_ERROR_INVALID_PARAMETER;
2211 /* Check current state */
2212 vcd_state_e state = vcd_config_get_service_state();
2214 /* Service state should be recording */
2215 if (VCD_STATE_RECORDING != state) {
2216 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Current service state is not Recording : pid(%d)", pid);
2217 return VCD_ERROR_INVALID_STATE;
2220 ret = vcd_server_mgr_stop();
2222 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Fail to start recognition");
2223 return VCD_ERROR_OPERATION_FAILED;
2226 return VCD_ERROR_NONE;
2229 int vcd_server_widget_cancel(int pid)
2231 /* check if pid is valid */
2232 int fore_pid = vcd_client_widget_get_foreground_pid();
2233 if (pid != fore_pid) {
2234 SLOG(LOG_ERROR, TAG_VCD, "[Server] pid is NOT foreground");
2235 return VCD_ERROR_INVALID_PARAMETER;
2239 /* Check current state */
2240 vcd_state_e state = vcd_config_get_service_state();
2242 /* Service state should be recording or processing */
2243 if (VCD_STATE_RECORDING != state && VCD_STATE_PROCESSING != state) {
2244 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Current state is not Recording or Processing : pid(%d)", pid);
2245 return VCD_ERROR_INVALID_STATE;
2248 ret = vcd_server_mgr_cancel();
2250 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Fail to cancel recognition : %d", ret);
2254 return VCD_ERROR_NONE;
2257 int vcd_server_widget_enable_asr_result(int pid, bool enable)
2260 ret = vcd_client_widget_set_asr_result_enabled(pid, enable);
2262 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Fail to enable asr result : %d", ret);