Debian Codegraph @

dbus/client/notify.c

raw symbols SCIP
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 // SPDX-FileCopyrightText: 2023 Bartosz Golaszewski <[email protected]>
3
4 #include <gio/gio.h>
5 #include <glib-unix.h>
6 #include <stdlib.h>
7
8 #include "common.h"
9
10 /*
11 * Used to keep line proxies and chip interfaces alive for the duration of the
12 * program, which is required for signals to work.
13 */
14 typedef struct {
15 GList *lines;
16 GList *chips;
17 GpiodbusObject *scoped_chip;
18 } NotifyData;
19
20 static void clear_notify_data(NotifyData *data)
21 {
22 g_list_free_full(data->lines, g_object_unref);
23 g_list_free_full(data->chips, g_object_unref);
24
25 if (data->scoped_chip)
26 g_clear_object(&data->scoped_chip);
27 }
28
29 G_DEFINE_AUTO_CLEANUP_CLEAR_FUNC(NotifyData, clear_notify_data);
30
31 static const gchar *bool_to_str(gboolean val)
32 {
33 return val ? "True" : "False";
34 }
35
36 static const gchar *bool_variant_to_str(GVariant *val)
37 {
38 return bool_to_str(g_variant_get_boolean(val));
39 }
40
41 static void
42 on_properties_changed(GpiodbusLine *line, GVariant *changed_properties,
43 GStrv invalidated_properties G_GNUC_UNUSED,
44 gpointer user_data)
45 {
46 GpiodbusChip *chip = user_data;
47 g_autofree gchar *name = NULL;
48 const gchar *consumer, *tmp;
49 GVariantIter iter;
50 GVariant *v;
51 gsize len;
52 gchar *k;
53
54 if (g_variant_n_children(changed_properties) == 0)
55 return;
56
57 tmp = gpiodbus_line_get_name(line);
58 name = tmp ? g_strdup_printf("\"%s\"", tmp) : g_strdup("unnamed");
59
60 g_variant_iter_init(&iter, changed_properties);
61 while (g_variant_iter_next(&iter, "{sv}", &k, &v)) {
62 g_autoptr(GString) change = g_string_new(NULL);
63 g_autofree gchar *req_name = NULL;
64 g_autoptr(GVariant) val = v;
65 g_autofree gchar *key = k;
66
67 if (g_strcmp0(key, "Consumer") == 0) {
68 consumer = g_variant_get_string(val, &len);
69 g_string_printf(change, "consumer=>\"%s\"",
70 len ? consumer : "unused");
71 } else if (g_strcmp0(key, "Used") == 0) {
72 g_string_printf(change, "used=>%s",
73 bool_variant_to_str(val));
74 } else if (g_strcmp0(key, "Debounced") == 0) {
75 g_string_printf(change, "debounced=>%s",
76 bool_variant_to_str(val));
77 } else if (g_strcmp0(key, "ActiveLow") == 0) {
78 g_string_printf(change, "active-low=>%s",
79 bool_variant_to_str(val));
80 } else if (g_strcmp0(key, "Direction") == 0) {
81 g_string_printf(change, "direction=>%s",
82 g_variant_get_string(val, NULL));
83 } else if (g_strcmp0(key, "Drive") == 0) {
84 g_string_printf(change, "drive=>%s",
85 g_variant_get_string(val, NULL));
86 } else if (g_strcmp0(key, "Bias") == 0) {
87 g_string_printf(change, "bias=>%s",
88 g_variant_get_string(val, NULL));
89 } else if (g_strcmp0(key, "EdgeDetection") == 0) {
90 g_string_printf(change, "edge=>%s",
91 g_variant_get_string(val, NULL));
92 } else if (g_strcmp0(key, "EventClock") == 0) {
93 g_string_printf(change, "event-clock=>%s",
94 g_variant_get_string(val, NULL));
95 } else if (g_strcmp0(key, "DebouncePeriodUs") == 0) {
96 g_string_printf(change, "debounce-period=>%"G_GUINT64_FORMAT"",
97 g_variant_get_uint64(val));
98 } else if (g_strcmp0(key, "Managed") == 0) {
99 g_string_printf(change, "managed=>%s",
100 bool_variant_to_str(val));
101 } else if (g_strcmp0(key, "RequestPath") == 0) {
102 req_name = sanitize_object_path(
103 g_variant_get_string(val, NULL));
104 g_string_printf(change, "request=>%s",
105 req_name);
106 } else {
107 die("unexpected property update received from manager: '%s'",
108 key);
109 }
110
111 g_print("%s - %u (%s): [%s]\n", gpiodbus_chip_get_name(chip),
112 gpiodbus_line_get_offset(line), name ?: "unnamed",
113 change->str);
114 }
115 }
116
117 static void print_line_info(GpiodbusLine *line, GpiodbusChip *chip)
118 {
119 g_autoptr(LineProperties) props = get_line_properties(line);
120 g_autoptr(GString) attrs = g_string_new(props->direction);
121 g_autofree gchar *name = NULL;
122
123 if (props->used)
124 g_string_append(attrs, ",used");
125
126 if (props->consumer)
127 g_string_append_printf(attrs, ",consumer=\"%s\"",
128 props->consumer);
129
130 if (props->drive && g_strcmp0(props->direction, "output") == 0)
131 g_string_append_printf(attrs, ",%s", props->drive);
132
133 if (props->bias) {
134 if (g_strcmp0(props->bias, "disabled") == 0)
135 g_string_append(attrs, ",bias-disabled");
136 else
137 g_string_append_printf(attrs, ",%s", props->bias);
138 }
139
140 if (props->active_low)
141 g_string_append(attrs, ",active-low");
142
143 if (props->edge) {
144 if (g_strcmp0(props->edge, "both") == 0)
145 g_string_append(attrs, ",both-edges");
146 else
147 g_string_append_printf(attrs, ",%s-edge", props->edge);
148
149 g_string_append_printf(attrs, ",%s-clock", props->event_clock);
150
151 if (props->debounced)
152 g_string_append_printf(attrs,
153 "debounced,debounce-period=%"G_GUINT64_FORMAT"",
154 props->debounce_period);
155 }
156
157 if (props->managed)
158 g_string_append_printf(attrs, ",managed,request=\"%s\"",
159 props->request_name);
160
161 name = props->name ? g_strdup_printf("\"%s\"", props->name) :
162 g_strdup("unnamed");
163
164 g_print("%s - %u (%s): [%s]\n", gpiodbus_chip_get_name(chip),
165 props->offset, name ?: "unnamed", attrs->str);
166 }
167
168 static void connect_line(gpointer elem, gpointer user_data)
169 {
170 g_autoptr(GpiodbusObject) line_obj = NULL;
171 g_autoptr(GpiodbusObject) chip_obj = NULL;
172 g_autoptr(GpiodbusLine) line = NULL;
173 g_autoptr(GpiodbusChip) chip = NULL;
174 g_autofree gchar *chip_name = NULL;
175 g_autoptr(GError) err = NULL;
176 NotifyData *data = user_data;
177 const gchar *line_obj_path;
178 GString *line_name = elem;
179 gboolean ret;
180
181 if (data->scoped_chip) {
182 chip_obj = g_object_ref(data->scoped_chip);
183 line_obj = get_line_obj_by_name_for_chip(chip_obj,
184 line_name->str);
185 if (!line_obj) {
186 chip_name = g_path_get_basename(
187 g_dbus_object_get_object_path(
188 G_DBUS_OBJECT(chip_obj)));
189 die("no line '%s' on chip '%s'",
190 line_name->str, chip_name);
191 }
192 } else {
193 ret = get_line_obj_by_name(line_name->str,
194 &line_obj, &chip_obj);
195 if (!ret)
196 die("line '%s' not found", line_name->str);
197 }
198
199 line_obj_path = g_dbus_object_get_object_path(G_DBUS_OBJECT(line_obj));
200
201 line = gpiodbus_line_proxy_new_for_bus_sync(G_BUS_TYPE_SYSTEM,
202 G_DBUS_PROXY_FLAGS_NONE,
203 "io.gpiod1", line_obj_path,
204 NULL, &err);
205 if (err)
206 die_gerror(err, "Failed to get D-Bus proxy for '%s'",
207 line_obj_path);
208
209 data->lines = g_list_append(data->lines, g_object_ref(line));
210
211 if (data->scoped_chip) {
212 if (g_list_length(data->chips) == 0) {
213 chip = gpiodbus_object_get_chip(chip_obj);
214 data->chips = g_list_append(data->chips, chip);
215 } else {
216 chip = g_list_first(data->chips)->data;
217 g_object_ref(chip);
218 }
219 } else {
220 chip = gpiodbus_object_get_chip(chip_obj);
221 data->chips = g_list_append(data->chips, g_object_ref(chip));
222 }
223
224 print_line_info(line, chip);
225
226 g_signal_connect(line, "g-properties-changed",
227 G_CALLBACK(on_properties_changed), chip);
228 }
229
230 int gpiocli_notify_main(int argc, char **argv)
231 {
232 static const gchar *const summary =
233 "Monitor a set of lines for property changes.";
234
235 static const gchar *const description =
236 "Lines are specified by name, or optionally by offset if the chip option\n"
237 "is provided.\n";
238
239 g_autolist(GString) line_name_list = NULL;
240 g_autoptr(GMainLoop) loop = NULL;
241 g_auto(GStrv) line_names = NULL;
242 const gchar *chip_name = NULL;
243 /*
244 * FIXME: data internals must be freed but there's some issue with
245 * unrefing the GpiodbusObject here. For now it's leaking memory.
246 */
247 NotifyData data = { };
248 guint watch_id;
249
250 const GOptionEntry opts[] = {
251 {
252 .long_name = "chip",
253 .short_name = 'c',
254 .flags = G_OPTION_FLAG_NONE,
255 .arg = G_OPTION_ARG_STRING,
256 .arg_data = &chip_name,
257 .description = "restrict scope to a particular chip",
258 .arg_description = "<chip>",
259 },
260 {
261 .long_name = G_OPTION_REMAINING,
262 .flags = G_OPTION_FLAG_NONE,
263 .arg = G_OPTION_ARG_STRING_ARRAY,
264 .arg_data = &line_names,
265 .arg_description = "<line1> [line2] ...",
266 },
267 { }
268 };
269
270 parse_options(opts, summary, description, &argc, &argv);
271
272 watch_id = g_bus_watch_name(G_BUS_TYPE_SYSTEM, "io.gpiod1",
273 G_BUS_NAME_WATCHER_FLAGS_NONE,
274 NULL, die_on_name_vanished, NULL, NULL);
275 check_manager();
276
277 if (!line_names)
278 die_parsing_opts("at least one line must be specified");
279
280 if (chip_name)
281 data.scoped_chip = get_chip_obj(chip_name);
282
283 line_name_list = strv_to_gstring_list(line_names);
284 g_list_foreach(line_name_list, connect_line, &data);
285
286 loop = g_main_loop_new(NULL, FALSE);
287 g_unix_signal_add(SIGTERM, quit_main_loop_on_signal, loop);
288 g_unix_signal_add(SIGINT, quit_main_loop_on_signal, loop);
289
290 g_main_loop_run(loop);
291
292 g_list_free_full(data.lines, g_object_unref);
293 g_list_free_full(data.chips, g_object_unref);
294 g_bus_unwatch_name(watch_id);
295
296 return EXIT_SUCCESS;
297 }