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