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;
393 iter = g_slist_nth(pid_list, 0);
394 while (NULL != iter) {
395 temp_cmd = iter->data;
397 if (NULL != temp_cmd && (pre_pid != temp_cmd->pid || pre_type == VC_COMMAND_TYPE_WIDGET || temp_cmd->type == VC_COMMAND_TYPE_WIDGET)) {
398 /* Launch deactivated several apps that is matched with result */
399 ret = __vcd_launch_app(result);
401 SLOG(LOG_ERROR, TAG_VCD, "Fail to launch or resume app, ret(%d) result(%s)", ret, result);
403 /* send result noti */
404 ret = vcdc_send_result(temp_cmd->pid, vcd_client_manager_get_pid(), temp_cmd->type);
406 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Fail to send result, ret(%d)", ret);
409 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Send result : pid(%d) type(%d)", temp_cmd->pid, temp_cmd->type);
410 pre_pid = temp_cmd->pid;
411 pre_type = temp_cmd->type;
417 pid_list = g_slist_remove_link(pid_list, iter);
418 iter = g_slist_nth(pid_list, 0);
423 if (VCD_RECOGNITION_MODE_RESTART_CONTINUOUSLY != vcd_client_get_recognition_mode()) {
424 vcd_config_set_service_state(VCD_STATE_READY);
425 vcdc_send_service_state(VCD_STATE_READY);
431 //static void __vcd_server_pre_result_cb(vcp_pre_result_event_e event, const char* pre_result, void *user_data)
432 static void __vcd_server_asr_result_cb(vcp_asr_result_event_e event, const char* asr_result, void *user_data)
434 if (NULL != asr_result) {
435 SLOG(LOG_DEBUG, TAG_VCD, "[Server] ASR result - Event(%d), Text(%s)", event, asr_result);
436 vcdc_send_pre_result_to_manager(vcd_client_manager_get_pid(), event, asr_result);
442 static void* __recorder_stop(void *data)
448 static void __vcd_server_result_cb(vcp_result_event_e event, int* result_id, int count, const char* all_result,
449 const char* non_fixed_result, const char* nlu_result, const char* msg, void *user_data)
451 if (VCD_STATE_PROCESSING != vcd_config_get_service_state()) {
452 if (VCD_RECOGNITION_MODE_RESTART_CONTINUOUSLY != vcd_client_get_recognition_mode()) {
453 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Current state is not 'Processing' and mode is not 'Restart continuously'");
458 vc_info_parser_unset_result(vcd_client_manager_get_exclusive());
459 vcd_client_manager_set_result_text(all_result);
461 SLOG(LOG_INFO, TAG_VCD, "[Server] Event(%d), Text(%s) Nonfixed(%s) Msg(%s) Result count(%d)",
462 event, all_result, non_fixed_result, msg, count);
464 if (VCD_RECOGNITION_MODE_RESTART_AFTER_REJECT == vcd_client_get_recognition_mode()) {
465 if (VCP_RESULT_EVENT_REJECTED == event) {
466 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Restart by no or rejected result");
467 /* If no result and restart option is ON */
468 /* Send reject message */
469 bool temp = vcd_client_manager_get_exclusive();
470 vc_info_parser_set_result(all_result, event, msg, NULL, temp);
471 if (0 != vcdc_send_result_to_manager(vcd_client_manager_get_pid(), VC_RESULT_TYPE_NOTIFICATION)) {
472 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Fail to send result");
475 g_restart_timer = ecore_timer_add(0, __restart_engine, NULL);
478 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Stop recorder due to success");
479 //vcd_recorder_stop();
480 ecore_main_loop_thread_safe_call_sync(__recorder_stop, NULL);
481 } else if (VCD_RECOGNITION_MODE_RESTART_CONTINUOUSLY == vcd_client_get_recognition_mode()) {
482 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Restart continuously");
483 /* Restart option is ON */
484 g_restart_timer = ecore_timer_add(0, __restart_engine, NULL);
485 if (VCP_RESULT_EVENT_REJECTED == event) {
486 bool temp = vcd_client_manager_get_exclusive();
487 vc_info_parser_set_result(all_result, event, msg, NULL, temp);
488 if (0 != vcdc_send_result_to_manager(vcd_client_manager_get_pid(), VC_RESULT_TYPE_NOTIFICATION)) {
489 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Fail to send result");
496 int pid = vcd_client_widget_get_foreground_pid();
498 if (NULL != all_result) {
499 vc_info_parser_set_result(all_result, event, msg, NULL, false);
501 vcd_client_widget_get_asr_result_enabled(pid, &enable);
502 if (true == enable) {
503 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Send ASR result to Widget client");
504 bool is_consumed = false;
505 if (0 != vcdc_send_asr_result(pid, VC_COMMAND_TYPE_WIDGET, &is_consumed)) {
506 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to send asr result");
508 SLOG(LOG_DEBUG, TAG_VCD, "[Server] ASR result is consumed(%d)", is_consumed);
509 if (true == is_consumed) {
510 vcdc_send_show_tooltip(pid, false);
511 if (-1 != vcd_client_manager_get_pid()) {
512 /* Manager client is available */
513 if (0 != vcdc_send_result_to_manager(vcd_client_manager_get_pid(), VC_RESULT_TYPE_NOTIFICATION)) {
514 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Fail to send result");
518 vcd_client_manager_set_exclusive(false);
526 /* if nlu_result is exist, Add command handle(is_action) into result list */
528 SLOG(LOG_DEBUG, TAG_VCD, "[Server] === Get engine result ===");
531 vc_cmd_s* temp_cmd = NULL;
532 vc_cmd_list_h vc_cmd_list = NULL;
534 if (0 != vc_cmd_list_create(&vc_cmd_list)) {
535 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Fail to create command list");
536 vcd_client_manager_set_exclusive(false);
537 vcd_config_set_service_state(VCD_STATE_READY);
538 vcdc_send_service_state(VCD_STATE_READY);
542 /* priority filter */
543 /* system > exclusive > foreground = widget > system_background > background */
545 int* filtered_id = (int*)calloc(count, sizeof(int));
546 int filtered_count = 0;
547 int top_priority = VC_COMMAND_PRIORITY_BACKGROUND;
548 for (i = 0; i < count; i++) {
549 SLOG(LOG_INFO, TAG_VCD, "[Server] [%d] Result id(%d)", i, result_id[i]);
551 if (0 > result_id[i]) {
552 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Result ID(%d) is NOT valid", result_id[i]);
556 ret = vcd_client_get_cmd_from_result_id(result_id[i], &temp_cmd);
557 if (0 == ret && NULL != temp_cmd) {
558 if (top_priority == temp_cmd->priority) {
559 filtered_id[filtered_count] = result_id[i];
561 } else if (top_priority < temp_cmd->priority) {
564 filtered_id[0] = result_id[i];
566 top_priority = temp_cmd->priority;
572 for (i = 0; i < filtered_count; i++) {
573 SLOG(LOG_INFO, TAG_VCD, "[Server] [%d] Filtered Result id(%d)", i, filtered_id[i]);
575 if (filtered_id[i] < 0) {
576 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Filtered ID(%d) is NOT valid", filtered_id[i]);
580 ret = vcd_client_get_cmd_from_result_id(filtered_id[i], &temp_cmd);
581 if (0 == ret && NULL != temp_cmd) {
582 switch (temp_cmd->format) {
583 case VC_CMD_FORMAT_FIXED:
584 case VC_CMD_FORMAT_FIXED_AND_VFIXED:
585 case VC_CMD_FORMAT_VFIXED_AND_FIXED:
586 case VC_CMD_FORMAT_PARTIAL:
587 case VC_CMD_FORMAT_FIXED_AND_NONFIXED:
588 case VC_CMD_FORMAT_NONFIXED_AND_FIXED:
590 case VC_CMD_FORMAT_ACTION:
594 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Unknown command type : %d", temp_cmd->type);
598 if (0 != vc_cmd_list_add(vc_cmd_list, (vc_cmd_h)temp_cmd)) {
599 SLOG(LOG_DEBUG, TAG_VCD, "Fail to add command to list");
600 vc_cmd_destroy((vc_cmd_h)temp_cmd);
603 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] NOT found matached result(%d)", filtered_id[i]);
607 if (NULL != filtered_id) {
612 if (NULL != nlu_result) {
613 SLOG(LOG_INFO, TAG_VCD, "[Server] NLU (%s)", nlu_result);
614 vc_info_parser_set_nlu_result(nlu_result);
615 if (0 == is_action) {
617 if (0 != vc_cmd_create(&nlu_cmd)) {
618 SLOG(LOG_ERROR, TAG_VCD, "Fail to nlu cmd create");
620 if (0 != vc_cmd_set_type(nlu_cmd, VC_COMMAND_TYPE_SYSTEM)) {
621 SLOG(LOG_ERROR, TAG_VCD, "Fail to set type");
623 if (0 != vc_cmd_set_pid(nlu_cmd, vcd_client_manager_get_pid())) {
624 SLOG(LOG_ERROR, TAG_VCD, "Fail to set pid");
626 if (0 != vc_cmd_set_format(nlu_cmd, VC_CMD_FORMAT_ACTION)) {
627 SLOG(LOG_ERROR, TAG_VCD, "Fail to set format");
629 if (0 != vc_cmd_list_add(vc_cmd_list, nlu_cmd)) {
630 SLOG(LOG_ERROR, TAG_VCD, "Fail to add nlu cmd to list");
631 vc_cmd_destroy(nlu_cmd);
637 vc_cmd_print_list(vc_cmd_list);
639 SLOG(LOG_DEBUG, TAG_VCD, "[Server] =========================");
641 int result_count = 0;
642 vc_cmd_list_get_count(vc_cmd_list, &result_count);
644 if (0 == result_count) {
646 if (NULL != all_result) {
647 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Engine result is no command : %s", all_result);
649 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Engine result is NULL");
651 bool temp = vcd_client_manager_get_exclusive();
652 vc_info_parser_set_result(all_result, event, msg, NULL, temp);
654 int pid = vcd_client_widget_get_foreground_pid();
656 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Request tooltip hide");
657 /* Send to hide tooltip */
658 vcdc_send_show_tooltip(pid, false);
661 if (-1 != vcd_client_manager_get_pid()) {
662 /* Manager client is available */
663 if (0 != vcdc_send_result_to_manager(vcd_client_manager_get_pid(), VC_RESULT_TYPE_NORMAL)) {
664 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Fail to send result");
668 vcd_client_manager_set_exclusive(false);
672 if (false == vcd_client_manager_get_exclusive()) {
673 int pid = vcd_client_widget_get_foreground_pid();
675 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Request tooltip hide");
676 vcdc_send_show_tooltip(pid, false);
679 vc_info_parser_set_result(all_result, event, msg, vc_cmd_list, false);
681 if (-1 != vcd_client_manager_get_pid()) {
682 /* Manager client is available */
683 if (0 != vcdc_send_result_to_manager(vcd_client_manager_get_pid(), VC_RESULT_TYPE_NORMAL)) {
684 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Fail to send result");
687 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Manager is NOT available. Send result to client directly");
688 /* Send result to client */
689 ecore_timer_add(0, __vcd_send_selected_result, NULL);
692 /* exclusive command */
693 vc_info_parser_set_result(all_result, event, msg, vc_cmd_list, true);
695 if (-1 != vcd_client_manager_get_pid()) {
696 /* Manager client is available */
697 if (0 != vcdc_send_result_to_manager(vcd_client_manager_get_pid(), VC_RESULT_TYPE_NORMAL)) {
698 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Fail to send result");
701 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Manager is NOT available");
704 vcd_client_manager_set_exclusive(false);
708 vc_cmd_list_destroy(vc_cmd_list, true);
714 if (NULL == result_id) {
716 if (NULL != all_result) {
717 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Engine result is no command : %s", all_result);
718 bool temp = vcd_client_manager_get_exclusive();
719 vc_info_parser_set_result(all_result, event, msg, NULL, temp);
722 int pid = vcd_client_widget_get_foreground_pid();
724 if (NULL != all_result) {
725 /* Send result text to widget */
726 vcdc_send_result(pid, vcd_client_manager_get_pid(), VC_COMMAND_TYPE_WIDGET);
729 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Request tooltip hide");
730 /* Send to hide tooltip */
731 vcdc_send_show_tooltip(pid, false);
734 if (-1 != vcd_client_manager_get_pid()) {
735 /* Manager client is available */
736 if (0 != vcdc_send_result_to_manager(vcd_client_manager_get_pid(), VC_RESULT_TYPE_NORMAL)) {
737 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Fail to send result");
741 vcd_client_manager_set_exclusive(false);
747 SLOG(LOG_DEBUG, TAG_VCD, "[Server] === Get engine result ===");
750 vc_cmd_s* temp_cmd = NULL;
751 vc_cmd_list_h vc_cmd_list = NULL;
753 if (0 != vc_cmd_list_create(&vc_cmd_list)) {
754 SLOG(LOG_ERROR, TAG_VCD, "[Server] Fail to create command list");
755 vcd_client_manager_set_exclusive(false);
756 vcd_config_set_service_state(VCD_STATE_READY);
757 vcdc_send_service_state(VCD_STATE_READY);
762 for (i = 0; i < count; i++) {
763 SLOG(LOG_DEBUG, TAG_VCD, "[Server] [%d] Result ID(%d)", i, result_id[i]);
765 if (result_id[i] < 0) {
766 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Result ID(%d) is NOT valid", result_id[i]);
770 ret = vcd_client_get_cmd_from_result_id(result_id[i], &temp_cmd);
771 if (0 == ret && NULL != temp_cmd) {
772 switch (temp_cmd->format) {
773 case VC_CMD_FORMAT_FIXED:
774 case VC_CMD_FORMAT_FIXED_AND_VFIXED:
775 case VC_CMD_FORMAT_VFIXED_AND_FIXED:
776 case VC_CMD_FORMAT_PARTIAL:
777 /* Nonfixed result is NOT valid */
779 case VC_CMD_FORMAT_FIXED_AND_NONFIXED:
780 if (NULL == temp_cmd->parameter) {
781 if (NULL != non_fixed_result) {
782 temp_cmd->parameter = strdup(non_fixed_result);
785 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Command type is NOT vaild. Parameter (%s)", temp_cmd->parameter);
788 case VC_CMD_FORMAT_NONFIXED_AND_FIXED:
789 if (NULL == temp_cmd->command) {
790 if (NULL != non_fixed_result) {
791 temp_cmd->command = strdup(non_fixed_result);
794 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Command type is NOT vaild. Command (%s)", temp_cmd->command);
799 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Unknown command type : %d", temp_cmd->type);
803 if (0 != vc_cmd_list_add(vc_cmd_list, (vc_cmd_h)temp_cmd)) {
804 SLOG(LOG_DEBUG, TAG_VCD, "Fail to add command to list");
805 vc_cmd_destroy((vc_cmd_h)temp_cmd);
808 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] NOT found matached result(%d)", result_id[i]);
812 vc_cmd_print_list(vc_cmd_list);
814 SLOG(LOG_DEBUG, TAG_VCD, "[Server] =========================");
816 int result_count = 0;
817 vc_cmd_list_get_count(vc_cmd_list, &result_count);
819 if (false == vcd_client_manager_get_exclusive()) {
820 int pid = vcd_client_widget_get_foreground_pid();
822 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Request tooltip hide");
823 vcdc_send_show_tooltip(pid, false);
826 vc_info_parser_set_result(all_result, event, msg, vc_cmd_list, false);
828 if (-1 != vcd_client_manager_get_pid()) {
829 /* Manager client is available */
830 if (0 != vcdc_send_result_to_manager(vcd_client_manager_get_pid(), VC_RESULT_TYPE_NORMAL)) {
831 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Fail to send result");
834 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Manager is NOT available. Send result to client directly");
835 /* Send result to client */
836 ecore_timer_add(0, __vcd_send_selected_result, NULL);
839 /* exclusive command */
840 vc_info_parser_set_result(all_result, event, msg, vc_cmd_list, true);
842 if (-1 != vcd_client_manager_get_pid()) {
843 /* Manager client is available */
844 if (0 != vcdc_send_result_to_manager(vcd_client_manager_get_pid(), VC_RESULT_TYPE_NORMAL)) {
845 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Fail to send result");
848 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Manager is NOT available");
851 vcd_client_manager_set_exclusive(false);
854 vc_cmd_list_destroy(vc_cmd_list, true);
861 static void __vcd_server_nlu_result_cb(vcp_result_event_e event, const char* nlu_result, void *user_data)
863 SLOG(LOG_DEBUG, TAG_VCD, "[Server] NLU result cb - event(%d)", event);
864 SLOG(LOG_DEBUG, TAG_VCD, "[Server] result (%s)", nlu_result);
866 int ret = vc_info_parser_set_nlu_result(nlu_result);
868 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to set nlu result (%d)", ret);
875 static void __vcd_server_error_cb(vcp_error_e error, const char* msg, void *user_data)
877 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Engine Error cb - reason(%d), msg(%s)", error, msg);
878 ecore_main_loop_thread_safe_call_async(__cancel_by_error, NULL);
880 char* error_msg = NULL;
881 error_msg = strdup(msg);
883 if (0 != vcdc_send_error_signal_to_manager(vcd_client_manager_get_pid(), error, error_msg)) {
884 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to send error signal");
887 if (NULL != error_msg) {
896 * vcd server Interfaces
898 static void __vcd_file_clean_up()
900 SLOG(LOG_DEBUG, TAG_VCD, "== Old file clean up == ");
903 struct dirent *dirp = NULL;
905 dp = opendir(VC_RUNTIME_INFO_ROOT);
907 SLOG(LOG_ERROR, TAG_VCD, "[File message WARN] Fail to open path : %s", VC_RUNTIME_INFO_ROOT);
911 char remove_path[256] = {0, };
916 if (!strncmp("vc_", dirp->d_name, strlen("vc_"))) {
917 memset(remove_path, 0, 256);
918 snprintf(remove_path, 256, "%s/%s", VC_RUNTIME_INFO_ROOT, dirp->d_name);
921 if (0 != remove(remove_path)) {
922 SLOG(LOG_WARN, TAG_VCD, "[File message WARN] Fail to remove file : %s", remove_path);
924 SLOG(LOG_DEBUG, TAG_VCD, "[File message] Remove file : %s", remove_path);
928 } while (NULL != dirp);
935 static int __vcd_db_clean_up()
938 int cnt = VC_COMMAND_TYPE_FOREGROUND;
940 if (VC_COMMAND_TYPE_BACKGROUND != cnt)
941 ret = vc_db_delete_commands(-1, cnt, NULL);
942 } while (VC_COMMAND_TYPE_EXCLUSIVE >= ++cnt);
951 /* Remove old file */
952 __vcd_file_clean_up();
954 /* initialize modules */
955 ret = vcd_config_initialize(__config_lang_changed_cb, __config_foreground_changed_cb, NULL);
957 SLOG(LOG_ERROR, TAG_VCD, "[Server WARNING] Fail to initialize config.");
961 ret = __vcd_db_clean_up();
963 SLOG(LOG_ERROR, TAG_VCD, "[Server WARNING] Fail to remove db data");
966 vcd_config_set_service_state(VCD_STATE_NONE);
968 //ret = vcd_engine_agent_init(__vcd_server_pre_result_cb, __vcd_server_result_cb, __vcd_server_nlu_result_cb, __vcd_server_error_cb);
969 ret = vcd_engine_agent_init(__vcd_server_asr_result_cb, __vcd_server_result_cb, __vcd_server_error_cb);
971 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to engine agent initialize : result(%d)", ret);
975 if (0 != vcd_recorder_create(__server_recorder_callback, __server_recorder_interrupt_callback)) {
976 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to create recorder");
977 return VCD_ERROR_OPERATION_FAILED;
981 ret = vcd_engine_agent_initialize_current_engine();
983 if (VCD_ERROR_ENGINE_NOT_FOUND == ret)
984 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] There is No Voice control engine");
986 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to init engine");
994 if (0 != vcd_engine_agent_load_current_engine()) {
995 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to load current engine");
996 return VCD_ERROR_OPERATION_FAILED;
999 /* Initialize manager info */
1000 vcd_client_manager_unset();
1002 vcd_config_set_service_state(VCD_STATE_READY);
1003 vcdc_send_service_state(VCD_STATE_READY);
1005 SLOG(LOG_DEBUG, TAG_VCD, "[Server SUCCESS] initialize");
1013 if (0 < g_list_length(g_proc_list)) {
1014 iter = g_list_first(g_proc_list);
1015 while (NULL != iter) {
1016 g_proc_list = g_list_remove_link(g_proc_list, iter);
1017 iter = g_list_first(g_proc_list);
1021 if (g_restart_timer != NULL) {
1022 ecore_timer_del(g_restart_timer);
1023 g_restart_timer = NULL;
1026 vcd_state_e state = vcd_config_get_service_state();
1027 if (VCD_STATE_READY != state) {
1028 if (VCD_STATE_RECORDING == state) {
1029 vcd_recorder_stop();
1031 vcd_engine_recognize_cancel();
1033 if (0 != vcd_recorder_destroy()) {
1034 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to destroy recorder");
1036 SLOG(LOG_DEBUG, TAG_VCD, "[Server] destroy recorder");
1039 if (0 != vcd_engine_agent_release()) {
1040 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to release engine");
1042 SLOG(LOG_DEBUG, TAG_VCD, "[Server] release engine");
1045 vcd_client_manager_unset_appid();
1047 vcd_config_set_service_state(VCD_STATE_NONE);
1048 vcdc_send_service_state(VCD_STATE_NONE);
1050 SLOG(LOG_DEBUG, TAG_VCD, "[Server] mode finalize");
1055 static Eina_Bool __finalize_quit_ecore_loop(void *data)
1057 SLOG(LOG_DEBUG, TAG_VCD, "[Server] quit ecore main loop");
1058 ecore_main_loop_quit();
1062 static void __read_proc()
1065 struct dirent *dirp = NULL;
1069 if (0 < g_list_length(g_proc_list)) {
1070 iter = g_list_first(g_proc_list);
1071 while (NULL != iter) {
1072 g_proc_list = g_list_remove_link(g_proc_list, iter);
1073 iter = g_list_first(g_proc_list);
1077 dp = opendir("/proc");
1079 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Fail to open proc");
1085 tmp = atoi(dirp->d_name);
1086 if (0 >= tmp) continue;
1087 g_proc_list = g_list_append(g_proc_list, GINT_TO_POINTER(tmp));
1089 } while (NULL != dirp);
1095 static void __vcd_cleanup_client(vcd_client_type_e type)
1097 int* client_list = NULL;
1098 int client_count = 0;
1105 if (VCD_CLIENT_TYPE_NORMAL == type) {
1106 ret = vcd_client_get_list(&client_list, &client_count);
1107 } else if (VCD_CLIENT_TYPE_WIDGET == type) {
1108 ret = vcd_client_widget_get_list(&client_list, &client_count);
1109 } else if (VCD_CLIENT_TYPE_MANAGER == type) {
1110 mgr_pid = vcd_client_manager_get_pid();
1111 client_list = &mgr_pid;
1113 if (-1 == mgr_pid) {
1114 SLOG(LOG_WARN, TAG_VCD, "[WARNING] Invalid Manager pid");
1119 if (0 == ret || mgr_pid > 0) {
1120 SLOG(LOG_DEBUG, TAG_VCD, "===== Clean up %s client ", type ? (type == 1) ? "Widget" : "Manager" : "Normal");
1121 if (NULL != client_list && client_count > 0) {
1122 for (i = 0; i < client_count; i++) {
1125 for (j = 0; j < g_list_length(g_proc_list); j++) {
1126 iter = g_list_nth(g_proc_list, j);
1128 if (client_list[i] == GPOINTER_TO_INT(iter->data)) {
1129 SLOG(LOG_DEBUG, TAG_VCD, "%s pid(%d) is running", type ? (type == 1) ? "Widget" : "Manager" : "Normal", client_list[i]);
1136 if (false == exist) {
1137 SLOG(LOG_ERROR, TAG_VCD, "%s pid(%d) should be removed", type ? (type == 1) ? "Widget" : "Manager" : "Normal", client_list[i]);
1138 if (VCD_CLIENT_TYPE_NORMAL == type)
1139 vcd_server_finalize(client_list[i]);
1140 else if (VCD_CLIENT_TYPE_WIDGET == type)
1141 vcd_server_widget_finalize(client_list[i]);
1143 vcd_server_mgr_finalize(mgr_pid);
1147 SLOG(LOG_DEBUG, TAG_VCD, "=====");
1148 SLOG(LOG_DEBUG, TAG_VCD, " ");
1150 if (NULL != client_list && -1 == mgr_pid) {
1157 Eina_Bool vcd_cleanup_client_all(void *data)
1161 __vcd_cleanup_client(VCD_CLIENT_TYPE_NORMAL);
1162 __vcd_cleanup_client(VCD_CLIENT_TYPE_WIDGET);
1163 __vcd_cleanup_client(VCD_CLIENT_TYPE_MANAGER);
1166 if (0 == vcd_client_get_list(&client_list, &client_count)) {
1167 SLOG(LOG_DEBUG, TAG_VCD, "===== Clean up client ");
1168 if (NULL != client_list && client_count > 0) {
1169 for (i = 0; i < client_count; i++) {
1172 for (j = 0; j < g_list_length(g_proc_list); j++) {
1173 iter = g_list_nth(g_proc_list, j);
1175 if (client_list[i] == GPOINTER_TO_INT(iter->data)) {
1176 SLOG(LOG_DEBUG, TAG_VCD, "pid(%d) is running", client_list[i]);
1183 if (false == exist) {
1184 SLOG(LOG_ERROR, TAG_VCD, "pid(%d) should be removed", client_list[i]);
1185 vcd_server_finalize(client_list[i]);
1188 result = vcdc_send_hello(client_list[i], VCD_CLIENT_TYPE_NORMAL);
1191 SLOG(LOG_DEBUG, TAG_VCD, "[Server] pid(%d) should be removed.", client_list[i]);
1192 vcd_server_finalize(client_list[i]);
1193 } else if (-1 == result) {
1194 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Hello result has error");
1199 SLOG(LOG_DEBUG, TAG_VCD, "=====");
1200 SLOG(LOG_DEBUG, TAG_VCD, " ");
1202 if (NULL != client_list) {
1207 /* If app is in background state, app cannot response message. */
1208 if (0 == vcd_client_widget_get_list(&client_list, &client_count)) {
1209 SLOG(LOG_DEBUG, TAG_VCD, "===== Clean up widget");
1210 if (NULL != client_list && client_count > 0) {
1211 for (i = 0; i < client_count; i++) {
1214 for (j = 0; j < g_list_length(g_proc_list); j++) {
1215 iter = g_list_nth(g_proc_list, j);
1217 if (client_list[i] == GPOINTER_TO_INT(iter->data)) {
1218 SLOG(LOG_DEBUG, TAG_VCD, "widget pid(%d) is running", client_list[i]);
1225 if (false == exist) {
1226 SLOG(LOG_ERROR, TAG_VCD, "widget pid(%d) should be removed", client_list[i]);
1227 vcd_server_widget_finalize(client_list[i]);
1230 result = vcdc_send_hello(client_list[i], VCD_CLIENT_TYPE_WIDGET);
1233 SLOG(LOG_DEBUG, TAG_VCD, "[Server] widget pid(%d) should be removed.", client_list[i]);
1234 vcd_server_widget_finalize(client_list[i]);
1235 } else if (-1 == result) {
1236 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Hello result has error");
1241 SLOG(LOG_DEBUG, TAG_VCD, "=====");
1242 SLOG(LOG_DEBUG, TAG_VCD, " ");
1245 if (NULL != client_list) {
1253 int mgr_pid = vcd_client_manager_get_pid();
1255 for (j = 0; j < g_list_length(g_proc_list); j++) {
1256 iter = g_list_nth(g_proc_list, j);
1258 if (mgr_pid == GPOINTER_TO_INT(iter->data)) {
1259 SLOG(LOG_DEBUG, TAG_VCD, "manager pid(%d) is running", mgr_pid);
1266 if (false == exist) {
1267 SLOG(LOG_ERROR, TAG_VCD, "manager pid (%d) should be removed", mgr_pid);
1268 vcd_server_mgr_finalize(mgr_pid);
1275 int vcd_server_get_service_state()
1277 return vcd_config_get_service_state();
1280 int vcd_server_get_foreground()
1283 vcd_config_get_foreground(&pid);
1291 int vcd_server_mgr_initialize(int pid)
1293 if (false == g_is_engine) {
1294 if (0 != vcd_engine_agent_initialize_current_engine()) {
1295 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] No Engine");
1296 g_is_engine = false;
1297 return VCD_ERROR_ENGINE_NOT_FOUND;
1303 /* check if pid is valid */
1304 if (false == vcd_client_manager_is_valid(pid)) {
1305 SLOG(LOG_ERROR, TAG_VCD, "[Server] old manager pid(%d) be removed", vcd_client_manager_get_pid());
1306 vcd_server_mgr_cancel();
1307 vcd_client_manager_unset();
1310 /* Add client information to client manager */
1311 if (0 != vcd_client_manager_set(pid)) {
1312 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to add manager");
1313 return VCD_ERROR_OPERATION_FAILED;
1316 if (0 != vcdc_send_manager_pid(pid))
1317 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Fail to send manager pid");
1319 SLOG(LOG_DEBUG, TAG_VCD, "[Server Success] Manager initialize : pid(%d)", pid);
1321 return VCD_ERROR_NONE;
1324 int vcd_server_mgr_finalize(int pid)
1326 /* check if pid is valid */
1327 if (false == vcd_client_manager_is_valid(pid)) {
1328 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] The manager pid(%d) is NOT valid", pid);
1329 return VCD_ERROR_INVALID_PARAMETER;
1332 /* Cancel recognition */
1333 vcd_server_mgr_cancel();
1335 if (0 != vcdc_send_manager_pid(-1))
1336 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Fail to send manager pid");
1338 /* Remove manager information */
1339 if (0 != vcd_client_manager_unset()) {
1340 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Fail to delete client");
1343 if (0 == vcd_client_get_ref_count()) {
1344 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Connected client list is empty");
1345 ecore_timer_add(0, __finalize_quit_ecore_loop, NULL);
1348 SLOG(LOG_DEBUG, TAG_VCD, "[Server Success] Manager Finalize : pid(%d)", pid);
1350 return VCD_ERROR_NONE;
1353 int vcd_server_mgr_set_command(int pid)
1355 if (0 != vcd_client_manager_set_command(pid)) {
1356 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] The manager pid(%d) is NOT valid", pid);
1357 return VCD_ERROR_INVALID_PARAMETER;
1359 return VCD_ERROR_NONE;
1362 int vcd_server_mgr_unset_command(int pid)
1364 if (0 != vcd_client_manager_unset_command(pid)) {
1365 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] The manager pid(%d) is NOT valid", pid);
1366 return VCD_ERROR_INVALID_PARAMETER;
1368 return VCD_ERROR_NONE;
1371 int vcd_server_mgr_set_demandable_client(int pid)
1373 /* check if pid is valid */
1374 if (false == vcd_client_manager_is_valid(pid)) {
1375 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] The manager pid(%d) is NOT valid", pid);
1376 return VCD_ERROR_INVALID_PARAMETER;
1379 GSList* client_list = NULL;
1380 if (0 != vc_info_parser_get_demandable_clients(&client_list)) {
1381 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to get demandable client");
1382 return VCD_ERROR_OPERATION_FAILED;
1385 /* Save client list */
1386 if (0 != vcd_client_manager_set_demandable_client(pid, client_list)) {
1387 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to set demandable client list");
1388 return VCD_ERROR_OPERATION_FAILED;
1391 return VCD_ERROR_NONE;
1394 int vcd_server_mgr_set_audio_type(int pid, const char* audio_type)
1396 /* check if pid is valid */
1397 if (false == vcd_client_manager_is_valid(pid)) {
1398 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] The manager pid(%d) is NOT valid", pid);
1399 return VCD_ERROR_INVALID_PARAMETER;
1403 vcp_audio_type_e type = VCP_AUDIO_TYPE_PCM_S16_LE;
1407 ret = vcd_engine_get_audio_format(audio_type, &type, &rate, &channel);
1409 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to get audio format : %d", ret);
1410 return VCD_ERROR_OPERATION_FAILED;
1413 ret = vcd_recorder_set(audio_type, type, rate, channel);
1415 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to set audio in type : %d", ret);
1416 return VCD_ERROR_OPERATION_FAILED;
1419 return VCD_ERROR_NONE;
1422 int vcd_server_mgr_get_audio_type(int pid, char** audio_type)
1424 /* check if pid is valid */
1425 if (false == vcd_client_manager_is_valid(pid)) {
1426 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] The manager pid(%d) is NOT valid", pid);
1427 return VCD_ERROR_INVALID_PARAMETER;
1430 int ret = vcd_recorder_get(audio_type);
1432 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to get audio in type : %d", ret);
1433 return VCD_ERROR_OPERATION_FAILED;
1436 return VCD_ERROR_NONE;
1439 int vcd_server_mgr_set_client_info(int pid)
1441 /* check if pid is valid */
1442 if (false == vcd_client_manager_is_valid(pid)) {
1443 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] The manager pid(%d) is NOT valid", pid);
1444 return VCD_ERROR_INVALID_PARAMETER;
1447 int ret = vcd_client_save_client_info();
1449 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to save client info : %d", ret);
1450 return VCD_ERROR_OPERATION_FAILED;
1453 return VCD_ERROR_NONE;
1456 static int __start_internal_recognition()
1460 if (0 != vcd_client_command_collect_command()) {
1461 SLOG(LOG_ERROR, TAG_VCD, "[Client Data ERROR] Fail to collect command");
1462 /* Send error cb to manager */
1463 int pid = vcd_client_widget_get_foreground_pid();
1465 vcdc_send_error_signal_to_manager(vcd_client_manager_get_pid(), VCD_ERROR_OPERATION_FAILED, "voice_engine.error.proc_fail");
1466 return VCD_ERROR_OPERATION_FAILED;
1469 /* 3. Set command to engine */
1470 ret = vcd_engine_set_commands();
1472 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to set commands : %d", ret);
1473 /* Send error cb to manager */
1474 int pid = vcd_client_widget_get_foreground_pid();
1476 vcdc_send_error_signal_to_manager(vcd_client_manager_get_pid(), VCD_ERROR_OPERATION_FAILED, "voice_engine.error.proc_fail");
1477 return VCD_ERROR_OPERATION_FAILED;
1480 SLOG(LOG_DEBUG, TAG_VCD, "[Server Success] Set command");
1482 bool stop_by_silence = true;
1483 if (VCD_RECOGNITION_MODE_MANUAL == vcd_client_get_recognition_mode()) {
1484 stop_by_silence = false;
1487 vcd_client_manager_set_result_text(NULL);
1489 /* 4. start recognition */
1490 ret = vcd_engine_recognize_start(stop_by_silence);
1492 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to start recognition : result(%d)", ret);
1493 /* Send error cb to manager */
1494 int pid = vcd_client_widget_get_foreground_pid();
1496 vcdc_send_error_signal_to_manager(vcd_client_manager_get_pid(), VCD_ERROR_OPERATION_FAILED, "voice_engine.error.proc_fail");
1497 return VCD_ERROR_OPERATION_FAILED;
1500 SLOG(LOG_DEBUG, TAG_VCD, "[Server Success] Start engine");
1502 /* 5. recorder start */
1503 ret = vcd_recorder_start();
1505 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to start recorder : result(%d)", ret);
1506 vcd_engine_recognize_cancel();
1507 /* Send error cb to manager */
1508 int pid = vcd_client_widget_get_foreground_pid();
1510 vcdc_send_error_signal_to_manager(vcd_client_manager_get_pid(), VCD_ERROR_OPERATION_FAILED, "voice_engine.error.proc_fail");
1514 vcd_config_set_service_state(VCD_STATE_RECORDING);
1515 vcdc_send_service_state(VCD_STATE_RECORDING);
1517 SLOG(LOG_DEBUG, TAG_VCD, "[Server Success] Start recognition(%d)", stop_by_silence);
1522 static Eina_Bool __vcd_request_show_tooltip(void *data)
1524 int pid = vcd_client_widget_get_foreground_pid();
1526 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Request tooltip show and widget command");
1527 vcdc_send_show_tooltip(pid, (bool)data);
1533 int vcd_server_mgr_start(vcd_recognition_mode_e recognition_mode, bool exclusive_cmd, bool start_by_client)
1535 /* 1. check current state */
1536 vcd_state_e state = vcd_config_get_service_state();
1538 if (VCD_STATE_READY != state) {
1539 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Current state is not ready");
1540 return VCD_ERROR_INVALID_STATE;
1542 if (-1 == vcd_client_manager_get_pid()) {
1543 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Manager is NOT available.");
1544 return VCD_ERROR_OPERATION_FAILED;
1547 SLOG(LOG_DEBUG, TAG_VCD, "[Server] set recognition mode = %d", recognition_mode);
1548 vcd_client_set_recognition_mode(recognition_mode);
1550 if (false == exclusive_cmd) {
1551 vcd_client_update_foreground_pid();
1552 /* Notify show tooltip */
1553 if (1 == vcd_config_get_command_type_enabled(VC_COMMAND_TYPE_WIDGET)) {
1554 int pid = vcd_client_widget_get_foreground_pid();
1556 SLOG(LOG_INFO, TAG_VCD, "[Server] Request tooltip show and widget command");
1557 ecore_timer_add(0, __vcd_request_show_tooltip, (void*)true);
1562 vcd_client_manager_set_exclusive(exclusive_cmd);
1566 if (true == start_by_client) {
1567 /* Get foreground pid */
1568 if (0 != vcd_config_get_foreground(&fg_pid)) {
1569 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to get foreground");
1572 /* Set client exclusive option */
1573 if (0 != vcd_client_set_exclusive_command(fg_pid)) {
1574 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to set exclusive command");
1578 int ret = __start_internal_recognition();
1580 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to start recongition : %d", ret);
1584 if (true == start_by_client) {
1585 vcd_client_unset_exclusive_command(fg_pid);
1588 return VCD_ERROR_NONE;
1591 int vcd_server_mgr_stop()
1593 /* 1. Check current state is recording */
1594 if (VCD_STATE_RECORDING != vcd_config_get_service_state()) {
1595 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Current state is not recording");
1596 return VCD_ERROR_INVALID_STATE;
1598 if (-1 == vcd_client_manager_get_pid()) {
1599 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Manager is NOT available.");
1600 return VCD_ERROR_OPERATION_FAILED;
1603 /* 2. Stop recorder */
1604 vcd_recorder_stop();
1606 /* 3. Stop engine recognition */
1607 int ret = vcd_engine_recognize_stop();
1609 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to stop recognition : %d", ret);
1612 /* 4. Set original mode */
1613 vcd_config_set_service_state(VCD_STATE_PROCESSING);
1614 vcdc_send_service_state(VCD_STATE_PROCESSING);
1616 return VCD_ERROR_NONE;
1619 int vcd_server_mgr_cancel()
1621 /* 1. Check current state */
1622 vcd_state_e state = vcd_config_get_service_state();
1623 if (VCD_STATE_RECORDING != state && VCD_STATE_PROCESSING != state) {
1624 SLOG(LOG_WARN, TAG_VCD, "[Server ERROR] Current state is not recording or processing");
1625 return VCD_ERROR_INVALID_STATE;
1627 if (-1 == vcd_client_manager_get_pid()) {
1628 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Manager is NOT available.");
1629 return VCD_ERROR_OPERATION_FAILED;
1632 if (g_restart_timer != NULL) {
1633 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Delete restart engine timer");
1634 ecore_timer_del(g_restart_timer);
1635 g_restart_timer = NULL;
1638 /* 2. Stop recorder */
1639 vcd_recorder_stop();
1640 /* 3. Cancel engine */
1641 int ret = vcd_engine_recognize_cancel();
1643 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Fail to cancel : %d", ret);
1646 if (false == vcd_client_manager_get_exclusive()) {
1647 if (1 == vcd_config_get_command_type_enabled(VC_COMMAND_TYPE_WIDGET)) {
1648 int pid = vcd_client_widget_get_foreground_pid();
1650 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Request tooltip hide");
1651 ecore_timer_add(0, __vcd_request_show_tooltip, (void*)false);
1655 vcd_client_manager_set_exclusive(false);
1659 vcd_config_set_service_state(VCD_STATE_READY);
1660 vcdc_send_service_state(VCD_STATE_READY);
1662 return VCD_ERROR_NONE;
1666 int vcd_server_mgr_result_select()
1668 __vcd_send_selected_result(NULL);
1670 return VCD_ERROR_NONE;
1673 int vcd_server_mgr_set_domain(int pid, const char* domain)
1675 /* check if pid is valid */
1676 if (false == vcd_client_manager_is_valid(pid)) {
1677 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] The manager pid(%d) is NOT valid", pid);
1678 return VCD_ERROR_INVALID_PARAMETER;
1681 vcd_state_e state = vcd_config_get_service_state();
1682 if (VCD_STATE_READY != state) {
1683 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Current state is not ready");
1684 return VCD_ERROR_INVALID_STATE;
1687 /* Set domain to engine */
1688 int ret = vcd_engine_set_domain(pid, domain);
1690 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to set domain : %d", ret);
1692 SLOG(LOG_DEBUG, TAG_VCD, "[Server SUCCESS] Set domain");
1698 int vcd_server_mgr_set_private_data(int pid, const char* key, const char* data)
1700 /* check if pid is valid */
1701 if (false == vcd_client_manager_is_valid(pid)) {
1702 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] The manager pid(%d) is NOT valid", pid);
1703 return VCD_ERROR_INVALID_PARAMETER;
1706 vcd_state_e state = vcd_config_get_service_state();
1707 if (VCD_STATE_READY != state) {
1708 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Current state is not ready");
1709 return VCD_ERROR_INVALID_STATE;
1712 /* Set private data to engine */
1713 int ret = vcd_engine_set_private_data(pid, key, data);
1715 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to set private data : %d", ret);
1717 SLOG(LOG_DEBUG, TAG_VCD, "[Server SUCCESS] Set private data");
1723 int vcd_server_mgr_get_private_data(int pid, const char* key, char** data)
1725 /* check if pid is valid */
1726 if (false == vcd_client_manager_is_valid(pid)) {
1727 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] The manager pid(%d) is NOT valid", pid);
1728 return VCD_ERROR_INVALID_PARAMETER;
1730 vcd_state_e state = vcd_config_get_service_state();
1731 if (VCD_STATE_READY != state) {
1732 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Current state is not ready");
1733 return VCD_ERROR_INVALID_STATE;
1736 /* Get private data to engine */
1737 int ret = vcd_engine_get_private_data(pid, key, data);
1739 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to get private data : %d", ret);
1741 SLOG(LOG_DEBUG, TAG_VCD, "[Server SUCCESS] Set private data");
1747 int vcd_server_mgr_do_action(int pid, int type, const char* action)
1751 /* check if pid is valid */
1752 if (false == vcd_client_manager_is_valid(pid)) {
1753 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] The manager pid(%d) is NOT valid", pid);
1754 return VCD_ERROR_INVALID_PARAMETER;
1757 vcd_state_e state = vcd_config_get_service_state();
1758 if (VCD_STATE_READY != state) {
1759 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Current state is not ready");
1760 return VCD_ERROR_INVALID_STATE;
1763 /* Reqeust do action to engine */
1764 if (VCD_SEND_EVENT_TYPE_TEXT == type)
1765 ret = vcd_engine_process_text(pid, action);
1766 else if (VCD_SEND_EVENT_TYPE_LIST_EVENT == type)
1767 ret = vcd_engine_process_list_event(pid, action);
1768 else if (VCD_SEND_EVENT_TYPE_HAPTIC_EVENT == type)
1769 ret = vcd_engine_process_haptic_event(pid, action);
1772 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to process do action : %d", ret);
1774 SLOG(LOG_DEBUG, TAG_VCD, "[Server SUCCESS] Process do action");
1780 int vcd_server_mgr_enable_command_type(int pid, int cmd_type)
1784 /* check if pid is valid */
1785 if (false == vcd_client_manager_is_valid(pid)) {
1786 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] The manager pid(%d) is NOT valid", pid);
1787 return VCD_ERROR_INVALID_PARAMETER;
1790 vcd_state_e state = vcd_config_get_service_state();
1791 if (VCD_STATE_READY != state) {
1792 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Current state is not ready");
1793 return VCD_ERROR_INVALID_STATE;
1796 ret = vcd_config_enable_command_type(cmd_type);
1798 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to enable command type");
1800 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Enable command type(%d)", cmd_type);
1806 int vcd_server_mgr_disable_command_type(int pid, int cmd_type)
1810 /* check if pid is valid */
1811 if (false == vcd_client_manager_is_valid(pid)) {
1812 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] The manager pid(%d) is NOT valid", pid);
1813 return VCD_ERROR_INVALID_PARAMETER;
1816 vcd_state_e state = vcd_config_get_service_state();
1817 if (VCD_STATE_READY != state) {
1818 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Current state is not ready");
1819 return VCD_ERROR_INVALID_STATE;
1822 ret = vcd_config_disable_command_type(cmd_type);
1824 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to disable command type");
1826 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Disable command type(%d)", cmd_type);
1833 * VC Server Functions for Client
1835 int vcd_server_initialize(int pid)
1837 if (false == g_is_engine) {
1838 if (0 != vcd_engine_agent_initialize_current_engine()) {
1839 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] No Engine");
1840 g_is_engine = false;
1841 return VCD_ERROR_ENGINE_NOT_FOUND;
1847 if (false == vcd_engine_is_available_engine()) {
1848 if (0 != vcd_engine_agent_initialize_current_engine()) {
1849 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] No Engine");
1850 return VCD_ERROR_ENGINE_NOT_FOUND;
1854 /* check if pid is valid */
1855 if (true == vcd_client_is_available(pid)) {
1856 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] The pid is already exist");
1857 return VCD_ERROR_INVALID_PARAMETER;
1860 /* Add client information to client manager */
1861 if (0 != vcd_client_add(pid)) {
1862 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to add client info");
1863 return VCD_ERROR_OPERATION_FAILED;
1866 SLOG(LOG_DEBUG, TAG_VCD, "[Server Success] Client Initialize : pid(%d)", pid);
1868 return VCD_ERROR_NONE;
1871 int vcd_server_finalize(int pid)
1873 /* check if pid is valid */
1874 if (false == vcd_client_is_available(pid)) {
1875 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] pid is NOT valid ");
1876 return VCD_ERROR_INVALID_PARAMETER;
1879 /* Remove client information */
1880 if (0 != vcd_client_delete(pid)) {
1881 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Fail to delete client");
1884 if (0 == vcd_client_get_ref_count()) {
1885 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Connected client list is empty");
1886 ecore_timer_add(0, __finalize_quit_ecore_loop, NULL);
1889 SLOG(LOG_DEBUG, TAG_VCD, "[Server Success] Client Finalize : pid(%d)", pid);
1891 return VCD_ERROR_NONE;
1894 int vcd_server_set_command(int pid, vc_cmd_type_e cmd_type)
1896 /* check if pid is valid */
1897 if (false == vcd_client_is_available(pid)) {
1898 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] pid is NOT valid ");
1899 return VCD_ERROR_INVALID_PARAMETER;
1902 if (0 != vcd_client_set_command_type(pid, cmd_type)) {
1903 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to set command type : pid(%d), cmd_type(%d)", pid, cmd_type);
1904 return VCD_ERROR_OPERATION_FAILED;
1910 int vcd_server_unset_command(int pid, vc_cmd_type_e cmd_type)
1912 /* check if pid is valid */
1913 if (false == vcd_client_is_available(pid)) {
1914 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] pid is NOT valid ");
1915 return VCD_ERROR_INVALID_PARAMETER;
1918 if (0 != vcd_client_unset_command_type(pid, cmd_type)) {
1919 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to unset command type : pid(%d), cmd_type(%d)", pid, cmd_type);
1920 return VCD_ERROR_OPERATION_FAILED;
1926 int vcd_server_set_foreground(int pid, bool value)
1928 /* check if pid is valid */
1929 if (false == vcd_client_is_available(pid) && false == vcd_client_widget_is_available(pid)) {
1930 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] pid is NOT valid ");
1931 return VCD_ERROR_INVALID_PARAMETER;
1934 if (0 != vcd_config_set_foreground(pid, value)) {
1935 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to set foreground : pid(%d), value(%d)", pid, value);
1936 return VCD_ERROR_OPERATION_FAILED;
1942 static int __vcd_server_launch_manager_app()
1945 bool running = false;
1948 ret = vcd_client_manager_get_appid(&appid);
1949 if (0 != ret || NULL == appid) {
1950 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Fail to get manager appid");
1951 return VCD_ERROR_OPERATION_FAILED;
1953 ret = app_manager_is_running(appid, &running);
1955 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Fail to check running with appid(%s)", appid);
1958 return VCD_ERROR_OPERATION_FAILED;
1960 if (false == running) {
1961 int tmp_ret = __vcd_is_package_installed(appid);
1962 if (false == tmp_ret) {
1963 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] manager app is not installed, appid(%s)", appid);
1965 ret = __vcd_activate_app_by_appcontrol(appid);
1967 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Fail to activate app");
1972 SLOG(LOG_ERROR, TAG_VCD, "Launch vc manager app: appid(%s)", appid);
1975 ret = __vcd_resume_app(appid);
1977 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Fail to resume app");
1982 SLOG(LOG_ERROR, TAG_VCD, "Resume vc manager app: appid(%s)", appid);
1988 return VCD_ERROR_NONE;
1991 int vcd_server_dialog(int pid, const char* disp_text, const char* utt_text, int continuous)
1993 /* check if pid is valid */
1994 if (false == vcd_client_is_available(pid) && false == vcd_client_widget_is_available(pid)) {
1995 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] pid is NOT valid ");
1996 return VCD_ERROR_INVALID_PARAMETER;
1999 int ret = __vcd_server_launch_manager_app();
2001 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);
2005 ret = vcdc_send_dialog(vcd_client_manager_get_pid(), pid, disp_text, utt_text, continuous);
2007 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);
2014 int vcd_server_is_system_command_valid(int pid, int* is_sys_cmd_valid)
2016 /* check if pid is valid */
2017 if (false == vcd_client_is_available(pid) && false == vcd_client_widget_is_available(pid)) {
2018 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] pid is NOT valid ");
2019 return VCD_ERROR_INVALID_PARAMETER;
2022 /* check if system command is valid */
2023 if (true == vcd_client_manager_is_system_command_valid(vcd_client_manager_get_pid()))
2024 *is_sys_cmd_valid = true;
2026 *is_sys_cmd_valid = false;
2032 int vcd_server_set_exclusive_command(int pid, bool value)
2034 /* check if pid is valid */
2035 if (false == vcd_client_is_available(pid)) {
2036 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] pid is NOT valid ");
2037 return VCD_ERROR_INVALID_PARAMETER;
2040 if (true == value) {
2041 if (0 != vcd_client_set_exclusive_command(pid)) {
2042 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to set exclusive command : pid(%d)", pid);
2043 return VCD_ERROR_OPERATION_FAILED;
2046 if (0 != vcd_client_unset_exclusive_command(pid)) {
2047 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to unset exclusive command : pid(%d)", pid);
2048 return VCD_ERROR_OPERATION_FAILED;
2055 int vcd_server_request_start(int pid, bool stop_by_silence)
2057 /* check if pid is valid */
2058 if (false == vcd_client_is_available(pid)) {
2059 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] pid(%d) is NOT forground client", pid);
2060 return VCD_ERROR_INVALID_PARAMETER;
2064 /* Check current state */
2065 vcd_state_e state = vcd_config_get_service_state();
2067 /* Service state should be ready */
2068 if (VCD_STATE_READY != state) {
2069 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Current state is not Ready : pid(%d)", pid);
2070 return VCD_ERROR_INVALID_STATE;
2073 if (-1 != vcd_client_manager_get_pid()) {
2074 /* Check current pid is valid */
2075 if (false == vcd_client_manager_check_demandable_client(pid)) {
2076 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Current client is NOT available : pid(%d)", pid);
2077 return VCD_ERROR_INVALID_PARAMETER;
2081 ret = vcd_server_mgr_start(stop_by_silence, false);
2083 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Fail to start recognition");
2084 return VCD_ERROR_INVALID_PARAMETER;
2090 int vcd_server_request_stop(int pid)
2093 /* Check current state */
2094 vcd_state_e state = vcd_config_get_service_state();
2096 /* Service state should be ready */
2097 if (VCD_STATE_RECORDING != state) {
2098 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Current state is not Recording : pid(%d)", pid);
2099 return VCD_ERROR_INVALID_STATE;
2102 if (-1 != vcd_client_manager_get_pid()) {
2103 /* Check current pid is valid */
2104 if (false == vcd_client_manager_check_demandable_client(pid)) {
2105 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Current client is NOT available : pid(%d)", pid);
2106 return VCD_ERROR_INVALID_PARAMETER;
2110 ret = vcd_server_mgr_stop();
2112 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Fail to start recognition");
2113 return VCD_ERROR_OPERATION_FAILED;
2116 return VCD_ERROR_NONE;
2119 int vcd_server_request_cancel(int pid)
2122 /* Check current state */
2123 vcd_state_e state = vcd_config_get_service_state();
2125 /* Service state should be recording or processing */
2126 if (VCD_STATE_RECORDING != state && VCD_STATE_PROCESSING != state) {
2127 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Current state is not Recording or Processing : pid(%d)", pid);
2128 return VCD_ERROR_INVALID_STATE;
2131 if (-1 != vcd_client_manager_get_pid()) {
2132 /* Check current pid is valid */
2133 if (false == vcd_client_manager_check_demandable_client(pid)) {
2134 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Current client is NOT available : pid(%d)", pid);
2135 return VCD_ERROR_INVALID_PARAMETER;
2139 ret = vcd_server_mgr_cancel();
2141 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Fail to start recognition");
2142 return VCD_ERROR_OPERATION_FAILED;
2145 return VCD_ERROR_NONE;
2150 * VC Server Functions for Widget lib
2152 int vcd_server_widget_initialize(int pid)
2154 if (false == g_is_engine) {
2155 if (0 != vcd_engine_agent_initialize_current_engine()) {
2156 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] No Engine");
2157 g_is_engine = false;
2158 return VCD_ERROR_ENGINE_NOT_FOUND;
2164 if (false == vcd_engine_is_available_engine()) {
2165 if (0 != vcd_engine_agent_initialize_current_engine()) {
2166 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] No Engine");
2167 return VCD_ERROR_ENGINE_NOT_FOUND;
2171 /* check if pid is valid */
2172 if (true == vcd_client_widget_is_available(pid)) {
2173 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] The pid is already exist");
2174 return VCD_ERROR_NONE;
2177 /* Add client information to client manager */
2178 if (0 != vcd_client_widget_add(pid)) {
2179 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to add client info");
2180 return VCD_ERROR_OPERATION_FAILED;
2183 SLOG(LOG_DEBUG, TAG_VCD, "[Server Success] Initialize widget : pid(%d)", pid);
2185 return VCD_ERROR_NONE;
2188 int vcd_server_widget_finalize(int pid)
2190 /* check if pid is valid */
2191 if (false == vcd_client_widget_is_available(pid)) {
2192 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] pid is NOT valid ");
2193 return VCD_ERROR_INVALID_PARAMETER;
2196 /* Remove client information */
2197 if (0 != vcd_client_widget_delete(pid)) {
2198 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Fail to delete client");
2201 if (0 == vcd_client_get_ref_count()) {
2202 SLOG(LOG_DEBUG, TAG_VCD, "[Server] connected client list is empty");
2203 ecore_timer_add(0, __finalize_quit_ecore_loop, NULL);
2206 return VCD_ERROR_NONE;
2209 int vcd_server_widget_start_recording(int pid, bool widget_command)
2211 /* check if pid is valid */
2212 if (false == vcd_client_widget_is_available(pid)) {
2213 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] pid is NOT valid ");
2214 return VCD_ERROR_INVALID_PARAMETER;
2217 if (true == widget_command) {
2218 vcd_client_widget_set_command(pid);
2219 SLOG(LOG_DEBUG, TAG_VCD, "[Server] widget command is available");
2221 vcd_client_widget_unset_command(pid);
2222 SLOG(LOG_WARN, TAG_VCD, "[Server] widget command is NOT available");
2225 int ret = __start_internal_recognition();
2227 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to start recongition : %d", ret);
2228 ecore_timer_add(0, __vcd_request_show_tooltip, (void*)false);
2234 int vcd_server_widget_start(int pid, bool stop_by_silence)
2236 /* check if pid is valid */
2237 int fore_pid = vcd_client_widget_get_foreground_pid();
2238 if (pid != fore_pid) {
2239 SLOG(LOG_ERROR, TAG_VCD, "[Server] pid is NOT foreground");
2240 return VCD_ERROR_INVALID_PARAMETER;
2243 /* Check current state */
2244 vcd_state_e state = vcd_config_get_service_state();
2246 /* Service state should be ready */
2247 if (VCD_STATE_READY != state) {
2248 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Current state is not Ready : pid(%d)", pid);
2249 return VCD_ERROR_INVALID_STATE;
2252 vcd_client_set_slience_detection(stop_by_silence);
2254 /* Notify show tooltip */
2255 ecore_timer_add(0, __vcd_request_show_tooltip, (void*)true);
2260 int vcd_server_widget_stop(int pid)
2262 /* check if pid is valid */
2263 int fore_pid = vcd_client_widget_get_foreground_pid();
2264 if (pid != fore_pid) {
2265 SLOG(LOG_ERROR, TAG_VCD, "[Server] pid is NOT foreground");
2266 return VCD_ERROR_INVALID_PARAMETER;
2270 /* Check current state */
2271 vcd_state_e state = vcd_config_get_service_state();
2273 /* Service state should be recording */
2274 if (VCD_STATE_RECORDING != state) {
2275 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Current service state is not Recording : pid(%d)", pid);
2276 return VCD_ERROR_INVALID_STATE;
2279 ret = vcd_server_mgr_stop();
2281 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Fail to start recognition");
2282 return VCD_ERROR_OPERATION_FAILED;
2285 return VCD_ERROR_NONE;
2288 int vcd_server_widget_cancel(int pid)
2290 /* check if pid is valid */
2291 int fore_pid = vcd_client_widget_get_foreground_pid();
2292 if (pid != fore_pid) {
2293 SLOG(LOG_ERROR, TAG_VCD, "[Server] pid is NOT foreground");
2294 return VCD_ERROR_INVALID_PARAMETER;
2298 /* Check current state */
2299 vcd_state_e state = vcd_config_get_service_state();
2301 /* Service state should be recording or processing */
2302 if (VCD_STATE_RECORDING != state && VCD_STATE_PROCESSING != state) {
2303 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Current state is not Recording or Processing : pid(%d)", pid);
2304 return VCD_ERROR_INVALID_STATE;
2307 ret = vcd_server_mgr_cancel();
2309 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Fail to cancel recognition : %d", ret);
2313 return VCD_ERROR_NONE;
2316 int vcd_server_widget_enable_asr_result(int pid, bool enable)
2319 ret = vcd_client_widget_set_asr_result_enabled(pid, enable);
2321 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Fail to enable asr result : %d", ret);