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;
38 * VC Server Internal Functions
40 static Eina_Bool __stop_by_silence(void *data)
42 SLOG(LOG_DEBUG, TAG_VCD, "===== Silence Detected ");
44 vcd_server_mgr_stop();
46 SLOG(LOG_DEBUG, TAG_VCD, "=====");
47 SLOG(LOG_DEBUG, TAG_VCD, " ");
51 static Eina_Bool __cancel_by_interrupt(void *data)
53 SLOG(LOG_DEBUG, TAG_VCD, "===== Cancel by interrupt");
55 vcd_server_mgr_cancel();
57 SLOG(LOG_DEBUG, TAG_VCD, "=====");
58 SLOG(LOG_DEBUG, TAG_VCD, " ");
62 static int __server_recorder_callback(const void* data, const unsigned int length)
64 vcd_state_e state = vcd_config_get_service_state();
65 if (VCD_STATE_RECORDING != state) {
69 vcp_speech_detect_e speech_detected = VCP_SPEECH_DETECT_NONE;
72 ret = vcd_engine_recognize_audio(data, length, &speech_detected);
76 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to set recording data to engine(%d)", ret);
77 ecore_timer_add(0, __cancel_by_interrupt, NULL);
81 if (VCP_SPEECH_DETECT_BEGIN == speech_detected) {
82 if (-1 != vcd_client_manager_get_pid()) {
83 /* Manager client is available */
84 if (0 != vcdc_send_speech_detected(vcd_client_manager_get_pid())) {
85 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Fail to send speech detected");
88 } else if (VCP_SPEECH_DETECT_END == speech_detected && vcd_client_get_slience_detection()) {
89 /* silence detected */
90 ecore_timer_add(0, __stop_by_silence, NULL);
96 void __server_recorder_interrupt_callback()
98 SLOG(LOG_DEBUG, TAG_VCD, "===== Cancel by sound interrupt");
100 ecore_timer_add(0, __cancel_by_interrupt, NULL);
102 SLOG(LOG_DEBUG, TAG_VCD, "=====");
103 SLOG(LOG_DEBUG, TAG_VCD, " ");
106 static void __config_lang_changed_cb(const char* current_lang, void* user_data)
108 SLOG(LOG_DEBUG, TAG_VCD, "===== Change language ");
110 /* Current state is recording */
111 vcd_state_e state = vcd_config_get_service_state();
112 if (VCD_STATE_RECORDING == state || VCD_STATE_PROCESSING == state) {
113 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Current state is 'Recording'. Cancel recognition");
114 vcd_server_mgr_cancel();
118 ret = vcd_engine_set_current_language(current_lang);
120 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to set language of engine : %d", ret);
123 SLOG(LOG_DEBUG, TAG_VCD, "=====");
124 SLOG(LOG_DEBUG, TAG_VCD, " ");
129 static void __config_foreground_changed_cb(int previous, int current, void* user_data)
131 SLOG(LOG_DEBUG, TAG_VCD, "===== Change foreground");
133 SLOG(LOG_DEBUG, TAG_VCD, "Foreground pid(%d)", current);
135 if (VC_NO_FOREGROUND_PID != current) {
136 /* Foreground app is changed */
137 vcd_state_e state = vcd_config_get_service_state();
138 if (VCD_STATE_RECORDING == state) {
139 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Foreground pid(%d) is changed. Cancel recognition", current);
140 ecore_timer_add(0, __cancel_by_interrupt, NULL);
144 SLOG(LOG_DEBUG, TAG_VCD, "=====");
145 SLOG(LOG_DEBUG, TAG_VCD, " ");
150 static Eina_Bool __vcd_send_selected_result(void *data)
152 GSList* pid_list = NULL;
153 if (0 != vc_info_parser_get_result_pid_list(&pid_list)) {
154 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Fail to get pid list. No result");
156 if (0 < g_slist_length(pid_list)) {
158 vc_cmd_s* temp_cmd = NULL;
162 iter = g_slist_nth(pid_list, 0);
163 while (NULL != iter) {
164 temp_cmd = iter->data;
166 if (NULL != temp_cmd) {
169 /* send result noti */
170 ret = vcdc_send_result(temp_cmd->pid, temp_cmd->type);
172 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Fail to send result");
173 if (VCD_ERROR_TIMED_OUT != ret) {
177 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Send result : pid(%d) type(%d)", temp_cmd->pid, temp_cmd->type);
181 if (100 == count) break;
182 /* While is retry code */
187 pid_list = g_slist_remove_link(pid_list, iter);
188 iter = g_slist_nth(pid_list, 0);
193 vcd_config_set_service_state(VCD_STATE_READY);
198 static void __vcd_server_result_cb(vcp_result_event_e event, int* result_id, int count,
199 const char* all_result, const char* non_fixed_result, const char* msg, void *user_data)
201 if (VCD_STATE_PROCESSING != vcd_config_get_service_state()) {
202 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Current state is not 'Processing'");
206 vc_info_parser_unset_result(vcd_client_manager_get_exclusive());
208 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Event(%d), Text(%s) Nonfixed(%s) Msg(%s) Result count(%d)", event, all_result, non_fixed_result, msg, count);
211 if (NULL == result_id) {
213 if (NULL != all_result) {
214 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Engine result is no command : %s", all_result);
215 bool temp = vcd_client_manager_get_exclusive();
216 vc_info_parser_set_result(all_result, event, msg, NULL, temp);
219 int pid = vcd_client_widget_get_foreground_pid();
221 if (NULL != all_result) {
222 /* Send result text to widget */
223 vcdc_send_result(pid, VC_COMMAND_TYPE_WIDGET);
226 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Request tooltip hide");
227 /* Send to hide tooltip */
228 vcdc_send_show_tooltip(pid, false);
231 if (-1 != vcd_client_manager_get_pid()) {
232 /* Manager client is available */
233 if (0 != vcdc_send_result_to_manager(vcd_client_manager_get_pid())) {
234 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Fail to send result");
238 vcd_client_manager_set_exclusive(false);
244 SLOG(LOG_DEBUG, TAG_VCD, "[Server] === Get engine result ===");
247 vc_cmd_s* temp_cmd = NULL;
248 vc_cmd_list_h vc_cmd_list = NULL;
250 if (0 != vc_cmd_list_create(&vc_cmd_list)) {
251 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Fail to create command list");
252 vcd_client_manager_set_exclusive(false);
253 vcd_config_set_service_state(VCD_STATE_READY);
258 for (i = 0; i < count; i++) {
259 SLOG(LOG_DEBUG, TAG_VCD, "[Server] [%d] Result ID(%d)", i, result_id[i]);
261 if (result_id[i] < 0) {
262 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Result ID(%d) is NOT valid", result_id[i]);
266 ret = vcd_client_get_cmd_from_result_id(result_id[i], &temp_cmd);
267 if (0 == ret && NULL != temp_cmd) {
268 switch (temp_cmd->format) {
269 case VC_CMD_FORMAT_FIXED:
270 /* Nonfixed result is NOT valid */
272 case VC_CMD_FORMAT_FIXED_AND_EXTRA:
273 if (NULL == temp_cmd->parameter) {
274 if (NULL != non_fixed_result) {
275 temp_cmd->parameter = strdup(non_fixed_result);
278 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Command type is NOT vaild. Parameter (%s)", temp_cmd->parameter);
281 case VC_CMD_FORMAT_EXTRA_AND_FIXED:
282 if (NULL == temp_cmd->command) {
283 if (NULL != non_fixed_result) {
284 temp_cmd->command = strdup(non_fixed_result);
287 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Command type is NOT vaild. Command (%s)", temp_cmd->command);
292 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Unknown command type : %d", temp_cmd->type);
296 if (0 != vc_cmd_list_add(vc_cmd_list, (vc_cmd_h)temp_cmd)) {
297 SLOG(LOG_DEBUG, TAG_VCD, "Fail to add command to list");
298 vc_cmd_destroy((vc_cmd_h)temp_cmd);
301 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] NOT found matached result(%d)", result_id[i]);
305 vc_cmd_print_list(vc_cmd_list);
307 SLOG(LOG_DEBUG, TAG_VCD, "[Server] =========================");
309 int result_count = 0;
310 vc_cmd_list_get_count(vc_cmd_list, &result_count);
312 if (false == vcd_client_manager_get_exclusive()) {
313 int pid = vcd_client_widget_get_foreground_pid();
315 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Request tooltip hide");
316 vcdc_send_show_tooltip(pid, false);
319 vc_info_parser_set_result(all_result, event, msg, vc_cmd_list, false);
321 if (-1 != vcd_client_manager_get_pid()) {
322 /* Manager client is available */
323 if (0 != vcdc_send_result_to_manager(vcd_client_manager_get_pid())) {
324 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Fail to send result");
327 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Manager is NOT available. Send result to client directly");
328 /* Send result to client */
329 ecore_timer_add(0, __vcd_send_selected_result, NULL);
332 /* exclusive command */
333 vc_info_parser_set_result(all_result, event, msg, vc_cmd_list, true);
335 if (-1 != vcd_client_manager_get_pid()) {
336 /* Manager client is available */
337 if (0 != vcdc_send_result_to_manager(vcd_client_manager_get_pid())) {
338 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Fail to send result");
341 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Manager is NOT available");
344 vcd_client_manager_set_exclusive(false);
347 vc_cmd_list_destroy(vc_cmd_list, true);
353 * vcd server Interfaces
355 static void __vcd_file_clean_up()
357 SLOG(LOG_DEBUG, TAG_VCD, "== Old file clean up == ");
362 struct dirent *dirp = NULL;
364 dp = opendir(VC_RUNTIME_INFO_ROOT);
366 SLOG(LOG_ERROR, TAG_VCD, "[File message WARN] Fail to open path : %s", VC_RUNTIME_INFO_ROOT);
370 char remove_path[256] = {0, };
372 ret = readdir_r(dp, &entry, &dirp);
374 SLOG(LOG_ERROR, TAG_VCD, "[File ERROR] Fail to read directory");
379 if (!strncmp("vc_", dirp->d_name, strlen("vc_"))) {
380 memset(remove_path, 0, 256);
381 snprintf(remove_path, 256, "%s/%s", VC_RUNTIME_INFO_ROOT, dirp->d_name);
384 if (0 != remove(remove_path)) {
385 SLOG(LOG_WARN, TAG_VCD, "[File message WARN] Fail to remove file : %s", remove_path);
387 SLOG(LOG_DEBUG, TAG_VCD, "[File message] Remove file : %s", remove_path);
391 } while (NULL != dirp);
402 /* Remove old file */
403 __vcd_file_clean_up();
405 /* initialize modules */
406 ret = vcd_config_initialize(__config_lang_changed_cb, __config_foreground_changed_cb, NULL);
408 SLOG(LOG_ERROR, TAG_VCD, "[Server WARNING] Fail to initialize config.");
411 vcd_config_set_service_state(VCD_STATE_NONE);
413 ret = vcd_engine_agent_init(__vcd_server_result_cb);
415 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to engine agent initialize : result(%d)", ret);
419 if (0 != vcd_recorder_create(__server_recorder_callback, __server_recorder_interrupt_callback)) {
420 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to create recorder");
421 return VCD_ERROR_OPERATION_FAILED;
425 ret = vcd_engine_agent_initialize_current_engine();
427 if (VCD_ERROR_ENGINE_NOT_FOUND == ret)
428 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] There is No Voice control engine");
430 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to init engine");
438 if (0 != vcd_engine_agent_load_current_engine()) {
439 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to load current engine");
440 return VCD_ERROR_OPERATION_FAILED;
443 /* Initialize manager info */
444 vcd_client_manager_unset();
446 vcd_config_set_service_state(VCD_STATE_READY);
448 SLOG(LOG_DEBUG, TAG_VCD, "[Server SUCCESS] initialize");
455 vcd_state_e state = vcd_config_get_service_state();
456 if (VCD_STATE_READY != state) {
457 if (VCD_STATE_RECORDING == state) {
460 vcd_engine_recognize_cancel();
462 if (0 != vcd_recorder_destroy()) {
463 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to destroy recorder");
465 SLOG(LOG_DEBUG, TAG_VCD, "[Server] destroy recorder");
468 if (0 != vcd_engine_agent_release()) {
469 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to release engine");
471 SLOG(LOG_DEBUG, TAG_VCD, "[Server] release engine");
474 vcd_config_set_service_state(VCD_STATE_NONE);
476 SLOG(LOG_DEBUG, TAG_VCD, "[Server] mode finalize");
481 static Eina_Bool __finalize_quit_ecore_loop(void *data)
483 SLOG(LOG_DEBUG, TAG_VCD, "[Server] quit ecore main loop");
484 ecore_main_loop_quit();
488 Eina_Bool vcd_cleanup_client(void *data)
490 int* client_list = NULL;
491 int client_count = 0;
495 if (0 == vcd_client_get_list(&client_list, &client_count)) {
496 SLOG(LOG_DEBUG, TAG_VCD, "===== Clean up client ");
497 if (NULL != client_list && client_count > 0) {
498 for (i = 0; i < client_count; i++) {
499 result = vcdc_send_hello(client_list[i], VCD_CLIENT_TYPE_NORMAL);
502 SLOG(LOG_DEBUG, TAG_VCD, "[Server] pid(%d) should be removed.", client_list[i]);
503 vcd_server_finalize(client_list[i]);
504 } else if (-1 == result) {
505 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Hello result has error");
509 SLOG(LOG_DEBUG, TAG_VCD, "=====");
510 SLOG(LOG_DEBUG, TAG_VCD, " ");
512 if (NULL != client_list) {
517 if (0 == vcd_client_widget_get_list(&client_list, &client_count)) {
518 SLOG(LOG_DEBUG, TAG_VCD, "===== Clean up widget");
519 if (NULL != client_list && client_count > 0) {
520 for (i = 0; i < client_count; i++) {
521 result = vcdc_send_hello(client_list[i], VCD_CLIENT_TYPE_WIDGET);
524 SLOG(LOG_DEBUG, TAG_VCD, "[Server] widget pid(%d) should be removed.", client_list[i]);
525 vcd_server_widget_finalize(client_list[i]);
526 } else if (-1 == result) {
527 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Hello result has error");
531 SLOG(LOG_DEBUG, TAG_VCD, "=====");
532 SLOG(LOG_DEBUG, TAG_VCD, " ");
535 if (NULL != client_list) {
548 int vcd_server_mgr_initialize(int pid)
550 if (false == g_is_engine) {
551 if (0 != vcd_engine_agent_initialize_current_engine()) {
552 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] No Engine");
554 return VCD_ERROR_ENGINE_NOT_FOUND;
560 /* check if pid is valid */
561 if (false == vcd_client_manager_is_valid(pid)) {
562 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] The pid(%d) is already exist", pid);
563 return VCD_ERROR_INVALID_PARAMETER;
566 /* Add client information to client manager */
567 if (0 != vcd_client_manager_set(pid)) {
568 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to add manager");
569 return VCD_ERROR_OPERATION_FAILED;
572 SLOG(LOG_DEBUG, TAG_VCD, "[Server Success] Manager initialize : pid(%d)", pid);
574 return VCD_ERROR_NONE;
577 int vcd_server_mgr_finalize(int pid)
579 /* check if pid is valid */
580 if (false == vcd_client_manager_is_valid(pid)) {
581 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] The manager pid(%d) is NOT valid", pid);
582 return VCD_ERROR_INVALID_PARAMETER;
585 /* Remove manager information */
586 if (0 != vcd_client_manager_unset()) {
587 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Fail to delete client");
590 if (0 == vcd_client_get_ref_count()) {
591 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Connected client list is empty");
592 ecore_timer_add(0, __finalize_quit_ecore_loop, NULL);
595 SLOG(LOG_DEBUG, TAG_VCD, "[Server Success] Manager Finalize : pid(%d)", pid);
597 return VCD_ERROR_NONE;
600 int vcd_server_mgr_set_command(int pid)
602 if (0 != vcd_client_manager_set_command(pid)) {
603 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] The manager pid(%d) is NOT valid", pid);
604 return VCD_ERROR_INVALID_PARAMETER;
606 return VCD_ERROR_NONE;
609 int vcd_server_mgr_unset_command(int pid)
611 if (0 != vcd_client_manager_unset_command(pid)) {
612 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] The manager pid(%d) is NOT valid", pid);
613 return VCD_ERROR_INVALID_PARAMETER;
615 return VCD_ERROR_NONE;
618 int vcd_server_mgr_set_demandable_client(int pid)
620 /* check if pid is valid */
621 if (false == vcd_client_manager_is_valid(pid)) {
622 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] The manager pid(%d) is NOT valid", pid);
623 return VCD_ERROR_INVALID_PARAMETER;
626 GSList* client_list = NULL;
627 if (0 != vc_info_parser_get_demandable_clients(&client_list)) {
628 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to get demandable client");
629 return VCD_ERROR_OPERATION_FAILED;
632 /* Save client list */
633 if (0 != vcd_client_manager_set_demandable_client(pid, client_list)) {
634 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to set demandable client list");
635 return VCD_ERROR_OPERATION_FAILED;
638 return VCD_ERROR_NONE;
641 int vcd_server_mgr_set_audio_type(int pid, const char* audio_type)
643 /* check if pid is valid */
644 if (false == vcd_client_manager_is_valid(pid)) {
645 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] The manager pid(%d) is NOT valid", pid);
646 return VCD_ERROR_INVALID_PARAMETER;
650 vcp_audio_type_e type = VCP_AUDIO_TYPE_PCM_S16_LE;
654 ret = vcd_engine_get_audio_format(audio_type, &type, &rate, &channel);
656 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to get audio format : %d", ret);
657 return VCD_ERROR_OPERATION_FAILED;
660 ret = vcd_recorder_set(audio_type, type, rate, channel);
662 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to set audio in type : %d", ret);
663 return VCD_ERROR_OPERATION_FAILED;
666 return VCD_ERROR_NONE;
669 int vcd_server_mgr_get_audio_type(int pid, char** audio_type)
671 /* check if pid is valid */
672 if (false == vcd_client_manager_is_valid(pid)) {
673 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] The manager pid(%d) is NOT valid", pid);
674 return VCD_ERROR_INVALID_PARAMETER;
677 int ret = vcd_recorder_get(audio_type);
679 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to get audio in type : %d", ret);
680 return VCD_ERROR_OPERATION_FAILED;
683 return VCD_ERROR_NONE;
686 int vcd_server_mgr_set_client_info(int pid)
688 /* check if pid is valid */
689 if (false == vcd_client_manager_is_valid(pid)) {
690 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] The manager pid(%d) is NOT valid", pid);
691 return VCD_ERROR_INVALID_PARAMETER;
694 int ret = vcd_client_save_client_info();
696 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to save client info : %d", ret);
697 return VCD_ERROR_OPERATION_FAILED;
700 return VCD_ERROR_NONE;
703 static int __start_internal_recognition()
707 /* 2. Get commands */
708 ret = vcd_client_command_collect_command();
710 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to collect command : %d", ret);
711 return VCD_ERROR_OPERATION_FAILED;
714 ret = vcd_client_get_length();
716 SLOG(LOG_WARN, TAG_VCD, "[Server WARNIING] No current command : %d", ret);
717 return VCD_ERROR_OPERATION_FAILED;
720 /* 3. Set command to engine */
721 ret = vcd_engine_set_commands();
723 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to collect command : %d", ret);
724 return VCD_ERROR_OPERATION_FAILED;
727 SLOG(LOG_DEBUG, TAG_VCD, "[Server Success] Set command");
729 /* 4. start recognition */
730 ret = vcd_engine_recognize_start(true);
732 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to start recognition : result(%d)", ret);
733 return VCD_ERROR_OPERATION_FAILED;
736 SLOG(LOG_DEBUG, TAG_VCD, "[Server Success] Start engine");
738 /* 5. recorder start */
739 ret = vcd_recorder_start();
741 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to start recorder : result(%d)", ret);
742 vcd_engine_recognize_cancel();
746 vcd_config_set_service_state(VCD_STATE_RECORDING);
748 SLOG(LOG_DEBUG, TAG_VCD, "[Server Success] Start recognition");
753 static Eina_Bool __vcd_request_show_tooltip(void *data)
755 int pid = vcd_client_widget_get_foreground_pid();
757 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Request tooltip show and widget command");
758 vcdc_send_show_tooltip(pid, (bool)data);
764 int vcd_server_mgr_start(bool stop_by_silence, bool exclusive_cmd, bool start_by_client)
766 /* 1. check current state */
767 vcd_state_e state = vcd_config_get_service_state();
769 if (VCD_STATE_READY != state) {
770 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Current state is not ready");
771 return VCD_ERROR_INVALID_STATE;
774 vcd_client_set_slience_detection(stop_by_silence);
776 if (false == exclusive_cmd) {
777 /* Notify show tooltip */
778 int pid = vcd_client_widget_get_foreground_pid();
780 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Request tooltip show and widget command");
781 ecore_timer_add(0, __vcd_request_show_tooltip, (void*)true);
785 vcd_client_manager_set_exclusive(exclusive_cmd);
789 if (true == start_by_client) {
790 /* Get foreground pid */
791 if (0 != vcd_config_get_foreground(&fg_pid)) {
792 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to get foreground");
795 /* Set client exclusive option */
796 if (0 != vcd_client_set_exclusive_command(fg_pid)) {
797 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to set exclusive command");
801 int ret = __start_internal_recognition();
803 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to start recongition : %d", ret);
807 if (true == start_by_client) {
808 vcd_client_unset_exclusive_command(fg_pid);
811 return VCD_ERROR_NONE;
814 int vcd_server_mgr_stop()
816 /* 1. Check current state is recording */
817 if (VCD_STATE_RECORDING != vcd_config_get_service_state()) {
818 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Current state is not recording");
819 return VCD_ERROR_INVALID_STATE;
822 /* 2. Stop recorder */
825 /* 3. Stop engine recognition */
826 int ret = vcd_engine_recognize_stop();
828 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to stop recognition : %d", ret);
831 /* 4. Set original mode */
832 vcd_config_set_service_state(VCD_STATE_PROCESSING);
834 return VCD_ERROR_NONE;
837 int vcd_server_mgr_cancel()
839 /* 1. Check current state */
840 vcd_state_e state = vcd_config_get_service_state();
841 if (VCD_STATE_RECORDING != state && VCD_STATE_PROCESSING != state) {
842 SLOG(LOG_WARN, TAG_VCD, "[Server ERROR] Current state is not recording or processing");
843 return VCD_ERROR_INVALID_STATE;
846 /* 2. Stop recorder */
848 /* 3. Cancel engine */
849 int ret = vcd_engine_recognize_cancel();
851 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Fail to cancel : %d", ret);
854 if (false == vcd_client_manager_get_exclusive()) {
855 int pid = vcd_client_widget_get_foreground_pid();
857 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Request tooltip hide");
858 ecore_timer_add(0, __vcd_request_show_tooltip, (void*)false);
861 vcd_client_manager_set_exclusive(false);
865 vcd_config_set_service_state(VCD_STATE_READY);
867 return VCD_ERROR_NONE;
871 int vcd_server_mgr_result_select()
873 ecore_timer_add(0.01, __vcd_send_selected_result, NULL);
875 return VCD_ERROR_NONE;
879 * VC Server Functions for Client
881 int vcd_server_initialize(int pid)
883 if (false == g_is_engine) {
884 if (0 != vcd_engine_agent_initialize_current_engine()) {
885 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] No Engine");
887 return VCD_ERROR_ENGINE_NOT_FOUND;
893 if (false == vcd_engine_is_available_engine()) {
894 if (0 != vcd_engine_agent_initialize_current_engine()) {
895 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] No Engine");
896 return VCD_ERROR_ENGINE_NOT_FOUND;
900 /* check if pid is valid */
901 if (true == vcd_client_is_available(pid)) {
902 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] The pid is already exist");
903 return VCD_ERROR_INVALID_PARAMETER;
906 /* Add client information to client manager */
907 if (0 != vcd_client_add(pid)) {
908 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to add client info");
909 return VCD_ERROR_OPERATION_FAILED;
912 SLOG(LOG_DEBUG, TAG_VCD, "[Server Success] Client Initialize : pid(%d)", pid);
914 return VCD_ERROR_NONE;
917 int vcd_server_finalize(int pid)
919 /* check if pid is valid */
920 if (false == vcd_client_is_available(pid)) {
921 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] pid is NOT valid ");
922 return VCD_ERROR_INVALID_PARAMETER;
925 /* Remove client information */
926 if (0 != vcd_client_delete(pid)) {
927 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Fail to delete client");
930 if (0 == vcd_client_get_ref_count()) {
931 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Connected client list is empty");
932 ecore_timer_add(0, __finalize_quit_ecore_loop, NULL);
935 SLOG(LOG_DEBUG, TAG_VCD, "[Server Success] Client Finalize : pid(%d)", pid);
937 return VCD_ERROR_NONE;
940 int vcd_server_set_command(int pid, vc_cmd_type_e cmd_type)
942 /* check if pid is valid */
943 if (false == vcd_client_is_available(pid)) {
944 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] pid is NOT valid ");
945 return VCD_ERROR_INVALID_PARAMETER;
948 if (0 != vcd_client_set_command_type(pid, cmd_type)) {
949 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to set command type : pid(%d), cmd_type(%d)", pid, cmd_type);
950 return VCD_ERROR_OPERATION_FAILED;
956 int vcd_server_unset_command(int pid, vc_cmd_type_e cmd_type)
958 /* check if pid is valid */
959 if (false == vcd_client_is_available(pid)) {
960 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] pid is NOT valid ");
961 return VCD_ERROR_INVALID_PARAMETER;
964 if (0 != vcd_client_unset_command_type(pid, cmd_type)) {
965 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to unset command type : pid(%d), cmd_type(%d)", pid, cmd_type);
966 return VCD_ERROR_OPERATION_FAILED;
973 int vcd_server_set_exclusive_command(int pid, bool value)
975 /* check if pid is valid */
976 if (false == vcd_client_is_available(pid)) {
977 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] pid is NOT valid ");
978 return VCD_ERROR_INVALID_PARAMETER;
982 if (0 != vcd_client_set_exclusive_command(pid)) {
983 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to set exclusive command : pid(%d)", pid);
984 return VCD_ERROR_OPERATION_FAILED;
987 if (0 != vcd_client_unset_exclusive_command(pid)) {
988 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to unset exclusive command : pid(%d)", pid);
989 return VCD_ERROR_OPERATION_FAILED;
996 int vcd_server_request_start(int pid, bool stop_by_silence)
998 /* check if pid is valid */
999 if (false == vcd_client_is_available(pid)) {
1000 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] pid(%d) is NOT forground client", pid);
1001 return VCD_ERROR_INVALID_PARAMETER;
1005 /* Check current state */
1006 vcd_state_e state = vcd_config_get_service_state();
1008 /* Service state should be ready */
1009 if (VCD_STATE_READY != state) {
1010 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Current state is not Ready : pid(%d)", pid);
1011 return VCD_ERROR_INVALID_STATE;
1014 if (-1 != vcd_client_manager_get_pid()) {
1015 /* Check current pid is valid */
1016 if (false == vcd_client_manager_check_demandable_client(pid)) {
1017 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Current client is NOT available : pid(%d)", pid);
1018 return VCD_ERROR_INVALID_PARAMETER;
1022 ret = vcd_server_mgr_start(stop_by_silence, false);
1024 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Fail to start recognition");
1025 return VCD_ERROR_INVALID_PARAMETER;
1031 int vcd_server_request_stop(int pid)
1034 /* Check current state */
1035 vcd_state_e state = vcd_config_get_service_state();
1037 /* Service state should be ready */
1038 if (VCD_STATE_RECORDING != state) {
1039 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Current state is not Recording : pid(%d)", pid);
1040 return VCD_ERROR_INVALID_STATE;
1043 if (-1 != vcd_client_manager_get_pid()) {
1044 /* Check current pid is valid */
1045 if (false == vcd_client_manager_check_demandable_client(pid)) {
1046 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Current client is NOT available : pid(%d)", pid);
1047 return VCD_ERROR_INVALID_PARAMETER;
1051 ret = vcd_server_mgr_stop();
1053 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Fail to start recognition");
1054 return VCD_ERROR_OPERATION_FAILED;
1057 return VCD_ERROR_NONE;
1060 int vcd_server_request_cancel(int pid)
1063 /* Check current state */
1064 vcd_state_e state = vcd_config_get_service_state();
1066 /* Service state should be recording or processing */
1067 if (VCD_STATE_RECORDING != state && VCD_STATE_PROCESSING != state) {
1068 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Current state is not Recording or Processing : pid(%d)", pid);
1069 return VCD_ERROR_INVALID_STATE;
1072 if (-1 != vcd_client_manager_get_pid()) {
1073 /* Check current pid is valid */
1074 if (false == vcd_client_manager_check_demandable_client(pid)) {
1075 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Current client is NOT available : pid(%d)", pid);
1076 return VCD_ERROR_INVALID_PARAMETER;
1080 ret = vcd_server_mgr_cancel();
1082 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Fail to start recognition");
1083 return VCD_ERROR_OPERATION_FAILED;
1086 return VCD_ERROR_NONE;
1091 * VC Server Functions for Widget lib
1093 int vcd_server_widget_initialize(int pid)
1095 if (false == g_is_engine) {
1096 if (0 != vcd_engine_agent_initialize_current_engine()) {
1097 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] No Engine");
1098 g_is_engine = false;
1099 return VCD_ERROR_ENGINE_NOT_FOUND;
1105 if (false == vcd_engine_is_available_engine()) {
1106 if (0 != vcd_engine_agent_initialize_current_engine()) {
1107 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] No Engine");
1108 return VCD_ERROR_ENGINE_NOT_FOUND;
1112 /* check if pid is valid */
1113 if (true == vcd_client_widget_is_available(pid)) {
1114 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] The pid is already exist");
1115 return VCD_ERROR_INVALID_PARAMETER;
1118 /* Add client information to client manager */
1119 if (0 != vcd_client_widget_add(pid)) {
1120 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to add client info");
1121 return VCD_ERROR_OPERATION_FAILED;
1124 SLOG(LOG_DEBUG, TAG_VCD, "[Server Success] Initialize widget : pid(%d)", pid);
1126 return VCD_ERROR_NONE;
1129 int vcd_server_widget_finalize(int pid)
1131 /* check if pid is valid */
1132 if (false == vcd_client_widget_is_available(pid)) {
1133 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] pid is NOT valid ");
1134 return VCD_ERROR_INVALID_PARAMETER;
1137 /* Remove client information */
1138 if (0 != vcd_client_widget_delete(pid)) {
1139 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Fail to delete client");
1142 if (0 == vcd_client_get_ref_count()) {
1143 SLOG(LOG_DEBUG, TAG_VCD, "[Server] connected client list is empty");
1144 ecore_timer_add(0, __finalize_quit_ecore_loop, NULL);
1147 return VCD_ERROR_NONE;
1150 int vcd_server_widget_start_recording(int pid, bool widget_command)
1152 /* check if pid is valid */
1153 if (false == vcd_client_widget_is_available(pid)) {
1154 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] pid is NOT valid ");
1155 return VCD_ERROR_INVALID_PARAMETER;
1158 if (true == widget_command) {
1159 vcd_client_widget_set_command(pid);
1160 SLOG(LOG_DEBUG, TAG_VCD, "[Server] widget command is available");
1162 vcd_client_widget_unset_command(pid);
1163 SLOG(LOG_DEBUG, TAG_VCD, "[Server] widget command is NOT available");
1166 int ret = __start_internal_recognition();
1168 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to start recongition : %d", ret);
1169 ecore_timer_add(0, __vcd_request_show_tooltip, (void*)false);
1175 int vcd_server_widget_start(int pid, bool stop_by_silence)
1177 /* check if pid is valid */
1178 int fore_pid = vcd_client_widget_get_foreground_pid();
1179 if (pid != fore_pid) {
1180 SLOG(LOG_ERROR, TAG_VCD, "[Server] pid is NOT foreground");
1181 return VCD_ERROR_INVALID_PARAMETER;
1184 /* Check current state */
1185 vcd_state_e state = vcd_config_get_service_state();
1187 /* Service state should be ready */
1188 if (VCD_STATE_READY != state) {
1189 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Current state is not Ready : pid(%d)", pid);
1190 return VCD_ERROR_INVALID_STATE;
1193 vcd_client_set_slience_detection(stop_by_silence);
1195 /* Notify show tooltip */
1196 ecore_timer_add(0, __vcd_request_show_tooltip, (void*)true);
1201 int vcd_server_widget_stop(int pid)
1203 /* check if pid is valid */
1204 int fore_pid = vcd_client_widget_get_foreground_pid();
1205 if (pid != fore_pid) {
1206 SLOG(LOG_ERROR, TAG_VCD, "[Server] pid is NOT foreground");
1207 return VCD_ERROR_INVALID_PARAMETER;
1211 /* Check current state */
1212 vcd_state_e state = vcd_config_get_service_state();
1214 /* Service state should be recording */
1215 if (VCD_STATE_RECORDING != state) {
1216 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Current service state is not Recording : pid(%d)", pid);
1217 return VCD_ERROR_INVALID_STATE;
1220 ret = vcd_server_mgr_stop();
1222 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Fail to start recognition");
1223 return VCD_ERROR_OPERATION_FAILED;
1226 return VCD_ERROR_NONE;
1229 int vcd_server_widget_cancel(int pid)
1231 /* check if pid is valid */
1232 int fore_pid = vcd_client_widget_get_foreground_pid();
1233 if (pid != fore_pid) {
1234 SLOG(LOG_ERROR, TAG_VCD, "[Server] pid is NOT foreground");
1235 return VCD_ERROR_INVALID_PARAMETER;
1239 /* Check current state */
1240 vcd_state_e state = vcd_config_get_service_state();
1242 /* Service state should be recording or processing */
1243 if (VCD_STATE_RECORDING != state && VCD_STATE_PROCESSING != state) {
1244 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Current state is not Recording or Processing : pid(%d)", pid);
1245 return VCD_ERROR_INVALID_STATE;
1248 ret = vcd_server_mgr_cancel();
1250 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Fail to start recognition");
1251 return VCD_ERROR_OPERATION_FAILED;
1254 return VCD_ERROR_NONE;