1 /* vi: set et sw=4 ts=4 cino=t0,(0: */
2 /* -*- Mode: C; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
4 * This file is part of message-port.
6 * Copyright (C) 2013 Intel Corporation.
8 * Contact: Amarnath Valluri <amarnath.valluri@linux.intel.com>
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.
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.
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
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"
33 _messageport_register_port (const char *name, gboolean is_trusted, messageport_message_cb cb)
35 int port_id = 0; /* id of the port created */
36 messageport_error_e res;
37 MsgPortManager *manager = msgport_factory_get_manager ();
39 if (!manager) return MESSAGEPORT_ERROR_IO_ERROR;
41 res = msgport_manager_register_service (manager, name, is_trusted, cb, &port_id);
43 return port_id > 0 ? port_id : (int)res;
46 static messageport_error_e
47 _messageport_check_remote_port (const char *app_id, const char *port, gboolean is_trusted, bool *exists)
49 messageport_error_e res;
50 MsgPortManager *manager = msgport_factory_get_manager ();
52 if (!manager) return MESSAGEPORT_ERROR_IO_ERROR;
54 res = msgport_manager_check_remote_service (manager, app_id, port, is_trusted, NULL);
56 if (exists) *exists = (bool)(res == MESSAGEPORT_ERROR_NONE);
61 static messageport_error_e
62 _messageport_send_message (const char *app_id, const char *port, gboolean is_trusted, bundle *message)
64 MsgPortManager *manager = msgport_factory_get_manager ();
66 if (!manager) return MESSAGEPORT_ERROR_IO_ERROR;
68 GVariant *v_data = bundle_to_variant_map (message);
70 return msgport_manager_send_message (manager, app_id, port, is_trusted, v_data);
74 _messageport_send_bidirectional_message (int id, const gchar *remote_app_id, const gchar *remote_port, gboolean is_trusted, bundle *message)
76 MsgPortManager *manager = msgport_factory_get_manager ();
78 if (!manager) return MESSAGEPORT_ERROR_IO_ERROR;
80 GVariant *v_data = bundle_to_variant_map (message);
82 return msgport_manager_send_bidirectional_message (manager, id, remote_app_id, remote_port, is_trusted, v_data);
90 messageport_register_local_port(const char* local_port, messageport_message_cb callback)
92 return _messageport_register_port (local_port, FALSE, callback);
96 messageport_register_trusted_local_port (const char *local_port, messageport_message_cb callback)
98 return _messageport_register_port (local_port, TRUE, callback);
102 messageport_check_remote_port (const char *remote_app_id, const char *port_name, bool *exists)
104 return _messageport_check_remote_port (remote_app_id, port_name, FALSE, exists);
108 messageport_check_trusted_remote_port (const char *remote_app_id, const char *port_name, bool *exists)
110 return _messageport_check_remote_port (remote_app_id, port_name, TRUE, exists);
114 messageport_send_message (const char* remote_app_id, const char* remote_port, bundle* message)
116 return _messageport_send_message (remote_app_id, remote_port, FALSE, message);
120 messageport_send_trusted_message(const char* remote_app_id, const char* remote_port, bundle* message)
122 return _messageport_send_message (remote_app_id, remote_port, TRUE, message);
126 messageport_send_bidirectional_message(int id, const char* remote_app_id, const char* remote_port, bundle* data)
128 return _messageport_send_bidirectional_message (id, remote_app_id, remote_port, FALSE, data);
132 messageport_send_bidirectional_trusted_message (int id, const char *remote_app_id, const char *remote_port, bundle *data)
134 return _messageport_send_bidirectional_message (id, remote_app_id, remote_port, TRUE, data);
138 messageport_get_local_port_name(int id, char **name_out)
140 MsgPortManager *manager = msgport_factory_get_manager ();
142 if (!manager) return MESSAGEPORT_ERROR_IO_ERROR;
144 return msgport_manager_get_service_name (manager, id, name_out);
148 messageport_check_trusted_local_port (int id, bool *is_trusted_out)
150 MsgPortManager *manager = msgport_factory_get_manager ();
152 if (!manager) return MESSAGEPORT_ERROR_IO_ERROR;
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;