upload tizen1.0 source
[platform/core/uifw/tts.git] / server / ttsd_dbus.c
1 /*
2 *  Copyright (c) 2011 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 <dbus/dbus.h>
16 #include <Ecore.h>
17
18 #include "ttsd_main.h"
19 #include "ttsd_dbus_server.h"
20 #include "ttsd_dbus.h"
21 #include "ttsd_server.h"
22
23
24 static DBusConnection* g_conn;
25
26 int ttsdc_send_message(int pid, int uid, int uttid, char *method)
27 {   
28         char service_name[64];
29         memset(service_name, 0, 64);
30         snprintf(service_name, 64, "%s%d", TTS_CLIENT_SERVICE_NAME, pid);
31
32         char target_if_name[64];
33         snprintf(target_if_name, sizeof(target_if_name), "%s%d", TTS_CLIENT_SERVICE_INTERFACE, pid);
34
35         DBusMessage* msg;
36
37         /* create a message & check for errors */
38         msg = dbus_message_new_method_call(
39                 service_name, 
40                 TTS_CLIENT_SERVICE_OBJECT_PATH, 
41                 target_if_name, 
42                 method);
43
44         if (NULL == msg) { 
45                 SLOG(LOG_ERROR, TAG_TTSD, "[Dbus ERROR] Fail to create message : type(%s), uid(%d)\n", method, uid); 
46                 return -1;
47         }
48
49         dbus_message_append_args(msg, DBUS_TYPE_INT32, &uid, DBUS_TYPE_INT32, &uttid, DBUS_TYPE_INVALID);
50
51         /* send the message and flush the connection */
52         if (!dbus_connection_send(g_conn, msg, NULL)) {
53                 SLOG(LOG_ERROR, TAG_TTSD, "[Dbus ERROR] <<<< send message : Out Of Memory, type(%s), ifname(%s), uid(%d), uttid(%d)", method, target_if_name, uid, uttid); 
54         } else {
55                 SLOG(LOG_DEBUG, TAG_TTSD, "<<<< send message : type(%s), uid(%d), uttid(%d)", method, uid, uttid);
56
57                 dbus_connection_flush(g_conn);
58         }
59
60         dbus_message_unref(msg);
61
62         return 0;
63 }
64
65 int ttsdc_send_utt_start_message(int pid, int uid, int uttid)
66 {
67         return ttsdc_send_message(pid, uid, uttid, TTS_METHOD_UTTERANCE_STARTED);
68 }
69
70 int ttsdc_send_utt_finish_message(int pid, int uid, int uttid) 
71 {
72         return ttsdc_send_message(pid, uid, uttid, TTS_METHOD_UTTERANCE_COMPLETED);
73 }
74
75 int ttsdc_send_interrupt_message(int pid, int uid, ttsd_interrupted_code_e code)
76 {
77         return ttsdc_send_message(pid, uid, (int)code, TTS_METHOD_INTERRUPT);
78 }
79
80 int ttsdc_send_error_message(int pid, int uid, int uttid, int reason)
81 {
82         char service_name[64];
83         memset(service_name, 0, 64);
84         snprintf(service_name, 64, "%s%d", TTS_CLIENT_SERVICE_NAME, pid);
85
86         char target_if_name[128];
87         snprintf(target_if_name, sizeof(target_if_name), "%s%d", TTS_CLIENT_SERVICE_INTERFACE, pid);
88
89         DBusMessage* msg;
90
91         msg = dbus_message_new_method_call(
92                 service_name, 
93                 TTS_CLIENT_SERVICE_OBJECT_PATH, 
94                 target_if_name, 
95                 TTS_METHOD_ERROR);
96
97         if (NULL == msg) { 
98                 SLOG(LOG_ERROR, TAG_TTSD, "[Dbus ERROR] Fail to create error message : uid(%d)\n", uid); 
99                 return -1;
100         }
101
102         dbus_message_append_args( msg, 
103                 DBUS_TYPE_INT32, &uid, 
104                 DBUS_TYPE_INT32, &uttid, 
105                 DBUS_TYPE_INT32, &reason, 
106                 DBUS_TYPE_INVALID);
107         
108         if (!dbus_connection_send(g_conn, msg, NULL)) {
109                 SLOG(LOG_ERROR, TAG_TTSD, "[Dbus ERROR] <<<< error message : Out Of Memory !\n"); 
110         } else {
111                 SLOG(LOG_DEBUG, TAG_TTSD, "<<<< Send error signal : uid(%d), reason(%d), uttid(%d)", uid, reason, uttid);
112                 dbus_connection_flush(g_conn);
113         }
114
115         dbus_message_unref(msg);
116
117         return 0;
118 }
119
120
121 static Eina_Bool listener_event_callback(void* data, Ecore_Fd_Handler *fd_handler)
122 {
123         DBusConnection* conn = (DBusConnection*)data;
124
125         if (NULL == conn)       return ECORE_CALLBACK_RENEW;
126
127         dbus_connection_read_write_dispatch(conn, 50);
128
129         DBusMessage* msg = NULL;
130         msg = dbus_connection_pop_message(conn);
131
132         /* loop again if we haven't read a message */
133         if (NULL == msg) { 
134                 return ECORE_CALLBACK_RENEW;
135         }
136         
137         /* client event */
138         if (dbus_message_is_method_call(msg, TTS_SERVER_SERVICE_INTERFACE, TTS_METHOD_HELLO))
139                 ttsd_dbus_server_hello(conn, msg);
140
141         else if( dbus_message_is_method_call(msg, TTS_SERVER_SERVICE_INTERFACE, TTS_METHOD_INITIALIZE) )
142                 ttsd_dbus_server_initialize(conn, msg);
143         
144         else if( dbus_message_is_method_call(msg, TTS_SERVER_SERVICE_INTERFACE, TTS_METHOD_FINALIZE) )
145                 ttsd_dbus_server_finalize(conn, msg);
146         
147         else if( dbus_message_is_method_call(msg, TTS_SERVER_SERVICE_INTERFACE, TTS_METHOD_GET_SUPPORT_VOICES) )
148                 ttsd_dbus_server_get_support_voices(conn, msg);
149
150         else if( dbus_message_is_method_call(msg, TTS_SERVER_SERVICE_INTERFACE, TTS_METHOD_GET_CURRENT_VOICE) )
151                 ttsd_dbus_server_get_current_voice(conn, msg);
152
153         else if( dbus_message_is_method_call(msg, TTS_SERVER_SERVICE_INTERFACE, TTS_METHOD_ADD_QUEUE) )
154                 ttsd_dbus_server_add_text(conn, msg);
155
156         else if (dbus_message_is_method_call(msg, TTS_SERVER_SERVICE_INTERFACE, TTS_METHOD_PLAY)) 
157                 ttsd_dbus_server_play(conn, msg);
158         
159         else if (dbus_message_is_method_call(msg, TTS_SERVER_SERVICE_INTERFACE, TTS_METHOD_STOP)) 
160                 ttsd_dbus_server_stop(conn, msg);
161
162         else if (dbus_message_is_method_call(msg, TTS_SERVER_SERVICE_INTERFACE, TTS_METHOD_PAUSE)) 
163                 ttsd_dbus_server_pause(conn, msg);
164
165         /* setting event */
166         else if (dbus_message_is_method_call(msg, TTS_SERVER_SERVICE_INTERFACE, TTS_SETTING_METHOD_HELLO))
167                 ttsd_dbus_server_hello(conn, msg);
168
169         else if (dbus_message_is_method_call(msg, TTS_SERVER_SERVICE_INTERFACE, TTS_SETTING_METHOD_INITIALIZE) )
170                 ttsd_dbus_server_setting_initialize(conn, msg);
171
172         else if (dbus_message_is_method_call(msg, TTS_SERVER_SERVICE_INTERFACE, TTS_SETTING_METHOD_FINALIZE) )
173                 ttsd_dbus_server_setting_finalize(conn, msg);
174
175         else if (dbus_message_is_method_call(msg, TTS_SERVER_SERVICE_INTERFACE, TTS_SETTING_METHOD_GET_ENGINE_LIST) )
176                 ttsd_dbus_server_setting_get_engine_list(conn, msg);
177
178         else if (dbus_message_is_method_call(msg, TTS_SERVER_SERVICE_INTERFACE, TTS_SETTING_METHOD_GET_ENGINE) )
179                 ttsd_dbus_server_setting_get_engine(conn, msg);
180
181         else if (dbus_message_is_method_call(msg, TTS_SERVER_SERVICE_INTERFACE, TTS_SETTING_METHOD_SET_ENGINE) )
182                 ttsd_dbus_server_setting_set_engine(conn, msg);
183
184         else if (dbus_message_is_method_call(msg, TTS_SERVER_SERVICE_INTERFACE, TTS_SETTING_METHOD_GET_VOICE_LIST) )
185                 ttsd_dbus_server_setting_get_voice_list(conn, msg);
186
187         else if (dbus_message_is_method_call(msg, TTS_SERVER_SERVICE_INTERFACE, TTS_SETTING_METHOD_GET_DEFAULT_VOICE) )
188                 ttsd_dbus_server_setting_get_default_voice(conn, msg);
189
190         else if (dbus_message_is_method_call(msg, TTS_SERVER_SERVICE_INTERFACE, TTS_SETTING_METHOD_SET_DEFAULT_VOICE) )
191                 ttsd_dbus_server_setting_set_default_voice(conn, msg);
192
193         else if (dbus_message_is_method_call(msg, TTS_SERVER_SERVICE_INTERFACE, TTS_SETTING_METHOD_GET_DEFAULT_SPEED) )
194                 ttsd_dbus_server_setting_get_speed(conn, msg);
195
196         else if (dbus_message_is_method_call(msg, TTS_SERVER_SERVICE_INTERFACE, TTS_SETTING_METHOD_SET_DEFAULT_SPEED) )
197                 ttsd_dbus_server_setting_set_speed(conn, msg);
198
199         else if (dbus_message_is_method_call(msg, TTS_SERVER_SERVICE_INTERFACE, TTS_SETTING_METHOD_GET_ENGINE_SETTING) )
200                 ttsd_dbus_server_setting_get_engine_setting(conn, msg);
201
202         else if (dbus_message_is_method_call(msg, TTS_SERVER_SERVICE_INTERFACE, TTS_SETTING_METHOD_SET_ENGINE_SETTING) )
203                 ttsd_dbus_server_setting_set_engine_setting(conn, msg);
204
205
206         else if (dbus_message_is_method_call(msg, TTS_SERVER_SERVICE_INTERFACE, TTS_METHOD_NEXT_PLAY)) 
207                 ttsd_dbus_server_start_next_play(msg);
208
209         else if (dbus_message_is_method_call(msg, TTS_SERVER_SERVICE_INTERFACE, TTS_METHOD_NEXT_SYNTHESIS)) 
210                 ttsd_dbus_server_start_next_synthesis(msg);
211         
212
213         /* free the message */
214         dbus_message_unref(msg);
215
216         return ECORE_CALLBACK_RENEW;
217 }
218
219 int ttsd_dbus_open_connection()
220 {
221         DBusError err;
222         dbus_error_init(&err);
223
224         int ret;
225
226         /* connect to the bus and check for errors */
227         g_conn = dbus_bus_get(DBUS_BUS_SYSTEM, &err);
228
229         if (dbus_error_is_set(&err)) { 
230                 SLOG(LOG_ERROR, TAG_TTSD, "[Dbus ERROR] fail dbus_bus_get : %s\n", err.message);
231                 dbus_error_free(&err); 
232         }
233
234         if (NULL == g_conn) { 
235                 SLOG(LOG_ERROR, TAG_TTSD, "[Dbus ERROR] fail to get dbus connection \n" );
236                 return -1;
237         }
238
239         /* request our name on the bus and check for errors */
240         ret = dbus_bus_request_name(g_conn, TTS_SERVER_SERVICE_NAME, DBUS_NAME_FLAG_REPLACE_EXISTING , &err);
241
242         if (DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER != ret) {
243                 printf("Fail to be primary owner in dbus request. \n");
244                 SLOG(LOG_ERROR, TAG_TTSD, "[Dbus ERROR] fail to be primary owner \n");
245                 return -1;
246         }
247
248         if (dbus_error_is_set(&err)) { 
249                 SLOG(LOG_ERROR, TAG_TTSD, "[Dbus ERROR] dbus_bus_request_name() : %s \n", err.message);
250                 dbus_error_free(&err); 
251
252                 return -1;
253         }
254
255         /* add a rule for getting signal */
256         char rule[128];
257         snprintf(rule, 128, "type='signal',interface='%s'", TTS_SERVER_SERVICE_INTERFACE);
258
259         /* add a rule for which messages we want to see */
260         dbus_bus_add_match(g_conn, rule, &err); /* see signals from the given interface */
261         dbus_connection_flush(g_conn);
262
263         if (dbus_error_is_set(&err)) { 
264                 SLOG(LOG_ERROR, TAG_TTSD, "[Dbus ERROR] dbus_bus_add_match() : %s \n", err.message);
265                 return -1; 
266         }
267
268         int fd = 0;
269         dbus_connection_get_unix_fd(g_conn, &fd);
270
271         Ecore_Fd_Handler* fd_handler;
272         fd_handler = ecore_main_fd_handler_add(fd, ECORE_FD_READ , (Ecore_Fd_Cb)listener_event_callback, g_conn, NULL, NULL);
273
274         if (NULL == fd_handler) {
275                 SLOG(LOG_ERROR, TAG_TTSD, "[Dbus ERROR] ecore_main_fd_handler_add() : fail to get fd handler \n");
276                 return -1;
277         }
278
279         SLOG(LOG_DEBUG, TAG_TTSD, "[Dbus SUCCESS] Open connection. ");
280         return 0;
281 }
282
283 int ttsd_dbus_close_connection()
284 {
285         DBusError err;
286         dbus_error_init(&err);
287
288         dbus_bus_release_name (g_conn, TTS_SERVER_SERVICE_NAME, &err);
289
290         if (dbus_error_is_set(&err)) {
291                 SLOG(LOG_ERROR, TAG_TTSD, "[Dbus ERROR] dbus_bus_release_name() : %s\n", err.message); 
292                 dbus_error_free(&err); 
293                 return -1;
294         }
295
296         SLOG(LOG_DEBUG, TAG_TTSD, "[Dbus SUCCESS] Close connection. ");
297
298         return 0;
299 }
300
301 int ttsd_send_start_next_play_message(int uid)
302 {
303         DBusMessage* msg;
304
305         msg = dbus_message_new_method_call(
306                 TTS_SERVER_SERVICE_NAME, 
307                 TTS_SERVER_SERVICE_OBJECT_PATH, 
308                 TTS_SERVER_SERVICE_INTERFACE, 
309                 TTS_METHOD_NEXT_PLAY);
310
311         if (NULL == msg) { 
312                 SLOG(LOG_ERROR, TAG_TTSD, "[Dbus ERROR] >>>> Fail to make message for 'start next play'"); 
313                 return -1;
314         } 
315
316         dbus_message_append_args(msg, DBUS_TYPE_INT32, &uid, DBUS_TYPE_INVALID);        
317
318         if (!dbus_connection_send(g_conn, msg, NULL)) {
319                 SLOG(LOG_ERROR, TAG_TTSD, "[Dbus ERROR] >>>> Fail to send message for 'start next play'\n"); 
320                 return -1;
321         }
322
323         dbus_connection_flush(g_conn);
324         dbus_message_unref(msg);
325
326         return 0;
327 }
328
329 int ttsd_send_start_next_synthesis_message(int uid)
330 {
331         DBusMessage* msg;
332
333         msg = dbus_message_new_method_call(
334                 TTS_SERVER_SERVICE_NAME, 
335                 TTS_SERVER_SERVICE_OBJECT_PATH, 
336                 TTS_SERVER_SERVICE_INTERFACE, 
337                 TTS_METHOD_NEXT_SYNTHESIS);
338
339         if (NULL == msg) { 
340                 SLOG(LOG_ERROR, TAG_TTSD, "[Dbus ERROR] >>>> Fail to make message for 'start next synthesis'\n"); 
341                 return -1;
342         } 
343         
344         dbus_message_append_args(msg, DBUS_TYPE_INT32, &uid, DBUS_TYPE_INVALID);        
345         
346         if (!dbus_connection_send(g_conn, msg, NULL)) {
347                 SLOG(LOG_ERROR, TAG_TTSD, "[Dbus ERROR] >>>> Fail to send message for 'start next synthesis'\n"); 
348                 return -1;
349         }
350
351         dbus_connection_flush(g_conn);
352         dbus_message_unref(msg);
353
354         return 0;
355 }