Debian Codegraph @

dbus/client/request.c

raw symbols SCIP
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 // SPDX-FileCopyrightText: 2023-2024 Bartosz Golaszewski <[email protected]>
3
4 #include <stdlib.h>
5
6 #include "common.h"
7
8 typedef struct {
9 LineConfigOpts line_cfg_opts;
10 const gchar *consumer;
11 } RequestOpts;
12
13 typedef struct {
14 const gchar *request_path;
15 gboolean done;
16 } RequestWaitData;
17
18 static GVariant *make_request_config(RequestOpts *opts)
19 {
20 GVariantBuilder builder;
21
22 g_variant_builder_init(&builder, G_VARIANT_TYPE_ARRAY);
23 g_variant_builder_add_value(&builder,
24 g_variant_new("{sv}", "consumer",
25 g_variant_new_string(opts->consumer)));
26
27 return g_variant_ref_sink(g_variant_builder_end(&builder));
28 }
29
30 static gboolean on_timeout(gpointer user_data G_GNUC_UNUSED)
31 {
32 die("wait for request to appear timed out!");
33 }
34
35 static void obj_match_request_path(GpiodbusObject *obj, RequestWaitData *data)
36 {
37 if (g_strcmp0(g_dbus_object_get_object_path(G_DBUS_OBJECT(obj)),
38 data->request_path) == 0)
39 data->done = TRUE;
40 }
41
42 static void match_request_path(gpointer elem, gpointer user_data)
43 {
44 RequestWaitData *data = user_data;
45 GpiodbusObject *obj = elem;
46
47 obj_match_request_path(obj, data);
48 }
49
50 static void on_object_added(GDBusObjectManager *manager G_GNUC_UNUSED,
51 GpiodbusObject *obj, gpointer user_data)
52 {
53 RequestWaitData *data = user_data;
54
55 obj_match_request_path(GPIODBUS_OBJECT(obj), data);
56 }
57
58 static void wait_for_request(const gchar *request_path)
59 {
60 RequestWaitData data = { .request_path = request_path };
61 g_autoptr(GDBusObjectManager) manager = NULL;
62 g_autolist(GpiodbusObject) objs = NULL;
63
64 manager = get_object_manager_client("/io/gpiod1/requests");
65
66 g_signal_connect(manager, "object-added",
67 G_CALLBACK(on_object_added), &data);
68
69 objs = g_dbus_object_manager_get_objects(manager);
70 g_list_foreach(objs, match_request_path, &data);
71
72 g_timeout_add(5000, on_timeout, NULL);
73
74 while (!data.done)
75 g_main_context_iteration(NULL, TRUE);
76 }
77
78 static int
79 request_lines(GList *line_names, const gchar *chip_name, RequestOpts *req_opts)
80 {
81 g_autoptr(GpiodbusObject) chip_obj = NULL;
82 g_autoptr(GVariant) request_config = NULL;
83 g_autoptr(GVariant) line_config = NULL;
84 g_autofree gchar *request_path = NULL;
85 g_autofree gchar *request_name = NULL;
86 g_autofree gchar *dyn_name = NULL;
87 g_autoptr(GArray) offsets = NULL;
88 g_autoptr(GError) err = NULL;
89 GpiodbusLine *line;
90 GpiodbusChip *chip;
91 GString *line_name;
92 guint i, *offset;
93 gboolean ret;
94 GList *pos;
95 gsize llen;
96
97 llen = g_list_length(line_names);
98 offsets = g_array_sized_new(FALSE, TRUE, sizeof(guint), llen);
99 g_array_set_size(offsets, llen);
100
101 if (chip_name)
102 chip_obj = get_chip_obj(chip_name);
103
104 for (i = 0, pos = g_list_first(line_names);
105 i < llen;
106 i++, pos = g_list_next(pos)) {
107 g_autoptr(GpiodbusObject) line_obj = NULL;
108
109 line_name = pos->data;
110
111 if (chip_obj) {
112 line_obj = get_line_obj_by_name_for_chip(chip_obj,
113 line_name->str);
114 if (!line_obj) {
115 if (dyn_name) {
116 ret = get_line_obj_by_name(
117 line_name->str,
118 &line_obj, NULL);
119 if (ret)
120 /*
121 * This means the line exists
122 * but on a different chip.
123 */
124 die("all requested lines must belong to the same chip");
125 }
126
127 die("no line '%s' on chip '%s'",
128 line_name->str, chip_name);
129 }
130 } else {
131 ret = get_line_obj_by_name(line_name->str, &line_obj,
132 &chip_obj);
133 if (!ret)
134 die("line '%s' not found", line_name->str);
135
136 dyn_name = g_path_get_basename(
137 g_dbus_object_get_object_path(
138 G_DBUS_OBJECT(chip_obj)));
139 chip_name = dyn_name;
140 }
141
142 line = gpiodbus_object_peek_line(line_obj);
143 offset = &g_array_index(offsets, guint, i);
144 *offset = gpiodbus_line_get_offset(line);
145 }
146
147 chip = gpiodbus_object_peek_chip(chip_obj);
148 line_config = make_line_config(offsets, &req_opts->line_cfg_opts);
149 request_config = make_request_config(req_opts);
150
151 ret = gpiodbus_chip_call_request_lines_sync(chip, line_config,
152 request_config,
153 G_DBUS_CALL_FLAGS_NONE, -1,
154 &request_path, NULL, &err);
155 if (err)
156 die_gerror(err, "failed to request lines from chip '%s'",
157 chip_name);
158
159 wait_for_request(request_path);
160
161 request_name = g_path_get_basename(request_path);
162 g_print("%s\n", request_name);
163
164 return EXIT_SUCCESS;
165 }
166
167 int gpiocli_request_main(int argc, char **argv)
168 {
169 static const gchar *const summary =
170 "Request a set of GPIO lines for exclusive usage by the gpio-manager.";
171
172 g_autoptr(GArray) output_values = NULL;
173 g_autolist(GString) line_names = NULL;
174 const gchar *chip_name = NULL;
175 g_auto(GStrv) lines = NULL;
176 RequestOpts req_opts = {};
177 gsize llen;
178 gint val;
179 guint i;
180
181 const GOptionEntry opts[] = {
182 {
183 .long_name = "chip",
184 .short_name = 'c',
185 .flags = G_OPTION_FLAG_NONE,
186 .arg = G_OPTION_ARG_STRING,
187 .arg_data = &chip_name,
188 .description =
189 "Explicitly specify the chip_name on which to resolve the lines which allows to use raw offsets instead of line names.",
190 .arg_description = "<chip name>",
191 },
192 {
193 .long_name = "consumer",
194 .flags = G_OPTION_FLAG_NONE,
195 .arg = G_OPTION_ARG_STRING,
196 .arg_data = &req_opts.consumer,
197 .description = "Consumer string (defaults to program name)",
198 .arg_description = "<consumer name>",
199 },
200 {
201 .long_name = G_OPTION_REMAINING,
202 .flags = G_OPTION_FLAG_NONE,
203 .arg = G_OPTION_ARG_STRING_ARRAY,
204 .arg_data = &lines,
205 .arg_description = "<line1>[=value1] [line2[=value2]] ...",
206 },
207 LINE_CONFIG_OPTIONS(&req_opts.line_cfg_opts),
208 { }
209 };
210
211 parse_options(opts, summary, NULL, &argc, &argv);
212 validate_line_config_opts(&req_opts.line_cfg_opts);
213
214 if (!lines)
215 die_parsing_opts("At least one line must be specified");
216
217 if (!req_opts.consumer)
218 req_opts.consumer = "gpio-manager";
219
220 for (i = 0, llen = g_strv_length(lines); i < llen; i++) {
221 g_auto(GStrv) tokens = NULL;
222
223 tokens = g_strsplit(lines[i], "=", 2);
224 line_names = g_list_append(line_names, g_string_new(tokens[0]));
225 if (g_strv_length(tokens) == 2) {
226 if (!req_opts.line_cfg_opts.output)
227 die_parsing_opts("Output values can only be set in output mode");
228
229 if (!output_values)
230 output_values = g_array_sized_new(FALSE, TRUE,
231 sizeof(gint),
232 llen);
233 val = output_value_from_str(tokens[1]);
234 g_array_append_val(output_values, val);
235 }
236 }
237
238 if (output_values && req_opts.line_cfg_opts.input)
239 die_parsing_opts("cannot set output values in input mode");
240
241 if (output_values &&
242 (g_list_length(line_names) != output_values->len))
243 die_parsing_opts("if values are set, they must be set for all lines");
244
245 req_opts.line_cfg_opts.output_values = output_values;
246
247 check_manager();
248
249 return request_lines(line_names, chip_name, &req_opts);
250 }