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_message(int pid, int uid, int uttid, char *method)
28 char service_name[64];
29 memset(service_name, 0, 64);
30 snprintf(service_name, 64, "%s%d", TTS_CLIENT_SERVICE_NAME, pid);
32 char target_if_name[64];
33 snprintf(target_if_name, sizeof(target_if_name), "%s%d", TTS_CLIENT_SERVICE_INTERFACE, pid);
37 /* create a message & check for errors */
38 msg = dbus_message_new_method_call(
40 TTS_CLIENT_SERVICE_OBJECT_PATH,
45 SLOG(LOG_ERROR, TAG_TTSD, "[Dbus ERROR] Fail to create message : type(%s), uid(%d)\n", method, uid);
49 dbus_message_append_args(msg, DBUS_TYPE_INT32, &uid, DBUS_TYPE_INT32, &uttid, DBUS_TYPE_INVALID);
51 /* send the message and flush the connection */
52 if (!dbus_connection_send(g_conn, msg, NULL)) {
53 SLOG(LOG_ERROR, TAG_TTSD, "[Dbus ERROR] <<<< send message : Out Of Memory, type(%s), ifname(%s), uid(%d), uttid(%d)", method, target_if_name, uid, uttid);
55 SLOG(LOG_DEBUG, TAG_TTSD, "<<<< send message : type(%s), uid(%d), uttid(%d)", method, uid, uttid);
57 dbus_connection_flush(g_conn);
60 dbus_message_unref(msg);
65 int ttsdc_send_utt_start_message(int pid, int uid, int uttid)
67 return ttsdc_send_message(pid, uid, uttid, TTS_METHOD_UTTERANCE_STARTED);
70 int ttsdc_send_utt_finish_message(int pid, int uid, int uttid)
72 return ttsdc_send_message(pid, uid, uttid, TTS_METHOD_UTTERANCE_COMPLETED);
75 int ttsdc_send_interrupt_message(int pid, int uid, ttsd_interrupted_code_e code)
77 return ttsdc_send_message(pid, uid, (int)code, TTS_METHOD_INTERRUPT);
80 int ttsdc_send_error_message(int pid, int uid, int uttid, int reason)
82 char service_name[64];
83 memset(service_name, 0, 64);
84 snprintf(service_name, 64, "%s%d", TTS_CLIENT_SERVICE_NAME, pid);
86 char target_if_name[128];
87 snprintf(target_if_name, sizeof(target_if_name), "%s%d", TTS_CLIENT_SERVICE_INTERFACE, pid);
91 msg = dbus_message_new_method_call(
93 TTS_CLIENT_SERVICE_OBJECT_PATH,
98 SLOG(LOG_ERROR, TAG_TTSD, "[Dbus ERROR] Fail to create error message : uid(%d)\n", uid);
102 dbus_message_append_args( msg,
103 DBUS_TYPE_INT32, &uid,
104 DBUS_TYPE_INT32, &uttid,
105 DBUS_TYPE_INT32, &reason,
108 if (!dbus_connection_send(g_conn, msg, NULL)) {
109 SLOG(LOG_ERROR, TAG_TTSD, "[Dbus ERROR] <<<< error message : Out Of Memory !\n");
111 SLOG(LOG_DEBUG, TAG_TTSD, "<<<< Send error signal : uid(%d), reason(%d), uttid(%d)", uid, reason, uttid);
112 dbus_connection_flush(g_conn);
115 dbus_message_unref(msg);
121 static Eina_Bool listener_event_callback(void* data, Ecore_Fd_Handler *fd_handler)
123 DBusConnection* conn = (DBusConnection*)data;
125 if (NULL == conn) return ECORE_CALLBACK_RENEW;
127 dbus_connection_read_write_dispatch(conn, 50);
129 DBusMessage* msg = NULL;
130 msg = dbus_connection_pop_message(conn);
132 /* loop again if we haven't read a message */
134 return ECORE_CALLBACK_RENEW;
138 if (dbus_message_is_method_call(msg, TTS_SERVER_SERVICE_INTERFACE, TTS_METHOD_HELLO))
139 ttsd_dbus_server_hello(conn, msg);
141 else if( dbus_message_is_method_call(msg, TTS_SERVER_SERVICE_INTERFACE, TTS_METHOD_INITIALIZE) )
142 ttsd_dbus_server_initialize(conn, msg);
144 else if( dbus_message_is_method_call(msg, TTS_SERVER_SERVICE_INTERFACE, TTS_METHOD_FINALIZE) )
145 ttsd_dbus_server_finalize(conn, msg);
147 else if( dbus_message_is_method_call(msg, TTS_SERVER_SERVICE_INTERFACE, TTS_METHOD_GET_SUPPORT_VOICES) )
148 ttsd_dbus_server_get_support_voices(conn, msg);
150 else if( dbus_message_is_method_call(msg, TTS_SERVER_SERVICE_INTERFACE, TTS_METHOD_GET_CURRENT_VOICE) )
151 ttsd_dbus_server_get_current_voice(conn, msg);
153 else if( dbus_message_is_method_call(msg, TTS_SERVER_SERVICE_INTERFACE, TTS_METHOD_ADD_QUEUE) )
154 ttsd_dbus_server_add_text(conn, msg);
156 else if (dbus_message_is_method_call(msg, TTS_SERVER_SERVICE_INTERFACE, TTS_METHOD_PLAY))
157 ttsd_dbus_server_play(conn, msg);
159 else if (dbus_message_is_method_call(msg, TTS_SERVER_SERVICE_INTERFACE, TTS_METHOD_STOP))
160 ttsd_dbus_server_stop(conn, msg);
162 else if (dbus_message_is_method_call(msg, TTS_SERVER_SERVICE_INTERFACE, TTS_METHOD_PAUSE))
163 ttsd_dbus_server_pause(conn, msg);
166 else if (dbus_message_is_method_call(msg, TTS_SERVER_SERVICE_INTERFACE, TTS_SETTING_METHOD_HELLO))
167 ttsd_dbus_server_hello(conn, msg);
169 else if (dbus_message_is_method_call(msg, TTS_SERVER_SERVICE_INTERFACE, TTS_SETTING_METHOD_INITIALIZE) )
170 ttsd_dbus_server_setting_initialize(conn, msg);
172 else if (dbus_message_is_method_call(msg, TTS_SERVER_SERVICE_INTERFACE, TTS_SETTING_METHOD_FINALIZE) )
173 ttsd_dbus_server_setting_finalize(conn, msg);
175 else if (dbus_message_is_method_call(msg, TTS_SERVER_SERVICE_INTERFACE, TTS_SETTING_METHOD_GET_ENGINE_LIST) )
176 ttsd_dbus_server_setting_get_engine_list(conn, msg);
178 else if (dbus_message_is_method_call(msg, TTS_SERVER_SERVICE_INTERFACE, TTS_SETTING_METHOD_GET_ENGINE) )
179 ttsd_dbus_server_setting_get_engine(conn, msg);
181 else if (dbus_message_is_method_call(msg, TTS_SERVER_SERVICE_INTERFACE, TTS_SETTING_METHOD_SET_ENGINE) )
182 ttsd_dbus_server_setting_set_engine(conn, msg);
184 else if (dbus_message_is_method_call(msg, TTS_SERVER_SERVICE_INTERFACE, TTS_SETTING_METHOD_GET_VOICE_LIST) )
185 ttsd_dbus_server_setting_get_voice_list(conn, msg);
187 else if (dbus_message_is_method_call(msg, TTS_SERVER_SERVICE_INTERFACE, TTS_SETTING_METHOD_GET_DEFAULT_VOICE) )
188 ttsd_dbus_server_setting_get_default_voice(conn, msg);
190 else if (dbus_message_is_method_call(msg, TTS_SERVER_SERVICE_INTERFACE, TTS_SETTING_METHOD_SET_DEFAULT_VOICE) )
191 ttsd_dbus_server_setting_set_default_voice(conn, msg);
193 else if (dbus_message_is_method_call(msg, TTS_SERVER_SERVICE_INTERFACE, TTS_SETTING_METHOD_GET_DEFAULT_SPEED) )
194 ttsd_dbus_server_setting_get_speed(conn, msg);
196 else if (dbus_message_is_method_call(msg, TTS_SERVER_SERVICE_INTERFACE, TTS_SETTING_METHOD_SET_DEFAULT_SPEED) )
197 ttsd_dbus_server_setting_set_speed(conn, msg);
199 else if (dbus_message_is_method_call(msg, TTS_SERVER_SERVICE_INTERFACE, TTS_SETTING_METHOD_GET_ENGINE_SETTING) )
200 ttsd_dbus_server_setting_get_engine_setting(conn, msg);
202 else if (dbus_message_is_method_call(msg, TTS_SERVER_SERVICE_INTERFACE, TTS_SETTING_METHOD_SET_ENGINE_SETTING) )
203 ttsd_dbus_server_setting_set_engine_setting(conn, msg);
206 else if (dbus_message_is_method_call(msg, TTS_SERVER_SERVICE_INTERFACE, TTS_METHOD_NEXT_PLAY))
207 ttsd_dbus_server_start_next_play(msg);
209 else if (dbus_message_is_method_call(msg, TTS_SERVER_SERVICE_INTERFACE, TTS_METHOD_NEXT_SYNTHESIS))
210 ttsd_dbus_server_start_next_synthesis(msg);
213 /* free the message */
214 dbus_message_unref(msg);
216 return ECORE_CALLBACK_RENEW;
219 int ttsd_dbus_open_connection()
222 dbus_error_init(&err);
226 /* connect to the bus and check for errors */
227 g_conn = dbus_bus_get(DBUS_BUS_SYSTEM, &err);
229 if (dbus_error_is_set(&err)) {
230 SLOG(LOG_ERROR, TAG_TTSD, "[Dbus ERROR] fail dbus_bus_get : %s\n", err.message);
231 dbus_error_free(&err);
234 if (NULL == g_conn) {
235 SLOG(LOG_ERROR, TAG_TTSD, "[Dbus ERROR] fail to get dbus connection \n" );
239 /* request our name on the bus and check for errors */
240 ret = dbus_bus_request_name(g_conn, TTS_SERVER_SERVICE_NAME, DBUS_NAME_FLAG_REPLACE_EXISTING , &err);
242 if (DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER != ret) {
243 printf("Fail to be primary owner in dbus request. \n");
244 SLOG(LOG_ERROR, TAG_TTSD, "[Dbus ERROR] fail to be primary owner \n");
248 if (dbus_error_is_set(&err)) {
249 SLOG(LOG_ERROR, TAG_TTSD, "[Dbus ERROR] dbus_bus_request_name() : %s \n", err.message);
250 dbus_error_free(&err);
255 /* add a rule for getting signal */
257 snprintf(rule, 128, "type='signal',interface='%s'", TTS_SERVER_SERVICE_INTERFACE);
259 /* add a rule for which messages we want to see */
260 dbus_bus_add_match(g_conn, rule, &err); /* see signals from the given interface */
261 dbus_connection_flush(g_conn);
263 if (dbus_error_is_set(&err)) {
264 SLOG(LOG_ERROR, TAG_TTSD, "[Dbus ERROR] dbus_bus_add_match() : %s \n", err.message);
269 dbus_connection_get_unix_fd(g_conn, &fd);
271 Ecore_Fd_Handler* fd_handler;
272 fd_handler = ecore_main_fd_handler_add(fd, ECORE_FD_READ , (Ecore_Fd_Cb)listener_event_callback, g_conn, NULL, NULL);
274 if (NULL == fd_handler) {
275 SLOG(LOG_ERROR, TAG_TTSD, "[Dbus ERROR] ecore_main_fd_handler_add() : fail to get fd handler \n");
279 SLOG(LOG_DEBUG, TAG_TTSD, "[Dbus SUCCESS] Open connection. ");
283 int ttsd_dbus_close_connection()
286 dbus_error_init(&err);
288 dbus_bus_release_name (g_conn, TTS_SERVER_SERVICE_NAME, &err);
290 if (dbus_error_is_set(&err)) {
291 SLOG(LOG_ERROR, TAG_TTSD, "[Dbus ERROR] dbus_bus_release_name() : %s\n", err.message);
292 dbus_error_free(&err);
296 SLOG(LOG_DEBUG, TAG_TTSD, "[Dbus SUCCESS] Close connection. ");
301 int ttsd_send_start_next_play_message(int uid)
305 msg = dbus_message_new_method_call(
306 TTS_SERVER_SERVICE_NAME,
307 TTS_SERVER_SERVICE_OBJECT_PATH,
308 TTS_SERVER_SERVICE_INTERFACE,
309 TTS_METHOD_NEXT_PLAY);
312 SLOG(LOG_ERROR, TAG_TTSD, "[Dbus ERROR] >>>> Fail to make message for 'start next play'");
316 dbus_message_append_args(msg, DBUS_TYPE_INT32, &uid, DBUS_TYPE_INVALID);
318 if (!dbus_connection_send(g_conn, msg, NULL)) {
319 SLOG(LOG_ERROR, TAG_TTSD, "[Dbus ERROR] >>>> Fail to send message for 'start next play'\n");
323 dbus_connection_flush(g_conn);
324 dbus_message_unref(msg);
329 int ttsd_send_start_next_synthesis_message(int uid)
333 msg = dbus_message_new_method_call(
334 TTS_SERVER_SERVICE_NAME,
335 TTS_SERVER_SERVICE_OBJECT_PATH,
336 TTS_SERVER_SERVICE_INTERFACE,
337 TTS_METHOD_NEXT_SYNTHESIS);
340 SLOG(LOG_ERROR, TAG_TTSD, "[Dbus ERROR] >>>> Fail to make message for 'start next synthesis'\n");
344 dbus_message_append_args(msg, DBUS_TYPE_INT32, &uid, DBUS_TYPE_INVALID);
346 if (!dbus_connection_send(g_conn, msg, NULL)) {
347 SLOG(LOG_ERROR, TAG_TTSD, "[Dbus ERROR] >>>> Fail to send message for 'start next synthesis'\n");
351 dbus_connection_flush(g_conn);
352 dbus_message_unref(msg);