Merge "Fix defect that is detected by static analysis tool" into tizen
[platform/core/uifw/tts.git] / server / ttsd_tidl.c
1 /*
2 *  Copyright (c) 2021 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 #include <stdlib.h>
15 #include <Ecore.h>
16 #include <pthread.h>
17
18 #include "ttsd_main.h"
19 #include "ttsd_server.h"
20 #include "ttsd_stub.h"
21
22 #include "ttsd_tidl.h"
23
24 typedef struct {
25         unsigned int uid;
26         rpc_port_stub_tts_notify_cb_h notify_cb_h;
27 } tts_tidl_proxy_info_s;
28
29 static GList* g_tidl_proxy_infos = NULL;
30
31 static rpc_port_stub_tts_callback_s g_callback;
32
33 static pthread_mutex_t g_tidl_proxy_infos_mutex = PTHREAD_MUTEX_INITIALIZER;
34
35
36 static tts_tidl_proxy_info_s* __get_tidl_proxy_info_s(unsigned int uid)
37 {
38         GList* iter = NULL;
39         tts_tidl_proxy_info_s* info = NULL;
40
41         if (g_list_length(g_tidl_proxy_infos) > 0) {
42                 /* Get a first item */
43                 iter = g_list_first(g_tidl_proxy_infos);
44
45                 while (NULL != iter) {
46                         info = iter->data;
47                         if (info->uid == uid) {
48                                 return info;
49                         }
50
51                         /* Next item */
52                         iter = g_list_next(iter);
53                 }
54         }
55
56         return NULL;
57 }
58
59 void __send_msg(int pid, unsigned int uid, bundle* msg)
60 {
61         SLOG(LOG_INFO, tts_tag(), "[TIDL] start : send msg : pid(%d), uid(%u)", pid, uid);
62         pthread_mutex_lock(&g_tidl_proxy_infos_mutex);
63         tts_tidl_proxy_info_s* info = __get_tidl_proxy_info_s(uid);
64         if (NULL == info) {
65                 SLOG(LOG_ERROR, tts_tag(), "[TIDL] Uid is not valid. pid(%d), uid(%u)", pid, uid);
66                 pthread_mutex_unlock(&g_tidl_proxy_infos_mutex);
67                 return;
68         }
69
70         rpc_port_stub_tts_notify_cb_h handle = info->notify_cb_h;
71         if (NULL == handle) {
72                 SLOG(LOG_INFO, tts_tag(), "notify callback handle null");
73                 pthread_mutex_unlock(&g_tidl_proxy_infos_mutex);
74                 return;
75         }
76
77         if (0 != rpc_port_stub_tts_notify_cb_invoke(handle, pid, (int)uid, msg)) {
78                 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to send msg");
79                 pthread_mutex_unlock(&g_tidl_proxy_infos_mutex);
80                 return;
81         }
82         SLOG(LOG_INFO, tts_tag(), "send msg : pid(%d), uid(%u)", pid, uid);
83         pthread_mutex_unlock(&g_tidl_proxy_infos_mutex);
84 }
85
86 static void __create_client_cb(rpc_port_stub_tts_context_h context, void *user_data)
87 {
88         char *sender = NULL;
89
90         rpc_port_stub_tts_context_get_sender(context, &sender);
91         if (!sender)
92                 return;
93
94         SLOG(LOG_ERROR, tts_tag(), ">>>>> Client connect. appid(%s)", sender);
95         free(sender);
96 }
97
98 static void __destroy_client_cb(rpc_port_stub_tts_context_h context, void *user_data)
99 {
100         void* tag = NULL;
101         rpc_port_stub_tts_context_get_tag(context, &tag);
102
103         if (NULL != tag) {
104                 unsigned int uid = (uintptr_t)tag;
105                 SLOG(LOG_ERROR, tts_tag(), ">>>>> TTS FINALIZE. uid(%u)", uid);
106
107                 if (0 != ttsd_server_finalize(uid)) {
108                         return;
109                 }
110
111                 pthread_mutex_lock(&g_tidl_proxy_infos_mutex);
112                 tts_tidl_proxy_info_s* info = __get_tidl_proxy_info_s(uid);
113                 if (NULL == info) {
114                         SLOG(LOG_ERROR, tts_tag(), "[TIDL ERROR] Fail to set notify callback");
115                         pthread_mutex_unlock(&g_tidl_proxy_infos_mutex);
116                         return;
117                 }
118
119                 rpc_port_stub_tts_notify_cb_destroy(info->notify_cb_h);
120                 info->notify_cb_h = NULL;
121
122                 g_tidl_proxy_infos = g_list_remove(g_tidl_proxy_infos, info);
123                 free(info);
124
125                 SLOG(LOG_DEBUG, tts_tag(), "<<<<<");
126
127                 pthread_mutex_unlock(&g_tidl_proxy_infos_mutex);
128         }
129         rpc_port_stub_tts_context_set_tag(context, NULL);
130
131         char *sender = NULL;
132         rpc_port_stub_tts_context_get_sender(context, &sender);
133         if (!sender)
134                 return;
135
136         SLOG(LOG_INFO, tts_tag(), ">>>>> Client disconnect. appid(%s)", sender);
137         free(sender);
138 }
139
140 static void __register_cb(rpc_port_stub_tts_context_h context, int pid, int uid, int mode, rpc_port_stub_tts_notify_cb_h callback, void *user_data)
141 {
142         unsigned int u_uid = (unsigned int)uid;
143         SLOG(LOG_ERROR, tts_tag(), ">>>>> TTS REGISTER CALLBACK uid(%u), mode(%d)", u_uid, mode);
144
145         bool is_initialized = false;
146         ttsd_server_is_already_initialized(pid, u_uid, &is_initialized);
147
148         int ret = -1;
149         int credential_needed = 0;
150         if (false == is_initialized) {
151                 bool is_credential_needed = false;
152                 ret = ttsd_server_initialize(pid, u_uid, mode, TTS_IPC_METHOD_TIDL, &is_credential_needed);
153                 if (0 != ret) {
154                         SLOG(LOG_ERROR, tts_tag(), "[IN ERROR] ttsd Hello : server initialize, ret(%d)", ret);
155                 }
156
157                 credential_needed = is_credential_needed ? 1 : 0;
158         } else {
159                 ret = TTS_ERROR_ALREADY_INITIALIZED;
160                 credential_needed = TTS_CREDENTIAL_NEEDED_ALREADY_INITIALIZED;
161         }
162
163         uintptr_t ptr_uid = u_uid;
164         rpc_port_stub_tts_context_set_tag(context, (void*)ptr_uid);
165
166         pthread_mutex_lock(&g_tidl_proxy_infos_mutex);
167         tts_tidl_proxy_info_s* info = __get_tidl_proxy_info_s(u_uid);
168         if (NULL == info) {
169                 info = (tts_tidl_proxy_info_s*)calloc(1, sizeof(tts_tidl_proxy_info_s));
170                 if (NULL == info) {
171                         SLOG(LOG_ERROR, tts_tag(), "[TIDL ERROR] Fail to allocate memory for tidl proxy");
172                         pthread_mutex_unlock(&g_tidl_proxy_infos_mutex);
173                         return;
174                 }
175         } else {
176                 g_tidl_proxy_infos = g_list_remove(g_tidl_proxy_infos, info);
177                 rpc_port_stub_tts_notify_cb_destroy(info->notify_cb_h);
178                 info->notify_cb_h = NULL;
179         }
180
181         if (0 != rpc_port_stub_tts_notify_cb_clone(callback, &info->notify_cb_h)) {
182                 SLOG(LOG_ERROR, tts_tag(), "[TIDL ERROR] Fail to set notify callback");
183                 free(info);
184                 pthread_mutex_unlock(&g_tidl_proxy_infos_mutex);
185                 return;
186         }
187
188         info->uid = u_uid;
189         g_tidl_proxy_infos = g_list_append(g_tidl_proxy_infos, info);
190         pthread_mutex_unlock(&g_tidl_proxy_infos_mutex);
191
192         SLOG(LOG_INFO, tts_tag(), "create player instance");
193         ttsdc_tidl_send_hello(pid, u_uid, ret, credential_needed);
194
195         SLOG(LOG_ERROR, tts_tag(), "<<<<<<<<<<<");
196         return;
197 }
198
199 static int __register_cb_sync(rpc_port_stub_tts_context_h context, int pid, int uid, rpc_port_stub_tts_notify_cb_h callback, void* user_data)
200 {
201         unsigned int u_uid = (unsigned int)uid;
202         SLOG(LOG_ERROR, tts_tag(), ">>>>> TTS REGISTER CALLBACK synchronously uid(%u)", u_uid);
203
204         uintptr_t ptr_uid = u_uid;
205         rpc_port_stub_tts_context_set_tag(context, (void*)ptr_uid);
206
207         pthread_mutex_lock(&g_tidl_proxy_infos_mutex);
208         tts_tidl_proxy_info_s* info = __get_tidl_proxy_info_s(u_uid);
209         if (NULL == info) {
210                 info = (tts_tidl_proxy_info_s*)calloc(1, sizeof(tts_tidl_proxy_info_s));
211                 if (NULL == info) {
212                         SLOG(LOG_ERROR, tts_tag(), "[TIDL ERROR] Fail to allocate memory for tidl proxy");
213                         pthread_mutex_unlock(&g_tidl_proxy_infos_mutex);
214                         return TTSD_ERROR_OUT_OF_MEMORY;
215                 }
216         } else {
217                 g_tidl_proxy_infos = g_list_remove(g_tidl_proxy_infos, info);
218                 rpc_port_stub_tts_notify_cb_destroy(info->notify_cb_h);
219                 info->notify_cb_h = NULL;
220         }
221
222         if (0 != rpc_port_stub_tts_notify_cb_clone(callback, &info->notify_cb_h)) {
223                 SLOG(LOG_ERROR, tts_tag(), "[TIDL ERROR] Fail to set notify callback");
224                 free(info);
225                 pthread_mutex_unlock(&g_tidl_proxy_infos_mutex);
226                 return TTSD_ERROR_OPERATION_FAILED;
227         }
228
229         info->uid = u_uid;
230         g_tidl_proxy_infos = g_list_append(g_tidl_proxy_infos, info);
231         pthread_mutex_unlock(&g_tidl_proxy_infos_mutex);
232
233         SLOG(LOG_ERROR, tts_tag(), "<<<<<<<<<<<");
234
235         return TTSD_ERROR_NONE;
236 }
237
238 int ttsdc_tidl_send_hello(int pid, unsigned int uid, int ret, int credential_needed)
239 {
240         SLOG(LOG_INFO, tts_tag(), "[TIDL] ttsdc_tidl_send_hello : pid(%d), uid(%u), credential_needed(%d)", pid, uid, credential_needed);
241
242         char tmp_val[10] = {0, };
243         char ret_val[12] = {0, };
244         snprintf(tmp_val, 10, "%d", credential_needed);
245         snprintf(ret_val, 12, "%d", ret);
246
247         bundle* msg = bundle_create();
248         if (NULL == msg) {
249                 SLOG(LOG_ERROR, tts_tag(), "[TIDL] Fail to create bundle: pid(%d), uid(%u)", pid, uid);
250                 return TTSD_ERROR_OUT_OF_MEMORY;
251         }
252
253         bundle_add_str(msg, TTS_BUNDLE_METHOD, TTSD_METHOD_HELLO);
254         bundle_add_str(msg, TTS_BUNDLE_REASON, ret_val);
255         bundle_add_str(msg, TTS_BUNDLE_CREDENTIAL_NEEDED, tmp_val);
256
257         SLOG(LOG_DEBUG, tts_tag(), ">>>>> TTSD SEND HELLO MSG");
258         __send_msg(pid, uid, msg);
259
260         bundle_free(msg);
261         return TTSD_ERROR_NONE;
262 }
263
264 static int __initialize_cb(rpc_port_stub_tts_context_h context, int pid, int uid, int mode, bool *credential_needed, void *user_data)
265 {
266         unsigned int u_uid = (unsigned int)uid;
267         SECURE_SLOG(LOG_ERROR, tts_tag(), "[IN] tts initialize : pid(%d), uid(%u), mode(%d)", pid, u_uid, mode);
268
269         if (0 != ttsd_server_initialize(pid, u_uid, mode, TTS_IPC_METHOD_TIDL, credential_needed)) {
270                 return TTSD_ERROR_OPERATION_FAILED;
271         }
272
273         SLOG(LOG_ERROR, tts_tag(), "[OUT] tts initialize credential_needed(%d)", *credential_needed);
274         return TTSD_ERROR_NONE;
275 }
276
277 static int __finalize_cb(rpc_port_stub_tts_context_h context, int uid, void *user_data)
278 {
279         unsigned int u_uid = (unsigned int)uid;
280         SLOG(LOG_ERROR, tts_tag(), ">>>>> TTS FINALIZE. uid(%u)", u_uid);
281
282         int ret = ttsd_server_finalize(u_uid);
283         if (TTSD_ERROR_NONE != ret) {
284                 SLOG(LOG_ERROR, tts_tag(), "[ERROR] TTS FINALIZE (%u) fail (%d/%s) <<<<<", u_uid, ret, get_error_message(ret));
285                 return ret;
286         }
287
288         pthread_mutex_lock(&g_tidl_proxy_infos_mutex);
289         tts_tidl_proxy_info_s* info = __get_tidl_proxy_info_s(u_uid);
290         if (NULL == info) {
291                 SLOG(LOG_ERROR, tts_tag(), "[TIDL ERROR] Fail to set notify callback");
292                 pthread_mutex_unlock(&g_tidl_proxy_infos_mutex);
293                 return TTSD_ERROR_INVALID_PARAMETER;
294         }
295
296         rpc_port_stub_tts_notify_cb_destroy(info->notify_cb_h);
297         info->notify_cb_h = NULL;
298
299         g_tidl_proxy_infos = g_list_remove(g_tidl_proxy_infos, info);
300         free(info);
301         pthread_mutex_unlock(&g_tidl_proxy_infos_mutex);
302
303         rpc_port_stub_tts_context_set_tag(context, NULL);
304         SLOG(LOG_ERROR, tts_tag(), "<<<<<");
305         return TTSD_ERROR_NONE;
306 }
307
308 static int __add_text_cb(rpc_port_stub_tts_context_h context, int uid, const char *text, const char *lang,
309                                                         int vctype, int speed, int uttid, const char *credential, void *user_data)
310 {
311         unsigned int u_uid = (unsigned int)uid;
312         SLOG(LOG_DEBUG, tts_tag(), ">>>>> TTS ADD TEXT (%u)", u_uid);
313
314         int ret = ttsd_server_add_text(u_uid, text, lang, vctype, speed, uttid, credential);
315         if (TTSD_ERROR_NONE != ret) {
316                 SLOG(LOG_ERROR, tts_tag(), "[ERROR] TTS ADD TEXT (%u) fail (%d/%s) <<<<<", u_uid, ret, get_error_message(ret));
317                 return ret;
318         }
319
320         SLOG(LOG_DEBUG, tts_tag(), "<<<<<");
321         return TTSD_ERROR_NONE;
322 }
323
324 static int __stop_cb(rpc_port_stub_tts_context_h context, int uid, void *user_data)
325 {
326         unsigned int u_uid = (unsigned int)uid;
327         SLOG(LOG_DEBUG, tts_tag(), ">>>>> TTS STOP (%u)", u_uid);
328
329         int ret = ttsd_server_stop(u_uid);
330         if (TTSD_ERROR_NONE != ret) {
331                 SLOG(LOG_ERROR, tts_tag(), "[ERROR] TTS STOP (%u) fail (%d/%s) <<<<<", u_uid, ret, get_error_message(ret));
332                 return ret;
333         }
334
335
336         SLOG(LOG_DEBUG, tts_tag(), "<<<<<");
337
338         return TTSD_ERROR_NONE;
339 }
340
341 static int __pause_cb(rpc_port_stub_tts_context_h context, int uid, void *user_data)
342 {
343         unsigned int u_uid = (unsigned int)uid;
344         SLOG(LOG_DEBUG, tts_tag(), ">>>>> TTS PLAYER PAUSE (%u)", u_uid);
345
346         int ret = ttsd_server_pause(u_uid);
347         if (TTSD_ERROR_NONE != ret) {
348                 SLOG(LOG_ERROR, tts_tag(), "[ERROR] TTS PLAYER PAUSE (%u) fail (%d/%s) <<<<<", u_uid, ret, get_error_message(ret));
349                 return ret;
350         }
351
352         SLOG(LOG_DEBUG, tts_tag(), "<<<<<");
353
354         return TTSD_ERROR_NONE;
355 }
356
357 static int __play_pcm_cb(rpc_port_stub_tts_context_h context, int uid, void *user_data)
358 {
359         unsigned int u_uid = (unsigned int)uid;
360         SLOG(LOG_DEBUG, tts_tag(), ">>>>> TTS PLAY PCM (%u)", u_uid);
361
362         int ret = ttsd_server_play_pcm(u_uid);
363         if (TTSD_ERROR_NONE != ret) {
364                 SLOG(LOG_ERROR, tts_tag(), "[ERROR] TTS PLAY PCM (%u) fail (%d/%s) <<<<<", u_uid, ret, get_error_message(ret));
365                 return ret;
366         }
367
368         SLOG(LOG_DEBUG, tts_tag(), "<<<<<");
369
370         return TTSD_ERROR_NONE;
371 }
372
373 static int __stop_pcm_cb(rpc_port_stub_tts_context_h context, int uid, void *user_data)
374 {
375         unsigned int u_uid = (unsigned int)uid;
376         SLOG(LOG_DEBUG, tts_tag(), ">>>>> TTS STOP PCM (%u)", u_uid);
377
378         int ret = ttsd_server_stop(u_uid);
379         if (TTSD_ERROR_NONE != ret) {
380                 SLOG(LOG_ERROR, tts_tag(), "[ERROR] TTS STOP PCM (%u) fail (%d/%s) <<<<<", u_uid, ret, get_error_message(ret));
381                 return ret;
382         }
383
384         SLOG(LOG_DEBUG, tts_tag(), "<<<<<");
385         return TTSD_ERROR_NONE;
386 }
387
388 static int __set_private_cb(rpc_port_stub_tts_context_h context, int uid, const char *key, const char *data, void *user_data)
389 {
390         unsigned int u_uid = (unsigned int)uid;
391         SLOG(LOG_DEBUG, tts_tag(), ">>>>> TTS SET PRIVATE DATA (%u)", u_uid);
392
393         int ret = ttsd_server_set_private_data(u_uid, key, data);
394         if (TTSD_ERROR_NONE != ret) {
395                 SLOG(LOG_ERROR, tts_tag(), "[ERROR] TTS SET PRIVATE DATA (%u) fail (%d/%s) <<<<<", u_uid, ret, get_error_message(ret));
396                 return ret;
397         }
398
399         SLOG(LOG_DEBUG, tts_tag(), "<<<<<");
400
401         return TTSD_ERROR_NONE;
402 }
403
404 static int __get_private_cb(rpc_port_stub_tts_context_h context, int uid, const char *key, char **data, void *user_data)
405 {
406         unsigned int u_uid = (unsigned int)uid;
407         SLOG(LOG_DEBUG, tts_tag(), ">>>>> TTS GET PRIVATE DATA (%u)", u_uid);
408
409         int ret = ttsd_server_get_private_data(u_uid, key, data);
410         if (TTSD_ERROR_NONE != ret) {
411                 SLOG(LOG_ERROR, tts_tag(), "[ERROR] TTS GET PRIVATE DATA (%u) fail (%d/%s) <<<<<", u_uid, ret, get_error_message(ret));
412                 return ret;
413         }
414
415         SLOG(LOG_DEBUG, tts_tag(), "<<<<<");
416         return TTSD_ERROR_NONE;
417 }
418
419 static int __play_cb(rpc_port_stub_tts_context_h context, int uid, const char *credential, void *user_data)
420 {
421         unsigned int u_uid = (unsigned int)uid;
422         SLOG(LOG_DEBUG, tts_tag(), ">>>>> TTS PLAY (%u)", u_uid);
423
424         int ret = ttsd_server_play(u_uid, credential);
425         if (TTSD_ERROR_NONE != ret) {
426                 SLOG(LOG_ERROR, tts_tag(), "[ERROR] TTS PLAY (%u) fail (%d/%s) <<<<<", u_uid, ret, get_error_message(ret));
427                 return ret;
428         }
429
430         SLOG(LOG_DEBUG, tts_tag(), "<<<<<");
431         return TTSD_ERROR_NONE;
432 }
433
434 static int __add_pcm_cb(rpc_port_stub_tts_context_h context, int uid, int event, rpc_port_stub_array_char_h pcm_data, int data_size, int audio_type, int rate, void *user_data)
435 {
436         unsigned int u_uid = (unsigned int)uid;
437         SLOG(LOG_DEBUG, tts_tag(), ">>>>> TTS ADD PCM (%u)", u_uid);
438
439         char* pcm_data_raw = NULL;
440         int pcm_data_size = 0;
441         rpc_port_stub_array_char_get(pcm_data, &pcm_data_raw, &pcm_data_size);
442
443         int ret = ttsd_server_add_pcm(u_uid, event, (void *)pcm_data_raw, data_size, audio_type, rate);
444         free(pcm_data_raw);
445
446         if (TTSD_ERROR_NONE != ret) {
447                 SLOG(LOG_ERROR, tts_tag(), "[ERROR] TTS ADD PCM (%u) fail (%d/%s) <<<<<", u_uid, ret, get_error_message(ret));
448                 return ret;
449         }
450
451         SLOG(LOG_DEBUG, tts_tag(), "<<<<<");
452         return TTSD_ERROR_NONE;
453 }
454
455 static int __get_service_state(rpc_port_stub_tts_context_h context, int uid, int* service_state, void *user_data)
456 {
457         unsigned int u_uid = (unsigned int)uid;
458         SLOG(LOG_DEBUG, tts_tag(), ">>>>> TTS GET SERVICE STATE (%u)", u_uid);
459
460         int ret = ttsd_server_get_service_state(u_uid, service_state);
461         if (TTSD_ERROR_NONE != ret) {
462                 SLOG(LOG_ERROR, tts_tag(), "[ERROR] TTS GET SERVICE STATE (%u) fail (%d/%s) <<<<<", u_uid, ret, get_error_message(ret));
463                 return ret;
464         }
465
466         SLOG(LOG_DEBUG, tts_tag(), "<<<<<");
467         return TTSD_ERROR_NONE;
468 }
469
470 int ttsd_tidl_open_connection()
471 {
472         SLOG(LOG_DEBUG, tts_tag(), "[TIDL] ttsd_tidl_open_connection");
473
474         g_callback.create = __create_client_cb;
475         g_callback.terminate = __destroy_client_cb;
476         g_callback.register_cb = __register_cb;
477         g_callback.register_cb_sync = __register_cb_sync;
478         g_callback.initialize = __initialize_cb;
479         g_callback.finalize = __finalize_cb;
480         g_callback.add_text = __add_text_cb;
481         g_callback.stop = __stop_cb;
482         g_callback.pause = __pause_cb;
483         g_callback.play_pcm = __play_pcm_cb;
484         g_callback.stop_pcm = __stop_pcm_cb;
485         g_callback.set_private = __set_private_cb;
486         g_callback.get_private = __get_private_cb;
487         g_callback.play = __play_cb;
488         g_callback.add_pcm = __add_pcm_cb;
489         g_callback.get_service_state = __get_service_state;
490
491
492         int ret = -1;
493         int count = 0;
494         while (TTS_RETRY_MIN_COUNT >= count) {
495                 ret = rpc_port_stub_tts_register(&g_callback, NULL);
496                 if (0 == ret) {
497                         SLOG(LOG_DEBUG, tts_tag(), "regitster callback");
498                         return TTSD_ERROR_NONE;
499                 }
500                 usleep(100000);
501                 count++;
502         }
503
504         SLOG(LOG_ERROR, tts_tag(), "Fail to register callback(%d)", ret);
505         return TTSE_ERROR_OPERATION_FAILED;
506 }
507
508 int ttsd_tidl_close_connection()
509 {
510         SLOG(LOG_DEBUG, tts_tag(), "[TIDL] ttsd_tidl_close_connection");
511         rpc_port_stub_tts_unregister();
512
513         return TTSD_ERROR_NONE;
514 }
515
516 static int __send_message(int pid, unsigned int uid, int data, const char *method)
517 {
518         SLOG(LOG_DEBUG, tts_tag(), "[TIDL] ttsdc_tidl_send_message");
519
520         char tmp_msg[10] = {0, };
521
522         bundle* msg = bundle_create();
523         snprintf(tmp_msg, 10, "%d", data);
524         bundle_add_str(msg, TTS_BUNDLE_METHOD, method);
525         bundle_add_str(msg, TTS_BUNDLE_MESSAGE, tmp_msg);
526
527         SLOG(LOG_DEBUG, tts_tag(), ">>>>> TTSD SEND MSG");
528         __send_msg(pid, uid, msg);
529
530         bundle_free(msg);
531         return TTSD_ERROR_NONE;
532 }
533
534 int ttsdc_tidl_send_utt_start_message(int pid, unsigned int uid, int uttid)
535 {
536         return __send_message(pid, uid, uttid, TTSD_METHOD_UTTERANCE_STARTED);
537 }
538
539 int ttsdc_tidl_send_utt_finish_message(int pid, unsigned int uid, int uttid)
540 {
541         return __send_message(pid, uid, uttid, TTSD_METHOD_UTTERANCE_COMPLETED);
542 }
543
544 int ttsdc_tidl_send_set_state_message(int pid, unsigned int uid, int state)
545 {
546         return __send_message(pid, uid, state, TTSD_METHOD_SET_STATE);
547 }
548
549 int ttsdc_tidl_send_set_service_state_message(int pid, unsigned int uid, int before_state, int current_state)
550 {
551         SLOG(LOG_DEBUG, tts_tag(), "[TIDL] ttsdc_tidl_send_set_service_state_message");
552
553         char str_before_state[10] = {0, };
554         char str_current_state[10] = {0, };
555
556         snprintf(str_before_state, 10, "%d", before_state);
557         snprintf(str_current_state, 10, "%d", current_state);
558
559         bundle* msg = bundle_create();
560         bundle_add_str(msg, TTS_BUNDLE_METHOD, TTSD_METHOD_SET_SERVICE_STATE);
561         bundle_add_str(msg, TTS_BUNDLE_BEFORE_STATE, str_before_state);
562         bundle_add_str(msg, TTS_BUNDLE_CURRENT_STATE, str_current_state);
563
564         SLOG(LOG_DEBUG, tts_tag(), ">>>>> TTSD SEND SET SERVICE STATE MESSAGE");
565         __send_msg(pid, uid, msg);
566
567         bundle_free(msg);
568         return TTSD_ERROR_NONE;
569 }
570
571 int ttsdc_tidl_send_error_message(int pid, unsigned int uid, int uttid, int reason, char* err_msg)
572 {
573         SLOG(LOG_DEBUG, tts_tag(), "[TIDL] ttsdc_tidl_send_error_message");
574
575         char tmp_uttid[10] = {0, };
576         char tmp_reason[10] = {0, };
577
578         bundle* msg = bundle_create();
579         snprintf(tmp_uttid, 10, "%d", uttid);
580         bundle_add_str(msg, TTS_BUNDLE_METHOD, TTSD_METHOD_ERROR);
581         bundle_add_str(msg, TTS_BUNDLE_UTTID, tmp_uttid);
582         bundle_add_str(msg, TTS_BUNDLE_REASON, tmp_reason);
583         bundle_add_str(msg, TTS_BUNDLE_ERR_MSG, err_msg);
584
585         SLOG(LOG_DEBUG, tts_tag(), ">>>>> TTSD SEND ERROR MSG");
586         __send_msg(pid, uid, msg);
587
588         bundle_free(msg);
589         return TTSD_ERROR_NONE;
590 }