Enabling Svoice and db
[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
27 /*
28 * Internal data structure
29 */
30 typedef struct {
31         /* engine info */
32         char*   engine_uuid;
33         char*   engine_name;
34         char*   engine_path;
35
36         /* engine load info */
37         bool    is_set;
38         bool    is_loaded;
39         bool    is_command_ready;
40         void    *handle;
41
42         vcpe_funcs_s*   pefuncs;
43         vcpd_funcs_s*   pdfuncs;
44
45         int (*vcp_load_engine)(vcpd_funcs_s* pdfuncs, vcpe_funcs_s* pefuncs);
46         int (*vcp_unload_engine)();
47 } vcengine_s;
48
49 typedef struct _vcengine_info {
50         char*   engine_uuid;
51         char*   engine_path;
52         char*   engine_name;
53 } vcengine_info_s;
54
55
56 /*
57 * static data
58 */
59
60 /** vc engine agent init */
61 static bool g_agent_init;
62
63 /** vc engine list */
64 static GList *g_engine_list;
65
66 /** current engine information */
67 static vcengine_s g_dynamic_engine;
68
69 static char* g_default_lang;
70
71 /** callback functions */
72 static result_callback g_result_cb = NULL;
73
74 static asr_result_callback g_asr_result_cb = NULL;
75
76 #if 0
77 static pre_result_callback g_pre_result_cb = NULL;
78
79 static nlu_result_callback g_nlu_result_cb = NULL;
80 #endif
81
82 static error_callback g_error_cb = NULL;
83
84 bool __supported_language_cb(const char* language, void* user_data);
85
86 void __engine_info_cb(const char* engine_uuid, const char* engine_name, const char* engine_setting, bool use_network, void* user_data);
87
88 bool __engine_setting_cb(const char* key, const char* value, void* user_data);
89
90 /** Free voice list */
91 void __free_language_list(GList* lang_list);
92
93
94 /*
95 * Internal Interfaces
96 */
97
98 /** check engine id */
99 int __internal_check_engine_id(const char* engine_uuid);
100
101 /** update engine list */
102 int __internal_update_engine_list();
103
104 /** get engine info */
105 int __internal_get_engine_info(const char* filepath, vcengine_info_s** info);
106
107 int __log_enginelist();
108
109 /*
110 * VCS Engine Agent Interfaces
111 */
112 //int vcd_engine_agent_init(pre_result_callback pre_result_cb, result_callback result_cb, nlu_result_callback nlu_result_cb, error_callback error_cb)
113 int vcd_engine_agent_init(asr_result_callback asr_result_cb, result_callback result_cb, error_callback error_cb)
114 {
115         if (/*NULL == pre_result_cb*/ NULL == asr_result_cb || NULL == result_cb /*|| NULL == nlu_result_cb*/ || NULL == error_cb) {
116                 SLOG(LOG_ERROR, TAG_VCD, "[Engine Agent ERROR] Input parameter is NULL");
117                 return VCD_ERROR_OPERATION_FAILED;
118         }
119
120         /* init dynamic engine */
121         g_dynamic_engine.engine_uuid = NULL;
122         g_dynamic_engine.engine_name = NULL;
123         g_dynamic_engine.engine_path = NULL;
124
125         g_dynamic_engine.is_set = false;
126         g_dynamic_engine.is_loaded = false;
127         g_dynamic_engine.handle = NULL;
128         g_dynamic_engine.is_command_ready = false;
129         g_dynamic_engine.pefuncs = (vcpe_funcs_s*)calloc(1, sizeof(vcpe_funcs_s));
130         g_dynamic_engine.pdfuncs = (vcpd_funcs_s*)calloc(1, sizeof(vcpd_funcs_s));
131
132         g_agent_init = true;
133
134         //g_pre_result_cb = pre_result_cb;
135         g_asr_result_cb = asr_result_cb;
136         g_result_cb = result_cb;
137         //g_nlu_result_cb = nlu_result_cb;
138         g_error_cb = error_cb;
139
140         if (0 != vcd_config_get_default_language(&g_default_lang)) {
141                 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] There is No default voice in config");
142                 /* Set default voice */
143                 g_default_lang = strdup(VC_BASE_LANGUAGE);
144         }
145
146         SLOG(LOG_DEBUG, TAG_VCD, "[Engine Agent SUCCESS] Engine Agent Initialize");
147
148         return 0;
149 }
150
151 int vcd_engine_agent_release()
152 {
153         if (false == g_agent_init) {
154                 SLOG(LOG_ERROR, TAG_VCD, "[Engine Agent ERROR] Not Initialized");
155                 return VCD_ERROR_OPERATION_FAILED;
156         }
157
158         /* unload current engine */
159         vcd_engine_agent_unload_current_engine();
160
161         /* release engine list */
162         GList *iter = NULL;
163         vcengine_s *data = NULL;
164
165         if (g_list_length(g_engine_list) > 0) {
166                 /* Get a first item */
167                 iter = g_list_first(g_engine_list);
168
169                 while (NULL != iter) {
170                         /* Get handle data from list */
171                         data = iter->data;
172                         iter = g_list_remove(iter, data);
173                 }
174         }
175
176         g_list_free(iter);
177
178         /* release current engine data */
179         if (NULL != g_dynamic_engine.pefuncs) {
180                 free(g_dynamic_engine.pefuncs);
181                 g_dynamic_engine.pefuncs = NULL;
182         }
183         if (NULL != g_dynamic_engine.pdfuncs) {
184                 free(g_dynamic_engine.pdfuncs);
185                 g_dynamic_engine.pdfuncs = NULL;
186         }
187
188         g_agent_init = false;
189
190         //g_pre_result_cb = NULL;
191         g_asr_result_cb = NULL;
192         g_result_cb = NULL;
193         //g_nlu_result_cb = NULL;
194         g_error_cb = NULL;
195
196         SLOG(LOG_DEBUG, TAG_VCD, "[Engine Agent SUCCESS] Engine Agent release");
197
198         return 0;
199 }
200
201 bool vcd_engine_is_available_engine()
202 {
203         if (true == g_dynamic_engine.is_loaded)
204                 return true;
205
206         return false;
207 }
208
209 int vcd_engine_agent_initialize_current_engine()
210 {
211         /* check agent init */
212         if (false == g_agent_init) {
213                 SLOG(LOG_ERROR, TAG_VCD, "[Engine Agent ERROR] Not Initialized");
214                 return VCD_ERROR_OPERATION_FAILED;
215         }
216
217         /* update engine list */
218         if (0 != __internal_update_engine_list()) {
219                 SLOG(LOG_ERROR, TAG_VCD, "[engine agent] vcd_engine_agent_init : __internal_update_engine_list : no engine error");
220                 return VCD_ERROR_ENGINE_NOT_FOUND;
221         }
222
223         /* check whether engine id is valid or not.*/
224         GList *iter = NULL;
225         vcengine_info_s *dynamic_engine = NULL;
226
227         if (g_list_length(g_engine_list) > 0) {
228                 /*Get a first item*/
229                 iter = g_list_first(g_engine_list);
230
231                 while (NULL != iter) {
232                         /*Get handle data from list*/
233                         dynamic_engine = iter->data;
234                         if (NULL != dynamic_engine) {
235                                 break;
236                         }
237
238                         /*Get next item*/
239                         iter = g_list_next(iter);
240                 }
241         } else {
242                 return VCD_ERROR_ENGINE_NOT_FOUND;
243         }
244
245         if (NULL == dynamic_engine) {
246                 return VCD_ERROR_ENGINE_NOT_FOUND;
247         } else {
248                 if (NULL != g_dynamic_engine.engine_uuid) {
249                         /* set data from g_engine_list */
250                         if (g_dynamic_engine.engine_uuid != NULL)       free(g_dynamic_engine.engine_uuid);
251                         if (g_dynamic_engine.engine_name != NULL)       free(g_dynamic_engine.engine_name);
252                         if (g_dynamic_engine.engine_path != NULL)       free(g_dynamic_engine.engine_path);
253                 }
254
255                 g_dynamic_engine.engine_uuid = g_strdup(dynamic_engine->engine_uuid);
256                 g_dynamic_engine.engine_name = g_strdup(dynamic_engine->engine_name);
257                 g_dynamic_engine.engine_path = g_strdup(dynamic_engine->engine_path);
258
259                 g_dynamic_engine.handle = NULL;
260                 g_dynamic_engine.is_loaded = false;
261                 g_dynamic_engine.is_set = true;
262
263                 SLOG(LOG_DEBUG, TAG_VCD, "-----");
264                 SLOG(LOG_DEBUG, TAG_VCD, " Dynamic engine uuid : %s", g_dynamic_engine.engine_uuid);
265                 SLOG(LOG_DEBUG, TAG_VCD, " Dynamic engine name : %s", g_dynamic_engine.engine_name);
266                 SLOG(LOG_DEBUG, TAG_VCD, " Dynamic engine path : %s", g_dynamic_engine.engine_path);
267                 SLOG(LOG_DEBUG, TAG_VCD, "-----");
268
269         }
270
271         return 0;
272 }
273
274 int __internal_check_engine_id(const char* engine_uuid)
275 {
276         if (NULL == engine_uuid) {
277                 SLOG(LOG_ERROR, TAG_VCD, "[Engine Agent ERROR] Invalid Parameter");
278                 return VCD_ERROR_INVALID_PARAMETER;
279         }
280
281         GList *iter = NULL;
282         vcengine_s *data = NULL;
283
284         if (0 < g_list_length(g_engine_list)) {
285                 /*Get a first item*/
286                 iter = g_list_first(g_engine_list);
287
288                 while (NULL != iter) {
289                         data = iter->data;
290
291                         if (0 == strncmp(engine_uuid, data->engine_uuid, strlen(data->engine_uuid))) {
292                                 return 0;
293                         }
294
295                         iter = g_list_next(iter);
296                 }
297         }
298
299         return -1;
300 }
301
302 void __engine_info_cb(const char* engine_uuid, const char* engine_name, const char* engine_setting, bool use_network, void* user_data)
303 {
304         vcengine_info_s* temp = (vcengine_info_s*)user_data;
305
306         temp->engine_uuid = g_strdup(engine_uuid);
307         temp->engine_name = g_strdup(engine_name);
308 }
309
310
311 int __internal_get_engine_info(const char* filepath, vcengine_info_s** info)
312 {
313         if (NULL == filepath || NULL == info) {
314                 SLOG(LOG_ERROR, TAG_VCD, "[Engine Agent ERROR] Invalid Parameter");
315                 return VCD_ERROR_INVALID_PARAMETER;
316         }
317
318         /* load engine */
319         char *error;
320         void* handle;
321
322         handle = dlopen(filepath, RTLD_LAZY);
323         if (!handle) {
324                 SLOG(LOG_WARN, TAG_VCD, "[Engine Agent] Invalid engine : %s", filepath);
325                 if ((error = dlerror()) != NULL) {
326                         SLOG(LOG_ERROR, TAG_VCD, "[ERROR] %s", error);
327                 }
328                 return -1;
329         }
330
331         /* link engine to daemon */
332         dlsym(handle, "vcp_load_engine");
333         if ((error = dlerror()) != NULL) {
334                 SLOG(LOG_WARN, TAG_VCD, "[Engine Agent] Invalid engine. Fail to open vcp_load_engine : %s", filepath);
335                 dlclose(handle);
336                 return -1;
337         }
338
339         dlsym(handle, "vcp_unload_engine");
340         if ((error = dlerror()) != NULL) {
341                 SLOG(LOG_WARN, TAG_VCD, "[Engine Agent] Invalid engine. Fail to open vcp_unload_engine : %s", filepath);
342                 dlclose(handle);
343                 return -1;
344         }
345
346         int (*get_engine_info)(vcpe_engine_info_cb callback, void* user_data);
347
348         get_engine_info = (int (*)(vcpe_engine_info_cb, void*))dlsym(handle, "vcp_get_engine_info");
349         if (NULL != (error = dlerror()) || NULL == get_engine_info) {
350                 SLOG(LOG_WARN, TAG_VCD, "[Engine Agent WARNING] Invalid engine. Fail to open vcp_get_engine_info : %s", filepath);
351                 dlclose(handle);
352                 return -1;
353         }
354
355         vcengine_info_s* temp;
356         temp = (vcengine_info_s*)calloc(1, sizeof(vcengine_info_s));
357         if (NULL == temp) {
358                 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Fail to allocate memory");
359                 dlclose(handle);
360                 return VCD_ERROR_OUT_OF_MEMORY;
361         }
362
363         /* get engine info */
364         if (0 != get_engine_info(__engine_info_cb, (void*)temp)) {
365                 SLOG(LOG_ERROR, TAG_VCD, "[Engine Agent ERROR] Fail to get engine info from engine");
366                 dlclose(handle);
367                 free(temp);
368                 return -1;
369         }
370
371         /* close engine */
372         dlclose(handle);
373
374         temp->engine_path = g_strdup(filepath);
375
376         SLOG(LOG_DEBUG, TAG_VCD, "----- Valid Engine");
377         SLOG(LOG_DEBUG, TAG_VCD, "Engine uuid : %s", temp->engine_uuid);
378         SLOG(LOG_DEBUG, TAG_VCD, "Engine name : %s", temp->engine_name);
379         SLOG(LOG_DEBUG, TAG_VCD, "Engine path : %s", temp->engine_path);
380         SLOG(LOG_DEBUG, TAG_VCD, "-----");
381         SLOG(LOG_DEBUG, TAG_VCD, "  ");
382
383         *info = temp;
384
385         return 0;
386 }
387
388 int __internal_update_engine_list()
389 {
390         /* relsease engine list */
391         GList *iter = NULL;
392         vcengine_info_s *data = NULL;
393
394         if (0 < g_list_length(g_engine_list)) {
395                 /* Get a first item */
396                 iter = g_list_first(g_engine_list);
397
398                 while (NULL != iter) {
399                         /* Get handle data from list */
400                         data = iter->data;
401
402                         if (NULL != data) {
403                                 if (NULL != data->engine_uuid)          free(data->engine_uuid);
404                                 if (NULL != data->engine_path)          free(data->engine_path);
405                                 if (NULL != data->engine_name)          free(data->engine_name);
406
407                                 free(data);
408                         }
409
410                         g_engine_list = g_list_remove_link(g_engine_list, iter);
411                         iter = g_list_first(g_engine_list);
412                 }
413         }
414
415         /* Get file name from default engine directory */
416         DIR *dp = NULL;
417         int ret = -1;
418         struct dirent entry;
419         struct dirent *dirp = NULL;
420
421         dp  = opendir(VC_DEFAULT_ENGINE);
422         if (NULL != dp) {
423                 do {
424                         ret = readdir_r(dp, &entry, &dirp);
425                         if (0 != ret) {
426                                 SLOG(LOG_ERROR, TAG_VCD, "[File ERROR] Fail to read directory");
427                                 break;
428                         }
429
430                         if (NULL != dirp) {
431                                 vcengine_info_s* info = NULL;
432                                 char* filepath = NULL;
433                                 int filesize = 0;
434
435                                 filesize = strlen(VC_DEFAULT_ENGINE) + strlen(dirp->d_name) + 5;
436                                 filepath = (char*)calloc(filesize, sizeof(char));
437
438                                 if (NULL != filepath) {
439                                         snprintf(filepath, filesize, "%s/%s", VC_DEFAULT_ENGINE, dirp->d_name);
440                                 } else {
441                                         SLOG(LOG_ERROR, TAG_VCD, "[Engine Agent ERROR] Memory not enough!!");
442                                         continue;
443                                 }
444
445                                 /* get its info and update engine list */
446                                 if (0 == __internal_get_engine_info(filepath, &info)) {
447                                         /* add engine info to g_engine_list */
448                                         g_engine_list = g_list_append(g_engine_list, info);
449                                 }
450
451                                 if (NULL != filepath) {
452                                         free(filepath);
453                                         filepath = NULL;
454                                 }
455                         }
456                 } while (NULL != dirp);
457
458                 closedir(dp);
459         } else {
460                 SLOG(LOG_WARN, TAG_VCD, "[Engine Agent WARNING] Fail to open default directory");
461         }
462
463         if (0 >= g_list_length(g_engine_list)) {
464                 SLOG(LOG_ERROR, TAG_VCD, "[Engine Agent ERROR] No Engine");
465                 return VCD_ERROR_ENGINE_NOT_FOUND;
466         }
467
468         __log_enginelist();
469
470         return 0;
471 }
472
473
474 int __foreach_command(vcp_cmd_h vc_command, vcpd_foreach_command_cb callback, void* user_data)
475 {
476         SLOG(LOG_DEBUG, TAG_VCD, "[Engine Agent] Request foreach command from engine");
477         return vcd_client_foreach_command((client_foreach_command_cb)callback, user_data);
478 }
479
480 int __command_get_length(vcp_cmd_h vc_command)
481 {
482         SLOG(LOG_DEBUG, TAG_VCD, "[Engine Agent] Request command length from engine");
483         return vcd_client_get_length();
484 }
485
486 int __get_audio_type(char** audio_type)
487 {
488         SLOG(LOG_DEBUG, TAG_VCD, "[Engine Agent] Request audio type");
489
490         return vcd_recorder_get(audio_type);
491 }
492
493 void __result_cb(vcp_result_event_e event, int* result_id, int count, const char* all_result, const char* non_fixed, const char* nlu_result, const char* msg, void *user_data)
494 {
495         SLOG(LOG_DEBUG, TAG_VCD, "[Engine agent] Event(%d), Count(%d) Text(%s) Nonfixed(%s) NLU result(%s) Msg(%s)", event, count, all_result, non_fixed, nlu_result, msg);
496
497         if (NULL != g_result_cb) {
498                 g_result_cb(event, result_id, count, all_result, non_fixed, nlu_result, msg, user_data);
499         } else {
500                 SLOG(LOG_DEBUG, TAG_VCD, "[Engine Agent ERROR] Result callback function is NOT valid");
501         }
502
503         return;
504 }
505
506 void __asr_result_cb(vcp_asr_result_event_e event, const char* asr_result, void *user_data)
507 {
508         SLOG(LOG_DEBUG, TAG_VCD, "[Engine Agent] ASR result -  Event(%d), Result(%s)", event, asr_result);
509
510         if (NULL != g_asr_result_cb) {
511                 g_asr_result_cb(event, asr_result, user_data);
512         } else {
513                 SLOG(LOG_DEBUG, TAG_VCD, "[Engine Agent ERROR] ASR result callback function is NOT valid");
514         }
515
516         return;
517 }
518
519 void __error_cb(vcp_error_e error, const char* msg, void *user_data)
520 {
521         SLOG(LOG_ERROR, TAG_VCD, "[Engine Agent] ERROR(%d)", error);
522
523         if (NULL != g_error_cb) {
524                 g_error_cb(error, msg, user_data);
525         } else {
526                 SLOG(LOG_DEBUG, TAG_VCD, "[Engine Agent ERROR] Error callback function is NOT vaild");
527         }
528
529         return;
530 }
531
532 int __load_engine(vcengine_s* engine)
533 {
534         /* check whether current engine is loaded or not */
535         if (true == engine->is_loaded) {
536                 SLOG(LOG_DEBUG, TAG_VCD, "[Engine Agent] Engine has already been loaded ");
537                 return 0;
538         }
539
540         SLOG(LOG_DEBUG, TAG_VCD, "[Engine Agent] Current engine path : %s", engine->engine_path);
541
542         /* open engine */
543         char *error;
544         engine->handle = dlopen(engine->engine_path, RTLD_LAZY);
545
546         if ((error = dlerror()) != NULL || !engine->handle) {
547                 SLOG(LOG_ERROR, TAG_VCD, "[Engine Agent ERROR] Fail to get engine handle");
548                 return VCD_ERROR_OPERATION_FAILED;
549         }
550
551         engine->vcp_unload_engine = (int (*)())dlsym(engine->handle, "vcp_unload_engine");
552         if ((error = dlerror()) != NULL) {
553                 SLOG(LOG_ERROR, TAG_VCD, "[Engine Agent ERROR] Fail to link daemon to vcp_unload_engine()");
554                 dlclose(engine->handle);
555                 return VCD_ERROR_OPERATION_FAILED;
556         }
557
558         engine->vcp_load_engine = (int (*)(vcpd_funcs_s*, vcpe_funcs_s*))dlsym(engine->handle, "vcp_load_engine");
559         if (NULL != (error = dlerror()) || NULL == engine->vcp_load_engine) {
560                 SLOG(LOG_ERROR, TAG_VCD, "[Engine Agent ERROR] Fail to link daemon to vcp_load_engine()");
561                 dlclose(engine->handle);
562                 return VCD_ERROR_OPERATION_FAILED;
563         }
564
565         /* load engine */
566         engine->pdfuncs->version = 1;
567         engine->pdfuncs->size = sizeof(vcpd_funcs_s);
568
569         engine->pdfuncs->foreach_command = __foreach_command;
570         engine->pdfuncs->get_command_count = __command_get_length;
571         engine->pdfuncs->get_audio_type = __get_audio_type;
572
573         if (0 != engine->vcp_load_engine(engine->pdfuncs, engine->pefuncs)) {
574                 SLOG(LOG_ERROR, TAG_VCD, "[Engine Agent ERROR] Fail vcp_load_engine()");
575                 dlclose(engine->handle);
576                 return VCD_ERROR_OPERATION_FAILED;
577         }
578
579         SLOG(LOG_DEBUG, TAG_VCD, "[Engine Agent] engine info : version(%d), size(%d)", engine->pefuncs->version, engine->pefuncs->size);
580
581         /* engine error check */
582         if (engine->pefuncs->size != sizeof(vcpe_funcs_s)) {
583                 SLOG(LOG_ERROR, TAG_VCD, "[Engine Agent ERROR] Engine is not valid");
584                 return VCD_ERROR_OPERATION_FAILED;
585         }
586
587         /* Check all engine functions */
588         if (NULL == engine->pefuncs->initialize ||
589                 NULL == engine->pefuncs->deinitialize ||
590                 NULL == engine->pefuncs->get_recording_format ||
591                 NULL == engine->pefuncs->foreach_langs ||
592                 NULL == engine->pefuncs->is_lang_supported ||
593                 NULL == engine->pefuncs->set_result_cb ||
594                 NULL == engine->pefuncs->set_language ||
595                 NULL == engine->pefuncs->set_commands ||
596                 NULL == engine->pefuncs->unset_commands ||
597                 NULL == engine->pefuncs->start ||
598                 NULL == engine->pefuncs->set_recording ||
599                 NULL == engine->pefuncs->stop ||
600                 NULL == engine->pefuncs->cancel ||
601                 NULL == engine->pefuncs->set_asr_result_cb ||
602                 //NULL == engine->pefuncs->set_pre_result_cb ||
603                 NULL == engine->pefuncs->set_error_cb ||
604                 NULL == engine->pefuncs->set_domain ||
605                 NULL == engine->pefuncs->get_nlu_base_info ||
606                 //NULL == engine->pefuncs->set_nlu_result_cb ||
607                 NULL == engine->pefuncs->set_private_data ||
608                 NULL == engine->pefuncs->get_private_data ||
609                 NULL == engine->pefuncs->process_text ||
610                 NULL == engine->pefuncs->process_list_event ||
611                 NULL == engine->pefuncs->process_haptic_event) {
612                 SLOG(LOG_ERROR, TAG_VCD, "[Engine Agent ERROR] The current engine is NOT valid");
613                 return VCD_ERROR_OPERATION_FAILED;
614         }
615
616         /* initalize engine */
617         if (0 != engine->pefuncs->initialize()) {
618                 SLOG(LOG_ERROR, TAG_VCD, "[Engine Agent ERROR] Fail to initialize vc-engine");
619                 return VCD_ERROR_OPERATION_FAILED;
620         }
621
622         if (0 != engine->pefuncs->set_result_cb(__result_cb, NULL)) {
623                 SLOG(LOG_ERROR, TAG_VCD, "[Engine Agent ERROR] Fail to set result callback of vc-engine");
624                 return VCD_ERROR_OPERATION_FAILED;
625         }
626
627         if (0 != engine->pefuncs->set_asr_result_cb(__asr_result_cb, NULL)) {
628                 SLOG(LOG_ERROR, TAG_VCD, "[Engine Agent ERROR] Fail to set asr result callback of vc-engine");
629                 return VCD_ERROR_OPERATION_FAILED;
630         }
631
632         if (0 != engine->pefuncs->set_error_cb(__error_cb, NULL)) {
633                 SLOG(LOG_ERROR, TAG_VCD, "[Engine Agent ERROR] Fail to set error callback of vc-engine");
634                 return VCD_ERROR_OPERATION_FAILED;
635         }
636
637         /* load engine */
638         if (true == engine->pefuncs->is_lang_supported(g_default_lang)) {
639                 if (0 != engine->pefuncs->set_language(g_default_lang)) {
640                         SLOG(LOG_ERROR, TAG_VCD, "[Engine Agent ERROR] Fail to load current engine");
641                         return VCD_ERROR_OPERATION_FAILED;
642                 }
643                 SLOG(LOG_DEBUG, TAG_VCD, "[Engine Agent SUCCESS] The %s has been loaded !!!", engine->engine_name);
644                 engine->is_loaded = true;
645         } else {
646                 SLOG(LOG_WARN, TAG_VCD, "[Engine Agent WARNING] This engine do not support default language : lang(%s)", g_default_lang);
647                 engine->is_loaded = false;
648         }
649
650         return 0;
651 }
652
653 int vcd_engine_agent_load_current_engine()
654 {
655         if (false == g_agent_init) {
656                 SLOG(LOG_ERROR, TAG_VCD, "[Engine Agent ERROR] Not Initialized");
657                 return VCD_ERROR_OPERATION_FAILED;
658         }
659
660         if (true == g_dynamic_engine.is_set) {
661                 if (0 != __load_engine(&g_dynamic_engine)) {
662                         SLOG(LOG_ERROR, TAG_VCD, "[Engine Agent ERROR] Fail to load dynamic engine");
663
664                         /* need to initialize dynamic engine data */
665                         g_dynamic_engine.is_loaded = false;
666                 } else {
667                         SLOG(LOG_DEBUG, TAG_VCD, "[Engine Agent SUCCESS] Load dynamic engine");
668                 }
669         }
670
671         return 0;
672 }
673
674 int vcd_engine_agent_unload_current_engine()
675 {
676         if (false == g_agent_init) {
677                 SLOG(LOG_ERROR, TAG_VCD, "[Engine Agent ERROR] Not Initialized ");
678                 return VCD_ERROR_OPERATION_FAILED;
679         }
680
681         if (true == g_dynamic_engine.is_set) {
682                 /* unload dynamic engine */
683                 if (true == g_dynamic_engine.is_loaded) {
684                         /* shutdown engine */
685                         g_dynamic_engine.pefuncs->deinitialize();
686                         g_dynamic_engine.vcp_unload_engine();
687                         dlclose(g_dynamic_engine.handle);
688                         g_dynamic_engine.handle = NULL;
689                         g_dynamic_engine.is_loaded = false;
690                 }
691         }
692         return 0;
693 }
694
695
696 /*
697 * VCS Engine Interfaces for client
698 */
699
700 int vcd_engine_set_commands()
701 {
702         if (false == g_agent_init) {
703                 SLOG(LOG_ERROR, TAG_VCD, "[Engine Agent ERROR] Not Initialized");
704                 return VCD_ERROR_OPERATION_FAILED;
705         }
706
707         int ret = -1;
708
709         if (true == g_dynamic_engine.is_loaded) {
710                 /* Set dynamic command */
711                 ret = g_dynamic_engine.pefuncs->set_commands((vcp_cmd_h)0);
712                 if (0 != ret) {
713                         SLOG(LOG_WARN, TAG_VCD, "[Engine Agent ERROR] Fail to set command of dynamic engine : error(%d)", ret);
714                         g_dynamic_engine.is_command_ready = false;
715                 } else {
716                         g_dynamic_engine.is_command_ready = true;
717                 }
718
719                 SLOG(LOG_DEBUG, TAG_VCD, "[Engine Agent SUCCESS] set command");
720         } else {
721                 SLOG(LOG_DEBUG, TAG_VCD, "[Engine Agent] Dynamic engine is not available");
722         }
723
724         return 0;
725 }
726
727 int vcd_engine_recognize_start(bool silence)
728 {
729         if (false == g_agent_init) {
730                 SLOG(LOG_ERROR, TAG_VCD, "[Engine Agent ERROR] Not Initialized");
731                 return VCD_ERROR_OPERATION_FAILED;
732         }
733
734         int ret = -1;
735         SLOG(LOG_DEBUG, TAG_VCD, "[Engine Agent] silence is %s", silence ? "true" : "false");
736
737         if (true == g_dynamic_engine.is_loaded && true == g_dynamic_engine.is_command_ready) {
738                 ret = g_dynamic_engine.pefuncs->start(silence);
739                 if (0 != ret) {
740                         SLOG(LOG_ERROR, TAG_VCD, "[Engine Agent] Fail to start engine error(%d)", ret);
741                         return VCD_ERROR_OPERATION_FAILED;
742                 }
743         } else {
744                 SLOG(LOG_DEBUG, TAG_VCD, "[Engine Agent] Engine is not available (Cannot start)");
745                 return VCD_ERROR_OPERATION_FAILED;
746         }
747
748         SLOG(LOG_DEBUG, TAG_VCD, "[Engine Agent SUCCESS] Engine start");
749         return 0;
750 }
751
752 int vcd_engine_recognize_audio(const void* data, unsigned int length, vcp_speech_detect_e* speech_detected)
753 {
754         if (false == g_agent_init) {
755                 SLOG(LOG_ERROR, TAG_VCD, "[Engine Agent ERROR] Not Initialized");
756                 return VCD_ERROR_OPERATION_FAILED;
757         }
758
759         if (NULL == data) {
760                 SLOG(LOG_ERROR, TAG_VCD, "[Engine Agent ERROR] Invalid Parameter");
761                 return VCD_ERROR_INVALID_PARAMETER;
762         }
763
764         int ret = -1;
765
766         if (true == g_dynamic_engine.is_loaded) {
767                 ret = g_dynamic_engine.pefuncs->set_recording(data, length, speech_detected);
768                 if (0 != ret) {
769                         SLOG(LOG_ERROR, TAG_VCD, "[Engine Agent ERROR] Fail to set recording dynamic engine error(%d)", ret);
770                         if (VCP_ERROR_OUT_OF_NETWORK == ret) {
771                                 return VCD_ERROR_TIMED_OUT;
772                         }
773                         return VCD_ERROR_OPERATION_FAILED;
774                 }
775         }
776
777         return 0;
778 }
779
780 int vcd_engine_recognize_stop()
781 {
782         if (false == g_agent_init) {
783                 SLOG(LOG_ERROR, TAG_VCD, "[Engine Agent ERROR] Not Initialized");
784                 return VCD_ERROR_OPERATION_FAILED;
785         }
786
787         if (true == g_dynamic_engine.is_loaded) {
788                 int ret = -1;
789                 ret = g_dynamic_engine.pefuncs->stop();
790                 if (0 != ret) {
791                         SLOG(LOG_ERROR, TAG_VCD, "[Engine Agent ERROR] Fail to stop dynamic engine error(%d)", ret);
792                         return VCD_ERROR_OPERATION_FAILED;
793                 }
794         } else {
795                 SLOG(LOG_WARN, TAG_VCD, "[Engine Agent] Dynamic engine is not recording state");
796                 return VCD_ERROR_OPERATION_FAILED;
797         }
798
799         return 0;
800 }
801
802 int vcd_engine_recognize_cancel()
803 {
804         if (false == g_agent_init) {
805                 SLOG(LOG_ERROR, TAG_VCD, "[Engine Agent ERROR] Not Initialized");
806                 return VCD_ERROR_OPERATION_FAILED;
807         }
808
809         int ret = -1;
810         if (true == g_dynamic_engine.is_loaded) {
811                 ret = g_dynamic_engine.pefuncs->cancel();
812                 if (0 != ret) {
813                         SLOG(LOG_ERROR, TAG_VCD, "[Engine Agent ERROR] Fail to cancel dynamic engine error(%d)", ret);
814                         return VCD_ERROR_OPERATION_FAILED;
815                 }
816         }
817
818         return 0;
819 }
820
821 int vcd_engine_set_domain(int pid, const char* domain)
822 {
823         if (false == g_agent_init) {
824                 SLOG(LOG_ERROR, TAG_VCD, "[Engine Agent ERROR] Not Initialized");
825                 return VCD_ERROR_OPERATION_FAILED;
826         }
827
828         int ret = -1;
829         if (true == g_dynamic_engine.is_loaded) {
830                 ret = g_dynamic_engine.pefuncs->set_domain(domain);
831                 if (0 != ret) {
832                         SLOG(LOG_ERROR, TAG_VCD, "[Engine Agent ERROR] Fail to set domain (%d)", ret);
833                         return VCD_ERROR_OPERATION_FAILED;
834                 }
835         }
836
837         return 0;
838 }
839
840 int vcd_engine_get_nlu_base_info(int pid, const char* key, char** value)
841 {
842         if (NULL == value) {
843                 SLOG(LOG_ERROR, TAG_VCD, "[Engine Agent ERROR] Invalid Parameter");
844                 return VCD_ERROR_INVALID_PARAMETER;
845         }
846
847         if (false == g_agent_init) {
848                 SLOG(LOG_ERROR, TAG_VCD, "[Engine Agent ERROR] Not Initialized");
849                 return VCD_ERROR_OPERATION_FAILED;
850         }
851
852         int ret = -1;
853         if (true == g_dynamic_engine.is_loaded) {
854                 ret = g_dynamic_engine.pefuncs->get_nlu_base_info(key, value);
855                 if (0 != ret) {
856                         SLOG(LOG_ERROR, TAG_VCD, "[Engine Agent ERROR] Fail to get nlu base info (%d)", ret);
857                         return VCD_ERROR_OPERATION_FAILED;
858                 }
859         }
860
861         return 0;
862 }
863
864 int vcd_engine_set_private_data(int pid, const char* key, const char* data)
865 {
866         if (false == g_agent_init) {
867                 SLOG(LOG_ERROR, TAG_VCD, "[Engine Agent ERROR] Not Initialized");
868                 return VCD_ERROR_OPERATION_FAILED;
869         }
870
871         int ret = -1;
872         if (true == g_dynamic_engine.is_loaded) {
873                 ret = g_dynamic_engine.pefuncs->set_private_data(key, data);
874                 if (0 != ret) {
875                         SLOG(LOG_ERROR, TAG_VCD, "[Engine Agent ERROR] Fail to set private data (%d)", ret);
876                         return VCD_ERROR_OPERATION_FAILED;
877                 }
878         }
879
880         return 0;
881 }
882
883 int vcd_engine_get_private_data(int pid, const char* key, char** data)
884 {
885         if (NULL == data) {
886                 SLOG(LOG_ERROR, TAG_VCD, "[Engine Agent ERROR] Invalid Parameter");
887                 return VCD_ERROR_INVALID_PARAMETER;
888         }
889
890         if (false == g_agent_init) {
891                 SLOG(LOG_ERROR, TAG_VCD, "[Engine Agent ERROR] Not Initialized");
892                 return VCD_ERROR_OPERATION_FAILED;
893         }
894
895         int ret = -1;
896         if (true == g_dynamic_engine.is_loaded) {
897                 ret = g_dynamic_engine.pefuncs->get_private_data(key, data);
898                 if (0 != ret) {
899                         SLOG(LOG_ERROR, TAG_VCD, "[Engine Agent ERROR] Fail to get private data (%d)", ret);
900                         return VCD_ERROR_OPERATION_FAILED;
901                 }
902         }
903
904         return 0;
905 }
906
907 int vcd_engine_process_text(int pid, const char* text)
908 {
909         if (false == g_agent_init) {
910                 SLOG(LOG_ERROR, TAG_VCD, "[Engine Agent ERROR] Not Initialized");
911                 return VCD_ERROR_OPERATION_FAILED;
912         }
913
914         int ret = -1;
915         if (true == g_dynamic_engine.is_loaded) {
916                 ret = g_dynamic_engine.pefuncs->process_text(text);
917                 if (0 != ret) {
918                         SLOG(LOG_ERROR, TAG_VCD, "[Engine Agent ERROR] Fail to process text (%d)", ret);
919                         return VCD_ERROR_OPERATION_FAILED;
920                 }
921         }
922
923         return 0;
924 }
925
926 int vcd_engine_process_list_event(int pid, const char* event)
927 {
928         if (false == g_agent_init) {
929                 SLOG(LOG_ERROR, TAG_VCD, "[Engine Agent ERROR] Not Initialized");
930                 return VCD_ERROR_OPERATION_FAILED;
931         }
932
933         int ret = -1;
934         if (true == g_dynamic_engine.is_loaded) {
935                 ret = g_dynamic_engine.pefuncs->process_list_event(event);
936                 if (0 != ret) {
937                         SLOG(LOG_ERROR, TAG_VCD, "[Engine Agent ERROR] Fail to process list event (%d)", ret);
938                         return VCD_ERROR_OPERATION_FAILED;
939                 }
940         }
941
942         return 0;
943 }
944
945 int vcd_engine_process_haptic_event(int pid, const char* event)
946 {
947         if (false == g_agent_init) {
948                 SLOG(LOG_ERROR, TAG_VCD, "[Engine Agent ERROR] Not Initialized");
949                 return VCD_ERROR_OPERATION_FAILED;
950         }
951
952         int ret = -1;
953         if (true == g_dynamic_engine.is_loaded) {
954                 ret = g_dynamic_engine.pefuncs->process_haptic_event(event);
955                 if (0 != ret) {
956                         SLOG(LOG_ERROR, TAG_VCD, "[Engine Agent ERROR] Fail to process haptic event (%d)", ret);
957                         return VCD_ERROR_OPERATION_FAILED;
958                 }
959         }
960
961         return 0;
962 }
963
964 /*
965 * VCS Engine Interfaces for client and setting
966 */
967
968 int vcd_engine_get_audio_format(const char* audio_id, vcp_audio_type_e* types, int* rate, int* channels)
969 {
970         if (false == g_agent_init) {
971                 SLOG(LOG_ERROR, TAG_VCD, "[Engine Agent ERROR] Not Initialized");
972                 return VCD_ERROR_OPERATION_FAILED;
973         }
974
975         if (true != g_dynamic_engine.is_loaded) {
976                 SLOG(LOG_ERROR, TAG_VCD, "[Engine Agent ERROR] Engine is not loaded");
977         }
978
979         if (NULL == g_dynamic_engine.pefuncs->get_recording_format) {
980                 SLOG(LOG_ERROR, TAG_VCD, "[Engine Agent ERROR] The function of engine is NULL!!");
981                 return VCD_ERROR_OPERATION_FAILED;
982         }
983
984         int ret = g_dynamic_engine.pefuncs->get_recording_format(audio_id, types, rate, channels);
985         if (0 != ret) {
986                 SLOG(LOG_ERROR, TAG_VCD, "[Engine Agent ERROR] get recording format(%d)", ret);
987                 return VCD_ERROR_OPERATION_FAILED;
988         }
989
990         return 0;
991 }
992
993 bool __supported_language_cb(const char* language, void* user_data)
994 {
995         GList** lang_list = (GList**)user_data;
996
997         if (NULL == language || NULL == lang_list) {
998                 SLOG(LOG_ERROR, TAG_VCD, "[Engine Agent ERROR] Input parameter is NULL in callback!!!!");
999                 return false;
1000         }
1001
1002         SLOG(LOG_DEBUG, TAG_VCD, "-- Language(%s)", language);
1003
1004         char* temp_lang = g_strdup(language);
1005
1006         *lang_list = g_list_append(*lang_list, temp_lang);
1007
1008         return true;
1009 }
1010
1011 int vcd_engine_supported_langs(GList** lang_list)
1012 {
1013         if (false == g_agent_init) {
1014                 SLOG(LOG_ERROR, TAG_VCD, "[Engine Agent ERROR] Not Initialized");
1015                 return VCD_ERROR_OPERATION_FAILED;
1016         }
1017
1018         if (true != g_dynamic_engine.is_loaded) {
1019                 SLOG(LOG_ERROR, TAG_VCD, "[Engine Agent ERROR] Engine is not loaded");
1020         }
1021
1022         if (NULL == g_dynamic_engine.pefuncs->foreach_langs) {
1023                 SLOG(LOG_ERROR, TAG_VCD, "[Engine Agent ERROR] The function of engine is NULL!!");
1024                 return VCD_ERROR_OPERATION_FAILED;
1025         }
1026
1027         int ret = g_dynamic_engine.pefuncs->foreach_langs(__supported_language_cb, (void*)lang_list);
1028         if (0 != ret) {
1029                 SLOG(LOG_ERROR, TAG_VCD, "[Engine Agent ERROR] get language list error(%d)", ret);
1030                 return VCD_ERROR_OPERATION_FAILED;
1031         }
1032
1033         return 0;
1034 }
1035
1036
1037 int vcd_engine_get_current_language(char** lang)
1038 {
1039         if (false == g_agent_init) {
1040                 SLOG(LOG_ERROR, TAG_VCD, "[Engine Agent ERROR] Not Initialized");
1041                 return VCD_ERROR_OPERATION_FAILED;
1042         }
1043
1044         if (NULL == lang) {
1045                 SLOG(LOG_ERROR, TAG_VCD, "[Engine Agent ERROR] Invalid Parameter");
1046                 return VCD_ERROR_INVALID_PARAMETER;
1047         }
1048
1049         /* get default language */
1050         *lang = g_strdup(g_default_lang);
1051
1052         return 0;
1053 }
1054
1055 int vcd_engine_set_current_language(const char* language)
1056 {
1057         if (false == g_agent_init) {
1058                 SLOG(LOG_ERROR, TAG_VCD, "[Engine Agent ERROR] Not Initialized");
1059                 return VCD_ERROR_OPERATION_FAILED;
1060         }
1061
1062         if (NULL == language) {
1063                 SLOG(LOG_ERROR, TAG_VCD, "[Engine Agent ERROR] Invalid Parameter");
1064                 return VCD_ERROR_INVALID_PARAMETER;
1065         }
1066
1067         int ret;
1068
1069         if (true == g_dynamic_engine.is_loaded) {
1070                 g_dynamic_engine.is_command_ready = false;
1071
1072                 ret = g_dynamic_engine.pefuncs->set_language(language);
1073                 if (0 != ret) {
1074                         SLOG(LOG_WARN, TAG_VCD, "[Engine Agent] Fail to set language of dynamic engine error(%d, %s)", ret, language);
1075                 }
1076         } else {
1077                 SLOG(LOG_DEBUG, TAG_VCD, "[Engine Agent] Dynamic engine is not available (Cannot start)");
1078         }
1079
1080         return 0;
1081 }
1082
1083 void __free_language_list(GList* lang_list)
1084 {
1085         GList *iter = NULL;
1086         char* data = NULL;
1087
1088         /* if list have item */
1089         if (g_list_length(lang_list) > 0) {
1090                 /* Get a first item */
1091                 iter = g_list_first(lang_list);
1092
1093                 while (NULL != iter) {
1094                         data = iter->data;
1095
1096                         if (NULL != data)
1097                                 free(data);
1098
1099                         lang_list = g_list_remove_link(lang_list, iter);
1100
1101                         iter = g_list_first(lang_list);
1102                 }
1103         }
1104 }
1105
1106 int __log_enginelist()
1107 {
1108         GList *iter = NULL;
1109         vcengine_info_s *data = NULL;
1110
1111         if (0 < g_list_length(g_engine_list)) {
1112
1113                 /* Get a first item */
1114                 iter = g_list_first(g_engine_list);
1115
1116                 SLOG(LOG_DEBUG, TAG_VCD, "--------------- engine list -------------------");
1117
1118                 int i = 1;
1119                 while (NULL != iter) {
1120                         /* Get handle data from list */
1121                         data = iter->data;
1122
1123                         SLOG(LOG_DEBUG, TAG_VCD, "[%dth]", i);
1124                         SLOG(LOG_DEBUG, TAG_VCD, "  engine uuid : %s", data->engine_uuid);
1125                         SLOG(LOG_DEBUG, TAG_VCD, "  engine name : %s", data->engine_name);
1126                         SLOG(LOG_DEBUG, TAG_VCD, "  engine path : %s", data->engine_path);
1127                         iter = g_list_next(iter);
1128                         i++;
1129                 }
1130                 SLOG(LOG_DEBUG, TAG_VCD, "----------------------------------------------");
1131         } else {
1132                 SLOG(LOG_DEBUG, TAG_VCD, "-------------- engine list -------------------");
1133                 SLOG(LOG_DEBUG, TAG_VCD, "  No Engine in engine directory");
1134                 SLOG(LOG_DEBUG, TAG_VCD, "----------------------------------------------");
1135         }
1136
1137         return 0;
1138 }
1139
1140
1141
1142