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