Bug Fix : XWALK-1885 - use bool as guint8
[profile/ivi/message-port.git] / lib / message-port.c
1 /* vi: set et sw=4 ts=4 cino=t0,(0: */
2 /* -*- Mode: C; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 /*
4  * This file is part of message-port.
5  *
6  * Copyright (C) 2013 Intel Corporation.
7  *
8  * Contact: Amarnath Valluri <amarnath.valluri@linux.intel.com>
9  *
10  * This library is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU Lesser General Public
12  * License as published by the Free Software Foundation; either
13  * version 2.1 of the License, or (at your option) any later version.
14  *
15  * This library is distributed in the hope that it will be useful, but
16  * WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18  * Lesser General Public License for more details.
19  *
20  * You should have received a copy of the GNU Lesser General Public
21  * License along with this library; if not, write to the Free Software
22  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
23  * 02110-1301 USA
24  */
25
26 #include "message-port.h"
27 #include "msgport-factory.h"
28 #include "msgport-manager.h"
29 #include "msgport-utils.h"
30 #include "common/log.h"
31
32 static int
33 _messageport_register_port (const char *name, gboolean is_trusted, messageport_message_cb cb)
34 {
35     int port_id = 0; /* id of the port created */
36     messageport_error_e res;
37     MsgPortManager *manager = msgport_factory_get_manager ();
38
39     if (!manager) return MESSAGEPORT_ERROR_IO_ERROR;
40
41     res = msgport_manager_register_service (manager, name, is_trusted, cb, &port_id);
42
43     return port_id > 0 ? port_id : (int)res;
44 }
45
46 static messageport_error_e
47 _messageport_check_remote_port (const char *app_id, const char *port, gboolean is_trusted, bool *exists)
48 {
49     messageport_error_e res;
50     MsgPortManager *manager = msgport_factory_get_manager ();
51
52     if (!manager) return MESSAGEPORT_ERROR_IO_ERROR;
53
54     res = msgport_manager_check_remote_service (manager, app_id, port, is_trusted, NULL);
55
56     if (exists) *exists = (bool)(res == MESSAGEPORT_ERROR_NONE);
57
58     return res;
59 }
60
61 static messageport_error_e
62 _messageport_send_message (const char *app_id, const char *port, gboolean is_trusted, bundle *message)
63 {
64     MsgPortManager *manager = msgport_factory_get_manager ();
65
66     if (!manager) return MESSAGEPORT_ERROR_IO_ERROR;
67
68     GVariant *v_data = bundle_to_variant_map (message);
69
70     return msgport_manager_send_message (manager, app_id, port, is_trusted, v_data);
71 }
72
73 messageport_error_e
74 _messageport_send_bidirectional_message (int id, const gchar *remote_app_id, const gchar *remote_port, gboolean is_trusted, bundle *message)
75 {
76     MsgPortManager *manager = msgport_factory_get_manager ();
77
78     if (!manager) return MESSAGEPORT_ERROR_IO_ERROR;
79
80     GVariant *v_data = bundle_to_variant_map (message);
81
82     return msgport_manager_send_bidirectional_message (manager, id, remote_app_id, remote_port, is_trusted, v_data);
83 }
84
85 /*
86  * API
87  */
88
89 int
90 messageport_register_local_port(const char* local_port, messageport_message_cb callback)
91 {
92     return _messageport_register_port (local_port, FALSE, callback);
93 }
94
95 messageport_error_e
96 messageport_register_trusted_local_port (const char *local_port, messageport_message_cb callback)
97 {
98     return _messageport_register_port (local_port, TRUE, callback);
99 }
100
101 messageport_error_e
102 messageport_check_remote_port (const char *remote_app_id, const char *port_name, bool *exists)
103 {
104     return _messageport_check_remote_port (remote_app_id, port_name, FALSE, exists);
105 }
106
107 messageport_error_e
108 messageport_check_trusted_remote_port (const char *remote_app_id, const char *port_name, bool *exists)
109 {
110     return _messageport_check_remote_port (remote_app_id, port_name, TRUE, exists);
111 }
112
113 messageport_error_e
114 messageport_send_message (const char* remote_app_id, const char* remote_port, bundle* message)
115 {
116     return _messageport_send_message (remote_app_id, remote_port, FALSE, message);
117 }
118
119 messageport_error_e
120 messageport_send_trusted_message(const char* remote_app_id, const char* remote_port, bundle* message)
121 {
122     return _messageport_send_message (remote_app_id, remote_port, TRUE, message);
123 }
124
125 messageport_error_e
126 messageport_send_bidirectional_message(int id, const char* remote_app_id, const char* remote_port, bundle* data)
127 {
128     return _messageport_send_bidirectional_message (id, remote_app_id, remote_port, FALSE, data);
129 }
130
131 messageport_error_e
132 messageport_send_bidirectional_trusted_message (int id, const char *remote_app_id, const char *remote_port, bundle *data)
133 {
134     return _messageport_send_bidirectional_message (id, remote_app_id, remote_port, TRUE, data);
135 }
136
137 messageport_error_e
138 messageport_get_local_port_name(int id, char **name_out)
139 {
140     MsgPortManager *manager = msgport_factory_get_manager ();
141
142     if (!manager) return MESSAGEPORT_ERROR_IO_ERROR;
143
144     return msgport_manager_get_service_name (manager, id, name_out);
145 }
146
147 messageport_error_e
148 messageport_check_trusted_local_port (int id, bool *is_trusted_out)
149 {
150     MsgPortManager *manager = msgport_factory_get_manager ();
151
152     if (!manager) return MESSAGEPORT_ERROR_IO_ERROR;
153
154     gboolean is_trusted = FALSE;
155     messageport_error_e res = msgport_manager_get_service_is_trusted (
156             manager, id, &is_trusted);
157     if (is_trusted_out) *is_trusted_out = (bool)is_trusted;
158
159     return res;
160 }