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_common.h"
22 #include "message_port_local.h"
23 #include "message_port_remote.h"
25 #include "message_port_log.h"
26 #include "message_port.h"
28 EXPORT_API int message_port_register_local_port(const char *local_port, message_port_message_cb callback, void *user_data)
32 if (local_port == NULL || callback == NULL) {
33 _LOGE("NULL value is not allowed.");
34 return MESSAGE_PORT_ERROR_INVALID_PARAMETER;
37 message_port_lock_mutex();
38 local_port_id = register_message_port(local_port, false, callback, user_data);
39 message_port_unlock_mutex();
41 if (local_port_id > 0)
42 _LOGD("Register local port ID (%d).", local_port_id);
44 _LOGE("Register local port fail (%d).", local_port_id);
49 EXPORT_API int message_port_register_trusted_local_port(const char *local_port, message_port_trusted_message_cb callback, void *user_data)
51 int trusted_local_port_id;
53 if (local_port == NULL || callback == NULL) {
54 _LOGE("NULL value is not allowed.");
55 return MESSAGE_PORT_ERROR_INVALID_PARAMETER;
58 message_port_lock_mutex();
59 trusted_local_port_id = register_message_port(local_port, true, callback, user_data);
60 message_port_unlock_mutex();
62 if (trusted_local_port_id > 0)
63 _LOGD("Register trusted local port ID (%d).", trusted_local_port_id);
65 _LOGE("Register trusted local port fail (%d).", trusted_local_port_id);
67 return trusted_local_port_id;
70 EXPORT_API int message_port_unregister_local_port(int local_port_id)
74 if (local_port_id <= 0) {
75 _LOGE("Neither 0 nor negative value is allowed.");
76 return MESSAGE_PORT_ERROR_INVALID_PARAMETER;
79 message_port_lock_mutex();
80 res = unregister_local_port(local_port_id, false);
81 message_port_unlock_mutex();
86 EXPORT_API int message_port_unregister_trusted_local_port(int trusted_local_port_id)
90 if (trusted_local_port_id <= 0) {
91 _LOGE("Neither 0 nor negative value is allowed.");
92 return MESSAGE_PORT_ERROR_INVALID_PARAMETER;
95 message_port_lock_mutex();
96 res = unregister_local_port(trusted_local_port_id, true);
97 message_port_unlock_mutex();
102 EXPORT_API int message_port_check_remote_port(const char *remote_app_id, const char *remote_port, bool *exist)
106 if (remote_app_id == NULL || remote_port == NULL) {
107 _LOGE("NULL value is not allowed.");
108 return MESSAGE_PORT_ERROR_INVALID_PARAMETER;
110 _LOGD("Check remote port (%s):(%s).", remote_app_id, remote_port);
112 message_port_lock_mutex();
113 ret = check_remote_port(remote_app_id, remote_port, false, exist);
114 message_port_unlock_mutex();
119 EXPORT_API int message_port_check_trusted_remote_port(const char *remote_app_id, const char *remote_port, bool *exist)
123 if (remote_app_id == NULL || remote_port == NULL) {
124 _LOGE("NULL value is not allowed.");
125 return MESSAGE_PORT_ERROR_INVALID_PARAMETER;
127 _LOGD("Check trusted remote port (%s):(%s).", remote_app_id, remote_port);
128 message_port_lock_mutex();
129 ret = check_remote_port(remote_app_id, remote_port, true, exist);
130 message_port_unlock_mutex();
135 EXPORT_API int message_port_send_message(const char *remote_app_id, const char *remote_port, bundle *message)
139 if (remote_app_id == NULL || remote_port == NULL || message == NULL) {
140 _LOGE("NULL value is not allowed.");
141 return MESSAGE_PORT_ERROR_INVALID_PARAMETER;
143 _SECURE_LOGI("Send a message to (%s):(%s).", remote_app_id, remote_port);
145 message_port_lock_mutex();
146 ret = send_message(remote_app_id, remote_port, NULL, false, false, false, message);
147 message_port_unlock_mutex();
152 EXPORT_API int message_port_send_trusted_message(const char *remote_app_id, const char *remote_port, bundle *message)
156 if (remote_app_id == NULL || remote_port == NULL || message == NULL) {
157 _LOGE("[MESSAGE_PORT_ERROR_INVALID_PARAMETER] NULL value is not allowed.");
158 return MESSAGE_PORT_ERROR_INVALID_PARAMETER;
160 _SECURE_LOGI("Send a trusted message to (%s):(%s).", remote_app_id, remote_port);
162 message_port_lock_mutex();
163 ret = send_message(remote_app_id, remote_port, NULL, true, false, false, message);
164 message_port_unlock_mutex();
169 EXPORT_API int message_port_send_message_with_local_port(const char *remote_app_id, const char *remote_port, bundle *message, int local_port_id)
173 if (remote_app_id == NULL || remote_port == NULL || message == NULL) {
174 _LOGE("[MESSAGE_PORT_ERROR_INVALID_PARAMETER] NULL value is not allowed.");
175 return MESSAGE_PORT_ERROR_INVALID_PARAMETER;
176 } else if (local_port_id <= 0) {
177 _LOGE("[MESSAGEPORT_ERROR_INVALID_PARAMETER] Neither 0 nor negative value is allowed.");
178 return MESSAGE_PORT_ERROR_INVALID_PARAMETER;
180 _SECURE_LOGI("Send a message to (%s):(%s) - (%d).", remote_app_id, remote_port, local_port_id);
182 message_port_lock_mutex();
183 ret = send_bidirectional_message(local_port_id, remote_app_id, remote_port, false, message);
184 message_port_unlock_mutex();
189 EXPORT_API int message_port_send_trusted_message_with_local_port(const char *remote_app_id, const char *remote_port, bundle *message, int local_port_id)
193 if (remote_app_id == NULL || remote_port == NULL || message == NULL) {
194 _LOGE("[MESSAGE_PORT_ERROR_INVALID_PARAMETER] NULL value is not allowed.");
195 return MESSAGE_PORT_ERROR_INVALID_PARAMETER;
196 } else if (local_port_id <= 0) {
197 _LOGE("[MESSAGEPORT_ERROR_INVALID_PARAMETER] Neither 0 nor negative value is allowed.");
198 return MESSAGE_PORT_ERROR_INVALID_PARAMETER;
200 _SECURE_LOGI("Send a trusted message to (%s):(%s) - (%d).", remote_app_id, remote_port, local_port_id);
202 message_port_lock_mutex();
203 ret = send_bidirectional_message(local_port_id, remote_app_id, remote_port, true, message);
204 message_port_unlock_mutex();
209 EXPORT_API 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)
213 if (watcher_id == NULL || remote_app_id == NULL || remote_port == NULL ||
214 registered_cb == NULL) {
215 _LOGE("[MESSAGE_PORT_ERROR_INVALID_PARAMETER] NULL value is not allowed.");
216 return MESSAGE_PORT_ERROR_INVALID_PARAMETER;
219 ret = watch_remote_port(watcher_id, remote_app_id, remote_port, is_trusted, registered_cb, NULL, user_data);
220 if (ret != MESSAGE_PORT_ERROR_NONE)
221 _SECURE_LOGI("add registered callback fail (%d).", ret);
227 EXPORT_API 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)
231 if (watcher_id == NULL || remote_app_id == NULL || remote_port == NULL ||
232 unregistered_cb == NULL) {
233 _LOGE("[MESSAGE_PORT_ERROR_INVALID_PARAMETER] NULL value is not allowed.");
234 return MESSAGE_PORT_ERROR_INVALID_PARAMETER;
237 ret = watch_remote_port(watcher_id, remote_app_id, remote_port, is_trusted, NULL, unregistered_cb, user_data);
238 if (ret != MESSAGE_PORT_ERROR_NONE)
239 _SECURE_LOGI("add registered callback fail (%d).", ret);
245 EXPORT_API int message_port_remove_registration_event_cb(int watcher_id)
247 int ret = MESSAGE_PORT_ERROR_NONE;
248 if (watcher_id < 1) {
249 _LOGE("[MESSAGE_PORT_ERROR_INVALID_PARAMETER] Invalid watcher_id.");
250 return MESSAGE_PORT_ERROR_INVALID_PARAMETER;
253 ret = remove_registration_event_cb(watcher_id);