2 * Copyright (c) 2012, 2013 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.
15 #include <dbus/dbus.h>
18 #include "ttsd_main.h"
19 #include "ttsd_dbus_server.h"
20 #include "ttsd_dbus.h"
21 #include "ttsd_server.h"
24 static DBusConnection* g_conn;
26 static int g_waiting_time = 3000;
28 static char *g_service_name;
29 static char *g_service_object;
30 static char *g_service_interface;
32 int ttsdc_send_hello(int pid, int uid)
35 SLOG(LOG_ERROR, get_tag(), "[Dbus ERROR] Dbus connection is not available" );
39 char service_name[64];
40 memset(service_name, 0, 64);
41 snprintf(service_name, 64, "%s%d", TTS_CLIENT_SERVICE_NAME, pid);
43 char target_if_name[64];
44 snprintf(target_if_name, sizeof(target_if_name), "%s%d", TTS_CLIENT_SERVICE_INTERFACE, pid);
48 /* create a message & check for errors */
49 msg = dbus_message_new_method_call(
51 TTS_CLIENT_SERVICE_OBJECT_PATH,
56 SLOG(LOG_ERROR, get_tag(), "<<<< [Dbus ERROR] Fail to create hello message : uid(%d)", uid);
59 SLOG(LOG_DEBUG, get_tag(), "<<<< [Dbus] Send hello message : uid(%d)", uid);
62 dbus_message_append_args(msg, DBUS_TYPE_INT32, &uid, DBUS_TYPE_INVALID);
65 dbus_error_init(&err);
67 DBusMessage* result_msg;
70 result_msg = dbus_connection_send_with_reply_and_block(g_conn, msg, g_waiting_time, &err);
71 dbus_message_unref(msg);
73 if (NULL != result_msg) {
74 dbus_message_get_args(result_msg, &err, DBUS_TYPE_INT32, &result, DBUS_TYPE_INVALID);
76 if (dbus_error_is_set(&err)) {
77 SLOG(LOG_ERROR, get_tag(), ">>>> [Dbus] Get arguments error (%s)", err.message);
78 dbus_error_free(&err);
82 dbus_message_unref(result_msg);
84 SLOG(LOG_DEBUG, get_tag(), ">>>> [Dbus] Result message is NULL. Client is not available");
91 int ttsdc_send_message(int pid, int uid, int data, char *method)
95 SLOG(LOG_ERROR, get_tag(), "[Dbus ERROR] Dbus connection is not available" );
99 char service_name[64];
100 memset(service_name, 0, 64);
101 snprintf(service_name, 64, "%s%d", TTS_CLIENT_SERVICE_NAME, pid);
103 char target_if_name[64];
104 snprintf(target_if_name, sizeof(target_if_name), "%s%d", TTS_CLIENT_SERVICE_INTERFACE, pid);
108 /* create a message & check for errors */
109 msg = dbus_message_new_method_call(
111 TTS_CLIENT_SERVICE_OBJECT_PATH,
116 SLOG(LOG_ERROR, get_tag(), "[Dbus ERROR] Fail to create message : type(%s), uid(%d)\n", method, uid);
120 dbus_message_append_args(msg, DBUS_TYPE_INT32, &uid, DBUS_TYPE_INT32, &data, DBUS_TYPE_INVALID);
122 /* send the message and flush the connection */
123 if (!dbus_connection_send(g_conn, msg, NULL)) {
124 SLOG(LOG_ERROR, get_tag(), "[Dbus ERROR] <<<< send message : Out Of Memory, type(%s), ifname(%s), uid(%d), data(%d)", method, target_if_name, uid, data);
126 SLOG(LOG_DEBUG, get_tag(), "<<<< send message : type(%s), uid(%d), data(%d)", method, uid, data);
128 dbus_connection_flush(g_conn);
131 dbus_message_unref(msg);
133 char send_filename[64];
134 memset(send_filename, 0, 64);
135 snprintf(send_filename, 64, "/%s_%d", MESSAGE_FILE_PATH, pid);
138 fp = fopen(send_filename, "a+");
140 SLOG(LOG_ERROR, get_tag(), "[File message ERROR] Fail to open message file");
143 SLOG(LOG_DEBUG, get_tag(), "[File message] Write send file - %s, uid=%d, send_data=%d", method, uid, data);
145 fprintf(fp, "%s %d %d\n", method, uid, data);
153 int ttsdc_send_utt_start_message(int pid, int uid, int uttid)
155 return ttsdc_send_message(pid, uid, uttid, TTSD_METHOD_UTTERANCE_STARTED);
158 int ttsdc_send_utt_finish_message(int pid, int uid, int uttid)
160 return ttsdc_send_message(pid, uid, uttid, TTSD_METHOD_UTTERANCE_COMPLETED);
163 int ttsdc_send_set_state_message(int pid, int uid, int state)
165 return ttsdc_send_message(pid, uid, state, TTSD_METHOD_SET_STATE);
168 int ttsdc_send_error_message(int pid, int uid, int uttid, int reason)
170 if (NULL == g_conn) {
171 SLOG(LOG_ERROR, get_tag(), "[Dbus ERROR] Dbus connection is not available" );
175 char service_name[64];
176 memset(service_name, 0, 64);
177 snprintf(service_name, 64, "%s%d", TTS_CLIENT_SERVICE_NAME, pid);
179 char target_if_name[128];
180 snprintf(target_if_name, sizeof(target_if_name), "%s%d", TTS_CLIENT_SERVICE_INTERFACE, pid);
184 msg = dbus_message_new_method_call(
186 TTS_CLIENT_SERVICE_OBJECT_PATH,
191 SLOG(LOG_ERROR, get_tag(), "[Dbus ERROR] Fail to create error message : uid(%d)\n", uid);
195 dbus_message_append_args( msg,
196 DBUS_TYPE_INT32, &uid,
197 DBUS_TYPE_INT32, &uttid,
198 DBUS_TYPE_INT32, &reason,
201 if (!dbus_connection_send(g_conn, msg, NULL)) {
202 SLOG(LOG_ERROR, get_tag(), "[Dbus ERROR] <<<< error message : Out Of Memory !\n");
204 SLOG(LOG_DEBUG, get_tag(), "<<<< Send error signal : uid(%d), reason(%d), uttid(%d)", uid, reason, uttid);
205 dbus_connection_flush(g_conn);
208 dbus_message_unref(msg);
214 int ttsd_send_start_next_synthesis()
216 if (NULL == g_conn) {
217 SLOG(LOG_ERROR, get_tag(), "[Dbus ERROR] Dbus connection is not available" );
223 msg = dbus_message_new_signal(
224 g_service_object, /* object name of the signal */
225 g_service_interface, /* interface name of the signal */
226 TTSD_SIGNAL_NEXT_SYNTHESIS); /* name of the signal */
229 SLOG(LOG_ERROR, get_tag(), "[Dbus ERROR] >>>> Fail to make message for 'start next synthesis'");
233 if (!dbus_connection_send(g_conn, msg, NULL)) {
234 SLOG(LOG_ERROR, get_tag(), "[Dbus ERROR] >>>> Fail to send message for 'start next synthesis'");
238 SLOG(LOG_DEBUG, get_tag(), "[Dbus] >>>> Send message for 'start next synthesis'");
240 dbus_connection_flush(g_conn);
241 dbus_message_unref(msg);
247 static Eina_Bool listener_event_callback(void* data, Ecore_Fd_Handler *fd_handler)
249 DBusConnection* conn = (DBusConnection*)data;
251 if (NULL == conn) return ECORE_CALLBACK_RENEW;
253 dbus_connection_read_write_dispatch(conn, 50);
255 DBusMessage* msg = NULL;
256 msg = dbus_connection_pop_message(conn);
258 /* loop again if we haven't read a message */
259 if (NULL == msg || NULL == conn) {
260 return ECORE_CALLBACK_RENEW;
264 if (dbus_message_is_method_call(msg, g_service_interface, TTS_METHOD_HELLO))
265 ttsd_dbus_server_hello(conn, msg);
267 else if( dbus_message_is_method_call(msg, g_service_interface, TTS_METHOD_INITIALIZE) )
268 ttsd_dbus_server_initialize(conn, msg);
270 else if( dbus_message_is_method_call(msg, g_service_interface, TTS_METHOD_FINALIZE) )
271 ttsd_dbus_server_finalize(conn, msg);
273 else if( dbus_message_is_method_call(msg, g_service_interface, TTS_METHOD_GET_SUPPORT_VOICES) )
274 ttsd_dbus_server_get_support_voices(conn, msg);
276 else if( dbus_message_is_method_call(msg, g_service_interface, TTS_METHOD_GET_CURRENT_VOICE) )
277 ttsd_dbus_server_get_current_voice(conn, msg);
279 else if( dbus_message_is_method_call(msg, g_service_interface, TTS_METHOD_ADD_QUEUE) )
280 ttsd_dbus_server_add_text(conn, msg);
282 else if (dbus_message_is_method_call(msg, g_service_interface, TTS_METHOD_PLAY))
283 ttsd_dbus_server_play(conn, msg);
285 else if (dbus_message_is_method_call(msg, g_service_interface, TTS_METHOD_STOP))
286 ttsd_dbus_server_stop(conn, msg);
288 else if (dbus_message_is_method_call(msg, g_service_interface, TTS_METHOD_PAUSE))
289 ttsd_dbus_server_pause(conn, msg);
291 /* daemon internal event*/
292 else if (dbus_message_is_signal(msg, g_service_interface, TTSD_SIGNAL_NEXT_SYNTHESIS))
293 ttsd_dbus_server_start_next_synthesis();
296 /* daemon internal event*/
297 else if (dbus_message_is_signal(msg, g_service_interface, TTSD_SIGNAL_NEXT_SYNTHESIS))
298 ttsd_dbus_server_start_next_synthesis();
301 else if (dbus_message_is_method_call(msg, g_service_interface, TTS_SETTING_METHOD_HELLO))
302 ttsd_dbus_server_hello(conn, msg);
304 else if (dbus_message_is_method_call(msg, g_service_interface, TTS_SETTING_METHOD_INITIALIZE) )
305 ttsd_dbus_server_setting_initialize(conn, msg);
307 else if (dbus_message_is_method_call(msg, g_service_interface, TTS_SETTING_METHOD_FINALIZE) )
308 ttsd_dbus_server_setting_finalize(conn, msg);
310 else if (dbus_message_is_method_call(msg, g_service_interface, TTS_SETTING_METHOD_GET_ENGINE_LIST) )
311 ttsd_dbus_server_setting_get_engine_list(conn, msg);
313 else if (dbus_message_is_method_call(msg, g_service_interface, TTS_SETTING_METHOD_GET_ENGINE) )
314 ttsd_dbus_server_setting_get_engine(conn, msg);
316 else if (dbus_message_is_method_call(msg, g_service_interface, TTS_SETTING_METHOD_SET_ENGINE) )
317 ttsd_dbus_server_setting_set_engine(conn, msg);
319 else if (dbus_message_is_method_call(msg, g_service_interface, TTS_SETTING_METHOD_GET_VOICE_LIST) )
320 ttsd_dbus_server_setting_get_voice_list(conn, msg);
322 else if (dbus_message_is_method_call(msg, g_service_interface, TTS_SETTING_METHOD_GET_DEFAULT_VOICE) )
323 ttsd_dbus_server_setting_get_default_voice(conn, msg);
325 else if (dbus_message_is_method_call(msg, g_service_interface, TTS_SETTING_METHOD_SET_DEFAULT_VOICE) )
326 ttsd_dbus_server_setting_set_default_voice(conn, msg);
328 else if (dbus_message_is_method_call(msg, g_service_interface, TTS_SETTING_METHOD_GET_DEFAULT_SPEED) )
329 ttsd_dbus_server_setting_get_speed(conn, msg);
331 else if (dbus_message_is_method_call(msg, g_service_interface, TTS_SETTING_METHOD_SET_DEFAULT_SPEED) )
332 ttsd_dbus_server_setting_set_speed(conn, msg);
334 else if (dbus_message_is_method_call(msg, g_service_interface, TTS_SETTING_METHOD_GET_ENGINE_SETTING) )
335 ttsd_dbus_server_setting_get_engine_setting(conn, msg);
337 else if (dbus_message_is_method_call(msg, g_service_interface, TTS_SETTING_METHOD_SET_ENGINE_SETTING) )
338 ttsd_dbus_server_setting_set_engine_setting(conn, msg);
341 /* free the message */
342 dbus_message_unref(msg);
344 return ECORE_CALLBACK_RENEW;
347 int ttsd_dbus_open_connection()
350 dbus_error_init(&err);
354 /* connect to the bus and check for errors */
355 g_conn = dbus_bus_get(DBUS_BUS_SYSTEM, &err);
357 if (dbus_error_is_set(&err)) {
358 SLOG(LOG_ERROR, get_tag(), "[Dbus ERROR] fail dbus_bus_get : %s\n", err.message);
359 dbus_error_free(&err);
362 if (NULL == g_conn) {
363 SLOG(LOG_ERROR, get_tag(), "[Dbus ERROR] fail to get dbus connection \n" );
367 if (TTSD_MODE_SCREEN_READER == ttsd_get_mode()) {
368 g_service_name = (char*)malloc(sizeof(char) * strlen(TTS_SR_SERVER_SERVICE_NAME) + 1);
369 g_service_object = (char*)malloc(sizeof(char) * strlen(TTS_SR_SERVER_SERVICE_OBJECT_PATH) + 1);
370 g_service_interface = (char*)malloc(sizeof(char) * strlen(TTS_SR_SERVER_SERVICE_INTERFACE) + 1);
372 strcpy(g_service_name, TTS_SR_SERVER_SERVICE_NAME);
373 strcpy(g_service_object, TTS_SR_SERVER_SERVICE_OBJECT_PATH);
374 strcpy(g_service_interface, TTS_SR_SERVER_SERVICE_INTERFACE);
375 } else if (TTSD_MODE_NOTIFICATION == ttsd_get_mode()) {
376 g_service_name = (char*)malloc(sizeof(char) * strlen(TTS_NOTI_SERVER_SERVICE_NAME) + 1);
377 g_service_object = (char*)malloc(sizeof(char) * strlen(TTS_NOTI_SERVER_SERVICE_OBJECT_PATH) + 1);
378 g_service_interface = (char*)malloc(sizeof(char) * strlen(TTS_NOTI_SERVER_SERVICE_INTERFACE) + 1);
380 strcpy(g_service_name, TTS_NOTI_SERVER_SERVICE_NAME);
381 strcpy(g_service_object, TTS_NOTI_SERVER_SERVICE_OBJECT_PATH);
382 strcpy(g_service_interface, TTS_NOTI_SERVER_SERVICE_INTERFACE);
384 g_service_name = (char*)malloc(sizeof(char) * strlen(TTS_SERVER_SERVICE_NAME) + 1);
385 g_service_object = (char*)malloc(sizeof(char) * strlen(TTS_SERVER_SERVICE_OBJECT_PATH) + 1);
386 g_service_interface = (char*)malloc(sizeof(char) * strlen(TTS_SERVER_SERVICE_INTERFACE) + 1);
388 strcpy(g_service_name, TTS_SERVER_SERVICE_NAME);
389 strcpy(g_service_object, TTS_SERVER_SERVICE_OBJECT_PATH);
390 strcpy(g_service_interface, TTS_SERVER_SERVICE_INTERFACE);
393 /* request our name on the bus and check for errors */
394 ret = dbus_bus_request_name(g_conn, g_service_name, DBUS_NAME_FLAG_REPLACE_EXISTING , &err);
396 if (DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER != ret) {
397 printf("Fail to be primary owner in dbus request. \n");
398 SLOG(LOG_ERROR, get_tag(), "[Dbus ERROR] fail to be primary owner \n");
402 if (dbus_error_is_set(&err)) {
403 SLOG(LOG_ERROR, get_tag(), "[Dbus ERROR] dbus_bus_request_name() : %s \n", err.message);
404 dbus_error_free(&err);
409 /* add a rule for getting signal */
411 snprintf(rule, 128, "type='signal',interface='%s'", g_service_interface);
413 /* add a rule for which messages we want to see */
414 dbus_bus_add_match(g_conn, rule, &err); /* see signals from the given interface */
415 dbus_connection_flush(g_conn);
417 if (dbus_error_is_set(&err)) {
418 SLOG(LOG_ERROR, get_tag(), "[Dbus ERROR] dbus_bus_add_match() : %s \n", err.message);
423 dbus_connection_get_unix_fd(g_conn, &fd);
425 Ecore_Fd_Handler* fd_handler;
426 fd_handler = ecore_main_fd_handler_add(fd, ECORE_FD_READ , (Ecore_Fd_Cb)listener_event_callback, g_conn, NULL, NULL);
428 if (NULL == fd_handler) {
429 SLOG(LOG_ERROR, get_tag(), "[Dbus ERROR] ecore_main_fd_handler_add() : fail to get fd handler \n");
433 SLOG(LOG_DEBUG, get_tag(), "[Dbus SUCCESS] Open connection. ");
437 int ttsd_dbus_close_connection()
440 dbus_error_init(&err);
442 dbus_bus_release_name (g_conn, g_service_name, &err);
444 if (dbus_error_is_set(&err)) {
445 SLOG(LOG_ERROR, get_tag(), "[Dbus ERROR] dbus_bus_release_name() : %s\n", err.message);
446 dbus_error_free(&err);
450 if (NULL != g_service_name)
451 free(g_service_name);
453 if (NULL != g_service_object)
454 free(g_service_object);
456 if (NULL != g_service_interface)
457 free(g_service_interface);
459 SLOG(LOG_DEBUG, get_tag(), "[Dbus SUCCESS] Close connection. ");
464 int ttsd_file_msg_open_connection(int pid)
466 /* Make file for Inotify */
467 char send_filename[64];
468 memset(send_filename, 0, 64);
469 snprintf(send_filename, 64, "%s_%d", MESSAGE_FILE_PATH, pid);
472 fp = fopen(send_filename, "a+");
474 SLOG(LOG_ERROR, get_tag(), "[File message ERROR] Fail to make message file");
477 SLOG(LOG_DEBUG, get_tag(), "[File message] Make message file");
482 int ttsd_file_msg_close_connection(int pid)
484 /* delete inotify file */
487 snprintf(path, 64, "%s_%d", MESSAGE_FILE_PATH, pid);
488 if (0 == access(path, R_OK)) {
489 if (0 != remove(path)) {
490 SLOG(LOG_WARN, get_tag(), "[File message WARN] Fail to remove message file");
493 SLOG(LOG_DEBUG, get_tag(), "[File message] Delete message file");