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, void *user_data)
50 message_port_callback_item *item =
51 (message_port_callback_item *)g_hash_table_lookup(__listeners, GINT_TO_POINTER(local_port_id));
52 do_callback(item->callback, local_port_id, remote_app_id, remote_port, trusted_remote_port, message, item->user_data);
55 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)
57 message_port_callback_item *item =
58 (message_port_callback_item *)g_hash_table_lookup(__trusted_listeners, GINT_TO_POINTER(trusted_local_port_id));
59 do_callback(item->callback, trusted_local_port_id, remote_app_id, remote_port, trusted_remote_port, message, item->user_data);
62 int message_port_register_local_port(const char *local_port, message_port_message_cb callback, void *user_data)
64 if (local_port == NULL || callback == NULL)
66 _LOGE("[MESSAGE_PORT_ERROR_INVALID_PARAMETER] NULL value is not allowed.");
67 return MESSAGE_PORT_ERROR_INVALID_PARAMETER;
70 int local_port_id = messageport_register_local_port(local_port, message_dispatcher);
71 if (local_port_id > 0)
73 _SECURE_LOGI("Register local port ID (%d).", local_port_id);
75 if (__listeners == NULL) {
76 __listeners = g_hash_table_new_full(g_direct_hash, g_direct_equal, NULL, g_free);
78 pthread_mutex_lock(&__mutex);
79 message_port_callback_item *item =
80 (message_port_callback_item *)g_hash_table_lookup(__listeners, GINT_TO_POINTER(local_port_id));
82 item = (message_port_callback_item *)calloc(1, sizeof(message_port_callback_item));
84 return MESSAGE_PORT_ERROR_OUT_OF_MEMORY;
86 g_hash_table_insert(__listeners, GINT_TO_POINTER(local_port_id), item);
90 item->callback = callback;
91 item->user_data = user_data;
92 pthread_mutex_unlock(&__mutex);
95 _SECURE_LOGI("Register local port fail (%d).", local_port_id);
98 return convert_to_tizen_error((messageport_error_e)local_port_id);
101 int message_port_register_trusted_local_port(const char *local_port, message_port_trusted_message_cb callback, void *user_data)
103 if (local_port == NULL || callback == NULL)
105 _LOGE("[MESSAGE_PORT_ERROR_INVALID_PARAMETER] NULL value is not allowed.");
106 return MESSAGE_PORT_ERROR_INVALID_PARAMETER;
109 int trusted_local_port_id = messageport_register_trusted_local_port(local_port, trusted_message_dispatcher);
110 if (trusted_local_port_id > 0)
112 _SECURE_LOGI("Register trusted local port ID (%d).", trusted_local_port_id);
114 if (__trusted_listeners == NULL) {
115 __trusted_listeners = g_hash_table_new_full(g_direct_hash, g_direct_equal, NULL, g_free);
117 pthread_mutex_lock(&__mutex);
118 message_port_callback_item *item =
119 (message_port_callback_item *)g_hash_table_lookup(__trusted_listeners, GINT_TO_POINTER(trusted_local_port_id));
121 item = (message_port_callback_item *)calloc(1, sizeof(message_port_callback_item));
123 return MESSAGE_PORT_ERROR_OUT_OF_MEMORY;
125 g_hash_table_insert(__trusted_listeners, GINT_TO_POINTER(trusted_local_port_id), item);
128 item->callback = callback;
129 item->user_data = user_data;
130 pthread_mutex_unlock(&__mutex);
132 _SECURE_LOGI("Register trusted local port fail (%d).", trusted_local_port_id);
136 return convert_to_tizen_error((messageport_error_e)trusted_local_port_id);
139 int message_port_unregister_local_port(int local_port_id)
141 int res = MESSAGE_PORT_ERROR_NONE;
142 if (local_port_id <= 0)
144 _LOGE("[MESSAGEPORT_ERROR_INVALID_PARAMETER] Neither 0 nor negative value is allowed.");
145 return MESSAGE_PORT_ERROR_INVALID_PARAMETER;
148 res = messageport_unregister_local_port(local_port_id, false);
149 g_hash_table_remove(__listeners, GINT_TO_POINTER(local_port_id));
151 return convert_to_tizen_error((messageport_error_e)res);
154 int message_port_unregister_trusted_local_port(int trusted_local_port_id)
157 int res = MESSAGE_PORT_ERROR_NONE;
158 if (trusted_local_port_id <= 0)
160 _LOGE("[MESSAGEPORT_ERROR_INVALID_PARAMETER] Neither 0 nor negative value is allowed.");
161 return MESSAGE_PORT_ERROR_INVALID_PARAMETER;
164 res = messageport_unregister_local_port(trusted_local_port_id, true);
165 g_hash_table_remove(__trusted_listeners, GINT_TO_POINTER(trusted_local_port_id));
168 return convert_to_tizen_error((messageport_error_e)res);
171 int message_port_check_remote_port(const char* remote_app_id, const char *remote_port, bool* exist)
173 if (remote_app_id == NULL || remote_port == NULL)
175 _LOGE("[MESSAGE_PORT_ERROR_INVALID_PARAMETER] NULL value is not allowed.");
176 return MESSAGE_PORT_ERROR_INVALID_PARAMETER;
178 _SECURE_LOGI("Check remote port (%s):(%s).", remote_app_id, remote_port);
179 return convert_to_tizen_error((messageport_error_e)messageport_check_remote_port(remote_app_id, remote_port, exist));
182 int message_port_check_trusted_remote_port(const char* remote_app_id, const char *remote_port, bool *exist)
184 if (remote_app_id == NULL || remote_port == NULL)
186 _LOGE("[MESSAGE_PORT_ERROR_INVALID_PARAMETER] NULL value is not allowed.");
187 return MESSAGE_PORT_ERROR_INVALID_PARAMETER;
189 _SECURE_LOGI("Check trusted remote port (%s):(%s).", remote_app_id, remote_port);
190 return convert_to_tizen_error((messageport_error_e)messageport_check_trusted_remote_port(remote_app_id, remote_port, exist));
193 int message_port_send_message(const char *remote_app_id, const char *remote_port, bundle *message)
195 if (remote_app_id == NULL || remote_port == NULL || message == NULL)
197 _LOGE("[MESSAGE_PORT_ERROR_INVALID_PARAMETER] NULL value is not allowed.");
198 return MESSAGE_PORT_ERROR_INVALID_PARAMETER;
200 return convert_to_tizen_error((messageport_error_e)messageport_send_message(remote_app_id, remote_port, message));
203 int message_port_send_trusted_message(const char *remote_app_id, const char *remote_port, bundle *message)
205 if (remote_app_id == NULL || remote_port == NULL || message == NULL)
207 _LOGE("[MESSAGE_PORT_ERROR_INVALID_PARAMETER] NULL value is not allowed.");
208 return MESSAGE_PORT_ERROR_INVALID_PARAMETER;
210 return convert_to_tizen_error((messageport_error_e)messageport_send_trusted_message(remote_app_id, remote_port, message));
213 int message_port_send_message_with_local_port(const char *remote_app_id, const char *remote_port, bundle *message, int local_port_id)
215 if (remote_app_id == NULL || remote_port == NULL || message == NULL)
217 _LOGE("[MESSAGE_PORT_ERROR_INVALID_PARAMETER] NULL value is not allowed.");
218 return MESSAGE_PORT_ERROR_INVALID_PARAMETER;
220 else if (local_port_id <= 0)
222 _LOGE("[MESSAGEPORT_ERROR_INVALID_PARAMETER] Neither 0 nor negative value is allowed.");
223 return MESSAGE_PORT_ERROR_INVALID_PARAMETER;
227 message_port_callback_item *item = NULL;
228 message_port_callback_item *trusted_item = NULL;
230 if (__listeners != NULL) {
231 item = (message_port_callback_item *)g_hash_table_lookup(__listeners, GINT_TO_POINTER(local_port_id));
233 if (item == NULL && __trusted_listeners != NULL) {
234 trusted_item = (message_port_callback_item *)g_hash_table_lookup(__trusted_listeners, GINT_TO_POINTER(local_port_id));
238 if (item == NULL && trusted_item == NULL)
240 _LOGE("[MESSAGE_PORT_ERROR_PORT_NOT_FOUND] The local port ID (%d) is not registered.", local_port_id);
241 return MESSAGE_PORT_ERROR_PORT_NOT_FOUND;
245 _SECURE_LOGI("Send a message to (%s):(%s) and listen at the local port ID (%d).", remote_app_id, remote_port, local_port_id);
246 return convert_to_tizen_error((messageport_error_e)messageport_send_bidirectional_message(local_port_id, remote_app_id, remote_port, message));
249 int message_port_send_trusted_message_with_local_port(const char* remote_app_id, const char *remote_port, bundle* message, int local_port_id)
251 if (remote_app_id == NULL || remote_port == NULL || message == NULL)
253 _LOGE("[MESSAGE_PORT_ERROR_INVALID_PARAMETER] NULL value is not allowed.");
254 return MESSAGE_PORT_ERROR_INVALID_PARAMETER;
256 else if (local_port_id <= 0)
258 _LOGE("[MESSAGEPORT_ERROR_INVALID_PARAMETER] Neither 0 nor negative value is allowed.");
259 return MESSAGE_PORT_ERROR_INVALID_PARAMETER;
262 message_port_callback_item *item = NULL;
263 message_port_callback_item *trusted_item = NULL;
265 if (__listeners != NULL) {
266 item = (message_port_callback_item *)g_hash_table_lookup(__listeners, GINT_TO_POINTER(local_port_id));
268 if (item == NULL && __trusted_listeners != NULL) {
269 trusted_item = (message_port_callback_item *)g_hash_table_lookup(__trusted_listeners, GINT_TO_POINTER(local_port_id));
273 if (item == NULL && trusted_item == NULL)
275 _LOGE("[MESSAGE_PORT_ERROR_PORT_NOT_FOUND] The local port ID (%d) is not registered.", local_port_id);
276 return MESSAGE_PORT_ERROR_PORT_NOT_FOUND;
280 _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);
281 return convert_to_tizen_error((messageport_error_e)messageport_send_bidirectional_trusted_message(local_port_id, remote_app_id, remote_port, message));