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 int ttsdc_send_hello(int pid, int uid)
30 char service_name[64];
31 memset(service_name, 0, 64);
32 snprintf(service_name, 64, "%s%d", TTS_CLIENT_SERVICE_NAME, pid);
34 char target_if_name[64];
35 snprintf(target_if_name, sizeof(target_if_name), "%s%d", TTS_CLIENT_SERVICE_INTERFACE, pid);
39 /* create a message & check for errors */
40 msg = dbus_message_new_method_call(
42 TTS_CLIENT_SERVICE_OBJECT_PATH,
47 SLOG(LOG_ERROR, TAG_TTSD, "<<<< [Dbus ERROR] Fail to create hello message : uid(%d)", uid);
50 SLOG(LOG_DEBUG, TAG_TTSD, "<<<< [Dbus] Send hello message : uid(%d)", uid);
53 dbus_message_append_args(msg, DBUS_TYPE_INT32, &uid, DBUS_TYPE_INVALID);
56 dbus_error_init(&err);
58 DBusMessage* result_msg;
61 result_msg = dbus_connection_send_with_reply_and_block(g_conn, msg, g_waiting_time, &err);
62 dbus_message_unref(msg);
64 if (NULL != result_msg) {
65 dbus_message_get_args(result_msg, &err, DBUS_TYPE_INT32, &result, DBUS_TYPE_INVALID);
67 if (dbus_error_is_set(&err)) {
68 SLOG(LOG_ERROR, TAG_TTSD, ">>>> [Dbus] Get arguments error (%s)", err.message);
69 dbus_error_free(&err);
73 dbus_message_unref(result_msg);
75 SLOG(LOG_DEBUG, TAG_TTSD, ">>>> [Dbus] Result message is NULL. Client is not available");
82 int ttsdc_send_message(int pid, int uid, int data, char *method)
84 char service_name[64];
85 memset(service_name, 0, 64);
86 snprintf(service_name, 64, "%s%d", TTS_CLIENT_SERVICE_NAME, pid);
88 char target_if_name[64];
89 snprintf(target_if_name, sizeof(target_if_name), "%s%d", TTS_CLIENT_SERVICE_INTERFACE, pid);
93 /* create a message & check for errors */
94 msg = dbus_message_new_method_call(
96 TTS_CLIENT_SERVICE_OBJECT_PATH,
101 SLOG(LOG_ERROR, TAG_TTSD, "[Dbus ERROR] Fail to create message : type(%s), uid(%d)\n", method, uid);
105 dbus_message_append_args(msg, DBUS_TYPE_INT32, &uid, DBUS_TYPE_INT32, &data, DBUS_TYPE_INVALID);
107 /* send the message and flush the connection */
108 if (!dbus_connection_send(g_conn, msg, NULL)) {
109 SLOG(LOG_ERROR, TAG_TTSD, "[Dbus ERROR] <<<< send message : Out Of Memory, type(%s), ifname(%s), uid(%d), data(%d)", method, target_if_name, uid, data);
111 SLOG(LOG_DEBUG, TAG_TTSD, "<<<< send message : type(%s), uid(%d), data(%d)", method, uid, data);
113 dbus_connection_flush(g_conn);
116 dbus_message_unref(msg);
121 int ttsdc_send_utt_start_message(int pid, int uid, int uttid)
123 return ttsdc_send_message(pid, uid, uttid, TTSD_METHOD_UTTERANCE_STARTED);
126 int ttsdc_send_utt_finish_message(int pid, int uid, int uttid)
128 return ttsdc_send_message(pid, uid, uttid, TTSD_METHOD_UTTERANCE_COMPLETED);
131 int ttsdc_send_set_state_message(int pid, int uid, int state)
133 return ttsdc_send_message(pid, uid, state, TTSD_METHOD_SET_STATE);
136 int ttsdc_send_error_message(int pid, int uid, int uttid, int reason)
138 char service_name[64];
139 memset(service_name, 0, 64);
140 snprintf(service_name, 64, "%s%d", TTS_CLIENT_SERVICE_NAME, pid);
142 char target_if_name[128];
143 snprintf(target_if_name, sizeof(target_if_name), "%s%d", TTS_CLIENT_SERVICE_INTERFACE, pid);
147 msg = dbus_message_new_method_call(
149 TTS_CLIENT_SERVICE_OBJECT_PATH,
154 SLOG(LOG_ERROR, TAG_TTSD, "[Dbus ERROR] Fail to create error message : uid(%d)\n", uid);
158 dbus_message_append_args( msg,
159 DBUS_TYPE_INT32, &uid,
160 DBUS_TYPE_INT32, &uttid,
161 DBUS_TYPE_INT32, &reason,
164 if (!dbus_connection_send(g_conn, msg, NULL)) {
165 SLOG(LOG_ERROR, TAG_TTSD, "[Dbus ERROR] <<<< error message : Out Of Memory !\n");
167 SLOG(LOG_DEBUG, TAG_TTSD, "<<<< Send error signal : uid(%d), reason(%d), uttid(%d)", uid, reason, uttid);
168 dbus_connection_flush(g_conn);
171 dbus_message_unref(msg);
177 static Eina_Bool listener_event_callback(void* data, Ecore_Fd_Handler *fd_handler)
179 DBusConnection* conn = (DBusConnection*)data;
181 if (NULL == conn) return ECORE_CALLBACK_RENEW;
183 dbus_connection_read_write_dispatch(conn, 50);
185 DBusMessage* msg = NULL;
186 msg = dbus_connection_pop_message(conn);
188 /* loop again if we haven't read a message */
189 if (NULL == msg || NULL == conn) {
190 return ECORE_CALLBACK_RENEW;
194 if (dbus_message_is_method_call(msg, TTS_SERVER_SERVICE_INTERFACE, TTS_METHOD_HELLO))
195 ttsd_dbus_server_hello(conn, msg);
197 else if( dbus_message_is_method_call(msg, TTS_SERVER_SERVICE_INTERFACE, TTS_METHOD_INITIALIZE) )
198 ttsd_dbus_server_initialize(conn, msg);
200 else if( dbus_message_is_method_call(msg, TTS_SERVER_SERVICE_INTERFACE, TTS_METHOD_FINALIZE) )
201 ttsd_dbus_server_finalize(conn, msg);
203 else if( dbus_message_is_method_call(msg, TTS_SERVER_SERVICE_INTERFACE, TTS_METHOD_GET_SUPPORT_VOICES) )
204 ttsd_dbus_server_get_support_voices(conn, msg);
206 else if( dbus_message_is_method_call(msg, TTS_SERVER_SERVICE_INTERFACE, TTS_METHOD_GET_CURRENT_VOICE) )
207 ttsd_dbus_server_get_current_voice(conn, msg);
209 else if( dbus_message_is_method_call(msg, TTS_SERVER_SERVICE_INTERFACE, TTS_METHOD_ADD_QUEUE) )
210 ttsd_dbus_server_add_text(conn, msg);
212 else if (dbus_message_is_method_call(msg, TTS_SERVER_SERVICE_INTERFACE, TTS_METHOD_PLAY))
213 ttsd_dbus_server_play(conn, msg);
215 else if (dbus_message_is_method_call(msg, TTS_SERVER_SERVICE_INTERFACE, TTS_METHOD_STOP))
216 ttsd_dbus_server_stop(conn, msg);
218 else if (dbus_message_is_method_call(msg, TTS_SERVER_SERVICE_INTERFACE, TTS_METHOD_PAUSE))
219 ttsd_dbus_server_pause(conn, msg);
222 else if (dbus_message_is_method_call(msg, TTS_SERVER_SERVICE_INTERFACE, TTS_SETTING_METHOD_HELLO))
223 ttsd_dbus_server_hello(conn, msg);
225 else if (dbus_message_is_method_call(msg, TTS_SERVER_SERVICE_INTERFACE, TTS_SETTING_METHOD_INITIALIZE) )
226 ttsd_dbus_server_setting_initialize(conn, msg);
228 else if (dbus_message_is_method_call(msg, TTS_SERVER_SERVICE_INTERFACE, TTS_SETTING_METHOD_FINALIZE) )
229 ttsd_dbus_server_setting_finalize(conn, msg);
231 else if (dbus_message_is_method_call(msg, TTS_SERVER_SERVICE_INTERFACE, TTS_SETTING_METHOD_GET_ENGINE_LIST) )
232 ttsd_dbus_server_setting_get_engine_list(conn, msg);
234 else if (dbus_message_is_method_call(msg, TTS_SERVER_SERVICE_INTERFACE, TTS_SETTING_METHOD_GET_ENGINE) )
235 ttsd_dbus_server_setting_get_engine(conn, msg);
237 else if (dbus_message_is_method_call(msg, TTS_SERVER_SERVICE_INTERFACE, TTS_SETTING_METHOD_SET_ENGINE) )
238 ttsd_dbus_server_setting_set_engine(conn, msg);
240 else if (dbus_message_is_method_call(msg, TTS_SERVER_SERVICE_INTERFACE, TTS_SETTING_METHOD_GET_VOICE_LIST) )
241 ttsd_dbus_server_setting_get_voice_list(conn, msg);
243 else if (dbus_message_is_method_call(msg, TTS_SERVER_SERVICE_INTERFACE, TTS_SETTING_METHOD_GET_DEFAULT_VOICE) )
244 ttsd_dbus_server_setting_get_default_voice(conn, msg);
246 else if (dbus_message_is_method_call(msg, TTS_SERVER_SERVICE_INTERFACE, TTS_SETTING_METHOD_SET_DEFAULT_VOICE) )
247 ttsd_dbus_server_setting_set_default_voice(conn, msg);
249 else if (dbus_message_is_method_call(msg, TTS_SERVER_SERVICE_INTERFACE, TTS_SETTING_METHOD_GET_DEFAULT_SPEED) )
250 ttsd_dbus_server_setting_get_speed(conn, msg);
252 else if (dbus_message_is_method_call(msg, TTS_SERVER_SERVICE_INTERFACE, TTS_SETTING_METHOD_SET_DEFAULT_SPEED) )
253 ttsd_dbus_server_setting_set_speed(conn, msg);
255 else if (dbus_message_is_method_call(msg, TTS_SERVER_SERVICE_INTERFACE, TTS_SETTING_METHOD_GET_ENGINE_SETTING) )
256 ttsd_dbus_server_setting_get_engine_setting(conn, msg);
258 else if (dbus_message_is_method_call(msg, TTS_SERVER_SERVICE_INTERFACE, TTS_SETTING_METHOD_SET_ENGINE_SETTING) )
259 ttsd_dbus_server_setting_set_engine_setting(conn, msg);
262 /* free the message */
263 dbus_message_unref(msg);
265 return ECORE_CALLBACK_RENEW;
268 int ttsd_dbus_open_connection()
271 dbus_error_init(&err);
275 /* connect to the bus and check for errors */
276 g_conn = dbus_bus_get(DBUS_BUS_SYSTEM, &err);
278 if (dbus_error_is_set(&err)) {
279 SLOG(LOG_ERROR, TAG_TTSD, "[Dbus ERROR] fail dbus_bus_get : %s\n", err.message);
280 dbus_error_free(&err);
283 if (NULL == g_conn) {
284 SLOG(LOG_ERROR, TAG_TTSD, "[Dbus ERROR] fail to get dbus connection \n" );
288 /* request our name on the bus and check for errors */
289 ret = dbus_bus_request_name(g_conn, TTS_SERVER_SERVICE_NAME, DBUS_NAME_FLAG_REPLACE_EXISTING , &err);
291 if (DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER != ret) {
292 printf("Fail to be primary owner in dbus request. \n");
293 SLOG(LOG_ERROR, TAG_TTSD, "[Dbus ERROR] fail to be primary owner \n");
297 if (dbus_error_is_set(&err)) {
298 SLOG(LOG_ERROR, TAG_TTSD, "[Dbus ERROR] dbus_bus_request_name() : %s \n", err.message);
299 dbus_error_free(&err);
304 /* add a rule for getting signal */
306 snprintf(rule, 128, "type='signal',interface='%s'", TTS_SERVER_SERVICE_INTERFACE);
308 /* add a rule for which messages we want to see */
309 dbus_bus_add_match(g_conn, rule, &err); /* see signals from the given interface */
310 dbus_connection_flush(g_conn);
312 if (dbus_error_is_set(&err)) {
313 SLOG(LOG_ERROR, TAG_TTSD, "[Dbus ERROR] dbus_bus_add_match() : %s \n", err.message);
318 dbus_connection_get_unix_fd(g_conn, &fd);
320 Ecore_Fd_Handler* fd_handler;
321 fd_handler = ecore_main_fd_handler_add(fd, ECORE_FD_READ , (Ecore_Fd_Cb)listener_event_callback, g_conn, NULL, NULL);
323 if (NULL == fd_handler) {
324 SLOG(LOG_ERROR, TAG_TTSD, "[Dbus ERROR] ecore_main_fd_handler_add() : fail to get fd handler \n");
328 SLOG(LOG_DEBUG, TAG_TTSD, "[Dbus SUCCESS] Open connection. ");
332 int ttsd_dbus_close_connection()
335 dbus_error_init(&err);
337 dbus_bus_release_name (g_conn, TTS_SERVER_SERVICE_NAME, &err);
339 if (dbus_error_is_set(&err)) {
340 SLOG(LOG_ERROR, TAG_TTSD, "[Dbus ERROR] dbus_bus_release_name() : %s\n", err.message);
341 dbus_error_free(&err);
345 SLOG(LOG_DEBUG, TAG_TTSD, "[Dbus SUCCESS] Close connection. ");