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; break;
261 case VC_COMMAND_TYPE_BACKGROUND: return 1; break;
262 case VC_COMMAND_TYPE_FOREGROUND: return 2; break;
263 case VC_COMMAND_TYPE_WIDGET: return 2; break;
264 case VC_COMMAND_TYPE_SYSTEM: return 3; break;
265 case VC_COMMAND_TYPE_EXCLUSIVE: return 3; break;
266 default: return 0; break;
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);
279 static void __vcd_server_result_cb(vcp_result_event_e event, int* result_id, int count, const char* all_result,
280 const char* non_fixed_result, const char* msg, const char* nlp_info, void *user_data)
282 if (VCD_STATE_PROCESSING != vcd_config_get_service_state()) {
283 if (VCD_RECOGNITION_MODE_RESTART_CONTINUOUSLY != vcd_client_get_recognition_mode()) {
284 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Current state is not 'Processing' and mode is not 'Restart continuously'");
290 if (NULL != nlp_info) {
291 if (0 != vc_info_parser_set_nlp_info(nlp_info)) {
292 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to save nlp info");
296 vc_info_parser_unset_result(vcd_client_manager_get_exclusive());
298 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Event(%d), Text(%s) Nonfixed(%s) Msg(%s) Result count(%d)",
299 event, all_result, non_fixed_result, msg, count);
301 if (VCD_RECOGNITION_MODE_RESTART_AFTER_REJECT == vcd_client_get_recognition_mode()) {
302 if (VCP_RESULT_EVENT_REJECTED == event) {
303 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Restart by no or rejected result");
304 /* If no result and restart option is ON */
305 /* Send reject message */
306 bool temp = vcd_client_manager_get_exclusive();
307 vc_info_parser_set_result(all_result, event, msg, NULL, temp);
308 if (0 != vcdc_send_result_to_manager(vcd_client_manager_get_pid(), VC_RESULT_TYPE_NOTIFICATION)) {
309 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Fail to send result");
312 g_restart_timer = ecore_timer_add(0, __restart_engine, NULL);
315 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Stop recorder due to success");
317 } else if (VCD_RECOGNITION_MODE_RESTART_CONTINUOUSLY == vcd_client_get_recognition_mode()) {
318 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Restart continuously");
319 /* Restart option is ON */
320 g_restart_timer = ecore_timer_add(0, __restart_engine, NULL);
321 if (VCP_RESULT_EVENT_REJECTED == event) {
322 bool temp = vcd_client_manager_get_exclusive();
323 vc_info_parser_set_result(all_result, event, msg, NULL, temp);
324 if (0 != vcdc_send_result_to_manager(vcd_client_manager_get_pid(), VC_RESULT_TYPE_NOTIFICATION)) {
325 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Fail to send result");
332 if (NULL == result_id) {
334 if (NULL != all_result) {
335 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Engine result is no command : %s", all_result);
336 bool temp = vcd_client_manager_get_exclusive();
337 vc_info_parser_set_result(all_result, event, msg, NULL, temp);
340 int pid = vcd_client_widget_get_foreground_pid();
342 if (NULL != all_result) {
343 /* Send result text to widget */
344 vcdc_send_result(pid, VC_COMMAND_TYPE_WIDGET);
347 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Request tooltip hide");
348 /* Send to hide tooltip */
349 vcdc_send_show_tooltip(pid, false);
352 if (-1 != vcd_client_manager_get_pid()) {
353 /* Manager client is available */
354 if (0 != vcdc_send_result_to_manager(vcd_client_manager_get_pid(), VC_RESULT_TYPE_NORMAL)) {
355 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Fail to send result");
359 vcd_client_manager_set_exclusive(false);
365 SLOG(LOG_DEBUG, TAG_VCD, "[Server] === Get engine result ===");
368 vc_cmd_s* temp_cmd = NULL;
369 vc_cmd_list_h vc_cmd_list = NULL;
371 if (0 != vc_cmd_list_create(&vc_cmd_list)) {
372 SLOG(LOG_ERROR, TAG_VCD, "[Server] Fail to create command list");
373 vcd_client_manager_set_exclusive(false);
374 vcd_config_set_service_state(VCD_STATE_READY);
375 vcdc_send_service_state(VCD_STATE_READY);
381 for (i = 0; i < count; i++) {
382 SLOG(LOG_DEBUG, TAG_VCD, "[Server] [%d] Result ID(%d)", i, result_id[i]);
384 if (result_id[i] < 0) {
385 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Result ID(%d) is NOT valid", result_id[i]);
389 ret = vcd_client_get_cmd_from_result_id(result_id[i], &temp_cmd);
390 if (0 == ret && NULL != temp_cmd) {
391 /* Add priority filter */
392 int temp_priority = __convert_type_to_priority(temp_cmd->type);
393 if (priority > temp_priority) {
394 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Ignore result by priority");
396 } else if (priority < temp_priority) {
397 SLOG(LOG_DEBUG, TAG_VCD, "[Server] High priority result!!");
398 priority = temp_priority;
400 if (0 != vc_cmd_list_remove_all(vc_cmd_list, true)) {
401 SLOG(LOG_ERROR, TAG_VCD, "[Server] Fail to list remove all");
405 switch (temp_cmd->format) {
406 case VC_CMD_FORMAT_FIXED:
407 /* Nonfixed result is NOT valid */
409 case VC_CMD_FORMAT_FIXED_AND_EXTRA:
410 if (NULL == temp_cmd->parameter) {
411 if (NULL != non_fixed_result) {
412 temp_cmd->parameter = strdup(non_fixed_result);
415 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Command type is NOT vaild. Parameter (%s)", temp_cmd->parameter);
418 case VC_CMD_FORMAT_EXTRA_AND_FIXED:
419 if (NULL == temp_cmd->command) {
420 if (NULL != non_fixed_result) {
421 temp_cmd->command = strdup(non_fixed_result);
424 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Command type is NOT vaild. Command (%s)", temp_cmd->command);
429 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Unknown command type : %d", temp_cmd->type);
433 if (0 != vc_cmd_list_add(vc_cmd_list, (vc_cmd_h)temp_cmd)) {
434 SLOG(LOG_DEBUG, TAG_VCD, "Fail to add command to list");
435 vc_cmd_destroy((vc_cmd_h)temp_cmd);
438 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] NOT found matached result(%d)", result_id[i]);
442 vc_cmd_print_list(vc_cmd_list);
444 SLOG(LOG_DEBUG, TAG_VCD, "[Server] =========================");
446 int result_count = 0;
447 vc_cmd_list_get_count(vc_cmd_list, &result_count);
449 if (false == vcd_client_manager_get_exclusive()) {
450 int pid = vcd_client_widget_get_foreground_pid();
452 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Request tooltip hide");
453 vcdc_send_show_tooltip(pid, false);
456 vc_info_parser_set_result(all_result, event, msg, vc_cmd_list, false);
458 if (-1 != vcd_client_manager_get_pid()) {
459 /* Manager client is available */
460 if (0 != vcdc_send_result_to_manager(vcd_client_manager_get_pid(), VC_RESULT_TYPE_NORMAL)) {
461 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Fail to send result");
464 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Manager is NOT available. Send result to client directly");
465 /* Send result to client */
466 ecore_timer_add(0, __vcd_send_selected_result, NULL);
469 /* exclusive command */
470 vc_info_parser_set_result(all_result, event, msg, vc_cmd_list, true);
472 if (-1 != vcd_client_manager_get_pid()) {
473 /* Manager client is available */
474 if (0 != vcdc_send_result_to_manager(vcd_client_manager_get_pid(), VC_RESULT_TYPE_NORMAL)) {
475 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Fail to send result");
478 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Manager is NOT available");
481 vcd_client_manager_set_exclusive(false);
484 vc_cmd_list_destroy(vc_cmd_list, true);
490 * vcd server Interfaces
492 static void __vcd_file_clean_up()
494 SLOG(LOG_DEBUG, TAG_VCD, "== Old file clean up == ");
499 struct dirent *dirp = NULL;
501 dp = opendir(VC_RUNTIME_INFO_ROOT);
503 SLOG(LOG_ERROR, TAG_VCD, "[File message WARN] Fail to open path : %s", VC_RUNTIME_INFO_ROOT);
507 char remove_path[256] = {0, };
509 ret = readdir_r(dp, &entry, &dirp);
511 SLOG(LOG_ERROR, TAG_VCD, "[File ERROR] Fail to read directory");
516 if (!strncmp("vc_", dirp->d_name, strlen("vc_"))) {
517 memset(remove_path, 0, 256);
518 snprintf(remove_path, 256, "%s/%s", VC_RUNTIME_INFO_ROOT, dirp->d_name);
521 if (0 != remove(remove_path)) {
522 SLOG(LOG_WARN, TAG_VCD, "[File message WARN] Fail to remove file : %s", remove_path);
524 SLOG(LOG_DEBUG, TAG_VCD, "[File message] Remove file : %s", remove_path);
528 } while (NULL != dirp);
539 /* Remove old file */
540 __vcd_file_clean_up();
541 /* Send error signal for notifying that daemon is reset*/
542 vcdc_send_error_signal(VCD_ERROR_SERVICE_RESET, "Daemon reset");
544 /* initialize modules */
545 ret = vcd_config_initialize(__config_lang_changed_cb, __config_foreground_changed_cb, NULL);
547 SLOG(LOG_ERROR, TAG_VCD, "[Server WARNING] Fail to initialize config.");
550 vcd_config_set_service_state(VCD_STATE_NONE);
552 ret = vcd_engine_agent_init(__vcd_server_pre_result_cb, __vcd_server_result_cb);
554 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to engine agent initialize : result(%d)", ret);
558 if (0 != vcd_recorder_create(__server_recorder_callback, __server_recorder_interrupt_callback)) {
559 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to create recorder");
560 return VCD_ERROR_OPERATION_FAILED;
564 ret = vcd_engine_agent_initialize_current_engine();
566 if (VCD_ERROR_ENGINE_NOT_FOUND == ret)
567 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] There is No Voice control engine");
569 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to init engine");
577 if (0 != vcd_engine_agent_load_current_engine()) {
578 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to load current engine");
579 return VCD_ERROR_OPERATION_FAILED;
582 /* Initialize manager info */
583 vcd_client_manager_unset();
585 vcd_config_set_service_state(VCD_STATE_READY);
586 vcdc_send_service_state(VCD_STATE_READY);
588 SLOG(LOG_DEBUG, TAG_VCD, "[Server SUCCESS] initialize");
596 if (0 < g_list_length(g_proc_list)) {
597 iter = g_list_first(g_proc_list);
598 while (NULL != iter) {
599 g_proc_list = g_list_remove_link(g_proc_list, iter);
600 iter = g_list_first(g_proc_list);
604 if (g_restart_timer != NULL) {
605 ecore_timer_del(g_restart_timer);
606 g_restart_timer = NULL;
609 vcd_state_e state = vcd_config_get_service_state();
610 if (VCD_STATE_READY != state) {
611 if (VCD_STATE_RECORDING == state) {
614 vcd_engine_recognize_cancel();
616 if (0 != vcd_recorder_destroy()) {
617 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to destroy recorder");
619 SLOG(LOG_DEBUG, TAG_VCD, "[Server] destroy recorder");
622 if (0 != vcd_engine_agent_release()) {
623 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to release engine");
625 SLOG(LOG_DEBUG, TAG_VCD, "[Server] release engine");
628 vcd_config_set_service_state(VCD_STATE_NONE);
629 vcdc_send_service_state(VCD_STATE_NONE);
631 SLOG(LOG_DEBUG, TAG_VCD, "[Server] mode finalize");
636 static Eina_Bool __finalize_quit_ecore_loop(void *data)
638 SLOG(LOG_DEBUG, TAG_VCD, "[Server] quit ecore main loop");
639 ecore_main_loop_quit();
643 static void __read_proc()
647 struct dirent *dirp = NULL;
652 if (0 < g_list_length(g_proc_list)) {
653 iter = g_list_first(g_proc_list);
654 while (NULL != iter) {
655 g_proc_list = g_list_remove_link(g_proc_list, iter);
656 iter = g_list_first(g_proc_list);
660 dp = opendir("/proc");
662 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Fail to open proc");
665 ret = readdir_r(dp, &entry, &dirp);
667 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Fail to readdir");
672 tmp = atoi(dirp->d_name);
673 if (0 >= tmp) continue;
674 g_proc_list = g_list_append(g_proc_list, GINT_TO_POINTER(tmp));
676 } while (NULL != dirp);
682 static void __vcd_cleanup_client(vcd_client_type_e type)
684 int* client_list = NULL;
685 int client_count = 0;
692 if (VCD_CLIENT_TYPE_NORMAL == type) {
693 ret = vcd_client_get_list(&client_list, &client_count);
694 } else if (VCD_CLIENT_TYPE_WIDGET == type) {
695 ret = vcd_client_widget_get_list(&client_list, &client_count);
696 } else if (VCD_CLIENT_TYPE_MANAGER == type) {
697 mgr_pid = vcd_client_manager_get_pid();
698 client_list = &mgr_pid;
701 SLOG(LOG_WARN, TAG_VCD, "[WARNING] Invalid Manager pid");
706 if (0 == ret || mgr_pid > 0) {
707 SLOG(LOG_DEBUG, TAG_VCD, "===== Clean up %s client ", type ? (type == 1) ? "Widget" : "Manager" : "Normal");
708 if (NULL != client_list && client_count > 0) {
709 for (i = 0; i < client_count; i++) {
712 for (j = 0; j < g_list_length(g_proc_list); j++) {
713 iter = g_list_nth(g_proc_list, j);
715 if (client_list[i] == GPOINTER_TO_INT(iter->data)) {
716 SLOG(LOG_DEBUG, TAG_VCD, "%s pid(%d) is running", type ? (type == 1) ? "Widget" : "Manager" : "Normal", client_list[i]);
723 if (false == exist) {
724 SLOG(LOG_ERROR, TAG_VCD, "%s pid(%d) should be removed", type ? (type == 1) ? "Widget" : "Manager" : "Normal", client_list[i]);
725 if (VCD_CLIENT_TYPE_NORMAL == type)
726 vcd_server_finalize(client_list[i]);
727 else if (VCD_CLIENT_TYPE_WIDGET == type)
728 vcd_server_widget_finalize(client_list[i]);
730 vcd_server_mgr_finalize(mgr_pid);
734 SLOG(LOG_DEBUG, TAG_VCD, "=====");
735 SLOG(LOG_DEBUG, TAG_VCD, " ");
737 if (NULL != client_list && -1 == mgr_pid) {
744 Eina_Bool vcd_cleanup_client_all(void *data)
748 __vcd_cleanup_client(VCD_CLIENT_TYPE_NORMAL);
749 __vcd_cleanup_client(VCD_CLIENT_TYPE_WIDGET);
750 __vcd_cleanup_client(VCD_CLIENT_TYPE_MANAGER);
753 if (0 == vcd_client_get_list(&client_list, &client_count)) {
754 SLOG(LOG_DEBUG, TAG_VCD, "===== Clean up client ");
755 if (NULL != client_list && client_count > 0) {
756 for (i = 0; i < client_count; i++) {
759 for (j = 0; j < g_list_length(g_proc_list); j++) {
760 iter = g_list_nth(g_proc_list, j);
762 if (client_list[i] == GPOINTER_TO_INT(iter->data)) {
763 SLOG(LOG_DEBUG, TAG_VCD, "pid(%d) is running", client_list[i]);
770 if (false == exist) {
771 SLOG(LOG_ERROR, TAG_VCD, "pid(%d) should be removed", client_list[i]);
772 vcd_server_finalize(client_list[i]);
775 result = vcdc_send_hello(client_list[i], VCD_CLIENT_TYPE_NORMAL);
778 SLOG(LOG_DEBUG, TAG_VCD, "[Server] pid(%d) should be removed.", client_list[i]);
779 vcd_server_finalize(client_list[i]);
780 } else if (-1 == result) {
781 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Hello result has error");
786 SLOG(LOG_DEBUG, TAG_VCD, "=====");
787 SLOG(LOG_DEBUG, TAG_VCD, " ");
789 if (NULL != client_list) {
794 /* If app is in background state, app cannot response message. */
795 if (0 == vcd_client_widget_get_list(&client_list, &client_count)) {
796 SLOG(LOG_DEBUG, TAG_VCD, "===== Clean up widget");
797 if (NULL != client_list && client_count > 0) {
798 for (i = 0; i < client_count; i++) {
801 for (j = 0; j < g_list_length(g_proc_list); j++) {
802 iter = g_list_nth(g_proc_list, j);
804 if (client_list[i] == GPOINTER_TO_INT(iter->data)) {
805 SLOG(LOG_DEBUG, TAG_VCD, "widget pid(%d) is running", client_list[i]);
812 if (false == exist) {
813 SLOG(LOG_ERROR, TAG_VCD, "widget pid(%d) should be removed", client_list[i]);
814 vcd_server_widget_finalize(client_list[i]);
817 result = vcdc_send_hello(client_list[i], VCD_CLIENT_TYPE_WIDGET);
820 SLOG(LOG_DEBUG, TAG_VCD, "[Server] widget pid(%d) should be removed.", client_list[i]);
821 vcd_server_widget_finalize(client_list[i]);
822 } else if (-1 == result) {
823 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Hello result has error");
828 SLOG(LOG_DEBUG, TAG_VCD, "=====");
829 SLOG(LOG_DEBUG, TAG_VCD, " ");
832 if (NULL != client_list) {
840 int mgr_pid = vcd_client_manager_get_pid();
842 for (j = 0; j < g_list_length(g_proc_list); j++) {
843 iter = g_list_nth(g_proc_list, j);
845 if (mgr_pid == GPOINTER_TO_INT(iter->data)) {
846 SLOG(LOG_DEBUG, TAG_VCD, "manager pid(%d) is running", mgr_pid);
853 if (false == exist) {
854 SLOG(LOG_ERROR, TAG_VCD, "manager pid (%d) should be removed", mgr_pid);
855 vcd_server_mgr_finalize(mgr_pid);
862 int vcd_server_get_service_state()
864 return vcd_config_get_service_state();
867 int vcd_server_get_foreground()
870 vcd_config_get_foreground(&pid);
878 int vcd_server_mgr_initialize(int pid)
880 if (false == g_is_engine) {
881 if (0 != vcd_engine_agent_initialize_current_engine()) {
882 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] No Engine");
884 return VCD_ERROR_ENGINE_NOT_FOUND;
890 /* check if pid is valid */
891 if (false == vcd_client_manager_is_valid(pid)) {
892 SLOG(LOG_ERROR, TAG_VCD, "[Server] old manager pid(%d) be removed", vcd_client_manager_get_pid());
893 vcd_server_mgr_cancel();
894 vcd_client_manager_unset();
897 /* Add client information to client manager */
898 if (0 != vcd_client_manager_set(pid)) {
899 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to add manager");
900 return VCD_ERROR_OPERATION_FAILED;
903 SLOG(LOG_DEBUG, TAG_VCD, "[Server Success] Manager initialize : pid(%d)", pid);
905 return VCD_ERROR_NONE;
908 int vcd_server_mgr_finalize(int pid)
910 /* check if pid is valid */
911 if (false == vcd_client_manager_is_valid(pid)) {
912 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] The manager pid(%d) is NOT valid", pid);
913 return VCD_ERROR_INVALID_PARAMETER;
916 /* Cancel recognition */
917 vcd_server_mgr_cancel();
919 /* Remove manager information */
920 if (0 != vcd_client_manager_unset()) {
921 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Fail to delete client");
924 if (0 == vcd_client_get_ref_count()) {
925 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Connected client list is empty");
926 ecore_timer_add(0, __finalize_quit_ecore_loop, NULL);
929 SLOG(LOG_DEBUG, TAG_VCD, "[Server Success] Manager Finalize : pid(%d)", pid);
931 return VCD_ERROR_NONE;
934 int vcd_server_mgr_set_command(int pid)
936 if (0 != vcd_client_manager_set_command(pid)) {
937 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] The manager pid(%d) is NOT valid", pid);
938 return VCD_ERROR_INVALID_PARAMETER;
940 return VCD_ERROR_NONE;
943 int vcd_server_mgr_unset_command(int pid)
945 if (0 != vcd_client_manager_unset_command(pid)) {
946 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] The manager pid(%d) is NOT valid", pid);
947 return VCD_ERROR_INVALID_PARAMETER;
949 return VCD_ERROR_NONE;
952 int vcd_server_mgr_set_demandable_client(int pid)
954 /* check if pid is valid */
955 if (false == vcd_client_manager_is_valid(pid)) {
956 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] The manager pid(%d) is NOT valid", pid);
957 return VCD_ERROR_INVALID_PARAMETER;
960 GSList* client_list = NULL;
961 if (0 != vc_info_parser_get_demandable_clients(&client_list)) {
962 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to get demandable client");
963 return VCD_ERROR_OPERATION_FAILED;
966 /* Save client list */
967 if (0 != vcd_client_manager_set_demandable_client(pid, client_list)) {
968 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to set demandable client list");
969 return VCD_ERROR_OPERATION_FAILED;
972 return VCD_ERROR_NONE;
975 int vcd_server_mgr_set_audio_type(int pid, const char* audio_type)
977 /* check if pid is valid */
978 if (false == vcd_client_manager_is_valid(pid)) {
979 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] The manager pid(%d) is NOT valid", pid);
980 return VCD_ERROR_INVALID_PARAMETER;
984 vcp_audio_type_e type = VCP_AUDIO_TYPE_PCM_S16_LE;
988 ret = vcd_engine_get_audio_format(audio_type, &type, &rate, &channel);
990 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to get audio format : %d", ret);
991 return VCD_ERROR_OPERATION_FAILED;
994 ret = vcd_recorder_set(audio_type, type, rate, channel);
996 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to set audio in type : %d", ret);
997 return VCD_ERROR_OPERATION_FAILED;
1000 return VCD_ERROR_NONE;
1003 int vcd_server_mgr_get_audio_type(int pid, char** audio_type)
1005 /* check if pid is valid */
1006 if (false == vcd_client_manager_is_valid(pid)) {
1007 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] The manager pid(%d) is NOT valid", pid);
1008 return VCD_ERROR_INVALID_PARAMETER;
1011 int ret = vcd_recorder_get(audio_type);
1013 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to get audio in type : %d", ret);
1014 return VCD_ERROR_OPERATION_FAILED;
1017 return VCD_ERROR_NONE;
1020 int vcd_server_mgr_set_client_info(int pid)
1022 /* check if pid is valid */
1023 if (false == vcd_client_manager_is_valid(pid)) {
1024 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] The manager pid(%d) is NOT valid", pid);
1025 return VCD_ERROR_INVALID_PARAMETER;
1028 int ret = vcd_client_save_client_info();
1030 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to save client info : %d", ret);
1031 return VCD_ERROR_OPERATION_FAILED;
1034 return VCD_ERROR_NONE;
1037 static int __start_internal_recognition()
1041 /* 2. Get commands */
1042 ret = vcd_client_command_collect_command();
1044 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to collect command : %d", ret);
1045 return VCD_ERROR_OPERATION_FAILED;
1048 ret = vcd_client_get_length();
1050 SLOG(LOG_WARN, TAG_VCD, "[Server WARNIING] No current command : %d", ret);
1051 return VCD_ERROR_OPERATION_FAILED;
1054 /* 3. Set command to engine */
1055 ret = vcd_engine_set_commands();
1057 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to collect command : %d", ret);
1058 return VCD_ERROR_OPERATION_FAILED;
1061 SLOG(LOG_DEBUG, TAG_VCD, "[Server Success] Set command");
1063 bool stop_by_silence = true;
1064 if (VCD_RECOGNITION_MODE_MANUAL == vcd_client_get_recognition_mode()) {
1065 stop_by_silence = false;
1068 /* 4. start recognition */
1069 ret = vcd_engine_recognize_start(stop_by_silence);
1071 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to start recognition : result(%d)", ret);
1072 return VCD_ERROR_OPERATION_FAILED;
1075 SLOG(LOG_DEBUG, TAG_VCD, "[Server Success] Start engine");
1077 /* 5. recorder start */
1078 ret = vcd_recorder_start();
1080 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to start recorder : result(%d)", ret);
1081 vcd_engine_recognize_cancel();
1085 vcd_config_set_service_state(VCD_STATE_RECORDING);
1086 vcdc_send_service_state(VCD_STATE_RECORDING);
1088 SLOG(LOG_DEBUG, TAG_VCD, "[Server Success] Start recognition(%d)", stop_by_silence);
1093 static Eina_Bool __vcd_request_show_tooltip(void *data)
1095 int pid = vcd_client_widget_get_foreground_pid();
1097 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Request tooltip show and widget command");
1098 vcdc_send_show_tooltip(pid, (bool)data);
1104 int vcd_server_mgr_start(vcd_recognition_mode_e recognition_mode, bool exclusive_cmd, bool start_by_client)
1106 /* 1. check current state */
1107 vcd_state_e state = vcd_config_get_service_state();
1109 if (VCD_STATE_READY != state) {
1110 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Current state is not ready");
1111 return VCD_ERROR_INVALID_STATE;
1114 SLOG(LOG_DEBUG, TAG_VCD, "[Server] set recognition mode = %d", recognition_mode);
1115 vcd_client_set_recognition_mode(recognition_mode);
1117 if (false == exclusive_cmd) {
1118 /* Notify show tooltip */
1119 int pid = vcd_client_widget_get_foreground_pid();
1121 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Request tooltip show and widget command");
1122 ecore_timer_add(0, __vcd_request_show_tooltip, (void*)true);
1126 vcd_client_manager_set_exclusive(exclusive_cmd);
1130 if (true == start_by_client) {
1131 /* Get foreground pid */
1132 if (0 != vcd_config_get_foreground(&fg_pid)) {
1133 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to get foreground");
1136 /* Set client exclusive option */
1137 if (0 != vcd_client_set_exclusive_command(fg_pid)) {
1138 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to set exclusive command");
1142 int ret = __start_internal_recognition();
1144 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to start recongition : %d", ret);
1148 if (true == start_by_client) {
1149 vcd_client_unset_exclusive_command(fg_pid);
1152 return VCD_ERROR_NONE;
1155 int vcd_server_mgr_stop()
1157 /* 1. Check current state is recording */
1158 if (VCD_STATE_RECORDING != vcd_config_get_service_state()) {
1159 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Current state is not recording");
1160 return VCD_ERROR_INVALID_STATE;
1163 /* 2. Stop recorder */
1164 vcd_recorder_stop();
1166 /* 3. Stop engine recognition */
1167 int ret = vcd_engine_recognize_stop();
1169 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to stop recognition : %d", ret);
1172 /* 4. Set original mode */
1173 vcd_config_set_service_state(VCD_STATE_PROCESSING);
1174 vcdc_send_service_state(VCD_STATE_PROCESSING);
1176 return VCD_ERROR_NONE;
1179 int vcd_server_mgr_cancel()
1181 /* 1. Check current state */
1182 vcd_state_e state = vcd_config_get_service_state();
1183 if (VCD_STATE_RECORDING != state && VCD_STATE_PROCESSING != state) {
1184 SLOG(LOG_WARN, TAG_VCD, "[Server ERROR] Current state is not recording or processing");
1185 return VCD_ERROR_INVALID_STATE;
1188 if (g_restart_timer != NULL) {
1189 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Delete restart engine timer");
1190 ecore_timer_del(g_restart_timer);
1191 g_restart_timer = NULL;
1194 /* 2. Stop recorder */
1195 vcd_recorder_stop();
1196 /* 3. Cancel engine */
1197 int ret = vcd_engine_recognize_cancel();
1199 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Fail to cancel : %d", ret);
1202 if (false == vcd_client_manager_get_exclusive()) {
1203 int pid = vcd_client_widget_get_foreground_pid();
1205 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Request tooltip hide");
1206 ecore_timer_add(0, __vcd_request_show_tooltip, (void*)false);
1209 vcd_client_manager_set_exclusive(false);
1213 vcd_config_set_service_state(VCD_STATE_READY);
1214 vcdc_send_service_state(VCD_STATE_READY);
1216 return VCD_ERROR_NONE;
1220 int vcd_server_mgr_result_select()
1222 __vcd_send_selected_result(NULL);
1224 return VCD_ERROR_NONE;
1228 * VC Server Functions for Client
1230 int vcd_server_initialize(int pid)
1232 if (false == g_is_engine) {
1233 if (0 != vcd_engine_agent_initialize_current_engine()) {
1234 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] No Engine");
1235 g_is_engine = false;
1236 return VCD_ERROR_ENGINE_NOT_FOUND;
1242 if (false == vcd_engine_is_available_engine()) {
1243 if (0 != vcd_engine_agent_initialize_current_engine()) {
1244 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] No Engine");
1245 return VCD_ERROR_ENGINE_NOT_FOUND;
1249 /* check if pid is valid */
1250 if (true == vcd_client_is_available(pid)) {
1251 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] The pid is already exist");
1252 return VCD_ERROR_INVALID_PARAMETER;
1255 /* Add client information to client manager */
1256 if (0 != vcd_client_add(pid)) {
1257 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to add client info");
1258 return VCD_ERROR_OPERATION_FAILED;
1261 SLOG(LOG_DEBUG, TAG_VCD, "[Server Success] Client Initialize : pid(%d)", pid);
1263 return VCD_ERROR_NONE;
1266 int vcd_server_finalize(int pid)
1268 /* check if pid is valid */
1269 if (false == vcd_client_is_available(pid)) {
1270 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] pid is NOT valid ");
1271 return VCD_ERROR_INVALID_PARAMETER;
1274 /* Remove client information */
1275 if (0 != vcd_client_delete(pid)) {
1276 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Fail to delete client");
1279 if (0 == vcd_client_get_ref_count()) {
1280 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Connected client list is empty");
1281 ecore_timer_add(0, __finalize_quit_ecore_loop, NULL);
1284 SLOG(LOG_DEBUG, TAG_VCD, "[Server Success] Client Finalize : pid(%d)", pid);
1286 return VCD_ERROR_NONE;
1289 int vcd_server_set_command(int pid, vc_cmd_type_e cmd_type)
1291 /* check if pid is valid */
1292 if (false == vcd_client_is_available(pid)) {
1293 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] pid is NOT valid ");
1294 return VCD_ERROR_INVALID_PARAMETER;
1297 if (0 != vcd_client_set_command_type(pid, cmd_type)) {
1298 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to set command type : pid(%d), cmd_type(%d)", pid, cmd_type);
1299 return VCD_ERROR_OPERATION_FAILED;
1305 int vcd_server_unset_command(int pid, vc_cmd_type_e cmd_type)
1307 /* check if pid is valid */
1308 if (false == vcd_client_is_available(pid)) {
1309 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] pid is NOT valid ");
1310 return VCD_ERROR_INVALID_PARAMETER;
1313 if (0 != vcd_client_unset_command_type(pid, cmd_type)) {
1314 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to unset command type : pid(%d), cmd_type(%d)", pid, cmd_type);
1315 return VCD_ERROR_OPERATION_FAILED;
1321 int vcd_server_set_foreground(int pid, bool value)
1323 /* check if pid is valid */
1324 if (false == vcd_client_is_available(pid) && false == vcd_client_widget_is_available(pid)) {
1325 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] pid is NOT valid ");
1326 return VCD_ERROR_INVALID_PARAMETER;
1329 if (0 != vcd_config_set_foreground(pid, value)) {
1330 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to set foreground : pid(%d), value(%d)", pid, value);
1331 return VCD_ERROR_OPERATION_FAILED;
1338 int vcd_server_set_exclusive_command(int pid, bool value)
1340 /* check if pid is valid */
1341 if (false == vcd_client_is_available(pid)) {
1342 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] pid is NOT valid ");
1343 return VCD_ERROR_INVALID_PARAMETER;
1346 if (true == value) {
1347 if (0 != vcd_client_set_exclusive_command(pid)) {
1348 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to set exclusive command : pid(%d)", pid);
1349 return VCD_ERROR_OPERATION_FAILED;
1352 if (0 != vcd_client_unset_exclusive_command(pid)) {
1353 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to unset exclusive command : pid(%d)", pid);
1354 return VCD_ERROR_OPERATION_FAILED;
1361 int vcd_server_request_start(int pid, bool stop_by_silence)
1363 /* check if pid is valid */
1364 if (false == vcd_client_is_available(pid)) {
1365 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] pid(%d) is NOT forground client", pid);
1366 return VCD_ERROR_INVALID_PARAMETER;
1370 /* Check current state */
1371 vcd_state_e state = vcd_config_get_service_state();
1373 /* Service state should be ready */
1374 if (VCD_STATE_READY != state) {
1375 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Current state is not Ready : pid(%d)", pid);
1376 return VCD_ERROR_INVALID_STATE;
1379 if (-1 != vcd_client_manager_get_pid()) {
1380 /* Check current pid is valid */
1381 if (false == vcd_client_manager_check_demandable_client(pid)) {
1382 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Current client is NOT available : pid(%d)", pid);
1383 return VCD_ERROR_INVALID_PARAMETER;
1387 ret = vcd_server_mgr_start(stop_by_silence, false);
1389 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Fail to start recognition");
1390 return VCD_ERROR_INVALID_PARAMETER;
1396 int vcd_server_request_stop(int pid)
1399 /* Check current state */
1400 vcd_state_e state = vcd_config_get_service_state();
1402 /* Service state should be ready */
1403 if (VCD_STATE_RECORDING != state) {
1404 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Current state is not Recording : pid(%d)", pid);
1405 return VCD_ERROR_INVALID_STATE;
1408 if (-1 != vcd_client_manager_get_pid()) {
1409 /* Check current pid is valid */
1410 if (false == vcd_client_manager_check_demandable_client(pid)) {
1411 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Current client is NOT available : pid(%d)", pid);
1412 return VCD_ERROR_INVALID_PARAMETER;
1416 ret = vcd_server_mgr_stop();
1418 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Fail to start recognition");
1419 return VCD_ERROR_OPERATION_FAILED;
1422 return VCD_ERROR_NONE;
1425 int vcd_server_request_cancel(int pid)
1428 /* Check current state */
1429 vcd_state_e state = vcd_config_get_service_state();
1431 /* Service state should be recording or processing */
1432 if (VCD_STATE_RECORDING != state && VCD_STATE_PROCESSING != state) {
1433 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Current state is not Recording or Processing : pid(%d)", pid);
1434 return VCD_ERROR_INVALID_STATE;
1437 if (-1 != vcd_client_manager_get_pid()) {
1438 /* Check current pid is valid */
1439 if (false == vcd_client_manager_check_demandable_client(pid)) {
1440 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Current client is NOT available : pid(%d)", pid);
1441 return VCD_ERROR_INVALID_PARAMETER;
1445 ret = vcd_server_mgr_cancel();
1447 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Fail to start recognition");
1448 return VCD_ERROR_OPERATION_FAILED;
1451 return VCD_ERROR_NONE;
1456 * VC Server Functions for Widget lib
1458 int vcd_server_widget_initialize(int pid)
1460 if (false == g_is_engine) {
1461 if (0 != vcd_engine_agent_initialize_current_engine()) {
1462 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] No Engine");
1463 g_is_engine = false;
1464 return VCD_ERROR_ENGINE_NOT_FOUND;
1470 if (false == vcd_engine_is_available_engine()) {
1471 if (0 != vcd_engine_agent_initialize_current_engine()) {
1472 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] No Engine");
1473 return VCD_ERROR_ENGINE_NOT_FOUND;
1477 /* check if pid is valid */
1478 if (true == vcd_client_widget_is_available(pid)) {
1479 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] The pid is already exist");
1480 return VCD_ERROR_INVALID_PARAMETER;
1483 /* Add client information to client manager */
1484 if (0 != vcd_client_widget_add(pid)) {
1485 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to add client info");
1486 return VCD_ERROR_OPERATION_FAILED;
1489 SLOG(LOG_DEBUG, TAG_VCD, "[Server Success] Initialize widget : pid(%d)", pid);
1491 return VCD_ERROR_NONE;
1494 int vcd_server_widget_finalize(int pid)
1496 /* check if pid is valid */
1497 if (false == vcd_client_widget_is_available(pid)) {
1498 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] pid is NOT valid ");
1499 return VCD_ERROR_INVALID_PARAMETER;
1502 /* Remove client information */
1503 if (0 != vcd_client_widget_delete(pid)) {
1504 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Fail to delete client");
1507 if (0 == vcd_client_get_ref_count()) {
1508 SLOG(LOG_DEBUG, TAG_VCD, "[Server] connected client list is empty");
1509 ecore_timer_add(0, __finalize_quit_ecore_loop, NULL);
1512 return VCD_ERROR_NONE;
1515 int vcd_server_widget_start_recording(int pid, bool widget_command)
1517 /* check if pid is valid */
1518 if (false == vcd_client_widget_is_available(pid)) {
1519 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] pid is NOT valid ");
1520 return VCD_ERROR_INVALID_PARAMETER;
1523 if (true == widget_command) {
1524 vcd_client_widget_set_command(pid);
1525 SLOG(LOG_DEBUG, TAG_VCD, "[Server] widget command is available");
1527 vcd_client_widget_unset_command(pid);
1528 SLOG(LOG_DEBUG, TAG_VCD, "[Server] widget command is NOT available");
1531 int ret = __start_internal_recognition();
1533 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to start recongition : %d", ret);
1534 ecore_timer_add(0, __vcd_request_show_tooltip, (void*)false);
1540 int vcd_server_widget_start(int pid, bool stop_by_silence)
1542 /* check if pid is valid */
1543 int fore_pid = vcd_client_widget_get_foreground_pid();
1544 if (pid != fore_pid) {
1545 SLOG(LOG_ERROR, TAG_VCD, "[Server] pid is NOT foreground");
1546 return VCD_ERROR_INVALID_PARAMETER;
1549 /* Check current state */
1550 vcd_state_e state = vcd_config_get_service_state();
1552 /* Service state should be ready */
1553 if (VCD_STATE_READY != state) {
1554 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Current state is not Ready : pid(%d)", pid);
1555 return VCD_ERROR_INVALID_STATE;
1558 vcd_client_set_slience_detection(stop_by_silence);
1560 /* Notify show tooltip */
1561 ecore_timer_add(0, __vcd_request_show_tooltip, (void*)true);
1566 int vcd_server_widget_stop(int pid)
1568 /* check if pid is valid */
1569 int fore_pid = vcd_client_widget_get_foreground_pid();
1570 if (pid != fore_pid) {
1571 SLOG(LOG_ERROR, TAG_VCD, "[Server] pid is NOT foreground");
1572 return VCD_ERROR_INVALID_PARAMETER;
1576 /* Check current state */
1577 vcd_state_e state = vcd_config_get_service_state();
1579 /* Service state should be recording */
1580 if (VCD_STATE_RECORDING != state) {
1581 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Current service state is not Recording : pid(%d)", pid);
1582 return VCD_ERROR_INVALID_STATE;
1585 ret = vcd_server_mgr_stop();
1587 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Fail to start recognition");
1588 return VCD_ERROR_OPERATION_FAILED;
1591 return VCD_ERROR_NONE;
1594 int vcd_server_widget_cancel(int pid)
1596 /* check if pid is valid */
1597 int fore_pid = vcd_client_widget_get_foreground_pid();
1598 if (pid != fore_pid) {
1599 SLOG(LOG_ERROR, TAG_VCD, "[Server] pid is NOT foreground");
1600 return VCD_ERROR_INVALID_PARAMETER;
1604 /* Check current state */
1605 vcd_state_e state = vcd_config_get_service_state();
1607 /* Service state should be recording or processing */
1608 if (VCD_STATE_RECORDING != state && VCD_STATE_PROCESSING != state) {
1609 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Current state is not Recording or Processing : pid(%d)", pid);
1610 return VCD_ERROR_INVALID_STATE;
1613 ret = vcd_server_mgr_cancel();
1615 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Fail to cancel recognition : %d", ret);
1619 return VCD_ERROR_NONE;