Merge "Fix log level of 'vcdb' and 'vcd' 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         struct dirent *dirp = NULL;
872
873         dp = opendir(VC_RUNTIME_INFO_ROOT);
874         if (dp == NULL) {
875                 SLOG(LOG_ERROR, TAG_VCD, "[File message WARN] Fail to open path : %s", VC_RUNTIME_INFO_ROOT);
876                 return;
877         }
878
879         char remove_path[256] = {0, };
880         do {
881                 dirp = readdir(dp);
882
883                 if (NULL != dirp) {
884                         if (!strncmp("vc_", dirp->d_name, strlen("vc_"))) {
885                                 memset(remove_path, 0, 256);
886                                 snprintf(remove_path, 256, "%s/%s", VC_RUNTIME_INFO_ROOT, dirp->d_name);
887
888                                 /* Clean up code */
889                                 if (0 != remove(remove_path)) {
890                                         SLOG(LOG_WARN, TAG_VCD, "[File message WARN] Fail to remove file : %s", remove_path);
891                                 } else {
892                                         SLOG(LOG_DEBUG, TAG_VCD, "[File message] Remove file : %s", remove_path);
893                                 }
894                         }
895                 }
896         } while (NULL != dirp);
897
898         closedir(dp);
899
900         return;
901 }
902
903 static int __vcd_db_clean_up()
904 {
905         int ret = 0;
906         int cnt = VC_COMMAND_TYPE_FOREGROUND;
907         do {
908                 if (VC_COMMAND_TYPE_BACKGROUND != cnt)
909                         ret = vc_db_delete_commands(-1, cnt, NULL);
910         } while (VC_COMMAND_TYPE_EXCLUSIVE >= ++cnt);
911
912         return ret;
913 }
914
915 int vcd_initialize()
916 {
917         int ret = 0;
918
919         /* Remove old file */
920         __vcd_file_clean_up();
921
922         /* initialize modules */
923         ret = vcd_config_initialize(__config_lang_changed_cb, __config_foreground_changed_cb, NULL);
924         if (0 != ret) {
925                 SLOG(LOG_ERROR, TAG_VCD, "[Server WARNING] Fail to initialize config.");
926         }
927
928         /* Remove db data */
929         ret = __vcd_db_clean_up();
930         if (0 != ret) {
931                 SLOG(LOG_ERROR, TAG_VCD, "[Server WARNING] Fail to remove db data");
932         }
933
934         vcd_config_set_service_state(VCD_STATE_NONE);
935
936         //ret = vcd_engine_agent_init(__vcd_server_pre_result_cb, __vcd_server_result_cb, __vcd_server_nlu_result_cb, __vcd_server_error_cb);
937         ret = vcd_engine_agent_init(__vcd_server_asr_result_cb, __vcd_server_result_cb, __vcd_server_error_cb);
938         if (0 != ret) {
939                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to engine agent initialize : result(%d)", ret);
940                 return ret;
941         }
942
943         if (0 != vcd_recorder_create(__server_recorder_callback, __server_recorder_interrupt_callback)) {
944                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to create recorder");
945                 return VCD_ERROR_OPERATION_FAILED;
946         }
947
948         /* Find engine */
949         ret = vcd_engine_agent_initialize_current_engine();
950         if (0 != ret) {
951                 if (VCD_ERROR_ENGINE_NOT_FOUND == ret)
952                         SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] There is No Voice control engine");
953                 else
954                         SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to init engine");
955
956                 g_is_engine = false;
957         } else {
958                 g_is_engine = true;
959         }
960
961         /* Load engine */
962         if (0 != vcd_engine_agent_load_current_engine()) {
963                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to load current engine");
964                 return VCD_ERROR_OPERATION_FAILED;
965         }
966
967         /* Initialize manager info */
968         vcd_client_manager_unset();
969
970         vcd_config_set_service_state(VCD_STATE_READY);
971         vcdc_send_service_state(VCD_STATE_READY);
972
973         SLOG(LOG_DEBUG, TAG_VCD, "[Server SUCCESS] initialize");
974
975         return 0;
976 }
977
978 void vcd_finalize()
979 {
980         GList *iter = NULL;
981         if (0 < g_list_length(g_proc_list)) {
982                 iter = g_list_first(g_proc_list);
983                 while (NULL != iter) {
984                         g_proc_list = g_list_remove_link(g_proc_list, iter);
985                         iter = g_list_first(g_proc_list);
986                 }
987         }
988
989         if (g_restart_timer != NULL) {
990                 ecore_timer_del(g_restart_timer);
991                 g_restart_timer = NULL;
992         }
993
994         vcd_state_e state = vcd_config_get_service_state();
995         if (VCD_STATE_READY != state) {
996                 if (VCD_STATE_RECORDING == state) {
997                         vcd_recorder_stop();
998                 }
999                 vcd_engine_recognize_cancel();
1000         }
1001         if (0 != vcd_recorder_destroy()) {
1002                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to destroy recorder");
1003         } else {
1004                 SLOG(LOG_DEBUG, TAG_VCD, "[Server] destroy recorder");
1005         }
1006
1007         if (0 != vcd_engine_agent_release()) {
1008                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to release engine");
1009         } else {
1010                 SLOG(LOG_DEBUG, TAG_VCD, "[Server] release engine");
1011         }
1012
1013         vcd_client_manager_unset_appid();
1014
1015         vcd_config_set_service_state(VCD_STATE_NONE);
1016         vcdc_send_service_state(VCD_STATE_NONE);
1017
1018         SLOG(LOG_DEBUG, TAG_VCD, "[Server] mode finalize");
1019
1020         return;
1021 }
1022
1023 static Eina_Bool __finalize_quit_ecore_loop(void *data)
1024 {
1025         SLOG(LOG_DEBUG, TAG_VCD, "[Server] quit ecore main loop");
1026         ecore_main_loop_quit();
1027         return EINA_FALSE;
1028 }
1029
1030 static void __read_proc()
1031 {
1032         DIR *dp = NULL;
1033         struct dirent *dirp = NULL;
1034         int tmp;
1035
1036         GList *iter = NULL;
1037         if (0 < g_list_length(g_proc_list)) {
1038                 iter = g_list_first(g_proc_list);
1039                 while (NULL != iter) {
1040                         g_proc_list = g_list_remove_link(g_proc_list, iter);
1041                         iter = g_list_first(g_proc_list);
1042                 }
1043         }
1044
1045         dp = opendir("/proc");
1046         if (NULL == dp) {
1047                 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Fail to open proc");
1048         } else {
1049                 do {
1050                         dirp = readdir(dp);
1051
1052                         if (NULL != dirp) {
1053                                 tmp = atoi(dirp->d_name);
1054                                 if (0 >= tmp)   continue;
1055                                 g_proc_list = g_list_append(g_proc_list, GINT_TO_POINTER(tmp));
1056                         }
1057                 } while (NULL != dirp);
1058                 closedir(dp);
1059         }
1060         return;
1061 }
1062
1063 static void __vcd_cleanup_client(vcd_client_type_e type)
1064 {
1065         int* client_list = NULL;
1066         int client_count = 0;
1067         int i = 0;
1068         int j = 0;
1069         bool exist = false;
1070         int mgr_pid = -1;
1071         int ret = -1;
1072
1073         if (VCD_CLIENT_TYPE_NORMAL == type) {
1074                 ret = vcd_client_get_list(&client_list, &client_count);
1075         } else if (VCD_CLIENT_TYPE_WIDGET == type) {
1076                 ret = vcd_client_widget_get_list(&client_list, &client_count);
1077         } else if (VCD_CLIENT_TYPE_MANAGER == type) {
1078                 mgr_pid = vcd_client_manager_get_pid();
1079                 client_list = &mgr_pid;
1080                 client_count = 1;
1081                 if (-1 == mgr_pid) {
1082                         SLOG(LOG_WARN, TAG_VCD, "[WARNING] Invalid Manager pid");
1083                         return;
1084                 }
1085         }
1086
1087         if (0 == ret || mgr_pid > 0) {
1088                 SLOG(LOG_DEBUG, TAG_VCD, "===== Clean up %s client ", type ? (type == 1) ? "Widget" : "Manager" : "Normal");
1089                 if (NULL != client_list && client_count > 0) {
1090                         for (i = 0; i < client_count; i++) {
1091                                 exist = false;
1092                                 GList *iter = NULL;
1093                                 for (j = 0; j < g_list_length(g_proc_list); j++) {
1094                                         iter = g_list_nth(g_proc_list, j);
1095                                         if (NULL != iter) {
1096                                                 if (client_list[i] == GPOINTER_TO_INT(iter->data)) {
1097                                                         SLOG(LOG_DEBUG, TAG_VCD, "%s pid(%d) is running", type ? (type == 1) ? "Widget" : "Manager" : "Normal", client_list[i]);
1098                                                         exist = true;
1099                                                         break;
1100                                                 }
1101                                         }
1102                                 }
1103
1104                                 if (false == exist) {
1105                                         SLOG(LOG_ERROR, TAG_VCD, "%s pid(%d) should be removed", type ? (type == 1) ? "Widget" : "Manager" : "Normal", client_list[i]);
1106                                         if (VCD_CLIENT_TYPE_NORMAL == type)
1107                                                 vcd_server_finalize(client_list[i]);
1108                                         else if (VCD_CLIENT_TYPE_WIDGET == type)
1109                                                 vcd_server_widget_finalize(client_list[i]);
1110                                         else
1111                                                 vcd_server_mgr_finalize(mgr_pid);
1112                                 }
1113                         }
1114                 }
1115                 SLOG(LOG_DEBUG, TAG_VCD, "=====");
1116                 SLOG(LOG_DEBUG, TAG_VCD, "  ");
1117         }
1118         if (NULL != client_list && -1 == mgr_pid) {
1119                 free(client_list);
1120                 client_list = NULL;
1121         }
1122         return;
1123 }
1124
1125 Eina_Bool vcd_cleanup_client_all(void *data)
1126 {
1127         __read_proc();
1128         
1129         __vcd_cleanup_client(VCD_CLIENT_TYPE_NORMAL);
1130         __vcd_cleanup_client(VCD_CLIENT_TYPE_WIDGET);
1131         __vcd_cleanup_client(VCD_CLIENT_TYPE_MANAGER);
1132
1133 #if 0
1134         if (0 == vcd_client_get_list(&client_list, &client_count)) {
1135                 SLOG(LOG_DEBUG, TAG_VCD, "===== Clean up client ");
1136                 if (NULL != client_list && client_count > 0) {
1137                         for (i = 0; i < client_count; i++) {
1138                                 exist = false;
1139                                 GList *iter = NULL;
1140                                 for (j = 0; j < g_list_length(g_proc_list); j++) {
1141                                         iter = g_list_nth(g_proc_list, j);
1142                                         if (NULL != iter) {
1143                                                 if (client_list[i] == GPOINTER_TO_INT(iter->data)) {
1144                                                         SLOG(LOG_DEBUG, TAG_VCD, "pid(%d) is running", client_list[i]);
1145                                                         exist = true;
1146                                                         break;
1147                                                 }
1148                                         }
1149                                 }
1150
1151                                 if (false == exist) {
1152                                         SLOG(LOG_ERROR, TAG_VCD, "pid(%d) should be removed", client_list[i]);
1153                                         vcd_server_finalize(client_list[i]);
1154                                 }
1155 #if 0
1156                                 result = vcdc_send_hello(client_list[i], VCD_CLIENT_TYPE_NORMAL);
1157
1158                                 if (0 == result) {
1159                                         SLOG(LOG_DEBUG, TAG_VCD, "[Server] pid(%d) should be removed.", client_list[i]);
1160                                         vcd_server_finalize(client_list[i]);
1161                                 } else if (-1 == result) {
1162                                         SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Hello result has error");
1163                                 }
1164 #endif
1165                         }
1166                 }
1167                 SLOG(LOG_DEBUG, TAG_VCD, "=====");
1168                 SLOG(LOG_DEBUG, TAG_VCD, "  ");
1169         }
1170         if (NULL != client_list) {
1171                 free(client_list);
1172                 client_list = NULL;
1173         }
1174
1175         /* If app is in background state, app cannot response message. */
1176         if (0 == vcd_client_widget_get_list(&client_list, &client_count)) {
1177                 SLOG(LOG_DEBUG, TAG_VCD, "===== Clean up widget");
1178                 if (NULL != client_list && client_count > 0) {
1179                         for (i = 0; i < client_count; i++) {
1180                                 exist = false;
1181                                 GList *iter = NULL;
1182                                 for (j = 0; j < g_list_length(g_proc_list); j++) {
1183                                         iter = g_list_nth(g_proc_list, j);
1184                                         if (NULL != iter) {
1185                                                 if (client_list[i] == GPOINTER_TO_INT(iter->data)) {
1186                                                         SLOG(LOG_DEBUG, TAG_VCD, "widget pid(%d) is running", client_list[i]);
1187                                                         exist = true;
1188                                                         break;
1189                                                 }
1190                                         }
1191                                 }
1192
1193                                 if (false == exist) {
1194                                         SLOG(LOG_ERROR, TAG_VCD, "widget pid(%d) should be removed", client_list[i]);
1195                                         vcd_server_widget_finalize(client_list[i]);
1196                                 }
1197 #if 0
1198                                 result = vcdc_send_hello(client_list[i], VCD_CLIENT_TYPE_WIDGET);
1199
1200                                 if (0 == result) {
1201                                         SLOG(LOG_DEBUG, TAG_VCD, "[Server] widget pid(%d) should be removed.", client_list[i]);
1202                                         vcd_server_widget_finalize(client_list[i]);
1203                                 } else if (-1 == result) {
1204                                         SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Hello result has error");
1205                                 }
1206 #endif
1207                         }
1208                 }
1209                 SLOG(LOG_DEBUG, TAG_VCD, "=====");
1210                 SLOG(LOG_DEBUG, TAG_VCD, "  ");
1211         }
1212
1213         if (NULL != client_list) {
1214                 free(client_list);
1215                 client_list = NULL;
1216         }
1217
1218         /* manager */
1219         exist = false;
1220         GList *iter = NULL;
1221         int mgr_pid = vcd_client_manager_get_pid();
1222         if (0 < mgr_pid) {
1223                 for (j = 0; j < g_list_length(g_proc_list); j++) {
1224                         iter = g_list_nth(g_proc_list, j);
1225                         if (NULL != iter) {
1226                                 if (mgr_pid == GPOINTER_TO_INT(iter->data)) {
1227                                         SLOG(LOG_DEBUG, TAG_VCD, "manager pid(%d) is running", mgr_pid);
1228                                         exist = true;
1229                                         break;
1230                                 }
1231                         }
1232                 }
1233
1234                 if (false == exist) {
1235                         SLOG(LOG_ERROR, TAG_VCD, "manager pid (%d) should be removed", mgr_pid);
1236                         vcd_server_mgr_finalize(mgr_pid);
1237                 }
1238         }
1239 #endif
1240         return EINA_TRUE;
1241 }
1242
1243 int vcd_server_get_service_state()
1244 {
1245         return vcd_config_get_service_state();
1246 }
1247
1248 int vcd_server_get_foreground()
1249 {
1250         int pid;
1251         vcd_config_get_foreground(&pid);
1252         return pid;
1253 }
1254
1255
1256 /*
1257 * API for manager
1258 */
1259 int vcd_server_mgr_initialize(int pid)
1260 {
1261         if (false == g_is_engine) {
1262                 if (0 != vcd_engine_agent_initialize_current_engine()) {
1263                         SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] No Engine");
1264                         g_is_engine = false;
1265                         return VCD_ERROR_ENGINE_NOT_FOUND;
1266                 } else {
1267                         g_is_engine = true;
1268                 }
1269         }
1270
1271         /* check if pid is valid */
1272         if (false == vcd_client_manager_is_valid(pid)) {
1273                 SLOG(LOG_ERROR, TAG_VCD, "[Server] old manager pid(%d) be removed", vcd_client_manager_get_pid());
1274                 vcd_server_mgr_cancel();
1275                 vcd_client_manager_unset();
1276         }
1277
1278         /* Add client information to client manager */
1279         if (0 != vcd_client_manager_set(pid)) {
1280                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to add manager");
1281                 return VCD_ERROR_OPERATION_FAILED;
1282         }
1283
1284         if (0 != vcdc_send_manager_pid(pid))
1285                 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Fail to send manager pid");
1286
1287         SLOG(LOG_DEBUG, TAG_VCD, "[Server Success] Manager initialize : pid(%d)", pid);
1288
1289         return VCD_ERROR_NONE;
1290 }
1291
1292 int vcd_server_mgr_finalize(int pid)
1293 {
1294         /* check if pid is valid */
1295         if (false == vcd_client_manager_is_valid(pid)) {
1296                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] The manager pid(%d) is NOT valid", pid);
1297                 return VCD_ERROR_INVALID_PARAMETER;
1298         }
1299
1300         /* Cancel recognition */
1301         vcd_server_mgr_cancel();
1302
1303         if (0 != vcdc_send_manager_pid(-1))
1304                 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Fail to send manager pid");
1305
1306         /* Remove manager information */
1307         if (0 != vcd_client_manager_unset()) {
1308                 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Fail to delete client");
1309         }
1310
1311         if (0 == vcd_client_get_ref_count()) {
1312                 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Connected client list is empty");
1313                 ecore_timer_add(0, __finalize_quit_ecore_loop, NULL);
1314         }
1315
1316         SLOG(LOG_DEBUG, TAG_VCD, "[Server Success] Manager Finalize : pid(%d)", pid);
1317
1318         return VCD_ERROR_NONE;
1319 }
1320
1321 int vcd_server_mgr_set_command(int pid)
1322 {
1323         if (0 != vcd_client_manager_set_command(pid)) {
1324                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] The manager pid(%d) is NOT valid", pid);
1325                 return VCD_ERROR_INVALID_PARAMETER;
1326         }
1327         return VCD_ERROR_NONE;
1328 }
1329
1330 int vcd_server_mgr_unset_command(int pid)
1331 {
1332         if (0 != vcd_client_manager_unset_command(pid)) {
1333                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] The manager pid(%d) is NOT valid", pid);
1334                 return VCD_ERROR_INVALID_PARAMETER;
1335         }
1336         return VCD_ERROR_NONE;
1337 }
1338
1339 int vcd_server_mgr_set_demandable_client(int pid)
1340 {
1341         /* check if pid is valid */
1342         if (false == vcd_client_manager_is_valid(pid)) {
1343                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] The manager pid(%d) is NOT valid", pid);
1344                 return VCD_ERROR_INVALID_PARAMETER;
1345         }
1346
1347         GSList* client_list = NULL;
1348         if (0 != vc_info_parser_get_demandable_clients(&client_list)) {
1349                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to get demandable client");
1350                 return VCD_ERROR_OPERATION_FAILED;
1351         }
1352
1353         /* Save client list */
1354         if (0 != vcd_client_manager_set_demandable_client(pid, client_list)) {
1355                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to set demandable client list");
1356                 return VCD_ERROR_OPERATION_FAILED;
1357         }
1358
1359         return VCD_ERROR_NONE;
1360 }
1361
1362 int vcd_server_mgr_set_audio_type(int pid, const char* audio_type)
1363 {
1364         /* check if pid is valid */
1365         if (false == vcd_client_manager_is_valid(pid)) {
1366                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] The manager pid(%d) is NOT valid", pid);
1367                 return VCD_ERROR_INVALID_PARAMETER;
1368         }
1369
1370         int ret = 0;
1371         vcp_audio_type_e type = VCP_AUDIO_TYPE_PCM_S16_LE;
1372         int rate = 16000;
1373         int channel = 1;
1374
1375         ret = vcd_engine_get_audio_format(audio_type, &type, &rate, &channel);
1376         if (0 != ret) {
1377                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to get audio format : %d", ret);
1378                 return VCD_ERROR_OPERATION_FAILED;
1379         }
1380
1381         ret = vcd_recorder_set(audio_type, type, rate, channel);
1382         if (0 != ret) {
1383                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to set audio in type : %d", ret);
1384                 return VCD_ERROR_OPERATION_FAILED;
1385         }
1386
1387         return VCD_ERROR_NONE;
1388 }
1389
1390 int vcd_server_mgr_get_audio_type(int pid, char** audio_type)
1391 {
1392         /* check if pid is valid */
1393         if (false == vcd_client_manager_is_valid(pid)) {
1394                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] The manager pid(%d) is NOT valid", pid);
1395                 return VCD_ERROR_INVALID_PARAMETER;
1396         }
1397
1398         int ret = vcd_recorder_get(audio_type);
1399         if (0 != ret) {
1400                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to get audio in type : %d", ret);
1401                 return VCD_ERROR_OPERATION_FAILED;
1402         }
1403
1404         return VCD_ERROR_NONE;
1405 }
1406
1407 int vcd_server_mgr_set_client_info(int pid)
1408 {
1409         /* check if pid is valid */
1410         if (false == vcd_client_manager_is_valid(pid)) {
1411                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] The manager pid(%d) is NOT valid", pid);
1412                 return VCD_ERROR_INVALID_PARAMETER;
1413         }
1414
1415         int ret = vcd_client_save_client_info();
1416         if (0 != ret) {
1417                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to save client info : %d", ret);
1418                 return VCD_ERROR_OPERATION_FAILED;
1419         }
1420
1421         return VCD_ERROR_NONE;
1422 }
1423
1424 static int __start_internal_recognition()
1425 {
1426         int ret;
1427
1428         /* 2. Get commands */
1429         ret = vcd_client_command_collect_command();
1430         if (0 != ret) {
1431                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to collect command : %d", ret);
1432                 return VCD_ERROR_OPERATION_FAILED;
1433         }
1434
1435         ret = vcd_client_get_length();
1436         if (0 == ret) {
1437                 SLOG(LOG_WARN, TAG_VCD, "[Server WARNIING] No current command : %d", ret);
1438                 return VCD_ERROR_OPERATION_FAILED;
1439         }
1440
1441         /* 3. Set command to engine */
1442         ret = vcd_engine_set_commands();
1443         if (0 != ret) {
1444                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to collect command : %d", ret);
1445                 return VCD_ERROR_OPERATION_FAILED;
1446         }
1447
1448         SLOG(LOG_DEBUG, TAG_VCD, "[Server Success] Set command");
1449
1450         bool stop_by_silence = true;
1451         if (VCD_RECOGNITION_MODE_MANUAL == vcd_client_get_recognition_mode()) {
1452                 stop_by_silence = false;
1453         }
1454
1455         vcd_client_manager_set_result_text(NULL);
1456
1457         /* 4. start recognition */
1458         ret = vcd_engine_recognize_start(stop_by_silence);
1459         if (0 != ret) {
1460                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to start recognition : result(%d)", ret);
1461                 return VCD_ERROR_OPERATION_FAILED;
1462         }
1463
1464         SLOG(LOG_DEBUG, TAG_VCD, "[Server Success] Start engine");
1465
1466         /* 5. recorder start */
1467         ret = vcd_recorder_start();
1468         if (0 != ret) {
1469                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to start recorder : result(%d)", ret);
1470                 vcd_engine_recognize_cancel();
1471                 return ret;
1472         }
1473
1474         vcd_config_set_service_state(VCD_STATE_RECORDING);
1475         vcdc_send_service_state(VCD_STATE_RECORDING);
1476
1477         SLOG(LOG_DEBUG, TAG_VCD, "[Server Success] Start recognition(%d)", stop_by_silence);
1478
1479         return 0;
1480 }
1481
1482 static Eina_Bool __vcd_request_show_tooltip(void *data)
1483 {
1484         int pid = vcd_client_widget_get_foreground_pid();
1485         if (-1 != pid) {
1486                 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Request tooltip show and widget command");
1487                 vcdc_send_show_tooltip(pid, (bool)data);
1488         }
1489
1490         return EINA_FALSE;
1491 }
1492
1493 int vcd_server_mgr_start(vcd_recognition_mode_e recognition_mode, bool exclusive_cmd, bool start_by_client)
1494 {
1495         /* 1. check current state */
1496         vcd_state_e state = vcd_config_get_service_state();
1497
1498         if (VCD_STATE_READY != state) {
1499                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Current state is not ready");
1500                 return VCD_ERROR_INVALID_STATE;
1501         }
1502         if (-1 == vcd_client_manager_get_pid()) {
1503                 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Manager is NOT available.");
1504                 return VCD_ERROR_OPERATION_FAILED;
1505         }
1506
1507         SLOG(LOG_DEBUG, TAG_VCD, "[Server] set recognition mode = %d", recognition_mode);
1508         vcd_client_set_recognition_mode(recognition_mode);
1509
1510         if (false == exclusive_cmd) {
1511                 /* Notify show tooltip */
1512                 int pid = vcd_client_widget_get_foreground_pid();
1513                 if (-1 != pid) {
1514                         SLOG(LOG_DEBUG, TAG_VCD, "[Server] Request tooltip show and widget command");
1515                         ecore_timer_add(0, __vcd_request_show_tooltip, (void*)true);
1516                         return 0;
1517                 }
1518         } else {
1519                 vcd_client_manager_set_exclusive(exclusive_cmd);
1520         }
1521
1522         int fg_pid = -1;
1523         if (true == start_by_client) {
1524                 /* Get foreground pid */
1525                 if (0 != vcd_config_get_foreground(&fg_pid)) {
1526                         SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to get foreground");
1527                 }
1528
1529                 /* Set client exclusive option */
1530                 if (0 != vcd_client_set_exclusive_command(fg_pid)) {
1531                         SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to set exclusive command");
1532                 }
1533         }
1534
1535         int ret = __start_internal_recognition();
1536         if (0 != ret) {
1537                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to start recongition : %d", ret);
1538                 return ret;
1539         }
1540
1541         if (true == start_by_client) {
1542                 vcd_client_unset_exclusive_command(fg_pid);
1543         }
1544
1545         return VCD_ERROR_NONE;
1546 }
1547
1548 int vcd_server_mgr_stop()
1549 {
1550         /* 1. Check current state is recording */
1551         if (VCD_STATE_RECORDING != vcd_config_get_service_state()) {
1552                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Current state is not recording");
1553                 return VCD_ERROR_INVALID_STATE;
1554         }
1555         if (-1 == vcd_client_manager_get_pid()) {
1556                 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Manager is NOT available.");
1557                 return VCD_ERROR_OPERATION_FAILED;
1558         }
1559
1560         /* 2. Stop recorder */
1561         vcd_recorder_stop();
1562
1563         /* 3. Stop engine recognition */
1564         int ret = vcd_engine_recognize_stop();
1565         if (0 != ret) {
1566                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to stop recognition : %d", ret);
1567         }
1568
1569         /* 4. Set original mode */
1570         vcd_config_set_service_state(VCD_STATE_PROCESSING);
1571         vcdc_send_service_state(VCD_STATE_PROCESSING);
1572
1573         return VCD_ERROR_NONE;
1574 }
1575
1576 int vcd_server_mgr_cancel()
1577 {
1578         /* 1. Check current state */
1579         vcd_state_e state = vcd_config_get_service_state();
1580         if (VCD_STATE_RECORDING != state && VCD_STATE_PROCESSING != state) {
1581                 SLOG(LOG_WARN, TAG_VCD, "[Server ERROR] Current state is not recording or processing");
1582                 return VCD_ERROR_INVALID_STATE;
1583         }
1584         if (-1 == vcd_client_manager_get_pid()) {
1585                 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Manager is NOT available.");
1586                 return VCD_ERROR_OPERATION_FAILED;
1587         }
1588
1589         if (g_restart_timer != NULL) {
1590                 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Delete restart engine timer");
1591                 ecore_timer_del(g_restart_timer);
1592                 g_restart_timer = NULL;
1593         }
1594
1595         /* 2. Stop recorder */
1596         vcd_recorder_stop();
1597         /* 3. Cancel engine */
1598         int ret = vcd_engine_recognize_cancel();
1599         if (0 != ret) {
1600                 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Fail to cancel : %d", ret);
1601         }
1602
1603         if (false == vcd_client_manager_get_exclusive()) {
1604                 int pid = vcd_client_widget_get_foreground_pid();
1605                 if (-1 != pid) {
1606                         SLOG(LOG_DEBUG, TAG_VCD, "[Server] Request tooltip hide");
1607                         ecore_timer_add(0, __vcd_request_show_tooltip, (void*)false);
1608                 }
1609         } else {
1610                 vcd_client_manager_set_exclusive(false);
1611         }
1612
1613         /* 4. Set state */
1614         vcd_config_set_service_state(VCD_STATE_READY);
1615         vcdc_send_service_state(VCD_STATE_READY);
1616
1617         return VCD_ERROR_NONE;
1618 }
1619
1620
1621 int vcd_server_mgr_result_select()
1622 {
1623         __vcd_send_selected_result(NULL);
1624
1625         return VCD_ERROR_NONE;
1626 }
1627
1628 int vcd_server_mgr_set_domain(int pid, const char* domain)
1629 {
1630         /* check if pid is valid */
1631         if (false == vcd_client_manager_is_valid(pid)) {
1632                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] The manager pid(%d) is NOT valid", pid);
1633                 return VCD_ERROR_INVALID_PARAMETER;
1634         }
1635
1636         vcd_state_e state = vcd_config_get_service_state();
1637         if (VCD_STATE_READY != state) {
1638                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Current state is not ready");
1639                 return VCD_ERROR_INVALID_STATE;
1640         }
1641
1642         /* Set domain to engine */
1643         int ret = vcd_engine_set_domain(pid, domain);
1644         if (0 != ret) {
1645                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to set domain : %d", ret);
1646         } else {
1647                 SLOG(LOG_DEBUG, TAG_VCD, "[Server SUCCESS] Set domain");
1648         }
1649
1650         return ret;
1651 }
1652
1653 int vcd_server_mgr_set_private_data(int pid, const char* key, const char* data)
1654 {
1655         /* check if pid is valid */
1656         if (false == vcd_client_manager_is_valid(pid)) {
1657                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] The manager pid(%d) is NOT valid", pid);
1658                 return VCD_ERROR_INVALID_PARAMETER;
1659         }
1660
1661         vcd_state_e state = vcd_config_get_service_state();
1662         if (VCD_STATE_READY != state) {
1663                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Current state is not ready");
1664                 return VCD_ERROR_INVALID_STATE;
1665         }
1666
1667         /* Set private data to engine */
1668         int ret = vcd_engine_set_private_data(pid, key, data);
1669         if (0 != ret) {
1670                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to set private data : %d", ret);
1671         } else {
1672                 SLOG(LOG_DEBUG, TAG_VCD, "[Server SUCCESS] Set private data");
1673         }
1674
1675         return ret;
1676 }
1677
1678 int vcd_server_mgr_get_private_data(int pid, const char* key, char** data)
1679 {
1680         /* check if pid is valid */
1681         if (false == vcd_client_manager_is_valid(pid)) {
1682                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] The manager pid(%d) is NOT valid", pid);
1683                 return VCD_ERROR_INVALID_PARAMETER;
1684         }
1685         vcd_state_e state = vcd_config_get_service_state();
1686         if (VCD_STATE_READY != state) {
1687                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Current state is not ready");
1688                 return VCD_ERROR_INVALID_STATE;
1689         }
1690
1691         /* Get private data to engine */
1692         int ret = vcd_engine_get_private_data(pid, key, data);
1693         if (0 != ret) {
1694                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to get private data : %d", ret);
1695         } else {
1696                 SLOG(LOG_DEBUG, TAG_VCD, "[Server SUCCESS] Set private data");
1697         }
1698
1699         return ret;
1700 }
1701
1702 int vcd_server_mgr_do_action(int pid, int type, const char* action)
1703 {
1704         int ret = -1;
1705
1706         /* check if pid is valid */
1707         if (false == vcd_client_manager_is_valid(pid)) {
1708                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] The manager pid(%d) is NOT valid", pid);
1709                 return VCD_ERROR_INVALID_PARAMETER;
1710         }
1711
1712         vcd_state_e state = vcd_config_get_service_state();
1713         if (VCD_STATE_READY != state) {
1714                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Current state is not ready");
1715                 return VCD_ERROR_INVALID_STATE;
1716         }
1717
1718         /* Reqeust do action to engine */
1719         if (VCD_SEND_EVENT_TYPE_TEXT == type)
1720                 ret = vcd_engine_process_text(pid, action);
1721         else if (VCD_SEND_EVENT_TYPE_LIST_EVENT == type)
1722                 ret = vcd_engine_process_list_event(pid, action);
1723         else if (VCD_SEND_EVENT_TYPE_HAPTIC_EVENT == type)
1724                 ret = vcd_engine_process_haptic_event(pid, action);
1725
1726         if (0 != ret) {
1727                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to process do action : %d", ret);
1728         } else {
1729                 SLOG(LOG_DEBUG, TAG_VCD, "[Server SUCCESS] Process do action");
1730         }
1731
1732         return ret;
1733 }
1734
1735 /*
1736 * VC Server Functions for Client
1737 */
1738 int vcd_server_initialize(int pid)
1739 {
1740         if (false == g_is_engine) {
1741                 if (0 != vcd_engine_agent_initialize_current_engine()) {
1742                         SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] No Engine");
1743                         g_is_engine = false;
1744                         return VCD_ERROR_ENGINE_NOT_FOUND;
1745                 } else {
1746                         g_is_engine = true;
1747                 }
1748         }
1749
1750         if (false == vcd_engine_is_available_engine()) {
1751                 if (0 != vcd_engine_agent_initialize_current_engine()) {
1752                         SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] No Engine");
1753                         return VCD_ERROR_ENGINE_NOT_FOUND;
1754                 }
1755         }
1756
1757         /* check if pid is valid */
1758         if (true == vcd_client_is_available(pid)) {
1759                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] The pid is already exist");
1760                 return VCD_ERROR_INVALID_PARAMETER;
1761         }
1762
1763         /* Add client information to client manager */
1764         if (0 != vcd_client_add(pid)) {
1765                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to add client info");
1766                 return VCD_ERROR_OPERATION_FAILED;
1767         }
1768
1769         SLOG(LOG_DEBUG, TAG_VCD, "[Server Success] Client Initialize : pid(%d)", pid);
1770
1771         return VCD_ERROR_NONE;
1772 }
1773
1774 int vcd_server_finalize(int pid)
1775 {
1776         /* check if pid is valid */
1777         if (false == vcd_client_is_available(pid)) {
1778                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] pid is NOT valid ");
1779                 return VCD_ERROR_INVALID_PARAMETER;
1780         }
1781
1782         /* Remove client information */
1783         if (0 != vcd_client_delete(pid)) {
1784                 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Fail to delete client");
1785         }
1786
1787         if (0 == vcd_client_get_ref_count()) {
1788                 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Connected client list is empty");
1789                 ecore_timer_add(0, __finalize_quit_ecore_loop, NULL);
1790         }
1791
1792         SLOG(LOG_DEBUG, TAG_VCD, "[Server Success] Client Finalize : pid(%d)", pid);
1793
1794         return VCD_ERROR_NONE;
1795 }
1796
1797 int vcd_server_set_command(int pid, vc_cmd_type_e cmd_type)
1798 {
1799         /* check if pid is valid */
1800         if (false == vcd_client_is_available(pid)) {
1801                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] pid is NOT valid ");
1802                 return VCD_ERROR_INVALID_PARAMETER;
1803         }
1804
1805         if (0 != vcd_client_set_command_type(pid, cmd_type)) {
1806                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to set command type : pid(%d), cmd_type(%d)", pid, cmd_type);
1807                 return VCD_ERROR_OPERATION_FAILED;
1808         }
1809
1810         return 0;
1811 }
1812
1813 int vcd_server_unset_command(int pid, vc_cmd_type_e cmd_type)
1814 {
1815         /* check if pid is valid */
1816         if (false == vcd_client_is_available(pid)) {
1817                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] pid is NOT valid ");
1818                 return VCD_ERROR_INVALID_PARAMETER;
1819         }
1820
1821         if (0 != vcd_client_unset_command_type(pid, cmd_type)) {
1822                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to unset command type : pid(%d), cmd_type(%d)", pid, cmd_type);
1823                 return VCD_ERROR_OPERATION_FAILED;
1824         }
1825
1826         return 0;
1827 }
1828
1829 int vcd_server_set_foreground(int pid, bool value)
1830 {
1831         /* check if pid is valid */
1832         if (false == vcd_client_is_available(pid) && false == vcd_client_widget_is_available(pid)) {
1833                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] pid is NOT valid ");
1834                 return VCD_ERROR_INVALID_PARAMETER;
1835         }
1836
1837         if (0 != vcd_config_set_foreground(pid, value)) {
1838                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to set foreground : pid(%d), value(%d)", pid, value);
1839                 return VCD_ERROR_OPERATION_FAILED;
1840         }
1841
1842         return 0;
1843 }
1844
1845 static int __vcd_server_launch_manager_app()
1846 {
1847         int ret = -1;
1848         bool running = false;
1849         char* appid = NULL;
1850
1851         ret = vcd_client_manager_get_appid(&appid);
1852         if (0 != ret || NULL == appid) {
1853                 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Fail to get manager appid");
1854                 return VCD_ERROR_OPERATION_FAILED;
1855         }
1856         ret = app_manager_is_running(appid, &running);
1857         if (0 != ret) {
1858                 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Fail to check running with appid(%s)", appid);
1859                 free(appid);
1860                 appid = NULL;
1861                 return VCD_ERROR_OPERATION_FAILED;
1862         }
1863         if (false == running) {
1864                 int tmp_ret = __vcd_is_package_installed(appid);
1865                 if (false == tmp_ret) {
1866                         SLOG(LOG_ERROR, TAG_VCD, "[ERROR] manager app is not installed, appid(%s)", appid);
1867                 } else {
1868                         ret = __vcd_activate_app_by_appcontrol(appid);
1869                         if (0 != ret) {
1870                                 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Fail to activate app");
1871                                 free(appid);
1872                                 appid = NULL;
1873                                 return ret;
1874                         }
1875                         SLOG(LOG_ERROR, TAG_VCD, "Launch vc manager app: appid(%s)", appid);
1876                 }
1877         } else {
1878                 ret = __vcd_resume_app(appid);
1879                 if (0 != ret) {
1880                         SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Fail to resume app");
1881                         free(appid);
1882                         appid = NULL;
1883                         return ret;
1884                 }
1885                 SLOG(LOG_ERROR, TAG_VCD, "Resume vc manager app: appid(%s)", appid);
1886         }
1887
1888         free(appid);
1889         appid = NULL;
1890
1891         return VCD_ERROR_NONE;
1892 }
1893
1894 int vcd_server_dialog(int pid, const char* disp_text, const char* utt_text, int continuous)
1895 {
1896         /* check if pid is valid */
1897         if (false == vcd_client_is_available(pid) && false == vcd_client_widget_is_available(pid)) {
1898                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] pid is NOT valid ");
1899                 return VCD_ERROR_INVALID_PARAMETER;
1900         }
1901
1902         int ret = __vcd_server_launch_manager_app();
1903         if (0 != ret) {
1904                 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);
1905                 return ret;
1906         }
1907
1908         ret = vcdc_send_dialog(vcd_client_manager_get_pid(), pid, disp_text, utt_text, continuous);
1909         if (0 != ret) {
1910                 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);
1911                 return ret;
1912         }
1913
1914         return 0;
1915 }
1916
1917 int vcd_server_is_system_command_valid(int pid, int* is_sys_cmd_valid)
1918 {
1919         /* check if pid is valid */
1920         if (false == vcd_client_is_available(pid) && false == vcd_client_widget_is_available(pid)) {
1921                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] pid is NOT valid ");
1922                 return VCD_ERROR_INVALID_PARAMETER;
1923         }
1924
1925         /* check if system command is valid */
1926         if (true == vcd_client_manager_is_system_command_valid(vcd_client_manager_get_pid()))
1927                 *is_sys_cmd_valid = true;
1928         else
1929                 *is_sys_cmd_valid = false;
1930
1931         return 0;
1932 }
1933
1934 #if 0
1935 int vcd_server_set_exclusive_command(int pid, bool value)
1936 {
1937         /* check if pid is valid */
1938         if (false == vcd_client_is_available(pid)) {
1939                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] pid is NOT valid ");
1940                 return VCD_ERROR_INVALID_PARAMETER;
1941         }
1942
1943         if (true == value) {
1944                 if (0 != vcd_client_set_exclusive_command(pid)) {
1945                         SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to set exclusive command : pid(%d)", pid);
1946                         return VCD_ERROR_OPERATION_FAILED;
1947                 }
1948         } else {
1949                 if (0 != vcd_client_unset_exclusive_command(pid)) {
1950                         SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to unset exclusive command : pid(%d)", pid);
1951                         return VCD_ERROR_OPERATION_FAILED;
1952                 }
1953         }
1954
1955         return 0;
1956 }
1957
1958 int vcd_server_request_start(int pid, bool stop_by_silence)
1959 {
1960         /* check if pid is valid */
1961         if (false == vcd_client_is_available(pid)) {
1962                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] pid(%d) is NOT forground client", pid);
1963                 return VCD_ERROR_INVALID_PARAMETER;
1964         }
1965
1966         int ret;
1967         /* Check current state */
1968         vcd_state_e state = vcd_config_get_service_state();
1969
1970         /* Service state should be ready */
1971         if (VCD_STATE_READY != state) {
1972                 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Current state is not Ready : pid(%d)", pid);
1973                 return VCD_ERROR_INVALID_STATE;
1974         }
1975
1976         if (-1 != vcd_client_manager_get_pid()) {
1977                 /* Check current pid is valid */
1978                 if (false == vcd_client_manager_check_demandable_client(pid)) {
1979                         SLOG(LOG_DEBUG, TAG_VCD, "[Server] Current client is NOT available : pid(%d)", pid);
1980                         return VCD_ERROR_INVALID_PARAMETER;
1981                 }
1982         }
1983
1984         ret = vcd_server_mgr_start(stop_by_silence, false);
1985         if (0 != ret) {
1986                 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Fail to start recognition");
1987                 return VCD_ERROR_INVALID_PARAMETER;
1988         }
1989
1990         return 0;
1991 }
1992
1993 int vcd_server_request_stop(int pid)
1994 {
1995         int ret;
1996         /* Check current state */
1997         vcd_state_e state = vcd_config_get_service_state();
1998
1999         /* Service state should be ready */
2000         if (VCD_STATE_RECORDING != state) {
2001                 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Current state is not Recording : pid(%d)", pid);
2002                 return VCD_ERROR_INVALID_STATE;
2003         }
2004
2005         if (-1 != vcd_client_manager_get_pid()) {
2006                 /* Check current pid is valid */
2007                 if (false == vcd_client_manager_check_demandable_client(pid)) {
2008                         SLOG(LOG_DEBUG, TAG_VCD, "[Server] Current client is NOT available : pid(%d)", pid);
2009                         return VCD_ERROR_INVALID_PARAMETER;
2010                 }
2011         }
2012
2013         ret = vcd_server_mgr_stop();
2014         if (0 != ret) {
2015                 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Fail to start recognition");
2016                 return VCD_ERROR_OPERATION_FAILED;
2017         }
2018
2019         return VCD_ERROR_NONE;
2020 }
2021
2022 int vcd_server_request_cancel(int pid)
2023 {
2024         int ret;
2025         /* Check current state */
2026         vcd_state_e state = vcd_config_get_service_state();
2027
2028         /* Service state should be recording or processing */
2029         if (VCD_STATE_RECORDING != state && VCD_STATE_PROCESSING != state) {
2030                 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Current state is not Recording or Processing : pid(%d)", pid);
2031                 return VCD_ERROR_INVALID_STATE;
2032         }
2033
2034         if (-1 != vcd_client_manager_get_pid()) {
2035                 /* Check current pid is valid */
2036                 if (false == vcd_client_manager_check_demandable_client(pid)) {
2037                         SLOG(LOG_DEBUG, TAG_VCD, "[Server] Current client is NOT available : pid(%d)", pid);
2038                         return VCD_ERROR_INVALID_PARAMETER;
2039                 }
2040         }
2041
2042         ret = vcd_server_mgr_cancel();
2043         if (0 != ret) {
2044                 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Fail to start recognition");
2045                 return VCD_ERROR_OPERATION_FAILED;
2046         }
2047
2048         return VCD_ERROR_NONE;
2049 }
2050 #endif
2051
2052 /*
2053 * VC Server Functions for Widget lib
2054 */
2055 int vcd_server_widget_initialize(int pid)
2056 {
2057         if (false == g_is_engine) {
2058                 if (0 != vcd_engine_agent_initialize_current_engine()) {
2059                         SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] No Engine");
2060                         g_is_engine = false;
2061                         return VCD_ERROR_ENGINE_NOT_FOUND;
2062                 } else {
2063                         g_is_engine = true;
2064                 }
2065         }
2066
2067         if (false == vcd_engine_is_available_engine()) {
2068                 if (0 != vcd_engine_agent_initialize_current_engine()) {
2069                         SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] No Engine");
2070                         return VCD_ERROR_ENGINE_NOT_FOUND;
2071                 }
2072         }
2073
2074         /* check if pid is valid */
2075         if (true == vcd_client_widget_is_available(pid)) {
2076                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] The pid is already exist");
2077                 return VCD_ERROR_INVALID_PARAMETER;
2078         }
2079
2080         /* Add client information to client manager */
2081         if (0 != vcd_client_widget_add(pid)) {
2082                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to add client info");
2083                 return VCD_ERROR_OPERATION_FAILED;
2084         }
2085
2086         SLOG(LOG_DEBUG, TAG_VCD, "[Server Success] Initialize widget : pid(%d)", pid);
2087
2088         return VCD_ERROR_NONE;
2089 }
2090
2091 int vcd_server_widget_finalize(int pid)
2092 {
2093         /* check if pid is valid */
2094         if (false == vcd_client_widget_is_available(pid)) {
2095                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] pid is NOT valid ");
2096                 return VCD_ERROR_INVALID_PARAMETER;
2097         }
2098
2099         /* Remove client information */
2100         if (0 != vcd_client_widget_delete(pid)) {
2101                 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Fail to delete client");
2102         }
2103
2104         if (0 == vcd_client_get_ref_count()) {
2105                 SLOG(LOG_DEBUG, TAG_VCD, "[Server] connected client list is empty");
2106                 ecore_timer_add(0, __finalize_quit_ecore_loop, NULL);
2107         }
2108
2109         return VCD_ERROR_NONE;
2110 }
2111
2112 int vcd_server_widget_start_recording(int pid, bool widget_command)
2113 {
2114         /* check if pid is valid */
2115         if (false == vcd_client_widget_is_available(pid)) {
2116                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] pid is NOT valid ");
2117                 return VCD_ERROR_INVALID_PARAMETER;
2118         }
2119
2120         if (true == widget_command) {
2121                 vcd_client_widget_set_command(pid);
2122                 SLOG(LOG_DEBUG, TAG_VCD, "[Server] widget command is available");
2123         } else {
2124                 vcd_client_widget_unset_command(pid);
2125                 SLOG(LOG_WARN, TAG_VCD, "[Server] widget command is NOT available");
2126         }
2127
2128         int ret = __start_internal_recognition();
2129         if (0 != ret) {
2130                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to start recongition : %d", ret);
2131                 ecore_timer_add(0, __vcd_request_show_tooltip, (void*)false);
2132         }
2133
2134         return 0;
2135 }
2136
2137 int vcd_server_widget_start(int pid, bool stop_by_silence)
2138 {
2139         /* check if pid is valid */
2140         int fore_pid = vcd_client_widget_get_foreground_pid();
2141         if (pid != fore_pid) {
2142                 SLOG(LOG_ERROR, TAG_VCD, "[Server] pid is NOT foreground");
2143                 return VCD_ERROR_INVALID_PARAMETER;
2144         }
2145
2146         /* Check current state */
2147         vcd_state_e state = vcd_config_get_service_state();
2148
2149         /* Service state should be ready */
2150         if (VCD_STATE_READY != state) {
2151                 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Current state is not Ready : pid(%d)", pid);
2152                 return VCD_ERROR_INVALID_STATE;
2153         }
2154
2155         vcd_client_set_slience_detection(stop_by_silence);
2156
2157         /* Notify show tooltip */
2158         ecore_timer_add(0, __vcd_request_show_tooltip, (void*)true);
2159
2160         return 0;
2161 }
2162
2163 int vcd_server_widget_stop(int pid)
2164 {
2165         /* check if pid is valid */
2166         int fore_pid = vcd_client_widget_get_foreground_pid();
2167         if (pid != fore_pid) {
2168                 SLOG(LOG_ERROR, TAG_VCD, "[Server] pid is NOT foreground");
2169                 return VCD_ERROR_INVALID_PARAMETER;
2170         }
2171
2172         int ret;
2173         /* Check current state */
2174         vcd_state_e state = vcd_config_get_service_state();
2175
2176         /* Service state should be recording */
2177         if (VCD_STATE_RECORDING != state) {
2178                 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Current service state is not Recording : pid(%d)", pid);
2179                 return VCD_ERROR_INVALID_STATE;
2180         }
2181
2182         ret = vcd_server_mgr_stop();
2183         if (0 != ret) {
2184                 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Fail to start recognition");
2185                 return VCD_ERROR_OPERATION_FAILED;
2186         }
2187
2188         return VCD_ERROR_NONE;
2189 }
2190
2191 int vcd_server_widget_cancel(int pid)
2192 {
2193         /* check if pid is valid */
2194         int fore_pid = vcd_client_widget_get_foreground_pid();
2195         if (pid != fore_pid) {
2196                 SLOG(LOG_ERROR, TAG_VCD, "[Server] pid is NOT foreground");
2197                 return VCD_ERROR_INVALID_PARAMETER;
2198         }
2199
2200         int ret;
2201         /* Check current state */
2202         vcd_state_e state = vcd_config_get_service_state();
2203
2204         /* Service state should be recording or processing */
2205         if (VCD_STATE_RECORDING != state && VCD_STATE_PROCESSING != state) {
2206                 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Current state is not Recording or Processing : pid(%d)", pid);
2207                 return VCD_ERROR_INVALID_STATE;
2208         }
2209
2210         ret = vcd_server_mgr_cancel();
2211         if (0 != ret) {
2212                 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Fail to cancel recognition : %d", ret);
2213                 return ret;
2214         }
2215
2216         return VCD_ERROR_NONE;
2217 }
2218