2 * Copyright (c) 2011-2016 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.
14 #include <dbus/dbus.h>
19 #include "ttsd_main.h"
20 #include "ttsd_dbus_server.h"
21 #include "ttsd_dbus.h"
23 static DBusConnection* g_conn_sender = NULL;
24 static DBusConnection* g_conn_listener = NULL;
26 static Ecore_Fd_Handler* g_dbus_fd_handler = NULL;
28 //static int g_waiting_time = 3000;
30 static char *g_service_name = NULL;
31 static char *g_service_object = NULL;
32 static char *g_service_interface = NULL;
34 const char* __ttsd_get_error_code(ttsd_error_e err)
37 case TTSD_ERROR_NONE: return "TTS_ERROR_NONE";
38 case TTSD_ERROR_OUT_OF_MEMORY: return "TTS_ERROR_OUT_OF_MEMORY";
39 case TTSD_ERROR_IO_ERROR: return "TTS_ERROR_IO_ERROR";
40 case TTSD_ERROR_INVALID_PARAMETER: return "TTS_ERROR_INVALID_PARAMETER";
41 case TTSD_ERROR_OUT_OF_NETWORK: return "TTS_ERROR_OUT_OF_NETWORK";
42 case TTSD_ERROR_INVALID_STATE: return "TTS_ERROR_INVALID_STATE";
43 case TTSD_ERROR_INVALID_VOICE: return "TTS_ERROR_INVALID_VOICE";
44 case TTSD_ERROR_ENGINE_NOT_FOUND: return "TTS_ERROR_ENGINE_NOT_FOUND";
45 case TTSD_ERROR_TIMED_OUT: return "TTS_ERROR_TIMED_OUT";
46 case TTSD_ERROR_OPERATION_FAILED: return "TTS_ERROR_OPERATION_FAILED";
47 case TTSD_ERROR_AUDIO_POLICY_BLOCKED: return "TTS_ERROR_AUDIO_POLICY_BLOCKED";
48 case TTSD_ERROR_NOT_SUPPORTED_FEATURE: return "TTSD_ERROR_NOT_SUPPORTED_FEATURE";
49 case TTSD_ERROR_SERVICE_RESET: return "TTSD_ERROR_SERVICE_RESET";
51 return "Invalid error code";
57 int ttsdc_dbus_send_hello(int pid, unsigned int uid, int ret, int service_state, int credential_needed)
59 if (NULL == g_conn_sender) {
60 SLOG(LOG_ERROR, tts_tag(), "[Dbus ERROR] Dbus connection is not available");
65 char target_if_name[64];
66 snprintf(target_if_name, sizeof(target_if_name), "%s%d", TTS_CLIENT_SERVICE_INTERFACE, pid);
68 /* create a message */
69 msg = dbus_message_new_signal(
70 TTS_CLIENT_SERVICE_OBJECT_PATH, /* object name of the signal */
71 target_if_name, /* interface name of the signal */
72 TTSD_METHOD_HELLO); /* name of the signal */
75 SLOG(LOG_ERROR, tts_tag(), "<<<< [Dbus ERROR] Fail to create hello message : uid(%u), ret(%d), credential_needed(%d)", uid, ret, credential_needed);
78 SLOG(LOG_INFO, tts_tag(), "<<<< [Dbus] Send hello message : uid(%u), ret(%d), credential_needed(%d)", uid, ret, credential_needed);
81 dbus_message_append_args(msg,
82 DBUS_TYPE_UINT32, &uid,
83 DBUS_TYPE_INT32, &ret,
84 DBUS_TYPE_INT32, &service_state,
85 DBUS_TYPE_INT32, &credential_needed,
88 if (1 != dbus_connection_send(g_conn_sender, msg, NULL)) {
89 SLOG(LOG_ERROR, tts_tag(), "[Dbus ERROR] Fail to Send");
90 return TTSD_ERROR_OPERATION_FAILED;
92 SLOG(LOG_INFO, tts_tag(), "[Dbus] SUCCESS Send");
93 dbus_connection_flush(g_conn_sender);
96 dbus_message_unref(msg);
100 static int __send_message(int pid, unsigned int uid, int data, const char *method)
102 if (NULL == g_conn_sender) {
103 SLOG(LOG_ERROR, tts_tag(), "[Dbus ERROR] Dbus connection is not available");
107 DBusMessage* msg = NULL;
109 char target_if_name[64];
110 snprintf(target_if_name, sizeof(target_if_name), "%s%d", TTS_CLIENT_SERVICE_INTERFACE, pid);
112 /* create a message */
113 msg = dbus_message_new_signal(
114 TTS_CLIENT_SERVICE_OBJECT_PATH, /* object name of the signal */
115 target_if_name, /* interface name of the signal */
116 method); /* name of the signal */
119 SLOG(LOG_ERROR, tts_tag(), "[Dbus ERROR] Fail to create message : %s", method);
122 SLOG(LOG_DEBUG, tts_tag(), "[Dbus] Send %s message : uid(%u) data(%d)", method, uid, data);
125 dbus_message_append_args(msg, DBUS_TYPE_UINT32, &uid, DBUS_TYPE_INT32, &data, DBUS_TYPE_INVALID);
127 if (1 != dbus_connection_send(g_conn_sender, msg, NULL)) {
128 SLOG(LOG_ERROR, tts_tag(), "[Dbus ERROR] Fail to Send");
131 SLOG(LOG_DEBUG, tts_tag(), "[Dbus] SUCCESS Send");
132 dbus_connection_flush(g_conn_sender);
135 dbus_message_unref(msg);
140 int ttsdc_dbus_send_utt_start_message(int pid, unsigned int uid, int uttid)
142 return __send_message(pid, uid, uttid, TTSD_METHOD_UTTERANCE_STARTED);
145 int ttsdc_dbus_send_utt_finish_message(int pid, unsigned int uid, int uttid)
147 return __send_message(pid, uid, uttid, TTSD_METHOD_UTTERANCE_COMPLETED);
150 int ttsdc_dbus_send_set_state_message(int pid, unsigned int uid, int state)
152 return __send_message(pid, uid, state, TTSD_METHOD_SET_STATE);
155 int ttsdc_dbus_send_set_service_state_message(int pid, unsigned int uid, int before_state, int current_state)
157 if (NULL == g_conn_sender) {
158 SLOG(LOG_ERROR, tts_tag(), "[Dbus ERROR] Dbus connection is not available");
159 return TTSD_ERROR_OPERATION_FAILED;
162 DBusMessage* msg = NULL;
164 char target_if_name[64];
165 snprintf(target_if_name, sizeof(target_if_name), "%s%d", TTS_CLIENT_SERVICE_INTERFACE, pid);
167 /* create a message */
168 msg = dbus_message_new_signal(
169 TTS_CLIENT_SERVICE_OBJECT_PATH, /* object name of the signal */
170 target_if_name, /* interface name of the signal */
171 TTSD_METHOD_SET_SERVICE_STATE); /* name of the signal */
174 SLOG(LOG_ERROR, tts_tag(), "[Dbus ERROR] Fail to create message : %s", TTSD_METHOD_SET_SERVICE_STATE);
175 return TTSD_ERROR_OPERATION_FAILED;
177 SLOG(LOG_DEBUG, tts_tag(), "[Dbus] Send set service state message : uid(%u) before_state(%d), current_state(%d)", uid, before_state, current_state);
180 dbus_message_append_args(msg, DBUS_TYPE_UINT32, &uid, DBUS_TYPE_INT32, &before_state, DBUS_TYPE_INT32, ¤t_state, DBUS_TYPE_INVALID);
182 if (1 != dbus_connection_send(g_conn_sender, msg, NULL)) {
183 SLOG(LOG_ERROR, tts_tag(), "[Dbus ERROR] Fail to Send");
184 return TTSD_ERROR_OPERATION_FAILED;
186 SLOG(LOG_DEBUG, tts_tag(), "[Dbus] SUCCESS Send");
187 dbus_connection_flush(g_conn_sender);
190 dbus_message_unref(msg);
192 return TTSD_ERROR_NONE;
195 int ttsdc_dbus_send_pcm(int pid, unsigned int uid, int uttid, int event, const void* pcm_data, int pcm_data_size, ttse_audio_type_e audio_type, int sample_rate)
197 if (NULL == g_conn_sender) {
198 SLOG(LOG_ERROR, tts_tag(), "[Dbus ERROR] Dbus connection is not available");
202 DBusMessage* msg = NULL;
204 char target_if_name[64];
205 snprintf(target_if_name, sizeof(target_if_name), "%s%d", TTS_CLIENT_SERVICE_INTERFACE, pid);
207 /* create a message */
208 msg = dbus_message_new_signal(
209 TTS_CLIENT_SERVICE_OBJECT_PATH, /* object name of the signal */
210 target_if_name, /* interface name of the signal */
211 TTSD_METHOD_SEND_PCM); /* name of the signal */
214 SLOG(LOG_ERROR, tts_tag(), "[Dbus ERROR] Fail to create error message : uid(%u)", uid);
218 dbus_message_append_args(msg,
219 DBUS_TYPE_UINT32, &uid,
220 DBUS_TYPE_INT32, &uttid,
221 DBUS_TYPE_INT32, &event,
222 DBUS_TYPE_ARRAY, DBUS_TYPE_BYTE, &pcm_data, pcm_data_size,
223 DBUS_TYPE_INT32, &audio_type,
224 DBUS_TYPE_INT32, &sample_rate,
227 if (1 != dbus_connection_send(g_conn_sender, msg, NULL)) {
228 SLOG(LOG_ERROR, tts_tag(), "[Dbus ERROR] Fail to Send");
229 return TTSD_ERROR_OPERATION_FAILED;
231 SLOG(LOG_DEBUG, tts_tag(), "[Dbus] SUCCESS Send");
232 dbus_connection_flush(g_conn_sender);
235 dbus_message_unref(msg);
240 int ttsdc_dbus_send_error_message(int pid, unsigned int uid, int uttid, int reason, char* err_msg)
242 if (NULL == g_conn_sender) {
243 SLOG(LOG_ERROR, tts_tag(), "[Dbus ERROR] Dbus connection is not available");
247 DBusMessage* msg = NULL;
249 char target_if_name[64];
250 snprintf(target_if_name, sizeof(target_if_name), "%s%d", TTS_CLIENT_SERVICE_INTERFACE, pid);
252 /* create a message */
253 msg = dbus_message_new_signal(
254 TTS_CLIENT_SERVICE_OBJECT_PATH, /* object name of the signal */
255 target_if_name, /* interface name of the signal */
256 TTSD_METHOD_ERROR); /* name of the signal */
259 SLOG(LOG_ERROR, tts_tag(), "[Dbus ERROR] Fail to create error message : uid(%u)", uid);
263 dbus_message_append_args(msg,
264 DBUS_TYPE_UINT32, &uid,
265 DBUS_TYPE_INT32, &uttid,
266 DBUS_TYPE_INT32, &reason,
267 DBUS_TYPE_STRING, &err_msg,
270 dbus_message_set_no_reply(msg, TRUE);
272 if (!dbus_connection_send(g_conn_sender, msg, NULL)) {
273 SLOG(LOG_ERROR, tts_tag(), "[Dbus ERROR] <<<< error message : Out Of Memory !");
275 SLOG(LOG_DEBUG, tts_tag(), "<<<< Send error message : uid(%u), reason(%s), uttid(%d), err_msg(%s)",
276 uid, __ttsd_get_error_code(reason), uttid, (NULL == err_msg) ? "NULL" : err_msg);
277 dbus_connection_flush(g_conn_sender);
280 dbus_message_unref(msg);
285 static Eina_Bool listener_event_callback(void* data, Ecore_Fd_Handler *fd_handler)
287 if (NULL == g_conn_listener) return ECORE_CALLBACK_RENEW;
289 dbus_connection_read_write_dispatch(g_conn_listener, 50);
292 DBusMessage* msg = NULL;
293 msg = dbus_connection_pop_message(g_conn_listener);
295 if (true != dbus_connection_get_is_connected(g_conn_listener)) {
296 SLOG(LOG_ERROR, tts_tag(), "[ERROR] Connection is disconnected");
297 return ECORE_CALLBACK_RENEW;
300 /* loop again if we haven't read a message */
302 return ECORE_CALLBACK_RENEW;
306 if (dbus_message_is_method_call(msg, g_service_interface, TTS_METHOD_HELLO)) {
307 ttsd_dbus_server_hello(g_conn_listener, msg);
309 } else if (dbus_message_is_method_call(msg, g_service_interface, TTS_METHOD_HELLO_SYNC)) {
310 ttsd_dbus_server_hello_sync(g_conn_listener, msg);
312 } else if (dbus_message_is_method_call(msg, g_service_interface, TTS_METHOD_INITIALIZE)) {
313 ttsd_dbus_server_initialize(g_conn_listener, msg);
315 } else if (dbus_message_is_method_call(msg, g_service_interface, TTS_METHOD_FINALIZE)) {
316 ttsd_dbus_server_finalize(g_conn_listener, msg);
318 } else if (dbus_message_is_method_call(msg, g_service_interface, TTS_METHOD_GET_SUPPORT_VOICES)) {
319 ttsd_dbus_server_get_support_voices(g_conn_listener, msg);
321 } else if (dbus_message_is_method_call(msg, g_service_interface, TTS_METHOD_GET_CURRENT_VOICE)) {
322 ttsd_dbus_server_get_current_voice(g_conn_listener, msg);
324 } else if (dbus_message_is_method_call(msg, g_service_interface, TTS_METHOD_ADD_TEXT)) {
325 ttsd_dbus_server_add_text(g_conn_listener, msg);
327 } else if (dbus_message_is_method_call(msg, g_service_interface, TTS_METHOD_PLAY)) {
328 ttsd_dbus_server_play(g_conn_listener, msg);
330 } else if (dbus_message_is_method_call(msg, g_service_interface, TTS_METHOD_STOP)) {
331 ttsd_dbus_server_stop(g_conn_listener, msg);
333 } else if (dbus_message_is_method_call(msg, g_service_interface, TTS_METHOD_PAUSE)) {
334 ttsd_dbus_server_pause(g_conn_listener, msg);
336 } else if (dbus_message_is_method_call(msg, g_service_interface, TTS_METHOD_SET_PRIVATE_DATA)) {
337 ttsd_dbus_server_set_private_data(g_conn_listener, msg);
339 } else if (dbus_message_is_method_call(msg, g_service_interface, TTS_METHOD_GET_PRIVATE_DATA)) {
340 ttsd_dbus_server_get_private_data(g_conn_listener, msg);
342 } else if (dbus_message_is_method_call(msg, g_service_interface, TTS_METHOD_PLAY_PCM)) {
343 ttsd_dbus_server_play_pcm(g_conn_listener, msg);
345 } else if (dbus_message_is_method_call(msg, g_service_interface, TTS_METHOD_STOP_PCM)) {
346 ttsd_dbus_server_stop_pcm(g_conn_listener, msg);
348 } else if (dbus_message_is_method_call(msg, g_service_interface, TTS_METHOD_ADD_PCM)) {
349 ttsd_dbus_server_add_pcm(g_conn_listener, msg);
351 } else if (dbus_message_is_method_call(msg, g_service_interface, TTS_METHOD_GET_SERVICE_STATE)) {
352 ttsd_dbus_server_get_service_state(g_conn_listener, msg);
355 SLOG(LOG_DEBUG, tts_tag(), "Message is NOT valid");
358 /* free the message */
359 dbus_message_unref(msg);
362 return ECORE_CALLBACK_RENEW;
365 void __ttsd_dbus_service_free()
367 if (NULL != g_service_name) {
368 free(g_service_name);
369 g_service_name = NULL;
372 if (NULL != g_service_object) {
373 free(g_service_object);
374 g_service_object = NULL;
377 if (NULL != g_service_interface) {
378 free(g_service_interface);
379 g_service_interface = NULL;
383 void __ttsd_dbus_connection_free()
385 if (NULL != g_conn_listener) {
386 dbus_connection_close(g_conn_listener);
387 dbus_connection_unref(g_conn_listener);
388 g_conn_listener = NULL;
390 if (NULL != g_conn_sender) {
391 dbus_connection_close(g_conn_sender);
392 dbus_connection_unref(g_conn_sender);
393 g_conn_sender = NULL;
397 int ttsd_dbus_open_connection()
399 SLOG(LOG_DEBUG, tts_tag(), "@@@ start dbus open connection");
401 dbus_error_init(&err);
405 /* Create connection for sender */
406 g_conn_sender = dbus_bus_get_private(DBUS_BUS_SYSTEM, &err);
407 if (dbus_error_is_set(&err)) {
408 SLOG(LOG_ERROR, tts_tag(), "[Dbus ERROR] Fail dbus_bus_get : %s", err.message);
409 dbus_error_free(&err);
412 if (NULL == g_conn_sender) {
413 SLOG(LOG_ERROR, tts_tag(), "[Dbus ERROR] Fail to get dbus connection sender");
417 dbus_connection_set_exit_on_disconnect(g_conn_sender, false);
419 /* connect to the bus and check for errors */
420 g_conn_listener = dbus_bus_get_private(DBUS_BUS_SYSTEM, &err);
421 if (dbus_error_is_set(&err)) {
422 SLOG(LOG_ERROR, tts_tag(), "[Dbus ERROR] Fail dbus_bus_get : %s", err.message);
423 dbus_error_free(&err);
424 __ttsd_dbus_connection_free();
428 if (NULL == g_conn_listener) {
429 SLOG(LOG_ERROR, tts_tag(), "[Dbus ERROR] Fail to get dbus connection");
430 __ttsd_dbus_connection_free();
434 dbus_connection_set_exit_on_disconnect(g_conn_listener, false);
436 __ttsd_dbus_service_free();
438 g_service_name = (char*)calloc(strlen(TTS_SERVER_SERVICE_NAME) + 1, sizeof(char));
439 g_service_object = (char*)calloc(strlen(TTS_SERVER_SERVICE_OBJECT_PATH) + 1, sizeof(char));
440 g_service_interface = (char*)calloc(strlen(TTS_SERVER_SERVICE_INTERFACE) + 1, sizeof(char));
442 if (NULL == g_service_name || NULL == g_service_object || NULL == g_service_interface) {
443 SLOG(LOG_ERROR, tts_tag(), "[ERROR] Fail to allocate memory");
444 __ttsd_dbus_service_free();
445 __ttsd_dbus_connection_free();
449 snprintf(g_service_name, strlen(TTS_SERVER_SERVICE_NAME) + 1, "%s", TTS_SERVER_SERVICE_NAME);
450 snprintf(g_service_object, strlen(TTS_SERVER_SERVICE_OBJECT_PATH) + 1, "%s", TTS_SERVER_SERVICE_OBJECT_PATH);
451 snprintf(g_service_interface, strlen(TTS_SERVER_SERVICE_INTERFACE) + 1, "%s", TTS_SERVER_SERVICE_INTERFACE);
453 /* request our name on the bus and check for errors */
454 ret = dbus_bus_request_name(g_conn_listener, g_service_name, DBUS_NAME_FLAG_REPLACE_EXISTING, &err);
455 if (DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER != ret) {
456 SLOG(LOG_ERROR, tts_tag(), "[Dbus ERROR] Fail to be primary owner");
457 __ttsd_dbus_connection_free();
461 if (dbus_error_is_set(&err)) {
462 SLOG(LOG_ERROR, tts_tag(), "[Dbus ERROR] Fail to request dbus name : %s", err.message);
463 dbus_error_free(&err);
464 __ttsd_dbus_connection_free();
468 /* Flush messages which are received before fd event handler registration */
469 while (DBUS_DISPATCH_DATA_REMAINS == dbus_connection_get_dispatch_status(g_conn_listener)) {
470 listener_event_callback(NULL, NULL);
473 /* add a rule for getting signal */
475 snprintf(rule, 128, "type='method_call',interface='%s'", g_service_interface);
477 /* add a rule for which messages we want to see */
478 dbus_bus_add_match(g_conn_listener, rule, &err);/* see signals from the given interface */
479 dbus_connection_flush(g_conn_listener);
481 if (dbus_error_is_set(&err)) {
482 SLOG(LOG_ERROR, tts_tag(), "[Dbus ERROR] dbus_bus_add_match() : %s", err.message);
483 __ttsd_dbus_connection_free();
488 dbus_connection_get_unix_fd(g_conn_listener, &fd);
490 g_dbus_fd_handler = ecore_main_fd_handler_add(fd, ECORE_FD_READ, (Ecore_Fd_Cb)listener_event_callback, g_conn_listener, NULL, NULL);
491 if (NULL == g_dbus_fd_handler) {
492 SLOG(LOG_ERROR, tts_tag(), "[Dbus ERROR] Fail to get fd handler");
493 __ttsd_dbus_connection_free();
497 SLOG(LOG_DEBUG, tts_tag(), "@@@");
501 int ttsd_dbus_close_connection()
503 SLOG(LOG_DEBUG, tts_tag(), "@@@ start dbus close connection");
505 dbus_error_init(&err);
507 if (NULL != g_dbus_fd_handler) {
508 ecore_main_fd_handler_del(g_dbus_fd_handler);
509 g_dbus_fd_handler = NULL;
512 if (NULL != g_conn_listener) {
513 dbus_bus_release_name(g_conn_listener, g_service_name, &err);
514 if (dbus_error_is_set(&err)) {
515 SLOG(LOG_ERROR, tts_tag(), "[Dbus ERROR] dbus_bus_release_name() : %s", err.message);
516 dbus_error_free(&err);
520 __ttsd_dbus_connection_free();
521 __ttsd_dbus_service_free();
523 SLOG(LOG_DEBUG, tts_tag(), "@@@");