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