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