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