1 /* Copyright (c) 2012 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
6 #include <dbus/dbus.h>
7 #include <errno.h>
8 #include <poll.h>
9 #include <stdint.h>
10 #include <stdlib.h>
11 #include <syslog.h>
12 #include <sys/select.h>
13 #include <unistd.h>
14
15 #include "cras_system_state.h"
16 #include "cras_tm.h"
17
dbus_watch_callback(void * arg)18 static void dbus_watch_callback(void *arg)
19 {
20 DBusWatch *watch = (DBusWatch *)arg;
21 int r, flags;
22 struct pollfd pollfd;
23
24 pollfd.fd = dbus_watch_get_unix_fd(watch);
25 pollfd.events = POLLIN | POLLOUT;
26
27 r = poll(&pollfd, 1, 0);
28 if (r <= 0)
29 return;
30
31 flags = 0;
32 if (pollfd.revents & POLLIN)
33 flags |= DBUS_WATCH_READABLE;
34 if (pollfd.revents & POLLOUT)
35 flags |= DBUS_WATCH_WRITABLE;
36
37 if (!dbus_watch_handle(watch, flags))
38 syslog(LOG_WARNING, "Failed to handle D-Bus watch.");
39 }
40
dbus_watch_add(DBusWatch * watch,void * data)41 static dbus_bool_t dbus_watch_add(DBusWatch *watch, void *data)
42 {
43 int r;
44 unsigned int flags = dbus_watch_get_flags(watch);
45
46 /* Only select the read watch.
47 * TODO(hychao): select on write watch when we have a use case.
48 */
49 if ((flags & DBUS_WATCH_READABLE) && dbus_watch_get_enabled(watch)) {
50 r = cras_system_add_select_fd(dbus_watch_get_unix_fd(watch),
51 dbus_watch_callback, watch);
52 if (r != 0)
53 return FALSE;
54 }
55
56 return TRUE;
57 }
58
dbus_watch_remove(DBusWatch * watch,void * data)59 static void dbus_watch_remove(DBusWatch *watch, void *data)
60 {
61 unsigned int flags = dbus_watch_get_flags(watch);
62
63 /* Only select the read watch. */
64 if (flags & DBUS_WATCH_READABLE)
65 cras_system_rm_select_fd(dbus_watch_get_unix_fd(watch));
66 }
67
dbus_watch_toggled(DBusWatch * watch,void * data)68 static void dbus_watch_toggled(DBusWatch *watch, void *data)
69 {
70 if (dbus_watch_get_enabled(watch)) {
71 dbus_watch_add(watch, NULL);
72 } else {
73 dbus_watch_remove(watch, NULL);
74 }
75 }
76
dbus_timeout_callback(struct cras_timer * t,void * data)77 static void dbus_timeout_callback(struct cras_timer *t, void *data)
78 {
79 struct cras_tm *tm = cras_system_state_get_tm();
80 struct DBusTimeout *timeout = data;
81
82 /* Timer is automatically removed after it fires. Add a new one so this
83 * fires until it is removed by dbus. */
84 t = cras_tm_create_timer(tm, dbus_timeout_get_interval(timeout),
85 dbus_timeout_callback, timeout);
86 dbus_timeout_set_data(timeout, t, NULL);
87
88 if (!dbus_timeout_handle(timeout))
89 syslog(LOG_WARNING, "Failed to handle D-Bus timeout.");
90 }
91
dbus_timeout_add(DBusTimeout * timeout,void * arg)92 static dbus_bool_t dbus_timeout_add(DBusTimeout *timeout, void *arg)
93 {
94 struct cras_tm *tm = cras_system_state_get_tm();
95 struct cras_timer *t = dbus_timeout_get_data(timeout);
96
97 if (t) {
98 dbus_timeout_set_data(timeout, NULL, NULL);
99 cras_tm_cancel_timer(tm, t);
100 }
101
102 if (dbus_timeout_get_enabled(timeout)) {
103 t = cras_tm_create_timer(tm, dbus_timeout_get_interval(timeout),
104 dbus_timeout_callback, timeout);
105 dbus_timeout_set_data(timeout, t, NULL);
106 if (t == NULL)
107 return FALSE;
108 }
109
110 return TRUE;
111 }
112
dbus_timeout_remove(DBusTimeout * timeout,void * arg)113 static void dbus_timeout_remove(DBusTimeout *timeout, void *arg)
114 {
115 struct cras_tm *tm = cras_system_state_get_tm();
116 struct cras_timer *t = dbus_timeout_get_data(timeout);
117
118 if (t) {
119 dbus_timeout_set_data(timeout, NULL, NULL);
120 cras_tm_cancel_timer(tm, t);
121 }
122 }
123
dbus_timeout_toggled(DBusTimeout * timeout,void * arg)124 static void dbus_timeout_toggled(DBusTimeout *timeout, void *arg)
125 {
126 if (dbus_timeout_get_enabled(timeout))
127 dbus_timeout_add(timeout, NULL);
128 else
129 dbus_timeout_remove(timeout, NULL);
130 }
131
cras_dbus_connect_system_bus()132 DBusConnection *cras_dbus_connect_system_bus()
133 {
134 DBusError dbus_error;
135 DBusConnection *conn;
136 int rc;
137
138 dbus_error_init(&dbus_error);
139
140 conn = dbus_bus_get(DBUS_BUS_SYSTEM, &dbus_error);
141 if (!conn) {
142 syslog(LOG_WARNING, "Failed to connect to D-Bus: %s",
143 dbus_error.message);
144 dbus_error_free(&dbus_error);
145 return NULL;
146 }
147
148 /* Request a name on the bus. */
149 rc = dbus_bus_request_name(conn, "org.chromium.cras", 0, &dbus_error);
150 if (dbus_error_is_set(&dbus_error)) {
151 syslog(LOG_ERR, "Requesting dbus name %s", dbus_error.message);
152 dbus_error_free(&dbus_error);
153 }
154 if (rc != DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER)
155 syslog(LOG_ERR, "Not primary owner of dbus name.");
156
157 if (!dbus_connection_set_watch_functions(
158 conn, dbus_watch_add, dbus_watch_remove, dbus_watch_toggled,
159 NULL, NULL))
160 goto error;
161 if (!dbus_connection_set_timeout_functions(
162 conn, dbus_timeout_add, dbus_timeout_remove,
163 dbus_timeout_toggled, NULL, NULL))
164 goto error;
165
166 return conn;
167
168 error:
169 syslog(LOG_WARNING, "Failed to setup D-Bus connection.");
170 dbus_connection_unref(conn);
171 return NULL;
172 }
173
cras_dbus_dispatch(DBusConnection * conn)174 void cras_dbus_dispatch(DBusConnection *conn)
175 {
176 while (dbus_connection_dispatch(conn) == DBUS_DISPATCH_DATA_REMAINS)
177 ;
178 }
179
cras_dbus_disconnect_system_bus(DBusConnection * conn)180 void cras_dbus_disconnect_system_bus(DBusConnection *conn)
181 {
182 dbus_connection_unref(conn);
183 }
184