1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646
|
// SPDX-License-Identifier: GPL-2.0-or-later
// SPDX-FileCopyrightText: 2022-2023 Bartosz Golaszewski <bartosz.golaszewski@linaro.org>
#include <glib/gprintf.h>
#include <stdarg.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "common.h"
static void print_err_msg(GError *err, const gchar *fmt, va_list va)
{
g_printerr("%s: ", g_get_prgname());
g_vfprintf(stderr, fmt, va);
if (err)
g_printerr(": %s", err->message);
g_printerr("\n");
}
void die(const gchar *fmt, ...)
{
va_list va;
va_start(va, fmt);
print_err_msg(NULL, fmt, va);
va_end(va);
exit(EXIT_FAILURE);
}
void die_gerror(GError *err, const gchar *fmt, ...)
{
va_list va;
va_start(va, fmt);
print_err_msg(err, fmt, va);
va_end(va);
exit(EXIT_FAILURE);
}
void die_parsing_opts(const char *fmt, ...)
{
va_list va;
va_start(va, fmt);
print_err_msg(NULL, fmt, va);
va_end(va);
g_printerr("\nSee %s --help\n", g_get_prgname());
exit(EXIT_FAILURE);
}
void parse_options(const GOptionEntry *opts, const gchar *summary,
const gchar *description, int *argc, char ***argv)
{
g_autoptr(GOptionContext) ctx = NULL;
g_autoptr(GError) err = NULL;
gboolean ret;
ctx = g_option_context_new(NULL);
g_option_context_set_summary(ctx, summary);
g_option_context_set_description(ctx, description);
g_option_context_add_main_entries(ctx, opts, NULL);
g_option_context_set_strict_posix(ctx, TRUE);
ret = g_option_context_parse(ctx, argc, argv, &err);
if (!ret) {
g_printerr("%s: Option parsing failed: %s\nSee %s --help\n",
g_get_prgname(), err->message, g_get_prgname());
exit(EXIT_FAILURE);
}
}
void check_manager(void)
{
g_autoptr(GDBusProxy) proxy = NULL;
g_autoptr(GVariant) result = NULL;
g_autoptr(GError) err = NULL;
proxy = g_dbus_proxy_new_for_bus_sync(
G_BUS_TYPE_SYSTEM, G_DBUS_PROXY_FLAGS_NONE, NULL,
"io.gpiod1", "/io/gpiod1", "org.freedesktop.DBus.Peer",
NULL, &err);
if (!proxy)
die_gerror(err, "Unable to create a proxy to '/io/gpiod1'");
result = g_dbus_proxy_call_sync(proxy, "Ping", NULL,
G_DBUS_CALL_FLAGS_NONE, -1, NULL,
&err);
if (!result) {
if (err->domain == G_DBUS_ERROR) {
switch (err->code) {
case G_DBUS_ERROR_ACCESS_DENIED:
die("Access to gpio-manager denied, check your permissions");
case G_DBUS_ERROR_SERVICE_UNKNOWN:
die("gpio-manager not running");
}
}
die_gerror(err, "Failed trying to contect the gpio manager");
}
}
gboolean quit_main_loop_on_signal(gpointer user_data)
{
GMainLoop *loop = user_data;
g_main_loop_quit(loop);
return G_SOURCE_REMOVE;
}
void die_on_name_vanished(GDBusConnection *con G_GNUC_UNUSED,
const gchar *name G_GNUC_UNUSED,
gpointer user_data G_GNUC_UNUSED)
{
die("gpio-manager exited unexpectedly");
}
GList *strv_to_gstring_list(GStrv lines)
{
gsize llen = g_strv_length(lines);
GList *list = NULL;
guint i;
for (i = 0; i < llen; i++)
list = g_list_append(list, g_string_new(lines[i]));
return list;
}
gint output_value_from_str(const gchar *value_str)
{
if ((g_strcmp0(value_str, "active") == 0) ||
(g_strcmp0(value_str, "1") == 0))
return 1;
else if ((g_strcmp0(value_str, "inactive") == 0) ||
(g_strcmp0(value_str, "0") == 0))
return 0;
die_parsing_opts("invalid output value: '%s'", value_str);
}
static gboolean str_is_all_digits(const gchar *str)
{
for (; *str; str++) {
if (!g_ascii_isdigit(*str))
return FALSE;
}
return TRUE;
}
static gint compare_objs_by_path(GDBusObject *a, GDBusObject *b)
{
return strverscmp(g_dbus_object_get_object_path(a),
g_dbus_object_get_object_path(b));
}
GDBusObjectManager *get_object_manager_client(const gchar *obj_path)
{
g_autoptr(GDBusObjectManager) manager = NULL;
g_autoptr(GError) err = NULL;
manager = gpiodbus_object_manager_client_new_for_bus_sync(
G_BUS_TYPE_SYSTEM,
G_DBUS_OBJECT_MANAGER_CLIENT_FLAGS_NONE,
"io.gpiod1", obj_path, NULL, &err);
if (!manager)
die_gerror(err,
"failed to create the object manager client for %s",
obj_path);
return g_object_ref(manager);
}
static gchar *make_chip_obj_path(const gchar *chip)
{
return g_strdup_printf(
str_is_all_digits(chip) ?
"/io/gpiod1/chips/gpiochip%s" :
"/io/gpiod1/chips/%s",
chip);
}
GpiodbusObject *get_chip_obj_by_path(const gchar *obj_path)
{
g_autoptr(GDBusObjectManager) manager = NULL;
g_autoptr(GpiodbusObject) chip_obj = NULL;
manager = get_object_manager_client("/io/gpiod1/chips");
chip_obj = GPIODBUS_OBJECT(g_dbus_object_manager_get_object(manager,
obj_path));
if (!chip_obj)
die("No such chip object: '%s'", obj_path);
return g_object_ref(chip_obj);
}
GpiodbusObject *get_chip_obj(const gchar *chip_name)
{
g_autofree gchar *chip_path = make_chip_obj_path(chip_name);
return get_chip_obj_by_path(chip_path);
}
GList *get_chip_objs(GStrv chip_names)
{
g_autoptr(GDBusObjectManager) manager = NULL;
GList *objs = NULL;
gint i;
manager = get_object_manager_client("/io/gpiod1/chips");
if (!chip_names)
return g_list_sort(g_dbus_object_manager_get_objects(manager),
(GCompareFunc)compare_objs_by_path);
for (i = 0; chip_names[i]; i++) {
g_autofree gchar *obj_path = make_chip_obj_path(chip_names[i]);
g_autoptr(GpiodbusObject) obj = NULL;
obj = GPIODBUS_OBJECT(
g_dbus_object_manager_get_object(manager, obj_path));
if (!obj)
die("No such chip: '%s'", chip_names[i]);
objs = g_list_insert_sorted(objs, g_object_ref(obj),
(GCompareFunc)compare_objs_by_path);
}
return objs;
}
gchar *make_request_obj_path(const gchar *request)
{
return g_strdup_printf(
str_is_all_digits(request) ?
"/io/gpiod1/requests/request%s" :
"/io/gpiod1/requests/%s",
request);
}
GpiodbusObject *get_request_obj(const gchar *request_name)
{
g_autoptr(GDBusObjectManager) manager = NULL;
g_autoptr(GpiodbusObject) req_obj = NULL;
g_autofree gchar *obj_path = NULL;
manager = get_object_manager_client("/io/gpiod1/requests");
obj_path = make_request_obj_path(request_name);
req_obj = GPIODBUS_OBJECT(g_dbus_object_manager_get_object(manager,
obj_path));
if (!req_obj)
die("No such request: '%s'", request_name);
return g_object_ref(req_obj);
}
GList *get_request_objs(void)
{
g_autoptr(GDBusObjectManager) manager = NULL;
GList *objs = NULL;
manager = get_object_manager_client("/io/gpiod1/requests");
objs = g_dbus_object_manager_get_objects(manager);
return g_list_sort(objs, (GCompareFunc)compare_objs_by_path);
}
GArray *get_request_offsets(GpiodbusRequest *request)
{
const gchar *chip_path, *line_path, *const *line_paths;
g_autoptr(GDBusObjectManager) manager = NULL;
g_autoptr(GArray) offsets = NULL;
GpiodbusLine *line;
guint i, offset;
chip_path = gpiodbus_request_get_chip_path(request);
line_paths = gpiodbus_request_get_line_paths(request);
offsets = g_array_new(FALSE, TRUE, sizeof(guint));
manager = get_object_manager_client(chip_path);
for (i = 0, line_path = line_paths[i];
line_path;
line_path = line_paths[++i]) {
g_autoptr(GDBusObject) line_obj = NULL;
line_obj = g_dbus_object_manager_get_object(manager, line_path);
line = gpiodbus_object_peek_line(GPIODBUS_OBJECT(line_obj));
offset = gpiodbus_line_get_offset(line);
g_array_append_val(offsets, offset);
}
return g_array_ref(offsets);
}
gboolean get_line_obj_by_name(const gchar *name, GpiodbusObject **line_obj,
GpiodbusObject **chip_obj)
{
g_autolist(GpiodbusObject) chip_objs = NULL;
GList *pos;
if (str_is_all_digits(name))
die("Refusing to use line offsets if chip is not specified");
chip_objs = get_chip_objs(NULL);
for (pos = g_list_first(chip_objs); pos; pos = g_list_next(pos)) {
*line_obj = get_line_obj_by_name_for_chip(pos->data, name);
if (*line_obj) {
if (chip_obj)
*chip_obj = g_object_ref(pos->data);
return TRUE;
}
}
return FALSE;
}
GpiodbusObject *
get_line_obj_by_name_for_chip(GpiodbusObject *chip_obj, const gchar *line_name)
{
g_autoptr(GDBusObjectManager) manager = NULL;
g_autolist(GpiodbusObject) line_objs = NULL;
const gchar *chip_path;
GpiodbusLine *line;
guint64 offset;
GList *pos;
chip_path = g_dbus_object_get_object_path(G_DBUS_OBJECT(chip_obj));
manager = get_object_manager_client(chip_path);
line_objs = g_dbus_object_manager_get_objects(manager);
for (pos = g_list_first(line_objs); pos; pos = g_list_next(pos)) {
line = gpiodbus_object_peek_line(pos->data);
if (g_strcmp0(gpiodbus_line_get_name(line), line_name) == 0)
return g_object_ref(pos->data);
if (str_is_all_digits(line_name)) {
offset = g_ascii_strtoull(line_name, NULL, 10);
if (offset == gpiodbus_line_get_offset(line))
return g_object_ref(pos->data);
}
}
return NULL;
}
GList *get_all_line_objs_for_chip(GpiodbusObject *chip_obj)
{
g_autoptr(GDBusObjectManager) manager = NULL;
const gchar *chip_path;
chip_path = g_dbus_object_get_object_path(G_DBUS_OBJECT(chip_obj));
manager = get_object_manager_client(chip_path);
return g_list_sort(g_dbus_object_manager_get_objects(manager),
(GCompareFunc)compare_objs_by_path);
}
static gchar *sanitize_str(const gchar *str)
{
if (!strlen(str))
return NULL;
return g_strdup(str);
}
static const gchar *sanitize_direction(const gchar *direction)
{
if ((g_strcmp0(direction, "input") == 0) ||
(g_strcmp0(direction, "output") == 0))
return direction;
die("invalid direction value received from manager: '%s'", direction);
}
static const gchar *sanitize_drive(const gchar *drive)
{
if ((g_strcmp0(drive, "push-pull") == 0) ||
(g_strcmp0(drive, "open-source") == 0) ||
(g_strcmp0(drive, "open-drain") == 0))
return drive;
die("invalid drive value received from manager: '%s'", drive);
}
static const gchar *sanitize_bias(const gchar *bias)
{
if ((g_strcmp0(bias, "pull-up") == 0) ||
(g_strcmp0(bias, "pull-down") == 0) ||
(g_strcmp0(bias, "disabled") == 0))
return bias;
if (g_strcmp0(bias, "unknown") == 0)
return NULL;
die("invalid bias value received from manager: '%s'", bias);
}
static const gchar *sanitize_edge(const gchar *edge)
{
if ((g_strcmp0(edge, "rising") == 0) ||
(g_strcmp0(edge, "falling") == 0) ||
(g_strcmp0(edge, "both") == 0))
return edge;
if (g_strcmp0(edge, "none") == 0)
return NULL;
die("invalid edge value received from manager: '%s'", edge);
}
static const gchar *sanitize_clock(const gchar *event_clock)
{
if ((g_strcmp0(event_clock, "monotonic") == 0) ||
(g_strcmp0(event_clock, "realtime") == 0) ||
(g_strcmp0(event_clock, "hte") == 0))
return event_clock;
die("invalid clock value received from manager: '%s'", event_clock);
}
gchar *sanitize_object_path(const gchar *path)
{
if (g_strcmp0(path, "/") == 0)
return g_strdup("N/A");
return g_path_get_basename(path);
}
LineProperties *get_line_properties(GpiodbusLine *line)
{
LineProperties *props;
props = g_malloc0(sizeof(*props));
props->name = sanitize_str(gpiodbus_line_get_name(line));
props->offset = gpiodbus_line_get_offset(line);
props->used = gpiodbus_line_get_used(line);
props->consumer = sanitize_str(gpiodbus_line_get_consumer(line));
props->direction = sanitize_direction(
gpiodbus_line_get_direction(line));
props->drive = sanitize_drive(gpiodbus_line_get_drive(line));
props->bias = sanitize_bias(gpiodbus_line_get_bias(line));
props->active_low = gpiodbus_line_get_active_low(line);
props->edge = sanitize_edge(gpiodbus_line_get_edge_detection(line));
props->debounced = gpiodbus_line_get_debounced(line);
props->debounce_period = gpiodbus_line_get_debounce_period_us(line);
props->event_clock = sanitize_clock(
gpiodbus_line_get_event_clock(line));
props->managed = gpiodbus_line_get_managed(line);
props->request_name = sanitize_object_path(
gpiodbus_line_get_request_path(line));
return props;
}
void free_line_properties(LineProperties *props)
{
g_free(props->name);
g_free(props->consumer);
g_free(props->request_name);
g_free(props);
}
void validate_line_config_opts(LineConfigOpts *opts)
{
gint counter;
if (opts->input && opts->output)
die_parsing_opts("--input and --output are mutually exclusive");
if (opts->both_edges)
opts->rising_edge = opts->falling_edge = TRUE;
if (!opts->input && (opts->rising_edge || opts->falling_edge))
die_parsing_opts("monitoring edges is only possible in input mode");
counter = 0;
if (opts->push_pull)
counter++;
if (opts->open_drain)
counter++;
if (opts->open_source)
counter++;
if (counter > 1)
die_parsing_opts("--push-pull, --open-drain and --open-source are mutually exclusive");
if (!opts->output && (counter > 0))
die_parsing_opts("--push-pull, --open-drain and --open-source are only available in output mode");
counter = 0;
if (opts->pull_up)
counter++;
if (opts->pull_down)
counter++;
if (opts->bias_disabled)
counter++;
if (counter > 1)
die_parsing_opts("--pull-up, --pull-down and --bias-disabled are mutually exclusive");
counter = 0;
if (opts->clock_monotonic)
counter++;
if (opts->clock_realtime)
counter++;
if (opts->clock_hte)
counter++;
if (counter > 1)
die_parsing_opts("--clock-monotonic, --clock-realtime and --clock-hte are mutually exclusive");
if (counter > 0 && (!opts->rising_edge && !opts->falling_edge))
die_parsing_opts("--clock-monotonic, --clock-realtime and --clock-hte can only be used with edge detection enabled");
if (opts->debounce_period && (!opts->rising_edge && !opts->falling_edge))
die_parsing_opts("--debounce-period can only be used with edge-detection enabled");
}
GVariant *make_line_config(GArray *offsets, LineConfigOpts *opts)
{
const char *direction, *edge = NULL, *bias = NULL, *drive = NULL,
*clock = NULL;
g_autoptr(GVariant) output_values = NULL;
g_autoptr(GVariant) line_settings = NULL;
g_autoptr(GVariant) line_offsets = NULL;
g_autoptr(GVariant) line_configs = NULL;
g_autoptr(GVariant) line_config = NULL;
GVariantBuilder builder;
guint i;
g_variant_builder_init(&builder, G_VARIANT_TYPE_ARRAY);
for (i = 0; i < offsets->len; i++)
g_variant_builder_add_value(&builder,
g_variant_new_uint32(g_array_index(offsets, guint, i)));
line_offsets = g_variant_builder_end(&builder);
g_variant_builder_init(&builder, G_VARIANT_TYPE_ARRAY);
if (opts->input)
direction = "input";
else if (opts->output)
direction = "output";
else
direction = "as-is";
if (direction)
g_variant_builder_add_value(&builder,
g_variant_new("{sv}", "direction",
g_variant_new_string(direction)));
if (opts->rising_edge && opts->falling_edge)
edge = "both";
else if (opts->falling_edge)
edge = "falling";
else if (opts->rising_edge)
edge = "rising";
if (edge)
g_variant_builder_add_value(&builder,
g_variant_new("{sv}", "edge",
g_variant_new_string(edge)));
if (opts->pull_up)
bias = "pull-up";
else if (opts->pull_down)
bias = "pull-down";
else if (opts->bias_disabled)
bias = "disabled";
if (bias)
g_variant_builder_add_value(&builder,
g_variant_new("{sv}", "bias",
g_variant_new_string(bias)));
if (opts->push_pull)
drive = "push-pull";
else if (opts->open_drain)
drive = "open-drain";
else if (opts->open_source)
drive = "open-source";
if (drive)
g_variant_builder_add_value(&builder,
g_variant_new("{sv}", "drive",
g_variant_new_string(drive)));
if (opts->active_low)
g_variant_builder_add_value(&builder,
g_variant_new("{sv}", "active-low",
g_variant_new_boolean(TRUE)));
if (opts->debounce_period)
g_variant_builder_add_value(&builder,
g_variant_new("{sv}", "debounce-period",
g_variant_new_int64(opts->debounce_period)));
if (opts->clock_monotonic)
clock = "monotonic";
else if (opts->clock_realtime)
clock = "realtime";
else if (opts->clock_hte)
clock = "hte";
if (clock)
g_variant_builder_add_value(&builder,
g_variant_new("{sv}", "event-clock",
g_variant_new_string(clock)));
line_settings = g_variant_builder_end(&builder);
g_variant_builder_init(&builder, G_VARIANT_TYPE_TUPLE);
g_variant_builder_add_value(&builder, g_variant_ref(line_offsets));
g_variant_builder_add_value(&builder, g_variant_ref(line_settings));
line_config = g_variant_builder_end(&builder);
g_variant_builder_init(&builder, G_VARIANT_TYPE_ARRAY);
g_variant_builder_add_value(&builder, g_variant_ref(line_config));
line_configs = g_variant_builder_end(&builder);
if (opts->output_values) {
g_variant_builder_init(&builder, G_VARIANT_TYPE_ARRAY);
for (i = 0; i < opts->output_values->len; i++) {
g_variant_builder_add(&builder, "i",
g_array_index(opts->output_values,
gint, i));
}
output_values = g_variant_builder_end(&builder);
} else {
output_values = g_variant_new("ai", opts->output_values);
}
g_variant_builder_init(&builder, G_VARIANT_TYPE_TUPLE);
g_variant_builder_add_value(&builder, g_variant_ref(line_configs));
g_variant_builder_add_value(&builder, g_variant_ref(output_values));
return g_variant_ref_sink(g_variant_builder_end(&builder));
}
|