2 * Copyright (c) 2011 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 int ttsdc_send_signal(int pid, int uid, int uttid, char *signal)
28 char target_if_name[64];
29 snprintf(target_if_name, sizeof(target_if_name), "%s%d", TTS_CLIENT_SERVICE_INTERFACE, pid);
33 /* create a signal & check for errors */
34 msg = dbus_message_new_signal(
35 TTS_CLIENT_SERVICE_OBJECT_PATH, /* object name of the signal */
36 target_if_name, /* interface name of the signal */
37 signal); /* name of the signal */
40 SLOG(LOG_ERROR, TAG_TTSD, "[Dbus ERROR] Fail to create signal message : type(%s), uid(%d)\n", signal, uid);
44 dbus_message_append_args(msg, DBUS_TYPE_INT32, &uid, DBUS_TYPE_INT32, &uttid, DBUS_TYPE_INVALID);
46 /* send the message and flush the connection */
47 if (!dbus_connection_send(g_conn, msg, NULL)) {
48 SLOG(LOG_ERROR, TAG_TTSD, "[Dbus ERROR] <<<< send signal : Out Of Memory, type(%s), ifname(%s), uid(%d), uttid(%d) \n",signal, target_if_name, uid, uttid);
50 SLOG(LOG_DEBUG, TAG_TTSD, "<<<< send signal : type(%s), uid(%d), uttid(%d) \n", signal, uid, uttid);
52 dbus_connection_flush(g_conn);
54 dbus_message_unref(msg);
59 int ttsdc_send_utt_start_signal(int pid, int uid, int uttid)
61 return ttsdc_send_signal(pid, uid, uttid, TTS_SIGNAL_UTTERANCE_STARTED);
64 int ttsdc_send_utt_finish_signal(int pid, int uid, int uttid)
66 return ttsdc_send_signal(pid, uid, uttid, TTS_SIGNAL_UTTERANCE_COMPLETED);
69 int ttsdc_send_interrupt_signal(int pid, int uid, ttsd_interrupted_code_e code)
71 return ttsdc_send_signal(pid, uid, (int)code, TTS_SIGNAL_INTERRUPT);
74 int ttsdc_send_error_signal(int pid, int uid, int uttid, int reason)
76 char target_if_name[128];
77 snprintf(target_if_name, sizeof(target_if_name), "%s%d", TTS_CLIENT_SERVICE_INTERFACE, pid);
80 msg = dbus_message_new_signal(
81 TTS_CLIENT_SERVICE_OBJECT_PATH, /* object name of the signal */
82 target_if_name, /* interface name of the signal */
83 TTS_SIGNAL_ERROR ); /* name of the signal */
86 SLOG(LOG_ERROR, TAG_TTSD, "[Dbus ERROR] Fail to create error message : uid(%d)\n", uid);
90 dbus_message_append_args( msg,
91 DBUS_TYPE_INT32, &uid,
92 DBUS_TYPE_INT32, &uttid,
93 DBUS_TYPE_INT32, &reason,
96 if (!dbus_connection_send(g_conn, msg, NULL)) {
97 SLOG(LOG_ERROR, TAG_TTSD, "[Dbus ERROR] <<<< Send error signal : Out Of Memory !\n");
99 SLOG(LOG_DEBUG, TAG_TTSD, "<<<< Send error signal : reason(%d), uttid(%d)", reason, uttid);
100 dbus_connection_flush(g_conn);
103 dbus_message_unref(msg);
108 static Eina_Bool listener_event_callback(void* data, Ecore_Fd_Handler *fd_handler)
110 DBusConnection* conn = (DBusConnection*)data;
112 if (NULL == conn) return ECORE_CALLBACK_RENEW;
114 dbus_connection_read_write_dispatch(conn, 50);
116 DBusMessage* msg = NULL;
117 msg = dbus_connection_pop_message(conn);
119 /* loop again if we haven't read a message */
121 return ECORE_CALLBACK_RENEW;
125 if( dbus_message_is_method_call(msg, TTS_SERVER_SERVICE_INTERFACE, TTS_METHOD_INITIALIZE) )
126 ttsd_dbus_server_initialize(conn, msg);
128 else if( dbus_message_is_method_call(msg, TTS_SERVER_SERVICE_INTERFACE, TTS_METHOD_FINALIZE) )
129 ttsd_dbus_server_finalize(conn, msg);
131 else if( dbus_message_is_method_call(msg, TTS_SERVER_SERVICE_INTERFACE, TTS_METHOD_GET_SUPPORT_VOICES) )
132 ttsd_dbus_server_get_support_voices(conn, msg);
134 else if( dbus_message_is_method_call(msg, TTS_SERVER_SERVICE_INTERFACE, TTS_METHOD_GET_CURRENT_VOICE) )
135 ttsd_dbus_server_get_current_voice(conn, msg);
137 else if( dbus_message_is_method_call(msg, TTS_SERVER_SERVICE_INTERFACE, TTS_METHOD_ADD_QUEUE) )
138 ttsd_dbus_server_add_text(conn, msg);
140 else if (dbus_message_is_method_call(msg, TTS_SERVER_SERVICE_INTERFACE, TTS_METHOD_PLAY))
141 ttsd_dbus_server_play(conn, msg);
143 else if (dbus_message_is_method_call(msg, TTS_SERVER_SERVICE_INTERFACE, TTS_METHOD_STOP))
144 ttsd_dbus_server_stop(conn, msg);
146 else if (dbus_message_is_method_call(msg, TTS_SERVER_SERVICE_INTERFACE, TTS_METHOD_PAUSE))
147 ttsd_dbus_server_pause(conn, msg);
150 else if (dbus_message_is_method_call(msg, TTS_SERVER_SERVICE_INTERFACE, TTS_SETTING_METHOD_INITIALIZE) )
151 ttsd_dbus_server_setting_initialize(conn, msg);
153 else if (dbus_message_is_method_call(msg, TTS_SERVER_SERVICE_INTERFACE, TTS_SETTING_METHOD_FINALIZE) )
154 ttsd_dbus_server_setting_finalize(conn, msg);
156 else if (dbus_message_is_method_call(msg, TTS_SERVER_SERVICE_INTERFACE, TTS_SETTING_METHOD_GET_ENGINE_LIST) )
157 ttsd_dbus_server_setting_get_engine_list(conn, msg);
159 else if (dbus_message_is_method_call(msg, TTS_SERVER_SERVICE_INTERFACE, TTS_SETTING_METHOD_GET_ENGINE) )
160 ttsd_dbus_server_setting_get_engine(conn, msg);
162 else if (dbus_message_is_method_call(msg, TTS_SERVER_SERVICE_INTERFACE, TTS_SETTING_METHOD_SET_ENGINE) )
163 ttsd_dbus_server_setting_set_engine(conn, msg);
165 else if (dbus_message_is_method_call(msg, TTS_SERVER_SERVICE_INTERFACE, TTS_SETTING_METHOD_GET_VOICE_LIST) )
166 ttsd_dbus_server_setting_get_voice_list(conn, msg);
168 else if (dbus_message_is_method_call(msg, TTS_SERVER_SERVICE_INTERFACE, TTS_SETTING_METHOD_GET_DEFAULT_VOICE) )
169 ttsd_dbus_server_setting_get_default_voice(conn, msg);
171 else if (dbus_message_is_method_call(msg, TTS_SERVER_SERVICE_INTERFACE, TTS_SETTING_METHOD_SET_DEFAULT_VOICE) )
172 ttsd_dbus_server_setting_set_default_voice(conn, msg);
174 else if (dbus_message_is_method_call(msg, TTS_SERVER_SERVICE_INTERFACE, TTS_SETTING_METHOD_GET_DEFAULT_SPEED) )
175 ttsd_dbus_server_setting_get_speed(conn, msg);
177 else if (dbus_message_is_method_call(msg, TTS_SERVER_SERVICE_INTERFACE, TTS_SETTING_METHOD_SET_DEFAULT_SPEED) )
178 ttsd_dbus_server_setting_set_speed(conn, msg);
180 else if (dbus_message_is_method_call(msg, TTS_SERVER_SERVICE_INTERFACE, TTS_SETTING_METHOD_GET_ENGINE_SETTING) )
181 ttsd_dbus_server_setting_get_engine_setting(conn, msg);
183 else if (dbus_message_is_method_call(msg, TTS_SERVER_SERVICE_INTERFACE, TTS_SETTING_METHOD_SET_ENGINE_SETTING) )
184 ttsd_dbus_server_setting_set_engine_setting(conn, msg);
186 /* daemon internal event*/
187 else if (dbus_message_is_signal(msg, TTS_SERVER_SERVICE_INTERFACE, TTS_SIGNAL_NEXT_PLAY))
188 ttsd_dbus_server_start_next_play(msg);
190 else if (dbus_message_is_signal(msg, TTS_SERVER_SERVICE_INTERFACE, TTS_SIGNAL_NEXT_SYNTHESIS))
191 ttsd_dbus_server_start_next_synthesis(msg);
193 /* free the message */
194 dbus_message_unref(msg);
196 return ECORE_CALLBACK_RENEW;
199 int ttsd_dbus_open_connection()
202 dbus_error_init(&err);
206 /* connect to the bus and check for errors */
207 g_conn = dbus_bus_get(DBUS_BUS_SYSTEM, &err);
209 if (dbus_error_is_set(&err)) {
210 SLOG(LOG_ERROR, TAG_TTSD, "[Dbus ERROR] fail dbus_bus_get : %s\n", err.message);
211 dbus_error_free(&err);
214 if (NULL == g_conn) {
215 SLOG(LOG_ERROR, TAG_TTSD, "[Dbus ERROR] fail to get dbus connection \n" );
219 /* request our name on the bus and check for errors */
220 ret = dbus_bus_request_name(g_conn, TTS_SERVER_SERVICE_NAME, DBUS_NAME_FLAG_REPLACE_EXISTING , &err);
222 if (DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER != ret) {
223 printf("Fail to be primary owner in dbus request. \n");
224 SLOG(LOG_ERROR, TAG_TTSD, "[Dbus ERROR] fail to be primary owner \n");
228 if (dbus_error_is_set(&err)) {
229 SLOG(LOG_ERROR, TAG_TTSD, "[Dbus ERROR] dbus_bus_request_name() : %s \n", err.message);
230 dbus_error_free(&err);
235 /* add a rule for getting signal */
237 snprintf(rule, 128, "type='signal',interface='%s'", TTS_SERVER_SERVICE_INTERFACE);
239 /* add a rule for which messages we want to see */
240 dbus_bus_add_match(g_conn, rule, &err); /* see signals from the given interface */
241 dbus_connection_flush(g_conn);
243 if (dbus_error_is_set(&err)) {
244 SLOG(LOG_ERROR, TAG_TTSD, "[Dbus ERROR] dbus_bus_add_match() : %s \n", err.message);
249 dbus_connection_get_unix_fd(g_conn, &fd);
251 Ecore_Fd_Handler* fd_handler;
252 fd_handler = ecore_main_fd_handler_add(fd, ECORE_FD_READ , (Ecore_Fd_Cb)listener_event_callback, g_conn, NULL, NULL);
254 if (NULL == fd_handler) {
255 SLOG(LOG_ERROR, TAG_TTSD, "[Dbus ERROR] ecore_main_fd_handler_add() : fail to get fd handler \n");
259 SLOG(LOG_DEBUG, TAG_TTSD, "[Dbus SUCCESS] Open connection. ");
263 int ttsd_dbus_close_connection()
266 dbus_error_init(&err);
268 dbus_bus_release_name (g_conn, TTS_SERVER_SERVICE_NAME, &err);
270 if (dbus_error_is_set(&err)) {
271 SLOG(LOG_ERROR, TAG_TTSD, "[Dbus ERROR] dbus_bus_release_name() : %s\n", err.message);
272 dbus_error_free(&err);
276 SLOG(LOG_DEBUG, TAG_TTSD, "[Dbus SUCCESS] Close connection. ");
281 int ttsd_send_start_next_play(int uid)
285 msg = dbus_message_new_signal(
286 TTS_SERVER_SERVICE_OBJECT_PATH, /* object name of the signal */
287 TTS_SERVER_SERVICE_INTERFACE, /* interface name of the signal */
288 TTS_SIGNAL_NEXT_PLAY ); /* name of the signal */
291 SLOG(LOG_ERROR, TAG_TTSD, "[Dbus ERROR] >>>> Fail to make message for 'start next play'");
295 dbus_message_append_args(msg, DBUS_TYPE_INT32, &uid, DBUS_TYPE_INVALID);
297 if (!dbus_connection_send(g_conn, msg, NULL)) {
298 SLOG(LOG_ERROR, TAG_TTSD, "[Dbus ERROR] >>>> Fail to send message for 'start next play'\n");
302 dbus_connection_flush(g_conn);
303 dbus_message_unref(msg);
308 int ttsd_send_start_next_synthesis(int uid)
312 msg = dbus_message_new_signal(
313 TTS_SERVER_SERVICE_OBJECT_PATH, /* object name of the signal */
314 TTS_SERVER_SERVICE_INTERFACE, /* interface name of the signal */
315 TTS_SIGNAL_NEXT_SYNTHESIS ); /* name of the signal */
318 SLOG(LOG_ERROR, TAG_TTSD, "[Dbus ERROR] >>>> Fail to make message for 'start next synthesis'\n");
322 dbus_message_append_args(msg, DBUS_TYPE_INT32, &uid, DBUS_TYPE_INVALID);
324 if (!dbus_connection_send(g_conn, msg, NULL)) {
325 SLOG(LOG_ERROR, TAG_TTSD, "[Dbus ERROR] >>>> Fail to send message for 'start next synthesis'\n");
329 dbus_connection_flush(g_conn);
330 dbus_message_unref(msg);