blob: 81840c53bc114092e345befa3e339a2b10d78983 [file] [log] [blame]
rspangler@google.com570c65b2009-10-09 20:56:14 +00001// Copyright (c) 2009 The Chromium OS Authors. 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.
4
5#ifndef CHROMEOS_DBUS_H_
6#define CHROMEOS_DBUS_H_
7
8#include <dbus/dbus-glib.h>
9#include <glib-object.h>
rspangler@google.com570c65b2009-10-09 20:56:14 +000010
11#include <algorithm>
12#include <string>
13
seanparent@google.comf2ea0242009-11-04 21:04:51 +000014#include "base/logging.h"
rspangler@google.com570c65b2009-10-09 20:56:14 +000015#include "chromeos/glib/object.h"
16
17namespace chromeos {
18
19// \precondition No functions in the dbus namespace can be called before
20// ::g_type_init();
21
22namespace dbus {
23
24// \brief BusConnection manages the ref-count for a ::DBusGConnection*.
25//
26// A BusConnection has reference semantics bound to a particular communication
27// bus.
28//
29// \models Copyable, Assignable
30// \related GetSystemBusConnection()
31
32class BusConnection {
33 public:
34 typedef ::DBusGConnection* value_type;
35
36 BusConnection(const BusConnection& x)
37 : object_(dbus_g_connection_ref(x.object_)) {
38 }
39
40 ~BusConnection() {
41 ::dbus_g_connection_unref(object_);
42 }
43
44 BusConnection& operator=(BusConnection x) {
45 swap(*this, x);
46 return *this;
47 }
48
Yusuke Satof8059812010-01-09 20:13:07 +090049 const value_type& g_connection() const {
50 DCHECK(object_) << "referencing an empty connection";
51 return object_;
52 }
53
rspangler@google.com570c65b2009-10-09 20:56:14 +000054 private:
55 friend void swap(BusConnection& x, BusConnection& y);
56
57 friend class Proxy;
58 friend BusConnection GetSystemBusConnection();
Yusuke Sato71da5ce2009-12-15 10:29:43 +090059 friend BusConnection GetPrivateBusConnection(const char* address);
rspangler@google.com570c65b2009-10-09 20:56:14 +000060
61 // Constructor takes ownership
62 explicit BusConnection(::DBusGConnection* x)
63 : object_(x) {
64 DCHECK(object_) << "Constructing BusConnection with NULL object.";
65 }
66
67 value_type object_;
68};
69
70inline void swap(BusConnection& x, BusConnection& y) {
71 std::swap(x.object_, y.object_);
72}
73
74// \brief Proxy manages the ref-count for a ::DBusGProxy*.
75//
76// Proxy has reference semantics and represents a connection to on object on
77// the bus. A proxy object is constructed with a connection to a bus, a name
78// to an entity on the bus, a path to an object owned by the entity, and an
79// interface protocol name used to communicate with the object.
80
81class Proxy {
82 public:
83 typedef ::DBusGProxy* value_type;
84
85 Proxy()
86 : object_(NULL) {
87 }
88
Yusuke Sato71da5ce2009-12-15 10:29:43 +090089 // Set |connect_to_name_owner| true if you'd like to use
90 // dbus_g_proxy_new_for_name_owner() rather than dbus_g_proxy_new_for_name().
91 Proxy(const BusConnection& connection,
92 const char* name,
93 const char* path,
94 const char* interface,
95 bool connect_to_name_owner)
96 : object_(GetGProxy(
97 connection, name, path, interface, connect_to_name_owner)) {
98 }
99
100 // Equivalent to Proxy(connection, name, path, interface, false).
rspangler@google.com570c65b2009-10-09 20:56:14 +0000101 Proxy(const BusConnection& connection,
102 const char* name,
103 const char* path,
104 const char* interface)
Yusuke Sato71da5ce2009-12-15 10:29:43 +0900105 : object_(GetGProxy(connection, name, path, interface, false)) {
rspangler@google.com570c65b2009-10-09 20:56:14 +0000106 }
107
108 Proxy(const Proxy& x)
109 : object_(x.object_) {
110 if (object_)
111 ::g_object_ref(object_);
112 }
113
114 ~Proxy() {
115 if (object_)
116 ::g_object_unref(object_);
117 }
118
119 Proxy& operator=(Proxy x) {
120 swap(*this, x);
121 return *this;
122 }
123
124 const char* path() const {
125 DCHECK(object_) << "referencing an empty proxy";
126 return ::dbus_g_proxy_get_path(object_);
127 }
128
129 // gproxy() returns a reference to the underlying ::DBusGProxy*. As this
130 // library evolves, the gproxy() will be moved to be private.
131
132 const value_type& gproxy() const {
133 DCHECK(object_) << "referencing an empty proxy";
134 return object_;
135 }
136
137 operator bool() const {
138 return object_;
139 }
140
141 private:
Yusuke Sato71da5ce2009-12-15 10:29:43 +0900142 static value_type GetGProxy(const BusConnection& connection,
143 const char* name,
144 const char* path,
145 const char* interface,
146 bool connect_to_name_owner);
147
rspangler@google.com570c65b2009-10-09 20:56:14 +0000148 operator int() const; // for safe bool cast
149 friend void swap(Proxy& x, Proxy& y);
150
151 value_type object_;
152};
153
154inline void swap(Proxy& x, Proxy& y) {
155 std::swap(x.object_, y.object_);
156}
157
158template <typename F> // F is a function signature
159class MonitorConnection;
160
161template <typename A1>
162class MonitorConnection<void (A1)> {
163 public:
164 MonitorConnection(const Proxy& proxy, const char* name,
165 void (*monitor)(void*, A1), void* object)
166 : proxy_(proxy), name_(name), monitor_(monitor), object_(object) {
167 }
168
169 static void Run(::DBusGProxy*, A1 x, MonitorConnection* self) {
170 self->monitor_(self->object_, x);
171 }
172 const Proxy& proxy() const {
173 return proxy_;
174 }
175 const std::string& name() const {
176 return name_;
177 }
178
179 private:
180 Proxy proxy_;
181 std::string name_;
182 void (*monitor_)(void*, A1);
183 void* object_;
184};
185
186template <typename A1, typename A2>
187class MonitorConnection<void (A1, A2)> {
188 public:
189 MonitorConnection(const Proxy& proxy, const char* name,
190 void (*monitor)(void*, A1, A2), void* object)
191 : proxy_(proxy), name_(name), monitor_(monitor), object_(object) {
192 }
193
194 static void Run(::DBusGProxy*, A1 x, A2 y, MonitorConnection* self) {
195 self->monitor_(self->object_, x, y);
196 }
197 const Proxy& proxy() const {
198 return proxy_;
199 }
200 const std::string& name() const {
201 return name_;
202 }
203
204 private:
205 Proxy proxy_;
206 std::string name_;
207 void (*monitor_)(void*, A1, A2);
208 void* object_;
209
210};
211
212template <typename A1>
213MonitorConnection<void (A1)>* Monitor(const Proxy& proxy, const char* name,
214 void (*monitor)(void*, A1),
215 void* object) {
216 typedef MonitorConnection<void (A1)> ConnectionType;
217
218 ConnectionType* result = new ConnectionType(proxy, name, monitor, object);
219
220 ::dbus_g_proxy_add_signal(proxy.gproxy(), name,
221 glib::type_to_gtypeid<A1>(), G_TYPE_INVALID);
222 ::dbus_g_proxy_connect_signal(proxy.gproxy(), name,
223 G_CALLBACK(&ConnectionType::Run),
224 result, NULL);
225 return result;
226}
227
228template <typename A1, typename A2>
229MonitorConnection<void (A1, A2)>* Monitor(const Proxy& proxy, const char* name,
230 void (*monitor)(void*, A1, A2),
231 void* object) {
232 typedef MonitorConnection<void (A1, A2)> ConnectionType;
233
234 ConnectionType* result = new ConnectionType(proxy, name, monitor, object);
235
236 ::dbus_g_proxy_add_signal(proxy.gproxy(), name,
237 glib::type_to_gtypeid<A1>(),
238 glib::type_to_gtypeid<A2>(),
239 G_TYPE_INVALID);
240 ::dbus_g_proxy_connect_signal(proxy.gproxy(), name,
241 G_CALLBACK(&ConnectionType::Run),
242 result, NULL);
243 return result;
244}
245
246template <typename F>
247void Disconnect(MonitorConnection<F>* connection) {
248 typedef MonitorConnection<F> ConnectionType;
249
250 ::dbus_g_proxy_disconnect_signal(connection->proxy().gproxy(),
251 connection->name().c_str(),
252 G_CALLBACK(&ConnectionType::Run),
253 connection);
254 delete connection;
255}
256
257// \brief call_PtrArray() invokes a method on a proxy returning a
258// glib::PtrArray.
259//
260// CallPtrArray is the first instance of what is likely to be a general
261// way to make method calls to a proxy. It will likely be replaced with
262// something like Call(proxy, method, arg1, arg2, ..., ResultType*) in the
263// future. However, I don't yet have enough cases to generalize from.
264
265bool CallPtrArray(const Proxy& proxy,
266 const char* method,
267 glib::ScopedPtrArray<const char*>* result);
268
269// \brief RetrieveProperty() retrieves a property of an object associated with a
270// proxy.
271//
272// Given a proxy to an object supporting the org.freedesktop.DBus.Properties
273// interface, the RetrieveProperty() call will retrieve a property of the
274// specified interface on the object storing it in \param result and returning
275// \true. If the dbus call fails or the object returned is not of type \param T,
276// then \false is returned and \param result is unchanged.
277//
278// \example
279// Proxy proxy(GetSystemBusConnection(),
280// "org.freedesktop.DeviceKit.Power", // A named entity on the bus
281// battery_name, // Path to a battery on the bus
282// "org.freedesktop.DBus.Properties") // Properties interface
283//
284// double x;
285// if (RetrieveProperty(proxy,
286// "org.freedesktop.DeviceKit.Power.Device",
287// "percentage")
288// std::cout << "Battery charge is " << x << "% of capacity.";
289// \end_example
290
291template <typename T>
292bool RetrieveProperty(const Proxy& proxy,
293 const char* interface,
294 const char* property,
295 T* result) {
296 glib::ScopedError error;
297 glib::Value value;
298
299 if (!::dbus_g_proxy_call(proxy.gproxy(), "Get", &Resetter(&error).lvalue(),
300 G_TYPE_STRING, interface,
301 G_TYPE_STRING, property,
302 G_TYPE_INVALID,
303 G_TYPE_VALUE, &value,
dhg@google.com7ec90e92009-10-27 16:44:35 +0000304 G_TYPE_INVALID)){
Yusuke Sato71da5ce2009-12-15 10:29:43 +0900305 LOG(ERROR) << "Getting property failed: "
306 << (error->message ? error->message : "Unknown Error.");
rspangler@google.com570c65b2009-10-09 20:56:14 +0000307 return false;
308
dhg@google.com7ec90e92009-10-27 16:44:35 +0000309 }
310 return glib::Retrieve(value, result);
rspangler@google.com570c65b2009-10-09 20:56:14 +0000311}
312
313// \brief RetrieveProperties returns a HashTable of all properties for the
314// specified interface.
315
316bool RetrieveProperties(const Proxy& proxy,
317 const char* interface,
318 glib::ScopedHashTable* result);
319
320// \brief Returns a connection to the system bus.
321
322BusConnection GetSystemBusConnection();
323
Yusuke Sato71da5ce2009-12-15 10:29:43 +0900324// \brief Returns a private connection to a bus at |address|.
325
326BusConnection GetPrivateBusConnection(const char* address);
327
rspangler@google.com570c65b2009-10-09 20:56:14 +0000328} // namespace dbus
329} // namespace chromeos
330
331#endif // CHROMEOS_DBUS_H_