57944a270037152b887cd408cf13b9866400f6a6
[platform/core/uifw/stt.git] / common / stt_engine.c
1 /*
2 *  Copyright (c) 2011-2016 Samsung Electronics Co., Ltd All Rights Reserved
3 *  Licensed under the Apache License, Version 2.0 (the "License");
4 *  you may not use this file except in compliance with the License.
5 *  You may obtain a copy of the License at
6 *  http://www.apache.org/licenses/LICENSE-2.0
7 *  Unless required by applicable law or agreed to in writing, software
8 *  distributed under the License is distributed on an "AS IS" BASIS,
9 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10 *  See the License for the specific language governing permissions and
11 *  limitations under the License.
12 */
13
14 #include <dlog.h>
15 #include <dlfcn.h>
16 #include <dirent.h>
17 #include <stdio.h>
18 #include <stdlib.h>
19 #include <stdbool.h>
20
21 #include "stt_engine.h"
22 #include "sttd_engine_agent.h"
23
24 /*
25 * Internal data structure
26 */
27
28 typedef struct {
29         char*   engine_path;
30         stte_request_callback_s *callback;
31 } sttengine_s;
32
33 extern const char* stt_tag();
34
35 /** stt engine */
36 static sttengine_s *g_engine = NULL;
37
38 static bool g_is_from_lib = false;
39
40 /** callback functions */
41 static stt_engine_result_cb g_result_cb = NULL;
42 static stte_private_data_set_cb g_set_private_data_cb = NULL;
43 static stte_private_data_requested_cb g_get_private_data_cb = NULL;
44
45
46 static int __stt_set_engine_from(bool is_from_lib)
47 {
48         g_is_from_lib = is_from_lib;
49         return 0;
50 }
51
52 static bool __stt_get_engine_from(void)
53 {
54         return g_is_from_lib;
55 }
56
57 static const char* __stt_get_engine_error_code(stte_error_e err)
58 {
59         switch (err) {
60         case STTE_ERROR_NONE:                   return "STTE_ERROR_NONE";
61         case STTE_ERROR_OUT_OF_MEMORY:          return "STTE_ERROR_OUT_OF_MEMORY";
62         case STTE_ERROR_IO_ERROR:               return "STTE_ERROR_IO_ERROR";
63         case STTE_ERROR_INVALID_PARAMETER:      return "STTE_ERROR_INVALID_PARAMETER";
64         case STTE_ERROR_NETWORK_DOWN:           return "STTE_ERROR_NETWORK_DOWN";
65         case STTE_ERROR_PERMISSION_DENIED:      return "STTE_ERROR_PERMISSION_DENIED";
66         case STTE_ERROR_NOT_SUPPORTED:          return "STTE_ERROR_NOT_SUPPORTED";
67         case STTE_ERROR_INVALID_STATE:          return "STTE_ERROR_INVALID_STATE";
68         case STTE_ERROR_INVALID_LANGUAGE:       return "STTE_ERROR_INVALID_LANGUAGE";
69         case STTE_ERROR_OPERATION_FAILED:       return "STTE_ERROR_OPERATION_FAILED";
70         case STTE_ERROR_NOT_SUPPORTED_FEATURE:  return "STTE_ERROR_NOT_SUPPORTED_FEATURE";
71         case STTE_ERROR_RECORDING_TIMED_OUT:    return "STTE_ERROR_RECORDING_TIMED_OUT";
72         default:
73                 return "Invalid error code";
74         }
75 }
76
77 /* Register engine id */
78 int stt_engine_load(const char* filepath, stte_request_callback_s *callback)
79 {
80         if (NULL == callback || NULL == filepath) {
81                 SLOG(LOG_ERROR, stt_tag(), "[Engine ERROR] Invalid Parameter");
82                 return STTE_ERROR_INVALID_PARAMETER;
83         }
84
85         /* allocation memory */
86         if (NULL != g_engine) {
87                 SLOG(LOG_WARN, stt_tag(), "[Engine WARNING] engine is already loaded");
88         } else {
89                 g_engine = (sttengine_s*)calloc(1, sizeof(sttengine_s));
90                 if (NULL == g_engine) {
91                         SLOG(LOG_ERROR, stt_tag(), "[ERROR] Fail to allocate memory");
92                         return STTE_ERROR_OUT_OF_MEMORY;
93                 }
94
95                 /* load engine */
96                 g_engine->callback = callback;
97                 g_engine->engine_path = strdup(filepath);
98         }
99
100         SLOG(LOG_DEBUG, stt_tag(), "[Engine Success] Load engine : version(%d)", g_engine->callback->version);
101
102         return 0;
103 }
104
105 /* Unregister engine id */
106 int stt_engine_unload()
107 {
108         if (NULL == g_engine) {
109                 SLOG(LOG_ERROR, stt_tag(), "[Engine ERROR] No engine");
110                 return STTE_ERROR_OPERATION_FAILED;
111         }
112
113         g_engine->callback = NULL;
114
115         if (NULL != g_engine->engine_path) {
116                 free(g_engine->engine_path);
117                 g_engine->engine_path = NULL;
118         }
119
120         free(g_engine);
121         g_engine = NULL;
122
123         return 0;
124 }
125
126
127 /* Initialize / Deinitialize */
128 int stt_engine_initialize(bool is_from_lib)
129 {
130         if (NULL == g_engine) {
131                 SLOG(LOG_ERROR, stt_tag(), "[Engine ERROR] No engine");
132                 return STTE_ERROR_OPERATION_FAILED;
133         }
134
135         if (NULL == g_engine->callback) {
136                 SLOG(LOG_ERROR, stt_tag(), "[Engine ERROR] Invalid engine");
137                 return STTE_ERROR_OPERATION_FAILED;
138         }
139
140         if (NULL == g_engine->callback->initialize) {
141                 SLOG(LOG_ERROR, stt_tag(), "[Engine ERROR] Invalid engine");
142                 return STTE_ERROR_OPERATION_FAILED;
143         }
144
145         int ret;
146         ret = __stt_set_engine_from(is_from_lib);
147         if (0 != ret) {
148                 SLOG(LOG_ERROR, stt_tag(), "[Engine ERROR] Fail to set engine : %s", __stt_get_engine_error_code(ret));
149                 return STTE_ERROR_OPERATION_FAILED;
150         }
151
152         SLOG(LOG_INFO, stt_tag(), "[Engine Info] request to initialize");
153
154         ret = g_engine->callback->initialize();
155         if (0 != ret) {
156                 SLOG(LOG_ERROR, stt_tag(), "[Engine ERROR] Fail to initialize : %s", __stt_get_engine_error_code(ret));
157         }
158
159         return ret;
160 }
161
162 int stt_engine_deinitialize()
163 {
164         if (NULL == g_engine) {
165                 SLOG(LOG_ERROR, stt_tag(), "[Engine ERROR] No engine");
166                 return STTE_ERROR_OPERATION_FAILED;
167         }
168
169         if (NULL == g_engine->callback) {
170                 SLOG(LOG_ERROR, stt_tag(), "[Engine ERROR] Invalid engine");
171                 return STTE_ERROR_OPERATION_FAILED;
172         }
173
174         if (NULL == g_engine->callback->deinitialize) {
175                 SLOG(LOG_ERROR, stt_tag(), "[Engine ERROR] Invalid engine");
176                 return STTE_ERROR_OPERATION_FAILED;
177         }
178
179         SLOG(LOG_INFO, stt_tag(), "[Engine Info] request to deinitialize");
180
181         int ret;
182         ret = g_engine->callback->deinitialize();
183         if (0 != ret) {
184                 SLOG(LOG_WARN, stt_tag(), "[Engine WARNING] Fail to deinitialize : %s", __stt_get_engine_error_code(ret));
185         }
186
187         return ret;
188 }
189
190 static bool __supported_language_cb(const char* language, void* user_data)
191 {
192         GSList** lang_list = (GSList**)user_data;
193
194         if (NULL == language || NULL == lang_list) {
195                 SLOG(LOG_ERROR, stt_tag(), "[Engine ERROR] Input parameter is NULL in callback!!!!");
196                 return false;
197         }
198
199         char* temp_lang = g_strdup(language);
200
201         *lang_list = g_slist_append(*lang_list, temp_lang);
202
203         return true;
204 }
205
206 /* Get option */
207 int stt_engine_get_supported_langs(GSList** lang_list)
208 {
209         if (NULL == lang_list) {
210                 SLOG(LOG_ERROR, stt_tag(), "[Engine ERROR] Invalid Parameter");
211                 return STTE_ERROR_INVALID_PARAMETER;
212         }
213
214         if (NULL == g_engine) {
215                 SLOG(LOG_ERROR, stt_tag(), "[Engine ERROR] No engine");
216                 return STTE_ERROR_OPERATION_FAILED;
217         }
218
219         if (NULL == g_engine->callback) {
220                 SLOG(LOG_ERROR, stt_tag(), "[Engine ERROR] Invalid engine");
221                 return STTE_ERROR_OPERATION_FAILED;
222         }
223
224         if (NULL == g_engine->callback->foreach_langs) {
225                 SLOG(LOG_ERROR, stt_tag(), "[Engine ERROR] Invalid engine");
226                 return STTE_ERROR_OPERATION_FAILED;
227         }
228
229         int ret;
230         ret = g_engine->callback->foreach_langs(__supported_language_cb, (void*)lang_list);
231         if (0 != ret) {
232                 SLOG(LOG_ERROR, stt_tag(), "[Engine ERROR] get language list error : %s", __stt_get_engine_error_code(ret));
233         }
234
235         return ret;
236 }
237
238 int stt_engine_is_valid_language(const char* language, bool *is_valid)
239 {
240         if (NULL == language || NULL == is_valid) {
241                 SLOG(LOG_ERROR, stt_tag(), "[Engine ERROR] Invalid Parameter");
242                 return STTE_ERROR_INVALID_PARAMETER;
243         }
244
245         if (NULL == g_engine) {
246                 SLOG(LOG_ERROR, stt_tag(), "[Engine ERROR] No engine");
247                 return STTE_ERROR_OPERATION_FAILED;
248         }
249
250         if (NULL == g_engine->callback) {
251                 SLOG(LOG_ERROR, stt_tag(), "[Engine ERROR] Invalid engine");
252                 return STTE_ERROR_OPERATION_FAILED;
253         }
254
255         if (NULL == g_engine->callback->is_valid_lang) {
256                 SLOG(LOG_ERROR, stt_tag(), "[Engine ERROR] Invalid engine");
257                 return STTE_ERROR_OPERATION_FAILED;
258         }
259
260         int ret = STTE_ERROR_NONE;
261         ret = g_engine->callback->is_valid_lang(language, is_valid);
262         if (0 != ret) {
263                 SLOG(LOG_ERROR, stt_tag(), "[Engine ERROR] Fail to check valid language(%d)", ret);
264         }
265         return ret;
266 }
267
268 int stt_engine_set_private_data(const char* key, const char* data)
269 {
270         if (NULL == key || NULL == data) {
271                 SLOG(LOG_ERROR, stt_tag(), "[Engine ERROR] Invalid Parameter");
272                 return STTE_ERROR_INVALID_PARAMETER;
273         }
274
275         int ret = STTE_ERROR_NONE;
276         if (NULL != g_set_private_data_cb) {
277                 ret = g_set_private_data_cb(key, data);
278                 if (0 != ret) {
279                         SLOG(LOG_ERROR, stt_tag(), "[Engine ERROR] Fail to set private data(%d)", ret);
280                 }
281         }
282
283         return ret;
284 }
285
286 int stt_engine_get_private_data(const char* key, char** data)
287 {
288         if (NULL == key || NULL == data) {
289                 SLOG(LOG_ERROR, stt_tag(), "[Engine ERROR] Invalid Parameter");
290                 return STTE_ERROR_INVALID_PARAMETER;
291         }
292
293         int ret = STTE_ERROR_NONE;
294         char* temp = NULL;
295         if (NULL != g_get_private_data_cb) {
296                 ret = g_get_private_data_cb(key, &temp);
297                 if (0 != ret) {
298                         SLOG(LOG_ERROR, stt_tag(), "[Engine ERROR] Fail to get private data(%d)", ret);
299                         return ret;
300                 }
301         } else {
302                 SLOG(LOG_ERROR, stt_tag(), "[Engine ERROR] There's no private data function)");
303         }
304
305         if (NULL == temp)
306                 *data = strdup("NULL");
307         else
308                 *data = strdup(temp);
309
310         return STTE_ERROR_NONE;
311 }
312
313 int stt_engine_get_first_language(char** language)
314 {
315         if (NULL == language) {
316                 SLOG(LOG_ERROR, stt_tag(), "[Engine ERROR] Invalid Parameter");
317                 return STTE_ERROR_INVALID_PARAMETER;
318         }
319
320         if (NULL == g_engine) {
321                 SLOG(LOG_ERROR, stt_tag(), "[Engine ERROR] No engine");
322                 return STTE_ERROR_OPERATION_FAILED;
323         }
324
325         if (NULL == g_engine->callback) {
326                 SLOG(LOG_ERROR, stt_tag(), "[Engine ERROR] Invalid engine");
327                 return STTE_ERROR_OPERATION_FAILED;
328         }
329
330         if (NULL == g_engine->callback->foreach_langs || NULL == g_engine->callback->is_valid_lang) {
331                 SLOG(LOG_ERROR, stt_tag(), "[Engine ERROR] Invalid engine");
332                 return STTE_ERROR_OPERATION_FAILED;
333         }
334
335         GSList* lang_list = NULL;
336         int ret;
337         ret = g_engine->callback->foreach_langs(__supported_language_cb, &lang_list);
338         if (0 != ret) {
339                 SLOG(LOG_ERROR, stt_tag(), "[Engine ERROR] get language list error : %s", __stt_get_engine_error_code(ret));
340                 return ret;
341         }
342
343         GSList *iter = NULL;
344         char* data = NULL;
345
346         iter = g_slist_nth(lang_list, 0);
347         if (NULL != iter) {
348                 data = iter->data;
349
350                 bool is_valid = false;
351                 ret = g_engine->callback->is_valid_lang(data, &is_valid);
352                 if (0 == ret && true == is_valid) {
353                         *language = strdup(data);
354                 } else {
355                         ret = STTE_ERROR_OPERATION_FAILED;
356                 }
357         }
358
359         /* if list have item */
360         if (g_slist_length(lang_list) > 0) {
361                 /* Get a first item */
362                 iter = g_slist_nth(lang_list, 0);
363
364                 while (NULL != iter) {
365                         data = iter->data;
366
367                         if (NULL != data) {
368                                 free(data);
369                                 data = NULL;
370                         }
371
372                         lang_list = g_slist_remove_link(lang_list, iter);
373
374                         iter = g_slist_nth(lang_list, 0);
375                 }
376         }
377
378         return ret;
379 }
380
381 int stt_engine_support_silence(bool* support)
382 {
383         if (NULL == support) {
384                 SLOG(LOG_ERROR, stt_tag(), "[Engine ERROR] Invalid Parameter");
385                 return STTE_ERROR_INVALID_PARAMETER;
386         }
387
388         if (NULL == g_engine) {
389                 SLOG(LOG_ERROR, stt_tag(), "[Engine ERROR] No engine");
390                 return STTE_ERROR_OPERATION_FAILED;
391         }
392
393         if (NULL == g_engine->callback) {
394                 SLOG(LOG_ERROR, stt_tag(), "[Engine ERROR] Invalid engine");
395                 return STTE_ERROR_OPERATION_FAILED;
396         }
397
398         SLOG(LOG_INFO, stt_tag(), "[Engine Info] request to support silence");
399
400         if (NULL == g_engine->callback->support_silence) {
401                 SLOG(LOG_ERROR, stt_tag(), "[Engine ERROR] Invalid engine");
402                 return STTE_ERROR_OPERATION_FAILED;
403         }
404
405         bool result;
406         result = g_engine->callback->support_silence();
407         *support = result;
408
409         return STTE_ERROR_NONE;
410 }
411
412 int stt_engine_need_app_credential(bool* need)
413 {
414         if (NULL == need) {
415                 SLOG(LOG_ERROR, stt_tag(), "[Engine ERROR] Invalid Parameter");
416                 return STTE_ERROR_INVALID_PARAMETER;
417         }
418
419         if (NULL == g_engine) {
420                 SLOG(LOG_ERROR, stt_tag(), "[Engine ERROR] No engine");
421                 return STTE_ERROR_OPERATION_FAILED;
422         }
423
424         if (NULL == g_engine->callback) {
425                 SLOG(LOG_ERROR, stt_tag(), "[Engine ERROR] Invalid engine");
426                 return STTE_ERROR_OPERATION_FAILED;
427         }
428
429         SLOG(LOG_INFO, stt_tag(), "[Engine Info] request to need app credential");
430
431         if (NULL == g_engine->callback->need_app_credential) {
432                 SLOG(LOG_ERROR, stt_tag(), "[Engine ERROR] Invalid engine");
433                 return STTE_ERROR_OPERATION_FAILED;
434         }
435
436         bool result;
437         result = g_engine->callback->need_app_credential();
438         *need = result;
439
440         return STTE_ERROR_NONE;
441 }
442
443 int stt_engine_support_recognition_type(const char* type, bool* support)
444 {
445         if (NULL == type || NULL == support) {
446                 SLOG(LOG_ERROR, stt_tag(), "[Engine ERROR] Invalid Parameter");
447                 return STTE_ERROR_INVALID_PARAMETER;
448         }
449
450         if (NULL == g_engine) {
451                 SLOG(LOG_ERROR, stt_tag(), "[Engine ERROR] No engine");
452                 return STTE_ERROR_OPERATION_FAILED;
453         }
454
455         if (NULL == g_engine->callback) {
456                 SLOG(LOG_ERROR, stt_tag(), "[Engine ERROR] Invalid engine");
457                 return STTE_ERROR_OPERATION_FAILED;
458         }
459
460         if (NULL == g_engine->callback->support_recognition_type) {
461                 SLOG(LOG_ERROR, stt_tag(), "[Engine ERROR} Invalid engine");
462                 return STTE_ERROR_OPERATION_FAILED;
463         }
464
465         SLOG(LOG_INFO, stt_tag(), "[Engine Info] request to support recognition type, type(%s)", type);
466
467         int ret = STTE_ERROR_NONE;
468         ret = g_engine->callback->support_recognition_type(type, support);
469         if (0 != ret) {
470                 SLOG(LOG_ERROR, stt_tag(), "[Engine ERROR] Fail to get supporting recognition type(%d)", ret);
471         }
472         return ret;
473 }
474
475 int stt_engine_get_audio_type(stte_audio_type_e* types, int* rate, int* channels)
476 {
477         if (NULL == types || NULL == rate || NULL == channels) {
478                 SLOG(LOG_ERROR, stt_tag(), "[Engine ERROR] Invalid Parameter");
479                 return STTE_ERROR_INVALID_PARAMETER;
480         }
481
482         if (NULL == g_engine) {
483                 SLOG(LOG_ERROR, stt_tag(), "[Engine ERROR] No engine");
484                 return STTE_ERROR_OPERATION_FAILED;
485         }
486
487         if (NULL == g_engine->callback) {
488                 SLOG(LOG_ERROR, stt_tag(), "[Engine ERROR] Invalid engine");
489                 return STTE_ERROR_OPERATION_FAILED;
490         }
491
492         if (NULL == g_engine->callback->get_audio_format) {
493                 SLOG(LOG_ERROR, stt_tag(), "[Engine ERROR] Invalid engine");
494                 return STTE_ERROR_OPERATION_FAILED;
495         }
496
497         SLOG(LOG_INFO, stt_tag(), "[Engine Info] request to get audio format");
498
499         int ret;
500         ret = g_engine->callback->get_audio_format(types, rate, channels);
501         if (0 != ret) {
502                 SLOG(LOG_ERROR, stt_tag(), "[Engine ERROR] Fail to get audio format : %s", __stt_get_engine_error_code(ret));
503         }
504
505         return ret;
506 }
507
508 /* Set option */
509 int stt_engine_set_silence_detection(bool value)
510 {
511         if (NULL == g_engine) {
512                 SLOG(LOG_ERROR, stt_tag(), "[Engine ERROR] No engine");
513                 return STTE_ERROR_OPERATION_FAILED;
514         }
515
516         if (NULL == g_engine->callback) {
517                 SLOG(LOG_ERROR, stt_tag(), "[Engine ERROR] Invalid engine");
518                 return STTE_ERROR_OPERATION_FAILED;
519         }
520
521         if (NULL == g_engine->callback->set_silence_detection) {
522                 SLOG(LOG_ERROR, stt_tag(), "[Engine ERROR] Invalid engine");
523                 return STTE_ERROR_OPERATION_FAILED;
524         }
525
526         SLOG(LOG_INFO, stt_tag(), "[Engine Info] request to set silence detection(%d)", value);
527
528         int ret = g_engine->callback->set_silence_detection(value);
529         if (STTE_ERROR_NOT_SUPPORTED_FEATURE == ret) {
530                 SLOG(LOG_WARN, stt_tag(), "[Engine WARNING] Not support silence detection");
531         } else if (0 != ret) {
532                 SLOG(LOG_ERROR, stt_tag(), "[Engine ERROR] Fail to set silence detection : %d", ret);
533         }
534         return ret;
535 }
536
537 int stt_engine_check_app_agreed(const char* appid, bool* is_agreed)
538 {
539         if (NULL == is_agreed) {
540                 SLOG(LOG_ERROR, stt_tag(), "[Engine ERROR] Invalid parameter");
541                 return STTE_ERROR_INVALID_PARAMETER;
542         }
543
544         if (NULL == g_engine) {
545                 SLOG(LOG_ERROR, stt_tag(), "[Engine ERROR] No engine");
546                 return STTE_ERROR_OPERATION_FAILED;
547         }
548
549         if (NULL == g_engine->callback) {
550                 SLOG(LOG_ERROR, stt_tag(), "[Engine ERROR] Invalid engine");
551                 return STTE_ERROR_OPERATION_FAILED;
552         }
553
554         if (NULL == g_engine->callback->check_app_agreed) {
555                 SLOG(LOG_WARN, stt_tag(), "[Engine WARNING] Not support app agreement. All app is available");
556                 *is_agreed = true;
557                 return 0;
558         }
559
560         SLOG(LOG_INFO, stt_tag(), "[Engine Info] request to app agreed, appid(%s), is_agreed(%d)", appid, *is_agreed);
561
562         int ret = g_engine->callback->check_app_agreed(appid, is_agreed);
563         if (0 != ret) {
564                 SLOG(LOG_ERROR, stt_tag(), "[Engine ERROR] Fail to get app agreement : %s", __stt_get_engine_error_code(ret));
565                 *is_agreed = false;
566         }
567
568         return ret;
569 }
570
571 /* Recognition */
572 int stt_engine_recognize_start(const char* lang, const char* recognition_type, const char* appid, const char* credential, void* user_param)
573 {
574         if (NULL == lang || NULL == recognition_type) {
575                 SLOG(LOG_ERROR, stt_tag(), "[Engine ERROR] Invalid Parameter");
576                 return STTE_ERROR_INVALID_PARAMETER;
577         }
578
579         if (NULL == g_engine) {
580                 SLOG(LOG_ERROR, stt_tag(), "[Engine ERROR] No engine");
581                 return STTE_ERROR_OPERATION_FAILED;
582         }
583
584         if (NULL == g_engine->callback) {
585                 SLOG(LOG_ERROR, stt_tag(), "[Engine ERROR] Invalid engine");
586                 return STTE_ERROR_OPERATION_FAILED;
587         }
588
589         if (NULL == g_engine->callback->start) {
590                 SLOG(LOG_ERROR, stt_tag(), "[Engine ERROR] Invalid engine");
591                 return STTE_ERROR_OPERATION_FAILED;
592         }
593
594         SLOG(LOG_INFO, stt_tag(), "[Engine Info] request to start, lang(%s), recognition_type(%s), credential(%s)", lang, recognition_type, credential);
595
596         int ret = g_engine->callback->start(lang, recognition_type, appid, credential, user_param);
597
598         if (0 != ret) {
599                 SLOG(LOG_ERROR, stt_tag(), "[Engine ERROR] Fail to start recognition : %s", __stt_get_engine_error_code(ret));
600                 SLOG(LOG_ERROR, stt_tag(), "[Engine ERROR] Fail to start recognition : lang(%s), recognition_type(%s), credential(%s)", lang, recognition_type, credential);
601         }
602
603         return ret;
604 }
605
606 int stt_engine_set_recording_data(const void* data, unsigned int length)
607 {
608         if (NULL == data) {
609                 SLOG(LOG_ERROR, stt_tag(), "[Engine ERROR] Invalid Parameter");
610                 return STTE_ERROR_INVALID_PARAMETER;
611         }
612
613         if (NULL == g_engine) {
614                 SLOG(LOG_ERROR, stt_tag(), "[Engine ERROR] No engine");
615                 return STTE_ERROR_OPERATION_FAILED;
616         }
617
618         if (NULL == g_engine->callback) {
619                 SLOG(LOG_ERROR, stt_tag(), "[Engine ERROR] Invalid engine");
620                 return STTE_ERROR_OPERATION_FAILED;
621         }
622
623         if (NULL == g_engine->callback->set_recording) {
624                 SLOG(LOG_ERROR, stt_tag(), "[Engine ERROR] Invalid engine");
625                 return STTE_ERROR_OPERATION_FAILED;
626         }
627
628         int ret = g_engine->callback->set_recording(data, length);
629         if (0 != ret) {
630                 SLOG(LOG_WARN, stt_tag(), "[Engine WARNING] Fail to set recording : %s", __stt_get_engine_error_code(ret));
631         }
632
633         return ret;
634 }
635
636 int stt_engine_recognize_stop()
637 {
638         if (NULL == g_engine) {
639                 SLOG(LOG_ERROR, stt_tag(), "[Engine ERROR] No engine");
640                 return STTE_ERROR_OPERATION_FAILED;
641         }
642
643         if (NULL == g_engine->callback) {
644                 SLOG(LOG_ERROR, stt_tag(), "[Engine ERROR] Invalid engine");
645                 return STTE_ERROR_OPERATION_FAILED;
646         }
647
648         if (NULL == g_engine->callback->stop) {
649                 SLOG(LOG_ERROR, stt_tag(), "[Engine ERROR] Invalid engine");
650                 return STTE_ERROR_OPERATION_FAILED;
651         }
652
653         SLOG(LOG_INFO, stt_tag(), "[Engine Info] request to stop");
654
655         int ret = g_engine->callback->stop();
656         if (0 != ret) {
657                 SLOG(LOG_ERROR, stt_tag(), "[Engine ERROR] Fail to stop : %s", __stt_get_engine_error_code(ret));
658         }
659
660         return ret;
661 }
662
663 int stt_engine_recognize_cancel()
664 {
665         if (NULL == g_engine) {
666                 SLOG(LOG_ERROR, stt_tag(), "[Engine ERROR] No engine");
667                 return STTE_ERROR_OPERATION_FAILED;
668         }
669
670         if (NULL == g_engine->callback) {
671                 SLOG(LOG_ERROR, stt_tag(), "[Engine ERROR] Invalid engine");
672                 return STTE_ERROR_OPERATION_FAILED;
673         }
674
675         if (NULL == g_engine->callback->cancel) {
676                 SLOG(LOG_ERROR, stt_tag(), "[Engine ERROR] Invalid engine");
677                 return STTE_ERROR_OPERATION_FAILED;
678         }
679
680         SLOG(LOG_INFO, stt_tag(), "[Engine Info] request to cancel");
681
682         int ret = g_engine->callback->cancel();
683         if (0 != ret) {
684                 SLOG(LOG_ERROR, stt_tag(), "[Engine ERROR] Fail to cancel : %s", __stt_get_engine_error_code(ret));
685         }
686
687         return ret;
688 }
689
690 int stt_engine_foreach_result_time(void* time_info, stte_result_time_cb callback, void* user_data)
691 {
692         if (NULL == g_engine) {
693                 SLOG(LOG_ERROR, stt_tag(), "[Engine ERROR] No engine");
694                 return STTE_ERROR_OPERATION_FAILED;
695         }
696
697         if (NULL == g_engine->callback) {
698                 SLOG(LOG_ERROR, stt_tag(), "[Engine ERROR] Invalid engine");
699                 return STTE_ERROR_OPERATION_FAILED;
700         }
701
702         if (NULL == g_engine->callback->foreach_result_time) {
703                 SLOG(LOG_ERROR, stt_tag(), "[Engine ERROR] Invalid engine");
704                 return STTE_ERROR_OPERATION_FAILED;
705         }
706
707         int ret = g_engine->callback->foreach_result_time(time_info, callback, user_data);
708         if (0 != ret) {
709                 SLOG(LOG_ERROR, stt_tag(), "[Engine ERROR] Fail to foreach result time : %s", __stt_get_engine_error_code(ret));
710         }
711
712         return ret;
713 }
714
715 int stt_engine_recognize_start_file(const char* lang, const char* recognition_type, 
716                                      const char* filepath, stte_audio_type_e audio_type, int sample_rate, void* user_param)
717 {
718         if (NULL == filepath || NULL == lang || NULL == recognition_type) {
719                 SLOG(LOG_ERROR, stt_tag(), "[Engine ERROR] Invalid Parameter");
720                 return STTE_ERROR_INVALID_PARAMETER;
721         }
722
723         if (NULL == g_engine) {
724                 SLOG(LOG_ERROR, stt_tag(), "[Engine ERROR] No engine");
725                 return STTE_ERROR_OPERATION_FAILED;
726         }
727
728         if (NULL == g_engine->callback) {
729                 SLOG(LOG_ERROR, stt_tag(), "[Engine ERROR] Invalid engine");
730                 return STTE_ERROR_OPERATION_FAILED;
731         }
732
733 #ifdef __UNUSED_CODES__
734         if (NULL == g_engine->callback->start_file) {
735                 SLOG(LOG_WARN, stt_tag(), "[Engine WARNING] engine API is invalid");
736                 return STTE_ERROR_NOT_SUPPORTED_FEATURE;
737         }
738
739         int ret = g_engine->callback->start_file(lang, recognition_type, filepath, audio_type, sample_rate, user_param);
740         if (0 != ret) {
741                 SLOG(LOG_ERROR, stt_tag(), "[Engine ERROR] Fail to start file recognition : %s", __stt_get_engine_error_code(ret));
742         }
743 #endif
744         return 0;
745 }
746
747 int stt_engine_recognize_cancel_file()
748 {
749         if (NULL == g_engine) {
750                 SLOG(LOG_ERROR, stt_tag(), "[Engine ERROR] No engine");
751                 return STTE_ERROR_OPERATION_FAILED;
752         }
753
754         if (NULL == g_engine->callback) {
755                 SLOG(LOG_ERROR, stt_tag(), "[Engine ERROR] Invalid engine");
756                 return STTE_ERROR_OPERATION_FAILED;
757         }
758
759 #ifdef __UNUSED_CODES__
760         if (NULL == g_engine->callback->cancel_file) {
761                 SLOG(LOG_WARN, stt_tag(), "[Engine WARNING] engine API is invalid");
762                 return STTE_ERROR_NOT_SUPPORTED_FEATURE;
763         }
764
765         int ret = g_engine->callback->cancel_file();
766         if (0 != ret) {
767                 SLOG(LOG_ERROR, stt_tag(), "[Engine ERROR] Fail to start file recognition : %s", __stt_get_engine_error_code(ret));
768         }
769 #endif
770         return 0;
771 }
772
773 int stt_engine_set_recognition_result_cb(stt_engine_result_cb result_cb, void* user_data)
774 {
775         if (NULL == result_cb) {
776                 SLOG(LOG_ERROR, stt_tag(), "[Engine ERROR] Invalid parameter");
777                 return STTE_ERROR_INVALID_PARAMETER;
778         }
779
780         g_result_cb = result_cb;
781
782         return 0;
783 }
784
785 int stt_engine_send_result(stte_result_event_e event, const char* type, const char** result, int result_count,
786                                 const char* msg, void* time_info, void* user_data)
787 {
788         if (NULL == type || NULL == result) {
789                 SLOG(LOG_ERROR, stt_tag(), "[Engine ERROR] Invalid parameter");
790         }
791
792         int ret = STTE_ERROR_NONE;
793         if (false == __stt_get_engine_from()) {
794                 ret = sttd_engine_agent_send_result(event, type, result, result_count, msg, time_info, user_data);
795                 if (0 != ret) {
796                         SLOG(LOG_ERROR, stt_tag(), "[Engine ERROR] Fail to send result");
797                 }
798         } else {
799                 ret = g_result_cb(event, type, result, result_count, msg, time_info, user_data);
800         }
801         return ret;
802 }
803
804 int stt_engine_send_error(stte_error_e error, const char* msg)
805 {
806         int ret = STTE_ERROR_NONE;
807         ret = sttd_engine_agent_send_error(error, msg);
808         if (0 != ret) {
809                 SLOG(LOG_ERROR, stt_tag(), "[Engine ERROR] Fail to send error info");
810         }
811         return ret;
812 }
813
814 int stt_engine_send_speech_status(stte_speech_status_e status, void* user_data)
815 {
816         int ret = STTE_ERROR_NONE;
817         ret = sttd_engine_agent_send_speech_status(status, user_data);
818         if (0 != ret) {
819                 SLOG(LOG_ERROR, stt_tag(), "[Engine ERROR] Fail to send speech status");
820         }
821         return ret;
822 }
823
824 int stt_engine_set_private_data_set_cb(stte_private_data_set_cb private_data_set_cb, void* user_data)
825 {
826         if (NULL == private_data_set_cb) {
827                 SLOG(LOG_ERROR, stt_tag(), "[Engine ERROR] Invalid parameter");
828                 return STTE_ERROR_INVALID_PARAMETER;
829         }
830
831         g_set_private_data_cb = private_data_set_cb;
832
833         return 0;
834 }
835
836 int stt_engine_set_private_data_requested_cb(stte_private_data_requested_cb private_data_requested_cb, void* user_data)
837 {
838         if (NULL == private_data_requested_cb) {
839                 SLOG(LOG_ERROR, stt_tag(), "[Engine ERROR] Invalid parameter");
840                 return STTE_ERROR_INVALID_PARAMETER;
841         }
842
843         g_get_private_data_cb = private_data_requested_cb;
844
845         return 0;
846 }