Fix thread safe issue
[platform/core/appfw/message-port.git] / src / message_port.c
1 /*
2  * Copyright (c) 2014 Samsung Electronics Co., Ltd All Rights Reserved
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 #include <glib.h>
18 #include <stdlib.h>
19 #include <stdbool.h>
20 #include <pthread.h>
21 #include "message_port_common.h"
22 #include "message_port_local.h"
23 #include "message_port_remote.h"
24
25 #include "message_port_log.h"
26 #include "message_port.h"
27
28 EXPORT_API int message_port_register_local_port(const char *local_port, message_port_message_cb callback, void *user_data)
29 {
30         int local_port_id;
31
32         if (local_port == NULL || callback == NULL) {
33                 _LOGE("NULL value is not allowed.");
34                 return MESSAGE_PORT_ERROR_INVALID_PARAMETER;
35         }
36
37         message_port_lock_mutex();
38         local_port_id = register_message_port(local_port, false, callback, user_data);
39         message_port_unlock_mutex();
40
41         if (local_port_id > 0)
42                 _LOGD("Register local port ID (%d).", local_port_id);
43         else
44                 _LOGE("Register local port fail (%d).", local_port_id);
45
46         return local_port_id;
47 }
48
49 EXPORT_API int message_port_register_trusted_local_port(const char *local_port, message_port_trusted_message_cb callback, void *user_data)
50 {
51         int trusted_local_port_id;
52
53         if (local_port == NULL || callback == NULL) {
54                 _LOGE("NULL value is not allowed.");
55                 return MESSAGE_PORT_ERROR_INVALID_PARAMETER;
56         }
57
58         message_port_lock_mutex();
59         trusted_local_port_id = register_message_port(local_port, true, callback, user_data);
60         message_port_unlock_mutex();
61
62         if (trusted_local_port_id > 0)
63                 _LOGD("Register trusted local port ID (%d).", trusted_local_port_id);
64         else
65                 _LOGE("Register trusted local port fail (%d).", trusted_local_port_id);
66
67         return trusted_local_port_id;
68 }
69
70 EXPORT_API int message_port_unregister_local_port(int local_port_id)
71 {
72         int res;
73
74         if (local_port_id <= 0) {
75                 _LOGE("Neither 0 nor negative value is allowed.");
76                 return MESSAGE_PORT_ERROR_INVALID_PARAMETER;
77         }
78
79         message_port_lock_mutex();
80         res = unregister_local_port(local_port_id, false);
81         message_port_unlock_mutex();
82
83         return res;
84 }
85
86 EXPORT_API int message_port_unregister_trusted_local_port(int trusted_local_port_id)
87 {
88         int res;
89
90         if (trusted_local_port_id <= 0) {
91                 _LOGE("Neither 0 nor negative value is allowed.");
92                 return MESSAGE_PORT_ERROR_INVALID_PARAMETER;
93         }
94
95         message_port_lock_mutex();
96         res = unregister_local_port(trusted_local_port_id, true);
97         message_port_unlock_mutex();
98
99         return res;
100 }
101
102 EXPORT_API int message_port_check_remote_port(const char *remote_app_id, const char *remote_port, bool *exist)
103 {
104         int ret;
105
106         if (remote_app_id == NULL || remote_port == NULL) {
107                 _LOGE("NULL value is not allowed.");
108                 return MESSAGE_PORT_ERROR_INVALID_PARAMETER;
109         }
110         _LOGD("Check remote port (%s):(%s).", remote_app_id, remote_port);
111
112         message_port_lock_mutex();
113         ret = check_remote_port(remote_app_id, remote_port, false, exist);
114         message_port_unlock_mutex();
115
116         return ret;
117 }
118
119 EXPORT_API int message_port_check_trusted_remote_port(const char *remote_app_id, const char *remote_port, bool *exist)
120 {
121         int ret;
122
123         if (remote_app_id == NULL || remote_port == NULL) {
124                 _LOGE("NULL value is not allowed.");
125                 return MESSAGE_PORT_ERROR_INVALID_PARAMETER;
126         }
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();
131
132         return ret;
133 }
134
135 EXPORT_API int message_port_send_message(const char *remote_app_id, const char *remote_port, bundle *message)
136 {
137         int ret;
138
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;
142         }
143         _SECURE_LOGI("Send a message to (%s):(%s).", remote_app_id, remote_port);
144
145         message_port_lock_mutex();
146         ret = send_message(remote_app_id, remote_port, NULL, false, false, false, message);
147         message_port_unlock_mutex();
148
149         return ret;
150 }
151
152 EXPORT_API int message_port_send_trusted_message(const char *remote_app_id, const char *remote_port, bundle *message)
153 {
154         int ret;
155
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;
159         }
160         _SECURE_LOGI("Send a trusted message to (%s):(%s).", remote_app_id, remote_port);
161
162         message_port_lock_mutex();
163         ret = send_message(remote_app_id, remote_port, NULL, true, false, false, message);
164         message_port_unlock_mutex();
165
166         return ret;
167 }
168
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)
170 {
171         int ret;
172
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;
179         }
180         _SECURE_LOGI("Send a message to (%s):(%s) - (%d).", remote_app_id, remote_port, local_port_id);
181
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();
185
186         return ret;
187 }
188
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)
190 {
191         int ret;
192
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;
199         }
200         _SECURE_LOGI("Send a trusted message to (%s):(%s) - (%d).", remote_app_id, remote_port, local_port_id);
201
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();
205
206         return ret;
207 }
208
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)
210 {
211         int ret;
212
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;
217         }
218
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);
222
223
224         return ret;
225 }
226
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)
228 {
229         int ret;
230
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;
235         }
236
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);
240
241
242         return ret;
243 }
244
245 EXPORT_API int message_port_remove_registration_event_cb(int watcher_id)
246 {
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;
251         }
252
253         ret = remove_registration_event_cb(watcher_id);
254         return ret;
255 }