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