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;
44 static Ecore_Timer *g_check_widget_client_timer = NULL;
47 * @brief Enumerations of send event type.
50 VCD_SEND_EVENT_TYPE_TEXT, /**< send text event to vc engine*/
51 VCD_SEND_EVENT_TYPE_LIST_EVENT, /**< send list event to vc engine */
52 VCD_SEND_EVENT_TYPE_HAPTIC_EVENT /**< send haptic event to vc engine */
53 } vcd_send_event_type_e;
55 static int __vcd_server_launch_manager_app();
58 * VC Server Internal Functions
60 static Eina_Bool __stop_by_silence(void *data)
62 SLOG(LOG_DEBUG, TAG_VCD, "@@@ Silence Detected ");
64 vcd_server_mgr_stop();
66 SLOG(LOG_DEBUG, TAG_VCD, "@@@");
70 static Eina_Bool __cancel_by_interrupt(void *data)
72 SLOG(LOG_DEBUG, TAG_VCD, "@@@ Cancel by interrupt");
74 vcd_server_mgr_cancel();
76 SLOG(LOG_DEBUG, TAG_VCD, "@@@");
80 static void __cancel_by_error(void *data)
82 SLOG(LOG_DEBUG, TAG_VCD, "@@@ Cancel by error");
84 vcd_server_mgr_cancel();
86 SLOG(LOG_DEBUG, TAG_VCD, "@@@");
90 static Eina_Bool __restart_engine(void *data)
92 SLOG(LOG_DEBUG, TAG_VCD, "@@@ Restart by no result");
94 g_restart_timer = NULL;
96 /* Restart recognition */
97 int ret = vcd_engine_recognize_start(true);
99 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to restart recognition : result(%d)", ret);
103 SLOG(LOG_DEBUG, TAG_VCD, "[Server Success] Start engine");
105 if (VCD_RECOGNITION_MODE_RESTART_AFTER_REJECT == vcd_client_get_recognition_mode()) {
106 vcd_config_set_service_state(VCD_STATE_RECORDING);
107 vcdc_send_service_state(VCD_STATE_RECORDING);
110 SLOG(LOG_DEBUG, TAG_VCD, "[Server Success] Restart recognition");
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, "@@@");
185 static void __config_lang_changed_cb(const char* current_lang, void* user_data)
187 SLOG(LOG_DEBUG, TAG_VCD, "@@@ Change language ");
189 /* Current state is recording */
190 vcd_state_e state = vcd_config_get_service_state();
191 if (VCD_STATE_RECORDING == state || VCD_STATE_PROCESSING == state) {
192 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Current state is 'Recording'. Cancel recognition");
193 vcd_server_mgr_cancel();
197 ret = vcd_engine_set_current_language(current_lang);
199 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to set language of engine : %d", ret);
202 SLOG(LOG_DEBUG, TAG_VCD, "@@@");
207 static void __config_foreground_changed_cb(int previous, int current, void* user_data)
209 SLOG(LOG_DEBUG, TAG_VCD, "@@@ Change foreground");
211 SLOG(LOG_DEBUG, TAG_VCD, "Foreground pid(%d)", current);
213 if (VC_NO_FOREGROUND_PID != current) {
214 /* Foreground app is changed */
215 vcd_state_e state = vcd_config_get_service_state();
216 if (VCD_STATE_RECORDING == state) {
217 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Foreground pid(%d) is changed. Cancel recognition", current);
218 ecore_timer_add(0, __cancel_by_interrupt, NULL);
222 SLOG(LOG_DEBUG, TAG_VCD, "@@@");
227 static int __vcd_activate_app_by_appcontrol(const char* appid)
230 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Invalid parameter");
231 return VCD_ERROR_INVALID_PARAMETER;
235 app_control_h app_control = NULL;
236 ret = app_control_create(&app_control);
237 if (APP_CONTROL_ERROR_NONE == ret) {
239 ret = app_control_add_extra_data(app_control, "voice_launch", "get_result");
240 if (APP_CONTROL_ERROR_NONE != ret) {
241 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Fail to add extra data, ret(%d)", ret);
242 app_control_destroy(app_control);
243 return VCD_ERROR_OPERATION_FAILED;
246 ret = app_control_set_app_id(app_control, appid);
247 if (APP_CONTROL_ERROR_NONE != ret) {
248 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Fail to set app id, ret(%d)", ret);
249 app_control_destroy(app_control);
250 return VCD_ERROR_OPERATION_FAILED;
252 // Sent launch request
253 ret = app_control_send_launch_request(app_control, NULL, NULL);
254 if (APP_CONTROL_ERROR_NONE != ret) {
255 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Fail to send launch request, ret(%d)", ret);
256 app_control_destroy(app_control);
257 return VCD_ERROR_OPERATION_FAILED;
259 // Destroy app control
260 ret = app_control_destroy(app_control);
261 if (APP_CONTROL_ERROR_NONE != ret) {
262 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Fail to destroy, ret(%d)", ret);
263 return VCD_ERROR_OPERATION_FAILED;
266 return VCD_ERROR_NONE;
269 static int __vcd_resume_app(const char* appid)
271 app_context_h app_context = NULL;
272 int ret = app_manager_get_app_context(appid, &app_context);
273 if (APP_MANAGER_ERROR_NONE != ret) {
274 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Fail to get app_context, ret(%d), appid(%s)", ret, appid);
275 return VCD_ERROR_OPERATION_FAILED;
278 ret = app_manager_resume_app(app_context);
279 if (APP_MANAGER_ERROR_NONE != ret) {
280 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Fail to resume app, ret(%d), appid(%s)", ret, appid);
281 return VCD_ERROR_OPERATION_FAILED;
283 return VCD_ERROR_NONE;
286 static bool __vcd_is_package_installed(const char* appid)
288 app_info_h app_info = NULL;
289 int ret = app_manager_get_app_info(appid, &app_info);
290 if (APP_MANAGER_ERROR_NONE != ret || NULL == app_info)
292 ret = app_info_destroy(app_info);
293 if (APP_MANAGER_ERROR_NONE != ret)
294 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Fail to destroy app_info, ret(%d)", ret);
298 static bool __vcd_launch_app(const char* result)
300 if (NULL == result) {
301 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Invalid parameter");
302 return VCD_ERROR_INVALID_PARAMETER;
305 GSList* app_list = NULL;
306 if (0 != vc_db_get_appid_list(result, &app_list)) {
307 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] result text is NULL");
308 return VCD_ERROR_INVALID_PARAMETER;
311 if (0 != g_slist_length(app_list)) {
314 vc_deactivated_app_s* temp_app = NULL;
315 iter = g_slist_nth(app_list, 0);
317 while (NULL != iter) {
318 temp_app = iter->data;
320 if (NULL != temp_app) {
321 if (NULL != temp_app->appid) {
323 bool running = false;
324 ret = app_manager_is_running(temp_app->appid, &running);
325 if (APP_MANAGER_ERROR_NONE != ret) {
326 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Fail to check running with appid(%s)", temp_app->appid);
327 free(temp_app->appid);
328 temp_app->appid = NULL;
331 return VCD_ERROR_OPERATION_FAILED;
333 if (false == running) {
334 int tmp_ret = __vcd_is_package_installed(temp_app->appid);
335 if (false == tmp_ret) {
336 SLOG(LOG_WARN, TAG_VCD, "[WARNING] app is not installed, appid(%s)", temp_app->appid);
338 ret = __vcd_activate_app_by_appcontrol(temp_app->appid);
340 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Fail to activate app");
341 free(temp_app->appid);
342 temp_app->appid = NULL;
347 SLOG(LOG_ERROR, TAG_VCD, "Launch app: appid(%s) result(%s)", temp_app->appid, result);
350 ret = __vcd_resume_app(temp_app->appid);
352 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Fail to resume app");
353 free(temp_app->appid);
354 temp_app->appid = NULL;
359 SLOG(LOG_ERROR, TAG_VCD, "Resume app: appid(%s) result(%s)", temp_app->appid, result);
361 free(temp_app->appid);
362 temp_app->appid = NULL;
367 iter = g_slist_next(iter);
372 return VCD_ERROR_NONE;
375 static Eina_Bool __vcd_send_selected_result(void *data)
377 GSList* pid_list = NULL;
378 const char* result = vcd_client_manager_get_result_text();
380 if (0 != vc_info_parser_get_result_pid_list(&pid_list, result)) {
381 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Fail to get pid list. No result");
383 if (0 < g_slist_length(pid_list)) {
385 vc_cmd_s* temp_cmd = NULL;
390 iter = g_slist_nth(pid_list, 0);
391 while (NULL != iter) {
392 temp_cmd = iter->data;
394 if (NULL != temp_cmd && (pre_pid != temp_cmd->pid || pre_type == VC_COMMAND_TYPE_WIDGET || temp_cmd->type == VC_COMMAND_TYPE_WIDGET)) {
395 /* Launch deactivated several apps that is matched with result */
396 ret = __vcd_launch_app(result);
398 SLOG(LOG_ERROR, TAG_VCD, "Fail to launch or resume app, ret(%d) result(%s)", ret, result);
400 /* send result noti */
401 ret = vcdc_send_result(temp_cmd->pid, vcd_client_manager_get_pid(), temp_cmd->type);
403 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Fail to send result, ret(%d)", ret);
406 SLOG(LOG_ERROR, TAG_VCD, "[Server] Send result : pid(%d) type(%d)", temp_cmd->pid, temp_cmd->type);
407 pre_pid = temp_cmd->pid;
408 pre_type = temp_cmd->type;
414 pid_list = g_slist_remove_link(pid_list, iter);
415 iter = g_slist_nth(pid_list, 0);
420 if (VCD_RECOGNITION_MODE_RESTART_CONTINUOUSLY != vcd_client_get_recognition_mode()) {
421 vcd_config_set_service_state(VCD_STATE_READY);
422 vcdc_send_service_state(VCD_STATE_READY);
428 //static void __vcd_server_pre_result_cb(vcp_pre_result_event_e event, const char* pre_result, void *user_data)
429 static void __vcd_server_asr_result_cb(vcp_asr_result_event_e event, const char* asr_result, void *user_data)
431 if (NULL != asr_result) {
432 SECURE_SLOG(LOG_DEBUG, TAG_VCD, "[Server] ASR result - Event(%d), Text(%s)", event, asr_result);
433 vcdc_send_pre_result_to_manager(vcd_client_manager_get_pid(), event, asr_result);
439 static void __vcd_server_nlg_result_cb(const char* nlg_result, void *user_data)
441 int ret = __vcd_server_launch_manager_app();
443 SECURE_SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to send dialog : mgr_pid(%d), nlg_result(%s)", vcd_client_manager_get_pid(), nlg_result);
447 SECURE_SLOG(LOG_ERROR, TAG_VCD, "[Server] send dialog : mgr_pid(%d), nlg_result(%s)", vcd_client_manager_get_pid(), nlg_result);
448 ret = vcdc_send_dialog(vcd_client_manager_get_pid(), -1, nlg_result, NULL, 0); //0: VC_DIALOG_END
450 SECURE_SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to send dialog : mgr_pid(%d), nlg_result(%s)", vcd_client_manager_get_pid(), nlg_result);
454 static void* __recorder_stop(void *data)
460 static void __vcd_server_result_cb(vcp_result_event_e event, int* result_id, int count, const char* all_result,
461 const char* non_fixed_result, const char* nlu_result, const char* msg, int* user_info, void *user_data)
463 vc_info_parser_unset_result(vcd_client_manager_get_exclusive());
464 vcd_client_manager_set_result_text(all_result);
466 SECURE_SLOG(LOG_ERROR, TAG_VCD, "[Server] Event(%d), Text(%s) Nonfixed(%s) Msg(%s) Result count(%d)",
467 event, all_result, non_fixed_result, msg, count);
469 SECURE_SLOG(LOG_ERROR, TAG_VCD, "[Server] NLU result(%s)", nlu_result);
471 if (VCD_RECOGNITION_MODE_RESTART_AFTER_REJECT == vcd_client_get_recognition_mode()) {
472 if (VCP_RESULT_EVENT_REJECTED == event) {
473 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Restart by no or rejected result");
474 /* If no result and restart option is ON */
475 /* Send reject message */
476 bool temp = vcd_client_manager_get_exclusive();
477 vc_info_parser_set_result(all_result, event, msg, NULL, temp);
478 if (0 != vcdc_send_result_to_manager(vcd_client_manager_get_pid(), VC_RESULT_TYPE_NOTIFICATION)) {
479 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Fail to send result");
482 g_restart_timer = ecore_timer_add(0, __restart_engine, NULL);
485 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Stop recorder due to success");
486 //vcd_recorder_stop();
487 ecore_main_loop_thread_safe_call_sync(__recorder_stop, NULL);
488 } else if (VCD_RECOGNITION_MODE_RESTART_CONTINUOUSLY == vcd_client_get_recognition_mode()) {
489 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Restart continuously");
490 /* Restart option is ON */
491 g_restart_timer = ecore_timer_add(0, __restart_engine, NULL);
492 if (VCP_RESULT_EVENT_REJECTED == event) {
493 bool temp = vcd_client_manager_get_exclusive();
494 vc_info_parser_set_result(all_result, event, msg, NULL, temp);
495 if (0 != vcdc_send_result_to_manager(vcd_client_manager_get_pid(), VC_RESULT_TYPE_NOTIFICATION)) {
496 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Fail to send result");
502 /* if nlu_result is exist, Add command handle(is_action) into result list */
504 SLOG(LOG_DEBUG, TAG_VCD, "[Server] @ Get engine result @");
507 vc_cmd_s* temp_cmd = NULL;
508 vc_cmd_list_h vc_cmd_list = NULL;
510 if (0 != vc_cmd_list_create(&vc_cmd_list)) {
511 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Fail to create command list");
512 vcd_client_manager_set_exclusive(false);
513 vcd_config_set_service_state(VCD_STATE_READY);
514 vcdc_send_service_state(VCD_STATE_READY);
518 /* priority filter */
519 /* system > exclusive > widget > foreground > system_background > background */
521 int* filtered_id = (int*)calloc(count, sizeof(int));
523 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to allocate memory");
526 int filtered_count = 0;
527 int top_priority = VC_COMMAND_PRIORITY_BACKGROUND;
528 for (i = 0; i < count; i++) {
529 SLOG(LOG_INFO, TAG_VCD, "[Server] [%d] Result id(%d)", i, result_id[i]);
531 if (0 > result_id[i]) {
532 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Result ID(%d) is NOT valid", result_id[i]);
536 ret = vcd_client_get_cmd_from_result_id(result_id[i], &temp_cmd);
537 if (0 == ret && NULL != temp_cmd) {
538 if (top_priority == temp_cmd->priority) {
539 filtered_id[filtered_count] = result_id[i];
541 } else if (top_priority > temp_cmd->priority) {
542 filtered_id[0] = result_id[i];
544 top_priority = temp_cmd->priority;
547 vc_cmd_destroy((vc_cmd_h)temp_cmd);
553 if (top_priority >= VC_COMMAND_PRIORITY_WIDGET) {
554 int pid = vcd_client_widget_get_foreground_pid();
556 if (NULL != all_result) {
557 vc_info_parser_set_result(all_result, event, msg, NULL, false);
559 vcd_client_widget_get_asr_result_enabled(pid, &enable);
560 if (true == enable) {
561 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Send ASR result to Widget client");
562 bool is_consumed = false;
563 if (NULL != user_info) {
566 if (0 != vcdc_send_asr_result(pid, event, all_result, VC_COMMAND_TYPE_WIDGET, &is_consumed)) {
567 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to send asr result");
569 SLOG(LOG_DEBUG, TAG_VCD, "[Server] ASR result is consumed(%d)", is_consumed);
570 if (true == is_consumed) {
571 if (NULL != user_info) {
573 SLOG(LOG_ERROR, TAG_VCD, "[Server] Send whether ASR result is consumed or not (%d)", *user_info);
575 vcdc_send_show_tooltip(pid, false);
576 if (-1 != vcd_client_manager_get_pid()) {
577 /* Manager client is available */
578 vc_info_parser_unset_result(false);
579 vc_info_parser_set_result(all_result, VC_RESULT_EVENT_RESULT_SUCCESS, msg, NULL, false);
580 if (0 != vcdc_send_result_to_manager(vcd_client_manager_get_pid(), VC_RESULT_TYPE_NOTIFICATION)) {
581 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Fail to send result");
585 vcd_client_manager_set_exclusive(false);
587 vcd_config_set_service_state(VCD_STATE_READY);
588 vcdc_send_service_state(VCD_STATE_READY);
589 vc_cmd_list_destroy(vc_cmd_list, true);
590 if (NULL != filtered_id) {
603 for (i = 0; i < filtered_count; i++) {
604 SLOG(LOG_INFO, TAG_VCD, "[Server] [%d] Filtered Result id(%d)", i, filtered_id[i]);
606 if (filtered_id[i] < 0) {
607 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Filtered ID(%d) is NOT valid", filtered_id[i]);
611 ret = vcd_client_get_cmd_from_result_id(filtered_id[i], &temp_cmd);
612 if (0 == ret && NULL != temp_cmd) {
613 switch (temp_cmd->format) {
614 case VC_CMD_FORMAT_FIXED:
615 case VC_CMD_FORMAT_FIXED_AND_VFIXED:
616 case VC_CMD_FORMAT_VFIXED_AND_FIXED:
617 case VC_CMD_FORMAT_PARTIAL:
618 case VC_CMD_FORMAT_FIXED_AND_NONFIXED:
619 case VC_CMD_FORMAT_NONFIXED_AND_FIXED:
621 case VC_CMD_FORMAT_ACTION:
625 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Unknown command type : %d", temp_cmd->type);
629 if (0 != vc_cmd_list_add(vc_cmd_list, (vc_cmd_h)temp_cmd)) {
630 SLOG(LOG_DEBUG, TAG_VCD, "Fail to add command to list");
631 vc_cmd_destroy((vc_cmd_h)temp_cmd);
635 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] NOT found matached result(%d)", filtered_id[i]);
639 if (NULL != filtered_id) {
644 if (NULL != nlu_result) {
645 SECURE_SLOG(LOG_INFO, TAG_VCD, "[Server] NLU (%s)", nlu_result);
646 vc_info_parser_set_nlu_result(nlu_result);
647 if (0 == is_action) {
649 if (0 != vc_cmd_create(&nlu_cmd)) {
650 SLOG(LOG_ERROR, TAG_VCD, "Fail to nlu cmd create");
652 if (0 != vc_cmd_set_type(nlu_cmd, VC_COMMAND_TYPE_SYSTEM)) {
653 SLOG(LOG_ERROR, TAG_VCD, "Fail to set type");
655 if (0 != vc_cmd_set_pid(nlu_cmd, vcd_client_manager_get_pid())) {
656 SLOG(LOG_ERROR, TAG_VCD, "Fail to set pid");
658 if (0 != vc_cmd_set_format(nlu_cmd, VC_CMD_FORMAT_ACTION)) {
659 SLOG(LOG_ERROR, TAG_VCD, "Fail to set format");
661 if (0 != vc_cmd_list_add(vc_cmd_list, nlu_cmd)) {
662 SLOG(LOG_ERROR, TAG_VCD, "Fail to add nlu cmd to list");
663 vc_cmd_destroy(nlu_cmd);
669 vc_cmd_print_list(vc_cmd_list);
671 SLOG(LOG_DEBUG, TAG_VCD, "[Server] @@@@");
673 int result_count = 0;
674 vc_cmd_list_get_count(vc_cmd_list, &result_count);
676 if (0 == result_count) {
678 if (NULL != all_result) {
679 SECURE_SLOG(LOG_DEBUG, TAG_VCD, "[Server] Engine result is no command : %s", all_result);
681 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Engine result is NULL");
683 bool temp = vcd_client_manager_get_exclusive();
684 vc_info_parser_set_result(all_result, event, msg, NULL, temp);
686 int pid = vcd_client_widget_get_foreground_pid();
688 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Request tooltip hide");
689 /* Send to hide tooltip */
690 vcdc_send_show_tooltip(pid, false);
693 if (-1 != vcd_client_manager_get_pid()) {
694 /* Manager client is available */
695 if (0 != vcdc_send_result_to_manager(vcd_client_manager_get_pid(), VC_RESULT_TYPE_NORMAL)) {
696 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Fail to send result");
700 vcd_client_manager_set_exclusive(false);
704 if (false == vcd_client_manager_get_exclusive()) {
705 int pid = vcd_client_widget_get_foreground_pid();
707 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Request tooltip hide");
708 vcdc_send_show_tooltip(pid, false);
711 vc_info_parser_set_result(all_result, event, msg, vc_cmd_list, false);
713 if (-1 != vcd_client_manager_get_pid()) {
714 /* Manager client is available */
715 if (0 != vcdc_send_result_to_manager(vcd_client_manager_get_pid(), VC_RESULT_TYPE_NORMAL)) {
716 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Fail to send result");
719 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Manager is NOT available. Send result to client directly");
720 /* Send result to client */
721 ecore_timer_add(0, __vcd_send_selected_result, NULL);
724 /* exclusive command */
725 vc_info_parser_set_result(all_result, event, msg, vc_cmd_list, true);
727 if (-1 != vcd_client_manager_get_pid()) {
728 /* Manager client is available */
729 if (0 != vcdc_send_result_to_manager(vcd_client_manager_get_pid(), VC_RESULT_TYPE_NORMAL)) {
730 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Fail to send result");
733 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Manager is NOT available");
736 vcd_client_manager_set_exclusive(false);
740 vc_cmd_list_destroy(vc_cmd_list, true);
746 if (NULL == result_id) {
748 if (NULL != all_result) {
749 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Engine result is no command : %s", all_result);
750 bool temp = vcd_client_manager_get_exclusive();
751 vc_info_parser_set_result(all_result, event, msg, NULL, temp);
754 int pid = vcd_client_widget_get_foreground_pid();
756 if (NULL != all_result) {
757 /* Send result text to widget */
758 vcdc_send_result(pid, vcd_client_manager_get_pid(), VC_COMMAND_TYPE_WIDGET);
761 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Request tooltip hide");
762 /* Send to hide tooltip */
763 vcdc_send_show_tooltip(pid, false);
766 if (-1 != vcd_client_manager_get_pid()) {
767 /* Manager client is available */
768 if (0 != vcdc_send_result_to_manager(vcd_client_manager_get_pid(), VC_RESULT_TYPE_NORMAL)) {
769 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Fail to send result");
773 vcd_client_manager_set_exclusive(false);
779 SLOG(LOG_DEBUG, TAG_VCD, "[Server] @ Get engine result @");
782 vc_cmd_s* temp_cmd = NULL;
783 vc_cmd_list_h vc_cmd_list = NULL;
785 if (0 != vc_cmd_list_create(&vc_cmd_list)) {
786 SLOG(LOG_ERROR, TAG_VCD, "[Server] Fail to create command list");
787 vcd_client_manager_set_exclusive(false);
788 vcd_config_set_service_state(VCD_STATE_READY);
789 vcdc_send_service_state(VCD_STATE_READY);
794 for (i = 0; i < count; i++) {
795 SLOG(LOG_DEBUG, TAG_VCD, "[Server] [%d] Result ID(%d)", i, result_id[i]);
797 if (result_id[i] < 0) {
798 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Result ID(%d) is NOT valid", result_id[i]);
802 ret = vcd_client_get_cmd_from_result_id(result_id[i], &temp_cmd);
803 if (0 == ret && NULL != temp_cmd) {
804 switch (temp_cmd->format) {
805 case VC_CMD_FORMAT_FIXED:
806 case VC_CMD_FORMAT_FIXED_AND_VFIXED:
807 case VC_CMD_FORMAT_VFIXED_AND_FIXED:
808 case VC_CMD_FORMAT_PARTIAL:
809 /* Nonfixed result is NOT valid */
811 case VC_CMD_FORMAT_FIXED_AND_NONFIXED:
812 if (NULL == temp_cmd->parameter) {
813 if (NULL != non_fixed_result) {
814 temp_cmd->parameter = strdup(non_fixed_result);
817 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Command type is NOT vaild. Parameter (%s)", temp_cmd->parameter);
820 case VC_CMD_FORMAT_NONFIXED_AND_FIXED:
821 if (NULL == temp_cmd->command) {
822 if (NULL != non_fixed_result) {
823 temp_cmd->command = strdup(non_fixed_result);
826 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Command type is NOT vaild. Command (%s)", temp_cmd->command);
831 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Unknown command type : %d", temp_cmd->type);
835 if (0 != vc_cmd_list_add(vc_cmd_list, (vc_cmd_h)temp_cmd)) {
836 SLOG(LOG_DEBUG, TAG_VCD, "Fail to add command to list");
837 vc_cmd_destroy((vc_cmd_h)temp_cmd);
840 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] NOT found matached result(%d)", result_id[i]);
844 vc_cmd_print_list(vc_cmd_list);
846 SLOG(LOG_DEBUG, TAG_VCD, "[Server] @@@@");
848 int result_count = 0;
849 vc_cmd_list_get_count(vc_cmd_list, &result_count);
851 if (false == vcd_client_manager_get_exclusive()) {
852 int pid = vcd_client_widget_get_foreground_pid();
854 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Request tooltip hide");
855 vcdc_send_show_tooltip(pid, false);
858 vc_info_parser_set_result(all_result, event, msg, vc_cmd_list, false);
860 if (-1 != vcd_client_manager_get_pid()) {
861 /* Manager client is available */
862 if (0 != vcdc_send_result_to_manager(vcd_client_manager_get_pid(), VC_RESULT_TYPE_NORMAL)) {
863 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Fail to send result");
866 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Manager is NOT available. Send result to client directly");
867 /* Send result to client */
868 ecore_timer_add(0, __vcd_send_selected_result, NULL);
871 /* exclusive command */
872 vc_info_parser_set_result(all_result, event, msg, vc_cmd_list, true);
874 if (-1 != vcd_client_manager_get_pid()) {
875 /* Manager client is available */
876 if (0 != vcdc_send_result_to_manager(vcd_client_manager_get_pid(), VC_RESULT_TYPE_NORMAL)) {
877 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Fail to send result");
880 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Manager is NOT available");
883 vcd_client_manager_set_exclusive(false);
886 vc_cmd_list_destroy(vc_cmd_list, true);
893 static void __vcd_server_nlu_result_cb(vcp_result_event_e event, const char* nlu_result, void *user_data)
895 SECURE_SLOG(LOG_DEBUG, TAG_VCD, "[Server] NLU result cb - event(%d)", event);
896 SECURE_SLOG(LOG_DEBUG, TAG_VCD, "[Server] result (%s)", nlu_result);
898 int ret = vc_info_parser_set_nlu_result(nlu_result);
900 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to set nlu result (%d)", ret);
907 static void __vcd_server_error_cb(vcp_error_e error, const char* msg, void *user_data)
909 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Engine Error cb - reason(%d), msg(%s)", error, msg);
910 ecore_main_loop_thread_safe_call_async(__cancel_by_error, NULL);
912 char* error_msg = NULL;
914 error_msg = strdup(msg);
917 if (0 != vcdc_send_error_signal_to_manager(vcd_client_manager_get_pid(), error, error_msg)) {
918 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to send error signal");
921 if (NULL != error_msg) {
930 * vcd server Interfaces
932 static void __vcd_file_clean_up()
934 SLOG(LOG_DEBUG, TAG_VCD, "== Old file clean up == ");
937 struct dirent *dirp = NULL;
939 dp = opendir(VC_RUNTIME_INFO_ROOT);
941 SLOG(LOG_ERROR, TAG_VCD, "[File message WARN] Fail to open path : %s", VC_RUNTIME_INFO_ROOT);
945 char remove_path[256] = {0, };
950 if (!strncmp("vc_", dirp->d_name, strlen("vc_"))) {
951 memset(remove_path, 0, 256);
952 snprintf(remove_path, 256, "%s/%s", VC_RUNTIME_INFO_ROOT, dirp->d_name);
955 if (0 != remove(remove_path)) {
956 SLOG(LOG_WARN, TAG_VCD, "[File message WARN] Fail to remove file : %s", remove_path);
958 SLOG(LOG_DEBUG, TAG_VCD, "[File message] Remove file : %s", remove_path);
962 } while (NULL != dirp);
969 static int __vcd_db_clean_up()
972 int cnt = VC_COMMAND_TYPE_FOREGROUND;
974 if (VC_COMMAND_TYPE_BACKGROUND != cnt)
975 ret = vc_db_delete_commands(-1, cnt, NULL);
976 } while (VC_COMMAND_TYPE_EXCLUSIVE >= ++cnt);
985 /* Remove old file */
986 __vcd_file_clean_up();
988 /* initialize modules */
989 ret = vcd_config_initialize(__config_lang_changed_cb, __config_foreground_changed_cb, NULL);
991 SLOG(LOG_ERROR, TAG_VCD, "[Server WARNING] Fail to initialize config.");
995 ret = __vcd_db_clean_up();
997 SLOG(LOG_ERROR, TAG_VCD, "[Server WARNING] Fail to remove db data");
1000 vcd_config_set_service_state(VCD_STATE_NONE);
1002 //ret = vcd_engine_agent_init(__vcd_server_pre_result_cb, __vcd_server_result_cb, __vcd_server_nlu_result_cb, __vcd_server_error_cb);
1003 ret = vcd_engine_agent_init(__vcd_server_asr_result_cb, __vcd_server_result_cb, __vcd_server_nlg_result_cb, __vcd_server_error_cb);
1005 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to engine agent initialize : result(%d)", ret);
1009 if (0 != vcd_recorder_create(__server_recorder_callback, __server_recorder_interrupt_callback)) {
1010 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to create recorder");
1011 return VCD_ERROR_OPERATION_FAILED;
1015 ret = vcd_engine_agent_initialize_current_engine();
1017 if (VCD_ERROR_ENGINE_NOT_FOUND == ret)
1018 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] There is No Voice control engine");
1020 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to init engine");
1022 g_is_engine = false;
1028 if (0 != vcd_engine_agent_load_current_engine()) {
1029 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to load current engine");
1030 return VCD_ERROR_OPERATION_FAILED;
1033 /* Initialize manager info */
1034 vcd_client_manager_unset();
1036 vcd_config_set_service_state(VCD_STATE_READY);
1037 vcdc_send_service_state(VCD_STATE_READY);
1039 SLOG(LOG_ERROR, TAG_VCD, "[Server SUCCESS] initialize");
1047 if (0 < g_list_length(g_proc_list)) {
1048 iter = g_list_first(g_proc_list);
1049 while (NULL != iter) {
1050 g_proc_list = g_list_remove_link(g_proc_list, iter);
1051 iter = g_list_first(g_proc_list);
1055 if (g_restart_timer != NULL) {
1056 ecore_timer_del(g_restart_timer);
1057 g_restart_timer = NULL;
1060 vcd_state_e state = vcd_config_get_service_state();
1061 if (VCD_STATE_READY != state) {
1062 if (VCD_STATE_RECORDING == state) {
1063 vcd_recorder_stop();
1065 vcd_engine_recognize_cancel();
1067 if (0 != vcd_recorder_destroy()) {
1068 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to destroy recorder");
1070 SLOG(LOG_DEBUG, TAG_VCD, "[Server] destroy recorder");
1073 if (0 != vcd_engine_agent_release()) {
1074 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to release engine");
1076 SLOG(LOG_DEBUG, TAG_VCD, "[Server] release engine");
1079 vcd_client_manager_unset_appid();
1081 vcd_config_set_service_state(VCD_STATE_NONE);
1082 vcdc_send_service_state(VCD_STATE_NONE);
1084 SLOG(LOG_ERROR, TAG_VCD, "[Server] mode finalize");
1089 static Eina_Bool __finalize_quit_ecore_loop(void *data)
1091 SLOG(LOG_ERROR, TAG_VCD, "[Server] quit ecore main loop");
1092 ecore_main_loop_quit();
1096 static void __read_proc()
1099 struct dirent *dirp = NULL;
1103 if (0 < g_list_length(g_proc_list)) {
1104 iter = g_list_first(g_proc_list);
1105 while (NULL != iter) {
1106 g_proc_list = g_list_remove_link(g_proc_list, iter);
1107 iter = g_list_first(g_proc_list);
1111 dp = opendir("/proc");
1113 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Fail to open proc");
1119 tmp = atoi(dirp->d_name);
1120 if (0 >= tmp) continue;
1121 g_proc_list = g_list_append(g_proc_list, GINT_TO_POINTER(tmp));
1123 } while (NULL != dirp);
1129 static void __vcd_cleanup_client(vcd_client_type_e type)
1131 int* client_list = NULL;
1132 int client_count = 0;
1139 if (VCD_CLIENT_TYPE_NORMAL == type) {
1140 ret = vcd_client_get_list(&client_list, &client_count);
1141 } else if (VCD_CLIENT_TYPE_WIDGET == type) {
1142 ret = vcd_client_widget_get_list(&client_list, &client_count);
1143 } else if (VCD_CLIENT_TYPE_MANAGER == type) {
1144 mgr_pid = vcd_client_manager_get_pid();
1145 client_list = &mgr_pid;
1147 if (-1 == mgr_pid) {
1148 SLOG(LOG_WARN, TAG_VCD, "[WARNING] Invalid Manager pid");
1153 if (0 == ret || mgr_pid > 0) {
1154 SLOG(LOG_DEBUG, TAG_VCD, "@@@ Clean up %s client ", type ? (type == 1) ? "Widget" : "Manager" : "Normal");
1155 if (NULL != client_list && client_count > 0) {
1156 for (i = 0; i < client_count; i++) {
1159 for (j = 0; j < g_list_length(g_proc_list); j++) {
1160 iter = g_list_nth(g_proc_list, j);
1162 if (*(client_list + i) == GPOINTER_TO_INT(iter->data)) {
1163 SLOG(LOG_DEBUG, TAG_VCD, "%s pid(%d) is running", type ? (type == 1) ? "Widget" : "Manager" : "Normal", *(client_list + i));
1170 if (false == exist) {
1171 SLOG(LOG_ERROR, TAG_VCD, "%s pid(%d) should be removed", type ? (type == 1) ? "Widget" : "Manager" : "Normal", *(client_list + i));
1172 if (VCD_CLIENT_TYPE_NORMAL == type)
1173 vcd_server_finalize(*(client_list + i));
1174 else if (VCD_CLIENT_TYPE_WIDGET == type)
1175 vcd_server_widget_finalize(*(client_list + i));
1177 vcd_server_mgr_finalize(mgr_pid);
1181 SLOG(LOG_DEBUG, TAG_VCD, "@@@");
1183 if (NULL != client_list && -1 == mgr_pid) {
1190 Eina_Bool vcd_cleanup_client_all(void *data)
1194 __vcd_cleanup_client(VCD_CLIENT_TYPE_NORMAL);
1195 __vcd_cleanup_client(VCD_CLIENT_TYPE_WIDGET);
1196 __vcd_cleanup_client(VCD_CLIENT_TYPE_MANAGER);
1199 if (0 == vcd_client_get_list(&client_list, &client_count)) {
1200 SLOG(LOG_DEBUG, TAG_VCD, "@@@ Clean up client ");
1201 if (NULL != client_list && client_count > 0) {
1202 for (i = 0; i < client_count; i++) {
1205 for (j = 0; j < g_list_length(g_proc_list); j++) {
1206 iter = g_list_nth(g_proc_list, j);
1208 if (client_list[i] == GPOINTER_TO_INT(iter->data)) {
1209 SLOG(LOG_DEBUG, TAG_VCD, "pid(%d) is running", client_list[i]);
1216 if (false == exist) {
1217 SLOG(LOG_ERROR, TAG_VCD, "pid(%d) should be removed", client_list[i]);
1218 vcd_server_finalize(client_list[i]);
1221 result = vcdc_send_hello(client_list[i], VCD_CLIENT_TYPE_NORMAL);
1224 SLOG(LOG_DEBUG, TAG_VCD, "[Server] pid(%d) should be removed.", client_list[i]);
1225 vcd_server_finalize(client_list[i]);
1226 } else if (-1 == result) {
1227 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Hello result has error");
1232 SLOG(LOG_DEBUG, TAG_VCD, "@@@");
1234 if (NULL != client_list) {
1239 /* If app is in background state, app cannot response message. */
1240 if (0 == vcd_client_widget_get_list(&client_list, &client_count)) {
1241 SLOG(LOG_DEBUG, TAG_VCD, "@@@ Clean up widget");
1242 if (NULL != client_list && client_count > 0) {
1243 for (i = 0; i < client_count; i++) {
1246 for (j = 0; j < g_list_length(g_proc_list); j++) {
1247 iter = g_list_nth(g_proc_list, j);
1249 if (client_list[i] == GPOINTER_TO_INT(iter->data)) {
1250 SLOG(LOG_DEBUG, TAG_VCD, "widget pid(%d) is running", client_list[i]);
1257 if (false == exist) {
1258 SLOG(LOG_ERROR, TAG_VCD, "widget pid(%d) should be removed", client_list[i]);
1259 vcd_server_widget_finalize(client_list[i]);
1262 result = vcdc_send_hello(client_list[i], VCD_CLIENT_TYPE_WIDGET);
1265 SLOG(LOG_DEBUG, TAG_VCD, "[Server] widget pid(%d) should be removed.", client_list[i]);
1266 vcd_server_widget_finalize(client_list[i]);
1267 } else if (-1 == result) {
1268 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Hello result has error");
1273 SLOG(LOG_DEBUG, TAG_VCD, "@@@");
1276 if (NULL != client_list) {
1284 int mgr_pid = vcd_client_manager_get_pid();
1286 for (j = 0; j < g_list_length(g_proc_list); j++) {
1287 iter = g_list_nth(g_proc_list, j);
1289 if (mgr_pid == GPOINTER_TO_INT(iter->data)) {
1290 SLOG(LOG_DEBUG, TAG_VCD, "manager pid(%d) is running", mgr_pid);
1297 if (false == exist) {
1298 SLOG(LOG_ERROR, TAG_VCD, "manager pid (%d) should be removed", mgr_pid);
1299 vcd_server_mgr_finalize(mgr_pid);
1306 int vcd_server_get_service_state()
1308 return vcd_config_get_service_state();
1311 int vcd_server_get_foreground()
1314 vcd_config_get_foreground(&pid);
1322 int vcd_server_mgr_initialize(int pid)
1324 if (false == g_is_engine) {
1325 if (0 != vcd_engine_agent_initialize_current_engine()) {
1326 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] No Engine");
1327 g_is_engine = false;
1328 return VCD_ERROR_ENGINE_NOT_FOUND;
1334 /* check if pid is valid */
1335 if (false == vcd_client_manager_is_valid(pid)) {
1336 SLOG(LOG_ERROR, TAG_VCD, "[Server] old manager pid(%d) be removed", vcd_client_manager_get_pid());
1337 vcd_server_mgr_cancel();
1338 vcd_client_manager_unset();
1341 /* Add client information to client manager */
1342 if (0 != vcd_client_manager_set(pid)) {
1343 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to add manager");
1344 return VCD_ERROR_OPERATION_FAILED;
1347 if (0 != vcdc_send_manager_pid(pid))
1348 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Fail to send manager pid");
1350 SLOG(LOG_ERROR, TAG_VCD, "[Server Success] Manager initialize : pid(%d)", pid);
1352 return VCD_ERROR_NONE;
1355 int vcd_server_mgr_finalize(int pid)
1357 /* check if pid is valid */
1358 if (false == vcd_client_manager_is_valid(pid)) {
1359 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] The manager pid(%d) is NOT valid", pid);
1360 return VCD_ERROR_INVALID_PARAMETER;
1363 /* Cancel recognition */
1364 vcd_server_mgr_cancel();
1366 if (0 != vcdc_send_manager_pid(-1))
1367 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Fail to send manager pid");
1369 /* Remove manager information */
1370 if (0 != vcd_client_manager_unset()) {
1371 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Fail to delete client");
1374 if (0 == vcd_client_get_ref_count()) {
1375 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Connected client list is empty");
1376 ecore_timer_add(0, __finalize_quit_ecore_loop, NULL);
1379 SLOG(LOG_ERROR, TAG_VCD, "[Server Success] Manager Finalize : pid(%d)", pid);
1381 return VCD_ERROR_NONE;
1384 int vcd_server_mgr_set_command(int pid)
1386 if (0 != vcd_client_manager_set_command(pid)) {
1387 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] The manager pid(%d) is NOT valid", pid);
1388 return VCD_ERROR_INVALID_PARAMETER;
1390 return VCD_ERROR_NONE;
1393 int vcd_server_mgr_unset_command(int pid)
1395 if (0 != vcd_client_manager_unset_command(pid)) {
1396 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] The manager pid(%d) is NOT valid", pid);
1397 return VCD_ERROR_INVALID_PARAMETER;
1399 return VCD_ERROR_NONE;
1402 int vcd_server_mgr_set_demandable_client(int pid)
1404 /* check if pid is valid */
1405 if (false == vcd_client_manager_is_valid(pid)) {
1406 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] The manager pid(%d) is NOT valid", pid);
1407 return VCD_ERROR_INVALID_PARAMETER;
1410 GSList* client_list = NULL;
1411 if (0 != vc_info_parser_get_demandable_clients(&client_list)) {
1412 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to get demandable client");
1413 return VCD_ERROR_OPERATION_FAILED;
1416 /* Save client list */
1417 if (0 != vcd_client_manager_set_demandable_client(pid, client_list)) {
1418 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to set demandable client list");
1419 return VCD_ERROR_OPERATION_FAILED;
1422 return VCD_ERROR_NONE;
1425 int vcd_server_mgr_set_audio_type(int pid, const char* audio_type)
1427 /* check if pid is valid */
1428 if (false == vcd_client_manager_is_valid(pid)) {
1429 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] The manager pid(%d) is NOT valid", pid);
1430 return VCD_ERROR_INVALID_PARAMETER;
1434 vcp_audio_type_e type = VCP_AUDIO_TYPE_PCM_S16_LE;
1438 ret = vcd_engine_get_audio_format(audio_type, &type, &rate, &channel);
1440 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to get audio format : %d", ret);
1441 return VCD_ERROR_OPERATION_FAILED;
1444 ret = vcd_recorder_set(audio_type, type, rate, channel);
1446 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to set audio in type : %d", ret);
1447 return VCD_ERROR_OPERATION_FAILED;
1450 return VCD_ERROR_NONE;
1453 int vcd_server_mgr_get_audio_type(int pid, char** audio_type)
1455 /* check if pid is valid */
1456 if (false == vcd_client_manager_is_valid(pid)) {
1457 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] The manager pid(%d) is NOT valid", pid);
1458 return VCD_ERROR_INVALID_PARAMETER;
1461 int ret = vcd_recorder_get(audio_type);
1463 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to get audio in type : %d", ret);
1464 return VCD_ERROR_OPERATION_FAILED;
1467 return VCD_ERROR_NONE;
1470 int vcd_server_mgr_set_client_info(int pid)
1472 /* check if pid is valid */
1473 if (false == vcd_client_manager_is_valid(pid)) {
1474 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] The manager pid(%d) is NOT valid", pid);
1475 return VCD_ERROR_INVALID_PARAMETER;
1478 int ret = vcd_client_save_client_info();
1480 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to save client info : %d", ret);
1481 return VCD_ERROR_OPERATION_FAILED;
1484 return VCD_ERROR_NONE;
1487 static int __reset_waiting_for_widget_recording(void)
1489 SLOG(LOG_ERROR, TAG_VCD, "[Server] Reet waiting for widget recording");
1490 // Delete timer to check that widget client is terminated
1491 if (g_check_widget_client_timer) {
1492 ecore_timer_del(g_check_widget_client_timer);
1493 g_check_widget_client_timer = NULL;
1495 // Reset flag to wait for recording from widget client
1496 vcd_client_widget_set_waiting_for_recording(-1, false);
1500 static Eina_Bool __send_waiting_timeout_error_to_manager(void* data)
1502 intptr_t ppid = (intptr_t)data;
1503 int pid = (int)ppid;
1504 SLOG(LOG_ERROR, TAG_VCD, "Widget client didn't send to start recording, pid(%d)", pid);
1505 __reset_waiting_for_widget_recording();
1507 vcdc_send_error_signal_to_manager(vcd_client_manager_get_pid(), VCD_ERROR_OPERATION_FAILED, "voice_engine.error.proc_fail");
1511 static int __set_waiting_for_widget_recording(int pid)
1513 SLOG(LOG_ERROR, TAG_VCD, "[Server] Set waiting for widget recording, pid(%d)", pid);
1515 // Check if the app included widget client is terminated or not. If it is terminated, vcd_server_widget_finalize() function will be called
1516 // In that function, it will start recording
1517 intptr_t ppid = (intptr_t)pid;
1518 g_check_widget_client_timer = ecore_timer_add(2.0, __send_waiting_timeout_error_to_manager, (void*)ppid);
1520 // Set flag to wait for recording from widget client
1521 vcd_client_widget_set_waiting_for_recording(pid, true);
1525 static int __start_internal_recognition()
1528 __reset_waiting_for_widget_recording();
1530 if (0 != vcd_client_command_collect_command()) {
1531 SLOG(LOG_ERROR, TAG_VCD, "[Client Data ERROR] Fail to collect command");
1532 /* Send error cb to manager */
1533 int pid = vcd_client_widget_get_foreground_pid();
1535 vcdc_send_error_signal_to_manager(vcd_client_manager_get_pid(), VCD_ERROR_OPERATION_FAILED, "voice_engine.error.proc_fail");
1536 return VCD_ERROR_OPERATION_FAILED;
1539 /* 3. Set command to engine */
1540 ret = vcd_engine_set_commands();
1542 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to set commands : %d", ret);
1543 /* Send error cb to manager */
1544 int pid = vcd_client_widget_get_foreground_pid();
1546 vcdc_send_error_signal_to_manager(vcd_client_manager_get_pid(), VCD_ERROR_OPERATION_FAILED, "voice_engine.error.proc_fail");
1547 return VCD_ERROR_OPERATION_FAILED;
1550 SLOG(LOG_ERROR, TAG_VCD, "[Server Success] Set command");
1552 bool stop_by_silence = true;
1553 if (VCD_RECOGNITION_MODE_MANUAL == vcd_client_get_recognition_mode()) {
1554 stop_by_silence = false;
1557 vcd_client_manager_set_result_text(NULL);
1559 /* 4. start recognition */
1560 ret = vcd_engine_recognize_start(stop_by_silence);
1562 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to start recognition : result(%d)", ret);
1563 /* Send error cb to manager */
1564 int pid = vcd_client_widget_get_foreground_pid();
1566 vcdc_send_error_signal_to_manager(vcd_client_manager_get_pid(), VCD_ERROR_OPERATION_FAILED, "voice_engine.error.proc_fail");
1567 return VCD_ERROR_OPERATION_FAILED;
1570 SLOG(LOG_ERROR, TAG_VCD, "[Server Success] Start engine");
1573 /* 5. recorder start */
1574 ret = vcd_recorder_start();
1576 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to start recorder : result(%d)", ret);
1577 vcd_engine_recognize_cancel();
1578 /* Send error cb to manager */
1579 int pid = vcd_client_widget_get_foreground_pid();
1581 vcdc_send_error_signal_to_manager(vcd_client_manager_get_pid(), VCD_ERROR_OPERATION_FAILED, "voice_engine.error.proc_fail");
1586 vcd_config_set_service_state(VCD_STATE_RECORDING);
1587 vcdc_send_service_state(VCD_STATE_RECORDING);
1589 SLOG(LOG_ERROR, TAG_VCD, "[Server Success] Start recognition(%d)", stop_by_silence);
1594 static Eina_Bool __vcd_request_show_tooltip(void *data)
1596 int pid = vcd_client_widget_get_foreground_pid();
1598 SLOG(LOG_ERROR, TAG_VCD, "[Server] Request tooltip show and widget command, show(%d)", (bool)data);
1599 vcdc_send_show_tooltip(pid, (bool)data);
1605 int vcd_server_mgr_start(vcd_recognition_mode_e recognition_mode, bool exclusive_cmd, bool start_by_client)
1607 /* 1. check current state */
1608 vcd_state_e state = vcd_config_get_service_state();
1610 if (VCD_STATE_READY != state) {
1611 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Current state is not ready");
1612 return VCD_ERROR_INVALID_STATE;
1614 if (-1 == vcd_client_manager_get_pid()) {
1615 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Manager is NOT available.");
1616 return VCD_ERROR_OPERATION_FAILED;
1618 __reset_waiting_for_widget_recording();
1620 SLOG(LOG_ERROR, TAG_VCD, "[Server] set recognition mode = %d", recognition_mode);
1621 vcd_client_set_recognition_mode(recognition_mode);
1623 if (false == exclusive_cmd) {
1624 vcd_client_update_foreground_pid();
1625 /* Notify show tooltip */
1626 if (1 == vcd_config_get_command_type_enabled(VC_COMMAND_TYPE_WIDGET)) {
1627 int pid = vcd_client_widget_get_foreground_pid();
1629 SLOG(LOG_ERROR, TAG_VCD, "[Server] Request tooltip show and widget command");
1630 ecore_timer_add(0, __vcd_request_show_tooltip, (void*)true);
1632 __set_waiting_for_widget_recording(pid);
1637 vcd_client_manager_set_exclusive(exclusive_cmd);
1641 if (true == start_by_client) {
1642 /* Get foreground pid */
1643 if (0 != vcd_config_get_foreground(&fg_pid)) {
1644 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to get foreground");
1647 /* Set client exclusive option */
1648 if (0 != vcd_client_set_exclusive_command(fg_pid)) {
1649 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to set exclusive command");
1653 SLOG(LOG_ERROR, TAG_VCD, "[Server] start internal recognition");
1655 int ret = __start_internal_recognition();
1657 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to start recongition : %d", ret);
1661 if (true == start_by_client) {
1662 vcd_client_unset_exclusive_command(fg_pid);
1665 SLOG(LOG_ERROR, TAG_VCD, "[Server Success] start internal recognition");
1666 return VCD_ERROR_NONE;
1669 int vcd_server_mgr_stop()
1671 /* 1. Check current state is recording */
1672 if (VCD_STATE_RECORDING != vcd_config_get_service_state()) {
1673 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Current state is not recording");
1674 return VCD_ERROR_INVALID_STATE;
1676 if (-1 == vcd_client_manager_get_pid()) {
1677 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Manager is NOT available.");
1678 return VCD_ERROR_OPERATION_FAILED;
1680 SLOG(LOG_ERROR, TAG_VCD, "[Server] stop internal recognition");
1683 /* 2. Stop recorder */
1684 vcd_recorder_stop();
1687 /* 3. Stop engine recognition */
1688 int ret = vcd_engine_recognize_stop();
1690 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to stop recognition : %d", ret);
1693 /* 4. Set original mode */
1694 vcd_config_set_service_state(VCD_STATE_PROCESSING);
1695 vcdc_send_service_state(VCD_STATE_PROCESSING);
1697 SLOG(LOG_ERROR, TAG_VCD, "[Server Success] stop internal recognition");
1698 return VCD_ERROR_NONE;
1701 int vcd_server_mgr_cancel()
1703 if (-1 == vcd_client_manager_get_pid()) {
1704 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Manager is NOT available.");
1705 return VCD_ERROR_OPERATION_FAILED;
1708 if (g_restart_timer != NULL) {
1709 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Delete restart engine timer");
1710 ecore_timer_del(g_restart_timer);
1711 g_restart_timer = NULL;
1714 /* 1. Check current state */
1715 vcd_state_e state = vcd_config_get_service_state();
1716 if (VCD_STATE_READY == state) {
1717 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Current state is READY");
1718 vcd_recorder_stop();
1720 if (false == vcd_client_manager_get_exclusive()) {
1721 if (1 == vcd_config_get_command_type_enabled(VC_COMMAND_TYPE_WIDGET)) {
1722 int pid = vcd_client_widget_get_foreground_pid();
1724 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Request tooltip hide");
1725 ecore_timer_add(0, __vcd_request_show_tooltip, (void*)false);
1730 vcdc_send_service_state(VCD_STATE_READY);
1731 return VCD_ERROR_NONE;
1733 SLOG(LOG_ERROR, TAG_VCD, "[Server] cancel internal recognition");
1736 /* 2. Stop recorder */
1737 vcd_recorder_stop();
1740 /* 3. Cancel engine */
1741 int ret = vcd_engine_recognize_cancel();
1743 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Fail to cancel : %d", ret);
1746 if (false == vcd_client_manager_get_exclusive()) {
1747 if (1 == vcd_config_get_command_type_enabled(VC_COMMAND_TYPE_WIDGET)) {
1748 int pid = vcd_client_widget_get_foreground_pid();
1750 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Request tooltip hide");
1751 ecore_timer_add(0, __vcd_request_show_tooltip, (void*)false);
1755 vcd_client_manager_set_exclusive(false);
1759 vcd_config_set_service_state(VCD_STATE_READY);
1760 vcdc_send_service_state(VCD_STATE_READY);
1762 SLOG(LOG_ERROR, TAG_VCD, "[Server Success] cancel internal recognition");
1763 return VCD_ERROR_NONE;
1767 int vcd_server_mgr_result_select()
1769 __vcd_send_selected_result(NULL);
1771 return VCD_ERROR_NONE;
1774 int vcd_server_mgr_set_domain(int pid, const char* domain)
1776 /* check if pid is valid */
1777 if (false == vcd_client_manager_is_valid(pid)) {
1778 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] The manager pid(%d) is NOT valid", pid);
1779 return VCD_ERROR_INVALID_PARAMETER;
1782 vcd_state_e state = vcd_config_get_service_state();
1783 if (VCD_STATE_READY != state) {
1784 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Current state is not ready");
1785 return VCD_ERROR_INVALID_STATE;
1788 /* Set domain to engine */
1789 int ret = vcd_engine_set_domain(pid, domain);
1791 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to set domain : %d", ret);
1793 SLOG(LOG_DEBUG, TAG_VCD, "[Server SUCCESS] Set domain");
1799 int vcd_server_mgr_set_private_data(int pid, const char* key, const char* data)
1801 /* check if pid is valid */
1802 if (false == vcd_client_manager_is_valid(pid)) {
1803 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] The manager pid(%d) is NOT valid", pid);
1804 return VCD_ERROR_INVALID_PARAMETER;
1807 vcd_state_e state = vcd_config_get_service_state();
1808 if (VCD_STATE_READY != state) {
1809 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Current state is not ready");
1810 return VCD_ERROR_INVALID_STATE;
1813 /* Set private data to engine */
1814 int ret = vcd_engine_set_private_data(pid, key, data);
1816 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to set private data : %d", ret);
1818 SLOG(LOG_DEBUG, TAG_VCD, "[Server SUCCESS] Set private data");
1824 int vcd_server_mgr_get_private_data(int pid, const char* key, char** data)
1826 /* check if pid is valid */
1827 if (false == vcd_client_manager_is_valid(pid)) {
1828 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] The manager pid(%d) is NOT valid", pid);
1829 return VCD_ERROR_INVALID_PARAMETER;
1831 vcd_state_e state = vcd_config_get_service_state();
1832 if (VCD_STATE_READY != state) {
1833 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Current state is not ready");
1834 return VCD_ERROR_INVALID_STATE;
1837 /* Get private data to engine */
1838 int ret = vcd_engine_get_private_data(pid, key, data);
1840 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to get private data : %d", ret);
1842 SLOG(LOG_DEBUG, TAG_VCD, "[Server SUCCESS] Set private data");
1848 int vcd_server_mgr_do_action(int pid, int type, const char* action)
1852 /* check if pid is valid */
1853 if (false == vcd_client_manager_is_valid(pid)) {
1854 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] The manager pid(%d) is NOT valid", pid);
1855 return VCD_ERROR_INVALID_PARAMETER;
1858 vcd_state_e state = vcd_config_get_service_state();
1859 if (VCD_STATE_READY != state) {
1860 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Current state is not ready");
1861 return VCD_ERROR_INVALID_STATE;
1864 /* Reqeust do action to engine */
1865 if (VCD_SEND_EVENT_TYPE_TEXT == type)
1866 ret = vcd_engine_process_text(pid, action);
1867 else if (VCD_SEND_EVENT_TYPE_LIST_EVENT == type)
1868 ret = vcd_engine_process_list_event(pid, action);
1869 else if (VCD_SEND_EVENT_TYPE_HAPTIC_EVENT == type)
1870 ret = vcd_engine_process_haptic_event(pid, action);
1873 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to process do action : %d", ret);
1875 vcd_config_set_service_state(VCD_STATE_PROCESSING);
1876 vcdc_send_service_state(VCD_STATE_PROCESSING);
1877 SLOG(LOG_DEBUG, TAG_VCD, "[Server SUCCESS] Process do action");
1883 int vcd_server_mgr_enable_command_type(int pid, int cmd_type)
1887 /* check if pid is valid */
1888 if (false == vcd_client_manager_is_valid(pid)) {
1889 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] The manager pid(%d) is NOT valid", pid);
1890 return VCD_ERROR_INVALID_PARAMETER;
1893 vcd_state_e state = vcd_config_get_service_state();
1894 if (VCD_STATE_READY != state) {
1895 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Current state is not ready");
1896 return VCD_ERROR_INVALID_STATE;
1899 ret = vcd_config_enable_command_type(cmd_type);
1901 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to enable command type");
1903 SLOG(LOG_ERROR, TAG_VCD, "[Server] Enable command type(%d)", cmd_type);
1909 int vcd_server_mgr_disable_command_type(int pid, int cmd_type)
1913 /* check if pid is valid */
1914 if (false == vcd_client_manager_is_valid(pid)) {
1915 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] The manager pid(%d) is NOT valid", pid);
1916 return VCD_ERROR_INVALID_PARAMETER;
1919 vcd_state_e state = vcd_config_get_service_state();
1920 if (VCD_STATE_READY != state) {
1921 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Current state is not ready");
1922 return VCD_ERROR_INVALID_STATE;
1925 ret = vcd_config_disable_command_type(cmd_type);
1927 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to disable command type");
1929 SLOG(LOG_ERROR, TAG_VCD, "[Server] Disable command type(%d)", cmd_type);
1936 * VC Server Functions for Client
1938 int vcd_server_initialize(int pid)
1940 if (false == g_is_engine) {
1941 if (0 != vcd_engine_agent_initialize_current_engine()) {
1942 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] No Engine");
1943 g_is_engine = false;
1944 return VCD_ERROR_ENGINE_NOT_FOUND;
1950 if (false == vcd_engine_is_available_engine()) {
1951 if (0 != vcd_engine_agent_initialize_current_engine()) {
1952 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] No Engine");
1953 return VCD_ERROR_ENGINE_NOT_FOUND;
1957 /* check if pid is valid */
1958 if (true == vcd_client_is_available(pid)) {
1959 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] The pid is already exist");
1960 return VCD_ERROR_INVALID_PARAMETER;
1963 /* Add client information to client manager */
1964 if (0 != vcd_client_add(pid)) {
1965 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to add client info");
1966 return VCD_ERROR_OPERATION_FAILED;
1969 SLOG(LOG_DEBUG, TAG_VCD, "[Server Success] Client Initialize : pid(%d)", pid);
1971 return VCD_ERROR_NONE;
1974 int vcd_server_finalize(int pid)
1976 /* check if pid is valid */
1977 if (false == vcd_client_is_available(pid)) {
1978 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] pid is NOT valid ");
1979 return VCD_ERROR_INVALID_PARAMETER;
1982 /* Remove client information */
1983 if (0 != vcd_client_delete(pid)) {
1984 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Fail to delete client");
1987 if (0 == vcd_client_get_ref_count()) {
1988 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Connected client list is empty");
1989 ecore_timer_add(0, __finalize_quit_ecore_loop, NULL);
1992 SLOG(LOG_DEBUG, TAG_VCD, "[Server Success] Client Finalize : pid(%d)", pid);
1994 return VCD_ERROR_NONE;
1997 int vcd_server_set_command(int pid, vc_cmd_type_e cmd_type)
1999 /* check if pid is valid */
2000 if (false == vcd_client_is_available(pid)) {
2001 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] pid is NOT valid ");
2002 return VCD_ERROR_INVALID_PARAMETER;
2005 if (0 != vcd_client_set_command_type(pid, cmd_type)) {
2006 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to set command type : pid(%d), cmd_type(%d)", pid, cmd_type);
2007 return VCD_ERROR_OPERATION_FAILED;
2013 int vcd_server_unset_command(int pid, vc_cmd_type_e cmd_type)
2015 /* check if pid is valid */
2016 if (false == vcd_client_is_available(pid)) {
2017 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] pid is NOT valid ");
2018 return VCD_ERROR_INVALID_PARAMETER;
2021 if (0 != vcd_client_unset_command_type(pid, cmd_type)) {
2022 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to unset command type : pid(%d), cmd_type(%d)", pid, cmd_type);
2023 return VCD_ERROR_OPERATION_FAILED;
2029 int vcd_server_set_foreground(int pid, bool value)
2031 /* check if pid is valid */
2032 if (false == vcd_client_is_available(pid) && false == vcd_client_widget_is_available(pid)) {
2033 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] pid is NOT valid ");
2034 return VCD_ERROR_INVALID_PARAMETER;
2037 if (0 != vcd_config_set_foreground(pid, value)) {
2038 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to set foreground : pid(%d), value(%d)", pid, value);
2039 return VCD_ERROR_OPERATION_FAILED;
2045 static int __vcd_server_launch_manager_app()
2048 bool running = false;
2051 ret = vcd_client_manager_get_appid(&appid);
2052 if (0 != ret || NULL == appid) {
2053 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Fail to get manager appid");
2054 return VCD_ERROR_OPERATION_FAILED;
2056 ret = app_manager_is_running(appid, &running);
2058 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Fail to check running with appid(%s)", appid);
2061 return VCD_ERROR_OPERATION_FAILED;
2063 if (false == running) {
2064 int tmp_ret = __vcd_is_package_installed(appid);
2065 if (false == tmp_ret) {
2066 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] manager app is not installed, appid(%s)", appid);
2068 ret = __vcd_activate_app_by_appcontrol(appid);
2070 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Fail to activate app");
2075 SLOG(LOG_ERROR, TAG_VCD, "Launch vc manager app: appid(%s)", appid);
2078 ret = __vcd_resume_app(appid);
2080 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Fail to resume app");
2085 SLOG(LOG_ERROR, TAG_VCD, "Resume vc manager app: appid(%s)", appid);
2091 return VCD_ERROR_NONE;
2094 int vcd_server_dialog(int pid, const char* disp_text, const char* utt_text, int continuous)
2096 /* check if pid is valid */
2097 if (false == vcd_client_is_available(pid) && false == vcd_client_widget_is_available(pid)) {
2098 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] pid is NOT valid ");
2099 return VCD_ERROR_INVALID_PARAMETER;
2102 int ret = __vcd_server_launch_manager_app();
2104 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);
2108 ret = vcdc_send_dialog(vcd_client_manager_get_pid(), pid, disp_text, utt_text, continuous);
2110 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);
2117 int vcd_server_is_system_command_valid(int pid, int* is_sys_cmd_valid)
2119 /* check if pid is valid */
2120 if (false == vcd_client_is_available(pid) && false == vcd_client_widget_is_available(pid)) {
2121 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] pid is NOT valid ");
2122 return VCD_ERROR_INVALID_PARAMETER;
2125 /* check if system command is valid */
2126 if (true == vcd_client_manager_is_system_command_valid(vcd_client_manager_get_pid()))
2127 *is_sys_cmd_valid = true;
2129 *is_sys_cmd_valid = false;
2135 int vcd_server_set_exclusive_command(int pid, bool value)
2137 /* check if pid is valid */
2138 if (false == vcd_client_is_available(pid)) {
2139 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] pid is NOT valid ");
2140 return VCD_ERROR_INVALID_PARAMETER;
2143 if (true == value) {
2144 if (0 != vcd_client_set_exclusive_command(pid)) {
2145 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to set exclusive command : pid(%d)", pid);
2146 return VCD_ERROR_OPERATION_FAILED;
2149 if (0 != vcd_client_unset_exclusive_command(pid)) {
2150 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to unset exclusive command : pid(%d)", pid);
2151 return VCD_ERROR_OPERATION_FAILED;
2158 int vcd_server_request_start(int pid, bool stop_by_silence)
2160 /* check if pid is valid */
2161 if (false == vcd_client_is_available(pid)) {
2162 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] pid(%d) is NOT forground client", pid);
2163 return VCD_ERROR_INVALID_PARAMETER;
2167 /* Check current state */
2168 vcd_state_e state = vcd_config_get_service_state();
2170 /* Service state should be ready */
2171 if (VCD_STATE_READY != state) {
2172 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Current state is not Ready : pid(%d)", pid);
2173 return VCD_ERROR_INVALID_STATE;
2176 if (-1 != vcd_client_manager_get_pid()) {
2177 /* Check current pid is valid */
2178 if (false == vcd_client_manager_check_demandable_client(pid)) {
2179 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Current client is NOT available : pid(%d)", pid);
2180 return VCD_ERROR_INVALID_PARAMETER;
2184 ret = vcd_server_mgr_start(stop_by_silence, false);
2186 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Fail to start recognition");
2187 return VCD_ERROR_INVALID_PARAMETER;
2193 int vcd_server_request_stop(int pid)
2196 /* Check current state */
2197 vcd_state_e state = vcd_config_get_service_state();
2199 /* Service state should be ready */
2200 if (VCD_STATE_RECORDING != state) {
2201 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Current state is not Recording : pid(%d)", pid);
2202 return VCD_ERROR_INVALID_STATE;
2205 if (-1 != vcd_client_manager_get_pid()) {
2206 /* Check current pid is valid */
2207 if (false == vcd_client_manager_check_demandable_client(pid)) {
2208 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Current client is NOT available : pid(%d)", pid);
2209 return VCD_ERROR_INVALID_PARAMETER;
2213 ret = vcd_server_mgr_stop();
2215 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Fail to start recognition");
2216 return VCD_ERROR_OPERATION_FAILED;
2219 return VCD_ERROR_NONE;
2222 int vcd_server_request_cancel(int pid)
2225 /* Check current state */
2226 vcd_state_e state = vcd_config_get_service_state();
2228 /* Service state should be recording or processing */
2229 if (VCD_STATE_RECORDING != state && VCD_STATE_PROCESSING != state) {
2230 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Current state is not Recording or Processing : pid(%d)", pid);
2231 return VCD_ERROR_INVALID_STATE;
2234 if (-1 != vcd_client_manager_get_pid()) {
2235 /* Check current pid is valid */
2236 if (false == vcd_client_manager_check_demandable_client(pid)) {
2237 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Current client is NOT available : pid(%d)", pid);
2238 return VCD_ERROR_INVALID_PARAMETER;
2242 ret = vcd_server_mgr_cancel();
2244 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Fail to start recognition");
2245 return VCD_ERROR_OPERATION_FAILED;
2248 return VCD_ERROR_NONE;
2253 * VC Server Functions for Widget lib
2255 int vcd_server_widget_initialize(int pid)
2257 if (false == g_is_engine) {
2258 if (0 != vcd_engine_agent_initialize_current_engine()) {
2259 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] No Engine");
2260 g_is_engine = false;
2261 return VCD_ERROR_ENGINE_NOT_FOUND;
2267 if (false == vcd_engine_is_available_engine()) {
2268 if (0 != vcd_engine_agent_initialize_current_engine()) {
2269 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] No Engine");
2270 return VCD_ERROR_ENGINE_NOT_FOUND;
2274 /* check if pid is valid */
2275 if (true == vcd_client_widget_is_available(pid)) {
2276 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] The pid is already exist");
2277 return VCD_ERROR_NONE;
2280 /* Add client information to client manager */
2281 if (0 != vcd_client_widget_add(pid)) {
2282 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to add client info");
2283 return VCD_ERROR_OPERATION_FAILED;
2286 SLOG(LOG_DEBUG, TAG_VCD, "[Server Success] Initialize widget : pid(%d)", pid);
2288 return VCD_ERROR_NONE;
2291 static void __vcd_server_widget_start_recording(void *data)
2293 SLOG(LOG_ERROR, TAG_VCD, "[Server INFO] start recording");
2295 if (0 != __start_internal_recognition()) {
2296 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to start recongition");
2300 int vcd_server_widget_finalize(int pid)
2302 /* check if pid is valid */
2303 if (false == vcd_client_widget_is_available(pid)) {
2304 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] pid is NOT valid ");
2305 return VCD_ERROR_INVALID_PARAMETER;
2308 /* Remove client information */
2309 if (0 != vcd_client_widget_delete(pid)) {
2310 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Fail to delete client");
2313 if (0 == vcd_client_get_ref_count()) {
2314 SLOG(LOG_ERROR, TAG_VCD, "[Server] connected client list is empty");
2315 ecore_timer_add(0, __finalize_quit_ecore_loop, NULL);
2319 bool is_waiting = false;
2320 if (0 != vcd_client_widget_get_waiting_for_recording(pid, &is_waiting)) {
2321 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Fail to get waiting to recording");
2324 if (true == is_waiting) {
2325 SLOG(LOG_ERROR, TAG_VCD, "[Server INFO] invoke to start recording");
2326 ecore_main_loop_thread_safe_call_async(__vcd_server_widget_start_recording, NULL);
2328 return VCD_ERROR_NONE;
2331 int vcd_server_widget_start_recording(int pid, bool widget_command)
2333 /* check if pid is valid */
2334 if (false == vcd_client_widget_is_available(pid)) {
2335 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] pid is NOT valid ");
2336 return VCD_ERROR_INVALID_PARAMETER;
2340 if (0 != vcd_client_widget_get_waiting_for_recording(pid, &waiting) || false == waiting) {
2341 SLOG(LOG_ERROR, TAG_VCD, "[Server] Server is not waiting for recording, pid(%d), waiting(%d)", pid, waiting);
2345 if (true == widget_command) {
2346 vcd_client_widget_set_command(pid);
2347 SLOG(LOG_DEBUG, TAG_VCD, "[Server] widget command is available");
2349 vcd_client_widget_unset_command(pid);
2350 SLOG(LOG_WARN, TAG_VCD, "[Server] widget command is NOT available");
2353 SLOG(LOG_ERROR, TAG_VCD, "[Server] start internal recongition : %d", widget_command);
2354 int ret = __start_internal_recognition();
2356 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to start recongition : %d", ret);
2357 ecore_timer_add(0, __vcd_request_show_tooltip, (void*)false);
2363 int vcd_server_widget_start(int pid, bool stop_by_silence)
2365 /* check if pid is valid */
2366 int fore_pid = vcd_client_widget_get_foreground_pid();
2367 if (pid != fore_pid) {
2368 SLOG(LOG_ERROR, TAG_VCD, "[Server] pid is NOT foreground");
2369 return VCD_ERROR_INVALID_PARAMETER;
2372 /* Check current state */
2373 vcd_state_e state = vcd_config_get_service_state();
2375 /* Service state should be ready */
2376 if (VCD_STATE_READY != state) {
2377 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Current state is not Ready : pid(%d)", pid);
2378 return VCD_ERROR_INVALID_STATE;
2381 vcd_client_set_slience_detection(stop_by_silence);
2383 /* Notify show tooltip */
2384 ecore_timer_add(0, __vcd_request_show_tooltip, (void*)true);
2389 int vcd_server_widget_stop(int pid)
2391 /* check if pid is valid */
2392 int fore_pid = vcd_client_widget_get_foreground_pid();
2393 if (pid != fore_pid) {
2394 SLOG(LOG_ERROR, TAG_VCD, "[Server] pid is NOT foreground");
2395 return VCD_ERROR_INVALID_PARAMETER;
2399 /* Check current state */
2400 vcd_state_e state = vcd_config_get_service_state();
2402 /* Service state should be recording */
2403 if (VCD_STATE_RECORDING != state) {
2404 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Current service state is not Recording : pid(%d)", pid);
2405 return VCD_ERROR_INVALID_STATE;
2408 ret = vcd_server_mgr_stop();
2410 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Fail to start recognition");
2411 return VCD_ERROR_OPERATION_FAILED;
2414 return VCD_ERROR_NONE;
2417 int vcd_server_widget_cancel(int pid)
2419 /* check if pid is valid */
2420 int fore_pid = vcd_client_widget_get_foreground_pid();
2421 if (pid != fore_pid) {
2422 SLOG(LOG_ERROR, TAG_VCD, "[Server] pid is NOT foreground");
2423 return VCD_ERROR_INVALID_PARAMETER;
2427 /* Check current state */
2428 vcd_state_e state = vcd_config_get_service_state();
2430 /* Service state should be recording or processing */
2431 if (VCD_STATE_RECORDING != state && VCD_STATE_PROCESSING != state) {
2432 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Current state is not Recording or Processing : pid(%d)", pid);
2433 return VCD_ERROR_INVALID_STATE;
2436 ret = vcd_server_mgr_cancel();
2438 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Fail to cancel recognition : %d", ret);
2442 return VCD_ERROR_NONE;
2445 int vcd_server_widget_enable_asr_result(int pid, bool enable)
2448 ret = vcd_client_widget_set_asr_result_enabled(pid, enable);
2450 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Fail to enable asr result : %d", ret);
2456 int vcd_server_set_language(const char* language)
2458 int ret = VCD_ERROR_NONE;
2460 ret = vcd_config_set_default_language(language);
2462 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Fail to set language : %d", ret);
2466 ret = vcd_engine_set_current_language(language);
2468 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Fail to set language : %d", ret);