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 == ");
873 struct dirent *dirp = NULL;
875 dp = opendir(VC_RUNTIME_INFO_ROOT);
877 SLOG(LOG_ERROR, TAG_VCD, "[File message WARN] Fail to open path : %s", VC_RUNTIME_INFO_ROOT);
881 char remove_path[256] = {0, };
883 ret = readdir_r(dp, &entry, &dirp);
885 SLOG(LOG_ERROR, TAG_VCD, "[File ERROR] Fail to read directory");
890 if (!strncmp("vc_", dirp->d_name, strlen("vc_"))) {
891 memset(remove_path, 0, 256);
892 snprintf(remove_path, 256, "%s/%s", VC_RUNTIME_INFO_ROOT, dirp->d_name);
895 if (0 != remove(remove_path)) {
896 SLOG(LOG_WARN, TAG_VCD, "[File message WARN] Fail to remove file : %s", remove_path);
898 SLOG(LOG_DEBUG, TAG_VCD, "[File message] Remove file : %s", remove_path);
902 } while (NULL != dirp);
909 static int __vcd_db_clean_up()
912 int cnt = VC_COMMAND_TYPE_FOREGROUND;
914 if (VC_COMMAND_TYPE_BACKGROUND != cnt)
915 ret = vc_db_delete_commands(-1, cnt, NULL);
916 } while (VC_COMMAND_TYPE_EXCLUSIVE >= ++cnt);
925 /* Remove old file */
926 __vcd_file_clean_up();
928 /* initialize modules */
929 ret = vcd_config_initialize(__config_lang_changed_cb, __config_foreground_changed_cb, NULL);
931 SLOG(LOG_ERROR, TAG_VCD, "[Server WARNING] Fail to initialize config.");
935 ret = __vcd_db_clean_up();
937 SLOG(LOG_ERROR, TAG_VCD, "[Server WARNING] Fail to remove db data");
940 vcd_config_set_service_state(VCD_STATE_NONE);
942 //ret = vcd_engine_agent_init(__vcd_server_pre_result_cb, __vcd_server_result_cb, __vcd_server_nlu_result_cb, __vcd_server_error_cb);
943 ret = vcd_engine_agent_init(__vcd_server_asr_result_cb, __vcd_server_result_cb, __vcd_server_error_cb);
945 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to engine agent initialize : result(%d)", ret);
949 if (0 != vcd_recorder_create(__server_recorder_callback, __server_recorder_interrupt_callback)) {
950 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to create recorder");
951 return VCD_ERROR_OPERATION_FAILED;
955 ret = vcd_engine_agent_initialize_current_engine();
957 if (VCD_ERROR_ENGINE_NOT_FOUND == ret)
958 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] There is No Voice control engine");
960 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to init engine");
968 if (0 != vcd_engine_agent_load_current_engine()) {
969 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to load current engine");
970 return VCD_ERROR_OPERATION_FAILED;
973 /* Initialize manager info */
974 vcd_client_manager_unset();
976 vcd_config_set_service_state(VCD_STATE_READY);
977 vcdc_send_service_state(VCD_STATE_READY);
979 SLOG(LOG_DEBUG, TAG_VCD, "[Server SUCCESS] initialize");
987 if (0 < g_list_length(g_proc_list)) {
988 iter = g_list_first(g_proc_list);
989 while (NULL != iter) {
990 g_proc_list = g_list_remove_link(g_proc_list, iter);
991 iter = g_list_first(g_proc_list);
995 if (g_restart_timer != NULL) {
996 ecore_timer_del(g_restart_timer);
997 g_restart_timer = NULL;
1000 vcd_state_e state = vcd_config_get_service_state();
1001 if (VCD_STATE_READY != state) {
1002 if (VCD_STATE_RECORDING == state) {
1003 vcd_recorder_stop();
1005 vcd_engine_recognize_cancel();
1007 if (0 != vcd_recorder_destroy()) {
1008 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to destroy recorder");
1010 SLOG(LOG_DEBUG, TAG_VCD, "[Server] destroy recorder");
1013 if (0 != vcd_engine_agent_release()) {
1014 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to release engine");
1016 SLOG(LOG_DEBUG, TAG_VCD, "[Server] release engine");
1019 vcd_client_manager_unset_appid();
1021 vcd_config_set_service_state(VCD_STATE_NONE);
1022 vcdc_send_service_state(VCD_STATE_NONE);
1024 SLOG(LOG_DEBUG, TAG_VCD, "[Server] mode finalize");
1029 static Eina_Bool __finalize_quit_ecore_loop(void *data)
1031 SLOG(LOG_DEBUG, TAG_VCD, "[Server] quit ecore main loop");
1032 ecore_main_loop_quit();
1036 static void __read_proc()
1039 struct dirent entry;
1040 struct dirent *dirp = NULL;
1045 if (0 < g_list_length(g_proc_list)) {
1046 iter = g_list_first(g_proc_list);
1047 while (NULL != iter) {
1048 g_proc_list = g_list_remove_link(g_proc_list, iter);
1049 iter = g_list_first(g_proc_list);
1053 dp = opendir("/proc");
1055 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Fail to open proc");
1058 ret = readdir_r(dp, &entry, &dirp);
1060 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Fail to readdir");
1065 tmp = atoi(dirp->d_name);
1066 if (0 >= tmp) continue;
1067 g_proc_list = g_list_append(g_proc_list, GINT_TO_POINTER(tmp));
1069 } while (NULL != dirp);
1075 static void __vcd_cleanup_client(vcd_client_type_e type)
1077 int* client_list = NULL;
1078 int client_count = 0;
1085 if (VCD_CLIENT_TYPE_NORMAL == type) {
1086 ret = vcd_client_get_list(&client_list, &client_count);
1087 } else if (VCD_CLIENT_TYPE_WIDGET == type) {
1088 ret = vcd_client_widget_get_list(&client_list, &client_count);
1089 } else if (VCD_CLIENT_TYPE_MANAGER == type) {
1090 mgr_pid = vcd_client_manager_get_pid();
1091 client_list = &mgr_pid;
1093 if (-1 == mgr_pid) {
1094 SLOG(LOG_WARN, TAG_VCD, "[WARNING] Invalid Manager pid");
1099 if (0 == ret || mgr_pid > 0) {
1100 SLOG(LOG_DEBUG, TAG_VCD, "===== Clean up %s client ", type ? (type == 1) ? "Widget" : "Manager" : "Normal");
1101 if (NULL != client_list && client_count > 0) {
1102 for (i = 0; i < client_count; i++) {
1105 for (j = 0; j < g_list_length(g_proc_list); j++) {
1106 iter = g_list_nth(g_proc_list, j);
1108 if (client_list[i] == GPOINTER_TO_INT(iter->data)) {
1109 SLOG(LOG_DEBUG, TAG_VCD, "%s pid(%d) is running", type ? (type == 1) ? "Widget" : "Manager" : "Normal", client_list[i]);
1116 if (false == exist) {
1117 SLOG(LOG_ERROR, TAG_VCD, "%s pid(%d) should be removed", type ? (type == 1) ? "Widget" : "Manager" : "Normal", client_list[i]);
1118 if (VCD_CLIENT_TYPE_NORMAL == type)
1119 vcd_server_finalize(client_list[i]);
1120 else if (VCD_CLIENT_TYPE_WIDGET == type)
1121 vcd_server_widget_finalize(client_list[i]);
1123 vcd_server_mgr_finalize(mgr_pid);
1127 SLOG(LOG_DEBUG, TAG_VCD, "=====");
1128 SLOG(LOG_DEBUG, TAG_VCD, " ");
1130 if (NULL != client_list && -1 == mgr_pid) {
1137 Eina_Bool vcd_cleanup_client_all(void *data)
1141 __vcd_cleanup_client(VCD_CLIENT_TYPE_NORMAL);
1142 __vcd_cleanup_client(VCD_CLIENT_TYPE_WIDGET);
1143 __vcd_cleanup_client(VCD_CLIENT_TYPE_MANAGER);
1146 if (0 == vcd_client_get_list(&client_list, &client_count)) {
1147 SLOG(LOG_DEBUG, TAG_VCD, "===== Clean up client ");
1148 if (NULL != client_list && client_count > 0) {
1149 for (i = 0; i < client_count; i++) {
1152 for (j = 0; j < g_list_length(g_proc_list); j++) {
1153 iter = g_list_nth(g_proc_list, j);
1155 if (client_list[i] == GPOINTER_TO_INT(iter->data)) {
1156 SLOG(LOG_DEBUG, TAG_VCD, "pid(%d) is running", client_list[i]);
1163 if (false == exist) {
1164 SLOG(LOG_ERROR, TAG_VCD, "pid(%d) should be removed", client_list[i]);
1165 vcd_server_finalize(client_list[i]);
1168 result = vcdc_send_hello(client_list[i], VCD_CLIENT_TYPE_NORMAL);
1171 SLOG(LOG_DEBUG, TAG_VCD, "[Server] pid(%d) should be removed.", client_list[i]);
1172 vcd_server_finalize(client_list[i]);
1173 } else if (-1 == result) {
1174 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Hello result has error");
1179 SLOG(LOG_DEBUG, TAG_VCD, "=====");
1180 SLOG(LOG_DEBUG, TAG_VCD, " ");
1182 if (NULL != client_list) {
1187 /* If app is in background state, app cannot response message. */
1188 if (0 == vcd_client_widget_get_list(&client_list, &client_count)) {
1189 SLOG(LOG_DEBUG, TAG_VCD, "===== Clean up widget");
1190 if (NULL != client_list && client_count > 0) {
1191 for (i = 0; i < client_count; i++) {
1194 for (j = 0; j < g_list_length(g_proc_list); j++) {
1195 iter = g_list_nth(g_proc_list, j);
1197 if (client_list[i] == GPOINTER_TO_INT(iter->data)) {
1198 SLOG(LOG_DEBUG, TAG_VCD, "widget pid(%d) is running", client_list[i]);
1205 if (false == exist) {
1206 SLOG(LOG_ERROR, TAG_VCD, "widget pid(%d) should be removed", client_list[i]);
1207 vcd_server_widget_finalize(client_list[i]);
1210 result = vcdc_send_hello(client_list[i], VCD_CLIENT_TYPE_WIDGET);
1213 SLOG(LOG_DEBUG, TAG_VCD, "[Server] widget pid(%d) should be removed.", client_list[i]);
1214 vcd_server_widget_finalize(client_list[i]);
1215 } else if (-1 == result) {
1216 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Hello result has error");
1221 SLOG(LOG_DEBUG, TAG_VCD, "=====");
1222 SLOG(LOG_DEBUG, TAG_VCD, " ");
1225 if (NULL != client_list) {
1233 int mgr_pid = vcd_client_manager_get_pid();
1235 for (j = 0; j < g_list_length(g_proc_list); j++) {
1236 iter = g_list_nth(g_proc_list, j);
1238 if (mgr_pid == GPOINTER_TO_INT(iter->data)) {
1239 SLOG(LOG_DEBUG, TAG_VCD, "manager pid(%d) is running", mgr_pid);
1246 if (false == exist) {
1247 SLOG(LOG_ERROR, TAG_VCD, "manager pid (%d) should be removed", mgr_pid);
1248 vcd_server_mgr_finalize(mgr_pid);
1255 int vcd_server_get_service_state()
1257 return vcd_config_get_service_state();
1260 int vcd_server_get_foreground()
1263 vcd_config_get_foreground(&pid);
1271 int vcd_server_mgr_initialize(int pid)
1273 if (false == g_is_engine) {
1274 if (0 != vcd_engine_agent_initialize_current_engine()) {
1275 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] No Engine");
1276 g_is_engine = false;
1277 return VCD_ERROR_ENGINE_NOT_FOUND;
1283 /* check if pid is valid */
1284 if (false == vcd_client_manager_is_valid(pid)) {
1285 SLOG(LOG_ERROR, TAG_VCD, "[Server] old manager pid(%d) be removed", vcd_client_manager_get_pid());
1286 vcd_server_mgr_cancel();
1287 vcd_client_manager_unset();
1290 /* Add client information to client manager */
1291 if (0 != vcd_client_manager_set(pid)) {
1292 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to add manager");
1293 return VCD_ERROR_OPERATION_FAILED;
1296 if (0 != vcdc_send_manager_pid(pid))
1297 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Fail to send manager pid");
1299 SLOG(LOG_DEBUG, TAG_VCD, "[Server Success] Manager initialize : pid(%d)", pid);
1301 return VCD_ERROR_NONE;
1304 int vcd_server_mgr_finalize(int pid)
1306 /* check if pid is valid */
1307 if (false == vcd_client_manager_is_valid(pid)) {
1308 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] The manager pid(%d) is NOT valid", pid);
1309 return VCD_ERROR_INVALID_PARAMETER;
1312 /* Cancel recognition */
1313 vcd_server_mgr_cancel();
1315 if (0 != vcdc_send_manager_pid(-1))
1316 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Fail to send manager pid");
1318 /* Remove manager information */
1319 if (0 != vcd_client_manager_unset()) {
1320 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Fail to delete client");
1323 if (0 == vcd_client_get_ref_count()) {
1324 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Connected client list is empty");
1325 ecore_timer_add(0, __finalize_quit_ecore_loop, NULL);
1328 SLOG(LOG_DEBUG, TAG_VCD, "[Server Success] Manager Finalize : pid(%d)", pid);
1330 return VCD_ERROR_NONE;
1333 int vcd_server_mgr_set_command(int pid)
1335 if (0 != vcd_client_manager_set_command(pid)) {
1336 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] The manager pid(%d) is NOT valid", pid);
1337 return VCD_ERROR_INVALID_PARAMETER;
1339 return VCD_ERROR_NONE;
1342 int vcd_server_mgr_unset_command(int pid)
1344 if (0 != vcd_client_manager_unset_command(pid)) {
1345 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] The manager pid(%d) is NOT valid", pid);
1346 return VCD_ERROR_INVALID_PARAMETER;
1348 return VCD_ERROR_NONE;
1351 int vcd_server_mgr_set_demandable_client(int pid)
1353 /* check if pid is valid */
1354 if (false == vcd_client_manager_is_valid(pid)) {
1355 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] The manager pid(%d) is NOT valid", pid);
1356 return VCD_ERROR_INVALID_PARAMETER;
1359 GSList* client_list = NULL;
1360 if (0 != vc_info_parser_get_demandable_clients(&client_list)) {
1361 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to get demandable client");
1362 return VCD_ERROR_OPERATION_FAILED;
1365 /* Save client list */
1366 if (0 != vcd_client_manager_set_demandable_client(pid, client_list)) {
1367 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to set demandable client list");
1368 return VCD_ERROR_OPERATION_FAILED;
1371 return VCD_ERROR_NONE;
1374 int vcd_server_mgr_set_audio_type(int pid, const char* audio_type)
1376 /* check if pid is valid */
1377 if (false == vcd_client_manager_is_valid(pid)) {
1378 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] The manager pid(%d) is NOT valid", pid);
1379 return VCD_ERROR_INVALID_PARAMETER;
1383 vcp_audio_type_e type = VCP_AUDIO_TYPE_PCM_S16_LE;
1387 ret = vcd_engine_get_audio_format(audio_type, &type, &rate, &channel);
1389 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to get audio format : %d", ret);
1390 return VCD_ERROR_OPERATION_FAILED;
1393 ret = vcd_recorder_set(audio_type, type, rate, channel);
1395 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to set audio in type : %d", ret);
1396 return VCD_ERROR_OPERATION_FAILED;
1399 return VCD_ERROR_NONE;
1402 int vcd_server_mgr_get_audio_type(int pid, char** audio_type)
1404 /* check if pid is valid */
1405 if (false == vcd_client_manager_is_valid(pid)) {
1406 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] The manager pid(%d) is NOT valid", pid);
1407 return VCD_ERROR_INVALID_PARAMETER;
1410 int ret = vcd_recorder_get(audio_type);
1412 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to get audio in type : %d", ret);
1413 return VCD_ERROR_OPERATION_FAILED;
1416 return VCD_ERROR_NONE;
1419 int vcd_server_mgr_set_client_info(int pid)
1421 /* check if pid is valid */
1422 if (false == vcd_client_manager_is_valid(pid)) {
1423 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] The manager pid(%d) is NOT valid", pid);
1424 return VCD_ERROR_INVALID_PARAMETER;
1427 int ret = vcd_client_save_client_info();
1429 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to save client info : %d", ret);
1430 return VCD_ERROR_OPERATION_FAILED;
1433 return VCD_ERROR_NONE;
1436 static int __start_internal_recognition()
1440 /* 2. Get commands */
1441 ret = vcd_client_command_collect_command();
1443 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to collect command : %d", ret);
1444 return VCD_ERROR_OPERATION_FAILED;
1447 ret = vcd_client_get_length();
1449 SLOG(LOG_WARN, TAG_VCD, "[Server WARNIING] No current command : %d", ret);
1450 return VCD_ERROR_OPERATION_FAILED;
1453 /* 3. Set command to engine */
1454 ret = vcd_engine_set_commands();
1456 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to collect command : %d", ret);
1457 return VCD_ERROR_OPERATION_FAILED;
1460 SLOG(LOG_DEBUG, TAG_VCD, "[Server Success] Set command");
1462 bool stop_by_silence = true;
1463 if (VCD_RECOGNITION_MODE_MANUAL == vcd_client_get_recognition_mode()) {
1464 stop_by_silence = false;
1467 vcd_client_manager_set_result_text(NULL);
1469 /* 4. start recognition */
1470 ret = vcd_engine_recognize_start(stop_by_silence);
1472 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to start recognition : result(%d)", ret);
1473 return VCD_ERROR_OPERATION_FAILED;
1476 SLOG(LOG_DEBUG, TAG_VCD, "[Server Success] Start engine");
1478 /* 5. recorder start */
1479 ret = vcd_recorder_start();
1481 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to start recorder : result(%d)", ret);
1482 vcd_engine_recognize_cancel();
1486 vcd_config_set_service_state(VCD_STATE_RECORDING);
1487 vcdc_send_service_state(VCD_STATE_RECORDING);
1489 SLOG(LOG_DEBUG, TAG_VCD, "[Server Success] Start recognition(%d)", stop_by_silence);
1494 static Eina_Bool __vcd_request_show_tooltip(void *data)
1496 int pid = vcd_client_widget_get_foreground_pid();
1498 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Request tooltip show and widget command");
1499 vcdc_send_show_tooltip(pid, (bool)data);
1505 int vcd_server_mgr_start(vcd_recognition_mode_e recognition_mode, bool exclusive_cmd, bool start_by_client)
1507 /* 1. check current state */
1508 vcd_state_e state = vcd_config_get_service_state();
1510 if (VCD_STATE_READY != state) {
1511 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Current state is not ready");
1512 return VCD_ERROR_INVALID_STATE;
1514 if (-1 == vcd_client_manager_get_pid()) {
1515 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Manager is NOT available.");
1516 return VCD_ERROR_OPERATION_FAILED;
1519 SLOG(LOG_DEBUG, TAG_VCD, "[Server] set recognition mode = %d", recognition_mode);
1520 vcd_client_set_recognition_mode(recognition_mode);
1522 if (false == exclusive_cmd) {
1523 /* Notify show tooltip */
1524 int pid = vcd_client_widget_get_foreground_pid();
1526 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Request tooltip show and widget command");
1527 ecore_timer_add(0, __vcd_request_show_tooltip, (void*)true);
1531 vcd_client_manager_set_exclusive(exclusive_cmd);
1535 if (true == start_by_client) {
1536 /* Get foreground pid */
1537 if (0 != vcd_config_get_foreground(&fg_pid)) {
1538 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to get foreground");
1541 /* Set client exclusive option */
1542 if (0 != vcd_client_set_exclusive_command(fg_pid)) {
1543 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to set exclusive command");
1547 int ret = __start_internal_recognition();
1549 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to start recongition : %d", ret);
1553 if (true == start_by_client) {
1554 vcd_client_unset_exclusive_command(fg_pid);
1557 return VCD_ERROR_NONE;
1560 int vcd_server_mgr_stop()
1562 /* 1. Check current state is recording */
1563 if (VCD_STATE_RECORDING != vcd_config_get_service_state()) {
1564 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Current state is not recording");
1565 return VCD_ERROR_INVALID_STATE;
1567 if (-1 == vcd_client_manager_get_pid()) {
1568 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Manager is NOT available.");
1569 return VCD_ERROR_OPERATION_FAILED;
1572 /* 2. Stop recorder */
1573 vcd_recorder_stop();
1575 /* 3. Stop engine recognition */
1576 int ret = vcd_engine_recognize_stop();
1578 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to stop recognition : %d", ret);
1581 /* 4. Set original mode */
1582 vcd_config_set_service_state(VCD_STATE_PROCESSING);
1583 vcdc_send_service_state(VCD_STATE_PROCESSING);
1585 return VCD_ERROR_NONE;
1588 int vcd_server_mgr_cancel()
1590 /* 1. Check current state */
1591 vcd_state_e state = vcd_config_get_service_state();
1592 if (VCD_STATE_RECORDING != state && VCD_STATE_PROCESSING != state) {
1593 SLOG(LOG_WARN, TAG_VCD, "[Server ERROR] Current state is not recording or processing");
1594 return VCD_ERROR_INVALID_STATE;
1596 if (-1 == vcd_client_manager_get_pid()) {
1597 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Manager is NOT available.");
1598 return VCD_ERROR_OPERATION_FAILED;
1601 if (g_restart_timer != NULL) {
1602 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Delete restart engine timer");
1603 ecore_timer_del(g_restart_timer);
1604 g_restart_timer = NULL;
1607 /* 2. Stop recorder */
1608 vcd_recorder_stop();
1609 /* 3. Cancel engine */
1610 int ret = vcd_engine_recognize_cancel();
1612 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Fail to cancel : %d", ret);
1615 if (false == vcd_client_manager_get_exclusive()) {
1616 int pid = vcd_client_widget_get_foreground_pid();
1618 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Request tooltip hide");
1619 ecore_timer_add(0, __vcd_request_show_tooltip, (void*)false);
1622 vcd_client_manager_set_exclusive(false);
1626 vcd_config_set_service_state(VCD_STATE_READY);
1627 vcdc_send_service_state(VCD_STATE_READY);
1629 return VCD_ERROR_NONE;
1633 int vcd_server_mgr_result_select()
1635 __vcd_send_selected_result(NULL);
1637 return VCD_ERROR_NONE;
1640 int vcd_server_mgr_set_domain(int pid, const char* domain)
1642 /* check if pid is valid */
1643 if (false == vcd_client_manager_is_valid(pid)) {
1644 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] The manager pid(%d) is NOT valid", pid);
1645 return VCD_ERROR_INVALID_PARAMETER;
1648 vcd_state_e state = vcd_config_get_service_state();
1649 if (VCD_STATE_READY != state) {
1650 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Current state is not ready");
1651 return VCD_ERROR_INVALID_STATE;
1654 /* Set domain to engine */
1655 int ret = vcd_engine_set_domain(pid, domain);
1657 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to set domain : %d", ret);
1659 SLOG(LOG_DEBUG, TAG_VCD, "[Server SUCCESS] Set domain");
1665 int vcd_server_mgr_set_private_data(int pid, const char* key, const char* data)
1667 /* check if pid is valid */
1668 if (false == vcd_client_manager_is_valid(pid)) {
1669 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] The manager pid(%d) is NOT valid", pid);
1670 return VCD_ERROR_INVALID_PARAMETER;
1673 vcd_state_e state = vcd_config_get_service_state();
1674 if (VCD_STATE_READY != state) {
1675 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Current state is not ready");
1676 return VCD_ERROR_INVALID_STATE;
1679 /* Set private data to engine */
1680 int ret = vcd_engine_set_private_data(pid, key, data);
1682 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to set private data : %d", ret);
1684 SLOG(LOG_DEBUG, TAG_VCD, "[Server SUCCESS] Set private data");
1690 int vcd_server_mgr_get_private_data(int pid, const char* key, char** data)
1692 /* check if pid is valid */
1693 if (false == vcd_client_manager_is_valid(pid)) {
1694 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] The manager pid(%d) is NOT valid", pid);
1695 return VCD_ERROR_INVALID_PARAMETER;
1697 vcd_state_e state = vcd_config_get_service_state();
1698 if (VCD_STATE_READY != state) {
1699 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Current state is not ready");
1700 return VCD_ERROR_INVALID_STATE;
1703 /* Get private data to engine */
1704 int ret = vcd_engine_get_private_data(pid, key, data);
1706 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to get private data : %d", ret);
1708 SLOG(LOG_DEBUG, TAG_VCD, "[Server SUCCESS] Set private data");
1714 int vcd_server_mgr_do_action(int pid, int type, const char* action)
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;
1724 vcd_state_e state = vcd_config_get_service_state();
1725 if (VCD_STATE_READY != state) {
1726 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Current state is not ready");
1727 return VCD_ERROR_INVALID_STATE;
1730 /* Reqeust do action to engine */
1731 if (VCD_SEND_EVENT_TYPE_TEXT == type)
1732 ret = vcd_engine_process_text(pid, action);
1733 else if (VCD_SEND_EVENT_TYPE_LIST_EVENT == type)
1734 ret = vcd_engine_process_list_event(pid, action);
1735 else if (VCD_SEND_EVENT_TYPE_HAPTIC_EVENT == type)
1736 ret = vcd_engine_process_haptic_event(pid, action);
1739 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to process do action : %d", ret);
1741 SLOG(LOG_DEBUG, TAG_VCD, "[Server SUCCESS] Process do action");
1748 * VC Server Functions for Client
1750 int vcd_server_initialize(int pid)
1752 if (false == g_is_engine) {
1753 if (0 != vcd_engine_agent_initialize_current_engine()) {
1754 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] No Engine");
1755 g_is_engine = false;
1756 return VCD_ERROR_ENGINE_NOT_FOUND;
1762 if (false == vcd_engine_is_available_engine()) {
1763 if (0 != vcd_engine_agent_initialize_current_engine()) {
1764 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] No Engine");
1765 return VCD_ERROR_ENGINE_NOT_FOUND;
1769 /* check if pid is valid */
1770 if (true == vcd_client_is_available(pid)) {
1771 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] The pid is already exist");
1772 return VCD_ERROR_INVALID_PARAMETER;
1775 /* Add client information to client manager */
1776 if (0 != vcd_client_add(pid)) {
1777 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to add client info");
1778 return VCD_ERROR_OPERATION_FAILED;
1781 SLOG(LOG_DEBUG, TAG_VCD, "[Server Success] Client Initialize : pid(%d)", pid);
1783 return VCD_ERROR_NONE;
1786 int vcd_server_finalize(int pid)
1788 /* check if pid is valid */
1789 if (false == vcd_client_is_available(pid)) {
1790 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] pid is NOT valid ");
1791 return VCD_ERROR_INVALID_PARAMETER;
1794 /* Remove client information */
1795 if (0 != vcd_client_delete(pid)) {
1796 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Fail to delete client");
1799 if (0 == vcd_client_get_ref_count()) {
1800 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Connected client list is empty");
1801 ecore_timer_add(0, __finalize_quit_ecore_loop, NULL);
1804 SLOG(LOG_DEBUG, TAG_VCD, "[Server Success] Client Finalize : pid(%d)", pid);
1806 return VCD_ERROR_NONE;
1809 int vcd_server_set_command(int pid, vc_cmd_type_e cmd_type)
1811 /* check if pid is valid */
1812 if (false == vcd_client_is_available(pid)) {
1813 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] pid is NOT valid ");
1814 return VCD_ERROR_INVALID_PARAMETER;
1817 if (0 != vcd_client_set_command_type(pid, cmd_type)) {
1818 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to set command type : pid(%d), cmd_type(%d)", pid, cmd_type);
1819 return VCD_ERROR_OPERATION_FAILED;
1825 int vcd_server_unset_command(int pid, vc_cmd_type_e cmd_type)
1827 /* check if pid is valid */
1828 if (false == vcd_client_is_available(pid)) {
1829 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] pid is NOT valid ");
1830 return VCD_ERROR_INVALID_PARAMETER;
1833 if (0 != vcd_client_unset_command_type(pid, cmd_type)) {
1834 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to unset command type : pid(%d), cmd_type(%d)", pid, cmd_type);
1835 return VCD_ERROR_OPERATION_FAILED;
1841 int vcd_server_set_foreground(int pid, bool value)
1843 /* check if pid is valid */
1844 if (false == vcd_client_is_available(pid) && false == vcd_client_widget_is_available(pid)) {
1845 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] pid is NOT valid ");
1846 return VCD_ERROR_INVALID_PARAMETER;
1849 if (0 != vcd_config_set_foreground(pid, value)) {
1850 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to set foreground : pid(%d), value(%d)", pid, value);
1851 return VCD_ERROR_OPERATION_FAILED;
1857 static int __vcd_server_launch_manager_app()
1860 bool running = false;
1863 ret = vcd_client_manager_get_appid(&appid);
1864 if (0 != ret || NULL == appid) {
1865 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Fail to get manager appid");
1866 return VCD_ERROR_OPERATION_FAILED;
1868 ret = app_manager_is_running(appid, &running);
1870 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Fail to check running with appid(%s)", appid);
1873 return VCD_ERROR_OPERATION_FAILED;
1875 if (false == running) {
1876 int tmp_ret = __vcd_is_package_installed(appid);
1877 if (false == tmp_ret) {
1878 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] manager app is not installed, appid(%s)", appid);
1880 ret = __vcd_activate_app_by_appcontrol(appid);
1882 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Fail to activate app");
1887 SLOG(LOG_ERROR, TAG_VCD, "Launch vc manager app: appid(%s)", appid);
1890 ret = __vcd_resume_app(appid);
1892 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Fail to resume app");
1897 SLOG(LOG_ERROR, TAG_VCD, "Resume vc manager app: appid(%s)", appid);
1903 return VCD_ERROR_NONE;
1906 int vcd_server_dialog(int pid, const char* disp_text, const char* utt_text, int continuous)
1908 /* check if pid is valid */
1909 if (false == vcd_client_is_available(pid) && false == vcd_client_widget_is_available(pid)) {
1910 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] pid is NOT valid ");
1911 return VCD_ERROR_INVALID_PARAMETER;
1914 int ret = __vcd_server_launch_manager_app();
1916 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);
1920 ret = vcdc_send_dialog(vcd_client_manager_get_pid(), pid, disp_text, utt_text, continuous);
1922 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);
1929 int vcd_server_is_system_command_valid(int pid, int* is_sys_cmd_valid)
1931 /* check if pid is valid */
1932 if (false == vcd_client_is_available(pid) && false == vcd_client_widget_is_available(pid)) {
1933 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] pid is NOT valid ");
1934 return VCD_ERROR_INVALID_PARAMETER;
1937 /* check if system command is valid */
1938 if (true == vcd_client_manager_is_system_command_valid(vcd_client_manager_get_pid()))
1939 *is_sys_cmd_valid = true;
1941 *is_sys_cmd_valid = false;
1947 int vcd_server_set_exclusive_command(int pid, bool value)
1949 /* check if pid is valid */
1950 if (false == vcd_client_is_available(pid)) {
1951 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] pid is NOT valid ");
1952 return VCD_ERROR_INVALID_PARAMETER;
1955 if (true == value) {
1956 if (0 != vcd_client_set_exclusive_command(pid)) {
1957 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to set exclusive command : pid(%d)", pid);
1958 return VCD_ERROR_OPERATION_FAILED;
1961 if (0 != vcd_client_unset_exclusive_command(pid)) {
1962 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to unset exclusive command : pid(%d)", pid);
1963 return VCD_ERROR_OPERATION_FAILED;
1970 int vcd_server_request_start(int pid, bool stop_by_silence)
1972 /* check if pid is valid */
1973 if (false == vcd_client_is_available(pid)) {
1974 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] pid(%d) is NOT forground client", pid);
1975 return VCD_ERROR_INVALID_PARAMETER;
1979 /* Check current state */
1980 vcd_state_e state = vcd_config_get_service_state();
1982 /* Service state should be ready */
1983 if (VCD_STATE_READY != state) {
1984 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Current state is not Ready : pid(%d)", pid);
1985 return VCD_ERROR_INVALID_STATE;
1988 if (-1 != vcd_client_manager_get_pid()) {
1989 /* Check current pid is valid */
1990 if (false == vcd_client_manager_check_demandable_client(pid)) {
1991 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Current client is NOT available : pid(%d)", pid);
1992 return VCD_ERROR_INVALID_PARAMETER;
1996 ret = vcd_server_mgr_start(stop_by_silence, false);
1998 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Fail to start recognition");
1999 return VCD_ERROR_INVALID_PARAMETER;
2005 int vcd_server_request_stop(int pid)
2008 /* Check current state */
2009 vcd_state_e state = vcd_config_get_service_state();
2011 /* Service state should be ready */
2012 if (VCD_STATE_RECORDING != state) {
2013 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Current state is not Recording : pid(%d)", pid);
2014 return VCD_ERROR_INVALID_STATE;
2017 if (-1 != vcd_client_manager_get_pid()) {
2018 /* Check current pid is valid */
2019 if (false == vcd_client_manager_check_demandable_client(pid)) {
2020 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Current client is NOT available : pid(%d)", pid);
2021 return VCD_ERROR_INVALID_PARAMETER;
2025 ret = vcd_server_mgr_stop();
2027 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Fail to start recognition");
2028 return VCD_ERROR_OPERATION_FAILED;
2031 return VCD_ERROR_NONE;
2034 int vcd_server_request_cancel(int pid)
2037 /* Check current state */
2038 vcd_state_e state = vcd_config_get_service_state();
2040 /* Service state should be recording or processing */
2041 if (VCD_STATE_RECORDING != state && VCD_STATE_PROCESSING != state) {
2042 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Current state is not Recording or Processing : pid(%d)", pid);
2043 return VCD_ERROR_INVALID_STATE;
2046 if (-1 != vcd_client_manager_get_pid()) {
2047 /* Check current pid is valid */
2048 if (false == vcd_client_manager_check_demandable_client(pid)) {
2049 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Current client is NOT available : pid(%d)", pid);
2050 return VCD_ERROR_INVALID_PARAMETER;
2054 ret = vcd_server_mgr_cancel();
2056 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Fail to start recognition");
2057 return VCD_ERROR_OPERATION_FAILED;
2060 return VCD_ERROR_NONE;
2065 * VC Server Functions for Widget lib
2067 int vcd_server_widget_initialize(int pid)
2069 if (false == g_is_engine) {
2070 if (0 != vcd_engine_agent_initialize_current_engine()) {
2071 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] No Engine");
2072 g_is_engine = false;
2073 return VCD_ERROR_ENGINE_NOT_FOUND;
2079 if (false == vcd_engine_is_available_engine()) {
2080 if (0 != vcd_engine_agent_initialize_current_engine()) {
2081 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] No Engine");
2082 return VCD_ERROR_ENGINE_NOT_FOUND;
2086 /* check if pid is valid */
2087 if (true == vcd_client_widget_is_available(pid)) {
2088 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] The pid is already exist");
2089 return VCD_ERROR_INVALID_PARAMETER;
2092 /* Add client information to client manager */
2093 if (0 != vcd_client_widget_add(pid)) {
2094 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to add client info");
2095 return VCD_ERROR_OPERATION_FAILED;
2098 SLOG(LOG_DEBUG, TAG_VCD, "[Server Success] Initialize widget : pid(%d)", pid);
2100 return VCD_ERROR_NONE;
2103 int vcd_server_widget_finalize(int pid)
2105 /* check if pid is valid */
2106 if (false == vcd_client_widget_is_available(pid)) {
2107 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] pid is NOT valid ");
2108 return VCD_ERROR_INVALID_PARAMETER;
2111 /* Remove client information */
2112 if (0 != vcd_client_widget_delete(pid)) {
2113 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Fail to delete client");
2116 if (0 == vcd_client_get_ref_count()) {
2117 SLOG(LOG_DEBUG, TAG_VCD, "[Server] connected client list is empty");
2118 ecore_timer_add(0, __finalize_quit_ecore_loop, NULL);
2121 return VCD_ERROR_NONE;
2124 int vcd_server_widget_start_recording(int pid, bool widget_command)
2126 /* check if pid is valid */
2127 if (false == vcd_client_widget_is_available(pid)) {
2128 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] pid is NOT valid ");
2129 return VCD_ERROR_INVALID_PARAMETER;
2132 if (true == widget_command) {
2133 vcd_client_widget_set_command(pid);
2134 SLOG(LOG_DEBUG, TAG_VCD, "[Server] widget command is available");
2136 vcd_client_widget_unset_command(pid);
2137 SLOG(LOG_WARN, TAG_VCD, "[Server] widget command is NOT available");
2140 int ret = __start_internal_recognition();
2142 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to start recongition : %d", ret);
2143 ecore_timer_add(0, __vcd_request_show_tooltip, (void*)false);
2149 int vcd_server_widget_start(int pid, bool stop_by_silence)
2151 /* check if pid is valid */
2152 int fore_pid = vcd_client_widget_get_foreground_pid();
2153 if (pid != fore_pid) {
2154 SLOG(LOG_ERROR, TAG_VCD, "[Server] pid is NOT foreground");
2155 return VCD_ERROR_INVALID_PARAMETER;
2158 /* Check current state */
2159 vcd_state_e state = vcd_config_get_service_state();
2161 /* Service state should be ready */
2162 if (VCD_STATE_READY != state) {
2163 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Current state is not Ready : pid(%d)", pid);
2164 return VCD_ERROR_INVALID_STATE;
2167 vcd_client_set_slience_detection(stop_by_silence);
2169 /* Notify show tooltip */
2170 ecore_timer_add(0, __vcd_request_show_tooltip, (void*)true);
2175 int vcd_server_widget_stop(int pid)
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;
2185 /* Check current state */
2186 vcd_state_e state = vcd_config_get_service_state();
2188 /* Service state should be recording */
2189 if (VCD_STATE_RECORDING != state) {
2190 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Current service state is not Recording : pid(%d)", pid);
2191 return VCD_ERROR_INVALID_STATE;
2194 ret = vcd_server_mgr_stop();
2196 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Fail to start recognition");
2197 return VCD_ERROR_OPERATION_FAILED;
2200 return VCD_ERROR_NONE;
2203 int vcd_server_widget_cancel(int pid)
2205 /* check if pid is valid */
2206 int fore_pid = vcd_client_widget_get_foreground_pid();
2207 if (pid != fore_pid) {
2208 SLOG(LOG_ERROR, TAG_VCD, "[Server] pid is NOT foreground");
2209 return VCD_ERROR_INVALID_PARAMETER;
2213 /* Check current state */
2214 vcd_state_e state = vcd_config_get_service_state();
2216 /* Service state should be recording or processing */
2217 if (VCD_STATE_RECORDING != state && VCD_STATE_PROCESSING != state) {
2218 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Current state is not Recording or Processing : pid(%d)", pid);
2219 return VCD_ERROR_INVALID_STATE;
2222 ret = vcd_server_mgr_cancel();
2224 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Fail to cancel recognition : %d", ret);
2228 return VCD_ERROR_NONE;