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