1 /* vi: set et sw=4 ts=4 cino=t0,(0: */
2 /* -*- Mode: C; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
4 * Copyright (C) 2013 Intel Corporation.
6 * Contact: Amarnath Valluri <amarnath.valluri@linux.intel.com>
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
24 #include "msgport-service.h"
25 #include "msgport-utils.h"
26 #include "common/dbus-service-glue.h"
27 #include "common/log.h"
31 struct _MsgPortService
35 MsgPortDbusGlueService *proxy;
36 guint on_messge_signal_id;
37 messageport_message_cb client_cb;
40 G_DEFINE_TYPE(MsgPortService, msgport_service, G_TYPE_OBJECT)
43 _service_dispose (GObject *self)
45 MsgPortService *service = MSGPORT_SERVICE (self);
47 g_clear_object (&service->proxy);
49 G_OBJECT_CLASS(msgport_service_parent_class)->dispose (self);
53 msgport_service_class_init (MsgPortServiceClass *klass)
55 GObjectClass *g_klass = G_OBJECT_CLASS(klass);
57 g_klass->dispose = _service_dispose;
61 msgport_service_init (MsgPortService *service)
63 service->proxy = NULL;
64 service->client_cb = NULL;
65 service->on_messge_signal_id = 0;
69 _on_got_message (MsgPortService *service, GVariant *data, const gchar *remote_app_id, const gchar *remote_port, gboolean remote_is_trusted, gpointer userdata)
72 gchar *str_data = g_variant_print (data, TRUE);
73 DBG ("Message received : '%s' from '%s':'%s':%d",
74 str_data, remote_app_id, remote_port, remote_is_trusted);
77 bundle *b = bundle_from_variant_map (data);
80 * NOTE: wrt plugin cannot handle empty strings for port_id and app_id.
81 * It is expecting NULL in this case, But we get empty stirng from Dbus.
82 * So check for this case:
84 if (remote_app_id && !remote_app_id[0]) remote_app_id = NULL;
85 if (remote_port && !remote_port[0]) remote_port = NULL;
87 service->client_cb (msgport_dbus_glue_service_get_id (service->proxy), remote_app_id, remote_port, remote_is_trusted, b);
93 msgport_service_new (GDBusConnection *connection, const gchar *path, messageport_message_cb message_cb)
97 MsgPortService *service = g_object_new (MSGPORT_TYPE_SERVICE, NULL);
102 service->proxy = msgport_dbus_glue_service_proxy_new_sync (connection,
103 G_DBUS_PROXY_FLAGS_NONE, NULL, path, NULL, &error);
104 if (!service->proxy) {
105 g_object_unref (service);
106 WARN ("failed create servie proxy for path '%s' : %s", path, error->message);
107 g_error_free (error);
111 service->client_cb = message_cb;
112 service->on_messge_signal_id = g_signal_connect_swapped (service->proxy, "on-message", G_CALLBACK (_on_got_message), service);
118 msgport_service_id (MsgPortService *service)
120 g_return_val_if_fail (service && MSGPORT_IS_SERVICE (service), 0);
121 g_return_val_if_fail (service->proxy && MSGPORT_DBUS_GLUE_IS_SERVICE (service->proxy), 0);
123 return msgport_dbus_glue_service_get_id (service->proxy);
127 msgport_service_name (MsgPortService *service)
129 g_return_val_if_fail (service && MSGPORT_IS_SERVICE (service), NULL);
130 g_return_val_if_fail (service->proxy && MSGPORT_DBUS_GLUE_IS_SERVICE (service->proxy), NULL);
132 return msgport_dbus_glue_service_get_port_name (service->proxy);
136 msgport_service_is_trusted (MsgPortService *service)
138 g_return_val_if_fail (service && MSGPORT_IS_SERVICE (service), FALSE);
139 g_return_val_if_fail (service->proxy && MSGPORT_DBUS_GLUE_IS_SERVICE (service->proxy), FALSE);
141 return msgport_dbus_glue_service_get_is_trusted (service->proxy);
145 msgport_service_set_message_handler (MsgPortService *service, messageport_message_cb handler)
147 g_return_if_fail (service && MSGPORT_IS_SERVICE (service));
149 service->client_cb = handler;
153 msgport_service_unregister (MsgPortService *service)
155 g_return_val_if_fail (service && MSGPORT_IS_SERVICE (service), FALSE);
156 g_return_val_if_fail (service->proxy, FALSE);
158 /* fire and forget */
159 return msgport_dbus_glue_service_call_unregister_sync (service->proxy, NULL, NULL);
163 msgport_service_send_message (MsgPortService *service, guint remote_service_id, GVariant *message)
165 GError *error = NULL;
166 g_return_val_if_fail (service && MSGPORT_IS_SERVICE (service), MESSAGEPORT_ERROR_IO_ERROR);
167 g_return_val_if_fail (service->proxy, MESSAGEPORT_ERROR_IO_ERROR);
168 g_return_val_if_fail (message, MESSAGEPORT_ERROR_INVALID_PARAMETER);
170 msgport_dbus_glue_service_call_send_message_sync (service->proxy, remote_service_id, message, NULL, &error);
173 messageport_error_e err = msgport_daemon_error_to_error (error);
174 WARN ("Fail to send message on service %p to %d : %s", service, remote_service_id, error->message);
175 g_error_free (error);
179 return MESSAGEPORT_ERROR_NONE;