| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
#include <stdlib.h> |
| 5 |
|
| 6 |
#include "common.h" |
| 7 |
|
| 8 |
static void free_str(gpointer data) |
| 9 |
{ |
| 10 |
GString *str = data; |
| 11 |
|
| 12 |
g_string_free(str, TRUE); |
| 13 |
} |
| 14 |
|
| 15 |
int gpiocli_set_main(int argc, char **argv) |
| 16 |
{ |
| 17 |
static const gchar *const summary = |
| 18 |
"Set values of one or more GPIO lines."; |
| 19 |
|
| 20 |
static const gchar *const description = |
| 21 |
"If -r/--request is specified then all the lines must belong to the same\n" |
| 22 |
"request (and - by extension - the same chip)."; |
| 23 |
|
| 24 |
const gchar *request_name = NULL, *chip_path, *req_path; |
| 25 |
g_autoptr(GpiodbusObject) chip_obj = NULL; |
| 26 |
g_autoptr(GpiodbusObject) req_obj = NULL; |
| 27 |
g_autoptr(GPtrArray) line_names = NULL; |
| 28 |
g_autoptr(GArray) values = NULL; |
| 29 |
g_autoptr(GError) err = NULL; |
| 30 |
g_auto(GStrv) lines = NULL; |
| 31 |
GpiodbusRequest *request; |
| 32 |
GVariantBuilder builder; |
| 33 |
GpiodbusLine *line; |
| 34 |
gsize num_lines, i; |
| 35 |
GString *line_name; |
| 36 |
gboolean ret; |
| 37 |
guint offset; |
| 38 |
gint val; |
| 39 |
|
| 40 |
const GOptionEntry opts[] = { |
| 41 |
{ |
| 42 |
.long_name = "request", |
| 43 |
.short_name = 'r', |
| 44 |
.flags = G_OPTION_FLAG_NONE, |
| 45 |
.arg = G_OPTION_ARG_STRING, |
| 46 |
.arg_data = &request_name, |
| 47 |
.description = "restrict scope to a particular request", |
| 48 |
.arg_description = "<request>", |
| 49 |
}, |
| 50 |
{ |
| 51 |
.long_name = G_OPTION_REMAINING, |
| 52 |
.flags = G_OPTION_FLAG_NONE, |
| 53 |
.arg = G_OPTION_ARG_STRING_ARRAY, |
| 54 |
.arg_data = &lines, |
| 55 |
.arg_description = "<line1=value1> [line2=value2] ...", |
| 56 |
}, |
| 57 |
{ } |
| 58 |
}; |
| 59 |
|
| 60 |
parse_options(opts, summary, description, &argc, &argv); |
| 61 |
|
| 62 |
if (!lines) |
| 63 |
die_parsing_opts("at least one line value must be specified"); |
| 64 |
|
| 65 |
num_lines = g_strv_length(lines); |
| 66 |
line_names = g_ptr_array_new_full(num_lines, free_str); |
| 67 |
values = g_array_sized_new(FALSE, TRUE, sizeof(gint), num_lines); |
| 68 |
|
| 69 |
for (i = 0; i < num_lines; i++) { |
| 70 |
g_auto(GStrv) tokens = NULL; |
| 71 |
|
| 72 |
tokens = g_strsplit(lines[i], "=", 2); |
| 73 |
if (g_strv_length(tokens) != 2) |
| 74 |
die_parsing_opts("line must have a single value assigned"); |
| 75 |
|
| 76 |
g_ptr_array_add(line_names, g_string_new(tokens[0])); |
| 77 |
val = output_value_from_str(tokens[1]); |
| 78 |
g_array_append_val(values, val); |
| 79 |
} |
| 80 |
|
| 81 |
check_manager(); |
| 82 |
|
| 83 |
if (request_name) { |
| 84 |
g_autoptr(GVariant) arg_values = NULL; |
| 85 |
g_autoptr(GArray) offsets = NULL; |
| 86 |
|
| 87 |
req_obj = get_request_obj(request_name); |
| 88 |
request = gpiodbus_object_peek_request(req_obj); |
| 89 |
chip_path = gpiodbus_request_get_chip_path(request); |
| 90 |
chip_obj = get_chip_obj_by_path(chip_path); |
| 91 |
offsets = g_array_sized_new(FALSE, TRUE, sizeof(guint), |
| 92 |
num_lines); |
| 93 |
|
| 94 |
for (i = 0; i < num_lines; i++) { |
| 95 |
g_autoptr(GpiodbusObject) line_obj = NULL; |
| 96 |
|
| 97 |
line_name = g_ptr_array_index(line_names, i); |
| 98 |
|
| 99 |
line_obj = get_line_obj_by_name_for_chip(chip_obj, |
| 100 |
line_name->str); |
| 101 |
if (!line_obj) |
| 102 |
die("Line not found: %s\n", line_name->str); |
| 103 |
|
| 104 |
line = gpiodbus_object_peek_line(line_obj); |
| 105 |
offset = gpiodbus_line_get_offset(line); |
| 106 |
g_array_append_val(offsets, offset); |
| 107 |
} |
| 108 |
|
| 109 |
g_variant_builder_init(&builder, G_VARIANT_TYPE_ARRAY); |
| 110 |
for (i = 0; i < num_lines; i++) { |
| 111 |
g_variant_builder_add(&builder, "{ui}", |
| 112 |
g_array_index(offsets, guint, i), |
| 113 |
g_array_index(values, gint, i)); |
| 114 |
} |
| 115 |
|
| 116 |
arg_values = g_variant_ref_sink( |
| 117 |
g_variant_builder_end(&builder)); |
| 118 |
|
| 119 |
ret = gpiodbus_request_call_set_values_sync( |
| 120 |
request, arg_values, |
| 121 |
G_DBUS_CALL_FLAGS_NONE, |
| 122 |
-1, NULL, &err); |
| 123 |
if (!ret) |
| 124 |
die_gerror(err, "Failed to set line values"); |
| 125 |
|
| 126 |
return EXIT_SUCCESS; |
| 127 |
} |
| 128 |
|
| 129 |
for (i = 0; i < num_lines; i++) { |
| 130 |
g_autoptr(GpiodbusRequest) req_proxy = NULL; |
| 131 |
g_autoptr(GpiodbusObject) line_obj = NULL; |
| 132 |
g_autoptr(GVariant) arg_values = NULL; |
| 133 |
|
| 134 |
line_name = g_ptr_array_index(line_names, i); |
| 135 |
|
| 136 |
ret = get_line_obj_by_name(line_name->str, &line_obj, NULL); |
| 137 |
if (!ret) |
| 138 |
die("Line not found: %s\n", line_name->str); |
| 139 |
|
| 140 |
line = gpiodbus_object_peek_line(line_obj); |
| 141 |
req_path = gpiodbus_line_get_request_path(line); |
| 142 |
|
| 143 |
if (!gpiodbus_line_get_managed(line)) |
| 144 |
die("Line '%s' not managed by gpio-manager, must be requested first", |
| 145 |
line_name->str); |
| 146 |
|
| 147 |
req_proxy = gpiodbus_request_proxy_new_for_bus_sync( |
| 148 |
G_BUS_TYPE_SYSTEM, |
| 149 |
G_DBUS_PROXY_FLAGS_NONE, |
| 150 |
"io.gpiod1", req_path, |
| 151 |
NULL, &err); |
| 152 |
if (err) |
| 153 |
die_gerror(err, "Failed to get D-Bus proxy for '%s'", |
| 154 |
req_path); |
| 155 |
|
| 156 |
offset = gpiodbus_line_get_offset(line); |
| 157 |
|
| 158 |
g_variant_builder_init(&builder, G_VARIANT_TYPE_ARRAY); |
| 159 |
g_variant_builder_add(&builder, "{ui}", offset, |
| 160 |
g_array_index(values, gint, i)); |
| 161 |
arg_values = g_variant_ref_sink( |
| 162 |
g_variant_builder_end(&builder)); |
| 163 |
|
| 164 |
ret = gpiodbus_request_call_set_values_sync( |
| 165 |
req_proxy, arg_values, |
| 166 |
G_DBUS_CALL_FLAGS_NONE, -1, |
| 167 |
NULL, &err); |
| 168 |
if (!ret) |
| 169 |
die_gerror(err, "Failed to set line values"); |
| 170 |
} |
| 171 |
|
| 172 |
return EXIT_SUCCESS; |
| 173 |
} |