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.
14 #include <dbus/dbus.h>
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"
25 static DBusConnection* g_conn_sender = NULL;
26 static DBusConnection* g_conn_listener = NULL;
28 static Ecore_Fd_Handler* g_dbus_fd_handler = NULL;
30 //static int g_waiting_time = 3000;
32 static char *g_service_name = NULL;
33 static char *g_service_object = NULL;
34 static char *g_service_interface = NULL;
36 const char* __ttsd_get_error_code(ttsd_error_e 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 case TTSD_ERROR_SERVICE_RESET: return "TTSD_ERROR_SERVICE_RESET";
53 return "Invalid error code";
59 int ttsdc_send_hello(int pid, int uid)
62 if (NULL == g_conn_sender) {
63 SLOG(LOG_ERROR, tts_tag(), "[Dbus ERROR] Dbus connection is not available");
67 char service_name[64];
68 memset(service_name, 0, 64);
69 snprintf(service_name, 64, "%s%d", TTS_CLIENT_SERVICE_NAME, pid);
71 char target_if_name[64];
72 snprintf(target_if_name, sizeof(target_if_name), "%s%d", TTS_CLIENT_SERVICE_INTERFACE, pid);
76 /* create a message & check for errors */
77 msg = dbus_message_new_method_call(
79 TTS_CLIENT_SERVICE_OBJECT_PATH,
84 SLOG(LOG_ERROR, tts_tag(), "<<<< [Dbus ERROR] Fail to create hello message : uid(%d)", uid);
87 SLOG(LOG_DEBUG, tts_tag(), "<<<< [Dbus] Send hello message : uid(%d)", uid);
90 dbus_message_append_args(msg, DBUS_TYPE_INT32, &uid, DBUS_TYPE_INVALID);
93 dbus_error_init(&err);
95 DBusMessage* result_msg;
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, tts_tag(), "[ERROR] Send error (%s)", err.message);
102 dbus_error_free(&err);
105 if (NULL != result_msg) {
106 dbus_message_get_args(result_msg, &err, DBUS_TYPE_INT32, &result, DBUS_TYPE_INVALID);
108 if (dbus_error_is_set(&err)) {
109 SLOG(LOG_ERROR, tts_tag(), ">>>> [Dbus] Get arguments error (%s)", err.message);
110 dbus_error_free(&err);
114 dbus_message_unref(result_msg);
116 SLOG(LOG_DEBUG, tts_tag(), ">>>> [Dbus] Result message is NULL. Client is not available");
125 int ttsdc_send_message(int pid, int uid, int data, const char *method)
127 if (NULL == g_conn_sender) {
128 SLOG(LOG_ERROR, tts_tag(), "[Dbus ERROR] Dbus connection is not available");
132 DBusMessage* msg = NULL;
134 /* create a message */
135 msg = dbus_message_new_signal(
136 TTS_CLIENT_SERVICE_OBJECT_PATH, /* object name of the signal */
137 TTS_CLIENT_SERVICE_INTERFACE, /* interface name of the signal */
138 method); /* name of the signal */
141 SLOG(LOG_ERROR, tts_tag(), "[Dbus ERROR] Fail to create message : %s", method);
144 SLOG(LOG_DEBUG, tts_tag(), "[Dbus] Send %s message : uid(%d) data(%d)", method, uid, data);
147 dbus_message_append_args(msg, DBUS_TYPE_INT32, &uid, DBUS_TYPE_INT32, &data, DBUS_TYPE_INVALID);
149 if (1 != dbus_connection_send(g_conn_sender, msg, NULL)) {
150 SLOG(LOG_ERROR, tts_tag(), "[Dbus ERROR] Fail to Send");
153 SLOG(LOG_DEBUG, tts_tag(), "[Dbus] SUCCESS Send");
154 dbus_connection_flush(g_conn_sender);
157 dbus_message_unref(msg);
162 int ttsdc_send_utt_start_message(int pid, int uid, int uttid)
164 return ttsdc_send_message(pid, uid, uttid, TTSD_METHOD_UTTERANCE_STARTED);
167 int ttsdc_send_utt_finish_message(int pid, int uid, int uttid)
169 return ttsdc_send_message(pid, uid, uttid, TTSD_METHOD_UTTERANCE_COMPLETED);
172 int ttsdc_send_set_state_message(int pid, int uid, int state)
174 return ttsdc_send_message(pid, uid, state, TTSD_METHOD_SET_STATE);
177 int ttsdc_send_error_message(int pid, int uid, int uttid, int reason, char* err_msg)
179 if (NULL == g_conn_sender) {
180 SLOG(LOG_ERROR, tts_tag(), "[Dbus ERROR] Dbus connection is not available");
184 DBusMessage* msg = NULL;
186 /* create a message */
187 msg = dbus_message_new_signal(
188 TTS_CLIENT_SERVICE_OBJECT_PATH, /* object name of the signal */
189 TTS_CLIENT_SERVICE_INTERFACE, /* interface name of the signal */
190 TTSD_METHOD_ERROR); /* name of the signal */
193 SLOG(LOG_ERROR, tts_tag(), "[Dbus ERROR] Fail to create error message : uid(%d)", uid);
197 dbus_message_append_args(msg,
198 DBUS_TYPE_INT32, &uid,
199 DBUS_TYPE_INT32, &uttid,
200 DBUS_TYPE_INT32, &reason,
201 DBUS_TYPE_STRING, &err_msg,
204 dbus_message_set_no_reply(msg, TRUE);
206 if (!dbus_connection_send(g_conn_sender, msg, NULL)) {
207 SLOG(LOG_ERROR, tts_tag(), "[Dbus ERROR] <<<< error message : Out Of Memory !");
209 SLOG(LOG_DEBUG, tts_tag(), "<<<< Send error message : uid(%d), reason(%s), uttid(%d), err_msg(%s)",
210 uid, __ttsd_get_error_code(reason), uttid, (NULL == err_msg) ? "NULL" : err_msg);
211 dbus_connection_flush(g_conn_sender);
214 dbus_message_unref(msg);
219 static Eina_Bool listener_event_callback(void* data, Ecore_Fd_Handler *fd_handler)
221 if (NULL == g_conn_listener) return ECORE_CALLBACK_RENEW;
223 dbus_connection_read_write_dispatch(g_conn_listener, 50);
226 DBusMessage* msg = NULL;
227 msg = dbus_connection_pop_message(g_conn_listener);
229 if (true != dbus_connection_get_is_connected(g_conn_listener)) {
230 SLOG(LOG_ERROR, tts_tag(), "[ERROR] Connection is disconnected");
231 return ECORE_CALLBACK_RENEW;
234 /* loop again if we haven't read a message */
236 return ECORE_CALLBACK_RENEW;
240 if (dbus_message_is_method_call(msg, g_service_interface, TTS_METHOD_HELLO)) {
241 ttsd_dbus_server_hello(g_conn_listener, msg);
243 } else if (dbus_message_is_method_call(msg, g_service_interface, TTS_METHOD_INITIALIZE)) {
244 ttsd_dbus_server_initialize(g_conn_listener, msg);
246 } else if (dbus_message_is_method_call(msg, g_service_interface, TTS_METHOD_FINALIZE)) {
247 ttsd_dbus_server_finalize(g_conn_listener, msg);
249 } else if (dbus_message_is_method_call(msg, g_service_interface, TTS_METHOD_GET_SUPPORT_VOICES)) {
250 ttsd_dbus_server_get_support_voices(g_conn_listener, msg);
252 } else if (dbus_message_is_method_call(msg, g_service_interface, TTS_METHOD_GET_CURRENT_VOICE)) {
253 ttsd_dbus_server_get_current_voice(g_conn_listener, msg);
255 } else if (dbus_message_is_method_call(msg, g_service_interface, TTS_METHOD_ADD_QUEUE)) {
256 ttsd_dbus_server_add_text(g_conn_listener, msg);
258 } else if (dbus_message_is_method_call(msg, g_service_interface, TTS_METHOD_PLAY)) {
259 ttsd_dbus_server_play(g_conn_listener, msg);
261 } else if (dbus_message_is_method_call(msg, g_service_interface, TTS_METHOD_STOP)) {
262 ttsd_dbus_server_stop(g_conn_listener, msg);
264 } else if (dbus_message_is_method_call(msg, g_service_interface, TTS_METHOD_PAUSE)) {
265 ttsd_dbus_server_pause(g_conn_listener, msg);
267 } else if (dbus_message_is_method_call(msg, g_service_interface, TTS_METHOD_SET_PRIVATE_DATA)) {
268 ttsd_dbus_server_set_private_data(g_conn_listener, msg);
270 } else if (dbus_message_is_method_call(msg, g_service_interface, TTS_METHOD_GET_PRIVATE_DATA)) {
271 ttsd_dbus_server_get_private_data(g_conn_listener, msg);
273 } else if (dbus_message_is_method_call(msg, g_service_interface, TTS_METHOD_PLAY_PCM)) {
274 ttsd_dbus_server_play_pcm(g_conn_listener, msg);
276 } else if (dbus_message_is_method_call(msg, g_service_interface, TTS_METHOD_STOP_PCM)) {
277 ttsd_dbus_server_stop_pcm(g_conn_listener, msg);
279 } else if (dbus_message_is_method_call(msg, g_service_interface, TTS_METHOD_ADD_PCM)) {
280 ttsd_dbus_server_add_pcm(g_conn_listener, msg);
283 SLOG(LOG_DEBUG, tts_tag(), "Message is NOT valid");
286 /* free the message */
287 dbus_message_unref(msg);
290 return ECORE_CALLBACK_RENEW;
293 void __ttsd_dbus_service_free()
295 if (NULL != g_service_name) {
296 free(g_service_name);
297 g_service_name = NULL;
300 if (NULL != g_service_object) {
301 free(g_service_object);
302 g_service_object = NULL;
305 if (NULL != g_service_interface) {
306 free(g_service_interface);
307 g_service_interface = NULL;
311 void __ttsd_dbus_connection_free()
313 if (NULL != g_conn_listener) {
314 dbus_connection_close(g_conn_listener);
315 g_conn_listener = NULL;
317 if (NULL != g_conn_sender) {
318 dbus_connection_close(g_conn_sender);
319 g_conn_sender = NULL;
323 int ttsd_dbus_open_connection()
325 SLOG(LOG_DEBUG, tts_tag(), "@@@ start dbus open connection");
327 dbus_error_init(&err);
331 /* Create connection for sender */
332 g_conn_sender = dbus_bus_get_private(DBUS_BUS_SESSION, &err);
333 if (dbus_error_is_set(&err)) {
334 SLOG(LOG_ERROR, tts_tag(), "[Dbus ERROR] Fail dbus_bus_get : %s", err.message);
335 dbus_error_free(&err);
338 if (NULL == g_conn_sender) {
339 SLOG(LOG_ERROR, tts_tag(), "[Dbus ERROR] Fail to get dbus connection sender");
343 dbus_connection_set_exit_on_disconnect(g_conn_sender, false);
345 /* connect to the bus and check for errors */
346 g_conn_listener = dbus_bus_get_private(DBUS_BUS_SESSION, &err);
347 if (dbus_error_is_set(&err)) {
348 SLOG(LOG_ERROR, tts_tag(), "[Dbus ERROR] Fail dbus_bus_get : %s", err.message);
349 dbus_error_free(&err);
350 __ttsd_dbus_connection_free();
354 if (NULL == g_conn_listener) {
355 SLOG(LOG_ERROR, tts_tag(), "[Dbus ERROR] Fail to get dbus connection");
356 __ttsd_dbus_connection_free();
360 dbus_connection_set_exit_on_disconnect(g_conn_listener, false);
362 __ttsd_dbus_service_free();
364 if (TTSD_MODE_SCREEN_READER == ttsd_get_mode()) {
365 g_service_name = (char*)calloc(strlen(TTS_SR_SERVER_SERVICE_NAME) + 1, sizeof(char));
366 g_service_object = (char*)calloc(strlen(TTS_SR_SERVER_SERVICE_OBJECT_PATH) + 1, sizeof(char));
367 g_service_interface = (char*)calloc(strlen(TTS_SR_SERVER_SERVICE_INTERFACE) + 1, sizeof(char));
369 if (NULL == g_service_name || NULL == g_service_object || NULL == g_service_interface) {
370 SLOG(LOG_ERROR, tts_tag(), "[ERROR] Fail to allocate memory");
371 __ttsd_dbus_service_free();
372 __ttsd_dbus_connection_free();
376 snprintf(g_service_name, strlen(TTS_SR_SERVER_SERVICE_NAME) + 1, "%s", TTS_SR_SERVER_SERVICE_NAME);
377 snprintf(g_service_object, strlen(TTS_SR_SERVER_SERVICE_OBJECT_PATH) + 1, "%s", TTS_SR_SERVER_SERVICE_OBJECT_PATH);
378 snprintf(g_service_interface, strlen(TTS_SR_SERVER_SERVICE_INTERFACE) + 1, "%s", TTS_SR_SERVER_SERVICE_INTERFACE);
379 } else if (TTSD_MODE_NOTIFICATION == ttsd_get_mode()) {
380 g_service_name = (char*)calloc(strlen(TTS_NOTI_SERVER_SERVICE_NAME) + 1, sizeof(char));
381 g_service_object = (char*)calloc(strlen(TTS_NOTI_SERVER_SERVICE_OBJECT_PATH) + 1, sizeof(char));
382 g_service_interface = (char*)calloc(strlen(TTS_NOTI_SERVER_SERVICE_INTERFACE) + 1, sizeof(char));
384 if (NULL == g_service_name || NULL == g_service_object || NULL == g_service_interface) {
385 SLOG(LOG_ERROR, tts_tag(), "[ERROR] Fail to allocate memory");
386 __ttsd_dbus_service_free();
387 __ttsd_dbus_connection_free();
391 snprintf(g_service_name, strlen(TTS_NOTI_SERVER_SERVICE_NAME) + 1, "%s", TTS_NOTI_SERVER_SERVICE_NAME);
392 snprintf(g_service_object, strlen(TTS_NOTI_SERVER_SERVICE_OBJECT_PATH) + 1, "%s", TTS_NOTI_SERVER_SERVICE_OBJECT_PATH);
393 snprintf(g_service_interface, strlen(TTS_NOTI_SERVER_SERVICE_INTERFACE) + 1, "%s", TTS_NOTI_SERVER_SERVICE_INTERFACE);
394 } else if (TTSD_MODE_INTERRUPT == ttsd_get_mode()) {
395 g_service_name = (char*)calloc(strlen(TTS_INTERRUPT_SERVER_SERVICE_NAME) + 1, sizeof(char));
396 g_service_object = (char*)calloc(strlen(TTS_INTERRUPT_SERVER_SERVICE_OBJECT_PATH) + 1, sizeof(char));
397 g_service_interface = (char*)calloc(strlen(TTS_INTERRUPT_SERVER_SERVICE_INTERFACE) + 1, sizeof(char));
399 if (NULL == g_service_name || NULL == g_service_object || NULL == g_service_interface) {
400 SLOG(LOG_ERROR, tts_tag(), "[ERROR] Fail to allocate memory");
401 __ttsd_dbus_service_free();
402 __ttsd_dbus_connection_free();
406 snprintf(g_service_name, strlen(TTS_INTERRUPT_SERVER_SERVICE_NAME) + 1, "%s", TTS_INTERRUPT_SERVER_SERVICE_NAME);
407 snprintf(g_service_object, strlen(TTS_INTERRUPT_SERVER_SERVICE_OBJECT_PATH) + 1, "%s", TTS_INTERRUPT_SERVER_SERVICE_OBJECT_PATH);
408 snprintf(g_service_interface, strlen(TTS_INTERRUPT_SERVER_SERVICE_INTERFACE) + 1, "%s", TTS_INTERRUPT_SERVER_SERVICE_INTERFACE);
410 g_service_name = (char*)calloc(strlen(TTS_SERVER_SERVICE_NAME) + 1, sizeof(char));
411 g_service_object = (char*)calloc(strlen(TTS_SERVER_SERVICE_OBJECT_PATH) + 1, sizeof(char));
412 g_service_interface = (char*)calloc(strlen(TTS_SERVER_SERVICE_INTERFACE) + 1, sizeof(char));
414 if (NULL == g_service_name || NULL == g_service_object || NULL == g_service_interface) {
415 SLOG(LOG_ERROR, tts_tag(), "[ERROR] Fail to allocate memory");
416 __ttsd_dbus_service_free();
417 __ttsd_dbus_connection_free();
421 snprintf(g_service_name, strlen(TTS_SERVER_SERVICE_NAME) + 1, "%s", TTS_SERVER_SERVICE_NAME);
422 snprintf(g_service_object, strlen(TTS_SERVER_SERVICE_OBJECT_PATH) + 1, "%s", TTS_SERVER_SERVICE_OBJECT_PATH);
423 snprintf(g_service_interface, strlen(TTS_SERVER_SERVICE_INTERFACE) + 1, "%s", TTS_SERVER_SERVICE_INTERFACE);
426 /* request our name on the bus and check for errors */
427 ret = dbus_bus_request_name(g_conn_listener, g_service_name, DBUS_NAME_FLAG_REPLACE_EXISTING, &err);
428 if (DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER != ret) {
429 SLOG(LOG_ERROR, tts_tag(), "[Dbus ERROR] Fail to be primary owner");
430 __ttsd_dbus_connection_free();
434 if (dbus_error_is_set(&err)) {
435 SLOG(LOG_ERROR, tts_tag(), "[Dbus ERROR] Fail to request dbus name : %s", err.message);
436 dbus_error_free(&err);
437 __ttsd_dbus_connection_free();
441 /* add a rule for getting signal */
443 snprintf(rule, 128, "type='method_call',interface='%s'", g_service_interface);
445 /* add a rule for which messages we want to see */
446 dbus_bus_add_match(g_conn_listener, rule, &err);/* see signals from the given interface */
447 dbus_connection_flush(g_conn_listener);
449 if (dbus_error_is_set(&err)) {
450 SLOG(LOG_ERROR, tts_tag(), "[Dbus ERROR] dbus_bus_add_match() : %s", err.message);
451 __ttsd_dbus_connection_free();
456 dbus_connection_get_unix_fd(g_conn_listener, &fd);
458 g_dbus_fd_handler = ecore_main_fd_handler_add(fd, ECORE_FD_READ, (Ecore_Fd_Cb)listener_event_callback, g_conn_listener, NULL, NULL);
459 if (NULL == g_dbus_fd_handler) {
460 SLOG(LOG_ERROR, tts_tag(), "[Dbus ERROR] Fail to get fd handler");
461 __ttsd_dbus_connection_free();
465 SLOG(LOG_DEBUG, tts_tag(), "@@@");
469 int ttsd_dbus_close_connection()
471 SLOG(LOG_DEBUG, tts_tag(), "@@@ start dbus close connection");
473 dbus_error_init(&err);
475 if (NULL != g_dbus_fd_handler) {
476 ecore_main_fd_handler_del(g_dbus_fd_handler);
477 g_dbus_fd_handler = NULL;
480 if (NULL != g_conn_listener) {
481 dbus_bus_release_name(g_conn_listener, g_service_name, &err);
482 if (dbus_error_is_set(&err)) {
483 SLOG(LOG_ERROR, tts_tag(), "[Dbus ERROR] dbus_bus_release_name() : %s", err.message);
484 dbus_error_free(&err);
488 __ttsd_dbus_connection_free();
489 __ttsd_dbus_service_free();
491 SLOG(LOG_DEBUG, tts_tag(), "@@@");