3
* Copyright 2004--2011, Google Inc.
5
* Redistribution and use in source and binary forms, with or without
6
* modification, are permitted provided that the following conditions are met:
8
* 1. Redistributions of source code must retain the above copyright notice,
9
* this list of conditions and the following disclaimer.
10
* 2. Redistributions in binary form must reproduce the above copyright notice,
11
* this list of conditions and the following disclaimer in the documentation
12
* and/or other materials provided with the distribution.
13
* 3. The name of the author may not be used to endorse or promote products
14
* derived from this software without specific prior written permission.
16
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
17
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
19
* EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
22
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
25
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28
#ifndef TALK_BASE_DBUS_H_
29
#define TALK_BASE_DBUS_H_
33
#include <dbus/dbus.h>
38
#include "talk/base/libdbusglibsymboltable.h"
39
#include "talk/base/messagehandler.h"
40
#include "talk/base/thread.h"
44
#define DBUS_TYPE "type"
45
#define DBUS_SIGNAL "signal"
46
#define DBUS_PATH "path"
47
#define DBUS_INTERFACE "interface"
48
#define DBUS_MEMBER "member"
51
#define CROS_PM_PATH "/"
52
#define CROS_PM_INTERFACE "org.chromium.PowerManager"
53
#define CROS_SIG_POWERCHANGED "PowerStateChanged"
54
#define CROS_VALUE_SLEEP "mem"
55
#define CROS_VALUE_RESUME "on"
57
#define UP_PATH "/org/freedesktop/UPower"
58
#define UP_INTERFACE "org.freedesktop.UPower"
59
#define UP_SIG_SLEEPING "Sleeping"
60
#define UP_SIG_RESUMING "Resuming"
63
// Wraps a DBus messages.
64
class DBusSigMessageData : public TypedMessageData<DBusMessage *> {
66
explicit DBusSigMessageData(DBusMessage *message);
67
~DBusSigMessageData();
70
// DBusSigFilter is an abstract class that defines the interface of DBus
72
// The subclasses implement ProcessSignal() for various purposes.
73
// When a DBus signal comes, a DSM_SIGNAL message is posted to the caller thread
74
// which will then invokes ProcessSignal().
75
class DBusSigFilter : protected MessageHandler {
77
enum DBusSigMessage { DSM_SIGNAL };
79
// This filter string should ususally come from BuildFilterString()
80
explicit DBusSigFilter(const std::string &filter)
81
: caller_thread_(Thread::Current()), filter_(filter) {
84
// Builds a DBus monitor filter string from given DBus path, interface, and
86
// See http://dbus.freedesktop.org/doc/api/html/group__DBusConnection.html
87
static std::string BuildFilterString(const std::string &path,
88
const std::string &interface,
89
const std::string &member);
91
// Handles callback on DBus messages by DBus system.
92
static DBusHandlerResult DBusCallback(DBusConnection *dbus_conn,
96
// Handles callback on DBus messages to each DBusSigFilter instance.
97
DBusHandlerResult Callback(DBusMessage *message);
99
// From MessageHandler.
100
virtual void OnMessage(Message *message);
102
// Returns the DBus monitor filter string.
103
const std::string &filter() const { return filter_; }
107
virtual void ProcessSignal(DBusMessage *message) = 0;
109
Thread *caller_thread_;
110
const std::string filter_;
113
// DBusMonitor is a class for DBus signal monitoring.
115
// The caller-thread calls AddFilter() first to add the signals that it wants to
116
// monitor and then calls StartMonitoring() to start the monitoring.
117
// This will create a worker-thread which listens on DBus connection and sends
118
// DBus signals back through the callback.
119
// The worker-thread will be running forever until either StopMonitoring() is
120
// called from the caller-thread or the worker-thread hit some error.
122
// Programming model:
123
// 1. Caller-thread: Creates an object of DBusMonitor.
124
// 2. Caller-thread: Calls DBusMonitor::AddFilter() one or several times.
125
// 3. Caller-thread: StartMonitoring().
127
// 4. Worker-thread: DBus signal recieved. Post a message to caller-thread.
128
// 5. Caller-thread: DBusFilterBase::ProcessSignal() is invoked.
130
// 6. Caller-thread: StopMonitoring().
133
// AddFilter(), StartMonitoring(), and StopMonitoring() methods are called by
134
// a single thread. Hence, there is no need to make them thread safe.
137
// Status of DBus monitoring.
138
enum DBusMonitorStatus {
139
DMS_NOT_INITIALIZED, // Not initialized.
140
DMS_INITIALIZING, // Initializing the monitoring thread.
141
DMS_RUNNING, // Monitoring.
142
DMS_STOPPED, // Not monitoring. Stopped normally.
143
DMS_FAILED, // Not monitoring. Failed.
146
// Returns the DBus-Glib symbol table.
147
// We should only use this function to access DBus-Glib symbols.
148
static LibDBusGlibSymbolTable *GetDBusGlibSymbolTable();
150
// Creates an instance of DBusMonitor.
151
static DBusMonitor *Create(DBusBusType type);
154
// Adds a filter to DBusMonitor.
155
bool AddFilter(DBusSigFilter *filter);
157
// Starts DBus message monitoring.
158
bool StartMonitoring();
160
// Stops DBus message monitoring.
161
bool StopMonitoring();
163
// Gets the status of DBus monitoring.
164
DBusMonitorStatus GetStatus();
167
// Forward declaration. Defined in the .cc file.
168
class DBusMonitoringThread;
170
explicit DBusMonitor(DBusBusType type);
172
// Updates status_ when monitoring status has changed.
173
void OnMonitoringStatusChanged(DBusMonitorStatus status);
176
DBusMonitorStatus status_;
177
DBusMonitoringThread *monitoring_thread_;
178
std::vector<DBusSigFilter *> filter_list_;
181
} // namespace talk_base
183
#endif // HAVE_DBUS_GLIB
185
#endif // TALK_BASE_DBUS_H_