| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
#include <stdlib.h> |
| 5 |
|
| 6 |
#include "common.h" |
| 7 |
|
| 8 |
int gpiocli_reconfigure_main(int argc, char **argv) |
| 9 |
{ |
| 10 |
static const gchar *const summary = |
| 11 |
"Change the line configuration for an existing request."; |
| 12 |
|
| 13 |
g_autoptr(GpiodbusObject) req_obj = NULL; |
| 14 |
g_autoptr(GVariant) line_config = NULL; |
| 15 |
g_autoptr(GArray) output_values = NULL; |
| 16 |
LineConfigOpts line_cfg_opts = { }; |
| 17 |
g_autoptr(GArray) offsets = NULL; |
| 18 |
g_auto(GStrv) remaining = NULL; |
| 19 |
g_autoptr(GError) err = NULL; |
| 20 |
GpiodbusRequest *request; |
| 21 |
gsize num_values; |
| 22 |
gboolean ret; |
| 23 |
gint val; |
| 24 |
guint i; |
| 25 |
|
| 26 |
const GOptionEntry opts[] = { |
| 27 |
LINE_CONFIG_OPTIONS(&line_cfg_opts), |
| 28 |
{ |
| 29 |
.long_name = G_OPTION_REMAINING, |
| 30 |
.flags = G_OPTION_FLAG_NONE, |
| 31 |
.arg = G_OPTION_ARG_STRING_ARRAY, |
| 32 |
.arg_data = &remaining, |
| 33 |
.arg_description = "<request> [value1] [value2]...", |
| 34 |
}, |
| 35 |
{ } |
| 36 |
}; |
| 37 |
|
| 38 |
parse_options(opts, summary, NULL, &argc, &argv); |
| 39 |
validate_line_config_opts(&line_cfg_opts); |
| 40 |
|
| 41 |
if (!remaining || g_strv_length(remaining) == 0) |
| 42 |
die_parsing_opts("Exactly one request to reconfigure must be specified."); |
| 43 |
|
| 44 |
num_values = g_strv_length(remaining) - 1; |
| 45 |
|
| 46 |
check_manager(); |
| 47 |
|
| 48 |
req_obj = get_request_obj(remaining[0]); |
| 49 |
request = gpiodbus_object_peek_request(req_obj); |
| 50 |
offsets = get_request_offsets(request); |
| 51 |
|
| 52 |
if (num_values) { |
| 53 |
if (num_values != offsets->len) |
| 54 |
die_parsing_opts("The number of output values must correspond to the number of lines in the request"); |
| 55 |
|
| 56 |
output_values = g_array_sized_new(FALSE, TRUE, sizeof(gint), |
| 57 |
num_values); |
| 58 |
|
| 59 |
for (i = 0; i < num_values; i++) { |
| 60 |
val = output_value_from_str(remaining[i + 1]); |
| 61 |
g_array_append_val(output_values, val); |
| 62 |
} |
| 63 |
} |
| 64 |
|
| 65 |
line_cfg_opts.output_values = output_values; |
| 66 |
line_config = make_line_config(offsets, &line_cfg_opts); |
| 67 |
|
| 68 |
ret = gpiodbus_request_call_reconfigure_lines_sync( |
| 69 |
request, line_config, |
| 70 |
G_DBUS_CALL_FLAGS_NONE, |
| 71 |
-1, NULL, &err); |
| 72 |
if (!ret) |
| 73 |
die_gerror(err, "Failed to reconfigure lines"); |
| 74 |
|
| 75 |
return EXIT_SUCCESS; |
| 76 |
} |