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");
491 /* if nlu_result is exist, Add command handle(is_action) into result list */
493 SLOG(LOG_DEBUG, TAG_VCD, "[Server] === Get engine result ===");
496 vc_cmd_s* temp_cmd = NULL;
497 vc_cmd_list_h vc_cmd_list = NULL;
499 if (0 != vc_cmd_list_create(&vc_cmd_list)) {
500 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Fail to create command list");
501 vcd_client_manager_set_exclusive(false);
502 vcd_config_set_service_state(VCD_STATE_READY);
503 vcdc_send_service_state(VCD_STATE_READY);
507 /* priority filter */
508 /* system > exclusive > foreground = widget > system_background > background */
510 int* filtered_id = (int*)calloc(count, sizeof(int));
511 int filtered_count = 0;
512 int top_priority = VC_COMMAND_PRIORITY_BACKGROUND;
513 for (i = 0; i < count; i++) {
514 SLOG(LOG_INFO, TAG_VCD, "[Server] [%d] Result id(%d)", i, result_id[i]);
516 if (0 > result_id[i]) {
517 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Result ID(%d) is NOT valid", result_id[i]);
521 ret = vcd_client_get_cmd_from_result_id(result_id[i], &temp_cmd);
522 if (0 == ret && NULL != temp_cmd) {
523 if (top_priority == temp_cmd->priority) {
524 filtered_id[filtered_count] = result_id[i];
526 } else if (top_priority < temp_cmd->priority) {
529 filtered_id[0] = result_id[i];
531 top_priority = temp_cmd->priority;
537 for (i = 0; i < filtered_count; i++) {
538 SLOG(LOG_INFO, TAG_VCD, "[Server] [%d] Filtered Result id(%d)", i, filtered_id[i]);
540 if (filtered_id[i] < 0) {
541 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Filtered ID(%d) is NOT valid", filtered_id[i]);
545 ret = vcd_client_get_cmd_from_result_id(filtered_id[i], &temp_cmd);
546 if (0 == ret && NULL != temp_cmd) {
547 switch (temp_cmd->format) {
548 case VC_CMD_FORMAT_FIXED:
549 case VC_CMD_FORMAT_FIXED_AND_VFIXED:
550 case VC_CMD_FORMAT_VFIXED_AND_FIXED:
551 case VC_CMD_FORMAT_PARTIAL:
552 case VC_CMD_FORMAT_FIXED_AND_NONFIXED:
553 case VC_CMD_FORMAT_NONFIXED_AND_FIXED:
555 case VC_CMD_FORMAT_ACTION:
559 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Unknown command type : %d", temp_cmd->type);
563 if (0 != vc_cmd_list_add(vc_cmd_list, (vc_cmd_h)temp_cmd)) {
564 SLOG(LOG_DEBUG, TAG_VCD, "Fail to add command to list");
565 vc_cmd_destroy((vc_cmd_h)temp_cmd);
568 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] NOT found matached result(%d)", filtered_id[i]);
572 if (NULL != filtered_id) {
577 if (NULL != nlu_result) {
578 SLOG(LOG_INFO, TAG_VCD, "[Server] NLU (%s)", nlu_result);
579 vc_info_parser_set_nlu_result(nlu_result);
580 if (0 == is_action) {
582 if (0 != vc_cmd_create(&nlu_cmd)) {
583 SLOG(LOG_ERROR, TAG_VCD, "Fail to nlu cmd create");
585 if (0 != vc_cmd_set_type(nlu_cmd, VC_COMMAND_TYPE_SYSTEM)) {
586 SLOG(LOG_ERROR, TAG_VCD, "Fail to set type");
588 if (0 != vc_cmd_set_pid(nlu_cmd, vcd_client_manager_get_pid())) {
589 SLOG(LOG_ERROR, TAG_VCD, "Fail to set pid");
591 if (0 != vc_cmd_set_format(nlu_cmd, VC_CMD_FORMAT_ACTION)) {
592 SLOG(LOG_ERROR, TAG_VCD, "Fail to set format");
594 if (0 != vc_cmd_list_add(vc_cmd_list, nlu_cmd)) {
595 SLOG(LOG_ERROR, TAG_VCD, "Fail to add nlu cmd to list");
596 vc_cmd_destroy(nlu_cmd);
602 vc_cmd_print_list(vc_cmd_list);
604 SLOG(LOG_DEBUG, TAG_VCD, "[Server] =========================");
606 int result_count = 0;
607 vc_cmd_list_get_count(vc_cmd_list, &result_count);
609 if (0 == result_count) {
611 if (NULL != all_result) {
612 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Engine result is no command : %s", all_result);
613 bool temp = vcd_client_manager_get_exclusive();
614 vc_info_parser_set_result(all_result, event, msg, NULL, temp);
617 int pid = vcd_client_widget_get_foreground_pid();
619 if (NULL != all_result) {
620 /* Send result text to widget */
621 vcdc_send_result(pid, vcd_client_manager_get_pid(), VC_COMMAND_TYPE_WIDGET);
624 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Request tooltip hide");
625 /* Send to hide tooltip */
626 vcdc_send_show_tooltip(pid, false);
629 if (-1 != vcd_client_manager_get_pid()) {
630 /* Manager client is available */
631 if (0 != vcdc_send_result_to_manager(vcd_client_manager_get_pid(), VC_RESULT_TYPE_NORMAL)) {
632 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Fail to send result");
636 vcd_client_manager_set_exclusive(false);
640 if (false == vcd_client_manager_get_exclusive()) {
641 int pid = vcd_client_widget_get_foreground_pid();
643 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Request tooltip hide");
644 vcdc_send_show_tooltip(pid, false);
647 vc_info_parser_set_result(all_result, event, msg, vc_cmd_list, false);
649 if (-1 != vcd_client_manager_get_pid()) {
650 /* Manager client is available */
651 if (0 != vcdc_send_result_to_manager(vcd_client_manager_get_pid(), VC_RESULT_TYPE_NORMAL)) {
652 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Fail to send result");
655 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Manager is NOT available. Send result to client directly");
656 /* Send result to client */
657 ecore_timer_add(0, __vcd_send_selected_result, NULL);
660 /* exclusive command */
661 vc_info_parser_set_result(all_result, event, msg, vc_cmd_list, true);
663 if (-1 != vcd_client_manager_get_pid()) {
664 /* Manager client is available */
665 if (0 != vcdc_send_result_to_manager(vcd_client_manager_get_pid(), VC_RESULT_TYPE_NORMAL)) {
666 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Fail to send result");
669 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Manager is NOT available");
672 vcd_client_manager_set_exclusive(false);
676 vc_cmd_list_destroy(vc_cmd_list, true);
682 if (NULL == result_id) {
684 if (NULL != all_result) {
685 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Engine result is no command : %s", all_result);
686 bool temp = vcd_client_manager_get_exclusive();
687 vc_info_parser_set_result(all_result, event, msg, NULL, temp);
690 int pid = vcd_client_widget_get_foreground_pid();
692 if (NULL != all_result) {
693 /* Send result text to widget */
694 vcdc_send_result(pid, vcd_client_manager_get_pid(), VC_COMMAND_TYPE_WIDGET);
697 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Request tooltip hide");
698 /* Send to hide tooltip */
699 vcdc_send_show_tooltip(pid, false);
702 if (-1 != vcd_client_manager_get_pid()) {
703 /* Manager client is available */
704 if (0 != vcdc_send_result_to_manager(vcd_client_manager_get_pid(), VC_RESULT_TYPE_NORMAL)) {
705 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Fail to send result");
709 vcd_client_manager_set_exclusive(false);
715 SLOG(LOG_DEBUG, TAG_VCD, "[Server] === Get engine result ===");
718 vc_cmd_s* temp_cmd = NULL;
719 vc_cmd_list_h vc_cmd_list = NULL;
721 if (0 != vc_cmd_list_create(&vc_cmd_list)) {
722 SLOG(LOG_ERROR, TAG_VCD, "[Server] Fail to create command list");
723 vcd_client_manager_set_exclusive(false);
724 vcd_config_set_service_state(VCD_STATE_READY);
725 vcdc_send_service_state(VCD_STATE_READY);
730 for (i = 0; i < count; i++) {
731 SLOG(LOG_DEBUG, TAG_VCD, "[Server] [%d] Result ID(%d)", i, result_id[i]);
733 if (result_id[i] < 0) {
734 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Result ID(%d) is NOT valid", result_id[i]);
738 ret = vcd_client_get_cmd_from_result_id(result_id[i], &temp_cmd);
739 if (0 == ret && NULL != temp_cmd) {
740 switch (temp_cmd->format) {
741 case VC_CMD_FORMAT_FIXED:
742 case VC_CMD_FORMAT_FIXED_AND_VFIXED:
743 case VC_CMD_FORMAT_VFIXED_AND_FIXED:
744 case VC_CMD_FORMAT_PARTIAL:
745 /* Nonfixed result is NOT valid */
747 case VC_CMD_FORMAT_FIXED_AND_NONFIXED:
748 if (NULL == temp_cmd->parameter) {
749 if (NULL != non_fixed_result) {
750 temp_cmd->parameter = strdup(non_fixed_result);
753 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Command type is NOT vaild. Parameter (%s)", temp_cmd->parameter);
756 case VC_CMD_FORMAT_NONFIXED_AND_FIXED:
757 if (NULL == temp_cmd->command) {
758 if (NULL != non_fixed_result) {
759 temp_cmd->command = strdup(non_fixed_result);
762 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Command type is NOT vaild. Command (%s)", temp_cmd->command);
767 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Unknown command type : %d", temp_cmd->type);
771 if (0 != vc_cmd_list_add(vc_cmd_list, (vc_cmd_h)temp_cmd)) {
772 SLOG(LOG_DEBUG, TAG_VCD, "Fail to add command to list");
773 vc_cmd_destroy((vc_cmd_h)temp_cmd);
776 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] NOT found matached result(%d)", result_id[i]);
780 vc_cmd_print_list(vc_cmd_list);
782 SLOG(LOG_DEBUG, TAG_VCD, "[Server] =========================");
784 int result_count = 0;
785 vc_cmd_list_get_count(vc_cmd_list, &result_count);
787 if (false == vcd_client_manager_get_exclusive()) {
788 int pid = vcd_client_widget_get_foreground_pid();
790 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Request tooltip hide");
791 vcdc_send_show_tooltip(pid, false);
794 vc_info_parser_set_result(all_result, event, msg, vc_cmd_list, false);
796 if (-1 != vcd_client_manager_get_pid()) {
797 /* Manager client is available */
798 if (0 != vcdc_send_result_to_manager(vcd_client_manager_get_pid(), VC_RESULT_TYPE_NORMAL)) {
799 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Fail to send result");
802 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Manager is NOT available. Send result to client directly");
803 /* Send result to client */
804 ecore_timer_add(0, __vcd_send_selected_result, NULL);
807 /* exclusive command */
808 vc_info_parser_set_result(all_result, event, msg, vc_cmd_list, true);
810 if (-1 != vcd_client_manager_get_pid()) {
811 /* Manager client is available */
812 if (0 != vcdc_send_result_to_manager(vcd_client_manager_get_pid(), VC_RESULT_TYPE_NORMAL)) {
813 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Fail to send result");
816 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Manager is NOT available");
819 vcd_client_manager_set_exclusive(false);
822 vc_cmd_list_destroy(vc_cmd_list, true);
829 static void __vcd_server_nlu_result_cb(vcp_result_event_e event, const char* nlu_result, void *user_data)
831 SLOG(LOG_DEBUG, TAG_VCD, "[Server] NLU result cb - event(%d)", event);
832 SLOG(LOG_DEBUG, TAG_VCD, "[Server] result (%s)", nlu_result);
834 int ret = vc_info_parser_set_nlu_result(nlu_result);
836 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to set nlu result (%d)", ret);
843 static void __vcd_server_error_cb(vcp_error_e error, const char* msg, void *user_data)
845 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Engine Error cb - reason(%d), msg(%s)", error, msg);
846 ecore_main_loop_thread_safe_call_async(__cancel_by_error, NULL);
848 char* error_msg = NULL;
849 error_msg = strdup(msg);
851 if (0 != vcdc_send_error_signal_to_manager(vcd_client_manager_get_pid(), error, error_msg)) {
852 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to send error signal");
855 if (NULL != error_msg) {
864 * vcd server Interfaces
866 static void __vcd_file_clean_up()
868 SLOG(LOG_DEBUG, TAG_VCD, "== Old file clean up == ");
871 struct dirent *dirp = NULL;
873 dp = opendir(VC_RUNTIME_INFO_ROOT);
875 SLOG(LOG_ERROR, TAG_VCD, "[File message WARN] Fail to open path : %s", VC_RUNTIME_INFO_ROOT);
879 char remove_path[256] = {0, };
884 if (!strncmp("vc_", dirp->d_name, strlen("vc_"))) {
885 memset(remove_path, 0, 256);
886 snprintf(remove_path, 256, "%s/%s", VC_RUNTIME_INFO_ROOT, dirp->d_name);
889 if (0 != remove(remove_path)) {
890 SLOG(LOG_WARN, TAG_VCD, "[File message WARN] Fail to remove file : %s", remove_path);
892 SLOG(LOG_DEBUG, TAG_VCD, "[File message] Remove file : %s", remove_path);
896 } while (NULL != dirp);
903 static int __vcd_db_clean_up()
906 int cnt = VC_COMMAND_TYPE_FOREGROUND;
908 if (VC_COMMAND_TYPE_BACKGROUND != cnt)
909 ret = vc_db_delete_commands(-1, cnt, NULL);
910 } while (VC_COMMAND_TYPE_EXCLUSIVE >= ++cnt);
919 /* Remove old file */
920 __vcd_file_clean_up();
922 /* initialize modules */
923 ret = vcd_config_initialize(__config_lang_changed_cb, __config_foreground_changed_cb, NULL);
925 SLOG(LOG_ERROR, TAG_VCD, "[Server WARNING] Fail to initialize config.");
929 ret = __vcd_db_clean_up();
931 SLOG(LOG_ERROR, TAG_VCD, "[Server WARNING] Fail to remove db data");
934 vcd_config_set_service_state(VCD_STATE_NONE);
936 //ret = vcd_engine_agent_init(__vcd_server_pre_result_cb, __vcd_server_result_cb, __vcd_server_nlu_result_cb, __vcd_server_error_cb);
937 ret = vcd_engine_agent_init(__vcd_server_asr_result_cb, __vcd_server_result_cb, __vcd_server_error_cb);
939 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to engine agent initialize : result(%d)", ret);
943 if (0 != vcd_recorder_create(__server_recorder_callback, __server_recorder_interrupt_callback)) {
944 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to create recorder");
945 return VCD_ERROR_OPERATION_FAILED;
949 ret = vcd_engine_agent_initialize_current_engine();
951 if (VCD_ERROR_ENGINE_NOT_FOUND == ret)
952 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] There is No Voice control engine");
954 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to init engine");
962 if (0 != vcd_engine_agent_load_current_engine()) {
963 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to load current engine");
964 return VCD_ERROR_OPERATION_FAILED;
967 /* Initialize manager info */
968 vcd_client_manager_unset();
970 vcd_config_set_service_state(VCD_STATE_READY);
971 vcdc_send_service_state(VCD_STATE_READY);
973 SLOG(LOG_DEBUG, TAG_VCD, "[Server SUCCESS] initialize");
981 if (0 < g_list_length(g_proc_list)) {
982 iter = g_list_first(g_proc_list);
983 while (NULL != iter) {
984 g_proc_list = g_list_remove_link(g_proc_list, iter);
985 iter = g_list_first(g_proc_list);
989 if (g_restart_timer != NULL) {
990 ecore_timer_del(g_restart_timer);
991 g_restart_timer = NULL;
994 vcd_state_e state = vcd_config_get_service_state();
995 if (VCD_STATE_READY != state) {
996 if (VCD_STATE_RECORDING == state) {
999 vcd_engine_recognize_cancel();
1001 if (0 != vcd_recorder_destroy()) {
1002 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to destroy recorder");
1004 SLOG(LOG_DEBUG, TAG_VCD, "[Server] destroy recorder");
1007 if (0 != vcd_engine_agent_release()) {
1008 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to release engine");
1010 SLOG(LOG_DEBUG, TAG_VCD, "[Server] release engine");
1013 vcd_client_manager_unset_appid();
1015 vcd_config_set_service_state(VCD_STATE_NONE);
1016 vcdc_send_service_state(VCD_STATE_NONE);
1018 SLOG(LOG_DEBUG, TAG_VCD, "[Server] mode finalize");
1023 static Eina_Bool __finalize_quit_ecore_loop(void *data)
1025 SLOG(LOG_DEBUG, TAG_VCD, "[Server] quit ecore main loop");
1026 ecore_main_loop_quit();
1030 static void __read_proc()
1033 struct dirent *dirp = NULL;
1037 if (0 < g_list_length(g_proc_list)) {
1038 iter = g_list_first(g_proc_list);
1039 while (NULL != iter) {
1040 g_proc_list = g_list_remove_link(g_proc_list, iter);
1041 iter = g_list_first(g_proc_list);
1045 dp = opendir("/proc");
1047 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Fail to open proc");
1053 tmp = atoi(dirp->d_name);
1054 if (0 >= tmp) continue;
1055 g_proc_list = g_list_append(g_proc_list, GINT_TO_POINTER(tmp));
1057 } while (NULL != dirp);
1063 static void __vcd_cleanup_client(vcd_client_type_e type)
1065 int* client_list = NULL;
1066 int client_count = 0;
1073 if (VCD_CLIENT_TYPE_NORMAL == type) {
1074 ret = vcd_client_get_list(&client_list, &client_count);
1075 } else if (VCD_CLIENT_TYPE_WIDGET == type) {
1076 ret = vcd_client_widget_get_list(&client_list, &client_count);
1077 } else if (VCD_CLIENT_TYPE_MANAGER == type) {
1078 mgr_pid = vcd_client_manager_get_pid();
1079 client_list = &mgr_pid;
1081 if (-1 == mgr_pid) {
1082 SLOG(LOG_WARN, TAG_VCD, "[WARNING] Invalid Manager pid");
1087 if (0 == ret || mgr_pid > 0) {
1088 SLOG(LOG_DEBUG, TAG_VCD, "===== Clean up %s client ", type ? (type == 1) ? "Widget" : "Manager" : "Normal");
1089 if (NULL != client_list && client_count > 0) {
1090 for (i = 0; i < client_count; i++) {
1093 for (j = 0; j < g_list_length(g_proc_list); j++) {
1094 iter = g_list_nth(g_proc_list, j);
1096 if (client_list[i] == GPOINTER_TO_INT(iter->data)) {
1097 SLOG(LOG_DEBUG, TAG_VCD, "%s pid(%d) is running", type ? (type == 1) ? "Widget" : "Manager" : "Normal", client_list[i]);
1104 if (false == exist) {
1105 SLOG(LOG_ERROR, TAG_VCD, "%s pid(%d) should be removed", type ? (type == 1) ? "Widget" : "Manager" : "Normal", client_list[i]);
1106 if (VCD_CLIENT_TYPE_NORMAL == type)
1107 vcd_server_finalize(client_list[i]);
1108 else if (VCD_CLIENT_TYPE_WIDGET == type)
1109 vcd_server_widget_finalize(client_list[i]);
1111 vcd_server_mgr_finalize(mgr_pid);
1115 SLOG(LOG_DEBUG, TAG_VCD, "=====");
1116 SLOG(LOG_DEBUG, TAG_VCD, " ");
1118 if (NULL != client_list && -1 == mgr_pid) {
1125 Eina_Bool vcd_cleanup_client_all(void *data)
1129 __vcd_cleanup_client(VCD_CLIENT_TYPE_NORMAL);
1130 __vcd_cleanup_client(VCD_CLIENT_TYPE_WIDGET);
1131 __vcd_cleanup_client(VCD_CLIENT_TYPE_MANAGER);
1134 if (0 == vcd_client_get_list(&client_list, &client_count)) {
1135 SLOG(LOG_DEBUG, TAG_VCD, "===== Clean up client ");
1136 if (NULL != client_list && client_count > 0) {
1137 for (i = 0; i < client_count; i++) {
1140 for (j = 0; j < g_list_length(g_proc_list); j++) {
1141 iter = g_list_nth(g_proc_list, j);
1143 if (client_list[i] == GPOINTER_TO_INT(iter->data)) {
1144 SLOG(LOG_DEBUG, TAG_VCD, "pid(%d) is running", client_list[i]);
1151 if (false == exist) {
1152 SLOG(LOG_ERROR, TAG_VCD, "pid(%d) should be removed", client_list[i]);
1153 vcd_server_finalize(client_list[i]);
1156 result = vcdc_send_hello(client_list[i], VCD_CLIENT_TYPE_NORMAL);
1159 SLOG(LOG_DEBUG, TAG_VCD, "[Server] pid(%d) should be removed.", client_list[i]);
1160 vcd_server_finalize(client_list[i]);
1161 } else if (-1 == result) {
1162 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Hello result has error");
1167 SLOG(LOG_DEBUG, TAG_VCD, "=====");
1168 SLOG(LOG_DEBUG, TAG_VCD, " ");
1170 if (NULL != client_list) {
1175 /* If app is in background state, app cannot response message. */
1176 if (0 == vcd_client_widget_get_list(&client_list, &client_count)) {
1177 SLOG(LOG_DEBUG, TAG_VCD, "===== Clean up widget");
1178 if (NULL != client_list && client_count > 0) {
1179 for (i = 0; i < client_count; i++) {
1182 for (j = 0; j < g_list_length(g_proc_list); j++) {
1183 iter = g_list_nth(g_proc_list, j);
1185 if (client_list[i] == GPOINTER_TO_INT(iter->data)) {
1186 SLOG(LOG_DEBUG, TAG_VCD, "widget pid(%d) is running", client_list[i]);
1193 if (false == exist) {
1194 SLOG(LOG_ERROR, TAG_VCD, "widget pid(%d) should be removed", client_list[i]);
1195 vcd_server_widget_finalize(client_list[i]);
1198 result = vcdc_send_hello(client_list[i], VCD_CLIENT_TYPE_WIDGET);
1201 SLOG(LOG_DEBUG, TAG_VCD, "[Server] widget pid(%d) should be removed.", client_list[i]);
1202 vcd_server_widget_finalize(client_list[i]);
1203 } else if (-1 == result) {
1204 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Hello result has error");
1209 SLOG(LOG_DEBUG, TAG_VCD, "=====");
1210 SLOG(LOG_DEBUG, TAG_VCD, " ");
1213 if (NULL != client_list) {
1221 int mgr_pid = vcd_client_manager_get_pid();
1223 for (j = 0; j < g_list_length(g_proc_list); j++) {
1224 iter = g_list_nth(g_proc_list, j);
1226 if (mgr_pid == GPOINTER_TO_INT(iter->data)) {
1227 SLOG(LOG_DEBUG, TAG_VCD, "manager pid(%d) is running", mgr_pid);
1234 if (false == exist) {
1235 SLOG(LOG_ERROR, TAG_VCD, "manager pid (%d) should be removed", mgr_pid);
1236 vcd_server_mgr_finalize(mgr_pid);
1243 int vcd_server_get_service_state()
1245 return vcd_config_get_service_state();
1248 int vcd_server_get_foreground()
1251 vcd_config_get_foreground(&pid);
1259 int vcd_server_mgr_initialize(int pid)
1261 if (false == g_is_engine) {
1262 if (0 != vcd_engine_agent_initialize_current_engine()) {
1263 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] No Engine");
1264 g_is_engine = false;
1265 return VCD_ERROR_ENGINE_NOT_FOUND;
1271 /* check if pid is valid */
1272 if (false == vcd_client_manager_is_valid(pid)) {
1273 SLOG(LOG_ERROR, TAG_VCD, "[Server] old manager pid(%d) be removed", vcd_client_manager_get_pid());
1274 vcd_server_mgr_cancel();
1275 vcd_client_manager_unset();
1278 /* Add client information to client manager */
1279 if (0 != vcd_client_manager_set(pid)) {
1280 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to add manager");
1281 return VCD_ERROR_OPERATION_FAILED;
1284 if (0 != vcdc_send_manager_pid(pid))
1285 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Fail to send manager pid");
1287 SLOG(LOG_DEBUG, TAG_VCD, "[Server Success] Manager initialize : pid(%d)", pid);
1289 return VCD_ERROR_NONE;
1292 int vcd_server_mgr_finalize(int pid)
1294 /* check if pid is valid */
1295 if (false == vcd_client_manager_is_valid(pid)) {
1296 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] The manager pid(%d) is NOT valid", pid);
1297 return VCD_ERROR_INVALID_PARAMETER;
1300 /* Cancel recognition */
1301 vcd_server_mgr_cancel();
1303 if (0 != vcdc_send_manager_pid(-1))
1304 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Fail to send manager pid");
1306 /* Remove manager information */
1307 if (0 != vcd_client_manager_unset()) {
1308 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Fail to delete client");
1311 if (0 == vcd_client_get_ref_count()) {
1312 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Connected client list is empty");
1313 ecore_timer_add(0, __finalize_quit_ecore_loop, NULL);
1316 SLOG(LOG_DEBUG, TAG_VCD, "[Server Success] Manager Finalize : pid(%d)", pid);
1318 return VCD_ERROR_NONE;
1321 int vcd_server_mgr_set_command(int pid)
1323 if (0 != vcd_client_manager_set_command(pid)) {
1324 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] The manager pid(%d) is NOT valid", pid);
1325 return VCD_ERROR_INVALID_PARAMETER;
1327 return VCD_ERROR_NONE;
1330 int vcd_server_mgr_unset_command(int pid)
1332 if (0 != vcd_client_manager_unset_command(pid)) {
1333 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] The manager pid(%d) is NOT valid", pid);
1334 return VCD_ERROR_INVALID_PARAMETER;
1336 return VCD_ERROR_NONE;
1339 int vcd_server_mgr_set_demandable_client(int pid)
1341 /* check if pid is valid */
1342 if (false == vcd_client_manager_is_valid(pid)) {
1343 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] The manager pid(%d) is NOT valid", pid);
1344 return VCD_ERROR_INVALID_PARAMETER;
1347 GSList* client_list = NULL;
1348 if (0 != vc_info_parser_get_demandable_clients(&client_list)) {
1349 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to get demandable client");
1350 return VCD_ERROR_OPERATION_FAILED;
1353 /* Save client list */
1354 if (0 != vcd_client_manager_set_demandable_client(pid, client_list)) {
1355 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to set demandable client list");
1356 return VCD_ERROR_OPERATION_FAILED;
1359 return VCD_ERROR_NONE;
1362 int vcd_server_mgr_set_audio_type(int pid, const char* audio_type)
1364 /* check if pid is valid */
1365 if (false == vcd_client_manager_is_valid(pid)) {
1366 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] The manager pid(%d) is NOT valid", pid);
1367 return VCD_ERROR_INVALID_PARAMETER;
1371 vcp_audio_type_e type = VCP_AUDIO_TYPE_PCM_S16_LE;
1375 ret = vcd_engine_get_audio_format(audio_type, &type, &rate, &channel);
1377 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to get audio format : %d", ret);
1378 return VCD_ERROR_OPERATION_FAILED;
1381 ret = vcd_recorder_set(audio_type, type, rate, channel);
1383 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to set audio in type : %d", ret);
1384 return VCD_ERROR_OPERATION_FAILED;
1387 return VCD_ERROR_NONE;
1390 int vcd_server_mgr_get_audio_type(int pid, 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;
1398 int ret = vcd_recorder_get(audio_type);
1400 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to get audio in type : %d", ret);
1401 return VCD_ERROR_OPERATION_FAILED;
1404 return VCD_ERROR_NONE;
1407 int vcd_server_mgr_set_client_info(int pid)
1409 /* check if pid is valid */
1410 if (false == vcd_client_manager_is_valid(pid)) {
1411 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] The manager pid(%d) is NOT valid", pid);
1412 return VCD_ERROR_INVALID_PARAMETER;
1415 int ret = vcd_client_save_client_info();
1417 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to save client info : %d", ret);
1418 return VCD_ERROR_OPERATION_FAILED;
1421 return VCD_ERROR_NONE;
1424 static int __start_internal_recognition()
1428 /* 2. Get commands */
1429 ret = vcd_client_command_collect_command();
1431 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to collect command : %d", ret);
1432 return VCD_ERROR_OPERATION_FAILED;
1435 ret = vcd_client_get_length();
1437 SLOG(LOG_WARN, TAG_VCD, "[Server WARNIING] No current command : %d", ret);
1438 return VCD_ERROR_OPERATION_FAILED;
1441 /* 3. Set command to engine */
1442 ret = vcd_engine_set_commands();
1444 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to collect command : %d", ret);
1445 return VCD_ERROR_OPERATION_FAILED;
1448 SLOG(LOG_DEBUG, TAG_VCD, "[Server Success] Set command");
1450 bool stop_by_silence = true;
1451 if (VCD_RECOGNITION_MODE_MANUAL == vcd_client_get_recognition_mode()) {
1452 stop_by_silence = false;
1455 vcd_client_manager_set_result_text(NULL);
1457 /* 4. start recognition */
1458 ret = vcd_engine_recognize_start(stop_by_silence);
1460 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to start recognition : result(%d)", ret);
1461 return VCD_ERROR_OPERATION_FAILED;
1464 SLOG(LOG_DEBUG, TAG_VCD, "[Server Success] Start engine");
1466 /* 5. recorder start */
1467 ret = vcd_recorder_start();
1469 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to start recorder : result(%d)", ret);
1470 vcd_engine_recognize_cancel();
1474 vcd_config_set_service_state(VCD_STATE_RECORDING);
1475 vcdc_send_service_state(VCD_STATE_RECORDING);
1477 SLOG(LOG_DEBUG, TAG_VCD, "[Server Success] Start recognition(%d)", stop_by_silence);
1482 static Eina_Bool __vcd_request_show_tooltip(void *data)
1484 int pid = vcd_client_widget_get_foreground_pid();
1486 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Request tooltip show and widget command");
1487 vcdc_send_show_tooltip(pid, (bool)data);
1493 int vcd_server_mgr_start(vcd_recognition_mode_e recognition_mode, bool exclusive_cmd, bool start_by_client)
1495 /* 1. check current state */
1496 vcd_state_e state = vcd_config_get_service_state();
1498 if (VCD_STATE_READY != state) {
1499 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Current state is not ready");
1500 return VCD_ERROR_INVALID_STATE;
1502 if (-1 == vcd_client_manager_get_pid()) {
1503 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Manager is NOT available.");
1504 return VCD_ERROR_OPERATION_FAILED;
1507 SLOG(LOG_DEBUG, TAG_VCD, "[Server] set recognition mode = %d", recognition_mode);
1508 vcd_client_set_recognition_mode(recognition_mode);
1510 if (false == exclusive_cmd) {
1511 /* Notify show tooltip */
1512 int pid = vcd_client_widget_get_foreground_pid();
1514 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Request tooltip show and widget command");
1515 ecore_timer_add(0, __vcd_request_show_tooltip, (void*)true);
1519 vcd_client_manager_set_exclusive(exclusive_cmd);
1523 if (true == start_by_client) {
1524 /* Get foreground pid */
1525 if (0 != vcd_config_get_foreground(&fg_pid)) {
1526 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to get foreground");
1529 /* Set client exclusive option */
1530 if (0 != vcd_client_set_exclusive_command(fg_pid)) {
1531 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to set exclusive command");
1535 int ret = __start_internal_recognition();
1537 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to start recongition : %d", ret);
1541 if (true == start_by_client) {
1542 vcd_client_unset_exclusive_command(fg_pid);
1545 return VCD_ERROR_NONE;
1548 int vcd_server_mgr_stop()
1550 /* 1. Check current state is recording */
1551 if (VCD_STATE_RECORDING != vcd_config_get_service_state()) {
1552 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Current state is not recording");
1553 return VCD_ERROR_INVALID_STATE;
1555 if (-1 == vcd_client_manager_get_pid()) {
1556 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Manager is NOT available.");
1557 return VCD_ERROR_OPERATION_FAILED;
1560 /* 2. Stop recorder */
1561 vcd_recorder_stop();
1563 /* 3. Stop engine recognition */
1564 int ret = vcd_engine_recognize_stop();
1566 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to stop recognition : %d", ret);
1569 /* 4. Set original mode */
1570 vcd_config_set_service_state(VCD_STATE_PROCESSING);
1571 vcdc_send_service_state(VCD_STATE_PROCESSING);
1573 return VCD_ERROR_NONE;
1576 int vcd_server_mgr_cancel()
1578 /* 1. Check current state */
1579 vcd_state_e state = vcd_config_get_service_state();
1580 if (VCD_STATE_RECORDING != state && VCD_STATE_PROCESSING != state) {
1581 SLOG(LOG_WARN, TAG_VCD, "[Server ERROR] Current state is not recording or processing");
1582 return VCD_ERROR_INVALID_STATE;
1584 if (-1 == vcd_client_manager_get_pid()) {
1585 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Manager is NOT available.");
1586 return VCD_ERROR_OPERATION_FAILED;
1589 if (g_restart_timer != NULL) {
1590 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Delete restart engine timer");
1591 ecore_timer_del(g_restart_timer);
1592 g_restart_timer = NULL;
1595 /* 2. Stop recorder */
1596 vcd_recorder_stop();
1597 /* 3. Cancel engine */
1598 int ret = vcd_engine_recognize_cancel();
1600 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Fail to cancel : %d", ret);
1603 if (false == vcd_client_manager_get_exclusive()) {
1604 int pid = vcd_client_widget_get_foreground_pid();
1606 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Request tooltip hide");
1607 ecore_timer_add(0, __vcd_request_show_tooltip, (void*)false);
1610 vcd_client_manager_set_exclusive(false);
1614 vcd_config_set_service_state(VCD_STATE_READY);
1615 vcdc_send_service_state(VCD_STATE_READY);
1617 return VCD_ERROR_NONE;
1621 int vcd_server_mgr_result_select()
1623 __vcd_send_selected_result(NULL);
1625 return VCD_ERROR_NONE;
1628 int vcd_server_mgr_set_domain(int pid, const char* domain)
1630 /* check if pid is valid */
1631 if (false == vcd_client_manager_is_valid(pid)) {
1632 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] The manager pid(%d) is NOT valid", pid);
1633 return VCD_ERROR_INVALID_PARAMETER;
1636 vcd_state_e state = vcd_config_get_service_state();
1637 if (VCD_STATE_READY != state) {
1638 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Current state is not ready");
1639 return VCD_ERROR_INVALID_STATE;
1642 /* Set domain to engine */
1643 int ret = vcd_engine_set_domain(pid, domain);
1645 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to set domain : %d", ret);
1647 SLOG(LOG_DEBUG, TAG_VCD, "[Server SUCCESS] Set domain");
1653 int vcd_server_mgr_set_private_data(int pid, const char* key, const char* data)
1655 /* check if pid is valid */
1656 if (false == vcd_client_manager_is_valid(pid)) {
1657 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] The manager pid(%d) is NOT valid", pid);
1658 return VCD_ERROR_INVALID_PARAMETER;
1661 vcd_state_e state = vcd_config_get_service_state();
1662 if (VCD_STATE_READY != state) {
1663 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Current state is not ready");
1664 return VCD_ERROR_INVALID_STATE;
1667 /* Set private data to engine */
1668 int ret = vcd_engine_set_private_data(pid, key, data);
1670 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to set private data : %d", ret);
1672 SLOG(LOG_DEBUG, TAG_VCD, "[Server SUCCESS] Set private data");
1678 int vcd_server_mgr_get_private_data(int pid, const char* key, char** data)
1680 /* check if pid is valid */
1681 if (false == vcd_client_manager_is_valid(pid)) {
1682 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] The manager pid(%d) is NOT valid", pid);
1683 return VCD_ERROR_INVALID_PARAMETER;
1685 vcd_state_e state = vcd_config_get_service_state();
1686 if (VCD_STATE_READY != state) {
1687 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Current state is not ready");
1688 return VCD_ERROR_INVALID_STATE;
1691 /* Get private data to engine */
1692 int ret = vcd_engine_get_private_data(pid, key, data);
1694 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to get private data : %d", ret);
1696 SLOG(LOG_DEBUG, TAG_VCD, "[Server SUCCESS] Set private data");
1702 int vcd_server_mgr_do_action(int pid, int type, const char* action)
1706 /* check if pid is valid */
1707 if (false == vcd_client_manager_is_valid(pid)) {
1708 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] The manager pid(%d) is NOT valid", pid);
1709 return VCD_ERROR_INVALID_PARAMETER;
1712 vcd_state_e state = vcd_config_get_service_state();
1713 if (VCD_STATE_READY != state) {
1714 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Current state is not ready");
1715 return VCD_ERROR_INVALID_STATE;
1718 /* Reqeust do action to engine */
1719 if (VCD_SEND_EVENT_TYPE_TEXT == type)
1720 ret = vcd_engine_process_text(pid, action);
1721 else if (VCD_SEND_EVENT_TYPE_LIST_EVENT == type)
1722 ret = vcd_engine_process_list_event(pid, action);
1723 else if (VCD_SEND_EVENT_TYPE_HAPTIC_EVENT == type)
1724 ret = vcd_engine_process_haptic_event(pid, action);
1727 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to process do action : %d", ret);
1729 SLOG(LOG_DEBUG, TAG_VCD, "[Server SUCCESS] Process do action");
1736 * VC Server Functions for Client
1738 int vcd_server_initialize(int pid)
1740 if (false == g_is_engine) {
1741 if (0 != vcd_engine_agent_initialize_current_engine()) {
1742 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] No Engine");
1743 g_is_engine = false;
1744 return VCD_ERROR_ENGINE_NOT_FOUND;
1750 if (false == vcd_engine_is_available_engine()) {
1751 if (0 != vcd_engine_agent_initialize_current_engine()) {
1752 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] No Engine");
1753 return VCD_ERROR_ENGINE_NOT_FOUND;
1757 /* check if pid is valid */
1758 if (true == vcd_client_is_available(pid)) {
1759 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] The pid is already exist");
1760 return VCD_ERROR_INVALID_PARAMETER;
1763 /* Add client information to client manager */
1764 if (0 != vcd_client_add(pid)) {
1765 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to add client info");
1766 return VCD_ERROR_OPERATION_FAILED;
1769 SLOG(LOG_DEBUG, TAG_VCD, "[Server Success] Client Initialize : pid(%d)", pid);
1771 return VCD_ERROR_NONE;
1774 int vcd_server_finalize(int pid)
1776 /* check if pid is valid */
1777 if (false == vcd_client_is_available(pid)) {
1778 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] pid is NOT valid ");
1779 return VCD_ERROR_INVALID_PARAMETER;
1782 /* Remove client information */
1783 if (0 != vcd_client_delete(pid)) {
1784 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Fail to delete client");
1787 if (0 == vcd_client_get_ref_count()) {
1788 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Connected client list is empty");
1789 ecore_timer_add(0, __finalize_quit_ecore_loop, NULL);
1792 SLOG(LOG_DEBUG, TAG_VCD, "[Server Success] Client Finalize : pid(%d)", pid);
1794 return VCD_ERROR_NONE;
1797 int vcd_server_set_command(int pid, vc_cmd_type_e cmd_type)
1799 /* check if pid is valid */
1800 if (false == vcd_client_is_available(pid)) {
1801 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] pid is NOT valid ");
1802 return VCD_ERROR_INVALID_PARAMETER;
1805 if (0 != vcd_client_set_command_type(pid, cmd_type)) {
1806 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to set command type : pid(%d), cmd_type(%d)", pid, cmd_type);
1807 return VCD_ERROR_OPERATION_FAILED;
1813 int vcd_server_unset_command(int pid, vc_cmd_type_e cmd_type)
1815 /* check if pid is valid */
1816 if (false == vcd_client_is_available(pid)) {
1817 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] pid is NOT valid ");
1818 return VCD_ERROR_INVALID_PARAMETER;
1821 if (0 != vcd_client_unset_command_type(pid, cmd_type)) {
1822 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to unset command type : pid(%d), cmd_type(%d)", pid, cmd_type);
1823 return VCD_ERROR_OPERATION_FAILED;
1829 int vcd_server_set_foreground(int pid, bool value)
1831 /* check if pid is valid */
1832 if (false == vcd_client_is_available(pid) && false == vcd_client_widget_is_available(pid)) {
1833 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] pid is NOT valid ");
1834 return VCD_ERROR_INVALID_PARAMETER;
1837 if (0 != vcd_config_set_foreground(pid, value)) {
1838 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to set foreground : pid(%d), value(%d)", pid, value);
1839 return VCD_ERROR_OPERATION_FAILED;
1845 static int __vcd_server_launch_manager_app()
1848 bool running = false;
1851 ret = vcd_client_manager_get_appid(&appid);
1852 if (0 != ret || NULL == appid) {
1853 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Fail to get manager appid");
1854 return VCD_ERROR_OPERATION_FAILED;
1856 ret = app_manager_is_running(appid, &running);
1858 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Fail to check running with appid(%s)", appid);
1861 return VCD_ERROR_OPERATION_FAILED;
1863 if (false == running) {
1864 int tmp_ret = __vcd_is_package_installed(appid);
1865 if (false == tmp_ret) {
1866 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] manager app is not installed, appid(%s)", appid);
1868 ret = __vcd_activate_app_by_appcontrol(appid);
1870 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Fail to activate app");
1875 SLOG(LOG_ERROR, TAG_VCD, "Launch vc manager app: appid(%s)", appid);
1878 ret = __vcd_resume_app(appid);
1880 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Fail to resume app");
1885 SLOG(LOG_ERROR, TAG_VCD, "Resume vc manager app: appid(%s)", appid);
1891 return VCD_ERROR_NONE;
1894 int vcd_server_dialog(int pid, const char* disp_text, const char* utt_text, int continuous)
1896 /* check if pid is valid */
1897 if (false == vcd_client_is_available(pid) && false == vcd_client_widget_is_available(pid)) {
1898 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] pid is NOT valid ");
1899 return VCD_ERROR_INVALID_PARAMETER;
1902 int ret = __vcd_server_launch_manager_app();
1904 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);
1908 ret = vcdc_send_dialog(vcd_client_manager_get_pid(), pid, disp_text, utt_text, continuous);
1910 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);
1917 int vcd_server_is_system_command_valid(int pid, int* is_sys_cmd_valid)
1919 /* check if pid is valid */
1920 if (false == vcd_client_is_available(pid) && false == vcd_client_widget_is_available(pid)) {
1921 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] pid is NOT valid ");
1922 return VCD_ERROR_INVALID_PARAMETER;
1925 /* check if system command is valid */
1926 if (true == vcd_client_manager_is_system_command_valid(vcd_client_manager_get_pid()))
1927 *is_sys_cmd_valid = true;
1929 *is_sys_cmd_valid = false;
1935 int vcd_server_set_exclusive_command(int pid, bool value)
1937 /* check if pid is valid */
1938 if (false == vcd_client_is_available(pid)) {
1939 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] pid is NOT valid ");
1940 return VCD_ERROR_INVALID_PARAMETER;
1943 if (true == value) {
1944 if (0 != vcd_client_set_exclusive_command(pid)) {
1945 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to set exclusive command : pid(%d)", pid);
1946 return VCD_ERROR_OPERATION_FAILED;
1949 if (0 != vcd_client_unset_exclusive_command(pid)) {
1950 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to unset exclusive command : pid(%d)", pid);
1951 return VCD_ERROR_OPERATION_FAILED;
1958 int vcd_server_request_start(int pid, bool stop_by_silence)
1960 /* check if pid is valid */
1961 if (false == vcd_client_is_available(pid)) {
1962 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] pid(%d) is NOT forground client", pid);
1963 return VCD_ERROR_INVALID_PARAMETER;
1967 /* Check current state */
1968 vcd_state_e state = vcd_config_get_service_state();
1970 /* Service state should be ready */
1971 if (VCD_STATE_READY != state) {
1972 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Current state is not Ready : pid(%d)", pid);
1973 return VCD_ERROR_INVALID_STATE;
1976 if (-1 != vcd_client_manager_get_pid()) {
1977 /* Check current pid is valid */
1978 if (false == vcd_client_manager_check_demandable_client(pid)) {
1979 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Current client is NOT available : pid(%d)", pid);
1980 return VCD_ERROR_INVALID_PARAMETER;
1984 ret = vcd_server_mgr_start(stop_by_silence, false);
1986 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Fail to start recognition");
1987 return VCD_ERROR_INVALID_PARAMETER;
1993 int vcd_server_request_stop(int pid)
1996 /* Check current state */
1997 vcd_state_e state = vcd_config_get_service_state();
1999 /* Service state should be ready */
2000 if (VCD_STATE_RECORDING != state) {
2001 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Current state is not Recording : pid(%d)", pid);
2002 return VCD_ERROR_INVALID_STATE;
2005 if (-1 != vcd_client_manager_get_pid()) {
2006 /* Check current pid is valid */
2007 if (false == vcd_client_manager_check_demandable_client(pid)) {
2008 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Current client is NOT available : pid(%d)", pid);
2009 return VCD_ERROR_INVALID_PARAMETER;
2013 ret = vcd_server_mgr_stop();
2015 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Fail to start recognition");
2016 return VCD_ERROR_OPERATION_FAILED;
2019 return VCD_ERROR_NONE;
2022 int vcd_server_request_cancel(int pid)
2025 /* Check current state */
2026 vcd_state_e state = vcd_config_get_service_state();
2028 /* Service state should be recording or processing */
2029 if (VCD_STATE_RECORDING != state && VCD_STATE_PROCESSING != state) {
2030 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Current state is not Recording or Processing : pid(%d)", pid);
2031 return VCD_ERROR_INVALID_STATE;
2034 if (-1 != vcd_client_manager_get_pid()) {
2035 /* Check current pid is valid */
2036 if (false == vcd_client_manager_check_demandable_client(pid)) {
2037 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Current client is NOT available : pid(%d)", pid);
2038 return VCD_ERROR_INVALID_PARAMETER;
2042 ret = vcd_server_mgr_cancel();
2044 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Fail to start recognition");
2045 return VCD_ERROR_OPERATION_FAILED;
2048 return VCD_ERROR_NONE;
2053 * VC Server Functions for Widget lib
2055 int vcd_server_widget_initialize(int pid)
2057 if (false == g_is_engine) {
2058 if (0 != vcd_engine_agent_initialize_current_engine()) {
2059 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] No Engine");
2060 g_is_engine = false;
2061 return VCD_ERROR_ENGINE_NOT_FOUND;
2067 if (false == vcd_engine_is_available_engine()) {
2068 if (0 != vcd_engine_agent_initialize_current_engine()) {
2069 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] No Engine");
2070 return VCD_ERROR_ENGINE_NOT_FOUND;
2074 /* check if pid is valid */
2075 if (true == vcd_client_widget_is_available(pid)) {
2076 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] The pid is already exist");
2077 return VCD_ERROR_INVALID_PARAMETER;
2080 /* Add client information to client manager */
2081 if (0 != vcd_client_widget_add(pid)) {
2082 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to add client info");
2083 return VCD_ERROR_OPERATION_FAILED;
2086 SLOG(LOG_DEBUG, TAG_VCD, "[Server Success] Initialize widget : pid(%d)", pid);
2088 return VCD_ERROR_NONE;
2091 int vcd_server_widget_finalize(int pid)
2093 /* check if pid is valid */
2094 if (false == vcd_client_widget_is_available(pid)) {
2095 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] pid is NOT valid ");
2096 return VCD_ERROR_INVALID_PARAMETER;
2099 /* Remove client information */
2100 if (0 != vcd_client_widget_delete(pid)) {
2101 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Fail to delete client");
2104 if (0 == vcd_client_get_ref_count()) {
2105 SLOG(LOG_DEBUG, TAG_VCD, "[Server] connected client list is empty");
2106 ecore_timer_add(0, __finalize_quit_ecore_loop, NULL);
2109 return VCD_ERROR_NONE;
2112 int vcd_server_widget_start_recording(int pid, bool widget_command)
2114 /* check if pid is valid */
2115 if (false == vcd_client_widget_is_available(pid)) {
2116 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] pid is NOT valid ");
2117 return VCD_ERROR_INVALID_PARAMETER;
2120 if (true == widget_command) {
2121 vcd_client_widget_set_command(pid);
2122 SLOG(LOG_DEBUG, TAG_VCD, "[Server] widget command is available");
2124 vcd_client_widget_unset_command(pid);
2125 SLOG(LOG_WARN, TAG_VCD, "[Server] widget command is NOT available");
2128 int ret = __start_internal_recognition();
2130 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to start recongition : %d", ret);
2131 ecore_timer_add(0, __vcd_request_show_tooltip, (void*)false);
2137 int vcd_server_widget_start(int pid, bool stop_by_silence)
2139 /* check if pid is valid */
2140 int fore_pid = vcd_client_widget_get_foreground_pid();
2141 if (pid != fore_pid) {
2142 SLOG(LOG_ERROR, TAG_VCD, "[Server] pid is NOT foreground");
2143 return VCD_ERROR_INVALID_PARAMETER;
2146 /* Check current state */
2147 vcd_state_e state = vcd_config_get_service_state();
2149 /* Service state should be ready */
2150 if (VCD_STATE_READY != state) {
2151 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Current state is not Ready : pid(%d)", pid);
2152 return VCD_ERROR_INVALID_STATE;
2155 vcd_client_set_slience_detection(stop_by_silence);
2157 /* Notify show tooltip */
2158 ecore_timer_add(0, __vcd_request_show_tooltip, (void*)true);
2163 int vcd_server_widget_stop(int pid)
2165 /* check if pid is valid */
2166 int fore_pid = vcd_client_widget_get_foreground_pid();
2167 if (pid != fore_pid) {
2168 SLOG(LOG_ERROR, TAG_VCD, "[Server] pid is NOT foreground");
2169 return VCD_ERROR_INVALID_PARAMETER;
2173 /* Check current state */
2174 vcd_state_e state = vcd_config_get_service_state();
2176 /* Service state should be recording */
2177 if (VCD_STATE_RECORDING != state) {
2178 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Current service state is not Recording : pid(%d)", pid);
2179 return VCD_ERROR_INVALID_STATE;
2182 ret = vcd_server_mgr_stop();
2184 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Fail to start recognition");
2185 return VCD_ERROR_OPERATION_FAILED;
2188 return VCD_ERROR_NONE;
2191 int vcd_server_widget_cancel(int pid)
2193 /* check if pid is valid */
2194 int fore_pid = vcd_client_widget_get_foreground_pid();
2195 if (pid != fore_pid) {
2196 SLOG(LOG_ERROR, TAG_VCD, "[Server] pid is NOT foreground");
2197 return VCD_ERROR_INVALID_PARAMETER;
2201 /* Check current state */
2202 vcd_state_e state = vcd_config_get_service_state();
2204 /* Service state should be recording or processing */
2205 if (VCD_STATE_RECORDING != state && VCD_STATE_PROCESSING != state) {
2206 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Current state is not Recording or Processing : pid(%d)", pid);
2207 return VCD_ERROR_INVALID_STATE;
2210 ret = vcd_server_mgr_cancel();
2212 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Fail to cancel recognition : %d", ret);
2216 return VCD_ERROR_NONE;