Add restore logic and function to get nlp info and pre result
[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 <dirent.h>
18 #include <sound_manager.h>
19
20 #include "vc_info_parser.h"
21 #include "vcd_main.h"
22 #include "vcd_server.h"
23 #include "vcd_client_data.h"
24
25 #include "vcd_engine_agent.h"
26 #include "vcd_config.h"
27 #include "vcd_recorder.h"
28 #include "vcd_dbus.h"
29
30 #include "voice_control_command_expand.h"
31
32 /*
33 * VC Server static variable
34 */
35 static bool     g_is_engine;
36
37 static GList *g_proc_list = NULL;
38
39 static Ecore_Timer *g_restart_timer = NULL;
40
41 /*
42 * VC Server Internal Functions
43 */
44 static Eina_Bool __stop_by_silence(void *data)
45 {
46         SLOG(LOG_DEBUG, TAG_VCD, "===== Silence Detected ");
47
48         vcd_server_mgr_stop();
49
50         SLOG(LOG_DEBUG, TAG_VCD, "=====");
51         SLOG(LOG_DEBUG, TAG_VCD, "  ");
52         return EINA_FALSE;
53 }
54
55 static Eina_Bool __cancel_by_interrupt(void *data)
56 {
57         SLOG(LOG_DEBUG, TAG_VCD, "===== Cancel by interrupt");
58
59         vcd_server_mgr_cancel();
60
61         SLOG(LOG_DEBUG, TAG_VCD, "=====");
62         SLOG(LOG_DEBUG, TAG_VCD, "  ");
63         return EINA_FALSE;
64 }
65
66 static Eina_Bool __restart_engine(void *data)
67 {
68         SLOG(LOG_DEBUG, TAG_VCD, "===== Restart by no result");
69
70         g_restart_timer = NULL;
71
72         /* Restart recognition */
73         int ret = vcd_engine_recognize_start(true);
74         if (0 != ret) {
75                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to restart recognition : result(%d)", ret);
76                 return EINA_FALSE;
77         }
78
79         SLOG(LOG_DEBUG, TAG_VCD, "[Server Success] Start engine");
80
81         if (VCD_RECOGNITION_MODE_RESTART_AFTER_REJECT == vcd_client_get_recognition_mode()) {
82                 vcd_config_set_service_state(VCD_STATE_RECORDING);
83                 vcdc_send_service_state(VCD_STATE_RECORDING);
84         }
85
86         SLOG(LOG_DEBUG, TAG_VCD, "[Server Success] Restart recognition");
87
88         SLOG(LOG_DEBUG, TAG_VCD, "=====");
89         SLOG(LOG_DEBUG, TAG_VCD, "  ");
90         return EINA_FALSE;
91 }
92
93 static int __server_recorder_callback(const void* data, const unsigned int length)
94 {
95         vcd_state_e state = vcd_config_get_service_state();
96         if (VCD_STATE_RECORDING != state) {
97                 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Not recording state, but recording");
98 //              return 0;
99         }
100
101         vcp_speech_detect_e speech_detected = VCP_SPEECH_DETECT_NONE;
102         int ret;
103
104         ret = vcd_engine_recognize_audio(data, length, &speech_detected);
105
106         if (0 > ret) {
107                 /* Error */
108                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to set recording data to engine(%d)", ret);
109                 ecore_timer_add(0, __cancel_by_interrupt, NULL);
110                 /* Send error cb to manager */
111                 if (VCP_ERROR_OUT_OF_NETWORK == ret) {
112                         vcdc_send_error_signal_to_manager(vcd_client_manager_get_pid(), VCD_ERROR_TIMED_OUT, "Engine connection failed");
113                 } else {
114                         vcdc_send_error_signal_to_manager(vcd_client_manager_get_pid(), VCD_ERROR_OPERATION_FAILED, "Engine recognition failed");
115                 }
116                 return 0;
117         }
118
119         if (VCP_SPEECH_DETECT_BEGIN == speech_detected) {
120                 if (-1 != vcd_client_manager_get_pid()) {
121                         /* Manager client is available */
122                         if (0 != vcdc_send_speech_detected(vcd_client_manager_get_pid())) {
123                                 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Fail to send speech detected");
124                         }
125                 }
126         } else if (VCP_SPEECH_DETECT_END == speech_detected) {
127                 if (VCD_RECOGNITION_MODE_STOP_BY_SILENCE == vcd_client_get_recognition_mode()) {
128                         /* silence detected */
129                         ecore_timer_add(0, __stop_by_silence, NULL);
130                 } else if (VCD_RECOGNITION_MODE_RESTART_AFTER_REJECT == vcd_client_get_recognition_mode()) {
131                         /* Stop engine recognition */
132                         int ret = vcd_engine_recognize_stop();
133                         if (0 != ret) {
134                                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to stop recognition : %d", ret);
135                         }
136                         vcd_config_set_service_state(VCD_STATE_PROCESSING);
137                         vcdc_send_service_state(VCD_STATE_PROCESSING);
138
139                         SLOG(LOG_DEBUG, TAG_VCD, "[Server] Stop engine only by silence");
140                 } else if (VCD_RECOGNITION_MODE_RESTART_CONTINUOUSLY == vcd_client_get_recognition_mode()) {
141                         /* Stop engine recognition */
142                         int ret = vcd_engine_recognize_stop();
143                         if (0 != ret) {
144                                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to stop recognition : %d", ret);
145                         }
146                 }
147         }
148
149         return 0;
150 }
151
152 void __server_recorder_interrupt_callback()
153 {
154         SLOG(LOG_DEBUG, TAG_VCD, "===== Cancel by sound interrupt");
155
156         ecore_timer_add(0, __cancel_by_interrupt, NULL);
157
158         SLOG(LOG_DEBUG, TAG_VCD, "=====");
159         SLOG(LOG_DEBUG, TAG_VCD, "  ");
160 }
161
162 static void __config_lang_changed_cb(const char* current_lang, void* user_data)
163 {
164         SLOG(LOG_DEBUG, TAG_VCD, "===== Change language ");
165
166         /* Current state is recording */
167         vcd_state_e state = vcd_config_get_service_state();
168         if (VCD_STATE_RECORDING == state || VCD_STATE_PROCESSING == state) {
169                 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Current state is 'Recording'. Cancel recognition");
170                 vcd_server_mgr_cancel();
171         }
172
173         int ret;
174         ret = vcd_engine_set_current_language(current_lang);
175         if (0 != ret) {
176                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to set language of engine : %d", ret);
177         }
178
179         SLOG(LOG_DEBUG, TAG_VCD, "=====");
180         SLOG(LOG_DEBUG, TAG_VCD, "  ");
181
182         return;
183 }
184
185 static void __config_foreground_changed_cb(int previous, int current, void* user_data)
186 {
187         SLOG(LOG_DEBUG, TAG_VCD, "===== Change foreground");
188
189         SLOG(LOG_DEBUG, TAG_VCD, "Foreground pid(%d)", current);
190
191         if (VC_NO_FOREGROUND_PID != current) {
192                 /* Foreground app is changed */
193                 vcd_state_e state = vcd_config_get_service_state();
194                 if (VCD_STATE_RECORDING == state) {
195                         SLOG(LOG_DEBUG, TAG_VCD, "[Server] Foreground pid(%d) is changed. Cancel recognition", current);
196                         ecore_timer_add(0, __cancel_by_interrupt, NULL);
197                 }
198         }
199
200         SLOG(LOG_DEBUG, TAG_VCD, "=====");
201         SLOG(LOG_DEBUG, TAG_VCD, "  ");
202
203         return;
204 }
205
206 static Eina_Bool __vcd_send_selected_result(void *data)
207 {
208         GSList* pid_list = NULL;
209         if (0 != vc_info_parser_get_result_pid_list(&pid_list)) {
210                 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Fail to get pid list. No result");
211         } else {
212                 if (0 < g_slist_length(pid_list)) {
213                         GSList* iter = NULL;
214                         vc_cmd_s* temp_cmd = NULL;
215                         int ret = 0;
216                         int count = 0;
217
218                         iter = g_slist_nth(pid_list, 0);
219                         while (NULL != iter) {
220                                 temp_cmd = iter->data;
221
222                                 if (NULL != temp_cmd) {
223                                         count = 0;
224                                         do {
225                                                 /* send result noti */
226                                                 ret = vcdc_send_result(temp_cmd->pid, temp_cmd->type);
227                                                 if (0 != ret) {
228                                                         SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Fail to send result");
229                                                         if (VCD_ERROR_TIMED_OUT != ret) {
230                                                                 break;
231                                                         }
232                                                 } else {
233                                                         SLOG(LOG_DEBUG, TAG_VCD, "[Server] Send result : pid(%d) type(%d)", temp_cmd->pid, temp_cmd->type);
234                                                 }
235                                                 count++;
236
237                                                 if (100 == count)       break;
238                                                 /* While is retry code */
239                                         } while (0 != ret);
240                                         free(temp_cmd);
241                                 }
242
243                                 pid_list = g_slist_remove_link(pid_list, iter);
244                                 iter = g_slist_nth(pid_list, 0);
245                         }
246                 }
247         }
248
249         if (VCD_RECOGNITION_MODE_RESTART_CONTINUOUSLY != vcd_client_get_recognition_mode()) {
250                 vcd_config_set_service_state(VCD_STATE_READY);
251                 vcdc_send_service_state(VCD_STATE_READY);
252         }
253
254         return EINA_FALSE;
255 }
256
257 static int __convert_type_to_priority(vc_cmd_type_e type)
258 {
259         switch (type) {
260                 case VC_COMMAND_TYPE_NONE:              return 0; break;
261                 case VC_COMMAND_TYPE_BACKGROUND:        return 1; break;
262                 case VC_COMMAND_TYPE_FOREGROUND:        return 2; break;
263                 case VC_COMMAND_TYPE_WIDGET:            return 2; break;
264                 case VC_COMMAND_TYPE_SYSTEM:            return 3; break;
265                 case VC_COMMAND_TYPE_EXCLUSIVE:         return 3; break;
266                 default:                                return 0; break;
267         }
268 }
269
270 static void __vcd_server_pre_result_cb(vcp_pre_result_event_e event, const char* pre_result, void* user_data)
271 {
272         if (NULL != pre_result) {
273                 vcdc_send_pre_result_to_manager(vcd_client_manager_get_pid(), event, pre_result);
274         }
275
276         return;
277 }
278
279 static void __vcd_server_result_cb(vcp_result_event_e event, int* result_id, int count, const char* all_result,
280                                    const char* non_fixed_result, const char* msg, const char* nlp_info, void *user_data)
281 {
282         if (VCD_STATE_PROCESSING != vcd_config_get_service_state()) {
283                 if (VCD_RECOGNITION_MODE_RESTART_CONTINUOUSLY != vcd_client_get_recognition_mode()) {
284                         SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Current state is not 'Processing' and mode is not 'Restart continuously'");
285                         return;
286                 }
287         }
288
289         // temp 
290         if (NULL != nlp_info) {
291                 if (0 != vc_info_parser_set_nlp_info(nlp_info)) {
292                         SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to save nlp info");
293                 }
294         }
295
296         vc_info_parser_unset_result(vcd_client_manager_get_exclusive());
297
298         SLOG(LOG_DEBUG, TAG_VCD, "[Server] Event(%d), Text(%s) Nonfixed(%s) Msg(%s) Result count(%d)", 
299                 event, all_result, non_fixed_result, msg, count);
300
301         if (VCD_RECOGNITION_MODE_RESTART_AFTER_REJECT == vcd_client_get_recognition_mode()) {
302                 if (VCP_RESULT_EVENT_REJECTED == event) {
303                         SLOG(LOG_DEBUG, TAG_VCD, "[Server] Restart by no or rejected result");
304                         /* If no result and restart option is ON */
305                         /* Send reject message */
306                         bool temp = vcd_client_manager_get_exclusive();
307                         vc_info_parser_set_result(all_result, event, msg, NULL, temp);
308                         if (0 != vcdc_send_result_to_manager(vcd_client_manager_get_pid(), VC_RESULT_TYPE_NOTIFICATION)) {
309                                 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Fail to send result");
310                         }
311
312                         g_restart_timer = ecore_timer_add(0, __restart_engine, NULL);
313                         return;
314                 }
315                 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Stop recorder due to success");
316                 vcd_recorder_stop();
317         } else if (VCD_RECOGNITION_MODE_RESTART_CONTINUOUSLY == vcd_client_get_recognition_mode()) {
318                 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Restart continuously");
319                 /* Restart option is ON */
320                 g_restart_timer = ecore_timer_add(0, __restart_engine, NULL);
321                 if (VCP_RESULT_EVENT_REJECTED == event) {
322                         bool temp = vcd_client_manager_get_exclusive();
323                         vc_info_parser_set_result(all_result, event, msg, NULL, temp);
324                         if (0 != vcdc_send_result_to_manager(vcd_client_manager_get_pid(), VC_RESULT_TYPE_NOTIFICATION)) {
325                                 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Fail to send result");
326                         }
327                         return;
328                 }
329         }
330
331         /* No result */
332         if (NULL == result_id) {
333                 /* No result */
334                 if (NULL != all_result) {
335                         SLOG(LOG_DEBUG, TAG_VCD, "[Server] Engine result is no command : %s", all_result);
336                         bool temp = vcd_client_manager_get_exclusive();
337                         vc_info_parser_set_result(all_result, event, msg, NULL, temp);
338                 }
339
340                 int pid = vcd_client_widget_get_foreground_pid();
341                 if (-1 != pid) {
342                         if (NULL != all_result) {
343                                 /* Send result text to widget */
344                                 vcdc_send_result(pid, VC_COMMAND_TYPE_WIDGET);
345                         }
346
347                         SLOG(LOG_DEBUG, TAG_VCD, "[Server] Request tooltip hide");
348                         /* Send to hide tooltip */
349                         vcdc_send_show_tooltip(pid, false);
350                 }
351
352                 if (-1 != vcd_client_manager_get_pid()) {
353                         /* Manager client is available */
354                         if (0 != vcdc_send_result_to_manager(vcd_client_manager_get_pid(), VC_RESULT_TYPE_NORMAL)) {
355                                 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Fail to send result");
356                         }
357                 }
358
359                 vcd_client_manager_set_exclusive(false);
360
361                 return;
362         }
363
364         /* Normal result */
365         SLOG(LOG_DEBUG, TAG_VCD, "[Server] === Get engine result ===");
366
367         int ret = -1;
368         vc_cmd_s* temp_cmd = NULL;
369         vc_cmd_list_h vc_cmd_list = NULL;
370
371         if (0 != vc_cmd_list_create(&vc_cmd_list)) {
372                 SLOG(LOG_ERROR, TAG_VCD, "[Server] Fail to create command list");
373                 vcd_client_manager_set_exclusive(false);
374                 vcd_config_set_service_state(VCD_STATE_READY);
375                 vcdc_send_service_state(VCD_STATE_READY);
376                 return;
377         }
378
379         int i = 0;
380         int priority = 0;
381         for (i = 0; i < count; i++) {
382                 SLOG(LOG_DEBUG, TAG_VCD, "[Server]   [%d] Result ID(%d)", i, result_id[i]);
383
384                 if (result_id[i] < 0) {
385                         SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Result ID(%d) is NOT valid", result_id[i]);
386                         continue;
387                 }
388
389                 ret = vcd_client_get_cmd_from_result_id(result_id[i], &temp_cmd);
390                 if (0 == ret && NULL != temp_cmd) {
391                         /* Add priority filter */
392                         int temp_priority = __convert_type_to_priority(temp_cmd->type);
393                         if (priority > temp_priority) {
394                                 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Ignore result by priority");
395                                 continue;
396                         } else if (priority < temp_priority) {
397                                 SLOG(LOG_DEBUG, TAG_VCD, "[Server] High priority result!!");
398                                 priority = temp_priority;
399
400                                 if (0 != vc_cmd_list_remove_all(vc_cmd_list, true)) {
401                                         SLOG(LOG_ERROR, TAG_VCD, "[Server] Fail to list remove all");
402                                 }
403                         }
404
405                         switch (temp_cmd->format) {
406                         case VC_CMD_FORMAT_FIXED:
407                                 /* Nonfixed result is NOT valid */
408                                 break;
409                         case VC_CMD_FORMAT_FIXED_AND_EXTRA:
410                                 if (NULL == temp_cmd->parameter) {
411                                         if (NULL != non_fixed_result) {
412                                                 temp_cmd->parameter = strdup(non_fixed_result);
413                                         }
414                                 } else {
415                                         SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Command type is NOT vaild. Parameter (%s)", temp_cmd->parameter);
416                                 }
417                                 break;
418                         case VC_CMD_FORMAT_EXTRA_AND_FIXED:
419                                 if (NULL == temp_cmd->command) {
420                                         if (NULL != non_fixed_result) {
421                                                 temp_cmd->command = strdup(non_fixed_result);
422                                         }
423                                 } else {
424                                         SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Command type is NOT vaild. Command (%s)", temp_cmd->command);
425                                 }
426
427                                 break;
428                         default:
429                                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Unknown command type : %d", temp_cmd->type);
430                         }
431
432                         temp_cmd->id = i;
433                         if (0 != vc_cmd_list_add(vc_cmd_list, (vc_cmd_h)temp_cmd)) {
434                                 SLOG(LOG_DEBUG, TAG_VCD, "Fail to add command to list");
435                                 vc_cmd_destroy((vc_cmd_h)temp_cmd);
436                         }
437                 } else {
438                         SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] NOT found matached result(%d)", result_id[i]);
439                 }
440         }
441
442         vc_cmd_print_list(vc_cmd_list);
443
444         SLOG(LOG_DEBUG, TAG_VCD, "[Server] =========================");
445
446         int result_count = 0;
447         vc_cmd_list_get_count(vc_cmd_list, &result_count);
448
449         if (false == vcd_client_manager_get_exclusive()) {
450                 int pid = vcd_client_widget_get_foreground_pid();
451                 if (-1 != pid) {
452                         SLOG(LOG_DEBUG, TAG_VCD, "[Server] Request tooltip hide");
453                         vcdc_send_show_tooltip(pid, false);
454                 }
455
456                 vc_info_parser_set_result(all_result, event, msg, vc_cmd_list, false);
457
458                 if (-1 != vcd_client_manager_get_pid()) {
459                         /* Manager client is available */
460                         if (0 != vcdc_send_result_to_manager(vcd_client_manager_get_pid(), VC_RESULT_TYPE_NORMAL)) {
461                                 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Fail to send result");
462                         }
463                 } else {
464                         SLOG(LOG_DEBUG, TAG_VCD, "[Server] Manager is NOT available. Send result to client directly");
465                         /* Send result to client */
466                         ecore_timer_add(0, __vcd_send_selected_result, NULL);
467                 }
468         } else {
469                 /* exclusive command */
470                 vc_info_parser_set_result(all_result, event, msg, vc_cmd_list, true);
471
472                 if (-1 != vcd_client_manager_get_pid()) {
473                         /* Manager client is available */
474                         if (0 != vcdc_send_result_to_manager(vcd_client_manager_get_pid(), VC_RESULT_TYPE_NORMAL)) {
475                                 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Fail to send result");
476                         }
477                 } else {
478                         SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Manager is NOT available");
479                 }
480
481                 vcd_client_manager_set_exclusive(false);
482         }
483
484         vc_cmd_list_destroy(vc_cmd_list, true);
485
486         return;
487 }
488
489 /*
490 * vcd server Interfaces
491 */
492 static void __vcd_file_clean_up()
493 {
494         SLOG(LOG_DEBUG, TAG_VCD, "== Old file clean up == ");
495
496         DIR *dp = NULL;
497         int ret = -1;
498         struct dirent entry;
499         struct dirent *dirp = NULL;
500
501         dp = opendir(VC_RUNTIME_INFO_ROOT);
502         if (dp == NULL) {
503                 SLOG(LOG_ERROR, TAG_VCD, "[File message WARN] Fail to open path : %s", VC_RUNTIME_INFO_ROOT);
504                 return;
505         }
506
507         char remove_path[256] = {0, };
508         do {
509                 ret = readdir_r(dp, &entry, &dirp);
510                 if (0 != ret) {
511                         SLOG(LOG_ERROR, TAG_VCD, "[File ERROR] Fail to read directory");
512                         break;
513                 }
514
515                 if (NULL != dirp) {
516                         if (!strncmp("vc_", dirp->d_name, strlen("vc_"))) {
517                                 memset(remove_path, 0, 256);
518                                 snprintf(remove_path, 256, "%s/%s", VC_RUNTIME_INFO_ROOT, dirp->d_name);
519
520                                 /* Clean up code */
521                                 if (0 != remove(remove_path)) {
522                                         SLOG(LOG_WARN, TAG_VCD, "[File message WARN] Fail to remove file : %s", remove_path);
523                                 } else {
524                                         SLOG(LOG_DEBUG, TAG_VCD, "[File message] Remove file : %s", remove_path);
525                                 }
526                         }
527                 }
528         } while (NULL != dirp);
529
530         closedir(dp);
531
532         return;
533 }
534
535 int vcd_initialize()
536 {
537         int ret = 0;
538
539         /* Remove old file */
540         __vcd_file_clean_up();
541         /* Send error signal for notifying that daemon is reset*/
542         vcdc_send_error_signal(VCD_ERROR_SERVICE_RESET, "Daemon reset");
543
544         /* initialize modules */
545         ret = vcd_config_initialize(__config_lang_changed_cb, __config_foreground_changed_cb, NULL);
546         if (0 != ret) {
547                 SLOG(LOG_ERROR, TAG_VCD, "[Server WARNING] Fail to initialize config.");
548         }
549
550         vcd_config_set_service_state(VCD_STATE_NONE);
551
552         ret = vcd_engine_agent_init(__vcd_server_pre_result_cb, __vcd_server_result_cb);
553         if (0 != ret) {
554                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to engine agent initialize : result(%d)", ret);
555                 return ret;
556         }
557
558         if (0 != vcd_recorder_create(__server_recorder_callback, __server_recorder_interrupt_callback)) {
559                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to create recorder");
560                 return VCD_ERROR_OPERATION_FAILED;
561         }
562
563         /* Find engine */
564         ret = vcd_engine_agent_initialize_current_engine();
565         if (0 != ret) {
566                 if (VCD_ERROR_ENGINE_NOT_FOUND == ret)
567                         SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] There is No Voice control engine");
568                 else
569                         SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to init engine");
570
571                 g_is_engine = false;
572         } else {
573                 g_is_engine = true;
574         }
575
576         /* Load engine */
577         if (0 != vcd_engine_agent_load_current_engine()) {
578                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to load current engine");
579                 return VCD_ERROR_OPERATION_FAILED;
580         }
581
582         /* Initialize manager info */
583         vcd_client_manager_unset();
584
585         vcd_config_set_service_state(VCD_STATE_READY);
586         vcdc_send_service_state(VCD_STATE_READY);
587
588         SLOG(LOG_DEBUG, TAG_VCD, "[Server SUCCESS] initialize");
589
590         return 0;
591 }
592
593 void vcd_finalize()
594 {
595         GList *iter = NULL;
596         if (0 < g_list_length(g_proc_list)) {
597                 iter = g_list_first(g_proc_list);
598                 while (NULL != iter) {
599                         g_proc_list = g_list_remove_link(g_proc_list, iter);
600                         iter = g_list_first(g_proc_list);
601                 }
602         }
603
604         if (g_restart_timer != NULL) {
605                 ecore_timer_del(g_restart_timer);
606                 g_restart_timer = NULL;
607         }
608
609         vcd_state_e state = vcd_config_get_service_state();
610         if (VCD_STATE_READY != state) {
611                 if (VCD_STATE_RECORDING == state) {
612                         vcd_recorder_stop();
613                 }
614                 vcd_engine_recognize_cancel();
615         }
616         if (0 != vcd_recorder_destroy()) {
617                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to destroy recorder");
618         } else {
619                 SLOG(LOG_DEBUG, TAG_VCD, "[Server] destroy recorder");
620         }
621
622         if (0 != vcd_engine_agent_release()) {
623                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to release engine");
624         } else {
625                 SLOG(LOG_DEBUG, TAG_VCD, "[Server] release engine");
626         }
627
628         vcd_config_set_service_state(VCD_STATE_NONE);
629         vcdc_send_service_state(VCD_STATE_NONE);
630
631         SLOG(LOG_DEBUG, TAG_VCD, "[Server] mode finalize");
632
633         return;
634 }
635
636 static Eina_Bool __finalize_quit_ecore_loop(void *data)
637 {
638         SLOG(LOG_DEBUG, TAG_VCD, "[Server] quit ecore main loop");
639         ecore_main_loop_quit();
640         return EINA_FALSE;
641 }
642
643 static void __read_proc()
644 {
645         DIR *dp = NULL;
646         struct dirent entry;
647         struct dirent *dirp = NULL;
648         int ret = -1;
649         int tmp;
650
651         GList *iter = NULL;
652         if (0 < g_list_length(g_proc_list)) {
653                 iter = g_list_first(g_proc_list);
654                 while (NULL != iter) {
655                         g_proc_list = g_list_remove_link(g_proc_list, iter);
656                         iter = g_list_first(g_proc_list);
657                 }
658         }
659
660         dp = opendir("/proc");
661         if (NULL == dp) {
662                 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Fail to open proc");
663         } else {
664                 do {
665                         ret = readdir_r(dp, &entry, &dirp);
666                         if (0 != ret) {
667                                 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Fail to readdir");
668                                 break;
669                         }
670
671                         if (NULL != dirp) {
672                                 tmp = atoi(dirp->d_name);
673                                 if (0 >= tmp)   continue;
674                                 g_proc_list = g_list_append(g_proc_list, GINT_TO_POINTER(tmp));
675                         }
676                 } while (NULL != dirp);
677                 closedir(dp);
678         }
679         return;
680 }
681
682 static void __vcd_cleanup_client(vcd_client_type_e type)
683 {
684         int* client_list = NULL;
685         int client_count = 0;
686         int i = 0;
687         int j = 0;
688         bool exist = false;
689         int mgr_pid = -1;
690         int ret = -1;
691
692         if (VCD_CLIENT_TYPE_NORMAL == type) {
693                 ret = vcd_client_get_list(&client_list, &client_count);
694         } else if (VCD_CLIENT_TYPE_WIDGET == type) {
695                 ret = vcd_client_widget_get_list(&client_list, &client_count);
696         } else if (VCD_CLIENT_TYPE_MANAGER == type) {
697                 mgr_pid = vcd_client_manager_get_pid();
698                 client_list = &mgr_pid;
699                 client_count = 1;
700         }
701
702         if (0 == ret || mgr_pid > 0) {
703                 SLOG(LOG_DEBUG, TAG_VCD, "===== Clean up %s client ", type ? (type == 1) ? "Widget" : "Manager" : "Normal");
704                 if (NULL != client_list && client_count > 0) {
705                         for (i = 0; i < client_count; i++) {
706                                 exist = false;
707                                 GList *iter = NULL;
708                                 for (j = 0; j < g_list_length(g_proc_list); j++) {
709                                         iter = g_list_nth(g_proc_list, j);
710                                         if (NULL != iter) {
711                                                 if (client_list[i] == GPOINTER_TO_INT(iter->data)) {
712                                                         SLOG(LOG_DEBUG, TAG_VCD, "%s pid(%d) is running", type ? (type == 1) ? "Widget" : "Manager" : "Normal", client_list[i]);
713                                                         exist = true;
714                                                         break;
715                                                 }
716                                         }
717                                 }
718
719                                 if (false == exist) {
720                                         SLOG(LOG_ERROR, TAG_VCD, "%s pid(%d) should be removed", type ? (type == 1) ? "Widget" : "Manager" : "Normal", client_list[i]);
721                                         if (VCD_CLIENT_TYPE_NORMAL == type)
722                                                 vcd_server_finalize(client_list[i]);
723                                         else if (VCD_CLIENT_TYPE_WIDGET == type)
724                                                 vcd_server_widget_finalize(client_list[i]);
725                                         else
726                                                 vcd_server_mgr_finalize(mgr_pid);
727                                 }
728                         }
729                 }
730                 SLOG(LOG_DEBUG, TAG_VCD, "=====");
731                 SLOG(LOG_DEBUG, TAG_VCD, "  ");
732         }
733         if (NULL != client_list && -1 == mgr_pid) {
734                 free(client_list);
735                 client_list = NULL;
736         }
737         return;
738 }
739
740 Eina_Bool vcd_cleanup_client_all(void *data)
741 {
742         __read_proc();
743         
744         __vcd_cleanup_client(VCD_CLIENT_TYPE_NORMAL);
745         __vcd_cleanup_client(VCD_CLIENT_TYPE_WIDGET);
746         __vcd_cleanup_client(VCD_CLIENT_TYPE_MANAGER);
747
748 #if 0
749         if (0 == vcd_client_get_list(&client_list, &client_count)) {
750                 SLOG(LOG_DEBUG, TAG_VCD, "===== Clean up client ");
751                 if (NULL != client_list && client_count > 0) {
752                         for (i = 0; i < client_count; i++) {
753                                 exist = false;
754                                 GList *iter = NULL;
755                                 for (j = 0; j < g_list_length(g_proc_list); j++) {
756                                         iter = g_list_nth(g_proc_list, j);
757                                         if (NULL != iter) {
758                                                 if (client_list[i] == GPOINTER_TO_INT(iter->data)) {
759                                                         SLOG(LOG_DEBUG, TAG_VCD, "pid(%d) is running", client_list[i]);
760                                                         exist = true;
761                                                         break;
762                                                 }
763                                         }
764                                 }
765
766                                 if (false == exist) {
767                                         SLOG(LOG_ERROR, TAG_VCD, "pid(%d) should be removed", client_list[i]);
768                                         vcd_server_finalize(client_list[i]);
769                                 }
770 #if 0
771                                 result = vcdc_send_hello(client_list[i], VCD_CLIENT_TYPE_NORMAL);
772
773                                 if (0 == result) {
774                                         SLOG(LOG_DEBUG, TAG_VCD, "[Server] pid(%d) should be removed.", client_list[i]);
775                                         vcd_server_finalize(client_list[i]);
776                                 } else if (-1 == result) {
777                                         SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Hello result has error");
778                                 }
779 #endif
780                         }
781                 }
782                 SLOG(LOG_DEBUG, TAG_VCD, "=====");
783                 SLOG(LOG_DEBUG, TAG_VCD, "  ");
784         }
785         if (NULL != client_list) {
786                 free(client_list);
787                 client_list = NULL;
788         }
789
790         /* If app is in background state, app cannot response message. */
791         if (0 == vcd_client_widget_get_list(&client_list, &client_count)) {
792                 SLOG(LOG_DEBUG, TAG_VCD, "===== Clean up widget");
793                 if (NULL != client_list && client_count > 0) {
794                         for (i = 0; i < client_count; i++) {
795                                 exist = false;
796                                 GList *iter = NULL;
797                                 for (j = 0; j < g_list_length(g_proc_list); j++) {
798                                         iter = g_list_nth(g_proc_list, j);
799                                         if (NULL != iter) {
800                                                 if (client_list[i] == GPOINTER_TO_INT(iter->data)) {
801                                                         SLOG(LOG_DEBUG, TAG_VCD, "widget pid(%d) is running", client_list[i]);
802                                                         exist = true;
803                                                         break;
804                                                 }
805                                         }
806                                 }
807
808                                 if (false == exist) {
809                                         SLOG(LOG_ERROR, TAG_VCD, "widget pid(%d) should be removed", client_list[i]);
810                                         vcd_server_widget_finalize(client_list[i]);
811                                 }
812 #if 0
813                                 result = vcdc_send_hello(client_list[i], VCD_CLIENT_TYPE_WIDGET);
814
815                                 if (0 == result) {
816                                         SLOG(LOG_DEBUG, TAG_VCD, "[Server] widget pid(%d) should be removed.", client_list[i]);
817                                         vcd_server_widget_finalize(client_list[i]);
818                                 } else if (-1 == result) {
819                                         SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Hello result has error");
820                                 }
821 #endif
822                         }
823                 }
824                 SLOG(LOG_DEBUG, TAG_VCD, "=====");
825                 SLOG(LOG_DEBUG, TAG_VCD, "  ");
826         }
827
828         if (NULL != client_list) {
829                 free(client_list);
830                 client_list = NULL;
831         }
832
833         /* manager */
834         exist = false;
835         GList *iter = NULL;
836         int mgr_pid = vcd_client_manager_get_pid();
837         if (0 < mgr_pid) {
838                 for (j = 0; j < g_list_length(g_proc_list); j++) {
839                         iter = g_list_nth(g_proc_list, j);
840                         if (NULL != iter) {
841                                 if (mgr_pid == GPOINTER_TO_INT(iter->data)) {
842                                         SLOG(LOG_DEBUG, TAG_VCD, "manager pid(%d) is running", mgr_pid);
843                                         exist = true;
844                                         break;
845                                 }
846                         }
847                 }
848
849                 if (false == exist) {
850                         SLOG(LOG_ERROR, TAG_VCD, "manager pid (%d) should be removed", mgr_pid);
851                         vcd_server_mgr_finalize(mgr_pid);
852                 }
853         }
854 #endif
855         return EINA_TRUE;
856 }
857
858 int vcd_server_get_service_state()
859 {
860         return vcd_config_get_service_state();
861 }
862
863 int vcd_server_get_foreground()
864 {
865         int pid;
866         vcd_config_get_foreground(&pid);
867         return pid;
868 }
869
870
871 /*
872 * API for manager
873 */
874 int vcd_server_mgr_initialize(int pid)
875 {
876         if (false == g_is_engine) {
877                 if (0 != vcd_engine_agent_initialize_current_engine()) {
878                         SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] No Engine");
879                         g_is_engine = false;
880                         return VCD_ERROR_ENGINE_NOT_FOUND;
881                 } else {
882                         g_is_engine = true;
883                 }
884         }
885
886         /* check if pid is valid */
887         if (false == vcd_client_manager_is_valid(pid)) {
888                 SLOG(LOG_ERROR, TAG_VCD, "[Server] old manager pid(%d) be removed", vcd_client_manager_get_pid());
889                 vcd_server_mgr_cancel();
890                 vcd_client_manager_unset();
891         }
892
893         /* Add client information to client manager */
894         if (0 != vcd_client_manager_set(pid)) {
895                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to add manager");
896                 return VCD_ERROR_OPERATION_FAILED;
897         }
898
899         SLOG(LOG_DEBUG, TAG_VCD, "[Server Success] Manager initialize : pid(%d)", pid);
900
901         return VCD_ERROR_NONE;
902 }
903
904 int vcd_server_mgr_finalize(int pid)
905 {
906         /* check if pid is valid */
907         if (false == vcd_client_manager_is_valid(pid)) {
908                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] The manager pid(%d) is NOT valid", pid);
909                 return VCD_ERROR_INVALID_PARAMETER;
910         }
911
912         /* Cancel recognition */
913         vcd_server_mgr_cancel();
914
915         /* Remove manager information */
916         if (0 != vcd_client_manager_unset()) {
917                 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Fail to delete client");
918         }
919
920         if (0 == vcd_client_get_ref_count()) {
921                 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Connected client list is empty");
922                 ecore_timer_add(0, __finalize_quit_ecore_loop, NULL);
923         }
924
925         SLOG(LOG_DEBUG, TAG_VCD, "[Server Success] Manager Finalize : pid(%d)", pid);
926
927         return VCD_ERROR_NONE;
928 }
929
930 int vcd_server_mgr_set_command(int pid)
931 {
932         if (0 != vcd_client_manager_set_command(pid)) {
933                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] The manager pid(%d) is NOT valid", pid);
934                 return VCD_ERROR_INVALID_PARAMETER;
935         }
936         return VCD_ERROR_NONE;
937 }
938
939 int vcd_server_mgr_unset_command(int pid)
940 {
941         if (0 != vcd_client_manager_unset_command(pid)) {
942                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] The manager pid(%d) is NOT valid", pid);
943                 return VCD_ERROR_INVALID_PARAMETER;
944         }
945         return VCD_ERROR_NONE;
946 }
947
948 int vcd_server_mgr_set_demandable_client(int pid)
949 {
950         /* check if pid is valid */
951         if (false == vcd_client_manager_is_valid(pid)) {
952                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] The manager pid(%d) is NOT valid", pid);
953                 return VCD_ERROR_INVALID_PARAMETER;
954         }
955
956         GSList* client_list = NULL;
957         if (0 != vc_info_parser_get_demandable_clients(&client_list)) {
958                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to get demandable client");
959                 return VCD_ERROR_OPERATION_FAILED;
960         }
961
962         /* Save client list */
963         if (0 != vcd_client_manager_set_demandable_client(pid, client_list)) {
964                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to set demandable client list");
965                 return VCD_ERROR_OPERATION_FAILED;
966         }
967
968         return VCD_ERROR_NONE;
969 }
970
971 int vcd_server_mgr_set_audio_type(int pid, const char* audio_type)
972 {
973         /* check if pid is valid */
974         if (false == vcd_client_manager_is_valid(pid)) {
975                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] The manager pid(%d) is NOT valid", pid);
976                 return VCD_ERROR_INVALID_PARAMETER;
977         }
978
979         int ret = 0;
980         vcp_audio_type_e type = VCP_AUDIO_TYPE_PCM_S16_LE;
981         int rate = 16000;
982         int channel = 1;
983
984         ret = vcd_engine_get_audio_format(audio_type, &type, &rate, &channel);
985         if (0 != ret) {
986                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to get audio format : %d", ret);
987                 return VCD_ERROR_OPERATION_FAILED;
988         }
989
990         ret = vcd_recorder_set(audio_type, type, rate, channel);
991         if (0 != ret) {
992                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to set audio in type : %d", ret);
993                 return VCD_ERROR_OPERATION_FAILED;
994         }
995
996         return VCD_ERROR_NONE;
997 }
998
999 int vcd_server_mgr_get_audio_type(int pid, char** audio_type)
1000 {
1001         /* check if pid is valid */
1002         if (false == vcd_client_manager_is_valid(pid)) {
1003                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] The manager pid(%d) is NOT valid", pid);
1004                 return VCD_ERROR_INVALID_PARAMETER;
1005         }
1006
1007         int ret = vcd_recorder_get(audio_type);
1008         if (0 != ret) {
1009                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to get audio in type : %d", ret);
1010                 return VCD_ERROR_OPERATION_FAILED;
1011         }
1012
1013         return VCD_ERROR_NONE;
1014 }
1015
1016 int vcd_server_mgr_set_client_info(int pid)
1017 {
1018         /* check if pid is valid */
1019         if (false == vcd_client_manager_is_valid(pid)) {
1020                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] The manager pid(%d) is NOT valid", pid);
1021                 return VCD_ERROR_INVALID_PARAMETER;
1022         }
1023
1024         int ret = vcd_client_save_client_info();
1025         if (0 != ret) {
1026                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to save client info : %d", ret);
1027                 return VCD_ERROR_OPERATION_FAILED;
1028         }
1029
1030         return VCD_ERROR_NONE;
1031 }
1032
1033 static int __start_internal_recognition()
1034 {
1035         int ret;
1036
1037         /* 2. Get commands */
1038         ret = vcd_client_command_collect_command();
1039         if (0 != ret) {
1040                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to collect command : %d", ret);
1041                 return VCD_ERROR_OPERATION_FAILED;
1042         }
1043
1044         ret = vcd_client_get_length();
1045         if (0 == ret) {
1046                 SLOG(LOG_WARN, TAG_VCD, "[Server WARNIING] No current command : %d", ret);
1047                 return VCD_ERROR_OPERATION_FAILED;
1048         }
1049
1050         /* 3. Set command to engine */
1051         ret = vcd_engine_set_commands();
1052         if (0 != ret) {
1053                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to collect command : %d", ret);
1054                 return VCD_ERROR_OPERATION_FAILED;
1055         }
1056
1057         SLOG(LOG_DEBUG, TAG_VCD, "[Server Success] Set command");
1058
1059         bool stop_by_silence = true;
1060         if (VCD_RECOGNITION_MODE_MANUAL == vcd_client_get_recognition_mode()) {
1061                 stop_by_silence = false;
1062         }
1063
1064         /* 4. start recognition */
1065         ret = vcd_engine_recognize_start(stop_by_silence);
1066         if (0 != ret) {
1067                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to start recognition : result(%d)", ret);
1068                 return VCD_ERROR_OPERATION_FAILED;
1069         }
1070
1071         SLOG(LOG_DEBUG, TAG_VCD, "[Server Success] Start engine");
1072
1073         /* 5. recorder start */
1074         ret = vcd_recorder_start();
1075         if (0 != ret) {
1076                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to start recorder : result(%d)", ret);
1077                 vcd_engine_recognize_cancel();
1078                 return ret;
1079         }
1080
1081         vcd_config_set_service_state(VCD_STATE_RECORDING);
1082         vcdc_send_service_state(VCD_STATE_RECORDING);
1083
1084         SLOG(LOG_DEBUG, TAG_VCD, "[Server Success] Start recognition(%d)", stop_by_silence);
1085
1086         return 0;
1087 }
1088
1089 static Eina_Bool __vcd_request_show_tooltip(void *data)
1090 {
1091         int pid = vcd_client_widget_get_foreground_pid();
1092         if (-1 != pid) {
1093                 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Request tooltip show and widget command");
1094                 vcdc_send_show_tooltip(pid, (bool)data);
1095         }
1096
1097         return EINA_FALSE;
1098 }
1099
1100 int vcd_server_mgr_start(vcd_recognition_mode_e recognition_mode, bool exclusive_cmd, bool start_by_client)
1101 {
1102         /* 1. check current state */
1103         vcd_state_e state = vcd_config_get_service_state();
1104
1105         if (VCD_STATE_READY != state) {
1106                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Current state is not ready");
1107                 return VCD_ERROR_INVALID_STATE;
1108         }
1109
1110         SLOG(LOG_DEBUG, TAG_VCD, "[Server] set recognition mode = %d", recognition_mode);
1111         vcd_client_set_recognition_mode(recognition_mode);
1112
1113         if (false == exclusive_cmd) {
1114                 /* Notify show tooltip */
1115                 int pid = vcd_client_widget_get_foreground_pid();
1116                 if (-1 != pid) {
1117                         SLOG(LOG_DEBUG, TAG_VCD, "[Server] Request tooltip show and widget command");
1118                         ecore_timer_add(0, __vcd_request_show_tooltip, (void*)true);
1119                         return 0;
1120                 }
1121         } else {
1122                 vcd_client_manager_set_exclusive(exclusive_cmd);
1123         }
1124
1125         int fg_pid = -1;
1126         if (true == start_by_client) {
1127                 /* Get foreground pid */
1128                 if (0 != vcd_config_get_foreground(&fg_pid)) {
1129                         SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to get foreground");
1130                 }
1131
1132                 /* Set client exclusive option */
1133                 if (0 != vcd_client_set_exclusive_command(fg_pid)) {
1134                         SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to set exclusive command");
1135                 }
1136         }
1137
1138         int ret = __start_internal_recognition();
1139         if (0 != ret) {
1140                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to start recongition : %d", ret);
1141                 return ret;
1142         }
1143
1144         if (true == start_by_client) {
1145                 vcd_client_unset_exclusive_command(fg_pid);
1146         }
1147
1148         return VCD_ERROR_NONE;
1149 }
1150
1151 int vcd_server_mgr_stop()
1152 {
1153         /* 1. Check current state is recording */
1154         if (VCD_STATE_RECORDING != vcd_config_get_service_state()) {
1155                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Current state is not recording");
1156                 return VCD_ERROR_INVALID_STATE;
1157         }
1158
1159         /* 2. Stop recorder */
1160         vcd_recorder_stop();
1161
1162         /* 3. Stop engine recognition */
1163         int ret = vcd_engine_recognize_stop();
1164         if (0 != ret) {
1165                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to stop recognition : %d", ret);
1166         }
1167
1168         /* 4. Set original mode */
1169         vcd_config_set_service_state(VCD_STATE_PROCESSING);
1170         vcdc_send_service_state(VCD_STATE_PROCESSING);
1171
1172         return VCD_ERROR_NONE;
1173 }
1174
1175 int vcd_server_mgr_cancel()
1176 {
1177         /* 1. Check current state */
1178         vcd_state_e state = vcd_config_get_service_state();
1179         if (VCD_STATE_RECORDING != state && VCD_STATE_PROCESSING != state) {
1180                 SLOG(LOG_WARN, TAG_VCD, "[Server ERROR] Current state is not recording or processing");
1181                 return VCD_ERROR_INVALID_STATE;
1182         }
1183
1184         if (g_restart_timer != NULL) {
1185                 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Delete restart engine timer");
1186                 ecore_timer_del(g_restart_timer);
1187                 g_restart_timer = NULL;
1188         }
1189
1190         /* 2. Stop recorder */
1191         vcd_recorder_stop();
1192         /* 3. Cancel engine */
1193         int ret = vcd_engine_recognize_cancel();
1194         if (0 != ret) {
1195                 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Fail to cancel : %d", ret);
1196         }
1197
1198         if (false == vcd_client_manager_get_exclusive()) {
1199                 int pid = vcd_client_widget_get_foreground_pid();
1200                 if (-1 != pid) {
1201                         SLOG(LOG_DEBUG, TAG_VCD, "[Server] Request tooltip hide");
1202                         ecore_timer_add(0, __vcd_request_show_tooltip, (void*)false);
1203                 }
1204         } else {
1205                 vcd_client_manager_set_exclusive(false);
1206         }
1207
1208         /* 4. Set state */
1209         vcd_config_set_service_state(VCD_STATE_READY);
1210         vcdc_send_service_state(VCD_STATE_READY);
1211
1212         return VCD_ERROR_NONE;
1213 }
1214
1215
1216 int vcd_server_mgr_result_select()
1217 {
1218         __vcd_send_selected_result(NULL);
1219
1220         return VCD_ERROR_NONE;
1221 }
1222
1223 /*
1224 * VC Server Functions for Client
1225 */
1226 int vcd_server_initialize(int pid)
1227 {
1228         if (false == g_is_engine) {
1229                 if (0 != vcd_engine_agent_initialize_current_engine()) {
1230                         SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] No Engine");
1231                         g_is_engine = false;
1232                         return VCD_ERROR_ENGINE_NOT_FOUND;
1233                 } else {
1234                         g_is_engine = true;
1235                 }
1236         }
1237
1238         if (false == vcd_engine_is_available_engine()) {
1239                 if (0 != vcd_engine_agent_initialize_current_engine()) {
1240                         SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] No Engine");
1241                         return VCD_ERROR_ENGINE_NOT_FOUND;
1242                 }
1243         }
1244
1245         /* check if pid is valid */
1246         if (true == vcd_client_is_available(pid)) {
1247                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] The pid is already exist");
1248                 return VCD_ERROR_INVALID_PARAMETER;
1249         }
1250
1251         /* Add client information to client manager */
1252         if (0 != vcd_client_add(pid)) {
1253                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to add client info");
1254                 return VCD_ERROR_OPERATION_FAILED;
1255         }
1256
1257         SLOG(LOG_DEBUG, TAG_VCD, "[Server Success] Client Initialize : pid(%d)", pid);
1258
1259         return VCD_ERROR_NONE;
1260 }
1261
1262 int vcd_server_finalize(int pid)
1263 {
1264         /* check if pid is valid */
1265         if (false == vcd_client_is_available(pid)) {
1266                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] pid is NOT valid ");
1267                 return VCD_ERROR_INVALID_PARAMETER;
1268         }
1269
1270         /* Remove client information */
1271         if (0 != vcd_client_delete(pid)) {
1272                 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Fail to delete client");
1273         }
1274
1275         if (0 == vcd_client_get_ref_count()) {
1276                 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Connected client list is empty");
1277                 ecore_timer_add(0, __finalize_quit_ecore_loop, NULL);
1278         }
1279
1280         SLOG(LOG_DEBUG, TAG_VCD, "[Server Success] Client Finalize : pid(%d)", pid);
1281
1282         return VCD_ERROR_NONE;
1283 }
1284
1285 int vcd_server_set_command(int pid, vc_cmd_type_e cmd_type)
1286 {
1287         /* check if pid is valid */
1288         if (false == vcd_client_is_available(pid)) {
1289                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] pid is NOT valid ");
1290                 return VCD_ERROR_INVALID_PARAMETER;
1291         }
1292
1293         if (0 != vcd_client_set_command_type(pid, cmd_type)) {
1294                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to set command type : pid(%d), cmd_type(%d)", pid, cmd_type);
1295                 return VCD_ERROR_OPERATION_FAILED;
1296         }
1297
1298         return 0;
1299 }
1300
1301 int vcd_server_unset_command(int pid, vc_cmd_type_e cmd_type)
1302 {
1303         /* check if pid is valid */
1304         if (false == vcd_client_is_available(pid)) {
1305                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] pid is NOT valid ");
1306                 return VCD_ERROR_INVALID_PARAMETER;
1307         }
1308
1309         if (0 != vcd_client_unset_command_type(pid, cmd_type)) {
1310                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to unset command type : pid(%d), cmd_type(%d)", pid, cmd_type);
1311                 return VCD_ERROR_OPERATION_FAILED;
1312         }
1313
1314         return 0;
1315 }
1316
1317 int vcd_server_set_foreground(int pid, bool value)
1318 {
1319         /* check if pid is valid */
1320         if (false == vcd_client_is_available(pid) && false == vcd_client_widget_is_available(pid)) {
1321                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] pid is NOT valid ");
1322                 return VCD_ERROR_INVALID_PARAMETER;
1323         }
1324
1325         if (0 != vcd_config_set_foreground(pid, value)) {
1326                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to set foreground : pid(%d), value(%d)", pid, value);
1327                 return VCD_ERROR_OPERATION_FAILED;
1328         }
1329
1330         return 0;
1331 }
1332
1333 #if 0
1334 int vcd_server_set_exclusive_command(int pid, bool value)
1335 {
1336         /* check if pid is valid */
1337         if (false == vcd_client_is_available(pid)) {
1338                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] pid is NOT valid ");
1339                 return VCD_ERROR_INVALID_PARAMETER;
1340         }
1341
1342         if (true == value) {
1343                 if (0 != vcd_client_set_exclusive_command(pid)) {
1344                         SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to set exclusive command : pid(%d)", pid);
1345                         return VCD_ERROR_OPERATION_FAILED;
1346                 }
1347         } else {
1348                 if (0 != vcd_client_unset_exclusive_command(pid)) {
1349                         SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to unset exclusive command : pid(%d)", pid);
1350                         return VCD_ERROR_OPERATION_FAILED;
1351                 }
1352         }
1353
1354         return 0;
1355 }
1356
1357 int vcd_server_request_start(int pid, bool stop_by_silence)
1358 {
1359         /* check if pid is valid */
1360         if (false == vcd_client_is_available(pid)) {
1361                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] pid(%d) is NOT forground client", pid);
1362                 return VCD_ERROR_INVALID_PARAMETER;
1363         }
1364
1365         int ret;
1366         /* Check current state */
1367         vcd_state_e state = vcd_config_get_service_state();
1368
1369         /* Service state should be ready */
1370         if (VCD_STATE_READY != state) {
1371                 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Current state is not Ready : pid(%d)", pid);
1372                 return VCD_ERROR_INVALID_STATE;
1373         }
1374
1375         if (-1 != vcd_client_manager_get_pid()) {
1376                 /* Check current pid is valid */
1377                 if (false == vcd_client_manager_check_demandable_client(pid)) {
1378                         SLOG(LOG_DEBUG, TAG_VCD, "[Server] Current client is NOT available : pid(%d)", pid);
1379                         return VCD_ERROR_INVALID_PARAMETER;
1380                 }
1381         }
1382
1383         ret = vcd_server_mgr_start(stop_by_silence, false);
1384         if (0 != ret) {
1385                 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Fail to start recognition");
1386                 return VCD_ERROR_INVALID_PARAMETER;
1387         }
1388
1389         return 0;
1390 }
1391
1392 int vcd_server_request_stop(int pid)
1393 {
1394         int ret;
1395         /* Check current state */
1396         vcd_state_e state = vcd_config_get_service_state();
1397
1398         /* Service state should be ready */
1399         if (VCD_STATE_RECORDING != state) {
1400                 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Current state is not Recording : pid(%d)", pid);
1401                 return VCD_ERROR_INVALID_STATE;
1402         }
1403
1404         if (-1 != vcd_client_manager_get_pid()) {
1405                 /* Check current pid is valid */
1406                 if (false == vcd_client_manager_check_demandable_client(pid)) {
1407                         SLOG(LOG_DEBUG, TAG_VCD, "[Server] Current client is NOT available : pid(%d)", pid);
1408                         return VCD_ERROR_INVALID_PARAMETER;
1409                 }
1410         }
1411
1412         ret = vcd_server_mgr_stop();
1413         if (0 != ret) {
1414                 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Fail to start recognition");
1415                 return VCD_ERROR_OPERATION_FAILED;
1416         }
1417
1418         return VCD_ERROR_NONE;
1419 }
1420
1421 int vcd_server_request_cancel(int pid)
1422 {
1423         int ret;
1424         /* Check current state */
1425         vcd_state_e state = vcd_config_get_service_state();
1426
1427         /* Service state should be recording or processing */
1428         if (VCD_STATE_RECORDING != state && VCD_STATE_PROCESSING != state) {
1429                 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Current state is not Recording or Processing : pid(%d)", pid);
1430                 return VCD_ERROR_INVALID_STATE;
1431         }
1432
1433         if (-1 != vcd_client_manager_get_pid()) {
1434                 /* Check current pid is valid */
1435                 if (false == vcd_client_manager_check_demandable_client(pid)) {
1436                         SLOG(LOG_DEBUG, TAG_VCD, "[Server] Current client is NOT available : pid(%d)", pid);
1437                         return VCD_ERROR_INVALID_PARAMETER;
1438                 }
1439         }
1440
1441         ret = vcd_server_mgr_cancel();
1442         if (0 != ret) {
1443                 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Fail to start recognition");
1444                 return VCD_ERROR_OPERATION_FAILED;
1445         }
1446
1447         return VCD_ERROR_NONE;
1448 }
1449 #endif
1450
1451 /*
1452 * VC Server Functions for Widget lib
1453 */
1454 int vcd_server_widget_initialize(int pid)
1455 {
1456         if (false == g_is_engine) {
1457                 if (0 != vcd_engine_agent_initialize_current_engine()) {
1458                         SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] No Engine");
1459                         g_is_engine = false;
1460                         return VCD_ERROR_ENGINE_NOT_FOUND;
1461                 } else {
1462                         g_is_engine = true;
1463                 }
1464         }
1465
1466         if (false == vcd_engine_is_available_engine()) {
1467                 if (0 != vcd_engine_agent_initialize_current_engine()) {
1468                         SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] No Engine");
1469                         return VCD_ERROR_ENGINE_NOT_FOUND;
1470                 }
1471         }
1472
1473         /* check if pid is valid */
1474         if (true == vcd_client_widget_is_available(pid)) {
1475                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] The pid is already exist");
1476                 return VCD_ERROR_INVALID_PARAMETER;
1477         }
1478
1479         /* Add client information to client manager */
1480         if (0 != vcd_client_widget_add(pid)) {
1481                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to add client info");
1482                 return VCD_ERROR_OPERATION_FAILED;
1483         }
1484
1485         SLOG(LOG_DEBUG, TAG_VCD, "[Server Success] Initialize widget : pid(%d)", pid);
1486
1487         return VCD_ERROR_NONE;
1488 }
1489
1490 int vcd_server_widget_finalize(int pid)
1491 {
1492         /* check if pid is valid */
1493         if (false == vcd_client_widget_is_available(pid)) {
1494                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] pid is NOT valid ");
1495                 return VCD_ERROR_INVALID_PARAMETER;
1496         }
1497
1498         /* Remove client information */
1499         if (0 != vcd_client_widget_delete(pid)) {
1500                 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Fail to delete client");
1501         }
1502
1503         if (0 == vcd_client_get_ref_count()) {
1504                 SLOG(LOG_DEBUG, TAG_VCD, "[Server] connected client list is empty");
1505                 ecore_timer_add(0, __finalize_quit_ecore_loop, NULL);
1506         }
1507
1508         return VCD_ERROR_NONE;
1509 }
1510
1511 int vcd_server_widget_start_recording(int pid, bool widget_command)
1512 {
1513         /* check if pid is valid */
1514         if (false == vcd_client_widget_is_available(pid)) {
1515                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] pid is NOT valid ");
1516                 return VCD_ERROR_INVALID_PARAMETER;
1517         }
1518
1519         if (true == widget_command) {
1520                 vcd_client_widget_set_command(pid);
1521                 SLOG(LOG_DEBUG, TAG_VCD, "[Server] widget command is available");
1522         } else {
1523                 vcd_client_widget_unset_command(pid);
1524                 SLOG(LOG_DEBUG, TAG_VCD, "[Server] widget command is NOT available");
1525         }
1526
1527         int ret = __start_internal_recognition();
1528         if (0 != ret) {
1529                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to start recongition : %d", ret);
1530                 ecore_timer_add(0, __vcd_request_show_tooltip, (void*)false);
1531         }
1532
1533         return 0;
1534 }
1535
1536 int vcd_server_widget_start(int pid, bool stop_by_silence)
1537 {
1538         /* check if pid is valid */
1539         int fore_pid = vcd_client_widget_get_foreground_pid();
1540         if (pid != fore_pid) {
1541                 SLOG(LOG_ERROR, TAG_VCD, "[Server] pid is NOT foreground");
1542                 return VCD_ERROR_INVALID_PARAMETER;
1543         }
1544
1545         /* Check current state */
1546         vcd_state_e state = vcd_config_get_service_state();
1547
1548         /* Service state should be ready */
1549         if (VCD_STATE_READY != state) {
1550                 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Current state is not Ready : pid(%d)", pid);
1551                 return VCD_ERROR_INVALID_STATE;
1552         }
1553
1554         vcd_client_set_slience_detection(stop_by_silence);
1555
1556         /* Notify show tooltip */
1557         ecore_timer_add(0, __vcd_request_show_tooltip, (void*)true);
1558
1559         return 0;
1560 }
1561
1562 int vcd_server_widget_stop(int pid)
1563 {
1564         /* check if pid is valid */
1565         int fore_pid = vcd_client_widget_get_foreground_pid();
1566         if (pid != fore_pid) {
1567                 SLOG(LOG_ERROR, TAG_VCD, "[Server] pid is NOT foreground");
1568                 return VCD_ERROR_INVALID_PARAMETER;
1569         }
1570
1571         int ret;
1572         /* Check current state */
1573         vcd_state_e state = vcd_config_get_service_state();
1574
1575         /* Service state should be recording */
1576         if (VCD_STATE_RECORDING != state) {
1577                 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Current service state is not Recording : pid(%d)", pid);
1578                 return VCD_ERROR_INVALID_STATE;
1579         }
1580
1581         ret = vcd_server_mgr_stop();
1582         if (0 != ret) {
1583                 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Fail to start recognition");
1584                 return VCD_ERROR_OPERATION_FAILED;
1585         }
1586
1587         return VCD_ERROR_NONE;
1588 }
1589
1590 int vcd_server_widget_cancel(int pid)
1591 {
1592         /* check if pid is valid */
1593         int fore_pid = vcd_client_widget_get_foreground_pid();
1594         if (pid != fore_pid) {
1595                 SLOG(LOG_ERROR, TAG_VCD, "[Server] pid is NOT foreground");
1596                 return VCD_ERROR_INVALID_PARAMETER;
1597         }
1598
1599         int ret;
1600         /* Check current state */
1601         vcd_state_e state = vcd_config_get_service_state();
1602
1603         /* Service state should be recording or processing */
1604         if (VCD_STATE_RECORDING != state && VCD_STATE_PROCESSING != state) {
1605                 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Current state is not Recording or Processing : pid(%d)", pid);
1606                 return VCD_ERROR_INVALID_STATE;
1607         }
1608
1609         ret = vcd_server_mgr_cancel();
1610         if (0 != ret) {
1611                 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Fail to cancel recognition : %d", ret);
1612                 return ret;
1613         }
1614
1615         return VCD_ERROR_NONE;
1616 }
1617