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