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_interface;
31 int ttsdc_send_hello(int pid, int uid)
33 char service_name[64];
34 memset(service_name, 0, 64);
35 snprintf(service_name, 64, "%s%d", TTS_CLIENT_SERVICE_NAME, pid);
37 char target_if_name[64];
38 snprintf(target_if_name, sizeof(target_if_name), "%s%d", TTS_CLIENT_SERVICE_INTERFACE, pid);
42 /* create a message & check for errors */
43 msg = dbus_message_new_method_call(
45 TTS_CLIENT_SERVICE_OBJECT_PATH,
50 SLOG(LOG_ERROR, get_tag(), "<<<< [Dbus ERROR] Fail to create hello message : uid(%d)", uid);
53 SLOG(LOG_DEBUG, get_tag(), "<<<< [Dbus] Send hello message : uid(%d)", uid);
56 dbus_message_append_args(msg, DBUS_TYPE_INT32, &uid, DBUS_TYPE_INVALID);
59 dbus_error_init(&err);
61 DBusMessage* result_msg;
64 result_msg = dbus_connection_send_with_reply_and_block(g_conn, msg, g_waiting_time, &err);
65 dbus_message_unref(msg);
67 if (NULL != result_msg) {
68 dbus_message_get_args(result_msg, &err, DBUS_TYPE_INT32, &result, DBUS_TYPE_INVALID);
70 if (dbus_error_is_set(&err)) {
71 SLOG(LOG_ERROR, get_tag(), ">>>> [Dbus] Get arguments error (%s)", err.message);
72 dbus_error_free(&err);
76 dbus_message_unref(result_msg);
78 SLOG(LOG_DEBUG, get_tag(), ">>>> [Dbus] Result message is NULL. Client is not available");
85 int ttsdc_send_message(int pid, int uid, int data, char *method)
87 char service_name[64];
88 memset(service_name, 0, 64);
89 snprintf(service_name, 64, "%s%d", TTS_CLIENT_SERVICE_NAME, pid);
91 char target_if_name[64];
92 snprintf(target_if_name, sizeof(target_if_name), "%s%d", TTS_CLIENT_SERVICE_INTERFACE, pid);
96 /* create a message & check for errors */
97 msg = dbus_message_new_method_call(
99 TTS_CLIENT_SERVICE_OBJECT_PATH,
104 SLOG(LOG_ERROR, get_tag(), "[Dbus ERROR] Fail to create message : type(%s), uid(%d)\n", method, uid);
108 dbus_message_append_args(msg, DBUS_TYPE_INT32, &uid, DBUS_TYPE_INT32, &data, DBUS_TYPE_INVALID);
110 /* send the message and flush the connection */
111 if (!dbus_connection_send(g_conn, msg, NULL)) {
112 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);
114 SLOG(LOG_DEBUG, get_tag(), "<<<< send message : type(%s), uid(%d), data(%d)", method, uid, data);
116 dbus_connection_flush(g_conn);
119 dbus_message_unref(msg);
124 int ttsdc_send_utt_start_message(int pid, int uid, int uttid)
126 return ttsdc_send_message(pid, uid, uttid, TTSD_METHOD_UTTERANCE_STARTED);
129 int ttsdc_send_utt_finish_message(int pid, int uid, int uttid)
131 return ttsdc_send_message(pid, uid, uttid, TTSD_METHOD_UTTERANCE_COMPLETED);
134 int ttsdc_send_set_state_message(int pid, int uid, int state)
136 return ttsdc_send_message(pid, uid, state, TTSD_METHOD_SET_STATE);
139 int ttsdc_send_error_message(int pid, int uid, int uttid, int reason)
141 char service_name[64];
142 memset(service_name, 0, 64);
143 snprintf(service_name, 64, "%s%d", TTS_CLIENT_SERVICE_NAME, pid);
145 char target_if_name[128];
146 snprintf(target_if_name, sizeof(target_if_name), "%s%d", TTS_CLIENT_SERVICE_INTERFACE, pid);
150 msg = dbus_message_new_method_call(
152 TTS_CLIENT_SERVICE_OBJECT_PATH,
157 SLOG(LOG_ERROR, get_tag(), "[Dbus ERROR] Fail to create error message : uid(%d)\n", uid);
161 dbus_message_append_args( msg,
162 DBUS_TYPE_INT32, &uid,
163 DBUS_TYPE_INT32, &uttid,
164 DBUS_TYPE_INT32, &reason,
167 if (!dbus_connection_send(g_conn, msg, NULL)) {
168 SLOG(LOG_ERROR, get_tag(), "[Dbus ERROR] <<<< error message : Out Of Memory !\n");
170 SLOG(LOG_DEBUG, get_tag(), "<<<< Send error signal : uid(%d), reason(%d), uttid(%d)", uid, reason, uttid);
171 dbus_connection_flush(g_conn);
174 dbus_message_unref(msg);
180 static Eina_Bool listener_event_callback(void* data, Ecore_Fd_Handler *fd_handler)
182 DBusConnection* conn = (DBusConnection*)data;
184 if (NULL == conn) return ECORE_CALLBACK_RENEW;
186 dbus_connection_read_write_dispatch(conn, 50);
188 DBusMessage* msg = NULL;
189 msg = dbus_connection_pop_message(conn);
191 /* loop again if we haven't read a message */
192 if (NULL == msg || NULL == conn) {
193 return ECORE_CALLBACK_RENEW;
197 if (dbus_message_is_method_call(msg, g_service_interface, TTS_METHOD_HELLO))
198 ttsd_dbus_server_hello(conn, msg);
200 else if( dbus_message_is_method_call(msg, g_service_interface, TTS_METHOD_INITIALIZE) )
201 ttsd_dbus_server_initialize(conn, msg);
203 else if( dbus_message_is_method_call(msg, g_service_interface, TTS_METHOD_FINALIZE) )
204 ttsd_dbus_server_finalize(conn, msg);
206 else if( dbus_message_is_method_call(msg, g_service_interface, TTS_METHOD_GET_SUPPORT_VOICES) )
207 ttsd_dbus_server_get_support_voices(conn, msg);
209 else if( dbus_message_is_method_call(msg, g_service_interface, TTS_METHOD_GET_CURRENT_VOICE) )
210 ttsd_dbus_server_get_current_voice(conn, msg);
212 else if( dbus_message_is_method_call(msg, g_service_interface, TTS_METHOD_ADD_QUEUE) )
213 ttsd_dbus_server_add_text(conn, msg);
215 else if (dbus_message_is_method_call(msg, g_service_interface, TTS_METHOD_PLAY))
216 ttsd_dbus_server_play(conn, msg);
218 else if (dbus_message_is_method_call(msg, g_service_interface, TTS_METHOD_STOP))
219 ttsd_dbus_server_stop(conn, msg);
221 else if (dbus_message_is_method_call(msg, g_service_interface, TTS_METHOD_PAUSE))
222 ttsd_dbus_server_pause(conn, msg);
225 else if (dbus_message_is_method_call(msg, g_service_interface, TTS_SETTING_METHOD_HELLO))
226 ttsd_dbus_server_hello(conn, msg);
228 else if (dbus_message_is_method_call(msg, g_service_interface, TTS_SETTING_METHOD_INITIALIZE) )
229 ttsd_dbus_server_setting_initialize(conn, msg);
231 else if (dbus_message_is_method_call(msg, g_service_interface, TTS_SETTING_METHOD_FINALIZE) )
232 ttsd_dbus_server_setting_finalize(conn, msg);
234 else if (dbus_message_is_method_call(msg, g_service_interface, TTS_SETTING_METHOD_GET_ENGINE_LIST) )
235 ttsd_dbus_server_setting_get_engine_list(conn, msg);
237 else if (dbus_message_is_method_call(msg, g_service_interface, TTS_SETTING_METHOD_GET_ENGINE) )
238 ttsd_dbus_server_setting_get_engine(conn, msg);
240 else if (dbus_message_is_method_call(msg, g_service_interface, TTS_SETTING_METHOD_SET_ENGINE) )
241 ttsd_dbus_server_setting_set_engine(conn, msg);
243 else if (dbus_message_is_method_call(msg, g_service_interface, TTS_SETTING_METHOD_GET_VOICE_LIST) )
244 ttsd_dbus_server_setting_get_voice_list(conn, msg);
246 else if (dbus_message_is_method_call(msg, g_service_interface, TTS_SETTING_METHOD_GET_DEFAULT_VOICE) )
247 ttsd_dbus_server_setting_get_default_voice(conn, msg);
249 else if (dbus_message_is_method_call(msg, g_service_interface, TTS_SETTING_METHOD_SET_DEFAULT_VOICE) )
250 ttsd_dbus_server_setting_set_default_voice(conn, msg);
252 else if (dbus_message_is_method_call(msg, g_service_interface, TTS_SETTING_METHOD_GET_DEFAULT_SPEED) )
253 ttsd_dbus_server_setting_get_speed(conn, msg);
255 else if (dbus_message_is_method_call(msg, g_service_interface, TTS_SETTING_METHOD_SET_DEFAULT_SPEED) )
256 ttsd_dbus_server_setting_set_speed(conn, msg);
258 else if (dbus_message_is_method_call(msg, g_service_interface, TTS_SETTING_METHOD_GET_ENGINE_SETTING) )
259 ttsd_dbus_server_setting_get_engine_setting(conn, msg);
261 else if (dbus_message_is_method_call(msg, g_service_interface, TTS_SETTING_METHOD_SET_ENGINE_SETTING) )
262 ttsd_dbus_server_setting_set_engine_setting(conn, msg);
265 /* free the message */
266 dbus_message_unref(msg);
268 return ECORE_CALLBACK_RENEW;
271 int ttsd_dbus_open_connection(ttsd_mode_e mode)
274 dbus_error_init(&err);
278 /* connect to the bus and check for errors */
279 g_conn = dbus_bus_get(DBUS_BUS_SYSTEM, &err);
281 if (dbus_error_is_set(&err)) {
282 SLOG(LOG_ERROR, get_tag(), "[Dbus ERROR] fail dbus_bus_get : %s\n", err.message);
283 dbus_error_free(&err);
286 if (NULL == g_conn) {
287 SLOG(LOG_ERROR, get_tag(), "[Dbus ERROR] fail to get dbus connection \n" );
291 if (TTSD_MODE_SCREEN_READER == ttsd_get_mode()) {
292 g_service_name = (char*)malloc(sizeof(char) * strlen(TTS_SR_SERVER_SERVICE_NAME) + 1);
293 g_service_interface = (char*)malloc(sizeof(char) * strlen(TTS_SR_SERVER_SERVICE_INTERFACE) + 1);
295 strcpy(g_service_name, TTS_SR_SERVER_SERVICE_NAME);
296 strcpy(g_service_interface, TTS_SR_SERVER_SERVICE_INTERFACE);
297 } else if (TTSD_MODE_NOTIFICATION == ttsd_get_mode()) {
298 g_service_name = (char*)malloc(sizeof(char) * strlen(TTS_NOTI_SERVER_SERVICE_NAME) + 1);
299 g_service_interface = (char*)malloc(sizeof(char) * strlen(TTS_NOTI_SERVER_SERVICE_INTERFACE) + 1);
301 strcpy(g_service_name, TTS_NOTI_SERVER_SERVICE_NAME);
302 strcpy(g_service_interface, TTS_NOTI_SERVER_SERVICE_INTERFACE);
304 g_service_name = (char*)malloc(sizeof(char) * strlen(TTS_SERVER_SERVICE_NAME) + 1);
305 g_service_interface = (char*)malloc(sizeof(char) * strlen(TTS_SERVER_SERVICE_INTERFACE) + 1);
307 strcpy(g_service_name, TTS_SERVER_SERVICE_NAME);
308 strcpy(g_service_interface, TTS_SERVER_SERVICE_INTERFACE);
311 /* request our name on the bus and check for errors */
312 ret = dbus_bus_request_name(g_conn, g_service_name, DBUS_NAME_FLAG_REPLACE_EXISTING , &err);
314 if (DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER != ret) {
315 printf("Fail to be primary owner in dbus request. \n");
316 SLOG(LOG_ERROR, get_tag(), "[Dbus ERROR] fail to be primary owner \n");
320 if (dbus_error_is_set(&err)) {
321 SLOG(LOG_ERROR, get_tag(), "[Dbus ERROR] dbus_bus_request_name() : %s \n", err.message);
322 dbus_error_free(&err);
327 /* add a rule for getting signal */
329 snprintf(rule, 128, "type='signal',interface='%s'", g_service_interface);
331 /* add a rule for which messages we want to see */
332 dbus_bus_add_match(g_conn, rule, &err); /* see signals from the given interface */
333 dbus_connection_flush(g_conn);
335 if (dbus_error_is_set(&err)) {
336 SLOG(LOG_ERROR, get_tag(), "[Dbus ERROR] dbus_bus_add_match() : %s \n", err.message);
341 dbus_connection_get_unix_fd(g_conn, &fd);
343 Ecore_Fd_Handler* fd_handler;
344 fd_handler = ecore_main_fd_handler_add(fd, ECORE_FD_READ , (Ecore_Fd_Cb)listener_event_callback, g_conn, NULL, NULL);
346 if (NULL == fd_handler) {
347 SLOG(LOG_ERROR, get_tag(), "[Dbus ERROR] ecore_main_fd_handler_add() : fail to get fd handler \n");
351 SLOG(LOG_DEBUG, get_tag(), "[Dbus SUCCESS] Open connection. ");
355 int ttsd_dbus_close_connection()
358 dbus_error_init(&err);
360 dbus_bus_release_name (g_conn, g_service_name, &err);
362 if (dbus_error_is_set(&err)) {
363 SLOG(LOG_ERROR, get_tag(), "[Dbus ERROR] dbus_bus_release_name() : %s\n", err.message);
364 dbus_error_free(&err);
368 if (NULL != g_service_name)
369 free(g_service_name);
371 if (NULL != g_service_interface)
372 free(g_service_interface);
374 SLOG(LOG_DEBUG, get_tag(), "[Dbus SUCCESS] Close connection. ");