Add g_steal_pointer to avoid double-free
[platform/core/uifw/tts.git] / server / ttsd_dbus_server.c
1 /*
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.
12 */
13
14
15 #include "ttsd_main.h"
16 #include "ttsd_dbus.h"
17 #include "ttsd_dbus_server.h"
18 #include "ttsd_server.h"
19
20 extern int ttsd_data_get_pid(const unsigned int uid);
21
22 /*
23 * Dbus Client-Daemon Server
24 */
25 int ttsd_dbus_server_hello(DBusConnection* conn, DBusMessage* msg)
26 {
27         SLOG(LOG_DEBUG, tts_tag(), ">>>>> TTS Hello");
28
29         DBusError err;
30         dbus_error_init(&err);
31
32         int pid;
33         unsigned int uid;
34         int mode = 0;
35         int playing_mode = 0;
36         int registered_event_mask = 0;
37         dbus_message_get_args(msg, &err,
38                 DBUS_TYPE_INT32, &pid,
39                 DBUS_TYPE_UINT32, &uid,
40                 DBUS_TYPE_INT32, &mode,
41                 DBUS_TYPE_INT32, &playing_mode,
42                 DBUS_TYPE_INT32, &registered_event_mask,
43                 DBUS_TYPE_INVALID);
44
45         if (dbus_error_is_set(&err)) {
46                 SLOG(LOG_ERROR, tts_tag(), "[IN ERROR] ttsd Hello : get arguments error (%s)", err.message);
47                 dbus_error_free(&err);
48         } else {
49                 SLOG(LOG_INFO, tts_tag(), "[IN] ttsd hello : pid(%d), uid(%u), mode(%d), playing_mode(%d)", pid, uid, mode, playing_mode);
50                 bool is_initialized = false;
51                 bool is_credential_needed = false;
52                 ttsd_state_e service_state = TTSD_STATE_INVALID;
53                 int credential_needed = 0;
54                 int ret = -1;
55
56                 ttsd_server_is_already_initialized(pid, uid, &is_initialized);
57                 if (false == is_initialized) {
58                         ret = ttsd_server_initialize(pid, uid, (ttsd_mode_e)mode, (ttsd_playing_mode_e)playing_mode, registered_event_mask, TTS_IPC_METHOD_DBUS, &service_state, &is_credential_needed);
59                         if (0 != ret) {
60                                 SLOG(LOG_ERROR, tts_tag(), "[IN ERROR] ttsd Hello : server initialize, ret(%d)", ret);
61                         }
62                         ttsd_server_update_instant_reprepare_client();
63
64                         credential_needed = is_credential_needed ? 1 : 0;
65                 } else {
66                         SLOG(LOG_INFO, tts_tag(), "[IN] ttsd hello : already initialized. pid(%d), uid(%u)", pid, uid);
67                         ret = TTS_ERROR_ALREADY_INITIALIZED;
68                         credential_needed = TTS_CREDENTIAL_NEEDED_ALREADY_INITIALIZED;
69                 }
70
71                 SLOG(LOG_INFO, tts_tag(), "[INFO] send_hello. pid(%d), uid(%u), ret(%d), service_state(%d), credential_needed(%d)",
72                                 pid, uid, ret, (int)service_state, credential_needed);
73
74                 ttsdc_dbus_send_hello(pid, uid, ret, (int)service_state, credential_needed);
75         }
76
77         return 0;
78 }
79
80 int ttsd_dbus_server_hello_sync(DBusConnection* conn, DBusMessage* msg)
81 {
82         SLOG(LOG_DEBUG, tts_tag(), ">>>>> TTS Hello");
83
84         DBusMessage* reply;
85         reply = dbus_message_new_method_return(msg);
86
87         if (NULL != reply) {
88                 if (!dbus_connection_send(conn, reply, NULL)) {
89                         SLOG(LOG_ERROR, tts_tag(), "[OUT ERROR] Out Of Memory!");
90                 }
91
92                 dbus_connection_flush(conn);
93                 dbus_message_unref(reply);
94         } else {
95                 SLOG(LOG_ERROR, tts_tag(), "[OUT ERROR] Fail to create reply message!!");
96         }
97
98         return 0;
99 }
100
101 int ttsd_dbus_server_initialize(DBusConnection* conn, DBusMessage* msg)
102 {
103         DBusError err;
104         dbus_error_init(&err);
105
106         int pid;
107         unsigned int uid;
108         bool credential_needed = 0;
109         ttsd_state_e service_state = TTSD_STATE_INVALID;
110         int mode = 0;
111         int playing_mode = 0;
112         int registered_event_mask = 0;
113         int ret = 0;
114
115         dbus_message_get_args(msg, &err,
116                 DBUS_TYPE_INT32, &pid,
117                 DBUS_TYPE_UINT32, &uid,
118                 DBUS_TYPE_INT32, &mode,
119                 DBUS_TYPE_INT32, &playing_mode,
120                 DBUS_TYPE_INT32, &registered_event_mask,
121                 DBUS_TYPE_INVALID);
122
123         SLOG(LOG_DEBUG, tts_tag(), ">>>>> TTS INITIALIZE");
124
125         if (dbus_error_is_set(&err)) {
126                 SLOG(LOG_ERROR, tts_tag(), "[IN ERROR] tts initialize : get arguments error (%s)", err.message);
127                 dbus_error_free(&err);
128                 ret = TTSD_ERROR_OPERATION_FAILED;
129         } else {
130                 SECURE_SLOG(LOG_DEBUG, tts_tag(), "[IN] tts initialize : pid(%d), uid(%u), mode(%d), playing_mode(%d)", pid, uid, mode, playing_mode);
131                 ret = ttsd_server_initialize(pid, uid, (ttsd_mode_e)mode, (ttsd_playing_mode_e)playing_mode, registered_event_mask, TTS_IPC_METHOD_DBUS, &service_state, &credential_needed);
132                 ttsd_server_update_instant_reprepare_client();
133         }
134
135         DBusMessage* reply;
136         reply = dbus_message_new_method_return(msg);
137
138         int tmp_credential_needed = (int)credential_needed;
139         int tmp_service_state = (int)service_state;
140         SLOG(LOG_DEBUG, tts_tag(), "[OUT] tts initialize : result(%d), service_state(%d), credential_needed(%d)", ret, tmp_service_state, tmp_credential_needed);
141         if (NULL != reply) {
142                 dbus_message_append_args(reply,
143                         DBUS_TYPE_INT32, &ret,
144                         DBUS_TYPE_INT32, &tmp_service_state,
145                         DBUS_TYPE_INT32, &tmp_credential_needed,
146                         DBUS_TYPE_INVALID);
147
148                 if (0 == ret) {
149                         SLOG(LOG_DEBUG, tts_tag(), "[OUT] tts initialize : result(%d), service_state(%d), credential_needed(%d)", ret, tmp_service_state, tmp_credential_needed);
150                 } else {
151                         SLOG(LOG_ERROR, tts_tag(), "[OUT ERROR] tts initialize : result(%d)", ret);
152                 }
153
154                 if (!dbus_connection_send(conn, reply, NULL)) {
155                         SLOG(LOG_ERROR, tts_tag(), "[OUT ERROR] tts initialize : Out Of Memory!");
156                 }
157
158                 dbus_connection_flush(conn);
159                 dbus_message_unref(reply);
160         } else {
161                 SLOG(LOG_ERROR, tts_tag(), "[OUT ERROR] tts initialize : Fail to create reply message!!");
162         }
163
164         return 0;
165 }
166
167 int ttsd_dbus_server_finalize(DBusConnection* conn, DBusMessage* msg)
168 {
169         DBusError err;
170         dbus_error_init(&err);
171
172         unsigned int uid;
173         int ret = 0;
174
175         dbus_message_get_args(msg, &err, DBUS_TYPE_UINT32, &uid, DBUS_TYPE_INVALID);
176
177         SLOG(LOG_DEBUG, tts_tag(), ">>>>> TTS FINALIZE");
178
179         if (dbus_error_is_set(&err)) {
180                 SLOG(LOG_ERROR, tts_tag(), "[IN ERROR] tts finalize : get arguments error (%s)", err.message);
181                 dbus_error_free(&err);
182                 ret = TTSD_ERROR_OPERATION_FAILED;
183         } else {
184                 SECURE_SLOG(LOG_DEBUG, tts_tag(), "[IN] tts finalize : uid(%u)", uid);
185                 ret = ttsd_server_finalize(uid);
186                 ttsd_server_update_instant_reprepare_client();
187         }
188
189         DBusMessage* reply;
190         reply = dbus_message_new_method_return(msg);
191
192         if (NULL != reply) {
193                 dbus_message_append_args(reply, DBUS_TYPE_INT32, &ret, DBUS_TYPE_INVALID);
194
195                 if (0 == ret) {
196                         SLOG(LOG_DEBUG, tts_tag(), "[OUT] tts finalize : result(%d)", ret);
197                 } else {
198                         SLOG(LOG_ERROR, tts_tag(), "[OUT ERROR] tts finalize : result(%d)", ret);
199                 }
200
201                 if (!dbus_connection_send(conn, reply, NULL)) {
202                         SLOG(LOG_ERROR, tts_tag(), "[OUT ERROR] tts finalize : Out Of Memory!");
203                 }
204
205                 dbus_connection_flush(conn);
206                 dbus_message_unref(reply);
207         } else {
208                 SLOG(LOG_ERROR, tts_tag(), "[OUT ERROR] tts finalize : Fail to create reply message!!");
209         }
210
211         return 0;
212 }
213
214 int ttsd_dbus_server_get_support_voices(DBusConnection* conn, DBusMessage* msg)
215 {
216         DBusError err;
217         dbus_error_init(&err);
218
219         unsigned int uid;
220         int ret = 0;
221         GList* voice_list = NULL;
222
223         dbus_message_get_args(msg, &err, DBUS_TYPE_UINT32, &uid, DBUS_TYPE_INVALID);
224
225         SLOG(LOG_DEBUG, tts_tag(), ">>>>> TTS GET VOICES");
226
227         if (dbus_error_is_set(&err)) {
228                 SLOG(LOG_ERROR, tts_tag(), "[IN ERROR] tts get supported voices : get arguments error (%s)", err.message);
229                 dbus_error_free(&err);
230                 ret = TTSD_ERROR_OPERATION_FAILED;
231         } else {
232                 SECURE_SLOG(LOG_DEBUG, tts_tag(), "[IN] get supported voices : uid(%u)", uid);
233                 ret = ttsd_server_get_support_voices(uid, &voice_list);
234         }
235
236         DBusMessage* reply;
237         reply = dbus_message_new_method_return(msg);
238
239         if (NULL != reply) {
240                 DBusMessageIter args;
241                 dbus_message_iter_init_append(reply, &args);
242
243                 /* Append result*/
244                 dbus_message_iter_append_basic(&args, DBUS_TYPE_INT32, &(ret));
245
246                 if (0 == ret) {
247                         /* Append voice size */
248                         unsigned int size = g_list_length(voice_list);
249
250                         if (!dbus_message_iter_append_basic(&args, DBUS_TYPE_INT32, &(size))) {
251                                 SLOG(LOG_ERROR, tts_tag(), "[OUT ERROR] tts supported voices : Fail to append type");
252                                 ret = TTSD_ERROR_OPERATION_FAILED;
253                         } else {
254
255                                 GList *iter = NULL;
256                                 voice_s* voice;
257
258                                 iter = g_list_first(voice_list);
259
260                                 while (NULL != iter) {
261                                         voice = iter->data;
262
263                                         if (NULL != voice) {
264                                                 dbus_message_iter_append_basic(&args, DBUS_TYPE_STRING, &(voice->language));
265                                                 dbus_message_iter_append_basic(&args, DBUS_TYPE_INT32, &(voice->type));
266
267                                                 if (NULL != voice->language)
268                                                         g_free(voice->language);
269
270                                                 g_free(voice);
271                                         }
272
273                                         voice_list = g_list_remove_link(voice_list, iter);
274                                         g_list_free(iter);
275                                         iter = g_list_first(voice_list);
276                                 }
277                         }
278                         SLOG(LOG_DEBUG, tts_tag(), "[OUT] tts supported voices : result(%d)", ret);
279                 } else {
280                         SLOG(LOG_ERROR, tts_tag(), "[OUT ERROR] tts supported voices : result(%d)", ret);
281                 }
282
283                 if (!dbus_connection_send(conn, reply, NULL)) {
284                         SLOG(LOG_ERROR, tts_tag(), "[OUT ERROR] error : Out Of Memory!");
285                 }
286
287                 dbus_connection_flush(conn);
288                 dbus_message_unref(reply);
289         } else {
290                 SLOG(LOG_ERROR, tts_tag(), "[OUT ERROR] tts supported voices : Fail to create reply message!!");
291         }
292
293         return 0;
294 }
295
296 int ttsd_dbus_server_get_current_voice(DBusConnection* conn, DBusMessage* msg)
297 {
298         DBusError err;
299         dbus_error_init(&err);
300
301         unsigned int uid;
302         char* lang = NULL;
303         int voice_type;
304         int ret;
305
306         dbus_message_get_args(msg, &err, DBUS_TYPE_UINT32, &uid, DBUS_TYPE_INVALID);
307
308         SLOG(LOG_DEBUG, tts_tag(), ">>>>> TTS GET DEFAULT VOICE");
309
310         if (dbus_error_is_set(&err)) {
311                 SLOG(LOG_ERROR, tts_tag(), "[IN ERROR] tts get default voice : Get arguments error (%s)", err.message);
312                 dbus_error_free(&err);
313                 ret = TTSD_ERROR_OPERATION_FAILED;
314         } else {
315                 SECURE_SLOG(LOG_DEBUG, tts_tag(), "[IN] tts get default voice : uid(%u)", uid);
316                 ret = ttsd_server_get_current_voice(uid, &lang, &voice_type);
317         }
318
319         DBusMessage* reply;
320         reply = dbus_message_new_method_return(msg);
321
322         if (NULL != reply) {
323                 if (0 == ret) {
324                         /* Append result and voice */
325                         dbus_message_append_args(reply,
326                                 DBUS_TYPE_INT32, &ret,
327                                 DBUS_TYPE_STRING, &lang,
328                                 DBUS_TYPE_INT32, &voice_type,
329                                 DBUS_TYPE_INVALID);
330                         SECURE_SLOG(LOG_DEBUG, tts_tag(), "[OUT] tts default voice : lang(%s), vctype(%d)", lang, voice_type);
331                 } else {
332                         dbus_message_append_args(reply,
333                                 DBUS_TYPE_INT32, &ret,
334                                 DBUS_TYPE_INVALID);
335                         SLOG(LOG_ERROR, tts_tag(), "[OUT ERROR] tts default voice : result(%d)", ret);
336                 }
337
338                 if (!dbus_connection_send(conn, reply, NULL)) {
339                         SLOG(LOG_ERROR, tts_tag(), "[OUT ERROR] tts default voice : Out Of Memory!");
340                 }
341
342                 dbus_connection_flush(conn);
343                 dbus_message_unref(reply);
344         } else {
345                 SLOG(LOG_ERROR, tts_tag(), "[OUT ERROR] tts default voice : Fail to create reply message!!");
346         }
347
348         if (NULL != lang)       free(lang);
349         return 0;
350 }
351
352 int ttsd_dbus_server_add_text(DBusConnection* conn, DBusMessage* msg)
353 {
354         DBusError err;
355         dbus_error_init(&err);
356
357         unsigned int uid, voicetype, speed, uttid;
358         char *text, *lang, *credential;
359         int ret = 0;
360
361         dbus_message_get_args(msg, &err,
362                 DBUS_TYPE_UINT32, &uid,
363                 DBUS_TYPE_STRING, &text,
364                 DBUS_TYPE_STRING, &lang,
365                 DBUS_TYPE_INT32, &voicetype,
366                 DBUS_TYPE_INT32, &speed,
367                 DBUS_TYPE_INT32, &uttid,
368                 DBUS_TYPE_STRING, &credential,
369                 DBUS_TYPE_INVALID);
370
371         SLOG(LOG_DEBUG, tts_tag(), ">>>>> TTS ADD TEXT");
372
373         if (dbus_error_is_set(&err)) {
374                 SLOG(LOG_ERROR, tts_tag(), "[IN ERROR] tts add text : Get arguments error (%s)", err.message);
375                 dbus_error_free(&err);
376                 ret = TTSD_ERROR_OPERATION_FAILED;
377         } else {
378                 SECURE_SLOG(LOG_DEBUG, tts_tag(), "[IN] tts add text : uid(%u), text(%s), lang(%s), type(%d), speed(%d), uttid(%d), credential(%s)",
379                         uid, (NULL == text) ? "NULL" : text, (NULL == lang) ? "NULL" : lang, voicetype, speed, uttid, (NULL == credential) ? "NULL" : credential);
380                 ret = ttsd_server_add_text(uid, text, lang, voicetype, speed, uttid, credential);
381         }
382
383         DBusMessage* reply;
384         reply = dbus_message_new_method_return(msg);
385
386         if (NULL != reply) {
387                 dbus_message_append_args(reply, DBUS_TYPE_INT32, &ret, DBUS_TYPE_INVALID);
388
389                 if (0 == ret) {
390                         SLOG(LOG_DEBUG, tts_tag(), "[OUT] tts add text : result(%d)", ret);
391                 } else {
392                         SLOG(LOG_ERROR, tts_tag(), "[OUT ERROR] tts add text : result(%d)", ret);
393                 }
394
395                 if (!dbus_connection_send(conn, reply, NULL)) {
396                         SLOG(LOG_ERROR, tts_tag(), "[OUT ERROR] tts add text : Out Of Memory!");
397                 }
398
399                 dbus_connection_flush(conn);
400                 dbus_message_unref(reply);
401         } else {
402                 SLOG(LOG_ERROR, tts_tag(), "[OUT ERROR] tts add text : Fail to create reply message!!");
403         }
404
405         SLOG(LOG_DEBUG, tts_tag(), "<<<<<");
406         SLOG(LOG_DEBUG, tts_tag(), "  ");
407
408         return 0;
409 }
410
411 int ttsd_dbus_server_add_silent_utterance(DBusConnection* conn, DBusMessage* msg)
412 {
413         DBusError err;
414         dbus_error_init(&err);
415
416         unsigned int uid, duration_in_msec;
417         int uttid;
418         char *credential;
419         int ret = 0;
420
421         dbus_message_get_args(msg, &err,
422                 DBUS_TYPE_UINT32, &uid,
423                 DBUS_TYPE_UINT32, &duration_in_msec,
424                 DBUS_TYPE_INT32, &uttid,
425                 DBUS_TYPE_STRING, &credential,
426                 DBUS_TYPE_INVALID);
427
428         SLOG(LOG_DEBUG, tts_tag(), ">>>>> TTS ADD SILENT UTTERANCE");
429
430         if (dbus_error_is_set(&err)) {
431                 SLOG(LOG_ERROR, tts_tag(), "[IN ERROR] tts add silent utterance : Get arguments error (%s)", err.message);
432                 dbus_error_free(&err);
433                 ret = TTSD_ERROR_OPERATION_FAILED;
434         } else {
435                 SECURE_SLOG(LOG_DEBUG, tts_tag(), "[IN] tts add silent utterance : uid(%u), duration_in_msec(%u), uttid(%d), credential(%s)",
436                         uid, duration_in_msec, uttid, (NULL == credential) ? "NULL" : credential);
437                 ret = ttsd_server_add_silent_utterance(uid, duration_in_msec, uttid, credential);
438         }
439
440         DBusMessage* reply;
441         reply = dbus_message_new_method_return(msg);
442
443         if (NULL != reply) {
444                 dbus_message_append_args(reply, DBUS_TYPE_INT32, &ret, DBUS_TYPE_INVALID);
445
446                 if (0 == ret) {
447                         SLOG(LOG_DEBUG, tts_tag(), "[OUT] tts add silent utterance : result(%d)", ret);
448                 } else {
449                         SLOG(LOG_ERROR, tts_tag(), "[OUT ERROR] tts add silent utterance : result(%d)", ret);
450                 }
451
452                 if (!dbus_connection_send(conn, reply, NULL)) {
453                         SLOG(LOG_ERROR, tts_tag(), "[OUT ERROR] tts add silent utterance : Out Of Memory!");
454                 }
455
456                 dbus_connection_flush(conn);
457                 dbus_message_unref(reply);
458         } else {
459                 SLOG(LOG_ERROR, tts_tag(), "[OUT ERROR] tts add silent utterance : Fail to create reply message!!");
460         }
461
462         SLOG(LOG_DEBUG, tts_tag(), "<<<<<");
463         SLOG(LOG_DEBUG, tts_tag(), "  ");
464
465         return 0;
466 }
467
468 int ttsd_dbus_server_play(DBusConnection* conn, DBusMessage* msg)
469 {
470         DBusError err;
471         dbus_error_init(&err);
472
473         unsigned int uid;
474         char* credential;
475         int ret = 0;
476
477         dbus_message_get_args(msg, &err,
478                 DBUS_TYPE_UINT32, &uid,
479                 DBUS_TYPE_STRING, &credential,
480                 DBUS_TYPE_INVALID);
481
482         SLOG(LOG_DEBUG, tts_tag(), ">>>>> TTS PLAY");
483
484         if (dbus_error_is_set(&err)) {
485                 SLOG(LOG_ERROR, tts_tag(), "[IN ERROR] tts play : Get arguments error (%s)", err.message);
486                 dbus_error_free(&err);
487                 ret = TTSD_ERROR_OPERATION_FAILED;
488         } else {
489                 SECURE_SLOG(LOG_DEBUG, tts_tag(), "[IN] tts play : uid(%u), credential(%s)", uid, (NULL == credential) ? "NULL" : credential);
490                 ret =  ttsd_server_play(uid, credential);
491         }
492
493         DBusMessage* reply;
494         reply = dbus_message_new_method_return(msg);
495
496         if (NULL != reply) {
497                 dbus_message_append_args(reply, DBUS_TYPE_INT32, &ret, DBUS_TYPE_INVALID);
498
499                 if (0 == ret) {
500                         SLOG(LOG_DEBUG, tts_tag(), "[OUT] tts play : result(%d)", ret);
501                 } else {
502                         SLOG(LOG_ERROR, tts_tag(), "[OUT ERROR] tts play : result(%d)", ret);
503                 }
504
505                 if (!dbus_connection_send(conn, reply, NULL)) {
506                         SLOG(LOG_ERROR, tts_tag(), "[OUT ERROR] tts play : Out Of Memory!");
507                 }
508
509                 dbus_connection_flush(conn);
510                 dbus_message_unref(reply);
511         } else {
512                 SLOG(LOG_ERROR, tts_tag(), "[OUT ERROR] tts play : Fail to create reply message!!");
513         }
514
515         SLOG(LOG_DEBUG, tts_tag(), "<<<<<");
516         SLOG(LOG_DEBUG, tts_tag(), "  ");
517
518         return 0;
519 }
520
521 int ttsd_dbus_server_stop(DBusConnection* conn, DBusMessage* msg)
522 {
523         DBusError err;
524         dbus_error_init(&err);
525
526         unsigned int uid;
527         int ret = 0;
528         dbus_message_get_args(msg, &err,
529                 DBUS_TYPE_UINT32, &uid,
530                 DBUS_TYPE_INVALID);
531
532         SLOG(LOG_DEBUG, tts_tag(), ">>>>> TTS STOP");
533
534         if (dbus_error_is_set(&err)) {
535                 SLOG(LOG_ERROR, tts_tag(), "[IN ERROR] tts stop : Get arguments error (%s)", err.message);
536                 dbus_error_free(&err);
537                 ret = TTSD_ERROR_OPERATION_FAILED;
538         } else {
539                 SECURE_SLOG(LOG_DEBUG, tts_tag(), "[IN] tts stop : uid(%u)", uid);
540                 ret = ttsd_server_stop(uid);
541         }
542
543         DBusMessage* reply;
544         reply = dbus_message_new_method_return(msg);
545
546         if (NULL != reply) {
547                 dbus_message_append_args(reply,
548                         DBUS_TYPE_INT32, &ret,
549                         DBUS_TYPE_INVALID);
550
551                 if (0 == ret) {
552                         SLOG(LOG_DEBUG, tts_tag(), "[OUT] tts stop : result(%d)", ret);
553                 } else {
554                         SLOG(LOG_ERROR, tts_tag(), "[OUT ERROR] tts stop : result(%d)", ret);
555                 }
556
557                 if (!dbus_connection_send(conn, reply, NULL)) {
558                         SLOG(LOG_ERROR, tts_tag(), "[OUT ERROR] tts stop : Out Of Memory!");
559                 }
560
561                 dbus_connection_flush(conn);
562                 dbus_message_unref(reply);
563         } else {
564                 SLOG(LOG_ERROR, tts_tag(), "[OUT ERROR] tts stop : Fail to create reply message!!");
565         }
566
567         SLOG(LOG_DEBUG, tts_tag(), "<<<<<");
568         SLOG(LOG_DEBUG, tts_tag(), "  ");
569
570         return 0;
571 }
572
573 int ttsd_dbus_server_pause(DBusConnection* conn, DBusMessage* msg)
574 {
575         DBusError err;
576         dbus_error_init(&err);
577
578         unsigned int uid;
579         int ret = 0;
580         dbus_message_get_args(msg, &err,
581                 DBUS_TYPE_UINT32, &uid,
582                 DBUS_TYPE_INVALID);
583
584         SLOG(LOG_DEBUG, tts_tag(), ">>>>> TTS PAUSE");
585
586         if (dbus_error_is_set(&err)) {
587                 SLOG(LOG_ERROR, tts_tag(), "[IN ERROR] tts pause : Get arguments error (%s)", err.message);
588                 dbus_error_free(&err);
589                 ret = TTSD_ERROR_OPERATION_FAILED;
590         } else {
591                 SECURE_SLOG(LOG_DEBUG, tts_tag(), "[IN] tts pause : uid(%u)", uid);
592                 ret = ttsd_server_pause(uid);
593         }
594
595         DBusMessage* reply;
596         reply = dbus_message_new_method_return(msg);
597
598         if (NULL != reply) {
599                 dbus_message_append_args(reply,
600                         DBUS_TYPE_INT32, &ret,
601                         DBUS_TYPE_INVALID);
602
603                 if (0 == ret) {
604                         SLOG(LOG_DEBUG, tts_tag(), "[OUT] tts pause : result(%d)", ret);
605                 } else {
606                         SLOG(LOG_ERROR, tts_tag(), "[OUT ERROR] tts pause : result(%d)", ret);
607                 }
608
609                 if (!dbus_connection_send(conn, reply, NULL)) {
610                         SLOG(LOG_ERROR, tts_tag(), "[OUT ERROR] tts pause : Out Of Memory!");
611                 }
612
613                 dbus_connection_flush(conn);
614                 dbus_message_unref(reply);
615         } else {
616                 SLOG(LOG_ERROR, tts_tag(), "[OUT ERROR] tts pause : Fail to create reply message!!");
617         }
618
619         SLOG(LOG_DEBUG, tts_tag(), "<<<<<");
620         SLOG(LOG_DEBUG, tts_tag(), "  ");
621
622         return 0;
623 }
624
625 int ttsd_dbus_server_set_private_data(DBusConnection* conn, DBusMessage* msg)
626 {
627         DBusError err;
628         dbus_error_init(&err);
629
630         unsigned int uid;
631         char* key;
632         char* data;
633         int ret = 0;
634         dbus_message_get_args(msg, &err,
635                 DBUS_TYPE_UINT32, &uid,
636                 DBUS_TYPE_STRING, &key,
637                 DBUS_TYPE_STRING, &data,
638                 DBUS_TYPE_INVALID);
639
640         SLOG(LOG_DEBUG, tts_tag(), ">>>>> TTS set private data");
641
642         if (dbus_error_is_set(&err)) {
643                 SLOG(LOG_ERROR, tts_tag(), "[IN ERROR] Fail to get arguments (%s)", err.message);
644                 dbus_error_free(&err);
645                 ret = TTSD_ERROR_OPERATION_FAILED;
646         } else {
647                 SLOG(LOG_DEBUG, tts_tag(), "[IN] tts set private data(%d)", uid);
648                 ret = ttsd_server_set_private_data(uid, key, data);
649         }
650
651         DBusMessage* reply;
652         reply = dbus_message_new_method_return(msg);
653
654         if (NULL != reply) {
655                 dbus_message_append_args(reply,
656                         DBUS_TYPE_INT32, &ret,
657                         DBUS_TYPE_INVALID);
658
659                 if (0 == ret) {
660                         SLOG(LOG_DEBUG, tts_tag(), "[OUT] tts set private data : (%d)", ret);
661                 } else {
662                         SLOG(LOG_ERROR, tts_tag(), "[OUT ERROR] tts set private data : (%d)", ret);
663                 }
664
665                 if (!dbus_connection_send(conn, reply, NULL)) {
666                         SLOG(LOG_ERROR, tts_tag(), "[OUT ERROR] Fail to send reply");
667                 }
668
669                 dbus_connection_flush(conn);
670                 dbus_message_unref(reply);
671         } else {
672                 SLOG(LOG_ERROR, tts_tag(), "[OUT ERROR] Fail to create reply message");
673         }
674
675         SLOG(LOG_DEBUG, tts_tag(), "<<<<<");
676         SLOG(LOG_DEBUG, tts_tag(), "");
677
678         return 0;
679 }
680
681 int ttsd_dbus_server_get_private_data(DBusConnection* conn, DBusMessage* msg)
682 {
683         DBusError err;
684         dbus_error_init(&err);
685
686         unsigned int uid;
687         char* key = NULL;
688         char* data = NULL;
689         int ret = 0;
690         dbus_message_get_args(msg, &err,
691                 DBUS_TYPE_UINT32, &uid,
692                 DBUS_TYPE_STRING, &key,
693                 DBUS_TYPE_INVALID);
694
695         SLOG(LOG_DEBUG, tts_tag(), ">>>>> TTS get private data");
696
697         if (dbus_error_is_set(&err)) {
698                 SLOG(LOG_ERROR, tts_tag(), "[IN ERROR] Fail to get arguments (%s)", err.message);
699                 dbus_error_free(&err);
700                 ret = TTSD_ERROR_OPERATION_FAILED;
701         } else {
702                 SLOG(LOG_DEBUG, tts_tag(), "[IN] tts get private data : uid(%u)", uid);
703                 ret = ttsd_server_get_private_data(uid, key, &data);
704         }
705
706         DBusMessage* reply;
707         reply = dbus_message_new_method_return(msg);
708
709         if (NULL != reply) {
710                 dbus_message_append_args(reply,
711                         DBUS_TYPE_INT32, &ret,
712                         DBUS_TYPE_STRING, &data,
713                         DBUS_TYPE_INVALID);
714
715                 if (0 == ret) {
716                         SLOG(LOG_DEBUG, tts_tag(), "[OUT] tts get private data : Result(%d), data(%s)", ret, (NULL == data) ? "NULL" : data);
717                 } else {
718                         SLOG(LOG_ERROR, tts_tag(), "[OUT ERROR] tts get private data : (%d)", ret);
719                 }
720
721                 if (!dbus_connection_send(conn, reply, NULL)) {
722                         SLOG(LOG_ERROR, tts_tag(), "[OUT ERROR] Fail to send reply");
723                 }
724
725                 dbus_connection_flush(conn);
726                 dbus_message_unref(reply);
727         } else {
728                 SLOG(LOG_ERROR, tts_tag(), "[OUT ERROR] Fail to create reply message");
729         }
730
731         SLOG(LOG_DEBUG, tts_tag(), "<<<<<");
732         SLOG(LOG_DEBUG, tts_tag(), "");
733
734         if (NULL != data) {
735                 free(data);
736                 data = NULL;
737         }
738
739         return 0;
740 }
741
742 int ttsd_dbus_server_play_pcm(DBusConnection* conn, DBusMessage* msg)
743 {
744         DBusError err;
745         dbus_error_init(&err);
746
747         unsigned int uid;
748         int ret = 0;
749
750         dbus_message_get_args(msg, &err,
751                 DBUS_TYPE_UINT32, &uid,
752                 DBUS_TYPE_INVALID);
753
754         SLOG(LOG_DEBUG, tts_tag(), ">>>>> TTS PLAY PCM");
755
756         if (dbus_error_is_set(&err)) {
757                 SLOG(LOG_ERROR, tts_tag(), "[IN ERROR] tts play pcm : Get arguments error (%s)", err.message);
758                 dbus_error_free(&err);
759                 ret = TTSD_ERROR_OPERATION_FAILED;
760         } else {
761                 SECURE_SLOG(LOG_DEBUG, tts_tag(), "[IN] tts play pcm : uid(%u)", uid);
762                 ret =  ttsd_server_play_pcm(uid);
763         }
764
765         DBusMessage* reply;
766         reply = dbus_message_new_method_return(msg);
767
768         if (NULL != reply) {
769                 dbus_message_append_args(reply, DBUS_TYPE_INT32, &ret, DBUS_TYPE_INVALID);
770
771                 if (0 == ret) {
772                         SLOG(LOG_DEBUG, tts_tag(), "[OUT] tts play pcm : result(%d)", ret);
773                 } else {
774                         SLOG(LOG_ERROR, tts_tag(), "[OUT ERROR] tts play pcm : result(%d)", ret);
775                 }
776
777                 if (!dbus_connection_send(conn, reply, NULL)) {
778                         SLOG(LOG_ERROR, tts_tag(), "[OUT ERROR] tts play pcm : Out Of Memory!");
779                 }
780
781                 dbus_connection_flush(conn);
782                 dbus_message_unref(reply);
783         } else {
784                 SLOG(LOG_ERROR, tts_tag(), "[OUT ERROR] tts play pcm : Fail to create reply message!!");
785         }
786
787         SLOG(LOG_DEBUG, tts_tag(), "<<<<<");
788         SLOG(LOG_DEBUG, tts_tag(), "  ");
789
790         return 0;
791 }
792
793 int ttsd_dbus_server_stop_pcm(DBusConnection* conn, DBusMessage* msg)
794 {
795         DBusError err;
796         dbus_error_init(&err);
797
798         unsigned int uid;
799         int ret = 0;
800
801         dbus_message_get_args(msg, &err,
802                 DBUS_TYPE_UINT32, &uid,
803                 DBUS_TYPE_INVALID);
804
805         SLOG(LOG_DEBUG, tts_tag(), ">>>>> TTS STOP PCM");
806
807         if (dbus_error_is_set(&err)) {
808                 SLOG(LOG_ERROR, tts_tag(), "[IN ERROR] tts stop pcm : Get arguments error (%s)", err.message);
809                 dbus_error_free(&err);
810                 ret = TTSD_ERROR_OPERATION_FAILED;
811         } else {
812                 SECURE_SLOG(LOG_DEBUG, tts_tag(), "[IN] tts stop pcm : uid(%u)", uid);
813                 ret = ttsd_server_stop(uid);
814         }
815
816         DBusMessage* reply;
817         reply = dbus_message_new_method_return(msg);
818
819         if (NULL != reply) {
820                 dbus_message_append_args(reply, DBUS_TYPE_INT32, &ret, DBUS_TYPE_INVALID);
821
822                 if (0 == ret) {
823                         SLOG(LOG_DEBUG, tts_tag(), "[OUT] tts stop pcm : result(%d)", ret);
824                 } else {
825                         SLOG(LOG_ERROR, tts_tag(), "[OUT ERROR] tts stop pcm : result(%d)", ret);
826                 }
827
828                 if (!dbus_connection_send(conn, reply, NULL)) {
829                         SLOG(LOG_ERROR, tts_tag(), "[OUT ERROR] tts stop pcm : Out Of Memory!");
830                 }
831
832                 dbus_connection_flush(conn);
833                 dbus_message_unref(reply);
834         } else {
835                 SLOG(LOG_ERROR, tts_tag(), "[OUT ERROR] tts stop pcm : Fail to create reply message!!");
836         }
837
838         SLOG(LOG_DEBUG, tts_tag(), "<<<<<");
839         SLOG(LOG_DEBUG, tts_tag(), "  ");
840
841         return 0;
842 }
843
844 int ttsd_dbus_server_add_pcm(DBusConnection* conn, DBusMessage* msg)
845 {
846         DBusError err;
847         dbus_error_init(&err);
848
849         unsigned int uid;
850         int event;
851         int audio_type;
852         int rate;
853         char* data = NULL;
854         int data_size;
855         int ret = 0;
856
857         dbus_message_get_args(msg, &err,
858                 DBUS_TYPE_UINT32, &uid,
859                 DBUS_TYPE_INT32, &event,
860                 DBUS_TYPE_INT32, &audio_type,
861                 DBUS_TYPE_INT32, &rate,
862                 //DBUS_TYPE_STRING, &data,
863                 //DBUS_TYPE_INT32, &data_size,
864                 DBUS_TYPE_ARRAY, DBUS_TYPE_BYTE,
865                 &data, &data_size,
866                 DBUS_TYPE_INVALID);
867
868         SLOG(LOG_DEBUG, tts_tag(), ">>>>> TTS ADD PCM");
869
870         if (dbus_error_is_set(&err)) {
871                 SLOG(LOG_ERROR, tts_tag(), "[IN ERROR] tts add pcm : Get arguments error (%s)", err.message);
872                 dbus_error_free(&err);
873                 ret = TTSD_ERROR_OPERATION_FAILED;
874         } else {
875                 SECURE_SLOG(LOG_DEBUG, tts_tag(), "[IN] tts add pcm : uid(%u)", uid);
876                 ret =  ttsd_server_add_pcm(uid, event, (void*)data, data_size, audio_type, rate);
877         }
878
879         DBusMessage* reply;
880         reply = dbus_message_new_method_return(msg);
881
882         if (NULL != reply) {
883                 dbus_message_append_args(reply, DBUS_TYPE_INT32, &ret, DBUS_TYPE_INVALID);
884
885                 if (0 == ret) {
886                         SLOG(LOG_DEBUG, tts_tag(), "[OUT] tts add pcm : result(%d)", ret);
887                 } else {
888                         SLOG(LOG_ERROR, tts_tag(), "[OUT ERROR] tts add pcm : result(%d)", ret);
889                 }
890
891                 if (!dbus_connection_send(conn, reply, NULL)) {
892                         SLOG(LOG_ERROR, tts_tag(), "[OUT ERROR] tts add pcm : Out Of Memory!");
893                 }
894
895                 dbus_connection_flush(conn);
896                 dbus_message_unref(reply);
897         } else {
898                 SLOG(LOG_ERROR, tts_tag(), "[OUT ERROR] tts add pcm : Fail to create reply message!!");
899         }
900
901         SLOG(LOG_DEBUG, tts_tag(), "<<<<<");
902         SLOG(LOG_DEBUG, tts_tag(), "  ");
903
904         return 0;
905 }
906
907 int ttsd_dbus_server_get_service_state(DBusConnection* conn, DBusMessage* msg)
908 {
909         DBusError err;
910         dbus_error_init(&err);
911
912         unsigned int uid;
913         int state = (int)TTSD_STATE_READY;
914         int ret = 0;
915
916         dbus_message_get_args(msg, &err,
917                 DBUS_TYPE_UINT32, &uid,
918                 DBUS_TYPE_INVALID);
919
920         SLOG(LOG_DEBUG, tts_tag(), ">>>>> TTS GET SERVICE STATE");
921
922         if (dbus_error_is_set(&err)) {
923                 SLOG(LOG_ERROR, tts_tag(), "[IN ERROR] tts get service state : Get arguments error (%s)", err.message);
924                 dbus_error_free(&err);
925                 ret = TTSD_ERROR_OPERATION_FAILED;
926         } else {
927                 SECURE_SLOG(LOG_DEBUG, tts_tag(), "[IN] tts get service state : uid(%u)", uid);
928                 ret = ttsd_server_get_service_state(uid, &state);
929         }
930
931         DBusMessage* reply;
932         reply = dbus_message_new_method_return(msg);
933
934         if (NULL != reply) {
935                 dbus_message_append_args(reply, DBUS_TYPE_INT32, &ret, DBUS_TYPE_INT32, &state, DBUS_TYPE_INVALID);
936
937                 if (0 == ret) {
938                         SLOG(LOG_DEBUG, tts_tag(), "[OUT] tts get service state : result(%d)", ret);
939                 } else {
940                         SLOG(LOG_ERROR, tts_tag(), "[OUT ERROR] tts get service state : result(%d)", ret);
941                 }
942
943                 if (!dbus_connection_send(conn, reply, NULL)) {
944                         SLOG(LOG_ERROR, tts_tag(), "[OUT ERROR] tts get service state : Out Of Memory!");
945                 }
946
947                 dbus_connection_flush(conn);
948                 dbus_message_unref(reply);
949         } else {
950                 SLOG(LOG_ERROR, tts_tag(), "[OUT ERROR] tts get service state : Fail to create reply message!!");
951         }
952
953         SLOG(LOG_DEBUG, tts_tag(), "<<<<<");
954         SLOG(LOG_DEBUG, tts_tag(), "  ");
955
956         return 0;
957 }