Apply tizen coding rules.
[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.h>
22 #include "message_port_internal.h"
23 #include "message_port_log.h"
24 #include "message_port.h"
25
26 typedef struct message_port_callback_item_s {
27         message_port_message_cb callback;
28         void *user_data;
29 } message_port_callback_item;
30
31 static GHashTable *__listeners;
32 static GHashTable *__trusted_listeners;
33 static pthread_mutex_t __mutex = PTHREAD_MUTEX_INITIALIZER;
34
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)
36 {
37         if (callback) {
38                 callback(local_port_id, remote_app_id, remote_port, trusted_remote_port, message, user_data);
39                 bundle_free(message);
40         } else {
41                 _LOGI("Ignored");
42         }
43 }
44
45 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)
46 {
47         message_port_callback_item *item =
48                 (message_port_callback_item *)g_hash_table_lookup(__listeners, GINT_TO_POINTER(local_port_id));
49         do_callback(item->callback, local_port_id, remote_app_id, remote_port, trusted_remote_port, message, item->user_data);
50 }
51
52 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)
53 {
54         message_port_callback_item *item =
55                 (message_port_callback_item *)g_hash_table_lookup(__trusted_listeners, GINT_TO_POINTER(trusted_local_port_id));
56         do_callback(item->callback, trusted_local_port_id, remote_app_id, remote_port, trusted_remote_port, message, item->user_data);
57 }
58
59 int message_port_register_local_port(const char *local_port, message_port_message_cb callback, void *user_data)
60 {
61         if (local_port == NULL || callback == NULL) {
62                 _LOGE("[MESSAGE_PORT_ERROR_INVALID_PARAMETER] NULL value is not allowed.");
63                 return MESSAGE_PORT_ERROR_INVALID_PARAMETER;
64         }
65
66         int local_port_id = messageport_register_local_port(local_port, message_dispatcher);
67         if (local_port_id > 0) {
68                 _SECURE_LOGI("Register local port ID (%d).", local_port_id);
69
70                 if (__listeners == NULL)
71                         __listeners = g_hash_table_new_full(g_direct_hash, g_direct_equal, NULL, g_free);
72
73                 pthread_mutex_lock(&__mutex);
74                 message_port_callback_item *item =
75                         (message_port_callback_item *)g_hash_table_lookup(__listeners, GINT_TO_POINTER(local_port_id));
76                 if (item == NULL) {
77                         item = (message_port_callback_item *)calloc(1, sizeof(message_port_callback_item));
78                         if (item == NULL)
79                                 return MESSAGE_PORT_ERROR_OUT_OF_MEMORY;
80
81                         g_hash_table_insert(__listeners, GINT_TO_POINTER(local_port_id), item);
82                 }
83
84                 item->callback = callback;
85                 item->user_data = user_data;
86                 pthread_mutex_unlock(&__mutex);
87
88         } else {
89                 _SECURE_LOGI("Register local port fail (%d).", local_port_id);
90         }
91
92         return convert_to_tizen_error((messageport_error_e)local_port_id);
93 }
94
95 int message_port_register_trusted_local_port(const char *local_port, message_port_trusted_message_cb callback, void *user_data)
96 {
97         if (local_port == NULL || callback == NULL) {
98                 _LOGE("[MESSAGE_PORT_ERROR_INVALID_PARAMETER] NULL value is not allowed.");
99                 return MESSAGE_PORT_ERROR_INVALID_PARAMETER;
100         }
101
102         int trusted_local_port_id = messageport_register_trusted_local_port(local_port, trusted_message_dispatcher);
103         if (trusted_local_port_id > 0) {
104                 _SECURE_LOGI("Register trusted local port ID (%d).", trusted_local_port_id);
105
106                 if (__trusted_listeners == NULL)
107                         __trusted_listeners = g_hash_table_new_full(g_direct_hash, g_direct_equal, NULL, g_free);
108
109                 pthread_mutex_lock(&__mutex);
110                 message_port_callback_item *item =
111                         (message_port_callback_item *)g_hash_table_lookup(__trusted_listeners, GINT_TO_POINTER(trusted_local_port_id));
112                 if (item == NULL) {
113                         item = (message_port_callback_item *)calloc(1, sizeof(message_port_callback_item));
114                         if (item == NULL)
115                                 return MESSAGE_PORT_ERROR_OUT_OF_MEMORY;
116
117                         g_hash_table_insert(__trusted_listeners, GINT_TO_POINTER(trusted_local_port_id), item);
118                 }
119
120                 item->callback = callback;
121                 item->user_data = user_data;
122                 pthread_mutex_unlock(&__mutex);
123         } else {
124                 _SECURE_LOGI("Register trusted local port fail (%d).", trusted_local_port_id);
125         }
126
127         return convert_to_tizen_error((messageport_error_e)trusted_local_port_id);
128 }
129
130 int message_port_unregister_local_port(int local_port_id)
131 {
132         int res = MESSAGE_PORT_ERROR_NONE;
133         if (local_port_id <= 0) {
134                 _LOGE("[MESSAGEPORT_ERROR_INVALID_PARAMETER] Neither 0 nor negative value is allowed.");
135                 return MESSAGE_PORT_ERROR_INVALID_PARAMETER;
136         } else {
137                 res = messageport_unregister_local_port(local_port_id, false);
138                 g_hash_table_remove(__listeners, GINT_TO_POINTER(local_port_id));
139         }
140         return convert_to_tizen_error((messageport_error_e)res);
141 }
142
143 int message_port_unregister_trusted_local_port(int trusted_local_port_id)
144 {
145
146         int res = MESSAGE_PORT_ERROR_NONE;
147         if (trusted_local_port_id <= 0) {
148                 _LOGE("[MESSAGEPORT_ERROR_INVALID_PARAMETER] Neither 0 nor negative value is allowed.");
149                 return MESSAGE_PORT_ERROR_INVALID_PARAMETER;
150         } else {
151                 res = messageport_unregister_local_port(trusted_local_port_id, true);
152                 g_hash_table_remove(__trusted_listeners, GINT_TO_POINTER(trusted_local_port_id));
153         }
154
155         return convert_to_tizen_error((messageport_error_e)res);
156 }
157
158 int message_port_check_remote_port(const char* remote_app_id, const char *remote_port, bool* exist)
159 {
160         if (remote_app_id == NULL || remote_port == NULL) {
161                 _LOGE("[MESSAGE_PORT_ERROR_INVALID_PARAMETER] NULL value is not allowed.");
162                 return MESSAGE_PORT_ERROR_INVALID_PARAMETER;
163         }
164         _SECURE_LOGI("Check remote port (%s):(%s).", remote_app_id, remote_port);
165         return convert_to_tizen_error((messageport_error_e)messageport_check_remote_port(remote_app_id, remote_port, exist));
166 }
167
168 int message_port_check_trusted_remote_port(const char* remote_app_id, const char *remote_port, bool *exist)
169 {
170         if (remote_app_id == NULL || remote_port == NULL) {
171                 _LOGE("[MESSAGE_PORT_ERROR_INVALID_PARAMETER] NULL value is not allowed.");
172                 return MESSAGE_PORT_ERROR_INVALID_PARAMETER;
173         }
174         _SECURE_LOGI("Check trusted remote port (%s):(%s).", remote_app_id, remote_port);
175         return convert_to_tizen_error((messageport_error_e)messageport_check_trusted_remote_port(remote_app_id, remote_port, exist));
176 }
177
178 int message_port_send_message(const char *remote_app_id, const char *remote_port, bundle *message)
179 {
180         if (remote_app_id == NULL || remote_port == NULL || message == NULL) {
181                 _LOGE("[MESSAGE_PORT_ERROR_INVALID_PARAMETER] NULL value is not allowed.");
182                 return MESSAGE_PORT_ERROR_INVALID_PARAMETER;
183         }
184         return convert_to_tizen_error((messageport_error_e)messageport_send_message(remote_app_id, remote_port, message));
185 }
186
187 int message_port_send_trusted_message(const char *remote_app_id, const char *remote_port, bundle *message)
188 {
189         if (remote_app_id == NULL || remote_port == NULL || message == NULL) {
190                 _LOGE("[MESSAGE_PORT_ERROR_INVALID_PARAMETER] NULL value is not allowed.");
191                 return MESSAGE_PORT_ERROR_INVALID_PARAMETER;
192         }
193         return convert_to_tizen_error((messageport_error_e)messageport_send_trusted_message(remote_app_id, remote_port, message));
194 }
195
196 int message_port_send_message_with_local_port(const char *remote_app_id, const char *remote_port, bundle *message, int local_port_id)
197 {
198         if (remote_app_id == NULL || remote_port == NULL || message == NULL) {
199                 _LOGE("[MESSAGE_PORT_ERROR_INVALID_PARAMETER] NULL value is not allowed.");
200                 return MESSAGE_PORT_ERROR_INVALID_PARAMETER;
201         } else if (local_port_id <= 0) {
202                 _LOGE("[MESSAGEPORT_ERROR_INVALID_PARAMETER] Neither 0 nor negative value is allowed.");
203                 return MESSAGE_PORT_ERROR_INVALID_PARAMETER;
204         } else {
205
206                 message_port_callback_item *item = NULL;
207                 message_port_callback_item *trusted_item = NULL;
208
209                 if (__listeners != NULL)
210                         item = (message_port_callback_item *)g_hash_table_lookup(__listeners, GINT_TO_POINTER(local_port_id));
211
212                 if (item == NULL && __trusted_listeners != NULL)
213                         trusted_item = (message_port_callback_item *)g_hash_table_lookup(__trusted_listeners, GINT_TO_POINTER(local_port_id));
214
215
216                 if (item == NULL && trusted_item == NULL) {
217                         _LOGE("[MESSAGE_PORT_ERROR_PORT_NOT_FOUND] The local port ID (%d) is not registered.", local_port_id);
218                         return MESSAGE_PORT_ERROR_PORT_NOT_FOUND;
219                 }
220         }
221
222         _SECURE_LOGI("Send a message to (%s):(%s) and listen at the local port ID (%d).", remote_app_id, remote_port, local_port_id);
223         return convert_to_tizen_error((messageport_error_e)messageport_send_bidirectional_message(local_port_id, remote_app_id, remote_port, message));
224 }
225
226 int message_port_send_trusted_message_with_local_port(const char* remote_app_id, const char *remote_port, bundle* message, int local_port_id)
227 {
228         if (remote_app_id == NULL || remote_port == NULL || message == NULL) {
229                 _LOGE("[MESSAGE_PORT_ERROR_INVALID_PARAMETER] NULL value is not allowed.");
230                 return MESSAGE_PORT_ERROR_INVALID_PARAMETER;
231         } else if (local_port_id <= 0) {
232                 _LOGE("[MESSAGEPORT_ERROR_INVALID_PARAMETER] Neither 0 nor negative value is allowed.");
233                 return MESSAGE_PORT_ERROR_INVALID_PARAMETER;
234         } else {
235                 message_port_callback_item *item = NULL;
236                 message_port_callback_item *trusted_item = NULL;
237
238                 if (__listeners != NULL)
239                         item = (message_port_callback_item *)g_hash_table_lookup(__listeners, GINT_TO_POINTER(local_port_id));
240
241                 if (item == NULL && __trusted_listeners != NULL)
242                         trusted_item = (message_port_callback_item *)g_hash_table_lookup(__trusted_listeners, GINT_TO_POINTER(local_port_id));
243
244                 if (item == NULL && trusted_item == NULL) {
245                         _LOGE("[MESSAGE_PORT_ERROR_PORT_NOT_FOUND] The local port ID (%d) is not registered.", local_port_id);
246                         return MESSAGE_PORT_ERROR_PORT_NOT_FOUND;
247                 }
248         }
249
250         _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);
251         return convert_to_tizen_error((messageport_error_e)messageport_send_bidirectional_trusted_message(local_port_id, remote_app_id, remote_port, message));
252 }
253