2 * Copyright (c) 2011-2015 Samsung Electronics Co., Ltd All Rights Reserved
4 * Licensed under the Apache License, Version 2.0 (the License);
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an AS IS BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
18 #include <app_manager.h>
20 #include <sound_manager.h>
22 #include "vc_cmd_db.h"
23 #include "vc_info_parser.h"
25 #include "vcd_server.h"
26 #include "vcd_client_data.h"
28 #include "vcd_engine_agent.h"
29 #include "vcd_config.h"
30 #include "vcd_recorder.h"
33 #include "voice_control_command_expand.h"
36 * VC Server static variable
38 static bool g_is_engine;
40 static GList *g_proc_list = NULL;
42 static Ecore_Timer *g_restart_timer = NULL;
45 * @brief Enumerations of send event type.
48 VCD_SEND_EVENT_TYPE_TEXT, /**< send text event to vc engine*/
49 VCD_SEND_EVENT_TYPE_LIST_EVENT, /**< send list event to vc engine */
50 VCD_SEND_EVENT_TYPE_HAPTIC_EVENT /**< send haptic event to vc engine */
51 } vcd_send_event_type_e;
54 * VC Server Internal Functions
56 static Eina_Bool __stop_by_silence(void *data)
58 SLOG(LOG_DEBUG, TAG_VCD, "===== Silence Detected ");
60 vcd_server_mgr_stop();
62 SLOG(LOG_DEBUG, TAG_VCD, "=====");
63 SLOG(LOG_DEBUG, TAG_VCD, " ");
67 static Eina_Bool __cancel_by_interrupt(void *data)
69 SLOG(LOG_DEBUG, TAG_VCD, "===== Cancel by interrupt");
71 vcd_server_mgr_cancel();
73 SLOG(LOG_DEBUG, TAG_VCD, "=====");
74 SLOG(LOG_DEBUG, TAG_VCD, " ");
78 static void __cancel_by_error(void *data)
80 SLOG(LOG_DEBUG, TAG_VCD, "===== Cancel by error");
82 vcd_server_mgr_cancel();
84 SLOG(LOG_DEBUG, TAG_VCD, "=====");
85 SLOG(LOG_DEBUG, TAG_VCD, " ");
89 static Eina_Bool __restart_engine(void *data)
91 SLOG(LOG_DEBUG, TAG_VCD, "===== Restart by no result");
93 g_restart_timer = NULL;
95 /* Restart recognition */
96 int ret = vcd_engine_recognize_start(true);
98 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to restart recognition : result(%d)", ret);
102 SLOG(LOG_DEBUG, TAG_VCD, "[Server Success] Start engine");
104 if (VCD_RECOGNITION_MODE_RESTART_AFTER_REJECT == vcd_client_get_recognition_mode()) {
105 vcd_config_set_service_state(VCD_STATE_RECORDING);
106 vcdc_send_service_state(VCD_STATE_RECORDING);
109 SLOG(LOG_DEBUG, TAG_VCD, "[Server Success] Restart recognition");
111 SLOG(LOG_DEBUG, TAG_VCD, "=====");
112 SLOG(LOG_DEBUG, TAG_VCD, " ");
116 static int __server_recorder_callback(const void* data, const unsigned int length)
118 vcd_state_e state = vcd_config_get_service_state();
119 if (VCD_STATE_READY == state) {
120 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Ready state, but recording");
121 } else if (VCD_STATE_PROCESSING == state) {
125 vcp_speech_detect_e speech_detected = VCP_SPEECH_DETECT_NONE;
128 ret = vcd_engine_recognize_audio(data, length, &speech_detected);
132 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to set recording data to engine(%d)", ret);
133 ecore_timer_add(0, __cancel_by_interrupt, NULL);
134 /* Send error cb to manager */
135 if (VCP_ERROR_OUT_OF_NETWORK == ret) {
136 vcdc_send_error_signal_to_manager(vcd_client_manager_get_pid(), VCD_ERROR_TIMED_OUT, "Engine connection failed");
138 vcdc_send_error_signal_to_manager(vcd_client_manager_get_pid(), VCD_ERROR_OPERATION_FAILED, "Engine recognition failed");
143 if (VCP_SPEECH_DETECT_BEGIN == speech_detected) {
144 if (-1 != vcd_client_manager_get_pid()) {
145 /* Manager client is available */
146 if (0 != vcdc_send_speech_detected(vcd_client_manager_get_pid())) {
147 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Fail to send speech detected");
150 } else if (VCP_SPEECH_DETECT_END == speech_detected) {
151 if (VCD_RECOGNITION_MODE_STOP_BY_SILENCE == vcd_client_get_recognition_mode()) {
152 /* silence detected */
153 ecore_timer_add(0, __stop_by_silence, NULL);
154 } else if (VCD_RECOGNITION_MODE_RESTART_AFTER_REJECT == vcd_client_get_recognition_mode()) {
155 /* Stop engine recognition */
156 int ret = vcd_engine_recognize_stop();
158 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to stop recognition : %d", ret);
160 vcd_config_set_service_state(VCD_STATE_PROCESSING);
161 vcdc_send_service_state(VCD_STATE_PROCESSING);
163 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Stop engine only by silence");
164 } else if (VCD_RECOGNITION_MODE_RESTART_CONTINUOUSLY == vcd_client_get_recognition_mode()) {
165 /* Stop engine recognition */
166 int ret = vcd_engine_recognize_stop();
168 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to stop recognition : %d", ret);
176 void __server_recorder_interrupt_callback()
178 SLOG(LOG_DEBUG, TAG_VCD, "===== Cancel by sound interrupt");
180 ecore_timer_add(0, __cancel_by_interrupt, NULL);
182 SLOG(LOG_DEBUG, TAG_VCD, "=====");
183 SLOG(LOG_DEBUG, TAG_VCD, " ");
186 static void __config_lang_changed_cb(const char* current_lang, void* user_data)
188 SLOG(LOG_DEBUG, TAG_VCD, "===== Change language ");
190 /* Current state is recording */
191 vcd_state_e state = vcd_config_get_service_state();
192 if (VCD_STATE_RECORDING == state || VCD_STATE_PROCESSING == state) {
193 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Current state is 'Recording'. Cancel recognition");
194 vcd_server_mgr_cancel();
198 ret = vcd_engine_set_current_language(current_lang);
200 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to set language of engine : %d", ret);
203 SLOG(LOG_DEBUG, TAG_VCD, "=====");
204 SLOG(LOG_DEBUG, TAG_VCD, " ");
209 static void __config_foreground_changed_cb(int previous, int current, void* user_data)
211 SLOG(LOG_DEBUG, TAG_VCD, "===== Change foreground");
213 SLOG(LOG_DEBUG, TAG_VCD, "Foreground pid(%d)", current);
215 if (VC_NO_FOREGROUND_PID != current) {
216 /* Foreground app is changed */
217 vcd_state_e state = vcd_config_get_service_state();
218 if (VCD_STATE_RECORDING == state) {
219 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Foreground pid(%d) is changed. Cancel recognition", current);
220 ecore_timer_add(0, __cancel_by_interrupt, NULL);
224 SLOG(LOG_DEBUG, TAG_VCD, "=====");
225 SLOG(LOG_DEBUG, TAG_VCD, " ");
230 static int __vcd_activate_app_by_appcontrol(const char* appid)
233 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Invalid parameter");
234 return VCD_ERROR_INVALID_PARAMETER;
238 app_control_h app_control = NULL;
239 ret = app_control_create(&app_control);
240 if (APP_CONTROL_ERROR_NONE == ret) {
242 ret = app_control_add_extra_data(app_control, "voice_launch", "get_result");
243 if (APP_CONTROL_ERROR_NONE != ret) {
244 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Fail to add extra data, ret(%d)", ret);
245 return VCD_ERROR_OPERATION_FAILED;
248 ret = app_control_set_app_id(app_control, appid);
249 if (APP_CONTROL_ERROR_NONE != ret) {
250 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Fail to set app id, ret(%d)", ret);
251 return VCD_ERROR_OPERATION_FAILED;
253 // Sent launch request
254 ret = app_control_send_launch_request(app_control, NULL, NULL);
255 if(APP_CONTROL_ERROR_NONE != ret) {
256 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Fail to send launch request, ret(%d)", ret);
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;
388 iter = g_slist_nth(pid_list, 0);
389 while (NULL != iter) {
390 temp_cmd = iter->data;
392 if (NULL != temp_cmd) {
393 /* Launch deactivated several apps that is matched with result */
394 ret = __vcd_launch_app(result);
396 SLOG(LOG_ERROR, TAG_VCD, "Fail to launch or resume app, ret(%d) result(%s)", ret, result);
398 /* send result noti */
399 ret = vcdc_send_result(temp_cmd->pid, vcd_client_manager_get_pid(), temp_cmd->type);
401 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Fail to send result, ret(%d)", ret);
404 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Send result : pid(%d) type(%d)", temp_cmd->pid, temp_cmd->type);
410 pid_list = g_slist_remove_link(pid_list, iter);
411 iter = g_slist_nth(pid_list, 0);
416 if (VCD_RECOGNITION_MODE_RESTART_CONTINUOUSLY != vcd_client_get_recognition_mode()) {
417 vcd_config_set_service_state(VCD_STATE_READY);
418 vcdc_send_service_state(VCD_STATE_READY);
424 //static void __vcd_server_pre_result_cb(vcp_pre_result_event_e event, const char* pre_result, void *user_data)
425 static void __vcd_server_asr_result_cb(vcp_asr_result_event_e event, const char* asr_result, void *user_data)
427 if (NULL != asr_result) {
428 SLOG(LOG_DEBUG, TAG_VCD, "[Server] ASR result - Event(%d), Text(%s)", event, asr_result);
429 vcdc_send_pre_result_to_manager(vcd_client_manager_get_pid(), event, asr_result);
435 static void* __recorder_stop(void *data)
441 static void __vcd_server_result_cb(vcp_result_event_e event, int* result_id, int count, const char* all_result,
442 const char* non_fixed_result, const char* nlu_result, const char* msg, void *user_data)
444 if (VCD_STATE_PROCESSING != vcd_config_get_service_state()) {
445 if (VCD_RECOGNITION_MODE_RESTART_CONTINUOUSLY != vcd_client_get_recognition_mode()) {
446 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Current state is not 'Processing' and mode is not 'Restart continuously'");
451 vc_info_parser_unset_result(vcd_client_manager_get_exclusive());
452 vcd_client_manager_set_result_text(all_result);
454 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Event(%d), Text(%s) Nonfixed(%s) Msg(%s) Result count(%d)",
455 event, all_result, non_fixed_result, msg, count);
457 if (VCD_RECOGNITION_MODE_RESTART_AFTER_REJECT == vcd_client_get_recognition_mode()) {
458 if (VCP_RESULT_EVENT_REJECTED == event) {
459 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Restart by no or rejected result");
460 /* If no result and restart option is ON */
461 /* Send reject message */
462 bool temp = vcd_client_manager_get_exclusive();
463 vc_info_parser_set_result(all_result, event, msg, NULL, temp);
464 if (0 != vcdc_send_result_to_manager(vcd_client_manager_get_pid(), VC_RESULT_TYPE_NOTIFICATION)) {
465 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Fail to send result");
468 g_restart_timer = ecore_timer_add(0, __restart_engine, NULL);
471 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Stop recorder due to success");
472 //vcd_recorder_stop();
473 ecore_main_loop_thread_safe_call_sync(__recorder_stop, NULL);
474 } else if (VCD_RECOGNITION_MODE_RESTART_CONTINUOUSLY == vcd_client_get_recognition_mode()) {
475 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Restart continuously");
476 /* Restart option is ON */
477 g_restart_timer = ecore_timer_add(0, __restart_engine, NULL);
478 if (VCP_RESULT_EVENT_REJECTED == event) {
479 bool temp = vcd_client_manager_get_exclusive();
480 vc_info_parser_set_result(all_result, event, msg, NULL, temp);
481 if (0 != vcdc_send_result_to_manager(vcd_client_manager_get_pid(), VC_RESULT_TYPE_NOTIFICATION)) {
482 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Fail to send result");
488 /* if nlu_result is exist, Add command handle(is_action) into result list */
490 SLOG(LOG_DEBUG, TAG_VCD, "[Server] === Get engine result ===");
493 vc_cmd_s* temp_cmd = NULL;
494 vc_cmd_list_h vc_cmd_list = NULL;
496 if (0 != vc_cmd_list_create(&vc_cmd_list)) {
497 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Fail to create command list");
498 vcd_client_manager_set_exclusive(false);
499 vcd_config_set_service_state(VCD_STATE_READY);
500 vcdc_send_service_state(VCD_STATE_READY);
504 /* priority filter */
505 /* system > exclusive > foreground = widget > system_background > background */
507 int* filtered_id = (int*)calloc(count, sizeof(int));
508 int filtered_count = 0;
509 int top_priority = VC_COMMAND_PRIORITY_BACKGROUND;
510 for (i = 0; i < count; i++) {
511 SLOG(LOG_DEBUG, TAG_VCD, "[Server] [%d] Result id(%d)", i, result_id[i]);
513 if (0 > result_id[i]) {
514 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Result ID(%d) is NOT valid", result_id[i]);
518 ret = vcd_client_get_cmd_from_result_id(result_id[i], &temp_cmd);
519 if (0 == ret && NULL != temp_cmd) {
520 if (top_priority == temp_cmd->priority) {
521 filtered_id[filtered_count] = result_id[i];
523 } else if (top_priority < temp_cmd->priority) {
526 filtered_id[0] = result_id[i];
528 top_priority = temp_cmd->priority;
534 for (i = 0; i < filtered_count; i++) {
535 SLOG(LOG_DEBUG, TAG_VCD, "[Server] [%d] Filtered Result id(%d)", i, filtered_id[i]);
537 if (filtered_id[i] < 0) {
538 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Filtered ID(%d) is NOT valid", filtered_id[i]);
542 ret = vcd_client_get_cmd_from_result_id(filtered_id[i], &temp_cmd);
543 if (0 == ret && NULL != temp_cmd) {
544 switch (temp_cmd->format) {
545 case VC_CMD_FORMAT_FIXED:
546 case VC_CMD_FORMAT_FIXED_AND_VFIXED:
547 case VC_CMD_FORMAT_VFIXED_AND_FIXED:
548 case VC_CMD_FORMAT_PARTIAL:
549 case VC_CMD_FORMAT_FIXED_AND_NONFIXED:
550 case VC_CMD_FORMAT_NONFIXED_AND_FIXED:
552 case VC_CMD_FORMAT_ACTION:
556 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Unknown command type : %d", temp_cmd->type);
560 if (0 != vc_cmd_list_add(vc_cmd_list, (vc_cmd_h)temp_cmd)) {
561 SLOG(LOG_DEBUG, TAG_VCD, "Fail to add command to list");
562 vc_cmd_destroy((vc_cmd_h)temp_cmd);
565 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] NOT found matached result(%d)", filtered_id[i]);
569 if (NULL != filtered_id) {
574 if (NULL != nlu_result) {
575 SLOG(LOG_DEBUG, TAG_VCD, "[Server] NLU (%s)", nlu_result);
576 vc_info_parser_set_nlu_result(nlu_result);
577 if (0 == is_action) {
579 if (0 != vc_cmd_create(&nlu_cmd)) {
580 SLOG(LOG_ERROR, TAG_VCD, "Fail to nlu cmd create");
582 if (0 != vc_cmd_set_type(nlu_cmd, VC_COMMAND_TYPE_SYSTEM)) {
583 SLOG(LOG_ERROR, TAG_VCD, "Fail to set type");
585 if (0 != vc_cmd_set_pid(nlu_cmd, vcd_client_manager_get_pid())) {
586 SLOG(LOG_ERROR, TAG_VCD, "Fail to set pid");
588 if (0 != vc_cmd_set_format(nlu_cmd, VC_CMD_FORMAT_ACTION)) {
589 SLOG(LOG_ERROR, TAG_VCD, "Fail to set format");
591 if (0 != vc_cmd_list_add(vc_cmd_list, nlu_cmd)) {
592 SLOG(LOG_ERROR, TAG_VCD, "Fail to add nlu cmd to list");
593 vc_cmd_destroy(nlu_cmd);
599 vc_cmd_print_list(vc_cmd_list);
601 SLOG(LOG_DEBUG, TAG_VCD, "[Server] =========================");
603 int result_count = 0;
604 vc_cmd_list_get_count(vc_cmd_list, &result_count);
606 if (0 == result_count) {
608 if (NULL != all_result) {
609 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Engine result is no command : %s", all_result);
610 bool temp = vcd_client_manager_get_exclusive();
611 vc_info_parser_set_result(all_result, event, msg, NULL, temp);
614 int pid = vcd_client_widget_get_foreground_pid();
616 if (NULL != all_result) {
617 /* Send result text to widget */
618 vcdc_send_result(pid, vcd_client_manager_get_pid(), VC_COMMAND_TYPE_WIDGET);
621 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Request tooltip hide");
622 /* Send to hide tooltip */
623 vcdc_send_show_tooltip(pid, false);
626 if (-1 != vcd_client_manager_get_pid()) {
627 /* Manager client is available */
628 if (0 != vcdc_send_result_to_manager(vcd_client_manager_get_pid(), VC_RESULT_TYPE_NORMAL)) {
629 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Fail to send result");
633 vcd_client_manager_set_exclusive(false);
637 if (false == vcd_client_manager_get_exclusive()) {
638 int pid = vcd_client_widget_get_foreground_pid();
640 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Request tooltip hide");
641 vcdc_send_show_tooltip(pid, false);
644 vc_info_parser_set_result(all_result, event, msg, vc_cmd_list, false);
646 if (-1 != vcd_client_manager_get_pid()) {
647 /* Manager client is available */
648 if (0 != vcdc_send_result_to_manager(vcd_client_manager_get_pid(), VC_RESULT_TYPE_NORMAL)) {
649 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Fail to send result");
652 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Manager is NOT available. Send result to client directly");
653 /* Send result to client */
654 ecore_timer_add(0, __vcd_send_selected_result, NULL);
657 /* exclusive command */
658 vc_info_parser_set_result(all_result, event, msg, vc_cmd_list, true);
660 if (-1 != vcd_client_manager_get_pid()) {
661 /* Manager client is available */
662 if (0 != vcdc_send_result_to_manager(vcd_client_manager_get_pid(), VC_RESULT_TYPE_NORMAL)) {
663 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Fail to send result");
666 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Manager is NOT available");
669 vcd_client_manager_set_exclusive(false);
673 vc_cmd_list_destroy(vc_cmd_list, true);
679 if (NULL == result_id) {
681 if (NULL != all_result) {
682 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Engine result is no command : %s", all_result);
683 bool temp = vcd_client_manager_get_exclusive();
684 vc_info_parser_set_result(all_result, event, msg, NULL, temp);
687 int pid = vcd_client_widget_get_foreground_pid();
689 if (NULL != all_result) {
690 /* Send result text to widget */
691 vcdc_send_result(pid, vcd_client_manager_get_pid(), VC_COMMAND_TYPE_WIDGET);
694 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Request tooltip hide");
695 /* Send to hide tooltip */
696 vcdc_send_show_tooltip(pid, false);
699 if (-1 != vcd_client_manager_get_pid()) {
700 /* Manager client is available */
701 if (0 != vcdc_send_result_to_manager(vcd_client_manager_get_pid(), VC_RESULT_TYPE_NORMAL)) {
702 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Fail to send result");
706 vcd_client_manager_set_exclusive(false);
712 SLOG(LOG_DEBUG, TAG_VCD, "[Server] === Get engine result ===");
715 vc_cmd_s* temp_cmd = NULL;
716 vc_cmd_list_h vc_cmd_list = NULL;
718 if (0 != vc_cmd_list_create(&vc_cmd_list)) {
719 SLOG(LOG_ERROR, TAG_VCD, "[Server] Fail to create command list");
720 vcd_client_manager_set_exclusive(false);
721 vcd_config_set_service_state(VCD_STATE_READY);
722 vcdc_send_service_state(VCD_STATE_READY);
727 for (i = 0; i < count; i++) {
728 SLOG(LOG_DEBUG, TAG_VCD, "[Server] [%d] Result ID(%d)", i, result_id[i]);
730 if (result_id[i] < 0) {
731 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Result ID(%d) is NOT valid", result_id[i]);
735 ret = vcd_client_get_cmd_from_result_id(result_id[i], &temp_cmd);
736 if (0 == ret && NULL != temp_cmd) {
737 switch (temp_cmd->format) {
738 case VC_CMD_FORMAT_FIXED:
739 case VC_CMD_FORMAT_FIXED_AND_VFIXED:
740 case VC_CMD_FORMAT_VFIXED_AND_FIXED:
741 case VC_CMD_FORMAT_PARTIAL:
742 /* Nonfixed result is NOT valid */
744 case VC_CMD_FORMAT_FIXED_AND_NONFIXED:
745 if (NULL == temp_cmd->parameter) {
746 if (NULL != non_fixed_result) {
747 temp_cmd->parameter = strdup(non_fixed_result);
750 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Command type is NOT vaild. Parameter (%s)", temp_cmd->parameter);
753 case VC_CMD_FORMAT_NONFIXED_AND_FIXED:
754 if (NULL == temp_cmd->command) {
755 if (NULL != non_fixed_result) {
756 temp_cmd->command = strdup(non_fixed_result);
759 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Command type is NOT vaild. Command (%s)", temp_cmd->command);
764 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Unknown command type : %d", temp_cmd->type);
768 if (0 != vc_cmd_list_add(vc_cmd_list, (vc_cmd_h)temp_cmd)) {
769 SLOG(LOG_DEBUG, TAG_VCD, "Fail to add command to list");
770 vc_cmd_destroy((vc_cmd_h)temp_cmd);
773 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] NOT found matached result(%d)", result_id[i]);
777 vc_cmd_print_list(vc_cmd_list);
779 SLOG(LOG_DEBUG, TAG_VCD, "[Server] =========================");
781 int result_count = 0;
782 vc_cmd_list_get_count(vc_cmd_list, &result_count);
784 if (false == vcd_client_manager_get_exclusive()) {
785 int pid = vcd_client_widget_get_foreground_pid();
787 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Request tooltip hide");
788 vcdc_send_show_tooltip(pid, false);
791 vc_info_parser_set_result(all_result, event, msg, vc_cmd_list, false);
793 if (-1 != vcd_client_manager_get_pid()) {
794 /* Manager client is available */
795 if (0 != vcdc_send_result_to_manager(vcd_client_manager_get_pid(), VC_RESULT_TYPE_NORMAL)) {
796 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Fail to send result");
799 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Manager is NOT available. Send result to client directly");
800 /* Send result to client */
801 ecore_timer_add(0, __vcd_send_selected_result, NULL);
804 /* exclusive command */
805 vc_info_parser_set_result(all_result, event, msg, vc_cmd_list, true);
807 if (-1 != vcd_client_manager_get_pid()) {
808 /* Manager client is available */
809 if (0 != vcdc_send_result_to_manager(vcd_client_manager_get_pid(), VC_RESULT_TYPE_NORMAL)) {
810 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Fail to send result");
813 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Manager is NOT available");
816 vcd_client_manager_set_exclusive(false);
819 vc_cmd_list_destroy(vc_cmd_list, true);
826 static void __vcd_server_nlu_result_cb(vcp_result_event_e event, const char* nlu_result, void *user_data)
828 SLOG(LOG_DEBUG, TAG_VCD, "[Server] NLU result cb - event(%d)", event);
829 SLOG(LOG_DEBUG, TAG_VCD, "[Server] result (%s)", nlu_result);
831 int ret = vc_info_parser_set_nlu_result(nlu_result);
833 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to set nlu result (%d)", ret);
840 static void __vcd_server_error_cb(vcp_error_e error, const char* msg, void *user_data)
842 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Engine Error cb - reason(%d), msg(%s)", error, msg);
843 ecore_main_loop_thread_safe_call_async(__cancel_by_error, NULL);
845 char* error_msg = NULL;
846 error_msg = strdup(msg);
848 if (0 != vcdc_send_error_signal_to_manager(vcd_client_manager_get_pid(), error, error_msg)) {
849 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to send error signal");
852 if (NULL != error_msg) {
861 * vcd server Interfaces
863 static void __vcd_file_clean_up()
865 SLOG(LOG_DEBUG, TAG_VCD, "== Old file clean up == ");
870 struct dirent *dirp = NULL;
872 dp = opendir(VC_RUNTIME_INFO_ROOT);
874 SLOG(LOG_ERROR, TAG_VCD, "[File message WARN] Fail to open path : %s", VC_RUNTIME_INFO_ROOT);
878 char remove_path[256] = {0, };
880 ret = readdir_r(dp, &entry, &dirp);
882 SLOG(LOG_ERROR, TAG_VCD, "[File ERROR] Fail to read directory");
887 if (!strncmp("vc_", dirp->d_name, strlen("vc_"))) {
888 memset(remove_path, 0, 256);
889 snprintf(remove_path, 256, "%s/%s", VC_RUNTIME_INFO_ROOT, dirp->d_name);
892 if (0 != remove(remove_path)) {
893 SLOG(LOG_WARN, TAG_VCD, "[File message WARN] Fail to remove file : %s", remove_path);
895 SLOG(LOG_DEBUG, TAG_VCD, "[File message] Remove file : %s", remove_path);
899 } while (NULL != dirp);
906 static int __vcd_db_clean_up()
909 int cnt = VC_COMMAND_TYPE_FOREGROUND;
911 if (VC_COMMAND_TYPE_BACKGROUND != cnt)
912 ret = vc_db_delete_commands(-1, cnt, NULL);
913 } while (VC_COMMAND_TYPE_EXCLUSIVE >= ++cnt);
918 static void __sig_handler(int signo)
920 /* restore signal handler */
921 signal(signo, SIG_DFL);
922 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR]");
924 if (0 == vcd_check_dbus_connection()) {
925 /* Send error signal for notifying that daemon is reset*/
926 vcdc_send_error_signal(VCD_ERROR_SERVICE_RESET, "Daemon reset");
929 /* invoke signal again */
933 static void __register_sig_handler()
935 signal(SIGSEGV, __sig_handler);
936 signal(SIGABRT, __sig_handler);
937 signal(SIGTERM, __sig_handler);
938 signal(SIGINT, __sig_handler);
939 signal(SIGQUIT, __sig_handler);
940 signal(SIGILL, __sig_handler);
941 signal(SIGBUS, __sig_handler);
944 void vcd_unregister_signal()
946 signal(SIGSEGV, SIG_IGN);
947 signal(SIGABRT, SIG_IGN);
948 signal(SIGTERM, SIG_IGN);
949 signal(SIGINT, SIG_IGN);
950 signal(SIGQUIT, SIG_IGN);
951 signal(SIGILL, SIG_IGN);
952 signal(SIGBUS, SIG_IGN);
959 __register_sig_handler();
961 /* Remove old file */
962 __vcd_file_clean_up();
964 /* initialize modules */
965 ret = vcd_config_initialize(__config_lang_changed_cb, __config_foreground_changed_cb, NULL);
967 SLOG(LOG_ERROR, TAG_VCD, "[Server WARNING] Fail to initialize config.");
971 ret = __vcd_db_clean_up();
973 SLOG(LOG_ERROR, TAG_VCD, "[Server WARNING] Fail to remove db data");
976 vcd_config_set_service_state(VCD_STATE_NONE);
978 //ret = vcd_engine_agent_init(__vcd_server_pre_result_cb, __vcd_server_result_cb, __vcd_server_nlu_result_cb, __vcd_server_error_cb);
979 ret = vcd_engine_agent_init(__vcd_server_asr_result_cb, __vcd_server_result_cb, __vcd_server_error_cb);
981 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to engine agent initialize : result(%d)", ret);
985 if (0 != vcd_recorder_create(__server_recorder_callback, __server_recorder_interrupt_callback)) {
986 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to create recorder");
987 return VCD_ERROR_OPERATION_FAILED;
991 ret = vcd_engine_agent_initialize_current_engine();
993 if (VCD_ERROR_ENGINE_NOT_FOUND == ret)
994 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] There is No Voice control engine");
996 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to init engine");
1004 if (0 != vcd_engine_agent_load_current_engine()) {
1005 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to load current engine");
1006 return VCD_ERROR_OPERATION_FAILED;
1009 /* Initialize manager info */
1010 vcd_client_manager_unset();
1012 vcd_config_set_service_state(VCD_STATE_READY);
1013 vcdc_send_service_state(VCD_STATE_READY);
1015 SLOG(LOG_DEBUG, TAG_VCD, "[Server SUCCESS] initialize");
1023 if (0 < g_list_length(g_proc_list)) {
1024 iter = g_list_first(g_proc_list);
1025 while (NULL != iter) {
1026 g_proc_list = g_list_remove_link(g_proc_list, iter);
1027 iter = g_list_first(g_proc_list);
1031 if (g_restart_timer != NULL) {
1032 ecore_timer_del(g_restart_timer);
1033 g_restart_timer = NULL;
1036 vcd_state_e state = vcd_config_get_service_state();
1037 if (VCD_STATE_READY != state) {
1038 if (VCD_STATE_RECORDING == state) {
1039 vcd_recorder_stop();
1041 vcd_engine_recognize_cancel();
1043 if (0 != vcd_recorder_destroy()) {
1044 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to destroy recorder");
1046 SLOG(LOG_DEBUG, TAG_VCD, "[Server] destroy recorder");
1049 if (0 != vcd_engine_agent_release()) {
1050 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to release engine");
1052 SLOG(LOG_DEBUG, TAG_VCD, "[Server] release engine");
1055 vcd_client_manager_unset_appid();
1057 vcd_config_set_service_state(VCD_STATE_NONE);
1058 vcdc_send_service_state(VCD_STATE_NONE);
1060 SLOG(LOG_DEBUG, TAG_VCD, "[Server] mode finalize");
1065 static Eina_Bool __finalize_quit_ecore_loop(void *data)
1067 SLOG(LOG_DEBUG, TAG_VCD, "[Server] quit ecore main loop");
1068 ecore_main_loop_quit();
1072 static void __read_proc()
1075 struct dirent entry;
1076 struct dirent *dirp = NULL;
1081 if (0 < g_list_length(g_proc_list)) {
1082 iter = g_list_first(g_proc_list);
1083 while (NULL != iter) {
1084 g_proc_list = g_list_remove_link(g_proc_list, iter);
1085 iter = g_list_first(g_proc_list);
1089 dp = opendir("/proc");
1091 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Fail to open proc");
1094 ret = readdir_r(dp, &entry, &dirp);
1096 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Fail to readdir");
1101 tmp = atoi(dirp->d_name);
1102 if (0 >= tmp) continue;
1103 g_proc_list = g_list_append(g_proc_list, GINT_TO_POINTER(tmp));
1105 } while (NULL != dirp);
1111 static void __vcd_cleanup_client(vcd_client_type_e type)
1113 int* client_list = NULL;
1114 int client_count = 0;
1121 if (VCD_CLIENT_TYPE_NORMAL == type) {
1122 ret = vcd_client_get_list(&client_list, &client_count);
1123 } else if (VCD_CLIENT_TYPE_WIDGET == type) {
1124 ret = vcd_client_widget_get_list(&client_list, &client_count);
1125 } else if (VCD_CLIENT_TYPE_MANAGER == type) {
1126 mgr_pid = vcd_client_manager_get_pid();
1127 client_list = &mgr_pid;
1129 if (-1 == mgr_pid) {
1130 SLOG(LOG_WARN, TAG_VCD, "[WARNING] Invalid Manager pid");
1135 if (0 == ret || mgr_pid > 0) {
1136 SLOG(LOG_DEBUG, TAG_VCD, "===== Clean up %s client ", type ? (type == 1) ? "Widget" : "Manager" : "Normal");
1137 if (NULL != client_list && client_count > 0) {
1138 for (i = 0; i < client_count; i++) {
1141 for (j = 0; j < g_list_length(g_proc_list); j++) {
1142 iter = g_list_nth(g_proc_list, j);
1144 if (client_list[i] == GPOINTER_TO_INT(iter->data)) {
1145 SLOG(LOG_DEBUG, TAG_VCD, "%s pid(%d) is running", type ? (type == 1) ? "Widget" : "Manager" : "Normal", client_list[i]);
1152 if (false == exist) {
1153 SLOG(LOG_ERROR, TAG_VCD, "%s pid(%d) should be removed", type ? (type == 1) ? "Widget" : "Manager" : "Normal", client_list[i]);
1154 if (VCD_CLIENT_TYPE_NORMAL == type)
1155 vcd_server_finalize(client_list[i]);
1156 else if (VCD_CLIENT_TYPE_WIDGET == type)
1157 vcd_server_widget_finalize(client_list[i]);
1159 vcd_server_mgr_finalize(mgr_pid);
1163 SLOG(LOG_DEBUG, TAG_VCD, "=====");
1164 SLOG(LOG_DEBUG, TAG_VCD, " ");
1166 if (NULL != client_list && -1 == mgr_pid) {
1173 Eina_Bool vcd_cleanup_client_all(void *data)
1177 __vcd_cleanup_client(VCD_CLIENT_TYPE_NORMAL);
1178 __vcd_cleanup_client(VCD_CLIENT_TYPE_WIDGET);
1179 __vcd_cleanup_client(VCD_CLIENT_TYPE_MANAGER);
1182 if (0 == vcd_client_get_list(&client_list, &client_count)) {
1183 SLOG(LOG_DEBUG, TAG_VCD, "===== Clean up client ");
1184 if (NULL != client_list && client_count > 0) {
1185 for (i = 0; i < client_count; i++) {
1188 for (j = 0; j < g_list_length(g_proc_list); j++) {
1189 iter = g_list_nth(g_proc_list, j);
1191 if (client_list[i] == GPOINTER_TO_INT(iter->data)) {
1192 SLOG(LOG_DEBUG, TAG_VCD, "pid(%d) is running", client_list[i]);
1199 if (false == exist) {
1200 SLOG(LOG_ERROR, TAG_VCD, "pid(%d) should be removed", client_list[i]);
1201 vcd_server_finalize(client_list[i]);
1204 result = vcdc_send_hello(client_list[i], VCD_CLIENT_TYPE_NORMAL);
1207 SLOG(LOG_DEBUG, TAG_VCD, "[Server] pid(%d) should be removed.", client_list[i]);
1208 vcd_server_finalize(client_list[i]);
1209 } else if (-1 == result) {
1210 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Hello result has error");
1215 SLOG(LOG_DEBUG, TAG_VCD, "=====");
1216 SLOG(LOG_DEBUG, TAG_VCD, " ");
1218 if (NULL != client_list) {
1223 /* If app is in background state, app cannot response message. */
1224 if (0 == vcd_client_widget_get_list(&client_list, &client_count)) {
1225 SLOG(LOG_DEBUG, TAG_VCD, "===== Clean up widget");
1226 if (NULL != client_list && client_count > 0) {
1227 for (i = 0; i < client_count; i++) {
1230 for (j = 0; j < g_list_length(g_proc_list); j++) {
1231 iter = g_list_nth(g_proc_list, j);
1233 if (client_list[i] == GPOINTER_TO_INT(iter->data)) {
1234 SLOG(LOG_DEBUG, TAG_VCD, "widget pid(%d) is running", client_list[i]);
1241 if (false == exist) {
1242 SLOG(LOG_ERROR, TAG_VCD, "widget pid(%d) should be removed", client_list[i]);
1243 vcd_server_widget_finalize(client_list[i]);
1246 result = vcdc_send_hello(client_list[i], VCD_CLIENT_TYPE_WIDGET);
1249 SLOG(LOG_DEBUG, TAG_VCD, "[Server] widget pid(%d) should be removed.", client_list[i]);
1250 vcd_server_widget_finalize(client_list[i]);
1251 } else if (-1 == result) {
1252 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Hello result has error");
1257 SLOG(LOG_DEBUG, TAG_VCD, "=====");
1258 SLOG(LOG_DEBUG, TAG_VCD, " ");
1261 if (NULL != client_list) {
1269 int mgr_pid = vcd_client_manager_get_pid();
1271 for (j = 0; j < g_list_length(g_proc_list); j++) {
1272 iter = g_list_nth(g_proc_list, j);
1274 if (mgr_pid == GPOINTER_TO_INT(iter->data)) {
1275 SLOG(LOG_DEBUG, TAG_VCD, "manager pid(%d) is running", mgr_pid);
1282 if (false == exist) {
1283 SLOG(LOG_ERROR, TAG_VCD, "manager pid (%d) should be removed", mgr_pid);
1284 vcd_server_mgr_finalize(mgr_pid);
1291 int vcd_server_get_service_state()
1293 return vcd_config_get_service_state();
1296 int vcd_server_get_foreground()
1299 vcd_config_get_foreground(&pid);
1307 int vcd_server_mgr_initialize(int pid)
1309 if (false == g_is_engine) {
1310 if (0 != vcd_engine_agent_initialize_current_engine()) {
1311 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] No Engine");
1312 g_is_engine = false;
1313 return VCD_ERROR_ENGINE_NOT_FOUND;
1319 /* check if pid is valid */
1320 if (false == vcd_client_manager_is_valid(pid)) {
1321 SLOG(LOG_ERROR, TAG_VCD, "[Server] old manager pid(%d) be removed", vcd_client_manager_get_pid());
1322 vcd_server_mgr_cancel();
1323 vcd_client_manager_unset();
1326 /* Add client information to client manager */
1327 if (0 != vcd_client_manager_set(pid)) {
1328 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to add manager");
1329 return VCD_ERROR_OPERATION_FAILED;
1332 SLOG(LOG_DEBUG, TAG_VCD, "[Server Success] Manager initialize : pid(%d)", pid);
1334 return VCD_ERROR_NONE;
1337 int vcd_server_mgr_finalize(int pid)
1339 /* check if pid is valid */
1340 if (false == vcd_client_manager_is_valid(pid)) {
1341 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] The manager pid(%d) is NOT valid", pid);
1342 return VCD_ERROR_INVALID_PARAMETER;
1345 /* Cancel recognition */
1346 vcd_server_mgr_cancel();
1348 /* Remove manager information */
1349 if (0 != vcd_client_manager_unset()) {
1350 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Fail to delete client");
1353 if (0 == vcd_client_get_ref_count()) {
1354 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Connected client list is empty");
1355 ecore_timer_add(0, __finalize_quit_ecore_loop, NULL);
1358 SLOG(LOG_DEBUG, TAG_VCD, "[Server Success] Manager Finalize : pid(%d)", pid);
1360 return VCD_ERROR_NONE;
1363 int vcd_server_mgr_set_command(int pid)
1365 if (0 != vcd_client_manager_set_command(pid)) {
1366 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] The manager pid(%d) is NOT valid", pid);
1367 return VCD_ERROR_INVALID_PARAMETER;
1369 return VCD_ERROR_NONE;
1372 int vcd_server_mgr_unset_command(int pid)
1374 if (0 != vcd_client_manager_unset_command(pid)) {
1375 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] The manager pid(%d) is NOT valid", pid);
1376 return VCD_ERROR_INVALID_PARAMETER;
1378 return VCD_ERROR_NONE;
1381 int vcd_server_mgr_set_demandable_client(int pid)
1383 /* check if pid is valid */
1384 if (false == vcd_client_manager_is_valid(pid)) {
1385 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] The manager pid(%d) is NOT valid", pid);
1386 return VCD_ERROR_INVALID_PARAMETER;
1389 GSList* client_list = NULL;
1390 if (0 != vc_info_parser_get_demandable_clients(&client_list)) {
1391 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to get demandable client");
1392 return VCD_ERROR_OPERATION_FAILED;
1395 /* Save client list */
1396 if (0 != vcd_client_manager_set_demandable_client(pid, client_list)) {
1397 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to set demandable client list");
1398 return VCD_ERROR_OPERATION_FAILED;
1401 return VCD_ERROR_NONE;
1404 int vcd_server_mgr_set_audio_type(int pid, const char* audio_type)
1406 /* check if pid is valid */
1407 if (false == vcd_client_manager_is_valid(pid)) {
1408 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] The manager pid(%d) is NOT valid", pid);
1409 return VCD_ERROR_INVALID_PARAMETER;
1413 vcp_audio_type_e type = VCP_AUDIO_TYPE_PCM_S16_LE;
1417 ret = vcd_engine_get_audio_format(audio_type, &type, &rate, &channel);
1419 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to get audio format : %d", ret);
1420 return VCD_ERROR_OPERATION_FAILED;
1423 ret = vcd_recorder_set(audio_type, type, rate, channel);
1425 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to set audio in type : %d", ret);
1426 return VCD_ERROR_OPERATION_FAILED;
1429 return VCD_ERROR_NONE;
1432 int vcd_server_mgr_get_audio_type(int pid, char** audio_type)
1434 /* check if pid is valid */
1435 if (false == vcd_client_manager_is_valid(pid)) {
1436 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] The manager pid(%d) is NOT valid", pid);
1437 return VCD_ERROR_INVALID_PARAMETER;
1440 int ret = vcd_recorder_get(audio_type);
1442 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to get audio in type : %d", ret);
1443 return VCD_ERROR_OPERATION_FAILED;
1446 return VCD_ERROR_NONE;
1449 int vcd_server_mgr_set_client_info(int pid)
1451 /* check if pid is valid */
1452 if (false == vcd_client_manager_is_valid(pid)) {
1453 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] The manager pid(%d) is NOT valid", pid);
1454 return VCD_ERROR_INVALID_PARAMETER;
1457 int ret = vcd_client_save_client_info();
1459 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to save client info : %d", ret);
1460 return VCD_ERROR_OPERATION_FAILED;
1463 return VCD_ERROR_NONE;
1466 static int __start_internal_recognition()
1470 /* 2. Get commands */
1471 ret = vcd_client_command_collect_command();
1473 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to collect command : %d", ret);
1474 return VCD_ERROR_OPERATION_FAILED;
1477 ret = vcd_client_get_length();
1479 SLOG(LOG_WARN, TAG_VCD, "[Server WARNIING] No current command : %d", ret);
1480 return VCD_ERROR_OPERATION_FAILED;
1483 /* 3. Set command to engine */
1484 ret = vcd_engine_set_commands();
1486 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to collect command : %d", ret);
1487 return VCD_ERROR_OPERATION_FAILED;
1490 SLOG(LOG_DEBUG, TAG_VCD, "[Server Success] Set command");
1492 bool stop_by_silence = true;
1493 if (VCD_RECOGNITION_MODE_MANUAL == vcd_client_get_recognition_mode()) {
1494 stop_by_silence = false;
1497 vcd_client_manager_set_result_text(NULL);
1499 /* 4. start recognition */
1500 ret = vcd_engine_recognize_start(stop_by_silence);
1502 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to start recognition : result(%d)", ret);
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();
1516 vcd_config_set_service_state(VCD_STATE_RECORDING);
1517 vcdc_send_service_state(VCD_STATE_RECORDING);
1519 SLOG(LOG_DEBUG, TAG_VCD, "[Server Success] Start recognition(%d)", stop_by_silence);
1524 static Eina_Bool __vcd_request_show_tooltip(void *data)
1526 int pid = vcd_client_widget_get_foreground_pid();
1528 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Request tooltip show and widget command");
1529 vcdc_send_show_tooltip(pid, (bool)data);
1535 int vcd_server_mgr_start(vcd_recognition_mode_e recognition_mode, bool exclusive_cmd, bool start_by_client)
1537 /* 1. check current state */
1538 vcd_state_e state = vcd_config_get_service_state();
1540 if (VCD_STATE_READY != state) {
1541 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Current state is not ready");
1542 return VCD_ERROR_INVALID_STATE;
1544 if (-1 == vcd_client_manager_get_pid()) {
1545 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Manager is NOT available.");
1546 return VCD_ERROR_OPERATION_FAILED;
1549 SLOG(LOG_DEBUG, TAG_VCD, "[Server] set recognition mode = %d", recognition_mode);
1550 vcd_client_set_recognition_mode(recognition_mode);
1552 if (false == exclusive_cmd) {
1553 /* Notify show tooltip */
1554 int pid = vcd_client_widget_get_foreground_pid();
1556 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Request tooltip show and widget command");
1557 ecore_timer_add(0, __vcd_request_show_tooltip, (void*)true);
1561 vcd_client_manager_set_exclusive(exclusive_cmd);
1565 if (true == start_by_client) {
1566 /* Get foreground pid */
1567 if (0 != vcd_config_get_foreground(&fg_pid)) {
1568 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to get foreground");
1571 /* Set client exclusive option */
1572 if (0 != vcd_client_set_exclusive_command(fg_pid)) {
1573 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to set exclusive command");
1577 int ret = __start_internal_recognition();
1579 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to start recongition : %d", ret);
1583 if (true == start_by_client) {
1584 vcd_client_unset_exclusive_command(fg_pid);
1587 return VCD_ERROR_NONE;
1590 int vcd_server_mgr_stop()
1592 /* 1. Check current state is recording */
1593 if (VCD_STATE_RECORDING != vcd_config_get_service_state()) {
1594 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Current state is not recording");
1595 return VCD_ERROR_INVALID_STATE;
1597 if (-1 == vcd_client_manager_get_pid()) {
1598 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Manager is NOT available.");
1599 return VCD_ERROR_OPERATION_FAILED;
1602 /* 2. Stop recorder */
1603 vcd_recorder_stop();
1605 /* 3. Stop engine recognition */
1606 int ret = vcd_engine_recognize_stop();
1608 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to stop recognition : %d", ret);
1611 /* 4. Set original mode */
1612 vcd_config_set_service_state(VCD_STATE_PROCESSING);
1613 vcdc_send_service_state(VCD_STATE_PROCESSING);
1615 return VCD_ERROR_NONE;
1618 int vcd_server_mgr_cancel()
1620 /* 1. Check current state */
1621 vcd_state_e state = vcd_config_get_service_state();
1622 if (VCD_STATE_RECORDING != state && VCD_STATE_PROCESSING != state) {
1623 SLOG(LOG_WARN, TAG_VCD, "[Server ERROR] Current state is not recording or processing");
1624 return VCD_ERROR_INVALID_STATE;
1626 if (-1 == vcd_client_manager_get_pid()) {
1627 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Manager is NOT available.");
1628 return VCD_ERROR_OPERATION_FAILED;
1631 if (g_restart_timer != NULL) {
1632 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Delete restart engine timer");
1633 ecore_timer_del(g_restart_timer);
1634 g_restart_timer = NULL;
1637 /* 2. Stop recorder */
1638 vcd_recorder_stop();
1639 /* 3. Cancel engine */
1640 int ret = vcd_engine_recognize_cancel();
1642 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Fail to cancel : %d", ret);
1645 if (false == vcd_client_manager_get_exclusive()) {
1646 int pid = vcd_client_widget_get_foreground_pid();
1648 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Request tooltip hide");
1649 ecore_timer_add(0, __vcd_request_show_tooltip, (void*)false);
1652 vcd_client_manager_set_exclusive(false);
1656 vcd_config_set_service_state(VCD_STATE_READY);
1657 vcdc_send_service_state(VCD_STATE_READY);
1659 return VCD_ERROR_NONE;
1663 int vcd_server_mgr_result_select()
1665 __vcd_send_selected_result(NULL);
1667 return VCD_ERROR_NONE;
1670 int vcd_server_mgr_set_domain(int pid, const char* domain)
1672 /* check if pid is valid */
1673 if (false == vcd_client_manager_is_valid(pid)) {
1674 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] The manager pid(%d) is NOT valid", pid);
1675 return VCD_ERROR_INVALID_PARAMETER;
1678 vcd_state_e state = vcd_config_get_service_state();
1679 if (VCD_STATE_READY != state) {
1680 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Current state is not ready");
1681 return VCD_ERROR_INVALID_STATE;
1684 /* Set domain to engine */
1685 int ret = vcd_engine_set_domain(pid, domain);
1687 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to set domain : %d", ret);
1689 SLOG(LOG_DEBUG, TAG_VCD, "[Server SUCCESS] Set domain");
1695 int vcd_server_mgr_set_private_data(int pid, const char* key, const char* data)
1697 /* check if pid is valid */
1698 if (false == vcd_client_manager_is_valid(pid)) {
1699 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] The manager pid(%d) is NOT valid", pid);
1700 return VCD_ERROR_INVALID_PARAMETER;
1703 vcd_state_e state = vcd_config_get_service_state();
1704 if (VCD_STATE_READY != state) {
1705 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Current state is not ready");
1706 return VCD_ERROR_INVALID_STATE;
1709 /* Set private data to engine */
1710 int ret = vcd_engine_set_private_data(pid, key, data);
1712 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to set private data : %d", ret);
1714 SLOG(LOG_DEBUG, TAG_VCD, "[Server SUCCESS] Set private data");
1720 int vcd_server_mgr_get_private_data(int pid, const char* key, char** data)
1722 /* check if pid is valid */
1723 if (false == vcd_client_manager_is_valid(pid)) {
1724 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] The manager pid(%d) is NOT valid", pid);
1725 return VCD_ERROR_INVALID_PARAMETER;
1727 vcd_state_e state = vcd_config_get_service_state();
1728 if (VCD_STATE_READY != state) {
1729 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Current state is not ready");
1730 return VCD_ERROR_INVALID_STATE;
1733 /* Get private data to engine */
1734 int ret = vcd_engine_get_private_data(pid, key, data);
1736 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to get private data : %d", ret);
1738 SLOG(LOG_DEBUG, TAG_VCD, "[Server SUCCESS] Set private data");
1744 int vcd_server_mgr_do_action(int pid, int type, const char* action)
1748 /* check if pid is valid */
1749 if (false == vcd_client_manager_is_valid(pid)) {
1750 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] The manager pid(%d) is NOT valid", pid);
1751 return VCD_ERROR_INVALID_PARAMETER;
1754 vcd_state_e state = vcd_config_get_service_state();
1755 if (VCD_STATE_READY != state) {
1756 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Current state is not ready");
1757 return VCD_ERROR_INVALID_STATE;
1760 /* Reqeust do action to engine */
1761 if (VCD_SEND_EVENT_TYPE_TEXT == type)
1762 ret = vcd_engine_process_text(pid, action);
1763 else if (VCD_SEND_EVENT_TYPE_LIST_EVENT == type)
1764 ret = vcd_engine_process_list_event(pid, action);
1765 else if (VCD_SEND_EVENT_TYPE_HAPTIC_EVENT == type)
1766 ret = vcd_engine_process_haptic_event(pid, action);
1769 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to process do action : %d", ret);
1771 SLOG(LOG_DEBUG, TAG_VCD, "[Server SUCCESS] Process do action");
1778 * VC Server Functions for Client
1780 int vcd_server_initialize(int pid)
1782 if (false == g_is_engine) {
1783 if (0 != vcd_engine_agent_initialize_current_engine()) {
1784 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] No Engine");
1785 g_is_engine = false;
1786 return VCD_ERROR_ENGINE_NOT_FOUND;
1792 if (false == vcd_engine_is_available_engine()) {
1793 if (0 != vcd_engine_agent_initialize_current_engine()) {
1794 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] No Engine");
1795 return VCD_ERROR_ENGINE_NOT_FOUND;
1799 /* check if pid is valid */
1800 if (true == vcd_client_is_available(pid)) {
1801 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] The pid is already exist");
1802 return VCD_ERROR_INVALID_PARAMETER;
1805 /* Add client information to client manager */
1806 if (0 != vcd_client_add(pid)) {
1807 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to add client info");
1808 return VCD_ERROR_OPERATION_FAILED;
1811 SLOG(LOG_DEBUG, TAG_VCD, "[Server Success] Client Initialize : pid(%d)", pid);
1813 return VCD_ERROR_NONE;
1816 int vcd_server_finalize(int pid)
1818 /* check if pid is valid */
1819 if (false == vcd_client_is_available(pid)) {
1820 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] pid is NOT valid ");
1821 return VCD_ERROR_INVALID_PARAMETER;
1824 /* Remove client information */
1825 if (0 != vcd_client_delete(pid)) {
1826 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Fail to delete client");
1829 if (0 == vcd_client_get_ref_count()) {
1830 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Connected client list is empty");
1831 ecore_timer_add(0, __finalize_quit_ecore_loop, NULL);
1834 SLOG(LOG_DEBUG, TAG_VCD, "[Server Success] Client Finalize : pid(%d)", pid);
1836 return VCD_ERROR_NONE;
1839 int vcd_server_set_command(int pid, vc_cmd_type_e cmd_type)
1841 /* check if pid is valid */
1842 if (false == vcd_client_is_available(pid)) {
1843 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] pid is NOT valid ");
1844 return VCD_ERROR_INVALID_PARAMETER;
1847 if (0 != vcd_client_set_command_type(pid, cmd_type)) {
1848 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to set command type : pid(%d), cmd_type(%d)", pid, cmd_type);
1849 return VCD_ERROR_OPERATION_FAILED;
1855 int vcd_server_unset_command(int pid, vc_cmd_type_e cmd_type)
1857 /* check if pid is valid */
1858 if (false == vcd_client_is_available(pid)) {
1859 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] pid is NOT valid ");
1860 return VCD_ERROR_INVALID_PARAMETER;
1863 if (0 != vcd_client_unset_command_type(pid, cmd_type)) {
1864 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to unset command type : pid(%d), cmd_type(%d)", pid, cmd_type);
1865 return VCD_ERROR_OPERATION_FAILED;
1871 int vcd_server_set_foreground(int pid, bool value)
1873 /* check if pid is valid */
1874 if (false == vcd_client_is_available(pid) && false == vcd_client_widget_is_available(pid)) {
1875 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] pid is NOT valid ");
1876 return VCD_ERROR_INVALID_PARAMETER;
1879 if (0 != vcd_config_set_foreground(pid, value)) {
1880 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to set foreground : pid(%d), value(%d)", pid, value);
1881 return VCD_ERROR_OPERATION_FAILED;
1887 static int __vcd_server_launch_manager_app()
1890 bool running = false;
1893 ret = vcd_client_manager_get_appid(&appid);
1894 if (0 != ret || NULL == appid) {
1895 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Fail to get manager appid");
1896 return VCD_ERROR_OPERATION_FAILED;
1898 ret = app_manager_is_running(appid, &running);
1900 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Fail to check running with appid(%s)", appid);
1903 return VCD_ERROR_OPERATION_FAILED;
1905 if (false == running) {
1906 int tmp_ret = __vcd_is_package_installed(appid);
1907 if (false == tmp_ret) {
1908 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] manager app is not installed, appid(%s)", appid);
1910 ret = __vcd_activate_app_by_appcontrol(appid);
1912 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Fail to activate app");
1917 SLOG(LOG_ERROR, TAG_VCD, "Launch vc manager app: appid(%s)", appid);
1920 ret = __vcd_resume_app(appid);
1922 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Fail to resume app");
1927 SLOG(LOG_ERROR, TAG_VCD, "Resume vc manager app: appid(%s)", appid);
1933 return VCD_ERROR_NONE;
1936 int vcd_server_dialog(int pid, const char* disp_text, const char* utt_text, int continuous)
1938 /* check if pid is valid */
1939 if (false == vcd_client_is_available(pid) && false == vcd_client_widget_is_available(pid)) {
1940 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] pid is NOT valid ");
1941 return VCD_ERROR_INVALID_PARAMETER;
1944 int ret = __vcd_server_launch_manager_app();
1946 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);
1950 ret = vcdc_send_dialog(vcd_client_manager_get_pid(), pid, disp_text, utt_text, continuous);
1952 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);
1959 int vcd_server_is_system_command_valid(int pid, int* is_sys_cmd_valid)
1961 /* check if pid is valid */
1962 if (false == vcd_client_is_available(pid) && false == vcd_client_widget_is_available(pid)) {
1963 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] pid is NOT valid ");
1964 return VCD_ERROR_INVALID_PARAMETER;
1967 /* check if system command is valid */
1968 if (true == vcd_client_manager_is_system_command_valid(vcd_client_manager_get_pid()))
1969 *is_sys_cmd_valid = true;
1971 *is_sys_cmd_valid = false;
1977 int vcd_server_set_exclusive_command(int pid, bool value)
1979 /* check if pid is valid */
1980 if (false == vcd_client_is_available(pid)) {
1981 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] pid is NOT valid ");
1982 return VCD_ERROR_INVALID_PARAMETER;
1985 if (true == value) {
1986 if (0 != vcd_client_set_exclusive_command(pid)) {
1987 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to set exclusive command : pid(%d)", pid);
1988 return VCD_ERROR_OPERATION_FAILED;
1991 if (0 != vcd_client_unset_exclusive_command(pid)) {
1992 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to unset exclusive command : pid(%d)", pid);
1993 return VCD_ERROR_OPERATION_FAILED;
2000 int vcd_server_request_start(int pid, bool stop_by_silence)
2002 /* check if pid is valid */
2003 if (false == vcd_client_is_available(pid)) {
2004 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] pid(%d) is NOT forground client", pid);
2005 return VCD_ERROR_INVALID_PARAMETER;
2009 /* Check current state */
2010 vcd_state_e state = vcd_config_get_service_state();
2012 /* Service state should be ready */
2013 if (VCD_STATE_READY != state) {
2014 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Current state is not Ready : pid(%d)", pid);
2015 return VCD_ERROR_INVALID_STATE;
2018 if (-1 != vcd_client_manager_get_pid()) {
2019 /* Check current pid is valid */
2020 if (false == vcd_client_manager_check_demandable_client(pid)) {
2021 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Current client is NOT available : pid(%d)", pid);
2022 return VCD_ERROR_INVALID_PARAMETER;
2026 ret = vcd_server_mgr_start(stop_by_silence, false);
2028 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Fail to start recognition");
2029 return VCD_ERROR_INVALID_PARAMETER;
2035 int vcd_server_request_stop(int pid)
2038 /* Check current state */
2039 vcd_state_e state = vcd_config_get_service_state();
2041 /* Service state should be ready */
2042 if (VCD_STATE_RECORDING != state) {
2043 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Current state is not Recording : pid(%d)", pid);
2044 return VCD_ERROR_INVALID_STATE;
2047 if (-1 != vcd_client_manager_get_pid()) {
2048 /* Check current pid is valid */
2049 if (false == vcd_client_manager_check_demandable_client(pid)) {
2050 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Current client is NOT available : pid(%d)", pid);
2051 return VCD_ERROR_INVALID_PARAMETER;
2055 ret = vcd_server_mgr_stop();
2057 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Fail to start recognition");
2058 return VCD_ERROR_OPERATION_FAILED;
2061 return VCD_ERROR_NONE;
2064 int vcd_server_request_cancel(int pid)
2067 /* Check current state */
2068 vcd_state_e state = vcd_config_get_service_state();
2070 /* Service state should be recording or processing */
2071 if (VCD_STATE_RECORDING != state && VCD_STATE_PROCESSING != state) {
2072 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Current state is not Recording or Processing : pid(%d)", pid);
2073 return VCD_ERROR_INVALID_STATE;
2076 if (-1 != vcd_client_manager_get_pid()) {
2077 /* Check current pid is valid */
2078 if (false == vcd_client_manager_check_demandable_client(pid)) {
2079 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Current client is NOT available : pid(%d)", pid);
2080 return VCD_ERROR_INVALID_PARAMETER;
2084 ret = vcd_server_mgr_cancel();
2086 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Fail to start recognition");
2087 return VCD_ERROR_OPERATION_FAILED;
2090 return VCD_ERROR_NONE;
2095 * VC Server Functions for Widget lib
2097 int vcd_server_widget_initialize(int pid)
2099 if (false == g_is_engine) {
2100 if (0 != vcd_engine_agent_initialize_current_engine()) {
2101 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] No Engine");
2102 g_is_engine = false;
2103 return VCD_ERROR_ENGINE_NOT_FOUND;
2109 if (false == vcd_engine_is_available_engine()) {
2110 if (0 != vcd_engine_agent_initialize_current_engine()) {
2111 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] No Engine");
2112 return VCD_ERROR_ENGINE_NOT_FOUND;
2116 /* check if pid is valid */
2117 if (true == vcd_client_widget_is_available(pid)) {
2118 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] The pid is already exist");
2119 return VCD_ERROR_INVALID_PARAMETER;
2122 /* Add client information to client manager */
2123 if (0 != vcd_client_widget_add(pid)) {
2124 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to add client info");
2125 return VCD_ERROR_OPERATION_FAILED;
2128 SLOG(LOG_DEBUG, TAG_VCD, "[Server Success] Initialize widget : pid(%d)", pid);
2130 return VCD_ERROR_NONE;
2133 int vcd_server_widget_finalize(int pid)
2135 /* check if pid is valid */
2136 if (false == vcd_client_widget_is_available(pid)) {
2137 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] pid is NOT valid ");
2138 return VCD_ERROR_INVALID_PARAMETER;
2141 /* Remove client information */
2142 if (0 != vcd_client_widget_delete(pid)) {
2143 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Fail to delete client");
2146 if (0 == vcd_client_get_ref_count()) {
2147 SLOG(LOG_DEBUG, TAG_VCD, "[Server] connected client list is empty");
2148 ecore_timer_add(0, __finalize_quit_ecore_loop, NULL);
2151 return VCD_ERROR_NONE;
2154 int vcd_server_widget_start_recording(int pid, bool widget_command)
2156 /* check if pid is valid */
2157 if (false == vcd_client_widget_is_available(pid)) {
2158 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] pid is NOT valid ");
2159 return VCD_ERROR_INVALID_PARAMETER;
2162 if (true == widget_command) {
2163 vcd_client_widget_set_command(pid);
2164 SLOG(LOG_DEBUG, TAG_VCD, "[Server] widget command is available");
2166 vcd_client_widget_unset_command(pid);
2167 SLOG(LOG_WARN, TAG_VCD, "[Server] widget command is NOT available");
2170 int ret = __start_internal_recognition();
2172 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to start recongition : %d", ret);
2173 ecore_timer_add(0, __vcd_request_show_tooltip, (void*)false);
2179 int vcd_server_widget_start(int pid, bool stop_by_silence)
2181 /* check if pid is valid */
2182 int fore_pid = vcd_client_widget_get_foreground_pid();
2183 if (pid != fore_pid) {
2184 SLOG(LOG_ERROR, TAG_VCD, "[Server] pid is NOT foreground");
2185 return VCD_ERROR_INVALID_PARAMETER;
2188 /* Check current state */
2189 vcd_state_e state = vcd_config_get_service_state();
2191 /* Service state should be ready */
2192 if (VCD_STATE_READY != state) {
2193 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Current state is not Ready : pid(%d)", pid);
2194 return VCD_ERROR_INVALID_STATE;
2197 vcd_client_set_slience_detection(stop_by_silence);
2199 /* Notify show tooltip */
2200 ecore_timer_add(0, __vcd_request_show_tooltip, (void*)true);
2205 int vcd_server_widget_stop(int pid)
2207 /* check if pid is valid */
2208 int fore_pid = vcd_client_widget_get_foreground_pid();
2209 if (pid != fore_pid) {
2210 SLOG(LOG_ERROR, TAG_VCD, "[Server] pid is NOT foreground");
2211 return VCD_ERROR_INVALID_PARAMETER;
2215 /* Check current state */
2216 vcd_state_e state = vcd_config_get_service_state();
2218 /* Service state should be recording */
2219 if (VCD_STATE_RECORDING != state) {
2220 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Current service state is not Recording : pid(%d)", pid);
2221 return VCD_ERROR_INVALID_STATE;
2224 ret = vcd_server_mgr_stop();
2226 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Fail to start recognition");
2227 return VCD_ERROR_OPERATION_FAILED;
2230 return VCD_ERROR_NONE;
2233 int vcd_server_widget_cancel(int pid)
2235 /* check if pid is valid */
2236 int fore_pid = vcd_client_widget_get_foreground_pid();
2237 if (pid != fore_pid) {
2238 SLOG(LOG_ERROR, TAG_VCD, "[Server] pid is NOT foreground");
2239 return VCD_ERROR_INVALID_PARAMETER;
2243 /* Check current state */
2244 vcd_state_e state = vcd_config_get_service_state();
2246 /* Service state should be recording or processing */
2247 if (VCD_STATE_RECORDING != state && VCD_STATE_PROCESSING != state) {
2248 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Current state is not Recording or Processing : pid(%d)", pid);
2249 return VCD_ERROR_INVALID_STATE;
2252 ret = vcd_server_mgr_cancel();
2254 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Fail to cancel recognition : %d", ret);
2258 return VCD_ERROR_NONE;