Fix to handle the null private data
[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
23 /*
24 * Internal data structure
25 */
26
27 typedef struct {
28         int     engine_id;
29         char*   engine_path;
30         void    *handle;
31
32         sttpe_funcs_s*  pefuncs;
33         sttpd_funcs_s*  pdfuncs;
34
35         int (*sttp_load_engine)(sttpd_funcs_s* pdfuncs, sttpe_funcs_s* pefuncs);
36         int (*sttp_unload_engine)();
37 } sttengine_s;
38
39 extern const char* stt_tag();
40
41 /** stt engine list */
42 static GSList *g_engine_list;
43
44 static const char* __stt_get_engine_error_code(sttp_error_e err)
45 {
46         switch (err) {
47         case STTP_ERROR_NONE:                   return "STTP_ERROR_NONE";
48         case STTP_ERROR_OUT_OF_MEMORY:          return "STTP_ERROR_OUT_OF_MEMORY";
49         case STTP_ERROR_IO_ERROR:               return "STTP_ERROR_IO_ERROR";
50         case STTP_ERROR_INVALID_PARAMETER:      return "STTP_ERROR_INVALID_PARAMETER";
51         case STTP_ERROR_TIMED_OUT:              return "STTP_ERROR_TIMED_OUT";
52         case STTP_ERROR_RECORDER_BUSY:          return "STTP_ERROR_RECORDER_BUSY";
53         case STTP_ERROR_OUT_OF_NETWORK:         return "STTP_ERROR_OUT_OF_NETWORK";
54         case STTP_ERROR_PERMISSION_DENIED:      return "STTP_ERROR_PERMISSION_DENIED";
55         case STTP_ERROR_NOT_SUPPORTED:          return "STTP_ERROR_NOT_SUPPORTED";
56         case STTP_ERROR_INVALID_STATE:          return "STTP_ERROR_INVALID_STATE";
57         case STTP_ERROR_INVALID_LANGUAGE:       return "STTP_ERROR_INVALID_LANGUAGE";
58         case STTP_ERROR_ENGINE_NOT_FOUND:       return "STTP_ERROR_ENGINE_NOT_FOUND";
59         case STTP_ERROR_OPERATION_FAILED:       return "STTP_ERROR_OPERATION_FAILED";
60         case STTP_ERROR_NOT_SUPPORTED_FEATURE:  return "STTP_ERROR_NOT_SUPPORTED_FEATURE";
61         default:
62                 return "Invalid error code";
63         }
64 }
65
66 static sttengine_s* __get_engine(int engine_id)
67 {
68         /* check whether engine id is valid or not.*/
69         GSList *iter = NULL;
70         sttengine_s *engine = NULL;
71
72         if (g_slist_length(g_engine_list) > 0) {
73                 /*Get a first item*/
74                 iter = g_slist_nth(g_engine_list, 0);
75
76                 while (NULL != iter) {
77                         /*Get handle data from list*/
78                         engine = iter->data;
79
80                         if (engine_id == engine->engine_id) {
81                                 return engine;
82                         }
83
84                         /*Get next item*/
85                         iter = g_slist_next(iter);
86                 }
87         }
88
89         return NULL;
90 }
91
92 /* Register engine id */
93 int stt_engine_load(int engine_id, const char* filepath)
94 {
95         if (NULL == filepath || engine_id < 0) {
96                 SLOG(LOG_ERROR, stt_tag(), "[Engine ERROR] Invalid Parameter");
97                 return STTP_ERROR_INVALID_PARAMETER;
98         }
99
100         sttengine_s* engine = NULL;
101         engine = __get_engine(engine_id);
102         if (NULL != engine) {
103                 SECURE_SLOG(LOG_WARN, stt_tag(), "[Engine WARNING] engine id(%d) is already loaded", engine_id);
104                 return 0;
105         }
106
107         SECURE_SLOG(LOG_DEBUG, stt_tag(), "[Engine] Load engine id(%d), path(%s)", engine_id, filepath);
108
109         /* allocation memory */
110         engine = (sttengine_s*)calloc(1, sizeof(sttengine_s));
111         if (NULL == engine) {
112                 SLOG(LOG_ERROR, stt_tag(), "[ERROR] Fail to allocate memory");
113                 return STTP_ERROR_OUT_OF_MEMORY;
114         }
115
116         /* load engine */
117         char *error;
118
119         engine->handle = dlopen(filepath, RTLD_LAZY);
120         if (!engine->handle) {
121                 SECURE_SLOG(LOG_WARN, stt_tag(), "[Engine] Invalid engine (Fail dlopen) : %s", filepath);
122                 free(engine);
123                 return STTP_ERROR_OPERATION_FAILED;
124         }
125
126         engine->pefuncs = (sttpe_funcs_s*)calloc(1, sizeof(sttpe_funcs_s));
127         if (NULL == engine->pefuncs) {
128                 SLOG(LOG_ERROR, stt_tag(), "[ERROR] Fail to allocate memory");
129                 dlclose(engine->handle);
130                 free(engine);
131                 return STTP_ERROR_OUT_OF_MEMORY;
132         }
133         engine->pdfuncs = (sttpd_funcs_s*)calloc(1, sizeof(sttpd_funcs_s));
134         if (NULL == engine->pdfuncs) {
135                 SLOG(LOG_ERROR, stt_tag(), "[ERROR] Fail to allocate memory");
136                 dlclose(engine->handle);
137                 free(engine->pefuncs);
138                 free(engine);
139                 return STTP_ERROR_OUT_OF_MEMORY;
140         }
141
142         engine->sttp_unload_engine = NULL;
143         engine->sttp_load_engine = NULL;
144
145         engine->sttp_unload_engine = (int (*)())dlsym(engine->handle, "sttp_unload_engine");
146         if (NULL != (error = dlerror()) || NULL == engine->sttp_unload_engine) {
147                 SLOG(LOG_ERROR, stt_tag(), "[Engine ERROR] Fail to link daemon to sttp_unload_engine() : %s", error);
148                 dlclose(engine->handle);
149                 free(engine->pefuncs);
150                 free(engine->pdfuncs);
151                 free(engine);
152                 return STTP_ERROR_OPERATION_FAILED;
153         }
154
155         engine->sttp_load_engine = (int (*)(sttpd_funcs_s*, sttpe_funcs_s*))dlsym(engine->handle, "sttp_load_engine");
156         if (NULL != (error = dlerror()) || NULL == engine->sttp_load_engine) {
157                 SLOG(LOG_ERROR, stt_tag(), "[Engine ERROR] Fail to link daemon to sttp_load_engine() : %s", error);
158                 dlclose(engine->handle);
159                 free(engine->pefuncs);
160                 free(engine->pdfuncs);
161                 free(engine);
162                 return STTP_ERROR_OPERATION_FAILED;
163         }
164
165         engine->engine_id = engine_id;
166         engine->engine_path = strdup(filepath);
167
168         engine->pdfuncs->version = 1;
169         engine->pdfuncs->size = sizeof(sttpd_funcs_s);
170
171         int ret = engine->sttp_load_engine(engine->pdfuncs, engine->pefuncs);
172         if (0 != ret) {
173                 SLOG(LOG_ERROR, stt_tag(), "[Engine ERROR] Fail sttp_load_engine() : %s", __stt_get_engine_error_code(ret));
174                 dlclose(engine->handle);
175                 free(engine->pefuncs);
176                 free(engine->pdfuncs);
177                 free(engine->engine_path);
178                 free(engine);
179                 return ret;
180         }
181
182         /* engine error check */
183         if (engine->pefuncs->size != sizeof(sttpe_funcs_s)) {
184                 SLOG(LOG_WARN, stt_tag(), "[Engine WARNING] engine is not valid : function size is not matched");
185         }
186
187         if (NULL == engine->pefuncs->initialize ||
188                 NULL == engine->pefuncs->deinitialize ||
189                 NULL == engine->pefuncs->foreach_langs ||
190                 NULL == engine->pefuncs->is_valid_lang ||
191                 NULL == engine->pefuncs->support_silence ||
192                 NULL == engine->pefuncs->support_recognition_type ||
193                 NULL == engine->pefuncs->get_audio_format ||
194                 NULL == engine->pefuncs->set_silence_detection ||
195                 NULL == engine->pefuncs->start ||
196                 NULL == engine->pefuncs->set_recording ||
197                 NULL == engine->pefuncs->stop ||
198                 NULL == engine->pefuncs->cancel ||
199                 NULL == engine->pefuncs->foreach_result_time)
200                 /* Current unused functions
201                 NULL == engine->pefuncs->start_file_recognition ||
202                 */
203         {
204                 SLOG(LOG_ERROR, stt_tag(), "[Engine ERROR] The engine functions are NOT valid");
205                 dlclose(engine->handle);
206                 free(engine->pefuncs);
207                 free(engine->pdfuncs);
208                 free(engine->engine_path);
209                 free(engine);
210
211                 return STTP_ERROR_OPERATION_FAILED;
212         }
213
214         SLOG(LOG_DEBUG, stt_tag(), "[Engine Success] Load engine : version(%d), size(%d)", engine->pefuncs->version, engine->pefuncs->size);
215
216         g_engine_list = g_slist_append(g_engine_list, engine);
217
218         return 0;
219 }
220
221 /* Unregister engine id */
222 int stt_engine_unload(int engine_id)
223 {
224         sttengine_s* engine = NULL;
225         engine = __get_engine(engine_id);
226         if (NULL == engine) {
227                 SECURE_SLOG(LOG_WARN, stt_tag(), "[Engine WARNING] engine id(%d) is invalid", engine_id);
228                 return STTP_ERROR_INVALID_PARAMETER;
229         }
230
231         /* unload engine */
232         engine->sttp_unload_engine();
233         dlclose(engine->handle);
234
235         if (NULL != engine->engine_path)        free(engine->engine_path);
236         if (NULL != engine->pefuncs)            free(engine->pefuncs);
237         if (NULL != engine->pdfuncs)            free(engine->pdfuncs);
238
239         g_engine_list = g_slist_remove(g_engine_list, engine);
240
241         free(engine);
242
243         return 0;
244 }
245
246
247 /* Initialize / Deinitialize */
248 int stt_engine_initialize(int engine_id, sttpe_result_cb result_cb, sttpe_silence_detected_cb silence_cb)
249 {
250         if (NULL == result_cb || NULL == silence_cb) {
251                 SLOG(LOG_ERROR, stt_tag(), "[Engine ERROR] Invalid Parameter");
252                 return STTP_ERROR_INVALID_PARAMETER;
253         }
254
255         sttengine_s* engine = NULL;
256         engine = __get_engine(engine_id);
257         if (NULL == engine) {
258                 SECURE_SLOG(LOG_WARN, stt_tag(), "[Engine WARNING] engine id(%d) is invalid", engine_id);
259                 return STTP_ERROR_INVALID_PARAMETER;
260         }
261
262         int ret;
263         ret = engine->pefuncs->initialize(result_cb, silence_cb);
264         if (0 != ret) {
265                 SLOG(LOG_ERROR, stt_tag(), "[Engine ERROR] Fail to initialize : %s", __stt_get_engine_error_code(ret));
266         }
267
268         return ret;
269 }
270
271 int stt_engine_deinitialize(int engine_id)
272 {
273         sttengine_s* engine = NULL;
274         engine = __get_engine(engine_id);
275         if (NULL == engine) {
276                 SECURE_SLOG(LOG_WARN, stt_tag(), "[Engine WARNING] engine id(%d) is invalid", engine_id);
277                 return STTP_ERROR_INVALID_PARAMETER;
278         }
279
280         int ret;
281         ret = engine->pefuncs->deinitialize();
282         if (0 != ret) {
283                 SLOG(LOG_WARN, stt_tag(), "[Engine WARNING] Fail to deinitialize : %s", __stt_get_engine_error_code(ret));
284         }
285
286         return ret;
287 }
288
289 static bool __supported_language_cb(const char* language, void* user_data)
290 {
291         GSList** lang_list = (GSList**)user_data;
292
293         if (NULL == language || NULL == lang_list) {
294                 SLOG(LOG_ERROR, stt_tag(), "[Engine ERROR] Input parameter is NULL in callback!!!!");
295                 return false;
296         }
297
298         char* temp_lang = g_strdup(language);
299
300         *lang_list = g_slist_append(*lang_list, temp_lang);
301
302         return true;
303 }
304
305 /* Get option */
306 int stt_engine_get_supported_langs(int engine_id, GSList** lang_list)
307 {
308         if (NULL == lang_list) {
309                 SLOG(LOG_ERROR, stt_tag(), "[Engine ERROR] Invalid Parameter");
310                 return STTP_ERROR_INVALID_PARAMETER;
311         }
312
313         sttengine_s* engine = NULL;
314         engine = __get_engine(engine_id);
315         if (NULL == engine) {
316                 SECURE_SLOG(LOG_WARN, stt_tag(), "[Engine WARNING] engine id(%d) is invalid", engine_id);
317                 return STTP_ERROR_INVALID_PARAMETER;
318         }
319
320         int ret;
321         ret = engine->pefuncs->foreach_langs(__supported_language_cb, (void*)lang_list);
322         if (0 != ret) {
323                 SLOG(LOG_ERROR, stt_tag(), "[Engine ERROR] get language list error : %s", __stt_get_engine_error_code(ret));
324         }
325
326         return ret;
327 }
328
329 int stt_engine_is_valid_language(int engine_id, const char* language, bool *is_valid)
330 {
331         if (NULL == language || NULL == is_valid) {
332                 SLOG(LOG_ERROR, stt_tag(), "[Engine ERROR] Invalid Parameter");
333                 return STTP_ERROR_INVALID_PARAMETER;
334         }
335
336         sttengine_s* engine = NULL;
337         engine = __get_engine(engine_id);
338         if (NULL == engine) {
339                 SECURE_SLOG(LOG_WARN, stt_tag(), "[Engine WARNING] engine id(%d) is invalid", engine_id);
340                 return STTP_ERROR_INVALID_PARAMETER;
341         }
342
343         bool result;
344         result = engine->pefuncs->is_valid_lang(language);
345
346         *is_valid = result;
347
348         return 0;
349 }
350
351 int stt_engine_set_private_data(int engine_id, const char* key, const char* data)
352 {
353         if (NULL == key || NULL == data) {
354                 SLOG(LOG_ERROR, stt_tag(), "[Engine ERROR] Invalid Parameter");
355                 return STTP_ERROR_INVALID_PARAMETER;
356         }
357
358         sttengine_s* engine = NULL;
359         engine = __get_engine(engine_id);
360         if (NULL == engine) {
361                 SECURE_SLOG(LOG_WARN, stt_tag(), "[Engine WARNING] engine id(%d) is invalid", engine_id);
362                 return STTP_ERROR_INVALID_PARAMETER;
363         }
364
365         int ret = engine->pefuncs->set_private_data(key, data);
366         if (0 != ret) {
367                 SLOG(LOG_ERROR, stt_tag(), "[Engine ERROR] Fail to set private data(%d)", ret);
368         }
369         return ret;
370 }
371
372 int stt_engine_get_private_data(int engine_id, const char* key, char** data)
373 {
374         if (NULL == key || NULL == data) {
375                 SLOG(LOG_ERROR, stt_tag(), "[Engine ERROR] Invalid Parameter");
376                 return STTP_ERROR_INVALID_PARAMETER;
377         }
378
379         sttengine_s* engine = NULL;
380         engine = __get_engine(engine_id);
381         if (NULL == engine) {
382                 SECURE_SLOG(LOG_WARN, stt_tag(), "[Engine WARNING] engine id(%d) is invalid", engine_id);
383                 return STTP_ERROR_INVALID_PARAMETER;
384         }
385
386         char* temp = NULL;
387         int ret = engine->pefuncs->get_private_data(key, &temp);
388         if (0 != ret) {
389                 SLOG(LOG_ERROR, stt_tag(), "[Engine ERROR] Fail to get private data(%d)", ret);
390                 return ret;
391         }
392
393         if (NULL == temp)
394                 *data = strdup("NULL");
395         else
396                 *data = strdup(temp);
397
398         return STTP_ERROR_NONE;
399 }
400
401 int stt_engine_get_first_language(int engine_id, char** language)
402 {
403         if (NULL == language) {
404                 SLOG(LOG_ERROR, stt_tag(), "[Engine ERROR] Invalid Parameter");
405                 return STTP_ERROR_INVALID_PARAMETER;
406         }
407
408         sttengine_s* engine = NULL;
409         engine = __get_engine(engine_id);
410         if (NULL == engine) {
411                 SECURE_SLOG(LOG_WARN, stt_tag(), "[Engine WARNING] engine id(%d) is invalid", engine_id);
412                 return STTP_ERROR_INVALID_PARAMETER;
413         }
414
415         GSList* lang_list = NULL;
416         int ret;
417         ret = engine->pefuncs->foreach_langs(__supported_language_cb, &lang_list);
418         if (0 != ret) {
419                 SLOG(LOG_ERROR, stt_tag(), "[Engine ERROR] get language list error : %s", __stt_get_engine_error_code(ret));
420                 return ret;
421         }
422
423         GSList *iter = NULL;
424         char* data = NULL;
425
426         iter = g_slist_nth(lang_list, 0);
427         if (NULL != iter) {
428                 data = iter->data;
429
430                 if (true == engine->pefuncs->is_valid_lang(data)) {
431                         *language = strdup(data);
432                 } else {
433                         ret = STTP_ERROR_OPERATION_FAILED;
434                 }
435         }
436
437         /* if list have item */
438         if (g_slist_length(lang_list) > 0) {
439                 /* Get a first item */
440                 iter = g_slist_nth(lang_list, 0);
441
442                 while (NULL != iter) {
443                         data = iter->data;
444
445                         if (NULL != data)
446                                 free(data);
447
448                         lang_list = g_slist_remove_link(lang_list, iter);
449
450                         iter = g_slist_nth(lang_list, 0);
451                 }
452         }
453
454         return ret;
455 }
456
457 int stt_engine_support_silence(int engine_id, bool* support)
458 {
459         if (NULL == support) {
460                 SLOG(LOG_ERROR, stt_tag(), "[Engine ERROR] Invalid Parameter");
461                 return STTP_ERROR_INVALID_PARAMETER;
462         }
463
464         sttengine_s* engine = NULL;
465         engine = __get_engine(engine_id);
466         if (NULL == engine) {
467                 SECURE_SLOG(LOG_WARN, stt_tag(), "[Engine WARNING] engine id(%d) is invalid", engine_id);
468                 return STTP_ERROR_INVALID_PARAMETER;
469         }
470
471         bool result;
472         result = engine->pefuncs->support_silence();
473         *support = result;
474
475         return 0;
476 }
477
478
479 int stt_engine_need_app_credential(int engine_id, bool* need)
480 {
481         if (NULL == need) {
482                 SLOG(LOG_ERROR, stt_tag(), "[Engine ERROR] Invalid Parameter");
483                 return STTP_ERROR_INVALID_PARAMETER;
484         }
485
486         sttengine_s* engine = NULL;
487         engine = __get_engine(engine_id);
488         if (NULL == engine) {
489                 SECURE_SLOG(LOG_WARN, stt_tag(), "[Engine WARNING] engine id(%d) is invalid", engine_id);
490                 return STTP_ERROR_INVALID_PARAMETER;
491         }
492
493         bool result;
494         if (NULL != engine->pefuncs->need_app_credential) {
495                 result = engine->pefuncs->need_app_credential();
496                 *need = result;
497                 return STTP_ERROR_NONE;
498         }
499
500         return STTP_ERROR_OPERATION_FAILED;
501 }
502
503 int stt_engine_support_recognition_type(int engine_id, const char* type, bool* support)
504 {
505         if (NULL == type || NULL == support) {
506                 SLOG(LOG_ERROR, stt_tag(), "[Engine ERROR] Invalid Parameter");
507                 return STTP_ERROR_INVALID_PARAMETER;
508         }
509
510         sttengine_s* engine = NULL;
511         engine = __get_engine(engine_id);
512         if (NULL == engine) {
513                 SECURE_SLOG(LOG_WARN, stt_tag(), "[Engine WARNING] engine id(%d) is invalid", engine_id);
514                 return STTP_ERROR_INVALID_PARAMETER;
515         }
516
517         bool result;
518         if (NULL != engine->pefuncs->support_recognition_type) {
519                 result = engine->pefuncs->support_recognition_type(type);
520                 *support = result;
521                 return 0;
522         }
523
524         return STTP_ERROR_OPERATION_FAILED;
525 }
526
527 int stt_engine_get_audio_type(int engine_id, sttp_audio_type_e* types, int* rate, int* channels)
528 {
529         if (NULL == types || NULL == rate || NULL == channels) {
530                 SLOG(LOG_ERROR, stt_tag(), "[Engine ERROR] Invalid Parameter");
531                 return STTP_ERROR_INVALID_PARAMETER;
532         }
533
534         sttengine_s* engine = NULL;
535         engine = __get_engine(engine_id);
536         if (NULL == engine) {
537                 SECURE_SLOG(LOG_WARN, stt_tag(), "[Engine WARNING] engine id(%d) is invalid", engine_id);
538                 return STTP_ERROR_INVALID_PARAMETER;
539         }
540
541         int ret;
542         ret = engine->pefuncs->get_audio_format(types, rate, channels);
543         if (0 != ret) {
544                 SLOG(LOG_ERROR, stt_tag(), "[Engine ERROR] Fail to get audio format : %s", __stt_get_engine_error_code(ret));
545         }
546
547         return ret;
548 }
549
550 /* Set option */
551 int stt_engine_set_silence_detection(int engine_id, bool value)
552 {
553         sttengine_s* engine = NULL;
554         engine = __get_engine(engine_id);
555         if (NULL == engine) {
556                 SECURE_SLOG(LOG_WARN, stt_tag(), "[Engine WARNING] engine id(%d) is invalid", engine_id);
557                 return STTP_ERROR_INVALID_PARAMETER;
558         }
559
560         int ret = engine->pefuncs->set_silence_detection(value);
561         if (STTP_ERROR_NOT_SUPPORTED_FEATURE == ret) {
562                 SLOG(LOG_WARN, stt_tag(), "[Engine WARNING] Not support silence detection");
563                 return STTP_ERROR_NOT_SUPPORTED_FEATURE;
564         } else if (0 != ret) {
565                 SLOG(LOG_ERROR, stt_tag(), "[Engine ERROR] Fail to set silence detection : %d", ret);
566         }
567
568         return ret;
569 }
570
571 int stt_engine_check_app_agreed(int engine_id, const char* appid, bool* value)
572 {
573         sttengine_s* engine = NULL;
574         engine = __get_engine(engine_id);
575         if (NULL == engine) {
576                 SECURE_SLOG(LOG_WARN, stt_tag(), "[Engine WARNING] engine id(%d) is invalid", engine_id);
577                 return STTP_ERROR_INVALID_PARAMETER;
578         }
579
580         if (NULL == engine->pefuncs->check_app_agreed) {
581                 SLOG(LOG_WARN, stt_tag(), "[Engine WARNING] Not support app agreement. All app is available");
582                 *value = true;
583                 return 0;
584         }
585
586         int ret = engine->pefuncs->check_app_agreed(appid, value);
587         if (0 != ret) {
588                 SLOG(LOG_ERROR, stt_tag(), "[Engine ERROR] Fail to get app agreement : %s", __stt_get_engine_error_code(ret));
589                 *value = false;
590         }
591
592         return ret;
593 }
594
595 /* Recognition */
596 int stt_engine_recognize_start(int engine_id, const char* lang, const char* recognition_type, const char* credential, void* user_param)
597 {
598         if (NULL == lang || NULL == recognition_type) {
599                 SLOG(LOG_ERROR, stt_tag(), "[Engine ERROR] Invalid Parameter");
600                 return STTP_ERROR_INVALID_PARAMETER;
601         }
602
603         sttengine_s* engine = NULL;
604         engine = __get_engine(engine_id);
605         if (NULL == engine) {
606                 SECURE_SLOG(LOG_WARN, stt_tag(), "[Engine WARNING] engine id(%d) is invalid", engine_id);
607                 return STTP_ERROR_INVALID_PARAMETER;
608         }
609
610         int ret = engine->pefuncs->start(lang, recognition_type, credential, user_param);
611         if (0 != ret) {
612                 SLOG(LOG_ERROR, stt_tag(), "[Engine ERROR] Fail to start recognition : %s", __stt_get_engine_error_code(ret));
613                 SLOG(LOG_ERROR, stt_tag(), "[Engine ERROR] Fail to start recognition : lang(%s), recognition_type(%s), credential(%s)", lang, recognition_type, credential);
614         }
615
616         return ret;
617 }
618
619 int stt_engine_set_recording_data(int engine_id, const void* data, unsigned int length)
620 {
621         if (NULL == data) {
622                 SLOG(LOG_ERROR, stt_tag(), "[Engine ERROR] Invalid Parameter");
623                 return STTP_ERROR_INVALID_PARAMETER;
624         }
625
626         sttengine_s* engine = NULL;
627         engine = __get_engine(engine_id);
628         if (NULL == engine) {
629                 SECURE_SLOG(LOG_WARN, stt_tag(), "[Engine WARNING] engine id(%d) is invalid", engine_id);
630                 return STTP_ERROR_INVALID_PARAMETER;
631         }
632
633         int ret = engine->pefuncs->set_recording(data, length);
634         if (0 != ret) {
635                 SLOG(LOG_WARN, stt_tag(), "[Engine WARNING] Fail to set recording : %s", __stt_get_engine_error_code(ret));
636         }
637
638         return ret;
639 }
640
641 int stt_engine_recognize_stop(int engine_id)
642 {
643         sttengine_s* engine = NULL;
644         engine = __get_engine(engine_id);
645         if (NULL == engine) {
646                 SECURE_SLOG(LOG_WARN, stt_tag(), "[Engine WARNING] engine id(%d) is invalid", engine_id);
647                 return STTP_ERROR_INVALID_PARAMETER;
648         }
649
650         int ret = engine->pefuncs->stop();
651         if (0 != ret) {
652                 SLOG(LOG_ERROR, stt_tag(), "[Engine ERROR] Fail to stop : %s", __stt_get_engine_error_code(ret));
653         }
654
655         return ret;
656 }
657
658 int stt_engine_recognize_cancel(int engine_id)
659 {
660         sttengine_s* engine = NULL;
661         engine = __get_engine(engine_id);
662         if (NULL == engine) {
663                 SECURE_SLOG(LOG_WARN, stt_tag(), "[Engine WARNING] engine id(%d) is invalid", engine_id);
664                 return STTP_ERROR_INVALID_PARAMETER;
665         }
666
667         int ret = engine->pefuncs->cancel();
668         if (0 != ret) {
669                 SLOG(LOG_ERROR, stt_tag(), "[Engine ERROR] Fail to cancel : %s", __stt_get_engine_error_code(ret));
670         }
671
672         return ret;
673 }
674
675 int stt_engine_foreach_result_time(int engine_id, void* time_info, sttpe_result_time_cb callback, void* user_data)
676 {
677         sttengine_s* engine = NULL;
678         engine = __get_engine(engine_id);
679         if (NULL == engine) {
680                 SECURE_SLOG(LOG_WARN, stt_tag(), "[Engine WARNING] engine id(%d) is invalid", engine_id);
681                 return STTP_ERROR_INVALID_PARAMETER;
682         }
683
684         int ret = engine->pefuncs->foreach_result_time(time_info, callback, user_data);
685         if (0 != ret) {
686                 SLOG(LOG_ERROR, stt_tag(), "[Engine ERROR] Fail to foreach result time : %s", __stt_get_engine_error_code(ret));
687         }
688
689         return ret;
690 }
691
692 int stt_engine_recognize_start_file(int engine_id, const char* lang, const char* recognition_type,
693                                      const char* filepath, sttp_audio_type_e audio_type, int sample_rate, void* user_param)
694 {
695         if (NULL == filepath || NULL == lang || NULL == recognition_type) {
696                 SLOG(LOG_ERROR, stt_tag(), "[Engine ERROR] Invalid Parameter");
697                 return STTP_ERROR_INVALID_PARAMETER;
698         }
699
700         sttengine_s* engine = NULL;
701         engine = __get_engine(engine_id);
702         if (NULL == engine) {
703                 SECURE_SLOG(LOG_WARN, stt_tag(), "[Engine WARNING] engine id(%d) is invalid", engine_id);
704                 return STTP_ERROR_INVALID_PARAMETER;
705         }
706
707         if (NULL == engine->pefuncs->start_file) {
708                 SLOG(LOG_WARN, stt_tag(), "[Engine WARNING] engine API is invalid");
709                 return STTP_ERROR_NOT_SUPPORTED_FEATURE;
710         }
711
712         int ret = engine->pefuncs->start_file(lang, recognition_type, filepath, audio_type, sample_rate, user_param);
713         if (0 != ret) {
714                 SLOG(LOG_ERROR, stt_tag(), "[Engine ERROR] Fail to start file recognition : %s", __stt_get_engine_error_code(ret));
715         }
716
717         return ret;
718 }
719
720 int stt_engine_recognize_cancel_file(int engine_id)
721 {
722         sttengine_s* engine = NULL;
723         engine = __get_engine(engine_id);
724         if (NULL == engine) {
725                 SECURE_SLOG(LOG_WARN, stt_tag(), "[Engine WARNING] engine id(%d) is invalid", engine_id);
726                 return STTP_ERROR_INVALID_PARAMETER;
727         }
728
729         if (NULL == engine->pefuncs->cancel_file) {
730                 SLOG(LOG_WARN, stt_tag(), "[Engine WARNING] engine API is invalid");
731                 return STTP_ERROR_NOT_SUPPORTED_FEATURE;
732         }
733
734         int ret = engine->pefuncs->cancel_file();
735         if (0 != ret) {
736                 SLOG(LOG_ERROR, stt_tag(), "[Engine ERROR] Fail to start file recognition : %s", __stt_get_engine_error_code(ret));
737         }
738
739         return ret;
740 }