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