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;
702 if (0 == ret || mgr_pid > 0) {
703 SLOG(LOG_DEBUG, TAG_VCD, "===== Clean up %s client ", type ? (type == 1) ? "Widget" : "Manager" : "Normal");
704 if (NULL != client_list && client_count > 0) {
705 for (i = 0; i < client_count; i++) {
708 for (j = 0; j < g_list_length(g_proc_list); j++) {
709 iter = g_list_nth(g_proc_list, j);
711 if (client_list[i] == GPOINTER_TO_INT(iter->data)) {
712 SLOG(LOG_DEBUG, TAG_VCD, "%s pid(%d) is running", type ? (type == 1) ? "Widget" : "Manager" : "Normal", client_list[i]);
719 if (false == exist) {
720 SLOG(LOG_ERROR, TAG_VCD, "%s pid(%d) should be removed", type ? (type == 1) ? "Widget" : "Manager" : "Normal", client_list[i]);
721 if (VCD_CLIENT_TYPE_NORMAL == type)
722 vcd_server_finalize(client_list[i]);
723 else if (VCD_CLIENT_TYPE_WIDGET == type)
724 vcd_server_widget_finalize(client_list[i]);
726 vcd_server_mgr_finalize(mgr_pid);
730 SLOG(LOG_DEBUG, TAG_VCD, "=====");
731 SLOG(LOG_DEBUG, TAG_VCD, " ");
733 if (NULL != client_list && -1 == mgr_pid) {
740 Eina_Bool vcd_cleanup_client_all(void *data)
744 __vcd_cleanup_client(VCD_CLIENT_TYPE_NORMAL);
745 __vcd_cleanup_client(VCD_CLIENT_TYPE_WIDGET);
746 __vcd_cleanup_client(VCD_CLIENT_TYPE_MANAGER);
749 if (0 == vcd_client_get_list(&client_list, &client_count)) {
750 SLOG(LOG_DEBUG, TAG_VCD, "===== Clean up client ");
751 if (NULL != client_list && client_count > 0) {
752 for (i = 0; i < client_count; i++) {
755 for (j = 0; j < g_list_length(g_proc_list); j++) {
756 iter = g_list_nth(g_proc_list, j);
758 if (client_list[i] == GPOINTER_TO_INT(iter->data)) {
759 SLOG(LOG_DEBUG, TAG_VCD, "pid(%d) is running", client_list[i]);
766 if (false == exist) {
767 SLOG(LOG_ERROR, TAG_VCD, "pid(%d) should be removed", client_list[i]);
768 vcd_server_finalize(client_list[i]);
771 result = vcdc_send_hello(client_list[i], VCD_CLIENT_TYPE_NORMAL);
774 SLOG(LOG_DEBUG, TAG_VCD, "[Server] pid(%d) should be removed.", client_list[i]);
775 vcd_server_finalize(client_list[i]);
776 } else if (-1 == result) {
777 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Hello result has error");
782 SLOG(LOG_DEBUG, TAG_VCD, "=====");
783 SLOG(LOG_DEBUG, TAG_VCD, " ");
785 if (NULL != client_list) {
790 /* If app is in background state, app cannot response message. */
791 if (0 == vcd_client_widget_get_list(&client_list, &client_count)) {
792 SLOG(LOG_DEBUG, TAG_VCD, "===== Clean up widget");
793 if (NULL != client_list && client_count > 0) {
794 for (i = 0; i < client_count; i++) {
797 for (j = 0; j < g_list_length(g_proc_list); j++) {
798 iter = g_list_nth(g_proc_list, j);
800 if (client_list[i] == GPOINTER_TO_INT(iter->data)) {
801 SLOG(LOG_DEBUG, TAG_VCD, "widget pid(%d) is running", client_list[i]);
808 if (false == exist) {
809 SLOG(LOG_ERROR, TAG_VCD, "widget pid(%d) should be removed", client_list[i]);
810 vcd_server_widget_finalize(client_list[i]);
813 result = vcdc_send_hello(client_list[i], VCD_CLIENT_TYPE_WIDGET);
816 SLOG(LOG_DEBUG, TAG_VCD, "[Server] widget pid(%d) should be removed.", client_list[i]);
817 vcd_server_widget_finalize(client_list[i]);
818 } else if (-1 == result) {
819 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Hello result has error");
824 SLOG(LOG_DEBUG, TAG_VCD, "=====");
825 SLOG(LOG_DEBUG, TAG_VCD, " ");
828 if (NULL != client_list) {
836 int mgr_pid = vcd_client_manager_get_pid();
838 for (j = 0; j < g_list_length(g_proc_list); j++) {
839 iter = g_list_nth(g_proc_list, j);
841 if (mgr_pid == GPOINTER_TO_INT(iter->data)) {
842 SLOG(LOG_DEBUG, TAG_VCD, "manager pid(%d) is running", mgr_pid);
849 if (false == exist) {
850 SLOG(LOG_ERROR, TAG_VCD, "manager pid (%d) should be removed", mgr_pid);
851 vcd_server_mgr_finalize(mgr_pid);
858 int vcd_server_get_service_state()
860 return vcd_config_get_service_state();
863 int vcd_server_get_foreground()
866 vcd_config_get_foreground(&pid);
874 int vcd_server_mgr_initialize(int pid)
876 if (false == g_is_engine) {
877 if (0 != vcd_engine_agent_initialize_current_engine()) {
878 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] No Engine");
880 return VCD_ERROR_ENGINE_NOT_FOUND;
886 /* check if pid is valid */
887 if (false == vcd_client_manager_is_valid(pid)) {
888 SLOG(LOG_ERROR, TAG_VCD, "[Server] old manager pid(%d) be removed", vcd_client_manager_get_pid());
889 vcd_server_mgr_cancel();
890 vcd_client_manager_unset();
893 /* Add client information to client manager */
894 if (0 != vcd_client_manager_set(pid)) {
895 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to add manager");
896 return VCD_ERROR_OPERATION_FAILED;
899 SLOG(LOG_DEBUG, TAG_VCD, "[Server Success] Manager initialize : pid(%d)", pid);
901 return VCD_ERROR_NONE;
904 int vcd_server_mgr_finalize(int pid)
906 /* check if pid is valid */
907 if (false == vcd_client_manager_is_valid(pid)) {
908 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] The manager pid(%d) is NOT valid", pid);
909 return VCD_ERROR_INVALID_PARAMETER;
912 /* Cancel recognition */
913 vcd_server_mgr_cancel();
915 /* Remove manager information */
916 if (0 != vcd_client_manager_unset()) {
917 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Fail to delete client");
920 if (0 == vcd_client_get_ref_count()) {
921 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Connected client list is empty");
922 ecore_timer_add(0, __finalize_quit_ecore_loop, NULL);
925 SLOG(LOG_DEBUG, TAG_VCD, "[Server Success] Manager Finalize : pid(%d)", pid);
927 return VCD_ERROR_NONE;
930 int vcd_server_mgr_set_command(int pid)
932 if (0 != vcd_client_manager_set_command(pid)) {
933 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] The manager pid(%d) is NOT valid", pid);
934 return VCD_ERROR_INVALID_PARAMETER;
936 return VCD_ERROR_NONE;
939 int vcd_server_mgr_unset_command(int pid)
941 if (0 != vcd_client_manager_unset_command(pid)) {
942 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] The manager pid(%d) is NOT valid", pid);
943 return VCD_ERROR_INVALID_PARAMETER;
945 return VCD_ERROR_NONE;
948 int vcd_server_mgr_set_demandable_client(int pid)
950 /* check if pid is valid */
951 if (false == vcd_client_manager_is_valid(pid)) {
952 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] The manager pid(%d) is NOT valid", pid);
953 return VCD_ERROR_INVALID_PARAMETER;
956 GSList* client_list = NULL;
957 if (0 != vc_info_parser_get_demandable_clients(&client_list)) {
958 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to get demandable client");
959 return VCD_ERROR_OPERATION_FAILED;
962 /* Save client list */
963 if (0 != vcd_client_manager_set_demandable_client(pid, client_list)) {
964 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to set demandable client list");
965 return VCD_ERROR_OPERATION_FAILED;
968 return VCD_ERROR_NONE;
971 int vcd_server_mgr_set_audio_type(int pid, const char* audio_type)
973 /* check if pid is valid */
974 if (false == vcd_client_manager_is_valid(pid)) {
975 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] The manager pid(%d) is NOT valid", pid);
976 return VCD_ERROR_INVALID_PARAMETER;
980 vcp_audio_type_e type = VCP_AUDIO_TYPE_PCM_S16_LE;
984 ret = vcd_engine_get_audio_format(audio_type, &type, &rate, &channel);
986 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to get audio format : %d", ret);
987 return VCD_ERROR_OPERATION_FAILED;
990 ret = vcd_recorder_set(audio_type, type, rate, channel);
992 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to set audio in type : %d", ret);
993 return VCD_ERROR_OPERATION_FAILED;
996 return VCD_ERROR_NONE;
999 int vcd_server_mgr_get_audio_type(int pid, char** audio_type)
1001 /* check if pid is valid */
1002 if (false == vcd_client_manager_is_valid(pid)) {
1003 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] The manager pid(%d) is NOT valid", pid);
1004 return VCD_ERROR_INVALID_PARAMETER;
1007 int ret = vcd_recorder_get(audio_type);
1009 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to get audio in type : %d", ret);
1010 return VCD_ERROR_OPERATION_FAILED;
1013 return VCD_ERROR_NONE;
1016 int vcd_server_mgr_set_client_info(int pid)
1018 /* check if pid is valid */
1019 if (false == vcd_client_manager_is_valid(pid)) {
1020 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] The manager pid(%d) is NOT valid", pid);
1021 return VCD_ERROR_INVALID_PARAMETER;
1024 int ret = vcd_client_save_client_info();
1026 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to save client info : %d", ret);
1027 return VCD_ERROR_OPERATION_FAILED;
1030 return VCD_ERROR_NONE;
1033 static int __start_internal_recognition()
1037 /* 2. Get commands */
1038 ret = vcd_client_command_collect_command();
1040 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to collect command : %d", ret);
1041 return VCD_ERROR_OPERATION_FAILED;
1044 ret = vcd_client_get_length();
1046 SLOG(LOG_WARN, TAG_VCD, "[Server WARNIING] No current command : %d", ret);
1047 return VCD_ERROR_OPERATION_FAILED;
1050 /* 3. Set command to engine */
1051 ret = vcd_engine_set_commands();
1053 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to collect command : %d", ret);
1054 return VCD_ERROR_OPERATION_FAILED;
1057 SLOG(LOG_DEBUG, TAG_VCD, "[Server Success] Set command");
1059 bool stop_by_silence = true;
1060 if (VCD_RECOGNITION_MODE_MANUAL == vcd_client_get_recognition_mode()) {
1061 stop_by_silence = false;
1064 /* 4. start recognition */
1065 ret = vcd_engine_recognize_start(stop_by_silence);
1067 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to start recognition : result(%d)", ret);
1068 return VCD_ERROR_OPERATION_FAILED;
1071 SLOG(LOG_DEBUG, TAG_VCD, "[Server Success] Start engine");
1073 /* 5. recorder start */
1074 ret = vcd_recorder_start();
1076 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to start recorder : result(%d)", ret);
1077 vcd_engine_recognize_cancel();
1081 vcd_config_set_service_state(VCD_STATE_RECORDING);
1082 vcdc_send_service_state(VCD_STATE_RECORDING);
1084 SLOG(LOG_DEBUG, TAG_VCD, "[Server Success] Start recognition(%d)", stop_by_silence);
1089 static Eina_Bool __vcd_request_show_tooltip(void *data)
1091 int pid = vcd_client_widget_get_foreground_pid();
1093 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Request tooltip show and widget command");
1094 vcdc_send_show_tooltip(pid, (bool)data);
1100 int vcd_server_mgr_start(vcd_recognition_mode_e recognition_mode, bool exclusive_cmd, bool start_by_client)
1102 /* 1. check current state */
1103 vcd_state_e state = vcd_config_get_service_state();
1105 if (VCD_STATE_READY != state) {
1106 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Current state is not ready");
1107 return VCD_ERROR_INVALID_STATE;
1110 SLOG(LOG_DEBUG, TAG_VCD, "[Server] set recognition mode = %d", recognition_mode);
1111 vcd_client_set_recognition_mode(recognition_mode);
1113 if (false == exclusive_cmd) {
1114 /* Notify show tooltip */
1115 int pid = vcd_client_widget_get_foreground_pid();
1117 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Request tooltip show and widget command");
1118 ecore_timer_add(0, __vcd_request_show_tooltip, (void*)true);
1122 vcd_client_manager_set_exclusive(exclusive_cmd);
1126 if (true == start_by_client) {
1127 /* Get foreground pid */
1128 if (0 != vcd_config_get_foreground(&fg_pid)) {
1129 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to get foreground");
1132 /* Set client exclusive option */
1133 if (0 != vcd_client_set_exclusive_command(fg_pid)) {
1134 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to set exclusive command");
1138 int ret = __start_internal_recognition();
1140 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to start recongition : %d", ret);
1144 if (true == start_by_client) {
1145 vcd_client_unset_exclusive_command(fg_pid);
1148 return VCD_ERROR_NONE;
1151 int vcd_server_mgr_stop()
1153 /* 1. Check current state is recording */
1154 if (VCD_STATE_RECORDING != vcd_config_get_service_state()) {
1155 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Current state is not recording");
1156 return VCD_ERROR_INVALID_STATE;
1159 /* 2. Stop recorder */
1160 vcd_recorder_stop();
1162 /* 3. Stop engine recognition */
1163 int ret = vcd_engine_recognize_stop();
1165 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to stop recognition : %d", ret);
1168 /* 4. Set original mode */
1169 vcd_config_set_service_state(VCD_STATE_PROCESSING);
1170 vcdc_send_service_state(VCD_STATE_PROCESSING);
1172 return VCD_ERROR_NONE;
1175 int vcd_server_mgr_cancel()
1177 /* 1. Check current state */
1178 vcd_state_e state = vcd_config_get_service_state();
1179 if (VCD_STATE_RECORDING != state && VCD_STATE_PROCESSING != state) {
1180 SLOG(LOG_WARN, TAG_VCD, "[Server ERROR] Current state is not recording or processing");
1181 return VCD_ERROR_INVALID_STATE;
1184 if (g_restart_timer != NULL) {
1185 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Delete restart engine timer");
1186 ecore_timer_del(g_restart_timer);
1187 g_restart_timer = NULL;
1190 /* 2. Stop recorder */
1191 vcd_recorder_stop();
1192 /* 3. Cancel engine */
1193 int ret = vcd_engine_recognize_cancel();
1195 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Fail to cancel : %d", ret);
1198 if (false == vcd_client_manager_get_exclusive()) {
1199 int pid = vcd_client_widget_get_foreground_pid();
1201 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Request tooltip hide");
1202 ecore_timer_add(0, __vcd_request_show_tooltip, (void*)false);
1205 vcd_client_manager_set_exclusive(false);
1209 vcd_config_set_service_state(VCD_STATE_READY);
1210 vcdc_send_service_state(VCD_STATE_READY);
1212 return VCD_ERROR_NONE;
1216 int vcd_server_mgr_result_select()
1218 __vcd_send_selected_result(NULL);
1220 return VCD_ERROR_NONE;
1224 * VC Server Functions for Client
1226 int vcd_server_initialize(int pid)
1228 if (false == g_is_engine) {
1229 if (0 != vcd_engine_agent_initialize_current_engine()) {
1230 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] No Engine");
1231 g_is_engine = false;
1232 return VCD_ERROR_ENGINE_NOT_FOUND;
1238 if (false == vcd_engine_is_available_engine()) {
1239 if (0 != vcd_engine_agent_initialize_current_engine()) {
1240 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] No Engine");
1241 return VCD_ERROR_ENGINE_NOT_FOUND;
1245 /* check if pid is valid */
1246 if (true == vcd_client_is_available(pid)) {
1247 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] The pid is already exist");
1248 return VCD_ERROR_INVALID_PARAMETER;
1251 /* Add client information to client manager */
1252 if (0 != vcd_client_add(pid)) {
1253 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to add client info");
1254 return VCD_ERROR_OPERATION_FAILED;
1257 SLOG(LOG_DEBUG, TAG_VCD, "[Server Success] Client Initialize : pid(%d)", pid);
1259 return VCD_ERROR_NONE;
1262 int vcd_server_finalize(int pid)
1264 /* check if pid is valid */
1265 if (false == vcd_client_is_available(pid)) {
1266 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] pid is NOT valid ");
1267 return VCD_ERROR_INVALID_PARAMETER;
1270 /* Remove client information */
1271 if (0 != vcd_client_delete(pid)) {
1272 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Fail to delete client");
1275 if (0 == vcd_client_get_ref_count()) {
1276 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Connected client list is empty");
1277 ecore_timer_add(0, __finalize_quit_ecore_loop, NULL);
1280 SLOG(LOG_DEBUG, TAG_VCD, "[Server Success] Client Finalize : pid(%d)", pid);
1282 return VCD_ERROR_NONE;
1285 int vcd_server_set_command(int pid, vc_cmd_type_e cmd_type)
1287 /* check if pid is valid */
1288 if (false == vcd_client_is_available(pid)) {
1289 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] pid is NOT valid ");
1290 return VCD_ERROR_INVALID_PARAMETER;
1293 if (0 != vcd_client_set_command_type(pid, cmd_type)) {
1294 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to set command type : pid(%d), cmd_type(%d)", pid, cmd_type);
1295 return VCD_ERROR_OPERATION_FAILED;
1301 int vcd_server_unset_command(int pid, vc_cmd_type_e cmd_type)
1303 /* check if pid is valid */
1304 if (false == vcd_client_is_available(pid)) {
1305 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] pid is NOT valid ");
1306 return VCD_ERROR_INVALID_PARAMETER;
1309 if (0 != vcd_client_unset_command_type(pid, cmd_type)) {
1310 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to unset command type : pid(%d), cmd_type(%d)", pid, cmd_type);
1311 return VCD_ERROR_OPERATION_FAILED;
1317 int vcd_server_set_foreground(int pid, bool value)
1319 /* check if pid is valid */
1320 if (false == vcd_client_is_available(pid) && false == vcd_client_widget_is_available(pid)) {
1321 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] pid is NOT valid ");
1322 return VCD_ERROR_INVALID_PARAMETER;
1325 if (0 != vcd_config_set_foreground(pid, value)) {
1326 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to set foreground : pid(%d), value(%d)", pid, value);
1327 return VCD_ERROR_OPERATION_FAILED;
1334 int vcd_server_set_exclusive_command(int pid, bool value)
1336 /* check if pid is valid */
1337 if (false == vcd_client_is_available(pid)) {
1338 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] pid is NOT valid ");
1339 return VCD_ERROR_INVALID_PARAMETER;
1342 if (true == value) {
1343 if (0 != vcd_client_set_exclusive_command(pid)) {
1344 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to set exclusive command : pid(%d)", pid);
1345 return VCD_ERROR_OPERATION_FAILED;
1348 if (0 != vcd_client_unset_exclusive_command(pid)) {
1349 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to unset exclusive command : pid(%d)", pid);
1350 return VCD_ERROR_OPERATION_FAILED;
1357 int vcd_server_request_start(int pid, bool stop_by_silence)
1359 /* check if pid is valid */
1360 if (false == vcd_client_is_available(pid)) {
1361 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] pid(%d) is NOT forground client", pid);
1362 return VCD_ERROR_INVALID_PARAMETER;
1366 /* Check current state */
1367 vcd_state_e state = vcd_config_get_service_state();
1369 /* Service state should be ready */
1370 if (VCD_STATE_READY != state) {
1371 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Current state is not Ready : pid(%d)", pid);
1372 return VCD_ERROR_INVALID_STATE;
1375 if (-1 != vcd_client_manager_get_pid()) {
1376 /* Check current pid is valid */
1377 if (false == vcd_client_manager_check_demandable_client(pid)) {
1378 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Current client is NOT available : pid(%d)", pid);
1379 return VCD_ERROR_INVALID_PARAMETER;
1383 ret = vcd_server_mgr_start(stop_by_silence, false);
1385 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Fail to start recognition");
1386 return VCD_ERROR_INVALID_PARAMETER;
1392 int vcd_server_request_stop(int pid)
1395 /* Check current state */
1396 vcd_state_e state = vcd_config_get_service_state();
1398 /* Service state should be ready */
1399 if (VCD_STATE_RECORDING != state) {
1400 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Current state is not Recording : pid(%d)", pid);
1401 return VCD_ERROR_INVALID_STATE;
1404 if (-1 != vcd_client_manager_get_pid()) {
1405 /* Check current pid is valid */
1406 if (false == vcd_client_manager_check_demandable_client(pid)) {
1407 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Current client is NOT available : pid(%d)", pid);
1408 return VCD_ERROR_INVALID_PARAMETER;
1412 ret = vcd_server_mgr_stop();
1414 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Fail to start recognition");
1415 return VCD_ERROR_OPERATION_FAILED;
1418 return VCD_ERROR_NONE;
1421 int vcd_server_request_cancel(int pid)
1424 /* Check current state */
1425 vcd_state_e state = vcd_config_get_service_state();
1427 /* Service state should be recording or processing */
1428 if (VCD_STATE_RECORDING != state && VCD_STATE_PROCESSING != state) {
1429 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Current state is not Recording or Processing : pid(%d)", pid);
1430 return VCD_ERROR_INVALID_STATE;
1433 if (-1 != vcd_client_manager_get_pid()) {
1434 /* Check current pid is valid */
1435 if (false == vcd_client_manager_check_demandable_client(pid)) {
1436 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Current client is NOT available : pid(%d)", pid);
1437 return VCD_ERROR_INVALID_PARAMETER;
1441 ret = vcd_server_mgr_cancel();
1443 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Fail to start recognition");
1444 return VCD_ERROR_OPERATION_FAILED;
1447 return VCD_ERROR_NONE;
1452 * VC Server Functions for Widget lib
1454 int vcd_server_widget_initialize(int pid)
1456 if (false == g_is_engine) {
1457 if (0 != vcd_engine_agent_initialize_current_engine()) {
1458 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] No Engine");
1459 g_is_engine = false;
1460 return VCD_ERROR_ENGINE_NOT_FOUND;
1466 if (false == vcd_engine_is_available_engine()) {
1467 if (0 != vcd_engine_agent_initialize_current_engine()) {
1468 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] No Engine");
1469 return VCD_ERROR_ENGINE_NOT_FOUND;
1473 /* check if pid is valid */
1474 if (true == vcd_client_widget_is_available(pid)) {
1475 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] The pid is already exist");
1476 return VCD_ERROR_INVALID_PARAMETER;
1479 /* Add client information to client manager */
1480 if (0 != vcd_client_widget_add(pid)) {
1481 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to add client info");
1482 return VCD_ERROR_OPERATION_FAILED;
1485 SLOG(LOG_DEBUG, TAG_VCD, "[Server Success] Initialize widget : pid(%d)", pid);
1487 return VCD_ERROR_NONE;
1490 int vcd_server_widget_finalize(int pid)
1492 /* check if pid is valid */
1493 if (false == vcd_client_widget_is_available(pid)) {
1494 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] pid is NOT valid ");
1495 return VCD_ERROR_INVALID_PARAMETER;
1498 /* Remove client information */
1499 if (0 != vcd_client_widget_delete(pid)) {
1500 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Fail to delete client");
1503 if (0 == vcd_client_get_ref_count()) {
1504 SLOG(LOG_DEBUG, TAG_VCD, "[Server] connected client list is empty");
1505 ecore_timer_add(0, __finalize_quit_ecore_loop, NULL);
1508 return VCD_ERROR_NONE;
1511 int vcd_server_widget_start_recording(int pid, bool widget_command)
1513 /* check if pid is valid */
1514 if (false == vcd_client_widget_is_available(pid)) {
1515 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] pid is NOT valid ");
1516 return VCD_ERROR_INVALID_PARAMETER;
1519 if (true == widget_command) {
1520 vcd_client_widget_set_command(pid);
1521 SLOG(LOG_DEBUG, TAG_VCD, "[Server] widget command is available");
1523 vcd_client_widget_unset_command(pid);
1524 SLOG(LOG_DEBUG, TAG_VCD, "[Server] widget command is NOT available");
1527 int ret = __start_internal_recognition();
1529 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to start recongition : %d", ret);
1530 ecore_timer_add(0, __vcd_request_show_tooltip, (void*)false);
1536 int vcd_server_widget_start(int pid, bool stop_by_silence)
1538 /* check if pid is valid */
1539 int fore_pid = vcd_client_widget_get_foreground_pid();
1540 if (pid != fore_pid) {
1541 SLOG(LOG_ERROR, TAG_VCD, "[Server] pid is NOT foreground");
1542 return VCD_ERROR_INVALID_PARAMETER;
1545 /* Check current state */
1546 vcd_state_e state = vcd_config_get_service_state();
1548 /* Service state should be ready */
1549 if (VCD_STATE_READY != state) {
1550 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Current state is not Ready : pid(%d)", pid);
1551 return VCD_ERROR_INVALID_STATE;
1554 vcd_client_set_slience_detection(stop_by_silence);
1556 /* Notify show tooltip */
1557 ecore_timer_add(0, __vcd_request_show_tooltip, (void*)true);
1562 int vcd_server_widget_stop(int pid)
1564 /* check if pid is valid */
1565 int fore_pid = vcd_client_widget_get_foreground_pid();
1566 if (pid != fore_pid) {
1567 SLOG(LOG_ERROR, TAG_VCD, "[Server] pid is NOT foreground");
1568 return VCD_ERROR_INVALID_PARAMETER;
1572 /* Check current state */
1573 vcd_state_e state = vcd_config_get_service_state();
1575 /* Service state should be recording */
1576 if (VCD_STATE_RECORDING != state) {
1577 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Current service state is not Recording : pid(%d)", pid);
1578 return VCD_ERROR_INVALID_STATE;
1581 ret = vcd_server_mgr_stop();
1583 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Fail to start recognition");
1584 return VCD_ERROR_OPERATION_FAILED;
1587 return VCD_ERROR_NONE;
1590 int vcd_server_widget_cancel(int pid)
1592 /* check if pid is valid */
1593 int fore_pid = vcd_client_widget_get_foreground_pid();
1594 if (pid != fore_pid) {
1595 SLOG(LOG_ERROR, TAG_VCD, "[Server] pid is NOT foreground");
1596 return VCD_ERROR_INVALID_PARAMETER;
1600 /* Check current state */
1601 vcd_state_e state = vcd_config_get_service_state();
1603 /* Service state should be recording or processing */
1604 if (VCD_STATE_RECORDING != state && VCD_STATE_PROCESSING != state) {
1605 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Current state is not Recording or Processing : pid(%d)", pid);
1606 return VCD_ERROR_INVALID_STATE;
1609 ret = vcd_server_mgr_cancel();
1611 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Fail to cancel recognition : %d", ret);
1615 return VCD_ERROR_NONE;