check and revise by coding rule
[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         } else if (VCD_CLIENT_TYPE_WIDGET == type) {
634                 ret = vcd_client_widget_get_list(&client_list, &client_count);
635         } else if (VCD_CLIENT_TYPE_MANAGER == type) {
636                 mgr_pid = vcd_client_manager_get_pid();
637                 client_list = &mgr_pid;
638                 client_count = 1;
639         }
640
641         if (0 == ret || mgr_pid > 0) {
642                 SLOG(LOG_DEBUG, TAG_VCD, "===== Clean up %s client ", type ? (type == 1) ? "Widget" : "Manager" : "Normal");
643                 if (NULL != client_list && client_count > 0) {
644                         for (i = 0; i < client_count; i++) {
645                                 exist = false;
646                                 GList *iter = NULL;
647                                 for (j = 0; j < g_list_length(g_proc_list); j++) {
648                                         iter = g_list_nth(g_proc_list, j);
649                                         if (NULL != iter) {
650                                                 if (client_list[i] == GPOINTER_TO_INT(iter->data)) {
651                                                         SLOG(LOG_DEBUG, TAG_VCD, "%s pid(%d) is running", type ? (type == 1) ? "Widget" : "Manager" : "Normal", client_list[i]);
652                                                         exist = true;
653                                                         break;
654                                                 }
655                                         }
656                                 }
657
658                                 if (false == exist) {
659                                         SLOG(LOG_ERROR, TAG_VCD, "%s pid(%d) should be removed", type ? (type == 1) ? "Widget" : "Manager" : "Normal", client_list[i]);
660                                         if (VCD_CLIENT_TYPE_NORMAL == type)
661                                                 vcd_server_finalize(client_list[i]);
662                                         else if (VCD_CLIENT_TYPE_WIDGET == type)
663                                                 vcd_server_widget_finalize(client_list[i]);
664                                         else
665                                                 vcd_server_mgr_finalize(mgr_pid);
666                                 }
667                         }
668                 }
669                 SLOG(LOG_DEBUG, TAG_VCD, "=====");
670                 SLOG(LOG_DEBUG, TAG_VCD, "  ");
671         }
672         if (NULL != client_list && -1 == mgr_pid) {
673                 free(client_list);
674                 client_list = NULL;
675         }
676         return;
677 }
678
679 Eina_Bool vcd_cleanup_client_all(void *data)
680 {
681         __read_proc();
682         
683         __vcd_cleanup_client(VCD_CLIENT_TYPE_NORMAL);
684         __vcd_cleanup_client(VCD_CLIENT_TYPE_WIDGET);
685         __vcd_cleanup_client(VCD_CLIENT_TYPE_MANAGER);
686
687 #if 0
688         if (0 == vcd_client_get_list(&client_list, &client_count)) {
689                 SLOG(LOG_DEBUG, TAG_VCD, "===== Clean up client ");
690                 if (NULL != client_list && client_count > 0) {
691                         for (i = 0; i < client_count; i++) {
692                                 exist = false;
693                                 GList *iter = NULL;
694                                 for (j = 0; j < g_list_length(g_proc_list); j++) {
695                                         iter = g_list_nth(g_proc_list, j);
696                                         if (NULL != iter) {
697                                                 if (client_list[i] == GPOINTER_TO_INT(iter->data)) {
698                                                         SLOG(LOG_DEBUG, TAG_VCD, "pid(%d) is running", client_list[i]);
699                                                         exist = true;
700                                                         break;
701                                                 }
702                                         }
703                                 }
704
705                                 if (false == exist) {
706                                         SLOG(LOG_ERROR, TAG_VCD, "pid(%d) should be removed", client_list[i]);
707                                         vcd_server_finalize(client_list[i]);
708                                 }
709 #if 0
710                                 result = vcdc_send_hello(client_list[i], VCD_CLIENT_TYPE_NORMAL);
711
712                                 if (0 == result) {
713                                         SLOG(LOG_DEBUG, TAG_VCD, "[Server] pid(%d) should be removed.", client_list[i]);
714                                         vcd_server_finalize(client_list[i]);
715                                 } else if (-1 == result) {
716                                         SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Hello result has error");
717                                 }
718 #endif
719                         }
720                 }
721                 SLOG(LOG_DEBUG, TAG_VCD, "=====");
722                 SLOG(LOG_DEBUG, TAG_VCD, "  ");
723         }
724         if (NULL != client_list) {
725                 free(client_list);
726                 client_list = NULL;
727         }
728
729         /* If app is in background state, app cannot response message. */
730         if (0 == vcd_client_widget_get_list(&client_list, &client_count)) {
731                 SLOG(LOG_DEBUG, TAG_VCD, "===== Clean up widget");
732                 if (NULL != client_list && client_count > 0) {
733                         for (i = 0; i < client_count; i++) {
734                                 exist = false;
735                                 GList *iter = NULL;
736                                 for (j = 0; j < g_list_length(g_proc_list); j++) {
737                                         iter = g_list_nth(g_proc_list, j);
738                                         if (NULL != iter) {
739                                                 if (client_list[i] == GPOINTER_TO_INT(iter->data)) {
740                                                         SLOG(LOG_DEBUG, TAG_VCD, "widget pid(%d) is running", client_list[i]);
741                                                         exist = true;
742                                                         break;
743                                                 }
744                                         }
745                                 }
746
747                                 if (false == exist) {
748                                         SLOG(LOG_ERROR, TAG_VCD, "widget pid(%d) should be removed", client_list[i]);
749                                         vcd_server_widget_finalize(client_list[i]);
750                                 }
751 #if 0
752                                 result = vcdc_send_hello(client_list[i], VCD_CLIENT_TYPE_WIDGET);
753
754                                 if (0 == result) {
755                                         SLOG(LOG_DEBUG, TAG_VCD, "[Server] widget pid(%d) should be removed.", client_list[i]);
756                                         vcd_server_widget_finalize(client_list[i]);
757                                 } else if (-1 == result) {
758                                         SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Hello result has error");
759                                 }
760 #endif
761                         }
762                 }
763                 SLOG(LOG_DEBUG, TAG_VCD, "=====");
764                 SLOG(LOG_DEBUG, TAG_VCD, "  ");
765         }
766
767         if (NULL != client_list) {
768                 free(client_list);
769                 client_list = NULL;
770         }
771
772         /* manager */
773         exist = false;
774         GList *iter = NULL;
775         int mgr_pid = vcd_client_manager_get_pid();
776         if (0 < mgr_pid) {
777                 for (j = 0; j < g_list_length(g_proc_list); j++) {
778                         iter = g_list_nth(g_proc_list, j);
779                         if (NULL != iter) {
780                                 if (mgr_pid == GPOINTER_TO_INT(iter->data)) {
781                                         SLOG(LOG_DEBUG, TAG_VCD, "manager pid(%d) is running", mgr_pid);
782                                         exist = true;
783                                         break;
784                                 }
785                         }
786                 }
787
788                 if (false == exist) {
789                         SLOG(LOG_ERROR, TAG_VCD, "manager pid (%d) should be removed", mgr_pid);
790                         vcd_server_mgr_finalize(mgr_pid);
791                 }
792         }
793 #endif
794         return EINA_TRUE;
795 }
796
797 int vcd_server_get_service_state()
798 {
799         return vcd_config_get_service_state();
800 }
801
802 int vcd_server_get_foreground()
803 {
804         int pid;
805         vcd_config_get_foreground(&pid);
806         return pid;
807 }
808
809
810 /*
811 * API for manager
812 */
813 int vcd_server_mgr_initialize(int pid)
814 {
815         if (false == g_is_engine) {
816                 if (0 != vcd_engine_agent_initialize_current_engine()) {
817                         SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] No Engine");
818                         g_is_engine = false;
819                         return VCD_ERROR_ENGINE_NOT_FOUND;
820                 } else {
821                         g_is_engine = true;
822                 }
823         }
824
825         /* check if pid is valid */
826         if (false == vcd_client_manager_is_valid(pid)) {
827                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] The pid(%d) is already exist", pid);
828                 return VCD_ERROR_INVALID_PARAMETER;
829         }
830
831         /* Add client information to client manager */
832         if (0 != vcd_client_manager_set(pid)) {
833                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to add manager");
834                 return VCD_ERROR_OPERATION_FAILED;
835         }
836
837         SLOG(LOG_DEBUG, TAG_VCD, "[Server Success] Manager initialize : pid(%d)", pid);
838
839         return VCD_ERROR_NONE;
840 }
841
842 int vcd_server_mgr_finalize(int pid)
843 {
844         /* check if pid is valid */
845         if (false == vcd_client_manager_is_valid(pid)) {
846                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] The manager pid(%d) is NOT valid", pid);
847                 return VCD_ERROR_INVALID_PARAMETER;
848         }
849
850         /* Cancel recognition */
851         vcd_server_mgr_cancel();
852
853         /* Remove manager information */
854         if (0 != vcd_client_manager_unset()) {
855                 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Fail to delete client");
856         }
857
858         if (0 == vcd_client_get_ref_count()) {
859                 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Connected client list is empty");
860                 ecore_timer_add(0, __finalize_quit_ecore_loop, NULL);
861         }
862
863         SLOG(LOG_DEBUG, TAG_VCD, "[Server Success] Manager Finalize : pid(%d)", pid);
864
865         return VCD_ERROR_NONE;
866 }
867
868 int vcd_server_mgr_set_command(int pid)
869 {
870         if (0 != vcd_client_manager_set_command(pid)) {
871                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] The manager pid(%d) is NOT valid", pid);
872                 return VCD_ERROR_INVALID_PARAMETER;
873         }
874         return VCD_ERROR_NONE;
875 }
876
877 int vcd_server_mgr_unset_command(int pid)
878 {
879         if (0 != vcd_client_manager_unset_command(pid)) {
880                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] The manager pid(%d) is NOT valid", pid);
881                 return VCD_ERROR_INVALID_PARAMETER;
882         }
883         return VCD_ERROR_NONE;
884 }
885
886 int vcd_server_mgr_set_demandable_client(int pid)
887 {
888         /* check if pid is valid */
889         if (false == vcd_client_manager_is_valid(pid)) {
890                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] The manager pid(%d) is NOT valid", pid);
891                 return VCD_ERROR_INVALID_PARAMETER;
892         }
893
894         GSList* client_list = NULL;
895         if (0 != vc_info_parser_get_demandable_clients(&client_list)) {
896                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to get demandable client");
897                 return VCD_ERROR_OPERATION_FAILED;
898         }
899
900         /* Save client list */
901         if (0 != vcd_client_manager_set_demandable_client(pid, client_list)) {
902                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to set demandable client list");
903                 return VCD_ERROR_OPERATION_FAILED;
904         }
905
906         return VCD_ERROR_NONE;
907 }
908
909 int vcd_server_mgr_set_audio_type(int pid, const char* audio_type)
910 {
911         /* check if pid is valid */
912         if (false == vcd_client_manager_is_valid(pid)) {
913                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] The manager pid(%d) is NOT valid", pid);
914                 return VCD_ERROR_INVALID_PARAMETER;
915         }
916
917         int ret = 0;
918         vcp_audio_type_e type = VCP_AUDIO_TYPE_PCM_S16_LE;
919         int rate = 16000;
920         int channel = 1;
921
922         ret = vcd_engine_get_audio_format(audio_type, &type, &rate, &channel);
923         if (0 != ret) {
924                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to get audio format : %d", ret);
925                 return VCD_ERROR_OPERATION_FAILED;
926         }
927
928         ret = vcd_recorder_set(audio_type, type, rate, channel);
929         if (0 != ret) {
930                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to set audio in type : %d", ret);
931                 return VCD_ERROR_OPERATION_FAILED;
932         }
933
934         return VCD_ERROR_NONE;
935 }
936
937 int vcd_server_mgr_get_audio_type(int pid, char** audio_type)
938 {
939         /* check if pid is valid */
940         if (false == vcd_client_manager_is_valid(pid)) {
941                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] The manager pid(%d) is NOT valid", pid);
942                 return VCD_ERROR_INVALID_PARAMETER;
943         }
944
945         int ret = vcd_recorder_get(audio_type);
946         if (0 != ret) {
947                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to get audio in type : %d", ret);
948                 return VCD_ERROR_OPERATION_FAILED;
949         }
950
951         return VCD_ERROR_NONE;
952 }
953
954 int vcd_server_mgr_set_client_info(int pid)
955 {
956         /* check if pid is valid */
957         if (false == vcd_client_manager_is_valid(pid)) {
958                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] The manager pid(%d) is NOT valid", pid);
959                 return VCD_ERROR_INVALID_PARAMETER;
960         }
961
962         int ret = vcd_client_save_client_info();
963         if (0 != ret) {
964                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to save client info : %d", ret);
965                 return VCD_ERROR_OPERATION_FAILED;
966         }
967
968         return VCD_ERROR_NONE;
969 }
970
971 static int __start_internal_recognition()
972 {
973         int ret;
974
975         /* 2. Get commands */
976         ret = vcd_client_command_collect_command();
977         if (0 != ret) {
978                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to collect command : %d", ret);
979                 return VCD_ERROR_OPERATION_FAILED;
980         }
981
982         ret = vcd_client_get_length();
983         if (0 == ret) {
984                 SLOG(LOG_WARN, TAG_VCD, "[Server WARNIING] No current command : %d", ret);
985                 return VCD_ERROR_OPERATION_FAILED;
986         }
987
988         /* 3. Set command to engine */
989         ret = vcd_engine_set_commands();
990         if (0 != ret) {
991                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to collect command : %d", ret);
992                 return VCD_ERROR_OPERATION_FAILED;
993         }
994
995         SLOG(LOG_DEBUG, TAG_VCD, "[Server Success] Set command");
996
997         bool stop_by_silence = true;
998         if (VCD_RECOGNITION_MODE_MANUAL == vcd_client_get_recognition_mode()) {
999                 stop_by_silence = false;
1000         }
1001
1002         /* 4. start recognition */
1003         ret = vcd_engine_recognize_start(stop_by_silence);
1004         if (0 != ret) {
1005                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to start recognition : result(%d)", ret);
1006                 return VCD_ERROR_OPERATION_FAILED;
1007         }
1008
1009         SLOG(LOG_DEBUG, TAG_VCD, "[Server Success] Start engine");
1010
1011         /* 5. recorder start */
1012         ret = vcd_recorder_start();
1013         if (0 != ret) {
1014                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to start recorder : result(%d)", ret);
1015                 vcd_engine_recognize_cancel();
1016                 return ret;
1017         }
1018
1019         vcd_config_set_service_state(VCD_STATE_RECORDING);
1020         vcdc_send_service_state(VCD_STATE_RECORDING);
1021
1022         SLOG(LOG_DEBUG, TAG_VCD, "[Server Success] Start recognition(%d)", stop_by_silence);
1023
1024         return 0;
1025 }
1026
1027 static Eina_Bool __vcd_request_show_tooltip(void *data)
1028 {
1029         int pid = vcd_client_widget_get_foreground_pid();
1030         if (-1 != pid) {
1031                 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Request tooltip show and widget command");
1032                 vcdc_send_show_tooltip(pid, (bool)data);
1033         }
1034
1035         return EINA_FALSE;
1036 }
1037
1038 int vcd_server_mgr_start(vcd_recognition_mode_e recognition_mode, bool exclusive_cmd, bool start_by_client)
1039 {
1040         /* 1. check current state */
1041         vcd_state_e state = vcd_config_get_service_state();
1042
1043         if (VCD_STATE_READY != state) {
1044                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Current state is not ready");
1045                 return VCD_ERROR_INVALID_STATE;
1046         }
1047
1048         SLOG(LOG_DEBUG, TAG_VCD, "[Server] set recognition mode = %d", recognition_mode);
1049         vcd_client_set_recognition_mode(recognition_mode);
1050
1051         if (false == exclusive_cmd) {
1052                 /* Notify show tooltip */
1053                 int pid = vcd_client_widget_get_foreground_pid();
1054                 if (-1 != pid) {
1055                         SLOG(LOG_DEBUG, TAG_VCD, "[Server] Request tooltip show and widget command");
1056                         ecore_timer_add(0, __vcd_request_show_tooltip, (void*)true);
1057                         return 0;
1058                 }
1059         } else {
1060                 vcd_client_manager_set_exclusive(exclusive_cmd);
1061         }
1062
1063         int fg_pid = -1;
1064         if (true == start_by_client) {
1065                 /* Get foreground pid */
1066                 if (0 != vcd_config_get_foreground(&fg_pid)) {
1067                         SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to get foreground");
1068                 }
1069
1070                 /* Set client exclusive option */
1071                 if (0 != vcd_client_set_exclusive_command(fg_pid)) {
1072                         SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to set exclusive command");
1073                 }
1074         }
1075
1076         int ret = __start_internal_recognition();
1077         if (0 != ret) {
1078                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to start recongition : %d", ret);
1079                 return ret;
1080         }
1081
1082         if (true == start_by_client) {
1083                 vcd_client_unset_exclusive_command(fg_pid);
1084         }
1085
1086         return VCD_ERROR_NONE;
1087 }
1088
1089 int vcd_server_mgr_stop()
1090 {
1091         /* 1. Check current state is recording */
1092         if (VCD_STATE_RECORDING != vcd_config_get_service_state()) {
1093                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Current state is not recording");
1094                 return VCD_ERROR_INVALID_STATE;
1095         }
1096
1097         /* 2. Stop recorder */
1098         vcd_recorder_stop();
1099
1100         /* 3. Stop engine recognition */
1101         int ret = vcd_engine_recognize_stop();
1102         if (0 != ret) {
1103                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to stop recognition : %d", ret);
1104         }
1105
1106         /* 4. Set original mode */
1107         vcd_config_set_service_state(VCD_STATE_PROCESSING);
1108         vcdc_send_service_state(VCD_STATE_PROCESSING);
1109
1110         return VCD_ERROR_NONE;
1111 }
1112
1113 int vcd_server_mgr_cancel()
1114 {
1115         /* 1. Check current state */
1116         vcd_state_e state = vcd_config_get_service_state();
1117         if (VCD_STATE_RECORDING != state && VCD_STATE_PROCESSING != state) {
1118                 SLOG(LOG_WARN, TAG_VCD, "[Server ERROR] Current state is not recording or processing");
1119                 return VCD_ERROR_INVALID_STATE;
1120         }
1121
1122         /* 2. Stop recorder */
1123         vcd_recorder_stop();
1124         /* 3. Cancel engine */
1125         int ret = vcd_engine_recognize_cancel();
1126         if (0 != ret) {
1127                 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Fail to cancel : %d", ret);
1128         }
1129
1130         if (false == vcd_client_manager_get_exclusive()) {
1131                 int pid = vcd_client_widget_get_foreground_pid();
1132                 if (-1 != pid) {
1133                         SLOG(LOG_DEBUG, TAG_VCD, "[Server] Request tooltip hide");
1134                         ecore_timer_add(0, __vcd_request_show_tooltip, (void*)false);
1135                 }
1136         } else {
1137                 vcd_client_manager_set_exclusive(false);
1138         }
1139
1140         /* 4. Set state */
1141         vcd_config_set_service_state(VCD_STATE_READY);
1142         vcdc_send_service_state(VCD_STATE_READY);
1143
1144         return VCD_ERROR_NONE;
1145 }
1146
1147
1148 int vcd_server_mgr_result_select()
1149 {
1150         __vcd_send_selected_result(NULL);
1151
1152         return VCD_ERROR_NONE;
1153 }
1154
1155 /*
1156 * VC Server Functions for Client
1157 */
1158 int vcd_server_initialize(int pid)
1159 {
1160         if (false == g_is_engine) {
1161                 if (0 != vcd_engine_agent_initialize_current_engine()) {
1162                         SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] No Engine");
1163                         g_is_engine = false;
1164                         return VCD_ERROR_ENGINE_NOT_FOUND;
1165                 } else {
1166                         g_is_engine = true;
1167                 }
1168         }
1169
1170         if (false == vcd_engine_is_available_engine()) {
1171                 if (0 != vcd_engine_agent_initialize_current_engine()) {
1172                         SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] No Engine");
1173                         return VCD_ERROR_ENGINE_NOT_FOUND;
1174                 }
1175         }
1176
1177         /* check if pid is valid */
1178         if (true == vcd_client_is_available(pid)) {
1179                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] The pid is already exist");
1180                 return VCD_ERROR_INVALID_PARAMETER;
1181         }
1182
1183         /* Add client information to client manager */
1184         if (0 != vcd_client_add(pid)) {
1185                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to add client info");
1186                 return VCD_ERROR_OPERATION_FAILED;
1187         }
1188
1189         SLOG(LOG_DEBUG, TAG_VCD, "[Server Success] Client Initialize : pid(%d)", pid);
1190
1191         return VCD_ERROR_NONE;
1192 }
1193
1194 int vcd_server_finalize(int pid)
1195 {
1196         /* check if pid is valid */
1197         if (false == vcd_client_is_available(pid)) {
1198                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] pid is NOT valid ");
1199                 return VCD_ERROR_INVALID_PARAMETER;
1200         }
1201
1202         /* Remove client information */
1203         if (0 != vcd_client_delete(pid)) {
1204                 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Fail to delete client");
1205         }
1206
1207         if (0 == vcd_client_get_ref_count()) {
1208                 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Connected client list is empty");
1209                 ecore_timer_add(0, __finalize_quit_ecore_loop, NULL);
1210         }
1211
1212         SLOG(LOG_DEBUG, TAG_VCD, "[Server Success] Client Finalize : pid(%d)", pid);
1213
1214         return VCD_ERROR_NONE;
1215 }
1216
1217 int vcd_server_set_command(int pid, vc_cmd_type_e cmd_type)
1218 {
1219         /* check if pid is valid */
1220         if (false == vcd_client_is_available(pid)) {
1221                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] pid is NOT valid ");
1222                 return VCD_ERROR_INVALID_PARAMETER;
1223         }
1224
1225         if (0 != vcd_client_set_command_type(pid, cmd_type)) {
1226                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to set command type : pid(%d), cmd_type(%d)", pid, cmd_type);
1227                 return VCD_ERROR_OPERATION_FAILED;
1228         }
1229
1230         return 0;
1231 }
1232
1233 int vcd_server_unset_command(int pid, vc_cmd_type_e cmd_type)
1234 {
1235         /* check if pid is valid */
1236         if (false == vcd_client_is_available(pid)) {
1237                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] pid is NOT valid ");
1238                 return VCD_ERROR_INVALID_PARAMETER;
1239         }
1240
1241         if (0 != vcd_client_unset_command_type(pid, cmd_type)) {
1242                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to unset command type : pid(%d), cmd_type(%d)", pid, cmd_type);
1243                 return VCD_ERROR_OPERATION_FAILED;
1244         }
1245
1246         return 0;
1247 }
1248
1249 int vcd_server_set_foreground(int pid, bool value)
1250 {
1251         /* check if pid is valid */
1252         if (false == vcd_client_is_available(pid) && false == vcd_client_widget_is_available(pid)) {
1253                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] pid is NOT valid ");
1254                 return VCD_ERROR_INVALID_PARAMETER;
1255         }
1256
1257         if (0 != vcd_config_set_foreground(pid, value)) {
1258                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to set foreground : pid(%d), value(%d)", pid, value);
1259                 return VCD_ERROR_OPERATION_FAILED;
1260         }
1261
1262         return 0;
1263 }
1264
1265 #if 0
1266 int vcd_server_set_exclusive_command(int pid, bool value)
1267 {
1268         /* check if pid is valid */
1269         if (false == vcd_client_is_available(pid)) {
1270                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] pid is NOT valid ");
1271                 return VCD_ERROR_INVALID_PARAMETER;
1272         }
1273
1274         if (true == value) {
1275                 if (0 != vcd_client_set_exclusive_command(pid)) {
1276                         SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to set exclusive command : pid(%d)", pid);
1277                         return VCD_ERROR_OPERATION_FAILED;
1278                 }
1279         } else {
1280                 if (0 != vcd_client_unset_exclusive_command(pid)) {
1281                         SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to unset exclusive command : pid(%d)", pid);
1282                         return VCD_ERROR_OPERATION_FAILED;
1283                 }
1284         }
1285
1286         return 0;
1287 }
1288
1289 int vcd_server_request_start(int pid, bool stop_by_silence)
1290 {
1291         /* check if pid is valid */
1292         if (false == vcd_client_is_available(pid)) {
1293                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] pid(%d) is NOT forground client", pid);
1294                 return VCD_ERROR_INVALID_PARAMETER;
1295         }
1296
1297         int ret;
1298         /* Check current state */
1299         vcd_state_e state = vcd_config_get_service_state();
1300
1301         /* Service state should be ready */
1302         if (VCD_STATE_READY != state) {
1303                 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Current state is not Ready : pid(%d)", pid);
1304                 return VCD_ERROR_INVALID_STATE;
1305         }
1306
1307         if (-1 != vcd_client_manager_get_pid()) {
1308                 /* Check current pid is valid */
1309                 if (false == vcd_client_manager_check_demandable_client(pid)) {
1310                         SLOG(LOG_DEBUG, TAG_VCD, "[Server] Current client is NOT available : pid(%d)", pid);
1311                         return VCD_ERROR_INVALID_PARAMETER;
1312                 }
1313         }
1314
1315         ret = vcd_server_mgr_start(stop_by_silence, false);
1316         if (0 != ret) {
1317                 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Fail to start recognition");
1318                 return VCD_ERROR_INVALID_PARAMETER;
1319         }
1320
1321         return 0;
1322 }
1323
1324 int vcd_server_request_stop(int pid)
1325 {
1326         int ret;
1327         /* Check current state */
1328         vcd_state_e state = vcd_config_get_service_state();
1329
1330         /* Service state should be ready */
1331         if (VCD_STATE_RECORDING != state) {
1332                 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Current state is not Recording : pid(%d)", pid);
1333                 return VCD_ERROR_INVALID_STATE;
1334         }
1335
1336         if (-1 != vcd_client_manager_get_pid()) {
1337                 /* Check current pid is valid */
1338                 if (false == vcd_client_manager_check_demandable_client(pid)) {
1339                         SLOG(LOG_DEBUG, TAG_VCD, "[Server] Current client is NOT available : pid(%d)", pid);
1340                         return VCD_ERROR_INVALID_PARAMETER;
1341                 }
1342         }
1343
1344         ret = vcd_server_mgr_stop();
1345         if (0 != ret) {
1346                 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Fail to start recognition");
1347                 return VCD_ERROR_OPERATION_FAILED;
1348         }
1349
1350         return VCD_ERROR_NONE;
1351 }
1352
1353 int vcd_server_request_cancel(int pid)
1354 {
1355         int ret;
1356         /* Check current state */
1357         vcd_state_e state = vcd_config_get_service_state();
1358
1359         /* Service state should be recording or processing */
1360         if (VCD_STATE_RECORDING != state && VCD_STATE_PROCESSING != state) {
1361                 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Current state is not Recording or Processing : pid(%d)", pid);
1362                 return VCD_ERROR_INVALID_STATE;
1363         }
1364
1365         if (-1 != vcd_client_manager_get_pid()) {
1366                 /* Check current pid is valid */
1367                 if (false == vcd_client_manager_check_demandable_client(pid)) {
1368                         SLOG(LOG_DEBUG, TAG_VCD, "[Server] Current client is NOT available : pid(%d)", pid);
1369                         return VCD_ERROR_INVALID_PARAMETER;
1370                 }
1371         }
1372
1373         ret = vcd_server_mgr_cancel();
1374         if (0 != ret) {
1375                 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Fail to start recognition");
1376                 return VCD_ERROR_OPERATION_FAILED;
1377         }
1378
1379         return VCD_ERROR_NONE;
1380 }
1381 #endif
1382
1383 /*
1384 * VC Server Functions for Widget lib
1385 */
1386 int vcd_server_widget_initialize(int pid)
1387 {
1388         if (false == g_is_engine) {
1389                 if (0 != vcd_engine_agent_initialize_current_engine()) {
1390                         SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] No Engine");
1391                         g_is_engine = false;
1392                         return VCD_ERROR_ENGINE_NOT_FOUND;
1393                 } else {
1394                         g_is_engine = true;
1395                 }
1396         }
1397
1398         if (false == vcd_engine_is_available_engine()) {
1399                 if (0 != vcd_engine_agent_initialize_current_engine()) {
1400                         SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] No Engine");
1401                         return VCD_ERROR_ENGINE_NOT_FOUND;
1402                 }
1403         }
1404
1405         /* check if pid is valid */
1406         if (true == vcd_client_widget_is_available(pid)) {
1407                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] The pid is already exist");
1408                 return VCD_ERROR_INVALID_PARAMETER;
1409         }
1410
1411         /* Add client information to client manager */
1412         if (0 != vcd_client_widget_add(pid)) {
1413                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to add client info");
1414                 return VCD_ERROR_OPERATION_FAILED;
1415         }
1416
1417         SLOG(LOG_DEBUG, TAG_VCD, "[Server Success] Initialize widget : pid(%d)", pid);
1418
1419         return VCD_ERROR_NONE;
1420 }
1421
1422 int vcd_server_widget_finalize(int pid)
1423 {
1424         /* check if pid is valid */
1425         if (false == vcd_client_widget_is_available(pid)) {
1426                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] pid is NOT valid ");
1427                 return VCD_ERROR_INVALID_PARAMETER;
1428         }
1429
1430         /* Remove client information */
1431         if (0 != vcd_client_widget_delete(pid)) {
1432                 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Fail to delete client");
1433         }
1434
1435         if (0 == vcd_client_get_ref_count()) {
1436                 SLOG(LOG_DEBUG, TAG_VCD, "[Server] connected client list is empty");
1437                 ecore_timer_add(0, __finalize_quit_ecore_loop, NULL);
1438         }
1439
1440         return VCD_ERROR_NONE;
1441 }
1442
1443 int vcd_server_widget_start_recording(int pid, bool widget_command)
1444 {
1445         /* check if pid is valid */
1446         if (false == vcd_client_widget_is_available(pid)) {
1447                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] pid is NOT valid ");
1448                 return VCD_ERROR_INVALID_PARAMETER;
1449         }
1450
1451         if (true == widget_command) {
1452                 vcd_client_widget_set_command(pid);
1453                 SLOG(LOG_DEBUG, TAG_VCD, "[Server] widget command is available");
1454         } else {
1455                 vcd_client_widget_unset_command(pid);
1456                 SLOG(LOG_WARN, TAG_VCD, "[Server] widget command is NOT available");
1457         }
1458
1459         int ret = __start_internal_recognition();
1460         if (0 != ret) {
1461                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to start recongition : %d", ret);
1462                 ecore_timer_add(0, __vcd_request_show_tooltip, (void*)false);
1463         }
1464
1465         return 0;
1466 }
1467
1468 int vcd_server_widget_start(int pid, bool stop_by_silence)
1469 {
1470         /* check if pid is valid */
1471         int fore_pid = vcd_client_widget_get_foreground_pid();
1472         if (pid != fore_pid) {
1473                 SLOG(LOG_ERROR, TAG_VCD, "[Server] pid is NOT foreground");
1474                 return VCD_ERROR_INVALID_PARAMETER;
1475         }
1476
1477         /* Check current state */
1478         vcd_state_e state = vcd_config_get_service_state();
1479
1480         /* Service state should be ready */
1481         if (VCD_STATE_READY != state) {
1482                 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Current state is not Ready : pid(%d)", pid);
1483                 return VCD_ERROR_INVALID_STATE;
1484         }
1485
1486         vcd_client_set_slience_detection(stop_by_silence);
1487
1488         /* Notify show tooltip */
1489         ecore_timer_add(0, __vcd_request_show_tooltip, (void*)true);
1490
1491         return 0;
1492 }
1493
1494 int vcd_server_widget_stop(int pid)
1495 {
1496         /* check if pid is valid */
1497         int fore_pid = vcd_client_widget_get_foreground_pid();
1498         if (pid != fore_pid) {
1499                 SLOG(LOG_ERROR, TAG_VCD, "[Server] pid is NOT foreground");
1500                 return VCD_ERROR_INVALID_PARAMETER;
1501         }
1502
1503         int ret;
1504         /* Check current state */
1505         vcd_state_e state = vcd_config_get_service_state();
1506
1507         /* Service state should be recording */
1508         if (VCD_STATE_RECORDING != state) {
1509                 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Current service state is not Recording : pid(%d)", pid);
1510                 return VCD_ERROR_INVALID_STATE;
1511         }
1512
1513         ret = vcd_server_mgr_stop();
1514         if (0 != ret) {
1515                 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Fail to start recognition");
1516                 return VCD_ERROR_OPERATION_FAILED;
1517         }
1518
1519         return VCD_ERROR_NONE;
1520 }
1521
1522 int vcd_server_widget_cancel(int pid)
1523 {
1524         /* check if pid is valid */
1525         int fore_pid = vcd_client_widget_get_foreground_pid();
1526         if (pid != fore_pid) {
1527                 SLOG(LOG_ERROR, TAG_VCD, "[Server] pid is NOT foreground");
1528                 return VCD_ERROR_INVALID_PARAMETER;
1529         }
1530
1531         int ret;
1532         /* Check current state */
1533         vcd_state_e state = vcd_config_get_service_state();
1534
1535         /* Service state should be recording or processing */
1536         if (VCD_STATE_RECORDING != state && VCD_STATE_PROCESSING != state) {
1537                 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Current state is not Recording or Processing : pid(%d)", pid);
1538                 return VCD_ERROR_INVALID_STATE;
1539         }
1540
1541         ret = vcd_server_mgr_cancel();
1542         if (0 != ret) {
1543                 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Fail to cancel recognition : %d", ret);
1544                 return ret;
1545         }
1546
1547         return VCD_ERROR_NONE;
1548 }
1549