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