sync codes to 2.4 spin
[platform/core/uifw/tts.git] / server / ttsd_dbus_server.c
1 /*
2 *  Copyright (c) 2011-2014 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 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, get_tag(), ">>>>> TTS Hello");
28
29         DBusMessage* reply;
30         reply = dbus_message_new_method_return(msg);
31
32         if (NULL != reply) {
33                 if (!dbus_connection_send(conn, reply, NULL)) {
34                         SLOG(LOG_ERROR, get_tag(), "[OUT ERROR] Out Of Memory!");
35                 }
36
37                 dbus_connection_flush(conn);
38                 dbus_message_unref(reply);
39         } else {
40                 SLOG(LOG_ERROR, get_tag(), "[OUT ERROR] Fail to create reply message!!");
41         }
42
43         return 0;
44 }
45
46 int ttsd_dbus_server_initialize(DBusConnection* conn, DBusMessage* msg)
47 {
48         DBusError err;
49         dbus_error_init(&err);
50
51         int pid, uid;
52         int ret = 0;
53
54         dbus_message_get_args(msg, &err,
55                 DBUS_TYPE_INT32, &pid,
56                 DBUS_TYPE_INT32, &uid,
57                 DBUS_TYPE_INVALID);
58
59         SLOG(LOG_DEBUG, get_tag(), ">>>>> TTS INITIALIZE");
60
61         if (dbus_error_is_set(&err)) {
62                 SLOG(LOG_ERROR, get_tag(), "[IN ERROR] tts initialize : get arguments error (%s)", err.message);
63                 dbus_error_free(&err);
64                 ret = TTSD_ERROR_OPERATION_FAILED;
65         } else {
66
67                 SECURE_SLOG(LOG_DEBUG, get_tag(), "[IN] tts initialize : pid(%d), uid(%d)", pid , uid);
68                 ret =  ttsd_server_initialize(pid, uid);
69         }
70
71         DBusMessage* reply;
72         reply = dbus_message_new_method_return(msg);
73
74         if (NULL != reply) {
75                 dbus_message_append_args(reply, DBUS_TYPE_INT32, &ret, DBUS_TYPE_INVALID);
76
77                 if (0 == ret) {
78                         SLOG(LOG_DEBUG, get_tag(), "[OUT] tts initialize : result(%d)", ret);
79                 } else {
80                         SLOG(LOG_ERROR, get_tag(), "[OUT ERROR] tts initialize : result(%d)", ret);
81                 }
82
83                 if (!dbus_connection_send(conn, reply, NULL)) {
84                         SLOG(LOG_ERROR, get_tag(), "[OUT ERROR] tts initialize : Out Of Memory!");
85                 }
86
87                 dbus_connection_flush(conn);
88                 dbus_message_unref(reply);
89         } else {
90                 SLOG(LOG_ERROR, get_tag(), "[OUT ERROR] tts initialize : Fail to create reply message!!");
91         }
92
93         return 0;
94 }
95
96 int ttsd_dbus_server_finalize(DBusConnection* conn, DBusMessage* msg)
97 {
98         DBusError err;
99         dbus_error_init(&err);
100
101         int uid;
102         int ret = 0;
103
104         dbus_message_get_args(msg, &err, DBUS_TYPE_INT32, &uid, DBUS_TYPE_INVALID);
105
106         SLOG(LOG_DEBUG, get_tag(), ">>>>> TTS FINALIZE");
107
108         if (dbus_error_is_set(&err)) {
109                 SLOG(LOG_ERROR, get_tag(), "[IN ERROR] tts finalize : get arguments error (%s)", err.message);
110                 dbus_error_free(&err);
111                 ret = TTSD_ERROR_OPERATION_FAILED;
112         } else {
113                 SECURE_SLOG(LOG_DEBUG, get_tag(), "[IN] tts finalize : uid(%d)", uid);
114                 ret =  ttsd_server_finalize(uid);
115         }
116
117         DBusMessage* reply;
118         reply = dbus_message_new_method_return(msg);
119
120         if (NULL != reply) {
121                 dbus_message_append_args(reply, DBUS_TYPE_INT32, &ret, DBUS_TYPE_INVALID);
122
123                 if (0 == ret) {
124                         SLOG(LOG_DEBUG, get_tag(), "[OUT] tts finalize : result(%d)", ret);
125                 } else {
126                         SLOG(LOG_ERROR, get_tag(), "[OUT ERROR] tts finalize : result(%d)", ret);
127                 }
128
129                 if (!dbus_connection_send(conn, reply, NULL)) {
130                         SLOG(LOG_ERROR, get_tag(), "[OUT ERROR] tts finalize : Out Of Memory!");
131                 }
132
133                 dbus_connection_flush(conn);
134                 dbus_message_unref(reply);
135         } else {
136                 SLOG(LOG_ERROR, get_tag(), "[OUT ERROR] tts finalize : Fail to create reply message!!");
137         }
138
139         return 0;
140 }
141
142 int ttsd_dbus_server_get_support_voices(DBusConnection* conn, DBusMessage* msg)
143 {
144         DBusError err;
145         dbus_error_init(&err);
146
147         int uid;
148         int ret = 0;
149         GList* voice_list = NULL;
150
151         dbus_message_get_args(msg, &err, DBUS_TYPE_INT32, &uid, DBUS_TYPE_INVALID);
152
153         SLOG(LOG_DEBUG, get_tag(), ">>>>> TTS GET VOICES");
154
155         if (dbus_error_is_set(&err)) {
156                 SLOG(LOG_ERROR, get_tag(), "[IN ERROR] tts get supported voices : get arguments error (%s)", err.message);
157                 dbus_error_free(&err);
158                 ret = TTSD_ERROR_OPERATION_FAILED;
159         } else {
160                 SECURE_SLOG(LOG_DEBUG, get_tag(), "[IN] get supported voices : uid(%d)", uid);
161                 ret = ttsd_server_get_support_voices(uid, &voice_list);
162         }
163
164         DBusMessage* reply;
165         reply = dbus_message_new_method_return(msg);
166
167         if (NULL != reply) {
168                 DBusMessageIter args;
169                 dbus_message_iter_init_append(reply, &args);
170
171                 /* Append result*/
172                 dbus_message_iter_append_basic(&args, DBUS_TYPE_INT32, &(ret));
173
174                 if (0 == ret) {
175                         /* Append voice size */
176                         int size = g_list_length(voice_list);
177
178                         if (!dbus_message_iter_append_basic(&args, DBUS_TYPE_INT32, &(size))) {
179                                 SLOG(LOG_ERROR, get_tag(), "[OUT ERROR] tts supported voices : Fail to append type");
180                                 ret = TTSD_ERROR_OPERATION_FAILED;
181                         } else {
182
183                                 GList *iter = NULL;
184                                 voice_s* voice;
185
186                                 iter = g_list_first(voice_list);
187
188                                 while (NULL != iter) {
189                                         voice = iter->data;
190
191                                         if (NULL != voice) {
192                                                 dbus_message_iter_append_basic(&args, DBUS_TYPE_STRING, &(voice->language));
193                                                 dbus_message_iter_append_basic(&args, DBUS_TYPE_INT32, &(voice->type));
194
195                                                 if (NULL != voice->language)
196                                                         g_free(voice->language);
197
198                                                 g_free(voice);
199                                         }
200
201                                         voice_list = g_list_remove_link(voice_list, iter);
202                                         g_list_free(iter);
203                                         iter = g_list_first(voice_list);
204                                 }
205                         }
206                         SLOG(LOG_DEBUG, get_tag(), "[OUT] tts supported voices : result(%d)", ret);
207                 } else {
208                         SLOG(LOG_ERROR, get_tag(), "[OUT ERROR] tts supported voices : result(%d)", ret);
209                 }
210
211                 if (!dbus_connection_send(conn, reply, NULL)) {
212                         SLOG(LOG_ERROR, get_tag(), "[OUT ERROR] error : Out Of Memory!");
213                 }
214
215                 dbus_connection_flush(conn);
216                 dbus_message_unref(reply);
217         } else {
218                 SLOG(LOG_ERROR, get_tag(), "[OUT ERROR] tts supported voices : Fail to create reply message!!");
219         }
220
221         return 0;
222 }
223
224 int ttsd_dbus_server_get_current_voice(DBusConnection* conn, DBusMessage* msg)
225 {
226         DBusError err;
227         dbus_error_init(&err);
228
229         int uid;
230         char* lang = NULL;
231         int voice_type;
232         int ret;
233
234         dbus_message_get_args(msg, &err, DBUS_TYPE_INT32, &uid, DBUS_TYPE_INVALID);
235
236         SLOG(LOG_DEBUG, get_tag(), ">>>>> TTS GET DEFAULT VOICE");
237
238         if (dbus_error_is_set(&err)) {
239                 SLOG(LOG_ERROR, get_tag(), "[IN ERROR] tts get default voice : Get arguments error (%s)", err.message);
240                 dbus_error_free(&err);
241                 ret = TTSD_ERROR_OPERATION_FAILED;
242         } else {
243                 SECURE_SLOG(LOG_DEBUG, get_tag(), "[IN] tts get default voice : uid(%d)", uid);
244                 ret = ttsd_server_get_current_voice(uid, &lang, &voice_type);
245         }
246
247         DBusMessage* reply;
248         reply = dbus_message_new_method_return(msg);
249
250         if (NULL != reply) {
251                 if (0 == ret) {
252                         /* Append result and voice */
253                         dbus_message_append_args(reply,
254                                 DBUS_TYPE_INT32, &ret,
255                                 DBUS_TYPE_STRING, &lang,
256                                 DBUS_TYPE_INT32, &voice_type,
257                                 DBUS_TYPE_INVALID);
258                         SECURE_SLOG(LOG_DEBUG, get_tag(), "[OUT] tts default voice : lang(%s), vctype(%d)", lang, voice_type);
259                 } else {
260                         dbus_message_append_args(reply,
261                                 DBUS_TYPE_INT32, &ret,
262                                 DBUS_TYPE_INVALID);
263                         SLOG(LOG_ERROR, get_tag(), "[OUT ERROR] tts default voice : result(%d)", ret); 
264                 }
265
266                 if (!dbus_connection_send(conn, reply, NULL)) {
267                         SLOG(LOG_ERROR, get_tag(), "[OUT ERROR] tts default voice : Out Of Memory!");
268                 }
269
270                 dbus_connection_flush(conn);
271                 dbus_message_unref(reply);
272         } else {
273                 SLOG(LOG_ERROR, get_tag(), "[OUT ERROR] tts default voice : Fail to create reply message!!");
274         }
275
276         if (NULL != lang)       free(lang);
277         return 0;
278 }
279
280 int ttsd_dbus_server_add_text(DBusConnection* conn, DBusMessage* msg)
281 {
282         DBusError err;
283         dbus_error_init(&err);
284
285         int uid, voicetype, speed, uttid;
286         char *text, *lang;
287         int ret = 0;
288
289         dbus_message_get_args(msg, &err,
290                 DBUS_TYPE_INT32, &uid,
291                 DBUS_TYPE_STRING, &text,
292                 DBUS_TYPE_STRING, &lang,
293                 DBUS_TYPE_INT32, &voicetype,
294                 DBUS_TYPE_INT32, &speed,
295                 DBUS_TYPE_INT32, &uttid,
296                 DBUS_TYPE_INVALID);
297
298         SLOG(LOG_DEBUG, get_tag(), ">>>>> TTS ADD TEXT");
299
300         if (dbus_error_is_set(&err)) {
301                 SLOG(LOG_ERROR, get_tag(), "[IN ERROR] tts add text : Get arguments error (%s)", err.message);
302                 dbus_error_free(&err);
303                 ret = TTSD_ERROR_OPERATION_FAILED;
304         } else {
305                 
306                 SECURE_SLOG(LOG_DEBUG, get_tag(), "[IN] tts add text : uid(%d), text(%s), lang(%s), type(%d), speed(%d), uttid(%d)", 
307                         uid, text, lang, voicetype, speed, uttid); 
308                 ret =  ttsd_server_add_queue(uid, text, lang, voicetype, speed, uttid);
309         }
310
311         DBusMessage* reply;
312         reply = dbus_message_new_method_return(msg);
313
314         if (NULL != reply) {
315                 dbus_message_append_args(reply, DBUS_TYPE_INT32, &ret, DBUS_TYPE_INVALID);
316
317                 if (0 == ret) {
318                         SLOG(LOG_DEBUG, get_tag(), "[OUT] tts add text : result(%d)", ret);
319                 } else {
320                         SLOG(LOG_ERROR, get_tag(), "[OUT ERROR] tts add text : result(%d)", ret);
321                 }
322
323                 if (!dbus_connection_send(conn, reply, NULL)) {
324                         SLOG(LOG_ERROR, get_tag(), "[OUT ERROR] tts add text : Out Of Memory!");
325                 }
326
327                 dbus_connection_flush(conn);
328                 dbus_message_unref(reply);
329         } else {
330                 SLOG(LOG_ERROR, get_tag(), "[OUT ERROR] tts add text : Fail to create reply message!!");
331         }
332
333         SLOG(LOG_DEBUG, get_tag(), "<<<<<");
334         SLOG(LOG_DEBUG, get_tag(), "  ");
335
336         return 0;
337 }
338
339 int ttsd_dbus_server_play(DBusConnection* conn, DBusMessage* msg)
340 {
341         DBusError err;
342         dbus_error_init(&err);
343
344         int uid;
345         int ret = 0;
346
347         dbus_message_get_args(msg, &err, DBUS_TYPE_INT32, &uid, DBUS_TYPE_INVALID);
348
349         SLOG(LOG_DEBUG, get_tag(), ">>>>> TTS PLAY");
350
351         if (dbus_error_is_set(&err)) {
352                 SLOG(LOG_ERROR, get_tag(), "[IN ERROR] tts play : Get arguments error (%s)", err.message);
353                 dbus_error_free(&err);
354                 ret = TTSD_ERROR_OPERATION_FAILED;
355         } else {
356                 SECURE_SLOG(LOG_DEBUG, get_tag(), "[IN] tts play : uid(%d)", uid);
357                 ret =  ttsd_server_play(uid);
358         }
359
360         DBusMessage* reply;
361         reply = dbus_message_new_method_return(msg);
362
363         if (NULL != reply) {
364                 dbus_message_append_args(reply, DBUS_TYPE_INT32, &ret, DBUS_TYPE_INVALID);
365
366                 if (0 == ret) {
367                         SLOG(LOG_DEBUG, get_tag(), "[OUT] tts play : result(%d)", ret);
368                 } else {
369                         SLOG(LOG_ERROR, get_tag(), "[OUT ERROR] tts play : result(%d)", ret);
370                 }
371
372                 if (!dbus_connection_send(conn, reply, NULL)) {
373                         SLOG(LOG_ERROR, get_tag(), "[OUT ERROR] tts play : Out Of Memory!");
374                 }
375
376                 dbus_connection_flush(conn);
377                 dbus_message_unref(reply);
378         } else {
379                 SLOG(LOG_ERROR, get_tag(), "[OUT ERROR] tts play : Fail to create reply message!!");
380         }
381
382         SLOG(LOG_DEBUG, get_tag(), "<<<<<");
383         SLOG(LOG_DEBUG, get_tag(), "  ");
384
385         return 0;
386 }
387
388 int ttsd_dbus_server_stop(DBusConnection* conn, DBusMessage* msg)
389 {
390         DBusError err;
391         dbus_error_init(&err);
392
393         int uid;
394         int ret = 0;
395         dbus_message_get_args(msg, &err, 
396                 DBUS_TYPE_INT32, &uid, 
397                 DBUS_TYPE_INVALID);
398
399         SLOG(LOG_DEBUG, get_tag(), ">>>>> TTS STOP");
400
401         if (dbus_error_is_set(&err)) {
402                 SLOG(LOG_ERROR, get_tag(), "[IN ERROR] tts stop : Get arguments error (%s)", err.message);
403                 dbus_error_free(&err);
404                 ret = TTSD_ERROR_OPERATION_FAILED;
405         } else {
406                 SECURE_SLOG(LOG_DEBUG, get_tag(), "[IN] tts stop : uid(%d)", uid);
407                 ret = ttsd_server_stop(uid);
408         }
409
410         DBusMessage* reply;
411         reply = dbus_message_new_method_return(msg);
412
413         if (NULL != reply) {
414                 dbus_message_append_args(reply, 
415                         DBUS_TYPE_INT32, &ret, 
416                         DBUS_TYPE_INVALID);
417
418                 if (0 == ret) {
419                         SLOG(LOG_DEBUG, get_tag(), "[OUT] tts stop : result(%d)", ret);
420                 } else {
421                         SLOG(LOG_ERROR, get_tag(), "[OUT ERROR] tts stop : result(%d)", ret);
422                 }
423
424                 if (!dbus_connection_send(conn, reply, NULL)) {
425                         SLOG(LOG_ERROR, get_tag(), "[OUT ERROR] tts stop : Out Of Memory!");
426                 }
427
428                 dbus_connection_flush(conn);
429                 dbus_message_unref(reply);
430         } else {
431                 SLOG(LOG_ERROR, get_tag(), "[OUT ERROR] tts stop : Fail to create reply message!!");
432         }
433
434         SLOG(LOG_DEBUG, get_tag(), "<<<<<");
435         SLOG(LOG_DEBUG, get_tag(), "  ");
436
437         return 0;
438 }
439
440 int ttsd_dbus_server_pause(DBusConnection* conn, DBusMessage* msg)
441 {
442         DBusError err;
443         dbus_error_init(&err);
444
445         int uid;
446         int uttid;
447         int ret = 0;
448         dbus_message_get_args(msg, &err, 
449                 DBUS_TYPE_INT32, &uid, 
450                 DBUS_TYPE_INVALID);
451
452         SLOG(LOG_DEBUG, get_tag(), ">>>>> TTS PAUSE");
453
454         if (dbus_error_is_set(&err)) {
455                 SLOG(LOG_ERROR, get_tag(), "[IN ERROR] tts pause : Get arguments error (%s)", err.message);
456                 dbus_error_free(&err);
457                 ret = TTSD_ERROR_OPERATION_FAILED;
458         } else {
459                 SECURE_SLOG(LOG_DEBUG, get_tag(), "[IN] tts pause : uid(%d)", uid);
460                 ret = ttsd_server_pause(uid, &uttid);
461         }
462
463         DBusMessage* reply;
464         reply = dbus_message_new_method_return(msg);
465
466         if (NULL != reply) {
467                 dbus_message_append_args(reply, 
468                         DBUS_TYPE_INT32, &ret, 
469                         DBUS_TYPE_INVALID);
470
471                 if (0 == ret) {
472                         SLOG(LOG_DEBUG, get_tag(), "[OUT] tts pause : result(%d)", ret);
473                 } else {
474                         SLOG(LOG_ERROR, get_tag(), "[OUT ERROR] tts pause : result(%d)", ret);
475                 }
476
477                 if (!dbus_connection_send(conn, reply, NULL)) {
478                         SLOG(LOG_ERROR, get_tag(), "[OUT ERROR] tts pause : Out Of Memory!");
479                 }
480
481                 dbus_connection_flush(conn);
482                 dbus_message_unref(reply);
483         } else {
484                 SLOG(LOG_ERROR, get_tag(), "[OUT ERROR] tts pause : Fail to create reply message!!");
485         }
486
487         SLOG(LOG_DEBUG, get_tag(), "<<<<<");
488         SLOG(LOG_DEBUG, get_tag(), "  ");
489
490         return 0;
491 }