2 * Copyright (c) 2014 Samsung Electronics Co., Ltd All Rights Reserved
4 * Licensed under the Apache License, Version 2.0 (the License);
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an AS IS BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
21 #include <message-port.h>
22 #include "message_port_internal.h"
23 #include "message_port_log.h"
24 #include "message_port.h"
26 typedef struct message_port_callback_item_s {
27 message_port_message_cb callback;
29 } message_port_callback_item;
31 static GHashTable *__listeners;
32 static GHashTable *__trusted_listeners;
33 static pthread_mutex_t __mutex = PTHREAD_MUTEX_INITIALIZER;
35 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)
38 callback(local_port_id, remote_app_id, remote_port, trusted_remote_port, message, user_data);
45 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)
47 message_port_callback_item *item =
48 (message_port_callback_item *)g_hash_table_lookup(__listeners, GINT_TO_POINTER(local_port_id));
49 do_callback(item->callback, local_port_id, remote_app_id, remote_port, trusted_remote_port, message, item->user_data);
52 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)
54 message_port_callback_item *item =
55 (message_port_callback_item *)g_hash_table_lookup(__trusted_listeners, GINT_TO_POINTER(trusted_local_port_id));
56 do_callback(item->callback, trusted_local_port_id, remote_app_id, remote_port, trusted_remote_port, message, item->user_data);
59 int message_port_register_local_port(const char *local_port, message_port_message_cb callback, void *user_data)
61 if (local_port == NULL || callback == NULL) {
62 _LOGE("[MESSAGE_PORT_ERROR_INVALID_PARAMETER] NULL value is not allowed.");
63 return MESSAGE_PORT_ERROR_INVALID_PARAMETER;
66 int local_port_id = messageport_register_local_port(local_port, message_dispatcher);
67 if (local_port_id > 0) {
68 _SECURE_LOGI("Register local port ID (%d).", local_port_id);
70 if (__listeners == NULL)
71 __listeners = g_hash_table_new_full(g_direct_hash, g_direct_equal, NULL, g_free);
73 pthread_mutex_lock(&__mutex);
74 message_port_callback_item *item =
75 (message_port_callback_item *)g_hash_table_lookup(__listeners, GINT_TO_POINTER(local_port_id));
77 item = (message_port_callback_item *)calloc(1, sizeof(message_port_callback_item));
79 return MESSAGE_PORT_ERROR_OUT_OF_MEMORY;
81 g_hash_table_insert(__listeners, GINT_TO_POINTER(local_port_id), item);
84 item->callback = callback;
85 item->user_data = user_data;
86 pthread_mutex_unlock(&__mutex);
89 _SECURE_LOGI("Register local port fail (%d).", local_port_id);
92 return convert_to_tizen_error((messageport_error_e)local_port_id);
95 int message_port_register_trusted_local_port(const char *local_port, message_port_trusted_message_cb callback, void *user_data)
97 if (local_port == NULL || callback == NULL) {
98 _LOGE("[MESSAGE_PORT_ERROR_INVALID_PARAMETER] NULL value is not allowed.");
99 return MESSAGE_PORT_ERROR_INVALID_PARAMETER;
102 int trusted_local_port_id = messageport_register_trusted_local_port(local_port, trusted_message_dispatcher);
103 if (trusted_local_port_id > 0) {
104 _SECURE_LOGI("Register trusted local port ID (%d).", trusted_local_port_id);
106 if (__trusted_listeners == NULL)
107 __trusted_listeners = g_hash_table_new_full(g_direct_hash, g_direct_equal, NULL, g_free);
109 pthread_mutex_lock(&__mutex);
110 message_port_callback_item *item =
111 (message_port_callback_item *)g_hash_table_lookup(__trusted_listeners, GINT_TO_POINTER(trusted_local_port_id));
113 item = (message_port_callback_item *)calloc(1, sizeof(message_port_callback_item));
115 return MESSAGE_PORT_ERROR_OUT_OF_MEMORY;
117 g_hash_table_insert(__trusted_listeners, GINT_TO_POINTER(trusted_local_port_id), item);
120 item->callback = callback;
121 item->user_data = user_data;
122 pthread_mutex_unlock(&__mutex);
124 _SECURE_LOGI("Register trusted local port fail (%d).", trusted_local_port_id);
127 return convert_to_tizen_error((messageport_error_e)trusted_local_port_id);
130 int message_port_unregister_local_port(int local_port_id)
132 int res = MESSAGE_PORT_ERROR_NONE;
133 if (local_port_id <= 0) {
134 _LOGE("[MESSAGEPORT_ERROR_INVALID_PARAMETER] Neither 0 nor negative value is allowed.");
135 return MESSAGE_PORT_ERROR_INVALID_PARAMETER;
137 res = messageport_unregister_local_port(local_port_id, false);
138 g_hash_table_remove(__listeners, GINT_TO_POINTER(local_port_id));
140 return convert_to_tizen_error((messageport_error_e)res);
143 int message_port_unregister_trusted_local_port(int trusted_local_port_id)
146 int res = MESSAGE_PORT_ERROR_NONE;
147 if (trusted_local_port_id <= 0) {
148 _LOGE("[MESSAGEPORT_ERROR_INVALID_PARAMETER] Neither 0 nor negative value is allowed.");
149 return MESSAGE_PORT_ERROR_INVALID_PARAMETER;
151 res = messageport_unregister_local_port(trusted_local_port_id, true);
152 g_hash_table_remove(__trusted_listeners, GINT_TO_POINTER(trusted_local_port_id));
155 return convert_to_tizen_error((messageport_error_e)res);
158 int message_port_check_remote_port(const char* remote_app_id, const char *remote_port, bool* exist)
160 if (remote_app_id == NULL || remote_port == NULL) {
161 _LOGE("[MESSAGE_PORT_ERROR_INVALID_PARAMETER] NULL value is not allowed.");
162 return MESSAGE_PORT_ERROR_INVALID_PARAMETER;
164 _SECURE_LOGI("Check remote port (%s):(%s).", remote_app_id, remote_port);
165 return convert_to_tizen_error((messageport_error_e)messageport_check_remote_port(remote_app_id, remote_port, exist));
168 int message_port_check_trusted_remote_port(const char* remote_app_id, const char *remote_port, bool *exist)
170 if (remote_app_id == NULL || remote_port == NULL) {
171 _LOGE("[MESSAGE_PORT_ERROR_INVALID_PARAMETER] NULL value is not allowed.");
172 return MESSAGE_PORT_ERROR_INVALID_PARAMETER;
174 _SECURE_LOGI("Check trusted remote port (%s):(%s).", remote_app_id, remote_port);
175 return convert_to_tizen_error((messageport_error_e)messageport_check_trusted_remote_port(remote_app_id, remote_port, exist));
178 int message_port_send_message(const char *remote_app_id, const char *remote_port, bundle *message)
180 if (remote_app_id == NULL || remote_port == NULL || message == NULL) {
181 _LOGE("[MESSAGE_PORT_ERROR_INVALID_PARAMETER] NULL value is not allowed.");
182 return MESSAGE_PORT_ERROR_INVALID_PARAMETER;
184 return convert_to_tizen_error((messageport_error_e)messageport_send_message(remote_app_id, remote_port, message));
187 int message_port_send_trusted_message(const char *remote_app_id, const char *remote_port, bundle *message)
189 if (remote_app_id == NULL || remote_port == NULL || message == NULL) {
190 _LOGE("[MESSAGE_PORT_ERROR_INVALID_PARAMETER] NULL value is not allowed.");
191 return MESSAGE_PORT_ERROR_INVALID_PARAMETER;
193 return convert_to_tizen_error((messageport_error_e)messageport_send_trusted_message(remote_app_id, remote_port, message));
196 int message_port_send_message_with_local_port(const char *remote_app_id, const char *remote_port, bundle *message, int local_port_id)
198 if (remote_app_id == NULL || remote_port == NULL || message == NULL) {
199 _LOGE("[MESSAGE_PORT_ERROR_INVALID_PARAMETER] NULL value is not allowed.");
200 return MESSAGE_PORT_ERROR_INVALID_PARAMETER;
201 } else if (local_port_id <= 0) {
202 _LOGE("[MESSAGEPORT_ERROR_INVALID_PARAMETER] Neither 0 nor negative value is allowed.");
203 return MESSAGE_PORT_ERROR_INVALID_PARAMETER;
206 message_port_callback_item *item = NULL;
207 message_port_callback_item *trusted_item = NULL;
209 if (__listeners != NULL)
210 item = (message_port_callback_item *)g_hash_table_lookup(__listeners, GINT_TO_POINTER(local_port_id));
212 if (item == NULL && __trusted_listeners != NULL)
213 trusted_item = (message_port_callback_item *)g_hash_table_lookup(__trusted_listeners, GINT_TO_POINTER(local_port_id));
216 if (item == NULL && trusted_item == NULL) {
217 _LOGE("[MESSAGE_PORT_ERROR_PORT_NOT_FOUND] The local port ID (%d) is not registered.", local_port_id);
218 return MESSAGE_PORT_ERROR_PORT_NOT_FOUND;
222 _SECURE_LOGI("Send a message to (%s):(%s) and listen at the local port ID (%d).", remote_app_id, remote_port, local_port_id);
223 return convert_to_tizen_error((messageport_error_e)messageport_send_bidirectional_message(local_port_id, remote_app_id, remote_port, message));
226 int message_port_send_trusted_message_with_local_port(const char* remote_app_id, const char *remote_port, bundle* message, int local_port_id)
228 if (remote_app_id == NULL || remote_port == NULL || message == NULL) {
229 _LOGE("[MESSAGE_PORT_ERROR_INVALID_PARAMETER] NULL value is not allowed.");
230 return MESSAGE_PORT_ERROR_INVALID_PARAMETER;
231 } else if (local_port_id <= 0) {
232 _LOGE("[MESSAGEPORT_ERROR_INVALID_PARAMETER] Neither 0 nor negative value is allowed.");
233 return MESSAGE_PORT_ERROR_INVALID_PARAMETER;
235 message_port_callback_item *item = NULL;
236 message_port_callback_item *trusted_item = NULL;
238 if (__listeners != NULL)
239 item = (message_port_callback_item *)g_hash_table_lookup(__listeners, GINT_TO_POINTER(local_port_id));
241 if (item == NULL && __trusted_listeners != NULL)
242 trusted_item = (message_port_callback_item *)g_hash_table_lookup(__trusted_listeners, GINT_TO_POINTER(local_port_id));
244 if (item == NULL && trusted_item == NULL) {
245 _LOGE("[MESSAGE_PORT_ERROR_PORT_NOT_FOUND] The local port ID (%d) is not registered.", local_port_id);
246 return MESSAGE_PORT_ERROR_PORT_NOT_FOUND;
250 _SECURE_LOGI("Send a trusted message to (%s):(%s) and listen at the local port ID (%d).", remote_app_id, remote_port, local_port_id);
251 return convert_to_tizen_error((messageport_error_e)messageport_send_bidirectional_trusted_message(local_port_id, remote_app_id, remote_port, message));