Add document in vce.h
[platform/core/uifw/voice-control.git] / server / vcd_engine_agent.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
18 #include <dlfcn.h>
19 #include <dirent.h>
20
21 #include "vcd_client_data.h"
22 #include "vcd_config.h"
23 #include "vcd_engine_agent.h"
24 #include "vcd_main.h"
25 #include "vcd_recorder.h"
26 #include "vcd_dbus.h"
27
28 /*
29  * Internal data structure
30  */
31 typedef struct {
32         /* engine info */
33         char*   engine_uuid;
34         char*   engine_name;
35         char*   engine_path;
36         char*   engine_setting_path;
37
38         /* engine load info */
39         bool    is_set;
40         bool    is_loaded;
41         bool    is_command_ready;
42         bool    use_network;
43         void    *handle;
44
45         vc_engine_callback_s* callbacks;
46 } vcengine_s;
47
48 typedef struct _vcengine_info {
49         char*   engine_uuid;
50         char*   engine_path;
51         char*   engine_name;
52 } vcengine_info_s;
53
54
55 /*
56  * static data
57  */
58
59 /** vc engine agent init */
60 static bool g_agent_init;
61
62 /** current engine information */
63 static vcengine_s g_dynamic_engine;
64
65 static char* g_default_lang;
66
67 bool __supported_language_cb(const char* language, void* user_data);
68
69 /*
70  * Internal Interfaces
71  */
72
73 /** get engine info */
74 int __internal_get_engine_info(vce_request_callback_s* callback);
75
76 /*
77  * VCS Engine Agent Interfaces
78  */
79 int vcd_engine_agent_init()
80 {
81         if (true == g_agent_init) {
82                 SLOG(LOG_DEBUG, TAG_VCD, "[Engine Agent] Already initialized");
83                 return VCD_ERROR_NONE;
84         }
85
86         /* init dynamic engine */
87         g_dynamic_engine.engine_uuid = NULL;
88         g_dynamic_engine.engine_name = NULL;
89         g_dynamic_engine.engine_path = NULL;
90
91         g_dynamic_engine.is_set = false;
92         g_dynamic_engine.is_loaded = false;
93         g_dynamic_engine.handle = NULL;
94         g_dynamic_engine.is_command_ready = false;
95
96         g_dynamic_engine.callbacks = (vc_engine_callback_s*)calloc(1, sizeof(vc_engine_callback_s));
97         if (NULL == g_dynamic_engine.callbacks) {
98                 SLOG(LOG_ERROR, TAG_VCD, "[Engine Agent] Fail to allocate memory");
99                 return VCD_ERROR_OUT_OF_MEMORY;
100         }
101
102         g_agent_init = true;
103
104         if (0 != vcd_config_get_default_language(&g_default_lang)) {
105                 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] There is No default voice in config");
106                 /* Set default voice */
107                 g_default_lang = strdup(VC_BASE_LANGUAGE);
108                 if (NULL == g_default_lang) {
109                         SLOG(LOG_ERROR, TAG_VCD, "[Engine Agent] Fail to allocate memory");
110                         return VCD_ERROR_OUT_OF_MEMORY;
111                 }
112         }
113
114         SLOG(LOG_DEBUG, TAG_VCD, "[Engine Agent SUCCESS] Engine Agent Initialize");
115
116         return 0;
117 }
118
119 int vcd_engine_agent_release()
120 {
121         if (false == g_agent_init) {
122                 SLOG(LOG_ERROR, TAG_VCD, "[Engine Agent ERROR] Not Initialized");
123                 return VCD_ERROR_OPERATION_FAILED;
124         }
125
126         /* unload current engine */
127         if (0 != vcd_engine_agent_unload_current_engine()) {
128                 SLOG(LOG_ERROR, TAG_VCD, "[Engine Agent ERROR] Fail to deinitialize");
129                 return VCD_ERROR_OPERATION_FAILED;
130         }
131
132         /* release current engine data */
133         if (NULL != g_dynamic_engine.callbacks) {
134                 free(g_dynamic_engine.callbacks);
135                 g_dynamic_engine.callbacks = NULL;
136         }
137
138         g_agent_init = false;
139
140         SLOG(LOG_DEBUG, TAG_VCD, "[Engine Agent SUCCESS] Engine Agent release");
141
142         return 0;
143 }
144
145 bool vcd_engine_is_available_engine()
146 {
147         if (true == g_dynamic_engine.is_loaded)
148                 return true;
149
150         return false;
151 }
152
153 int __internal_get_engine_info(vce_request_callback_s* callback)
154 {
155         SLOG(LOG_DEBUG, TAG_VCD, "[Engine Agent] inside __internal_get_engine_info");
156
157         if (NULL == callback) {
158                 SLOG(LOG_ERROR, TAG_VCD, "[Engine Agent ERROR] Invalid engine");
159                 return VCD_ERROR_ENGINE_NOT_FOUND;
160         }
161
162         if (NULL == callback->get_info) {
163                 SLOG(LOG_ERROR, TAG_VCD, "[Engine Agent ERROR] Invalid engine");
164                 return VCD_ERROR_ENGINE_NOT_FOUND;
165         }
166
167         if (0 != callback->get_info(&(g_dynamic_engine.engine_uuid), &(g_dynamic_engine.engine_name), &(g_dynamic_engine.engine_setting_path), &(g_dynamic_engine.use_network))) {
168                 SLOG(LOG_ERROR, TAG_VCD, "[Engine Agent ERROR] Fail to get engine info");
169                 return VCD_ERROR_ENGINE_NOT_FOUND;
170         }
171
172         if (NULL != g_dynamic_engine.callbacks) {
173                 free(g_dynamic_engine.callbacks);
174                 g_dynamic_engine.callbacks = NULL;
175         }
176         g_dynamic_engine.callbacks = (vc_engine_callback_s*)calloc(1, sizeof(vc_engine_callback_s));
177         if (NULL == g_dynamic_engine.callbacks) {
178                 SLOG(LOG_ERROR, TAG_VCD, "[Engine Agent ERROR] Fail to allocate memory");
179                 return VCD_ERROR_OUT_OF_MEMORY;
180         }
181
182         g_dynamic_engine.callbacks->get_info = callback->get_info;
183         g_dynamic_engine.callbacks->get_recording_format = callback->get_recording_format;
184         g_dynamic_engine.callbacks->foreach_langs = callback->foreach_langs;
185         g_dynamic_engine.callbacks->is_lang_supported = callback->is_lang_supported;
186         g_dynamic_engine.callbacks->initialize = callback->initialize;
187         g_dynamic_engine.callbacks->deinitialize = callback->deinitialize;
188         g_dynamic_engine.callbacks->set_language = callback->set_language;
189         g_dynamic_engine.callbacks->set_commands = callback->set_commands;
190         g_dynamic_engine.callbacks->unset_commands = callback->unset_commands;
191
192         g_dynamic_engine.callbacks->start = callback->start;
193         g_dynamic_engine.callbacks->set_recording = callback->set_recording;
194         g_dynamic_engine.callbacks->stop = callback->stop;
195         g_dynamic_engine.callbacks->cancel = callback->cancel;
196         g_dynamic_engine.callbacks->set_domain = callback->set_domain;
197         g_dynamic_engine.callbacks->set_audio_type = callback->set_audio_type;
198         g_dynamic_engine.callbacks->process_text = callback->process_text;
199         g_dynamic_engine.callbacks->process_list_event = callback->process_list_event;
200         g_dynamic_engine.callbacks->process_haptic_event = callback->process_haptic_event;
201
202         g_dynamic_engine.callbacks->private_data_set = NULL;
203         g_dynamic_engine.callbacks->private_data_request = NULL;
204         g_dynamic_engine.callbacks->nlu_base_info_request = NULL;
205
206         SLOG(LOG_DEBUG, TAG_VCD, "@@@ Valid Engine");
207         SLOG(LOG_DEBUG, TAG_VCD, "Engine uuid : %s", g_dynamic_engine.engine_uuid);
208         SLOG(LOG_DEBUG, TAG_VCD, "Engine name : %s", g_dynamic_engine.engine_name);
209         SLOG(LOG_DEBUG, TAG_VCD, "@@@");
210
211         return 0;
212 }
213
214 int vcd_engine_agent_load_current_engine(vce_request_callback_s* callback)
215 {
216         SLOG(LOG_DEBUG, TAG_VCD, "[Engine Agent] load current engine START");
217
218         if (false == g_agent_init) {
219                 SLOG(LOG_ERROR, TAG_VCD, "[Engine Agent ERROR] Not Initialized");
220                 return VCD_ERROR_OPERATION_FAILED;
221         }
222
223         if (true == g_dynamic_engine.is_loaded) {
224                 SLOG(LOG_ERROR, TAG_VCD, "[Engine Agent] Engine has already been loaded");
225                 return 0;
226         }
227
228         if (NULL == callback) {
229                 SLOG(LOG_ERROR, TAG_VCD, "[Engine Agent ERROR] Invalid engine");
230                 return VCD_ERROR_ENGINE_NOT_FOUND;
231         }
232
233         /* Get current engine info */
234         int ret = __internal_get_engine_info(callback);
235         if (0 != ret) {
236                 SLOG(LOG_ERROR, TAG_VCD, "[Engine Agent ERROR] Fail to get engine info");
237                 return ret;
238         }
239
240         /* Check all engine functions */
241         if (NULL == g_dynamic_engine.callbacks->get_info ||
242                         NULL == g_dynamic_engine.callbacks->get_recording_format ||
243                         NULL == g_dynamic_engine.callbacks->foreach_langs ||
244                         NULL == g_dynamic_engine.callbacks->is_lang_supported ||
245                         NULL == g_dynamic_engine.callbacks->initialize ||
246                         NULL == g_dynamic_engine.callbacks->deinitialize ||
247                         NULL == g_dynamic_engine.callbacks->set_language ||
248                         NULL == g_dynamic_engine.callbacks->set_commands ||
249                         NULL == g_dynamic_engine.callbacks->unset_commands ||
250                         NULL == g_dynamic_engine.callbacks->start ||
251                         NULL == g_dynamic_engine.callbacks->set_recording ||
252                         NULL == g_dynamic_engine.callbacks->stop ||
253                         NULL == g_dynamic_engine.callbacks->cancel ||
254                         NULL == g_dynamic_engine.callbacks->set_audio_type ||
255                         NULL == g_dynamic_engine.callbacks->set_domain ||
256                         NULL == g_dynamic_engine.callbacks->process_text ||
257                         NULL == g_dynamic_engine.callbacks->process_list_event ||
258                         NULL == g_dynamic_engine.callbacks->process_haptic_event) {
259                 SLOG(LOG_ERROR, TAG_VCD, "[Engine Agent ERROR] The current engine is NOT valid");
260                 return VCD_ERROR_ENGINE_NOT_FOUND;
261         }
262
263         /* initalize engine */
264         ret = g_dynamic_engine.callbacks->initialize();
265         if (0 != ret) {
266                 SLOG(LOG_ERROR, TAG_VCD, "[Engine Agent ERROR] Fail to initialize vc engine service");
267                 return ret;
268         }
269
270         /* set the supported language */
271         if (true == g_dynamic_engine.callbacks->is_lang_supported(g_default_lang)) {
272                 ret = g_dynamic_engine.callbacks->set_language(g_default_lang);
273                 if (0 != ret) {
274                         SLOG(LOG_ERROR, TAG_VCD, "[Engine Agent ERROR] Fail to set the supported language");
275                         return ret;
276                 }
277
278                 SLOG(LOG_DEBUG, TAG_VCD, "[Engine Agent SUCCESS] The %s has been loaded!!!", g_dynamic_engine.engine_name);
279                 g_dynamic_engine.is_loaded = true;
280         } else {
281                 SLOG(LOG_ERROR, TAG_VCD, "[Engine Agent WARNING] This engine do not support default language : lang(%s)", g_default_lang);
282                 g_dynamic_engine.is_loaded = false;
283                 return VCD_ERROR_OPERATION_FAILED;
284         }
285
286         SLOG(LOG_DEBUG, TAG_VCD, "[Engine Agent] load current engine FINISH");
287
288         return 0;
289 }
290
291 int vcd_engine_agent_unload_current_engine()
292 {
293         if (false == g_agent_init) {
294                 SLOG(LOG_ERROR, TAG_VCD, "[Engine Agent ERROR] Not Initialized");
295                 return VCD_ERROR_OPERATION_FAILED;
296         }
297
298         if (false == g_dynamic_engine.is_loaded) {
299                 SLOG(LOG_DEBUG, TAG_VCD, "[Engine Agent] Engine has already been unloaded");
300                 return VCD_ERROR_NONE;
301         }
302
303         /* shut down engine */
304         int ret = 0;
305         ret = g_dynamic_engine.callbacks->deinitialize();
306         if (0 != ret) {
307                 SLOG(LOG_ERROR, TAG_VCD, "[Engine Agent ERROR] Fail to deinitialize");
308         }
309
310         /* reset current engine data */
311         g_dynamic_engine.is_loaded = false;
312
313         return 0;
314 }
315
316
317 /*
318  * VCS Engine Interfaces for client
319  */
320
321 int vcd_engine_set_commands()
322 {
323         if (false == g_agent_init) {
324                 SLOG(LOG_ERROR, TAG_VCD, "[Engine Agent ERROR] Not Initialized");
325                 return VCD_ERROR_OPERATION_FAILED;
326         }
327
328         int ret = -1;
329
330         if (true == g_dynamic_engine.is_loaded) {
331                 /* Set dynamic command */
332                 ret = g_dynamic_engine.callbacks->set_commands((vce_cmd_h)0);
333                 if (0 != ret) {
334                         SLOG(LOG_WARN, TAG_VCD, "[Engine Agent ERROR] Fail to set command of dynamic engine : error(%d)", ret);
335                         g_dynamic_engine.is_command_ready = false;
336                 } else {
337                         g_dynamic_engine.is_command_ready = true;
338                 }
339
340                 SLOG(LOG_DEBUG, TAG_VCD, "[Engine Agent SUCCESS] set command");
341         } else {
342                 SLOG(LOG_DEBUG, TAG_VCD, "[Engine Agent] Dynamic engine is not available");
343         }
344
345         return 0;
346 }
347
348 int vcd_engine_recognize_start(bool silence)
349 {
350         if (false == g_agent_init) {
351                 SLOG(LOG_ERROR, TAG_VCD, "[Engine Agent ERROR] Not Initialized");
352                 return VCD_ERROR_OPERATION_FAILED;
353         }
354
355         int ret = -1;
356         SLOG(LOG_DEBUG, TAG_VCD, "[Engine Agent] silence is %s", silence ? "true" : "false");
357
358         if (true == g_dynamic_engine.is_loaded && true == g_dynamic_engine.is_command_ready) {
359                 ret = g_dynamic_engine.callbacks->start(silence);
360                 if (0 != ret) {
361                         SLOG(LOG_ERROR, TAG_VCD, "[Engine Agent] Fail to start engine error(%d)", ret);
362                         return VCD_ERROR_OPERATION_FAILED;
363                 }
364         } else {
365                 SLOG(LOG_DEBUG, TAG_VCD, "[Engine Agent] Engine is not available (Cannot start)");
366                 return VCD_ERROR_OPERATION_FAILED;
367         }
368
369         SLOG(LOG_DEBUG, TAG_VCD, "[Engine Agent SUCCESS] Engine start");
370         return 0;
371 }
372
373 int vcd_engine_recognize_audio(const void* data, unsigned int length, vce_speech_detect_e* speech_detected)
374 {
375         if (false == g_agent_init) {
376                 SLOG(LOG_ERROR, TAG_VCD, "[Engine Agent ERROR] Not Initialized");
377                 return VCD_ERROR_OPERATION_FAILED;
378         }
379
380         if (NULL == data) {
381                 SLOG(LOG_ERROR, TAG_VCD, "[Engine Agent ERROR] Invalid Parameter");
382                 return VCD_ERROR_INVALID_PARAMETER;
383         }
384
385         int ret = -1;
386
387         if (true == g_dynamic_engine.is_loaded) {
388                 ret = g_dynamic_engine.callbacks->set_recording(data, length, speech_detected);
389                 if (0 != ret) {
390                         SLOG(LOG_ERROR, TAG_VCD, "[Engine Agent ERROR] Fail to set recording dynamic engine error(%d)", ret);
391                         if (VCE_ERROR_OUT_OF_NETWORK == ret) {
392                                 return VCD_ERROR_TIMED_OUT;
393                         }
394                         return VCD_ERROR_OPERATION_FAILED;
395                 }
396         }
397
398         return 0;
399 }
400
401 int vcd_engine_recognize_stop()
402 {
403         if (false == g_agent_init) {
404                 SLOG(LOG_ERROR, TAG_VCD, "[Engine Agent ERROR] Not Initialized");
405                 return VCD_ERROR_OPERATION_FAILED;
406         }
407
408         if (true == g_dynamic_engine.is_loaded) {
409                 int ret = -1;
410                 ret = g_dynamic_engine.callbacks->stop();
411                 if (0 != ret) {
412                         SLOG(LOG_ERROR, TAG_VCD, "[Engine Agent ERROR] Fail to stop dynamic engine error(%d)", ret);
413                         return VCD_ERROR_OPERATION_FAILED;
414                 }
415         } else {
416                 SLOG(LOG_WARN, TAG_VCD, "[Engine Agent] Dynamic engine is not recording state");
417                 return VCD_ERROR_OPERATION_FAILED;
418         }
419
420         return 0;
421 }
422
423 int vcd_engine_recognize_cancel()
424 {
425         if (false == g_agent_init) {
426                 SLOG(LOG_ERROR, TAG_VCD, "[Engine Agent ERROR] Not Initialized");
427                 return VCD_ERROR_OPERATION_FAILED;
428         }
429
430         int ret = -1;
431         if (true == g_dynamic_engine.is_loaded) {
432                 ret = g_dynamic_engine.callbacks->cancel();
433                 if (0 != ret) {
434                         SLOG(LOG_ERROR, TAG_VCD, "[Engine Agent ERROR] Fail to cancel dynamic engine error(%d)", ret);
435                         return VCD_ERROR_OPERATION_FAILED;
436                 }
437         }
438
439         return 0;
440 }
441
442 int vcd_engine_set_audio_type(const char* audio)
443 {
444         if (false == g_agent_init) {
445                 SLOG(LOG_ERROR, TAG_VCD, "[Engine Agent ERROR] Not Initialized");
446                 return VCD_ERROR_OPERATION_FAILED;
447         }
448
449         int ret = -1;
450         if (true == g_dynamic_engine.is_loaded) {
451                 ret = g_dynamic_engine.callbacks->set_audio_type(audio);
452                 if (0 != ret) {
453                         SLOG(LOG_ERROR, TAG_VCD, "[Engine Agent ERROR] Fail to set audio type(%d)", ret);
454                         return VCD_ERROR_OPERATION_FAILED;
455                 }
456         }
457
458         return 0;
459 }
460
461 int vcd_engine_set_domain(int pid, const char* domain)
462 {
463         if (false == g_agent_init) {
464                 SLOG(LOG_ERROR, TAG_VCD, "[Engine Agent ERROR] Not Initialized");
465                 return VCD_ERROR_OPERATION_FAILED;
466         }
467
468         int ret = -1;
469         if (true == g_dynamic_engine.is_loaded) {
470                 ret = g_dynamic_engine.callbacks->set_domain(domain);
471                 if (0 != ret) {
472                         SLOG(LOG_ERROR, TAG_VCD, "[Engine Agent ERROR] Fail to set domain (%d)", ret);
473                         return VCD_ERROR_OPERATION_FAILED;
474                 }
475         }
476
477         return 0;
478 }
479
480 int vcd_engine_get_nlu_base_info(int pid, const char* key, char** value)
481 {
482         if (NULL == value) {
483                 SLOG(LOG_ERROR, TAG_VCD, "[Engine Agent ERROR] Invalid Parameter");
484                 return VCD_ERROR_INVALID_PARAMETER;
485         }
486
487         if (false == g_agent_init) {
488                 SLOG(LOG_ERROR, TAG_VCD, "[Engine Agent ERROR] Not Initialized");
489                 return VCD_ERROR_OPERATION_FAILED;
490         }
491
492         int ret = -1;
493         if (true == g_dynamic_engine.is_loaded && NULL != g_dynamic_engine.callbacks->nlu_base_info_request) {
494                 ret = g_dynamic_engine.callbacks->nlu_base_info_request(key, value);
495                 if (0 != ret) {
496                         SLOG(LOG_ERROR, TAG_VCD, "[Engine Agent ERROR] Fail to get nlu base info (%d)", ret);
497                         return VCD_ERROR_OPERATION_FAILED;
498                 }
499         } else {
500                 SLOG(LOG_ERROR, TAG_VCD, "[Engine Agent ERROR] Engine is not loaded or There is no nlu_base_info_request callback");
501                 return VCD_ERROR_OPERATION_FAILED;
502         }
503
504         return 0;
505 }
506
507 int vcd_engine_set_private_data(int pid, const char* key, const char* data)
508 {
509         if (false == g_agent_init) {
510                 SLOG(LOG_ERROR, TAG_VCD, "[Engine Agent ERROR] Not Initialized");
511                 return VCD_ERROR_OPERATION_FAILED;
512         }
513
514         int ret = -1;
515         if (true == g_dynamic_engine.is_loaded && NULL != g_dynamic_engine.callbacks->private_data_set) {
516                 ret = g_dynamic_engine.callbacks->private_data_set(key, data);
517                 if (0 != ret) {
518                         SLOG(LOG_ERROR, TAG_VCD, "[Engine Agent ERROR] Fail to set private data (%d)", ret);
519                         return VCD_ERROR_OPERATION_FAILED;
520                 }
521         } else {
522                 SLOG(LOG_ERROR, TAG_VCD, "[Engine Agent ERROR] Engine is not loaded or There is no private_data_set callback");
523                 return VCD_ERROR_OPERATION_FAILED;
524         }
525
526         return 0;
527 }
528
529 int vcd_engine_get_private_data(int pid, const char* key, char** data)
530 {
531         if (NULL == data) {
532                 SLOG(LOG_ERROR, TAG_VCD, "[Engine Agent ERROR] Invalid Parameter");
533                 return VCD_ERROR_INVALID_PARAMETER;
534         }
535
536         if (false == g_agent_init) {
537                 SLOG(LOG_ERROR, TAG_VCD, "[Engine Agent ERROR] Not Initialized");
538                 return VCD_ERROR_OPERATION_FAILED;
539         }
540
541         int ret = -1;
542         if (true == g_dynamic_engine.is_loaded && NULL != g_dynamic_engine.callbacks->private_data_request) {
543                 ret = g_dynamic_engine.callbacks->private_data_request(key, data);
544                 if (0 != ret) {
545                         SLOG(LOG_ERROR, TAG_VCD, "[Engine Agent ERROR] Fail to get private data (%d)", ret);
546                         return VCD_ERROR_OPERATION_FAILED;
547                 }
548         } else {
549                 SLOG(LOG_ERROR, TAG_VCD, "[Engine Agent ERROR] Engine is not loaded or There is no private_data_request callback");
550                 return VCD_ERROR_OPERATION_FAILED;
551         }
552
553         return 0;
554 }
555
556 int vcd_engine_process_text(int pid, const char* text)
557 {
558         if (false == g_agent_init) {
559                 SLOG(LOG_ERROR, TAG_VCD, "[Engine Agent ERROR] Not Initialized");
560                 return VCD_ERROR_OPERATION_FAILED;
561         }
562
563         int ret = -1;
564         if (true == g_dynamic_engine.is_loaded) {
565                 ret = g_dynamic_engine.callbacks->process_text(text);
566                 if (0 != ret) {
567                         SLOG(LOG_ERROR, TAG_VCD, "[Engine Agent ERROR] Fail to process text (%d)", ret);
568                         return VCD_ERROR_OPERATION_FAILED;
569                 }
570         }
571
572         return 0;
573 }
574
575 int vcd_engine_process_list_event(int pid, const char* event)
576 {
577         if (false == g_agent_init) {
578                 SLOG(LOG_ERROR, TAG_VCD, "[Engine Agent ERROR] Not Initialized");
579                 return VCD_ERROR_OPERATION_FAILED;
580         }
581
582         int ret = -1;
583         if (true == g_dynamic_engine.is_loaded) {
584                 ret = g_dynamic_engine.callbacks->process_list_event(event);
585                 if (0 != ret) {
586                         SLOG(LOG_ERROR, TAG_VCD, "[Engine Agent ERROR] Fail to process list event (%d)", ret);
587                         return VCD_ERROR_OPERATION_FAILED;
588                 }
589         }
590
591         return 0;
592 }
593
594 int vcd_engine_process_haptic_event(int pid, const char* event)
595 {
596         if (false == g_agent_init) {
597                 SLOG(LOG_ERROR, TAG_VCD, "[Engine Agent ERROR] Not Initialized");
598                 return VCD_ERROR_OPERATION_FAILED;
599         }
600
601         int ret = -1;
602         if (true == g_dynamic_engine.is_loaded) {
603                 ret = g_dynamic_engine.callbacks->process_haptic_event(event);
604                 if (0 != ret) {
605                         SLOG(LOG_ERROR, TAG_VCD, "[Engine Agent ERROR] Fail to process haptic event (%d)", ret);
606                         return VCD_ERROR_OPERATION_FAILED;
607                 }
608         }
609
610         return 0;
611 }
612
613 /*
614  * VCS Engine Interfaces for client and setting
615  */
616
617 int vcd_engine_get_audio_format(const char* audio_id, vce_audio_type_e* types, int* rate, int* channels)
618 {
619         if (false == g_agent_init) {
620                 SLOG(LOG_ERROR, TAG_VCD, "[Engine Agent ERROR] Not Initialized");
621                 return VCD_ERROR_OPERATION_FAILED;
622         }
623
624         if (true != g_dynamic_engine.is_loaded) {
625                 SLOG(LOG_ERROR, TAG_VCD, "[Engine Agent ERROR] Engine is not loaded");
626         }
627
628         int ret = g_dynamic_engine.callbacks->get_recording_format(audio_id, types, rate, channels);
629         if (0 != ret) {
630                 SLOG(LOG_ERROR, TAG_VCD, "[Engine Agent ERROR] get recording format(%d)", ret);
631                 return VCD_ERROR_OPERATION_FAILED;
632         }
633
634         return 0;
635 }
636
637 bool __supported_language_cb(const char* language, void* user_data)
638 {
639         GList** lang_list = (GList**)user_data;
640
641         if (NULL == language || NULL == lang_list) {
642                 SLOG(LOG_ERROR, TAG_VCD, "[Engine Agent ERROR] Input parameter is NULL in callback!!!!");
643                 return false;
644         }
645
646         SLOG(LOG_DEBUG, TAG_VCD, "-- Language(%s)", language);
647
648         char* temp_lang = g_strdup(language);
649
650         *lang_list = g_list_append(*lang_list, temp_lang);
651
652         return true;
653 }
654
655 int vcd_engine_supported_langs(GList** lang_list)
656 {
657         if (false == g_agent_init) {
658                 SLOG(LOG_ERROR, TAG_VCD, "[Engine Agent ERROR] Not Initialized");
659                 return VCD_ERROR_OPERATION_FAILED;
660         }
661
662         if (true != g_dynamic_engine.is_loaded) {
663                 SLOG(LOG_ERROR, TAG_VCD, "[Engine Agent ERROR] Engine is not loaded");
664         }
665
666         int ret = g_dynamic_engine.callbacks->foreach_langs(__supported_language_cb, (void*)lang_list);
667         if (0 != ret) {
668                 SLOG(LOG_ERROR, TAG_VCD, "[Engine Agent ERROR] get language list error(%d)", ret);
669                 return VCD_ERROR_OPERATION_FAILED;
670         }
671
672         return 0;
673 }
674
675
676 int vcd_engine_get_current_language(char** lang)
677 {
678         if (false == g_agent_init) {
679                 SLOG(LOG_ERROR, TAG_VCD, "[Engine Agent ERROR] Not Initialized");
680                 return VCD_ERROR_OPERATION_FAILED;
681         }
682
683         if (NULL == lang) {
684                 SLOG(LOG_ERROR, TAG_VCD, "[Engine Agent ERROR] Invalid Parameter");
685                 return VCD_ERROR_INVALID_PARAMETER;
686         }
687
688         /* get default language */
689         *lang = g_strdup(g_default_lang);
690
691         return 0;
692 }
693
694 int vcd_engine_set_current_language(const char* language)
695 {
696         if (false == g_agent_init) {
697                 SLOG(LOG_ERROR, TAG_VCD, "[Engine Agent ERROR] Not Initialized");
698                 return VCD_ERROR_OPERATION_FAILED;
699         }
700
701         if (NULL == language) {
702                 SLOG(LOG_ERROR, TAG_VCD, "[Engine Agent ERROR] Invalid Parameter");
703                 return VCD_ERROR_INVALID_PARAMETER;
704         }
705
706         int ret;
707
708         if (true == g_dynamic_engine.is_loaded) {
709                 g_dynamic_engine.is_command_ready = false;
710
711                 ret = g_dynamic_engine.callbacks->set_language(language);
712                 if (0 != ret) {
713                         SLOG(LOG_WARN, TAG_VCD, "[Engine Agent] Fail to set language of dynamic engine error(%d, %s)", ret, language);
714                 }
715         } else {
716                 SLOG(LOG_DEBUG, TAG_VCD, "[Engine Agent] Dynamic engine is not available (Cannot start)");
717         }
718
719         return 0;
720 }
721
722 int vcd_engine_agent_get_foreach_command(vce_cmd_h vce_command, vce_command_cb callback, void* user_data)
723 {
724         SLOG(LOG_DEBUG, TAG_VCD, "[Engine Agent] Request foreach command from engine");
725         return vcd_client_foreach_command((client_foreach_command_cb)callback, user_data);
726 }
727
728 int vcd_engine_agent_get_command_count(vce_cmd_h vce_command)
729 {
730         SLOG(LOG_DEBUG, TAG_VCD, "[Engine Agent] Request command length from engine");
731
732         return vcd_client_get_length();
733 }
734
735 int vcd_engine_agent_get_audio_type(char** audio_type)
736 {
737         SLOG(LOG_DEBUG, TAG_VCD, "[Engine Agent] Request audio type");
738
739         return vcd_recorder_get(audio_type);
740 }
741
742 int vcd_engine_agent_set_private_data(const char* key, const char* data)
743 {
744         SLOG(LOG_DEBUG, TAG_VCD, "[Engine Agent] Request set private data, key(%s), data(%s)", key, data);
745         vcdc_send_request_set_private_data(vcd_client_manager_get_pid(), key, data);
746
747         return VCD_ERROR_NONE;
748 }
749
750 int vcd_engine_agent_get_private_data(const char* key, char** data)
751 {
752         SLOG(LOG_DEBUG, TAG_VCD, "[Engine Agent] Request get private data, key(%s)", key);
753         vcdc_send_request_get_private_data(vcd_client_manager_get_pid(), key, data);
754
755         return VCD_ERROR_NONE;
756 }
757
758 int vcd_engine_agent_start_recording()
759 {
760         SLOG(LOG_DEBUG, TAG_VCD, "[Engine Agent] Request start recording");
761
762         int ret = vcd_recorder_start();
763         if (0 != ret) {
764                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to start recorder : result(%d)", ret);
765                 vcd_engine_recognize_cancel();
766                 /* Send error cb to manager */
767                 vcdc_send_error_signal_to_manager(vcd_client_manager_get_pid(), VCD_ERROR_OPERATION_FAILED, "voice_engine.error.proc_fail");
768                 return ret;
769         }
770
771         return VCD_ERROR_NONE;
772 }
773
774 int vcd_engine_agent_stop_recording()
775 {
776         SLOG(LOG_DEBUG, TAG_VCD, "[Engine Agent] Request stop recording");
777
778         return vcd_recorder_stop();
779 }
780
781 int vcd_engine_agent_set_private_data_set_cb(vce_private_data_set_cb callback_func)
782 {
783         if (false == g_agent_init) {
784                 SLOG(LOG_ERROR, TAG_VCD, "[Engine Agent ERROR] Not Initialized");
785                 return VCD_ERROR_OPERATION_FAILED;
786         }
787
788         if (false == g_dynamic_engine.is_loaded) {
789                 SLOG(LOG_ERROR, TAG_VCD, "[Engine Agent ERROR] Not loaded engine");
790                 return VCD_ERROR_OPERATION_FAILED;
791         }
792
793         g_dynamic_engine.callbacks->private_data_set = callback_func;
794
795         return VCD_ERROR_NONE;
796 }
797
798 int vcd_engine_agent_set_private_data_requested_cb(vce_private_data_requested_cb callback_func)
799 {
800         if (false == g_agent_init) {
801                 SLOG(LOG_ERROR, TAG_VCD, "[Engine Agent ERROR] Not Initialized");
802                 return VCD_ERROR_OPERATION_FAILED;
803         }
804
805         if (false == g_dynamic_engine.is_loaded) {
806                 SLOG(LOG_ERROR, TAG_VCD, "[Engine Agent ERROR] Not loaded engine");
807                 return VCD_ERROR_OPERATION_FAILED;
808         }
809
810         g_dynamic_engine.callbacks->private_data_request = callback_func;
811
812         return VCD_ERROR_NONE;
813 }
814
815 int vcd_engine_agent_set_nlu_base_info_requested_cb(vce_nlu_base_info_requested_cb callback_func)
816 {
817         if (false == g_agent_init) {
818                 SLOG(LOG_ERROR, TAG_VCD, "[Engine Agent ERROR] Not Initialized");
819                 return VCD_ERROR_OPERATION_FAILED;
820         }
821
822         if (false == g_dynamic_engine.is_loaded) {
823                 SLOG(LOG_ERROR, TAG_VCD, "[Engine Agent ERROR] Not loaded engine");
824                 return VCD_ERROR_OPERATION_FAILED;
825         }
826
827         g_dynamic_engine.callbacks->nlu_base_info_request = callback_func;
828
829         return VCD_ERROR_NONE;
830 }
831