Support TIDL / dbus IPC
[platform/core/uifw/tts.git] / server / ttse.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
15 #include "ttsd_main.h"
16 #include "ttsd_server.h"
17 #include "ttsd_dbus.h"
18 #include "ttsd_ipc.h"
19 #include "ttsd_network.h"
20
21 #include <aul.h>
22 #include <bundle.h>
23 #include <bundle_internal.h>
24 #include <dlog.h>
25 #include <Ecore.h>
26 #include <vconf.h>
27
28 #include "ttse.h"
29 #include "ttse_internal.h"
30
31 static ttsd_mode_e g_tts_mode = TTSD_MODE_DEFAULT;
32
33 const char* tts_tag()
34 {
35         if (TTSD_MODE_NOTIFICATION == g_tts_mode) {
36                 return "ttsdnoti";
37         } else if (TTSD_MODE_SCREEN_READER == g_tts_mode) {
38                 return "ttsdsr";
39         } else if (TTSD_MODE_INTERRUPT == g_tts_mode) {
40                 return "ttsdinterrupt";
41         } else {
42                 return "ttsd";
43         }
44 }
45
46 ttsd_mode_e ttsd_get_mode()
47 {
48         return g_tts_mode;
49 }
50
51 void ttsd_set_mode(ttsd_mode_e mode)
52 {
53         g_tts_mode = mode;
54         return;
55 }
56
57 static bool __is_default_engine()
58 {
59         char* engine = NULL;
60         engine = vconf_get_str(VCONFKEY_TTS_ENGINE_DEFAULT);
61         if (NULL == engine) {
62                 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to get sting for vc engine");
63                 return FALSE;
64         }
65
66         char appid[1024] = {'\0', };
67         if (0 != aul_app_get_appid_bypid(getpid(), appid, sizeof(appid) - 1)) {
68                 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to get callee appid by pid");
69         }
70
71         SLOG(LOG_DEBUG, tts_tag(), "[Server] TTS Default Engine(%s), appId(%s)", engine, appid);
72         if (0 == strncmp(engine, appid, strlen(engine))) {
73                 free(engine);
74                 return TRUE;
75         }
76         free(engine);
77         return FALSE;
78 }
79
80 int ttse_main(int argc, char** argv, ttse_request_callback_s *callback)
81 {
82         bundle *b = NULL;
83         ttsd_mode_e mode = TTSD_MODE_DEFAULT;
84         int ret = TTSE_ERROR_NONE;
85
86         b = bundle_import_from_argv(argc, argv);
87         if (NULL != b) {
88                 char *val = NULL;
89                 if (0 == bundle_get_str(b, "mode", &val)) {
90                         if (NULL != val) {
91                                 if (!strcmp("noti", val)) {
92                                         mode = TTSD_MODE_NOTIFICATION;
93                                 } else if (!strcmp("sr", val)) {
94                                         mode = TTSD_MODE_SCREEN_READER;
95                                 } else if (!strcmp("interrupt", val)) {
96                                         mode = TTSD_MODE_INTERRUPT;
97                                 } else {
98                                         SLOG(LOG_WARN, tts_tag(), "[WARNING] mode (%s)", val);
99                                 }
100                         } else {
101                                 SLOG(LOG_ERROR, tts_tag(), "[ERROR] NULL data");
102                         }
103                 } else {
104                         SLOG(LOG_ERROR, tts_tag(), "[ERROR] Fail to get data from bundle");
105                 }
106                 bundle_free(b);
107                 val = NULL;
108         } else {
109                 SLOG(LOG_ERROR, tts_tag(), "[ERROR] Fail to get bundle");
110         }
111
112         ttsd_set_mode(mode);
113
114         SLOG(LOG_DEBUG, tts_tag(), "Start engine as [%d] mode", mode);
115
116         if (!ecore_init()) {
117                 SLOG(LOG_ERROR, tts_tag(), "[ERROR] Fail to initialize Ecore");
118                 return TTSE_ERROR_OPERATION_FAILED;
119         }
120
121         ret = ttsd_initialize(callback);
122         if (0 != ret) {
123                 SLOG(LOG_ERROR, tts_tag(), "[ERROR] Fail to initialize");
124                 ecore_shutdown();
125                 return ret;
126         }
127
128         if (TRUE == __is_default_engine()) {
129                 if (0 != ttsd_ipc_open_connection()) {
130                         SLOG(LOG_ERROR, tts_tag(), "[ERROR] Fail to open ipc connection");
131                         ecore_shutdown();
132                         return TTSE_ERROR_OPERATION_FAILED;
133                 }
134         }
135
136         if (0 != ttsd_network_initialize()) {
137                 SLOG(LOG_WARN, tts_tag(), "[WARNING] Fail to initialize network");
138         }
139
140         SLOG(LOG_DEBUG, tts_tag(), "@@@");
141
142         return TTSE_ERROR_NONE;
143 }
144
145 int ttse_terminate()
146 {
147         ttsd_terminate();
148
149         return TTSE_ERROR_NONE;
150 }
151
152 int ttse_get_speed_range(int* min, int* normal, int* max)
153 {
154         if (NULL == min || NULL == normal || NULL == max) {
155                 SLOG(LOG_ERROR, tts_tag(), "[ERROR] Input parameter is null");
156                 return TTSE_ERROR_INVALID_PARAMETER;
157         }
158
159         *min = TTS_SPEED_MIN;
160         *normal = TTS_SPEED_NORMAL;
161         *max = TTS_SPEED_MAX;
162
163         return 0;
164 }
165
166 int ttse_get_pitch_range(int* min, int* normal, int* max)
167 {
168         if (NULL == min || NULL == normal || NULL == max) {
169                 SLOG(LOG_ERROR, tts_tag(), "[ERROR] Input parameter is null");
170                 return TTSE_ERROR_INVALID_PARAMETER;
171         }
172
173         *min = TTS_PITCH_MIN;
174         *normal = TTS_PITCH_NORMAL;
175         *max = TTS_PITCH_MAX;
176
177         return 0;
178 }
179
180 int ttse_send_result(ttse_result_event_e event, const void* data, unsigned int data_size, ttse_audio_type_e audio_type, int rate, void* user_data)
181 {
182         int ret;
183
184         if (NULL == data) {
185                 SLOG(LOG_ERROR, tts_tag(), "[ERROR] Input parameter is null");
186         }
187
188         ret = ttsd_send_result(event, data, data_size, audio_type, rate, user_data);
189
190         if (0 != ret) {
191                 SLOG(LOG_ERROR, tts_tag(), "[ERROR] Fail to send result");
192         }
193
194         return ret;
195 }
196
197 int ttse_send_error(ttse_error_e error, const char* msg)
198 {
199         int ret;
200
201         ret = ttsd_send_error(error, msg);
202
203         if (0 != ret) {
204                 SLOG(LOG_ERROR, tts_tag(), "[ERROR] Fail to send error");
205         }
206
207         return ret;
208 }
209
210 int ttse_set_private_data_set_cb(ttse_private_data_set_cb callback_func)
211 {
212         if (NULL == callback_func) {
213                 SLOG(LOG_ERROR, tts_tag(), "[ERROR] Invalid parameter");
214                 return TTSE_ERROR_INVALID_PARAMETER;
215         }
216
217         int ret = ttsd_set_private_data_set_cb(callback_func);
218
219         if (0 != ret) {
220                 SLOG(LOG_ERROR, tts_tag(), "[ERROR] Fail to set private data set cb");
221         }
222
223         return ret;
224 }
225
226 int ttse_set_private_data_requested_cb(ttse_private_data_requested_cb callback_func)
227 {
228         if (NULL == callback_func) {
229                 SLOG(LOG_ERROR, tts_tag(), "[ERROR] Invalid parameter");
230                 return TTSE_ERROR_INVALID_PARAMETER;
231         }
232
233         int ret = ttsd_set_private_data_requested_cb(callback_func);
234
235         if (0 != ret) {
236                 SLOG(LOG_ERROR, tts_tag(), "[ERROR] Fail to set private data requested cb");
237         }
238
239         return ret;
240 }