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)
34 char service_name[64];
35 memset(service_name, 0, 64);
36 snprintf(service_name, 64, "%s%d", TTS_CLIENT_SERVICE_NAME, pid);
38 char target_if_name[64];
39 snprintf(target_if_name, sizeof(target_if_name), "%s%d", TTS_CLIENT_SERVICE_INTERFACE, pid);
43 /* create a message & check for errors */
44 msg = dbus_message_new_method_call(
46 TTS_CLIENT_SERVICE_OBJECT_PATH,
51 SLOG(LOG_ERROR, get_tag(), "<<<< [Dbus ERROR] Fail to create hello message : uid(%d)", uid);
54 SLOG(LOG_DEBUG, get_tag(), "<<<< [Dbus] Send hello message : uid(%d)", uid);
57 dbus_message_append_args(msg, DBUS_TYPE_INT32, &uid, DBUS_TYPE_INVALID);
60 dbus_error_init(&err);
62 DBusMessage* result_msg;
65 result_msg = dbus_connection_send_with_reply_and_block(g_conn, msg, g_waiting_time, &err);
66 dbus_message_unref(msg);
68 if (NULL != result_msg) {
69 dbus_message_get_args(result_msg, &err, DBUS_TYPE_INT32, &result, DBUS_TYPE_INVALID);
71 if (dbus_error_is_set(&err)) {
72 SLOG(LOG_ERROR, get_tag(), ">>>> [Dbus] Get arguments error (%s)", err.message);
73 dbus_error_free(&err);
77 dbus_message_unref(result_msg);
79 SLOG(LOG_DEBUG, get_tag(), ">>>> [Dbus] Result message is NULL. Client is not available");
86 int ttsdc_send_message(int pid, int uid, int data, char *method)
88 char service_name[64];
89 memset(service_name, 0, 64);
90 snprintf(service_name, 64, "%s%d", TTS_CLIENT_SERVICE_NAME, pid);
92 char target_if_name[64];
93 snprintf(target_if_name, sizeof(target_if_name), "%s%d", TTS_CLIENT_SERVICE_INTERFACE, pid);
97 /* create a message & check for errors */
98 msg = dbus_message_new_method_call(
100 TTS_CLIENT_SERVICE_OBJECT_PATH,
105 SLOG(LOG_ERROR, get_tag(), "[Dbus ERROR] Fail to create message : type(%s), uid(%d)\n", method, uid);
109 dbus_message_append_args(msg, DBUS_TYPE_INT32, &uid, DBUS_TYPE_INT32, &data, DBUS_TYPE_INVALID);
111 /* send the message and flush the connection */
112 if (!dbus_connection_send(g_conn, msg, NULL)) {
113 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);
115 SLOG(LOG_DEBUG, get_tag(), "<<<< send message : type(%s), uid(%d), data(%d)", method, uid, data);
117 dbus_connection_flush(g_conn);
120 dbus_message_unref(msg);
125 int ttsdc_send_utt_start_message(int pid, int uid, int uttid)
127 return ttsdc_send_message(pid, uid, uttid, TTSD_METHOD_UTTERANCE_STARTED);
130 int ttsdc_send_utt_finish_message(int pid, int uid, int uttid)
132 return ttsdc_send_message(pid, uid, uttid, TTSD_METHOD_UTTERANCE_COMPLETED);
135 int ttsdc_send_set_state_message(int pid, int uid, int state)
137 return ttsdc_send_message(pid, uid, state, TTSD_METHOD_SET_STATE);
140 int ttsdc_send_error_message(int pid, int uid, int uttid, int reason)
142 char service_name[64];
143 memset(service_name, 0, 64);
144 snprintf(service_name, 64, "%s%d", TTS_CLIENT_SERVICE_NAME, pid);
146 char target_if_name[128];
147 snprintf(target_if_name, sizeof(target_if_name), "%s%d", TTS_CLIENT_SERVICE_INTERFACE, pid);
151 msg = dbus_message_new_method_call(
153 TTS_CLIENT_SERVICE_OBJECT_PATH,
158 SLOG(LOG_ERROR, get_tag(), "[Dbus ERROR] Fail to create error message : uid(%d)\n", uid);
162 dbus_message_append_args( msg,
163 DBUS_TYPE_INT32, &uid,
164 DBUS_TYPE_INT32, &uttid,
165 DBUS_TYPE_INT32, &reason,
168 if (!dbus_connection_send(g_conn, msg, NULL)) {
169 SLOG(LOG_ERROR, get_tag(), "[Dbus ERROR] <<<< error message : Out Of Memory !\n");
171 SLOG(LOG_DEBUG, get_tag(), "<<<< Send error signal : uid(%d), reason(%d), uttid(%d)", uid, reason, uttid);
172 dbus_connection_flush(g_conn);
175 dbus_message_unref(msg);
181 static Eina_Bool listener_event_callback(void* data, Ecore_Fd_Handler *fd_handler)
183 DBusConnection* conn = (DBusConnection*)data;
185 if (NULL == conn) return ECORE_CALLBACK_RENEW;
187 dbus_connection_read_write_dispatch(conn, 50);
189 DBusMessage* msg = NULL;
190 msg = dbus_connection_pop_message(conn);
192 /* loop again if we haven't read a message */
193 if (NULL == msg || NULL == conn) {
194 return ECORE_CALLBACK_RENEW;
198 if (dbus_message_is_method_call(msg, g_service_interface, TTS_METHOD_HELLO))
199 ttsd_dbus_server_hello(conn, msg);
201 else if( dbus_message_is_method_call(msg, g_service_interface, TTS_METHOD_INITIALIZE) )
202 ttsd_dbus_server_initialize(conn, msg);
204 else if( dbus_message_is_method_call(msg, g_service_interface, TTS_METHOD_FINALIZE) )
205 ttsd_dbus_server_finalize(conn, msg);
207 else if( dbus_message_is_method_call(msg, g_service_interface, TTS_METHOD_GET_SUPPORT_VOICES) )
208 ttsd_dbus_server_get_support_voices(conn, msg);
210 else if( dbus_message_is_method_call(msg, g_service_interface, TTS_METHOD_GET_CURRENT_VOICE) )
211 ttsd_dbus_server_get_current_voice(conn, msg);
213 else if( dbus_message_is_method_call(msg, g_service_interface, TTS_METHOD_ADD_QUEUE) )
214 ttsd_dbus_server_add_text(conn, msg);
216 else if (dbus_message_is_method_call(msg, g_service_interface, TTS_METHOD_PLAY))
217 ttsd_dbus_server_play(conn, msg);
219 else if (dbus_message_is_method_call(msg, g_service_interface, TTS_METHOD_STOP))
220 ttsd_dbus_server_stop(conn, msg);
222 else if (dbus_message_is_method_call(msg, g_service_interface, TTS_METHOD_PAUSE))
223 ttsd_dbus_server_pause(conn, msg);
225 /* daemon internal event*/
226 else if (dbus_message_is_signal(msg, g_service_interface, TTSD_SIGNAL_NEXT_SYNTHESIS))
227 ttsd_dbus_server_start_next_synthesis();
230 else if (dbus_message_is_method_call(msg, g_service_interface, TTS_SETTING_METHOD_HELLO))
231 ttsd_dbus_server_hello(conn, msg);
233 else if (dbus_message_is_method_call(msg, g_service_interface, TTS_SETTING_METHOD_INITIALIZE) )
234 ttsd_dbus_server_setting_initialize(conn, msg);
236 else if (dbus_message_is_method_call(msg, g_service_interface, TTS_SETTING_METHOD_FINALIZE) )
237 ttsd_dbus_server_setting_finalize(conn, msg);
239 else if (dbus_message_is_method_call(msg, g_service_interface, TTS_SETTING_METHOD_GET_ENGINE_LIST) )
240 ttsd_dbus_server_setting_get_engine_list(conn, msg);
242 else if (dbus_message_is_method_call(msg, g_service_interface, TTS_SETTING_METHOD_GET_ENGINE) )
243 ttsd_dbus_server_setting_get_engine(conn, msg);
245 else if (dbus_message_is_method_call(msg, g_service_interface, TTS_SETTING_METHOD_SET_ENGINE) )
246 ttsd_dbus_server_setting_set_engine(conn, msg);
248 else if (dbus_message_is_method_call(msg, g_service_interface, TTS_SETTING_METHOD_GET_VOICE_LIST) )
249 ttsd_dbus_server_setting_get_voice_list(conn, msg);
251 else if (dbus_message_is_method_call(msg, g_service_interface, TTS_SETTING_METHOD_GET_DEFAULT_VOICE) )
252 ttsd_dbus_server_setting_get_default_voice(conn, msg);
254 else if (dbus_message_is_method_call(msg, g_service_interface, TTS_SETTING_METHOD_SET_DEFAULT_VOICE) )
255 ttsd_dbus_server_setting_set_default_voice(conn, msg);
257 else if (dbus_message_is_method_call(msg, g_service_interface, TTS_SETTING_METHOD_GET_DEFAULT_SPEED) )
258 ttsd_dbus_server_setting_get_speed(conn, msg);
260 else if (dbus_message_is_method_call(msg, g_service_interface, TTS_SETTING_METHOD_SET_DEFAULT_SPEED) )
261 ttsd_dbus_server_setting_set_speed(conn, msg);
263 else if (dbus_message_is_method_call(msg, g_service_interface, TTS_SETTING_METHOD_GET_ENGINE_SETTING) )
264 ttsd_dbus_server_setting_get_engine_setting(conn, msg);
266 else if (dbus_message_is_method_call(msg, g_service_interface, TTS_SETTING_METHOD_SET_ENGINE_SETTING) )
267 ttsd_dbus_server_setting_set_engine_setting(conn, msg);
270 /* free the message */
271 dbus_message_unref(msg);
273 return ECORE_CALLBACK_RENEW;
276 int ttsd_dbus_open_connection()
279 dbus_error_init(&err);
283 /* connect to the bus and check for errors */
284 g_conn = dbus_bus_get(DBUS_BUS_SYSTEM, &err);
286 if (dbus_error_is_set(&err)) {
287 SLOG(LOG_ERROR, get_tag(), "[Dbus ERROR] fail dbus_bus_get : %s\n", err.message);
288 dbus_error_free(&err);
291 if (NULL == g_conn) {
292 SLOG(LOG_ERROR, get_tag(), "[Dbus ERROR] fail to get dbus connection \n" );
296 if (TTSD_MODE_SCREEN_READER == ttsd_get_mode()) {
297 g_service_name = (char*)malloc(sizeof(char) * strlen(TTS_SR_SERVER_SERVICE_NAME) + 1);
298 g_service_object = (char*)malloc(sizeof(char) * strlen(TTS_SR_SERVER_SERVICE_OBJECT_PATH) + 1);
299 g_service_interface = (char*)malloc(sizeof(char) * strlen(TTS_SR_SERVER_SERVICE_INTERFACE) + 1);
301 strcpy(g_service_name, TTS_SR_SERVER_SERVICE_NAME);
302 strcpy(g_service_object, TTS_SR_SERVER_SERVICE_OBJECT_PATH);
303 strcpy(g_service_interface, TTS_SR_SERVER_SERVICE_INTERFACE);
304 } else if (TTSD_MODE_NOTIFICATION == ttsd_get_mode()) {
305 g_service_name = (char*)malloc(sizeof(char) * strlen(TTS_NOTI_SERVER_SERVICE_NAME) + 1);
306 g_service_object = (char*)malloc(sizeof(char) * strlen(TTS_NOTI_SERVER_SERVICE_OBJECT_PATH) + 1);
307 g_service_interface = (char*)malloc(sizeof(char) * strlen(TTS_NOTI_SERVER_SERVICE_INTERFACE) + 1);
309 strcpy(g_service_name, TTS_NOTI_SERVER_SERVICE_NAME);
310 strcpy(g_service_object, TTS_NOTI_SERVER_SERVICE_OBJECT_PATH);
311 strcpy(g_service_interface, TTS_NOTI_SERVER_SERVICE_INTERFACE);
313 g_service_name = (char*)malloc(sizeof(char) * strlen(TTS_SERVER_SERVICE_NAME) + 1);
314 g_service_object = (char*)malloc(sizeof(char) * strlen(TTS_SERVER_SERVICE_OBJECT_PATH) + 1);
315 g_service_interface = (char*)malloc(sizeof(char) * strlen(TTS_SERVER_SERVICE_INTERFACE) + 1);
317 strcpy(g_service_name, TTS_SERVER_SERVICE_NAME);
318 strcpy(g_service_object, TTS_SERVER_SERVICE_OBJECT_PATH);
319 strcpy(g_service_interface, TTS_SERVER_SERVICE_INTERFACE);
322 /* request our name on the bus and check for errors */
323 ret = dbus_bus_request_name(g_conn, g_service_name, DBUS_NAME_FLAG_REPLACE_EXISTING , &err);
325 if (DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER != ret) {
326 printf("Fail to be primary owner in dbus request. \n");
327 SLOG(LOG_ERROR, get_tag(), "[Dbus ERROR] fail to be primary owner \n");
331 if (dbus_error_is_set(&err)) {
332 SLOG(LOG_ERROR, get_tag(), "[Dbus ERROR] dbus_bus_request_name() : %s \n", err.message);
333 dbus_error_free(&err);
338 /* add a rule for getting signal */
340 snprintf(rule, 128, "type='signal',interface='%s'", g_service_interface);
342 /* add a rule for which messages we want to see */
343 dbus_bus_add_match(g_conn, rule, &err); /* see signals from the given interface */
344 dbus_connection_flush(g_conn);
346 if (dbus_error_is_set(&err)) {
347 SLOG(LOG_ERROR, get_tag(), "[Dbus ERROR] dbus_bus_add_match() : %s \n", err.message);
352 dbus_connection_get_unix_fd(g_conn, &fd);
354 Ecore_Fd_Handler* fd_handler;
355 fd_handler = ecore_main_fd_handler_add(fd, ECORE_FD_READ , (Ecore_Fd_Cb)listener_event_callback, g_conn, NULL, NULL);
357 if (NULL == fd_handler) {
358 SLOG(LOG_ERROR, get_tag(), "[Dbus ERROR] ecore_main_fd_handler_add() : fail to get fd handler \n");
362 SLOG(LOG_DEBUG, get_tag(), "[Dbus SUCCESS] Open connection. ");
366 int ttsd_dbus_close_connection()
369 dbus_error_init(&err);
371 dbus_bus_release_name (g_conn, g_service_name, &err);
373 if (dbus_error_is_set(&err)) {
374 SLOG(LOG_ERROR, get_tag(), "[Dbus ERROR] dbus_bus_release_name() : %s\n", err.message);
375 dbus_error_free(&err);
379 if (NULL != g_service_name)
380 free(g_service_name);
382 if (NULL != g_service_object)
383 free(g_service_object);
385 if (NULL != g_service_interface)
386 free(g_service_interface);
388 SLOG(LOG_DEBUG, get_tag(), "[Dbus SUCCESS] Close connection. ");
393 int ttsd_send_start_next_synthesis()
397 msg = dbus_message_new_signal(
398 g_service_object, /* object name of the signal */
399 g_service_interface, /* interface name of the signal */
400 TTSD_SIGNAL_NEXT_SYNTHESIS); /* name of the signal */
403 SLOG(LOG_ERROR, get_tag(), "[Dbus ERROR] >>>> Fail to make message for 'start next synthesis'");
407 if (!dbus_connection_send(g_conn, msg, NULL)) {
408 SLOG(LOG_ERROR, get_tag(), "[Dbus ERROR] >>>> Fail to send message for 'start next synthesis'");
412 dbus_connection_flush(g_conn);
413 dbus_message_unref(msg);