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