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