Refactor codes 82/141982/4
authorjusung son <jusung07.son@samsung.com>
Wed, 2 Aug 2017 06:51:24 +0000 (15:51 +0900)
committerjusung son <jusung07.son@samsung.com>
Thu, 3 Aug 2017 04:28:53 +0000 (04:28 +0000)
- Remove duplicated codes

Change-Id: I441952eeab9ca0c0c85cb3824fe2684057aeb827
Signed-off-by: jusung son <jusung07.son@samsung.com>
13 files changed:
include/message-port.h [deleted file]
include/message_port_error.h [deleted file]
include/message_port_internal.h [deleted file]
src/inc/message-port-log.h [deleted file]
src/inc/message_port_common.h [new file with mode: 0755]
src/inc/message_port_local.h [new file with mode: 0755]
src/inc/message_port_remote.h [new file with mode: 0755]
src/message-port.c [deleted file]
src/message_port.c
src/message_port_common.c [new file with mode: 0755]
src/message_port_internal.c [deleted file]
src/message_port_local.c [new file with mode: 0755]
src/message_port_remote.c [new file with mode: 0755]

diff --git a/include/message-port.h b/include/message-port.h
deleted file mode 100644 (file)
index fe748c0..0000000
+++ /dev/null
@@ -1,330 +0,0 @@
-/*
- * Open Service Platform
- * Copyright (c) 2012 Samsung Electronics Co., Ltd.
- *
- * Licensed under the Apache License, Version 2.0 (the License);
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- *     http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-
-#ifndef __APPFW_MESSAGE_PORT_H__
-#define __APPFW_MESSAGE_PORT_H__
-
-#include <bundle.h>
-#include <message_port_error.h>
-
-#ifdef __cplusplus
-extern "C" {
-#endif
-
-/**
- * @brief   Called when a message is received from the remote application.
- *
- * @param [in] id The message port id returned by messageport_register_local_port() or messageport_register_trusted_local_port()
- * @param [in] remote_app_id The ID of the remote application which has sent this message
- * @param [in] remote_port The name of the remote message port
- * @param [in] trusted_message @c true if the trusted remote message port is ready to receive the response data
- * @param [in] data the data passed from the remote application
- * @remarks @a data must be released with bundle_free() by you
- * @remark @a remote_app_id and @a remote_port will be set if the remote application sends a bidirectional message, otherwise they are NULL.
- */
-typedef void (*messageport_message_cb)(int id, const char *remote_app_id, const char *remote_port, bool trusted_message, bundle *data, void *user_data);
-
-/**
- * @brief Called when a remote port is registered or unregistered.
- * @details The function is called when a remote port is registered or unregistered
- *          from the remote application.
- * @remarks @a remote_app_id and @a remote_port can be used until
- *          messageport_remove_registration_event_cb() is called for the watcher which reported the event.
- * @param[in] remote_app_id        The ID of the remote application that sent this message
- * @param[in] remote_port          The name of the remote message port
- * @param[in] trusted_remote_port  Indicates whether remote port is trusted
- * @param[in] user_data            The user data passed from the register function
- * @pre Called when a remote port is registered or unregistered if you add it using
- *      messageport_add_registered_cb() or messageport_add_unregistered_cb() respectively.
- * @see messageport_add_registered_cb()
- * @see messageport_add_unregistered_cb()
- * @see messageport_remove_registration_event_cb()
- */
-typedef void (*messageport_registration_event_cb)(const char *remote_app_id,
-                                                  const char *remote_port,
-                                                  bool trusted_remote_port,
-                                                  void *user_data);
-
-/**
- * @brief Unregisters the local message port. @n
- *
- * @param [in] local_port_id the id of the local message port
- * @param [in] trusted_port true if target port is trusted port
- * @return Return positive on success, otherwise a negative error value.
- * @retval #MESSAGEPORT_ERROR_NONE Successful
- * @retval #MESSAGEPORT_ERROR_INVALID_PARAMETER Invalid parameter
- * @retval #MESSAGEPORT_ERROR_OUT_OF_MEMORY Out of memory
- * @retval #MESSAGEPORT_ERROR_MESSAGEPORT_NOT_FOUND The message port of the remote application is not found
- */
- int messageport_unregister_local_port(int local_port_id, bool trusted_port);
-
-/**
- * @brief Registers the local message port. @n
- * If the message port name is already registered, the previous message port id returns and the callback function is changed.
- *
- * @param [in] local_port the name of the local message port
- * @param [in] callback The callback function to be called when a message is received
- * @return A message port id on success, otherwise a negative error value.
- * @retval #MESSAGEPORT_ERROR_NONE Successful
- * @retval #MESSAGEPORT_ERROR_INVALID_PARAMETER Invalid parameter
- * @retval #MESSAGEPORT_ERROR_OUT_OF_MEMORY Out of memory
- * @retval #MESSAGEPORT_ERROR_IO_ERROR Internal I/O error
- * @retval #MESSAGEPORT_ERROR_RESOURCE_UNAVAILABLE Resource temporarily unavailable
- */
- int messageport_register_local_port(const char *local_port, messageport_message_cb callback);
-
-/**
- * @brief Registers the trusted local message port. @n
- * If the message port name is already registered, the previous message port id returns and the callback function is changed. @n
- * This allows communications only if the applications are signed with the same certificate which is uniquely assigned to the developer.
- *
- * @param [in] local_port the name of the local message port
- * @param [in] callback The callback function to be called when a message is received
- * @return A message port id on success, otherwise a negative error value.
- * @retval #MESSAGEPORT_ERROR_NONE Successful
- * @retval #MESSAGEPORT_ERROR_INVALID_PARAMETER Invalid parameter
- * @retval #MESSAGEPORT_ERROR_OUT_OF_MEMORY Out of memory
- * @retval #MESSAGEPORT_ERROR_IO_ERROR Internal I/O error
- * @retval #MESSAGEPORT_ERROR_RESOURCE_UNAVAILABLE Resource temporarily unavailable
- */
- int messageport_register_trusted_local_port(const char *local_port, messageport_message_cb callback);
-
-/**
- * @brief Checks if the message port of a remote application is registered.
- *
- * @param [in] remote_app_id The ID of the remote application
- * @param [in] remote_port the name of the remote message port
- * @param [out] exist @c true if the message port of the remote application exists, otherwise @c false
- * @return 0 on success, otherwise a negative error value.
- * @retval #MESSAGEPORT_ERROR_NONE Successful
- * @retval #MESSAGEPORT_ERROR_INVALID_PARAMETER Invalid parameter
- * @retval #MESSAGEPORT_ERROR_OUT_OF_MEMORY Out of memory
- * @retval #MESSAGEPORT_ERROR_IO_ERROR Internal I/O error
- * @retval #MESSAGEPORT_ERROR_RESOURCE_UNAVAILABLE Resource temporarily unavailable
- */
- int messageport_check_remote_port(const char *remote_app_id, const char *remote_port, bool *exist);
-
-/**
- * @brief Checks if the trusted message port of a remote application is registered.
- *
- * @param [in] remote_app_id The ID of the remote application
- * @param [in] remote_port the name of the remote message port
- * @param [out] exist @c true if the message port of the remote application exists, otherwise @c false
- * @return 0 on success, otherwise a negative error value.
- * @retval #MESSAGEPORT_ERROR_NONE Successful
- * @retval #MESSAGEPORT_ERROR_INVALID_PARAMETER Invalid parameter
- * @retval #MESSAGEPORT_ERROR_OUT_OF_MEMORY Out of memory
- * @retval #MESSAGEPORT_ERROR_CERTIFICATE_NOT_MATCH The remote application is not signed with the same certificate
- * @retval #MESSAGEPORT_ERROR_IO_ERROR Internal I/O error
- * @retval #MESSAGEPORT_ERROR_RESOURCE_UNAVAILABLE Resource temporarily unavailable
- */
- int messageport_check_trusted_remote_port(const char *remote_app_id, const char *remote_port, bool *exist);
-
-/**
- * @brief Sends a message to the message port of a remote application.
- *
- * @param [in] remote_app_id The ID of the remote application
- * @param [in] remote_port the name of the remote message port
- * @param [in] message the message to be passed to the remote application, the recommended message size is under 4KB
- * @return 0 on success, otherwise a negative error value.
- * @retval #MESSAGEPORT_ERROR_NONE Successful
- * @retval #MESSAGEPORT_ERROR_INVALID_PARAMETER Invalid parameter
- * @retval #MESSAGEPORT_ERROR_OUT_OF_MEMORY Out of memory
- * @retval #MESSAGEPORT_ERROR_MESSAGEPORT_NOT_FOUND The message port of the remote application is not found
- * @retval #MESSAGEPORT_ERROR_MAX_EXCEEDED The size of message has exceeded the maximum limit
- * @retval #MESSAGEPORT_ERROR_IO_ERROR Internal I/O error
- * @retval #MESSAGEPORT_ERROR_RESOURCE_UNAVAILABLE Resource temporarily unavailable
- *
- * @code
- * #include <message-port.h>
- *
- * bundle *b = bundle_create();
- * bundle_add(b, "key1", "value1");
- * bundle_add(b, "key2", "value2");
- *
- * int ret = messageport_send_message("0123456789.BasicApp", "BasicAppPort", b);
- *
- * bundle_free(b);
- * @endcode
- */
- int messageport_send_message(const char *remote_app_id, const char *remote_port, bundle *message);
-
-/**
- * @brief Sends a trusted message to the message port of a remote application. @n
- *  This allows communications only if the applications are signed with the same certificate which is uniquely assigned to the developer.
- *
- * @param [in] remote_app_id The ID of the remote application
- * @param [in] remote_port the name of the remote message port
- * @param [in] message the message to be passed to the remote application, the recommended message size is under 4KB
- * @return 0 on success, otherwise a negative error value.
- * @retval #MESSAGEPORT_ERROR_NONE Successful
- * @retval #MESSAGEPORT_ERROR_INVALID_PARAMETER Invalid parameter
- * @retval #MESSAGEPORT_ERROR_OUT_OF_MEMORY Out of memory
- * @retval #MESSAGEPORT_ERROR_MESSAGEPORT_NOT_FOUND The message port of the remote application is not found
- * @retval #MESSAGEPORT_ERROR_CERTIFICATE_NOT_MATCH The remote application is not signed with the same certificate
- * @retval #MESSAGEPORT_ERROR_MAX_EXCEEDED The size of message has exceeded the maximum limit
- * @retval #MESSAGEPORT_ERROR_IO_ERROR Internal I/O error
- * @retval #MESSAGEPORT_ERROR_RESOURCE_UNAVAILABLE Resource temporarily unavailable
- */
- int messageport_send_trusted_message(const char *remote_app_id, const char *remote_port, bundle *message);
-
-/**
- * @brief Sends a message to the message port of a remote application. This method is used for the bidirectional communication.
- *
- * @param [in] id The message port id returned by messageport_register_local_port() or messageport_register_trusted_local_port()
- * @param [in] remote_app_id The ID of the remote application
- * @param [in] remote_port the name of the remote message port
- * @param [in] message the message to be passed to the remote application, the recommended message size is under 4KB
- * @return 0 on success, otherwise a negative error value.
- * @retval #MESSAGEPORT_ERROR_NONE Successful
- * @retval #MESSAGEPORT_ERROR_INVALID_PARAMETER Invalid parameter
- * @retval #MESSAGEPORT_ERROR_OUT_OF_MEMORY Out of memory
- * @retval #MESSAGEPORT_ERROR_MESSAGEPORT_NOT_FOUND The message port of the remote application is not found
- * @retval #MESSAGEPORT_ERROR_MAX_EXCEEDED The size of message has exceeded the maximum limit
- * @retval #MESSAGEPORT_ERROR_IO_ERROR Internal I/O error
- * @retval #MESSAGEPORT_ERROR_RESOURCE_UNAVAILABLE Resource temporarily unavailable
- *
- * @code
- * #include <message-port.h>
- *
- * static void
- * OnMessageReceived(int id, const char* remote_app_id, const char* remote_port, bool trusted_port, bundle* data)
- * {
- * }
- *
- * int main(int argc, char *argv[])
- * {
- *   bundle *b = bundle_create();
- *   bundle_add(b, "key1", "value1");
- *   bundle_add(b, "key2", "value2");
- *
- *   int id = messageport_register_local_port("HelloPort", OnMessageReceived);
- *
- *   int ret = messageport_send_bidirectional_message(id, "0123456789.BasicApp", "BasicAppPort", b);
- *
- *   bundle_free(b);
- * }
- */
- int messageport_send_bidirectional_message(int id, const char *remote_app_id, const char *remote_port, bundle *data);
-
-/**
- * @brief Sends a trusted message to the message port of a remote application. This method is used for the bidirectional communication.
- *  This allows communications only if the applications are signed with the same certificate which is uniquely assigned to the developer.
- *
- * @param [in] id The message port id returned by messageport_register_local_port() or messageport_register_trusted_local_port()
- * @param [in] remote_app_id The ID of the remote application
- * @param [in] remote_port the name of the remote message port
- * @param [in] message the message to be passed to the remote application, the recommended message size is under 4KB
- * @return 0 on success, otherwise a negative error value.
- * @retval #MESSAGEPORT_ERROR_NONE Successful
- * @retval #MESSAGEPORT_ERROR_INVALID_PARAMETER Invalid parameter
- * @retval #MESSAGEPORT_ERROR_OUT_OF_MEMORY Out of memory
- * @retval #MESSAGEPORT_ERROR_MESSAGEPORT_NOT_FOUND The message port of the remote application is not found
- * @retval #MESSAGEPORT_ERROR_CERTIFICATE_NOT_MATCH The remote application is not signed with the same certificate
- * @retval #MESSAGEPORT_ERROR_MAX_EXCEEDED The size of message has exceeded the maximum limit
- * @retval #MESSAGEPORT_ERROR_IO_ERROR Internal I/O error
- * @retval #MESSAGEPORT_ERROR_RESOURCE_UNAVAILABLE Resource temporarily unavailable
- */
- int messageport_send_bidirectional_trusted_message(int id, const char *remote_app_id, const char *remote_port, bundle *data);
-
-/**
- * @brief Adds a callback called when a remote port is registered.
- * @details When remote port is registered, @a registered_cb function is called.
- *          Each added callback has its own separate watcher.
- * @remarks The specified callback is called only in the main thread.
- * @param[in] remote_app_id        The ID of the remote application
- * @param[in] remote_port          The name of the remote message port
- * @param[in] trusted_remote_port  Indicates whether remote port is trusted
- * @param[in] registered_cb        The callback function to be called
- *                                 when remote port is registered
- * @param[in] user_data            The user data to be passed to the callback function
- * @param[out] watcher_id          The ID of the watcher which is monitoring the remote port
- *                                 registration events
- * @return @c 0 on success,
- *         otherwise a negative error value
- * @retval #MESSAGEPORT_ERROR_INVALID_PARAMETER  The specified @a remote_app_id or @a remote_port
- *                                               or @a registered_cb is NULL
- * @retval #MESSAGE_PORT_ERROR_OUT_OF_MEMORY     Out of memory
- * @retval #MESSAGE_PORT_ERROR_IO_ERROR          Internal I/O error
- * @see messageport_registration_event_cb()
- * @see messageport_add_unregistered_cb()
- * @see messageport_remove_registration_event_cb()
- */
-int messageport_add_registered_cb(const char *remote_app_id,
-                                const char *remote_port,
-                                bool trusted_remote_port,
-                                messageport_registration_event_cb registered_cb,
-                                void *user_data,
-                                int *watcher_id);
-
-/**
- * @brief Adds a callback called when a remote port is unregistered.
- * @details When remote port is unregistered, @a unregistered_cb function is called.
- *          Each added callback has its own separate watcher.
- * @remarks The specified callback is called only in the main thread.
- * @param[in] remote_app_id        The ID of the remote application
- * @param[in] remote_port          The name of the remote message port
- * @param[in] trusted_remote_port  Indicates whether remote port is trusted
- * @param[in] unregistered_cb      The callback function to be called
- *                                 when remote port is unregistered
- * @param[in] user_data            The user data to be passed to the callback function
- * @param[out] watcher_id          The ID of the watcher which is monitoring the remote port
- *                                 unregistration events
- * @return @c 0 on success,
- *         otherwise a negative error value
- * @retval #MESSAGEPORT_ERROR_INVALID_PARAMETER  The specified @a remote_app_id or @a remote_port
- *                                               or @a unregistered_cb is NULL
- * @retval #MESSAGE_PORT_ERROR_OUT_OF_MEMORY     Out of memory
- * @retval #MESSAGE_PORT_ERROR_IO_ERROR          Internal I/O error
- * @see messageport_registration_event_cb()
- * @see messageport_add_registered_cb()
- * @see messageport_remove_registration_event_cb()
- */
-int messageport_add_unregistered_cb(const char *remote_app_id,
-                                const char *remote_port,
-                                bool trusted_remote_port,
-                                messageport_registration_event_cb unregistered_cb,
-                                void *user_data,
-                                int *watcher_id);
-
-
-
-/**
- * @brief Removes the registration/unregistration callbacks associated with the given watcher.
- * @param[in] watcher_id  The ID of watcher which is monitoring remote port
- *                        registration/unregistration events
- * @return @c 0 on success,
- *         otherwise a negative error value
- * @retval #MESSAGE_PORT_ERROR_INVALID_PARAMETER  The specified @a watcher_id is not correct
- * @retval #MESSAGE_PORT_ERROR_IO_ERROR           Internal I/O error
- * @see messageport_registration_event_cb()
- * @see messageport_add_registered_cb()
- * @see messageport_add_unregistered_cb()
- */
-int messageport_remove_registration_event_cb(int watcher_id);
-
-
-/**
- * @}
- */
-
-#ifdef __cplusplus
-}
-#endif
-
-#endif /* __APPFW_MESSAGE_PORT_H__ */
diff --git a/include/message_port_error.h b/include/message_port_error.h
deleted file mode 100644 (file)
index 3a16b0c..0000000
+++ /dev/null
@@ -1,38 +0,0 @@
-/*
- * Open Service Platform
- * Copyright (c) 2012 Samsung Electronics Co., Ltd.
- *
- * Licensed under the Apache License, Version 2.0 (the License);
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- *    http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-
-#ifndef __APPFW_MESSAGE_PORT_ERROR_H__
-#define __APPFW_MESSAGE_PORT_ERROR_H__
-
-#include <errno.h>
-
-/**
- * @brief Enumerations of error code for Application.
- */
-typedef enum {
-       MESSAGEPORT_ERROR_NONE = 0,                                     /**< Successful */
-       MESSAGEPORT_ERROR_IO_ERROR = -EIO,                      /**< Internal I/O error */
-       MESSAGEPORT_ERROR_OUT_OF_MEMORY = -ENOMEM,              /**< Out of memory */
-       MESSAGEPORT_ERROR_INVALID_PARAMETER = -EINVAL,  /**< Invalid parameter */
-       MESSAGEPORT_ERROR_MESSAGEPORT_NOT_FOUND = -ENOKEY,      /**< The message port of the remote application is not found */
-       MESSAGEPORT_ERROR_CERTIFICATE_NOT_MATCH = -EACCES,      /**< The remote application is not signed with the same certificate */
-       MESSAGEPORT_ERROR_MAX_EXCEEDED = -EMSGSIZE,                     /**< The size of message has exceeded the maximum limit */
-       MESSAGEPORT_ERROR_RESOURCE_UNAVAILABLE = -EBUSY,        /**< Resource temporarily unavailable */
-} messageport_error_e;
-
-#endif /* __APPFW_MESSAGE_PORT_ERROR_H__ */
diff --git a/include/message_port_internal.h b/include/message_port_internal.h
deleted file mode 100644 (file)
index d97ba29..0000000
+++ /dev/null
@@ -1,33 +0,0 @@
-/*
- * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
- *
- * Licensed under the Apache License, Version 2.0 (the License);
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an AS IS BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-
-#ifndef __TIZEN_APPFW_MESSAGE_PORT_INTERNAL_H__
-#define __TIZEN_APPFW_MESSAGE_PORT_INTERNAL_H__
-
-#include <message_port_error.h>
-
-#ifdef __cplusplus
-extern "C" {
-#endif
-
-int convert_to_tizen_error(messageport_error_e error);
-
-#ifdef __cplusplus
-}
-#endif
-
-#endif /*  __TIZEN_APPFW_MESSAGE_PORT_INTERNAL_H__ */
diff --git a/src/inc/message-port-log.h b/src/inc/message-port-log.h
deleted file mode 100755 (executable)
index e32eccf..0000000
+++ /dev/null
@@ -1,48 +0,0 @@
-/*
- * Open Service Platform
- * Copyright (c) 2012 Samsung Electronics Co., Ltd.
- *
- * Licensed under the Apache License, Version 2.0 (the License);
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- *     http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-
-#ifndef __APPFW_MESSAGE_PORT_LOG_H__
-#define __APPFW_MESSAGE_PORT_LOG_H__
-
-#include <dlog.h>
-#include <bundle.h>
-
-#ifdef __cplusplus
-extern "C" {
-#endif
-
-#undef LOG_TAG
-#define LOG_TAG "MESSAGE_PORT"
-
-#define _LOGE(fmt, arg...) LOGE(fmt, ##arg)
-#define _LOGI(fmt, arg...) LOGI(fmt, ##arg)
-#define _LOGD(fmt, arg...) LOGD(fmt, ##arg)
-
-#define _SECURE_LOGE(fmt, arg...) SECURE_LOGE(fmt, ##arg)
-#define _SECURE_LOGI(fmt, arg...) SECURE_LOGI(fmt, ##arg)
-#define _SECURE_LOGD(fmt, arg...) SECURE_LOGD(fmt, ##arg)
-
-/**
- * @}
- */
-
-#ifdef __cplusplus
-}
-#endif
-
-#endif /* __APPFW_MESSAGE_PORT_LOG_H__ */
diff --git a/src/inc/message_port_common.h b/src/inc/message_port_common.h
new file mode 100755 (executable)
index 0000000..568cea2
--- /dev/null
@@ -0,0 +1,93 @@
+/*
+ * Copyright (c) 2017 Samsung Electronics Co., Ltd All Rights Reserved
+ *
+ * Licensed under the Apache License, Version 2.0 (the License);
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an AS IS BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef __TIZEN_APPFW_MESSAGE_PORT_COMMOM_H__
+#define __TIZEN_APPFW_MESSAGE_PORT_COMMOM_H__
+
+#include <bundle.h>
+#include <gio/gio.h>
+
+#include <message_port.h>
+#include <message_port_log.h>
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+#define MESSAGEPORT_BUS_NAME_PREFIX "org.tizen.messageport._"
+#define MESSAGEPORT_OBJECT_PATH "/org/tizen/messageport"
+#define MESSAGEPORT_INTERFACE_PREFIX "org.tizen.messageport._"
+
+#define DBUS_SERVICE_DBUS "org.freedesktop.DBus"
+#define DBUS_PATH_DBUS "/org/freedesktop/DBus"
+#define DBUS_INTERFACE_DBUS "org.freedesktop.DBus"
+
+
+#define retvm_if(expr, val, fmt, arg...) do { \
+       if (expr) { \
+               _LOGE(fmt, ##arg); \
+               _LOGE("(%s) -> %s() return", #expr, __func__); \
+               return val; \
+       } \
+} while (0)
+
+#define retv_if(expr, val) do { \
+       if (expr) { \
+               _LOGE("(%s) -> %s() return", #expr, __func__); \
+               return val; \
+       } \
+} while (0)
+
+#define FREE_AND_NULL(ptr) do { \
+       if (ptr) { \
+               free((void *)ptr); \
+               ptr = NULL; \
+       } \
+} while (0)
+
+int write_socket(int fd,
+               const char *buffer,
+               unsigned int nbytes,
+               unsigned int *bytes_write,
+               int *sequence);
+int write_string_to_socket(int fd,
+               const char *buffer,
+               int string_len,
+               unsigned int *bytes_write,
+               int *sequence);
+int read_socket(int fd,
+               char *buffer,
+               unsigned int nbytes,
+               unsigned int *bytes_read);
+int read_string_from_socket(int fd, char **buffer, int *string_len);
+bool initialize_common();
+bool is_preloaded(const char *local_appid, const char *remote_appid);
+int check_certificate(const char *local_appid, const char *remote_appid);
+char *get_encoded_name(const char *remote_app_id, const char *port_name, bool is_trusted);
+
+bool initialized_common;
+GDBusConnection *gdbus_conn;
+char *app_id;
+
+/**
+ * @}
+ */
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /*  __TIZEN_APPFW_MESSAGE_PORT_COMMOM_H__ */
diff --git a/src/inc/message_port_local.h b/src/inc/message_port_local.h
new file mode 100755 (executable)
index 0000000..7edc6d9
--- /dev/null
@@ -0,0 +1,64 @@
+/*
+ * Copyright (c) 2017 Samsung Electronics Co., Ltd All Rights Reserved
+ *
+ * Licensed under the Apache License, Version 2.0 (the License);
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an AS IS BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef __TIZEN_APPFW_MESSAGE_PORT_LOCAL_H__
+#define __TIZEN_APPFW_MESSAGE_PORT_LOCAL_H__
+
+#include <bundle.h>
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+int check_remote_port(const char *remote_app_id,
+                       const char *remote_port,
+                       bool is_trusted,
+                       bool *exist);
+int register_message_port(const char *local_port,
+                       bool is_trusted,
+                       message_port_message_cb callback,
+                       void *user_data);
+int remove_registration_event_cb(int watcher_id);
+int send_bidirectional_message(int id,
+                       const char *remote_app_id,
+                       const char *remote_port,
+                       bool trusted_message,
+                       bundle *message);
+int send_message(const char *remote_appid,
+               const char *remote_port,
+               const char *local_port,
+               bool trusted_message,
+               bool local_trusted,
+               bool bi_dir,
+               bundle *message);
+int unregister_local_port(int local_port_id, bool trusted_port);
+int watch_remote_port(int *watcher_id,
+       const char *remote_app_id,
+       const char *remote_port,
+       bool trusted_remote_port,
+       message_port_registration_event_cb registered_cb,
+       message_port_registration_event_cb unregistered_cb,
+       void *user_data);
+
+/**
+ * @}
+ */
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /*  __TIZEN_APPFW_MESSAGE_PORT_LOCAL_H__ */
diff --git a/src/inc/message_port_remote.h b/src/inc/message_port_remote.h
new file mode 100755 (executable)
index 0000000..aa67bcd
--- /dev/null
@@ -0,0 +1,48 @@
+/*
+ * Copyright (c) 2017 Samsung Electronics Co., Ltd All Rights Reserved
+ *
+ * Licensed under the Apache License, Version 2.0 (the License);
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an AS IS BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef __TIZEN_APPFW_MESSAGE_PORT_REMOTE_H__
+#define __TIZEN_APPFW_MESSAGE_PORT_REMOTE_H__
+
+#include <bundle.h>
+#include <message_port.h>
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+typedef struct message_port_local_port_info {
+       message_port_message_cb callback;
+       void *user_data;
+       bool is_trusted;
+       char *port_name;
+       int local_id;
+} message_port_local_port_info_s;
+
+int get_local_port_info(int id, message_port_local_port_info_s **info);
+int register_message_port(const char *local_port, bool is_trusted, message_port_message_cb callback, void *user_data);
+int unregister_local_port(int local_port_id, bool trusted_port);
+bool is_local_port_registed(const char *local_port, bool trusted, int *local_id, message_port_local_port_info_s **lpi);
+
+/**
+ * @}
+ */
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /*  __TIZEN_APPFW_MESSAGE_PORT_REMOTE_H__ */
diff --git a/src/message-port.c b/src/message-port.c
deleted file mode 100755 (executable)
index 3e6525b..0000000
+++ /dev/null
@@ -1,2255 +0,0 @@
-/*
- * Message Port
- * Copyright (c) 2015 Samsung Electronics Co., Ltd.
- *
- * Licensed under the Apache License, Version 2.0 (the License);
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- *     http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-/**
- * @file       message-port.cpp
- * @brief      This is the implementation file for the MessagePort.
- */
-#define _GNU_SOURCE
-
-#include <sys/socket.h>
-#include <stdlib.h>
-#include <stdio.h>
-#include <glib.h>
-#include <gio/gio.h>
-#include <aul/aul.h>
-#include <openssl/md5.h>
-#include <bundle.h>
-#include <bundle_internal.h>
-#include <pkgmgr-info.h>
-#include <aul.h>
-#include <gio/gio.h>
-#include <gio/gunixfdlist.h>
-#include <pthread.h>
-#include <glib-unix.h>
-#include <poll.h>
-
-#include "message-port.h"
-#include "message-port-log.h"
-
-#define MAX_PACKAGE_STR_SIZE 512
-#define MESSAGEPORT_BUS_NAME_PREFIX "org.tizen.messageport._"
-#define MESSAGEPORT_OBJECT_PATH "/org/tizen/messageport"
-#define MESSAGEPORT_INTERFACE_PREFIX "org.tizen.messageport._"
-
-#define DBUS_SERVICE_DBUS "org.freedesktop.DBus"
-#define DBUS_PATH_DBUS "/org/freedesktop/DBus"
-#define DBUS_INTERFACE_DBUS "org.freedesktop.DBus"
-
-#define DBUS_RELEASE_NAME_REPLY_RELEASED        1 /* *< Service was released from the given name */
-#define DBUS_RELEASE_NAME_REPLY_NON_EXISTENT    2 /* *< The given name does not exist on the bus */
-#define DBUS_RELEASE_NAME_REPLY_NOT_OWNER       3 /* *< Service is not an owner of the given name */
-#define HEADER_LEN 8
-#define MAX_RETRY_CNT 10
-#define SOCK_PAIR_SENDER 0
-#define SOCK_PAIR_RECEIVER 1
-
-
-#define retvm_if(expr, val, fmt, arg...) do { \
-       if (expr) { \
-               _LOGE(fmt, ##arg); \
-               _LOGE("(%s) -> %s() return", #expr, __func__); \
-               return val; \
-       } \
-} while (0)
-
-#define retv_if(expr, val) do { \
-       if (expr) { \
-               _LOGE("(%s) -> %s() return", #expr, __func__); \
-               return val; \
-       } \
-} while (0)
-
-#define FREE_AND_NULL(ptr) do { \
-       if (ptr) { \
-               free((void *)ptr); \
-               ptr = NULL; \
-       } \
-} while (0)
-
-static bool _initialized = false;
-static GDBusConnection *__gdbus_conn;
-static char *__app_id;
-static GHashTable *__local_port_info;
-static GHashTable *__remote_app_info;
-static GHashTable *__sender_appid_hash;
-static GHashTable *__trusted_app_list_hash;
-static GHashTable *__callback_info_hash;
-static GHashTable *__registered_callback_info_hash;
-static const int MAX_MESSAGE_SIZE = 16 * 1024;
-
-enum __certificate_info_type {
-       UNKNOWN = 0,
-       CERTIFICATE_MATCH,
-       CERTIFICATE_NOT_MATCH,
-};
-
-typedef struct message_port_pkt {
-       int remote_port_name_len;
-       char *remote_port_name;
-       bool is_bidirection;
-       bool is_trusted;
-       int data_len;
-       unsigned char *data;
-} message_port_pkt_s;
-
-typedef struct message_port_callback_info {
-       messageport_message_cb callback;
-       int local_id;
-       char *remote_app_id;
-       GIOChannel *gio_read;
-       int g_src_id;
-} message_port_callback_info_s;
-
-typedef struct message_port_local_port_info {
-       messageport_message_cb callback;
-       bool is_trusted;
-       char *port_name;
-       int local_id;
-} message_port_local_port_info_s;
-
-typedef struct message_port_remote_port_info {
-       char *remote_app_id;
-       int certificate_info;
-       GList *port_list;
-} message_port_remote_app_info_s;
-
-typedef struct port_list_info {
-       message_port_remote_app_info_s *remote_app_info;
-       char *port_name;
-       char *encoded_bus_name;
-       bool is_trusted;
-       int send_sock_fd;
-       bool exist;
-       GIOChannel *gio_read;
-       int g_src_id;
-       GList *delayed_message_list;
-       unsigned int delayed_message_size;
-       int delay_src_id;
-} port_list_info_s;
-
-typedef struct registered_callback_info {
-       char *remote_app_id;
-       char *remote_port;
-       bool is_trusted;
-       int watcher_id;
-       void *user_data;
-       messageport_registration_event_cb registered_cb;
-       messageport_registration_event_cb unregistered_cb;
-} registered_callback_info_s;
-
-enum transmission_sequence {
-       SEQUENCE_START = 0,
-       SEQUENCE_PORT_LEN,
-       SEQUENCE_PORT_NAME,
-       SEQUENCE_BIDIRECTION,
-       SEQUENCE_TRUSTED,
-       SEQUENCE_DTAT_LEN,
-       SEQUENCE_DATA,
-       SEQUENCE_END
-};
-
-typedef struct delay_message {
-       unsigned int size;
-       unsigned int sent_bytes;
-       int sequence;
-       int local_port_len;
-       char *local_port_name;
-       bool is_bidirection;
-       bool local_trusted;
-       int data_len;
-       bundle_raw *data;
-} delay_message_info_s;
-
-
-extern pthread_mutex_t mutex;
-static void __free_list_delay_message_info(gpointer data);
-
-
-static void __callback_info_free(gpointer data)
-{
-       message_port_callback_info_s *callback_info = (message_port_callback_info_s *)data;
-       GError *error = NULL;
-       if (callback_info == NULL)
-               return;
-
-       if (callback_info->remote_app_id)
-               FREE_AND_NULL(callback_info->remote_app_id);
-
-       if (callback_info->gio_read != NULL) {
-               g_io_channel_shutdown(callback_info->gio_read, TRUE, &error);
-               if (error) {
-                       _LOGE("g_io_channel_shutdown error : %s", error->message);
-                       g_error_free(error);
-               }
-               g_io_channel_unref(callback_info->gio_read);
-               callback_info->gio_read = NULL;
-       }
-
-       if (callback_info->g_src_id != 0) {
-               g_source_remove(callback_info->g_src_id);
-               callback_info->g_src_id = 0;
-       }
-
-       FREE_AND_NULL(callback_info);
-}
-
-static void __callback_info_free_by_info(message_port_callback_info_s *callback_info)
-{
-       GList *callback_info_list = g_hash_table_lookup(__callback_info_hash, GUINT_TO_POINTER(callback_info->local_id));
-       GList *find_list;
-
-       if (callback_info_list == NULL)
-               return;
-
-       find_list = g_list_find(callback_info_list, callback_info);
-       if (find_list == NULL)
-               return;
-
-       callback_info_list = g_list_remove_link(callback_info_list, find_list);
-       __callback_info_free(callback_info);
-       g_list_free(find_list);
-}
-
-static void __registered_callback_info_free(gpointer data)
-{
-       registered_callback_info_s *callback_info = (registered_callback_info_s *)data;
-       if (callback_info == NULL)
-               return;
-
-       if (callback_info->remote_app_id)
-               free(callback_info->remote_app_id);
-
-       if (callback_info->remote_port)
-               free(callback_info->remote_port);
-
-       free(callback_info);
-}
-
-
-static void __hash_destroy_callback_info(gpointer data)
-{
-
-       GList *callback_list = (GList *)data;
-       if (callback_list != NULL)
-               g_list_free_full(callback_list, __callback_info_free);
-}
-
-static char *__get_encoded_name(const char *remote_app_id, const char *port_name, bool is_trusted)
-{
-
-       int prefix_len = strlen(MESSAGEPORT_BUS_NAME_PREFIX);
-       int postfix_len = 1;
-       char *postfix = is_trusted ? "1" : "0";
-
-       unsigned char c[MD5_DIGEST_LENGTH] = {0};
-       char *md5_interface = NULL;
-       char *temp;
-       int index = 0;
-       MD5_CTX mdContext;
-       int encoded_bus_name_len = prefix_len + postfix_len + (MD5_DIGEST_LENGTH * 2) + 2;
-       int bus_name_len = strlen(remote_app_id) + strlen(port_name) + 2;
-       char *bus_name = (char *)calloc(bus_name_len, sizeof(char));
-       if (bus_name == NULL) {
-               _LOGE("bus_name calloc failed");
-               return 0;
-       }
-
-       snprintf(bus_name, bus_name_len, "%s_%s", remote_app_id, port_name);
-
-       MD5_Init(&mdContext);
-       MD5_Update(&mdContext, bus_name, bus_name_len);
-       MD5_Final(c, &mdContext);
-
-       md5_interface = (char *)calloc(encoded_bus_name_len , sizeof(char));
-       if (md5_interface == NULL) {
-               if (bus_name)
-                       free(bus_name);
-
-               _LOGE("md5_interface calloc failed!!");
-               return 0;
-       }
-
-       snprintf(md5_interface, encoded_bus_name_len, "%s", MESSAGEPORT_BUS_NAME_PREFIX);
-       temp = md5_interface;
-       temp += prefix_len;
-
-       for (index = 0; index < MD5_DIGEST_LENGTH; index++) {
-               snprintf(temp, 3, "%02x", c[index]);
-               temp += 2;
-       }
-
-       if (postfix && postfix_len > 0)
-               snprintf(temp, encoded_bus_name_len - (temp - md5_interface), "%s", postfix);
-       if (bus_name)
-               free(bus_name);
-
-       _LOGD("encoded_bus_name : %s ", md5_interface);
-
-       return md5_interface;
-}
-
-static int __remote_port_compare_cb(gconstpointer a, gconstpointer b)
-{
-       port_list_info_s *key1 = (port_list_info_s *)a;
-       port_list_info_s *key2 = (port_list_info_s *)b;
-
-       if (key1->is_trusted == key2->is_trusted)
-               return strcmp(key1->port_name, key2->port_name);
-
-       return 1;
-}
-
-
-static bool __is_preloaded(const char *local_appid, const char *remote_appid)
-{
-       _LOGD("IsPreloaded");
-
-       bool preload_local = false;
-       bool preload_remote = false;
-
-       pkgmgrinfo_appinfo_h handle = NULL;
-       int ret = pkgmgrinfo_appinfo_get_usr_appinfo(local_appid, getuid(), &handle);
-       if (ret != PMINFO_R_OK) {
-               _LOGE("Failed to get the appinfo. %d", ret);
-               pkgmgrinfo_appinfo_destroy_appinfo(handle);
-               return false;
-       }
-       ret = pkgmgrinfo_appinfo_is_preload(handle, &preload_local);
-       if (ret != PMINFO_R_OK) {
-               _LOGE("Failed to check the preloaded application. %d", ret);
-               pkgmgrinfo_appinfo_destroy_appinfo(handle);
-               return false;
-       }
-       pkgmgrinfo_appinfo_destroy_appinfo(handle);
-
-       ret = pkgmgrinfo_appinfo_get_usr_appinfo(remote_appid, getuid(), &handle);
-       if (ret != PMINFO_R_OK) {
-               _LOGE("Failed to get the appinfo. %d", ret);
-               pkgmgrinfo_appinfo_destroy_appinfo(handle);
-               return false;
-       }
-       ret = pkgmgrinfo_appinfo_is_preload(handle, &preload_remote);
-       if (ret != PMINFO_R_OK) {
-               _LOGE("Failed to check the preloaded application. %d", ret);
-               pkgmgrinfo_appinfo_destroy_appinfo(handle);
-               return false;
-       }
-
-       if (preload_local && preload_remote) {
-               pkgmgrinfo_appinfo_destroy_appinfo(handle);
-               return true;
-       }
-       pkgmgrinfo_appinfo_destroy_appinfo(handle);
-       return false;
-}
-
-static int __check_certificate(const char *local_appid, const char *remote_appid)
-{
-       _LOGD("CheckCertificate");
-
-       pkgmgrinfo_cert_compare_result_type_e res;
-       int ret = pkgmgrinfo_pkginfo_compare_usr_app_cert_info(local_appid, remote_appid, getuid(), &res);
-       if (ret < 0) {
-               _LOGE(":CheckCertificate() Failed");
-               return MESSAGEPORT_ERROR_IO_ERROR;
-       }
-       if (res != PMINFO_CERT_COMPARE_MATCH) {
-               _LOGE("CheckCertificate() Failed : MESSAGEPORT_ERROR_CERTIFICATE_NOT_MATCH");
-               return MESSAGEPORT_ERROR_CERTIFICATE_NOT_MATCH;
-       }
-
-       return MESSAGEPORT_ERROR_NONE;
-}
-
-static int __get_local_port_info(int id, message_port_local_port_info_s **info)
-{
-       message_port_local_port_info_s *mi = (message_port_local_port_info_s *)g_hash_table_lookup(__local_port_info, GINT_TO_POINTER(id));
-
-       if (mi == NULL)
-               return MESSAGEPORT_ERROR_INVALID_PARAMETER;
-       *info = mi;
-
-       return MESSAGEPORT_ERROR_NONE;
-}
-
-static port_list_info_s *__set_remote_port_info(const char *remote_app_id, const char *remote_port, bool is_trusted)
-{
-       int ret_val = MESSAGEPORT_ERROR_NONE;
-       port_list_info_s *port_info = (port_list_info_s *)calloc(1, sizeof(port_list_info_s));
-
-       if (!port_info) {
-               ret_val = MESSAGEPORT_ERROR_OUT_OF_MEMORY;
-               goto out;
-       }
-       port_info->port_name = strdup(remote_port);
-       if (!port_info->port_name) {
-               ret_val = MESSAGEPORT_ERROR_OUT_OF_MEMORY;
-               goto out;
-       }
-       port_info->is_trusted = is_trusted;
-       port_info->encoded_bus_name = __get_encoded_name(remote_app_id, remote_port, is_trusted);
-       if (port_info->encoded_bus_name == NULL) {
-               ret_val = MESSAGEPORT_ERROR_OUT_OF_MEMORY;
-               goto out;
-       }
-       port_info->send_sock_fd = 0;
-out:
-       if (ret_val != MESSAGEPORT_ERROR_NONE) {
-               if (port_info) {
-                       FREE_AND_NULL(port_info->port_name);
-                       FREE_AND_NULL(port_info->encoded_bus_name);
-                       free(port_info);
-               }
-               return NULL;
-       }
-       return port_info;
-}
-
-static message_port_remote_app_info_s *__set_remote_app_info(const char *remote_app_id, const char *remote_port, bool is_trusted)
-{
-       message_port_remote_app_info_s *remote_app_info = NULL;
-       int ret_val = MESSAGEPORT_ERROR_NONE;
-
-       remote_app_info = (message_port_remote_app_info_s *)calloc(1, sizeof(message_port_remote_app_info_s));
-       if (!remote_app_info) {
-               ret_val = MESSAGEPORT_ERROR_OUT_OF_MEMORY;
-               goto out;
-       }
-
-       remote_app_info->remote_app_id = strdup(remote_app_id);
-       if (remote_app_info->remote_app_id == NULL) {
-               ret_val = MESSAGEPORT_ERROR_OUT_OF_MEMORY;;
-               goto out;
-       }
-
-out:
-       if (ret_val != MESSAGEPORT_ERROR_NONE) {
-               if (remote_app_info) {
-                       FREE_AND_NULL(remote_app_info->remote_app_id);
-                       FREE_AND_NULL(remote_app_info);
-               }
-               return NULL;
-       }
-       return remote_app_info;
-}
-
-static void __clear_disconnect_socket(port_list_info_s *port_info)
-{
-       GError *error = NULL;
-
-       if (port_info == NULL)
-               return;
-
-       if (port_info->gio_read != NULL) {
-               g_io_channel_shutdown(port_info->gio_read, TRUE, &error);
-               if (error) {
-                       _LOGE("g_io_channel_shutdown error : %s", error->message);
-                       g_error_free(error);
-               }
-               g_io_channel_unref(port_info->gio_read);
-               port_info->gio_read = NULL;
-       }
-
-       if (port_info->g_src_id != 0) {
-               g_source_remove(port_info->g_src_id);
-               port_info->g_src_id = 0;
-       }
-
-       if (port_info->delay_src_id != 0) {
-               g_source_remove(port_info->delay_src_id);
-               port_info->delay_src_id = 0;
-       }
-
-       if (port_info->delayed_message_list != NULL) {
-               g_list_free_full(port_info->delayed_message_list, __free_list_delay_message_info);
-               /* can be reused */
-               port_info->delayed_message_list = NULL;
-       }
-
-       port_info->delayed_message_size = 0;
-       port_info->send_sock_fd = 0;
-}
-
-/* LCOV_EXCL_START */
-void __free_port_info(gpointer data)
-{
-       port_list_info_s *port_info = (port_list_info_s *)data;
-       message_port_remote_app_info_s *remote_app_info;
-
-       if (port_info == NULL)
-               return;
-
-       remote_app_info = port_info->remote_app_info;
-
-       _LOGI("__free_port_info : remote_app_id : %s port_name : %s",
-                       remote_app_info->remote_app_id,
-                       port_info->port_name);
-
-       remote_app_info->port_list = g_list_remove(remote_app_info->port_list,
-                       port_info);
-
-       __clear_disconnect_socket(port_info);
-
-       if (port_info->encoded_bus_name)
-               free(port_info->encoded_bus_name);
-       if (port_info->port_name)
-               free(port_info->port_name);
-
-       free(port_info);
-
-       if (g_list_length(remote_app_info->port_list) == 0) {
-               g_hash_table_remove(__remote_app_info,
-                               remote_app_info->remote_app_id);
-       }
-}
-/* LCOV_EXCL_STOP */
-
-static gboolean __socket_disconnect_handler(GIOChannel *gio,
-               GIOCondition cond,
-               gpointer data)
-{
-       _LOGI("__socket_disconnect_handler %d", cond);
-       __free_port_info(data);
-
-       return FALSE;
-}
-
-static int __get_remote_port_info(const char *remote_app_id, const char *remote_port, bool is_trusted,
-               message_port_remote_app_info_s **mri, port_list_info_s **pli)
-{
-       message_port_remote_app_info_s *remote_app_info = NULL;
-       port_list_info_s port_info;
-       GList *cb_list = NULL;
-       int ret_val = MESSAGEPORT_ERROR_NONE;
-
-       remote_app_info = (message_port_remote_app_info_s *)g_hash_table_lookup(__remote_app_info, remote_app_id);
-
-       if (remote_app_info == NULL) {
-               remote_app_info = __set_remote_app_info(remote_app_id, remote_port, is_trusted);
-
-               if (remote_app_info == NULL) {
-                       ret_val = MESSAGEPORT_ERROR_OUT_OF_MEMORY;
-                       goto out;
-               }
-               g_hash_table_insert(__remote_app_info, remote_app_info->remote_app_id, remote_app_info);
-       }
-       *mri = remote_app_info;
-
-       port_info.port_name = strdup(remote_port);
-       if (port_info.port_name == NULL) {
-               ret_val = MESSAGEPORT_ERROR_OUT_OF_MEMORY;
-               goto out;
-       }
-       port_info.is_trusted = is_trusted;
-       cb_list = g_list_find_custom(remote_app_info->port_list, &port_info,
-                                       (GCompareFunc)__remote_port_compare_cb);
-       if (port_info.port_name)
-               free(port_info.port_name);
-       if (cb_list == NULL) {
-               port_list_info_s *tmp = __set_remote_port_info(remote_app_id, remote_port, is_trusted);
-               if (tmp == NULL) {
-                       ret_val = MESSAGEPORT_ERROR_OUT_OF_MEMORY;
-                       goto out;
-               }
-               remote_app_info->port_list = g_list_append(remote_app_info->port_list, tmp);
-               tmp->remote_app_info = remote_app_info;
-               *pli = tmp;
-       } else {
-               *pli = (port_list_info_s *)cb_list->data;
-       }
-out:
-
-       return ret_val;
-}
-
-static bool __is_local_port_registed(const char *local_port, bool trusted, int *local_id, message_port_local_port_info_s **lpi)
-{
-       GHashTableIter iter;
-       gpointer key, value;
-
-       g_hash_table_iter_init(&iter, __local_port_info);
-       while (g_hash_table_iter_next(&iter, &key, &value)) {
-               message_port_local_port_info_s *mi = (message_port_local_port_info_s *)value;
-
-               if ((mi->is_trusted == trusted) && strcmp(mi->port_name, local_port) == 0) {
-                       *local_id = mi->local_id;
-                       if (lpi != NULL)
-                               *lpi = mi;
-                       return true;
-               }
-       }
-       return false;
-}
-
-static int __get_sender_pid(GDBusConnection *conn, const char *sender_name)
-{
-       GDBusMessage *msg = NULL;
-       GDBusMessage *reply = NULL;
-       GError *err = NULL;
-       GVariant *body;
-       int pid = 0;
-
-       msg = g_dbus_message_new_method_call("org.freedesktop.DBus", "/org/freedesktop/DBus",
-                       "org.freedesktop.DBus", "GetConnectionUnixProcessID");
-       if (!msg) {
-               _LOGE("Can't allocate new method call");
-               goto out;
-       }
-
-       g_dbus_message_set_body(msg, g_variant_new("(s)", sender_name));
-       reply = g_dbus_connection_send_message_with_reply_sync(conn, msg,
-                                                       G_DBUS_SEND_MESSAGE_FLAGS_NONE, -1, NULL, NULL, &err);
-
-       if (!reply) {
-               if (err != NULL) {
-                       _LOGE("Failed to get pid [%s]", err->message);
-                       g_error_free(err);
-               }
-               goto out;
-       }
-
-       body = g_dbus_message_get_body(reply);
-       g_variant_get(body, "(u)", &pid);
-
-out:
-       if (msg)
-               g_object_unref(msg);
-       if (reply)
-               g_object_unref(reply);
-
-       return pid;
-}
-
-static int __write_socket(int fd,
-               const char *buffer,
-               unsigned int nbytes,
-               unsigned int *bytes_write,
-               int *sequence)
-{
-#define SEND_TIMEOUT 500 /* milliseconds */
-
-       unsigned int left = nbytes;
-       ssize_t nb;
-       int retry_cnt = 0;
-       struct pollfd fds[1];
-       int ret;
-
-       *sequence += 1;
-       *bytes_write = 0;
-
-       fds[0].fd = fd;
-       fds[0].events = POLLOUT;
-       fds[0].revents = 0;
-
-       ret = poll(fds, 1, SEND_TIMEOUT);
-       if (ret == 0) {
-               LOGE("__write_socket: : fd %d poll timeout", fd);
-               return MESSAGEPORT_ERROR_RESOURCE_UNAVAILABLE;
-       }
-
-       while (left && (retry_cnt < MAX_RETRY_CNT)) {
-               nb = write(fd, buffer, left);
-               if (nb == -1) {
-                       if (errno == EINTR) {
-                               LOGE("__write_socket: EINTR error continue ...");
-                               retry_cnt++;
-                               continue;
-                       }
-                       LOGE("__write_socket: ...error fd %d: errno %d\n", fd, errno);
-
-                       if (errno == EWOULDBLOCK || errno == EAGAIN)
-                               return MESSAGEPORT_ERROR_RESOURCE_UNAVAILABLE;
-
-                       return MESSAGEPORT_ERROR_IO_ERROR;
-               }
-
-               left -= nb;
-               buffer += nb;
-               *bytes_write += nb;
-               retry_cnt = 0;
-       }
-       return MESSAGEPORT_ERROR_NONE;
-}
-
-static int __write_string_to_socket(int fd,
-               const char *buffer,
-               int string_len,
-               unsigned int *bytes_write,
-               int *sequence)
-{
-       int ret;
-
-       ret = __write_socket(fd, (char *)&string_len, sizeof(string_len),
-                       bytes_write, sequence);
-       if (ret != MESSAGEPORT_ERROR_NONE) {
-               _LOGE("write string_len fail");
-               return ret;
-       }
-
-       if (string_len > 0) {
-               ret = __write_socket(fd, buffer, string_len, bytes_write, sequence);
-               if (ret != MESSAGEPORT_ERROR_NONE) {
-                       _LOGE("wirte buffer fail");
-                       return ret;
-               }
-       } else {
-               *sequence += 1;
-       }
-
-       return MESSAGEPORT_ERROR_NONE;
-}
-
-static int __read_socket(int fd,
-               char *buffer,
-               unsigned int nbytes,
-               unsigned int *bytes_read)
-{
-       unsigned int left = nbytes;
-       ssize_t nb;
-       int retry_cnt = 0;
-       const struct timespec TRY_SLEEP_TIME = { 0, 500 * 1000 * 1000 };
-
-       *bytes_read = 0;
-       while (left && (retry_cnt < MAX_RETRY_CNT)) {
-               nb = read(fd, buffer, left);
-               if (nb == 0) {
-                       LOGE("__read_socket: ...read EOF, socket closed %d: nb %d\n", fd, nb);
-                       return MESSAGEPORT_ERROR_IO_ERROR;
-               } else if (nb == -1) {
-                       /*  wrt(nodejs) could change socket to none-blocking socket :-( */
-                       if (errno == EINTR || errno == EAGAIN || errno == EWOULDBLOCK) {
-                               LOGE("__read_socket: %d errno, sleep and retry ...", errno);
-                               retry_cnt++;
-                               nanosleep(&TRY_SLEEP_TIME, 0);
-                               continue;
-                       }
-                       LOGE("__read_socket: ...error fd %d: errno %d\n", fd, errno);
-                       return MESSAGEPORT_ERROR_IO_ERROR;
-               }
-
-               left -= nb;
-               buffer += nb;
-               *bytes_read += nb;
-               retry_cnt = 0;
-       }
-       return MESSAGEPORT_ERROR_NONE;
-}
-
-static int __read_string_from_socket(int fd, char **buffer, int *string_len)
-{
-       unsigned int nb;
-       if (__read_socket(fd, (char *)string_len, sizeof(*string_len), &nb) != MESSAGEPORT_ERROR_NONE) {
-               LOGE("read socket fail");
-               return MESSAGEPORT_ERROR_IO_ERROR;
-       }
-       if (*string_len > 0 && *string_len < MAX_MESSAGE_SIZE) {
-               *buffer = (char *)calloc(*string_len, sizeof(char));
-               if (*buffer == NULL) {
-                       LOGE("Out of memory.");
-                       return MESSAGEPORT_ERROR_IO_ERROR;
-               }
-               if (__read_socket(fd, *buffer, *string_len, &nb) != MESSAGEPORT_ERROR_NONE) {
-                       LOGE("read socket fail");
-                       return MESSAGEPORT_ERROR_IO_ERROR;
-               }
-       } else {
-               LOGE("Invalid string len %d", *string_len);
-               return MESSAGEPORT_ERROR_IO_ERROR;
-       }
-       return MESSAGEPORT_ERROR_NONE;
-}
-
-message_port_pkt_s *__message_port_recv_raw(int fd)
-{
-       message_port_pkt_s *pkt = NULL;
-       unsigned int nb;
-
-       pkt = (message_port_pkt_s *)calloc(sizeof(message_port_pkt_s), 1);
-       if (pkt == NULL) {
-               close(fd);
-               return NULL;
-       }
-
-       if (__read_string_from_socket(fd, (char **)&pkt->remote_port_name, &pkt->remote_port_name_len) != MESSAGEPORT_ERROR_NONE) {
-               LOGE("read socket fail: port_name");
-               free(pkt->remote_port_name);
-               free(pkt);
-               return NULL;
-       }
-
-       if (__read_socket(fd, (char *)&pkt->is_bidirection, sizeof(pkt->is_bidirection), &nb) != MESSAGEPORT_ERROR_NONE) {
-               LOGE("read socket fail: is_bidirection");
-               free(pkt->remote_port_name);
-               free(pkt);
-               return NULL;
-       }
-
-       if (__read_socket(fd, (char *)&pkt->is_trusted, sizeof(pkt->is_trusted), &nb) != MESSAGEPORT_ERROR_NONE) {
-               LOGE("read socket fail: is_trusted");
-               free(pkt->remote_port_name);
-               free(pkt);
-               return NULL;
-       }
-
-       if (__read_string_from_socket(fd, (char **)&pkt->data, &pkt->data_len) != MESSAGEPORT_ERROR_NONE) {
-               LOGE("read socket fail: data");
-               if (pkt->data)
-                       free(pkt->data);
-               free(pkt->remote_port_name);
-               free(pkt);
-               return NULL;
-       }
-
-       return pkt;
-}
-
-static gboolean __socket_request_handler(GIOChannel *gio,
-               GIOCondition cond,
-               gpointer data)
-{
-       int fd = 0;
-       message_port_callback_info_s *mi;
-       message_port_pkt_s *pkt;
-       bundle *kb = NULL;
-       GError *error = NULL;
-
-       mi = (message_port_callback_info_s *)data;
-       if (mi == NULL) {
-
-               g_io_channel_shutdown(gio, TRUE, &error);
-               if (error) {
-                       _LOGE("g_io_channel_shutdown error : %s", error->message);
-                       g_error_free(error);
-               }
-               g_io_channel_unref(gio);
-               return FALSE;
-       }
-
-       if (cond == G_IO_HUP) {
-
-               _LOGI("socket G_IO_HUP");
-               __callback_info_free_by_info(mi);
-               return FALSE;
-
-       } else {
-
-               if ((fd = g_io_channel_unix_get_fd(gio)) < 0) {
-                       _LOGE("fail to get fd from io channel");
-                       __callback_info_free_by_info(mi);
-                       return FALSE;
-               }
-
-               if ((pkt = __message_port_recv_raw(fd)) == NULL) {
-                       _LOGE("recv error on SOCKET");
-                       __callback_info_free_by_info(mi);
-                       return FALSE;
-               }
-
-               kb = bundle_decode(pkt->data, pkt->data_len);
-               if (pkt->is_bidirection)
-                       mi->callback(mi->local_id, mi->remote_app_id, pkt->remote_port_name, pkt->is_trusted, kb, NULL);
-               else
-                       mi->callback(mi->local_id, mi->remote_app_id, NULL, pkt->is_trusted, kb, NULL);
-
-               bundle_free(kb);
-               if (pkt) {
-                       if (pkt->remote_port_name)
-                               free(pkt->remote_port_name);
-                       if (pkt->data)
-                               free(pkt->data);
-                       free(pkt);
-               }
-       }
-
-       return TRUE;
-}
-
-static bool send_message(GVariant *parameters, GDBusMethodInvocation *invocation)
-{
-       char *local_port = NULL;
-       char *local_appid = NULL;
-       char *remote_appid = NULL;
-       char *remote_port = NULL;
-       gboolean local_trusted = false;
-       gboolean remote_trusted = false;
-       gboolean bi_dir = false;
-       int len = 0;
-
-       bundle *data = NULL;
-       bundle_raw *raw = NULL;
-       message_port_local_port_info_s *mi;
-       int local_reg_id = 0;
-       message_port_callback_info_s *callback_info = NULL;
-       message_port_callback_info_s *head_callback_info;
-       GList *callback_info_list = NULL;
-
-       char buf[1024];
-       GDBusMessage *msg;
-       GUnixFDList *fd_list;
-       int fd_len;
-       int *returned_fds = NULL;
-       int fd;
-       bool ret = false;
-
-       g_variant_get(parameters, "(&s&sbb&s&sbu&s)", &local_appid, &local_port, &local_trusted, &bi_dir,
-                       &remote_appid, &remote_port, &remote_trusted, &len, &raw);
-
-       if (!remote_port) {
-               _LOGE("Invalid argument : remote_port is NULL");
-               goto out;
-       }
-       if (!remote_appid) {
-               _LOGE("Invalid argument : remote_appid is NULL");
-               goto out;
-       }
-       if (!__is_local_port_registed(remote_port, remote_trusted, &local_reg_id, &mi)) {
-               _LOGE("Invalid argument : remote_port:(%s) trusted(%d)", remote_port, remote_trusted);
-               goto out;
-       }
-       if (!local_appid) {
-               _LOGE("Invalid argument : local_appid");
-               goto out;
-       }
-       if (!local_port) {
-               _LOGE("Invalid argument : local_port");
-               goto out;
-       }
-       if (strcmp(remote_appid, __app_id) != 0) {
-               _LOGE("Invalid argument : remote_appid (%s)", remote_appid);
-               goto out;
-       }
-       if (strcmp(remote_port, mi->port_name) != 0) {
-               _LOGE("Invalid argument : remote_port (%s)", remote_port);
-               goto out;
-       }
-       if (!len) {
-               _LOGE("Invalid argument : data_len");
-               goto out;
-       }
-       if (remote_trusted) {
-               if (g_hash_table_lookup(__trusted_app_list_hash, (gpointer)local_appid) == NULL) {
-                       if (!__is_preloaded(local_appid, remote_appid)) {
-                               int ret = __check_certificate(local_appid, remote_appid);
-                               if (ret == MESSAGEPORT_ERROR_NONE)
-                                       g_hash_table_insert(__trusted_app_list_hash, local_appid, "TRUE");
-                               else {
-                                       _LOGE("The application (%s) is not signed with the same certificate",
-                                                       local_appid);
-                                       goto out;
-                               }
-                       }
-               }
-       }
-
-       callback_info = (message_port_callback_info_s *)calloc(1, sizeof(message_port_callback_info_s));
-       if (callback_info == NULL)
-               goto out;
-
-       callback_info->local_id = mi->local_id;
-       callback_info->remote_app_id = strdup(local_appid);
-       if (callback_info->remote_app_id == NULL) {
-               _LOGE("out of memory");
-               goto out;
-       }
-       callback_info->callback = mi->callback;
-
-       msg = g_dbus_method_invocation_get_message(invocation);
-       fd_list = g_dbus_message_get_unix_fd_list(msg);
-
-       /* When application send message to self fd_list is NULL */
-       if (fd_list != NULL) {
-               returned_fds = g_unix_fd_list_steal_fds(fd_list, &fd_len);
-               if (returned_fds == NULL) {
-                       _LOGE("fail to get fds");
-                       goto out;
-               }
-               fd = returned_fds[0];
-
-               LOGI("g_unix_fd_list_get %d fd: [%d]", fd_len, fd);
-               if (fd > 0) {
-
-                       callback_info->gio_read = g_io_channel_unix_new(fd);
-                       if (!callback_info->gio_read) {
-                               _LOGE("Error is %s\n", strerror_r(errno, buf, sizeof(buf)));
-                               goto out;
-                       }
-
-                       callback_info->g_src_id = g_io_add_watch(callback_info->gio_read, G_IO_IN | G_IO_HUP,
-                                       __socket_request_handler, (gpointer)callback_info);
-                       if (callback_info->g_src_id == 0) {
-                               _LOGE("fail to add watch on socket");
-                               goto out;
-                       }
-
-                       callback_info_list = g_hash_table_lookup(__callback_info_hash, GUINT_TO_POINTER(mi->local_id));
-                       if (callback_info_list == NULL) {
-                               head_callback_info = (message_port_callback_info_s *)calloc(1, sizeof(message_port_callback_info_s));
-                               if (head_callback_info == NULL) {
-                                       _LOGE("fail to alloc head_callback_info");
-                                       goto out;
-                               }
-                               head_callback_info->local_id = 0;
-                               head_callback_info->remote_app_id = NULL;
-                               head_callback_info->callback = NULL;
-                               head_callback_info->gio_read = NULL;
-                               head_callback_info->g_src_id = 0;
-                               callback_info_list = g_list_append(callback_info_list, head_callback_info);
-                               callback_info_list = g_list_append(callback_info_list, callback_info);
-                               g_hash_table_insert(__callback_info_hash, GUINT_TO_POINTER(mi->local_id), callback_info_list);
-                       } else {
-                               callback_info_list = g_list_append(callback_info_list, callback_info);
-                       }
-               }
-       }
-
-       data = bundle_decode(raw, len);
-       if (!data) {
-               _LOGE("Invalid argument : message");
-               goto out;
-       }
-
-       LOGD("call calback %s", local_appid);
-       if (bi_dir)
-               mi->callback(mi->local_id, local_appid, local_port, local_trusted, data, NULL);
-       else
-               mi->callback(mi->local_id, local_appid, NULL, false, data, NULL);
-       bundle_free(data);
-
-       ret = true;
-out:
-       if (ret == false)
-               __callback_info_free(callback_info);
-
-       if (returned_fds)
-               free(returned_fds);
-
-       return ret;
-}
-
-static int __check_remote_port(const char *remote_app_id, const char *remote_port, bool is_trusted, bool *exist)
-{
-       _LOGD("Check a remote port : [%s:%s]", remote_app_id, remote_port);
-
-       GVariant *result = NULL;
-       GError *err = NULL;
-       int ret_val = MESSAGEPORT_ERROR_NONE;
-       char *bus_name = NULL;
-       message_port_remote_app_info_s *remote_app_info = NULL;
-       port_list_info_s *port_info = NULL;
-       int local_reg_id = 0;
-       message_port_local_port_info_s *mi = NULL;
-       gboolean name_exist = false;
-
-       _LOGD("remote_app_id, app_id :[%s : %s] ", remote_app_id, __app_id);
-
-       ret_val = __get_remote_port_info(remote_app_id, remote_port, is_trusted, &remote_app_info, &port_info);
-       if (ret_val != MESSAGEPORT_ERROR_NONE)
-               return ret_val;
-
-       /* self check */
-       if (strcmp(remote_app_id, __app_id) == 0) {
-
-               _LOGD("__is_local_port_registed ");
-               if (!__is_local_port_registed(remote_port, is_trusted, &local_reg_id, &mi))
-                       *exist = false;
-               else
-                       *exist = true;
-
-               _LOGD("__is_local_port_registed : %d ", *exist);
-               return MESSAGEPORT_ERROR_NONE;
-       }
-
-       port_info->exist = false;
-       bus_name = port_info->encoded_bus_name;
-
-       result = g_dbus_connection_call_sync(
-                       __gdbus_conn,
-                       DBUS_SERVICE_DBUS,
-                       DBUS_PATH_DBUS,
-                       DBUS_INTERFACE_DBUS,
-                       "NameHasOwner",
-                       g_variant_new("(s)", bus_name),
-                       G_VARIANT_TYPE("(b)"),
-                       G_DBUS_CALL_FLAGS_NONE,
-                       -1,
-                       NULL,
-                       &err);
-
-       if (err || (result == NULL)) {
-               if (err) {
-                       _LOGE("No reply. error = %s", err->message);
-                       g_error_free(err);
-               }
-               ret_val = MESSAGEPORT_ERROR_RESOURCE_UNAVAILABLE;
-       } else {
-               g_variant_get(result, "(b)", &name_exist);
-
-               if (!name_exist) {
-                       _LOGI("Name not exist %s", bus_name);
-                       *exist = false;
-                       ret_val = MESSAGEPORT_ERROR_NONE;
-               } else {
-
-                       if (is_trusted) {
-                               if (remote_app_info->certificate_info != CERTIFICATE_MATCH) {
-                                       if (!__is_preloaded(__app_id, remote_app_id)) {
-                                               if (__check_certificate(__app_id, remote_app_id) != MESSAGEPORT_ERROR_NONE) {
-                                                       ret_val = MESSAGEPORT_ERROR_CERTIFICATE_NOT_MATCH;
-                                                       goto out;
-                                               }
-                                       }
-                                       remote_app_info->certificate_info = CERTIFICATE_MATCH;
-                               }
-                       }
-                       port_info->exist = true;
-                       *exist = true;
-                       ret_val = MESSAGEPORT_ERROR_NONE;
-               }
-       }
-out:
-       if (result)
-               g_variant_unref(result);
-
-       if (ret_val != MESSAGEPORT_ERROR_NONE || !name_exist)
-               __free_port_info((gpointer)port_info);
-
-       return ret_val;
-}
-
-static void __on_sender_name_appeared(GDBusConnection *connection,
-               const gchar     *name,
-               const gchar     *name_owner,
-               gpointer         user_data)
-{
-       _LOGI("sender name appeared : %s", name);
-}
-
-static void __on_sender_name_vanished(GDBusConnection *connection,
-               const gchar     *name,
-               gpointer         user_data)
-{
-       gboolean remove_result = FALSE;
-       int *watcher_id = (int *)user_data;
-       remove_result = g_hash_table_remove(__sender_appid_hash, (gpointer)name);
-       if (!remove_result)
-               _LOGE("Fail to remove sender appid from hash : %s", name);
-
-       if (watcher_id) {
-               if (*watcher_id > 0)
-                       g_bus_unwatch_name(*watcher_id);
-               else
-                       LOGE("Invalid watcher_id %d", *watcher_id);
-               free(watcher_id);
-       } else {
-               LOGE("watcher_id is NULL");
-       }
-}
-
-static bool __check_sender_validation(GVariant *parameters, const char *sender, GDBusConnection *conn)
-{
-       int ret = 0;
-       char buffer[MAX_PACKAGE_STR_SIZE] = {0, };
-       char *local_appid = NULL;
-       int pid = __get_sender_pid(conn, sender);
-       int *watcher_id = (int *)calloc(1, sizeof(int));
-       char *_sender;
-       retvm_if(!watcher_id, false, "Malloc failed");
-
-       ret = aul_app_get_appid_bypid(pid, buffer, sizeof(buffer));
-       if (ret != AUL_R_OK) {
-               _LOGE("Failed to get the sender ID: (%s) (%d)", sender, pid);
-               free(watcher_id);
-               return false;
-       }
-
-       g_variant_get_child(parameters, 0, "&s", &local_appid);
-       if (local_appid == NULL) {
-               _LOGE("appid is NULL : (%s) (%d)", sender, pid);
-               free(watcher_id);
-               return false;
-       }
-
-       if (strncmp(buffer, local_appid, MAX_PACKAGE_STR_SIZE) == 0) {
-               _LOGD("insert sender !!!!! %s", sender);
-               _sender = strdup(sender);
-               if (_sender == NULL) {
-                       _LOGE("out of memory");
-                       free(watcher_id);
-                       return false;
-               }
-               g_hash_table_insert(__sender_appid_hash, (gpointer)_sender, GINT_TO_POINTER(pid));
-               *watcher_id = g_bus_watch_name_on_connection(
-                                       __gdbus_conn,
-                                       sender,
-                                       G_BUS_NAME_WATCHER_FLAGS_NONE,
-                                       __on_sender_name_appeared,
-                                       __on_sender_name_vanished,
-                                       watcher_id,
-                                       NULL);
-       } else {
-               free(watcher_id);
-               return false;
-       }
-       return true;
-}
-
-static void __dbus_method_call_handler(GDBusConnection *conn,
-                               const gchar *sender, const gchar *object_path,
-                               const gchar *iface_name, const gchar *method_name,
-                               GVariant *parameters, GDBusMethodInvocation *invocation,
-                               gpointer user_data)
-{
-       _LOGI("method_name: %s, sender: %s", method_name, sender);
-       gpointer sender_pid = g_hash_table_lookup(__sender_appid_hash, sender);
-       if (sender_pid == NULL) {
-               if (!__check_sender_validation(parameters, sender, conn))
-                       goto out;
-       }
-       if (g_strcmp0(method_name, "send_message") == 0)
-               send_message(parameters, invocation);
-out:
-       g_dbus_method_invocation_return_value(invocation, NULL);
-}
-
-static const GDBusInterfaceVTable interface_vtable = {
-       __dbus_method_call_handler,
-       NULL,
-       NULL
-};
-
-static int __dbus_init(void)
-{
-       bool ret = false;
-       GError *error = NULL;
-
-       __gdbus_conn = g_bus_get_sync(G_BUS_TYPE_SESSION, NULL, &error);
-       if (__gdbus_conn == NULL) {
-               if (error != NULL) {
-                       _LOGE("Failed to get dbus [%s]", error->message);
-                       g_error_free(error);
-               }
-               goto out;
-       }
-
-       ret = true;
-
-out:
-       if (!__gdbus_conn)
-               g_object_unref(__gdbus_conn);
-
-       return ret;
-
-}
-
-int __register_dbus_interface(const char *port_name, bool is_trusted)
-{
-
-       GDBusNodeInfo *introspection_data = NULL;
-       int registration_id = 0;
-
-       static gchar introspection_prefix[] =
-               "<node>"
-               "  <interface name='";
-
-       static gchar introspection_postfix[] =
-               "'>"
-               "        <method name='send_message'>"
-               "          <arg type='s' name='local_appid' direction='in'/>"
-               "          <arg type='s' name='local_port' direction='in'/>"
-               "          <arg type='b' name='local_trusted' direction='in'/>"
-               "          <arg type='b' name='bi_dir' direction='in'/>"
-               "          <arg type='s' name='remote_appid' direction='in'/>"
-               "          <arg type='s' name='remote_port' direction='in'/>"
-               "          <arg type='b' name='remote_trusted' direction='in'/>"
-               "          <arg type='u' name='data_len' direction='in'/>"
-               "          <arg type='s' name='data' direction='in'/>"
-               "        </method>"
-               "  </interface>"
-               "</node>";
-
-       char *introspection_xml = NULL;
-       int introspection_xml_len = 0;
-
-
-       int owner_id = 0;
-       GError *error = NULL;
-       char *bus_name = NULL;
-       char *interface_name = NULL;
-       GVariant *result = NULL;
-
-       bus_name = __get_encoded_name(__app_id, port_name, is_trusted);
-       if (!bus_name) {
-               _LOGE("Fail to get bus name");
-               goto out;
-       }
-       interface_name = bus_name;
-
-       introspection_xml_len = strlen(introspection_prefix) + strlen(interface_name) +
-               strlen(introspection_postfix) + 1;
-
-       introspection_xml = (char *)calloc(introspection_xml_len, sizeof(char));
-       if (!introspection_xml) {
-               _LOGE("out of memory");
-               goto out;
-       }
-
-
-       result = g_dbus_connection_call_sync(
-                       __gdbus_conn,
-                       DBUS_SERVICE_DBUS,
-                       DBUS_PATH_DBUS,
-                       DBUS_INTERFACE_DBUS,
-                       "RequestName",
-                       g_variant_new("(su)", bus_name, G_BUS_NAME_OWNER_FLAGS_NONE),
-                       G_VARIANT_TYPE("(u)"),
-                       G_DBUS_CALL_FLAGS_NONE,
-                       -1,
-                       NULL,
-                       &error);
-       if (error) {
-               _LOGE("RequestName fail : %s", error->message);
-               g_error_free(error);
-               goto out;
-       }
-       if (result == NULL) {
-               _LOGE("fail to get name NULL");
-               goto out;
-       }
-       g_variant_get(result, "(u)", &owner_id);
-       if (owner_id == 0) {
-               _LOGE("Acquiring the own name is failed");
-               goto out;
-       }
-
-       _LOGD("Acquiring the own name : %d", owner_id);
-
-       snprintf(introspection_xml, introspection_xml_len, "%s%s%s", introspection_prefix, interface_name, introspection_postfix);
-
-       introspection_data = g_dbus_node_info_new_for_xml(introspection_xml, NULL);
-       if (!introspection_data) {
-               _LOGE("g_dbus_node_info_new_for_xml() is failed.");
-               goto out;
-       }
-
-       registration_id = g_dbus_connection_register_object(__gdbus_conn,
-                                               MESSAGEPORT_OBJECT_PATH, introspection_data->interfaces[0],
-                                               &interface_vtable, NULL, NULL, NULL);
-
-       _LOGD("registration_id %d", registration_id);
-
-       if (registration_id == 0) {
-               _LOGE("Failed to g_dbus_connection_register_object");
-               goto out;
-       }
-
-out:
-       if (introspection_data)
-               g_dbus_node_info_unref(introspection_data);
-       if (introspection_xml)
-               free(introspection_xml);
-       if (bus_name)
-               free(bus_name);
-       if (result)
-               g_variant_unref(result);
-
-
-       return registration_id;
-}
-
-/* LCOV_EXCL_START */
-static void __hash_destory_local_value(gpointer data)
-{
-       message_port_local_port_info_s *mli = (message_port_local_port_info_s *)data;
-       if (mli) {
-               if (mli->port_name)
-                       free(mli->port_name);
-               free(mli);
-       }
-}
-/* LCOV_EXCL_STOP */
-
-/* LCOV_EXCL_START */
-static void __hash_destory_remote_value(gpointer data)
-{
-       message_port_remote_app_info_s *mri = (message_port_remote_app_info_s *)data;
-       if (mri) {
-               FREE_AND_NULL(mri->remote_app_id);
-               if (mri->port_list)
-                       g_list_free_full(mri->port_list, __free_port_info);
-
-               free(mri);
-       }
-}
-/* LCOV_EXCL_STOP */
-
-static bool __initialize(void)
-{
-
-#if !GLIB_CHECK_VERSION(2, 35, 0)
-       g_type_init();
-#endif
-
-       int pid = getpid();
-       int ret = 0;
-       char buffer[MAX_PACKAGE_STR_SIZE] = {0, };
-
-       ret = aul_app_get_appid_bypid(pid, buffer, sizeof(buffer));
-       retvm_if(ret != AUL_R_OK, false, "Failed to get the application ID: %d", ret);
-
-       __app_id = strdup(buffer);
-       retvm_if(!__app_id, false, "Malloc failed");
-       _LOGI("init : %s", __app_id);
-
-       if (__local_port_info == NULL) {
-               __local_port_info = g_hash_table_new_full(g_direct_hash,  g_direct_equal, NULL, __hash_destory_local_value);
-               retvm_if(!__local_port_info, false, "fail to create __local_port_info");
-       }
-
-       if (__remote_app_info == NULL) {
-               __remote_app_info = g_hash_table_new_full(g_str_hash, g_str_equal, NULL, __hash_destory_remote_value);
-               retvm_if(!__remote_app_info, false, "fail to create __remote_app_info");
-       }
-
-       if (__sender_appid_hash == NULL) {
-               __sender_appid_hash = g_hash_table_new_full(g_str_hash, g_str_equal, free, NULL);
-               retvm_if(!__sender_appid_hash, false, "fail to create __sender_appid_hash");
-       }
-
-       if (__trusted_app_list_hash == NULL) {
-               __trusted_app_list_hash = g_hash_table_new(g_str_hash, g_str_equal);
-               retvm_if(!__trusted_app_list_hash, false, "fail to create __trusted_app_list_hash");
-       }
-
-       if (__callback_info_hash == NULL) {
-               __callback_info_hash = g_hash_table_new_full(g_direct_hash, g_direct_equal, NULL, __hash_destroy_callback_info);
-               retvm_if(!__callback_info_hash, false, "fail to create __callback_info_hash");
-       }
-
-       if (!__dbus_init())
-               return false;
-       _initialized = true;
-
-       return true;
-}
-
-
-static bool __message_port_register_port(const int local_id, const char *local_port, bool is_trusted, messageport_message_cb callback)
-{
-       message_port_local_port_info_s *mi = (message_port_local_port_info_s *)calloc(1, sizeof(message_port_local_port_info_s));
-       retvm_if(!mi, false, "Malloc failed");
-
-       mi->callback = callback;
-       mi->is_trusted = is_trusted;
-       mi->port_name = strdup(local_port);
-       if (mi->port_name == NULL) {
-               _LOGE("Malloc failed (%s)", local_port);
-               free(mi);
-               return false;
-       }
-       mi->local_id = local_id;
-
-       g_hash_table_insert(__local_port_info, GINT_TO_POINTER(mi->local_id), mi);
-       return true;
-}
-
-static int __register_message_port(const char *local_port, bool is_trusted, messageport_message_cb callback)
-{
-       _SECURE_LOGI("local_port : [%s:%s]", local_port, is_trusted ? "trusted" : "non-trusted");
-
-       int local_id = 0;
-
-       /* Check the message port is already registed */
-       if (__is_local_port_registed(local_port, is_trusted, &local_id, NULL))
-               return local_id;
-
-       local_id = __register_dbus_interface(local_port, is_trusted);
-       if (local_id < 1) {
-               _LOGE("register_dbus_interface fail !!");
-               return MESSAGEPORT_ERROR_OUT_OF_MEMORY;
-       }
-
-       if (!__message_port_register_port(local_id, local_port, is_trusted, callback))
-               return MESSAGEPORT_ERROR_OUT_OF_MEMORY;
-
-       return local_id;
-}
-
-static void __free_delay_message_info(delay_message_info_s *message)
-{
-       if (message != NULL) {
-               FREE_AND_NULL(message->local_port_name);
-               FREE_AND_NULL(message->data);
-               FREE_AND_NULL(message);
-       }
-}
-
-static void __free_list_delay_message_info(gpointer data)
-{
-       delay_message_info_s *message = (delay_message_info_s *)data;
-
-       if (message != NULL)
-               __free_delay_message_info(message);
-}
-
-static int __send_delayed_message(int sockfd, delay_message_info_s *message)
-{
-       unsigned int nb = 0;
-       int sequence = message->sequence - 1;
-       int ret = MESSAGEPORT_ERROR_NONE;
-       bool is_startline = true;
-       int offset = 0;
-
-       _LOGI("send_delayed_message : sockfd (%d) sequence(%d) sent byte(%d)",
-               sockfd, message->sequence, message->sent_bytes);
-
-       switch (message->sequence) {
-       case SEQUENCE_START:
-               sequence++;
-               is_startline = false;
-
-       case SEQUENCE_PORT_LEN:
-               if (is_startline)
-                       offset = message->sent_bytes;
-
-               ret = __write_socket(sockfd, ((char *)&message->local_port_len) + offset,
-                               sizeof(message->local_port_len) - offset, &nb, &sequence);
-               if (ret != MESSAGEPORT_ERROR_NONE) {
-                       _LOGE("write local_port_len fail");
-                       goto out;
-               }
-               offset = 0;
-               is_startline = false;
-
-       case SEQUENCE_PORT_NAME:
-               if (is_startline)
-                       offset = message->sent_bytes;
-
-               if (message->local_port_len > 0)
-                       ret = __write_socket(sockfd, message->local_port_name + offset,
-                               message->local_port_len - offset , &nb, &sequence);
-               else
-                       sequence++;
-
-               if (ret != MESSAGEPORT_ERROR_NONE) {
-                       _LOGE("write local_port fail");
-                       goto out;
-               }
-               offset = 0;
-               is_startline = false;
-
-       case SEQUENCE_BIDIRECTION:
-               if (is_startline)
-                       offset = message->sent_bytes;
-
-               ret = __write_socket(sockfd, ((char *)&message->is_bidirection) + offset,
-                               sizeof(message->is_bidirection) - offset, &nb, &sequence);
-               if (ret != MESSAGEPORT_ERROR_NONE) {
-                       _LOGE("write is_bidirection fail");
-                       goto out;
-               }
-               offset = 0;
-               is_startline = false;
-
-       case SEQUENCE_TRUSTED:
-               if (is_startline)
-                       offset = message->sent_bytes;
-
-               ret = __write_socket(sockfd, ((char *)&message->local_trusted) + offset,
-                               sizeof(message->local_trusted) - offset, &nb, &sequence);
-               if (ret != MESSAGEPORT_ERROR_NONE) {
-                       _LOGE("write local_trusted fail");
-                       goto out;
-               }
-               offset = 0;
-               is_startline = false;
-
-       case SEQUENCE_DTAT_LEN:
-               if (is_startline)
-                       offset = message->sent_bytes;
-
-               ret = __write_socket(sockfd, ((char *)&message->data_len) + offset,
-                               sizeof(message->data_len) - offset, &nb, &sequence);
-               if (ret != MESSAGEPORT_ERROR_NONE) {
-                       _LOGE("write data_len fail");
-                       goto out;
-               }
-               offset = 0;
-               is_startline = false;
-
-       case SEQUENCE_DATA:
-               if (is_startline)
-                       offset = message->sent_bytes;
-
-               ret = __write_socket(sockfd, (char *)message->data + offset,
-                       message->data_len -offset, &nb, &sequence);
-
-               if (ret != MESSAGEPORT_ERROR_NONE) {
-                       _LOGE("write data fail");
-                       goto out;
-               }
-               offset = 0;
-               is_startline = false;
-
-       default:
-               ret = MESSAGEPORT_ERROR_NONE;
-
-       }
-
-out:
-       if (ret == MESSAGEPORT_ERROR_RESOURCE_UNAVAILABLE) {
-               if (is_startline)
-                        message->sent_bytes += nb;
-               else
-                        message->sent_bytes = nb;
-
-               message->sequence = sequence;
-               _LOGE("send_delayed_message fail : sockfd (%d) sequence(%d) sent byte(%d)",
-                       sockfd, message->sequence, message->sent_bytes);
-       }
-
-       return ret;
-
-}
-
-static gboolean __process_delayed_message(gint fd, GIOCondition cond, gpointer data)
-{
-       port_list_info_s *port_info = (port_list_info_s *)data;
-       delay_message_info_s *message;
-       int ret;
-
-       if (port_info == NULL)
-               return G_SOURCE_REMOVE;
-
-       pthread_mutex_lock(&mutex);
-
-       if (port_info->delayed_message_list == NULL) {
-               port_info->delayed_message_size = 0;
-               port_info->delay_src_id = 0;
-               pthread_mutex_unlock(&mutex);
-               return G_SOURCE_REMOVE;
-       } else {
-               message = g_list_nth_data(port_info->delayed_message_list, 0);
-               ret = __send_delayed_message(port_info->send_sock_fd, message);
-
-               if (ret == MESSAGEPORT_ERROR_RESOURCE_UNAVAILABLE) {
-                       pthread_mutex_unlock(&mutex);
-                       return G_SOURCE_CONTINUE;
-               } else if (ret == MESSAGEPORT_ERROR_IO_ERROR) {
-                       __free_port_info((gpointer)port_info);
-                       pthread_mutex_unlock(&mutex);
-                       return G_SOURCE_REMOVE;
-               }
-
-               port_info->delayed_message_size -= message->size;
-
-               port_info->delayed_message_list = g_list_remove(port_info->delayed_message_list, message);
-               __free_delay_message_info(message);
-       }
-
-       pthread_mutex_unlock(&mutex);
-
-       return G_SOURCE_CONTINUE;
-}
-
-static int __insert_delayed_message(port_list_info_s *port_info,
-       int sequence,
-       bundle_raw *kb_data,
-       int data_len,
-       unsigned int sent_bytes,
-       const char *local_port,
-       bool local_trusted,
-       bool is_bidirection)
-{
-#define QUEUE_SIZE_MAX (1024 * 1024) /* 1MB per remote port (MAX) */
-
-       unsigned int tmp_size;
-       unsigned int message_size;
-       int ret = MESSAGEPORT_ERROR_NONE;
-
-       if (port_info->delayed_message_size >= QUEUE_SIZE_MAX) {
-               _LOGE("cache fail : delayed_message_size (%d), count(%d)",
-                       port_info->delayed_message_size, g_list_length(port_info->delayed_message_list));
-               return MESSAGEPORT_ERROR_RESOURCE_UNAVAILABLE;
-       }
-
-       delay_message_info_s *message = (delay_message_info_s *)calloc(1, sizeof(delay_message_info_s));
-       retvm_if(!message, MESSAGEPORT_ERROR_OUT_OF_MEMORY, "Malloc failed");
-
-       message_size = sizeof(delay_message_info_s);
-
-       message->sequence = sequence;
-       tmp_size = strlen(local_port) + 1;
-       message_size += tmp_size;
-       message->local_port_len = tmp_size;
-       message->local_port_name = strdup(local_port);
-       if (message->local_port_name == NULL) {
-               _LOGE("local_port_name strdup fail");
-               ret = MESSAGEPORT_ERROR_OUT_OF_MEMORY;
-               goto out;
-       }
-       message->is_bidirection = is_bidirection;
-       message->local_trusted = local_trusted;
-       message_size += data_len;
-       message->data_len = data_len;
-       message->data = (bundle_raw *)strdup((const char *)kb_data);
-       if (message->data == NULL) {
-               _LOGE("data strdup fail");
-               ret = MESSAGEPORT_ERROR_OUT_OF_MEMORY;
-               goto out;
-       }
-
-
-       message->sent_bytes = sent_bytes;
-       message->size = message_size;
-       port_info->delayed_message_size += message_size;
-
-       port_info->delayed_message_list = g_list_append(port_info->delayed_message_list, message);
-
-       if (port_info->delay_src_id == 0) {
-                       port_info->delay_src_id = g_unix_fd_add_full(G_PRIORITY_DEFAULT,
-                                                       port_info->send_sock_fd, G_IO_OUT, __process_delayed_message,
-                                                       port_info, NULL);
-       }
-
-       _LOGE("inserted : pm(%s) fd(%d) ms(%d) ds(%d) dlc(%d) sqn(%d) sb (%d)",
-               port_info->port_name, port_info->send_sock_fd, message_size,
-               port_info->delayed_message_size,
-               g_list_length(port_info->delayed_message_list), sequence, sent_bytes);
-
-
-
-out:
-       if (ret != MESSAGEPORT_ERROR_NONE)
-               __free_delay_message_info(message);
-
-       return ret;
-}
-
-int __message_port_send_async(port_list_info_s *port_info, bundle *kb, const char *local_port,
-               bool local_trusted, bool is_bidirection)
-{
-       int ret = 0;
-       int data_len;
-       int local_port_len = 0;
-       unsigned int nb = 0;
-       bundle_raw *kb_data = NULL;
-       int sequence = SEQUENCE_START;
-
-       bundle_encode(kb, &kb_data, &data_len);
-       if (kb_data == NULL) {
-               _LOGE("bundle encode fail");
-               ret = MESSAGEPORT_ERROR_INVALID_PARAMETER;
-               goto out;
-       }
-
-       if (data_len > MAX_MESSAGE_SIZE) {
-               _LOGE("bigger than max size\n");
-               ret = MESSAGEPORT_ERROR_MAX_EXCEEDED;
-               goto out;
-       }
-
-       if (g_list_length(port_info->delayed_message_list) > 0) {
-               ret = MESSAGEPORT_ERROR_RESOURCE_UNAVAILABLE;
-               _LOGE("There are messages in the delayed_message_list (count %d)",
-                       g_list_length(port_info->delayed_message_list));
-               goto out;
-       }
-
-       if (local_port != NULL)
-               local_port_len = strlen(local_port) + 1;
-
-       ret = __write_string_to_socket(port_info->send_sock_fd, local_port,
-                       local_port_len, &nb, &sequence);
-       if (ret != MESSAGEPORT_ERROR_NONE) {
-               _LOGE("write local_port fail");
-               goto out;
-       }
-
-       ret = __write_socket(port_info->send_sock_fd, (char *)&is_bidirection,
-                       sizeof(is_bidirection), &nb, &sequence);
-       if (ret != MESSAGEPORT_ERROR_NONE) {
-               _LOGE("write is_bidirection fail");
-               goto out;
-       }
-
-       ret = __write_socket(port_info->send_sock_fd, (char *)&local_trusted,
-                       sizeof(local_trusted), &nb, &sequence);
-       if (ret != MESSAGEPORT_ERROR_NONE) {
-               _LOGE("write local_trusted fail");
-               goto out;
-       }
-
-       ret = __write_string_to_socket(port_info->send_sock_fd, (void *)kb_data,
-                       data_len, &nb, &sequence);
-       if (ret != MESSAGEPORT_ERROR_NONE) {
-               _LOGE("write kb_data fail");
-               goto out;
-       }
-
-out:
-       if (ret == MESSAGEPORT_ERROR_RESOURCE_UNAVAILABLE) {
-               ret = __insert_delayed_message(port_info, sequence, kb_data, data_len, nb,
-                       local_port, local_trusted, is_bidirection);
-               if (ret != MESSAGEPORT_ERROR_NONE)
-                       ret = MESSAGEPORT_ERROR_IO_ERROR;
-       }
-
-       if (kb_data)
-               free(kb_data);
-
-       return ret;
-}
-
-static int __message_port_send_message(const char *remote_appid, const char *remote_port,
-               const char *local_port, bool trusted_message, bool local_trusted, bool bi_dir, bundle *message)
-{
-
-       int ret = MESSAGEPORT_ERROR_NONE;
-       GUnixFDList *fd_list = NULL;
-
-       int len = 0;
-       bundle_raw *raw = NULL;
-       char *bus_name = NULL;
-       char *interface_name = NULL;
-
-       message_port_remote_app_info_s *remote_app_info = NULL;
-       port_list_info_s *port_info = NULL;
-       GDBusMessage *msg = NULL;
-       GError *err = NULL;
-       GVariant *body = NULL;
-       int sock_pair[2] = {0,};
-       char buf[1024];
-
-       ret = __get_remote_port_info(remote_appid, remote_port, trusted_message, &remote_app_info, &port_info);
-       if (ret != MESSAGEPORT_ERROR_NONE)
-               return ret;
-
-       if (port_info->exist == false) {
-               bool exist = false;
-               _LOGD("port exist check !!");
-               ret =  __check_remote_port(remote_appid, remote_port, trusted_message, &exist);
-               if (ret != MESSAGEPORT_ERROR_NONE)
-                       return ret;
-               else if (!exist)
-                       return MESSAGEPORT_ERROR_MESSAGEPORT_NOT_FOUND;
-       }
-
-       if (port_info->send_sock_fd > 0) {
-               ret = __message_port_send_async(port_info, message,
-                               (local_port) ? local_port : "", local_trusted, bi_dir);
-       } else {
-
-               bus_name = port_info->encoded_bus_name;
-               interface_name = bus_name;
-
-               if (bundle_encode(message, &raw, &len) != BUNDLE_ERROR_NONE) {
-                       ret = MESSAGEPORT_ERROR_INVALID_PARAMETER;
-                       goto out;
-               }
-
-               if (MAX_MESSAGE_SIZE < len) {
-                       _LOGE("The size of message (%d) has exceeded the maximum limit.", len);
-                       ret = MESSAGEPORT_ERROR_MAX_EXCEEDED;
-                       goto out;
-               }
-
-               body = g_variant_new("(ssbbssbus)", __app_id, (local_port) ? local_port : "", local_trusted, bi_dir,
-                               remote_appid, remote_port, trusted_message, len, raw);
-               if (strcmp(remote_appid, __app_id) != 0) { /* self send */
-
-                       /*  if message-port fail to get socket pair, communicate using GDBus */
-                       if (aul_request_message_port_socket_pair(sock_pair) != AUL_R_OK) {
-                               _LOGE("error create socket pair");
-                       } else {
-
-                               _LOGI("sock pair : %d, %d",
-                                               sock_pair[SOCK_PAIR_SENDER], sock_pair[SOCK_PAIR_RECEIVER]);
-                               fd_list = g_unix_fd_list_new();
-                               g_unix_fd_list_append(fd_list, sock_pair[SOCK_PAIR_RECEIVER], &err);
-                               if (err != NULL) {
-                                       _LOGE("g_unix_fd_list_append [%s]", err->message);
-                                       ret = MESSAGEPORT_ERROR_IO_ERROR;
-                                       g_error_free(err);
-                                       goto out;
-                               }
-
-                               port_info->send_sock_fd = sock_pair[SOCK_PAIR_SENDER];
-                               close(sock_pair[SOCK_PAIR_RECEIVER]);
-                               sock_pair[SOCK_PAIR_RECEIVER] = 0;
-
-                               port_info->gio_read = g_io_channel_unix_new(port_info->send_sock_fd);
-                               if (!port_info->gio_read) {
-                                       _LOGE("Error is %s\n", strerror_r(errno, buf, sizeof(buf)));
-                                       ret = MESSAGEPORT_ERROR_IO_ERROR;
-                                       goto out;
-                               }
-
-                               port_info->g_src_id = g_io_add_watch(
-                                               port_info->gio_read,
-                                               G_IO_IN | G_IO_HUP,
-                                               __socket_disconnect_handler,
-                                               (gpointer)port_info);
-                               if (port_info->g_src_id == 0) {
-                                       _LOGE("fail to add watch on socket");
-                                       ret = MESSAGEPORT_ERROR_IO_ERROR;
-                                       goto out;
-                               }
-
-                       }
-               }
-
-               msg = g_dbus_message_new_method_call(bus_name, MESSAGEPORT_OBJECT_PATH, interface_name, "send_message");
-               if (!msg) {
-                       _LOGE("Can't allocate new method call");
-                       ret = MESSAGEPORT_ERROR_OUT_OF_MEMORY;
-                       goto out;
-               }
-
-               g_dbus_message_set_unix_fd_list(msg, fd_list);
-               g_dbus_message_set_body(msg, body);
-               g_dbus_connection_send_message(__gdbus_conn, msg, G_DBUS_SEND_MESSAGE_FLAGS_NONE, NULL, &err);
-               if (err != NULL) {
-                       _LOGE("No reply. error = %s", err->message);
-                       g_error_free(err);
-                       ret = MESSAGEPORT_ERROR_IO_ERROR;
-                       goto out;
-               }
-       }
-
-out:
-       if (msg)
-               g_object_unref(msg);
-       if (raw)
-               bundle_free_encoded_rawdata(&raw);
-       if (fd_list)
-               g_object_unref(fd_list);
-
-       if (ret != MESSAGEPORT_ERROR_NONE) {
-               __free_port_info((gpointer)port_info);
-               if (sock_pair[SOCK_PAIR_SENDER])
-                       close(sock_pair[SOCK_PAIR_SENDER]);
-               if (sock_pair[SOCK_PAIR_RECEIVER])
-                       close(sock_pair[SOCK_PAIR_RECEIVER]);
-       }
-
-       return ret;
-}
-
-int __message_send_bidirectional_message(int id, const char *remote_app_id, const char *remote_port,  bool trusted_message, bundle *message)
-{
-       message_port_local_port_info_s *local_info;
-       int ret = __get_local_port_info(id, &local_info);
-       if (ret != MESSAGEPORT_ERROR_NONE)
-               return ret;
-
-       _LOGD("bidirectional_message %s", local_info->port_name);
-       return __message_port_send_message(remote_app_id, remote_port,
-                       local_info->port_name, trusted_message, local_info->is_trusted, true, message);
-}
-
-static void __name_registered(GDBusConnection *connection,
-               const gchar *name,
-               const gchar *name_owner,
-               gpointer user_data)
-{
-
-       registered_callback_info_s *info = (registered_callback_info_s *)user_data;
-       if (info == NULL) {
-               LOGE("NULL registered_callback_info");
-               return;
-       }
-
-       _LOGI("watcher_id : %d, appeared name : %s , name_owner : %s\n", info->watcher_id, name, name_owner);
-       if (info->registered_cb)
-               info->registered_cb(info->remote_app_id, info->remote_port, info->is_trusted, info->user_data);
-}
-
-static void __name_unregistered(GDBusConnection *connection,
-               const gchar *name,
-               gpointer user_data)
-{
-
-       registered_callback_info_s *info = (registered_callback_info_s *)user_data;
-       if (info == NULL) {
-               LOGE("NULL registered_callback_info");
-               return;
-       }
-
-       _LOGI("watcher_id : %d, vanished name : %s\n", info->watcher_id, name);
-       if (info->unregistered_cb)
-               info->unregistered_cb(info->remote_app_id, info->remote_port, info->is_trusted, info->user_data);
-}
-
-int __messageport_watch_remote_port(int *watcher_id, const char *remote_app_id, const char *remote_port, bool trusted_remote_port, messageport_registration_event_cb registered_cb, messageport_registration_event_cb unregistered_cb, void *user_data)
-{
-       int ret_val = MESSAGEPORT_ERROR_NONE;
-       message_port_remote_app_info_s *remote_app_info = NULL;
-       port_list_info_s *port_info = NULL;
-
-       _LOGI("remote_app_id, app_id :[%s : %s] ", remote_app_id, __app_id);
-
-       ret_val = __get_remote_port_info(remote_app_id, remote_port, trusted_remote_port, &remote_app_info, &port_info);
-       if (ret_val != MESSAGEPORT_ERROR_NONE)
-               return ret_val;
-
-       if (__registered_callback_info_hash == NULL)
-               __registered_callback_info_hash = g_hash_table_new_full(g_direct_hash,  g_direct_equal, NULL, __registered_callback_info_free);
-
-       registered_callback_info_s *registered_cb_info = (registered_callback_info_s *)calloc(1, sizeof(registered_callback_info_s));
-       retvm_if(!registered_cb_info, MESSAGEPORT_ERROR_OUT_OF_MEMORY, "Malloc failed");
-
-       registered_cb_info->registered_cb = registered_cb;
-       registered_cb_info->unregistered_cb = unregistered_cb;
-       registered_cb_info->user_data = user_data;
-       registered_cb_info->remote_app_id = strdup(remote_app_info->remote_app_id);
-       if (registered_cb_info->remote_app_id == NULL) {
-               free(registered_cb_info);
-               return MESSAGEPORT_ERROR_OUT_OF_MEMORY;
-       }
-       registered_cb_info->remote_port = strdup(port_info->port_name);
-       if (registered_cb_info->remote_port == NULL) {
-               free(registered_cb_info->remote_app_id);
-               free(registered_cb_info);
-               return MESSAGEPORT_ERROR_OUT_OF_MEMORY;
-       }
-
-       registered_cb_info->watcher_id = g_bus_watch_name_on_connection(
-                       __gdbus_conn,
-                       port_info->encoded_bus_name,
-                       G_BUS_NAME_WATCHER_FLAGS_NONE,
-                       __name_registered,
-                       __name_unregistered,
-                       registered_cb_info,
-                       NULL);
-       if (registered_cb_info->watcher_id == 0) {
-               free(registered_cb_info->remote_app_id);
-               free(registered_cb_info->remote_port);
-               free(registered_cb_info);
-               return MESSAGEPORT_ERROR_IO_ERROR;
-       }
-
-       g_hash_table_insert(__registered_callback_info_hash,
-                       GINT_TO_POINTER(registered_cb_info->watcher_id), registered_cb_info);
-
-       *watcher_id = registered_cb_info->watcher_id;
-       return MESSAGEPORT_ERROR_NONE;
-}
-
-int messageport_unregister_local_port(int local_port_id, bool trusted_port)
-{
-
-       GVariant *result;
-       char *bus_name = NULL;
-       GError *err = NULL;
-       int ret = 0;
-
-       _LOGI("unregister : %d", local_port_id);
-
-       message_port_local_port_info_s *mi =
-               (message_port_local_port_info_s *)
-               g_hash_table_lookup(__local_port_info, GINT_TO_POINTER(local_port_id));
-       if (mi == NULL)
-               return MESSAGEPORT_ERROR_MESSAGEPORT_NOT_FOUND;
-
-       if (mi->is_trusted != trusted_port)
-               return MESSAGEPORT_ERROR_INVALID_PARAMETER;
-
-       g_hash_table_remove(__callback_info_hash, GUINT_TO_POINTER(local_port_id));
-
-       bus_name = __get_encoded_name(__app_id, mi->port_name, mi->is_trusted);
-       if (bus_name == NULL)
-               return MESSAGEPORT_ERROR_OUT_OF_MEMORY;
-
-       g_dbus_connection_unregister_object(__gdbus_conn, local_port_id);
-
-       result = g_dbus_connection_call_sync(
-                       __gdbus_conn,
-                       DBUS_SERVICE_DBUS,
-                       DBUS_PATH_DBUS,
-                       DBUS_INTERFACE_DBUS,
-                       "ReleaseName",
-                       g_variant_new("(s)", bus_name),
-                       G_VARIANT_TYPE("(u)"),
-                       G_DBUS_CALL_FLAGS_NONE,
-                       -1,
-                       NULL,
-                       &err);
-
-       if (bus_name)
-               free(bus_name);
-
-       if (err) {
-               _LOGE("RequestName fail : %s", err->message);
-               g_error_free(err);
-               return MESSAGEPORT_ERROR_MESSAGEPORT_NOT_FOUND;
-       }
-       g_variant_get(result, "(u)", &ret);
-
-       if (result)
-               g_variant_unref(result);
-
-       if (ret != DBUS_RELEASE_NAME_REPLY_RELEASED) {
-
-               if (ret == DBUS_RELEASE_NAME_REPLY_NON_EXISTENT) {
-                       _LOGE("Port Not exist");
-                       return MESSAGEPORT_ERROR_MESSAGEPORT_NOT_FOUND;
-               } else if (ret == DBUS_RELEASE_NAME_REPLY_NOT_OWNER) {
-                       _LOGE("Try to release not owned name. MESSAGEPORT_ERROR_INVALID_PARAMETER");
-                       return MESSAGEPORT_ERROR_INVALID_PARAMETER;
-               }
-       }
-
-
-       g_hash_table_remove(__local_port_info, GINT_TO_POINTER(local_port_id));
-
-       return MESSAGEPORT_ERROR_NONE;
-}
-
-int messageport_register_local_port(const char *local_port, messageport_message_cb callback)
-{
-       if (!_initialized) {
-               if (!__initialize())
-                       return MESSAGEPORT_ERROR_IO_ERROR;
-       }
-
-       return __register_message_port(local_port, false, callback);
-}
-
-int messageport_register_trusted_local_port(const char *local_port, messageport_message_cb callback)
-{
-       if (!_initialized) {
-               if (!__initialize())
-                       return MESSAGEPORT_ERROR_IO_ERROR;
-       }
-
-       return __register_message_port(local_port, true, callback);
-
-}
-
-int messageport_check_remote_port(const char *remote_app_id, const char *remote_port, bool *exist)
-{
-       if (!_initialized) {
-               if (!__initialize())
-                       return MESSAGEPORT_ERROR_IO_ERROR;
-       }
-
-       int ret = __check_remote_port(remote_app_id, remote_port, false, exist);
-       if (ret == MESSAGEPORT_ERROR_MESSAGEPORT_NOT_FOUND) {
-               *exist = false;
-               ret = MESSAGEPORT_ERROR_NONE;
-       }
-
-       return ret;
-}
-
-int messageport_check_trusted_remote_port(const char *remote_app_id, const char *remote_port, bool *exist)
-{
-       if (!_initialized) {
-               if (!__initialize())
-                       return MESSAGEPORT_ERROR_IO_ERROR;
-       }
-
-       int ret = __check_remote_port(remote_app_id, remote_port, true, exist);
-       if (ret == MESSAGEPORT_ERROR_MESSAGEPORT_NOT_FOUND) {
-               *exist = false;
-               ret = MESSAGEPORT_ERROR_NONE;
-       }
-
-       return ret;
-}
-
-int messageport_send_message(const char *remote_app_id, const char *remote_port, bundle *message)
-{
-       if (!_initialized) {
-               if (!__initialize())
-                       return MESSAGEPORT_ERROR_IO_ERROR;
-       }
-
-       return __message_port_send_message(remote_app_id, remote_port, NULL, false, false, false, message);
-}
-
-int messageport_send_trusted_message(const char *remote_app_id, const char *remote_port, bundle *message)
-{
-       if (!_initialized) {
-               if (!__initialize())
-                       return MESSAGEPORT_ERROR_IO_ERROR;
-       }
-
-       return __message_port_send_message(remote_app_id, remote_port, NULL, true, false, false, message);
-}
-
-int messageport_send_bidirectional_message(int id, const char *remote_app_id, const char *remote_port,
-               bundle *message)
-{
-       if (!_initialized) {
-               if (!__initialize())
-                       return MESSAGEPORT_ERROR_IO_ERROR;
-       }
-
-       return __message_send_bidirectional_message(id, remote_app_id, remote_port, false, message);
-}
-
-int messageport_send_bidirectional_trusted_message(int id, const char *remote_app_id, const char *remote_port,
-               bundle *message)
-{
-       if (!_initialized) {
-               if (!__initialize())
-                       return MESSAGEPORT_ERROR_IO_ERROR;
-       }
-       return __message_send_bidirectional_message(id, remote_app_id, remote_port, true, message);
-}
-
-int messageport_add_registered_cb(const char *remote_app_id, const char *remote_port, bool is_trusted, messageport_registration_event_cb registered_cb, void *user_data, int *watcher_id)
-{
-       if (!_initialized) {
-               if (!__initialize())
-                       return MESSAGEPORT_ERROR_IO_ERROR;
-       }
-       return __messageport_watch_remote_port(watcher_id, remote_app_id, remote_port, is_trusted, registered_cb, NULL, user_data);
-}
-
-int messageport_add_unregistered_cb(const char *remote_app_id, const char *remote_port, bool is_trusted, messageport_registration_event_cb unregistered_cb, void *user_data, int *watcher_id)
-{
-       if (!_initialized) {
-               if (!__initialize())
-                       return MESSAGEPORT_ERROR_IO_ERROR;
-       }
-       return __messageport_watch_remote_port(watcher_id, remote_app_id, remote_port, is_trusted, NULL, unregistered_cb, user_data);
-}
-
-
-int messageport_remove_registration_event_cb(int watcher_id)
-{
-       registered_callback_info_s *registered_cb_info = NULL;
-       gboolean remove_result = FALSE;
-
-       if (watcher_id < 1)
-               return MESSAGEPORT_ERROR_INVALID_PARAMETER;
-
-       registered_cb_info = g_hash_table_lookup(__registered_callback_info_hash, GINT_TO_POINTER(watcher_id));
-       if (registered_cb_info == NULL)
-               return MESSAGEPORT_ERROR_INVALID_PARAMETER;
-
-       remove_result = g_hash_table_remove(__registered_callback_info_hash, GINT_TO_POINTER(watcher_id));
-       if (!remove_result)
-               return MESSAGEPORT_ERROR_IO_ERROR;
-
-       g_bus_unwatch_name(watcher_id);
-
-       return MESSAGEPORT_ERROR_NONE;
-}
index 7044870..877d054 100755 (executable)
 #include <stdlib.h>
 #include <stdbool.h>
 #include <pthread.h>
-#include <message-port.h>
-#include "message_port_internal.h"
+#include "message_port_common.h"
+#include "message_port_local.h"
+#include "message_port_remote.h"
+
 #include "message_port_log.h"
 #include "message_port.h"
 
-typedef struct message_port_callback_item_s {
-       message_port_message_cb callback;
-       void *user_data;
-} message_port_callback_item;
-
-static GHashTable *__listeners;
-static GHashTable *__trusted_listeners;
 pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
 
-static void do_callback(message_port_message_cb callback, int local_port_id, const char *remote_app_id, const char *remote_port, bool trusted_remote_port, bundle *message, void *user_data)
-{
-       if (callback)
-               callback(local_port_id, remote_app_id, remote_port, trusted_remote_port, message, user_data);
-       else
-               _LOGI("Ignored");
-}
-
-static void message_dispatcher(int local_port_id, const char *remote_app_id, const char *remote_port, bool trusted_remote_port, bundle *message, void *user_data)
-{
-       message_port_callback_item *item = NULL;
-       if (__listeners == NULL)
-               return;
-       item = (message_port_callback_item *)g_hash_table_lookup(__listeners, GINT_TO_POINTER(local_port_id));
-       if (item != NULL)
-               do_callback(item->callback, local_port_id, remote_app_id, remote_port, trusted_remote_port, message, item->user_data);
-}
-
-static void trusted_message_dispatcher(int trusted_local_port_id, const char *remote_app_id, const char *remote_port, bool trusted_remote_port, bundle *message, void *user_data)
-{
-       message_port_callback_item *item = NULL;
-
-       if (__trusted_listeners == NULL)
-               return;
-       item = (message_port_callback_item *)g_hash_table_lookup(__trusted_listeners, GINT_TO_POINTER(trusted_local_port_id));
-       if (item != NULL)
-               do_callback(item->callback, trusted_local_port_id, remote_app_id, remote_port, trusted_remote_port, message, item->user_data);
-}
-
 int message_port_register_local_port(const char *local_port, message_port_message_cb callback, void *user_data)
 {
+       int local_port_id;
+
        if (local_port == NULL || callback == NULL) {
-               _LOGE("[MESSAGE_PORT_ERROR_INVALID_PARAMETER] NULL value is not allowed.");
+               _LOGE("NULL value is not allowed.");
                return MESSAGE_PORT_ERROR_INVALID_PARAMETER;
        }
 
-       int local_port_id = messageport_register_local_port(local_port, message_dispatcher);
-       if (local_port_id > 0) {
-               _LOGD("Register local port ID (%d).", local_port_id);
-
-               if (__listeners == NULL)
-                       __listeners = g_hash_table_new_full(g_direct_hash, g_direct_equal, NULL, g_free);
-
-               pthread_mutex_lock(&mutex);
-               message_port_callback_item *item =
-                       (message_port_callback_item *)g_hash_table_lookup(__listeners, GINT_TO_POINTER(local_port_id));
-               if (item == NULL) {
-                       item = (message_port_callback_item *)calloc(1, sizeof(message_port_callback_item));
-                       if (item == NULL) {
-                               pthread_mutex_unlock(&mutex);
-                               return MESSAGE_PORT_ERROR_OUT_OF_MEMORY;
-                       }
-
-                       g_hash_table_insert(__listeners, GINT_TO_POINTER(local_port_id), item);
-               }
-
-               item->callback = callback;
-               item->user_data = user_data;
-               pthread_mutex_unlock(&mutex);
+       pthread_mutex_lock(&mutex);
+       local_port_id = register_message_port(local_port, false, callback, user_data);
+       pthread_mutex_unlock(&mutex);
 
-       } else {
-               _SECURE_LOGE("Register local port fail (%d).", local_port_id);
-       }
+       if (local_port_id > 0)
+               _LOGD("Register local port ID (%d).", local_port_id);
+       else
+               _LOGE("Register local port fail (%d).", local_port_id);
 
-       return convert_to_tizen_error((messageport_error_e)local_port_id);
+       return local_port_id;
 }
 
 int message_port_register_trusted_local_port(const char *local_port, message_port_trusted_message_cb callback, void *user_data)
 {
+       int trusted_local_port_id;
+
        if (local_port == NULL || callback == NULL) {
-               _LOGE("[MESSAGE_PORT_ERROR_INVALID_PARAMETER] NULL value is not allowed.");
+               _LOGE("NULL value is not allowed.");
                return MESSAGE_PORT_ERROR_INVALID_PARAMETER;
        }
 
-       int trusted_local_port_id = messageport_register_trusted_local_port(local_port, trusted_message_dispatcher);
-       if (trusted_local_port_id > 0) {
-               _SECURE_LOGD("Register trusted local port ID (%d).", trusted_local_port_id);
-
-               if (__trusted_listeners == NULL)
-                       __trusted_listeners = g_hash_table_new_full(g_direct_hash, g_direct_equal, NULL, g_free);
-
-               pthread_mutex_lock(&mutex);
-               message_port_callback_item *item =
-                       (message_port_callback_item *)g_hash_table_lookup(__trusted_listeners, GINT_TO_POINTER(trusted_local_port_id));
-               if (item == NULL) {
-                       item = (message_port_callback_item *)calloc(1, sizeof(message_port_callback_item));
-                       if (item == NULL) {
-                               pthread_mutex_unlock(&mutex);
-                               return MESSAGE_PORT_ERROR_OUT_OF_MEMORY;
-                       }
-
-                       g_hash_table_insert(__trusted_listeners, GINT_TO_POINTER(trusted_local_port_id), item);
-               }
-
-               item->callback = callback;
-               item->user_data = user_data;
-               pthread_mutex_unlock(&mutex);
-       } else {
-               _SECURE_LOGI("Register trusted local port fail (%d).", trusted_local_port_id);
-       }
+       pthread_mutex_lock(&mutex);
+       trusted_local_port_id = register_message_port(local_port, true, callback, user_data);
+       pthread_mutex_unlock(&mutex);
+
+       if (trusted_local_port_id > 0)
+               _LOGD("Register trusted local port ID (%d).", trusted_local_port_id);
+       else
+               _LOGE("Register trusted local port fail (%d).", trusted_local_port_id);
 
-       return convert_to_tizen_error((messageport_error_e)trusted_local_port_id);
+       return trusted_local_port_id;
 }
 
 int message_port_unregister_local_port(int local_port_id)
 {
-       int res = MESSAGE_PORT_ERROR_NONE;
+       int res;
+
        if (local_port_id <= 0) {
-               _LOGE("[MESSAGEPORT_ERROR_INVALID_PARAMETER] Neither 0 nor negative value is allowed.");
+               _LOGE("Neither 0 nor negative value is allowed.");
                return MESSAGE_PORT_ERROR_INVALID_PARAMETER;
-       } else {
-               res = messageport_unregister_local_port(local_port_id, false);
-               g_hash_table_remove(__listeners, GINT_TO_POINTER(local_port_id));
        }
-       return convert_to_tizen_error((messageport_error_e)res);
+
+       res = unregister_local_port(local_port_id, false);
+
+       return res;
 }
 
 int message_port_unregister_trusted_local_port(int trusted_local_port_id)
 {
+       int res;
 
-       int res = MESSAGE_PORT_ERROR_NONE;
        if (trusted_local_port_id <= 0) {
-               _LOGE("[MESSAGEPORT_ERROR_INVALID_PARAMETER] Neither 0 nor negative value is allowed.");
+               _LOGE("Neither 0 nor negative value is allowed.");
                return MESSAGE_PORT_ERROR_INVALID_PARAMETER;
-       } else {
-               res = messageport_unregister_local_port(trusted_local_port_id, true);
-               g_hash_table_remove(__trusted_listeners, GINT_TO_POINTER(trusted_local_port_id));
        }
 
-       return convert_to_tizen_error((messageport_error_e)res);
+       res = unregister_local_port(trusted_local_port_id, true);
+
+       return res;
 }
 
 int message_port_check_remote_port(const char *remote_app_id, const char *remote_port, bool *exist)
 {
+       int ret;
+
        if (remote_app_id == NULL || remote_port == NULL) {
-               _LOGE("[MESSAGE_PORT_ERROR_INVALID_PARAMETER] NULL value is not allowed.");
+               _LOGE("NULL value is not allowed.");
                return MESSAGE_PORT_ERROR_INVALID_PARAMETER;
        }
        _LOGD("Check remote port (%s):(%s).", remote_app_id, remote_port);
-       return convert_to_tizen_error((messageport_error_e)messageport_check_remote_port(remote_app_id, remote_port, exist));
+
+       ret = check_remote_port(remote_app_id, remote_port, false, exist);
+
+       return ret;
 }
 
 int message_port_check_trusted_remote_port(const char *remote_app_id, const char *remote_port, bool *exist)
 {
+       int ret;
+
        if (remote_app_id == NULL || remote_port == NULL) {
-               _LOGE("[MESSAGE_PORT_ERROR_INVALID_PARAMETER] NULL value is not allowed.");
+               _LOGE("NULL value is not allowed.");
                return MESSAGE_PORT_ERROR_INVALID_PARAMETER;
        }
        _LOGD("Check trusted remote port (%s):(%s).", remote_app_id, remote_port);
-       return convert_to_tizen_error((messageport_error_e)messageport_check_trusted_remote_port(remote_app_id, remote_port, exist));
+
+       ret = check_remote_port(remote_app_id, remote_port, true, exist);
+
+       return ret;
 }
 
 int message_port_send_message(const char *remote_app_id, const char *remote_port, bundle *message)
 {
        int ret;
+
        if (remote_app_id == NULL || remote_port == NULL || message == NULL) {
-               _LOGE("[MESSAGE_PORT_ERROR_INVALID_PARAMETER] NULL value is not allowed.");
+               _LOGE("NULL value is not allowed.");
                return MESSAGE_PORT_ERROR_INVALID_PARAMETER;
        }
-
        _SECURE_LOGI("Send a message to (%s):(%s).", remote_app_id, remote_port);
+
        pthread_mutex_lock(&mutex);
-       ret = messageport_send_message(remote_app_id, remote_port, message);
+       ret = send_message(remote_app_id, remote_port, NULL, false, false, false, message);
        pthread_mutex_unlock(&mutex);
 
-       return convert_to_tizen_error((messageport_error_e)ret);
+       return ret;
 }
 
 int message_port_send_trusted_message(const char *remote_app_id, const char *remote_port, bundle *message)
 {
        int ret;
+
        if (remote_app_id == NULL || remote_port == NULL || message == NULL) {
                _LOGE("[MESSAGE_PORT_ERROR_INVALID_PARAMETER] NULL value is not allowed.");
                return MESSAGE_PORT_ERROR_INVALID_PARAMETER;
@@ -210,83 +155,55 @@ int message_port_send_trusted_message(const char *remote_app_id, const char *rem
        _SECURE_LOGI("Send a trusted message to (%s):(%s).", remote_app_id, remote_port);
 
        pthread_mutex_lock(&mutex);
-       ret = messageport_send_trusted_message(remote_app_id, remote_port, message);
+       ret = send_message(remote_app_id, remote_port, NULL, true, false, false, message);
        pthread_mutex_unlock(&mutex);
 
-       return convert_to_tizen_error((messageport_error_e)ret);
+       return ret;
 }
 
 int message_port_send_message_with_local_port(const char *remote_app_id, const char *remote_port, bundle *message, int local_port_id)
 {
        int ret;
+
        if (remote_app_id == NULL || remote_port == NULL || message == NULL) {
                _LOGE("[MESSAGE_PORT_ERROR_INVALID_PARAMETER] NULL value is not allowed.");
                return MESSAGE_PORT_ERROR_INVALID_PARAMETER;
        } else if (local_port_id <= 0) {
                _LOGE("[MESSAGEPORT_ERROR_INVALID_PARAMETER] Neither 0 nor negative value is allowed.");
                return MESSAGE_PORT_ERROR_INVALID_PARAMETER;
-       } else {
-
-               message_port_callback_item *item = NULL;
-               message_port_callback_item *trusted_item = NULL;
-
-               if (__listeners != NULL)
-                       item = (message_port_callback_item *)g_hash_table_lookup(__listeners, GINT_TO_POINTER(local_port_id));
-
-               if (item == NULL && __trusted_listeners != NULL)
-                       trusted_item = (message_port_callback_item *)g_hash_table_lookup(__trusted_listeners, GINT_TO_POINTER(local_port_id));
-
-
-               if (item == NULL && trusted_item == NULL) {
-                       _LOGE("[MESSAGE_PORT_ERROR_PORT_NOT_FOUND] The local port ID (%d) is not registered.", local_port_id);
-                       return MESSAGE_PORT_ERROR_PORT_NOT_FOUND;
-               }
        }
-
        _SECURE_LOGI("Send a message to (%s):(%s) - (%d).", remote_app_id, remote_port, local_port_id);
+
        pthread_mutex_lock(&mutex);
-       ret = messageport_send_bidirectional_message(local_port_id, remote_app_id, remote_port, message);
+       ret = send_bidirectional_message(local_port_id, remote_app_id, remote_port, false, message);
        pthread_mutex_unlock(&mutex);
 
-       return convert_to_tizen_error((messageport_error_e)ret);
+       return ret;
 }
 
 int message_port_send_trusted_message_with_local_port(const char *remote_app_id, const char *remote_port, bundle *message, int local_port_id)
 {
        int ret;
+
        if (remote_app_id == NULL || remote_port == NULL || message == NULL) {
                _LOGE("[MESSAGE_PORT_ERROR_INVALID_PARAMETER] NULL value is not allowed.");
                return MESSAGE_PORT_ERROR_INVALID_PARAMETER;
        } else if (local_port_id <= 0) {
                _LOGE("[MESSAGEPORT_ERROR_INVALID_PARAMETER] Neither 0 nor negative value is allowed.");
                return MESSAGE_PORT_ERROR_INVALID_PARAMETER;
-       } else {
-               message_port_callback_item *item = NULL;
-               message_port_callback_item *trusted_item = NULL;
-
-               if (__listeners != NULL)
-                       item = (message_port_callback_item *)g_hash_table_lookup(__listeners, GINT_TO_POINTER(local_port_id));
-
-               if (item == NULL && __trusted_listeners != NULL)
-                       trusted_item = (message_port_callback_item *)g_hash_table_lookup(__trusted_listeners, GINT_TO_POINTER(local_port_id));
-
-               if (item == NULL && trusted_item == NULL) {
-                       _LOGE("[MESSAGE_PORT_ERROR_PORT_NOT_FOUND] The local port ID (%d) is not registered.", local_port_id);
-                       return MESSAGE_PORT_ERROR_PORT_NOT_FOUND;
-               }
        }
-
        _SECURE_LOGI("Send a trusted message to (%s):(%s) - (%d).", remote_app_id, remote_port, local_port_id);
+
        pthread_mutex_lock(&mutex);
-       ret = messageport_send_bidirectional_trusted_message(local_port_id, remote_app_id, remote_port, message);
+       ret = send_bidirectional_message(local_port_id, remote_app_id, remote_port, true, message);
        pthread_mutex_unlock(&mutex);
 
-       return convert_to_tizen_error((messageport_error_e)ret);
+       return ret;
 }
 
 int message_port_add_registered_cb(const char *remote_app_id, const char *remote_port, bool is_trusted, message_port_registration_event_cb registered_cb, void *user_data, int *watcher_id)
 {
-       int ret = MESSAGE_PORT_ERROR_NONE;
+       int ret;
 
        if (watcher_id == NULL || remote_app_id == NULL || remote_port == NULL ||
                        registered_cb == NULL) {
@@ -294,18 +211,17 @@ int message_port_add_registered_cb(const char *remote_app_id, const char *remote
                return MESSAGE_PORT_ERROR_INVALID_PARAMETER;
        }
 
-       ret = messageport_add_registered_cb(remote_app_id, remote_port, is_trusted, registered_cb, user_data, watcher_id);
-       if (ret != MESSAGE_PORT_ERROR_NONE) {
+       ret = watch_remote_port(watcher_id, remote_app_id, remote_port, is_trusted, registered_cb, NULL, user_data);
+       if (ret != MESSAGE_PORT_ERROR_NONE)
                _SECURE_LOGI("add registered callback fail (%d).", ret);
-               return convert_to_tizen_error((messageport_error_e)ret);
-       }
 
-       return MESSAGE_PORT_ERROR_NONE;
+
+       return ret;
 }
 
 int message_port_add_unregistered_cb(const char *remote_app_id, const char *remote_port, bool is_trusted, message_port_registration_event_cb unregistered_cb, void *user_data, int *watcher_id)
 {
-       int ret = MESSAGE_PORT_ERROR_NONE;
+       int ret;
 
        if (watcher_id == NULL || remote_app_id == NULL || remote_port == NULL ||
                        unregistered_cb == NULL) {
@@ -313,13 +229,12 @@ int message_port_add_unregistered_cb(const char *remote_app_id, const char *remo
                return MESSAGE_PORT_ERROR_INVALID_PARAMETER;
        }
 
-       ret = messageport_add_unregistered_cb(remote_app_id, remote_port, is_trusted, unregistered_cb, user_data, watcher_id);
-       if (ret != MESSAGE_PORT_ERROR_NONE) {
+       ret = watch_remote_port(watcher_id, remote_app_id, remote_port, is_trusted, NULL, unregistered_cb, user_data);
+       if (ret != MESSAGE_PORT_ERROR_NONE)
                _SECURE_LOGI("add registered callback fail (%d).", ret);
-               return convert_to_tizen_error((messageport_error_e)ret);
-       }
 
-       return MESSAGE_PORT_ERROR_NONE;
+
+       return ret;
 }
 
 int message_port_remove_registration_event_cb(int watcher_id)
@@ -330,6 +245,6 @@ int message_port_remove_registration_event_cb(int watcher_id)
                return MESSAGE_PORT_ERROR_INVALID_PARAMETER;
        }
 
-       ret = messageport_remove_registration_event_cb(watcher_id);
-       return convert_to_tizen_error((messageport_error_e)ret);
+       ret = remove_registration_event_cb(watcher_id);
+       return ret;
 }
diff --git a/src/message_port_common.c b/src/message_port_common.c
new file mode 100755 (executable)
index 0000000..4758751
--- /dev/null
@@ -0,0 +1,345 @@
+/*
+ * Copyright (c) 2017 Samsung Electronics Co., Ltd All Rights Reserved
+ *
+ * Licensed under the Apache License, Version 2.0 (the License);
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an AS IS BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#define _GNU_SOURCE
+
+#include <bundle.h>
+#include <bundle_internal.h>
+#include <pkgmgr-info.h>
+#include <aul.h>
+
+#include <sys/socket.h>
+#include <stdlib.h>
+#include <stdio.h>
+#include <glib.h>
+#include <gio/gio.h>
+#include <openssl/md5.h>
+#include <gio/gio.h>
+#include <pthread.h>
+#include <glib-unix.h>
+#include <poll.h>
+
+#include "message_port_log.h"
+#include "message_port.h"
+#include "message_port_common.h"
+
+#define MAX_PACKAGE_STR_SIZE 512
+#define MAX_RETRY_CNT 10
+
+static const int MAX_MESSAGE_SIZE = 16 * 1024;
+
+int write_socket(int fd,
+               const char *buffer,
+               unsigned int nbytes,
+               unsigned int *bytes_write,
+               int *sequence)
+{
+#define SEND_TIMEOUT 500 /* milliseconds */
+
+       unsigned int left = nbytes;
+       ssize_t nb;
+       int retry_cnt = 0;
+       struct pollfd fds[1];
+       int ret;
+
+       *sequence += 1;
+       *bytes_write = 0;
+
+       fds[0].fd = fd;
+       fds[0].events = POLLOUT;
+       fds[0].revents = 0;
+
+       ret = poll(fds, 1, SEND_TIMEOUT);
+       if (ret == 0) {
+               LOGE("write_socket: : fd %d poll timeout", fd);
+               return MESSAGE_PORT_ERROR_RESOURCE_UNAVAILABLE;
+       }
+
+       while (left && (retry_cnt < MAX_RETRY_CNT)) {
+               nb = write(fd, buffer, left);
+               if (nb == -1) {
+                       if (errno == EINTR) {
+                               LOGE("write_socket: EINTR error continue ...");
+                               retry_cnt++;
+                               continue;
+                       }
+                       LOGE("write_socket: ...error fd %d: errno %d\n", fd, errno);
+
+                       if (errno == EWOULDBLOCK || errno == EAGAIN)
+                               return MESSAGE_PORT_ERROR_RESOURCE_UNAVAILABLE;
+
+                       return MESSAGE_PORT_ERROR_IO_ERROR;
+               }
+
+               left -= nb;
+               buffer += nb;
+               *bytes_write += nb;
+               retry_cnt = 0;
+       }
+       return MESSAGE_PORT_ERROR_NONE;
+}
+
+int write_string_to_socket(int fd,
+               const char *buffer,
+               int string_len,
+               unsigned int *bytes_write,
+               int *sequence)
+{
+       int ret;
+
+       ret = write_socket(fd, (char *)&string_len, sizeof(string_len),
+                       bytes_write, sequence);
+       if (ret != MESSAGE_PORT_ERROR_NONE) {
+               _LOGE("write string_len fail");
+               return ret;
+       }
+
+       if (string_len > 0) {
+               ret = write_socket(fd, buffer, string_len, bytes_write, sequence);
+               if (ret != MESSAGE_PORT_ERROR_NONE) {
+                       _LOGE("wirte buffer fail");
+                       return ret;
+               }
+       } else {
+               *sequence += 1;
+       }
+
+       return MESSAGE_PORT_ERROR_NONE;
+}
+
+int read_socket(int fd,
+               char *buffer,
+               unsigned int nbytes,
+               unsigned int *bytes_read)
+{
+       unsigned int left = nbytes;
+       ssize_t nb;
+       int retry_cnt = 0;
+       const struct timespec TRY_SLEEP_TIME = { 0, 500 * 1000 * 1000 };
+
+       *bytes_read = 0;
+       while (left && (retry_cnt < MAX_RETRY_CNT)) {
+               nb = read(fd, buffer, left);
+               if (nb == 0) {
+                       LOGE("read_socket: ...read EOF, socket closed %d: nb %d\n", fd, nb);
+                       return MESSAGE_PORT_ERROR_IO_ERROR;
+               } else if (nb == -1) {
+                       /*  wrt(nodejs) could change socket to none-blocking socket :-( */
+                       if (errno == EINTR || errno == EAGAIN || errno == EWOULDBLOCK) {
+                               LOGE("read_socket: %d errno, sleep and retry ...", errno);
+                               retry_cnt++;
+                               nanosleep(&TRY_SLEEP_TIME, 0);
+                               continue;
+                       }
+                       LOGE("read_socket: ...error fd %d: errno %d\n", fd, errno);
+                       return MESSAGE_PORT_ERROR_IO_ERROR;
+               }
+
+               left -= nb;
+               buffer += nb;
+               *bytes_read += nb;
+               retry_cnt = 0;
+       }
+       return MESSAGE_PORT_ERROR_NONE;
+}
+
+int read_string_from_socket(int fd, char **buffer, int *string_len)
+{
+       unsigned int nb;
+       if (read_socket(fd, (char *)string_len, sizeof(*string_len), &nb) != MESSAGE_PORT_ERROR_NONE) {
+               LOGE("read socket fail");
+               return MESSAGE_PORT_ERROR_IO_ERROR;
+       }
+       if (*string_len > 0 && *string_len < MAX_MESSAGE_SIZE) {
+               *buffer = (char *)calloc(*string_len, sizeof(char));
+               if (*buffer == NULL) {
+                       LOGE("Out of memory.");
+                       return MESSAGE_PORT_ERROR_IO_ERROR;
+               }
+               if (read_socket(fd, *buffer, *string_len, &nb) != MESSAGE_PORT_ERROR_NONE) {
+                       LOGE("read socket fail");
+                       return MESSAGE_PORT_ERROR_IO_ERROR;
+               }
+       } else {
+               LOGE("Invalid string len %d", *string_len);
+               return MESSAGE_PORT_ERROR_IO_ERROR;
+       }
+       return MESSAGE_PORT_ERROR_NONE;
+}
+
+static int __dbus_init(void)
+{
+       bool ret = false;
+       GError *error = NULL;
+
+       gdbus_conn = g_bus_get_sync(G_BUS_TYPE_SESSION, NULL, &error);
+       if (gdbus_conn == NULL) {
+               if (error != NULL) {
+                       _LOGE("Failed to get dbus [%s]", error->message);
+                       g_error_free(error);
+               }
+               goto out;
+       }
+
+       ret = true;
+
+out:
+       if (!gdbus_conn)
+               g_object_unref(gdbus_conn);
+
+       return ret;
+
+}
+
+bool initialize_common(void)
+{
+
+#if !GLIB_CHECK_VERSION(2, 35, 0)
+       g_type_init();
+#endif
+
+       int pid = getpid();
+       int ret = 0;
+       char buffer[MAX_PACKAGE_STR_SIZE] = {0, };
+
+       ret = aul_app_get_appid_bypid(pid, buffer, sizeof(buffer));
+       retvm_if(ret != AUL_R_OK, false, "Failed to get the application ID: %d", ret);
+
+       app_id = strdup(buffer);
+       retvm_if(!app_id, false, "Malloc failed");
+       _LOGI("init : %s", app_id);
+
+       if (!__dbus_init())
+               return false;
+       initialized_common = true;
+
+       return true;
+}
+
+bool is_preloaded(const char *local_appid, const char *remote_appid)
+{
+       _LOGD("IsPreloaded");
+
+       bool preload_local = false;
+       bool preload_remote = false;
+
+       pkgmgrinfo_appinfo_h handle = NULL;
+       int ret = pkgmgrinfo_appinfo_get_usr_appinfo(local_appid, getuid(), &handle);
+       if (ret != PMINFO_R_OK) {
+               _LOGE("Failed to get the appinfo. %d", ret);
+               pkgmgrinfo_appinfo_destroy_appinfo(handle);
+               return false;
+       }
+       ret = pkgmgrinfo_appinfo_is_preload(handle, &preload_local);
+       if (ret != PMINFO_R_OK) {
+               _LOGE("Failed to check the preloaded application. %d", ret);
+               pkgmgrinfo_appinfo_destroy_appinfo(handle);
+               return false;
+       }
+       pkgmgrinfo_appinfo_destroy_appinfo(handle);
+
+       ret = pkgmgrinfo_appinfo_get_usr_appinfo(remote_appid, getuid(), &handle);
+       if (ret != PMINFO_R_OK) {
+               _LOGE("Failed to get the appinfo. %d", ret);
+               pkgmgrinfo_appinfo_destroy_appinfo(handle);
+               return false;
+       }
+       ret = pkgmgrinfo_appinfo_is_preload(handle, &preload_remote);
+       if (ret != PMINFO_R_OK) {
+               _LOGE("Failed to check the preloaded application. %d", ret);
+               pkgmgrinfo_appinfo_destroy_appinfo(handle);
+               return false;
+       }
+
+       if (preload_local && preload_remote) {
+               pkgmgrinfo_appinfo_destroy_appinfo(handle);
+               return true;
+       }
+       pkgmgrinfo_appinfo_destroy_appinfo(handle);
+       return false;
+}
+
+int check_certificate(const char *local_appid, const char *remote_appid)
+{
+       _LOGD("CheckCertificate");
+
+       pkgmgrinfo_cert_compare_result_type_e res;
+       int ret = pkgmgrinfo_pkginfo_compare_usr_app_cert_info(local_appid, remote_appid, getuid(), &res);
+       if (ret < 0) {
+               _LOGE(":CheckCertificate() Failed");
+               return MESSAGE_PORT_ERROR_IO_ERROR;
+       }
+       if (res != PMINFO_CERT_COMPARE_MATCH) {
+               _LOGE("CheckCertificate() Failed : MESSAGE_PORT_ERROR_CERTIFICATE_NOT_MATCH");
+               return MESSAGE_PORT_ERROR_CERTIFICATE_NOT_MATCH;
+       }
+
+       return MESSAGE_PORT_ERROR_NONE;
+}
+
+char *get_encoded_name(const char *remote_app_id, const char *port_name, bool is_trusted)
+{
+
+       int prefix_len = strlen(MESSAGEPORT_BUS_NAME_PREFIX);
+       int postfix_len = 1;
+       char *postfix = is_trusted ? "1" : "0";
+
+       unsigned char c[MD5_DIGEST_LENGTH] = {0};
+       char *md5_interface = NULL;
+       char *temp;
+       int index = 0;
+       MD5_CTX mdContext;
+       int encoded_bus_name_len = prefix_len + postfix_len + (MD5_DIGEST_LENGTH * 2) + 2;
+       int bus_name_len = strlen(remote_app_id) + strlen(port_name) + 2;
+       char *bus_name = (char *)calloc(bus_name_len, sizeof(char));
+       if (bus_name == NULL) {
+               _LOGE("bus_name calloc failed");
+               return 0;
+       }
+
+       snprintf(bus_name, bus_name_len, "%s_%s", remote_app_id, port_name);
+
+       MD5_Init(&mdContext);
+       MD5_Update(&mdContext, bus_name, bus_name_len);
+       MD5_Final(c, &mdContext);
+
+       md5_interface = (char *)calloc(encoded_bus_name_len , sizeof(char));
+       if (md5_interface == NULL) {
+               if (bus_name)
+                       free(bus_name);
+
+               _LOGE("md5_interface calloc failed!!");
+               return 0;
+       }
+
+       snprintf(md5_interface, encoded_bus_name_len, "%s", MESSAGEPORT_BUS_NAME_PREFIX);
+       temp = md5_interface;
+       temp += prefix_len;
+
+       for (index = 0; index < MD5_DIGEST_LENGTH; index++) {
+               snprintf(temp, 3, "%02x", c[index]);
+               temp += 2;
+       }
+
+       if (postfix && postfix_len > 0)
+               snprintf(temp, encoded_bus_name_len - (temp - md5_interface), "%s", postfix);
+       if (bus_name)
+               free(bus_name);
+
+       _LOGD("encoded_bus_name : %s ", md5_interface);
+
+       return md5_interface;
+}
diff --git a/src/message_port_internal.c b/src/message_port_internal.c
deleted file mode 100644 (file)
index 258b41a..0000000
+++ /dev/null
@@ -1,41 +0,0 @@
-/*
- * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
- *
- * Licensed under the Apache License, Version 2.0 (the License);
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an AS IS BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-#include "message_port.h"
-#include "message_port_internal.h"
-
-int convert_to_tizen_error(messageport_error_e error)
-{
-       switch (error) {
-       case MESSAGEPORT_ERROR_NONE:
-               return MESSAGE_PORT_ERROR_NONE;
-       case MESSAGEPORT_ERROR_IO_ERROR:
-               return MESSAGE_PORT_ERROR_IO_ERROR;
-       case MESSAGEPORT_ERROR_OUT_OF_MEMORY:
-               return MESSAGE_PORT_ERROR_OUT_OF_MEMORY;
-       case MESSAGEPORT_ERROR_INVALID_PARAMETER:
-               return MESSAGE_PORT_ERROR_INVALID_PARAMETER;
-       case MESSAGEPORT_ERROR_MESSAGEPORT_NOT_FOUND:
-               return MESSAGE_PORT_ERROR_PORT_NOT_FOUND;
-       case MESSAGEPORT_ERROR_CERTIFICATE_NOT_MATCH:
-               return MESSAGE_PORT_ERROR_CERTIFICATE_NOT_MATCH;
-       case MESSAGEPORT_ERROR_MAX_EXCEEDED:
-               return MESSAGE_PORT_ERROR_MAX_EXCEEDED;
-       case MESSAGEPORT_ERROR_RESOURCE_UNAVAILABLE:
-               return MESSAGE_PORT_ERROR_RESOURCE_UNAVAILABLE;
-       default:
-               return error;
-       }
-}
diff --git a/src/message_port_local.c b/src/message_port_local.c
new file mode 100755 (executable)
index 0000000..39ad094
--- /dev/null
@@ -0,0 +1,1047 @@
+/*
+ * Copyright (c) 2017 Samsung Electronics Co., Ltd All Rights Reserved
+ *
+ * Licensed under the Apache License, Version 2.0 (the License);
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an AS IS BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#define _GNU_SOURCE
+
+#include <bundle.h>
+#include <bundle_internal.h>
+#include <aul.h>
+
+#include <sys/socket.h>
+#include <stdlib.h>
+#include <stdio.h>
+#include <pthread.h>
+
+#include <glib.h>
+#include <gio/gio.h>
+#include <gio/gunixfdlist.h>
+#include <glib-unix.h>
+
+#include "message_port_log.h"
+#include "message_port.h"
+#include "message_port_remote.h"
+#include "message_port_common.h"
+
+#define MAX_RETRY_CNT 10
+#define SOCK_PAIR_SENDER 0
+#define SOCK_PAIR_RECEIVER 1
+
+static bool _initialized = false;
+static GHashTable *__remote_app_info;
+static GHashTable *__registered_callback_info_hash;
+static const int MAX_MESSAGE_SIZE = 16 * 1024;
+
+enum __certificate_info_type {
+       UNKNOWN = 0,
+       CERTIFICATE_MATCH,
+       CERTIFICATE_NOT_MATCH,
+};
+
+typedef struct message_port_remote_port_info {
+       char *remote_app_id;
+       int certificate_info;
+       GList *port_list;
+} message_port_remote_app_info_s;
+
+typedef struct port_list_info {
+       message_port_remote_app_info_s *remote_app_info;
+       char *port_name;
+       char *encoded_bus_name;
+       bool is_trusted;
+       int send_sock_fd;
+       bool exist;
+       GIOChannel *gio_read;
+       int g_src_id;
+       GList *delayed_message_list;
+       unsigned int delayed_message_size;
+       int delay_src_id;
+} port_list_info_s;
+
+typedef struct registered_callback_info {
+       char *remote_app_id;
+       char *remote_port;
+       bool is_trusted;
+       int watcher_id;
+       void *user_data;
+       message_port_registration_event_cb registered_cb;
+       message_port_registration_event_cb unregistered_cb;
+} registered_callback_info_s;
+
+enum transmission_sequence {
+       SEQUENCE_START = 0,
+       SEQUENCE_PORT_LEN,
+       SEQUENCE_PORT_NAME,
+       SEQUENCE_BIDIRECTION,
+       SEQUENCE_TRUSTED,
+       SEQUENCE_DTAT_LEN,
+       SEQUENCE_DATA,
+       SEQUENCE_END
+};
+
+typedef struct delay_message {
+       unsigned int size;
+       unsigned int sent_bytes;
+       int sequence;
+       int local_port_len;
+       char *local_port_name;
+       bool is_bidirection;
+       bool local_trusted;
+       int data_len;
+       bundle_raw *data;
+} delay_message_info_s;
+
+extern pthread_mutex_t mutex;
+
+static void __free_delay_message_info(delay_message_info_s *message)
+{
+       if (message != NULL) {
+               FREE_AND_NULL(message->local_port_name);
+               FREE_AND_NULL(message->data);
+               FREE_AND_NULL(message);
+       }
+}
+
+static void __free_list_delay_message_info(gpointer data)
+{
+       delay_message_info_s *message = (delay_message_info_s *)data;
+
+       if (message != NULL)
+               __free_delay_message_info(message);
+}
+
+static void __clear_disconnect_socket(port_list_info_s *port_info)
+{
+       GError *error = NULL;
+
+       if (port_info == NULL)
+               return;
+
+       if (port_info->gio_read != NULL) {
+               g_io_channel_shutdown(port_info->gio_read, TRUE, &error);
+               if (error) {
+                       _LOGE("g_io_channel_shutdown error : %s", error->message);
+                       g_error_free(error);
+               }
+               g_io_channel_unref(port_info->gio_read);
+               port_info->gio_read = NULL;
+       }
+
+       if (port_info->g_src_id != 0) {
+               g_source_remove(port_info->g_src_id);
+               port_info->g_src_id = 0;
+       }
+
+       if (port_info->delay_src_id != 0) {
+               g_source_remove(port_info->delay_src_id);
+               port_info->delay_src_id = 0;
+       }
+
+       if (port_info->delayed_message_list != NULL) {
+               g_list_free_full(port_info->delayed_message_list, __free_list_delay_message_info);
+               /* can be reused */
+               port_info->delayed_message_list = NULL;
+       }
+
+       port_info->delayed_message_size = 0;
+       port_info->send_sock_fd = 0;
+}
+
+/* LCOV_EXCL_START */
+static void __free_port_info(gpointer data)
+{
+       port_list_info_s *port_info = (port_list_info_s *)data;
+       message_port_remote_app_info_s *remote_app_info;
+
+       if (port_info == NULL)
+               return;
+
+       remote_app_info = port_info->remote_app_info;
+
+       _LOGI("__free_port_info : remote_app_id : %s port_name : %s",
+                       remote_app_info->remote_app_id,
+                       port_info->port_name);
+
+       remote_app_info->port_list = g_list_remove(remote_app_info->port_list,
+                       port_info);
+
+       __clear_disconnect_socket(port_info);
+
+       if (port_info->encoded_bus_name)
+               free(port_info->encoded_bus_name);
+       if (port_info->port_name)
+               free(port_info->port_name);
+
+       free(port_info);
+
+       if (g_list_length(remote_app_info->port_list) == 0) {
+               g_hash_table_remove(__remote_app_info,
+                               remote_app_info->remote_app_id);
+       }
+}
+/* LCOV_EXCL_STOP */
+
+/* LCOV_EXCL_START */
+static void __hash_destory_remote_value(gpointer data)
+{
+       message_port_remote_app_info_s *mri = (message_port_remote_app_info_s *)data;
+       if (mri) {
+               FREE_AND_NULL(mri->remote_app_id);
+               if (mri->port_list)
+                       g_list_free_full(mri->port_list, __free_port_info);
+
+               free(mri);
+       }
+}
+/* LCOV_EXCL_STOP */
+
+static void __registered_callback_info_free(gpointer data)
+{
+       registered_callback_info_s *callback_info = (registered_callback_info_s *)data;
+       if (callback_info == NULL)
+               return;
+
+       if (callback_info->remote_app_id)
+               free(callback_info->remote_app_id);
+
+       if (callback_info->remote_port)
+               free(callback_info->remote_port);
+
+       free(callback_info);
+}
+
+static bool __initialize(void)
+{
+       if (!initialized_common) {
+               if (!initialize_common())
+                       return MESSAGE_PORT_ERROR_IO_ERROR;
+       }
+
+       if (__remote_app_info == NULL) {
+               __remote_app_info = g_hash_table_new_full(g_str_hash, g_str_equal, NULL, __hash_destory_remote_value);
+               retvm_if(!__remote_app_info, false, "fail to create __remote_app_info");
+       }
+
+       if (__registered_callback_info_hash == NULL) {
+               __registered_callback_info_hash = g_hash_table_new_full(g_direct_hash,  g_direct_equal, NULL, __registered_callback_info_free);
+               retvm_if(!__registered_callback_info_hash, false, "fail to create __registered_callback_info_hash");
+       }
+
+       _initialized = true;
+
+       return true;
+}
+
+static int __remote_port_compare_cb(gconstpointer a, gconstpointer b)
+{
+       port_list_info_s *key1 = (port_list_info_s *)a;
+       port_list_info_s *key2 = (port_list_info_s *)b;
+
+       if (key1->is_trusted == key2->is_trusted)
+               return strcmp(key1->port_name, key2->port_name);
+
+       return 1;
+}
+
+static port_list_info_s *__set_remote_port_info(const char *remote_app_id, const char *remote_port, bool is_trusted)
+{
+       int ret_val = MESSAGE_PORT_ERROR_NONE;
+       port_list_info_s *port_info = (port_list_info_s *)calloc(1, sizeof(port_list_info_s));
+
+       if (!port_info) {
+               ret_val = MESSAGE_PORT_ERROR_OUT_OF_MEMORY;
+               goto out;
+       }
+       port_info->port_name = strdup(remote_port);
+       if (!port_info->port_name) {
+               ret_val = MESSAGE_PORT_ERROR_OUT_OF_MEMORY;
+               goto out;
+       }
+       port_info->is_trusted = is_trusted;
+       port_info->encoded_bus_name = get_encoded_name(remote_app_id, remote_port, is_trusted);
+       if (port_info->encoded_bus_name == NULL) {
+               ret_val = MESSAGE_PORT_ERROR_OUT_OF_MEMORY;
+               goto out;
+       }
+       port_info->send_sock_fd = 0;
+out:
+       if (ret_val != MESSAGE_PORT_ERROR_NONE) {
+               if (port_info) {
+                       FREE_AND_NULL(port_info->port_name);
+                       FREE_AND_NULL(port_info->encoded_bus_name);
+                       free(port_info);
+               }
+               return NULL;
+       }
+       return port_info;
+}
+
+static message_port_remote_app_info_s *__set_remote_app_info(const char *remote_app_id, const char *remote_port, bool is_trusted)
+{
+       message_port_remote_app_info_s *remote_app_info = NULL;
+       int ret_val = MESSAGE_PORT_ERROR_NONE;
+
+       remote_app_info = (message_port_remote_app_info_s *)calloc(1, sizeof(message_port_remote_app_info_s));
+       if (!remote_app_info) {
+               ret_val = MESSAGE_PORT_ERROR_OUT_OF_MEMORY;
+               goto out;
+       }
+
+       remote_app_info->remote_app_id = strdup(remote_app_id);
+       if (remote_app_info->remote_app_id == NULL) {
+               ret_val = MESSAGE_PORT_ERROR_OUT_OF_MEMORY;;
+               goto out;
+       }
+
+out:
+       if (ret_val != MESSAGE_PORT_ERROR_NONE) {
+               if (remote_app_info) {
+                       FREE_AND_NULL(remote_app_info->remote_app_id);
+                       FREE_AND_NULL(remote_app_info);
+               }
+               return NULL;
+       }
+       return remote_app_info;
+}
+
+static gboolean __socket_disconnect_handler(GIOChannel *gio,
+               GIOCondition cond,
+               gpointer data)
+{
+       _LOGI("__socket_disconnect_handler %d", cond);
+       __free_port_info(data);
+
+       return FALSE;
+}
+
+static int __get_remote_port_info(const char *remote_app_id, const char *remote_port, bool is_trusted,
+               message_port_remote_app_info_s **mri, port_list_info_s **pli)
+{
+       message_port_remote_app_info_s *remote_app_info = NULL;
+       port_list_info_s port_info;
+       GList *cb_list = NULL;
+       int ret_val = MESSAGE_PORT_ERROR_NONE;
+
+       remote_app_info = (message_port_remote_app_info_s *)g_hash_table_lookup(__remote_app_info, remote_app_id);
+
+       if (remote_app_info == NULL) {
+               remote_app_info = __set_remote_app_info(remote_app_id, remote_port, is_trusted);
+
+               if (remote_app_info == NULL) {
+                       ret_val = MESSAGE_PORT_ERROR_OUT_OF_MEMORY;
+                       goto out;
+               }
+               g_hash_table_insert(__remote_app_info, remote_app_info->remote_app_id, remote_app_info);
+       }
+       *mri = remote_app_info;
+
+       port_info.port_name = strdup(remote_port);
+       if (port_info.port_name == NULL) {
+               ret_val = MESSAGE_PORT_ERROR_OUT_OF_MEMORY;
+               goto out;
+       }
+       port_info.is_trusted = is_trusted;
+       cb_list = g_list_find_custom(remote_app_info->port_list, &port_info,
+                                       (GCompareFunc)__remote_port_compare_cb);
+       if (port_info.port_name)
+               free(port_info.port_name);
+       if (cb_list == NULL) {
+               port_list_info_s *tmp = __set_remote_port_info(remote_app_id, remote_port, is_trusted);
+               if (tmp == NULL) {
+                       ret_val = MESSAGE_PORT_ERROR_OUT_OF_MEMORY;
+                       goto out;
+               }
+               remote_app_info->port_list = g_list_append(remote_app_info->port_list, tmp);
+               tmp->remote_app_info = remote_app_info;
+               *pli = tmp;
+       } else {
+               *pli = (port_list_info_s *)cb_list->data;
+       }
+out:
+
+       return ret_val;
+}
+
+int check_remote_port(const char *remote_app_id, const char *remote_port, bool is_trusted, bool *exist)
+{
+       _LOGD("Check a remote port : [%s:%s]", remote_app_id, remote_port);
+
+       GVariant *result = NULL;
+       GError *err = NULL;
+       int ret_val = MESSAGE_PORT_ERROR_NONE;
+       char *bus_name = NULL;
+       message_port_remote_app_info_s *remote_app_info = NULL;
+       port_list_info_s *port_info = NULL;
+       int local_reg_id = 0;
+       message_port_local_port_info_s *mi = NULL;
+       gboolean name_exist = false;
+
+       if (!_initialized) {
+               if (!__initialize())
+                       return MESSAGE_PORT_ERROR_IO_ERROR;
+       }
+
+       _LOGD("remote_app_id, app_id :[%s : %s] ", remote_app_id, app_id);
+
+       ret_val = __get_remote_port_info(remote_app_id, remote_port, is_trusted, &remote_app_info, &port_info);
+       if (ret_val != MESSAGE_PORT_ERROR_NONE)
+               return ret_val;
+
+       /* self check */
+       if (strcmp(remote_app_id, app_id) == 0) {
+
+               _LOGD("__is_local_port_registed ");
+               if (!is_local_port_registed(remote_port, is_trusted, &local_reg_id, &mi))
+                       *exist = false;
+               else
+                       *exist = true;
+
+               _LOGD("__is_local_port_registed : %d ", *exist);
+               return MESSAGE_PORT_ERROR_NONE;
+       }
+
+       port_info->exist = false;
+       bus_name = port_info->encoded_bus_name;
+
+       result = g_dbus_connection_call_sync(
+                       gdbus_conn,
+                       DBUS_SERVICE_DBUS,
+                       DBUS_PATH_DBUS,
+                       DBUS_INTERFACE_DBUS,
+                       "NameHasOwner",
+                       g_variant_new("(s)", bus_name),
+                       G_VARIANT_TYPE("(b)"),
+                       G_DBUS_CALL_FLAGS_NONE,
+                       -1,
+                       NULL,
+                       &err);
+
+       if (err || (result == NULL)) {
+               if (err) {
+                       _LOGE("No reply. error = %s", err->message);
+                       g_error_free(err);
+               }
+               ret_val = MESSAGE_PORT_ERROR_RESOURCE_UNAVAILABLE;
+       } else {
+               g_variant_get(result, "(b)", &name_exist);
+
+               if (!name_exist) {
+                       _LOGI("Name not exist %s", bus_name);
+                       *exist = false;
+                       ret_val = MESSAGE_PORT_ERROR_NONE;
+               } else {
+
+                       if (is_trusted) {
+                               if (remote_app_info->certificate_info != CERTIFICATE_MATCH) {
+                                       if (!is_preloaded(app_id, remote_app_id)) {
+                                               if (check_certificate(app_id, remote_app_id) != MESSAGE_PORT_ERROR_NONE) {
+                                                       ret_val = MESSAGE_PORT_ERROR_CERTIFICATE_NOT_MATCH;
+                                                       goto out;
+                                               }
+                                       }
+                                       remote_app_info->certificate_info = CERTIFICATE_MATCH;
+                               }
+                       }
+                       port_info->exist = true;
+                       *exist = true;
+                       ret_val = MESSAGE_PORT_ERROR_NONE;
+               }
+       }
+out:
+       if (result)
+               g_variant_unref(result);
+
+       if (ret_val != MESSAGE_PORT_ERROR_NONE || !name_exist)
+               __free_port_info((gpointer)port_info);
+
+       return ret_val;
+}
+
+static int __send_delayed_message(int sockfd, delay_message_info_s *message)
+{
+       unsigned int nb = 0;
+       int sequence = message->sequence - 1;
+       int ret = MESSAGE_PORT_ERROR_NONE;
+       bool is_startline = true;
+       int offset = 0;
+
+       _LOGI("send_delayed_message : sockfd (%d) sequence(%d) sent byte(%d)",
+               sockfd, message->sequence, message->sent_bytes);
+
+       switch (message->sequence) {
+       case SEQUENCE_START:
+               sequence++;
+               is_startline = false;
+
+       case SEQUENCE_PORT_LEN:
+               if (is_startline)
+                       offset = message->sent_bytes;
+
+               ret = write_socket(sockfd, ((char *)&message->local_port_len) + offset,
+                               sizeof(message->local_port_len) - offset, &nb, &sequence);
+               if (ret != MESSAGE_PORT_ERROR_NONE) {
+                       _LOGE("write local_port_len fail");
+                       goto out;
+               }
+               offset = 0;
+               is_startline = false;
+
+       case SEQUENCE_PORT_NAME:
+               if (is_startline)
+                       offset = message->sent_bytes;
+
+               if (message->local_port_len > 0)
+                       ret = write_socket(sockfd, message->local_port_name + offset,
+                               message->local_port_len - offset , &nb, &sequence);
+               else
+                       sequence++;
+
+               if (ret != MESSAGE_PORT_ERROR_NONE) {
+                       _LOGE("write local_port fail");
+                       goto out;
+               }
+               offset = 0;
+               is_startline = false;
+
+       case SEQUENCE_BIDIRECTION:
+               if (is_startline)
+                       offset = message->sent_bytes;
+
+               ret = write_socket(sockfd, ((char *)&message->is_bidirection) + offset,
+                               sizeof(message->is_bidirection) - offset, &nb, &sequence);
+               if (ret != MESSAGE_PORT_ERROR_NONE) {
+                       _LOGE("write is_bidirection fail");
+                       goto out;
+               }
+               offset = 0;
+               is_startline = false;
+
+       case SEQUENCE_TRUSTED:
+               if (is_startline)
+                       offset = message->sent_bytes;
+
+               ret = write_socket(sockfd, ((char *)&message->local_trusted) + offset,
+                               sizeof(message->local_trusted) - offset, &nb, &sequence);
+               if (ret != MESSAGE_PORT_ERROR_NONE) {
+                       _LOGE("write local_trusted fail");
+                       goto out;
+               }
+               offset = 0;
+               is_startline = false;
+
+       case SEQUENCE_DTAT_LEN:
+               if (is_startline)
+                       offset = message->sent_bytes;
+
+               ret = write_socket(sockfd, ((char *)&message->data_len) + offset,
+                               sizeof(message->data_len) - offset, &nb, &sequence);
+               if (ret != MESSAGE_PORT_ERROR_NONE) {
+                       _LOGE("write data_len fail");
+                       goto out;
+               }
+               offset = 0;
+               is_startline = false;
+
+       case SEQUENCE_DATA:
+               if (is_startline)
+                       offset = message->sent_bytes;
+
+               ret = write_socket(sockfd, (char *)message->data + offset,
+                       message->data_len -offset, &nb, &sequence);
+
+               if (ret != MESSAGE_PORT_ERROR_NONE) {
+                       _LOGE("write data fail");
+                       goto out;
+               }
+               offset = 0;
+               is_startline = false;
+
+       default:
+               ret = MESSAGE_PORT_ERROR_NONE;
+
+       }
+
+out:
+       if (ret == MESSAGE_PORT_ERROR_RESOURCE_UNAVAILABLE) {
+               if (is_startline)
+                        message->sent_bytes += nb;
+               else
+                        message->sent_bytes = nb;
+
+               message->sequence = sequence;
+               _LOGE("send_delayed_message fail : sockfd (%d) sequence(%d) sent byte(%d)",
+                       sockfd, message->sequence, message->sent_bytes);
+       }
+
+       return ret;
+
+}
+
+static gboolean __process_delayed_message(gint fd, GIOCondition cond, gpointer data)
+{
+       port_list_info_s *port_info = (port_list_info_s *)data;
+       delay_message_info_s *message;
+       int ret;
+
+       if (port_info == NULL)
+               return G_SOURCE_REMOVE;
+
+       pthread_mutex_lock(&mutex);
+
+       if (port_info->delayed_message_list == NULL) {
+               port_info->delayed_message_size = 0;
+               port_info->delay_src_id = 0;
+               pthread_mutex_unlock(&mutex);
+               return G_SOURCE_REMOVE;
+       } else {
+               message = g_list_nth_data(port_info->delayed_message_list, 0);
+               ret = __send_delayed_message(port_info->send_sock_fd, message);
+
+               if (ret == MESSAGE_PORT_ERROR_RESOURCE_UNAVAILABLE) {
+                       pthread_mutex_unlock(&mutex);
+                       return G_SOURCE_CONTINUE;
+               } else if (ret == MESSAGE_PORT_ERROR_IO_ERROR) {
+                       __free_port_info((gpointer)port_info);
+                       pthread_mutex_unlock(&mutex);
+                       return G_SOURCE_REMOVE;
+               }
+
+               port_info->delayed_message_size -= message->size;
+
+               port_info->delayed_message_list = g_list_remove(port_info->delayed_message_list, message);
+               __free_delay_message_info(message);
+       }
+
+       pthread_mutex_unlock(&mutex);
+
+       return G_SOURCE_CONTINUE;
+}
+
+static int __insert_delayed_message(port_list_info_s *port_info,
+       int sequence,
+       bundle_raw *kb_data,
+       int data_len,
+       unsigned int sent_bytes,
+       const char *local_port,
+       bool local_trusted,
+       bool is_bidirection)
+{
+#define QUEUE_SIZE_MAX (1024 * 1024) /* 1MB per remote port (MAX) */
+
+       unsigned int tmp_size;
+       unsigned int message_size;
+       int ret = MESSAGE_PORT_ERROR_NONE;
+
+       if (port_info->delayed_message_size >= QUEUE_SIZE_MAX) {
+               _LOGE("cache fail : delayed_message_size (%d), count(%d)",
+                       port_info->delayed_message_size, g_list_length(port_info->delayed_message_list));
+               return MESSAGE_PORT_ERROR_RESOURCE_UNAVAILABLE;
+       }
+
+       delay_message_info_s *message = (delay_message_info_s *)calloc(1, sizeof(delay_message_info_s));
+       retvm_if(!message, MESSAGE_PORT_ERROR_OUT_OF_MEMORY, "Malloc failed");
+
+       message_size = sizeof(delay_message_info_s);
+
+       message->sequence = sequence;
+       tmp_size = strlen(local_port) + 1;
+       message_size += tmp_size;
+       message->local_port_len = tmp_size;
+       message->local_port_name = strdup(local_port);
+       if (message->local_port_name == NULL) {
+               _LOGE("local_port_name strdup fail");
+               ret = MESSAGE_PORT_ERROR_OUT_OF_MEMORY;
+               goto out;
+       }
+       message->is_bidirection = is_bidirection;
+       message->local_trusted = local_trusted;
+       message_size += data_len;
+       message->data_len = data_len;
+       message->data = (bundle_raw *)strdup((const char *)kb_data);
+       if (message->data == NULL) {
+               _LOGE("data strdup fail");
+               ret = MESSAGE_PORT_ERROR_OUT_OF_MEMORY;
+               goto out;
+       }
+
+
+       message->sent_bytes = sent_bytes;
+       message->size = message_size;
+       port_info->delayed_message_size += message_size;
+
+       port_info->delayed_message_list = g_list_append(port_info->delayed_message_list, message);
+
+       if (port_info->delay_src_id == 0) {
+                       port_info->delay_src_id = g_unix_fd_add_full(G_PRIORITY_DEFAULT,
+                                                       port_info->send_sock_fd, G_IO_OUT, __process_delayed_message,
+                                                       port_info, NULL);
+       }
+
+       _LOGE("inserted : pm(%s) fd(%d) ms(%d) ds(%d) dlc(%d) sqn(%d) sb (%d)",
+               port_info->port_name, port_info->send_sock_fd, message_size,
+               port_info->delayed_message_size,
+               g_list_length(port_info->delayed_message_list), sequence, sent_bytes);
+
+
+
+out:
+       if (ret != MESSAGE_PORT_ERROR_NONE)
+               __free_delay_message_info(message);
+
+       return ret;
+}
+
+static int __message_port_send_async(port_list_info_s *port_info, bundle *kb, const char *local_port,
+               bool local_trusted, bool is_bidirection)
+{
+       int ret = 0;
+       int data_len;
+       int local_port_len = 0;
+       unsigned int nb = 0;
+       bundle_raw *kb_data = NULL;
+       int sequence = SEQUENCE_START;
+
+       bundle_encode(kb, &kb_data, &data_len);
+       if (kb_data == NULL) {
+               _LOGE("bundle encode fail");
+               ret = MESSAGE_PORT_ERROR_INVALID_PARAMETER;
+               goto out;
+       }
+
+       if (data_len > MAX_MESSAGE_SIZE) {
+               _LOGE("bigger than max size\n");
+               ret = MESSAGE_PORT_ERROR_MAX_EXCEEDED;
+               goto out;
+       }
+
+       if (g_list_length(port_info->delayed_message_list) > 0) {
+               ret = MESSAGE_PORT_ERROR_RESOURCE_UNAVAILABLE;
+               _LOGE("There are messages in the delayed_message_list (count %d)",
+                       g_list_length(port_info->delayed_message_list));
+               goto out;
+       }
+
+       if (local_port != NULL)
+               local_port_len = strlen(local_port) + 1;
+
+       ret = write_string_to_socket(port_info->send_sock_fd, local_port,
+                       local_port_len, &nb, &sequence);
+       if (ret != MESSAGE_PORT_ERROR_NONE) {
+               _LOGE("write local_port fail");
+               goto out;
+       }
+
+       ret = write_socket(port_info->send_sock_fd, (char *)&is_bidirection,
+                       sizeof(is_bidirection), &nb, &sequence);
+       if (ret != MESSAGE_PORT_ERROR_NONE) {
+               _LOGE("write is_bidirection fail");
+               goto out;
+       }
+
+       ret = write_socket(port_info->send_sock_fd, (char *)&local_trusted,
+                       sizeof(local_trusted), &nb, &sequence);
+       if (ret != MESSAGE_PORT_ERROR_NONE) {
+               _LOGE("write local_trusted fail");
+               goto out;
+       }
+
+       ret = write_string_to_socket(port_info->send_sock_fd, (void *)kb_data,
+                       data_len, &nb, &sequence);
+       if (ret != MESSAGE_PORT_ERROR_NONE) {
+               _LOGE("write kb_data fail");
+               goto out;
+       }
+
+out:
+       if (ret == MESSAGE_PORT_ERROR_RESOURCE_UNAVAILABLE) {
+               ret = __insert_delayed_message(port_info, sequence, kb_data, data_len, nb,
+                       local_port, local_trusted, is_bidirection);
+               if (ret != MESSAGE_PORT_ERROR_NONE)
+                       ret = MESSAGE_PORT_ERROR_IO_ERROR;
+       }
+
+       if (kb_data)
+               free(kb_data);
+
+       return ret;
+}
+
+int send_message(const char *remote_appid, const char *remote_port,
+               const char *local_port, bool trusted_message, bool local_trusted, bool bi_dir, bundle *message)
+{
+
+       int ret = MESSAGE_PORT_ERROR_NONE;
+       GUnixFDList *fd_list = NULL;
+
+       int len = 0;
+       bundle_raw *raw = NULL;
+       char *bus_name = NULL;
+       char *interface_name = NULL;
+
+       message_port_remote_app_info_s *remote_app_info = NULL;
+       port_list_info_s *port_info = NULL;
+       GDBusMessage *msg = NULL;
+       GError *err = NULL;
+       GVariant *body = NULL;
+       int sock_pair[2] = {0,};
+       char buf[1024];
+
+       if (!_initialized) {
+               if (!__initialize())
+                       return MESSAGE_PORT_ERROR_IO_ERROR;
+       }
+
+       ret = __get_remote_port_info(remote_appid, remote_port, trusted_message, &remote_app_info, &port_info);
+       if (ret != MESSAGE_PORT_ERROR_NONE)
+               return ret;
+
+       if (port_info->exist == false) {
+               bool exist = false;
+               _LOGD("port exist check !!");
+               ret =  check_remote_port(remote_appid, remote_port, trusted_message, &exist);
+               if (ret != MESSAGE_PORT_ERROR_NONE)
+                       return ret;
+               else if (!exist)
+                       return MESSAGE_PORT_ERROR_PORT_NOT_FOUND;
+       }
+
+       if (port_info->send_sock_fd > 0) {
+               ret = __message_port_send_async(port_info, message,
+                               (local_port) ? local_port : "", local_trusted, bi_dir);
+       } else {
+
+               bus_name = port_info->encoded_bus_name;
+               interface_name = bus_name;
+
+               if (bundle_encode(message, &raw, &len) != BUNDLE_ERROR_NONE) {
+                       ret = MESSAGE_PORT_ERROR_INVALID_PARAMETER;
+                       goto out;
+               }
+
+               if (MAX_MESSAGE_SIZE < len) {
+                       _LOGE("The size of message (%d) has exceeded the maximum limit.", len);
+                       ret = MESSAGE_PORT_ERROR_MAX_EXCEEDED;
+                       goto out;
+               }
+
+               body = g_variant_new("(ssbbssbus)", app_id, (local_port) ? local_port : "", local_trusted, bi_dir,
+                               remote_appid, remote_port, trusted_message, len, raw);
+               if (strcmp(remote_appid, app_id) != 0) { /* self send */
+
+                       /*  if message-port fail to get socket pair, communicate using GDBus */
+                       if (aul_request_message_port_socket_pair(sock_pair) != AUL_R_OK) {
+                               _LOGE("error create socket pair");
+                       } else {
+
+                               _LOGI("sock pair : %d, %d",
+                                               sock_pair[SOCK_PAIR_SENDER], sock_pair[SOCK_PAIR_RECEIVER]);
+                               fd_list = g_unix_fd_list_new();
+                               g_unix_fd_list_append(fd_list, sock_pair[SOCK_PAIR_RECEIVER], &err);
+                               if (err != NULL) {
+                                       _LOGE("g_unix_fd_list_append [%s]", err->message);
+                                       ret = MESSAGE_PORT_ERROR_IO_ERROR;
+                                       g_error_free(err);
+                                       goto out;
+                               }
+
+                               port_info->send_sock_fd = sock_pair[SOCK_PAIR_SENDER];
+                               close(sock_pair[SOCK_PAIR_RECEIVER]);
+                               sock_pair[SOCK_PAIR_RECEIVER] = 0;
+
+                               port_info->gio_read = g_io_channel_unix_new(port_info->send_sock_fd);
+                               if (!port_info->gio_read) {
+                                       _LOGE("Error is %s\n", strerror_r(errno, buf, sizeof(buf)));
+                                       ret = MESSAGE_PORT_ERROR_IO_ERROR;
+                                       goto out;
+                               }
+
+                               port_info->g_src_id = g_io_add_watch(
+                                               port_info->gio_read,
+                                               G_IO_IN | G_IO_HUP,
+                                               __socket_disconnect_handler,
+                                               (gpointer)port_info);
+                               if (port_info->g_src_id == 0) {
+                                       _LOGE("fail to add watch on socket");
+                                       ret = MESSAGE_PORT_ERROR_IO_ERROR;
+                                       goto out;
+                               }
+
+                       }
+               }
+
+               msg = g_dbus_message_new_method_call(bus_name, MESSAGEPORT_OBJECT_PATH, interface_name, "send_message");
+               if (!msg) {
+                       _LOGE("Can't allocate new method call");
+                       ret = MESSAGE_PORT_ERROR_OUT_OF_MEMORY;
+                       goto out;
+               }
+
+               g_dbus_message_set_unix_fd_list(msg, fd_list);
+               g_dbus_message_set_body(msg, body);
+               g_dbus_connection_send_message(gdbus_conn, msg, G_DBUS_SEND_MESSAGE_FLAGS_NONE, NULL, &err);
+               if (err != NULL) {
+                       _LOGE("No reply. error = %s", err->message);
+                       g_error_free(err);
+                       ret = MESSAGE_PORT_ERROR_IO_ERROR;
+                       goto out;
+               }
+       }
+
+out:
+       if (msg)
+               g_object_unref(msg);
+       if (raw)
+               bundle_free_encoded_rawdata(&raw);
+       if (fd_list)
+               g_object_unref(fd_list);
+
+       if (ret != MESSAGE_PORT_ERROR_NONE) {
+               __free_port_info((gpointer)port_info);
+               if (sock_pair[SOCK_PAIR_SENDER])
+                       close(sock_pair[SOCK_PAIR_SENDER]);
+               if (sock_pair[SOCK_PAIR_RECEIVER])
+                       close(sock_pair[SOCK_PAIR_RECEIVER]);
+       }
+
+       return ret;
+}
+
+int send_bidirectional_message(int id, const char *remote_app_id, const char *remote_port,  bool trusted_message, bundle *message)
+{
+       message_port_local_port_info_s *local_info;
+       int ret = get_local_port_info(id, &local_info);
+       if (ret != MESSAGE_PORT_ERROR_NONE)
+               return ret;
+
+       _LOGD("bidirectional_message %s", local_info->port_name);
+       return send_message(remote_app_id, remote_port,
+                       local_info->port_name, trusted_message, local_info->is_trusted, true, message);
+}
+
+static void __name_registered(GDBusConnection *connection,
+               const gchar *name,
+               const gchar *name_owner,
+               gpointer user_data)
+{
+
+       registered_callback_info_s *info = (registered_callback_info_s *)user_data;
+       if (info == NULL) {
+               LOGE("NULL registered_callback_info");
+               return;
+       }
+
+       _LOGI("watcher_id : %d, appeared name : %s , name_owner : %s\n", info->watcher_id, name, name_owner);
+       if (info->registered_cb)
+               info->registered_cb(info->remote_app_id, info->remote_port, info->is_trusted, info->user_data);
+}
+
+static void __name_unregistered(GDBusConnection *connection,
+               const gchar *name,
+               gpointer user_data)
+{
+
+       registered_callback_info_s *info = (registered_callback_info_s *)user_data;
+       if (info == NULL) {
+               LOGE("NULL registered_callback_info");
+               return;
+       }
+
+       _LOGI("watcher_id : %d, vanished name : %s\n", info->watcher_id, name);
+       if (info->unregistered_cb)
+               info->unregistered_cb(info->remote_app_id, info->remote_port, info->is_trusted, info->user_data);
+}
+
+int watch_remote_port(int *watcher_id, const char *remote_app_id, const char *remote_port, bool trusted_remote_port, message_port_registration_event_cb registered_cb, message_port_registration_event_cb unregistered_cb, void *user_data)
+{
+       int ret_val = MESSAGE_PORT_ERROR_NONE;
+       message_port_remote_app_info_s *remote_app_info = NULL;
+       port_list_info_s *port_info = NULL;
+
+       if (!_initialized) {
+               if (!__initialize())
+                       return MESSAGE_PORT_ERROR_IO_ERROR;
+       }
+       _LOGI("remote_app_id, app_id :[%s : %s] ", remote_app_id, app_id);
+
+       ret_val = __get_remote_port_info(remote_app_id, remote_port, trusted_remote_port, &remote_app_info, &port_info);
+       if (ret_val != MESSAGE_PORT_ERROR_NONE) {
+               _LOGE("Failed to get remote_port_info %d", ret_val);
+               return ret_val;
+       }
+
+       registered_callback_info_s *registered_cb_info = (registered_callback_info_s *)calloc(1, sizeof(registered_callback_info_s));
+       retvm_if(!registered_cb_info, MESSAGE_PORT_ERROR_OUT_OF_MEMORY, "Malloc failed");
+
+       registered_cb_info->registered_cb = registered_cb;
+       registered_cb_info->unregistered_cb = unregistered_cb;
+       registered_cb_info->user_data = user_data;
+       registered_cb_info->remote_app_id = strdup(remote_app_info->remote_app_id);
+       if (registered_cb_info->remote_app_id == NULL) {
+               free(registered_cb_info);
+               _LOGE("Failed to alloc memory");
+               return MESSAGE_PORT_ERROR_OUT_OF_MEMORY;
+       }
+       registered_cb_info->remote_port = strdup(port_info->port_name);
+       if (registered_cb_info->remote_port == NULL) {
+               free(registered_cb_info->remote_app_id);
+               free(registered_cb_info);
+               _LOGE("Failed to alloc memory");
+               return MESSAGE_PORT_ERROR_OUT_OF_MEMORY;
+       }
+
+       registered_cb_info->watcher_id = g_bus_watch_name_on_connection(
+                       gdbus_conn,
+                       port_info->encoded_bus_name,
+                       G_BUS_NAME_WATCHER_FLAGS_NONE,
+                       __name_registered,
+                       __name_unregistered,
+                       registered_cb_info,
+                       NULL);
+       if (registered_cb_info->watcher_id == 0) {
+               free(registered_cb_info->remote_app_id);
+               free(registered_cb_info->remote_port);
+               free(registered_cb_info);
+               _LOGE("Failed to watch name");
+               return MESSAGE_PORT_ERROR_IO_ERROR;
+       }
+
+       g_hash_table_insert(__registered_callback_info_hash,
+                       GINT_TO_POINTER(registered_cb_info->watcher_id), registered_cb_info);
+
+       *watcher_id = registered_cb_info->watcher_id;
+       return MESSAGE_PORT_ERROR_NONE;
+}
+
+int remove_registration_event_cb(int watcher_id)
+{
+       registered_callback_info_s *registered_cb_info = NULL;
+       gboolean remove_result = FALSE;
+
+       if (watcher_id < 1)
+               return MESSAGE_PORT_ERROR_INVALID_PARAMETER;
+
+       registered_cb_info = g_hash_table_lookup(__registered_callback_info_hash, GINT_TO_POINTER(watcher_id));
+       if (registered_cb_info == NULL)
+               return MESSAGE_PORT_ERROR_INVALID_PARAMETER;
+
+       remove_result = g_hash_table_remove(__registered_callback_info_hash, GINT_TO_POINTER(watcher_id));
+       if (!remove_result)
+               return MESSAGE_PORT_ERROR_IO_ERROR;
+
+       g_bus_unwatch_name(watcher_id);
+
+       return MESSAGE_PORT_ERROR_NONE;
+}
+
diff --git a/src/message_port_remote.c b/src/message_port_remote.c
new file mode 100755 (executable)
index 0000000..a1b7128
--- /dev/null
@@ -0,0 +1,848 @@
+/*
+ * Copyright (c) 2017 Samsung Electronics Co., Ltd All Rights Reserved
+ *
+ * Licensed under the Apache License, Version 2.0 (the License);
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an AS IS BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#define _GNU_SOURCE
+
+#include <bundle.h>
+#include <bundle_internal.h>
+#include <aul.h>
+
+#include <sys/socket.h>
+#include <stdlib.h>
+#include <stdio.h>
+
+#include <glib.h>
+#include <gio/gio.h>
+#include <gio/gunixfdlist.h>
+#include <glib-unix.h>
+
+#include "message_port_log.h"
+#include "message_port_common.h"
+#include "message_port_remote.h"
+
+
+#define MAX_PACKAGE_STR_SIZE 512
+
+#define DBUS_RELEASE_NAME_REPLY_RELEASED        1 /* *< Service was released from the given name */
+#define DBUS_RELEASE_NAME_REPLY_NON_EXISTENT    2 /* *< The given name does not exist on the bus */
+#define DBUS_RELEASE_NAME_REPLY_NOT_OWNER       3 /* *< Service is not an owner of the given name */
+
+#define MAX_RETRY_CNT 10
+#define SOCK_PAIR_SENDER 0
+#define SOCK_PAIR_RECEIVER 1
+
+static bool _initialized = false;
+static GHashTable *__local_port_info;
+static GHashTable *__trusted_app_list_hash;
+static GHashTable *__callback_info_hash;
+static GHashTable *__sender_appid_hash;
+
+typedef struct message_port_pkt {
+       int remote_port_name_len;
+       char *remote_port_name;
+       bool is_bidirection;
+       bool is_trusted;
+       int data_len;
+       unsigned char *data;
+} message_port_pkt_s;
+
+typedef struct message_port_callback_info {
+       message_port_message_cb callback;
+       void *user_data;
+       int local_id;
+       char *remote_app_id;
+       GIOChannel *gio_read;
+       int g_src_id;
+} message_port_callback_info_s;
+
+static void __callback_info_free(gpointer data)
+{
+       message_port_callback_info_s *callback_info = (message_port_callback_info_s *)data;
+       GError *error = NULL;
+       if (callback_info == NULL)
+               return;
+
+       if (callback_info->remote_app_id)
+               FREE_AND_NULL(callback_info->remote_app_id);
+
+       if (callback_info->gio_read != NULL) {
+               g_io_channel_shutdown(callback_info->gio_read, TRUE, &error);
+               if (error) {
+                       _LOGE("g_io_channel_shutdown error : %s", error->message);
+                       g_error_free(error);
+               }
+               g_io_channel_unref(callback_info->gio_read);
+               callback_info->gio_read = NULL;
+       }
+
+       if (callback_info->g_src_id != 0) {
+               g_source_remove(callback_info->g_src_id);
+               callback_info->g_src_id = 0;
+       }
+
+       FREE_AND_NULL(callback_info);
+}
+
+static void __callback_info_free_by_info(message_port_callback_info_s *callback_info)
+{
+       GList *callback_info_list = g_hash_table_lookup(__callback_info_hash, GUINT_TO_POINTER(callback_info->local_id));
+       GList *find_list;
+
+       if (callback_info_list == NULL)
+               return;
+
+       find_list = g_list_find(callback_info_list, callback_info);
+       if (find_list == NULL)
+               return;
+
+       callback_info_list = g_list_remove_link(callback_info_list, find_list);
+       __callback_info_free(callback_info);
+       g_list_free(find_list);
+}
+
+static void __hash_destroy_callback_info(gpointer data)
+{
+
+       GList *callback_list = (GList *)data;
+       if (callback_list != NULL)
+               g_list_free_full(callback_list, __callback_info_free);
+}
+
+/* LCOV_EXCL_START */
+static void __hash_destory_local_value(gpointer data)
+{
+       message_port_local_port_info_s *mli = (message_port_local_port_info_s *)data;
+       if (mli) {
+               if (mli->port_name)
+                       free(mli->port_name);
+               free(mli);
+       }
+}
+/* LCOV_EXCL_STOP */
+
+static bool __initialize(void)
+{
+       if (!initialized_common) {
+               if (!initialize_common())
+                       return MESSAGE_PORT_ERROR_IO_ERROR;
+       }
+
+       if (__local_port_info == NULL) {
+               __local_port_info = g_hash_table_new_full(g_direct_hash,  g_direct_equal, NULL, __hash_destory_local_value);
+               retvm_if(!__local_port_info, false, "fail to create __local_port_info");
+       }
+
+       if (__sender_appid_hash == NULL) {
+               __sender_appid_hash = g_hash_table_new_full(g_str_hash, g_str_equal, free, NULL);
+               retvm_if(!__sender_appid_hash, false, "fail to create __sender_appid_hash");
+       }
+
+       if (__trusted_app_list_hash == NULL) {
+               __trusted_app_list_hash = g_hash_table_new(g_str_hash, g_str_equal);
+               retvm_if(!__trusted_app_list_hash, false, "fail to create __trusted_app_list_hash");
+       }
+
+       if (__callback_info_hash == NULL) {
+               __callback_info_hash = g_hash_table_new_full(g_direct_hash, g_direct_equal, NULL, __hash_destroy_callback_info);
+               retvm_if(!__callback_info_hash, false, "fail to create __callback_info_hash");
+       }
+
+       _initialized = true;
+
+       return true;
+}
+
+bool is_local_port_registed(const char *local_port, bool trusted, int *local_id, message_port_local_port_info_s **lpi)
+{
+       GHashTableIter iter;
+       gpointer key, value;
+
+       g_hash_table_iter_init(&iter, __local_port_info);
+       while (g_hash_table_iter_next(&iter, &key, &value)) {
+               message_port_local_port_info_s *mi = (message_port_local_port_info_s *)value;
+
+               if ((mi->is_trusted == trusted) && strcmp(mi->port_name, local_port) == 0) {
+                       *local_id = mi->local_id;
+                       if (lpi != NULL)
+                               *lpi = mi;
+                       return true;
+               }
+       }
+       return false;
+}
+
+static int __get_sender_pid(GDBusConnection *conn, const char *sender_name)
+{
+       GDBusMessage *msg = NULL;
+       GDBusMessage *reply = NULL;
+       GError *err = NULL;
+       GVariant *body;
+       int pid = 0;
+
+       msg = g_dbus_message_new_method_call("org.freedesktop.DBus", "/org/freedesktop/DBus",
+                       "org.freedesktop.DBus", "GetConnectionUnixProcessID");
+       if (!msg) {
+               _LOGE("Can't allocate new method call");
+               goto out;
+       }
+
+       g_dbus_message_set_body(msg, g_variant_new("(s)", sender_name));
+       reply = g_dbus_connection_send_message_with_reply_sync(conn, msg,
+                                                       G_DBUS_SEND_MESSAGE_FLAGS_NONE, -1, NULL, NULL, &err);
+
+       if (!reply) {
+               if (err != NULL) {
+                       _LOGE("Failed to get pid [%s]", err->message);
+                       g_error_free(err);
+               }
+               goto out;
+       }
+
+       body = g_dbus_message_get_body(reply);
+       g_variant_get(body, "(u)", &pid);
+
+out:
+       if (msg)
+               g_object_unref(msg);
+       if (reply)
+               g_object_unref(reply);
+
+       return pid;
+}
+
+static message_port_pkt_s *__message_port_recv_raw(int fd)
+{
+       message_port_pkt_s *pkt = NULL;
+       unsigned int nb;
+
+       pkt = (message_port_pkt_s *)calloc(sizeof(message_port_pkt_s), 1);
+       if (pkt == NULL) {
+               close(fd);
+               return NULL;
+       }
+
+       if (read_string_from_socket(fd, (char **)&pkt->remote_port_name, &pkt->remote_port_name_len) != MESSAGE_PORT_ERROR_NONE) {
+               LOGE("read socket fail: port_name");
+               free(pkt->remote_port_name);
+               free(pkt);
+               return NULL;
+       }
+
+       if (read_socket(fd, (char *)&pkt->is_bidirection, sizeof(pkt->is_bidirection), &nb) != MESSAGE_PORT_ERROR_NONE) {
+               LOGE("read socket fail: is_bidirection");
+               free(pkt->remote_port_name);
+               free(pkt);
+               return NULL;
+       }
+
+       if (read_socket(fd, (char *)&pkt->is_trusted, sizeof(pkt->is_trusted), &nb) != MESSAGE_PORT_ERROR_NONE) {
+               LOGE("read socket fail: is_trusted");
+               free(pkt->remote_port_name);
+               free(pkt);
+               return NULL;
+       }
+
+       if (read_string_from_socket(fd, (char **)&pkt->data, &pkt->data_len) != MESSAGE_PORT_ERROR_NONE) {
+               LOGE("read socket fail: data");
+               if (pkt->data)
+                       free(pkt->data);
+               free(pkt->remote_port_name);
+               free(pkt);
+               return NULL;
+       }
+
+       return pkt;
+}
+
+static gboolean __socket_request_handler(GIOChannel *gio,
+               GIOCondition cond,
+               gpointer data)
+{
+       int fd = 0;
+       message_port_callback_info_s *mi;
+       message_port_pkt_s *pkt;
+       bundle *kb = NULL;
+       GError *error = NULL;
+
+       mi = (message_port_callback_info_s *)data;
+       if (mi == NULL) {
+
+               g_io_channel_shutdown(gio, TRUE, &error);
+               if (error) {
+                       _LOGE("g_io_channel_shutdown error : %s", error->message);
+                       g_error_free(error);
+               }
+               g_io_channel_unref(gio);
+               return FALSE;
+       }
+
+       if (cond == G_IO_HUP) {
+
+               _LOGI("socket G_IO_HUP");
+               __callback_info_free_by_info(mi);
+               return FALSE;
+
+       } else {
+
+               if ((fd = g_io_channel_unix_get_fd(gio)) < 0) {
+                       _LOGE("fail to get fd from io channel");
+                       __callback_info_free_by_info(mi);
+                       return FALSE;
+               }
+
+               if ((pkt = __message_port_recv_raw(fd)) == NULL) {
+                       _LOGE("recv error on SOCKET");
+                       __callback_info_free_by_info(mi);
+                       return FALSE;
+               }
+
+               kb = bundle_decode(pkt->data, pkt->data_len);
+               if (pkt->is_bidirection) {
+                       if (mi->callback)
+                               mi->callback(mi->local_id, mi->remote_app_id, pkt->remote_port_name, pkt->is_trusted, kb, mi->user_data);
+               } else {
+                       if (mi->callback)
+                               mi->callback(mi->local_id, mi->remote_app_id, NULL, pkt->is_trusted, kb, mi->user_data);
+               }
+
+               bundle_free(kb);
+               if (pkt) {
+                       if (pkt->remote_port_name)
+                               free(pkt->remote_port_name);
+                       if (pkt->data)
+                               free(pkt->data);
+                       free(pkt);
+               }
+       }
+
+       return TRUE;
+}
+
+static bool __receive_message(GVariant *parameters, GDBusMethodInvocation *invocation)
+{
+       char *local_port = NULL;
+       char *local_appid = NULL;
+       char *remote_appid = NULL;
+       char *remote_port = NULL;
+       gboolean local_trusted = false;
+       gboolean remote_trusted = false;
+       gboolean bi_dir = false;
+       int len = 0;
+
+       bundle *data = NULL;
+       bundle_raw *raw = NULL;
+       message_port_local_port_info_s *mi;
+       int local_reg_id = 0;
+       message_port_callback_info_s *callback_info = NULL;
+       message_port_callback_info_s *head_callback_info;
+       GList *callback_info_list = NULL;
+
+       char buf[1024];
+       GDBusMessage *msg;
+       GUnixFDList *fd_list;
+       int fd_len;
+       int *returned_fds = NULL;
+       int fd;
+       bool ret = false;
+
+       g_variant_get(parameters, "(&s&sbb&s&sbu&s)", &local_appid, &local_port, &local_trusted, &bi_dir,
+                       &remote_appid, &remote_port, &remote_trusted, &len, &raw);
+
+       if (!remote_port) {
+               _LOGE("Invalid argument : remote_port is NULL");
+               goto out;
+       }
+       if (!remote_appid) {
+               _LOGE("Invalid argument : remote_appid is NULL");
+               goto out;
+       }
+       if (!is_local_port_registed(remote_port, remote_trusted, &local_reg_id, &mi)) {
+               _LOGE("Invalid argument : remote_port:(%s) trusted(%d)", remote_port, remote_trusted);
+               goto out;
+       }
+       if (!local_appid) {
+               _LOGE("Invalid argument : local_appid");
+               goto out;
+       }
+       if (!local_port) {
+               _LOGE("Invalid argument : local_port");
+               goto out;
+       }
+       if (strcmp(remote_appid, app_id) != 0) {
+               _LOGE("Invalid argument : remote_appid (%s)", remote_appid);
+               goto out;
+       }
+       if (strcmp(remote_port, mi->port_name) != 0) {
+               _LOGE("Invalid argument : remote_port (%s)", remote_port);
+               goto out;
+       }
+       if (!len) {
+               _LOGE("Invalid argument : data_len");
+               goto out;
+       }
+       if (remote_trusted) {
+               if (g_hash_table_lookup(__trusted_app_list_hash, (gpointer)local_appid) == NULL) {
+                       if (!is_preloaded(local_appid, remote_appid)) {
+                               int ret = check_certificate(local_appid, remote_appid);
+                               if (ret == MESSAGE_PORT_ERROR_NONE)
+                                       g_hash_table_insert(__trusted_app_list_hash, local_appid, "TRUE");
+                               else {
+                                       _LOGE("The application (%s) is not signed with the same certificate",
+                                                       local_appid);
+                                       goto out;
+                               }
+                       }
+               }
+       }
+
+       callback_info = (message_port_callback_info_s *)calloc(1, sizeof(message_port_callback_info_s));
+       if (callback_info == NULL) {
+               _LOGE("out of memory");
+               goto out;
+       }
+
+       callback_info->local_id = mi->local_id;
+       callback_info->remote_app_id = strdup(local_appid);
+       if (callback_info->remote_app_id == NULL) {
+               _LOGE("out of memory");
+               goto out;
+       }
+       callback_info->callback = mi->callback;
+       callback_info->user_data = mi->user_data;
+
+       msg = g_dbus_method_invocation_get_message(invocation);
+       fd_list = g_dbus_message_get_unix_fd_list(msg);
+
+       /* When application send message to self fd_list is NULL */
+       if (fd_list != NULL) {
+               returned_fds = g_unix_fd_list_steal_fds(fd_list, &fd_len);
+               if (returned_fds == NULL) {
+                       _LOGE("fail to get fds");
+                       goto out;
+               }
+               fd = returned_fds[0];
+
+               LOGI("g_unix_fd_list_get %d fd: [%d]", fd_len, fd);
+               if (fd > 0) {
+
+                       callback_info->gio_read = g_io_channel_unix_new(fd);
+                       if (!callback_info->gio_read) {
+                               _LOGE("Error is %s\n", strerror_r(errno, buf, sizeof(buf)));
+                               goto out;
+                       }
+
+                       callback_info->g_src_id = g_io_add_watch(callback_info->gio_read, G_IO_IN | G_IO_HUP,
+                                       __socket_request_handler, (gpointer)callback_info);
+                       if (callback_info->g_src_id == 0) {
+                               _LOGE("fail to add watch on socket");
+                               goto out;
+                       }
+
+                       callback_info_list = g_hash_table_lookup(__callback_info_hash, GUINT_TO_POINTER(mi->local_id));
+                       if (callback_info_list == NULL) {
+                               head_callback_info = (message_port_callback_info_s *)calloc(1, sizeof(message_port_callback_info_s));
+                               if (head_callback_info == NULL) {
+                                       _LOGE("fail to alloc head_callback_info");
+                                       goto out;
+                               }
+                               head_callback_info->local_id = 0;
+                               head_callback_info->remote_app_id = NULL;
+                               head_callback_info->callback = NULL;
+                               head_callback_info->gio_read = NULL;
+                               head_callback_info->g_src_id = 0;
+                               callback_info_list = g_list_append(callback_info_list, head_callback_info);
+                               callback_info_list = g_list_append(callback_info_list, callback_info);
+                               g_hash_table_insert(__callback_info_hash, GUINT_TO_POINTER(mi->local_id), callback_info_list);
+                       } else {
+                               callback_info_list = g_list_append(callback_info_list, callback_info);
+                       }
+               }
+       }
+
+       data = bundle_decode(raw, len);
+       if (!data) {
+               _LOGE("Invalid argument : message");
+               goto out;
+       }
+
+       LOGD("call calback %s", local_appid);
+       if (bi_dir)
+               mi->callback(mi->local_id, local_appid, local_port, local_trusted, data, NULL);
+       else
+               mi->callback(mi->local_id, local_appid, NULL, false, data, NULL);
+       bundle_free(data);
+
+       ret = true;
+out:
+       if (ret == false)
+               __callback_info_free(callback_info);
+
+       if (returned_fds)
+               free(returned_fds);
+
+       return ret;
+}
+
+static void __on_sender_name_appeared(GDBusConnection *connection,
+               const gchar     *name,
+               const gchar     *name_owner,
+               gpointer         user_data)
+{
+       _LOGI("sender name appeared : %s", name);
+}
+
+static void __on_sender_name_vanished(GDBusConnection *connection,
+               const gchar     *name,
+               gpointer         user_data)
+{
+       gboolean remove_result = FALSE;
+       int *watcher_id = (int *)user_data;
+       remove_result = g_hash_table_remove(__sender_appid_hash, (gpointer)name);
+       if (!remove_result)
+               _LOGE("Fail to remove sender appid from hash : %s", name);
+
+       if (watcher_id) {
+               if (*watcher_id > 0)
+                       g_bus_unwatch_name(*watcher_id);
+               else
+                       LOGE("Invalid watcher_id %d", *watcher_id);
+               free(watcher_id);
+       } else {
+               LOGE("watcher_id is NULL");
+       }
+}
+
+static bool __check_sender_validation(GVariant *parameters, const char *sender, GDBusConnection *conn)
+{
+       int ret = 0;
+       char buffer[MAX_PACKAGE_STR_SIZE] = {0, };
+       char *local_appid = NULL;
+       int pid = __get_sender_pid(conn, sender);
+       int *watcher_id = (int *)calloc(1, sizeof(int));
+       char *_sender;
+       retvm_if(!watcher_id, false, "Malloc failed");
+
+       ret = aul_app_get_appid_bypid(pid, buffer, sizeof(buffer));
+       if (ret != AUL_R_OK) {
+               _LOGE("Failed to get the sender ID: (%s) (%d)", sender, pid);
+               free(watcher_id);
+               return false;
+       }
+
+       g_variant_get_child(parameters, 0, "&s", &local_appid);
+       if (local_appid == NULL) {
+               _LOGE("appid is NULL : (%s) (%d)", sender, pid);
+               free(watcher_id);
+               return false;
+       }
+
+       if (strncmp(buffer, local_appid, MAX_PACKAGE_STR_SIZE) == 0) {
+               _LOGD("insert sender !!!!! %s", sender);
+               _sender = strdup(sender);
+               if (_sender == NULL) {
+                       _LOGE("out of memory");
+                       free(watcher_id);
+                       return false;
+               }
+               g_hash_table_insert(__sender_appid_hash, (gpointer)_sender, GINT_TO_POINTER(pid));
+               *watcher_id = g_bus_watch_name_on_connection(
+                                       gdbus_conn,
+                                       sender,
+                                       G_BUS_NAME_WATCHER_FLAGS_NONE,
+                                       __on_sender_name_appeared,
+                                       __on_sender_name_vanished,
+                                       watcher_id,
+                                       NULL);
+       } else {
+               free(watcher_id);
+               return false;
+       }
+       return true;
+}
+
+static void __dbus_method_call_handler(GDBusConnection *conn,
+                               const gchar *sender, const gchar *object_path,
+                               const gchar *iface_name, const gchar *method_name,
+                               GVariant *parameters, GDBusMethodInvocation *invocation,
+                               gpointer user_data)
+{
+       _LOGI("method_name: %s, sender: %s", method_name, sender);
+       gpointer sender_pid = g_hash_table_lookup(__sender_appid_hash, sender);
+       if (sender_pid == NULL) {
+               if (!__check_sender_validation(parameters, sender, conn))
+                       goto out;
+       }
+       if (g_strcmp0(method_name, "send_message") == 0)
+               __receive_message(parameters, invocation);
+out:
+       g_dbus_method_invocation_return_value(invocation, NULL);
+}
+
+static const GDBusInterfaceVTable interface_vtable = {
+       __dbus_method_call_handler,
+       NULL,
+       NULL
+};
+
+static int __register_dbus_interface(const char *port_name, bool is_trusted)
+{
+
+       GDBusNodeInfo *introspection_data = NULL;
+       int registration_id = 0;
+
+       static gchar introspection_prefix[] =
+               "<node>"
+               "  <interface name='";
+
+       static gchar introspection_postfix[] =
+               "'>"
+               "        <method name='send_message'>"
+               "          <arg type='s' name='local_appid' direction='in'/>"
+               "          <arg type='s' name='local_port' direction='in'/>"
+               "          <arg type='b' name='local_trusted' direction='in'/>"
+               "          <arg type='b' name='bi_dir' direction='in'/>"
+               "          <arg type='s' name='remote_appid' direction='in'/>"
+               "          <arg type='s' name='remote_port' direction='in'/>"
+               "          <arg type='b' name='remote_trusted' direction='in'/>"
+               "          <arg type='u' name='data_len' direction='in'/>"
+               "          <arg type='s' name='data' direction='in'/>"
+               "        </method>"
+               "  </interface>"
+               "</node>";
+
+       char *introspection_xml = NULL;
+       int introspection_xml_len = 0;
+
+
+       int owner_id = 0;
+       GError *error = NULL;
+       char *bus_name = NULL;
+       char *interface_name = NULL;
+       GVariant *result = NULL;
+
+       bus_name = get_encoded_name(app_id, port_name, is_trusted);
+       if (!bus_name) {
+               _LOGE("Fail to get bus name");
+               goto out;
+       }
+       interface_name = bus_name;
+
+       introspection_xml_len = strlen(introspection_prefix) + strlen(interface_name) +
+               strlen(introspection_postfix) + 1;
+
+       introspection_xml = (char *)calloc(introspection_xml_len, sizeof(char));
+       if (!introspection_xml) {
+               _LOGE("out of memory");
+               goto out;
+       }
+
+
+       result = g_dbus_connection_call_sync(
+                       gdbus_conn,
+                       DBUS_SERVICE_DBUS,
+                       DBUS_PATH_DBUS,
+                       DBUS_INTERFACE_DBUS,
+                       "RequestName",
+                       g_variant_new("(su)", bus_name, G_BUS_NAME_OWNER_FLAGS_NONE),
+                       G_VARIANT_TYPE("(u)"),
+                       G_DBUS_CALL_FLAGS_NONE,
+                       -1,
+                       NULL,
+                       &error);
+       if (error) {
+               _LOGE("RequestName fail : %s", error->message);
+               g_error_free(error);
+               goto out;
+       }
+       if (result == NULL) {
+               _LOGE("fail to get name NULL");
+               goto out;
+       }
+       g_variant_get(result, "(u)", &owner_id);
+       if (owner_id == 0) {
+               _LOGE("Acquiring the own name is failed");
+               goto out;
+       }
+
+       _LOGD("Acquiring the own name : %d", owner_id);
+
+       snprintf(introspection_xml, introspection_xml_len, "%s%s%s", introspection_prefix, interface_name, introspection_postfix);
+
+       introspection_data = g_dbus_node_info_new_for_xml(introspection_xml, NULL);
+       if (!introspection_data) {
+               _LOGE("g_dbus_node_info_new_for_xml() is failed.");
+               goto out;
+       }
+
+       registration_id = g_dbus_connection_register_object(gdbus_conn,
+                                               MESSAGEPORT_OBJECT_PATH, introspection_data->interfaces[0],
+                                               &interface_vtable, NULL, NULL, NULL);
+
+       _LOGD("registration_id %d", registration_id);
+
+       if (registration_id == 0) {
+               _LOGE("Failed to g_dbus_connection_register_object");
+               goto out;
+       }
+
+out:
+       if (introspection_data)
+               g_dbus_node_info_unref(introspection_data);
+       if (introspection_xml)
+               free(introspection_xml);
+       if (bus_name)
+               free(bus_name);
+       if (result)
+               g_variant_unref(result);
+
+
+       return registration_id;
+}
+
+static bool __message_port_register_port(const int local_id, const char *local_port, bool is_trusted, message_port_message_cb callback, void *user_data)
+{
+       message_port_local_port_info_s *mi = (message_port_local_port_info_s *)calloc(1, sizeof(message_port_local_port_info_s));
+       retvm_if(!mi, false, "Malloc failed");
+
+       mi->callback = callback;
+       mi->is_trusted = is_trusted;
+       mi->port_name = strdup(local_port);
+       if (mi->port_name == NULL) {
+               _LOGE("Malloc failed (%s)", local_port);
+               free(mi);
+               return false;
+       }
+       mi->local_id = local_id;
+       mi->user_data = user_data;
+
+       g_hash_table_insert(__local_port_info, GINT_TO_POINTER(mi->local_id), mi);
+       return true;
+}
+
+int get_local_port_info(int id, message_port_local_port_info_s **info)
+{
+       message_port_local_port_info_s *mi = (message_port_local_port_info_s *)g_hash_table_lookup(__local_port_info, GINT_TO_POINTER(id));
+
+       if (mi == NULL)
+               return MESSAGE_PORT_ERROR_PORT_NOT_FOUND;
+       *info = mi;
+
+       return MESSAGE_PORT_ERROR_NONE;
+}
+
+int register_message_port(const char *local_port, bool is_trusted, message_port_message_cb callback, void *user_data)
+{
+       _SECURE_LOGI("local_port : [%s:%s]", local_port, is_trusted ? "trusted" : "non-trusted");
+
+       int local_id = 0;
+       message_port_local_port_info_s *port_info;
+       if (!_initialized) {
+               if (!__initialize())
+                       return MESSAGE_PORT_ERROR_IO_ERROR;
+       }
+
+       /* Check the message port is already registed */
+       if (is_local_port_registed(local_port, is_trusted, &local_id, &port_info)) {
+               port_info->callback = callback;
+               port_info->user_data = user_data;
+               return local_id;
+       }
+
+       local_id = __register_dbus_interface(local_port, is_trusted);
+       if (local_id < 1) {
+               _LOGE("register_dbus_interface fail !!");
+               return MESSAGE_PORT_ERROR_OUT_OF_MEMORY;
+       }
+
+       if (!__message_port_register_port(local_id, local_port, is_trusted, callback, user_data))
+               return MESSAGE_PORT_ERROR_OUT_OF_MEMORY;
+
+       return local_id;
+}
+
+int unregister_local_port(int local_port_id, bool trusted_port)
+{
+
+       GVariant *result;
+       char *bus_name = NULL;
+       GError *err = NULL;
+       int ret = 0;
+       message_port_local_port_info_s *mi;
+
+       _LOGI("unregister : %d", local_port_id);
+
+       if (!_initialized) {
+               if (!__initialize())
+                       return MESSAGE_PORT_ERROR_IO_ERROR;
+       }
+
+       mi = (message_port_local_port_info_s *)
+               g_hash_table_lookup(__local_port_info, GINT_TO_POINTER(local_port_id));
+       if (mi == NULL)
+               return MESSAGE_PORT_ERROR_PORT_NOT_FOUND;
+
+       if (mi->is_trusted != trusted_port)
+               return MESSAGE_PORT_ERROR_INVALID_PARAMETER;
+
+       g_hash_table_remove(__callback_info_hash, GUINT_TO_POINTER(local_port_id));
+
+       bus_name = get_encoded_name(app_id, mi->port_name, mi->is_trusted);
+       if (bus_name == NULL)
+               return MESSAGE_PORT_ERROR_OUT_OF_MEMORY;
+
+       g_dbus_connection_unregister_object(gdbus_conn, local_port_id);
+
+       result = g_dbus_connection_call_sync(
+                       gdbus_conn,
+                       DBUS_SERVICE_DBUS,
+                       DBUS_PATH_DBUS,
+                       DBUS_INTERFACE_DBUS,
+                       "ReleaseName",
+                       g_variant_new("(s)", bus_name),
+                       G_VARIANT_TYPE("(u)"),
+                       G_DBUS_CALL_FLAGS_NONE,
+                       -1,
+                       NULL,
+                       &err);
+
+       if (bus_name)
+               free(bus_name);
+
+       if (err) {
+               _LOGE("RequestName fail : %s", err->message);
+               g_error_free(err);
+               return MESSAGE_PORT_ERROR_PORT_NOT_FOUND;
+       }
+       g_variant_get(result, "(u)", &ret);
+
+       if (result)
+               g_variant_unref(result);
+
+       if (ret != DBUS_RELEASE_NAME_REPLY_RELEASED) {
+
+               if (ret == DBUS_RELEASE_NAME_REPLY_NON_EXISTENT) {
+                       _LOGE("Port Not exist");
+                       return MESSAGE_PORT_ERROR_PORT_NOT_FOUND;
+               } else if (ret == DBUS_RELEASE_NAME_REPLY_NOT_OWNER) {
+                       _LOGE("Try to release not owned name. MESSAGE_PORT_ERROR_INVALID_PARAMETER");
+                       return MESSAGE_PORT_ERROR_INVALID_PARAMETER;
+               }
+       }
+
+       g_hash_table_remove(__local_port_info, GINT_TO_POINTER(local_port_id));
+
+       return MESSAGE_PORT_ERROR_NONE;
+}