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