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);
43 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)
45 message_port_callback_item *item = NULL;
46 if (__listeners == NULL)
48 item = (message_port_callback_item *)g_hash_table_lookup(__listeners, GINT_TO_POINTER(local_port_id));
50 do_callback(item->callback, local_port_id, remote_app_id, remote_port, trusted_remote_port, message, item->user_data);
53 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)
55 message_port_callback_item *item = NULL;
57 if (__trusted_listeners == NULL)
59 item = (message_port_callback_item *)g_hash_table_lookup(__trusted_listeners, GINT_TO_POINTER(trusted_local_port_id));
61 do_callback(item->callback, trusted_local_port_id, remote_app_id, remote_port, trusted_remote_port, message, item->user_data);
64 int message_port_register_local_port(const char *local_port, message_port_message_cb callback, void *user_data)
66 if (local_port == NULL || callback == NULL) {
67 _LOGE("[MESSAGE_PORT_ERROR_INVALID_PARAMETER] NULL value is not allowed.");
68 return MESSAGE_PORT_ERROR_INVALID_PARAMETER;
71 int local_port_id = messageport_register_local_port(local_port, message_dispatcher);
72 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 pthread_mutex_unlock(&__mutex);
85 return MESSAGE_PORT_ERROR_OUT_OF_MEMORY;
88 g_hash_table_insert(__listeners, GINT_TO_POINTER(local_port_id), item);
91 item->callback = callback;
92 item->user_data = user_data;
93 pthread_mutex_unlock(&__mutex);
96 _SECURE_LOGI("Register local port fail (%d).", local_port_id);
99 return convert_to_tizen_error((messageport_error_e)local_port_id);
102 int message_port_register_trusted_local_port(const char *local_port, message_port_trusted_message_cb callback, void *user_data)
104 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) {
111 _SECURE_LOGI("Register trusted local port ID (%d).", trusted_local_port_id);
113 if (__trusted_listeners == NULL)
114 __trusted_listeners = g_hash_table_new_full(g_direct_hash, g_direct_equal, NULL, g_free);
116 pthread_mutex_lock(&__mutex);
117 message_port_callback_item *item =
118 (message_port_callback_item *)g_hash_table_lookup(__trusted_listeners, GINT_TO_POINTER(trusted_local_port_id));
120 item = (message_port_callback_item *)calloc(1, sizeof(message_port_callback_item));
122 pthread_mutex_unlock(&__mutex);
123 return MESSAGE_PORT_ERROR_OUT_OF_MEMORY;
126 g_hash_table_insert(__trusted_listeners, GINT_TO_POINTER(trusted_local_port_id), item);
129 item->callback = callback;
130 item->user_data = user_data;
131 pthread_mutex_unlock(&__mutex);
133 _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) {
143 _LOGE("[MESSAGEPORT_ERROR_INVALID_PARAMETER] Neither 0 nor negative value is allowed.");
144 return MESSAGE_PORT_ERROR_INVALID_PARAMETER;
146 res = messageport_unregister_local_port(local_port_id, false);
147 g_hash_table_remove(__listeners, GINT_TO_POINTER(local_port_id));
149 return convert_to_tizen_error((messageport_error_e)res);
152 int message_port_unregister_trusted_local_port(int trusted_local_port_id)
155 int res = MESSAGE_PORT_ERROR_NONE;
156 if (trusted_local_port_id <= 0) {
157 _LOGE("[MESSAGEPORT_ERROR_INVALID_PARAMETER] Neither 0 nor negative value is allowed.");
158 return MESSAGE_PORT_ERROR_INVALID_PARAMETER;
160 res = messageport_unregister_local_port(trusted_local_port_id, true);
161 g_hash_table_remove(__trusted_listeners, GINT_TO_POINTER(trusted_local_port_id));
164 return convert_to_tizen_error((messageport_error_e)res);
167 int message_port_check_remote_port(const char *remote_app_id, const char *remote_port, bool *exist)
169 if (remote_app_id == NULL || remote_port == NULL) {
170 _LOGE("[MESSAGE_PORT_ERROR_INVALID_PARAMETER] NULL value is not allowed.");
171 return MESSAGE_PORT_ERROR_INVALID_PARAMETER;
173 _SECURE_LOGI("Check remote port (%s):(%s).", remote_app_id, remote_port);
174 return convert_to_tizen_error((messageport_error_e)messageport_check_remote_port(remote_app_id, remote_port, exist));
177 int message_port_check_trusted_remote_port(const char *remote_app_id, const char *remote_port, bool *exist)
179 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 trusted remote port (%s):(%s).", remote_app_id, remote_port);
184 return convert_to_tizen_error((messageport_error_e)messageport_check_trusted_remote_port(remote_app_id, remote_port, exist));
187 int message_port_send_message(const char *remote_app_id, const char *remote_port, bundle *message)
190 if (remote_app_id == NULL || remote_port == NULL || message == NULL) {
191 _LOGE("[MESSAGE_PORT_ERROR_INVALID_PARAMETER] NULL value is not allowed.");
192 return MESSAGE_PORT_ERROR_INVALID_PARAMETER;
195 _SECURE_LOGI("Send a message to (%s):(%s).", remote_app_id, remote_port);
196 pthread_mutex_lock(&__mutex);
197 ret = messageport_send_message(remote_app_id, remote_port, message);
198 pthread_mutex_unlock(&__mutex);
200 return convert_to_tizen_error((messageport_error_e)ret);
203 int message_port_send_trusted_message(const char *remote_app_id, const char *remote_port, bundle *message)
206 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 _SECURE_LOGI("Send a trusted message to (%s):(%s).", remote_app_id, remote_port);
212 pthread_mutex_lock(&__mutex);
213 ret = messageport_send_trusted_message(remote_app_id, remote_port, message);
214 pthread_mutex_unlock(&__mutex);
216 return convert_to_tizen_error((messageport_error_e)ret);
219 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) {
223 _LOGE("[MESSAGE_PORT_ERROR_INVALID_PARAMETER] NULL value is not allowed.");
224 return MESSAGE_PORT_ERROR_INVALID_PARAMETER;
225 } else if (local_port_id <= 0) {
226 _LOGE("[MESSAGEPORT_ERROR_INVALID_PARAMETER] Neither 0 nor negative value is allowed.");
227 return MESSAGE_PORT_ERROR_INVALID_PARAMETER;
230 message_port_callback_item *item = NULL;
231 message_port_callback_item *trusted_item = NULL;
233 if (__listeners != NULL)
234 item = (message_port_callback_item *)g_hash_table_lookup(__listeners, GINT_TO_POINTER(local_port_id));
236 if (item == NULL && __trusted_listeners != NULL)
237 trusted_item = (message_port_callback_item *)g_hash_table_lookup(__trusted_listeners, GINT_TO_POINTER(local_port_id));
240 if (item == NULL && trusted_item == NULL) {
241 _LOGE("[MESSAGE_PORT_ERROR_PORT_NOT_FOUND] The local port ID (%d) is not registered.", local_port_id);
242 return MESSAGE_PORT_ERROR_PORT_NOT_FOUND;
246 _SECURE_LOGI("Send a message to (%s):(%s) and listen at the local port ID (%d).", remote_app_id, remote_port, local_port_id);
247 pthread_mutex_lock(&__mutex);
248 ret = messageport_send_bidirectional_message(local_port_id, remote_app_id, remote_port, message);
249 pthread_mutex_unlock(&__mutex);
251 return convert_to_tizen_error((messageport_error_e)ret);
254 int message_port_send_trusted_message_with_local_port(const char *remote_app_id, const char *remote_port, bundle *message, int local_port_id)
257 if (remote_app_id == NULL || remote_port == NULL || message == NULL) {
258 _LOGE("[MESSAGE_PORT_ERROR_INVALID_PARAMETER] NULL value is not allowed.");
259 return MESSAGE_PORT_ERROR_INVALID_PARAMETER;
260 } else if (local_port_id <= 0) {
261 _LOGE("[MESSAGEPORT_ERROR_INVALID_PARAMETER] Neither 0 nor negative value is allowed.");
262 return MESSAGE_PORT_ERROR_INVALID_PARAMETER;
264 message_port_callback_item *item = NULL;
265 message_port_callback_item *trusted_item = NULL;
267 if (__listeners != NULL)
268 item = (message_port_callback_item *)g_hash_table_lookup(__listeners, GINT_TO_POINTER(local_port_id));
270 if (item == NULL && __trusted_listeners != NULL)
271 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) {
274 _LOGE("[MESSAGE_PORT_ERROR_PORT_NOT_FOUND] The local port ID (%d) is not registered.", local_port_id);
275 return MESSAGE_PORT_ERROR_PORT_NOT_FOUND;
279 _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);
280 pthread_mutex_lock(&__mutex);
281 ret = messageport_send_bidirectional_trusted_message(local_port_id, remote_app_id, remote_port, message);
282 pthread_mutex_unlock(&__mutex);
284 return convert_to_tizen_error((messageport_error_e)ret);