Fix bugs
[platform/core/uifw/stt.git] / server / sttd_dbus.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 <dbus/dbus.h>
16 #include <Ecore.h>
17
18 #include "sttd_main.h"
19 #include "sttd_dbus.h"
20 #include "sttd_client_data.h"
21 #include "sttd_dbus_server.h"
22 #include "stt_defs.h"
23
24 static DBusConnection* g_conn_sender = NULL;
25 static DBusConnection* g_conn_listener = NULL;
26
27 static Ecore_Fd_Handler* g_dbus_fd_handler = NULL;
28
29 static int g_waiting_time = 3000;
30
31 static int g_internal_result_id = 0;
32
33 int sttdc_send_hello(int uid)
34 {
35         int pid = sttd_client_get_pid(uid);
36         if (0 > pid) {
37                 SLOG(LOG_ERROR, TAG_STTD, "[Dbus ERROR] pid is NOT valid");
38                 return STTD_ERROR_INVALID_PARAMETER;
39         }
40
41         char service_name[64];
42         memset(service_name, 0, 64);
43         snprintf(service_name, 64, "%s%d", STT_CLIENT_SERVICE_NAME, pid);
44
45         char target_if_name[128];
46         snprintf(target_if_name, sizeof(target_if_name), "%s%d", STT_CLIENT_SERVICE_INTERFACE, pid);
47
48         DBusMessage* msg = NULL;
49
50         SLOG(LOG_DEBUG, TAG_STTD, "[Dbus] Send hello message : uid(%d)", uid);
51
52         msg = dbus_message_new_method_call(
53                 service_name,
54                 STT_CLIENT_SERVICE_OBJECT_PATH,
55                 target_if_name,
56                 STTD_METHOD_HELLO);
57
58         if (NULL == msg) {
59                 SLOG(LOG_ERROR, TAG_STTD, "[Dbus ERROR] Fail to create message");
60                 return STTD_ERROR_OUT_OF_MEMORY;
61         }
62
63         dbus_message_append_args(msg, DBUS_TYPE_INT32, &uid, DBUS_TYPE_INVALID);
64
65         DBusError err;
66         dbus_error_init(&err);
67         int result = -1;
68
69         DBusMessage* result_msg = NULL;
70         result_msg = dbus_connection_send_with_reply_and_block(g_conn_sender, msg, g_waiting_time, &err);
71         dbus_message_unref(msg);
72         if (dbus_error_is_set(&err)) {
73                 SLOG(LOG_ERROR, TAG_STTD, "[ERROR] Send error (%s)", err.message);
74                 if (NULL != err.name) {
75                         if (!strcmp(err.name, DBUS_ERROR_SERVICE_UNKNOWN)) {
76                                 SLOG(LOG_ERROR, TAG_STTD, "[ERROR] Unknown service. Client is not available");
77                                 dbus_error_free(&err);
78                                 return 0;
79                         }
80                 }
81                 dbus_error_free(&err);
82         }
83
84         if (NULL != result_msg) {
85                 dbus_message_get_args(result_msg, &err, DBUS_TYPE_INT32, &result, DBUS_TYPE_INVALID);
86
87                 if (dbus_error_is_set(&err)) {
88                         SLOG(LOG_ERROR, TAG_STTD, "[Dbus] Get arguments error (%s)", err.message);
89                         dbus_error_free(&err);
90                 }
91
92                 dbus_message_unref(result_msg);
93         } else {
94                 SLOG(LOG_DEBUG, TAG_STTD, "[Dbus] Result message is NULL. Client is not available");
95         }
96
97         return result;
98 }
99
100 int sttdc_send_set_volume(int uid, float volume)
101 {
102         int pid = sttd_client_get_pid(uid);
103
104         if (0 > pid) {
105                 SLOG(LOG_ERROR, TAG_STTD, "[Dbus ERROR] pid is NOT valid");
106                 return -1;
107         }
108
109         char service_name[64];
110         memset(service_name, 0, 64);
111         snprintf(service_name, 64, "%s%d", STT_CLIENT_SERVICE_NAME, pid);
112
113         char target_if_name[128];
114         snprintf(target_if_name, sizeof(target_if_name), "%s%d", STT_CLIENT_SERVICE_INTERFACE, pid);
115
116         DBusMessage* msg;
117
118         /* SLOG(LOG_DEBUG, TAG_STTD, "[Dbus] Send set volume : uid(%d), volume(%f) volume size(%d)", uid, volume, sizeof(float)); */
119
120         msg = dbus_message_new_signal(
121                 STT_CLIENT_SERVICE_OBJECT_PATH, /* object name of the signal */
122                 target_if_name,                 /* interface name of the signal */
123                 STTD_METHOD_SET_VOLUME);        /* name of the signal */
124
125         if (NULL == msg) {
126                 SLOG(LOG_ERROR, TAG_STTD, "[Dbus ERROR] Fail to create message");
127                 return -1;
128         }
129
130         dbus_message_append_args(msg,
131                 DBUS_TYPE_INT32, &uid,
132                 DBUS_TYPE_INT32, &volume,
133                 DBUS_TYPE_INVALID);
134
135         if (!dbus_connection_send(g_conn_sender, msg, NULL)) {
136                 SLOG(LOG_ERROR, TAG_STTD, "[Dbus ERROR] Fail to send message : Out Of Memory !");
137         } else {
138                 SLOG(LOG_DEBUG, TAG_STTD, "<<<< Send set volume : uid(%d), volume(%f)", uid, volume);
139                 dbus_connection_flush(g_conn_sender);
140         }
141
142         dbus_message_unref(msg);
143
144         return 0;
145 }
146
147 int sttdc_send_set_state(int uid, int state)
148 {
149         int pid = sttd_client_get_pid(uid);
150
151         if (0 > pid) {
152                 SLOG(LOG_ERROR, TAG_STTD, "[Dbus ERROR] pid is NOT valid");
153                 return -1;
154         }
155
156         char service_name[64];
157         memset(service_name, 0, 64);
158         snprintf(service_name, 64, "%s%d", STT_CLIENT_SERVICE_NAME, pid);
159
160         char target_if_name[128];
161         snprintf(target_if_name, sizeof(target_if_name), "%s%d", STT_CLIENT_SERVICE_INTERFACE, pid);
162
163         DBusMessage* msg = NULL;
164         msg = dbus_message_new_signal(
165                 STT_CLIENT_SERVICE_OBJECT_PATH, /* object name of the signal */
166                 target_if_name,                 /* interface name of the signal */
167                 STTD_METHOD_SET_STATE);         /* name of the signal */
168
169         if (NULL == msg) {
170                 SLOG(LOG_ERROR, TAG_STTD, "[Dbus ERROR] Fail to create message");
171                 return -1;
172         }
173
174         dbus_message_append_args(msg,
175                 DBUS_TYPE_INT32, &uid,
176                 DBUS_TYPE_INT32, &state,
177                 DBUS_TYPE_INVALID);
178
179         if (!dbus_connection_send(g_conn_sender, msg, NULL)) {
180                 SLOG(LOG_ERROR, TAG_STTD, "[Dbus ERROR] Fail to send change state message : Out Of Memory !");
181         } else {
182                 SLOG(LOG_DEBUG, TAG_STTD, "<<<< Send change state message : uid(%d), state(%d)", uid, state);
183                 dbus_connection_flush(g_conn_sender);
184         }
185
186         dbus_message_unref(msg);
187
188         return 0;
189 }
190
191 int sttdc_send_result(int uid, int event, const char** data, int data_count, const char* result_msg)
192 {
193         int pid = sttd_client_get_pid(uid);
194         if (0 > pid) {
195                 SLOG(LOG_ERROR, TAG_STTD, "[Dbus ERROR] pid is NOT valid");
196                 return STTD_ERROR_INVALID_PARAMETER;
197         }
198
199         char service_name[64];
200         memset(service_name, 0, 64);
201         snprintf(service_name, 64, "%s%d", STT_CLIENT_SERVICE_NAME, pid);
202
203         char target_if_name[128];
204         memset(target_if_name, 0, 128);
205         snprintf(target_if_name, sizeof(target_if_name), "%s%d", STT_CLIENT_SERVICE_INTERFACE, pid);
206
207         DBusMessage* msg = NULL;
208         SLOG(LOG_DEBUG, TAG_STTD, "[Dbus] send result signal : uid(%d), event(%d), result count(%d) result id(%d)", 
209                 uid, event, data_count, g_internal_result_id);
210
211         msg = dbus_message_new_signal(
212                 STT_CLIENT_SERVICE_OBJECT_PATH, /* object name of the signal */
213                 target_if_name,                 /* interface name of the signal */
214                 STTD_METHOD_RESULT);            /* name of the signal */
215
216         if (NULL == msg) {
217                 SLOG(LOG_ERROR, TAG_STTD, "[Dbus ERROR] Fail to create message");
218                 return STTD_ERROR_OUT_OF_MEMORY;
219         }
220
221         DBusMessageIter args;
222         dbus_message_iter_init_append(msg, &args);
223
224         /* Append uid & type */
225         dbus_message_iter_append_basic(&args, DBUS_TYPE_INT32, &uid);
226
227         char* msg_temp;
228         dbus_message_iter_append_basic(&args, DBUS_TYPE_INT32, &event);
229
230         /* Append result msg */
231         if (NULL == result_msg) {
232                 msg_temp = strdup("None");
233                 dbus_message_iter_append_basic(&args, DBUS_TYPE_STRING, &msg_temp);
234                 SLOG(LOG_WARN, TAG_STTD, "[Dbus] result message is NULL");
235                 free(msg_temp);
236         } else {
237                 SLOG(LOG_DEBUG, TAG_STTD, "[Dbus] result message(%s)", result_msg);
238                 dbus_message_iter_append_basic(&args, DBUS_TYPE_STRING, &(result_msg));
239         }
240
241         /* Append result size */
242         if (!dbus_message_iter_append_basic(&args, DBUS_TYPE_INT32, &(data_count))) {
243                 SLOG(LOG_ERROR, TAG_STTD, "[Dbus] response message : Fail to append result size");
244                 dbus_message_unref(msg);
245                 return -1;
246         }
247
248         if (!dbus_message_iter_append_basic(&args, DBUS_TYPE_INT32, &(g_internal_result_id))) {
249                 SLOG(LOG_ERROR, TAG_STTD, "[Dbus] response message : Fail to append internal result id");
250                 dbus_message_unref(msg);
251                 return -1;
252         }
253
254         g_internal_result_id++;
255
256         if (10000 == g_internal_result_id) {
257                 g_internal_result_id = 1;
258         }
259
260         int i;
261         SLOG(LOG_DEBUG, TAG_STTD, "[Dbus] result size (%d)", data_count);
262         for (i = 0; i < data_count; i++) {
263                 if (NULL != data[i]) {
264                         SLOG(LOG_DEBUG, TAG_STTD, "[Dbus] result (%d, %s)", i, data[i]);
265
266                         if (!dbus_message_iter_append_basic(&args, DBUS_TYPE_STRING, &data[i])) {
267                                 SLOG(LOG_ERROR, TAG_STTD, "[Dbus] response message : Fail to append result data");
268                                 dbus_message_unref(msg);
269                                 return STTD_ERROR_OPERATION_FAILED;
270                         }
271                 } else {
272                         int reason = (int)STTD_ERROR_OPERATION_FAILED;
273
274                         if (0 != sttdc_send_error_signal(uid, reason, "Fail to get recognition result from engine")) {
275                                 SLOG(LOG_ERROR, TAG_STTD, "[Dbus ERROR] Fail to send error info. Remove client data");
276
277                                 /* clean client data */
278                                 sttd_client_delete(uid);
279                         }
280
281                         dbus_message_unref(msg);
282                         SLOG(LOG_ERROR, TAG_STTD, "[Dbus ERROR] Result from engine is NULL(%d)", i);
283                         return STTD_ERROR_OPERATION_FAILED;
284                 }
285         }
286
287         if (!dbus_connection_send(g_conn_sender, msg, NULL)) {
288                 SLOG(LOG_ERROR, TAG_STTD, "[Dbus ERROR] Fail to send message : Out Of Memory !");
289         }
290
291         dbus_connection_flush(g_conn_sender);
292         dbus_message_unref(msg);
293
294         return 0;
295 }
296
297 int sttdc_send_error_signal(int uid, int reason, const char *err_msg)
298 {
299         if (NULL == err_msg) {
300                 SLOG(LOG_ERROR, TAG_STTD, "[Dbus ERROR] Input parameter is NULL");
301                 return STTD_ERROR_INVALID_PARAMETER;
302         }
303
304         int pid = sttd_client_get_pid(uid);
305         if (0 > pid) {
306                 SLOG(LOG_ERROR, TAG_STTD, "[Dbus ERROR] pid is NOT valid");
307                 return STTD_ERROR_INVALID_PARAMETER;
308         }
309
310         char service_name[64];
311         memset(service_name, 0, 64);
312         snprintf(service_name, 64, "%s%d", STT_CLIENT_SERVICE_NAME, pid);
313
314         char target_if_name[128];
315         snprintf(target_if_name, sizeof(target_if_name), "%s%d", STT_CLIENT_SERVICE_INTERFACE, pid);
316
317         DBusMessage* msg = NULL;
318         msg = dbus_message_new_signal(
319                 STT_CLIENT_SERVICE_OBJECT_PATH, /* object name of the signal */
320                 target_if_name,                 /* interface name of the signal */
321                 STTD_METHOD_ERROR);             /* name of the signal */
322
323         if (NULL == msg) {
324                 SLOG(LOG_ERROR, TAG_STTD, "[Dbus ERROR] Fail to create message");
325                 return STTD_ERROR_OUT_OF_MEMORY;
326         }
327
328         dbus_message_append_args(msg,
329                 DBUS_TYPE_INT32, &uid,
330                 DBUS_TYPE_INT32, &reason,
331                 DBUS_TYPE_STRING, &err_msg,
332                 DBUS_TYPE_INVALID);
333
334         if (!dbus_connection_send(g_conn_sender, msg, NULL)) {
335                 SLOG(LOG_ERROR, TAG_STTD, "[Dbus ERROR] <<<< error message : Out Of Memory !");
336         } else {
337                 SLOG(LOG_DEBUG, TAG_STTD, "<<<< Send error message : uid(%d), reason(%d), err_msg(%s)", uid, reason, (NULL == err_msg) ? "NULL" : err_msg);
338                 dbus_connection_flush(g_conn_sender);
339         }
340
341         dbus_message_unref(msg);
342
343         return 0;
344 }
345
346 static Eina_Bool listener_event_callback(void* data, Ecore_Fd_Handler *fd_handler)
347 {
348         if (NULL == g_conn_listener)    return ECORE_CALLBACK_RENEW;
349
350         dbus_connection_read_write_dispatch(g_conn_listener, 50);
351
352         DBusMessage* msg = NULL;
353         msg = dbus_connection_pop_message(g_conn_listener);
354
355         if (true != dbus_connection_get_is_connected(g_conn_listener)) {
356                 SLOG(LOG_ERROR, TAG_STTD, "[Dbus ERROR] Connection is disconnected");
357                 return ECORE_CALLBACK_RENEW;
358         }
359
360         /* loop again if we haven't read a message */
361         if (NULL == msg) {
362                 return ECORE_CALLBACK_RENEW;
363         }
364
365         /* client event */
366         if (dbus_message_is_method_call(msg, STT_SERVER_SERVICE_INTERFACE, STT_METHOD_HELLO))
367                 sttd_dbus_server_hello(g_conn_listener, msg);
368
369         else if (dbus_message_is_method_call(msg, STT_SERVER_SERVICE_INTERFACE, STT_METHOD_INITIALIZE))
370                 sttd_dbus_server_initialize(g_conn_listener, msg);
371
372         else if (dbus_message_is_method_call(msg, STT_SERVER_SERVICE_INTERFACE, STT_METHOD_FINALIZE))
373                 sttd_dbus_server_finalize(g_conn_listener, msg);
374
375         else if (dbus_message_is_method_call(msg, STT_SERVER_SERVICE_INTERFACE, STT_METHOD_SET_CURRENT_ENGINE))
376                 sttd_dbus_server_set_current_engine(g_conn_listener, msg);
377
378         else if (dbus_message_is_method_call(msg, STT_SERVER_SERVICE_INTERFACE, STT_METHOD_CHECK_APP_AGREED))
379                 sttd_dbus_server_check_app_agreed(g_conn_listener, msg);
380
381         else if (dbus_message_is_method_call(msg, STT_SERVER_SERVICE_INTERFACE, STT_METHOD_GET_SUPPORT_LANGS))
382                 sttd_dbus_server_get_support_lang(g_conn_listener, msg);
383
384         else if (dbus_message_is_method_call(msg, STT_SERVER_SERVICE_INTERFACE, STT_METHOD_GET_CURRENT_LANG))
385                 sttd_dbus_server_get_default_lang(g_conn_listener, msg);
386
387         else if (dbus_message_is_method_call(msg, STT_SERVER_SERVICE_INTERFACE, STT_METHOD_SET_PRIVATE_DATA))
388                 sttd_dbus_server_set_private_data(g_conn_listener, msg);
389
390         else if (dbus_message_is_method_call(msg, STT_SERVER_SERVICE_INTERFACE, STT_METHOD_GET_PRIVATE_DATA))
391                 sttd_dbus_server_get_private_data(g_conn_listener, msg);
392
393         else if (dbus_message_is_method_call(msg, STT_SERVER_SERVICE_INTERFACE, STT_METHOD_IS_TYPE_SUPPORTED))
394                 sttd_dbus_server_is_recognition_type_supported(g_conn_listener, msg);
395
396
397         else if (dbus_message_is_method_call(msg, STT_SERVER_SERVICE_INTERFACE, STT_METHOD_SET_START_SOUND))
398                 sttd_dbus_server_set_start_sound(g_conn_listener, msg);
399
400         else if (dbus_message_is_method_call(msg, STT_SERVER_SERVICE_INTERFACE, STT_METHOD_UNSET_START_SOUND))
401                 sttd_dbus_server_unset_start_sound(g_conn_listener, msg);
402
403         else if (dbus_message_is_method_call(msg, STT_SERVER_SERVICE_INTERFACE, STT_METHOD_SET_STOP_SOUND))
404                 sttd_dbus_server_set_stop_sound(g_conn_listener, msg);
405
406         else if (dbus_message_is_method_call(msg, STT_SERVER_SERVICE_INTERFACE, STT_METHOD_UNSET_STOP_SOUND))
407                 sttd_dbus_server_unset_stop_sound(g_conn_listener, msg);
408
409
410         else if (dbus_message_is_method_call(msg, STT_SERVER_SERVICE_INTERFACE, STT_METHOD_START))
411                 sttd_dbus_server_start(g_conn_listener, msg);
412
413         else if (dbus_message_is_method_call(msg, STT_SERVER_SERVICE_INTERFACE, STT_METHOD_STOP))
414                 sttd_dbus_server_stop(g_conn_listener, msg);
415
416         else if (dbus_message_is_method_call(msg, STT_SERVER_SERVICE_INTERFACE, STT_METHOD_CANCEL))
417                 sttd_dbus_server_cancel(g_conn_listener, msg);
418
419
420         /* free the message */
421         dbus_message_unref(msg);
422
423         return ECORE_CALLBACK_RENEW;
424 }
425
426 int sttd_dbus_open_connection()
427 {
428         DBusError err;
429         dbus_error_init(&err);
430
431         int ret;
432
433         /* Create connection for sender */
434         g_conn_sender = dbus_bus_get_private(DBUS_BUS_SESSION, &err);
435         if (dbus_error_is_set(&err)) {
436                 SLOG(LOG_ERROR, TAG_STTD, "[Dbus ERROR] Fail dbus_bus_get : %s", err.message);
437                 dbus_error_free(&err);
438         }
439
440         if (NULL == g_conn_sender) {
441                 SLOG(LOG_ERROR, TAG_STTD, "[Dbus ERROR] Fail to get dbus connection sender");
442                 return STTD_ERROR_OPERATION_FAILED;
443         }
444
445         /* connect to the bus and check for errors */
446         g_conn_listener = dbus_bus_get_private(DBUS_BUS_SESSION, &err);
447
448         if (dbus_error_is_set(&err)) {
449                 SLOG(LOG_ERROR, TAG_STTD, "[Dbus ERROR] Fail dbus_bus_get : %s", err.message);
450                 dbus_error_free(&err);
451         }
452
453         if (NULL == g_conn_listener) {
454                 SLOG(LOG_ERROR, TAG_STTD, "[Dbus ERROR] Fail to get dbus connection");
455                 return STTD_ERROR_OPERATION_FAILED;
456         }
457
458         /* request our name on the bus and check for errors */
459         ret = dbus_bus_request_name(g_conn_listener, STT_SERVER_SERVICE_NAME, DBUS_NAME_FLAG_REPLACE_EXISTING, &err);
460
461         if (DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER != ret) {
462                 SLOG(LOG_ERROR, TAG_STTD, "[Dbus ERROR] Fail to be primary owner");
463                 return STTD_ERROR_OPERATION_FAILED;
464         }
465
466         if (dbus_error_is_set(&err)) {
467                 SLOG(LOG_ERROR, TAG_STTD, "[Dbus ERROR] dbus_bus_request_name() : %s", err.message);
468                 dbus_error_free(&err);
469                 return STTD_ERROR_OPERATION_FAILED;
470         }
471
472         /* add a rule for getting signal */
473         char rule[128];
474         snprintf(rule, 128, "type='signal',interface='%s'", STT_SERVER_SERVICE_INTERFACE);
475
476         /* add a rule for which messages we want to see */
477         dbus_bus_add_match(g_conn_listener, rule, &err); /* see signals from the given interface */
478         dbus_connection_flush(g_conn_listener);
479
480         if (dbus_error_is_set(&err)) {
481                 SLOG(LOG_ERROR, TAG_STTD, "[Dbus ERROR] dbus_bus_add_match() : %s", err.message);
482                 dbus_error_free(&err);
483                 return STTD_ERROR_OPERATION_FAILED;
484         }
485
486         int fd = 0;
487         dbus_connection_get_unix_fd(g_conn_listener, &fd);
488
489         g_dbus_fd_handler = ecore_main_fd_handler_add(fd, ECORE_FD_READ, (Ecore_Fd_Cb)listener_event_callback, g_conn_listener, NULL, NULL);
490         if (NULL == g_dbus_fd_handler) {
491                 SLOG(LOG_ERROR, TAG_STTD, "[Dbus ERROR] Fail to get fd handler");
492                 return STTD_ERROR_OPERATION_FAILED;
493         }
494
495         return 0;
496 }
497
498 int sttd_dbus_close_connection()
499 {
500         DBusError err;
501         dbus_error_init(&err);
502
503         if (NULL != g_dbus_fd_handler) {
504                 ecore_main_fd_handler_del(g_dbus_fd_handler);
505                 g_dbus_fd_handler = NULL;
506         }
507
508         dbus_bus_release_name(g_conn_listener, STT_SERVER_SERVICE_NAME, &err);
509
510         if (dbus_error_is_set(&err)) {
511                 SLOG(LOG_ERROR, TAG_STTD, "[Dbus ERROR] dbus_bus_release_name() : %s", err.message);
512                 dbus_error_free(&err);
513         }
514
515         dbus_connection_close(g_conn_listener);
516         dbus_connection_close(g_conn_sender);
517
518         dbus_connection_unref(g_conn_sender);
519         dbus_connection_unref(g_conn_listener);
520
521         g_conn_listener = NULL;
522         g_conn_sender = NULL;
523
524         SLOG(LOG_DEBUG, TAG_STTD, "[Server] Close dbus connection");
525
526         return 0;
527 }