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