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 <sound_manager.h>
20 #include "vc_info_parser.h"
22 #include "vcd_server.h"
23 #include "vcd_client_data.h"
25 #include "vcd_engine_agent.h"
26 #include "vcd_config.h"
27 #include "vcd_recorder.h"
30 #include "voice_control_command_expand.h"
33 * VC Server static variable
35 static bool g_is_engine;
37 static GList *g_proc_list = NULL;
39 static Ecore_Timer *g_restart_timer = NULL;
42 * VC Server Internal Functions
44 static Eina_Bool __stop_by_silence(void *data)
46 SLOG(LOG_DEBUG, TAG_VCD, "===== Silence Detected ");
48 vcd_server_mgr_stop();
50 SLOG(LOG_DEBUG, TAG_VCD, "=====");
51 SLOG(LOG_DEBUG, TAG_VCD, " ");
55 static Eina_Bool __cancel_by_interrupt(void *data)
57 SLOG(LOG_DEBUG, TAG_VCD, "===== Cancel by interrupt");
59 vcd_server_mgr_cancel();
61 SLOG(LOG_DEBUG, TAG_VCD, "=====");
62 SLOG(LOG_DEBUG, TAG_VCD, " ");
66 static Eina_Bool __restart_engine(void *data)
68 SLOG(LOG_DEBUG, TAG_VCD, "===== Restart by no result");
70 g_restart_timer = NULL;
72 /* Restart recognition */
73 int ret = vcd_engine_recognize_start(true);
75 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to restart recognition : result(%d)", ret);
79 SLOG(LOG_DEBUG, TAG_VCD, "[Server Success] Start engine");
81 if (VCD_RECOGNITION_MODE_RESTART_AFTER_REJECT == vcd_client_get_recognition_mode()) {
82 vcd_config_set_service_state(VCD_STATE_RECORDING);
83 vcdc_send_service_state(VCD_STATE_RECORDING);
86 SLOG(LOG_DEBUG, TAG_VCD, "[Server Success] Restart recognition");
88 SLOG(LOG_DEBUG, TAG_VCD, "=====");
89 SLOG(LOG_DEBUG, TAG_VCD, " ");
93 static int __server_recorder_callback(const void* data, const unsigned int length)
95 vcd_state_e state = vcd_config_get_service_state();
96 if (VCD_STATE_RECORDING != state) {
97 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Not recording state, but recording");
101 vcp_speech_detect_e speech_detected = VCP_SPEECH_DETECT_NONE;
104 ret = vcd_engine_recognize_audio(data, length, &speech_detected);
108 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to set recording data to engine(%d)", ret);
109 ecore_timer_add(0, __cancel_by_interrupt, NULL);
110 /* Send error cb to manager */
111 if (VCP_ERROR_OUT_OF_NETWORK == ret) {
112 vcdc_send_error_signal_to_manager(vcd_client_manager_get_pid(), VCD_ERROR_TIMED_OUT, "Engine connection failed");
114 vcdc_send_error_signal_to_manager(vcd_client_manager_get_pid(), VCD_ERROR_OPERATION_FAILED, "Engine recognition failed");
119 if (VCP_SPEECH_DETECT_BEGIN == speech_detected) {
120 if (-1 != vcd_client_manager_get_pid()) {
121 /* Manager client is available */
122 if (0 != vcdc_send_speech_detected(vcd_client_manager_get_pid())) {
123 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Fail to send speech detected");
126 } else if (VCP_SPEECH_DETECT_END == speech_detected) {
127 if (VCD_RECOGNITION_MODE_STOP_BY_SILENCE == vcd_client_get_recognition_mode()) {
128 /* silence detected */
129 ecore_timer_add(0, __stop_by_silence, NULL);
130 } else if (VCD_RECOGNITION_MODE_RESTART_AFTER_REJECT == vcd_client_get_recognition_mode()) {
131 /* Stop engine recognition */
132 int ret = vcd_engine_recognize_stop();
134 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to stop recognition : %d", ret);
136 vcd_config_set_service_state(VCD_STATE_PROCESSING);
137 vcdc_send_service_state(VCD_STATE_PROCESSING);
139 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Stop engine only by silence");
140 } else if (VCD_RECOGNITION_MODE_RESTART_CONTINUOUSLY == vcd_client_get_recognition_mode()) {
141 /* Stop engine recognition */
142 int ret = vcd_engine_recognize_stop();
144 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to stop recognition : %d", ret);
152 void __server_recorder_interrupt_callback()
154 SLOG(LOG_DEBUG, TAG_VCD, "===== Cancel by sound interrupt");
156 ecore_timer_add(0, __cancel_by_interrupt, NULL);
158 SLOG(LOG_DEBUG, TAG_VCD, "=====");
159 SLOG(LOG_DEBUG, TAG_VCD, " ");
162 static void __config_lang_changed_cb(const char* current_lang, void* user_data)
164 SLOG(LOG_DEBUG, TAG_VCD, "===== Change language ");
166 /* Current state is recording */
167 vcd_state_e state = vcd_config_get_service_state();
168 if (VCD_STATE_RECORDING == state || VCD_STATE_PROCESSING == state) {
169 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Current state is 'Recording'. Cancel recognition");
170 vcd_server_mgr_cancel();
174 ret = vcd_engine_set_current_language(current_lang);
176 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to set language of engine : %d", ret);
179 SLOG(LOG_DEBUG, TAG_VCD, "=====");
180 SLOG(LOG_DEBUG, TAG_VCD, " ");
185 static void __config_foreground_changed_cb(int previous, int current, void* user_data)
187 SLOG(LOG_DEBUG, TAG_VCD, "===== Change foreground");
189 SLOG(LOG_DEBUG, TAG_VCD, "Foreground pid(%d)", current);
191 if (VC_NO_FOREGROUND_PID != current) {
192 /* Foreground app is changed */
193 vcd_state_e state = vcd_config_get_service_state();
194 if (VCD_STATE_RECORDING == state) {
195 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Foreground pid(%d) is changed. Cancel recognition", current);
196 ecore_timer_add(0, __cancel_by_interrupt, NULL);
200 SLOG(LOG_DEBUG, TAG_VCD, "=====");
201 SLOG(LOG_DEBUG, TAG_VCD, " ");
206 static Eina_Bool __vcd_send_selected_result(void *data)
208 GSList* pid_list = NULL;
209 if (0 != vc_info_parser_get_result_pid_list(&pid_list)) {
210 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Fail to get pid list. No result");
212 if (0 < g_slist_length(pid_list)) {
214 vc_cmd_s* temp_cmd = NULL;
218 iter = g_slist_nth(pid_list, 0);
219 while (NULL != iter) {
220 temp_cmd = iter->data;
222 if (NULL != temp_cmd) {
225 /* send result noti */
226 ret = vcdc_send_result(temp_cmd->pid, temp_cmd->type);
228 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Fail to send result");
229 if (VCD_ERROR_TIMED_OUT != ret) {
233 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Send result : pid(%d) type(%d)", temp_cmd->pid, temp_cmd->type);
237 if (100 == count) break;
238 /* While is retry code */
243 pid_list = g_slist_remove_link(pid_list, iter);
244 iter = g_slist_nth(pid_list, 0);
249 if (VCD_RECOGNITION_MODE_RESTART_CONTINUOUSLY != vcd_client_get_recognition_mode()) {
250 vcd_config_set_service_state(VCD_STATE_READY);
251 vcdc_send_service_state(VCD_STATE_READY);
257 static int __convert_type_to_priority(vc_cmd_type_e type)
260 case VC_COMMAND_TYPE_NONE: return 0;
261 case VC_COMMAND_TYPE_BACKGROUND: return 1;
262 case VC_COMMAND_TYPE_FOREGROUND: return 2;
263 case VC_COMMAND_TYPE_WIDGET: return 2;
264 case VC_COMMAND_TYPE_SYSTEM: return 3;
265 case VC_COMMAND_TYPE_EXCLUSIVE: return 3;
270 static void __vcd_server_pre_result_cb(vcp_pre_result_event_e event, const char* pre_result, void* user_data)
272 if (NULL != pre_result) {
273 vcdc_send_pre_result_to_manager(vcd_client_manager_get_pid(), event, pre_result);
277 static void __vcd_server_result_cb(vcp_result_event_e event, int* result_id, int count, const char* all_result,
278 const char* non_fixed_result, const char* msg, const char* nlp_info, void *user_data)
280 if (VCD_STATE_PROCESSING != vcd_config_get_service_state()) {
281 if (VCD_RECOGNITION_MODE_RESTART_CONTINUOUSLY != vcd_client_get_recognition_mode()) {
282 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Current state is not 'Processing' and mode is not 'Restart continuously'");
288 if (NULL != nlp_info) {
289 if (0 != vc_info_parser_set_nlp_info(nlp_info)) {
290 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to save nlp info");
294 vc_info_parser_unset_result(vcd_client_manager_get_exclusive());
296 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Event(%d), Text(%s) Nonfixed(%s) Msg(%s) Result count(%d)",
297 event, all_result, non_fixed_result, msg, count);
299 if (VCD_RECOGNITION_MODE_RESTART_AFTER_REJECT == vcd_client_get_recognition_mode()) {
300 if (VCP_RESULT_EVENT_REJECTED == event) {
301 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Restart by no or rejected result");
302 /* If no result and restart option is ON */
303 /* Send reject message */
304 bool temp = vcd_client_manager_get_exclusive();
305 vc_info_parser_set_result(all_result, event, msg, NULL, temp);
306 if (0 != vcdc_send_result_to_manager(vcd_client_manager_get_pid(), VC_RESULT_TYPE_NOTIFICATION)) {
307 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Fail to send result");
310 g_restart_timer = ecore_timer_add(0, __restart_engine, NULL);
313 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Stop recorder due to success");
315 } else if (VCD_RECOGNITION_MODE_RESTART_CONTINUOUSLY == vcd_client_get_recognition_mode()) {
316 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Restart continuously");
317 /* Restart option is ON */
318 g_restart_timer = ecore_timer_add(0, __restart_engine, NULL);
319 if (VCP_RESULT_EVENT_REJECTED == event) {
320 bool temp = vcd_client_manager_get_exclusive();
321 vc_info_parser_set_result(all_result, event, msg, NULL, temp);
322 if (0 != vcdc_send_result_to_manager(vcd_client_manager_get_pid(), VC_RESULT_TYPE_NOTIFICATION)) {
323 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Fail to send result");
330 if (NULL == result_id) {
332 if (NULL != all_result) {
333 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Engine result is no command : %s", all_result);
334 bool temp = vcd_client_manager_get_exclusive();
335 vc_info_parser_set_result(all_result, event, msg, NULL, temp);
338 int pid = vcd_client_widget_get_foreground_pid();
340 if (NULL != all_result) {
341 /* Send result text to widget */
342 vcdc_send_result(pid, VC_COMMAND_TYPE_WIDGET);
345 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Request tooltip hide");
346 /* Send to hide tooltip */
347 vcdc_send_show_tooltip(pid, false);
350 if (-1 != vcd_client_manager_get_pid()) {
351 /* Manager client is available */
352 if (0 != vcdc_send_result_to_manager(vcd_client_manager_get_pid(), VC_RESULT_TYPE_NORMAL)) {
353 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Fail to send result");
357 vcd_client_manager_set_exclusive(false);
363 SLOG(LOG_DEBUG, TAG_VCD, "[Server] === Get engine result ===");
366 vc_cmd_s* temp_cmd = NULL;
367 vc_cmd_list_h vc_cmd_list = NULL;
369 if (0 != vc_cmd_list_create(&vc_cmd_list)) {
370 SLOG(LOG_ERROR, TAG_VCD, "[Server] Fail to create command list");
371 vcd_client_manager_set_exclusive(false);
372 vcd_config_set_service_state(VCD_STATE_READY);
373 vcdc_send_service_state(VCD_STATE_READY);
379 for (i = 0; i < count; i++) {
380 SLOG(LOG_DEBUG, TAG_VCD, "[Server] [%d] Result ID(%d)", i, result_id[i]);
382 if (result_id[i] < 0) {
383 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Result ID(%d) is NOT valid", result_id[i]);
387 ret = vcd_client_get_cmd_from_result_id(result_id[i], &temp_cmd);
388 if (0 == ret && NULL != temp_cmd) {
389 /* Add priority filter */
390 int temp_priority = __convert_type_to_priority(temp_cmd->type);
391 if (priority > temp_priority) {
392 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Ignore result by priority");
394 } else if (priority < temp_priority) {
395 SLOG(LOG_DEBUG, TAG_VCD, "[Server] High priority result!!");
396 priority = temp_priority;
398 if (0 != vc_cmd_list_remove_all(vc_cmd_list, true)) {
399 SLOG(LOG_ERROR, TAG_VCD, "[Server] Fail to list remove all");
403 switch (temp_cmd->format) {
404 case VC_CMD_FORMAT_FIXED:
405 /* Nonfixed result is NOT valid */
407 case VC_CMD_FORMAT_FIXED_AND_EXTRA:
408 if (NULL == temp_cmd->parameter) {
409 if (NULL != non_fixed_result) {
410 temp_cmd->parameter = strdup(non_fixed_result);
413 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Command type is NOT vaild. Parameter (%s)", temp_cmd->parameter);
416 case VC_CMD_FORMAT_EXTRA_AND_FIXED:
417 if (NULL == temp_cmd->command) {
418 if (NULL != non_fixed_result) {
419 temp_cmd->command = strdup(non_fixed_result);
422 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Command type is NOT vaild. Command (%s)", temp_cmd->command);
427 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Unknown command type : %d", temp_cmd->type);
431 if (0 != vc_cmd_list_add(vc_cmd_list, (vc_cmd_h)temp_cmd)) {
432 SLOG(LOG_DEBUG, TAG_VCD, "Fail to add command to list");
433 vc_cmd_destroy((vc_cmd_h)temp_cmd);
436 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] NOT found matached result(%d)", result_id[i]);
440 vc_cmd_print_list(vc_cmd_list);
442 SLOG(LOG_DEBUG, TAG_VCD, "[Server] =========================");
444 int result_count = 0;
445 vc_cmd_list_get_count(vc_cmd_list, &result_count);
447 if (false == vcd_client_manager_get_exclusive()) {
448 int pid = vcd_client_widget_get_foreground_pid();
450 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Request tooltip hide");
451 vcdc_send_show_tooltip(pid, false);
454 vc_info_parser_set_result(all_result, event, msg, vc_cmd_list, false);
456 if (-1 != vcd_client_manager_get_pid()) {
457 /* Manager client is available */
458 if (0 != vcdc_send_result_to_manager(vcd_client_manager_get_pid(), VC_RESULT_TYPE_NORMAL)) {
459 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Fail to send result");
462 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Manager is NOT available. Send result to client directly");
463 /* Send result to client */
464 ecore_timer_add(0, __vcd_send_selected_result, NULL);
467 /* exclusive command */
468 vc_info_parser_set_result(all_result, event, msg, vc_cmd_list, true);
470 if (-1 != vcd_client_manager_get_pid()) {
471 /* Manager client is available */
472 if (0 != vcdc_send_result_to_manager(vcd_client_manager_get_pid(), VC_RESULT_TYPE_NORMAL)) {
473 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Fail to send result");
476 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Manager is NOT available");
479 vcd_client_manager_set_exclusive(false);
482 vc_cmd_list_destroy(vc_cmd_list, true);
488 * vcd server Interfaces
490 static void __vcd_file_clean_up()
492 SLOG(LOG_DEBUG, TAG_VCD, "== Old file clean up == ");
497 struct dirent *dirp = NULL;
499 dp = opendir(VC_RUNTIME_INFO_ROOT);
501 SLOG(LOG_ERROR, TAG_VCD, "[File message WARN] Fail to open path : %s", VC_RUNTIME_INFO_ROOT);
505 char remove_path[256] = {0, };
507 ret = readdir_r(dp, &entry, &dirp);
509 SLOG(LOG_ERROR, TAG_VCD, "[File ERROR] Fail to read directory");
514 if (!strncmp("vc_", dirp->d_name, strlen("vc_"))) {
515 memset(remove_path, 0, 256);
516 snprintf(remove_path, 256, "%s/%s", VC_RUNTIME_INFO_ROOT, dirp->d_name);
519 if (0 != remove(remove_path)) {
520 SLOG(LOG_WARN, TAG_VCD, "[File message WARN] Fail to remove file : %s", remove_path);
522 SLOG(LOG_DEBUG, TAG_VCD, "[File message] Remove file : %s", remove_path);
526 } while (NULL != dirp);
537 /* Remove old file */
538 __vcd_file_clean_up();
539 /* Send error signal for notifying that daemon is reset*/
540 vcdc_send_error_signal(VCD_ERROR_SERVICE_RESET, "Daemon reset");
542 /* initialize modules */
543 ret = vcd_config_initialize(__config_lang_changed_cb, __config_foreground_changed_cb, NULL);
545 SLOG(LOG_ERROR, TAG_VCD, "[Server WARNING] Fail to initialize config.");
548 vcd_config_set_service_state(VCD_STATE_NONE);
550 ret = vcd_engine_agent_init(__vcd_server_pre_result_cb, __vcd_server_result_cb);
552 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to engine agent initialize : result(%d)", ret);
556 if (0 != vcd_recorder_create(__server_recorder_callback, __server_recorder_interrupt_callback)) {
557 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to create recorder");
558 return VCD_ERROR_OPERATION_FAILED;
562 ret = vcd_engine_agent_initialize_current_engine();
564 if (VCD_ERROR_ENGINE_NOT_FOUND == ret)
565 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] There is No Voice control engine");
567 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to init engine");
575 if (0 != vcd_engine_agent_load_current_engine()) {
576 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to load current engine");
577 return VCD_ERROR_OPERATION_FAILED;
580 /* Initialize manager info */
581 vcd_client_manager_unset();
583 vcd_config_set_service_state(VCD_STATE_READY);
584 vcdc_send_service_state(VCD_STATE_READY);
586 SLOG(LOG_DEBUG, TAG_VCD, "[Server SUCCESS] initialize");
594 if (0 < g_list_length(g_proc_list)) {
595 iter = g_list_first(g_proc_list);
596 while (NULL != iter) {
597 g_proc_list = g_list_remove_link(g_proc_list, iter);
598 iter = g_list_first(g_proc_list);
602 if (g_restart_timer != NULL) {
603 ecore_timer_del(g_restart_timer);
604 g_restart_timer = NULL;
607 vcd_state_e state = vcd_config_get_service_state();
608 if (VCD_STATE_READY != state) {
609 if (VCD_STATE_RECORDING == state) {
612 vcd_engine_recognize_cancel();
614 if (0 != vcd_recorder_destroy()) {
615 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to destroy recorder");
617 SLOG(LOG_DEBUG, TAG_VCD, "[Server] destroy recorder");
620 if (0 != vcd_engine_agent_release()) {
621 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to release engine");
623 SLOG(LOG_DEBUG, TAG_VCD, "[Server] release engine");
626 vcd_config_set_service_state(VCD_STATE_NONE);
627 vcdc_send_service_state(VCD_STATE_NONE);
629 SLOG(LOG_DEBUG, TAG_VCD, "[Server] mode finalize");
634 static Eina_Bool __finalize_quit_ecore_loop(void *data)
636 SLOG(LOG_DEBUG, TAG_VCD, "[Server] quit ecore main loop");
637 ecore_main_loop_quit();
641 static void __read_proc()
645 struct dirent *dirp = NULL;
650 if (0 < g_list_length(g_proc_list)) {
651 iter = g_list_first(g_proc_list);
652 while (NULL != iter) {
653 g_proc_list = g_list_remove_link(g_proc_list, iter);
654 iter = g_list_first(g_proc_list);
658 dp = opendir("/proc");
660 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Fail to open proc");
663 ret = readdir_r(dp, &entry, &dirp);
665 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Fail to readdir");
670 tmp = atoi(dirp->d_name);
671 if (0 >= tmp) continue;
672 g_proc_list = g_list_append(g_proc_list, GINT_TO_POINTER(tmp));
674 } while (NULL != dirp);
680 static void __vcd_cleanup_client(vcd_client_type_e type)
682 int* client_list = NULL;
683 int client_count = 0;
690 if (VCD_CLIENT_TYPE_NORMAL == type) {
691 ret = vcd_client_get_list(&client_list, &client_count);
692 } else if (VCD_CLIENT_TYPE_WIDGET == type) {
693 ret = vcd_client_widget_get_list(&client_list, &client_count);
694 } else if (VCD_CLIENT_TYPE_MANAGER == type) {
695 mgr_pid = vcd_client_manager_get_pid();
696 client_list = &mgr_pid;
699 SLOG(LOG_WARN, TAG_VCD, "[WARNING] Invalid Manager pid");
704 if (0 == ret || mgr_pid > 0) {
705 SLOG(LOG_DEBUG, TAG_VCD, "===== Clean up %s client ", type ? (type == 1) ? "Widget" : "Manager" : "Normal");
706 if (NULL != client_list && client_count > 0) {
707 for (i = 0; i < client_count; i++) {
710 for (j = 0; j < g_list_length(g_proc_list); j++) {
711 iter = g_list_nth(g_proc_list, j);
713 if (client_list[i] == GPOINTER_TO_INT(iter->data)) {
714 SLOG(LOG_DEBUG, TAG_VCD, "%s pid(%d) is running", type ? (type == 1) ? "Widget" : "Manager" : "Normal", client_list[i]);
721 if (false == exist) {
722 SLOG(LOG_ERROR, TAG_VCD, "%s pid(%d) should be removed", type ? (type == 1) ? "Widget" : "Manager" : "Normal", client_list[i]);
723 if (VCD_CLIENT_TYPE_NORMAL == type)
724 vcd_server_finalize(client_list[i]);
725 else if (VCD_CLIENT_TYPE_WIDGET == type)
726 vcd_server_widget_finalize(client_list[i]);
728 vcd_server_mgr_finalize(mgr_pid);
732 SLOG(LOG_DEBUG, TAG_VCD, "=====");
733 SLOG(LOG_DEBUG, TAG_VCD, " ");
735 if (NULL != client_list && -1 == mgr_pid) {
742 Eina_Bool vcd_cleanup_client_all(void *data)
746 __vcd_cleanup_client(VCD_CLIENT_TYPE_NORMAL);
747 __vcd_cleanup_client(VCD_CLIENT_TYPE_WIDGET);
748 __vcd_cleanup_client(VCD_CLIENT_TYPE_MANAGER);
751 if (0 == vcd_client_get_list(&client_list, &client_count)) {
752 SLOG(LOG_DEBUG, TAG_VCD, "===== Clean up client ");
753 if (NULL != client_list && client_count > 0) {
754 for (i = 0; i < client_count; i++) {
757 for (j = 0; j < g_list_length(g_proc_list); j++) {
758 iter = g_list_nth(g_proc_list, j);
760 if (client_list[i] == GPOINTER_TO_INT(iter->data)) {
761 SLOG(LOG_DEBUG, TAG_VCD, "pid(%d) is running", client_list[i]);
768 if (false == exist) {
769 SLOG(LOG_ERROR, TAG_VCD, "pid(%d) should be removed", client_list[i]);
770 vcd_server_finalize(client_list[i]);
773 result = vcdc_send_hello(client_list[i], VCD_CLIENT_TYPE_NORMAL);
776 SLOG(LOG_DEBUG, TAG_VCD, "[Server] pid(%d) should be removed.", client_list[i]);
777 vcd_server_finalize(client_list[i]);
778 } else if (-1 == result) {
779 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Hello result has error");
784 SLOG(LOG_DEBUG, TAG_VCD, "=====");
785 SLOG(LOG_DEBUG, TAG_VCD, " ");
787 if (NULL != client_list) {
792 /* If app is in background state, app cannot response message. */
793 if (0 == vcd_client_widget_get_list(&client_list, &client_count)) {
794 SLOG(LOG_DEBUG, TAG_VCD, "===== Clean up widget");
795 if (NULL != client_list && client_count > 0) {
796 for (i = 0; i < client_count; i++) {
799 for (j = 0; j < g_list_length(g_proc_list); j++) {
800 iter = g_list_nth(g_proc_list, j);
802 if (client_list[i] == GPOINTER_TO_INT(iter->data)) {
803 SLOG(LOG_DEBUG, TAG_VCD, "widget pid(%d) is running", client_list[i]);
810 if (false == exist) {
811 SLOG(LOG_ERROR, TAG_VCD, "widget pid(%d) should be removed", client_list[i]);
812 vcd_server_widget_finalize(client_list[i]);
815 result = vcdc_send_hello(client_list[i], VCD_CLIENT_TYPE_WIDGET);
818 SLOG(LOG_DEBUG, TAG_VCD, "[Server] widget pid(%d) should be removed.", client_list[i]);
819 vcd_server_widget_finalize(client_list[i]);
820 } else if (-1 == result) {
821 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Hello result has error");
826 SLOG(LOG_DEBUG, TAG_VCD, "=====");
827 SLOG(LOG_DEBUG, TAG_VCD, " ");
830 if (NULL != client_list) {
838 int mgr_pid = vcd_client_manager_get_pid();
840 for (j = 0; j < g_list_length(g_proc_list); j++) {
841 iter = g_list_nth(g_proc_list, j);
843 if (mgr_pid == GPOINTER_TO_INT(iter->data)) {
844 SLOG(LOG_DEBUG, TAG_VCD, "manager pid(%d) is running", mgr_pid);
851 if (false == exist) {
852 SLOG(LOG_ERROR, TAG_VCD, "manager pid (%d) should be removed", mgr_pid);
853 vcd_server_mgr_finalize(mgr_pid);
860 int vcd_server_get_service_state()
862 return vcd_config_get_service_state();
865 int vcd_server_get_foreground()
868 vcd_config_get_foreground(&pid);
876 int vcd_server_mgr_initialize(int pid)
878 if (false == g_is_engine) {
879 if (0 != vcd_engine_agent_initialize_current_engine()) {
880 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] No Engine");
882 return VCD_ERROR_ENGINE_NOT_FOUND;
888 /* check if pid is valid */
889 if (false == vcd_client_manager_is_valid(pid)) {
890 SLOG(LOG_ERROR, TAG_VCD, "[Server] old manager pid(%d) be removed", vcd_client_manager_get_pid());
891 vcd_server_mgr_cancel();
892 vcd_client_manager_unset();
895 /* Add client information to client manager */
896 if (0 != vcd_client_manager_set(pid)) {
897 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to add manager");
898 return VCD_ERROR_OPERATION_FAILED;
901 SLOG(LOG_DEBUG, TAG_VCD, "[Server Success] Manager initialize : pid(%d)", pid);
903 return VCD_ERROR_NONE;
906 int vcd_server_mgr_finalize(int pid)
908 /* check if pid is valid */
909 if (false == vcd_client_manager_is_valid(pid)) {
910 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] The manager pid(%d) is NOT valid", pid);
911 return VCD_ERROR_INVALID_PARAMETER;
914 /* Cancel recognition */
915 vcd_server_mgr_cancel();
917 /* Remove manager information */
918 if (0 != vcd_client_manager_unset()) {
919 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Fail to delete client");
922 if (0 == vcd_client_get_ref_count()) {
923 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Connected client list is empty");
924 ecore_timer_add(0, __finalize_quit_ecore_loop, NULL);
927 SLOG(LOG_DEBUG, TAG_VCD, "[Server Success] Manager Finalize : pid(%d)", pid);
929 return VCD_ERROR_NONE;
932 int vcd_server_mgr_set_command(int pid)
934 if (0 != vcd_client_manager_set_command(pid)) {
935 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] The manager pid(%d) is NOT valid", pid);
936 return VCD_ERROR_INVALID_PARAMETER;
938 return VCD_ERROR_NONE;
941 int vcd_server_mgr_unset_command(int pid)
943 if (0 != vcd_client_manager_unset_command(pid)) {
944 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] The manager pid(%d) is NOT valid", pid);
945 return VCD_ERROR_INVALID_PARAMETER;
947 return VCD_ERROR_NONE;
950 int vcd_server_mgr_set_demandable_client(int pid)
952 /* check if pid is valid */
953 if (false == vcd_client_manager_is_valid(pid)) {
954 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] The manager pid(%d) is NOT valid", pid);
955 return VCD_ERROR_INVALID_PARAMETER;
958 GSList* client_list = NULL;
959 if (0 != vc_info_parser_get_demandable_clients(&client_list)) {
960 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to get demandable client");
961 return VCD_ERROR_OPERATION_FAILED;
964 /* Save client list */
965 if (0 != vcd_client_manager_set_demandable_client(pid, client_list)) {
966 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to set demandable client list");
967 return VCD_ERROR_OPERATION_FAILED;
970 return VCD_ERROR_NONE;
973 int vcd_server_mgr_set_audio_type(int pid, const char* audio_type)
975 /* check if pid is valid */
976 if (false == vcd_client_manager_is_valid(pid)) {
977 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] The manager pid(%d) is NOT valid", pid);
978 return VCD_ERROR_INVALID_PARAMETER;
982 vcp_audio_type_e type = VCP_AUDIO_TYPE_PCM_S16_LE;
986 ret = vcd_engine_get_audio_format(audio_type, &type, &rate, &channel);
988 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to get audio format : %d", ret);
989 return VCD_ERROR_OPERATION_FAILED;
992 ret = vcd_recorder_set(audio_type, type, rate, channel);
994 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to set audio in type : %d", ret);
995 return VCD_ERROR_OPERATION_FAILED;
998 return VCD_ERROR_NONE;
1001 int vcd_server_mgr_get_audio_type(int pid, char** audio_type)
1003 /* check if pid is valid */
1004 if (false == vcd_client_manager_is_valid(pid)) {
1005 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] The manager pid(%d) is NOT valid", pid);
1006 return VCD_ERROR_INVALID_PARAMETER;
1009 int ret = vcd_recorder_get(audio_type);
1011 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to get audio in type : %d", ret);
1012 return VCD_ERROR_OPERATION_FAILED;
1015 return VCD_ERROR_NONE;
1018 int vcd_server_mgr_set_client_info(int pid)
1020 /* check if pid is valid */
1021 if (false == vcd_client_manager_is_valid(pid)) {
1022 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] The manager pid(%d) is NOT valid", pid);
1023 return VCD_ERROR_INVALID_PARAMETER;
1026 int ret = vcd_client_save_client_info();
1028 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to save client info : %d", ret);
1029 return VCD_ERROR_OPERATION_FAILED;
1032 return VCD_ERROR_NONE;
1035 static int __start_internal_recognition()
1039 /* 2. Get commands */
1040 ret = vcd_client_command_collect_command();
1042 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to collect command : %d", ret);
1043 return VCD_ERROR_OPERATION_FAILED;
1046 ret = vcd_client_get_length();
1048 SLOG(LOG_WARN, TAG_VCD, "[Server WARNIING] No current command : %d", ret);
1049 return VCD_ERROR_OPERATION_FAILED;
1052 /* 3. Set command to engine */
1053 ret = vcd_engine_set_commands();
1055 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to collect command : %d", ret);
1056 return VCD_ERROR_OPERATION_FAILED;
1059 SLOG(LOG_DEBUG, TAG_VCD, "[Server Success] Set command");
1061 bool stop_by_silence = true;
1062 if (VCD_RECOGNITION_MODE_MANUAL == vcd_client_get_recognition_mode()) {
1063 stop_by_silence = false;
1066 /* 4. start recognition */
1067 ret = vcd_engine_recognize_start(stop_by_silence);
1069 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to start recognition : result(%d)", ret);
1070 return VCD_ERROR_OPERATION_FAILED;
1073 SLOG(LOG_DEBUG, TAG_VCD, "[Server Success] Start engine");
1075 /* 5. recorder start */
1076 ret = vcd_recorder_start();
1078 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to start recorder : result(%d)", ret);
1079 vcd_engine_recognize_cancel();
1083 vcd_config_set_service_state(VCD_STATE_RECORDING);
1084 vcdc_send_service_state(VCD_STATE_RECORDING);
1086 SLOG(LOG_DEBUG, TAG_VCD, "[Server Success] Start recognition(%d)", stop_by_silence);
1091 static Eina_Bool __vcd_request_show_tooltip(void *data)
1093 int pid = vcd_client_widget_get_foreground_pid();
1095 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Request tooltip show and widget command");
1096 vcdc_send_show_tooltip(pid, (bool)data);
1102 int vcd_server_mgr_start(vcd_recognition_mode_e recognition_mode, bool exclusive_cmd, bool start_by_client)
1104 /* 1. check current state */
1105 vcd_state_e state = vcd_config_get_service_state();
1107 if (VCD_STATE_READY != state) {
1108 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Current state is not ready");
1109 return VCD_ERROR_INVALID_STATE;
1112 SLOG(LOG_DEBUG, TAG_VCD, "[Server] set recognition mode = %d", recognition_mode);
1113 vcd_client_set_recognition_mode(recognition_mode);
1115 if (false == exclusive_cmd) {
1116 /* Notify show tooltip */
1117 int pid = vcd_client_widget_get_foreground_pid();
1119 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Request tooltip show and widget command");
1120 ecore_timer_add(0, __vcd_request_show_tooltip, (void*)true);
1124 vcd_client_manager_set_exclusive(exclusive_cmd);
1128 if (true == start_by_client) {
1129 /* Get foreground pid */
1130 if (0 != vcd_config_get_foreground(&fg_pid)) {
1131 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to get foreground");
1134 /* Set client exclusive option */
1135 if (0 != vcd_client_set_exclusive_command(fg_pid)) {
1136 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to set exclusive command");
1140 int ret = __start_internal_recognition();
1142 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to start recongition : %d", ret);
1146 if (true == start_by_client) {
1147 vcd_client_unset_exclusive_command(fg_pid);
1150 return VCD_ERROR_NONE;
1153 int vcd_server_mgr_stop()
1155 /* 1. Check current state is recording */
1156 if (VCD_STATE_RECORDING != vcd_config_get_service_state()) {
1157 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Current state is not recording");
1158 return VCD_ERROR_INVALID_STATE;
1161 /* 2. Stop recorder */
1162 vcd_recorder_stop();
1164 /* 3. Stop engine recognition */
1165 int ret = vcd_engine_recognize_stop();
1167 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to stop recognition : %d", ret);
1170 /* 4. Set original mode */
1171 vcd_config_set_service_state(VCD_STATE_PROCESSING);
1172 vcdc_send_service_state(VCD_STATE_PROCESSING);
1174 return VCD_ERROR_NONE;
1177 int vcd_server_mgr_cancel()
1179 /* 1. Check current state */
1180 vcd_state_e state = vcd_config_get_service_state();
1181 if (VCD_STATE_RECORDING != state && VCD_STATE_PROCESSING != state) {
1182 SLOG(LOG_WARN, TAG_VCD, "[Server ERROR] Current state is not recording or processing");
1183 return VCD_ERROR_INVALID_STATE;
1186 if (g_restart_timer != NULL) {
1187 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Delete restart engine timer");
1188 ecore_timer_del(g_restart_timer);
1189 g_restart_timer = NULL;
1192 /* 2. Stop recorder */
1193 vcd_recorder_stop();
1194 /* 3. Cancel engine */
1195 int ret = vcd_engine_recognize_cancel();
1197 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Fail to cancel : %d", ret);
1200 if (false == vcd_client_manager_get_exclusive()) {
1201 int pid = vcd_client_widget_get_foreground_pid();
1203 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Request tooltip hide");
1204 ecore_timer_add(0, __vcd_request_show_tooltip, (void*)false);
1207 vcd_client_manager_set_exclusive(false);
1211 vcd_config_set_service_state(VCD_STATE_READY);
1212 vcdc_send_service_state(VCD_STATE_READY);
1214 return VCD_ERROR_NONE;
1218 int vcd_server_mgr_result_select()
1220 __vcd_send_selected_result(NULL);
1222 return VCD_ERROR_NONE;
1226 * VC Server Functions for Client
1228 int vcd_server_initialize(int pid)
1230 if (false == g_is_engine) {
1231 if (0 != vcd_engine_agent_initialize_current_engine()) {
1232 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] No Engine");
1233 g_is_engine = false;
1234 return VCD_ERROR_ENGINE_NOT_FOUND;
1240 if (false == vcd_engine_is_available_engine()) {
1241 if (0 != vcd_engine_agent_initialize_current_engine()) {
1242 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] No Engine");
1243 return VCD_ERROR_ENGINE_NOT_FOUND;
1247 /* check if pid is valid */
1248 if (true == vcd_client_is_available(pid)) {
1249 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] The pid is already exist");
1250 return VCD_ERROR_INVALID_PARAMETER;
1253 /* Add client information to client manager */
1254 if (0 != vcd_client_add(pid)) {
1255 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to add client info");
1256 return VCD_ERROR_OPERATION_FAILED;
1259 SLOG(LOG_DEBUG, TAG_VCD, "[Server Success] Client Initialize : pid(%d)", pid);
1261 return VCD_ERROR_NONE;
1264 int vcd_server_finalize(int pid)
1266 /* check if pid is valid */
1267 if (false == vcd_client_is_available(pid)) {
1268 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] pid is NOT valid ");
1269 return VCD_ERROR_INVALID_PARAMETER;
1272 /* Remove client information */
1273 if (0 != vcd_client_delete(pid)) {
1274 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Fail to delete client");
1277 if (0 == vcd_client_get_ref_count()) {
1278 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Connected client list is empty");
1279 ecore_timer_add(0, __finalize_quit_ecore_loop, NULL);
1282 SLOG(LOG_DEBUG, TAG_VCD, "[Server Success] Client Finalize : pid(%d)", pid);
1284 return VCD_ERROR_NONE;
1287 int vcd_server_set_command(int pid, vc_cmd_type_e cmd_type)
1289 /* check if pid is valid */
1290 if (false == vcd_client_is_available(pid)) {
1291 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] pid is NOT valid ");
1292 return VCD_ERROR_INVALID_PARAMETER;
1295 if (0 != vcd_client_set_command_type(pid, cmd_type)) {
1296 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to set command type : pid(%d), cmd_type(%d)", pid, cmd_type);
1297 return VCD_ERROR_OPERATION_FAILED;
1303 int vcd_server_unset_command(int pid, vc_cmd_type_e cmd_type)
1305 /* check if pid is valid */
1306 if (false == vcd_client_is_available(pid)) {
1307 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] pid is NOT valid ");
1308 return VCD_ERROR_INVALID_PARAMETER;
1311 if (0 != vcd_client_unset_command_type(pid, cmd_type)) {
1312 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to unset command type : pid(%d), cmd_type(%d)", pid, cmd_type);
1313 return VCD_ERROR_OPERATION_FAILED;
1319 int vcd_server_set_foreground(int pid, bool value)
1321 /* check if pid is valid */
1322 if (false == vcd_client_is_available(pid) && false == vcd_client_widget_is_available(pid)) {
1323 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] pid is NOT valid ");
1324 return VCD_ERROR_INVALID_PARAMETER;
1327 if (0 != vcd_config_set_foreground(pid, value)) {
1328 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to set foreground : pid(%d), value(%d)", pid, value);
1329 return VCD_ERROR_OPERATION_FAILED;
1336 int vcd_server_set_exclusive_command(int pid, bool value)
1338 /* check if pid is valid */
1339 if (false == vcd_client_is_available(pid)) {
1340 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] pid is NOT valid ");
1341 return VCD_ERROR_INVALID_PARAMETER;
1344 if (true == value) {
1345 if (0 != vcd_client_set_exclusive_command(pid)) {
1346 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to set exclusive command : pid(%d)", pid);
1347 return VCD_ERROR_OPERATION_FAILED;
1350 if (0 != vcd_client_unset_exclusive_command(pid)) {
1351 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to unset exclusive command : pid(%d)", pid);
1352 return VCD_ERROR_OPERATION_FAILED;
1359 int vcd_server_request_start(int pid, bool stop_by_silence)
1361 /* check if pid is valid */
1362 if (false == vcd_client_is_available(pid)) {
1363 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] pid(%d) is NOT forground client", pid);
1364 return VCD_ERROR_INVALID_PARAMETER;
1368 /* Check current state */
1369 vcd_state_e state = vcd_config_get_service_state();
1371 /* Service state should be ready */
1372 if (VCD_STATE_READY != state) {
1373 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Current state is not Ready : pid(%d)", pid);
1374 return VCD_ERROR_INVALID_STATE;
1377 if (-1 != vcd_client_manager_get_pid()) {
1378 /* Check current pid is valid */
1379 if (false == vcd_client_manager_check_demandable_client(pid)) {
1380 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Current client is NOT available : pid(%d)", pid);
1381 return VCD_ERROR_INVALID_PARAMETER;
1385 ret = vcd_server_mgr_start(stop_by_silence, false);
1387 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Fail to start recognition");
1388 return VCD_ERROR_INVALID_PARAMETER;
1394 int vcd_server_request_stop(int pid)
1397 /* Check current state */
1398 vcd_state_e state = vcd_config_get_service_state();
1400 /* Service state should be ready */
1401 if (VCD_STATE_RECORDING != state) {
1402 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Current state is not Recording : pid(%d)", pid);
1403 return VCD_ERROR_INVALID_STATE;
1406 if (-1 != vcd_client_manager_get_pid()) {
1407 /* Check current pid is valid */
1408 if (false == vcd_client_manager_check_demandable_client(pid)) {
1409 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Current client is NOT available : pid(%d)", pid);
1410 return VCD_ERROR_INVALID_PARAMETER;
1414 ret = vcd_server_mgr_stop();
1416 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Fail to start recognition");
1417 return VCD_ERROR_OPERATION_FAILED;
1420 return VCD_ERROR_NONE;
1423 int vcd_server_request_cancel(int pid)
1426 /* Check current state */
1427 vcd_state_e state = vcd_config_get_service_state();
1429 /* Service state should be recording or processing */
1430 if (VCD_STATE_RECORDING != state && VCD_STATE_PROCESSING != state) {
1431 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Current state is not Recording or Processing : pid(%d)", pid);
1432 return VCD_ERROR_INVALID_STATE;
1435 if (-1 != vcd_client_manager_get_pid()) {
1436 /* Check current pid is valid */
1437 if (false == vcd_client_manager_check_demandable_client(pid)) {
1438 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Current client is NOT available : pid(%d)", pid);
1439 return VCD_ERROR_INVALID_PARAMETER;
1443 ret = vcd_server_mgr_cancel();
1445 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Fail to start recognition");
1446 return VCD_ERROR_OPERATION_FAILED;
1449 return VCD_ERROR_NONE;
1454 * VC Server Functions for Widget lib
1456 int vcd_server_widget_initialize(int pid)
1458 if (false == g_is_engine) {
1459 if (0 != vcd_engine_agent_initialize_current_engine()) {
1460 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] No Engine");
1461 g_is_engine = false;
1462 return VCD_ERROR_ENGINE_NOT_FOUND;
1468 if (false == vcd_engine_is_available_engine()) {
1469 if (0 != vcd_engine_agent_initialize_current_engine()) {
1470 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] No Engine");
1471 return VCD_ERROR_ENGINE_NOT_FOUND;
1475 /* check if pid is valid */
1476 if (true == vcd_client_widget_is_available(pid)) {
1477 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] The pid is already exist");
1478 return VCD_ERROR_INVALID_PARAMETER;
1481 /* Add client information to client manager */
1482 if (0 != vcd_client_widget_add(pid)) {
1483 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to add client info");
1484 return VCD_ERROR_OPERATION_FAILED;
1487 SLOG(LOG_DEBUG, TAG_VCD, "[Server Success] Initialize widget : pid(%d)", pid);
1489 return VCD_ERROR_NONE;
1492 int vcd_server_widget_finalize(int pid)
1494 /* check if pid is valid */
1495 if (false == vcd_client_widget_is_available(pid)) {
1496 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] pid is NOT valid ");
1497 return VCD_ERROR_INVALID_PARAMETER;
1500 /* Remove client information */
1501 if (0 != vcd_client_widget_delete(pid)) {
1502 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Fail to delete client");
1505 if (0 == vcd_client_get_ref_count()) {
1506 SLOG(LOG_DEBUG, TAG_VCD, "[Server] connected client list is empty");
1507 ecore_timer_add(0, __finalize_quit_ecore_loop, NULL);
1510 return VCD_ERROR_NONE;
1513 int vcd_server_widget_start_recording(int pid, bool widget_command)
1515 /* check if pid is valid */
1516 if (false == vcd_client_widget_is_available(pid)) {
1517 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] pid is NOT valid ");
1518 return VCD_ERROR_INVALID_PARAMETER;
1521 if (true == widget_command) {
1522 vcd_client_widget_set_command(pid);
1523 SLOG(LOG_DEBUG, TAG_VCD, "[Server] widget command is available");
1525 vcd_client_widget_unset_command(pid);
1526 SLOG(LOG_DEBUG, TAG_VCD, "[Server] widget command is NOT available");
1529 int ret = __start_internal_recognition();
1531 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to start recongition : %d", ret);
1532 ecore_timer_add(0, __vcd_request_show_tooltip, (void*)false);
1538 int vcd_server_widget_start(int pid, bool stop_by_silence)
1540 /* check if pid is valid */
1541 int fore_pid = vcd_client_widget_get_foreground_pid();
1542 if (pid != fore_pid) {
1543 SLOG(LOG_ERROR, TAG_VCD, "[Server] pid is NOT foreground");
1544 return VCD_ERROR_INVALID_PARAMETER;
1547 /* Check current state */
1548 vcd_state_e state = vcd_config_get_service_state();
1550 /* Service state should be ready */
1551 if (VCD_STATE_READY != state) {
1552 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Current state is not Ready : pid(%d)", pid);
1553 return VCD_ERROR_INVALID_STATE;
1556 vcd_client_set_slience_detection(stop_by_silence);
1558 /* Notify show tooltip */
1559 ecore_timer_add(0, __vcd_request_show_tooltip, (void*)true);
1564 int vcd_server_widget_stop(int pid)
1566 /* check if pid is valid */
1567 int fore_pid = vcd_client_widget_get_foreground_pid();
1568 if (pid != fore_pid) {
1569 SLOG(LOG_ERROR, TAG_VCD, "[Server] pid is NOT foreground");
1570 return VCD_ERROR_INVALID_PARAMETER;
1574 /* Check current state */
1575 vcd_state_e state = vcd_config_get_service_state();
1577 /* Service state should be recording */
1578 if (VCD_STATE_RECORDING != state) {
1579 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Current service state is not Recording : pid(%d)", pid);
1580 return VCD_ERROR_INVALID_STATE;
1583 ret = vcd_server_mgr_stop();
1585 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Fail to start recognition");
1586 return VCD_ERROR_OPERATION_FAILED;
1589 return VCD_ERROR_NONE;
1592 int vcd_server_widget_cancel(int pid)
1594 /* check if pid is valid */
1595 int fore_pid = vcd_client_widget_get_foreground_pid();
1596 if (pid != fore_pid) {
1597 SLOG(LOG_ERROR, TAG_VCD, "[Server] pid is NOT foreground");
1598 return VCD_ERROR_INVALID_PARAMETER;
1602 /* Check current state */
1603 vcd_state_e state = vcd_config_get_service_state();
1605 /* Service state should be recording or processing */
1606 if (VCD_STATE_RECORDING != state && VCD_STATE_PROCESSING != state) {
1607 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Current state is not Recording or Processing : pid(%d)", pid);
1608 return VCD_ERROR_INVALID_STATE;
1611 ret = vcd_server_mgr_cancel();
1613 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Fail to cancel recognition : %d", ret);
1617 return VCD_ERROR_NONE;