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