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)
39 callback(local_port_id, remote_app_id, remote_port, trusted_remote_port, message, user_data);
48 static void message_dispatcher(int local_port_id, const char *remote_app_id, const char *remote_port, bool trusted_remote_port, bundle *message)
50 _SECURE_LOGI("A message has been received to specific local port id (%d) from%s remote port (%s):(%s).",
51 local_port_id, trusted_remote_port ? " trusted" : "", remote_app_id, remote_port);
53 message_port_callback_item *item =
54 (message_port_callback_item *)g_hash_table_lookup(__listeners, GINT_TO_POINTER(local_port_id));
55 do_callback(item->callback, local_port_id, remote_app_id, remote_port, trusted_remote_port, message, item->user_data);
58 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)
60 _SECURE_LOGI("A message has been received to specific trusted local port id (%d) from%s remote port (%s):(%s).",
61 trusted_local_port_id, trusted_remote_port ? " trusted" : "", remote_app_id, remote_port);
62 message_port_callback_item *item =
63 (message_port_callback_item *)g_hash_table_lookup(__trusted_listeners, GINT_TO_POINTER(trusted_local_port_id));
64 do_callback(item->callback, trusted_local_port_id, remote_app_id, remote_port, trusted_remote_port, message, item->user_data);
67 int message_port_register_local_port(const char *local_port, message_port_message_cb callback, void *user_data)
69 if (local_port == NULL || callback == NULL)
71 _LOGE("[MESSAGE_PORT_ERROR_INVALID_PARAMETER] NULL value is not allowed.");
72 return MESSAGE_PORT_ERROR_INVALID_PARAMETER;
75 int local_port_id = messageport_register_local_port(local_port, message_dispatcher);
76 if (local_port_id > 0)
78 _SECURE_LOGI("Register local port ID (%d).", local_port_id);
80 if (__listeners == NULL) {
81 __listeners = g_hash_table_new_full(g_direct_hash, g_direct_equal, NULL, g_free);
83 pthread_mutex_lock(&__mutex);
84 message_port_callback_item *item =
85 (message_port_callback_item *)g_hash_table_lookup(__listeners, GINT_TO_POINTER(local_port_id));
87 item = (message_port_callback_item *)calloc(1, sizeof(message_port_callback_item));
89 return MESSAGE_PORT_ERROR_OUT_OF_MEMORY;
91 g_hash_table_insert(__listeners, GINT_TO_POINTER(local_port_id), item);
95 item->callback = callback;
96 item->user_data = user_data;
97 pthread_mutex_unlock(&__mutex);
100 _SECURE_LOGI("Register local port fail (%d).", local_port_id);
103 return convert_to_tizen_error((messageport_error_e)local_port_id);
106 int message_port_register_trusted_local_port(const char *local_port, message_port_trusted_message_cb callback, void *user_data)
108 if (local_port == NULL || callback == NULL)
110 _LOGE("[MESSAGE_PORT_ERROR_INVALID_PARAMETER] NULL value is not allowed.");
111 return MESSAGE_PORT_ERROR_INVALID_PARAMETER;
114 int trusted_local_port_id = messageport_register_trusted_local_port(local_port, trusted_message_dispatcher);
115 if (trusted_local_port_id > 0)
117 _SECURE_LOGI("Register trusted local port ID (%d).", trusted_local_port_id);
119 if (__trusted_listeners == NULL) {
120 __trusted_listeners = g_hash_table_new_full(g_direct_hash, g_direct_equal, NULL, g_free);
122 pthread_mutex_lock(&__mutex);
123 message_port_callback_item *item =
124 (message_port_callback_item *)g_hash_table_lookup(__trusted_listeners, GINT_TO_POINTER(trusted_local_port_id));
126 item = (message_port_callback_item *)calloc(1, sizeof(message_port_callback_item));
128 return MESSAGE_PORT_ERROR_OUT_OF_MEMORY;
130 g_hash_table_insert(__trusted_listeners, GINT_TO_POINTER(trusted_local_port_id), item);
133 item->callback = callback;
134 item->user_data = user_data;
135 pthread_mutex_unlock(&__mutex);
137 _SECURE_LOGI("Register trusted local port fail (%d).", trusted_local_port_id);
141 return convert_to_tizen_error((messageport_error_e)trusted_local_port_id);
144 int message_port_unregister_local_port(int local_port_id)
146 int res = MESSAGE_PORT_ERROR_NONE;
147 if (local_port_id <= 0)
149 _LOGE("[MESSAGEPORT_ERROR_INVALID_PARAMETER] Neither 0 nor negative value is allowed.");
150 return MESSAGE_PORT_ERROR_INVALID_PARAMETER;
153 res = messageport_unregister_local_port(local_port_id, false);
154 g_hash_table_remove(__listeners, GINT_TO_POINTER(local_port_id));
156 return convert_to_tizen_error((messageport_error_e)res);
159 int message_port_unregister_trusted_local_port(int trusted_local_port_id)
162 int res = MESSAGE_PORT_ERROR_NONE;
163 if (trusted_local_port_id <= 0)
165 _LOGE("[MESSAGEPORT_ERROR_INVALID_PARAMETER] Neither 0 nor negative value is allowed.");
166 return MESSAGE_PORT_ERROR_INVALID_PARAMETER;
169 res = messageport_unregister_local_port(trusted_local_port_id, true);
170 g_hash_table_remove(__trusted_listeners, GINT_TO_POINTER(trusted_local_port_id));
173 return convert_to_tizen_error((messageport_error_e)res);
176 int message_port_check_remote_port(const char* remote_app_id, const char *remote_port, bool* exist)
178 if (remote_app_id == NULL || remote_port == NULL)
180 _LOGE("[MESSAGE_PORT_ERROR_INVALID_PARAMETER] NULL value is not allowed.");
181 return MESSAGE_PORT_ERROR_INVALID_PARAMETER;
183 _SECURE_LOGI("Check remote port (%s):(%s).", remote_app_id, remote_port);
184 return convert_to_tizen_error((messageport_error_e)messageport_check_remote_port(remote_app_id, remote_port, exist));
187 int message_port_check_trusted_remote_port(const char* remote_app_id, const char *remote_port, bool *exist)
189 if (remote_app_id == NULL || remote_port == NULL)
191 _LOGE("[MESSAGE_PORT_ERROR_INVALID_PARAMETER] NULL value is not allowed.");
192 return MESSAGE_PORT_ERROR_INVALID_PARAMETER;
194 _SECURE_LOGI("Check trusted remote port (%s):(%s).", remote_app_id, remote_port);
195 return convert_to_tizen_error((messageport_error_e)messageport_check_trusted_remote_port(remote_app_id, remote_port, exist));
198 int message_port_send_message(const char *remote_app_id, const char *remote_port, bundle *message)
200 if (remote_app_id == NULL || remote_port == NULL || message == NULL)
202 _LOGE("[MESSAGE_PORT_ERROR_INVALID_PARAMETER] NULL value is not allowed.");
203 return MESSAGE_PORT_ERROR_INVALID_PARAMETER;
205 _SECURE_LOGI("Send a message to the remote port (%s):(%s).", remote_app_id, remote_port);
206 return convert_to_tizen_error((messageport_error_e)messageport_send_message(remote_app_id, remote_port, message));
209 int message_port_send_trusted_message(const char *remote_app_id, const char *remote_port, bundle *message)
211 if (remote_app_id == NULL || remote_port == NULL || message == NULL)
213 _LOGE("[MESSAGE_PORT_ERROR_INVALID_PARAMETER] NULL value is not allowed.");
214 return MESSAGE_PORT_ERROR_INVALID_PARAMETER;
216 _SECURE_LOGI("Send a trusted message to the remote port (%s):(%s).", remote_app_id, remote_port);
217 return convert_to_tizen_error((messageport_error_e)messageport_send_trusted_message(remote_app_id, remote_port, message));
220 int message_port_send_message_with_local_port(const char *remote_app_id, const char *remote_port, bundle *message, int local_port_id)
222 if (remote_app_id == NULL || remote_port == NULL || message == NULL)
224 _LOGE("[MESSAGE_PORT_ERROR_INVALID_PARAMETER] NULL value is not allowed.");
225 return MESSAGE_PORT_ERROR_INVALID_PARAMETER;
227 else if (local_port_id <= 0)
229 _LOGE("[MESSAGEPORT_ERROR_INVALID_PARAMETER] Neither 0 nor negative value is allowed.");
230 return MESSAGE_PORT_ERROR_INVALID_PARAMETER;
234 message_port_callback_item *item = NULL;
235 message_port_callback_item *trusted_item = NULL;
237 if (__listeners != NULL) {
238 item = (message_port_callback_item *)g_hash_table_lookup(__listeners, GINT_TO_POINTER(local_port_id));
240 if (item == NULL && __trusted_listeners != NULL) {
241 trusted_item = (message_port_callback_item *)g_hash_table_lookup(__trusted_listeners, GINT_TO_POINTER(local_port_id));
245 if (item == NULL && trusted_item == NULL)
247 _LOGE("[MESSAGE_PORT_ERROR_PORT_NOT_FOUND] The local port ID (%d) is not registered.", local_port_id);
248 return MESSAGE_PORT_ERROR_PORT_NOT_FOUND;
252 _SECURE_LOGI("Send a message to (%s):(%s) and listen at the local port ID (%d).", remote_app_id, remote_port, local_port_id);
253 return convert_to_tizen_error((messageport_error_e)messageport_send_bidirectional_message(local_port_id, remote_app_id, remote_port, message));
256 int message_port_send_trusted_message_with_local_port(const char* remote_app_id, const char *remote_port, bundle* message, int local_port_id)
258 if (remote_app_id == NULL || remote_port == NULL || message == NULL)
260 _LOGE("[MESSAGE_PORT_ERROR_INVALID_PARAMETER] NULL value is not allowed.");
261 return MESSAGE_PORT_ERROR_INVALID_PARAMETER;
263 else if (local_port_id <= 0)
265 _LOGE("[MESSAGEPORT_ERROR_INVALID_PARAMETER] Neither 0 nor negative value is allowed.");
266 return MESSAGE_PORT_ERROR_INVALID_PARAMETER;
269 message_port_callback_item *item = NULL;
270 message_port_callback_item *trusted_item = NULL;
272 if (__listeners != NULL) {
273 item = (message_port_callback_item *)g_hash_table_lookup(__listeners, GINT_TO_POINTER(local_port_id));
275 if (item == NULL && __trusted_listeners != NULL) {
276 trusted_item = (message_port_callback_item *)g_hash_table_lookup(__trusted_listeners, GINT_TO_POINTER(local_port_id));
280 if (item == NULL && trusted_item == NULL)
282 _LOGE("[MESSAGE_PORT_ERROR_PORT_NOT_FOUND] The local port ID (%d) is not registered.", local_port_id);
283 return MESSAGE_PORT_ERROR_PORT_NOT_FOUND;
287 _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);
288 return convert_to_tizen_error((messageport_error_e)messageport_send_bidirectional_trusted_message(local_port_id, remote_app_id, remote_port, message));