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