Merge "Fix memory leak of ecore timer" into tizen_3.0
[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_network.h"
19
20 #include <bundle.h>
21 #include <bundle_internal.h>
22 #include <dlog.h>
23 #include <Ecore.h>
24
25 #include "ttse.h"
26
27 static ttsd_mode_e g_tts_mode = TTSD_MODE_DEFAULT;
28
29 const char* tts_tag()
30 {
31         if (TTSD_MODE_NOTIFICATION == g_tts_mode) {
32                 return "ttsdnoti";
33         } else if (TTSD_MODE_SCREEN_READER == g_tts_mode) {
34                 return "ttsdsr";
35         } else {
36                 return "ttsd";
37         }
38 }
39
40 ttsd_mode_e ttsd_get_mode()
41 {
42         return g_tts_mode;
43 }
44
45 void ttsd_set_mode(ttsd_mode_e mode)
46 {
47         g_tts_mode = mode;
48         return;
49 }
50
51 int ttse_main(int argc, char** argv, ttse_request_callback_s *callback)
52 {
53         bundle *b = NULL;
54         ttsd_mode_e mode = TTSD_MODE_DEFAULT;
55         int ret = TTSE_ERROR_NONE;
56
57         b = bundle_import_from_argv(argc, argv);
58         if (NULL != b) {
59                 char *val = NULL;
60                 if (0 == bundle_get_str(b, "mode", &val)) {
61                         if (NULL != val) {
62                                 if (!strcmp("noti", val)) {
63                                         mode = TTSD_MODE_NOTIFICATION;
64                                 } else if (!strcmp("sr", val)) {
65                                         mode = TTSD_MODE_SCREEN_READER;
66                                 } else {
67                                         SLOG(LOG_WARN, tts_tag(), "[WARNING] mode (%s)", val);
68                                 }
69                         } else {
70                                 SLOG(LOG_ERROR, tts_tag(), "[ERROR] NULL data");
71                         }
72                 } else {
73                         SLOG(LOG_ERROR, tts_tag(), "[ERROR] Fail to get data from bundle");
74                 }
75                 bundle_free(b);
76                 val = NULL;
77         } else {
78                 SLOG(LOG_ERROR, tts_tag(), "[ERROR] Fail to get bundle");
79         }
80
81         ttsd_set_mode(mode);
82
83         SLOG(LOG_DEBUG, tts_tag(), "Start engine as [%d] mode", mode);
84
85         if (!ecore_init()) {
86                 SLOG(LOG_ERROR, tts_tag(), "[ERROR] Fail to initialize Ecore");
87                 return TTSE_ERROR_OPERATION_FAILED;
88         }
89
90         if (0 != ttsd_dbus_open_connection()) {
91                 SLOG(LOG_ERROR, tts_tag(), "[ERROR] Fail to open dbus connection");
92                 ecore_shutdown();
93                 return TTSE_ERROR_OPERATION_FAILED;
94         }
95
96         ret = ttsd_initialize(callback);
97         if (0 != ret) {
98                 SLOG(LOG_ERROR, tts_tag(), "[ERROR] Fail to initialize");
99                 ttsd_dbus_close_connection();
100                 ecore_shutdown();
101                 return ret;
102         }
103
104         if (0 != ttsd_network_initialize()) {
105                 SLOG(LOG_WARN, tts_tag(), "[WARNING] Fail to initialize network");
106         }
107
108         SLOG(LOG_DEBUG, tts_tag(), "====");
109         SLOG(LOG_DEBUG, tts_tag(), "");
110
111         return TTSE_ERROR_NONE;
112 }
113
114 int ttse_get_speed_range(int* min, int* normal, int* max)
115 {
116         if (NULL == min || NULL == normal || NULL == max) {
117                 SLOG(LOG_ERROR, tts_tag(), "[ERROR] Input parameter is null");
118                 return TTSE_ERROR_INVALID_PARAMETER;
119         }
120
121         *min = TTS_SPEED_MIN;
122         *normal = TTS_SPEED_NORMAL;
123         *max = TTS_SPEED_MAX;
124
125         return 0;
126 }
127
128 int ttse_get_pitch_range(int* min, int* normal, int* max)
129 {
130         if (NULL == min || NULL == normal || NULL == max) {
131                 SLOG(LOG_ERROR, tts_tag(), "[ERROR] Input parameter is null");
132                 return TTSE_ERROR_INVALID_PARAMETER;
133         }
134
135         *min = TTS_PITCH_MIN;
136         *normal = TTS_PITCH_NORMAL;
137         *max = TTS_PITCH_MAX;
138
139         return 0;
140 }
141
142 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)
143 {
144         int ret;
145
146         if (NULL == data) {
147                 SLOG(LOG_ERROR, tts_tag(), "[ERROR] Input parameter is null");
148         }
149
150         ret = ttsd_send_result(event, data, data_size, audio_type, rate, user_data);
151
152         if (0 != ret) {
153                 SLOG(LOG_ERROR, tts_tag(), "[ERROR] Fail to send result");
154         }
155
156         return ret;
157 }
158
159 int ttse_send_error(ttse_error_e error, const char* msg)
160 {
161         int ret;
162
163         if (NULL == msg) {
164                 SLOG(LOG_ERROR, tts_tag(), "[ERROR] Input parameter is null");
165                 return TTSE_ERROR_INVALID_PARAMETER;
166         }
167
168         ret = ttsd_send_error(error, msg);
169
170         if (0 != ret) {
171                 SLOG(LOG_ERROR, tts_tag(), "[ERROR] Fail to send error");
172         }
173
174         return ret;
175 }
176
177 int ttse_set_private_data_set_cb(ttse_private_data_set_cb callback_func)
178 {
179         if (NULL == callback_func) {
180                 SLOG(LOG_ERROR, tts_tag(), "[ERROR] Invalid parameter");
181                 return TTSE_ERROR_INVALID_PARAMETER;
182         }
183
184         int ret = ttsd_set_private_data_set_cb(callback_func);
185
186         if (0 != ret) {
187                 SLOG(LOG_ERROR, tts_tag(), "[ERROR] Fail to set private data set cb");
188         }
189
190         return ret;
191 }
192
193 int ttse_set_private_data_requested_cb(ttse_private_data_requested_cb callback_func)
194 {
195         if (NULL == callback_func) {
196                 SLOG(LOG_ERROR, tts_tag(), "[ERROR] Invalid parameter");
197                 return TTSE_ERROR_INVALID_PARAMETER;
198         }
199
200         int ret = ttsd_set_private_data_requested_cb(callback_func);
201
202         if (0 != ret) {
203                 SLOG(LOG_ERROR, tts_tag(), "[ERROR] Fail to set private data requested cb");
204         }
205
206         return ret;
207 }