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