Update year information of license boilerplate
[platform/core/uifw/multi-assistant-service.git] / src / multi_assistant_dbus.c
1 /*
2  * Copyright 2018-2019 Samsung Electronics Co., Ltd
3  *
4  * Licensed under the Flora License, Version 1.1 (the License);
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://floralicense.org/license/
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an AS IS BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #include <dbus/dbus.h>
18 #include <dirent.h>
19 #include <dlfcn.h>
20 #include <Ecore.h>
21
22 #include "multi_assistant_main.h"
23 #include "multi_assistant_dbus_server.h"
24 #include "multi_assistant_dbus.h"
25
26 static DBusConnection* g_conn_sender = NULL;
27 static DBusConnection* g_conn_listener = NULL;
28
29 static Ecore_Fd_Handler* g_dbus_fd_handler = NULL;
30
31 int mas_dbus_reconnect()
32 {
33         if (!g_conn_sender || !g_conn_listener) {
34                 mas_dbus_close_connection();
35
36                 if (0 != mas_dbus_open_connection()) {
37                         MAS_LOGE("[ERROR] Fail to reconnect");
38                         return -1;
39                 }
40
41                 MAS_LOGD("[DBUS] Reconnect");
42                 return 0;
43         }
44
45         bool sender_connected = dbus_connection_get_is_connected(g_conn_sender);
46         bool listener_connected = dbus_connection_get_is_connected(g_conn_listener);
47         MAS_LOGW("[DBUS] Sender(%s) Listener(%s)",
48                 sender_connected ? "Connected" : "Not connected", listener_connected ? "Connected" : "Not connected");
49
50         if (false == sender_connected || false == listener_connected) {
51                 mas_dbus_close_connection();
52
53                 if (0 != mas_dbus_open_connection()) {
54                         MAS_LOGE("[ERROR] Fail to reconnect");
55                         return -1;
56                 }
57
58                 MAS_LOGD("[DBUS] Reconnect");
59         }
60
61         return 0;
62 }
63
64 static int __dbus_check()
65 {
66         if (NULL == g_conn_sender || NULL == g_conn_listener) {
67                 MAS_LOGE("[ERROR] NULL connection");
68                 return mas_dbus_reconnect();
69         }
70         return 0;
71 }
72
73 int mas_check_dbus_connection()
74 {
75         if (NULL == g_conn_sender || NULL == g_conn_listener) {
76                 MAS_LOGE("[ERROR] NULL connection sender(%p), listener(%p)", g_conn_sender, g_conn_listener);
77                 return -1;
78         }
79         return 0;
80 }
81
82 int masc_dbus_send_hello(int pid)
83 {
84         if (0 != __dbus_check()) {
85                 return -1; //MAS_ERROR_OPERATION_FAILED;
86         }
87
88         DBusMessage* msg;
89         DBusError err;
90         dbus_error_init(&err);
91
92         msg = dbus_message_new_method_call(
93                         MA_CLIENT_SERVICE_NAME,
94                         MA_CLIENT_SERVICE_OBJECT_PATH,
95                         MA_CLIENT_SERVICE_INTERFACE,
96                         MAS_METHOD_HELLO);
97
98         if (NULL == msg) {
99                 MAS_LOGE("[DBus ERROR] Request masc hello : Fail to make message");
100                 return -1;
101         }
102
103         int result = -1;
104         DBusMessage* result_msg = NULL;
105         result_msg = dbus_connection_send_with_reply_and_block(g_conn_sender, msg, 500, &err);
106
107         if (dbus_error_is_set(&err)) {
108                 MAS_LOGE("[Dbus ERROR] Dbus Error (%s)", err.message);
109                 dbus_error_free(&err);
110         }
111
112         dbus_message_unref(msg);
113
114         if (NULL != result_msg) {
115                 dbus_message_unref(result_msg);
116                 result = 0;
117         } else {
118                 result = -1; //MAS_ERROR_TIMED_OUT;
119         }
120
121         return result;
122 }
123
124 int masc_dbus_send_error_message(int reason, const char* err_msg)
125 {
126         if (0 != __dbus_check()) {
127                 return -1; //MAS_ERROR_OPERATION_FAILED;
128         }
129
130         if (NULL == g_conn_sender) {
131                 MAS_LOGE("[Dbus ERROR] Dbus connection is not available");
132                 return -1;
133         }
134
135         DBusMessage* msg = NULL;
136
137         /* create a message */
138         msg = dbus_message_new_signal(
139                 MA_CLIENT_SERVICE_OBJECT_PATH,  /* object name of the signal */
140                 MA_CLIENT_SERVICE_INTERFACE,    /* interface name of the signal */
141                 MAS_METHOD_ERROR);              /* name of the signal */
142
143         if (NULL == msg) {
144                 MAS_LOGE("[Dbus ERROR] Fail to create error message");
145                 return -1;
146         }
147
148         char* temp_err_msg = NULL;
149
150         if (err_msg)
151                 temp_err_msg = strdup(err_msg);
152         else
153                 temp_err_msg = strdup("#NULL");
154
155         dbus_message_append_args(msg,
156                 DBUS_TYPE_INT32, &reason,
157                 DBUS_TYPE_STRING, &temp_err_msg,
158                 DBUS_TYPE_INVALID);
159
160         dbus_message_set_no_reply(msg, TRUE);
161
162         if (!dbus_connection_send(g_conn_sender, msg, NULL)) {
163                 MAS_LOGE("[Dbus ERROR] <<<< error message : Out Of Memory !");
164         } else {
165                 MAS_LOGD("<<<< Send error message : reason(%d), err_msg(%s)", reason, temp_err_msg);
166                 dbus_connection_flush(g_conn_sender);
167         }
168
169         dbus_message_unref(msg);
170
171         if (temp_err_msg)
172                 free(temp_err_msg);
173         return 0;
174 }
175
176 int masc_dbus_send_streaming_audio_data(int pid, int event, unsigned char* data, unsigned int data_size)
177 {
178         if (0 != __dbus_check()) {
179                 return -1; //MAS_ERROR_OPERATION_FAILED;
180         }
181
182         DBusMessage* msg;
183         DBusError err;
184         dbus_error_init(&err);
185
186         char service_name[64];
187         memset(service_name, '\0', 64);
188         snprintf(service_name, 64, "%s_%d", MA_CLIENT_SERVICE_NAME, pid);
189
190         msg = dbus_message_new_method_call(
191                         service_name,
192                         MA_CLIENT_SERVICE_OBJECT_PATH,
193                         MA_CLIENT_SERVICE_INTERFACE,
194                         MAS_METHOD_STREAMING_AUDIO_DATA);
195
196         static int count = 0;
197         if (NULL == msg) {
198                 MAS_LOGE(">>>> Request mas send utterance stream : Fail to make message");
199                 return -1; // MAS_ERROR_OPERATION_FAILED;
200         } else {
201                 MAS_LOGD(">>>> Request mas send utterance stream : %s event(%d) %d", service_name, event, count++);
202         }
203
204         if (true != dbus_message_append_args(msg,
205                 DBUS_TYPE_INT32, &event,
206                 DBUS_TYPE_ARRAY, DBUS_TYPE_BYTE,
207                 &data, data_size,
208                 DBUS_TYPE_INVALID)) {
209                 dbus_message_unref(msg);
210                 MAS_LOGE("[ERROR] Fail to append args");
211                 return -1;
212         }
213
214         dbus_message_set_no_reply(msg, TRUE);
215
216         if (1 != dbus_connection_send(g_conn_sender, msg, NULL)) {
217                 MAS_LOGE("[Dbus ERROR] Fail to Send");
218                 return -1; // MAS_ERROR_OPERATION_FAILED;
219         } else {
220                 MAS_LOGD("[Dbus DEBUG] Success to Send utterance stream");
221                 dbus_connection_flush(g_conn_sender);
222         }
223
224         dbus_message_unref(msg);
225         return 0;
226 }
227
228 int masc_dbus_active_state_change(int pid, int state)
229 {
230         if (0 != __dbus_check()) {
231                 return -1; //MAS_ERROR_OPERATION_FAILED;
232         }
233
234         DBusMessage* msg;
235
236         DBusError err;
237         dbus_error_init(&err);
238
239         char service_name[64];
240         memset(service_name, '\0', 64);
241         snprintf(service_name, 64, "%s_%d", MA_CLIENT_SERVICE_NAME, pid);
242
243         msg = dbus_message_new_method_call(
244                         service_name,
245                         MA_CLIENT_SERVICE_OBJECT_PATH,
246                         MA_CLIENT_SERVICE_INTERFACE,
247                         MAS_METHOD_ACTIVE_STATE_CHANGE);
248
249         static int count = 0;
250         if (NULL == msg) {
251                 MAS_LOGE(">>>> Request mas send activate message : Fail to make message");
252                 return -1; // MAS_ERROR_OPERATION_FAILED;
253         } else {
254                 MAS_LOGD(">>>> Request mas send activate message : %s", service_name);
255         }
256
257         if (true != dbus_message_append_args(msg,
258                 DBUS_TYPE_INT32, &state,
259                 DBUS_TYPE_INVALID)) {
260                 dbus_message_unref(msg);
261                 MAS_LOGE("[ERROR] Fail to append args");
262                 return -1;
263         }
264
265         dbus_message_set_no_reply(msg, TRUE);
266
267         if (1 != dbus_connection_send(g_conn_sender, msg, NULL)) {
268                 MAS_LOGE("[Dbus ERROR] Fail to Send");
269                 return -1; // MAS_ERROR_OPERATION_FAILED;
270         } else {
271                 MAS_LOGD("[Dbus DEBUG] Success to Send activate message : %d", state);
272                 dbus_connection_flush(g_conn_sender);
273         }
274
275         dbus_message_unref(msg);
276         return 0;
277 }
278
279 int masc_dbus_send_preprocessing_information(int pid, const char* app_id)
280 {
281         if (0 != __dbus_check()) {
282                 return -1; //MAS_ERROR_OPERATION_FAILED;
283         }
284
285         DBusMessage* msg;
286
287         DBusError err;
288         dbus_error_init(&err);
289
290         char service_name[64];
291         memset(service_name, '\0', 64);
292         snprintf(service_name, 64, "%s_%d", MA_CLIENT_SERVICE_NAME, pid);
293
294         msg = dbus_message_new_method_call(
295                         service_name,
296                         MA_CLIENT_SERVICE_OBJECT_PATH,
297                         MA_CLIENT_SERVICE_INTERFACE,
298                         MAS_METHOD_SEND_PREPROCESSING_INFORMATION);
299
300         static int count = 0;
301         if (NULL == msg) {
302                 MAS_LOGE(">>>> Request mas send preprocessing assistant information : Fail to make message");
303                 return -1; // MAS_ERROR_OPERATION_FAILED;
304         } else {
305                 MAS_LOGD(">>>> Request mas send preprocessing assistant information : %s", service_name);
306         }
307
308         char* temp_app_id = NULL;
309         if (!app_id)
310                 temp_app_id = strdup("#NULL");
311         else
312                 temp_app_id = strdup(app_id);
313
314         if (true != dbus_message_append_args(msg,
315                 DBUS_TYPE_STRING, &temp_app_id,
316                 DBUS_TYPE_INVALID)) {
317                 dbus_message_unref(msg);
318                 MAS_LOGE("[ERROR] Fail to append args");
319                 if (temp_app_id)
320                         free(temp_app_id);
321                 return -1;
322         }
323
324         dbus_message_set_no_reply(msg, TRUE);
325
326         if (1 != dbus_connection_send(g_conn_sender, msg, NULL)) {
327                 MAS_LOGE("[Dbus ERROR] Fail to Send");
328                 if (temp_app_id)
329                         free(temp_app_id);
330                 return -1; // MAS_ERROR_OPERATION_FAILED;
331         } else {
332                 MAS_LOGD("[Dbus DEBUG] Success to Send preprocessing assistant information : %s", app_id);
333                 dbus_connection_flush(g_conn_sender);
334         }
335
336         dbus_message_unref(msg);
337
338         if (temp_app_id)
339                 free(temp_app_id);
340
341         return 0;
342 }
343
344 int masc_dbus_send_streaming_section_changed(int pid, int section)
345 {
346         if (0 != __dbus_check()) {
347                 return -1; //MAS_ERROR_OPERATION_FAILED;
348         }
349
350         DBusMessage* msg;
351
352         DBusError err;
353         dbus_error_init(&err);
354
355         char service_name[64];
356         memset(service_name, '\0', 64);
357         snprintf(service_name, 64, "%s_%d", MA_CLIENT_SERVICE_NAME, pid);
358
359         msg = dbus_message_new_method_call(
360                         service_name,
361                         MA_CLIENT_SERVICE_OBJECT_PATH,
362                         MA_CLIENT_SERVICE_INTERFACE,
363                         MAS_METHOD_AUDIO_STREAMING_DATA_SECTION);
364
365         static int count = 0;
366         if (NULL == msg) {
367                 MAS_LOGE(">>>> Request mas send streaming section changed information : Fail to make message");
368                 return -1; // MAS_ERROR_OPERATION_FAILED;
369         } else {
370                 MAS_LOGD(">>>> Request mas send streaming section changed information : %s", service_name);
371         }
372
373         if (true != dbus_message_append_args(msg,
374                 DBUS_TYPE_INT32, &section,
375                 DBUS_TYPE_INVALID)) {
376                 dbus_message_unref(msg);
377                 MAS_LOGE("[ERROR] Fail to append args");
378                 return -1;
379         }
380
381         dbus_message_set_no_reply(msg, TRUE);
382
383         if (1 != dbus_connection_send(g_conn_sender, msg, NULL)) {
384                 MAS_LOGE("[Dbus ERROR] Fail to Send");
385                 return -1; // MAS_ERROR_OPERATION_FAILED;
386         } else {
387                 MAS_LOGD("[Dbus DEBUG] Success to Send streaming section changed information : %d", section);
388                 dbus_connection_flush(g_conn_sender);
389         }
390
391         dbus_message_unref(msg);
392
393         return 0;
394 }
395
396 int masc_dbus_send_preprocessing_result(int pid, bool result)
397 {
398         if (0 != __dbus_check()) {
399                 return -1; //MAS_ERROR_OPERATION_FAILED;
400         }
401
402         DBusMessage* msg;
403
404         DBusError err;
405         dbus_error_init(&err);
406
407         char service_name[64];
408         memset(service_name, '\0', 64);
409         snprintf(service_name, 64, "%s_%d", MA_CLIENT_SERVICE_NAME, pid);
410
411         msg = dbus_message_new_method_call(
412                         service_name,
413                         MA_CLIENT_SERVICE_OBJECT_PATH,
414                         MA_CLIENT_SERVICE_INTERFACE,
415                         MAS_METHOD_SEND_PREPROCESSING_RESULT);
416
417         static int count = 0;
418         if (NULL == msg) {
419                 MAS_LOGE(">>>> Request mas send preprocessing result : Fail to make message");
420                 return -1; // MAS_ERROR_OPERATION_FAILED;
421         } else {
422                 MAS_LOGD(">>>> Request mas send preprocessing result : %s", service_name);
423         }
424
425         int temp_result = result;
426
427         if (true != dbus_message_append_args(msg,
428                 DBUS_TYPE_INT32, &temp_result,
429                 DBUS_TYPE_INVALID)) {
430                 dbus_message_unref(msg);
431                 MAS_LOGE("[ERROR] Fail to append args");
432                 return -1;
433         }
434
435         dbus_message_set_no_reply(msg, TRUE);
436
437         if (1 != dbus_connection_send(g_conn_sender, msg, NULL)) {
438                 MAS_LOGE("[Dbus ERROR] Fail to Send");
439                 return -1; // MAS_ERROR_OPERATION_FAILED;
440         } else {
441                 MAS_LOGD("[Dbus DEBUG] Success to Send preprocessing result : %d", temp_result);
442                 dbus_connection_flush(g_conn_sender);
443         }
444
445         dbus_message_unref(msg);
446
447         return 0;
448 }
449
450 int masc_dbus_send_wakeup_engine_command(int pid, const char* command)
451 {
452         if (0 != __dbus_check()) {
453                 return -1; //MAS_ERROR_OPERATION_FAILED;
454         }
455
456         DBusMessage* msg;
457
458         DBusError err;
459         dbus_error_init(&err);
460
461         char service_name[64];
462         memset(service_name, '\0', 64);
463         snprintf(service_name, 64, "%s_%d", MA_CLIENT_SERVICE_NAME, pid);
464
465         msg = dbus_message_new_method_call(
466                         service_name,
467                         MA_CLIENT_SERVICE_OBJECT_PATH,
468                         MA_CLIENT_SERVICE_INTERFACE,
469                         MAS_METHOD_SEND_WAKEUP_ENGINE_COMMAND);
470
471         static int count = 0;
472         if (NULL == msg) {
473                 MAS_LOGE(">>>> Request mas send wakeup engine command : Fail to make message");
474                 return -1; // MAS_ERROR_OPERATION_FAILED;
475         } else {
476                 MAS_LOGD(">>>> Request mas send wakeup engine command : %s", service_name);
477         }
478
479         char* temp_command = NULL;
480         if (!command)
481                 temp_command = strdup("#NULL");
482         else
483                 temp_command = strdup(command);
484
485         if (true != dbus_message_append_args(msg,
486                 DBUS_TYPE_STRING, &temp_command,
487                 DBUS_TYPE_INVALID)) {
488                 dbus_message_unref(msg);
489                 MAS_LOGE("[ERROR] Fail to append args");
490                 if (temp_command)
491                         free(temp_command);
492                 return -1;
493         }
494
495         dbus_message_set_no_reply(msg, TRUE);
496
497         if (1 != dbus_connection_send(g_conn_sender, msg, NULL)) {
498                 MAS_LOGE("[Dbus ERROR] Fail to Send");
499                 if (temp_command)
500                         free(temp_command);
501                 return -1; // MAS_ERROR_OPERATION_FAILED;
502         } else {
503                 MAS_LOGD("[Dbus DEBUG] Success to Send wakeup_engine_command : %s", command);
504                 dbus_connection_flush(g_conn_sender);
505         }
506
507         dbus_message_unref(msg);
508
509         if (temp_command)
510                 free(temp_command);
511
512         return 0;
513 }
514
515 int masc_ui_dbus_send_hello(void)
516 {
517         if (0 != __dbus_check()) {
518                 return -1; //MAS_ERROR_OPERATION_FAILED;
519         }
520
521         DBusMessage* msg;
522
523         DBusError err;
524         dbus_error_init(&err);
525
526         msg = dbus_message_new_method_call(
527                         MA_UI_CLIENT_SERVICE_NAME,
528                         MA_UI_CLIENT_SERVICE_OBJECT_PATH,
529                         MA_UI_CLIENT_SERVICE_INTERFACE,
530                         MAS_METHOD_HELLO);
531
532         if (NULL == msg) {
533                 MAS_LOGE("[DBus ERROR] Request masc hello : Fail to make message");
534                 return -1;
535         }
536
537         DBusMessage* result_msg = NULL;
538         int result = 0;
539
540         result_msg = dbus_connection_send_with_reply_and_block(g_conn_sender, msg, 500, &err);
541
542         if (dbus_error_is_set(&err)) {
543                 MAS_LOGE("[Dbus ERROR] Dbus Error (%s)", err.message);
544                 dbus_error_free(&err);
545         }
546
547         dbus_message_unref(msg);
548
549         if (NULL != result_msg) {
550                 dbus_message_unref(result_msg);
551                 result = 0;
552         } else {
553                 result = -1; //ERROR_TIMED_OUT;
554         }
555
556         return result;
557 }
558
559 int masc_ui_dbus_send_asr_result(int pid, int event, char* asr_result)
560 {
561         if (0 != __dbus_check()) {
562                 return -1; //MAS_ERROR_OPERATION_FAILED;
563         }
564
565         DBusMessage* msg;
566
567         msg = dbus_message_new_method_call(
568                         MA_UI_CLIENT_SERVICE_NAME,
569                         MA_UI_CLIENT_SERVICE_OBJECT_PATH,
570                         MA_UI_CLIENT_SERVICE_INTERFACE,
571                         MAS_UI_METHOD_SEND_ASR_RESULT);
572
573         if (NULL == msg) {
574                 MAS_LOGE("@@ Request multi-assistant send ASR result : Fail to make message");
575                 return -1; //MA_ERROR_OPERATION_FAILED;
576         } else {
577                 MAS_LOGD("[DEBUG] multi-assistant send ASR result, asr_result(%p)", asr_result);
578         }
579
580         char* temp_asr_result = NULL;
581         if (!asr_result)
582                 temp_asr_result = strdup("#NULL");
583         else
584                 temp_asr_result = strdup(asr_result);
585
586         dbus_message_append_args(msg,
587                 DBUS_TYPE_INT32, &pid,
588                 DBUS_TYPE_INT32, &event,
589                 DBUS_TYPE_STRING, &temp_asr_result,
590                 DBUS_TYPE_INVALID);
591
592         dbus_message_set_no_reply(msg, TRUE);
593
594         if (1 != dbus_connection_send(g_conn_sender, msg, NULL)) {
595                 MAS_LOGE("[Dbus ERROR] Fail to Send");
596                 if (NULL != temp_asr_result) {
597                         free(temp_asr_result);
598                         temp_asr_result = NULL;
599                 }
600                 return -1; // MAS_ERROR_OPERATION_FAILED;
601         } else {
602                 MAS_LOGD("[Dbus DEBUG] Success to Send ASR result");
603                 dbus_connection_flush(g_conn_sender);
604         }
605
606         dbus_message_unref(msg);
607
608         if (temp_asr_result)
609                 free(temp_asr_result);
610         return 0;
611 }
612
613 int masc_ui_dbus_send_result(int pid, const char* display_text, const char* utterance_text, const char* result_json)
614 {
615         if (0 != __dbus_check()) {
616                 return -1; //MA_ERROR_OPERATION_FAILED;
617         }
618
619         DBusMessage* msg;
620
621         msg = dbus_message_new_method_call(
622                         MA_UI_CLIENT_SERVICE_NAME,
623                         MA_UI_CLIENT_SERVICE_OBJECT_PATH,
624                         MA_UI_CLIENT_SERVICE_INTERFACE,
625                         MAS_UI_METHOD_SEND_RESULT);
626
627         if (NULL == msg) {
628                 MAS_LOGE("@@ Request multi-assistant send result : Fail to make message");
629                 return -1; //MA_ERROR_OPERATION_FAILED;
630         } else {
631                 MAS_LOGD("[DEBUG] multi-assistant send result");
632         }
633         char* temp_display_text = NULL;
634         char* temp_utterance_text = NULL;
635         char* temp_result_json = NULL;
636
637 #if 0
638         dbus_message_append_args(msg,
639                 DBUS_TYPE_INT32, &pid,
640                 DBUS_TYPE_STRING, &display_text,
641                 DBUS_TYPE_STRING, &utterance_text,
642                 DBUS_TYPE_STRING, &result_json,
643                 DBUS_TYPE_INVALID);
644
645         dbus_message_set_no_reply(msg, TRUE);
646
647         if (1 != dbus_connection_send(g_conn_sender, msg, NULL)) {
648                 MAS_LOGE("[Dbus ERROR] Fail to Send");
649                 return -1; //MA_ERROR_OPERATION_FAILED;
650         } else {
651                 MAS_LOGD("[Dbus DEBUG] Success to Send result");
652                 dbus_connection_flush(g_conn_sender);
653         }
654
655         dbus_message_unref(msg);
656 #else
657         if (!display_text)
658                 temp_display_text = strdup("#NULL");
659         else
660                 temp_display_text = strdup(display_text);
661         if (!utterance_text)
662                 temp_utterance_text = strdup("#NULL");
663         else
664                 temp_utterance_text = strdup(utterance_text);
665         if (!result_json)
666                 temp_result_json = strdup("#NULL");
667         else
668                 temp_result_json = strdup(result_json);
669
670         dbus_message_append_args(msg,
671                 DBUS_TYPE_INT32, &pid,
672                 DBUS_TYPE_STRING, &temp_display_text,
673                 DBUS_TYPE_STRING, &temp_utterance_text,
674                 DBUS_TYPE_STRING, &temp_result_json,
675                 DBUS_TYPE_INVALID);
676
677         dbus_message_set_no_reply(msg, TRUE);
678
679         if (1 != dbus_connection_send(g_conn_sender, msg, NULL)) {
680                 MAS_LOGE("[Dbus ERROR] Fail to Send");
681                 if (temp_display_text)
682                         free(temp_display_text);
683                 if (temp_utterance_text)
684                         free(temp_utterance_text);
685                 if (temp_result_json)
686                         free(temp_result_json);
687                 return -1; //MA_ERROR_OPERATION_FAILED;
688         } else {
689                 MAS_LOGD("[Dbus DEBUG] Success to Send result");
690                 dbus_connection_flush(g_conn_sender);
691         }
692
693         dbus_message_unref(msg);
694
695         if (temp_display_text)
696                 free(temp_display_text);
697         if (temp_utterance_text)
698                 free(temp_utterance_text);
699         if (temp_result_json)
700                 free(temp_result_json);
701 #endif
702         return 0;
703 }
704
705 int masc_ui_dbus_change_assistant(char* app_id)
706 {
707         if (0 != __dbus_check()) {
708                 return -1; //MAS_ERROR_OPERATION_FAILED;
709         }
710
711         if (NULL == app_id) {
712                 MAS_LOGE("@@ Request multi-assistant send change assistant request : Fail to make message");
713                 return -1; //MA_ERROR_OPERATION_FAILED;
714         } else {
715                 MAS_LOGD("[DEBUG] multi-assistant send change assistant request app_id(%s)", app_id);
716         }
717
718         DBusMessage* msg;
719
720         msg = dbus_message_new_method_call(
721                         MA_UI_CLIENT_SERVICE_NAME,
722                         MA_UI_CLIENT_SERVICE_OBJECT_PATH,
723                         MA_UI_CLIENT_SERVICE_INTERFACE,
724                         MAS_UI_METHOD_CHANGE_ASSISTANT);
725
726         dbus_message_append_args(msg,
727                 DBUS_TYPE_STRING, &app_id,
728                 DBUS_TYPE_INVALID);
729
730         dbus_message_set_no_reply(msg, TRUE);
731
732         if (1 != dbus_connection_send(g_conn_sender, msg, NULL)) {
733                 MAS_LOGE("[Dbus ERROR] Fail to Send");
734                 return -1; // MAS_ERROR_OPERATION_FAILED;
735         } else {
736                 MAS_LOGD("[Dbus DEBUG] Success to Send change assistant request");
737                 dbus_connection_flush(g_conn_sender);
738         }
739
740         dbus_message_unref(msg);
741
742         return 0;
743 }
744
745 int masc_ui_dbus_send_error_message(int reason, const char* err_msg)
746 {
747         if (NULL == g_conn_sender) {
748                 MAS_LOGE("[Dbus ERROR] Dbus connection is not available");
749                 return -1;
750         }
751
752         DBusMessage* msg = NULL;
753
754         /* create a message */
755         msg = dbus_message_new_signal(
756                 MA_UI_CLIENT_SERVICE_OBJECT_PATH,       /* object name of the signal */
757                 MA_UI_CLIENT_SERVICE_INTERFACE,         /* interface name of the signal */
758                 MAS_UI_METHOD_ERROR);                           /* name of the signal */
759
760         if (NULL == msg) {
761                 MAS_LOGE("[Dbus ERROR] Fail to create error message");
762                 return -1;
763         }
764
765         char* temp_err_msg = NULL;
766
767         if (err_msg)
768                 temp_err_msg = strdup(err_msg);
769         else
770                 temp_err_msg = strdup("#NULL");
771
772         dbus_message_append_args(msg,
773                 DBUS_TYPE_INT32, &reason,
774                 DBUS_TYPE_STRING, &temp_err_msg,
775                 DBUS_TYPE_INVALID);
776
777         dbus_message_set_no_reply(msg, TRUE);
778
779         if (!dbus_connection_send(g_conn_sender, msg, NULL)) {
780                 MAS_LOGE("[Dbus ERROR] <<<< error message : Out Of Memory !");
781         } else {
782                 MAS_LOGD("<<<< Send error message : reason(%d), err_msg(%s)", reason, temp_err_msg);
783                 dbus_connection_flush(g_conn_sender);
784         }
785
786         dbus_message_unref(msg);
787
788         if (temp_err_msg)
789                 free(temp_err_msg);
790         return 0;
791 }
792
793 int masc_ui_dbus_send_recognition_result(int pid, int result)
794 {
795         if (0 != __dbus_check()) {
796                 return -1; //MAS_ERROR_OPERATION_FAILED;
797         }
798
799         DBusMessage* msg;
800
801         msg = dbus_message_new_method_call(
802                         MA_UI_CLIENT_SERVICE_NAME,
803                         MA_UI_CLIENT_SERVICE_OBJECT_PATH,
804                         MA_UI_CLIENT_SERVICE_INTERFACE,
805                         MAS_UI_METHOD_SEND_RECOGNITION_RESULT);
806
807         if (NULL == msg) {
808                 MAS_LOGE("@@ Request multi-assistant send recognition result : Fail to make message");
809                 return -1; //MA_ERROR_OPERATION_FAILED;
810         } else {
811                 MAS_LOGD("[DEBUG] multi-assistant send recognition result(%d)", result);
812         }
813
814         dbus_message_append_args(msg,
815                 DBUS_TYPE_INT32, &pid,
816                 DBUS_TYPE_INT32, &result,
817                 DBUS_TYPE_INVALID);
818
819         dbus_message_set_no_reply(msg, TRUE);
820
821         if (1 != dbus_connection_send(g_conn_sender, msg, NULL)) {
822                 MAS_LOGE("[Dbus ERROR] Fail to Send");
823                 return -1; // MAS_ERROR_OPERATION_FAILED;
824         } else {
825                 MAS_LOGD("[Dbus DEBUG] Success to Send ASR result");
826                 dbus_connection_flush(g_conn_sender);
827         }
828
829         dbus_message_unref(msg);
830
831         return 0;
832 }
833
834 int masc_ui_dbus_enable_common_ui(int enable)
835 {
836         if (0 != __dbus_check()) {
837                 return -1; //MAS_ERROR_OPERATION_FAILED;
838         }
839
840         DBusMessage* msg;
841
842         msg = dbus_message_new_method_call(
843                         MA_UI_CLIENT_SERVICE_NAME,
844                         MA_UI_CLIENT_SERVICE_OBJECT_PATH,
845                         MA_UI_CLIENT_SERVICE_INTERFACE,
846                         MAS_UI_METHOD_ENABLE_COMMON_UI);
847
848         if (NULL == msg) {
849                 MAS_LOGE("@@ Request multi-assistant enable common ui : Fail to make message");
850                 return -1; //MA_ERROR_OPERATION_FAILED;
851         } else {
852                 MAS_LOGD("[DEBUG] multi-assistant enable common ui (%d)", enable);
853         }
854
855         dbus_message_append_args(msg,
856                 DBUS_TYPE_INT32, &enable,
857                 DBUS_TYPE_INVALID);
858
859         dbus_message_set_no_reply(msg, TRUE);
860
861         if (1 != dbus_connection_send(g_conn_sender, msg, NULL)) {
862                 MAS_LOGE("[Dbus ERROR] Fail to Send");
863                 return -1; // MAS_ERROR_OPERATION_FAILED;
864         } else {
865                 MAS_LOGD("[Dbus DEBUG] Success to Send ASR result");
866                 dbus_connection_flush(g_conn_sender);
867         }
868
869         dbus_message_unref(msg);
870
871         return 0;
872 }
873
874 static Eina_Bool listener_event_callback(void* data, Ecore_Fd_Handler *fd_handler)
875 {
876         if (NULL == g_conn_listener)    return ECORE_CALLBACK_RENEW;
877
878         dbus_connection_read_write_dispatch(g_conn_listener, 50);
879
880         while (1) {
881                 DBusMessage* msg = NULL;
882                 msg = dbus_connection_pop_message(g_conn_listener);
883
884                 if (true != dbus_connection_get_is_connected(g_conn_listener)) {
885                         MAS_LOGE("[ERROR] Connection is disconnected");
886                         return ECORE_CALLBACK_RENEW;
887                 }
888
889                 /* loop again if we haven't read a message */
890                 if (NULL == msg) {
891                         return ECORE_CALLBACK_RENEW;
892                 }
893
894                 /* client event */
895                 if (dbus_message_is_method_call(msg, MA_SERVER_SERVICE_INTERFACE, MA_METHOD_HELLO)) {
896                         ma_service_dbus_hello(g_conn_listener, msg);
897
898                 } else if (dbus_message_is_method_call(msg, MA_SERVER_SERVICE_INTERFACE, MA_METHOD_INITIALIZE)) {
899                         ma_service_dbus_initialize(g_conn_listener, msg);
900
901                 } else if (dbus_message_is_method_call(msg, MA_SERVER_SERVICE_INTERFACE, MA_METHOD_DEINITIALIZE)) {
902                         ma_service_dbus_deinitialize(g_conn_listener, msg);
903
904                 } else if (dbus_message_is_method_call(msg, MA_SERVER_SERVICE_INTERFACE, MA_METHOD_GET_RECORDING_AUDIO_FORMAT)) {
905                         ma_service_dbus_get_audio_format(g_conn_listener, msg);
906
907                 } else if (dbus_message_is_method_call(msg, MA_SERVER_SERVICE_INTERFACE, MA_METHOD_GET_RECORDING_AUDIO_SOURCE_TYPE)) {
908                         ma_service_dbus_get_audio_source_type(g_conn_listener, msg);
909
910                 } else if (dbus_message_is_method_call(msg, MA_SERVER_SERVICE_INTERFACE, MA_METHOD_SEND_ASR_RESULT)) {
911                         ma_service_dbus_send_asr_result(g_conn_listener, msg);
912
913                 } else if (dbus_message_is_method_call(msg, MA_SERVER_SERVICE_INTERFACE, MA_METHOD_SEND_RESULT)) {
914                         ma_service_dbus_send_result(g_conn_listener, msg);
915
916                 } else if (dbus_message_is_method_call(msg, MA_SERVER_SERVICE_INTERFACE, MA_METHOD_SEND_RECOGNITION_RESULT)) {
917                         ma_service_dbus_send_recognition_result(g_conn_listener, msg);
918
919                 } else if (dbus_message_is_method_call(msg, MA_SERVER_SERVICE_INTERFACE, MA_METHOD_START_STREAMING_AUDIO_DATA)) {
920                         ma_service_dbus_start_streaming_audio_data(g_conn_listener, msg);
921
922                 } else if (dbus_message_is_method_call(msg, MA_SERVER_SERVICE_INTERFACE, MA_METHOD_STOP_STREAMING_AUDIO_DATA)) {
923                         ma_service_dbus_stop_streaming_audio_data(g_conn_listener, msg);
924
925                 } else if (dbus_message_is_method_call(msg, MA_SERVER_SERVICE_INTERFACE, MA_METHOD_UPDATE_VOICE_FEEDBACK_STATE)) {
926                         ma_service_dbus_update_voice_feedback_state(g_conn_listener, msg);
927
928                 } else if (dbus_message_is_method_call(msg, MA_SERVER_SERVICE_INTERFACE, MA_METHOD_SEND_ASSISTANT_SPECIFIC_COMMAND)) {
929                         ma_service_dbus_send_assistant_specific_command(g_conn_listener, msg);
930
931                 } else if (dbus_message_is_method_call(msg, MA_SERVER_SERVICE_INTERFACE, MA_METHOD_SET_BACKGROUND_VOLUME)) {
932                         ma_service_dbus_set_background_volume(g_conn_listener, msg);
933
934                 } else if (dbus_message_is_method_call(msg, MA_SERVER_SERVICE_INTERFACE, MA_METHOD_SET_PREPROCESSING_ALLOW_MODE)) {
935                         ma_service_dbus_set_preprocessing_allow_mode(g_conn_listener, msg);
936
937                 } else if (dbus_message_is_method_call(msg, MA_SERVER_SERVICE_INTERFACE, MA_METHOD_SEND_PREPROCESSING_RESULT)) {
938                         ma_service_dbus_send_preprocessing_result(g_conn_listener, msg);
939
940                 } else if (dbus_message_is_method_call(msg, MA_SERVER_SERVICE_INTERFACE, MA_METHOD_SET_WAKE_WORD_AUDIO_REQUIRE_FLAG)) {
941                         ma_service_dbus_set_wake_word_audio_require_flag(g_conn_listener, msg);
942
943                 } else if (dbus_message_is_method_call(msg, MA_SERVER_SERVICE_INTERFACE, MA_METHOD_SET_ASSISTANT_LANGUAGE)) {
944                         ma_service_dbus_set_assistant_language(g_conn_listener, msg);
945
946                 } else if (dbus_message_is_method_call(msg, MA_SERVER_SERVICE_INTERFACE, MA_UI_METHOD_INITIALIZE)) {
947                         ma_service_ui_dbus_initialize(g_conn_listener, msg);
948
949                 } else if (dbus_message_is_method_call(msg, MA_SERVER_SERVICE_INTERFACE, MA_UI_METHOD_DEINITIALIZE)) {
950                         ma_service_ui_dbus_deinitialize(g_conn_listener, msg);
951
952                 } else if (dbus_message_is_method_call(msg, MA_SERVER_SERVICE_INTERFACE, MA_UI_METHOD_CHANGE_ASSISTANT)) {
953                         ma_service_ui_dbus_change_assistant(g_conn_listener, msg);
954
955                 } else {
956                         MAS_LOGD("Message is NOT valid");
957                         /* Invalid method */
958                 }
959                 /* free the message */
960                 dbus_message_unref(msg);
961         }
962
963         return ECORE_CALLBACK_RENEW;
964 }
965
966 static void __mas_dbus_connection_free()
967 {
968         if (NULL != g_conn_listener) {
969                 dbus_connection_close(g_conn_listener);
970                 dbus_connection_unref(g_conn_listener);
971                 g_conn_listener = NULL;
972         }
973         if (NULL != g_conn_sender) {
974                 dbus_connection_close(g_conn_sender);
975                 dbus_connection_unref(g_conn_sender);
976                 g_conn_sender = NULL;
977         }
978 }
979
980 int mas_dbus_open_connection()
981 {
982         DBusError err;
983         dbus_error_init(&err);
984
985         int ret;
986
987         /* Create connection for sender */
988         g_conn_sender = dbus_bus_get_private(DBUS_BUS_SESSION, &err);
989
990         if (dbus_error_is_set(&err)) {
991                 MAS_LOGE("[Dbus ERROR] Fail dbus_bus_get : %s", err.message);
992                 dbus_error_free(&err);
993         }
994
995         if (NULL == g_conn_sender) {
996                 MAS_LOGE("[Dbus ERROR] Fail to get dbus connection");
997                 return -1;
998         }
999
1000         dbus_connection_set_exit_on_disconnect(g_conn_sender, false);
1001
1002         /* connect to the bus and check for errors */
1003         g_conn_listener = dbus_bus_get_private(DBUS_BUS_SESSION, &err);
1004
1005         if (dbus_error_is_set(&err)) {
1006                 MAS_LOGE("[Dbus ERROR] Fail dbus_bus_get : %s", err.message);
1007                 dbus_error_free(&err);
1008         }
1009
1010         if (NULL == g_conn_listener) {
1011                 MAS_LOGE("[Dbus ERROR] Fail to get dbus connection");
1012                 __mas_dbus_connection_free();
1013                 return -1;
1014         }
1015
1016         dbus_connection_set_exit_on_disconnect(g_conn_listener, false);
1017
1018         /* request our name on the bus and check for errors */
1019         ret = dbus_bus_request_name(g_conn_listener, MA_SERVER_SERVICE_NAME, DBUS_NAME_FLAG_REPLACE_EXISTING, &err);
1020
1021         if (DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER != ret) {
1022                 printf("Fail to be primary owner in dbus request.");
1023                 MAS_LOGE("[Dbus ERROR] Fail to be primary owner");
1024                 __mas_dbus_connection_free();
1025                 return -1;
1026         }
1027
1028         if (dbus_error_is_set(&err)) {
1029                 MAS_LOGE("[Dbus ERROR] dbus_bus_request_name() : %s", err.message);
1030                 dbus_error_free(&err);
1031                 __mas_dbus_connection_free();
1032                 return -1;
1033         }
1034
1035         /* Flush messages which are received before fd event handler registration */
1036         while (DBUS_DISPATCH_DATA_REMAINS == dbus_connection_get_dispatch_status(g_conn_listener)) {
1037                 listener_event_callback(NULL, NULL);
1038         }
1039
1040         /* add a rule for getting signal */
1041         char rule[128];
1042         snprintf(rule, 128, "type='signal',interface='%s'", MA_SERVER_SERVICE_INTERFACE);
1043
1044         /* add a rule for which messages we want to see */
1045         dbus_bus_add_match(g_conn_listener, rule, &err);/* see signals from the given interface */
1046
1047         if (dbus_error_is_set(&err)) {
1048                 MAS_LOGE("[Dbus ERROR] dbus_bus_add_match() : %s", err.message);
1049                 dbus_error_free(&err);
1050                 __mas_dbus_connection_free();
1051                 return -1;
1052         }
1053
1054         int fd = 0;
1055         if (1 != dbus_connection_get_unix_fd(g_conn_listener, &fd)) {
1056                 MAS_LOGE("fail to get fd from dbus ");
1057                 __mas_dbus_connection_free();
1058                 return -1;
1059         } else {
1060                 MAS_LOGD("Get fd from dbus : %d", fd);
1061         }
1062
1063
1064         g_dbus_fd_handler = ecore_main_fd_handler_add(fd, ECORE_FD_READ, (Ecore_Fd_Cb)listener_event_callback, g_conn_listener, NULL, NULL);
1065
1066         if (NULL == g_dbus_fd_handler) {
1067                 MAS_LOGE("[Dbus ERROR] Fail to get fd handler");
1068                 __mas_dbus_connection_free();
1069                 return -1;
1070         }
1071
1072         return 0;
1073 }
1074
1075 int mas_dbus_close_connection()
1076 {
1077         DBusError err;
1078         dbus_error_init(&err);
1079
1080         if (NULL != g_dbus_fd_handler) {
1081                 ecore_main_fd_handler_del(g_dbus_fd_handler);
1082                 g_dbus_fd_handler = NULL;
1083         }
1084
1085         dbus_bus_release_name(g_conn_listener, MA_SERVER_SERVICE_NAME, &err);
1086
1087         if (dbus_error_is_set(&err)) {
1088                 MAS_LOGE("[Dbus ERROR] dbus_bus_release_name() : %s", err.message);
1089                 dbus_error_free(&err);
1090         }
1091
1092         __mas_dbus_connection_free();
1093
1094         return 0;
1095 }