Make atomic variable to avoid race condition
[platform/core/uifw/stt.git] / server / stte.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 <aul.h>
15 #include <Ecore.h>
16 #include <vconf.h>
17
18 #include "stt_engine.h"
19 #include "stt_defs.h"
20 #include "stt_network.h"
21 #include "sttd_dbus.h"
22 #include "sttd_server.h"
23 #include "sttd_engine_agent.h"
24
25 #include "stte.h"
26
27 static bool __is_default_engine()
28 {
29         char* engine = NULL;
30         engine = vconf_get_str(VCONFKEY_STT_ENGINE_DEFAULT);
31         if (NULL == engine) {
32                 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to get sting for stt engine");
33                 return FALSE;
34         }
35
36         char appid[1024] = {'\0', };
37         if (0 != aul_app_get_appid_bypid(getpid(), appid, sizeof(appid) - 1)) {
38                 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to get callee appid by pid");
39         }
40
41         SLOG(LOG_DEBUG, TAG_STTD, "[Server] STT Default Engine(%s), appId(%s)", engine, appid);
42         if (0 == strncmp(engine, appid, strlen(engine))) {
43                 free(engine);
44                 return TRUE;
45         }
46         free(engine);
47         return FALSE;
48 }
49
50 int stte_main(int argc, char**argv, stte_request_callback_s *callback)
51 {
52         SLOG(LOG_DEBUG, TAG_STTD, "===== Start engine");
53
54         int ret = STTE_ERROR_NONE;
55
56         if (!ecore_init()) {
57                 SLOG(LOG_ERROR, TAG_STTD, "[ERROR] Fail to initialize Ecore");
58                 return STTE_ERROR_OPERATION_FAILED;
59         }
60
61         if (TRUE == __is_default_engine()) {
62                 if (0 != sttd_dbus_open_connection()) {
63                         SLOG(LOG_ERROR, TAG_STTD, "[ERROR] Fail to open connection");
64                         ecore_shutdown();
65                         return STTE_ERROR_OPERATION_FAILED;
66                 }
67         }
68
69         ret = sttd_initialize(callback);
70         if (0 != ret) {
71                 SLOG(LOG_ERROR, TAG_STTD, "[ERROR] Fail to initialize stt-service");
72                 sttd_dbus_close_connection();
73                 ecore_shutdown();
74                 return ret;
75         }
76
77         stt_network_initialize();
78
79         SLOG(LOG_DEBUG, TAG_STTD, "[Main] stt-service start...");
80
81         SLOG(LOG_DEBUG, TAG_STTD, "=====");
82         SLOG(LOG_DEBUG, TAG_STTD, "  ");
83         SLOG(LOG_DEBUG, TAG_STTD, "  ");
84
85         return STTE_ERROR_NONE;
86 }
87
88 int stte_send_result(stte_result_event_e event, const char* type, const char** result, int result_count,
89                                 const char* msg, void* time_info, void* user_data)
90 {
91         if (NULL == type || NULL == result) {
92                 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Invalid parameter");
93         }
94
95         int ret = STTE_ERROR_NONE;
96         stt_engine_result_cb result_cb = NULL;
97         ret = stt_engine_get_recognition_result_cb(&result_cb);
98         if (STTE_ERROR_NONE == ret && NULL != result_cb) {
99                 ret = result_cb(event, type, result, result_count, msg, time_info, user_data);
100         } else {
101                 ret = sttd_engine_agent_send_result(event, type, result, result_count, msg, time_info, user_data);
102         }
103
104         if (STTE_ERROR_NONE != ret) {
105                 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to send result");
106         }
107         return ret;
108 }
109
110 int stte_send_error(stte_error_e error, const char* msg)
111 {
112         int ret = STTE_ERROR_NONE;
113         ret = sttd_engine_agent_send_error(error, msg);
114         if (0 != ret) {
115                 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to send error info");
116         }
117         return ret;
118 }
119
120 int stte_send_speech_status(stte_speech_status_e status, void* user_data)
121 {
122         int ret = STTE_ERROR_NONE;
123         ret = sttd_engine_agent_send_speech_status(status, user_data);
124         if (0 != ret) {
125                 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to send speech status");
126         }
127         return ret;
128 }
129
130 int stte_set_private_data_set_cb(stte_private_data_set_cb callback)
131 {
132         int ret = STTE_ERROR_NONE;
133         ret = stt_engine_set_private_data_set_cb(callback, NULL);
134         if (0 != ret) {
135                 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to set private data set callback");
136         }
137         return ret;
138 }
139
140 int stte_set_private_data_requested_cb(stte_private_data_requested_cb callback)
141 {
142         int ret = STTE_ERROR_NONE;
143         ret = stt_engine_set_private_data_requested_cb(callback, NULL);
144         if (0 != ret) {
145                 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to set private data requested callback");
146         }
147         return ret;
148 }
149
150 int stte_set_audio_type_set_cb(stte_audio_type_cb callback, void* user_data)
151 {
152         SLOG(LOG_INFO, TAG_STTD, "[Server Info] Set audio type set callback");
153
154         int ret = STTE_ERROR_NONE;
155         ret = stt_engine_set_audio_type_set_cb(callback, user_data);
156         if (0 != ret) {
157                 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to set audio type set");
158         }
159         return ret;
160 }
161
162 int stte_unset_audio_type_set_cb(void)
163 {
164         SLOG(LOG_INFO, TAG_STTD, "[Server Info] Unset audio type set callback");
165
166         int ret = STTE_ERROR_NONE;
167         ret = stt_engine_unset_audio_type_set_cb();
168         if (0 != ret) {
169                 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to unset audio type set");
170         }
171         return ret;
172 }