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.
17 #include <app_control.h>
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"
34 #include "voice_control_common.h"
37 * VC Server static variable
39 static bool g_is_engine;
41 static GList *g_proc_list = NULL;
43 static Ecore_Timer *g_restart_timer = NULL;
46 * @brief Enumerations of send event type.
49 VCD_SEND_EVENT_TYPE_TEXT, /**< send text event to vc engine*/
50 VCD_SEND_EVENT_TYPE_LIST_EVENT, /**< send list event to vc engine */
51 VCD_SEND_EVENT_TYPE_HAPTIC_EVENT /**< send haptic event to vc engine */
52 } vcd_send_event_type_e;
54 static int __vcd_server_launch_manager_app();
57 * VC Server Internal Functions
59 static Eina_Bool __stop_by_silence(void *data)
61 SLOG(LOG_DEBUG, TAG_VCD, "@@@ Silence Detected ");
63 vcd_server_mgr_stop();
65 SLOG(LOG_DEBUG, TAG_VCD, "@@@");
69 static Eina_Bool __cancel_by_interrupt(void *data)
71 SLOG(LOG_DEBUG, TAG_VCD, "@@@ Cancel by interrupt");
73 vcd_server_mgr_cancel();
75 SLOG(LOG_DEBUG, TAG_VCD, "@@@");
79 static void __cancel_by_error(void *data)
81 SLOG(LOG_DEBUG, TAG_VCD, "@@@ Cancel by error");
83 vcd_server_mgr_cancel();
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, "@@@");
115 static int __server_recorder_callback(const void* data, const unsigned int length)
117 vcd_state_e state = vcd_config_get_service_state();
118 if (VCD_STATE_READY == state) {
119 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Ready state, but recording");
120 } else if (VCD_STATE_PROCESSING == state) {
124 vcp_speech_detect_e speech_detected = VCP_SPEECH_DETECT_NONE;
127 ret = vcd_engine_recognize_audio(data, length, &speech_detected);
131 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to set recording data to engine(%d)", ret);
132 ecore_timer_add(0, __cancel_by_interrupt, NULL);
133 /* Send error cb to manager */
134 if (VCP_ERROR_OUT_OF_NETWORK == ret) {
135 vcdc_send_error_signal_to_manager(vcd_client_manager_get_pid(), VCD_ERROR_TIMED_OUT, "Engine connection failed");
137 vcdc_send_error_signal_to_manager(vcd_client_manager_get_pid(), VCD_ERROR_OPERATION_FAILED, "Engine recognition failed");
142 if (VCP_SPEECH_DETECT_BEGIN == speech_detected) {
143 if (-1 != vcd_client_manager_get_pid()) {
144 /* Manager client is available */
145 if (0 != vcdc_send_speech_detected(vcd_client_manager_get_pid())) {
146 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Fail to send speech detected");
149 } else if (VCP_SPEECH_DETECT_END == speech_detected) {
150 if (VCD_RECOGNITION_MODE_STOP_BY_SILENCE == vcd_client_get_recognition_mode()) {
151 /* silence detected */
152 ecore_timer_add(0, __stop_by_silence, NULL);
153 } else if (VCD_RECOGNITION_MODE_RESTART_AFTER_REJECT == vcd_client_get_recognition_mode()) {
154 /* Stop engine recognition */
155 int ret = vcd_engine_recognize_stop();
157 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to stop recognition : %d", ret);
159 vcd_config_set_service_state(VCD_STATE_PROCESSING);
160 vcdc_send_service_state(VCD_STATE_PROCESSING);
162 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Stop engine only by silence");
163 } else if (VCD_RECOGNITION_MODE_RESTART_CONTINUOUSLY == vcd_client_get_recognition_mode()) {
164 /* Stop engine recognition */
165 int ret = vcd_engine_recognize_stop();
167 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to stop recognition : %d", ret);
175 void __server_recorder_interrupt_callback()
177 SLOG(LOG_DEBUG, TAG_VCD, "@@@ Cancel by sound interrupt");
179 ecore_timer_add(0, __cancel_by_interrupt, NULL);
181 SLOG(LOG_DEBUG, TAG_VCD, "@@@");
184 static void __config_lang_changed_cb(const char* current_lang, void* user_data)
186 SLOG(LOG_DEBUG, TAG_VCD, "@@@ Change language ");
188 /* Current state is recording */
189 vcd_state_e state = vcd_config_get_service_state();
190 if (VCD_STATE_RECORDING == state || VCD_STATE_PROCESSING == state) {
191 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Current state is 'Recording'. Cancel recognition");
192 vcd_server_mgr_cancel();
196 ret = vcd_engine_set_current_language(current_lang);
198 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to set language of engine : %d", ret);
201 SLOG(LOG_DEBUG, TAG_VCD, "@@@");
206 static void __config_foreground_changed_cb(int previous, int current, void* user_data)
208 SLOG(LOG_DEBUG, TAG_VCD, "@@@ Change foreground");
210 SLOG(LOG_DEBUG, TAG_VCD, "Foreground pid(%d)", current);
212 if (VC_NO_FOREGROUND_PID != current) {
213 /* Foreground app is changed */
214 vcd_state_e state = vcd_config_get_service_state();
215 if (VCD_STATE_RECORDING == state) {
216 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Foreground pid(%d) is changed. Cancel recognition", current);
217 ecore_timer_add(0, __cancel_by_interrupt, NULL);
221 SLOG(LOG_DEBUG, TAG_VCD, "@@@");
226 static int __vcd_activate_app_by_appcontrol(const char* appid)
229 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Invalid parameter");
230 return VCD_ERROR_INVALID_PARAMETER;
234 app_control_h app_control = NULL;
235 ret = app_control_create(&app_control);
236 if (APP_CONTROL_ERROR_NONE == ret) {
238 ret = app_control_add_extra_data(app_control, "voice_launch", "get_result");
239 if (APP_CONTROL_ERROR_NONE != ret) {
240 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Fail to add extra data, ret(%d)", ret);
241 app_control_destroy(app_control);
242 return VCD_ERROR_OPERATION_FAILED;
245 ret = app_control_set_app_id(app_control, appid);
246 if (APP_CONTROL_ERROR_NONE != ret) {
247 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Fail to set app id, ret(%d)", ret);
248 app_control_destroy(app_control);
249 return VCD_ERROR_OPERATION_FAILED;
251 // Sent launch request
252 ret = app_control_send_launch_request(app_control, NULL, NULL);
253 if (APP_CONTROL_ERROR_NONE != ret) {
254 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Fail to send launch request, ret(%d)", ret);
255 app_control_destroy(app_control);
256 return VCD_ERROR_OPERATION_FAILED;
258 // Destroy app control
259 ret = app_control_destroy(app_control);
260 if (APP_CONTROL_ERROR_NONE != ret) {
261 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Fail to destroy, ret(%d)", ret);
262 return VCD_ERROR_OPERATION_FAILED;
265 return VCD_ERROR_NONE;
268 static int __vcd_resume_app(const char* appid)
270 app_context_h app_context = NULL;
271 int ret = app_manager_get_app_context(appid, &app_context);
272 if (APP_MANAGER_ERROR_NONE != ret) {
273 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Fail to get app_context, ret(%d), appid(%s)", ret, appid);
274 return VCD_ERROR_OPERATION_FAILED;
277 ret = app_manager_resume_app(app_context);
278 if (APP_MANAGER_ERROR_NONE != ret) {
279 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Fail to resume app, ret(%d), appid(%s)", ret, appid);
280 return VCD_ERROR_OPERATION_FAILED;
282 return VCD_ERROR_NONE;
285 static bool __vcd_is_package_installed(const char* appid)
287 app_info_h app_info = NULL;
288 int ret = app_manager_get_app_info(appid, &app_info);
289 if (APP_MANAGER_ERROR_NONE != ret || NULL == app_info)
291 ret = app_info_destroy(app_info);
292 if (APP_MANAGER_ERROR_NONE != ret)
293 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Fail to destroy app_info, ret(%d)", ret);
297 static bool __vcd_launch_app(const char* result)
299 if (NULL == result) {
300 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Invalid parameter");
301 return VCD_ERROR_INVALID_PARAMETER;
304 GSList* app_list = NULL;
305 if (0 != vc_db_get_appid_list(result, &app_list)) {
306 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] result text is NULL");
307 return VCD_ERROR_INVALID_PARAMETER;
310 if (0 != g_slist_length(app_list)) {
313 vc_deactivated_app_s* temp_app = NULL;
314 iter = g_slist_nth(app_list, 0);
316 while (NULL != iter) {
317 temp_app = iter->data;
319 if (NULL != temp_app) {
320 if (NULL != temp_app->appid) {
322 bool running = false;
323 ret = app_manager_is_running(temp_app->appid, &running);
324 if (APP_MANAGER_ERROR_NONE != ret) {
325 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Fail to check running with appid(%s)", temp_app->appid);
326 free(temp_app->appid);
327 temp_app->appid = NULL;
330 return VCD_ERROR_OPERATION_FAILED;
332 if (false == running) {
333 int tmp_ret = __vcd_is_package_installed(temp_app->appid);
334 if (false == tmp_ret) {
335 SLOG(LOG_WARN, TAG_VCD, "[WARNING] app is not installed, appid(%s)", temp_app->appid);
337 ret = __vcd_activate_app_by_appcontrol(temp_app->appid);
339 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Fail to activate app");
340 free(temp_app->appid);
341 temp_app->appid = NULL;
346 SLOG(LOG_ERROR, TAG_VCD, "Launch app: appid(%s) result(%s)", temp_app->appid, result);
349 ret = __vcd_resume_app(temp_app->appid);
351 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Fail to resume app");
352 free(temp_app->appid);
353 temp_app->appid = NULL;
358 SLOG(LOG_ERROR, TAG_VCD, "Resume app: appid(%s) result(%s)", temp_app->appid, result);
360 free(temp_app->appid);
361 temp_app->appid = NULL;
366 iter = g_slist_next(iter);
371 return VCD_ERROR_NONE;
374 static Eina_Bool __vcd_send_selected_result(void *data)
376 GSList* pid_list = NULL;
377 const char* result = vcd_client_manager_get_result_text();
379 if (0 != vc_info_parser_get_result_pid_list(&pid_list, result)) {
380 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Fail to get pid list. No result");
382 if (0 < g_slist_length(pid_list)) {
384 vc_cmd_s* temp_cmd = NULL;
389 iter = g_slist_nth(pid_list, 0);
390 while (NULL != iter) {
391 temp_cmd = iter->data;
393 if (NULL != temp_cmd && (pre_pid != temp_cmd->pid || pre_type == VC_COMMAND_TYPE_WIDGET || temp_cmd->type == VC_COMMAND_TYPE_WIDGET)) {
394 /* Launch deactivated several apps that is matched with result */
395 ret = __vcd_launch_app(result);
397 SLOG(LOG_ERROR, TAG_VCD, "Fail to launch or resume app, ret(%d) result(%s)", ret, result);
399 /* send result noti */
400 ret = vcdc_send_result(temp_cmd->pid, vcd_client_manager_get_pid(), temp_cmd->type);
402 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Fail to send result, ret(%d)", ret);
405 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Send result : pid(%d) type(%d)", temp_cmd->pid, temp_cmd->type);
406 pre_pid = temp_cmd->pid;
407 pre_type = 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 __vcd_server_nlg_result_cb(const char* nlg_result, void *user_data)
440 int ret = __vcd_server_launch_manager_app();
442 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to send dialog : mgr_pid(%d), nlg_result(%s)", vcd_client_manager_get_pid(), nlg_result);
446 ret = vcdc_send_dialog(vcd_client_manager_get_pid(), -1, nlg_result, NULL, 0); //0: VC_DIALOG_END
448 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to send dialog : mgr_pid(%d), nlg_result(%s)", vcd_client_manager_get_pid(), nlg_result);
452 static void* __recorder_stop(void *data)
458 static void __vcd_server_result_cb(vcp_result_event_e event, int* result_id, int count, const char* all_result,
459 const char* non_fixed_result, const char* nlu_result, const char* msg, void *user_data)
461 vc_info_parser_unset_result(vcd_client_manager_get_exclusive());
462 vcd_client_manager_set_result_text(all_result);
464 SLOG(LOG_INFO, TAG_VCD, "[Server] Event(%d), Text(%s) Nonfixed(%s) Msg(%s) Result count(%d)",
465 event, all_result, non_fixed_result, msg, count);
467 if (VCD_RECOGNITION_MODE_RESTART_AFTER_REJECT == vcd_client_get_recognition_mode()) {
468 if (VCP_RESULT_EVENT_REJECTED == event) {
469 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Restart by no or rejected result");
470 /* If no result and restart option is ON */
471 /* Send reject message */
472 bool temp = vcd_client_manager_get_exclusive();
473 vc_info_parser_set_result(all_result, event, msg, NULL, temp);
474 if (0 != vcdc_send_result_to_manager(vcd_client_manager_get_pid(), VC_RESULT_TYPE_NOTIFICATION)) {
475 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Fail to send result");
478 g_restart_timer = ecore_timer_add(0, __restart_engine, NULL);
481 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Stop recorder due to success");
482 //vcd_recorder_stop();
483 ecore_main_loop_thread_safe_call_sync(__recorder_stop, NULL);
484 } else if (VCD_RECOGNITION_MODE_RESTART_CONTINUOUSLY == vcd_client_get_recognition_mode()) {
485 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Restart continuously");
486 /* Restart option is ON */
487 g_restart_timer = ecore_timer_add(0, __restart_engine, NULL);
488 if (VCP_RESULT_EVENT_REJECTED == event) {
489 bool temp = vcd_client_manager_get_exclusive();
490 vc_info_parser_set_result(all_result, event, msg, NULL, temp);
491 if (0 != vcdc_send_result_to_manager(vcd_client_manager_get_pid(), VC_RESULT_TYPE_NOTIFICATION)) {
492 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Fail to send result");
499 int pid = vcd_client_widget_get_foreground_pid();
501 if (NULL != all_result) {
502 vc_info_parser_set_result(all_result, event, msg, NULL, false);
504 vcd_client_widget_get_asr_result_enabled(pid, &enable);
505 if (true == enable) {
506 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Send ASR result to Widget client");
507 bool is_consumed = false;
508 if (0 != vcdc_send_asr_result(pid, VC_COMMAND_TYPE_WIDGET, &is_consumed)) {
509 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to send asr result");
511 SLOG(LOG_DEBUG, TAG_VCD, "[Server] ASR result is consumed(%d)", is_consumed);
512 if (true == is_consumed) {
513 vcdc_send_show_tooltip(pid, false);
514 if (-1 != vcd_client_manager_get_pid()) {
515 /* Manager client is available */
516 vc_info_parser_unset_result(false);
517 vc_info_parser_set_result(all_result, VC_RESULT_EVENT_RESULT_SUCCESS, msg, NULL, false);
518 if (0 != vcdc_send_result_to_manager(vcd_client_manager_get_pid(), VC_RESULT_TYPE_NOTIFICATION)) {
519 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Fail to send result");
523 vcd_client_manager_set_exclusive(false);
525 vcd_config_set_service_state(VCD_STATE_READY);
526 vcdc_send_service_state(VCD_STATE_READY);
534 /* if nlu_result is exist, Add command handle(is_action) into result list */
536 SLOG(LOG_DEBUG, TAG_VCD, "[Server] @ Get engine result @");
539 vc_cmd_s* temp_cmd = NULL;
540 vc_cmd_list_h vc_cmd_list = NULL;
542 if (0 != vc_cmd_list_create(&vc_cmd_list)) {
543 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Fail to create command list");
544 vcd_client_manager_set_exclusive(false);
545 vcd_config_set_service_state(VCD_STATE_READY);
546 vcdc_send_service_state(VCD_STATE_READY);
550 /* priority filter */
551 /* system > exclusive > foreground = widget > system_background > background */
553 int* filtered_id = (int*)calloc(count, sizeof(int));
555 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to allocate memory");
558 int filtered_count = 0;
559 int top_priority = VC_COMMAND_PRIORITY_BACKGROUND;
560 for (i = 0; i < count; i++) {
561 SLOG(LOG_INFO, TAG_VCD, "[Server] [%d] Result id(%d)", i, result_id[i]);
563 if (0 > result_id[i]) {
564 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Result ID(%d) is NOT valid", result_id[i]);
568 ret = vcd_client_get_cmd_from_result_id(result_id[i], &temp_cmd);
569 if (0 == ret && NULL != temp_cmd) {
570 if (top_priority == temp_cmd->priority) {
571 filtered_id[filtered_count] = result_id[i];
573 } else if (top_priority > temp_cmd->priority) {
574 filtered_id[0] = result_id[i];
576 top_priority = temp_cmd->priority;
579 vc_cmd_destroy((vc_cmd_h)temp_cmd);
585 for (i = 0; i < filtered_count; i++) {
586 SLOG(LOG_INFO, TAG_VCD, "[Server] [%d] Filtered Result id(%d)", i, filtered_id[i]);
588 if (filtered_id[i] < 0) {
589 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Filtered ID(%d) is NOT valid", filtered_id[i]);
593 ret = vcd_client_get_cmd_from_result_id(filtered_id[i], &temp_cmd);
594 if (0 == ret && NULL != temp_cmd) {
595 switch (temp_cmd->format) {
596 case VC_CMD_FORMAT_FIXED:
597 case VC_CMD_FORMAT_FIXED_AND_VFIXED:
598 case VC_CMD_FORMAT_VFIXED_AND_FIXED:
599 case VC_CMD_FORMAT_PARTIAL:
600 case VC_CMD_FORMAT_FIXED_AND_NONFIXED:
601 case VC_CMD_FORMAT_NONFIXED_AND_FIXED:
603 case VC_CMD_FORMAT_ACTION:
607 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Unknown command type : %d", temp_cmd->type);
611 if (0 != vc_cmd_list_add(vc_cmd_list, (vc_cmd_h)temp_cmd)) {
612 SLOG(LOG_DEBUG, TAG_VCD, "Fail to add command to list");
613 vc_cmd_destroy((vc_cmd_h)temp_cmd);
617 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] NOT found matached result(%d)", filtered_id[i]);
621 if (NULL != filtered_id) {
626 if (NULL != nlu_result) {
627 SLOG(LOG_INFO, TAG_VCD, "[Server] NLU (%s)", nlu_result);
628 vc_info_parser_set_nlu_result(nlu_result);
629 if (0 == is_action) {
631 if (0 != vc_cmd_create(&nlu_cmd)) {
632 SLOG(LOG_ERROR, TAG_VCD, "Fail to nlu cmd create");
634 if (0 != vc_cmd_set_type(nlu_cmd, VC_COMMAND_TYPE_SYSTEM)) {
635 SLOG(LOG_ERROR, TAG_VCD, "Fail to set type");
637 if (0 != vc_cmd_set_pid(nlu_cmd, vcd_client_manager_get_pid())) {
638 SLOG(LOG_ERROR, TAG_VCD, "Fail to set pid");
640 if (0 != vc_cmd_set_format(nlu_cmd, VC_CMD_FORMAT_ACTION)) {
641 SLOG(LOG_ERROR, TAG_VCD, "Fail to set format");
643 if (0 != vc_cmd_list_add(vc_cmd_list, nlu_cmd)) {
644 SLOG(LOG_ERROR, TAG_VCD, "Fail to add nlu cmd to list");
645 vc_cmd_destroy(nlu_cmd);
651 vc_cmd_print_list(vc_cmd_list);
653 SLOG(LOG_DEBUG, TAG_VCD, "[Server] @@@@");
655 int result_count = 0;
656 vc_cmd_list_get_count(vc_cmd_list, &result_count);
658 if (0 == result_count) {
660 if (NULL != all_result) {
661 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Engine result is no command : %s", all_result);
663 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Engine result is NULL");
665 bool temp = vcd_client_manager_get_exclusive();
666 vc_info_parser_set_result(all_result, event, msg, NULL, temp);
668 int pid = vcd_client_widget_get_foreground_pid();
670 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Request tooltip hide");
671 /* Send to hide tooltip */
672 vcdc_send_show_tooltip(pid, false);
675 if (-1 != vcd_client_manager_get_pid()) {
676 /* Manager client is available */
677 if (0 != vcdc_send_result_to_manager(vcd_client_manager_get_pid(), VC_RESULT_TYPE_NORMAL)) {
678 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Fail to send result");
682 vcd_client_manager_set_exclusive(false);
686 if (false == vcd_client_manager_get_exclusive()) {
687 int pid = vcd_client_widget_get_foreground_pid();
689 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Request tooltip hide");
690 vcdc_send_show_tooltip(pid, false);
693 vc_info_parser_set_result(all_result, event, msg, vc_cmd_list, false);
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_DEBUG, TAG_VCD, "[Server] Manager is NOT available. Send result to client directly");
702 /* Send result to client */
703 ecore_timer_add(0, __vcd_send_selected_result, NULL);
706 /* exclusive command */
707 vc_info_parser_set_result(all_result, event, msg, vc_cmd_list, true);
709 if (-1 != vcd_client_manager_get_pid()) {
710 /* Manager client is available */
711 if (0 != vcdc_send_result_to_manager(vcd_client_manager_get_pid(), VC_RESULT_TYPE_NORMAL)) {
712 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Fail to send result");
715 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Manager is NOT available");
718 vcd_client_manager_set_exclusive(false);
722 vc_cmd_list_destroy(vc_cmd_list, true);
728 if (NULL == result_id) {
730 if (NULL != all_result) {
731 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Engine result is no command : %s", all_result);
732 bool temp = vcd_client_manager_get_exclusive();
733 vc_info_parser_set_result(all_result, event, msg, NULL, temp);
736 int pid = vcd_client_widget_get_foreground_pid();
738 if (NULL != all_result) {
739 /* Send result text to widget */
740 vcdc_send_result(pid, vcd_client_manager_get_pid(), VC_COMMAND_TYPE_WIDGET);
743 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Request tooltip hide");
744 /* Send to hide tooltip */
745 vcdc_send_show_tooltip(pid, false);
748 if (-1 != vcd_client_manager_get_pid()) {
749 /* Manager client is available */
750 if (0 != vcdc_send_result_to_manager(vcd_client_manager_get_pid(), VC_RESULT_TYPE_NORMAL)) {
751 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Fail to send result");
755 vcd_client_manager_set_exclusive(false);
761 SLOG(LOG_DEBUG, TAG_VCD, "[Server] @ Get engine result @");
764 vc_cmd_s* temp_cmd = NULL;
765 vc_cmd_list_h vc_cmd_list = NULL;
767 if (0 != vc_cmd_list_create(&vc_cmd_list)) {
768 SLOG(LOG_ERROR, TAG_VCD, "[Server] Fail to create command list");
769 vcd_client_manager_set_exclusive(false);
770 vcd_config_set_service_state(VCD_STATE_READY);
771 vcdc_send_service_state(VCD_STATE_READY);
776 for (i = 0; i < count; i++) {
777 SLOG(LOG_DEBUG, TAG_VCD, "[Server] [%d] Result ID(%d)", i, result_id[i]);
779 if (result_id[i] < 0) {
780 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Result ID(%d) is NOT valid", result_id[i]);
784 ret = vcd_client_get_cmd_from_result_id(result_id[i], &temp_cmd);
785 if (0 == ret && NULL != temp_cmd) {
786 switch (temp_cmd->format) {
787 case VC_CMD_FORMAT_FIXED:
788 case VC_CMD_FORMAT_FIXED_AND_VFIXED:
789 case VC_CMD_FORMAT_VFIXED_AND_FIXED:
790 case VC_CMD_FORMAT_PARTIAL:
791 /* Nonfixed result is NOT valid */
793 case VC_CMD_FORMAT_FIXED_AND_NONFIXED:
794 if (NULL == temp_cmd->parameter) {
795 if (NULL != non_fixed_result) {
796 temp_cmd->parameter = strdup(non_fixed_result);
799 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Command type is NOT vaild. Parameter (%s)", temp_cmd->parameter);
802 case VC_CMD_FORMAT_NONFIXED_AND_FIXED:
803 if (NULL == temp_cmd->command) {
804 if (NULL != non_fixed_result) {
805 temp_cmd->command = strdup(non_fixed_result);
808 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Command type is NOT vaild. Command (%s)", temp_cmd->command);
813 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Unknown command type : %d", temp_cmd->type);
817 if (0 != vc_cmd_list_add(vc_cmd_list, (vc_cmd_h)temp_cmd)) {
818 SLOG(LOG_DEBUG, TAG_VCD, "Fail to add command to list");
819 vc_cmd_destroy((vc_cmd_h)temp_cmd);
822 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] NOT found matached result(%d)", result_id[i]);
826 vc_cmd_print_list(vc_cmd_list);
828 SLOG(LOG_DEBUG, TAG_VCD, "[Server] @@@@");
830 int result_count = 0;
831 vc_cmd_list_get_count(vc_cmd_list, &result_count);
833 if (false == vcd_client_manager_get_exclusive()) {
834 int pid = vcd_client_widget_get_foreground_pid();
836 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Request tooltip hide");
837 vcdc_send_show_tooltip(pid, false);
840 vc_info_parser_set_result(all_result, event, msg, vc_cmd_list, false);
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_DEBUG, TAG_VCD, "[Server] Manager is NOT available. Send result to client directly");
849 /* Send result to client */
850 ecore_timer_add(0, __vcd_send_selected_result, NULL);
853 /* exclusive command */
854 vc_info_parser_set_result(all_result, event, msg, vc_cmd_list, true);
856 if (-1 != vcd_client_manager_get_pid()) {
857 /* Manager client is available */
858 if (0 != vcdc_send_result_to_manager(vcd_client_manager_get_pid(), VC_RESULT_TYPE_NORMAL)) {
859 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Fail to send result");
862 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Manager is NOT available");
865 vcd_client_manager_set_exclusive(false);
868 vc_cmd_list_destroy(vc_cmd_list, true);
875 static void __vcd_server_nlu_result_cb(vcp_result_event_e event, const char* nlu_result, void *user_data)
877 SLOG(LOG_DEBUG, TAG_VCD, "[Server] NLU result cb - event(%d)", event);
878 SLOG(LOG_DEBUG, TAG_VCD, "[Server] result (%s)", nlu_result);
880 int ret = vc_info_parser_set_nlu_result(nlu_result);
882 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to set nlu result (%d)", ret);
889 static void __vcd_server_error_cb(vcp_error_e error, const char* msg, void *user_data)
891 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Engine Error cb - reason(%d), msg(%s)", error, msg);
892 ecore_main_loop_thread_safe_call_async(__cancel_by_error, NULL);
894 char* error_msg = NULL;
896 error_msg = strdup(msg);
899 if (0 != vcdc_send_error_signal_to_manager(vcd_client_manager_get_pid(), error, error_msg)) {
900 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to send error signal");
903 if (NULL != error_msg) {
912 * vcd server Interfaces
914 static void __vcd_file_clean_up()
916 SLOG(LOG_DEBUG, TAG_VCD, "== Old file clean up == ");
919 struct dirent *dirp = NULL;
921 dp = opendir(VC_RUNTIME_INFO_ROOT);
923 SLOG(LOG_ERROR, TAG_VCD, "[File message WARN] Fail to open path : %s", VC_RUNTIME_INFO_ROOT);
927 char remove_path[256] = {0, };
932 if (!strncmp("vc_", dirp->d_name, strlen("vc_"))) {
933 memset(remove_path, 0, 256);
934 snprintf(remove_path, 256, "%s/%s", VC_RUNTIME_INFO_ROOT, dirp->d_name);
937 if (0 != remove(remove_path)) {
938 SLOG(LOG_WARN, TAG_VCD, "[File message WARN] Fail to remove file : %s", remove_path);
940 SLOG(LOG_DEBUG, TAG_VCD, "[File message] Remove file : %s", remove_path);
944 } while (NULL != dirp);
951 static int __vcd_db_clean_up()
954 int cnt = VC_COMMAND_TYPE_FOREGROUND;
956 if (VC_COMMAND_TYPE_BACKGROUND != cnt)
957 ret = vc_db_delete_commands(-1, cnt, NULL);
958 } while (VC_COMMAND_TYPE_EXCLUSIVE >= ++cnt);
967 /* Remove old file */
968 __vcd_file_clean_up();
970 /* initialize modules */
971 ret = vcd_config_initialize(__config_lang_changed_cb, __config_foreground_changed_cb, NULL);
973 SLOG(LOG_ERROR, TAG_VCD, "[Server WARNING] Fail to initialize config.");
977 ret = __vcd_db_clean_up();
979 SLOG(LOG_ERROR, TAG_VCD, "[Server WARNING] Fail to remove db data");
982 vcd_config_set_service_state(VCD_STATE_NONE);
984 //ret = vcd_engine_agent_init(__vcd_server_pre_result_cb, __vcd_server_result_cb, __vcd_server_nlu_result_cb, __vcd_server_error_cb);
985 ret = vcd_engine_agent_init(__vcd_server_asr_result_cb, __vcd_server_result_cb, __vcd_server_nlg_result_cb, __vcd_server_error_cb);
987 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to engine agent initialize : result(%d)", ret);
991 if (0 != vcd_recorder_create(__server_recorder_callback, __server_recorder_interrupt_callback)) {
992 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to create recorder");
993 return VCD_ERROR_OPERATION_FAILED;
997 ret = vcd_engine_agent_initialize_current_engine();
999 if (VCD_ERROR_ENGINE_NOT_FOUND == ret)
1000 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] There is No Voice control engine");
1002 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to init engine");
1004 g_is_engine = false;
1010 if (0 != vcd_engine_agent_load_current_engine()) {
1011 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to load current engine");
1012 return VCD_ERROR_OPERATION_FAILED;
1015 /* Initialize manager info */
1016 vcd_client_manager_unset();
1018 vcd_config_set_service_state(VCD_STATE_READY);
1019 vcdc_send_service_state(VCD_STATE_READY);
1021 SLOG(LOG_DEBUG, TAG_VCD, "[Server SUCCESS] initialize");
1029 if (0 < g_list_length(g_proc_list)) {
1030 iter = g_list_first(g_proc_list);
1031 while (NULL != iter) {
1032 g_proc_list = g_list_remove_link(g_proc_list, iter);
1033 iter = g_list_first(g_proc_list);
1037 if (g_restart_timer != NULL) {
1038 ecore_timer_del(g_restart_timer);
1039 g_restart_timer = NULL;
1042 vcd_state_e state = vcd_config_get_service_state();
1043 if (VCD_STATE_READY != state) {
1044 if (VCD_STATE_RECORDING == state) {
1045 vcd_recorder_stop();
1047 vcd_engine_recognize_cancel();
1049 if (0 != vcd_recorder_destroy()) {
1050 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to destroy recorder");
1052 SLOG(LOG_DEBUG, TAG_VCD, "[Server] destroy recorder");
1055 if (0 != vcd_engine_agent_release()) {
1056 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to release engine");
1058 SLOG(LOG_DEBUG, TAG_VCD, "[Server] release engine");
1061 vcd_client_manager_unset_appid();
1063 vcd_config_set_service_state(VCD_STATE_NONE);
1064 vcdc_send_service_state(VCD_STATE_NONE);
1066 SLOG(LOG_DEBUG, TAG_VCD, "[Server] mode finalize");
1071 static Eina_Bool __finalize_quit_ecore_loop(void *data)
1073 SLOG(LOG_DEBUG, TAG_VCD, "[Server] quit ecore main loop");
1074 ecore_main_loop_quit();
1078 static void __read_proc()
1081 struct dirent *dirp = NULL;
1085 if (0 < g_list_length(g_proc_list)) {
1086 iter = g_list_first(g_proc_list);
1087 while (NULL != iter) {
1088 g_proc_list = g_list_remove_link(g_proc_list, iter);
1089 iter = g_list_first(g_proc_list);
1093 dp = opendir("/proc");
1095 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Fail to open proc");
1101 tmp = atoi(dirp->d_name);
1102 if (0 >= tmp) continue;
1103 g_proc_list = g_list_append(g_proc_list, GINT_TO_POINTER(tmp));
1105 } while (NULL != dirp);
1111 static void __vcd_cleanup_client(vcd_client_type_e type)
1113 int* client_list = NULL;
1114 int client_count = 0;
1121 if (VCD_CLIENT_TYPE_NORMAL == type) {
1122 ret = vcd_client_get_list(&client_list, &client_count);
1123 } else if (VCD_CLIENT_TYPE_WIDGET == type) {
1124 ret = vcd_client_widget_get_list(&client_list, &client_count);
1125 } else if (VCD_CLIENT_TYPE_MANAGER == type) {
1126 mgr_pid = vcd_client_manager_get_pid();
1127 client_list = &mgr_pid;
1129 if (-1 == mgr_pid) {
1130 SLOG(LOG_WARN, TAG_VCD, "[WARNING] Invalid Manager pid");
1135 if (0 == ret || mgr_pid > 0) {
1136 SLOG(LOG_DEBUG, TAG_VCD, "@@@ Clean up %s client ", type ? (type == 1) ? "Widget" : "Manager" : "Normal");
1137 if (NULL != client_list && client_count > 0) {
1138 for (i = 0; i < client_count; i++) {
1141 for (j = 0; j < g_list_length(g_proc_list); j++) {
1142 iter = g_list_nth(g_proc_list, j);
1144 if (*(client_list + i) == GPOINTER_TO_INT(iter->data)) {
1145 SLOG(LOG_DEBUG, TAG_VCD, "%s pid(%d) is running", type ? (type == 1) ? "Widget" : "Manager" : "Normal", *(client_list + i));
1152 if (false == exist) {
1153 SLOG(LOG_ERROR, TAG_VCD, "%s pid(%d) should be removed", type ? (type == 1) ? "Widget" : "Manager" : "Normal", *(client_list + i));
1154 if (VCD_CLIENT_TYPE_NORMAL == type)
1155 vcd_server_finalize(*(client_list + i));
1156 else if (VCD_CLIENT_TYPE_WIDGET == type)
1157 vcd_server_widget_finalize(*(client_list + i));
1159 vcd_server_mgr_finalize(mgr_pid);
1163 SLOG(LOG_DEBUG, TAG_VCD, "@@@");
1165 if (NULL != client_list && -1 == mgr_pid) {
1172 Eina_Bool vcd_cleanup_client_all(void *data)
1176 __vcd_cleanup_client(VCD_CLIENT_TYPE_NORMAL);
1177 __vcd_cleanup_client(VCD_CLIENT_TYPE_WIDGET);
1178 __vcd_cleanup_client(VCD_CLIENT_TYPE_MANAGER);
1181 if (0 == vcd_client_get_list(&client_list, &client_count)) {
1182 SLOG(LOG_DEBUG, TAG_VCD, "@@@ Clean up client ");
1183 if (NULL != client_list && client_count > 0) {
1184 for (i = 0; i < client_count; i++) {
1187 for (j = 0; j < g_list_length(g_proc_list); j++) {
1188 iter = g_list_nth(g_proc_list, j);
1190 if (client_list[i] == GPOINTER_TO_INT(iter->data)) {
1191 SLOG(LOG_DEBUG, TAG_VCD, "pid(%d) is running", client_list[i]);
1198 if (false == exist) {
1199 SLOG(LOG_ERROR, TAG_VCD, "pid(%d) should be removed", client_list[i]);
1200 vcd_server_finalize(client_list[i]);
1203 result = vcdc_send_hello(client_list[i], VCD_CLIENT_TYPE_NORMAL);
1206 SLOG(LOG_DEBUG, TAG_VCD, "[Server] pid(%d) should be removed.", client_list[i]);
1207 vcd_server_finalize(client_list[i]);
1208 } else if (-1 == result) {
1209 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Hello result has error");
1214 SLOG(LOG_DEBUG, TAG_VCD, "@@@");
1216 if (NULL != client_list) {
1221 /* If app is in background state, app cannot response message. */
1222 if (0 == vcd_client_widget_get_list(&client_list, &client_count)) {
1223 SLOG(LOG_DEBUG, TAG_VCD, "@@@ Clean up widget");
1224 if (NULL != client_list && client_count > 0) {
1225 for (i = 0; i < client_count; i++) {
1228 for (j = 0; j < g_list_length(g_proc_list); j++) {
1229 iter = g_list_nth(g_proc_list, j);
1231 if (client_list[i] == GPOINTER_TO_INT(iter->data)) {
1232 SLOG(LOG_DEBUG, TAG_VCD, "widget pid(%d) is running", client_list[i]);
1239 if (false == exist) {
1240 SLOG(LOG_ERROR, TAG_VCD, "widget pid(%d) should be removed", client_list[i]);
1241 vcd_server_widget_finalize(client_list[i]);
1244 result = vcdc_send_hello(client_list[i], VCD_CLIENT_TYPE_WIDGET);
1247 SLOG(LOG_DEBUG, TAG_VCD, "[Server] widget pid(%d) should be removed.", client_list[i]);
1248 vcd_server_widget_finalize(client_list[i]);
1249 } else if (-1 == result) {
1250 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Hello result has error");
1255 SLOG(LOG_DEBUG, TAG_VCD, "@@@");
1258 if (NULL != client_list) {
1266 int mgr_pid = vcd_client_manager_get_pid();
1268 for (j = 0; j < g_list_length(g_proc_list); j++) {
1269 iter = g_list_nth(g_proc_list, j);
1271 if (mgr_pid == GPOINTER_TO_INT(iter->data)) {
1272 SLOG(LOG_DEBUG, TAG_VCD, "manager pid(%d) is running", mgr_pid);
1279 if (false == exist) {
1280 SLOG(LOG_ERROR, TAG_VCD, "manager pid (%d) should be removed", mgr_pid);
1281 vcd_server_mgr_finalize(mgr_pid);
1288 int vcd_server_get_service_state()
1290 return vcd_config_get_service_state();
1293 int vcd_server_get_foreground()
1296 vcd_config_get_foreground(&pid);
1304 int vcd_server_mgr_initialize(int pid)
1306 if (false == g_is_engine) {
1307 if (0 != vcd_engine_agent_initialize_current_engine()) {
1308 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] No Engine");
1309 g_is_engine = false;
1310 return VCD_ERROR_ENGINE_NOT_FOUND;
1316 /* check if pid is valid */
1317 if (false == vcd_client_manager_is_valid(pid)) {
1318 SLOG(LOG_ERROR, TAG_VCD, "[Server] old manager pid(%d) be removed", vcd_client_manager_get_pid());
1319 vcd_server_mgr_cancel();
1320 vcd_client_manager_unset();
1323 /* Add client information to client manager */
1324 if (0 != vcd_client_manager_set(pid)) {
1325 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to add manager");
1326 return VCD_ERROR_OPERATION_FAILED;
1329 if (0 != vcdc_send_manager_pid(pid))
1330 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Fail to send manager pid");
1332 SLOG(LOG_DEBUG, TAG_VCD, "[Server Success] Manager initialize : pid(%d)", pid);
1334 return VCD_ERROR_NONE;
1337 int vcd_server_mgr_finalize(int pid)
1339 /* check if pid is valid */
1340 if (false == vcd_client_manager_is_valid(pid)) {
1341 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] The manager pid(%d) is NOT valid", pid);
1342 return VCD_ERROR_INVALID_PARAMETER;
1345 /* Cancel recognition */
1346 vcd_server_mgr_cancel();
1348 if (0 != vcdc_send_manager_pid(-1))
1349 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Fail to send manager pid");
1351 /* Remove manager information */
1352 if (0 != vcd_client_manager_unset()) {
1353 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Fail to delete client");
1356 if (0 == vcd_client_get_ref_count()) {
1357 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Connected client list is empty");
1358 ecore_timer_add(0, __finalize_quit_ecore_loop, NULL);
1361 SLOG(LOG_DEBUG, TAG_VCD, "[Server Success] Manager Finalize : pid(%d)", pid);
1363 return VCD_ERROR_NONE;
1366 int vcd_server_mgr_set_command(int pid)
1368 if (0 != vcd_client_manager_set_command(pid)) {
1369 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] The manager pid(%d) is NOT valid", pid);
1370 return VCD_ERROR_INVALID_PARAMETER;
1372 return VCD_ERROR_NONE;
1375 int vcd_server_mgr_unset_command(int pid)
1377 if (0 != vcd_client_manager_unset_command(pid)) {
1378 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] The manager pid(%d) is NOT valid", pid);
1379 return VCD_ERROR_INVALID_PARAMETER;
1381 return VCD_ERROR_NONE;
1384 int vcd_server_mgr_set_demandable_client(int pid)
1386 /* check if pid is valid */
1387 if (false == vcd_client_manager_is_valid(pid)) {
1388 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] The manager pid(%d) is NOT valid", pid);
1389 return VCD_ERROR_INVALID_PARAMETER;
1392 GSList* client_list = NULL;
1393 if (0 != vc_info_parser_get_demandable_clients(&client_list)) {
1394 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to get demandable client");
1395 return VCD_ERROR_OPERATION_FAILED;
1398 /* Save client list */
1399 if (0 != vcd_client_manager_set_demandable_client(pid, client_list)) {
1400 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to set demandable client list");
1401 return VCD_ERROR_OPERATION_FAILED;
1404 return VCD_ERROR_NONE;
1407 int vcd_server_mgr_set_audio_type(int pid, const char* audio_type)
1409 /* check if pid is valid */
1410 if (false == vcd_client_manager_is_valid(pid)) {
1411 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] The manager pid(%d) is NOT valid", pid);
1412 return VCD_ERROR_INVALID_PARAMETER;
1416 vcp_audio_type_e type = VCP_AUDIO_TYPE_PCM_S16_LE;
1420 ret = vcd_engine_get_audio_format(audio_type, &type, &rate, &channel);
1422 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to get audio format : %d", ret);
1423 return VCD_ERROR_OPERATION_FAILED;
1426 ret = vcd_recorder_set(audio_type, type, rate, channel);
1428 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to set audio in type : %d", ret);
1429 return VCD_ERROR_OPERATION_FAILED;
1432 return VCD_ERROR_NONE;
1435 int vcd_server_mgr_get_audio_type(int pid, char** audio_type)
1437 /* check if pid is valid */
1438 if (false == vcd_client_manager_is_valid(pid)) {
1439 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] The manager pid(%d) is NOT valid", pid);
1440 return VCD_ERROR_INVALID_PARAMETER;
1443 int ret = vcd_recorder_get(audio_type);
1445 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to get audio in type : %d", ret);
1446 return VCD_ERROR_OPERATION_FAILED;
1449 return VCD_ERROR_NONE;
1452 int vcd_server_mgr_set_client_info(int pid)
1454 /* check if pid is valid */
1455 if (false == vcd_client_manager_is_valid(pid)) {
1456 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] The manager pid(%d) is NOT valid", pid);
1457 return VCD_ERROR_INVALID_PARAMETER;
1460 int ret = vcd_client_save_client_info();
1462 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to save client info : %d", ret);
1463 return VCD_ERROR_OPERATION_FAILED;
1466 return VCD_ERROR_NONE;
1469 static int __start_internal_recognition()
1473 if (0 != vcd_client_command_collect_command()) {
1474 SLOG(LOG_ERROR, TAG_VCD, "[Client Data ERROR] Fail to collect command");
1475 /* Send error cb to manager */
1476 int pid = vcd_client_widget_get_foreground_pid();
1478 vcdc_send_error_signal_to_manager(vcd_client_manager_get_pid(), VCD_ERROR_OPERATION_FAILED, "voice_engine.error.proc_fail");
1479 return VCD_ERROR_OPERATION_FAILED;
1482 /* 3. Set command to engine */
1483 ret = vcd_engine_set_commands();
1485 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to set commands : %d", ret);
1486 /* Send error cb to manager */
1487 int pid = vcd_client_widget_get_foreground_pid();
1489 vcdc_send_error_signal_to_manager(vcd_client_manager_get_pid(), VCD_ERROR_OPERATION_FAILED, "voice_engine.error.proc_fail");
1490 return VCD_ERROR_OPERATION_FAILED;
1493 SLOG(LOG_DEBUG, TAG_VCD, "[Server Success] Set command");
1495 bool stop_by_silence = true;
1496 if (VCD_RECOGNITION_MODE_MANUAL == vcd_client_get_recognition_mode()) {
1497 stop_by_silence = false;
1500 vcd_client_manager_set_result_text(NULL);
1502 /* 4. start recognition */
1503 ret = vcd_engine_recognize_start(stop_by_silence);
1505 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to start recognition : result(%d)", ret);
1506 /* Send error cb to manager */
1507 int pid = vcd_client_widget_get_foreground_pid();
1509 vcdc_send_error_signal_to_manager(vcd_client_manager_get_pid(), VCD_ERROR_OPERATION_FAILED, "voice_engine.error.proc_fail");
1510 return VCD_ERROR_OPERATION_FAILED;
1513 SLOG(LOG_DEBUG, TAG_VCD, "[Server Success] Start engine");
1516 /* 5. recorder start */
1517 ret = vcd_recorder_start();
1519 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to start recorder : result(%d)", ret);
1520 vcd_engine_recognize_cancel();
1521 /* Send error cb to manager */
1522 int pid = vcd_client_widget_get_foreground_pid();
1524 vcdc_send_error_signal_to_manager(vcd_client_manager_get_pid(), VCD_ERROR_OPERATION_FAILED, "voice_engine.error.proc_fail");
1529 vcd_config_set_service_state(VCD_STATE_RECORDING);
1530 vcdc_send_service_state(VCD_STATE_RECORDING);
1532 SLOG(LOG_DEBUG, TAG_VCD, "[Server Success] Start recognition(%d)", stop_by_silence);
1537 static Eina_Bool __vcd_request_show_tooltip(void *data)
1539 int pid = vcd_client_widget_get_foreground_pid();
1541 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Request tooltip show and widget command");
1542 vcdc_send_show_tooltip(pid, (bool)data);
1548 int vcd_server_mgr_start(vcd_recognition_mode_e recognition_mode, bool exclusive_cmd, bool start_by_client)
1550 /* 1. check current state */
1551 vcd_state_e state = vcd_config_get_service_state();
1553 if (VCD_STATE_READY != state) {
1554 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Current state is not ready");
1555 return VCD_ERROR_INVALID_STATE;
1557 if (-1 == vcd_client_manager_get_pid()) {
1558 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Manager is NOT available.");
1559 return VCD_ERROR_OPERATION_FAILED;
1562 SLOG(LOG_DEBUG, TAG_VCD, "[Server] set recognition mode = %d", recognition_mode);
1563 vcd_client_set_recognition_mode(recognition_mode);
1565 if (false == exclusive_cmd) {
1566 vcd_client_update_foreground_pid();
1567 /* Notify show tooltip */
1568 if (1 == vcd_config_get_command_type_enabled(VC_COMMAND_TYPE_WIDGET)) {
1569 int pid = vcd_client_widget_get_foreground_pid();
1571 SLOG(LOG_INFO, TAG_VCD, "[Server] Request tooltip show and widget command");
1572 ecore_timer_add(0, __vcd_request_show_tooltip, (void*)true);
1577 vcd_client_manager_set_exclusive(exclusive_cmd);
1581 if (true == start_by_client) {
1582 /* Get foreground pid */
1583 if (0 != vcd_config_get_foreground(&fg_pid)) {
1584 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to get foreground");
1587 /* Set client exclusive option */
1588 if (0 != vcd_client_set_exclusive_command(fg_pid)) {
1589 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to set exclusive command");
1593 int ret = __start_internal_recognition();
1595 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to start recongition : %d", ret);
1599 if (true == start_by_client) {
1600 vcd_client_unset_exclusive_command(fg_pid);
1603 return VCD_ERROR_NONE;
1606 int vcd_server_mgr_stop()
1608 /* 1. Check current state is recording */
1609 if (VCD_STATE_RECORDING != vcd_config_get_service_state()) {
1610 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Current state is not recording");
1611 return VCD_ERROR_INVALID_STATE;
1613 if (-1 == vcd_client_manager_get_pid()) {
1614 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Manager is NOT available.");
1615 return VCD_ERROR_OPERATION_FAILED;
1619 /* 2. Stop recorder */
1620 vcd_recorder_stop();
1623 /* 3. Stop engine recognition */
1624 int ret = vcd_engine_recognize_stop();
1626 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to stop recognition : %d", ret);
1629 /* 4. Set original mode */
1630 vcd_config_set_service_state(VCD_STATE_PROCESSING);
1631 vcdc_send_service_state(VCD_STATE_PROCESSING);
1633 return VCD_ERROR_NONE;
1636 int vcd_server_mgr_cancel()
1638 if (-1 == vcd_client_manager_get_pid()) {
1639 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Manager is NOT available.");
1640 return VCD_ERROR_OPERATION_FAILED;
1643 if (g_restart_timer != NULL) {
1644 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Delete restart engine timer");
1645 ecore_timer_del(g_restart_timer);
1646 g_restart_timer = NULL;
1649 /* 1. Check current state */
1650 vcd_state_e state = vcd_config_get_service_state();
1651 if (VCD_STATE_READY == state) {
1652 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Current state is READY");
1653 vcd_recorder_stop();
1654 vcdc_send_service_state(VCD_STATE_READY);
1655 return VCD_ERROR_NONE;
1659 /* 2. Stop recorder */
1660 vcd_recorder_stop();
1663 /* 3. Cancel engine */
1664 int ret = vcd_engine_recognize_cancel();
1666 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Fail to cancel : %d", ret);
1669 if (false == vcd_client_manager_get_exclusive()) {
1670 if (1 == vcd_config_get_command_type_enabled(VC_COMMAND_TYPE_WIDGET)) {
1671 int pid = vcd_client_widget_get_foreground_pid();
1673 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Request tooltip hide");
1674 ecore_timer_add(0, __vcd_request_show_tooltip, (void*)false);
1678 vcd_client_manager_set_exclusive(false);
1682 vcd_config_set_service_state(VCD_STATE_READY);
1683 vcdc_send_service_state(VCD_STATE_READY);
1685 return VCD_ERROR_NONE;
1689 int vcd_server_mgr_result_select()
1691 __vcd_send_selected_result(NULL);
1693 return VCD_ERROR_NONE;
1696 int vcd_server_mgr_set_domain(int pid, const char* domain)
1698 /* check if pid is valid */
1699 if (false == vcd_client_manager_is_valid(pid)) {
1700 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] The manager pid(%d) is NOT valid", pid);
1701 return VCD_ERROR_INVALID_PARAMETER;
1704 vcd_state_e state = vcd_config_get_service_state();
1705 if (VCD_STATE_READY != state) {
1706 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Current state is not ready");
1707 return VCD_ERROR_INVALID_STATE;
1710 /* Set domain to engine */
1711 int ret = vcd_engine_set_domain(pid, domain);
1713 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to set domain : %d", ret);
1715 SLOG(LOG_DEBUG, TAG_VCD, "[Server SUCCESS] Set domain");
1721 int vcd_server_mgr_set_private_data(int pid, const char* key, const char* data)
1723 /* check if pid is valid */
1724 if (false == vcd_client_manager_is_valid(pid)) {
1725 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] The manager pid(%d) is NOT valid", pid);
1726 return VCD_ERROR_INVALID_PARAMETER;
1729 vcd_state_e state = vcd_config_get_service_state();
1730 if (VCD_STATE_READY != state) {
1731 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Current state is not ready");
1732 return VCD_ERROR_INVALID_STATE;
1735 /* Set private data to engine */
1736 int ret = vcd_engine_set_private_data(pid, key, data);
1738 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to set private data : %d", ret);
1740 SLOG(LOG_DEBUG, TAG_VCD, "[Server SUCCESS] Set private data");
1746 int vcd_server_mgr_get_private_data(int pid, const char* key, char** data)
1748 /* check if pid is valid */
1749 if (false == vcd_client_manager_is_valid(pid)) {
1750 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] The manager pid(%d) is NOT valid", pid);
1751 return VCD_ERROR_INVALID_PARAMETER;
1753 vcd_state_e state = vcd_config_get_service_state();
1754 if (VCD_STATE_READY != state) {
1755 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Current state is not ready");
1756 return VCD_ERROR_INVALID_STATE;
1759 /* Get private data to engine */
1760 int ret = vcd_engine_get_private_data(pid, key, data);
1762 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to get private data : %d", ret);
1764 SLOG(LOG_DEBUG, TAG_VCD, "[Server SUCCESS] Set private data");
1770 int vcd_server_mgr_do_action(int pid, int type, const char* action)
1774 /* check if pid is valid */
1775 if (false == vcd_client_manager_is_valid(pid)) {
1776 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] The manager pid(%d) is NOT valid", pid);
1777 return VCD_ERROR_INVALID_PARAMETER;
1780 vcd_state_e state = vcd_config_get_service_state();
1781 if (VCD_STATE_READY != state) {
1782 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Current state is not ready");
1783 return VCD_ERROR_INVALID_STATE;
1786 /* Reqeust do action to engine */
1787 if (VCD_SEND_EVENT_TYPE_TEXT == type)
1788 ret = vcd_engine_process_text(pid, action);
1789 else if (VCD_SEND_EVENT_TYPE_LIST_EVENT == type)
1790 ret = vcd_engine_process_list_event(pid, action);
1791 else if (VCD_SEND_EVENT_TYPE_HAPTIC_EVENT == type)
1792 ret = vcd_engine_process_haptic_event(pid, action);
1795 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to process do action : %d", ret);
1797 vcd_config_set_service_state(VCD_STATE_PROCESSING);
1798 vcdc_send_service_state(VCD_STATE_PROCESSING);
1799 SLOG(LOG_DEBUG, TAG_VCD, "[Server SUCCESS] Process do action");
1805 int vcd_server_mgr_enable_command_type(int pid, int cmd_type)
1809 /* check if pid is valid */
1810 if (false == vcd_client_manager_is_valid(pid)) {
1811 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] The manager pid(%d) is NOT valid", pid);
1812 return VCD_ERROR_INVALID_PARAMETER;
1815 vcd_state_e state = vcd_config_get_service_state();
1816 if (VCD_STATE_READY != state) {
1817 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Current state is not ready");
1818 return VCD_ERROR_INVALID_STATE;
1821 ret = vcd_config_enable_command_type(cmd_type);
1823 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to enable command type");
1825 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Enable command type(%d)", cmd_type);
1831 int vcd_server_mgr_disable_command_type(int pid, int cmd_type)
1835 /* check if pid is valid */
1836 if (false == vcd_client_manager_is_valid(pid)) {
1837 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] The manager pid(%d) is NOT valid", pid);
1838 return VCD_ERROR_INVALID_PARAMETER;
1841 vcd_state_e state = vcd_config_get_service_state();
1842 if (VCD_STATE_READY != state) {
1843 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Current state is not ready");
1844 return VCD_ERROR_INVALID_STATE;
1847 ret = vcd_config_disable_command_type(cmd_type);
1849 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to disable command type");
1851 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Disable command type(%d)", cmd_type);
1858 * VC Server Functions for Client
1860 int vcd_server_initialize(int pid)
1862 if (false == g_is_engine) {
1863 if (0 != vcd_engine_agent_initialize_current_engine()) {
1864 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] No Engine");
1865 g_is_engine = false;
1866 return VCD_ERROR_ENGINE_NOT_FOUND;
1872 if (false == vcd_engine_is_available_engine()) {
1873 if (0 != vcd_engine_agent_initialize_current_engine()) {
1874 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] No Engine");
1875 return VCD_ERROR_ENGINE_NOT_FOUND;
1879 /* check if pid is valid */
1880 if (true == vcd_client_is_available(pid)) {
1881 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] The pid is already exist");
1882 return VCD_ERROR_INVALID_PARAMETER;
1885 /* Add client information to client manager */
1886 if (0 != vcd_client_add(pid)) {
1887 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to add client info");
1888 return VCD_ERROR_OPERATION_FAILED;
1891 SLOG(LOG_DEBUG, TAG_VCD, "[Server Success] Client Initialize : pid(%d)", pid);
1893 return VCD_ERROR_NONE;
1896 int vcd_server_finalize(int pid)
1898 /* check if pid is valid */
1899 if (false == vcd_client_is_available(pid)) {
1900 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] pid is NOT valid ");
1901 return VCD_ERROR_INVALID_PARAMETER;
1904 /* Remove client information */
1905 if (0 != vcd_client_delete(pid)) {
1906 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Fail to delete client");
1909 if (0 == vcd_client_get_ref_count()) {
1910 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Connected client list is empty");
1911 ecore_timer_add(0, __finalize_quit_ecore_loop, NULL);
1914 SLOG(LOG_DEBUG, TAG_VCD, "[Server Success] Client Finalize : pid(%d)", pid);
1916 return VCD_ERROR_NONE;
1919 int vcd_server_set_command(int pid, vc_cmd_type_e cmd_type)
1921 /* check if pid is valid */
1922 if (false == vcd_client_is_available(pid)) {
1923 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] pid is NOT valid ");
1924 return VCD_ERROR_INVALID_PARAMETER;
1927 if (0 != vcd_client_set_command_type(pid, cmd_type)) {
1928 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to set command type : pid(%d), cmd_type(%d)", pid, cmd_type);
1929 return VCD_ERROR_OPERATION_FAILED;
1935 int vcd_server_unset_command(int pid, vc_cmd_type_e cmd_type)
1937 /* check if pid is valid */
1938 if (false == vcd_client_is_available(pid)) {
1939 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] pid is NOT valid ");
1940 return VCD_ERROR_INVALID_PARAMETER;
1943 if (0 != vcd_client_unset_command_type(pid, cmd_type)) {
1944 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to unset command type : pid(%d), cmd_type(%d)", pid, cmd_type);
1945 return VCD_ERROR_OPERATION_FAILED;
1951 int vcd_server_set_foreground(int pid, bool value)
1953 /* check if pid is valid */
1954 if (false == vcd_client_is_available(pid) && false == vcd_client_widget_is_available(pid)) {
1955 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] pid is NOT valid ");
1956 return VCD_ERROR_INVALID_PARAMETER;
1959 if (0 != vcd_config_set_foreground(pid, value)) {
1960 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to set foreground : pid(%d), value(%d)", pid, value);
1961 return VCD_ERROR_OPERATION_FAILED;
1967 static int __vcd_server_launch_manager_app()
1970 bool running = false;
1973 ret = vcd_client_manager_get_appid(&appid);
1974 if (0 != ret || NULL == appid) {
1975 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Fail to get manager appid");
1976 return VCD_ERROR_OPERATION_FAILED;
1978 ret = app_manager_is_running(appid, &running);
1980 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Fail to check running with appid(%s)", appid);
1983 return VCD_ERROR_OPERATION_FAILED;
1985 if (false == running) {
1986 int tmp_ret = __vcd_is_package_installed(appid);
1987 if (false == tmp_ret) {
1988 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] manager app is not installed, appid(%s)", appid);
1990 ret = __vcd_activate_app_by_appcontrol(appid);
1992 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Fail to activate app");
1997 SLOG(LOG_ERROR, TAG_VCD, "Launch vc manager app: appid(%s)", appid);
2000 ret = __vcd_resume_app(appid);
2002 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Fail to resume app");
2007 SLOG(LOG_ERROR, TAG_VCD, "Resume vc manager app: appid(%s)", appid);
2013 return VCD_ERROR_NONE;
2016 int vcd_server_dialog(int pid, const char* disp_text, const char* utt_text, int continuous)
2018 /* check if pid is valid */
2019 if (false == vcd_client_is_available(pid) && false == vcd_client_widget_is_available(pid)) {
2020 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] pid is NOT valid ");
2021 return VCD_ERROR_INVALID_PARAMETER;
2024 int ret = __vcd_server_launch_manager_app();
2026 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);
2030 ret = vcdc_send_dialog(vcd_client_manager_get_pid(), pid, disp_text, utt_text, continuous);
2032 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);
2039 int vcd_server_is_system_command_valid(int pid, int* is_sys_cmd_valid)
2041 /* check if pid is valid */
2042 if (false == vcd_client_is_available(pid) && false == vcd_client_widget_is_available(pid)) {
2043 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] pid is NOT valid ");
2044 return VCD_ERROR_INVALID_PARAMETER;
2047 /* check if system command is valid */
2048 if (true == vcd_client_manager_is_system_command_valid(vcd_client_manager_get_pid()))
2049 *is_sys_cmd_valid = true;
2051 *is_sys_cmd_valid = false;
2057 int vcd_server_set_exclusive_command(int pid, bool value)
2059 /* check if pid is valid */
2060 if (false == vcd_client_is_available(pid)) {
2061 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] pid is NOT valid ");
2062 return VCD_ERROR_INVALID_PARAMETER;
2065 if (true == value) {
2066 if (0 != vcd_client_set_exclusive_command(pid)) {
2067 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to set exclusive command : pid(%d)", pid);
2068 return VCD_ERROR_OPERATION_FAILED;
2071 if (0 != vcd_client_unset_exclusive_command(pid)) {
2072 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to unset exclusive command : pid(%d)", pid);
2073 return VCD_ERROR_OPERATION_FAILED;
2080 int vcd_server_request_start(int pid, bool stop_by_silence)
2082 /* check if pid is valid */
2083 if (false == vcd_client_is_available(pid)) {
2084 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] pid(%d) is NOT forground client", pid);
2085 return VCD_ERROR_INVALID_PARAMETER;
2089 /* Check current state */
2090 vcd_state_e state = vcd_config_get_service_state();
2092 /* Service state should be ready */
2093 if (VCD_STATE_READY != state) {
2094 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Current state is not Ready : pid(%d)", pid);
2095 return VCD_ERROR_INVALID_STATE;
2098 if (-1 != vcd_client_manager_get_pid()) {
2099 /* Check current pid is valid */
2100 if (false == vcd_client_manager_check_demandable_client(pid)) {
2101 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Current client is NOT available : pid(%d)", pid);
2102 return VCD_ERROR_INVALID_PARAMETER;
2106 ret = vcd_server_mgr_start(stop_by_silence, false);
2108 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Fail to start recognition");
2109 return VCD_ERROR_INVALID_PARAMETER;
2115 int vcd_server_request_stop(int pid)
2118 /* Check current state */
2119 vcd_state_e state = vcd_config_get_service_state();
2121 /* Service state should be ready */
2122 if (VCD_STATE_RECORDING != state) {
2123 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Current state is not Recording : pid(%d)", pid);
2124 return VCD_ERROR_INVALID_STATE;
2127 if (-1 != vcd_client_manager_get_pid()) {
2128 /* Check current pid is valid */
2129 if (false == vcd_client_manager_check_demandable_client(pid)) {
2130 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Current client is NOT available : pid(%d)", pid);
2131 return VCD_ERROR_INVALID_PARAMETER;
2135 ret = vcd_server_mgr_stop();
2137 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Fail to start recognition");
2138 return VCD_ERROR_OPERATION_FAILED;
2141 return VCD_ERROR_NONE;
2144 int vcd_server_request_cancel(int pid)
2147 /* Check current state */
2148 vcd_state_e state = vcd_config_get_service_state();
2150 /* Service state should be recording or processing */
2151 if (VCD_STATE_RECORDING != state && VCD_STATE_PROCESSING != state) {
2152 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Current state is not Recording or Processing : pid(%d)", pid);
2153 return VCD_ERROR_INVALID_STATE;
2156 if (-1 != vcd_client_manager_get_pid()) {
2157 /* Check current pid is valid */
2158 if (false == vcd_client_manager_check_demandable_client(pid)) {
2159 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Current client is NOT available : pid(%d)", pid);
2160 return VCD_ERROR_INVALID_PARAMETER;
2164 ret = vcd_server_mgr_cancel();
2166 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Fail to start recognition");
2167 return VCD_ERROR_OPERATION_FAILED;
2170 return VCD_ERROR_NONE;
2175 * VC Server Functions for Widget lib
2177 int vcd_server_widget_initialize(int pid)
2179 if (false == g_is_engine) {
2180 if (0 != vcd_engine_agent_initialize_current_engine()) {
2181 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] No Engine");
2182 g_is_engine = false;
2183 return VCD_ERROR_ENGINE_NOT_FOUND;
2189 if (false == vcd_engine_is_available_engine()) {
2190 if (0 != vcd_engine_agent_initialize_current_engine()) {
2191 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] No Engine");
2192 return VCD_ERROR_ENGINE_NOT_FOUND;
2196 /* check if pid is valid */
2197 if (true == vcd_client_widget_is_available(pid)) {
2198 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] The pid is already exist");
2199 return VCD_ERROR_NONE;
2202 /* Add client information to client manager */
2203 if (0 != vcd_client_widget_add(pid)) {
2204 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to add client info");
2205 return VCD_ERROR_OPERATION_FAILED;
2208 SLOG(LOG_DEBUG, TAG_VCD, "[Server Success] Initialize widget : pid(%d)", pid);
2210 return VCD_ERROR_NONE;
2213 int vcd_server_widget_finalize(int pid)
2215 /* check if pid is valid */
2216 if (false == vcd_client_widget_is_available(pid)) {
2217 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] pid is NOT valid ");
2218 return VCD_ERROR_INVALID_PARAMETER;
2221 /* Remove client information */
2222 if (0 != vcd_client_widget_delete(pid)) {
2223 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Fail to delete client");
2226 if (0 == vcd_client_get_ref_count()) {
2227 SLOG(LOG_DEBUG, TAG_VCD, "[Server] connected client list is empty");
2228 ecore_timer_add(0, __finalize_quit_ecore_loop, NULL);
2231 return VCD_ERROR_NONE;
2234 int vcd_server_widget_start_recording(int pid, bool widget_command)
2236 /* check if pid is valid */
2237 if (false == vcd_client_widget_is_available(pid)) {
2238 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] pid is NOT valid ");
2239 return VCD_ERROR_INVALID_PARAMETER;
2242 if (true == widget_command) {
2243 vcd_client_widget_set_command(pid);
2244 SLOG(LOG_DEBUG, TAG_VCD, "[Server] widget command is available");
2246 vcd_client_widget_unset_command(pid);
2247 SLOG(LOG_WARN, TAG_VCD, "[Server] widget command is NOT available");
2250 int ret = __start_internal_recognition();
2252 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to start recongition : %d", ret);
2253 ecore_timer_add(0, __vcd_request_show_tooltip, (void*)false);
2259 int vcd_server_widget_start(int pid, bool stop_by_silence)
2261 /* check if pid is valid */
2262 int fore_pid = vcd_client_widget_get_foreground_pid();
2263 if (pid != fore_pid) {
2264 SLOG(LOG_ERROR, TAG_VCD, "[Server] pid is NOT foreground");
2265 return VCD_ERROR_INVALID_PARAMETER;
2268 /* Check current state */
2269 vcd_state_e state = vcd_config_get_service_state();
2271 /* Service state should be ready */
2272 if (VCD_STATE_READY != state) {
2273 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Current state is not Ready : pid(%d)", pid);
2274 return VCD_ERROR_INVALID_STATE;
2277 vcd_client_set_slience_detection(stop_by_silence);
2279 /* Notify show tooltip */
2280 ecore_timer_add(0, __vcd_request_show_tooltip, (void*)true);
2285 int vcd_server_widget_stop(int pid)
2287 /* check if pid is valid */
2288 int fore_pid = vcd_client_widget_get_foreground_pid();
2289 if (pid != fore_pid) {
2290 SLOG(LOG_ERROR, TAG_VCD, "[Server] pid is NOT foreground");
2291 return VCD_ERROR_INVALID_PARAMETER;
2295 /* Check current state */
2296 vcd_state_e state = vcd_config_get_service_state();
2298 /* Service state should be recording */
2299 if (VCD_STATE_RECORDING != state) {
2300 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Current service state is not Recording : pid(%d)", pid);
2301 return VCD_ERROR_INVALID_STATE;
2304 ret = vcd_server_mgr_stop();
2306 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Fail to start recognition");
2307 return VCD_ERROR_OPERATION_FAILED;
2310 return VCD_ERROR_NONE;
2313 int vcd_server_widget_cancel(int pid)
2315 /* check if pid is valid */
2316 int fore_pid = vcd_client_widget_get_foreground_pid();
2317 if (pid != fore_pid) {
2318 SLOG(LOG_ERROR, TAG_VCD, "[Server] pid is NOT foreground");
2319 return VCD_ERROR_INVALID_PARAMETER;
2323 /* Check current state */
2324 vcd_state_e state = vcd_config_get_service_state();
2326 /* Service state should be recording or processing */
2327 if (VCD_STATE_RECORDING != state && VCD_STATE_PROCESSING != state) {
2328 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Current state is not Recording or Processing : pid(%d)", pid);
2329 return VCD_ERROR_INVALID_STATE;
2332 ret = vcd_server_mgr_cancel();
2334 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Fail to cancel recognition : %d", ret);
2338 return VCD_ERROR_NONE;
2341 int vcd_server_widget_enable_asr_result(int pid, bool enable)
2344 ret = vcd_client_widget_set_asr_result_enabled(pid, enable);
2346 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Fail to enable asr result : %d", ret);
2352 int vcd_server_set_language(const char* language)
2354 int ret = VCD_ERROR_NONE;
2356 ret = vcd_config_set_default_language(language);
2358 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Fail to set language : %d", ret);
2362 ret = vcd_engine_set_current_language(language);
2364 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Fail to set language : %d", ret);