Fix bug on resume and file message(IPC) is applied
[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         /* setting event */
297         else if (dbus_message_is_method_call(msg, g_service_interface, TTS_SETTING_METHOD_HELLO))
298                 ttsd_dbus_server_hello(conn, msg);
299
300         else if (dbus_message_is_method_call(msg, g_service_interface, TTS_SETTING_METHOD_INITIALIZE) )
301                 ttsd_dbus_server_setting_initialize(conn, msg);
302
303         else if (dbus_message_is_method_call(msg, g_service_interface, TTS_SETTING_METHOD_FINALIZE) )
304                 ttsd_dbus_server_setting_finalize(conn, msg);
305
306         else if (dbus_message_is_method_call(msg, g_service_interface, TTS_SETTING_METHOD_GET_ENGINE_LIST) )
307                 ttsd_dbus_server_setting_get_engine_list(conn, msg);
308
309         else if (dbus_message_is_method_call(msg, g_service_interface, TTS_SETTING_METHOD_GET_ENGINE) )
310                 ttsd_dbus_server_setting_get_engine(conn, msg);
311
312         else if (dbus_message_is_method_call(msg, g_service_interface, TTS_SETTING_METHOD_SET_ENGINE) )
313                 ttsd_dbus_server_setting_set_engine(conn, msg);
314
315         else if (dbus_message_is_method_call(msg, g_service_interface, TTS_SETTING_METHOD_GET_VOICE_LIST) )
316                 ttsd_dbus_server_setting_get_voice_list(conn, msg);
317
318         else if (dbus_message_is_method_call(msg, g_service_interface, TTS_SETTING_METHOD_GET_DEFAULT_VOICE) )
319                 ttsd_dbus_server_setting_get_default_voice(conn, msg);
320
321         else if (dbus_message_is_method_call(msg, g_service_interface, TTS_SETTING_METHOD_SET_DEFAULT_VOICE) )
322                 ttsd_dbus_server_setting_set_default_voice(conn, msg);
323
324         else if (dbus_message_is_method_call(msg, g_service_interface, TTS_SETTING_METHOD_GET_DEFAULT_SPEED) )
325                 ttsd_dbus_server_setting_get_speed(conn, msg);
326
327         else if (dbus_message_is_method_call(msg, g_service_interface, TTS_SETTING_METHOD_SET_DEFAULT_SPEED) )
328                 ttsd_dbus_server_setting_set_speed(conn, msg);
329
330         else if (dbus_message_is_method_call(msg, g_service_interface, TTS_SETTING_METHOD_GET_ENGINE_SETTING) )
331                 ttsd_dbus_server_setting_get_engine_setting(conn, msg);
332
333         else if (dbus_message_is_method_call(msg, g_service_interface, TTS_SETTING_METHOD_SET_ENGINE_SETTING) )
334                 ttsd_dbus_server_setting_set_engine_setting(conn, msg);
335         
336
337         /* free the message */
338         dbus_message_unref(msg);
339
340         return ECORE_CALLBACK_RENEW;
341 }
342
343 int ttsd_dbus_open_connection()
344 {
345         DBusError err;
346         dbus_error_init(&err);
347
348         int ret;
349
350         /* connect to the bus and check for errors */
351         g_conn = dbus_bus_get(DBUS_BUS_SYSTEM, &err);
352
353         if (dbus_error_is_set(&err)) { 
354                 SLOG(LOG_ERROR, get_tag(), "[Dbus ERROR] fail dbus_bus_get : %s\n", err.message);
355                 dbus_error_free(&err); 
356         }
357
358         if (NULL == g_conn) { 
359                 SLOG(LOG_ERROR, get_tag(), "[Dbus ERROR] fail to get dbus connection \n" );
360                 return -1;
361         }
362
363         if (TTSD_MODE_SCREEN_READER == ttsd_get_mode()) {
364                 g_service_name = (char*)malloc(sizeof(char) * strlen(TTS_SR_SERVER_SERVICE_NAME) + 1);
365                 g_service_object = (char*)malloc(sizeof(char) * strlen(TTS_SR_SERVER_SERVICE_OBJECT_PATH) + 1);
366                 g_service_interface = (char*)malloc(sizeof(char) * strlen(TTS_SR_SERVER_SERVICE_INTERFACE) + 1);
367
368                 strcpy(g_service_name, TTS_SR_SERVER_SERVICE_NAME);
369                 strcpy(g_service_object, TTS_SR_SERVER_SERVICE_OBJECT_PATH);
370                 strcpy(g_service_interface, TTS_SR_SERVER_SERVICE_INTERFACE);
371         } else if (TTSD_MODE_NOTIFICATION == ttsd_get_mode()) {
372                 g_service_name = (char*)malloc(sizeof(char) * strlen(TTS_NOTI_SERVER_SERVICE_NAME) + 1);
373                 g_service_object = (char*)malloc(sizeof(char) * strlen(TTS_NOTI_SERVER_SERVICE_OBJECT_PATH) + 1);
374                 g_service_interface = (char*)malloc(sizeof(char) * strlen(TTS_NOTI_SERVER_SERVICE_INTERFACE) + 1);
375
376                 strcpy(g_service_name, TTS_NOTI_SERVER_SERVICE_NAME);
377                 strcpy(g_service_object, TTS_NOTI_SERVER_SERVICE_OBJECT_PATH);
378                 strcpy(g_service_interface, TTS_NOTI_SERVER_SERVICE_INTERFACE);
379         } else {
380                 g_service_name = (char*)malloc(sizeof(char) * strlen(TTS_SERVER_SERVICE_NAME) + 1);
381                 g_service_object = (char*)malloc(sizeof(char) * strlen(TTS_SERVER_SERVICE_OBJECT_PATH) + 1);
382                 g_service_interface = (char*)malloc(sizeof(char) * strlen(TTS_SERVER_SERVICE_INTERFACE) + 1);
383
384                 strcpy(g_service_name, TTS_SERVER_SERVICE_NAME);
385                 strcpy(g_service_object, TTS_SERVER_SERVICE_OBJECT_PATH);
386                 strcpy(g_service_interface, TTS_SERVER_SERVICE_INTERFACE);
387         }
388
389         /* request our name on the bus and check for errors */
390         ret = dbus_bus_request_name(g_conn, g_service_name, DBUS_NAME_FLAG_REPLACE_EXISTING , &err);
391
392         if (DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER != ret) {
393                 printf("Fail to be primary owner in dbus request. \n");
394                 SLOG(LOG_ERROR, get_tag(), "[Dbus ERROR] fail to be primary owner \n");
395                 return -1;
396         }
397
398         if (dbus_error_is_set(&err)) { 
399                 SLOG(LOG_ERROR, get_tag(), "[Dbus ERROR] dbus_bus_request_name() : %s \n", err.message);
400                 dbus_error_free(&err); 
401
402                 return -1;
403         }
404
405         /* add a rule for getting signal */
406         char rule[128];
407         snprintf(rule, 128, "type='signal',interface='%s'", g_service_interface);
408
409         /* add a rule for which messages we want to see */
410         dbus_bus_add_match(g_conn, rule, &err); /* see signals from the given interface */
411         dbus_connection_flush(g_conn);
412
413         if (dbus_error_is_set(&err)) { 
414                 SLOG(LOG_ERROR, get_tag(), "[Dbus ERROR] dbus_bus_add_match() : %s \n", err.message);
415                 return -1; 
416         }
417
418         int fd = 0;
419         dbus_connection_get_unix_fd(g_conn, &fd);
420
421         Ecore_Fd_Handler* fd_handler;
422         fd_handler = ecore_main_fd_handler_add(fd, ECORE_FD_READ , (Ecore_Fd_Cb)listener_event_callback, g_conn, NULL, NULL);
423
424         if (NULL == fd_handler) {
425                 SLOG(LOG_ERROR, get_tag(), "[Dbus ERROR] ecore_main_fd_handler_add() : fail to get fd handler \n");
426                 return -1;
427         }
428
429         SLOG(LOG_DEBUG, get_tag(), "[Dbus SUCCESS] Open connection. ");
430         return 0;
431 }
432
433 int ttsd_dbus_close_connection()
434 {
435         DBusError err;
436         dbus_error_init(&err);
437
438         dbus_bus_release_name (g_conn, g_service_name, &err);
439
440         if (dbus_error_is_set(&err)) {
441                 SLOG(LOG_ERROR, get_tag(), "[Dbus ERROR] dbus_bus_release_name() : %s\n", err.message); 
442                 dbus_error_free(&err); 
443                 return -1;
444         }
445
446         if (NULL != g_service_name)
447                 free(g_service_name);
448
449         if (NULL != g_service_object)
450                 free(g_service_object);
451
452         if (NULL != g_service_interface)
453                 free(g_service_interface);
454
455         SLOG(LOG_DEBUG, get_tag(), "[Dbus SUCCESS] Close connection. ");
456
457         return 0;
458 }
459
460 int ttsd_file_msg_open_connection(int pid)
461 {
462         /* Make file for Inotify */
463         char send_filename[64];
464         memset(send_filename, 0, 64);
465         snprintf(send_filename, 64, "%s_%d", MESSAGE_FILE_PATH, pid);
466
467         FILE* fp;
468         fp = fopen(send_filename, "a+");
469         if (NULL == fp) {
470                 SLOG(LOG_ERROR, get_tag(), "[File message ERROR] Fail to make message file");
471                 return -1;
472         }
473         SLOG(LOG_DEBUG, get_tag(), "[File message] Make message file");
474         fclose(fp);
475         return 0;
476 }
477
478 int ttsd_file_msg_close_connection(int pid)
479 {
480         /* delete inotify file */
481         char path[64];
482         memset(path, 0, 64);
483         snprintf(path, 64, "%s_%d", MESSAGE_FILE_PATH, pid);
484         if (0 == access(path, R_OK)) {
485                 if (0 != remove(path)) {
486                         SLOG(LOG_WARN, get_tag(), "[File message WARN] Fail to remove message file");
487                 }
488         }
489         SLOG(LOG_DEBUG, get_tag(), "[File message] Delete message file");
490         return 0;
491 }