2 * Copyright (c) 2011-2015 Samsung Electronics Co., Ltd All Rights Reserved
4 * Licensed under the Apache License, Version 2.0 (the License);
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an AS IS BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
18 #include <app_manager.h>
20 #include <sound_manager.h>
22 #include "vc_cmd_db.h"
23 #include "vc_info_parser.h"
25 #include "vcd_server.h"
26 #include "vcd_client_data.h"
28 #include "vcd_engine_agent.h"
29 #include "vcd_config.h"
30 #include "vcd_recorder.h"
33 #include "voice_control_command_expand.h"
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;
55 * VC Server Internal Functions
57 static Eina_Bool __stop_by_silence(void *data)
59 SLOG(LOG_DEBUG, TAG_VCD, "===== Silence Detected ");
61 vcd_server_mgr_stop();
63 SLOG(LOG_DEBUG, TAG_VCD, "=====");
64 SLOG(LOG_DEBUG, TAG_VCD, " ");
68 static Eina_Bool __cancel_by_interrupt(void *data)
70 SLOG(LOG_DEBUG, TAG_VCD, "===== Cancel by interrupt");
72 vcd_server_mgr_cancel();
74 SLOG(LOG_DEBUG, TAG_VCD, "=====");
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, "=====");
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, "=====");
113 SLOG(LOG_DEBUG, TAG_VCD, " ");
117 static int __server_recorder_callback(const void* data, const unsigned int length)
119 vcd_state_e state = vcd_config_get_service_state();
120 if (VCD_STATE_READY == state) {
121 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Ready state, but recording");
122 } else if (VCD_STATE_PROCESSING == state) {
126 vcp_speech_detect_e speech_detected = VCP_SPEECH_DETECT_NONE;
129 ret = vcd_engine_recognize_audio(data, length, &speech_detected);
133 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to set recording data to engine(%d)", ret);
134 ecore_timer_add(0, __cancel_by_interrupt, NULL);
135 /* Send error cb to manager */
136 if (VCP_ERROR_OUT_OF_NETWORK == ret) {
137 vcdc_send_error_signal_to_manager(vcd_client_manager_get_pid(), VCD_ERROR_TIMED_OUT, "Engine connection failed");
139 vcdc_send_error_signal_to_manager(vcd_client_manager_get_pid(), VCD_ERROR_OPERATION_FAILED, "Engine recognition failed");
144 if (VCP_SPEECH_DETECT_BEGIN == speech_detected) {
145 if (-1 != vcd_client_manager_get_pid()) {
146 /* Manager client is available */
147 if (0 != vcdc_send_speech_detected(vcd_client_manager_get_pid())) {
148 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Fail to send speech detected");
151 } else if (VCP_SPEECH_DETECT_END == speech_detected) {
152 if (VCD_RECOGNITION_MODE_STOP_BY_SILENCE == vcd_client_get_recognition_mode()) {
153 /* silence detected */
154 ecore_timer_add(0, __stop_by_silence, NULL);
155 } else if (VCD_RECOGNITION_MODE_RESTART_AFTER_REJECT == vcd_client_get_recognition_mode()) {
156 /* Stop engine recognition */
157 int ret = vcd_engine_recognize_stop();
159 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to stop recognition : %d", ret);
161 vcd_config_set_service_state(VCD_STATE_PROCESSING);
162 vcdc_send_service_state(VCD_STATE_PROCESSING);
164 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Stop engine only by silence");
165 } else if (VCD_RECOGNITION_MODE_RESTART_CONTINUOUSLY == vcd_client_get_recognition_mode()) {
166 /* Stop engine recognition */
167 int ret = vcd_engine_recognize_stop();
169 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to stop recognition : %d", ret);
177 void __server_recorder_interrupt_callback()
179 SLOG(LOG_DEBUG, TAG_VCD, "===== Cancel by sound interrupt");
181 ecore_timer_add(0, __cancel_by_interrupt, NULL);
183 SLOG(LOG_DEBUG, TAG_VCD, "=====");
184 SLOG(LOG_DEBUG, TAG_VCD, " ");
187 static void __config_lang_changed_cb(const char* current_lang, void* user_data)
189 SLOG(LOG_DEBUG, TAG_VCD, "===== Change language ");
191 /* Current state is recording */
192 vcd_state_e state = vcd_config_get_service_state();
193 if (VCD_STATE_RECORDING == state || VCD_STATE_PROCESSING == state) {
194 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Current state is 'Recording'. Cancel recognition");
195 vcd_server_mgr_cancel();
199 ret = vcd_engine_set_current_language(current_lang);
201 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to set language of engine : %d", ret);
204 SLOG(LOG_DEBUG, TAG_VCD, "=====");
205 SLOG(LOG_DEBUG, TAG_VCD, " ");
210 static void __config_foreground_changed_cb(int previous, int current, void* user_data)
212 SLOG(LOG_DEBUG, TAG_VCD, "===== Change foreground");
214 SLOG(LOG_DEBUG, TAG_VCD, "Foreground pid(%d)", current);
216 if (VC_NO_FOREGROUND_PID != current) {
217 /* Foreground app is changed */
218 vcd_state_e state = vcd_config_get_service_state();
219 if (VCD_STATE_RECORDING == state) {
220 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Foreground pid(%d) is changed. Cancel recognition", current);
221 ecore_timer_add(0, __cancel_by_interrupt, NULL);
225 SLOG(LOG_DEBUG, TAG_VCD, "=====");
226 SLOG(LOG_DEBUG, TAG_VCD, " ");
231 static int __vcd_activate_app_by_appcontrol(const char* appid)
234 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Invalid parameter");
235 return VCD_ERROR_INVALID_PARAMETER;
239 app_control_h app_control = NULL;
240 ret = app_control_create(&app_control);
241 if (APP_CONTROL_ERROR_NONE == ret) {
243 ret = app_control_add_extra_data(app_control, "voice_launch", "get_result");
244 if (APP_CONTROL_ERROR_NONE != ret) {
245 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Fail to add extra data, ret(%d)", ret);
246 app_control_destroy(app_control);
247 return VCD_ERROR_OPERATION_FAILED;
250 ret = app_control_set_app_id(app_control, appid);
251 if (APP_CONTROL_ERROR_NONE != ret) {
252 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Fail to set app id, ret(%d)", ret);
253 app_control_destroy(app_control);
254 return VCD_ERROR_OPERATION_FAILED;
256 // Sent launch request
257 ret = app_control_send_launch_request(app_control, NULL, NULL);
258 if (APP_CONTROL_ERROR_NONE != ret) {
259 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Fail to send launch request, ret(%d)", ret);
260 app_control_destroy(app_control);
261 return VCD_ERROR_OPERATION_FAILED;
263 // Destroy app control
264 ret = app_control_destroy(app_control);
265 if (APP_CONTROL_ERROR_NONE != ret) {
266 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Fail to destroy, ret(%d)", ret);
267 return VCD_ERROR_OPERATION_FAILED;
270 return VCD_ERROR_NONE;
273 static int __vcd_resume_app(const char* appid)
275 app_context_h app_context = NULL;
276 int ret = app_manager_get_app_context(appid, &app_context);
277 if (APP_MANAGER_ERROR_NONE != ret) {
278 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Fail to get app_context, ret(%d), appid(%s)", ret, appid);
279 return VCD_ERROR_OPERATION_FAILED;
282 ret = app_manager_resume_app(app_context);
283 if (APP_MANAGER_ERROR_NONE != ret) {
284 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Fail to resume app, ret(%d), appid(%s)", ret, appid);
285 return VCD_ERROR_OPERATION_FAILED;
287 return VCD_ERROR_NONE;
290 static bool __vcd_is_package_installed(const char* appid)
292 app_info_h app_info = NULL;
293 int ret = app_manager_get_app_info(appid, &app_info);
294 if (APP_MANAGER_ERROR_NONE != ret || NULL == app_info)
296 ret = app_info_destroy(app_info);
297 if (APP_MANAGER_ERROR_NONE != ret)
298 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Fail to destroy app_info, ret(%d)", ret);
302 static bool __vcd_launch_app(const char* result)
304 if (NULL == result) {
305 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Invalid parameter");
306 return VCD_ERROR_INVALID_PARAMETER;
309 GSList* app_list = NULL;
310 if (0 != vc_db_get_appid_list(result, &app_list)) {
311 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] result text is NULL");
312 return VCD_ERROR_INVALID_PARAMETER;
315 if (0 != g_slist_length(app_list)) {
318 vc_deactivated_app_s* temp_app = NULL;
319 iter = g_slist_nth(app_list, 0);
321 while (NULL != iter) {
322 temp_app = iter->data;
324 if (NULL != temp_app) {
325 if (NULL != temp_app->appid) {
327 bool running = false;
328 ret = app_manager_is_running(temp_app->appid, &running);
329 if (APP_MANAGER_ERROR_NONE != ret) {
330 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Fail to check running with appid(%s)", temp_app->appid);
331 free(temp_app->appid);
332 temp_app->appid = NULL;
335 return VCD_ERROR_OPERATION_FAILED;
337 if (false == running) {
338 int tmp_ret = __vcd_is_package_installed(temp_app->appid);
339 if (false == tmp_ret) {
340 SLOG(LOG_WARN, TAG_VCD, "[WARNING] app is not installed, appid(%s)", temp_app->appid);
342 ret = __vcd_activate_app_by_appcontrol(temp_app->appid);
344 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Fail to activate app");
345 free(temp_app->appid);
346 temp_app->appid = NULL;
351 SLOG(LOG_ERROR, TAG_VCD, "Launch app: appid(%s) result(%s)", temp_app->appid, result);
354 ret = __vcd_resume_app(temp_app->appid);
356 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Fail to resume app");
357 free(temp_app->appid);
358 temp_app->appid = NULL;
363 SLOG(LOG_ERROR, TAG_VCD, "Resume app: appid(%s) result(%s)", temp_app->appid, result);
365 free(temp_app->appid);
366 temp_app->appid = NULL;
371 iter = g_slist_next(iter);
376 return VCD_ERROR_NONE;
379 static Eina_Bool __vcd_send_selected_result(void *data)
381 GSList* pid_list = NULL;
382 const char* result = vcd_client_manager_get_result_text();
384 if (0 != vc_info_parser_get_result_pid_list(&pid_list, result)) {
385 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Fail to get pid list. No result");
387 if (0 < g_slist_length(pid_list)) {
389 vc_cmd_s* temp_cmd = NULL;
394 iter = g_slist_nth(pid_list, 0);
395 while (NULL != iter) {
396 temp_cmd = iter->data;
398 if (NULL != temp_cmd && (pre_pid != temp_cmd->pid || pre_type == VC_COMMAND_TYPE_WIDGET || temp_cmd->type == VC_COMMAND_TYPE_WIDGET)) {
399 /* Launch deactivated several apps that is matched with result */
400 ret = __vcd_launch_app(result);
402 SLOG(LOG_ERROR, TAG_VCD, "Fail to launch or resume app, ret(%d) result(%s)", ret, result);
404 /* send result noti */
405 ret = vcdc_send_result(temp_cmd->pid, vcd_client_manager_get_pid(), temp_cmd->type);
407 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Fail to send result, ret(%d)", ret);
410 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Send result : pid(%d) type(%d)", temp_cmd->pid, temp_cmd->type);
411 pre_pid = temp_cmd->pid;
412 pre_type = temp_cmd->type;
418 pid_list = g_slist_remove_link(pid_list, iter);
419 iter = g_slist_nth(pid_list, 0);
424 if (VCD_RECOGNITION_MODE_RESTART_CONTINUOUSLY != vcd_client_get_recognition_mode()) {
425 vcd_config_set_service_state(VCD_STATE_READY);
426 vcdc_send_service_state(VCD_STATE_READY);
432 //static void __vcd_server_pre_result_cb(vcp_pre_result_event_e event, const char* pre_result, void *user_data)
433 static void __vcd_server_asr_result_cb(vcp_asr_result_event_e event, const char* asr_result, void *user_data)
435 if (NULL != asr_result) {
436 SLOG(LOG_DEBUG, TAG_VCD, "[Server] ASR result - Event(%d), Text(%s)", event, asr_result);
437 vcdc_send_pre_result_to_manager(vcd_client_manager_get_pid(), event, asr_result);
443 static void* __recorder_stop(void *data)
449 static void __vcd_server_result_cb(vcp_result_event_e event, int* result_id, int count, const char* all_result,
450 const char* non_fixed_result, const char* nlu_result, const char* msg, void *user_data)
452 if (VCD_STATE_PROCESSING != vcd_config_get_service_state()) {
453 if (VCD_RECOGNITION_MODE_RESTART_CONTINUOUSLY != vcd_client_get_recognition_mode()) {
454 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Current state is not 'Processing' and mode is not 'Restart continuously'");
459 vc_info_parser_unset_result(vcd_client_manager_get_exclusive());
460 vcd_client_manager_set_result_text(all_result);
462 SLOG(LOG_INFO, TAG_VCD, "[Server] Event(%d), Text(%s) Nonfixed(%s) Msg(%s) Result count(%d)",
463 event, all_result, non_fixed_result, msg, count);
465 if (VCD_RECOGNITION_MODE_RESTART_AFTER_REJECT == vcd_client_get_recognition_mode()) {
466 if (VCP_RESULT_EVENT_REJECTED == event) {
467 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Restart by no or rejected result");
468 /* If no result and restart option is ON */
469 /* Send reject message */
470 bool temp = vcd_client_manager_get_exclusive();
471 vc_info_parser_set_result(all_result, event, msg, NULL, temp);
472 if (0 != vcdc_send_result_to_manager(vcd_client_manager_get_pid(), VC_RESULT_TYPE_NOTIFICATION)) {
473 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Fail to send result");
476 g_restart_timer = ecore_timer_add(0, __restart_engine, NULL);
479 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Stop recorder due to success");
480 //vcd_recorder_stop();
481 ecore_main_loop_thread_safe_call_sync(__recorder_stop, NULL);
482 } else if (VCD_RECOGNITION_MODE_RESTART_CONTINUOUSLY == vcd_client_get_recognition_mode()) {
483 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Restart continuously");
484 /* Restart option is ON */
485 g_restart_timer = ecore_timer_add(0, __restart_engine, NULL);
486 if (VCP_RESULT_EVENT_REJECTED == event) {
487 bool temp = vcd_client_manager_get_exclusive();
488 vc_info_parser_set_result(all_result, event, msg, NULL, temp);
489 if (0 != vcdc_send_result_to_manager(vcd_client_manager_get_pid(), VC_RESULT_TYPE_NOTIFICATION)) {
490 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Fail to send result");
497 int pid = vcd_client_widget_get_foreground_pid();
499 if (NULL != all_result) {
500 vc_info_parser_set_result(all_result, event, msg, NULL, false);
502 vcd_client_widget_get_asr_result_enabled(pid, &enable);
503 if (true == enable) {
504 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Send ASR result to Widget client");
505 bool is_consumed = false;
506 if (0 != vcdc_send_asr_result(pid, VC_COMMAND_TYPE_WIDGET, &is_consumed)) {
507 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to send asr result");
509 SLOG(LOG_DEBUG, TAG_VCD, "[Server] ASR result is consumed(%d)", is_consumed);
510 if (true == is_consumed) {
511 vcdc_send_show_tooltip(pid, false);
512 if (-1 != vcd_client_manager_get_pid()) {
513 /* Manager client is available */
514 vc_info_parser_unset_result(false);
515 vc_info_parser_set_result(all_result, VC_RESULT_EVENT_RESULT_SUCCESS, msg, NULL, false);
516 if (0 != vcdc_send_result_to_manager(vcd_client_manager_get_pid(), VC_RESULT_TYPE_NOTIFICATION)) {
517 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Fail to send result");
521 vcd_client_manager_set_exclusive(false);
523 vcd_config_set_service_state(VCD_STATE_READY);
524 vcdc_send_service_state(VCD_STATE_READY);
532 /* if nlu_result is exist, Add command handle(is_action) into result list */
534 SLOG(LOG_DEBUG, TAG_VCD, "[Server] === Get engine result ===");
537 vc_cmd_s* temp_cmd = NULL;
538 vc_cmd_list_h vc_cmd_list = NULL;
540 if (0 != vc_cmd_list_create(&vc_cmd_list)) {
541 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Fail to create command list");
542 vcd_client_manager_set_exclusive(false);
543 vcd_config_set_service_state(VCD_STATE_READY);
544 vcdc_send_service_state(VCD_STATE_READY);
548 /* priority filter */
549 /* system > exclusive > foreground = widget > system_background > background */
551 int* filtered_id = (int*)calloc(count, sizeof(int));
552 int filtered_count = 0;
553 int top_priority = VC_COMMAND_PRIORITY_BACKGROUND;
554 for (i = 0; i < count; i++) {
555 SLOG(LOG_INFO, TAG_VCD, "[Server] [%d] Result id(%d)", i, result_id[i]);
557 if (0 > result_id[i]) {
558 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Result ID(%d) is NOT valid", result_id[i]);
562 ret = vcd_client_get_cmd_from_result_id(result_id[i], &temp_cmd);
563 if (0 == ret && NULL != temp_cmd) {
564 if (top_priority == temp_cmd->priority) {
565 filtered_id[filtered_count] = result_id[i];
567 } else if (top_priority < temp_cmd->priority) {
570 filtered_id[0] = result_id[i];
572 top_priority = temp_cmd->priority;
578 for (i = 0; i < filtered_count; i++) {
579 SLOG(LOG_INFO, TAG_VCD, "[Server] [%d] Filtered Result id(%d)", i, filtered_id[i]);
581 if (filtered_id[i] < 0) {
582 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Filtered ID(%d) is NOT valid", filtered_id[i]);
586 ret = vcd_client_get_cmd_from_result_id(filtered_id[i], &temp_cmd);
587 if (0 == ret && NULL != temp_cmd) {
588 switch (temp_cmd->format) {
589 case VC_CMD_FORMAT_FIXED:
590 case VC_CMD_FORMAT_FIXED_AND_VFIXED:
591 case VC_CMD_FORMAT_VFIXED_AND_FIXED:
592 case VC_CMD_FORMAT_PARTIAL:
593 case VC_CMD_FORMAT_FIXED_AND_NONFIXED:
594 case VC_CMD_FORMAT_NONFIXED_AND_FIXED:
596 case VC_CMD_FORMAT_ACTION:
600 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Unknown command type : %d", temp_cmd->type);
604 if (0 != vc_cmd_list_add(vc_cmd_list, (vc_cmd_h)temp_cmd)) {
605 SLOG(LOG_DEBUG, TAG_VCD, "Fail to add command to list");
606 vc_cmd_destroy((vc_cmd_h)temp_cmd);
609 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] NOT found matached result(%d)", filtered_id[i]);
613 if (NULL != filtered_id) {
618 if (NULL != nlu_result) {
619 SLOG(LOG_INFO, TAG_VCD, "[Server] NLU (%s)", nlu_result);
620 vc_info_parser_set_nlu_result(nlu_result);
621 if (0 == is_action) {
623 if (0 != vc_cmd_create(&nlu_cmd)) {
624 SLOG(LOG_ERROR, TAG_VCD, "Fail to nlu cmd create");
626 if (0 != vc_cmd_set_type(nlu_cmd, VC_COMMAND_TYPE_SYSTEM)) {
627 SLOG(LOG_ERROR, TAG_VCD, "Fail to set type");
629 if (0 != vc_cmd_set_pid(nlu_cmd, vcd_client_manager_get_pid())) {
630 SLOG(LOG_ERROR, TAG_VCD, "Fail to set pid");
632 if (0 != vc_cmd_set_format(nlu_cmd, VC_CMD_FORMAT_ACTION)) {
633 SLOG(LOG_ERROR, TAG_VCD, "Fail to set format");
635 if (0 != vc_cmd_list_add(vc_cmd_list, nlu_cmd)) {
636 SLOG(LOG_ERROR, TAG_VCD, "Fail to add nlu cmd to list");
637 vc_cmd_destroy(nlu_cmd);
643 vc_cmd_print_list(vc_cmd_list);
645 SLOG(LOG_DEBUG, TAG_VCD, "[Server] =========================");
647 int result_count = 0;
648 vc_cmd_list_get_count(vc_cmd_list, &result_count);
650 if (0 == result_count) {
652 if (NULL != all_result) {
653 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Engine result is no command : %s", all_result);
655 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Engine result is NULL");
657 bool temp = vcd_client_manager_get_exclusive();
658 vc_info_parser_set_result(all_result, event, msg, NULL, temp);
660 int pid = vcd_client_widget_get_foreground_pid();
662 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Request tooltip hide");
663 /* Send to hide tooltip */
664 vcdc_send_show_tooltip(pid, false);
667 if (-1 != vcd_client_manager_get_pid()) {
668 /* Manager client is available */
669 if (0 != vcdc_send_result_to_manager(vcd_client_manager_get_pid(), VC_RESULT_TYPE_NORMAL)) {
670 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Fail to send result");
674 vcd_client_manager_set_exclusive(false);
678 if (false == vcd_client_manager_get_exclusive()) {
679 int pid = vcd_client_widget_get_foreground_pid();
681 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Request tooltip hide");
682 vcdc_send_show_tooltip(pid, false);
685 vc_info_parser_set_result(all_result, event, msg, vc_cmd_list, false);
687 if (-1 != vcd_client_manager_get_pid()) {
688 /* Manager client is available */
689 if (0 != vcdc_send_result_to_manager(vcd_client_manager_get_pid(), VC_RESULT_TYPE_NORMAL)) {
690 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Fail to send result");
693 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Manager is NOT available. Send result to client directly");
694 /* Send result to client */
695 ecore_timer_add(0, __vcd_send_selected_result, NULL);
698 /* exclusive command */
699 vc_info_parser_set_result(all_result, event, msg, vc_cmd_list, true);
701 if (-1 != vcd_client_manager_get_pid()) {
702 /* Manager client is available */
703 if (0 != vcdc_send_result_to_manager(vcd_client_manager_get_pid(), VC_RESULT_TYPE_NORMAL)) {
704 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Fail to send result");
707 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Manager is NOT available");
710 vcd_client_manager_set_exclusive(false);
714 vc_cmd_list_destroy(vc_cmd_list, true);
720 if (NULL == result_id) {
722 if (NULL != all_result) {
723 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Engine result is no command : %s", all_result);
724 bool temp = vcd_client_manager_get_exclusive();
725 vc_info_parser_set_result(all_result, event, msg, NULL, temp);
728 int pid = vcd_client_widget_get_foreground_pid();
730 if (NULL != all_result) {
731 /* Send result text to widget */
732 vcdc_send_result(pid, vcd_client_manager_get_pid(), VC_COMMAND_TYPE_WIDGET);
735 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Request tooltip hide");
736 /* Send to hide tooltip */
737 vcdc_send_show_tooltip(pid, false);
740 if (-1 != vcd_client_manager_get_pid()) {
741 /* Manager client is available */
742 if (0 != vcdc_send_result_to_manager(vcd_client_manager_get_pid(), VC_RESULT_TYPE_NORMAL)) {
743 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Fail to send result");
747 vcd_client_manager_set_exclusive(false);
753 SLOG(LOG_DEBUG, TAG_VCD, "[Server] === Get engine result ===");
756 vc_cmd_s* temp_cmd = NULL;
757 vc_cmd_list_h vc_cmd_list = NULL;
759 if (0 != vc_cmd_list_create(&vc_cmd_list)) {
760 SLOG(LOG_ERROR, TAG_VCD, "[Server] Fail to create command list");
761 vcd_client_manager_set_exclusive(false);
762 vcd_config_set_service_state(VCD_STATE_READY);
763 vcdc_send_service_state(VCD_STATE_READY);
768 for (i = 0; i < count; i++) {
769 SLOG(LOG_DEBUG, TAG_VCD, "[Server] [%d] Result ID(%d)", i, result_id[i]);
771 if (result_id[i] < 0) {
772 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Result ID(%d) is NOT valid", result_id[i]);
776 ret = vcd_client_get_cmd_from_result_id(result_id[i], &temp_cmd);
777 if (0 == ret && NULL != temp_cmd) {
778 switch (temp_cmd->format) {
779 case VC_CMD_FORMAT_FIXED:
780 case VC_CMD_FORMAT_FIXED_AND_VFIXED:
781 case VC_CMD_FORMAT_VFIXED_AND_FIXED:
782 case VC_CMD_FORMAT_PARTIAL:
783 /* Nonfixed result is NOT valid */
785 case VC_CMD_FORMAT_FIXED_AND_NONFIXED:
786 if (NULL == temp_cmd->parameter) {
787 if (NULL != non_fixed_result) {
788 temp_cmd->parameter = strdup(non_fixed_result);
791 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Command type is NOT vaild. Parameter (%s)", temp_cmd->parameter);
794 case VC_CMD_FORMAT_NONFIXED_AND_FIXED:
795 if (NULL == temp_cmd->command) {
796 if (NULL != non_fixed_result) {
797 temp_cmd->command = strdup(non_fixed_result);
800 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Command type is NOT vaild. Command (%s)", temp_cmd->command);
805 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Unknown command type : %d", temp_cmd->type);
809 if (0 != vc_cmd_list_add(vc_cmd_list, (vc_cmd_h)temp_cmd)) {
810 SLOG(LOG_DEBUG, TAG_VCD, "Fail to add command to list");
811 vc_cmd_destroy((vc_cmd_h)temp_cmd);
814 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] NOT found matached result(%d)", result_id[i]);
818 vc_cmd_print_list(vc_cmd_list);
820 SLOG(LOG_DEBUG, TAG_VCD, "[Server] =========================");
822 int result_count = 0;
823 vc_cmd_list_get_count(vc_cmd_list, &result_count);
825 if (false == vcd_client_manager_get_exclusive()) {
826 int pid = vcd_client_widget_get_foreground_pid();
828 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Request tooltip hide");
829 vcdc_send_show_tooltip(pid, false);
832 vc_info_parser_set_result(all_result, event, msg, vc_cmd_list, false);
834 if (-1 != vcd_client_manager_get_pid()) {
835 /* Manager client is available */
836 if (0 != vcdc_send_result_to_manager(vcd_client_manager_get_pid(), VC_RESULT_TYPE_NORMAL)) {
837 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Fail to send result");
840 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Manager is NOT available. Send result to client directly");
841 /* Send result to client */
842 ecore_timer_add(0, __vcd_send_selected_result, NULL);
845 /* exclusive command */
846 vc_info_parser_set_result(all_result, event, msg, vc_cmd_list, true);
848 if (-1 != vcd_client_manager_get_pid()) {
849 /* Manager client is available */
850 if (0 != vcdc_send_result_to_manager(vcd_client_manager_get_pid(), VC_RESULT_TYPE_NORMAL)) {
851 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Fail to send result");
854 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Manager is NOT available");
857 vcd_client_manager_set_exclusive(false);
860 vc_cmd_list_destroy(vc_cmd_list, true);
867 static void __vcd_server_nlu_result_cb(vcp_result_event_e event, const char* nlu_result, void *user_data)
869 SLOG(LOG_DEBUG, TAG_VCD, "[Server] NLU result cb - event(%d)", event);
870 SLOG(LOG_DEBUG, TAG_VCD, "[Server] result (%s)", nlu_result);
872 int ret = vc_info_parser_set_nlu_result(nlu_result);
874 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to set nlu result (%d)", ret);
881 static void __vcd_server_error_cb(vcp_error_e error, const char* msg, void *user_data)
883 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Engine Error cb - reason(%d), msg(%s)", error, msg);
884 ecore_main_loop_thread_safe_call_async(__cancel_by_error, NULL);
886 char* error_msg = NULL;
887 error_msg = strdup(msg);
889 if (0 != vcdc_send_error_signal_to_manager(vcd_client_manager_get_pid(), error, error_msg)) {
890 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to send error signal");
893 if (NULL != error_msg) {
902 * vcd server Interfaces
904 static void __vcd_file_clean_up()
906 SLOG(LOG_DEBUG, TAG_VCD, "== Old file clean up == ");
909 struct dirent *dirp = NULL;
911 dp = opendir(VC_RUNTIME_INFO_ROOT);
913 SLOG(LOG_ERROR, TAG_VCD, "[File message WARN] Fail to open path : %s", VC_RUNTIME_INFO_ROOT);
917 char remove_path[256] = {0, };
922 if (!strncmp("vc_", dirp->d_name, strlen("vc_"))) {
923 memset(remove_path, 0, 256);
924 snprintf(remove_path, 256, "%s/%s", VC_RUNTIME_INFO_ROOT, dirp->d_name);
927 if (0 != remove(remove_path)) {
928 SLOG(LOG_WARN, TAG_VCD, "[File message WARN] Fail to remove file : %s", remove_path);
930 SLOG(LOG_DEBUG, TAG_VCD, "[File message] Remove file : %s", remove_path);
934 } while (NULL != dirp);
941 static int __vcd_db_clean_up()
944 int cnt = VC_COMMAND_TYPE_FOREGROUND;
946 if (VC_COMMAND_TYPE_BACKGROUND != cnt)
947 ret = vc_db_delete_commands(-1, cnt, NULL);
948 } while (VC_COMMAND_TYPE_EXCLUSIVE >= ++cnt);
957 /* Remove old file */
958 __vcd_file_clean_up();
960 /* initialize modules */
961 ret = vcd_config_initialize(__config_lang_changed_cb, __config_foreground_changed_cb, NULL);
963 SLOG(LOG_ERROR, TAG_VCD, "[Server WARNING] Fail to initialize config.");
967 ret = __vcd_db_clean_up();
969 SLOG(LOG_ERROR, TAG_VCD, "[Server WARNING] Fail to remove db data");
972 vcd_config_set_service_state(VCD_STATE_NONE);
974 //ret = vcd_engine_agent_init(__vcd_server_pre_result_cb, __vcd_server_result_cb, __vcd_server_nlu_result_cb, __vcd_server_error_cb);
975 ret = vcd_engine_agent_init(__vcd_server_asr_result_cb, __vcd_server_result_cb, __vcd_server_error_cb);
977 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to engine agent initialize : result(%d)", ret);
981 if (0 != vcd_recorder_create(__server_recorder_callback, __server_recorder_interrupt_callback)) {
982 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to create recorder");
983 return VCD_ERROR_OPERATION_FAILED;
987 ret = vcd_engine_agent_initialize_current_engine();
989 if (VCD_ERROR_ENGINE_NOT_FOUND == ret)
990 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] There is No Voice control engine");
992 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to init engine");
1000 if (0 != vcd_engine_agent_load_current_engine()) {
1001 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to load current engine");
1002 return VCD_ERROR_OPERATION_FAILED;
1005 /* Initialize manager info */
1006 vcd_client_manager_unset();
1008 vcd_config_set_service_state(VCD_STATE_READY);
1009 vcdc_send_service_state(VCD_STATE_READY);
1011 SLOG(LOG_DEBUG, TAG_VCD, "[Server SUCCESS] initialize");
1019 if (0 < g_list_length(g_proc_list)) {
1020 iter = g_list_first(g_proc_list);
1021 while (NULL != iter) {
1022 g_proc_list = g_list_remove_link(g_proc_list, iter);
1023 iter = g_list_first(g_proc_list);
1027 if (g_restart_timer != NULL) {
1028 ecore_timer_del(g_restart_timer);
1029 g_restart_timer = NULL;
1032 vcd_state_e state = vcd_config_get_service_state();
1033 if (VCD_STATE_READY != state) {
1034 if (VCD_STATE_RECORDING == state) {
1035 vcd_recorder_stop();
1037 vcd_engine_recognize_cancel();
1039 if (0 != vcd_recorder_destroy()) {
1040 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to destroy recorder");
1042 SLOG(LOG_DEBUG, TAG_VCD, "[Server] destroy recorder");
1045 if (0 != vcd_engine_agent_release()) {
1046 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to release engine");
1048 SLOG(LOG_DEBUG, TAG_VCD, "[Server] release engine");
1051 vcd_client_manager_unset_appid();
1053 vcd_config_set_service_state(VCD_STATE_NONE);
1054 vcdc_send_service_state(VCD_STATE_NONE);
1056 SLOG(LOG_DEBUG, TAG_VCD, "[Server] mode finalize");
1061 static Eina_Bool __finalize_quit_ecore_loop(void *data)
1063 SLOG(LOG_DEBUG, TAG_VCD, "[Server] quit ecore main loop");
1064 ecore_main_loop_quit();
1068 static void __read_proc()
1071 struct dirent *dirp = NULL;
1075 if (0 < g_list_length(g_proc_list)) {
1076 iter = g_list_first(g_proc_list);
1077 while (NULL != iter) {
1078 g_proc_list = g_list_remove_link(g_proc_list, iter);
1079 iter = g_list_first(g_proc_list);
1083 dp = opendir("/proc");
1085 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Fail to open proc");
1091 tmp = atoi(dirp->d_name);
1092 if (0 >= tmp) continue;
1093 g_proc_list = g_list_append(g_proc_list, GINT_TO_POINTER(tmp));
1095 } while (NULL != dirp);
1101 static void __vcd_cleanup_client(vcd_client_type_e type)
1103 int* client_list = NULL;
1104 int client_count = 0;
1111 if (VCD_CLIENT_TYPE_NORMAL == type) {
1112 ret = vcd_client_get_list(&client_list, &client_count);
1113 } else if (VCD_CLIENT_TYPE_WIDGET == type) {
1114 ret = vcd_client_widget_get_list(&client_list, &client_count);
1115 } else if (VCD_CLIENT_TYPE_MANAGER == type) {
1116 mgr_pid = vcd_client_manager_get_pid();
1117 client_list = &mgr_pid;
1119 if (-1 == mgr_pid) {
1120 SLOG(LOG_WARN, TAG_VCD, "[WARNING] Invalid Manager pid");
1125 if (0 == ret || mgr_pid > 0) {
1126 SLOG(LOG_DEBUG, TAG_VCD, "===== Clean up %s client ", type ? (type == 1) ? "Widget" : "Manager" : "Normal");
1127 if (NULL != client_list && client_count > 0) {
1128 for (i = 0; i < client_count; i++) {
1131 for (j = 0; j < g_list_length(g_proc_list); j++) {
1132 iter = g_list_nth(g_proc_list, j);
1134 if (client_list[i] == GPOINTER_TO_INT(iter->data)) {
1135 SLOG(LOG_DEBUG, TAG_VCD, "%s pid(%d) is running", type ? (type == 1) ? "Widget" : "Manager" : "Normal", client_list[i]);
1142 if (false == exist) {
1143 SLOG(LOG_ERROR, TAG_VCD, "%s pid(%d) should be removed", type ? (type == 1) ? "Widget" : "Manager" : "Normal", client_list[i]);
1144 if (VCD_CLIENT_TYPE_NORMAL == type)
1145 vcd_server_finalize(client_list[i]);
1146 else if (VCD_CLIENT_TYPE_WIDGET == type)
1147 vcd_server_widget_finalize(client_list[i]);
1149 vcd_server_mgr_finalize(mgr_pid);
1153 SLOG(LOG_DEBUG, TAG_VCD, "=====");
1154 SLOG(LOG_DEBUG, TAG_VCD, " ");
1156 if (NULL != client_list && -1 == mgr_pid) {
1163 Eina_Bool vcd_cleanup_client_all(void *data)
1167 __vcd_cleanup_client(VCD_CLIENT_TYPE_NORMAL);
1168 __vcd_cleanup_client(VCD_CLIENT_TYPE_WIDGET);
1169 __vcd_cleanup_client(VCD_CLIENT_TYPE_MANAGER);
1172 if (0 == vcd_client_get_list(&client_list, &client_count)) {
1173 SLOG(LOG_DEBUG, TAG_VCD, "===== Clean up client ");
1174 if (NULL != client_list && client_count > 0) {
1175 for (i = 0; i < client_count; i++) {
1178 for (j = 0; j < g_list_length(g_proc_list); j++) {
1179 iter = g_list_nth(g_proc_list, j);
1181 if (client_list[i] == GPOINTER_TO_INT(iter->data)) {
1182 SLOG(LOG_DEBUG, TAG_VCD, "pid(%d) is running", client_list[i]);
1189 if (false == exist) {
1190 SLOG(LOG_ERROR, TAG_VCD, "pid(%d) should be removed", client_list[i]);
1191 vcd_server_finalize(client_list[i]);
1194 result = vcdc_send_hello(client_list[i], VCD_CLIENT_TYPE_NORMAL);
1197 SLOG(LOG_DEBUG, TAG_VCD, "[Server] pid(%d) should be removed.", client_list[i]);
1198 vcd_server_finalize(client_list[i]);
1199 } else if (-1 == result) {
1200 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Hello result has error");
1205 SLOG(LOG_DEBUG, TAG_VCD, "=====");
1206 SLOG(LOG_DEBUG, TAG_VCD, " ");
1208 if (NULL != client_list) {
1213 /* If app is in background state, app cannot response message. */
1214 if (0 == vcd_client_widget_get_list(&client_list, &client_count)) {
1215 SLOG(LOG_DEBUG, TAG_VCD, "===== Clean up widget");
1216 if (NULL != client_list && client_count > 0) {
1217 for (i = 0; i < client_count; i++) {
1220 for (j = 0; j < g_list_length(g_proc_list); j++) {
1221 iter = g_list_nth(g_proc_list, j);
1223 if (client_list[i] == GPOINTER_TO_INT(iter->data)) {
1224 SLOG(LOG_DEBUG, TAG_VCD, "widget pid(%d) is running", client_list[i]);
1231 if (false == exist) {
1232 SLOG(LOG_ERROR, TAG_VCD, "widget pid(%d) should be removed", client_list[i]);
1233 vcd_server_widget_finalize(client_list[i]);
1236 result = vcdc_send_hello(client_list[i], VCD_CLIENT_TYPE_WIDGET);
1239 SLOG(LOG_DEBUG, TAG_VCD, "[Server] widget pid(%d) should be removed.", client_list[i]);
1240 vcd_server_widget_finalize(client_list[i]);
1241 } else if (-1 == result) {
1242 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Hello result has error");
1247 SLOG(LOG_DEBUG, TAG_VCD, "=====");
1248 SLOG(LOG_DEBUG, TAG_VCD, " ");
1251 if (NULL != client_list) {
1259 int mgr_pid = vcd_client_manager_get_pid();
1261 for (j = 0; j < g_list_length(g_proc_list); j++) {
1262 iter = g_list_nth(g_proc_list, j);
1264 if (mgr_pid == GPOINTER_TO_INT(iter->data)) {
1265 SLOG(LOG_DEBUG, TAG_VCD, "manager pid(%d) is running", mgr_pid);
1272 if (false == exist) {
1273 SLOG(LOG_ERROR, TAG_VCD, "manager pid (%d) should be removed", mgr_pid);
1274 vcd_server_mgr_finalize(mgr_pid);
1281 int vcd_server_get_service_state()
1283 return vcd_config_get_service_state();
1286 int vcd_server_get_foreground()
1289 vcd_config_get_foreground(&pid);
1297 int vcd_server_mgr_initialize(int pid)
1299 if (false == g_is_engine) {
1300 if (0 != vcd_engine_agent_initialize_current_engine()) {
1301 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] No Engine");
1302 g_is_engine = false;
1303 return VCD_ERROR_ENGINE_NOT_FOUND;
1309 /* check if pid is valid */
1310 if (false == vcd_client_manager_is_valid(pid)) {
1311 SLOG(LOG_ERROR, TAG_VCD, "[Server] old manager pid(%d) be removed", vcd_client_manager_get_pid());
1312 vcd_server_mgr_cancel();
1313 vcd_client_manager_unset();
1316 /* Add client information to client manager */
1317 if (0 != vcd_client_manager_set(pid)) {
1318 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to add manager");
1319 return VCD_ERROR_OPERATION_FAILED;
1322 if (0 != vcdc_send_manager_pid(pid))
1323 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Fail to send manager pid");
1325 SLOG(LOG_DEBUG, TAG_VCD, "[Server Success] Manager initialize : pid(%d)", pid);
1327 return VCD_ERROR_NONE;
1330 int vcd_server_mgr_finalize(int pid)
1332 /* check if pid is valid */
1333 if (false == vcd_client_manager_is_valid(pid)) {
1334 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] The manager pid(%d) is NOT valid", pid);
1335 return VCD_ERROR_INVALID_PARAMETER;
1338 /* Cancel recognition */
1339 vcd_server_mgr_cancel();
1341 if (0 != vcdc_send_manager_pid(-1))
1342 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Fail to send manager pid");
1344 /* Remove manager information */
1345 if (0 != vcd_client_manager_unset()) {
1346 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Fail to delete client");
1349 if (0 == vcd_client_get_ref_count()) {
1350 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Connected client list is empty");
1351 ecore_timer_add(0, __finalize_quit_ecore_loop, NULL);
1354 SLOG(LOG_DEBUG, TAG_VCD, "[Server Success] Manager Finalize : pid(%d)", pid);
1356 return VCD_ERROR_NONE;
1359 int vcd_server_mgr_set_command(int pid)
1361 if (0 != vcd_client_manager_set_command(pid)) {
1362 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] The manager pid(%d) is NOT valid", pid);
1363 return VCD_ERROR_INVALID_PARAMETER;
1365 return VCD_ERROR_NONE;
1368 int vcd_server_mgr_unset_command(int pid)
1370 if (0 != vcd_client_manager_unset_command(pid)) {
1371 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] The manager pid(%d) is NOT valid", pid);
1372 return VCD_ERROR_INVALID_PARAMETER;
1374 return VCD_ERROR_NONE;
1377 int vcd_server_mgr_set_demandable_client(int pid)
1379 /* check if pid is valid */
1380 if (false == vcd_client_manager_is_valid(pid)) {
1381 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] The manager pid(%d) is NOT valid", pid);
1382 return VCD_ERROR_INVALID_PARAMETER;
1385 GSList* client_list = NULL;
1386 if (0 != vc_info_parser_get_demandable_clients(&client_list)) {
1387 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to get demandable client");
1388 return VCD_ERROR_OPERATION_FAILED;
1391 /* Save client list */
1392 if (0 != vcd_client_manager_set_demandable_client(pid, client_list)) {
1393 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to set demandable client list");
1394 return VCD_ERROR_OPERATION_FAILED;
1397 return VCD_ERROR_NONE;
1400 int vcd_server_mgr_set_audio_type(int pid, const char* audio_type)
1402 /* check if pid is valid */
1403 if (false == vcd_client_manager_is_valid(pid)) {
1404 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] The manager pid(%d) is NOT valid", pid);
1405 return VCD_ERROR_INVALID_PARAMETER;
1409 vcp_audio_type_e type = VCP_AUDIO_TYPE_PCM_S16_LE;
1413 ret = vcd_engine_get_audio_format(audio_type, &type, &rate, &channel);
1415 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to get audio format : %d", ret);
1416 return VCD_ERROR_OPERATION_FAILED;
1419 ret = vcd_recorder_set(audio_type, type, rate, channel);
1421 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to set audio in type : %d", ret);
1422 return VCD_ERROR_OPERATION_FAILED;
1425 return VCD_ERROR_NONE;
1428 int vcd_server_mgr_get_audio_type(int pid, char** audio_type)
1430 /* check if pid is valid */
1431 if (false == vcd_client_manager_is_valid(pid)) {
1432 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] The manager pid(%d) is NOT valid", pid);
1433 return VCD_ERROR_INVALID_PARAMETER;
1436 int ret = vcd_recorder_get(audio_type);
1438 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to get audio in type : %d", ret);
1439 return VCD_ERROR_OPERATION_FAILED;
1442 return VCD_ERROR_NONE;
1445 int vcd_server_mgr_set_client_info(int pid)
1447 /* check if pid is valid */
1448 if (false == vcd_client_manager_is_valid(pid)) {
1449 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] The manager pid(%d) is NOT valid", pid);
1450 return VCD_ERROR_INVALID_PARAMETER;
1453 int ret = vcd_client_save_client_info();
1455 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to save client info : %d", ret);
1456 return VCD_ERROR_OPERATION_FAILED;
1459 return VCD_ERROR_NONE;
1462 static int __start_internal_recognition()
1466 if (0 != vcd_client_command_collect_command()) {
1467 SLOG(LOG_ERROR, TAG_VCD, "[Client Data ERROR] Fail to collect command");
1468 /* Send error cb to manager */
1469 int pid = vcd_client_widget_get_foreground_pid();
1471 vcdc_send_error_signal_to_manager(vcd_client_manager_get_pid(), VCD_ERROR_OPERATION_FAILED, "voice_engine.error.proc_fail");
1472 return VCD_ERROR_OPERATION_FAILED;
1475 /* 3. Set command to engine */
1476 ret = vcd_engine_set_commands();
1478 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to set commands : %d", ret);
1479 /* Send error cb to manager */
1480 int pid = vcd_client_widget_get_foreground_pid();
1482 vcdc_send_error_signal_to_manager(vcd_client_manager_get_pid(), VCD_ERROR_OPERATION_FAILED, "voice_engine.error.proc_fail");
1483 return VCD_ERROR_OPERATION_FAILED;
1486 SLOG(LOG_DEBUG, TAG_VCD, "[Server Success] Set command");
1488 bool stop_by_silence = true;
1489 if (VCD_RECOGNITION_MODE_MANUAL == vcd_client_get_recognition_mode()) {
1490 stop_by_silence = false;
1493 vcd_client_manager_set_result_text(NULL);
1495 /* 4. start recognition */
1496 ret = vcd_engine_recognize_start(stop_by_silence);
1498 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to start recognition : result(%d)", ret);
1499 /* Send error cb to manager */
1500 int pid = vcd_client_widget_get_foreground_pid();
1502 vcdc_send_error_signal_to_manager(vcd_client_manager_get_pid(), VCD_ERROR_OPERATION_FAILED, "voice_engine.error.proc_fail");
1503 return VCD_ERROR_OPERATION_FAILED;
1506 SLOG(LOG_DEBUG, TAG_VCD, "[Server Success] Start engine");
1508 /* 5. recorder start */
1509 ret = vcd_recorder_start();
1511 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to start recorder : result(%d)", ret);
1512 vcd_engine_recognize_cancel();
1513 /* Send error cb to manager */
1514 int pid = vcd_client_widget_get_foreground_pid();
1516 vcdc_send_error_signal_to_manager(vcd_client_manager_get_pid(), VCD_ERROR_OPERATION_FAILED, "voice_engine.error.proc_fail");
1520 vcd_config_set_service_state(VCD_STATE_RECORDING);
1521 vcdc_send_service_state(VCD_STATE_RECORDING);
1523 SLOG(LOG_DEBUG, TAG_VCD, "[Server Success] Start recognition(%d)", stop_by_silence);
1528 static Eina_Bool __vcd_request_show_tooltip(void *data)
1530 int pid = vcd_client_widget_get_foreground_pid();
1532 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Request tooltip show and widget command");
1533 vcdc_send_show_tooltip(pid, (bool)data);
1539 int vcd_server_mgr_start(vcd_recognition_mode_e recognition_mode, bool exclusive_cmd, bool start_by_client)
1541 /* 1. check current state */
1542 vcd_state_e state = vcd_config_get_service_state();
1544 if (VCD_STATE_READY != state) {
1545 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Current state is not ready");
1546 return VCD_ERROR_INVALID_STATE;
1548 if (-1 == vcd_client_manager_get_pid()) {
1549 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Manager is NOT available.");
1550 return VCD_ERROR_OPERATION_FAILED;
1553 SLOG(LOG_DEBUG, TAG_VCD, "[Server] set recognition mode = %d", recognition_mode);
1554 vcd_client_set_recognition_mode(recognition_mode);
1556 if (false == exclusive_cmd) {
1557 vcd_client_update_foreground_pid();
1558 /* Notify show tooltip */
1559 if (1 == vcd_config_get_command_type_enabled(VC_COMMAND_TYPE_WIDGET)) {
1560 int pid = vcd_client_widget_get_foreground_pid();
1562 SLOG(LOG_INFO, TAG_VCD, "[Server] Request tooltip show and widget command");
1563 ecore_timer_add(0, __vcd_request_show_tooltip, (void*)true);
1568 vcd_client_manager_set_exclusive(exclusive_cmd);
1572 if (true == start_by_client) {
1573 /* Get foreground pid */
1574 if (0 != vcd_config_get_foreground(&fg_pid)) {
1575 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to get foreground");
1578 /* Set client exclusive option */
1579 if (0 != vcd_client_set_exclusive_command(fg_pid)) {
1580 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to set exclusive command");
1584 int ret = __start_internal_recognition();
1586 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to start recongition : %d", ret);
1590 if (true == start_by_client) {
1591 vcd_client_unset_exclusive_command(fg_pid);
1594 return VCD_ERROR_NONE;
1597 int vcd_server_mgr_stop()
1599 /* 1. Check current state is recording */
1600 if (VCD_STATE_RECORDING != vcd_config_get_service_state()) {
1601 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Current state is not recording");
1602 return VCD_ERROR_INVALID_STATE;
1604 if (-1 == vcd_client_manager_get_pid()) {
1605 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Manager is NOT available.");
1606 return VCD_ERROR_OPERATION_FAILED;
1609 /* 2. Stop recorder */
1610 vcd_recorder_stop();
1612 /* 3. Stop engine recognition */
1613 int ret = vcd_engine_recognize_stop();
1615 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to stop recognition : %d", ret);
1618 /* 4. Set original mode */
1619 vcd_config_set_service_state(VCD_STATE_PROCESSING);
1620 vcdc_send_service_state(VCD_STATE_PROCESSING);
1622 return VCD_ERROR_NONE;
1625 int vcd_server_mgr_cancel()
1627 /* 1. Check current state */
1628 vcd_state_e state = vcd_config_get_service_state();
1629 if (VCD_STATE_RECORDING != state && VCD_STATE_PROCESSING != state) {
1630 SLOG(LOG_WARN, TAG_VCD, "[Server ERROR] Current state is not recording or processing");
1631 return VCD_ERROR_INVALID_STATE;
1633 if (-1 == vcd_client_manager_get_pid()) {
1634 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Manager is NOT available.");
1635 return VCD_ERROR_OPERATION_FAILED;
1638 if (g_restart_timer != NULL) {
1639 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Delete restart engine timer");
1640 ecore_timer_del(g_restart_timer);
1641 g_restart_timer = NULL;
1644 /* 2. Stop recorder */
1645 vcd_recorder_stop();
1646 /* 3. Cancel engine */
1647 int ret = vcd_engine_recognize_cancel();
1649 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Fail to cancel : %d", ret);
1652 if (false == vcd_client_manager_get_exclusive()) {
1653 if (1 == vcd_config_get_command_type_enabled(VC_COMMAND_TYPE_WIDGET)) {
1654 int pid = vcd_client_widget_get_foreground_pid();
1656 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Request tooltip hide");
1657 ecore_timer_add(0, __vcd_request_show_tooltip, (void*)false);
1661 vcd_client_manager_set_exclusive(false);
1665 vcd_config_set_service_state(VCD_STATE_READY);
1666 vcdc_send_service_state(VCD_STATE_READY);
1668 return VCD_ERROR_NONE;
1672 int vcd_server_mgr_result_select()
1674 __vcd_send_selected_result(NULL);
1676 return VCD_ERROR_NONE;
1679 int vcd_server_mgr_set_domain(int pid, const char* domain)
1681 /* check if pid is valid */
1682 if (false == vcd_client_manager_is_valid(pid)) {
1683 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] The manager pid(%d) is NOT valid", pid);
1684 return VCD_ERROR_INVALID_PARAMETER;
1687 vcd_state_e state = vcd_config_get_service_state();
1688 if (VCD_STATE_READY != state) {
1689 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Current state is not ready");
1690 return VCD_ERROR_INVALID_STATE;
1693 /* Set domain to engine */
1694 int ret = vcd_engine_set_domain(pid, domain);
1696 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to set domain : %d", ret);
1698 SLOG(LOG_DEBUG, TAG_VCD, "[Server SUCCESS] Set domain");
1704 int vcd_server_mgr_set_private_data(int pid, const char* key, const char* data)
1706 /* check if pid is valid */
1707 if (false == vcd_client_manager_is_valid(pid)) {
1708 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] The manager pid(%d) is NOT valid", pid);
1709 return VCD_ERROR_INVALID_PARAMETER;
1712 vcd_state_e state = vcd_config_get_service_state();
1713 if (VCD_STATE_READY != state) {
1714 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Current state is not ready");
1715 return VCD_ERROR_INVALID_STATE;
1718 /* Set private data to engine */
1719 int ret = vcd_engine_set_private_data(pid, key, data);
1721 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to set private data : %d", ret);
1723 SLOG(LOG_DEBUG, TAG_VCD, "[Server SUCCESS] Set private data");
1729 int vcd_server_mgr_get_private_data(int pid, const char* key, char** data)
1731 /* check if pid is valid */
1732 if (false == vcd_client_manager_is_valid(pid)) {
1733 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] The manager pid(%d) is NOT valid", pid);
1734 return VCD_ERROR_INVALID_PARAMETER;
1736 vcd_state_e state = vcd_config_get_service_state();
1737 if (VCD_STATE_READY != state) {
1738 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Current state is not ready");
1739 return VCD_ERROR_INVALID_STATE;
1742 /* Get private data to engine */
1743 int ret = vcd_engine_get_private_data(pid, key, data);
1745 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to get private data : %d", ret);
1747 SLOG(LOG_DEBUG, TAG_VCD, "[Server SUCCESS] Set private data");
1753 int vcd_server_mgr_do_action(int pid, int type, const char* action)
1757 /* check if pid is valid */
1758 if (false == vcd_client_manager_is_valid(pid)) {
1759 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] The manager pid(%d) is NOT valid", pid);
1760 return VCD_ERROR_INVALID_PARAMETER;
1763 vcd_state_e state = vcd_config_get_service_state();
1764 if (VCD_STATE_READY != state) {
1765 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Current state is not ready");
1766 return VCD_ERROR_INVALID_STATE;
1769 /* Reqeust do action to engine */
1770 if (VCD_SEND_EVENT_TYPE_TEXT == type)
1771 ret = vcd_engine_process_text(pid, action);
1772 else if (VCD_SEND_EVENT_TYPE_LIST_EVENT == type)
1773 ret = vcd_engine_process_list_event(pid, action);
1774 else if (VCD_SEND_EVENT_TYPE_HAPTIC_EVENT == type)
1775 ret = vcd_engine_process_haptic_event(pid, action);
1778 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to process do action : %d", ret);
1780 SLOG(LOG_DEBUG, TAG_VCD, "[Server SUCCESS] Process do action");
1786 int vcd_server_mgr_enable_command_type(int pid, int cmd_type)
1790 /* check if pid is valid */
1791 if (false == vcd_client_manager_is_valid(pid)) {
1792 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] The manager pid(%d) is NOT valid", pid);
1793 return VCD_ERROR_INVALID_PARAMETER;
1796 vcd_state_e state = vcd_config_get_service_state();
1797 if (VCD_STATE_READY != state) {
1798 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Current state is not ready");
1799 return VCD_ERROR_INVALID_STATE;
1802 ret = vcd_config_enable_command_type(cmd_type);
1804 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to enable command type");
1806 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Enable command type(%d)", cmd_type);
1812 int vcd_server_mgr_disable_command_type(int pid, int cmd_type)
1816 /* check if pid is valid */
1817 if (false == vcd_client_manager_is_valid(pid)) {
1818 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] The manager pid(%d) is NOT valid", pid);
1819 return VCD_ERROR_INVALID_PARAMETER;
1822 vcd_state_e state = vcd_config_get_service_state();
1823 if (VCD_STATE_READY != state) {
1824 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Current state is not ready");
1825 return VCD_ERROR_INVALID_STATE;
1828 ret = vcd_config_disable_command_type(cmd_type);
1830 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to disable command type");
1832 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Disable command type(%d)", cmd_type);
1839 * VC Server Functions for Client
1841 int vcd_server_initialize(int pid)
1843 if (false == g_is_engine) {
1844 if (0 != vcd_engine_agent_initialize_current_engine()) {
1845 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] No Engine");
1846 g_is_engine = false;
1847 return VCD_ERROR_ENGINE_NOT_FOUND;
1853 if (false == vcd_engine_is_available_engine()) {
1854 if (0 != vcd_engine_agent_initialize_current_engine()) {
1855 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] No Engine");
1856 return VCD_ERROR_ENGINE_NOT_FOUND;
1860 /* check if pid is valid */
1861 if (true == vcd_client_is_available(pid)) {
1862 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] The pid is already exist");
1863 return VCD_ERROR_INVALID_PARAMETER;
1866 /* Add client information to client manager */
1867 if (0 != vcd_client_add(pid)) {
1868 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to add client info");
1869 return VCD_ERROR_OPERATION_FAILED;
1872 SLOG(LOG_DEBUG, TAG_VCD, "[Server Success] Client Initialize : pid(%d)", pid);
1874 return VCD_ERROR_NONE;
1877 int vcd_server_finalize(int pid)
1879 /* check if pid is valid */
1880 if (false == vcd_client_is_available(pid)) {
1881 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] pid is NOT valid ");
1882 return VCD_ERROR_INVALID_PARAMETER;
1885 /* Remove client information */
1886 if (0 != vcd_client_delete(pid)) {
1887 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Fail to delete client");
1890 if (0 == vcd_client_get_ref_count()) {
1891 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Connected client list is empty");
1892 ecore_timer_add(0, __finalize_quit_ecore_loop, NULL);
1895 SLOG(LOG_DEBUG, TAG_VCD, "[Server Success] Client Finalize : pid(%d)", pid);
1897 return VCD_ERROR_NONE;
1900 int vcd_server_set_command(int pid, vc_cmd_type_e cmd_type)
1902 /* check if pid is valid */
1903 if (false == vcd_client_is_available(pid)) {
1904 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] pid is NOT valid ");
1905 return VCD_ERROR_INVALID_PARAMETER;
1908 if (0 != vcd_client_set_command_type(pid, cmd_type)) {
1909 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to set command type : pid(%d), cmd_type(%d)", pid, cmd_type);
1910 return VCD_ERROR_OPERATION_FAILED;
1916 int vcd_server_unset_command(int pid, vc_cmd_type_e cmd_type)
1918 /* check if pid is valid */
1919 if (false == vcd_client_is_available(pid)) {
1920 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] pid is NOT valid ");
1921 return VCD_ERROR_INVALID_PARAMETER;
1924 if (0 != vcd_client_unset_command_type(pid, cmd_type)) {
1925 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to unset command type : pid(%d), cmd_type(%d)", pid, cmd_type);
1926 return VCD_ERROR_OPERATION_FAILED;
1932 int vcd_server_set_foreground(int pid, bool value)
1934 /* check if pid is valid */
1935 if (false == vcd_client_is_available(pid) && false == vcd_client_widget_is_available(pid)) {
1936 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] pid is NOT valid ");
1937 return VCD_ERROR_INVALID_PARAMETER;
1940 if (0 != vcd_config_set_foreground(pid, value)) {
1941 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to set foreground : pid(%d), value(%d)", pid, value);
1942 return VCD_ERROR_OPERATION_FAILED;
1948 static int __vcd_server_launch_manager_app()
1951 bool running = false;
1954 ret = vcd_client_manager_get_appid(&appid);
1955 if (0 != ret || NULL == appid) {
1956 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Fail to get manager appid");
1957 return VCD_ERROR_OPERATION_FAILED;
1959 ret = app_manager_is_running(appid, &running);
1961 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Fail to check running with appid(%s)", appid);
1964 return VCD_ERROR_OPERATION_FAILED;
1966 if (false == running) {
1967 int tmp_ret = __vcd_is_package_installed(appid);
1968 if (false == tmp_ret) {
1969 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] manager app is not installed, appid(%s)", appid);
1971 ret = __vcd_activate_app_by_appcontrol(appid);
1973 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Fail to activate app");
1978 SLOG(LOG_ERROR, TAG_VCD, "Launch vc manager app: appid(%s)", appid);
1981 ret = __vcd_resume_app(appid);
1983 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Fail to resume app");
1988 SLOG(LOG_ERROR, TAG_VCD, "Resume vc manager app: appid(%s)", appid);
1994 return VCD_ERROR_NONE;
1997 int vcd_server_dialog(int pid, const char* disp_text, const char* utt_text, int continuous)
1999 /* check if pid is valid */
2000 if (false == vcd_client_is_available(pid) && false == vcd_client_widget_is_available(pid)) {
2001 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] pid is NOT valid ");
2002 return VCD_ERROR_INVALID_PARAMETER;
2005 int ret = __vcd_server_launch_manager_app();
2007 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to send dialog : mgr_pid(%d), pid(%d), disp_text(%s), utt_text(%s), continue(%d)", vcd_client_manager_get_pid(), pid, disp_text, utt_text, continuous);
2011 ret = vcdc_send_dialog(vcd_client_manager_get_pid(), pid, disp_text, utt_text, continuous);
2013 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);
2020 int vcd_server_is_system_command_valid(int pid, int* is_sys_cmd_valid)
2022 /* check if pid is valid */
2023 if (false == vcd_client_is_available(pid) && false == vcd_client_widget_is_available(pid)) {
2024 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] pid is NOT valid ");
2025 return VCD_ERROR_INVALID_PARAMETER;
2028 /* check if system command is valid */
2029 if (true == vcd_client_manager_is_system_command_valid(vcd_client_manager_get_pid()))
2030 *is_sys_cmd_valid = true;
2032 *is_sys_cmd_valid = false;
2038 int vcd_server_set_exclusive_command(int pid, bool value)
2040 /* check if pid is valid */
2041 if (false == vcd_client_is_available(pid)) {
2042 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] pid is NOT valid ");
2043 return VCD_ERROR_INVALID_PARAMETER;
2046 if (true == value) {
2047 if (0 != vcd_client_set_exclusive_command(pid)) {
2048 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to set exclusive command : pid(%d)", pid);
2049 return VCD_ERROR_OPERATION_FAILED;
2052 if (0 != vcd_client_unset_exclusive_command(pid)) {
2053 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to unset exclusive command : pid(%d)", pid);
2054 return VCD_ERROR_OPERATION_FAILED;
2061 int vcd_server_request_start(int pid, bool stop_by_silence)
2063 /* check if pid is valid */
2064 if (false == vcd_client_is_available(pid)) {
2065 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] pid(%d) is NOT forground client", pid);
2066 return VCD_ERROR_INVALID_PARAMETER;
2070 /* Check current state */
2071 vcd_state_e state = vcd_config_get_service_state();
2073 /* Service state should be ready */
2074 if (VCD_STATE_READY != state) {
2075 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Current state is not Ready : pid(%d)", pid);
2076 return VCD_ERROR_INVALID_STATE;
2079 if (-1 != vcd_client_manager_get_pid()) {
2080 /* Check current pid is valid */
2081 if (false == vcd_client_manager_check_demandable_client(pid)) {
2082 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Current client is NOT available : pid(%d)", pid);
2083 return VCD_ERROR_INVALID_PARAMETER;
2087 ret = vcd_server_mgr_start(stop_by_silence, false);
2089 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Fail to start recognition");
2090 return VCD_ERROR_INVALID_PARAMETER;
2096 int vcd_server_request_stop(int pid)
2099 /* Check current state */
2100 vcd_state_e state = vcd_config_get_service_state();
2102 /* Service state should be ready */
2103 if (VCD_STATE_RECORDING != state) {
2104 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Current state is not Recording : pid(%d)", pid);
2105 return VCD_ERROR_INVALID_STATE;
2108 if (-1 != vcd_client_manager_get_pid()) {
2109 /* Check current pid is valid */
2110 if (false == vcd_client_manager_check_demandable_client(pid)) {
2111 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Current client is NOT available : pid(%d)", pid);
2112 return VCD_ERROR_INVALID_PARAMETER;
2116 ret = vcd_server_mgr_stop();
2118 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Fail to start recognition");
2119 return VCD_ERROR_OPERATION_FAILED;
2122 return VCD_ERROR_NONE;
2125 int vcd_server_request_cancel(int pid)
2128 /* Check current state */
2129 vcd_state_e state = vcd_config_get_service_state();
2131 /* Service state should be recording or processing */
2132 if (VCD_STATE_RECORDING != state && VCD_STATE_PROCESSING != state) {
2133 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Current state is not Recording or Processing : pid(%d)", pid);
2134 return VCD_ERROR_INVALID_STATE;
2137 if (-1 != vcd_client_manager_get_pid()) {
2138 /* Check current pid is valid */
2139 if (false == vcd_client_manager_check_demandable_client(pid)) {
2140 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Current client is NOT available : pid(%d)", pid);
2141 return VCD_ERROR_INVALID_PARAMETER;
2145 ret = vcd_server_mgr_cancel();
2147 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Fail to start recognition");
2148 return VCD_ERROR_OPERATION_FAILED;
2151 return VCD_ERROR_NONE;
2156 * VC Server Functions for Widget lib
2158 int vcd_server_widget_initialize(int pid)
2160 if (false == g_is_engine) {
2161 if (0 != vcd_engine_agent_initialize_current_engine()) {
2162 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] No Engine");
2163 g_is_engine = false;
2164 return VCD_ERROR_ENGINE_NOT_FOUND;
2170 if (false == vcd_engine_is_available_engine()) {
2171 if (0 != vcd_engine_agent_initialize_current_engine()) {
2172 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] No Engine");
2173 return VCD_ERROR_ENGINE_NOT_FOUND;
2177 /* check if pid is valid */
2178 if (true == vcd_client_widget_is_available(pid)) {
2179 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] The pid is already exist");
2180 return VCD_ERROR_NONE;
2183 /* Add client information to client manager */
2184 if (0 != vcd_client_widget_add(pid)) {
2185 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to add client info");
2186 return VCD_ERROR_OPERATION_FAILED;
2189 SLOG(LOG_DEBUG, TAG_VCD, "[Server Success] Initialize widget : pid(%d)", pid);
2191 return VCD_ERROR_NONE;
2194 int vcd_server_widget_finalize(int pid)
2196 /* check if pid is valid */
2197 if (false == vcd_client_widget_is_available(pid)) {
2198 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] pid is NOT valid ");
2199 return VCD_ERROR_INVALID_PARAMETER;
2202 /* Remove client information */
2203 if (0 != vcd_client_widget_delete(pid)) {
2204 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Fail to delete client");
2207 if (0 == vcd_client_get_ref_count()) {
2208 SLOG(LOG_DEBUG, TAG_VCD, "[Server] connected client list is empty");
2209 ecore_timer_add(0, __finalize_quit_ecore_loop, NULL);
2212 return VCD_ERROR_NONE;
2215 int vcd_server_widget_start_recording(int pid, bool widget_command)
2217 /* check if pid is valid */
2218 if (false == vcd_client_widget_is_available(pid)) {
2219 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] pid is NOT valid ");
2220 return VCD_ERROR_INVALID_PARAMETER;
2223 if (true == widget_command) {
2224 vcd_client_widget_set_command(pid);
2225 SLOG(LOG_DEBUG, TAG_VCD, "[Server] widget command is available");
2227 vcd_client_widget_unset_command(pid);
2228 SLOG(LOG_WARN, TAG_VCD, "[Server] widget command is NOT available");
2231 int ret = __start_internal_recognition();
2233 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to start recongition : %d", ret);
2234 ecore_timer_add(0, __vcd_request_show_tooltip, (void*)false);
2240 int vcd_server_widget_start(int pid, bool stop_by_silence)
2242 /* check if pid is valid */
2243 int fore_pid = vcd_client_widget_get_foreground_pid();
2244 if (pid != fore_pid) {
2245 SLOG(LOG_ERROR, TAG_VCD, "[Server] pid is NOT foreground");
2246 return VCD_ERROR_INVALID_PARAMETER;
2249 /* Check current state */
2250 vcd_state_e state = vcd_config_get_service_state();
2252 /* Service state should be ready */
2253 if (VCD_STATE_READY != state) {
2254 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Current state is not Ready : pid(%d)", pid);
2255 return VCD_ERROR_INVALID_STATE;
2258 vcd_client_set_slience_detection(stop_by_silence);
2260 /* Notify show tooltip */
2261 ecore_timer_add(0, __vcd_request_show_tooltip, (void*)true);
2266 int vcd_server_widget_stop(int pid)
2268 /* check if pid is valid */
2269 int fore_pid = vcd_client_widget_get_foreground_pid();
2270 if (pid != fore_pid) {
2271 SLOG(LOG_ERROR, TAG_VCD, "[Server] pid is NOT foreground");
2272 return VCD_ERROR_INVALID_PARAMETER;
2276 /* Check current state */
2277 vcd_state_e state = vcd_config_get_service_state();
2279 /* Service state should be recording */
2280 if (VCD_STATE_RECORDING != state) {
2281 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Current service state is not Recording : pid(%d)", pid);
2282 return VCD_ERROR_INVALID_STATE;
2285 ret = vcd_server_mgr_stop();
2287 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Fail to start recognition");
2288 return VCD_ERROR_OPERATION_FAILED;
2291 return VCD_ERROR_NONE;
2294 int vcd_server_widget_cancel(int pid)
2296 /* check if pid is valid */
2297 int fore_pid = vcd_client_widget_get_foreground_pid();
2298 if (pid != fore_pid) {
2299 SLOG(LOG_ERROR, TAG_VCD, "[Server] pid is NOT foreground");
2300 return VCD_ERROR_INVALID_PARAMETER;
2304 /* Check current state */
2305 vcd_state_e state = vcd_config_get_service_state();
2307 /* Service state should be recording or processing */
2308 if (VCD_STATE_RECORDING != state && VCD_STATE_PROCESSING != state) {
2309 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Current state is not Recording or Processing : pid(%d)", pid);
2310 return VCD_ERROR_INVALID_STATE;
2313 ret = vcd_server_mgr_cancel();
2315 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Fail to cancel recognition : %d", ret);
2319 return VCD_ERROR_NONE;
2322 int vcd_server_widget_enable_asr_result(int pid, bool enable)
2325 ret = vcd_client_widget_set_asr_result_enabled(pid, enable);
2327 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Fail to enable asr result : %d", ret);