1 // Copyright 2015 Samsung Electronics Co, Ltd. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "common/dbus_client.h"
7 #include "common/logger.h"
13 void OnSignalReceived(GDBusConnection* /*connection*/,
14 const gchar* /*sender_name*/,
15 const gchar* /*object_path*/,
16 const gchar* interface_name,
17 const gchar* signal_name,
20 DBusClient* self = reinterpret_cast<DBusClient*>(user_data);
21 auto callback = self->GetSignalCallback(interface_name);
23 callback(signal_name, parameters);
29 DBusClient::DBusClient()
31 signal_subscription_id_(0) {
34 DBusClient::~DBusClient() {
36 g_dbus_connection_signal_unsubscribe(connection_, signal_subscription_id_);
37 g_dbus_connection_close_sync(connection_, NULL, NULL);
41 bool DBusClient::ConnectByName(const std::string& name) {
42 std::string address("unix:path=");
43 address.append(g_get_user_runtime_dir());
46 return Connect(address);
49 bool DBusClient::Connect(const std::string& address) {
51 connection_ = g_dbus_connection_new_for_address_sync(
53 G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT,
56 LOGGER(ERROR) << "Failed to connect to bus address " << address
57 << " : " << err->message;
62 signal_subscription_id_ = g_dbus_connection_signal_subscribe(
63 connection_, NULL, NULL, NULL, NULL, NULL, G_DBUS_SIGNAL_FLAGS_NONE,
64 OnSignalReceived, this, NULL);
69 GVariant* DBusClient::Call(const std::string& iface,
70 const std::string& method,
72 const GVariantType* reply_type) {
78 GVariant* reply = NULL;
81 reply = g_dbus_connection_call_sync(
82 connection_, NULL, "/", iface.c_str(), method.c_str(), parameters,
83 reply_type, G_DBUS_CALL_FLAGS_NONE, -1, NULL, &err);
85 LOGGER(ERROR) << "Failed to CallSync : " << err->message;
89 g_dbus_connection_call(
90 connection_, NULL, "/", iface.c_str(), method.c_str(), parameters,
91 NULL, G_DBUS_CALL_FLAGS_NONE, -1, NULL, NULL, NULL);
97 void DBusClient::SetSignalCallback(const std::string& iface,
98 SignalCallback func) {
99 signal_callbacks_[iface] = func;
102 DBusClient::SignalCallback
103 DBusClient::GetSignalCallback(const std::string& iface) {
104 return signal_callbacks_[iface];