Fix bugs
[platform/core/uifw/tts.git] / server / ttsd_dbus.c
1 /*
2 *  Copyright (c) 2011-2016 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 #include <dbus/dbus.h>
15 #include <dirent.h>
16 #include <dlfcn.h>
17 #include <Ecore.h>
18
19 #include "tts_config_mgr.h"
20 #include "ttsd_main.h"
21 #include "ttsd_dbus_server.h"
22 #include "ttsd_dbus.h"
23 #include "ttsd_server.h"
24
25 static DBusConnection* g_conn_sender = NULL;
26 static DBusConnection* g_conn_listener = NULL;
27
28 static Ecore_Fd_Handler* g_dbus_fd_handler = NULL;
29
30 //static int g_waiting_time = 3000;
31
32 static char *g_service_name = NULL;
33 static char *g_service_object = NULL;
34 static char *g_service_interface = NULL;
35
36 const char* __ttsd_get_error_code(ttsd_error_e err)
37 {
38         switch (err) {
39         case TTSD_ERROR_NONE:                   return "TTS_ERROR_NONE";
40         case TTSD_ERROR_OUT_OF_MEMORY:          return "TTS_ERROR_OUT_OF_MEMORY";
41         case TTSD_ERROR_IO_ERROR:               return "TTS_ERROR_IO_ERROR";
42         case TTSD_ERROR_INVALID_PARAMETER:      return "TTS_ERROR_INVALID_PARAMETER";
43         case TTSD_ERROR_OUT_OF_NETWORK:         return "TTS_ERROR_OUT_OF_NETWORK";
44         case TTSD_ERROR_INVALID_STATE:          return "TTS_ERROR_INVALID_STATE";
45         case TTSD_ERROR_INVALID_VOICE:          return "TTS_ERROR_INVALID_VOICE";
46         case TTSD_ERROR_ENGINE_NOT_FOUND:       return "TTS_ERROR_ENGINE_NOT_FOUND";
47         case TTSD_ERROR_TIMED_OUT:              return "TTS_ERROR_TIMED_OUT";
48         case TTSD_ERROR_OPERATION_FAILED:       return "TTS_ERROR_OPERATION_FAILED";
49         case TTSD_ERROR_AUDIO_POLICY_BLOCKED:   return "TTS_ERROR_AUDIO_POLICY_BLOCKED";
50         case TTSD_ERROR_NOT_SUPPORTED_FEATURE:  return "TTSD_ERROR_NOT_SUPPORTED_FEATURE";
51         default:
52                 return "Invalid error code";
53         }
54
55         return NULL;
56 }
57
58 int ttsdc_send_hello(int pid, int uid)
59 {
60 #if 0
61         if (NULL == g_conn_sender) {
62                 SLOG(LOG_ERROR, get_tag(), "[Dbus ERROR] Dbus connection is not available");
63                 return -1;
64         }
65
66         char service_name[64];
67         memset(service_name, 0, 64);
68         snprintf(service_name, 64, "%s%d", TTS_CLIENT_SERVICE_NAME, pid);
69
70         char target_if_name[64];
71         snprintf(target_if_name, sizeof(target_if_name), "%s%d", TTS_CLIENT_SERVICE_INTERFACE, pid);
72
73         DBusMessage* msg;
74
75         /* create a message & check for errors */
76         msg = dbus_message_new_method_call(
77                 service_name, 
78                 TTS_CLIENT_SERVICE_OBJECT_PATH, 
79                 target_if_name, 
80                 TTSD_METHOD_HELLO);
81
82         if (NULL == msg) {
83                 SLOG(LOG_ERROR, get_tag(), "<<<< [Dbus ERROR] Fail to create hello message : uid(%d)", uid);
84                 return -1;
85         } else {
86                 SLOG(LOG_DEBUG, get_tag(), "<<<< [Dbus] Send hello message : uid(%d)", uid);
87         }
88
89         dbus_message_append_args(msg, DBUS_TYPE_INT32, &uid, DBUS_TYPE_INVALID);
90
91         DBusError err;
92         dbus_error_init(&err);
93
94         DBusMessage* result_msg;
95         int result = -1;
96
97         result_msg = dbus_connection_send_with_reply_and_block(g_conn_sender, msg, g_waiting_time, &err);
98         dbus_message_unref(msg);
99         if (dbus_error_is_set(&err)) {
100                 SLOG(LOG_ERROR, get_tag(), "[ERROR] Send error (%s)", err.message);
101                 dbus_error_free(&err);
102         }
103
104         if (NULL != result_msg) {
105                 dbus_message_get_args(result_msg, &err, DBUS_TYPE_INT32, &result, DBUS_TYPE_INVALID);
106
107                 if (dbus_error_is_set(&err)) {
108                         SLOG(LOG_ERROR, get_tag(), ">>>> [Dbus] Get arguments error (%s)", err.message);
109                         dbus_error_free(&err);
110                         result = -1;
111                 }
112
113                 dbus_message_unref(result_msg);
114         } else {
115                 SLOG(LOG_DEBUG, get_tag(), ">>>> [Dbus] Result message is NULL. Client is not available");
116                 result = 0;
117         }
118
119         return result;
120 #endif
121         return 0;
122 }
123
124 int ttsdc_send_message(int pid, int uid, int data, const char *method)
125 {
126         if (NULL == g_conn_sender) {
127                 SLOG(LOG_ERROR, get_tag(), "[Dbus ERROR] Dbus connection is not available");
128                 return -1;
129         }
130
131         DBusMessage* msg = NULL;
132
133         /* create a message */
134         msg = dbus_message_new_signal(
135                 TTS_CLIENT_SERVICE_OBJECT_PATH, /* object name of the signal */
136                 TTS_CLIENT_SERVICE_INTERFACE,   /* interface name of the signal */
137                 method);                        /* name of the signal */
138
139         if (NULL == msg) {
140                 SLOG(LOG_ERROR, get_tag(), "<<<< [Dbus ERROR] Fail to create message : %s", method);
141                 return -1;
142         } else {
143                 SLOG(LOG_DEBUG, get_tag(), "<<<< [Dbus] Send %s message : uid(%d) data(%d)", method, uid, data);
144         }
145
146         dbus_message_append_args(msg, DBUS_TYPE_INT32, &uid, DBUS_TYPE_INT32, &data, DBUS_TYPE_INVALID);
147
148         if (1 != dbus_connection_send(g_conn_sender, msg, NULL)) {
149                 SLOG(LOG_ERROR, get_tag(), "[Dbus ERROR] Fail to Send");
150                 return -1;
151         } else {
152                 SLOG(LOG_DEBUG, get_tag(), "[Dbus] SUCCESS Send");
153                 dbus_connection_flush(g_conn_sender);
154         }
155
156         dbus_message_unref(msg);
157
158         return 0;
159 }
160
161 int ttsdc_send_utt_start_message(int pid, int uid, int uttid)
162 {
163         return ttsdc_send_message(pid, uid, uttid, TTSD_METHOD_UTTERANCE_STARTED);
164 }
165
166 int ttsdc_send_utt_finish_message(int pid, int uid, int uttid)
167 {
168         return ttsdc_send_message(pid, uid, uttid, TTSD_METHOD_UTTERANCE_COMPLETED);
169 }
170
171 int ttsdc_send_set_state_message(int pid, int uid, int state)
172 {
173         return ttsdc_send_message(pid, uid, state, TTSD_METHOD_SET_STATE);
174 }
175
176 int ttsdc_send_error_message(int pid, int uid, int uttid, int reason, char* err_msg)
177 {
178         if (NULL == g_conn_sender) {
179                 SLOG(LOG_ERROR, get_tag(), "[Dbus ERROR] Dbus connection is not available");
180                 return -1;
181         }
182
183         DBusMessage* msg = NULL;
184
185         /* create a message */
186         msg = dbus_message_new_signal(
187                 TTS_CLIENT_SERVICE_OBJECT_PATH, /* object name of the signal */
188                 TTS_CLIENT_SERVICE_INTERFACE,   /* interface name of the signal */
189                 TTSD_METHOD_ERROR);             /* name of the signal */
190
191         if (NULL == msg) {
192                 SLOG(LOG_ERROR, get_tag(), "[Dbus ERROR] Fail to create error message : uid(%d)", uid);
193                 return -1;
194         }
195
196         dbus_message_append_args(msg,
197                 DBUS_TYPE_INT32, &uid,
198                 DBUS_TYPE_INT32, &uttid,
199                 DBUS_TYPE_INT32, &reason,
200                 DBUS_TYPE_INT32, &err_msg,
201                 DBUS_TYPE_INVALID);
202
203         dbus_message_set_no_reply(msg, TRUE);
204
205         if (!dbus_connection_send(g_conn_sender, msg, NULL)) {
206                 SLOG(LOG_ERROR, get_tag(), "[Dbus ERROR] <<<< error message : Out Of Memory !");
207         } else {
208                 SLOG(LOG_DEBUG, get_tag(), "<<<< Send error message : uid(%d), reason(%s), uttid(%d), err_msg(%d)",
209                          uid, __ttsd_get_error_code(reason), uttid, (NULL == err_msg) ? "NULL" : err_msg);
210                 dbus_connection_flush(g_conn_sender);
211         }
212
213         dbus_message_unref(msg);
214
215         return 0;
216 }
217
218 static Eina_Bool listener_event_callback(void* data, Ecore_Fd_Handler *fd_handler)
219 {
220         if (NULL == g_conn_listener)    return ECORE_CALLBACK_RENEW;
221
222         dbus_connection_read_write_dispatch(g_conn_listener, 50);
223
224         DBusMessage* msg = NULL;
225         msg = dbus_connection_pop_message(g_conn_listener);
226
227         if (true != dbus_connection_get_is_connected(g_conn_listener)) {
228                 SLOG(LOG_ERROR, get_tag(), "[ERROR] Connection is disconnected");
229                 return ECORE_CALLBACK_RENEW;
230         }
231
232         /* loop again if we haven't read a message */
233         if (NULL == msg) {
234                 return ECORE_CALLBACK_RENEW;
235         }
236
237         /* client event */
238         if (dbus_message_is_method_call(msg, g_service_interface, TTS_METHOD_HELLO)) {
239                 ttsd_dbus_server_hello(g_conn_listener, msg);
240
241         } else if (dbus_message_is_method_call(msg, g_service_interface, TTS_METHOD_INITIALIZE)) {
242                 ttsd_dbus_server_initialize(g_conn_listener, msg);
243
244         } else if (dbus_message_is_method_call(msg, g_service_interface, TTS_METHOD_FINALIZE)) {
245                 ttsd_dbus_server_finalize(g_conn_listener, msg);
246
247         } else if (dbus_message_is_method_call(msg, g_service_interface, TTS_METHOD_GET_SUPPORT_VOICES)) {
248                 ttsd_dbus_server_get_support_voices(g_conn_listener, msg);
249
250         } else if (dbus_message_is_method_call(msg, g_service_interface, TTS_METHOD_GET_CURRENT_VOICE)) {
251                 ttsd_dbus_server_get_current_voice(g_conn_listener, msg);
252
253         } else if (dbus_message_is_method_call(msg, g_service_interface, TTS_METHOD_ADD_QUEUE)) {
254                 ttsd_dbus_server_add_text(g_conn_listener, msg);
255
256         } else if (dbus_message_is_method_call(msg, g_service_interface, TTS_METHOD_PLAY)) {
257                 ttsd_dbus_server_play(g_conn_listener, msg);
258
259         } else if (dbus_message_is_method_call(msg, g_service_interface, TTS_METHOD_STOP)) {
260                 ttsd_dbus_server_stop(g_conn_listener, msg);
261
262         } else if (dbus_message_is_method_call(msg, g_service_interface, TTS_METHOD_PAUSE)) {
263                 ttsd_dbus_server_pause(g_conn_listener, msg);
264
265         } else if (dbus_message_is_method_call(msg, g_service_interface, TTS_METHOD_SET_PRIVATE_DATA)) {
266                 ttsd_dbus_server_set_private_data(g_conn_listener, msg);
267
268         } else if (dbus_message_is_method_call(msg, g_service_interface, TTS_METHOD_GET_PRIVATE_DATA)) {
269                 ttsd_dbus_server_get_private_data(g_conn_listener, msg);
270
271         } else {
272                 /* Invalid method */
273         }
274
275         /* free the message */
276         dbus_message_unref(msg);
277
278         return ECORE_CALLBACK_RENEW;
279 }
280
281 int ttsd_dbus_open_connection()
282 {
283         DBusError err;
284         dbus_error_init(&err);
285
286         int ret;
287
288         /* Create connection for sender */
289         g_conn_sender = dbus_bus_get_private(DBUS_BUS_SESSION, &err);
290         if (dbus_error_is_set(&err)) {
291                 SLOG(LOG_ERROR, get_tag(), "[Dbus ERROR] Fail dbus_bus_get : %s", err.message);
292                 dbus_error_free(&err);
293         }
294
295         if (NULL == g_conn_sender) {
296                 SLOG(LOG_ERROR, get_tag(), "[Dbus ERROR] Fail to get dbus connection sender");
297                 return -1;
298         }
299
300         /* connect to the bus and check for errors */
301         g_conn_listener = dbus_bus_get_private(DBUS_BUS_SESSION, &err);
302         if (dbus_error_is_set(&err)) {
303                 SLOG(LOG_ERROR, get_tag(), "[Dbus ERROR] Fail dbus_bus_get : %s", err.message);
304                 dbus_error_free(&err);
305                 return -1;
306         }
307
308         if (NULL == g_conn_listener) {
309                 SLOG(LOG_ERROR, get_tag(), "[Dbus ERROR] Fail to get dbus connection");
310                 return -1;
311         }
312
313         if (TTSD_MODE_SCREEN_READER == ttsd_get_mode()) {
314                 g_service_name = (char*)calloc(strlen(TTS_SR_SERVER_SERVICE_NAME) + 1, sizeof(char));
315                 g_service_object = (char*)calloc(strlen(TTS_SR_SERVER_SERVICE_OBJECT_PATH) + 1, sizeof(char));
316                 g_service_interface = (char*)calloc(strlen(TTS_SR_SERVER_SERVICE_INTERFACE) + 1, sizeof(char));
317
318                 snprintf(g_service_name, strlen(TTS_SR_SERVER_SERVICE_NAME) + 1, "%s", TTS_SR_SERVER_SERVICE_NAME);
319                 snprintf(g_service_object, strlen(TTS_SR_SERVER_SERVICE_OBJECT_PATH) + 1, "%s", TTS_SR_SERVER_SERVICE_OBJECT_PATH);
320                 snprintf(g_service_interface, strlen(TTS_SR_SERVER_SERVICE_INTERFACE) + 1, "%s", TTS_SR_SERVER_SERVICE_INTERFACE);
321         } else if (TTSD_MODE_NOTIFICATION == ttsd_get_mode()) {
322                 g_service_name = (char*)calloc(strlen(TTS_NOTI_SERVER_SERVICE_NAME) + 1, sizeof(char));
323                 g_service_object = (char*)calloc(strlen(TTS_NOTI_SERVER_SERVICE_OBJECT_PATH) + 1, sizeof(char));
324                 g_service_interface = (char*)calloc(strlen(TTS_NOTI_SERVER_SERVICE_INTERFACE) + 1, sizeof(char));
325
326                 snprintf(g_service_name, strlen(TTS_NOTI_SERVER_SERVICE_NAME) + 1, "%s", TTS_NOTI_SERVER_SERVICE_NAME);
327                 snprintf(g_service_object, strlen(TTS_NOTI_SERVER_SERVICE_OBJECT_PATH) + 1, "%s", TTS_NOTI_SERVER_SERVICE_OBJECT_PATH);
328                 snprintf(g_service_interface, strlen(TTS_NOTI_SERVER_SERVICE_INTERFACE) + 1, "%s", TTS_NOTI_SERVER_SERVICE_INTERFACE);
329         } else {
330                 g_service_name = (char*)calloc(strlen(TTS_SERVER_SERVICE_NAME) + 1, sizeof(char));
331                 g_service_object = (char*)calloc(strlen(TTS_SERVER_SERVICE_OBJECT_PATH) + 1, sizeof(char));
332                 g_service_interface = (char*)calloc(strlen(TTS_SERVER_SERVICE_INTERFACE) + 1, sizeof(char));
333
334                 snprintf(g_service_name, strlen(TTS_SERVER_SERVICE_NAME) + 1, "%s", TTS_SERVER_SERVICE_NAME);
335                 snprintf(g_service_object, strlen(TTS_SERVER_SERVICE_OBJECT_PATH) + 1, "%s", TTS_SERVER_SERVICE_OBJECT_PATH);
336                 snprintf(g_service_interface, strlen(TTS_SERVER_SERVICE_INTERFACE) + 1, "%s", TTS_SERVER_SERVICE_INTERFACE);
337         }
338
339         /* request our name on the bus and check for errors */
340         ret = dbus_bus_request_name(g_conn_listener, g_service_name, DBUS_NAME_FLAG_REPLACE_EXISTING, &err);
341         if (DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER != ret) {
342                 SLOG(LOG_ERROR, get_tag(), "[Dbus ERROR] Fail to be primary owner");
343                 return -1;
344         }
345
346         if (dbus_error_is_set(&err)) {
347                 SLOG(LOG_ERROR, get_tag(), "[Dbus ERROR] Fail to request dbus name : %s", err.message);
348                 dbus_error_free(&err);
349                 return -1;
350         }
351
352         /* add a rule for getting signal */
353         char rule[128];
354         snprintf(rule, 128, "type='method_call',interface='%s'", g_service_interface);
355
356         /* add a rule for which messages we want to see */
357         dbus_bus_add_match(g_conn_listener, rule, &err);/* see signals from the given interface */
358         dbus_connection_flush(g_conn_listener);
359
360         if (dbus_error_is_set(&err)) {
361                 SLOG(LOG_ERROR, get_tag(), "[Dbus ERROR] dbus_bus_add_match() : %s", err.message);
362                 return -1;
363         }
364
365         int fd = 0;
366         dbus_connection_get_unix_fd(g_conn_listener, &fd);
367
368         g_dbus_fd_handler = ecore_main_fd_handler_add(fd, ECORE_FD_READ, (Ecore_Fd_Cb)listener_event_callback, g_conn_listener, NULL, NULL);
369         if (NULL == g_dbus_fd_handler) {
370                 SLOG(LOG_ERROR, get_tag(), "[Dbus ERROR] Fail to get fd handler");
371                 return -1;
372         }
373
374         return 0;
375 }
376
377 int ttsd_dbus_close_connection()
378 {
379         DBusError err;
380         dbus_error_init(&err);
381
382         if (NULL != g_dbus_fd_handler) {
383                 ecore_main_fd_handler_del(g_dbus_fd_handler);
384                 g_dbus_fd_handler = NULL;
385         }
386
387         dbus_bus_release_name(g_conn_listener, g_service_name, &err);
388         if (dbus_error_is_set(&err)) {
389                 SLOG(LOG_ERROR, get_tag(), "[Dbus ERROR] dbus_bus_release_name() : %s", err.message);
390                 dbus_error_free(&err);
391         }
392
393         dbus_connection_close(g_conn_sender);
394         dbus_connection_close(g_conn_listener);
395
396         dbus_connection_unref(g_conn_sender);
397         dbus_connection_unref(g_conn_listener);
398
399         g_conn_listener = NULL;
400         g_conn_sender = NULL;
401
402         if (NULL != g_service_name)     free(g_service_name);
403         if (NULL != g_service_object)   free(g_service_object);
404         if (NULL != g_service_interface)free(g_service_interface);
405
406         return 0;
407 }