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, int ret, int credential_needed)
61 if (NULL == g_conn_sender) {
62 SLOG(LOG_ERROR, tts_tag(), "[Dbus ERROR] Dbus connection is not available");
67 char target_if_name[64];
68 snprintf(target_if_name, sizeof(target_if_name), "%s%d", TTS_CLIENT_SERVICE_INTERFACE, pid);
70 /* create a message */
71 msg = dbus_message_new_signal(
72 TTS_CLIENT_SERVICE_OBJECT_PATH, /* object name of the signal */
73 target_if_name, /* interface name of the signal */
74 TTSD_METHOD_HELLO); /* name of the signal */
77 SLOG(LOG_ERROR, tts_tag(), "<<<< [Dbus ERROR] Fail to create hello message : uid(%d), ret(%d), credential_needed(%d)", uid, ret, credential_needed);
80 SLOG(LOG_INFO, tts_tag(), "<<<< [Dbus] Send hello message : uid(%d), ret(%d), credential_needed(%d)", uid, ret, credential_needed);
83 dbus_message_append_args(msg, DBUS_TYPE_INT32, &uid, DBUS_TYPE_INT32, &ret, DBUS_TYPE_INT32, &credential_needed, DBUS_TYPE_INVALID);
85 if (1 != dbus_connection_send(g_conn_sender, msg, NULL)) {
86 SLOG(LOG_ERROR, tts_tag(), "[Dbus ERROR] Fail to Send");
87 return TTSD_ERROR_OPERATION_FAILED;
89 SLOG(LOG_INFO, tts_tag(), "[Dbus] SUCCESS Send");
90 dbus_connection_flush(g_conn_sender);
93 dbus_message_unref(msg);
97 int ttsdc_send_message(int pid, int uid, int data, const char *method)
99 if (NULL == g_conn_sender) {
100 SLOG(LOG_ERROR, tts_tag(), "[Dbus ERROR] Dbus connection is not available");
104 DBusMessage* msg = NULL;
106 char target_if_name[64];
107 snprintf(target_if_name, sizeof(target_if_name), "%s%d", TTS_CLIENT_SERVICE_INTERFACE, pid);
109 /* create a message */
110 msg = dbus_message_new_signal(
111 TTS_CLIENT_SERVICE_OBJECT_PATH, /* object name of the signal */
112 target_if_name, /* interface name of the signal */
113 method); /* name of the signal */
116 SLOG(LOG_ERROR, tts_tag(), "[Dbus ERROR] Fail to create message : %s", method);
119 SLOG(LOG_DEBUG, tts_tag(), "[Dbus] Send %s message : uid(%d) data(%d)", method, uid, data);
122 dbus_message_append_args(msg, DBUS_TYPE_INT32, &uid, DBUS_TYPE_INT32, &data, DBUS_TYPE_INVALID);
124 if (1 != dbus_connection_send(g_conn_sender, msg, NULL)) {
125 SLOG(LOG_ERROR, tts_tag(), "[Dbus ERROR] Fail to Send");
128 SLOG(LOG_DEBUG, tts_tag(), "[Dbus] SUCCESS Send");
129 dbus_connection_flush(g_conn_sender);
132 dbus_message_unref(msg);
137 int ttsdc_send_utt_start_message(int pid, int uid, int uttid)
139 return ttsdc_send_message(pid, uid, uttid, TTSD_METHOD_UTTERANCE_STARTED);
142 int ttsdc_send_utt_finish_message(int pid, int uid, int uttid)
144 return ttsdc_send_message(pid, uid, uttid, TTSD_METHOD_UTTERANCE_COMPLETED);
147 int ttsdc_send_set_state_message(int pid, int uid, int state)
149 return ttsdc_send_message(pid, uid, state, TTSD_METHOD_SET_STATE);
152 int ttsdc_send_error_message(int pid, int uid, int uttid, int reason, char* err_msg)
154 if (NULL == g_conn_sender) {
155 SLOG(LOG_ERROR, tts_tag(), "[Dbus ERROR] Dbus connection is not available");
159 DBusMessage* msg = NULL;
161 char target_if_name[64];
162 snprintf(target_if_name, sizeof(target_if_name), "%s%d", TTS_CLIENT_SERVICE_INTERFACE, pid);
164 /* create a message */
165 msg = dbus_message_new_signal(
166 TTS_CLIENT_SERVICE_OBJECT_PATH, /* object name of the signal */
167 target_if_name, /* interface name of the signal */
168 TTSD_METHOD_ERROR); /* name of the signal */
171 SLOG(LOG_ERROR, tts_tag(), "[Dbus ERROR] Fail to create error message : uid(%d)", uid);
175 dbus_message_append_args(msg,
176 DBUS_TYPE_INT32, &uid,
177 DBUS_TYPE_INT32, &uttid,
178 DBUS_TYPE_INT32, &reason,
179 DBUS_TYPE_STRING, &err_msg,
182 dbus_message_set_no_reply(msg, TRUE);
184 if (!dbus_connection_send(g_conn_sender, msg, NULL)) {
185 SLOG(LOG_ERROR, tts_tag(), "[Dbus ERROR] <<<< error message : Out Of Memory !");
187 SLOG(LOG_DEBUG, tts_tag(), "<<<< Send error message : uid(%d), reason(%s), uttid(%d), err_msg(%s)",
188 uid, __ttsd_get_error_code(reason), uttid, (NULL == err_msg) ? "NULL" : err_msg);
189 dbus_connection_flush(g_conn_sender);
192 dbus_message_unref(msg);
197 static Eina_Bool listener_event_callback(void* data, Ecore_Fd_Handler *fd_handler)
199 if (NULL == g_conn_listener) return ECORE_CALLBACK_RENEW;
201 dbus_connection_read_write_dispatch(g_conn_listener, 50);
204 DBusMessage* msg = NULL;
205 msg = dbus_connection_pop_message(g_conn_listener);
207 if (true != dbus_connection_get_is_connected(g_conn_listener)) {
208 SLOG(LOG_ERROR, tts_tag(), "[ERROR] Connection is disconnected");
209 return ECORE_CALLBACK_RENEW;
212 /* loop again if we haven't read a message */
214 return ECORE_CALLBACK_RENEW;
218 if (dbus_message_is_method_call(msg, g_service_interface, TTS_METHOD_HELLO)) {
219 ttsd_dbus_server_hello(g_conn_listener, msg);
221 } else if (dbus_message_is_method_call(msg, g_service_interface, TTS_METHOD_HELLO_SYNC)) {
222 ttsd_dbus_server_hello_sync(g_conn_listener, msg);
224 } else if (dbus_message_is_method_call(msg, g_service_interface, TTS_METHOD_INITIALIZE)) {
225 ttsd_dbus_server_initialize(g_conn_listener, msg);
227 } else if (dbus_message_is_method_call(msg, g_service_interface, TTS_METHOD_FINALIZE)) {
228 ttsd_dbus_server_finalize(g_conn_listener, msg);
230 } else if (dbus_message_is_method_call(msg, g_service_interface, TTS_METHOD_GET_SUPPORT_VOICES)) {
231 ttsd_dbus_server_get_support_voices(g_conn_listener, msg);
233 } else if (dbus_message_is_method_call(msg, g_service_interface, TTS_METHOD_GET_CURRENT_VOICE)) {
234 ttsd_dbus_server_get_current_voice(g_conn_listener, msg);
236 } else if (dbus_message_is_method_call(msg, g_service_interface, TTS_METHOD_ADD_QUEUE)) {
237 ttsd_dbus_server_add_text(g_conn_listener, msg);
239 } else if (dbus_message_is_method_call(msg, g_service_interface, TTS_METHOD_PLAY)) {
240 ttsd_dbus_server_play(g_conn_listener, msg);
242 } else if (dbus_message_is_method_call(msg, g_service_interface, TTS_METHOD_STOP)) {
243 ttsd_dbus_server_stop(g_conn_listener, msg);
245 } else if (dbus_message_is_method_call(msg, g_service_interface, TTS_METHOD_PAUSE)) {
246 ttsd_dbus_server_pause(g_conn_listener, msg);
248 } else if (dbus_message_is_method_call(msg, g_service_interface, TTS_METHOD_SET_PRIVATE_DATA)) {
249 ttsd_dbus_server_set_private_data(g_conn_listener, msg);
251 } else if (dbus_message_is_method_call(msg, g_service_interface, TTS_METHOD_GET_PRIVATE_DATA)) {
252 ttsd_dbus_server_get_private_data(g_conn_listener, msg);
254 } else if (dbus_message_is_method_call(msg, g_service_interface, TTS_METHOD_PLAY_PCM)) {
255 ttsd_dbus_server_play_pcm(g_conn_listener, msg);
257 } else if (dbus_message_is_method_call(msg, g_service_interface, TTS_METHOD_STOP_PCM)) {
258 ttsd_dbus_server_stop_pcm(g_conn_listener, msg);
260 } else if (dbus_message_is_method_call(msg, g_service_interface, TTS_METHOD_ADD_PCM)) {
261 ttsd_dbus_server_add_pcm(g_conn_listener, msg);
264 SLOG(LOG_DEBUG, tts_tag(), "Message is NOT valid");
267 /* free the message */
268 dbus_message_unref(msg);
271 return ECORE_CALLBACK_RENEW;
274 void __ttsd_dbus_service_free()
276 if (NULL != g_service_name) {
277 free(g_service_name);
278 g_service_name = NULL;
281 if (NULL != g_service_object) {
282 free(g_service_object);
283 g_service_object = NULL;
286 if (NULL != g_service_interface) {
287 free(g_service_interface);
288 g_service_interface = NULL;
292 void __ttsd_dbus_connection_free()
294 if (NULL != g_conn_listener) {
295 dbus_connection_close(g_conn_listener);
296 dbus_connection_unref(g_conn_listener);
297 g_conn_listener = NULL;
299 if (NULL != g_conn_sender) {
300 dbus_connection_close(g_conn_sender);
301 dbus_connection_unref(g_conn_sender);
302 g_conn_sender = NULL;
306 int ttsd_dbus_open_connection()
308 SLOG(LOG_DEBUG, tts_tag(), "@@@ start dbus open connection");
310 dbus_error_init(&err);
314 /* Create connection for sender */
315 g_conn_sender = dbus_bus_get_private(DBUS_BUS_SYSTEM, &err);
316 if (dbus_error_is_set(&err)) {
317 SLOG(LOG_ERROR, tts_tag(), "[Dbus ERROR] Fail dbus_bus_get : %s", err.message);
318 dbus_error_free(&err);
321 if (NULL == g_conn_sender) {
322 SLOG(LOG_ERROR, tts_tag(), "[Dbus ERROR] Fail to get dbus connection sender");
326 dbus_connection_set_exit_on_disconnect(g_conn_sender, false);
328 /* connect to the bus and check for errors */
329 g_conn_listener = dbus_bus_get_private(DBUS_BUS_SYSTEM, &err);
330 if (dbus_error_is_set(&err)) {
331 SLOG(LOG_ERROR, tts_tag(), "[Dbus ERROR] Fail dbus_bus_get : %s", err.message);
332 dbus_error_free(&err);
333 __ttsd_dbus_connection_free();
337 if (NULL == g_conn_listener) {
338 SLOG(LOG_ERROR, tts_tag(), "[Dbus ERROR] Fail to get dbus connection");
339 __ttsd_dbus_connection_free();
343 dbus_connection_set_exit_on_disconnect(g_conn_listener, false);
345 __ttsd_dbus_service_free();
347 if (TTSD_MODE_SCREEN_READER == ttsd_get_mode()) {
348 g_service_name = (char*)calloc(strlen(TTS_SR_SERVER_SERVICE_NAME) + 1, sizeof(char));
349 g_service_object = (char*)calloc(strlen(TTS_SR_SERVER_SERVICE_OBJECT_PATH) + 1, sizeof(char));
350 g_service_interface = (char*)calloc(strlen(TTS_SR_SERVER_SERVICE_INTERFACE) + 1, sizeof(char));
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();
359 snprintf(g_service_name, strlen(TTS_SR_SERVER_SERVICE_NAME) + 1, "%s", TTS_SR_SERVER_SERVICE_NAME);
360 snprintf(g_service_object, strlen(TTS_SR_SERVER_SERVICE_OBJECT_PATH) + 1, "%s", TTS_SR_SERVER_SERVICE_OBJECT_PATH);
361 snprintf(g_service_interface, strlen(TTS_SR_SERVER_SERVICE_INTERFACE) + 1, "%s", TTS_SR_SERVER_SERVICE_INTERFACE);
362 } else if (TTSD_MODE_NOTIFICATION == ttsd_get_mode()) {
363 g_service_name = (char*)calloc(strlen(TTS_NOTI_SERVER_SERVICE_NAME) + 1, sizeof(char));
364 g_service_object = (char*)calloc(strlen(TTS_NOTI_SERVER_SERVICE_OBJECT_PATH) + 1, sizeof(char));
365 g_service_interface = (char*)calloc(strlen(TTS_NOTI_SERVER_SERVICE_INTERFACE) + 1, sizeof(char));
367 if (NULL == g_service_name || NULL == g_service_object || NULL == g_service_interface) {
368 SLOG(LOG_ERROR, tts_tag(), "[ERROR] Fail to allocate memory");
369 __ttsd_dbus_service_free();
370 __ttsd_dbus_connection_free();
374 snprintf(g_service_name, strlen(TTS_NOTI_SERVER_SERVICE_NAME) + 1, "%s", TTS_NOTI_SERVER_SERVICE_NAME);
375 snprintf(g_service_object, strlen(TTS_NOTI_SERVER_SERVICE_OBJECT_PATH) + 1, "%s", TTS_NOTI_SERVER_SERVICE_OBJECT_PATH);
376 snprintf(g_service_interface, strlen(TTS_NOTI_SERVER_SERVICE_INTERFACE) + 1, "%s", TTS_NOTI_SERVER_SERVICE_INTERFACE);
377 } else if (TTSD_MODE_INTERRUPT == ttsd_get_mode()) {
378 g_service_name = (char*)calloc(strlen(TTS_INTERRUPT_SERVER_SERVICE_NAME) + 1, sizeof(char));
379 g_service_object = (char*)calloc(strlen(TTS_INTERRUPT_SERVER_SERVICE_OBJECT_PATH) + 1, sizeof(char));
380 g_service_interface = (char*)calloc(strlen(TTS_INTERRUPT_SERVER_SERVICE_INTERFACE) + 1, sizeof(char));
382 if (NULL == g_service_name || NULL == g_service_object || NULL == g_service_interface) {
383 SLOG(LOG_ERROR, tts_tag(), "[ERROR] Fail to allocate memory");
384 __ttsd_dbus_service_free();
385 __ttsd_dbus_connection_free();
389 snprintf(g_service_name, strlen(TTS_INTERRUPT_SERVER_SERVICE_NAME) + 1, "%s", TTS_INTERRUPT_SERVER_SERVICE_NAME);
390 snprintf(g_service_object, strlen(TTS_INTERRUPT_SERVER_SERVICE_OBJECT_PATH) + 1, "%s", TTS_INTERRUPT_SERVER_SERVICE_OBJECT_PATH);
391 snprintf(g_service_interface, strlen(TTS_INTERRUPT_SERVER_SERVICE_INTERFACE) + 1, "%s", TTS_INTERRUPT_SERVER_SERVICE_INTERFACE);
393 g_service_name = (char*)calloc(strlen(TTS_SERVER_SERVICE_NAME) + 1, sizeof(char));
394 g_service_object = (char*)calloc(strlen(TTS_SERVER_SERVICE_OBJECT_PATH) + 1, sizeof(char));
395 g_service_interface = (char*)calloc(strlen(TTS_SERVER_SERVICE_INTERFACE) + 1, sizeof(char));
397 if (NULL == g_service_name || NULL == g_service_object || NULL == g_service_interface) {
398 SLOG(LOG_ERROR, tts_tag(), "[ERROR] Fail to allocate memory");
399 __ttsd_dbus_service_free();
400 __ttsd_dbus_connection_free();
404 snprintf(g_service_name, strlen(TTS_SERVER_SERVICE_NAME) + 1, "%s", TTS_SERVER_SERVICE_NAME);
405 snprintf(g_service_object, strlen(TTS_SERVER_SERVICE_OBJECT_PATH) + 1, "%s", TTS_SERVER_SERVICE_OBJECT_PATH);
406 snprintf(g_service_interface, strlen(TTS_SERVER_SERVICE_INTERFACE) + 1, "%s", TTS_SERVER_SERVICE_INTERFACE);
409 /* request our name on the bus and check for errors */
410 ret = dbus_bus_request_name(g_conn_listener, g_service_name, DBUS_NAME_FLAG_REPLACE_EXISTING, &err);
411 if (DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER != ret) {
412 SLOG(LOG_ERROR, tts_tag(), "[Dbus ERROR] Fail to be primary owner");
413 __ttsd_dbus_connection_free();
417 if (dbus_error_is_set(&err)) {
418 SLOG(LOG_ERROR, tts_tag(), "[Dbus ERROR] Fail to request dbus name : %s", err.message);
419 dbus_error_free(&err);
420 __ttsd_dbus_connection_free();
424 /* Flush messages which are received before fd event handler registration */
425 while (DBUS_DISPATCH_DATA_REMAINS == dbus_connection_get_dispatch_status(g_conn_listener)) {
426 listener_event_callback(NULL, NULL);
429 /* add a rule for getting signal */
431 snprintf(rule, 128, "type='method_call',interface='%s'", g_service_interface);
433 /* add a rule for which messages we want to see */
434 dbus_bus_add_match(g_conn_listener, rule, &err);/* see signals from the given interface */
435 dbus_connection_flush(g_conn_listener);
437 if (dbus_error_is_set(&err)) {
438 SLOG(LOG_ERROR, tts_tag(), "[Dbus ERROR] dbus_bus_add_match() : %s", err.message);
439 __ttsd_dbus_connection_free();
444 dbus_connection_get_unix_fd(g_conn_listener, &fd);
446 g_dbus_fd_handler = ecore_main_fd_handler_add(fd, ECORE_FD_READ, (Ecore_Fd_Cb)listener_event_callback, g_conn_listener, NULL, NULL);
447 if (NULL == g_dbus_fd_handler) {
448 SLOG(LOG_ERROR, tts_tag(), "[Dbus ERROR] Fail to get fd handler");
449 __ttsd_dbus_connection_free();
453 SLOG(LOG_DEBUG, tts_tag(), "@@@");
457 int ttsd_dbus_close_connection()
459 SLOG(LOG_DEBUG, tts_tag(), "@@@ start dbus close connection");
461 dbus_error_init(&err);
463 if (NULL != g_dbus_fd_handler) {
464 ecore_main_fd_handler_del(g_dbus_fd_handler);
465 g_dbus_fd_handler = NULL;
468 if (NULL != g_conn_listener) {
469 dbus_bus_release_name(g_conn_listener, g_service_name, &err);
470 if (dbus_error_is_set(&err)) {
471 SLOG(LOG_ERROR, tts_tag(), "[Dbus ERROR] dbus_bus_release_name() : %s", err.message);
472 dbus_error_free(&err);
476 __ttsd_dbus_connection_free();
477 __ttsd_dbus_service_free();
479 SLOG(LOG_DEBUG, tts_tag(), "@@@");