Merge with master branch
[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_object;
30 static char *g_service_interface;
31
32 int ttsdc_send_hello(int pid, int uid)
33 {
34         if (NULL == g_conn) { 
35                 SLOG(LOG_ERROR, get_tag(), "[Dbus ERROR] Dbus connection is not available" );
36                 return -1;
37         }
38
39         char service_name[64];
40         memset(service_name, 0, 64);
41         snprintf(service_name, 64, "%s%d", TTS_CLIENT_SERVICE_NAME, pid);
42
43         char target_if_name[64];
44         snprintf(target_if_name, sizeof(target_if_name), "%s%d", TTS_CLIENT_SERVICE_INTERFACE, pid);
45
46         DBusMessage* msg;
47
48         /* create a message & check for errors */
49         msg = dbus_message_new_method_call(
50                 service_name, 
51                 TTS_CLIENT_SERVICE_OBJECT_PATH, 
52                 target_if_name, 
53                 TTSD_METHOD_HELLO);
54
55         if (NULL == msg) { 
56                 SLOG(LOG_ERROR, get_tag(), "<<<< [Dbus ERROR] Fail to create hello message : uid(%d)", uid); 
57                 return -1;
58         } else {
59                 SLOG(LOG_DEBUG, get_tag(), "<<<< [Dbus] Send hello message : uid(%d)", uid);
60         }
61
62         dbus_message_append_args(msg, DBUS_TYPE_INT32, &uid, DBUS_TYPE_INVALID);
63
64         DBusError err;
65         dbus_error_init(&err);
66
67         DBusMessage* result_msg;
68         int result = -1;
69
70         result_msg = dbus_connection_send_with_reply_and_block(g_conn, msg, g_waiting_time, &err);
71         dbus_message_unref(msg);
72
73         if (NULL != result_msg) {
74                 dbus_message_get_args(result_msg, &err, DBUS_TYPE_INT32, &result, DBUS_TYPE_INVALID);
75
76                 if (dbus_error_is_set(&err)) { 
77                         SLOG(LOG_ERROR, get_tag(), ">>>> [Dbus] Get arguments error (%s)", err.message);
78                         dbus_error_free(&err); 
79                         result = -1;
80                 }
81
82                 dbus_message_unref(result_msg);
83         } else {
84                 SLOG(LOG_DEBUG, get_tag(), ">>>> [Dbus] Result message is NULL. Client is not available");
85                 result = 0;
86         }
87
88         return result;
89 }
90
91 int ttsdc_send_message(int pid, int uid, int data, char *method)
92 {
93 #if 0
94         if (NULL == g_conn) { 
95                 SLOG(LOG_ERROR, get_tag(), "[Dbus ERROR] Dbus connection is not available" );
96                 return -1;
97         }
98
99         char service_name[64];
100         memset(service_name, 0, 64);
101         snprintf(service_name, 64, "%s%d", TTS_CLIENT_SERVICE_NAME, pid);
102
103         char target_if_name[64];
104         snprintf(target_if_name, sizeof(target_if_name), "%s%d", TTS_CLIENT_SERVICE_INTERFACE, pid);
105
106         DBusMessage* msg;
107
108         /* create a message & check for errors */
109         msg = dbus_message_new_method_call(
110                 service_name, 
111                 TTS_CLIENT_SERVICE_OBJECT_PATH, 
112                 target_if_name, 
113                 method);
114
115         if (NULL == msg) { 
116                 SLOG(LOG_ERROR, get_tag(), "[Dbus ERROR] Fail to create message : type(%s), uid(%d)\n", method, uid); 
117                 return -1;
118         } 
119
120         dbus_message_append_args(msg, DBUS_TYPE_INT32, &uid, DBUS_TYPE_INT32, &data, DBUS_TYPE_INVALID);
121
122         /* send the message and flush the connection */
123         if (!dbus_connection_send(g_conn, msg, NULL)) {
124                 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); 
125         } else {
126                 SLOG(LOG_DEBUG, get_tag(), "<<<< send message : type(%s), uid(%d), data(%d)", method, uid, data);
127
128                 dbus_connection_flush(g_conn);
129         }
130
131         dbus_message_unref(msg);
132 #endif
133         char send_filename[64];
134         memset(send_filename, 0, 64);
135         snprintf(send_filename, 64, "/%s_%d", MESSAGE_FILE_PATH, pid);
136
137         FILE* fp;
138         fp = fopen(send_filename, "a+");
139         if (NULL == fp) {
140                 SLOG(LOG_ERROR, get_tag(), "[File message ERROR] Fail to open message file");
141                 return -1;
142         }
143         SLOG(LOG_DEBUG, get_tag(), "[File message] Write send file - %s, uid=%d, send_data=%d", method, uid, data);
144
145         fprintf(fp, "%s %d %d\n", method, uid, data);
146
147         fclose(fp);
148
149
150         return 0;
151 }
152
153 int ttsdc_send_utt_start_message(int pid, int uid, int uttid)
154 {
155         return ttsdc_send_message(pid, uid, uttid, TTSD_METHOD_UTTERANCE_STARTED);
156 }
157
158 int ttsdc_send_utt_finish_message(int pid, int uid, int uttid) 
159 {
160         return ttsdc_send_message(pid, uid, uttid, TTSD_METHOD_UTTERANCE_COMPLETED);
161 }
162
163 int ttsdc_send_set_state_message(int pid, int uid, int state)
164 {
165         return ttsdc_send_message(pid, uid, state, TTSD_METHOD_SET_STATE);
166 }
167
168 int ttsdc_send_error_message(int pid, int uid, int uttid, int reason)
169 {
170         if (NULL == g_conn) { 
171                 SLOG(LOG_ERROR, get_tag(), "[Dbus ERROR] Dbus connection is not available" );
172                 return -1;
173         }
174
175         char service_name[64];
176         memset(service_name, 0, 64);
177         snprintf(service_name, 64, "%s%d", TTS_CLIENT_SERVICE_NAME, pid);
178
179         char target_if_name[128];
180         snprintf(target_if_name, sizeof(target_if_name), "%s%d", TTS_CLIENT_SERVICE_INTERFACE, pid);
181
182         DBusMessage* msg;
183
184         msg = dbus_message_new_method_call(
185                 service_name, 
186                 TTS_CLIENT_SERVICE_OBJECT_PATH, 
187                 target_if_name, 
188                 TTSD_METHOD_ERROR);
189
190         if (NULL == msg) { 
191                 SLOG(LOG_ERROR, get_tag(), "[Dbus ERROR] Fail to create error message : uid(%d)\n", uid); 
192                 return -1;
193         }
194
195         dbus_message_append_args( msg, 
196                 DBUS_TYPE_INT32, &uid, 
197                 DBUS_TYPE_INT32, &uttid, 
198                 DBUS_TYPE_INT32, &reason, 
199                 DBUS_TYPE_INVALID);
200         
201         if (!dbus_connection_send(g_conn, msg, NULL)) {
202                 SLOG(LOG_ERROR, get_tag(), "[Dbus ERROR] <<<< error message : Out Of Memory !\n"); 
203         } else {
204                 SLOG(LOG_DEBUG, get_tag(), "<<<< Send error signal : uid(%d), reason(%d), uttid(%d)", uid, reason, uttid);
205                 dbus_connection_flush(g_conn);
206         }
207
208         dbus_message_unref(msg);
209
210         return 0;
211 }
212
213 #if 0
214 int ttsd_send_start_next_synthesis()
215 {
216         if (NULL == g_conn) { 
217                 SLOG(LOG_ERROR, get_tag(), "[Dbus ERROR] Dbus connection is not available" );
218                 return -1;
219         }
220
221         DBusMessage* msg;
222
223         msg = dbus_message_new_signal(
224                 g_service_object,               /* object name of the signal */
225                 g_service_interface,            /* interface name of the signal */
226                 TTSD_SIGNAL_NEXT_SYNTHESIS);    /* name of the signal */
227
228         if (NULL == msg) { 
229                 SLOG(LOG_ERROR, get_tag(), "[Dbus ERROR] >>>> Fail to make message for 'start next synthesis'"); 
230                 return -1;
231         }
232
233         if (!dbus_connection_send(g_conn, msg, NULL)) {
234                 SLOG(LOG_ERROR, get_tag(), "[Dbus ERROR] >>>> Fail to send message for 'start next synthesis'"); 
235                 return -1;
236         }
237
238         SLOG(LOG_DEBUG, get_tag(), "[Dbus] >>>> Send message for 'start next synthesis'"); 
239
240         dbus_connection_flush(g_conn);
241         dbus_message_unref(msg);
242
243         return 0;
244 }
245 #endif
246
247 static Eina_Bool listener_event_callback(void* data, Ecore_Fd_Handler *fd_handler)
248 {
249         DBusConnection* conn = (DBusConnection*)data;
250
251         if (NULL == conn)       return ECORE_CALLBACK_RENEW;
252
253         dbus_connection_read_write_dispatch(conn, 50);
254
255         DBusMessage* msg = NULL;
256         msg = dbus_connection_pop_message(conn);
257
258         /* loop again if we haven't read a message */
259         if (NULL == msg || NULL == conn) { 
260                 return ECORE_CALLBACK_RENEW;
261         }
262
263         /* client event */
264         if (dbus_message_is_method_call(msg, g_service_interface, TTS_METHOD_HELLO))
265                 ttsd_dbus_server_hello(conn, msg);
266
267         else if( dbus_message_is_method_call(msg, g_service_interface, TTS_METHOD_INITIALIZE) )
268                 ttsd_dbus_server_initialize(conn, msg);
269         
270         else if( dbus_message_is_method_call(msg, g_service_interface, TTS_METHOD_FINALIZE) )
271                 ttsd_dbus_server_finalize(conn, msg);
272         
273         else if( dbus_message_is_method_call(msg, g_service_interface, TTS_METHOD_GET_SUPPORT_VOICES) )
274                 ttsd_dbus_server_get_support_voices(conn, msg);
275
276         else if( dbus_message_is_method_call(msg, g_service_interface, TTS_METHOD_GET_CURRENT_VOICE) )
277                 ttsd_dbus_server_get_current_voice(conn, msg);
278
279         else if( dbus_message_is_method_call(msg, g_service_interface, TTS_METHOD_ADD_QUEUE) )
280                 ttsd_dbus_server_add_text(conn, msg);
281
282         else if (dbus_message_is_method_call(msg, g_service_interface, TTS_METHOD_PLAY)) 
283                 ttsd_dbus_server_play(conn, msg);
284         
285         else if (dbus_message_is_method_call(msg, g_service_interface, TTS_METHOD_STOP)) 
286                 ttsd_dbus_server_stop(conn, msg);
287
288         else if (dbus_message_is_method_call(msg, g_service_interface, TTS_METHOD_PAUSE)) 
289                 ttsd_dbus_server_pause(conn, msg);
290 #if 0
291         /* daemon internal event*/
292         else if (dbus_message_is_signal(msg, g_service_interface, TTSD_SIGNAL_NEXT_SYNTHESIS)) 
293                 ttsd_dbus_server_start_next_synthesis();
294 #endif
295
296         /* daemon internal event*/
297         else if (dbus_message_is_signal(msg, g_service_interface, TTSD_SIGNAL_NEXT_SYNTHESIS)) 
298                 ttsd_dbus_server_start_next_synthesis();
299
300         /* setting event */
301         else if (dbus_message_is_method_call(msg, g_service_interface, TTS_SETTING_METHOD_HELLO))
302                 ttsd_dbus_server_hello(conn, msg);
303
304         else if (dbus_message_is_method_call(msg, g_service_interface, TTS_SETTING_METHOD_INITIALIZE) )
305                 ttsd_dbus_server_setting_initialize(conn, msg);
306
307         else if (dbus_message_is_method_call(msg, g_service_interface, TTS_SETTING_METHOD_FINALIZE) )
308                 ttsd_dbus_server_setting_finalize(conn, msg);
309
310         else if (dbus_message_is_method_call(msg, g_service_interface, TTS_SETTING_METHOD_GET_ENGINE_LIST) )
311                 ttsd_dbus_server_setting_get_engine_list(conn, msg);
312
313         else if (dbus_message_is_method_call(msg, g_service_interface, TTS_SETTING_METHOD_GET_ENGINE) )
314                 ttsd_dbus_server_setting_get_engine(conn, msg);
315
316         else if (dbus_message_is_method_call(msg, g_service_interface, TTS_SETTING_METHOD_SET_ENGINE) )
317                 ttsd_dbus_server_setting_set_engine(conn, msg);
318
319         else if (dbus_message_is_method_call(msg, g_service_interface, TTS_SETTING_METHOD_GET_VOICE_LIST) )
320                 ttsd_dbus_server_setting_get_voice_list(conn, msg);
321
322         else if (dbus_message_is_method_call(msg, g_service_interface, TTS_SETTING_METHOD_GET_DEFAULT_VOICE) )
323                 ttsd_dbus_server_setting_get_default_voice(conn, msg);
324
325         else if (dbus_message_is_method_call(msg, g_service_interface, TTS_SETTING_METHOD_SET_DEFAULT_VOICE) )
326                 ttsd_dbus_server_setting_set_default_voice(conn, msg);
327
328         else if (dbus_message_is_method_call(msg, g_service_interface, TTS_SETTING_METHOD_GET_DEFAULT_SPEED) )
329                 ttsd_dbus_server_setting_get_speed(conn, msg);
330
331         else if (dbus_message_is_method_call(msg, g_service_interface, TTS_SETTING_METHOD_SET_DEFAULT_SPEED) )
332                 ttsd_dbus_server_setting_set_speed(conn, msg);
333
334         else if (dbus_message_is_method_call(msg, g_service_interface, TTS_SETTING_METHOD_GET_ENGINE_SETTING) )
335                 ttsd_dbus_server_setting_get_engine_setting(conn, msg);
336
337         else if (dbus_message_is_method_call(msg, g_service_interface, TTS_SETTING_METHOD_SET_ENGINE_SETTING) )
338                 ttsd_dbus_server_setting_set_engine_setting(conn, msg);
339         
340
341         /* free the message */
342         dbus_message_unref(msg);
343
344         return ECORE_CALLBACK_RENEW;
345 }
346
347 int ttsd_dbus_open_connection()
348 {
349         DBusError err;
350         dbus_error_init(&err);
351
352         int ret;
353
354         /* connect to the bus and check for errors */
355         g_conn = dbus_bus_get(DBUS_BUS_SYSTEM, &err);
356
357         if (dbus_error_is_set(&err)) { 
358                 SLOG(LOG_ERROR, get_tag(), "[Dbus ERROR] fail dbus_bus_get : %s\n", err.message);
359                 dbus_error_free(&err); 
360         }
361
362         if (NULL == g_conn) { 
363                 SLOG(LOG_ERROR, get_tag(), "[Dbus ERROR] fail to get dbus connection \n" );
364                 return -1;
365         }
366
367         if (TTSD_MODE_SCREEN_READER == ttsd_get_mode()) {
368                 g_service_name = (char*)malloc(sizeof(char) * strlen(TTS_SR_SERVER_SERVICE_NAME) + 1);
369                 g_service_object = (char*)malloc(sizeof(char) * strlen(TTS_SR_SERVER_SERVICE_OBJECT_PATH) + 1);
370                 g_service_interface = (char*)malloc(sizeof(char) * strlen(TTS_SR_SERVER_SERVICE_INTERFACE) + 1);
371
372                 strcpy(g_service_name, TTS_SR_SERVER_SERVICE_NAME);
373                 strcpy(g_service_object, TTS_SR_SERVER_SERVICE_OBJECT_PATH);
374                 strcpy(g_service_interface, TTS_SR_SERVER_SERVICE_INTERFACE);
375         } else if (TTSD_MODE_NOTIFICATION == ttsd_get_mode()) {
376                 g_service_name = (char*)malloc(sizeof(char) * strlen(TTS_NOTI_SERVER_SERVICE_NAME) + 1);
377                 g_service_object = (char*)malloc(sizeof(char) * strlen(TTS_NOTI_SERVER_SERVICE_OBJECT_PATH) + 1);
378                 g_service_interface = (char*)malloc(sizeof(char) * strlen(TTS_NOTI_SERVER_SERVICE_INTERFACE) + 1);
379
380                 strcpy(g_service_name, TTS_NOTI_SERVER_SERVICE_NAME);
381                 strcpy(g_service_object, TTS_NOTI_SERVER_SERVICE_OBJECT_PATH);
382                 strcpy(g_service_interface, TTS_NOTI_SERVER_SERVICE_INTERFACE);
383         } else {
384                 g_service_name = (char*)malloc(sizeof(char) * strlen(TTS_SERVER_SERVICE_NAME) + 1);
385                 g_service_object = (char*)malloc(sizeof(char) * strlen(TTS_SERVER_SERVICE_OBJECT_PATH) + 1);
386                 g_service_interface = (char*)malloc(sizeof(char) * strlen(TTS_SERVER_SERVICE_INTERFACE) + 1);
387
388                 strcpy(g_service_name, TTS_SERVER_SERVICE_NAME);
389                 strcpy(g_service_object, TTS_SERVER_SERVICE_OBJECT_PATH);
390                 strcpy(g_service_interface, TTS_SERVER_SERVICE_INTERFACE);
391         }
392
393         /* request our name on the bus and check for errors */
394         ret = dbus_bus_request_name(g_conn, g_service_name, DBUS_NAME_FLAG_REPLACE_EXISTING , &err);
395
396         if (DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER != ret) {
397                 printf("Fail to be primary owner in dbus request. \n");
398                 SLOG(LOG_ERROR, get_tag(), "[Dbus ERROR] fail to be primary owner \n");
399                 return -1;
400         }
401
402         if (dbus_error_is_set(&err)) { 
403                 SLOG(LOG_ERROR, get_tag(), "[Dbus ERROR] dbus_bus_request_name() : %s \n", err.message);
404                 dbus_error_free(&err); 
405
406                 return -1;
407         }
408
409         /* add a rule for getting signal */
410         char rule[128];
411         snprintf(rule, 128, "type='signal',interface='%s'", g_service_interface);
412
413         /* add a rule for which messages we want to see */
414         dbus_bus_add_match(g_conn, rule, &err); /* see signals from the given interface */
415         dbus_connection_flush(g_conn);
416
417         if (dbus_error_is_set(&err)) { 
418                 SLOG(LOG_ERROR, get_tag(), "[Dbus ERROR] dbus_bus_add_match() : %s \n", err.message);
419                 return -1; 
420         }
421
422         int fd = 0;
423         dbus_connection_get_unix_fd(g_conn, &fd);
424
425         Ecore_Fd_Handler* fd_handler;
426         fd_handler = ecore_main_fd_handler_add(fd, ECORE_FD_READ , (Ecore_Fd_Cb)listener_event_callback, g_conn, NULL, NULL);
427
428         if (NULL == fd_handler) {
429                 SLOG(LOG_ERROR, get_tag(), "[Dbus ERROR] ecore_main_fd_handler_add() : fail to get fd handler \n");
430                 return -1;
431         }
432
433         SLOG(LOG_DEBUG, get_tag(), "[Dbus SUCCESS] Open connection. ");
434         return 0;
435 }
436
437 int ttsd_dbus_close_connection()
438 {
439         DBusError err;
440         dbus_error_init(&err);
441
442         dbus_bus_release_name (g_conn, g_service_name, &err);
443
444         if (dbus_error_is_set(&err)) {
445                 SLOG(LOG_ERROR, get_tag(), "[Dbus ERROR] dbus_bus_release_name() : %s\n", err.message); 
446                 dbus_error_free(&err); 
447                 return -1;
448         }
449
450         if (NULL != g_service_name)
451                 free(g_service_name);
452
453         if (NULL != g_service_object)
454                 free(g_service_object);
455
456         if (NULL != g_service_interface)
457                 free(g_service_interface);
458
459         SLOG(LOG_DEBUG, get_tag(), "[Dbus SUCCESS] Close connection. ");
460
461         return 0;
462 }
463
464 int ttsd_file_msg_open_connection(int pid)
465 {
466         /* Make file for Inotify */
467         char send_filename[64];
468         memset(send_filename, 0, 64);
469         snprintf(send_filename, 64, "%s_%d", MESSAGE_FILE_PATH, pid);
470
471         FILE* fp;
472         fp = fopen(send_filename, "a+");
473         if (NULL == fp) {
474                 SLOG(LOG_ERROR, get_tag(), "[File message ERROR] Fail to make message file");
475                 return -1;
476         }
477         SLOG(LOG_DEBUG, get_tag(), "[File message] Make message file");
478         fclose(fp);
479         return 0;
480 }
481
482 int ttsd_file_msg_close_connection(int pid)
483 {
484         /* delete inotify file */
485         char path[64];
486         memset(path, 0, 64);
487         snprintf(path, 64, "%s_%d", MESSAGE_FILE_PATH, pid);
488         if (0 == access(path, R_OK)) {
489                 if (0 != remove(path)) {
490                         SLOG(LOG_WARN, get_tag(), "[File message WARN] Fail to remove message file");
491                 }
492         }
493         SLOG(LOG_DEBUG, get_tag(), "[File message] Delete message file");
494         return 0;
495 }