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