Bump version to 0.2.30
[platform/core/uifw/multi-assistant-service.git] / src / service_ipc_dbus.cpp
1 /*
2  * Copyright 2020 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
18 #include <dirent.h>
19 #include <dlfcn.h>
20 #include <message_port.h>
21
22 #include "service_common.h"
23 #include "service_main.h"
24 #include "service_ipc_dbus.h"
25
26 int CServiceIpcDbus::reconnect()
27 {
28         if (!mConnectionSender || !mConnectionListener) {
29                 close_connection();
30
31                 if (0 != open_connection()) {
32                         MAS_LOGE("[ERROR] Fail to reconnect");
33                         return -1;
34                 }
35
36                 MAS_LOGD("[DBUS] Reconnect");
37                 return 0;
38         }
39
40         bool sender_connected = dbus_connection_get_is_connected(mConnectionSender);
41         bool listener_connected = dbus_connection_get_is_connected(mConnectionListener);
42         MAS_LOGW("[DBUS] Sender(%s) Listener(%s)",
43                 sender_connected ? "Connected" : "Not connected", listener_connected ? "Connected" : "Not connected");
44
45         if (false == sender_connected || false == listener_connected) {
46                 close_connection();
47
48                 if (0 != open_connection()) {
49                         MAS_LOGE("[ERROR] Fail to reconnect");
50                         return -1;
51                 }
52
53                 MAS_LOGD("[DBUS] Reconnect");
54         }
55
56         return 0;
57 }
58
59 int CServiceIpcDbus::__dbus_check()
60 {
61         if (NULL == mConnectionSender || NULL == mConnectionListener) {
62                 MAS_LOGE("[ERROR] NULL connection");
63                 return reconnect();
64         }
65         return 0;
66 }
67
68 int CServiceIpcDbus::mas_check_dbus_connection()
69 {
70         if (NULL == mConnectionSender || NULL == mConnectionListener) {
71                 MAS_LOGE("[ERROR] NULL connection sender(%p), listener(%p)", mConnectionSender, mConnectionListener);
72                 return -1;
73         }
74         return 0;
75 }
76
77 int CServiceIpcDbus::send_hello(int pid)
78 {
79         if (0 != __dbus_check()) {
80                 return -1; //MAS_ERROR_OPERATION_FAILED;
81         }
82
83         DBusMessage* msg;
84         DBusError err;
85         dbus_error_init(&err);
86
87         msg = dbus_message_new_method_call(
88                         MA_CLIENT_SERVICE_NAME,
89                         MA_CLIENT_SERVICE_OBJECT_PATH,
90                         MA_CLIENT_SERVICE_INTERFACE,
91                         MAS_METHOD_HELLO);
92
93         if (NULL == msg) {
94                 MAS_LOGE("[DBus ERROR] Request masc hello : Fail to make message");
95                 return -1;
96         }
97
98         int result = -1;
99         DBusMessage* result_msg = NULL;
100         result_msg = dbus_connection_send_with_reply_and_block(mConnectionSender, msg, 500, &err);
101
102         if (dbus_error_is_set(&err)) {
103                 MAS_LOGE("[Dbus ERROR] Dbus Error (%s)", err.message);
104                 dbus_error_free(&err);
105         }
106
107         dbus_message_unref(msg);
108
109         if (NULL != result_msg) {
110                 dbus_message_unref(result_msg);
111                 result = 0;
112         } else {
113                 result = -1; //MAS_ERROR_TIMED_OUT;
114         }
115
116         return result;
117 }
118
119 int CServiceIpcDbus::send_error_message(int reason, const char* err_msg)
120 {
121         if (0 != __dbus_check()) {
122                 return -1; //MAS_ERROR_OPERATION_FAILED;
123         }
124
125         if (NULL == mConnectionSender) {
126                 MAS_LOGE("[Dbus ERROR] Dbus connection is not available");
127                 return -1;
128         }
129
130         DBusMessage* msg = NULL;
131
132         /* create a message */
133         msg = dbus_message_new_signal(
134                 MA_CLIENT_SERVICE_OBJECT_PATH,  /* object name of the signal */
135                 MA_CLIENT_SERVICE_INTERFACE,    /* interface name of the signal */
136                 MAS_METHOD_ERROR);              /* name of the signal */
137
138         if (NULL == msg) {
139                 MAS_LOGE("[Dbus ERROR] Fail to create error message");
140                 return -1;
141         }
142
143         char* temp_err_msg = NULL;
144
145         if (err_msg)
146                 temp_err_msg = strdup(err_msg);
147         else
148                 temp_err_msg = strdup("#NULL");
149
150         dbus_message_append_args(msg,
151                 DBUS_TYPE_INT32, &reason,
152                 DBUS_TYPE_STRING, &temp_err_msg,
153                 DBUS_TYPE_INVALID);
154
155         dbus_message_set_no_reply(msg, TRUE);
156
157         if (!dbus_connection_send(mConnectionSender, msg, NULL)) {
158                 MAS_LOGE("[Dbus ERROR] <<<< error message : Out Of Memory !");
159         } else {
160                 MAS_LOGI("<<<< Send error message : reason(%d), err_msg(%s)", reason, temp_err_msg);
161                 dbus_connection_flush(mConnectionSender);
162         }
163
164         dbus_message_unref(msg);
165
166         if (temp_err_msg)
167                 free(temp_err_msg);
168         return 0;
169 }
170
171 const char *message_port = "ma_streaming_port";
172
173 #define STREAMING_BUFFER_SIZE 4096
174
175 typedef enum {
176         streaming_data_type_audio_data,
177         streaming_data_type_streaming_section
178 } streaming_data_type_e;
179
180 typedef struct {
181         unsigned int streaming_data_size;
182         int streaming_data_type;
183         int streaming_data_serial;
184 } streaming_data_header;
185
186 typedef struct {
187         int event;
188         unsigned int data_size;
189 } streaming_data_audio_data_header;
190
191 typedef struct {
192         int section;
193 } streaming_data_streaming_section_header;
194
195 static void masc_message_port_error(int error)
196 {
197         MAS_LOGE("message_port error found : %s",
198                 (MESSAGE_PORT_ERROR_NONE == error) ? "MESSAGE_PORT_ERROR_NONE" :
199                 (MESSAGE_PORT_ERROR_IO_ERROR == error) ? "MESSAGE_PORT_ERROR_IO_ERROR" :
200                 (MESSAGE_PORT_ERROR_OUT_OF_MEMORY == error) ? "MESSAGE_PORT_ERROR_OUT_OF_MEMORY" :
201                 (MESSAGE_PORT_ERROR_INVALID_PARAMETER == error) ? "MESSAGE_PORT_ERROR_INVALID_PARAMETER" :
202                 (MESSAGE_PORT_ERROR_PORT_NOT_FOUND == error) ? "MESSAGE_PORT_ERROR_PORT_NOT_FOUND" :
203                 (MESSAGE_PORT_ERROR_CERTIFICATE_NOT_MATCH == error) ? "MESSAGE_PORT_ERROR_CERTIFICATE_NOT_MATCH" :
204                 (MESSAGE_PORT_ERROR_MAX_EXCEEDED == error) ? "MESSAGE_PORT_ERROR_MAX_EXCEEDED" :
205                 (MESSAGE_PORT_ERROR_RESOURCE_UNAVAILABLE == error) ? "MESSAGE_PORT_ERROR_RESOURCE_UNAVAILABLE" :
206                 "MESSAGE_PORT_ERROR_UNKNOWN");
207 }
208
209 int CServiceIpcDbus::send_streaming_audio_data(int pid, int event, void* data, unsigned int data_size)
210 {
211         if (nullptr == mClientManager) {
212                 MAS_LOGE("mClientManager variable is NULL");
213                 return -1;
214         }
215
216         static unsigned char pending_buffer[STREAMING_BUFFER_SIZE];
217         static unsigned int pending_buffer_size = 0;
218
219         streaming_data_header header;
220         header.streaming_data_type = 0;
221         header.streaming_data_size = sizeof(streaming_data_header) + sizeof(streaming_data_audio_data_header) + data_size;
222         header.streaming_data_serial = mStreamingDataSerial++;
223
224         streaming_data_audio_data_header audio_data_header;
225         audio_data_header.event = event;
226         audio_data_header.data_size = data_size;
227
228         unsigned char buffer[STREAMING_BUFFER_SIZE];
229         size_t total_size = 0;
230         memcpy(buffer, &header, sizeof(header));
231         total_size += sizeof(header);
232         memcpy(buffer + total_size, &audio_data_header, sizeof(audio_data_header));
233         total_size += sizeof(audio_data_header);
234         memcpy(buffer + total_size, data, data_size);
235         total_size += data_size;
236
237         static int last_serial_waiting_for_flush = -1;
238         if (0 == header.streaming_data_serial % 50) {
239                 last_serial_waiting_for_flush = header.streaming_data_serial;
240                 MAS_LOGI("queueing streaming data, serial : %d", last_serial_waiting_for_flush);
241         }
242         if (pending_buffer_size + total_size > STREAMING_BUFFER_SIZE ||
243                 MAS_SPEECH_STREAMING_EVENT_FINISH == event) {
244                 bundle *b = bundle_create();
245                 if (b) {
246                         bundle_add_byte(b, "content", pending_buffer, pending_buffer_size);
247                         int ret = message_port_send_message(
248                                 mClientManager->find_client_appid_by_pid(pid).c_str(), message_port, b);
249                         if (MESSAGE_PORT_ERROR_NONE != ret)
250                                 masc_message_port_error(ret);
251
252                         pending_buffer_size = 0;
253                         bundle_free(b);
254                 } else {
255                         MAS_LOGE("Bundle creation failed!!!");
256                 }
257
258                 if (-1 != last_serial_waiting_for_flush) {
259                         MAS_LOGI("flushing streaming data, serial : %d", last_serial_waiting_for_flush);
260                         last_serial_waiting_for_flush =  -1;
261                 }
262         }
263
264         if (MAS_SPEECH_STREAMING_EVENT_FINISH == event) {
265                 bundle *b = bundle_create();
266                 if (b) {
267                         bundle_add_byte(b, "content", buffer, total_size);
268                         int ret = message_port_send_message(
269                                 mClientManager->find_client_appid_by_pid(pid).c_str(), message_port, b);
270                         if (MESSAGE_PORT_ERROR_NONE != ret)
271                                 masc_message_port_error(ret);
272
273                         bundle_free(b);
274                 } else {
275                         MAS_LOGE("Bundle creation failed!!!");
276                 }
277         } else {
278                 memcpy(pending_buffer + pending_buffer_size, buffer, total_size);
279                 pending_buffer_size += total_size;
280         }
281         return 0;
282 }
283
284 int CServiceIpcDbus::change_active_state(int pid, int state)
285 {
286         if (0 != __dbus_check()) {
287                 return -1; //MAS_ERROR_OPERATION_FAILED;
288         }
289
290         DBusMessage* msg;
291
292         DBusError err;
293         dbus_error_init(&err);
294
295         char service_name[64];
296         memset(service_name, '\0', 64);
297         snprintf(service_name, 64, "%s_%d", MA_CLIENT_SERVICE_NAME, pid);
298
299         msg = dbus_message_new_method_call(
300                         service_name,
301                         MA_CLIENT_SERVICE_OBJECT_PATH,
302                         MA_CLIENT_SERVICE_INTERFACE,
303                         MAS_METHOD_ACTIVE_STATE_CHANGE);
304
305         if (NULL == msg) {
306                 MAS_LOGE(">>>> Request mas send activate message : Fail to make message");
307                 return -1; // MAS_ERROR_OPERATION_FAILED;
308         } else {
309                 MAS_LOGD(">>>> Request mas send activate message : %s", service_name);
310         }
311
312         if (true != dbus_message_append_args(msg,
313                 DBUS_TYPE_INT32, &state,
314                 DBUS_TYPE_INVALID)) {
315                 dbus_message_unref(msg);
316                 MAS_LOGE("[ERROR] Fail to append args");
317                 return -1;
318         }
319
320         dbus_message_set_no_reply(msg, TRUE);
321
322         if (1 != dbus_connection_send(mConnectionSender, msg, NULL)) {
323                 MAS_LOGE("[Dbus ERROR] Fail to Send");
324                 return -1; // MAS_ERROR_OPERATION_FAILED;
325         } else {
326                 MAS_LOGI("[Dbus DEBUG] Success to Send activate message : %d %d", pid, state);
327                 dbus_connection_flush(mConnectionSender);
328         }
329
330         dbus_message_unref(msg);
331         return 0;
332 }
333
334 int CServiceIpcDbus::send_preprocessing_information(int pid, const char* app_id)
335 {
336         if (0 != __dbus_check()) {
337                 return -1; //MAS_ERROR_OPERATION_FAILED;
338         }
339
340         DBusMessage* msg;
341
342         DBusError err;
343         dbus_error_init(&err);
344
345         char service_name[64];
346         memset(service_name, '\0', 64);
347         snprintf(service_name, 64, "%s_%d", MA_CLIENT_SERVICE_NAME, pid);
348
349         msg = dbus_message_new_method_call(
350                         service_name,
351                         MA_CLIENT_SERVICE_OBJECT_PATH,
352                         MA_CLIENT_SERVICE_INTERFACE,
353                         MAS_METHOD_SEND_PREPROCESSING_INFORMATION);
354
355         if (NULL == msg) {
356                 MAS_LOGE(">>>> Request mas send preprocessing assistant information : Fail to make message");
357                 return -1; // MAS_ERROR_OPERATION_FAILED;
358         } else {
359                 MAS_LOGD(">>>> Request mas send preprocessing assistant information : %s", service_name);
360         }
361
362         char* temp_app_id = NULL;
363         if (!app_id)
364                 temp_app_id = strdup("#NULL");
365         else
366                 temp_app_id = strdup(app_id);
367
368         if (true != dbus_message_append_args(msg,
369                 DBUS_TYPE_STRING, &temp_app_id,
370                 DBUS_TYPE_INVALID)) {
371                 dbus_message_unref(msg);
372                 MAS_LOGE("[ERROR] Fail to append args");
373                 if (temp_app_id)
374                         free(temp_app_id);
375                 return -1;
376         }
377
378         dbus_message_set_no_reply(msg, TRUE);
379
380         if (1 != dbus_connection_send(mConnectionSender, msg, NULL)) {
381                 MAS_LOGE("[Dbus ERROR] Fail to Send");
382                 if (temp_app_id)
383                         free(temp_app_id);
384                 return -1; // MAS_ERROR_OPERATION_FAILED;
385         } else {
386                 MAS_LOGI("[Dbus DEBUG] Success to Send preprocessing assistant information : %d %s", pid, app_id);
387                 dbus_connection_flush(mConnectionSender);
388         }
389
390         dbus_message_unref(msg);
391
392         if (temp_app_id)
393                 free(temp_app_id);
394
395         return 0;
396 }
397
398 int CServiceIpcDbus::send_streaming_section_changed(int pid, int section)
399 {
400         if (nullptr == mClientManager) {
401                 MAS_LOGE("mClientManager variable is NULL");
402                 return -1;
403         }
404
405         bundle *b = bundle_create();
406
407         streaming_data_header header;
408         header.streaming_data_type = 1;
409         header.streaming_data_size = sizeof(streaming_data_header) + sizeof(streaming_data_streaming_section_header);
410         header.streaming_data_serial = mStreamingDataSerial++;
411
412         streaming_data_streaming_section_header streaming_section_header;
413         streaming_section_header.section = section;
414
415         unsigned char buffer[STREAMING_BUFFER_SIZE];
416         size_t total_size = 0;
417         memcpy(buffer, &header, sizeof(header));
418         total_size += sizeof(header);
419         memcpy(buffer + total_size, &streaming_section_header, sizeof(streaming_section_header));
420         total_size += sizeof(streaming_section_header);
421
422         bundle_add_byte(b, "content", buffer, total_size);
423         int ret = message_port_send_message(
424                 mClientManager->find_client_appid_by_pid(pid).c_str(), message_port, b);
425         if (MESSAGE_PORT_ERROR_NONE != ret)
426                 masc_message_port_error(ret);
427
428         bundle_free(b);
429
430         return 0;
431 }
432
433 int CServiceIpcDbus::send_preprocessing_result(int pid, bool result)
434 {
435         if (0 != __dbus_check()) {
436                 return -1; //MAS_ERROR_OPERATION_FAILED;
437         }
438
439         DBusMessage* msg;
440
441         DBusError err;
442         dbus_error_init(&err);
443
444         char service_name[64];
445         memset(service_name, '\0', 64);
446         snprintf(service_name, 64, "%s_%d", MA_CLIENT_SERVICE_NAME, pid);
447
448         msg = dbus_message_new_method_call(
449                         service_name,
450                         MA_CLIENT_SERVICE_OBJECT_PATH,
451                         MA_CLIENT_SERVICE_INTERFACE,
452                         MAS_METHOD_SEND_PREPROCESSING_RESULT);
453
454         if (NULL == msg) {
455                 MAS_LOGE(">>>> Request mas send preprocessing result : Fail to make message");
456                 return -1; // MAS_ERROR_OPERATION_FAILED;
457         } else {
458                 MAS_LOGD(">>>> Request mas send preprocessing result : %s", service_name);
459         }
460
461         int temp_result = result;
462
463         if (true != dbus_message_append_args(msg,
464                 DBUS_TYPE_INT32, &temp_result,
465                 DBUS_TYPE_INVALID)) {
466                 dbus_message_unref(msg);
467                 MAS_LOGE("[ERROR] Fail to append args");
468                 return -1;
469         }
470
471         dbus_message_set_no_reply(msg, TRUE);
472
473         if (1 != dbus_connection_send(mConnectionSender, msg, NULL)) {
474                 MAS_LOGE("[Dbus ERROR] Fail to Send");
475                 return -1; // MAS_ERROR_OPERATION_FAILED;
476         } else {
477                 MAS_LOGI("[Dbus DEBUG] Success to Send preprocessing result : %d %d", pid, temp_result);
478                 dbus_connection_flush(mConnectionSender);
479         }
480
481         dbus_message_unref(msg);
482
483         return 0;
484 }
485
486 int CServiceIpcDbus::send_wakeup_engine_command(int pid, const char* command)
487 {
488         if (0 != __dbus_check()) {
489                 return -1; //MAS_ERROR_OPERATION_FAILED;
490         }
491
492         DBusMessage* msg;
493
494         DBusError err;
495         dbus_error_init(&err);
496
497         char service_name[64];
498         memset(service_name, '\0', 64);
499         snprintf(service_name, 64, "%s_%d", MA_CLIENT_SERVICE_NAME, pid);
500
501         msg = dbus_message_new_method_call(
502                         service_name,
503                         MA_CLIENT_SERVICE_OBJECT_PATH,
504                         MA_CLIENT_SERVICE_INTERFACE,
505                         MAS_METHOD_SEND_WAKEUP_ENGINE_COMMAND);
506
507         if (NULL == msg) {
508                 MAS_LOGE(">>>> Request mas send wakeup engine command : Fail to make message");
509                 return -1; // MAS_ERROR_OPERATION_FAILED;
510         } else {
511                 MAS_LOGD(">>>> Request mas send wakeup engine command : %s", service_name);
512         }
513
514         char* temp_command = NULL;
515         if (!command)
516                 temp_command = strdup("#NULL");
517         else
518                 temp_command = strdup(command);
519
520         if (true != dbus_message_append_args(msg,
521                 DBUS_TYPE_STRING, &temp_command,
522                 DBUS_TYPE_INVALID)) {
523                 dbus_message_unref(msg);
524                 MAS_LOGE("[ERROR] Fail to append args");
525                 if (temp_command)
526                         free(temp_command);
527                 return -1;
528         }
529
530         dbus_message_set_no_reply(msg, TRUE);
531
532         if (1 != dbus_connection_send(mConnectionSender, msg, NULL)) {
533                 MAS_LOGE("[Dbus ERROR] Fail to Send");
534                 if (temp_command)
535                         free(temp_command);
536                 return -1; // MAS_ERROR_OPERATION_FAILED;
537         } else {
538                 MAS_LOGI("[Dbus DEBUG] Success to Send wakeup_engine_command : %d %s", pid, command);
539                 dbus_connection_flush(mConnectionSender);
540         }
541
542         dbus_message_unref(msg);
543
544         if (temp_command)
545                 free(temp_command);
546
547         return 0;
548 }
549
550 int CServiceIpcDbus::change_service_state(int pid, int state)
551 {
552         if (0 != __dbus_check()) {
553                 return -1; //MAS_ERROR_OPERATION_FAILED;
554         }
555
556         DBusMessage* msg;
557
558         DBusError err;
559         dbus_error_init(&err);
560
561         char service_name[64];
562         memset(service_name, '\0', 64);
563         snprintf(service_name, 64, "%s_%d", MA_CLIENT_SERVICE_NAME, pid);
564
565         msg = dbus_message_new_method_call(
566                         service_name,
567                         MA_CLIENT_SERVICE_OBJECT_PATH,
568                         MA_CLIENT_SERVICE_INTERFACE,
569                         MAS_METHOD_SERVICE_STATE_CHANGE);
570
571         if (NULL == msg) {
572                 MAS_LOGE(">>>> Request mas send service state message : Fail to make message");
573                 return -1; // MAS_ERROR_OPERATION_FAILED;
574         } else {
575                 MAS_LOGD(">>>> Request mas send service state message : %s", service_name);
576         }
577
578         if (true != dbus_message_append_args(msg,
579                 DBUS_TYPE_INT32, &state,
580                 DBUS_TYPE_INVALID)) {
581                 dbus_message_unref(msg);
582                 MAS_LOGE("[ERROR] Fail to append args");
583                 return -1;
584         }
585
586         dbus_message_set_no_reply(msg, TRUE);
587
588         if (1 != dbus_connection_send(mConnectionSender, msg, NULL)) {
589                 MAS_LOGE("[Dbus ERROR] Fail to Send");
590                 return -1; // MAS_ERROR_OPERATION_FAILED;
591         } else {
592                 MAS_LOGI("[Dbus DEBUG] Success to Send service state message : %d %d", pid, state);
593                 dbus_connection_flush(mConnectionSender);
594         }
595
596         dbus_message_unref(msg);
597         return 0;
598 }
599
600 int CServiceIpcDbus::change_voice_key_status(int pid, int status)
601 {
602         if (0 != __dbus_check()) {
603                 return -1; //MAS_ERROR_OPERATION_FAILED;
604         }
605
606         DBusMessage* msg;
607
608         DBusError err;
609         dbus_error_init(&err);
610
611         char service_name[64];
612         memset(service_name, '\0', 64);
613         snprintf(service_name, 64, "%s_%d", MA_CLIENT_SERVICE_NAME, pid);
614
615         msg = dbus_message_new_method_call(
616                         service_name,
617                         MA_CLIENT_SERVICE_OBJECT_PATH,
618                         MA_CLIENT_SERVICE_INTERFACE,
619                         MAS_METHOD_VOICE_KEY_STATUS_CHANGE);
620
621         if (NULL == msg) {
622                 MAS_LOGE(">>>> Request mas send voice key status change message : Fail to make message");
623                 return -1; // MAS_ERROR_OPERATION_FAILED;
624         } else {
625                 MAS_LOGD(">>>> Request mas send voice key status change message : %s", service_name);
626         }
627
628         if (true != dbus_message_append_args(msg,
629                 DBUS_TYPE_INT32, &status,
630                 DBUS_TYPE_INVALID)) {
631                 dbus_message_unref(msg);
632                 MAS_LOGE("[ERROR] Fail to append args");
633                 return -1;
634         }
635
636         dbus_message_set_no_reply(msg, TRUE);
637
638         if (1 != dbus_connection_send(mConnectionSender, msg, NULL)) {
639                 MAS_LOGE("[Dbus ERROR] Fail to Send");
640                 return -1; // MAS_ERROR_OPERATION_FAILED;
641         } else {
642                 MAS_LOGI("[Dbus DEBUG] Success to Send voice key status change : %d %d", pid, status);
643                 dbus_connection_flush(mConnectionSender);
644         }
645
646         dbus_message_unref(msg);
647         return 0;
648 }
649
650 int CServiceIpcDbus::masc_ui_dbus_send_hello(void)
651 {
652         if (0 != __dbus_check()) {
653                 return -1; //MAS_ERROR_OPERATION_FAILED;
654         }
655
656         DBusMessage* msg;
657
658         DBusError err;
659         dbus_error_init(&err);
660
661         msg = dbus_message_new_method_call(
662                         MA_UI_CLIENT_SERVICE_NAME,
663                         MA_UI_CLIENT_SERVICE_OBJECT_PATH,
664                         MA_UI_CLIENT_SERVICE_INTERFACE,
665                         MAS_METHOD_HELLO);
666
667         if (NULL == msg) {
668                 MAS_LOGE("[DBus ERROR] Request masc hello : Fail to make message");
669                 return -1;
670         }
671
672         DBusMessage* result_msg = NULL;
673         int result = 0;
674
675         result_msg = dbus_connection_send_with_reply_and_block(mConnectionSender, msg, 500, &err);
676
677         if (dbus_error_is_set(&err)) {
678                 MAS_LOGE("[Dbus ERROR] Dbus Error (%s)", err.message);
679                 dbus_error_free(&err);
680         }
681
682         dbus_message_unref(msg);
683
684         if (NULL != result_msg) {
685                 dbus_message_unref(result_msg);
686                 result = 0;
687         } else {
688                 result = -1; //ERROR_TIMED_OUT;
689         }
690
691         return result;
692 }
693
694 int CServiceIpcDbus::masc_ui_dbus_send_asr_result(int pid, int event, const char* asr_result)
695 {
696         if (0 != __dbus_check()) {
697                 return -1; //MAS_ERROR_OPERATION_FAILED;
698         }
699
700         DBusMessage* msg;
701
702         msg = dbus_message_new_method_call(
703                         MA_UI_CLIENT_SERVICE_NAME,
704                         MA_UI_CLIENT_SERVICE_OBJECT_PATH,
705                         MA_UI_CLIENT_SERVICE_INTERFACE,
706                         MAS_UI_METHOD_SEND_ASR_RESULT);
707
708         if (NULL == msg) {
709                 MAS_LOGE("@@ Request multi-assistant send ASR result : Fail to make message");
710                 return -1; //MA_ERROR_OPERATION_FAILED;
711         } else {
712                 MAS_LOGD("[DEBUG] multi-assistant send ASR result, asr_result(%p)", asr_result);
713         }
714
715         char* temp_asr_result = NULL;
716         if (!asr_result)
717                 temp_asr_result = strdup("#NULL");
718         else
719                 temp_asr_result = strdup(asr_result);
720
721         dbus_message_append_args(msg,
722                 DBUS_TYPE_INT32, &pid,
723                 DBUS_TYPE_INT32, &event,
724                 DBUS_TYPE_STRING, &temp_asr_result,
725                 DBUS_TYPE_INVALID);
726
727         dbus_message_set_no_reply(msg, TRUE);
728
729         if (1 != dbus_connection_send(mConnectionSender, msg, NULL)) {
730                 MAS_LOGE("[Dbus ERROR] Fail to Send");
731                 if (NULL != temp_asr_result) {
732                         free(temp_asr_result);
733                         temp_asr_result = NULL;
734                 }
735                 return -1; // MAS_ERROR_OPERATION_FAILED;
736         } else {
737                 MAS_LOGD("[Dbus DEBUG] Success to Send ASR result");
738                 dbus_connection_flush(mConnectionSender);
739         }
740
741         dbus_message_unref(msg);
742
743         if (temp_asr_result)
744                 free(temp_asr_result);
745         return 0;
746 }
747
748 int CServiceIpcDbus::masc_ui_dbus_send_result(int pid, const char* display_text, const char* utterance_text, const char* result_json)
749 {
750         if (0 != __dbus_check()) {
751                 return -1; //MA_ERROR_OPERATION_FAILED;
752         }
753
754         DBusMessage* msg;
755
756         msg = dbus_message_new_method_call(
757                         MA_UI_CLIENT_SERVICE_NAME,
758                         MA_UI_CLIENT_SERVICE_OBJECT_PATH,
759                         MA_UI_CLIENT_SERVICE_INTERFACE,
760                         MAS_UI_METHOD_SEND_RESULT);
761
762         if (NULL == msg) {
763                 MAS_LOGE("@@ Request multi-assistant send result : Fail to make message");
764                 return -1; //MA_ERROR_OPERATION_FAILED;
765         } else {
766                 MAS_LOGD("[DEBUG] multi-assistant send result");
767         }
768         char* temp_display_text = NULL;
769         char* temp_utterance_text = NULL;
770         char* temp_result_json = NULL;
771
772 #if 0
773         dbus_message_append_args(msg,
774                 DBUS_TYPE_INT32, &pid,
775                 DBUS_TYPE_STRING, &display_text,
776                 DBUS_TYPE_STRING, &utterance_text,
777                 DBUS_TYPE_STRING, &result_json,
778                 DBUS_TYPE_INVALID);
779
780         dbus_message_set_no_reply(msg, TRUE);
781
782         if (1 != dbus_connection_send(mConnectionSender, msg, NULL)) {
783                 MAS_LOGE("[Dbus ERROR] Fail to Send");
784                 return -1; //MA_ERROR_OPERATION_FAILED;
785         } else {
786                 MAS_LOGD("[Dbus DEBUG] Success to Send result");
787                 dbus_connection_flush(mConnectionSender);
788         }
789
790         dbus_message_unref(msg);
791 #else
792         if (!display_text)
793                 temp_display_text = strdup("#NULL");
794         else
795                 temp_display_text = strdup(display_text);
796         if (!utterance_text)
797                 temp_utterance_text = strdup("#NULL");
798         else
799                 temp_utterance_text = strdup(utterance_text);
800         if (!result_json)
801                 temp_result_json = strdup("#NULL");
802         else
803                 temp_result_json = strdup(result_json);
804
805         dbus_message_append_args(msg,
806                 DBUS_TYPE_INT32, &pid,
807                 DBUS_TYPE_STRING, &temp_display_text,
808                 DBUS_TYPE_STRING, &temp_utterance_text,
809                 DBUS_TYPE_STRING, &temp_result_json,
810                 DBUS_TYPE_INVALID);
811
812         dbus_message_set_no_reply(msg, TRUE);
813
814         if (1 != dbus_connection_send(mConnectionSender, msg, NULL)) {
815                 MAS_LOGE("[Dbus ERROR] Fail to Send");
816                 if (temp_display_text)
817                         free(temp_display_text);
818                 if (temp_utterance_text)
819                         free(temp_utterance_text);
820                 if (temp_result_json)
821                         free(temp_result_json);
822                 return -1; //MA_ERROR_OPERATION_FAILED;
823         } else {
824                 MAS_LOGD("[Dbus DEBUG] Success to Send result");
825                 dbus_connection_flush(mConnectionSender);
826         }
827
828         dbus_message_unref(msg);
829
830         if (temp_display_text)
831                 free(temp_display_text);
832         if (temp_utterance_text)
833                 free(temp_utterance_text);
834         if (temp_result_json)
835                 free(temp_result_json);
836 #endif
837         return 0;
838 }
839
840 int CServiceIpcDbus::masc_ui_dbus_change_assistant(const char* app_id)
841 {
842         if (0 != __dbus_check()) {
843                 return -1; //MAS_ERROR_OPERATION_FAILED;
844         }
845
846         if (NULL == app_id) {
847                 MAS_LOGE("@@ Request multi-assistant send change assistant request : Fail to make message");
848                 return -1; //MA_ERROR_OPERATION_FAILED;
849         } else {
850                 MAS_LOGD("[DEBUG] multi-assistant send change assistant request app_id(%s)", app_id);
851         }
852
853         DBusMessage* msg;
854
855         msg = dbus_message_new_method_call(
856                         MA_UI_CLIENT_SERVICE_NAME,
857                         MA_UI_CLIENT_SERVICE_OBJECT_PATH,
858                         MA_UI_CLIENT_SERVICE_INTERFACE,
859                         MAS_UI_METHOD_CHANGE_ASSISTANT);
860
861         dbus_message_append_args(msg,
862                 DBUS_TYPE_STRING, &app_id,
863                 DBUS_TYPE_INVALID);
864
865         dbus_message_set_no_reply(msg, TRUE);
866
867         if (1 != dbus_connection_send(mConnectionSender, msg, NULL)) {
868                 MAS_LOGE("[Dbus ERROR] Fail to Send");
869                 return -1; // MAS_ERROR_OPERATION_FAILED;
870         } else {
871                 MAS_LOGD("[Dbus DEBUG] Success to Send change assistant request");
872                 dbus_connection_flush(mConnectionSender);
873         }
874
875         dbus_message_unref(msg);
876
877         return 0;
878 }
879
880 int CServiceIpcDbus::masc_ui_dbus_send_error_message(int reason, const char* err_msg)
881 {
882         if (NULL == mConnectionSender) {
883                 MAS_LOGE("[Dbus ERROR] Dbus connection is not available");
884                 return -1;
885         }
886
887         DBusMessage* msg = NULL;
888
889         /* create a message */
890         msg = dbus_message_new_signal(
891                 MA_UI_CLIENT_SERVICE_OBJECT_PATH,       /* object name of the signal */
892                 MA_UI_CLIENT_SERVICE_INTERFACE,         /* interface name of the signal */
893                 MAS_UI_METHOD_ERROR);                           /* name of the signal */
894
895         if (NULL == msg) {
896                 MAS_LOGE("[Dbus ERROR] Fail to create error message");
897                 return -1;
898         }
899
900         char* temp_err_msg = NULL;
901
902         if (err_msg)
903                 temp_err_msg = strdup(err_msg);
904         else
905                 temp_err_msg = strdup("#NULL");
906
907         dbus_message_append_args(msg,
908                 DBUS_TYPE_INT32, &reason,
909                 DBUS_TYPE_STRING, &temp_err_msg,
910                 DBUS_TYPE_INVALID);
911
912         dbus_message_set_no_reply(msg, TRUE);
913
914         if (!dbus_connection_send(mConnectionSender, msg, NULL)) {
915                 MAS_LOGE("[Dbus ERROR] <<<< error message : Out Of Memory !");
916         } else {
917                 MAS_LOGD("<<<< Send error message : reason(%d), err_msg(%s)", reason, temp_err_msg);
918                 dbus_connection_flush(mConnectionSender);
919         }
920
921         dbus_message_unref(msg);
922
923         if (temp_err_msg)
924                 free(temp_err_msg);
925         return 0;
926 }
927
928 int CServiceIpcDbus::masc_ui_dbus_send_recognition_result(int pid, int result)
929 {
930         if (0 != __dbus_check()) {
931                 return -1; //MAS_ERROR_OPERATION_FAILED;
932         }
933
934         DBusMessage* msg;
935
936         msg = dbus_message_new_method_call(
937                         MA_UI_CLIENT_SERVICE_NAME,
938                         MA_UI_CLIENT_SERVICE_OBJECT_PATH,
939                         MA_UI_CLIENT_SERVICE_INTERFACE,
940                         MAS_UI_METHOD_SEND_RECOGNITION_RESULT);
941
942         if (NULL == msg) {
943                 MAS_LOGE("@@ Request multi-assistant send recognition result : Fail to make message");
944                 return -1; //MA_ERROR_OPERATION_FAILED;
945         } else {
946                 MAS_LOGD("[DEBUG] multi-assistant send recognition result(%d)", result);
947         }
948
949         dbus_message_append_args(msg,
950                 DBUS_TYPE_INT32, &pid,
951                 DBUS_TYPE_INT32, &result,
952                 DBUS_TYPE_INVALID);
953
954         dbus_message_set_no_reply(msg, TRUE);
955
956         if (1 != dbus_connection_send(mConnectionSender, msg, NULL)) {
957                 MAS_LOGE("[Dbus ERROR] Fail to Send");
958                 return -1; // MAS_ERROR_OPERATION_FAILED;
959         } else {
960                 MAS_LOGD("[Dbus DEBUG] Success to Send ASR result");
961                 dbus_connection_flush(mConnectionSender);
962         }
963
964         dbus_message_unref(msg);
965
966         return 0;
967 }
968
969 int CServiceIpcDbus::masc_ui_dbus_enable_common_ui(int enable)
970 {
971         if (0 != __dbus_check()) {
972                 return -1; //MAS_ERROR_OPERATION_FAILED;
973         }
974
975         DBusMessage* msg;
976
977         msg = dbus_message_new_method_call(
978                         MA_UI_CLIENT_SERVICE_NAME,
979                         MA_UI_CLIENT_SERVICE_OBJECT_PATH,
980                         MA_UI_CLIENT_SERVICE_INTERFACE,
981                         MAS_UI_METHOD_ENABLE_COMMON_UI);
982
983         if (NULL == msg) {
984                 MAS_LOGE("@@ Request multi-assistant enable common ui : Fail to make message");
985                 return -1; //MA_ERROR_OPERATION_FAILED;
986         } else {
987                 MAS_LOGD("[DEBUG] multi-assistant enable common ui (%d)", enable);
988         }
989
990         dbus_message_append_args(msg,
991                 DBUS_TYPE_INT32, &enable,
992                 DBUS_TYPE_INVALID);
993
994         dbus_message_set_no_reply(msg, TRUE);
995
996         if (1 != dbus_connection_send(mConnectionSender, msg, NULL)) {
997                 MAS_LOGE("[Dbus ERROR] Fail to Send");
998                 return -1; // MAS_ERROR_OPERATION_FAILED;
999         } else {
1000                 MAS_LOGD("[Dbus DEBUG] Success to Send ASR result");
1001                 dbus_connection_flush(mConnectionSender);
1002         }
1003
1004         dbus_message_unref(msg);
1005
1006         return 0;
1007 }
1008
1009 static Eina_Bool listener_event_callback(void* data, Ecore_Fd_Handler *fd_handler)
1010 {
1011         CServiceIpcDbus* service_ipc = static_cast<CServiceIpcDbus*>(data);
1012         if (NULL == service_ipc) {
1013                 MAS_LOGE("Error : service_ipc NULL");
1014                 return ECORE_CALLBACK_RENEW;
1015         }
1016
1017         DBusConnection* mConnectionListener = service_ipc->get_connection_listener();
1018         if (NULL == mConnectionListener) {
1019                 MAS_LOGE("Error : mConnectionListener NULL");
1020                 return ECORE_CALLBACK_RENEW;
1021         }
1022
1023         CServiceIpcDbusDispatcher* dispatcher = service_ipc->get_dispatcher();
1024         if (NULL == dispatcher) {
1025                 MAS_LOGE("Error : dispatcher NULL");
1026                 return ECORE_CALLBACK_RENEW;
1027         }
1028
1029         dbus_connection_read_write_dispatch(mConnectionListener, 50);
1030
1031         while (1) {
1032                 DBusMessage* msg = NULL;
1033                 msg = dbus_connection_pop_message(mConnectionListener);
1034
1035                 if (true != dbus_connection_get_is_connected(mConnectionListener)) {
1036                         MAS_LOGE("[ERROR] Connection is disconnected");
1037                         return ECORE_CALLBACK_RENEW;
1038                 }
1039
1040                 /* loop again if we haven't read a message */
1041                 if (NULL == msg) {
1042                         return ECORE_CALLBACK_RENEW;
1043                 }
1044
1045                 /* client event */
1046                 if (dbus_message_is_method_call(msg, MA_SERVER_SERVICE_INTERFACE, MA_METHOD_HELLO)) {
1047                         dispatcher->on_hello(mConnectionListener, msg);
1048
1049                 } else if (dbus_message_is_method_call(msg, MA_SERVER_SERVICE_INTERFACE, MA_METHOD_INITIALIZE)) {
1050                         dispatcher->on_initialize(mConnectionListener, msg);
1051
1052                 } else if (dbus_message_is_method_call(msg, MA_SERVER_SERVICE_INTERFACE, MA_METHOD_DEINITIALIZE)) {
1053                         dispatcher->on_deinitialize(mConnectionListener, msg);
1054
1055                 } else if (dbus_message_is_method_call(msg, MA_SERVER_SERVICE_INTERFACE, MA_METHOD_GET_RECORDING_AUDIO_FORMAT)) {
1056                         dispatcher->on_get_audio_format(mConnectionListener, msg);
1057
1058                 } else if (dbus_message_is_method_call(msg, MA_SERVER_SERVICE_INTERFACE, MA_METHOD_GET_RECORDING_AUDIO_SOURCE_TYPE)) {
1059                         dispatcher->on_get_audio_source_type(mConnectionListener, msg);
1060
1061                 } else if (dbus_message_is_method_call(msg, MA_SERVER_SERVICE_INTERFACE, MA_METHOD_SEND_ASR_RESULT)) {
1062                         dispatcher->on_send_asr_result(mConnectionListener, msg);
1063
1064                 } else if (dbus_message_is_method_call(msg, MA_SERVER_SERVICE_INTERFACE, MA_METHOD_SEND_RESULT)) {
1065                         dispatcher->on_send_result(mConnectionListener, msg);
1066
1067                 } else if (dbus_message_is_method_call(msg, MA_SERVER_SERVICE_INTERFACE, MA_METHOD_SEND_RECOGNITION_RESULT)) {
1068                         dispatcher->on_send_recognition_result(mConnectionListener, msg);
1069
1070                 } else if (dbus_message_is_method_call(msg, MA_SERVER_SERVICE_INTERFACE, MA_METHOD_START_STREAMING_AUDIO_DATA)) {
1071                         dispatcher->on_start_streaming_audio_data(mConnectionListener, msg);
1072
1073                 } else if (dbus_message_is_method_call(msg, MA_SERVER_SERVICE_INTERFACE, MA_METHOD_STOP_STREAMING_AUDIO_DATA)) {
1074                         dispatcher->on_stop_streaming_audio_data(mConnectionListener, msg);
1075
1076                 } else if (dbus_message_is_method_call(msg, MA_SERVER_SERVICE_INTERFACE, MA_METHOD_UPDATE_VOICE_FEEDBACK_STATE)) {
1077                         dispatcher->on_update_voice_feedback_state(mConnectionListener, msg);
1078
1079                 } else if (dbus_message_is_method_call(msg, MA_SERVER_SERVICE_INTERFACE, MA_METHOD_SEND_ASSISTANT_SPECIFIC_COMMAND)) {
1080                         dispatcher->on_send_assistant_specific_command(mConnectionListener, msg);
1081
1082                 } else if (dbus_message_is_method_call(msg, MA_SERVER_SERVICE_INTERFACE, MA_METHOD_SET_BACKGROUND_VOLUME)) {
1083                         dispatcher->on_set_background_volume(mConnectionListener, msg);
1084
1085                 } else if (dbus_message_is_method_call(msg, MA_SERVER_SERVICE_INTERFACE, MA_METHOD_SET_PREPROCESSING_ALLOW_MODE)) {
1086                         dispatcher->on_set_preprocessing_allow_mode(mConnectionListener, msg);
1087
1088                 } else if (dbus_message_is_method_call(msg, MA_SERVER_SERVICE_INTERFACE, MA_METHOD_SEND_PREPROCESSING_RESULT)) {
1089                         dispatcher->on_send_preprocessing_result(mConnectionListener, msg);
1090
1091                 } else if (dbus_message_is_method_call(msg, MA_SERVER_SERVICE_INTERFACE, MA_METHOD_SET_WAKE_WORD_AUDIO_REQUIRE_FLAG)) {
1092                         dispatcher->on_set_wake_word_audio_require_flag(mConnectionListener, msg);
1093
1094                 } else if (dbus_message_is_method_call(msg, MA_SERVER_SERVICE_INTERFACE, MA_METHOD_SET_ASSISTANT_WAKEUP_LANGUAGE)) {
1095                         dispatcher->on_set_assistant_language(mConnectionListener, msg);
1096
1097                 } else if (dbus_message_is_method_call(msg, MA_SERVER_SERVICE_INTERFACE, MA_METHOD_ADD_WAKE_WORD)) {
1098                         dispatcher->on_add_wake_word(mConnectionListener, msg);
1099
1100                 } else if (dbus_message_is_method_call(msg, MA_SERVER_SERVICE_INTERFACE, MA_METHOD_REMOVE_WAKE_WORD)) {
1101                         dispatcher->on_remove_wake_word(mConnectionListener, msg);
1102
1103                 } else if (dbus_message_is_method_call(msg, MA_SERVER_SERVICE_INTERFACE, MA_UI_METHOD_INITIALIZE)) {
1104                         dispatcher->on_ui_initialize(mConnectionListener, msg);
1105
1106                 } else if (dbus_message_is_method_call(msg, MA_SERVER_SERVICE_INTERFACE, MA_UI_METHOD_DEINITIALIZE)) {
1107                         dispatcher->on_ui_deinitialize(mConnectionListener, msg);
1108
1109                 } else if (dbus_message_is_method_call(msg, MA_SERVER_SERVICE_INTERFACE, MA_UI_METHOD_CHANGE_ASSISTANT)) {
1110                         dispatcher->on_ui_change_assistant(mConnectionListener, msg);
1111
1112                 } else {
1113                         MAS_LOGD("Message is NOT valid");
1114                         /* Invalid method */
1115                 }
1116                 /* free the message */
1117                 dbus_message_unref(msg);
1118         }
1119
1120         return ECORE_CALLBACK_RENEW;
1121 }
1122
1123 void CServiceIpcDbus::connection_free()
1124 {
1125         if (NULL != mConnectionListener) {
1126                 dbus_connection_close(mConnectionListener);
1127                 dbus_connection_unref(mConnectionListener);
1128                 mConnectionListener = NULL;
1129         }
1130         if (NULL != mConnectionSender) {
1131                 dbus_connection_close(mConnectionSender);
1132                 dbus_connection_unref(mConnectionSender);
1133                 mConnectionSender = NULL;
1134         }
1135 }
1136
1137 int CServiceIpcDbus::open_connection()
1138 {
1139         DBusError err;
1140         dbus_error_init(&err);
1141
1142         int ret;
1143
1144         /* Create connection for sender */
1145         mConnectionSender = dbus_bus_get_private(DBUS_BUS_SESSION, &err);
1146
1147         if (dbus_error_is_set(&err)) {
1148                 MAS_LOGE("[Dbus ERROR] Fail dbus_bus_get : %s", err.message);
1149                 dbus_error_free(&err);
1150         }
1151
1152         if (NULL == mConnectionSender) {
1153                 MAS_LOGE("[Dbus ERROR] Fail to get dbus connection");
1154                 return -1;
1155         }
1156
1157         dbus_connection_set_exit_on_disconnect(mConnectionSender, false);
1158
1159         /* connect to the bus and check for errors */
1160         mConnectionListener = dbus_bus_get_private(DBUS_BUS_SESSION, &err);
1161
1162         if (dbus_error_is_set(&err)) {
1163                 MAS_LOGE("[Dbus ERROR] Fail dbus_bus_get : %s", err.message);
1164                 dbus_error_free(&err);
1165         }
1166
1167         if (NULL == mConnectionListener) {
1168                 MAS_LOGE("[Dbus ERROR] Fail to get dbus connection");
1169                 connection_free();
1170                 return -1;
1171         }
1172
1173         dbus_connection_set_exit_on_disconnect(mConnectionListener, false);
1174
1175         /* request our name on the bus and check for errors */
1176         ret = dbus_bus_request_name(mConnectionListener, MA_SERVER_SERVICE_NAME, DBUS_NAME_FLAG_REPLACE_EXISTING, &err);
1177
1178         if (DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER != ret) {
1179                 printf("Fail to be primary owner in dbus request.");
1180                 MAS_LOGE("[Dbus ERROR] Fail to be primary owner");
1181                 connection_free();
1182                 return -1;
1183         }
1184
1185         if (dbus_error_is_set(&err)) {
1186                 MAS_LOGE("[Dbus ERROR] dbus_bus_request_name() : %s", err.message);
1187                 dbus_error_free(&err);
1188                 connection_free();
1189                 return -1;
1190         }
1191
1192         /* Flush messages which are received before fd event handler registration */
1193         while (DBUS_DISPATCH_DATA_REMAINS == dbus_connection_get_dispatch_status(mConnectionListener)) {
1194                 listener_event_callback(this, NULL);
1195         }
1196
1197         /* add a rule for getting signal */
1198         char rule[128];
1199         snprintf(rule, 128, "type='signal',interface='%s'", MA_SERVER_SERVICE_INTERFACE);
1200
1201         /* add a rule for which messages we want to see */
1202         dbus_bus_add_match(mConnectionListener, rule, &err);/* see signals from the given interface */
1203
1204         if (dbus_error_is_set(&err)) {
1205                 MAS_LOGE("[Dbus ERROR] dbus_bus_add_match() : %s", err.message);
1206                 dbus_error_free(&err);
1207                 connection_free();
1208                 return -1;
1209         }
1210
1211         int fd = 0;
1212         if (1 != dbus_connection_get_unix_fd(mConnectionListener, &fd)) {
1213                 MAS_LOGE("fail to get fd from dbus ");
1214                 connection_free();
1215                 return -1;
1216         } else {
1217                 MAS_LOGD("Get fd from dbus : %d", fd);
1218         }
1219
1220         mFdHandler = ecore_main_fd_handler_add(fd, ECORE_FD_READ, (Ecore_Fd_Cb)listener_event_callback, this, NULL, NULL);
1221
1222         if (NULL == mFdHandler) {
1223                 MAS_LOGE("[Dbus ERROR] Fail to get fd handler");
1224                 connection_free();
1225                 return -1;
1226         }
1227
1228         return 0;
1229 }
1230
1231 int CServiceIpcDbus::close_connection()
1232 {
1233         DBusError err;
1234         dbus_error_init(&err);
1235
1236         if (NULL != mFdHandler) {
1237                 ecore_main_fd_handler_del(mFdHandler);
1238                 mFdHandler = NULL;
1239         }
1240
1241         dbus_bus_release_name(mConnectionListener, MA_SERVER_SERVICE_NAME, &err);
1242
1243         if (dbus_error_is_set(&err)) {
1244                 MAS_LOGE("[Dbus ERROR] dbus_bus_release_name() : %s", err.message);
1245                 dbus_error_free(&err);
1246         }
1247
1248         connection_free();
1249
1250         return 0;
1251 }