Apply sound manager to fix recorder issue
[platform/core/uifw/voice-control.git] / server / vcd_server.c
1 /*
2 * Copyright (c) 2011-2015 Samsung Electronics Co., Ltd All Rights Reserved
3 *
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
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
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.
15 */
16
17 #include <app_control.h>
18 #include <app_manager.h>
19 #include <dirent.h>
20 #include <sound_manager.h>
21
22 #include "vc_cmd_db.h"
23 #include "vc_info_parser.h"
24 #include "vcd_main.h"
25 #include "vcd_server.h"
26 #include "vcd_client_data.h"
27
28 #include "vcd_engine_agent.h"
29 #include "vcd_config.h"
30 #include "vcd_recorder.h"
31 #include "vcd_dbus.h"
32
33 #include "voice_control_command_expand.h"
34 #include "voice_control_common.h"
35
36 /*
37 * VC Server static variable
38 */
39 static bool     g_is_engine;
40
41 static GList *g_proc_list = NULL;
42
43 static Ecore_Timer *g_restart_timer = NULL;
44
45 /**
46 * @brief Enumerations of send event type.
47 */
48 typedef enum {
49         VCD_SEND_EVENT_TYPE_TEXT,               /**< send text event to vc engine*/
50         VCD_SEND_EVENT_TYPE_LIST_EVENT,         /**< send list event to vc engine */
51         VCD_SEND_EVENT_TYPE_HAPTIC_EVENT        /**< send haptic event to vc engine */
52 } vcd_send_event_type_e;
53
54 static int __vcd_server_launch_manager_app();
55
56 /*
57 * VC Server Internal Functions
58 */
59 static Eina_Bool __stop_by_silence(void *data)
60 {
61         SLOG(LOG_DEBUG, TAG_VCD, "===== Silence Detected ");
62
63         vcd_server_mgr_stop();
64
65         SLOG(LOG_DEBUG, TAG_VCD, "=====");
66         SLOG(LOG_DEBUG, TAG_VCD, "  ");
67         return EINA_FALSE;
68 }
69
70 static Eina_Bool __cancel_by_interrupt(void *data)
71 {
72         SLOG(LOG_DEBUG, TAG_VCD, "===== Cancel by interrupt");
73
74         vcd_server_mgr_cancel();
75
76         SLOG(LOG_DEBUG, TAG_VCD, "=====");
77         SLOG(LOG_DEBUG, TAG_VCD, "  ");
78         return EINA_FALSE;
79 }
80
81 static void __cancel_by_error(void *data)
82 {
83         SLOG(LOG_DEBUG, TAG_VCD, "===== Cancel by error");
84
85         vcd_server_mgr_cancel();
86
87         SLOG(LOG_DEBUG, TAG_VCD, "=====");
88         SLOG(LOG_DEBUG, TAG_VCD, "  ");
89         return;
90 }
91
92 static Eina_Bool __restart_engine(void *data)
93 {
94         SLOG(LOG_DEBUG, TAG_VCD, "===== Restart by no result");
95
96         g_restart_timer = NULL;
97
98         /* Restart recognition */
99         int ret = vcd_engine_recognize_start(true);
100         if (0 != ret) {
101                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to restart recognition : result(%d)", ret);
102                 return EINA_FALSE;
103         }
104
105         SLOG(LOG_DEBUG, TAG_VCD, "[Server Success] Start engine");
106
107         if (VCD_RECOGNITION_MODE_RESTART_AFTER_REJECT == vcd_client_get_recognition_mode()) {
108                 vcd_config_set_service_state(VCD_STATE_RECORDING);
109                 vcdc_send_service_state(VCD_STATE_RECORDING);
110         }
111
112         SLOG(LOG_DEBUG, TAG_VCD, "[Server Success] Restart recognition");
113
114         SLOG(LOG_DEBUG, TAG_VCD, "=====");
115         SLOG(LOG_DEBUG, TAG_VCD, "  ");
116         return EINA_FALSE;
117 }
118
119 static int __server_recorder_callback(const void* data, const unsigned int length)
120 {
121         vcd_state_e state = vcd_config_get_service_state();
122         if (VCD_STATE_READY == state) {
123                 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Ready state, but recording");
124         } else if (VCD_STATE_PROCESSING == state) {
125                 return 0;
126         }
127
128         vcp_speech_detect_e speech_detected = VCP_SPEECH_DETECT_NONE;
129         int ret;
130
131         ret = vcd_engine_recognize_audio(data, length, &speech_detected);
132
133         if (0 > ret) {
134                 /* Error */
135                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to set recording data to engine(%d)", ret);
136                 ecore_timer_add(0, __cancel_by_interrupt, NULL);
137                 /* Send error cb to manager */
138                 if (VCP_ERROR_OUT_OF_NETWORK == ret) {
139                         vcdc_send_error_signal_to_manager(vcd_client_manager_get_pid(), VCD_ERROR_TIMED_OUT, "Engine connection failed");
140                 } else {
141                         vcdc_send_error_signal_to_manager(vcd_client_manager_get_pid(), VCD_ERROR_OPERATION_FAILED, "Engine recognition failed");
142                 }
143                 return 0;
144         }
145
146         if (VCP_SPEECH_DETECT_BEGIN == speech_detected) {
147                 if (-1 != vcd_client_manager_get_pid()) {
148                         /* Manager client is available */
149                         if (0 != vcdc_send_speech_detected(vcd_client_manager_get_pid())) {
150                                 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Fail to send speech detected");
151                         }
152                 }
153         } else if (VCP_SPEECH_DETECT_END == speech_detected) {
154                 if (VCD_RECOGNITION_MODE_STOP_BY_SILENCE == vcd_client_get_recognition_mode()) {
155                         /* silence detected */
156                         ecore_timer_add(0, __stop_by_silence, NULL);
157                 } else if (VCD_RECOGNITION_MODE_RESTART_AFTER_REJECT == vcd_client_get_recognition_mode()) {
158                         /* Stop engine recognition */
159                         int ret = vcd_engine_recognize_stop();
160                         if (0 != ret) {
161                                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to stop recognition : %d", ret);
162                         }
163                         vcd_config_set_service_state(VCD_STATE_PROCESSING);
164                         vcdc_send_service_state(VCD_STATE_PROCESSING);
165
166                         SLOG(LOG_DEBUG, TAG_VCD, "[Server] Stop engine only by silence");
167                 } else if (VCD_RECOGNITION_MODE_RESTART_CONTINUOUSLY == vcd_client_get_recognition_mode()) {
168                         /* Stop engine recognition */
169                         int ret = vcd_engine_recognize_stop();
170                         if (0 != ret) {
171                                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to stop recognition : %d", ret);
172                         }
173                 }
174         }
175
176         return 0;
177 }
178
179 void __server_recorder_interrupt_callback()
180 {
181         SLOG(LOG_DEBUG, TAG_VCD, "===== Cancel by sound interrupt");
182
183         ecore_timer_add(0, __cancel_by_interrupt, NULL);
184
185         SLOG(LOG_DEBUG, TAG_VCD, "=====");
186         SLOG(LOG_DEBUG, TAG_VCD, "  ");
187 }
188
189 static void __config_lang_changed_cb(const char* current_lang, void* user_data)
190 {
191         SLOG(LOG_DEBUG, TAG_VCD, "===== Change language ");
192
193         /* Current state is recording */
194         vcd_state_e state = vcd_config_get_service_state();
195         if (VCD_STATE_RECORDING == state || VCD_STATE_PROCESSING == state) {
196                 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Current state is 'Recording'. Cancel recognition");
197                 vcd_server_mgr_cancel();
198         }
199
200         int ret;
201         ret = vcd_engine_set_current_language(current_lang);
202         if (0 != ret) {
203                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to set language of engine : %d", ret);
204         }
205
206         SLOG(LOG_DEBUG, TAG_VCD, "=====");
207         SLOG(LOG_DEBUG, TAG_VCD, "  ");
208
209         return;
210 }
211
212 static void __config_foreground_changed_cb(int previous, int current, void* user_data)
213 {
214         SLOG(LOG_DEBUG, TAG_VCD, "===== Change foreground");
215
216         SLOG(LOG_DEBUG, TAG_VCD, "Foreground pid(%d)", current);
217
218         if (VC_NO_FOREGROUND_PID != current) {
219                 /* Foreground app is changed */
220                 vcd_state_e state = vcd_config_get_service_state();
221                 if (VCD_STATE_RECORDING == state) {
222                         SLOG(LOG_DEBUG, TAG_VCD, "[Server] Foreground pid(%d) is changed. Cancel recognition", current);
223                         ecore_timer_add(0, __cancel_by_interrupt, NULL);
224                 }
225         }
226
227         SLOG(LOG_DEBUG, TAG_VCD, "=====");
228         SLOG(LOG_DEBUG, TAG_VCD, "  ");
229
230         return;
231 }
232
233 static int __vcd_activate_app_by_appcontrol(const char* appid)
234 {
235         if (NULL == appid) {
236                 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Invalid parameter");
237                 return VCD_ERROR_INVALID_PARAMETER;
238         }
239
240         int ret = -1;
241         app_control_h app_control = NULL;
242         ret = app_control_create(&app_control);
243         if (APP_CONTROL_ERROR_NONE == ret) {
244                 // Set extra data
245                 ret = app_control_add_extra_data(app_control, "voice_launch", "get_result");
246                 if (APP_CONTROL_ERROR_NONE != ret) {
247                         SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Fail to add extra data, ret(%d)", ret);
248                         app_control_destroy(app_control);
249                         return VCD_ERROR_OPERATION_FAILED;
250                 }
251                 // Set an app ID.
252                 ret = app_control_set_app_id(app_control, appid);
253                 if (APP_CONTROL_ERROR_NONE != ret) {
254                         SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Fail to set app id, ret(%d)", ret);
255                         app_control_destroy(app_control);
256                         return VCD_ERROR_OPERATION_FAILED;
257                 }
258                 // Sent launch request
259                 ret = app_control_send_launch_request(app_control, NULL, NULL);
260                 if (APP_CONTROL_ERROR_NONE != ret) {
261                         SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Fail to send launch request, ret(%d)", ret);
262                         app_control_destroy(app_control);
263                         return VCD_ERROR_OPERATION_FAILED;
264                 }
265                 // Destroy app control
266                 ret = app_control_destroy(app_control);
267                 if (APP_CONTROL_ERROR_NONE != ret) {
268                         SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Fail to destroy, ret(%d)", ret);
269                         return VCD_ERROR_OPERATION_FAILED;
270                 }
271         }
272         return VCD_ERROR_NONE;
273 }
274
275 static int __vcd_resume_app(const char* appid)
276 {
277         app_context_h app_context = NULL;
278         int ret = app_manager_get_app_context(appid, &app_context);
279         if (APP_MANAGER_ERROR_NONE != ret) {
280                 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Fail to get app_context, ret(%d), appid(%s)", ret, appid);
281                 return VCD_ERROR_OPERATION_FAILED;
282         }
283
284         ret = app_manager_resume_app(app_context);
285         if (APP_MANAGER_ERROR_NONE != ret) {
286                 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Fail to resume app, ret(%d), appid(%s)", ret, appid);
287                 return VCD_ERROR_OPERATION_FAILED;
288         }
289         return VCD_ERROR_NONE;
290 }
291
292 static bool __vcd_is_package_installed(const char* appid)
293 {
294         app_info_h app_info = NULL;
295         int ret = app_manager_get_app_info(appid, &app_info);
296         if (APP_MANAGER_ERROR_NONE != ret || NULL == app_info)
297                 return false;
298         ret = app_info_destroy(app_info);
299         if (APP_MANAGER_ERROR_NONE != ret)
300                 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Fail to destroy app_info, ret(%d)", ret);
301         return true;
302 }
303
304 static bool __vcd_launch_app(const char* result)
305 {
306         if (NULL == result) {
307                 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Invalid parameter");
308                 return VCD_ERROR_INVALID_PARAMETER;
309         }
310
311         GSList* app_list = NULL;
312         if (0 != vc_db_get_appid_list(result, &app_list)) {
313                 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] result text is NULL");
314                 return VCD_ERROR_INVALID_PARAMETER;
315         }
316
317         if (0 != g_slist_length(app_list)) {
318                 /* releaes data */
319                 GSList *iter = NULL;
320                 vc_deactivated_app_s* temp_app = NULL;
321                 iter = g_slist_nth(app_list, 0);
322
323                 while (NULL != iter) {
324                         temp_app = iter->data;
325
326                         if (NULL != temp_app) {
327                                 if (NULL != temp_app->appid) {
328                                         int ret = -1;
329                                         bool running = false;
330                                         ret = app_manager_is_running(temp_app->appid, &running);
331                                         if (APP_MANAGER_ERROR_NONE != ret) {
332                                                 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Fail to check running with appid(%s)", temp_app->appid);
333                                                 free(temp_app->appid);
334                                                 temp_app->appid = NULL;
335                                                 free(temp_app);
336                                                 temp_app = NULL;
337                                                 return VCD_ERROR_OPERATION_FAILED;
338                                         }
339                                         if (false == running) {
340                                                 int tmp_ret = __vcd_is_package_installed(temp_app->appid);
341                                                 if (false == tmp_ret) {
342                                                         SLOG(LOG_WARN, TAG_VCD, "[WARNING] app is not installed, appid(%s)", temp_app->appid);
343                                                 } else {
344                                                         ret = __vcd_activate_app_by_appcontrol(temp_app->appid);
345                                                         if (0 != ret) {
346                                                                 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Fail to activate app");
347                                                                 free(temp_app->appid);
348                                                                 temp_app->appid = NULL;
349                                                                 free(temp_app);
350                                                                 temp_app = NULL;
351                                                                 return ret;
352                                                         }
353                                                         SLOG(LOG_ERROR, TAG_VCD, "Launch app: appid(%s) result(%s)", temp_app->appid, result);
354                                                 }
355                                         } else {
356                                                 ret = __vcd_resume_app(temp_app->appid);
357                                                 if (0 != ret) {
358                                                         SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Fail to resume app");
359                                                         free(temp_app->appid);
360                                                         temp_app->appid = NULL;
361                                                         free(temp_app);
362                                                         temp_app = NULL;
363                                                         return ret;
364                                                 }
365                                                 SLOG(LOG_ERROR, TAG_VCD, "Resume app: appid(%s) result(%s)", temp_app->appid, result);
366                                         }
367                                         free(temp_app->appid);
368                                         temp_app->appid = NULL;
369                                 }
370                                 free(temp_app);
371                                 temp_app = NULL;
372                         }
373                         iter = g_slist_next(iter);
374                 }
375                 app_list = NULL;
376         }
377
378         return VCD_ERROR_NONE;
379 }
380
381 static Eina_Bool __vcd_send_selected_result(void *data)
382 {
383         GSList* pid_list = NULL;
384         const char* result = vcd_client_manager_get_result_text();
385
386         if (0 != vc_info_parser_get_result_pid_list(&pid_list, result)) {
387                 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Fail to get pid list. No result");
388         } else {
389                 if (0 < g_slist_length(pid_list)) {
390                         GSList* iter = NULL;
391                         vc_cmd_s* temp_cmd = NULL;
392                         int ret = 0;
393                         int pre_pid = -1;
394                         int pre_type = -1;
395
396                         iter = g_slist_nth(pid_list, 0);
397                         while (NULL != iter) {
398                                 temp_cmd = iter->data;
399
400                                 if (NULL != temp_cmd && (pre_pid != temp_cmd->pid || pre_type == VC_COMMAND_TYPE_WIDGET || temp_cmd->type == VC_COMMAND_TYPE_WIDGET)) {
401                                         /* Launch deactivated several apps that is matched with result */
402                                         ret = __vcd_launch_app(result);
403                                         if (0 != ret) {
404                                                 SLOG(LOG_ERROR, TAG_VCD, "Fail to launch or resume app, ret(%d) result(%s)", ret, result);
405                                         } else {
406                                                 /* send result noti */
407                                                 ret = vcdc_send_result(temp_cmd->pid, vcd_client_manager_get_pid(), temp_cmd->type);
408                                                 if (0 != ret) {
409                                                         SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Fail to send result, ret(%d)", ret);
410                                                         break;
411                                                 } else {
412                                                         SLOG(LOG_DEBUG, TAG_VCD, "[Server] Send result : pid(%d) type(%d)", temp_cmd->pid, temp_cmd->type);
413                                                         pre_pid = temp_cmd->pid;
414                                                         pre_type = temp_cmd->type;
415                                                 }
416                                         }
417                                         free(temp_cmd);
418                                         temp_cmd = NULL;
419                                 }
420                                 pid_list = g_slist_remove_link(pid_list, iter);
421                                 iter = g_slist_nth(pid_list, 0);
422                         }
423                 }
424         }
425
426         if (VCD_RECOGNITION_MODE_RESTART_CONTINUOUSLY != vcd_client_get_recognition_mode()) {
427                 vcd_config_set_service_state(VCD_STATE_READY);
428                 vcdc_send_service_state(VCD_STATE_READY);
429         }
430
431         return EINA_FALSE;
432 }
433
434 //static void __vcd_server_pre_result_cb(vcp_pre_result_event_e event, const char* pre_result, void *user_data)
435 static void __vcd_server_asr_result_cb(vcp_asr_result_event_e event, const char* asr_result, void *user_data)
436 {
437         if (NULL != asr_result) {
438                 SLOG(LOG_DEBUG, TAG_VCD, "[Server] ASR result - Event(%d), Text(%s)", event, asr_result);
439                 vcdc_send_pre_result_to_manager(vcd_client_manager_get_pid(), event, asr_result);
440         }
441
442         return;
443 }
444
445 static void __vcd_server_nlg_result_cb(const char* nlg_result, void *user_data)
446 {
447         int ret = __vcd_server_launch_manager_app();
448         if (0 != ret) {
449                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to send dialog : mgr_pid(%d), nlg_result(%s)", vcd_client_manager_get_pid(), nlg_result);
450                 return;
451         }
452
453         ret = vcdc_send_dialog(vcd_client_manager_get_pid(), -1, nlg_result, NULL, 0); //0: VC_DIALOG_END
454         if (0 != ret)
455                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to send dialog : mgr_pid(%d), nlg_result(%s)", vcd_client_manager_get_pid(), nlg_result);
456         return;
457 }
458
459 static void* __recorder_stop(void *data)
460 {
461         vcd_recorder_stop();
462         return NULL;
463 }
464
465 static void __vcd_server_result_cb(vcp_result_event_e event, int* result_id, int count, const char* all_result,
466                                                                    const char* non_fixed_result, const char* nlu_result, const char* msg, void *user_data)
467 {
468         vc_info_parser_unset_result(vcd_client_manager_get_exclusive());
469         vcd_client_manager_set_result_text(all_result);
470
471         SLOG(LOG_INFO, TAG_VCD, "[Server] Event(%d), Text(%s) Nonfixed(%s) Msg(%s) Result count(%d)", 
472                 event, all_result, non_fixed_result, msg, count);
473
474         if (VCD_RECOGNITION_MODE_RESTART_AFTER_REJECT == vcd_client_get_recognition_mode()) {
475                 if (VCP_RESULT_EVENT_REJECTED == event) {
476                         SLOG(LOG_DEBUG, TAG_VCD, "[Server] Restart by no or rejected result");
477                         /* If no result and restart option is ON */
478                         /* Send reject message */
479                         bool temp = vcd_client_manager_get_exclusive();
480                         vc_info_parser_set_result(all_result, event, msg, NULL, temp);
481                         if (0 != vcdc_send_result_to_manager(vcd_client_manager_get_pid(), VC_RESULT_TYPE_NOTIFICATION)) {
482                                 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Fail to send result");
483                         }
484
485                         g_restart_timer = ecore_timer_add(0, __restart_engine, NULL);
486                         return;
487                 }
488                 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Stop recorder due to success");
489                 //vcd_recorder_stop();
490                 ecore_main_loop_thread_safe_call_sync(__recorder_stop, NULL);
491         } else if (VCD_RECOGNITION_MODE_RESTART_CONTINUOUSLY == vcd_client_get_recognition_mode()) {
492                 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Restart continuously");
493                 /* Restart option is ON */
494                 g_restart_timer = ecore_timer_add(0, __restart_engine, NULL);
495                 if (VCP_RESULT_EVENT_REJECTED == event) {
496                         bool temp = vcd_client_manager_get_exclusive();
497                         vc_info_parser_set_result(all_result, event, msg, NULL, temp);
498                         if (0 != vcdc_send_result_to_manager(vcd_client_manager_get_pid(), VC_RESULT_TYPE_NOTIFICATION)) {
499                                 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Fail to send result");
500                         }
501                         return;
502                 }
503         }
504 #if 1
505
506         int pid = vcd_client_widget_get_foreground_pid();
507         if (-1 != pid) {
508                 if (NULL != all_result) {
509                         vc_info_parser_set_result(all_result, event, msg, NULL, false);
510                         bool enable = false;
511                         vcd_client_widget_get_asr_result_enabled(pid, &enable);
512                         if (true == enable) {
513                                 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Send ASR result to Widget client");
514                                 bool is_consumed = false;
515                                 if (0 != vcdc_send_asr_result(pid, VC_COMMAND_TYPE_WIDGET, &is_consumed)) {
516                                         SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to send asr result");
517                                 } else {
518                                         SLOG(LOG_DEBUG, TAG_VCD, "[Server] ASR result is consumed(%d)", is_consumed);
519                                         if (true == is_consumed) {
520                                                 vcdc_send_show_tooltip(pid, false);
521                                                 if (-1 != vcd_client_manager_get_pid()) {
522                                                         /* Manager client is available */
523                                                         vc_info_parser_unset_result(false);
524                                                         vc_info_parser_set_result(all_result, VC_RESULT_EVENT_RESULT_SUCCESS, msg, NULL, false);
525                                                         if (0 != vcdc_send_result_to_manager(vcd_client_manager_get_pid(), VC_RESULT_TYPE_NOTIFICATION)) {
526                                                                 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Fail to send result");
527                                                         }
528                                                 }
529
530                                                 vcd_client_manager_set_exclusive(false);
531
532                                                 vcd_config_set_service_state(VCD_STATE_READY);
533                                                 vcdc_send_service_state(VCD_STATE_READY);
534                                                 return;
535                                         }
536                                 }
537                         }
538                 }
539         }
540
541         /* if nlu_result is exist, Add command handle(is_action) into result list */
542         /* Normal result */
543         SLOG(LOG_DEBUG, TAG_VCD, "[Server] === Get engine result ===");
544
545         int ret = -1;
546         vc_cmd_s* temp_cmd = NULL;
547         vc_cmd_list_h vc_cmd_list = NULL;
548
549         if (0 != vc_cmd_list_create(&vc_cmd_list)) {
550                 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Fail to create command list");
551                 vcd_client_manager_set_exclusive(false);
552                 vcd_config_set_service_state(VCD_STATE_READY);
553                 vcdc_send_service_state(VCD_STATE_READY);
554                 return;
555         }
556
557         /* priority filter */
558         /* system > exclusive > foreground = widget > system_background > background */
559         int i = 0;
560         int* filtered_id = (int*)calloc(count, sizeof(int));
561         int filtered_count = 0;
562         int top_priority = VC_COMMAND_PRIORITY_BACKGROUND;
563         for (i = 0; i < count; i++) {
564                 SLOG(LOG_INFO, TAG_VCD, "[Server]   [%d] Result id(%d)", i, result_id[i]);
565
566                 if (0 > result_id[i]) {
567                         SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Result ID(%d) is NOT valid", result_id[i]);
568                         continue;
569                 }
570
571                 ret = vcd_client_get_cmd_from_result_id(result_id[i], &temp_cmd);
572                 if (0 == ret && NULL != temp_cmd) {
573                         if (top_priority == temp_cmd->priority) {
574                                 filtered_id[filtered_count] = result_id[i];
575                                 filtered_count++;
576                         } else if (top_priority < temp_cmd->priority) {
577                                 continue;
578                         } else {
579                                 filtered_id[0] = result_id[i];
580                                 filtered_count = 1;
581                                 top_priority = temp_cmd->priority;
582                         }
583                 }
584         }
585
586         int is_action = 0;
587         for (i = 0; i < filtered_count; i++) {
588                 SLOG(LOG_INFO, TAG_VCD, "[Server]   [%d] Filtered Result id(%d)", i, filtered_id[i]);
589
590                 if (filtered_id[i] < 0) {
591                         SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Filtered ID(%d) is NOT valid", filtered_id[i]);
592                         continue;
593                 }
594
595                 ret = vcd_client_get_cmd_from_result_id(filtered_id[i], &temp_cmd);
596                 if (0 == ret && NULL != temp_cmd) {
597                         switch (temp_cmd->format) {
598                         case VC_CMD_FORMAT_FIXED:
599                         case VC_CMD_FORMAT_FIXED_AND_VFIXED:
600                         case VC_CMD_FORMAT_VFIXED_AND_FIXED:
601                         case VC_CMD_FORMAT_PARTIAL:
602                         case VC_CMD_FORMAT_FIXED_AND_NONFIXED:
603                         case VC_CMD_FORMAT_NONFIXED_AND_FIXED:
604                                 break;
605                         case VC_CMD_FORMAT_ACTION:
606                                 is_action = 1;
607                                 break;
608                         default:
609                                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Unknown command type : %d", temp_cmd->type);
610                         }
611
612                         temp_cmd->id = i;
613                         if (0 != vc_cmd_list_add(vc_cmd_list, (vc_cmd_h)temp_cmd)) {
614                                 SLOG(LOG_DEBUG, TAG_VCD, "Fail to add command to list");
615                                 vc_cmd_destroy((vc_cmd_h)temp_cmd);
616                         }
617                 } else {
618                         SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] NOT found matached result(%d)", filtered_id[i]);
619                 }
620         }
621
622         if (NULL != filtered_id) {
623                 free(filtered_id);
624                 filtered_id = NULL;
625         }
626
627         if (NULL != nlu_result) {
628                 SLOG(LOG_INFO, TAG_VCD, "[Server] NLU (%s)", nlu_result);
629                 vc_info_parser_set_nlu_result(nlu_result);
630                 if (0 == is_action) {
631                         vc_cmd_h nlu_cmd;
632                         if (0 != vc_cmd_create(&nlu_cmd)) {
633                                 SLOG(LOG_ERROR, TAG_VCD, "Fail to nlu cmd create");
634                         } else {
635                                 if (0 != vc_cmd_set_type(nlu_cmd, VC_COMMAND_TYPE_SYSTEM)) {
636                                         SLOG(LOG_ERROR, TAG_VCD, "Fail to set type");
637                                 }
638                                 if (0 != vc_cmd_set_pid(nlu_cmd, vcd_client_manager_get_pid())) {
639                                         SLOG(LOG_ERROR, TAG_VCD, "Fail to set pid");
640                                 }
641                                 if (0 != vc_cmd_set_format(nlu_cmd, VC_CMD_FORMAT_ACTION)) {
642                                         SLOG(LOG_ERROR, TAG_VCD, "Fail to set format");
643                                 }
644                                 if (0 != vc_cmd_list_add(vc_cmd_list, nlu_cmd)) {
645                                         SLOG(LOG_ERROR, TAG_VCD, "Fail to add nlu cmd to list");
646                                         vc_cmd_destroy(nlu_cmd);
647                                 }
648                         }
649                 }
650         }
651
652         vc_cmd_print_list(vc_cmd_list);
653
654         SLOG(LOG_DEBUG, TAG_VCD, "[Server] =========================");
655
656         int result_count = 0;
657         vc_cmd_list_get_count(vc_cmd_list, &result_count);
658
659         if (0 == result_count) {
660                 /* No result */
661                 if (NULL != all_result) {
662                         SLOG(LOG_DEBUG, TAG_VCD, "[Server] Engine result is no command : %s", all_result);
663                 } else {
664                         SLOG(LOG_DEBUG, TAG_VCD, "[Server] Engine result is NULL");
665                 }
666                 bool temp = vcd_client_manager_get_exclusive();
667                 vc_info_parser_set_result(all_result, event, msg, NULL, temp);
668
669                 int pid = vcd_client_widget_get_foreground_pid();
670                 if (-1 != pid) {
671                         SLOG(LOG_DEBUG, TAG_VCD, "[Server] Request tooltip hide");
672                         /* Send to hide tooltip */
673                         vcdc_send_show_tooltip(pid, false);
674                 }
675
676                 if (-1 != vcd_client_manager_get_pid()) {
677                         /* Manager client is available */
678                         if (0 != vcdc_send_result_to_manager(vcd_client_manager_get_pid(), VC_RESULT_TYPE_NORMAL)) {
679                                 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Fail to send result");
680                         }
681                 }
682
683                 vcd_client_manager_set_exclusive(false);
684
685                 return;
686         } else {
687                 if (false == vcd_client_manager_get_exclusive()) {
688                         int pid = vcd_client_widget_get_foreground_pid();
689                         if (-1 != pid) {
690                                 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Request tooltip hide");
691                                 vcdc_send_show_tooltip(pid, false);
692                         }
693
694                         vc_info_parser_set_result(all_result, event, msg, vc_cmd_list, false);
695
696                         if (-1 != vcd_client_manager_get_pid()) {
697                                 /* Manager client is available */
698                                 if (0 != vcdc_send_result_to_manager(vcd_client_manager_get_pid(), VC_RESULT_TYPE_NORMAL)) {
699                                         SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Fail to send result");
700                                 }
701                         } else {
702                                 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Manager is NOT available. Send result to client directly");
703                                 /* Send result to client */
704                                 ecore_timer_add(0, __vcd_send_selected_result, NULL);
705                         }
706                 } else {
707                         /* exclusive command */
708                         vc_info_parser_set_result(all_result, event, msg, vc_cmd_list, true);
709
710                         if (-1 != vcd_client_manager_get_pid()) {
711                                 /* Manager client is available */
712                                 if (0 != vcdc_send_result_to_manager(vcd_client_manager_get_pid(), VC_RESULT_TYPE_NORMAL)) {
713                                         SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Fail to send result");
714                                 }
715                         } else {
716                                 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Manager is NOT available");
717                         }
718
719                         vcd_client_manager_set_exclusive(false);
720                 }
721         }
722
723         vc_cmd_list_destroy(vc_cmd_list, true);
724
725         return;
726
727 #else
728         /* No result */
729         if (NULL == result_id) {
730                 /* No result */
731                 if (NULL != all_result) {
732                         SLOG(LOG_DEBUG, TAG_VCD, "[Server] Engine result is no command : %s", all_result);
733                         bool temp = vcd_client_manager_get_exclusive();
734                         vc_info_parser_set_result(all_result, event, msg, NULL, temp);
735                 }
736
737                 int pid = vcd_client_widget_get_foreground_pid();
738                 if (-1 != pid) {
739                         if (NULL != all_result) {
740                                 /* Send result text to widget */
741                                 vcdc_send_result(pid, vcd_client_manager_get_pid(), VC_COMMAND_TYPE_WIDGET);
742                         }
743
744                         SLOG(LOG_DEBUG, TAG_VCD, "[Server] Request tooltip hide");
745                         /* Send to hide tooltip */
746                         vcdc_send_show_tooltip(pid, false);
747                 }
748
749                 if (-1 != vcd_client_manager_get_pid()) {
750                         /* Manager client is available */
751                         if (0 != vcdc_send_result_to_manager(vcd_client_manager_get_pid(), VC_RESULT_TYPE_NORMAL)) {
752                                 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Fail to send result");
753                         }
754                 }
755
756                 vcd_client_manager_set_exclusive(false);
757
758                 return;
759         }
760
761         /* Normal result */
762         SLOG(LOG_DEBUG, TAG_VCD, "[Server] === Get engine result ===");
763
764         int ret = -1;
765         vc_cmd_s* temp_cmd = NULL;
766         vc_cmd_list_h vc_cmd_list = NULL;
767
768         if (0 != vc_cmd_list_create(&vc_cmd_list)) {
769                 SLOG(LOG_ERROR, TAG_VCD, "[Server] Fail to create command list");
770                 vcd_client_manager_set_exclusive(false);
771                 vcd_config_set_service_state(VCD_STATE_READY);
772                 vcdc_send_service_state(VCD_STATE_READY);
773                 return;
774         }
775
776         int i = 0;
777         for (i = 0; i < count; i++) {
778                 SLOG(LOG_DEBUG, TAG_VCD, "[Server]   [%d] Result ID(%d)", i, result_id[i]);
779
780                 if (result_id[i] < 0) {
781                         SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Result ID(%d) is NOT valid", result_id[i]);
782                         continue;
783                 }
784
785                 ret = vcd_client_get_cmd_from_result_id(result_id[i], &temp_cmd);
786                 if (0 == ret && NULL != temp_cmd) {
787                         switch (temp_cmd->format) {
788                         case VC_CMD_FORMAT_FIXED:
789                         case VC_CMD_FORMAT_FIXED_AND_VFIXED:
790                         case VC_CMD_FORMAT_VFIXED_AND_FIXED:
791                         case VC_CMD_FORMAT_PARTIAL:
792                                 /* Nonfixed result is NOT valid */
793                                 break;
794                         case VC_CMD_FORMAT_FIXED_AND_NONFIXED:
795                                 if (NULL == temp_cmd->parameter) {
796                                         if (NULL != non_fixed_result) {
797                                                 temp_cmd->parameter = strdup(non_fixed_result);
798                                         }
799                                 } else {
800                                         SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Command type is NOT vaild. Parameter (%s)", temp_cmd->parameter);
801                                 }
802                                 break;
803                         case VC_CMD_FORMAT_NONFIXED_AND_FIXED:
804                                 if (NULL == temp_cmd->command) {
805                                         if (NULL != non_fixed_result) {
806                                                 temp_cmd->command = strdup(non_fixed_result);
807                                         }
808                                 } else {
809                                         SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Command type is NOT vaild. Command (%s)", temp_cmd->command);
810                                 }
811
812                                 break;
813                         default:
814                                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Unknown command type : %d", temp_cmd->type);
815                         }
816
817                         temp_cmd->id = i;
818                         if (0 != vc_cmd_list_add(vc_cmd_list, (vc_cmd_h)temp_cmd)) {
819                                 SLOG(LOG_DEBUG, TAG_VCD, "Fail to add command to list");
820                                 vc_cmd_destroy((vc_cmd_h)temp_cmd);
821                         }
822                 } else {
823                         SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] NOT found matached result(%d)", result_id[i]);
824                 }
825         }
826
827         vc_cmd_print_list(vc_cmd_list);
828
829         SLOG(LOG_DEBUG, TAG_VCD, "[Server] =========================");
830
831         int result_count = 0;
832         vc_cmd_list_get_count(vc_cmd_list, &result_count);
833
834         if (false == vcd_client_manager_get_exclusive()) {
835                 int pid = vcd_client_widget_get_foreground_pid();
836                 if (-1 != pid) {
837                         SLOG(LOG_DEBUG, TAG_VCD, "[Server] Request tooltip hide");
838                         vcdc_send_show_tooltip(pid, false);
839                 }
840
841                 vc_info_parser_set_result(all_result, event, msg, vc_cmd_list, false);
842
843                 if (-1 != vcd_client_manager_get_pid()) {
844                         /* Manager client is available */
845                         if (0 != vcdc_send_result_to_manager(vcd_client_manager_get_pid(), VC_RESULT_TYPE_NORMAL)) {
846                                 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Fail to send result");
847                         }
848                 } else {
849                         SLOG(LOG_DEBUG, TAG_VCD, "[Server] Manager is NOT available. Send result to client directly");
850                         /* Send result to client */
851                         ecore_timer_add(0, __vcd_send_selected_result, NULL);
852                 }
853         } else {
854                 /* exclusive command */
855                 vc_info_parser_set_result(all_result, event, msg, vc_cmd_list, true);
856
857                 if (-1 != vcd_client_manager_get_pid()) {
858                         /* Manager client is available */
859                         if (0 != vcdc_send_result_to_manager(vcd_client_manager_get_pid(), VC_RESULT_TYPE_NORMAL)) {
860                                 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Fail to send result");
861                         }
862                 } else {
863                         SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Manager is NOT available");
864                 }
865
866                 vcd_client_manager_set_exclusive(false);
867         }
868
869         vc_cmd_list_destroy(vc_cmd_list, true);
870
871         return;
872 #endif
873 }
874
875 #if 0
876 static void __vcd_server_nlu_result_cb(vcp_result_event_e event, const char* nlu_result, void *user_data)
877 {
878         SLOG(LOG_DEBUG, TAG_VCD, "[Server] NLU result cb - event(%d)", event);
879         SLOG(LOG_DEBUG, TAG_VCD, "[Server] result (%s)", nlu_result);
880
881         int ret = vc_info_parser_set_nlu_result(nlu_result);
882         if (0 != ret) {
883                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to set nlu result (%d)", ret);
884         }
885
886         return;
887 }
888 #endif
889
890 static void __vcd_server_error_cb(vcp_error_e error, const char* msg, void *user_data)
891 {
892         SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Engine Error cb - reason(%d), msg(%s)", error, msg);
893         ecore_main_loop_thread_safe_call_async(__cancel_by_error, NULL);
894
895         char* error_msg = NULL;
896         if (NULL != msg) {
897                 error_msg = strdup(msg);
898         }
899
900         if (0 != vcdc_send_error_signal_to_manager(vcd_client_manager_get_pid(), error, error_msg)) {
901                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to send error signal");
902         }
903
904         if (NULL != error_msg) {
905                 free(error_msg);
906                 error_msg = NULL;
907         }
908
909         return;
910 }
911
912 /*
913 * vcd server Interfaces
914 */
915 static void __vcd_file_clean_up()
916 {
917         SLOG(LOG_DEBUG, TAG_VCD, "== Old file clean up == ");
918
919         DIR *dp = NULL;
920         struct dirent *dirp = NULL;
921
922         dp = opendir(VC_RUNTIME_INFO_ROOT);
923         if (dp == NULL) {
924                 SLOG(LOG_ERROR, TAG_VCD, "[File message WARN] Fail to open path : %s", VC_RUNTIME_INFO_ROOT);
925                 return;
926         }
927
928         char remove_path[256] = {0, };
929         do {
930                 dirp = readdir(dp);
931
932                 if (NULL != dirp) {
933                         if (!strncmp("vc_", dirp->d_name, strlen("vc_"))) {
934                                 memset(remove_path, 0, 256);
935                                 snprintf(remove_path, 256, "%s/%s", VC_RUNTIME_INFO_ROOT, dirp->d_name);
936
937                                 /* Clean up code */
938                                 if (0 != remove(remove_path)) {
939                                         SLOG(LOG_WARN, TAG_VCD, "[File message WARN] Fail to remove file : %s", remove_path);
940                                 } else {
941                                         SLOG(LOG_DEBUG, TAG_VCD, "[File message] Remove file : %s", remove_path);
942                                 }
943                         }
944                 }
945         } while (NULL != dirp);
946
947         closedir(dp);
948
949         return;
950 }
951
952 static int __vcd_db_clean_up()
953 {
954         int ret = 0;
955         int cnt = VC_COMMAND_TYPE_FOREGROUND;
956         do {
957                 if (VC_COMMAND_TYPE_BACKGROUND != cnt)
958                         ret = vc_db_delete_commands(-1, cnt, NULL);
959         } while (VC_COMMAND_TYPE_EXCLUSIVE >= ++cnt);
960
961         return ret;
962 }
963
964 int vcd_initialize()
965 {
966         int ret = 0;
967
968         /* Remove old file */
969         __vcd_file_clean_up();
970
971         /* initialize modules */
972         ret = vcd_config_initialize(__config_lang_changed_cb, __config_foreground_changed_cb, NULL);
973         if (0 != ret) {
974                 SLOG(LOG_ERROR, TAG_VCD, "[Server WARNING] Fail to initialize config.");
975         }
976
977         /* Remove db data */
978         ret = __vcd_db_clean_up();
979         if (0 != ret) {
980                 SLOG(LOG_ERROR, TAG_VCD, "[Server WARNING] Fail to remove db data");
981         }
982
983         vcd_config_set_service_state(VCD_STATE_NONE);
984
985         //ret = vcd_engine_agent_init(__vcd_server_pre_result_cb, __vcd_server_result_cb, __vcd_server_nlu_result_cb, __vcd_server_error_cb);
986         ret = vcd_engine_agent_init(__vcd_server_asr_result_cb, __vcd_server_result_cb, __vcd_server_nlg_result_cb, __vcd_server_error_cb);
987         if (0 != ret) {
988                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to engine agent initialize : result(%d)", ret);
989                 return ret;
990         }
991
992         if (0 != vcd_recorder_create(__server_recorder_callback, __server_recorder_interrupt_callback)) {
993                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to create recorder");
994                 return VCD_ERROR_OPERATION_FAILED;
995         }
996
997         /* Find engine */
998         ret = vcd_engine_agent_initialize_current_engine();
999         if (0 != ret) {
1000                 if (VCD_ERROR_ENGINE_NOT_FOUND == ret)
1001                         SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] There is No Voice control engine");
1002                 else
1003                         SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to init engine");
1004
1005                 g_is_engine = false;
1006         } else {
1007                 g_is_engine = true;
1008         }
1009
1010         /* Load engine */
1011         if (0 != vcd_engine_agent_load_current_engine()) {
1012                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to load current engine");
1013                 return VCD_ERROR_OPERATION_FAILED;
1014         }
1015
1016         /* Initialize manager info */
1017         vcd_client_manager_unset();
1018
1019         vcd_config_set_service_state(VCD_STATE_READY);
1020         vcdc_send_service_state(VCD_STATE_READY);
1021
1022         SLOG(LOG_DEBUG, TAG_VCD, "[Server SUCCESS] initialize");
1023
1024         return 0;
1025 }
1026
1027 void vcd_finalize()
1028 {
1029         GList *iter = NULL;
1030         if (0 < g_list_length(g_proc_list)) {
1031                 iter = g_list_first(g_proc_list);
1032                 while (NULL != iter) {
1033                         g_proc_list = g_list_remove_link(g_proc_list, iter);
1034                         iter = g_list_first(g_proc_list);
1035                 }
1036         }
1037
1038         if (g_restart_timer != NULL) {
1039                 ecore_timer_del(g_restart_timer);
1040                 g_restart_timer = NULL;
1041         }
1042
1043         vcd_state_e state = vcd_config_get_service_state();
1044         if (VCD_STATE_READY != state) {
1045                 if (VCD_STATE_RECORDING == state) {
1046                         vcd_recorder_stop();
1047                 }
1048                 vcd_engine_recognize_cancel();
1049         }
1050         if (0 != vcd_recorder_destroy()) {
1051                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to destroy recorder");
1052         } else {
1053                 SLOG(LOG_DEBUG, TAG_VCD, "[Server] destroy recorder");
1054         }
1055
1056         if (0 != vcd_engine_agent_release()) {
1057                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to release engine");
1058         } else {
1059                 SLOG(LOG_DEBUG, TAG_VCD, "[Server] release engine");
1060         }
1061
1062         vcd_client_manager_unset_appid();
1063
1064         vcd_config_set_service_state(VCD_STATE_NONE);
1065         vcdc_send_service_state(VCD_STATE_NONE);
1066
1067         SLOG(LOG_DEBUG, TAG_VCD, "[Server] mode finalize");
1068
1069         return;
1070 }
1071
1072 static Eina_Bool __finalize_quit_ecore_loop(void *data)
1073 {
1074         SLOG(LOG_DEBUG, TAG_VCD, "[Server] quit ecore main loop");
1075         ecore_main_loop_quit();
1076         return EINA_FALSE;
1077 }
1078
1079 static void __read_proc()
1080 {
1081         DIR *dp = NULL;
1082         struct dirent *dirp = NULL;
1083         int tmp;
1084
1085         GList *iter = NULL;
1086         if (0 < g_list_length(g_proc_list)) {
1087                 iter = g_list_first(g_proc_list);
1088                 while (NULL != iter) {
1089                         g_proc_list = g_list_remove_link(g_proc_list, iter);
1090                         iter = g_list_first(g_proc_list);
1091                 }
1092         }
1093
1094         dp = opendir("/proc");
1095         if (NULL == dp) {
1096                 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Fail to open proc");
1097         } else {
1098                 do {
1099                         dirp = readdir(dp);
1100
1101                         if (NULL != dirp) {
1102                                 tmp = atoi(dirp->d_name);
1103                                 if (0 >= tmp)   continue;
1104                                 g_proc_list = g_list_append(g_proc_list, GINT_TO_POINTER(tmp));
1105                         }
1106                 } while (NULL != dirp);
1107                 closedir(dp);
1108         }
1109         return;
1110 }
1111
1112 static void __vcd_cleanup_client(vcd_client_type_e type)
1113 {
1114         int* client_list = NULL;
1115         int client_count = 0;
1116         int i = 0;
1117         int j = 0;
1118         bool exist = false;
1119         int mgr_pid = -1;
1120         int ret = -1;
1121
1122         if (VCD_CLIENT_TYPE_NORMAL == type) {
1123                 ret = vcd_client_get_list(&client_list, &client_count);
1124         } else if (VCD_CLIENT_TYPE_WIDGET == type) {
1125                 ret = vcd_client_widget_get_list(&client_list, &client_count);
1126         } else if (VCD_CLIENT_TYPE_MANAGER == type) {
1127                 mgr_pid = vcd_client_manager_get_pid();
1128                 client_list = &mgr_pid;
1129                 client_count = 1;
1130                 if (-1 == mgr_pid) {
1131                         SLOG(LOG_WARN, TAG_VCD, "[WARNING] Invalid Manager pid");
1132                         return;
1133                 }
1134         }
1135
1136         if (0 == ret || mgr_pid > 0) {
1137                 SLOG(LOG_DEBUG, TAG_VCD, "===== Clean up %s client ", type ? (type == 1) ? "Widget" : "Manager" : "Normal");
1138                 if (NULL != client_list && client_count > 0) {
1139                         for (i = 0; i < client_count; i++) {
1140                                 exist = false;
1141                                 GList *iter = NULL;
1142                                 for (j = 0; j < g_list_length(g_proc_list); j++) {
1143                                         iter = g_list_nth(g_proc_list, j);
1144                                         if (NULL != iter) {
1145                                                 if (client_list[i] == GPOINTER_TO_INT(iter->data)) {
1146                                                         SLOG(LOG_DEBUG, TAG_VCD, "%s pid(%d) is running", type ? (type == 1) ? "Widget" : "Manager" : "Normal", client_list[i]);
1147                                                         exist = true;
1148                                                         break;
1149                                                 }
1150                                         }
1151                                 }
1152
1153                                 if (false == exist) {
1154                                         SLOG(LOG_ERROR, TAG_VCD, "%s pid(%d) should be removed", type ? (type == 1) ? "Widget" : "Manager" : "Normal", client_list[i]);
1155                                         if (VCD_CLIENT_TYPE_NORMAL == type)
1156                                                 vcd_server_finalize(client_list[i]);
1157                                         else if (VCD_CLIENT_TYPE_WIDGET == type)
1158                                                 vcd_server_widget_finalize(client_list[i]);
1159                                         else
1160                                                 vcd_server_mgr_finalize(mgr_pid);
1161                                 }
1162                         }
1163                 }
1164                 SLOG(LOG_DEBUG, TAG_VCD, "=====");
1165                 SLOG(LOG_DEBUG, TAG_VCD, "  ");
1166         }
1167         if (NULL != client_list && -1 == mgr_pid) {
1168                 free(client_list);
1169                 client_list = NULL;
1170         }
1171         return;
1172 }
1173
1174 Eina_Bool vcd_cleanup_client_all(void *data)
1175 {
1176         __read_proc();
1177         
1178         __vcd_cleanup_client(VCD_CLIENT_TYPE_NORMAL);
1179         __vcd_cleanup_client(VCD_CLIENT_TYPE_WIDGET);
1180         __vcd_cleanup_client(VCD_CLIENT_TYPE_MANAGER);
1181
1182 #if 0
1183         if (0 == vcd_client_get_list(&client_list, &client_count)) {
1184                 SLOG(LOG_DEBUG, TAG_VCD, "===== Clean up client ");
1185                 if (NULL != client_list && client_count > 0) {
1186                         for (i = 0; i < client_count; i++) {
1187                                 exist = false;
1188                                 GList *iter = NULL;
1189                                 for (j = 0; j < g_list_length(g_proc_list); j++) {
1190                                         iter = g_list_nth(g_proc_list, j);
1191                                         if (NULL != iter) {
1192                                                 if (client_list[i] == GPOINTER_TO_INT(iter->data)) {
1193                                                         SLOG(LOG_DEBUG, TAG_VCD, "pid(%d) is running", client_list[i]);
1194                                                         exist = true;
1195                                                         break;
1196                                                 }
1197                                         }
1198                                 }
1199
1200                                 if (false == exist) {
1201                                         SLOG(LOG_ERROR, TAG_VCD, "pid(%d) should be removed", client_list[i]);
1202                                         vcd_server_finalize(client_list[i]);
1203                                 }
1204 #if 0
1205                                 result = vcdc_send_hello(client_list[i], VCD_CLIENT_TYPE_NORMAL);
1206
1207                                 if (0 == result) {
1208                                         SLOG(LOG_DEBUG, TAG_VCD, "[Server] pid(%d) should be removed.", client_list[i]);
1209                                         vcd_server_finalize(client_list[i]);
1210                                 } else if (-1 == result) {
1211                                         SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Hello result has error");
1212                                 }
1213 #endif
1214                         }
1215                 }
1216                 SLOG(LOG_DEBUG, TAG_VCD, "=====");
1217                 SLOG(LOG_DEBUG, TAG_VCD, "  ");
1218         }
1219         if (NULL != client_list) {
1220                 free(client_list);
1221                 client_list = NULL;
1222         }
1223
1224         /* If app is in background state, app cannot response message. */
1225         if (0 == vcd_client_widget_get_list(&client_list, &client_count)) {
1226                 SLOG(LOG_DEBUG, TAG_VCD, "===== Clean up widget");
1227                 if (NULL != client_list && client_count > 0) {
1228                         for (i = 0; i < client_count; i++) {
1229                                 exist = false;
1230                                 GList *iter = NULL;
1231                                 for (j = 0; j < g_list_length(g_proc_list); j++) {
1232                                         iter = g_list_nth(g_proc_list, j);
1233                                         if (NULL != iter) {
1234                                                 if (client_list[i] == GPOINTER_TO_INT(iter->data)) {
1235                                                         SLOG(LOG_DEBUG, TAG_VCD, "widget pid(%d) is running", client_list[i]);
1236                                                         exist = true;
1237                                                         break;
1238                                                 }
1239                                         }
1240                                 }
1241
1242                                 if (false == exist) {
1243                                         SLOG(LOG_ERROR, TAG_VCD, "widget pid(%d) should be removed", client_list[i]);
1244                                         vcd_server_widget_finalize(client_list[i]);
1245                                 }
1246 #if 0
1247                                 result = vcdc_send_hello(client_list[i], VCD_CLIENT_TYPE_WIDGET);
1248
1249                                 if (0 == result) {
1250                                         SLOG(LOG_DEBUG, TAG_VCD, "[Server] widget pid(%d) should be removed.", client_list[i]);
1251                                         vcd_server_widget_finalize(client_list[i]);
1252                                 } else if (-1 == result) {
1253                                         SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Hello result has error");
1254                                 }
1255 #endif
1256                         }
1257                 }
1258                 SLOG(LOG_DEBUG, TAG_VCD, "=====");
1259                 SLOG(LOG_DEBUG, TAG_VCD, "  ");
1260         }
1261
1262         if (NULL != client_list) {
1263                 free(client_list);
1264                 client_list = NULL;
1265         }
1266
1267         /* manager */
1268         exist = false;
1269         GList *iter = NULL;
1270         int mgr_pid = vcd_client_manager_get_pid();
1271         if (0 < mgr_pid) {
1272                 for (j = 0; j < g_list_length(g_proc_list); j++) {
1273                         iter = g_list_nth(g_proc_list, j);
1274                         if (NULL != iter) {
1275                                 if (mgr_pid == GPOINTER_TO_INT(iter->data)) {
1276                                         SLOG(LOG_DEBUG, TAG_VCD, "manager pid(%d) is running", mgr_pid);
1277                                         exist = true;
1278                                         break;
1279                                 }
1280                         }
1281                 }
1282
1283                 if (false == exist) {
1284                         SLOG(LOG_ERROR, TAG_VCD, "manager pid (%d) should be removed", mgr_pid);
1285                         vcd_server_mgr_finalize(mgr_pid);
1286                 }
1287         }
1288 #endif
1289         return EINA_TRUE;
1290 }
1291
1292 int vcd_server_get_service_state()
1293 {
1294         return vcd_config_get_service_state();
1295 }
1296
1297 int vcd_server_get_foreground()
1298 {
1299         int pid;
1300         vcd_config_get_foreground(&pid);
1301         return pid;
1302 }
1303
1304
1305 /*
1306 * API for manager
1307 */
1308 int vcd_server_mgr_initialize(int pid)
1309 {
1310         if (false == g_is_engine) {
1311                 if (0 != vcd_engine_agent_initialize_current_engine()) {
1312                         SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] No Engine");
1313                         g_is_engine = false;
1314                         return VCD_ERROR_ENGINE_NOT_FOUND;
1315                 } else {
1316                         g_is_engine = true;
1317                 }
1318         }
1319
1320         /* check if pid is valid */
1321         if (false == vcd_client_manager_is_valid(pid)) {
1322                 SLOG(LOG_ERROR, TAG_VCD, "[Server] old manager pid(%d) be removed", vcd_client_manager_get_pid());
1323                 vcd_server_mgr_cancel();
1324                 vcd_client_manager_unset();
1325         }
1326
1327         /* Add client information to client manager */
1328         if (0 != vcd_client_manager_set(pid)) {
1329                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to add manager");
1330                 return VCD_ERROR_OPERATION_FAILED;
1331         }
1332
1333         if (0 != vcdc_send_manager_pid(pid))
1334                 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Fail to send manager pid");
1335
1336         SLOG(LOG_DEBUG, TAG_VCD, "[Server Success] Manager initialize : pid(%d)", pid);
1337
1338         return VCD_ERROR_NONE;
1339 }
1340
1341 int vcd_server_mgr_finalize(int pid)
1342 {
1343         /* check if pid is valid */
1344         if (false == vcd_client_manager_is_valid(pid)) {
1345                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] The manager pid(%d) is NOT valid", pid);
1346                 return VCD_ERROR_INVALID_PARAMETER;
1347         }
1348
1349         /* Cancel recognition */
1350         vcd_server_mgr_cancel();
1351
1352         if (0 != vcdc_send_manager_pid(-1))
1353                 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Fail to send manager pid");
1354
1355         /* Remove manager information */
1356         if (0 != vcd_client_manager_unset()) {
1357                 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Fail to delete client");
1358         }
1359
1360         if (0 == vcd_client_get_ref_count()) {
1361                 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Connected client list is empty");
1362                 ecore_timer_add(0, __finalize_quit_ecore_loop, NULL);
1363         }
1364
1365         SLOG(LOG_DEBUG, TAG_VCD, "[Server Success] Manager Finalize : pid(%d)", pid);
1366
1367         return VCD_ERROR_NONE;
1368 }
1369
1370 int vcd_server_mgr_set_command(int pid)
1371 {
1372         if (0 != vcd_client_manager_set_command(pid)) {
1373                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] The manager pid(%d) is NOT valid", pid);
1374                 return VCD_ERROR_INVALID_PARAMETER;
1375         }
1376         return VCD_ERROR_NONE;
1377 }
1378
1379 int vcd_server_mgr_unset_command(int pid)
1380 {
1381         if (0 != vcd_client_manager_unset_command(pid)) {
1382                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] The manager pid(%d) is NOT valid", pid);
1383                 return VCD_ERROR_INVALID_PARAMETER;
1384         }
1385         return VCD_ERROR_NONE;
1386 }
1387
1388 int vcd_server_mgr_set_demandable_client(int pid)
1389 {
1390         /* check if pid is valid */
1391         if (false == vcd_client_manager_is_valid(pid)) {
1392                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] The manager pid(%d) is NOT valid", pid);
1393                 return VCD_ERROR_INVALID_PARAMETER;
1394         }
1395
1396         GSList* client_list = NULL;
1397         if (0 != vc_info_parser_get_demandable_clients(&client_list)) {
1398                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to get demandable client");
1399                 return VCD_ERROR_OPERATION_FAILED;
1400         }
1401
1402         /* Save client list */
1403         if (0 != vcd_client_manager_set_demandable_client(pid, client_list)) {
1404                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to set demandable client list");
1405                 return VCD_ERROR_OPERATION_FAILED;
1406         }
1407
1408         return VCD_ERROR_NONE;
1409 }
1410
1411 int vcd_server_mgr_set_audio_type(int pid, const char* audio_type)
1412 {
1413         /* check if pid is valid */
1414         if (false == vcd_client_manager_is_valid(pid)) {
1415                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] The manager pid(%d) is NOT valid", pid);
1416                 return VCD_ERROR_INVALID_PARAMETER;
1417         }
1418
1419         int ret = 0;
1420         vcp_audio_type_e type = VCP_AUDIO_TYPE_PCM_S16_LE;
1421         int rate = 16000;
1422         int channel = 1;
1423
1424         ret = vcd_engine_get_audio_format(audio_type, &type, &rate, &channel);
1425         if (0 != ret) {
1426                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to get audio format : %d", ret);
1427                 return VCD_ERROR_OPERATION_FAILED;
1428         }
1429
1430         ret = vcd_recorder_set(audio_type, type, rate, channel);
1431         if (0 != ret) {
1432                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to set audio in type : %d", ret);
1433                 return VCD_ERROR_OPERATION_FAILED;
1434         }
1435
1436         return VCD_ERROR_NONE;
1437 }
1438
1439 int vcd_server_mgr_get_audio_type(int pid, char** audio_type)
1440 {
1441         /* check if pid is valid */
1442         if (false == vcd_client_manager_is_valid(pid)) {
1443                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] The manager pid(%d) is NOT valid", pid);
1444                 return VCD_ERROR_INVALID_PARAMETER;
1445         }
1446
1447         int ret = vcd_recorder_get(audio_type);
1448         if (0 != ret) {
1449                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to get audio in type : %d", ret);
1450                 return VCD_ERROR_OPERATION_FAILED;
1451         }
1452
1453         return VCD_ERROR_NONE;
1454 }
1455
1456 int vcd_server_mgr_set_client_info(int pid)
1457 {
1458         /* check if pid is valid */
1459         if (false == vcd_client_manager_is_valid(pid)) {
1460                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] The manager pid(%d) is NOT valid", pid);
1461                 return VCD_ERROR_INVALID_PARAMETER;
1462         }
1463
1464         int ret = vcd_client_save_client_info();
1465         if (0 != ret) {
1466                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to save client info : %d", ret);
1467                 return VCD_ERROR_OPERATION_FAILED;
1468         }
1469
1470         return VCD_ERROR_NONE;
1471 }
1472
1473 static int __start_internal_recognition()
1474 {
1475         int ret;
1476
1477         if (0 != vcd_client_command_collect_command()) {
1478                 SLOG(LOG_ERROR, TAG_VCD, "[Client Data ERROR] Fail to collect command");
1479                 /* Send error cb to manager */
1480                 int pid = vcd_client_widget_get_foreground_pid();
1481                 if (-1 != pid)
1482                         vcdc_send_error_signal_to_manager(vcd_client_manager_get_pid(), VCD_ERROR_OPERATION_FAILED, "voice_engine.error.proc_fail");
1483                 return VCD_ERROR_OPERATION_FAILED;
1484         }
1485
1486         /* 3. Set command to engine */
1487         ret = vcd_engine_set_commands();
1488         if (0 != ret) {
1489                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to set commands : %d", ret);
1490                 /* Send error cb to manager */
1491                 int pid = vcd_client_widget_get_foreground_pid();
1492                 if (-1 != pid)
1493                         vcdc_send_error_signal_to_manager(vcd_client_manager_get_pid(), VCD_ERROR_OPERATION_FAILED, "voice_engine.error.proc_fail");
1494                 return VCD_ERROR_OPERATION_FAILED;
1495         }
1496
1497         SLOG(LOG_DEBUG, TAG_VCD, "[Server Success] Set command");
1498
1499         bool stop_by_silence = true;
1500         if (VCD_RECOGNITION_MODE_MANUAL == vcd_client_get_recognition_mode()) {
1501                 stop_by_silence = false;
1502         }
1503
1504         vcd_client_manager_set_result_text(NULL);
1505
1506         /* 4. start recognition */
1507         ret = vcd_engine_recognize_start(stop_by_silence);
1508         if (0 != ret) {
1509                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to start recognition : result(%d)", ret);
1510                 /* Send error cb to manager */
1511                 int pid = vcd_client_widget_get_foreground_pid();
1512                 if (-1 != pid)
1513                         vcdc_send_error_signal_to_manager(vcd_client_manager_get_pid(), VCD_ERROR_OPERATION_FAILED, "voice_engine.error.proc_fail");
1514                 return VCD_ERROR_OPERATION_FAILED;
1515         }
1516
1517         SLOG(LOG_DEBUG, TAG_VCD, "[Server Success] Start engine");
1518
1519 #if 1
1520         /* 5. recorder start */
1521         ret = vcd_recorder_start();
1522         if (0 != ret) {
1523                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to start recorder : result(%d)", ret);
1524                 vcd_engine_recognize_cancel();
1525                 /* Send error cb to manager */
1526                 int pid = vcd_client_widget_get_foreground_pid();
1527                 if (-1 != pid)
1528                         vcdc_send_error_signal_to_manager(vcd_client_manager_get_pid(), VCD_ERROR_OPERATION_FAILED, "voice_engine.error.proc_fail");
1529                 return ret;
1530         }
1531 #endif
1532
1533         vcd_config_set_service_state(VCD_STATE_RECORDING);
1534         vcdc_send_service_state(VCD_STATE_RECORDING);
1535
1536         SLOG(LOG_DEBUG, TAG_VCD, "[Server Success] Start recognition(%d)", stop_by_silence);
1537
1538         return 0;
1539 }
1540
1541 static Eina_Bool __vcd_request_show_tooltip(void *data)
1542 {
1543         int pid = vcd_client_widget_get_foreground_pid();
1544         if (-1 != pid) {
1545                 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Request tooltip show and widget command");
1546                 vcdc_send_show_tooltip(pid, (bool)data);
1547         }
1548
1549         return EINA_FALSE;
1550 }
1551
1552 int vcd_server_mgr_start(vcd_recognition_mode_e recognition_mode, bool exclusive_cmd, bool start_by_client)
1553 {
1554         /* 1. check current state */
1555         vcd_state_e state = vcd_config_get_service_state();
1556
1557         if (VCD_STATE_READY != state) {
1558                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Current state is not ready");
1559                 return VCD_ERROR_INVALID_STATE;
1560         }
1561         if (-1 == vcd_client_manager_get_pid()) {
1562                 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Manager is NOT available.");
1563                 return VCD_ERROR_OPERATION_FAILED;
1564         }
1565
1566         SLOG(LOG_DEBUG, TAG_VCD, "[Server] set recognition mode = %d", recognition_mode);
1567         vcd_client_set_recognition_mode(recognition_mode);
1568
1569         if (false == exclusive_cmd) {
1570                 vcd_client_update_foreground_pid();
1571                 /* Notify show tooltip */
1572                 if (1 == vcd_config_get_command_type_enabled(VC_COMMAND_TYPE_WIDGET)) {
1573                         int pid = vcd_client_widget_get_foreground_pid();
1574                         if (-1 != pid) {
1575                                 SLOG(LOG_INFO, TAG_VCD, "[Server] Request tooltip show and widget command");
1576                                 ecore_timer_add(0, __vcd_request_show_tooltip, (void*)true);
1577                                 return 0;
1578                         }
1579                 }
1580         } else {
1581                 vcd_client_manager_set_exclusive(exclusive_cmd);
1582         }
1583
1584         int fg_pid = -1;
1585         if (true == start_by_client) {
1586                 /* Get foreground pid */
1587                 if (0 != vcd_config_get_foreground(&fg_pid)) {
1588                         SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to get foreground");
1589                 }
1590
1591                 /* Set client exclusive option */
1592                 if (0 != vcd_client_set_exclusive_command(fg_pid)) {
1593                         SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to set exclusive command");
1594                 }
1595         }
1596
1597         int ret = __start_internal_recognition();
1598         if (0 != ret) {
1599                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to start recongition : %d", ret);
1600                 return ret;
1601         }
1602
1603         if (true == start_by_client) {
1604                 vcd_client_unset_exclusive_command(fg_pid);
1605         }
1606
1607         return VCD_ERROR_NONE;
1608 }
1609
1610 int vcd_server_mgr_stop()
1611 {
1612         /* 1. Check current state is recording */
1613         if (VCD_STATE_RECORDING != vcd_config_get_service_state()) {
1614                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Current state is not recording");
1615                 return VCD_ERROR_INVALID_STATE;
1616         }
1617         if (-1 == vcd_client_manager_get_pid()) {
1618                 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Manager is NOT available.");
1619                 return VCD_ERROR_OPERATION_FAILED;
1620         }
1621
1622 #if 1
1623         /* 2. Stop recorder */
1624         vcd_recorder_stop();
1625 #endif
1626
1627         /* 3. Stop engine recognition */
1628         int ret = vcd_engine_recognize_stop();
1629         if (0 != ret) {
1630                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to stop recognition : %d", ret);
1631         }
1632
1633         /* 4. Set original mode */
1634         vcd_config_set_service_state(VCD_STATE_PROCESSING);
1635         vcdc_send_service_state(VCD_STATE_PROCESSING);
1636
1637         return VCD_ERROR_NONE;
1638 }
1639
1640 int vcd_server_mgr_cancel()
1641 {
1642         if (-1 == vcd_client_manager_get_pid()) {
1643                 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Manager is NOT available.");
1644                 return VCD_ERROR_OPERATION_FAILED;
1645         }
1646
1647         if (g_restart_timer != NULL) {
1648                 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Delete restart engine timer");
1649                 ecore_timer_del(g_restart_timer);
1650                 g_restart_timer = NULL;
1651         }
1652
1653         /* 1. Check current state */
1654         vcd_state_e state = vcd_config_get_service_state();
1655         if (VCD_STATE_READY == state) {
1656                 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Current state is READY");
1657                 vcd_recorder_stop();
1658                 return VCD_ERROR_NONE;
1659         }
1660
1661 #if 1
1662         /* 2. Stop recorder */
1663         vcd_recorder_stop();
1664 #endif
1665
1666         /* 3. Cancel engine */
1667         int ret = vcd_engine_recognize_cancel();
1668         if (0 != ret) {
1669                 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Fail to cancel : %d", ret);
1670         }
1671
1672         if (false == vcd_client_manager_get_exclusive()) {
1673                 if (1 == vcd_config_get_command_type_enabled(VC_COMMAND_TYPE_WIDGET)) {
1674                         int pid = vcd_client_widget_get_foreground_pid();
1675                         if (-1 != pid) {
1676                                 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Request tooltip hide");
1677                                 ecore_timer_add(0, __vcd_request_show_tooltip, (void*)false);
1678                         }
1679                 }
1680         } else {
1681                 vcd_client_manager_set_exclusive(false);
1682         }
1683
1684         /* 4. Set state */
1685         vcd_config_set_service_state(VCD_STATE_READY);
1686         vcdc_send_service_state(VCD_STATE_READY);
1687
1688         return VCD_ERROR_NONE;
1689 }
1690
1691
1692 int vcd_server_mgr_result_select()
1693 {
1694         __vcd_send_selected_result(NULL);
1695
1696         return VCD_ERROR_NONE;
1697 }
1698
1699 int vcd_server_mgr_set_domain(int pid, const char* domain)
1700 {
1701         /* check if pid is valid */
1702         if (false == vcd_client_manager_is_valid(pid)) {
1703                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] The manager pid(%d) is NOT valid", pid);
1704                 return VCD_ERROR_INVALID_PARAMETER;
1705         }
1706
1707         vcd_state_e state = vcd_config_get_service_state();
1708         if (VCD_STATE_READY != state) {
1709                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Current state is not ready");
1710                 return VCD_ERROR_INVALID_STATE;
1711         }
1712
1713         /* Set domain to engine */
1714         int ret = vcd_engine_set_domain(pid, domain);
1715         if (0 != ret) {
1716                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to set domain : %d", ret);
1717         } else {
1718                 SLOG(LOG_DEBUG, TAG_VCD, "[Server SUCCESS] Set domain");
1719         }
1720
1721         return ret;
1722 }
1723
1724 int vcd_server_mgr_set_private_data(int pid, const char* key, const char* data)
1725 {
1726         /* check if pid is valid */
1727         if (false == vcd_client_manager_is_valid(pid)) {
1728                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] The manager pid(%d) is NOT valid", pid);
1729                 return VCD_ERROR_INVALID_PARAMETER;
1730         }
1731
1732         vcd_state_e state = vcd_config_get_service_state();
1733         if (VCD_STATE_READY != state) {
1734                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Current state is not ready");
1735                 return VCD_ERROR_INVALID_STATE;
1736         }
1737
1738         /* Set private data to engine */
1739         int ret = vcd_engine_set_private_data(pid, key, data);
1740         if (0 != ret) {
1741                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to set private data : %d", ret);
1742         } else {
1743                 SLOG(LOG_DEBUG, TAG_VCD, "[Server SUCCESS] Set private data");
1744         }
1745
1746         return ret;
1747 }
1748
1749 int vcd_server_mgr_get_private_data(int pid, const char* key, char** data)
1750 {
1751         /* check if pid is valid */
1752         if (false == vcd_client_manager_is_valid(pid)) {
1753                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] The manager pid(%d) is NOT valid", pid);
1754                 return VCD_ERROR_INVALID_PARAMETER;
1755         }
1756         vcd_state_e state = vcd_config_get_service_state();
1757         if (VCD_STATE_READY != state) {
1758                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Current state is not ready");
1759                 return VCD_ERROR_INVALID_STATE;
1760         }
1761
1762         /* Get private data to engine */
1763         int ret = vcd_engine_get_private_data(pid, key, data);
1764         if (0 != ret) {
1765                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to get private data : %d", ret);
1766         } else {
1767                 SLOG(LOG_DEBUG, TAG_VCD, "[Server SUCCESS] Set private data");
1768         }
1769
1770         return ret;
1771 }
1772
1773 int vcd_server_mgr_do_action(int pid, int type, const char* action)
1774 {
1775         int ret = -1;
1776
1777         /* check if pid is valid */
1778         if (false == vcd_client_manager_is_valid(pid)) {
1779                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] The manager pid(%d) is NOT valid", pid);
1780                 return VCD_ERROR_INVALID_PARAMETER;
1781         }
1782
1783         vcd_state_e state = vcd_config_get_service_state();
1784         if (VCD_STATE_READY != state) {
1785                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Current state is not ready");
1786                 return VCD_ERROR_INVALID_STATE;
1787         }
1788
1789         /* Reqeust do action to engine */
1790         if (VCD_SEND_EVENT_TYPE_TEXT == type)
1791                 ret = vcd_engine_process_text(pid, action);
1792         else if (VCD_SEND_EVENT_TYPE_LIST_EVENT == type)
1793                 ret = vcd_engine_process_list_event(pid, action);
1794         else if (VCD_SEND_EVENT_TYPE_HAPTIC_EVENT == type)
1795                 ret = vcd_engine_process_haptic_event(pid, action);
1796
1797         if (0 != ret) {
1798                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to process do action : %d", ret);
1799         } else {
1800                 vcd_config_set_service_state(VCD_STATE_PROCESSING);
1801                 vcdc_send_service_state(VCD_STATE_PROCESSING);
1802                 SLOG(LOG_DEBUG, TAG_VCD, "[Server SUCCESS] Process do action");
1803         }
1804
1805         return ret;
1806 }
1807
1808 int vcd_server_mgr_enable_command_type(int pid, int cmd_type)
1809 {
1810         int ret = -1;
1811
1812         /* check if pid is valid */
1813         if (false == vcd_client_manager_is_valid(pid)) {
1814                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] The manager pid(%d) is NOT valid", pid);
1815                 return VCD_ERROR_INVALID_PARAMETER;
1816         }
1817
1818         vcd_state_e state = vcd_config_get_service_state();
1819         if (VCD_STATE_READY != state) {
1820                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Current state is not ready");
1821                 return VCD_ERROR_INVALID_STATE;
1822         }
1823
1824         ret = vcd_config_enable_command_type(cmd_type);
1825         if (0 != ret) {
1826                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to enable command type");
1827         } else {
1828                 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Enable command type(%d)", cmd_type);
1829         }
1830
1831         return ret;
1832 }
1833
1834 int vcd_server_mgr_disable_command_type(int pid, int cmd_type)
1835 {
1836         int ret = -1;
1837
1838         /* check if pid is valid */
1839         if (false == vcd_client_manager_is_valid(pid)) {
1840                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] The manager pid(%d) is NOT valid", pid);
1841                 return VCD_ERROR_INVALID_PARAMETER;
1842         }
1843
1844         vcd_state_e state = vcd_config_get_service_state();
1845         if (VCD_STATE_READY != state) {
1846                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Current state is not ready");
1847                 return VCD_ERROR_INVALID_STATE;
1848         }
1849
1850         ret = vcd_config_disable_command_type(cmd_type);
1851         if (0 != ret) {
1852                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to disable command type");
1853         } else {
1854                 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Disable command type(%d)", cmd_type);
1855         }
1856
1857         return ret;
1858 }
1859
1860 /*
1861 * VC Server Functions for Client
1862 */
1863 int vcd_server_initialize(int pid)
1864 {
1865         if (false == g_is_engine) {
1866                 if (0 != vcd_engine_agent_initialize_current_engine()) {
1867                         SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] No Engine");
1868                         g_is_engine = false;
1869                         return VCD_ERROR_ENGINE_NOT_FOUND;
1870                 } else {
1871                         g_is_engine = true;
1872                 }
1873         }
1874
1875         if (false == vcd_engine_is_available_engine()) {
1876                 if (0 != vcd_engine_agent_initialize_current_engine()) {
1877                         SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] No Engine");
1878                         return VCD_ERROR_ENGINE_NOT_FOUND;
1879                 }
1880         }
1881
1882         /* check if pid is valid */
1883         if (true == vcd_client_is_available(pid)) {
1884                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] The pid is already exist");
1885                 return VCD_ERROR_INVALID_PARAMETER;
1886         }
1887
1888         /* Add client information to client manager */
1889         if (0 != vcd_client_add(pid)) {
1890                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to add client info");
1891                 return VCD_ERROR_OPERATION_FAILED;
1892         }
1893
1894         SLOG(LOG_DEBUG, TAG_VCD, "[Server Success] Client Initialize : pid(%d)", pid);
1895
1896         return VCD_ERROR_NONE;
1897 }
1898
1899 int vcd_server_finalize(int pid)
1900 {
1901         /* check if pid is valid */
1902         if (false == vcd_client_is_available(pid)) {
1903                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] pid is NOT valid ");
1904                 return VCD_ERROR_INVALID_PARAMETER;
1905         }
1906
1907         /* Remove client information */
1908         if (0 != vcd_client_delete(pid)) {
1909                 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Fail to delete client");
1910         }
1911
1912         if (0 == vcd_client_get_ref_count()) {
1913                 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Connected client list is empty");
1914                 ecore_timer_add(0, __finalize_quit_ecore_loop, NULL);
1915         }
1916
1917         SLOG(LOG_DEBUG, TAG_VCD, "[Server Success] Client Finalize : pid(%d)", pid);
1918
1919         return VCD_ERROR_NONE;
1920 }
1921
1922 int vcd_server_set_command(int pid, vc_cmd_type_e cmd_type)
1923 {
1924         /* check if pid is valid */
1925         if (false == vcd_client_is_available(pid)) {
1926                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] pid is NOT valid ");
1927                 return VCD_ERROR_INVALID_PARAMETER;
1928         }
1929
1930         if (0 != vcd_client_set_command_type(pid, cmd_type)) {
1931                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to set command type : pid(%d), cmd_type(%d)", pid, cmd_type);
1932                 return VCD_ERROR_OPERATION_FAILED;
1933         }
1934
1935         return 0;
1936 }
1937
1938 int vcd_server_unset_command(int pid, vc_cmd_type_e cmd_type)
1939 {
1940         /* check if pid is valid */
1941         if (false == vcd_client_is_available(pid)) {
1942                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] pid is NOT valid ");
1943                 return VCD_ERROR_INVALID_PARAMETER;
1944         }
1945
1946         if (0 != vcd_client_unset_command_type(pid, cmd_type)) {
1947                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to unset command type : pid(%d), cmd_type(%d)", pid, cmd_type);
1948                 return VCD_ERROR_OPERATION_FAILED;
1949         }
1950
1951         return 0;
1952 }
1953
1954 int vcd_server_set_foreground(int pid, bool value)
1955 {
1956         /* check if pid is valid */
1957         if (false == vcd_client_is_available(pid) && false == vcd_client_widget_is_available(pid)) {
1958                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] pid is NOT valid ");
1959                 return VCD_ERROR_INVALID_PARAMETER;
1960         }
1961
1962         if (0 != vcd_config_set_foreground(pid, value)) {
1963                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to set foreground : pid(%d), value(%d)", pid, value);
1964                 return VCD_ERROR_OPERATION_FAILED;
1965         }
1966
1967         return 0;
1968 }
1969
1970 static int __vcd_server_launch_manager_app()
1971 {
1972         int ret = -1;
1973         bool running = false;
1974         char* appid = NULL;
1975
1976         ret = vcd_client_manager_get_appid(&appid);
1977         if (0 != ret || NULL == appid) {
1978                 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Fail to get manager appid");
1979                 return VCD_ERROR_OPERATION_FAILED;
1980         }
1981         ret = app_manager_is_running(appid, &running);
1982         if (0 != ret) {
1983                 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Fail to check running with appid(%s)", appid);
1984                 free(appid);
1985                 appid = NULL;
1986                 return VCD_ERROR_OPERATION_FAILED;
1987         }
1988         if (false == running) {
1989                 int tmp_ret = __vcd_is_package_installed(appid);
1990                 if (false == tmp_ret) {
1991                         SLOG(LOG_ERROR, TAG_VCD, "[ERROR] manager app is not installed, appid(%s)", appid);
1992                 } else {
1993                         ret = __vcd_activate_app_by_appcontrol(appid);
1994                         if (0 != ret) {
1995                                 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Fail to activate app");
1996                                 free(appid);
1997                                 appid = NULL;
1998                                 return ret;
1999                         }
2000                         SLOG(LOG_ERROR, TAG_VCD, "Launch vc manager app: appid(%s)", appid);
2001                 }
2002         } else {
2003                 ret = __vcd_resume_app(appid);
2004                 if (0 != ret) {
2005                         SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Fail to resume app");
2006                         free(appid);
2007                         appid = NULL;
2008                         return ret;
2009                 }
2010                 SLOG(LOG_ERROR, TAG_VCD, "Resume vc manager app: appid(%s)", appid);
2011         }
2012
2013         free(appid);
2014         appid = NULL;
2015
2016         return VCD_ERROR_NONE;
2017 }
2018
2019 int vcd_server_dialog(int pid, const char* disp_text, const char* utt_text, int continuous)
2020 {
2021         /* check if pid is valid */
2022         if (false == vcd_client_is_available(pid) && false == vcd_client_widget_is_available(pid)) {
2023                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] pid is NOT valid ");
2024                 return VCD_ERROR_INVALID_PARAMETER;
2025         }
2026
2027         int ret = __vcd_server_launch_manager_app();
2028         if (0 != ret) {
2029                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to send dialog : mgr_pid(%d), pid(%d), disp_text(%s), utt_text(%s), continue(%d)", vcd_client_manager_get_pid(), pid, disp_text, utt_text, continuous);
2030                 return ret;
2031         }
2032
2033         ret = vcdc_send_dialog(vcd_client_manager_get_pid(), pid, disp_text, utt_text, continuous);
2034         if (0 != ret) {
2035                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to send dialog : mgr_pid(%d), pid(%d), disp_text(%s), utt_text(%s), continue(%d)", vcd_client_manager_get_pid(), pid, disp_text, utt_text, continuous);
2036                 return ret;
2037         }
2038
2039         return 0;
2040 }
2041
2042 int vcd_server_is_system_command_valid(int pid, int* is_sys_cmd_valid)
2043 {
2044         /* check if pid is valid */
2045         if (false == vcd_client_is_available(pid) && false == vcd_client_widget_is_available(pid)) {
2046                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] pid is NOT valid ");
2047                 return VCD_ERROR_INVALID_PARAMETER;
2048         }
2049
2050         /* check if system command is valid */
2051         if (true == vcd_client_manager_is_system_command_valid(vcd_client_manager_get_pid()))
2052                 *is_sys_cmd_valid = true;
2053         else
2054                 *is_sys_cmd_valid = false;
2055
2056         return 0;
2057 }
2058
2059 #if 0
2060 int vcd_server_set_exclusive_command(int pid, bool value)
2061 {
2062         /* check if pid is valid */
2063         if (false == vcd_client_is_available(pid)) {
2064                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] pid is NOT valid ");
2065                 return VCD_ERROR_INVALID_PARAMETER;
2066         }
2067
2068         if (true == value) {
2069                 if (0 != vcd_client_set_exclusive_command(pid)) {
2070                         SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to set exclusive command : pid(%d)", pid);
2071                         return VCD_ERROR_OPERATION_FAILED;
2072                 }
2073         } else {
2074                 if (0 != vcd_client_unset_exclusive_command(pid)) {
2075                         SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to unset exclusive command : pid(%d)", pid);
2076                         return VCD_ERROR_OPERATION_FAILED;
2077                 }
2078         }
2079
2080         return 0;
2081 }
2082
2083 int vcd_server_request_start(int pid, bool stop_by_silence)
2084 {
2085         /* check if pid is valid */
2086         if (false == vcd_client_is_available(pid)) {
2087                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] pid(%d) is NOT forground client", pid);
2088                 return VCD_ERROR_INVALID_PARAMETER;
2089         }
2090
2091         int ret;
2092         /* Check current state */
2093         vcd_state_e state = vcd_config_get_service_state();
2094
2095         /* Service state should be ready */
2096         if (VCD_STATE_READY != state) {
2097                 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Current state is not Ready : pid(%d)", pid);
2098                 return VCD_ERROR_INVALID_STATE;
2099         }
2100
2101         if (-1 != vcd_client_manager_get_pid()) {
2102                 /* Check current pid is valid */
2103                 if (false == vcd_client_manager_check_demandable_client(pid)) {
2104                         SLOG(LOG_DEBUG, TAG_VCD, "[Server] Current client is NOT available : pid(%d)", pid);
2105                         return VCD_ERROR_INVALID_PARAMETER;
2106                 }
2107         }
2108
2109         ret = vcd_server_mgr_start(stop_by_silence, false);
2110         if (0 != ret) {
2111                 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Fail to start recognition");
2112                 return VCD_ERROR_INVALID_PARAMETER;
2113         }
2114
2115         return 0;
2116 }
2117
2118 int vcd_server_request_stop(int pid)
2119 {
2120         int ret;
2121         /* Check current state */
2122         vcd_state_e state = vcd_config_get_service_state();
2123
2124         /* Service state should be ready */
2125         if (VCD_STATE_RECORDING != state) {
2126                 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Current state is not Recording : pid(%d)", pid);
2127                 return VCD_ERROR_INVALID_STATE;
2128         }
2129
2130         if (-1 != vcd_client_manager_get_pid()) {
2131                 /* Check current pid is valid */
2132                 if (false == vcd_client_manager_check_demandable_client(pid)) {
2133                         SLOG(LOG_DEBUG, TAG_VCD, "[Server] Current client is NOT available : pid(%d)", pid);
2134                         return VCD_ERROR_INVALID_PARAMETER;
2135                 }
2136         }
2137
2138         ret = vcd_server_mgr_stop();
2139         if (0 != ret) {
2140                 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Fail to start recognition");
2141                 return VCD_ERROR_OPERATION_FAILED;
2142         }
2143
2144         return VCD_ERROR_NONE;
2145 }
2146
2147 int vcd_server_request_cancel(int pid)
2148 {
2149         int ret;
2150         /* Check current state */
2151         vcd_state_e state = vcd_config_get_service_state();
2152
2153         /* Service state should be recording or processing */
2154         if (VCD_STATE_RECORDING != state && VCD_STATE_PROCESSING != state) {
2155                 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Current state is not Recording or Processing : pid(%d)", pid);
2156                 return VCD_ERROR_INVALID_STATE;
2157         }
2158
2159         if (-1 != vcd_client_manager_get_pid()) {
2160                 /* Check current pid is valid */
2161                 if (false == vcd_client_manager_check_demandable_client(pid)) {
2162                         SLOG(LOG_DEBUG, TAG_VCD, "[Server] Current client is NOT available : pid(%d)", pid);
2163                         return VCD_ERROR_INVALID_PARAMETER;
2164                 }
2165         }
2166
2167         ret = vcd_server_mgr_cancel();
2168         if (0 != ret) {
2169                 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Fail to start recognition");
2170                 return VCD_ERROR_OPERATION_FAILED;
2171         }
2172
2173         return VCD_ERROR_NONE;
2174 }
2175 #endif
2176
2177 /*
2178 * VC Server Functions for Widget lib
2179 */
2180 int vcd_server_widget_initialize(int pid)
2181 {
2182         if (false == g_is_engine) {
2183                 if (0 != vcd_engine_agent_initialize_current_engine()) {
2184                         SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] No Engine");
2185                         g_is_engine = false;
2186                         return VCD_ERROR_ENGINE_NOT_FOUND;
2187                 } else {
2188                         g_is_engine = true;
2189                 }
2190         }
2191
2192         if (false == vcd_engine_is_available_engine()) {
2193                 if (0 != vcd_engine_agent_initialize_current_engine()) {
2194                         SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] No Engine");
2195                         return VCD_ERROR_ENGINE_NOT_FOUND;
2196                 }
2197         }
2198
2199         /* check if pid is valid */
2200         if (true == vcd_client_widget_is_available(pid)) {
2201                 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] The pid is already exist");
2202                 return VCD_ERROR_NONE;
2203         }
2204
2205         /* Add client information to client manager */
2206         if (0 != vcd_client_widget_add(pid)) {
2207                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to add client info");
2208                 return VCD_ERROR_OPERATION_FAILED;
2209         }
2210
2211         SLOG(LOG_DEBUG, TAG_VCD, "[Server Success] Initialize widget : pid(%d)", pid);
2212
2213         return VCD_ERROR_NONE;
2214 }
2215
2216 int vcd_server_widget_finalize(int pid)
2217 {
2218         /* check if pid is valid */
2219         if (false == vcd_client_widget_is_available(pid)) {
2220                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] pid is NOT valid ");
2221                 return VCD_ERROR_INVALID_PARAMETER;
2222         }
2223
2224         /* Remove client information */
2225         if (0 != vcd_client_widget_delete(pid)) {
2226                 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Fail to delete client");
2227         }
2228
2229         if (0 == vcd_client_get_ref_count()) {
2230                 SLOG(LOG_DEBUG, TAG_VCD, "[Server] connected client list is empty");
2231                 ecore_timer_add(0, __finalize_quit_ecore_loop, NULL);
2232         }
2233
2234         return VCD_ERROR_NONE;
2235 }
2236
2237 int vcd_server_widget_start_recording(int pid, bool widget_command)
2238 {
2239         /* check if pid is valid */
2240         if (false == vcd_client_widget_is_available(pid)) {
2241                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] pid is NOT valid ");
2242                 return VCD_ERROR_INVALID_PARAMETER;
2243         }
2244
2245         if (true == widget_command) {
2246                 vcd_client_widget_set_command(pid);
2247                 SLOG(LOG_DEBUG, TAG_VCD, "[Server] widget command is available");
2248         } else {
2249                 vcd_client_widget_unset_command(pid);
2250                 SLOG(LOG_WARN, TAG_VCD, "[Server] widget command is NOT available");
2251         }
2252
2253         int ret = __start_internal_recognition();
2254         if (0 != ret) {
2255                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to start recongition : %d", ret);
2256                 ecore_timer_add(0, __vcd_request_show_tooltip, (void*)false);
2257         }
2258
2259         return 0;
2260 }
2261
2262 int vcd_server_widget_start(int pid, bool stop_by_silence)
2263 {
2264         /* check if pid is valid */
2265         int fore_pid = vcd_client_widget_get_foreground_pid();
2266         if (pid != fore_pid) {
2267                 SLOG(LOG_ERROR, TAG_VCD, "[Server] pid is NOT foreground");
2268                 return VCD_ERROR_INVALID_PARAMETER;
2269         }
2270
2271         /* Check current state */
2272         vcd_state_e state = vcd_config_get_service_state();
2273
2274         /* Service state should be ready */
2275         if (VCD_STATE_READY != state) {
2276                 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Current state is not Ready : pid(%d)", pid);
2277                 return VCD_ERROR_INVALID_STATE;
2278         }
2279
2280         vcd_client_set_slience_detection(stop_by_silence);
2281
2282         /* Notify show tooltip */
2283         ecore_timer_add(0, __vcd_request_show_tooltip, (void*)true);
2284
2285         return 0;
2286 }
2287
2288 int vcd_server_widget_stop(int pid)
2289 {
2290         /* check if pid is valid */
2291         int fore_pid = vcd_client_widget_get_foreground_pid();
2292         if (pid != fore_pid) {
2293                 SLOG(LOG_ERROR, TAG_VCD, "[Server] pid is NOT foreground");
2294                 return VCD_ERROR_INVALID_PARAMETER;
2295         }
2296
2297         int ret;
2298         /* Check current state */
2299         vcd_state_e state = vcd_config_get_service_state();
2300
2301         /* Service state should be recording */
2302         if (VCD_STATE_RECORDING != state) {
2303                 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Current service state is not Recording : pid(%d)", pid);
2304                 return VCD_ERROR_INVALID_STATE;
2305         }
2306
2307         ret = vcd_server_mgr_stop();
2308         if (0 != ret) {
2309                 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Fail to start recognition");
2310                 return VCD_ERROR_OPERATION_FAILED;
2311         }
2312
2313         return VCD_ERROR_NONE;
2314 }
2315
2316 int vcd_server_widget_cancel(int pid)
2317 {
2318         /* check if pid is valid */
2319         int fore_pid = vcd_client_widget_get_foreground_pid();
2320         if (pid != fore_pid) {
2321                 SLOG(LOG_ERROR, TAG_VCD, "[Server] pid is NOT foreground");
2322                 return VCD_ERROR_INVALID_PARAMETER;
2323         }
2324
2325         int ret;
2326         /* Check current state */
2327         vcd_state_e state = vcd_config_get_service_state();
2328
2329         /* Service state should be recording or processing */
2330         if (VCD_STATE_RECORDING != state && VCD_STATE_PROCESSING != state) {
2331                 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Current state is not Recording or Processing : pid(%d)", pid);
2332                 return VCD_ERROR_INVALID_STATE;
2333         }
2334
2335         ret = vcd_server_mgr_cancel();
2336         if (0 != ret) {
2337                 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Fail to cancel recognition : %d", ret);
2338                 return ret;
2339         }
2340
2341         return VCD_ERROR_NONE;
2342 }
2343
2344 int vcd_server_widget_enable_asr_result(int pid, bool enable)
2345 {
2346         int ret;
2347         ret = vcd_client_widget_set_asr_result_enabled(pid, enable);
2348         if (0 != ret) {
2349                 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Fail to enable asr result : %d", ret);
2350         }
2351
2352         return ret;
2353 }