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