Debian Codegraph @

dbus/client/get.c

raw symbols SCIP
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 // SPDX-FileCopyrightText: 2022-2024 Bartosz Golaszewski <[email protected]>
3
4 #include <stdlib.h>
5
6 #include "common.h"
7
8 int gpiocli_get_main(int argc, char **argv)
9 {
10 static const gchar *const summary =
11 "Get values of one or more GPIO lines.";
12
13 static const gchar *const description =
14 "If -r/--request is specified then all the lines must belong to the same\n"
15 "request (and - by extension - the same chip).\n"
16 "\n"
17 "If no lines are specified but -r/--request was passed then all lines within\n"
18 "the request will be used.";
19
20 const gchar *request_name = NULL, *chip_path, *req_path;
21 gboolean ret, unquoted = FALSE, numeric = FALSE;
22 g_autoptr(GpiodbusObject) chip_obj = NULL;
23 g_autoptr(GpiodbusObject) req_obj = NULL;
24 g_autoptr(GArray) offsets = NULL;
25 g_autoptr(GArray) values = NULL;
26 g_autoptr(GError) err = NULL;
27 g_auto(GStrv) lines = NULL;
28 GpiodbusRequest *request;
29 GVariantBuilder builder;
30 GpiodbusLine *line;
31 gsize num_lines, i;
32 GVariantIter iter;
33 guint offset;
34 gint value;
35
36 const GOptionEntry opts[] = {
37 {
38 .long_name = "request",
39 .short_name = 'r',
40 .flags = G_OPTION_FLAG_NONE,
41 .arg = G_OPTION_ARG_STRING,
42 .arg_data = &request_name,
43 .description = "restrict scope to a particular request",
44 .arg_description = "<request>",
45 },
46 {
47 .long_name = "unquoted",
48 .flags = G_OPTION_FLAG_NONE,
49 .arg = G_OPTION_ARG_NONE,
50 .arg_data = &unquoted,
51 .description = "don't quote line names",
52 },
53 {
54 .long_name = "numeric",
55 .flags = G_OPTION_FLAG_NONE,
56 .arg = G_OPTION_ARG_NONE,
57 .arg_data = &numeric,
58 .description = "display line values as '0' (inactive) or '1' (active)",
59 },
60 {
61 .long_name = G_OPTION_REMAINING,
62 .flags = G_OPTION_FLAG_NONE,
63 .arg = G_OPTION_ARG_STRING_ARRAY,
64 .arg_data = &lines,
65 .arg_description = "[line0] [line1]...",
66 },
67 { }
68 };
69
70 parse_options(opts, summary, description, &argc, &argv);
71 check_manager();
72
73 if (!lines && !request_name)
74 die_parsing_opts("either at least one line or the request must be specified");
75
76 offsets = g_array_new(FALSE, TRUE, sizeof(guint));
77 num_lines = lines ? g_strv_length(lines) : 0;
78
79 if (!request_name) {
80 /*
81 * TODO Limit the number of D-Bus calls by gathering the requests
82 * and their relevant lines into a container of some kind first.
83 */
84
85 values = g_array_sized_new(FALSE, TRUE, sizeof(gint),
86 num_lines);
87
88 for (i = 0; i < num_lines; i++) {
89 g_autoptr(GpiodbusRequest) req_proxy = NULL;
90 g_autoptr(GpiodbusObject) line_obj = NULL;
91 g_autoptr(GVariant) arg_offsets = NULL;
92 g_autoptr(GVariant) arg_values = NULL;
93
94 ret = get_line_obj_by_name(lines[i], &line_obj, NULL);
95 if (!ret)
96 die("Line not found: %s\n", lines[i]);
97
98 line = gpiodbus_object_peek_line(line_obj);
99 req_path = gpiodbus_line_get_request_path(line);
100
101 if (!gpiodbus_line_get_managed(line))
102 die("Line '%s' not managed by gpio-manager, must be requested first",
103 lines[i]);
104
105 req_proxy = gpiodbus_request_proxy_new_for_bus_sync(
106 G_BUS_TYPE_SYSTEM,
107 G_DBUS_PROXY_FLAGS_NONE,
108 "io.gpiod1", req_path,
109 NULL, &err);
110 if (err)
111 die_gerror(err,
112 "Failed to get D-Bus proxy for '%s'",
113 req_path);
114
115 offset = gpiodbus_line_get_offset(line);
116 g_array_append_val(offsets, offset);
117
118 g_variant_builder_init(&builder, G_VARIANT_TYPE_ARRAY);
119 g_variant_builder_add(&builder, "u", offset);
120 arg_offsets = g_variant_ref_sink(
121 g_variant_builder_end(&builder));
122
123 ret = gpiodbus_request_call_get_values_sync(
124 req_proxy, arg_offsets,
125 G_DBUS_CALL_FLAGS_NONE,
126 -1, &arg_values, NULL,
127 &err);
128 if (!ret)
129 die_gerror(err, "Failed to get line values");
130
131 g_variant_iter_init(&iter, arg_values);
132 while (g_variant_iter_next(&iter, "i", &value))
133 g_array_append_val(values, value);
134 }
135 } else {
136 g_autoptr(GVariant) arg_offsets = NULL;
137 g_autoptr(GVariant) arg_values = NULL;
138
139 req_obj = get_request_obj(request_name);
140 request = gpiodbus_object_peek_request(req_obj);
141 chip_path = gpiodbus_request_get_chip_path(request);
142 chip_obj = get_chip_obj_by_path(chip_path);
143
144 if (lines) {
145 for (i = 0; i < num_lines; i++) {
146 g_autoptr(GpiodbusObject) line_obj = NULL;
147
148 line_obj = get_line_obj_by_name_for_chip(
149 chip_obj, lines[i]);
150 if (!line_obj)
151 die("Line not found: %s\n", lines[i]);
152
153 line = gpiodbus_object_peek_line(line_obj);
154
155 if (!gpiodbus_line_get_managed(line))
156 die("Line '%s' not managed by gpio-manager, must be requested first",
157 lines[i]);
158
159 offset = gpiodbus_line_get_offset(line);
160 g_array_append_val(offsets, offset);
161 }
162 } else {
163 offsets = get_request_offsets(request);
164 num_lines = offsets->len;
165 }
166
167 g_variant_builder_init(&builder, G_VARIANT_TYPE_ARRAY);
168 for (i = 0; i < offsets->len; i++)
169 g_variant_builder_add(&builder, "u",
170 g_array_index(offsets, guint, i));
171 arg_offsets = g_variant_ref_sink(
172 g_variant_builder_end(&builder));
173
174 ret = gpiodbus_request_call_get_values_sync(
175 request, arg_offsets,
176 G_DBUS_CALL_FLAGS_NONE,
177 -1, &arg_values, NULL,
178 &err);
179 if (!ret)
180 die_gerror(err, "Failed to get line values");
181
182 values = g_array_sized_new(FALSE, TRUE, sizeof(gint),
183 g_variant_n_children(arg_values));
184
185 g_variant_iter_init(&iter, arg_values);
186 while (g_variant_iter_next(&iter, "i", &value))
187 g_array_append_val(values, value);
188 }
189
190 for (i = 0; i < num_lines; i++) {
191 if (!unquoted)
192 g_print("\"");
193
194 if (lines)
195 g_print("%s", lines[i]);
196 else
197 g_print("%u", g_array_index(offsets, guint, i));
198
199 if (!unquoted)
200 g_print("\"");
201
202 g_print("=%s", g_array_index(values, guint, i) ?
203 numeric ? "1" : "active" :
204 numeric ? "0" : "inactive");
205
206 if (i != (num_lines - 1))
207 g_print(" ");
208 }
209 g_print("\n");
210
211 return EXIT_SUCCESS;
212 }