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