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