list-gpio-lines.c 2.12 KB
Newer Older
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

// ---------------------------------------------------------------------
// list-gpio-lines.c
//
//   This is an example of GPIO access with the libGPIOd library.
//
// Copyright 2018 Christophe BLAESS (christophe@blaess.fr)
// License GPL v2.
// ---------------------------------------------------------------------

#include <gpiod.h>
#include <stdio.h>
#include <stdlib.h>

int list_gpio_lines(struct gpiod_chip *chip)
{
	struct gpiod_line_bulk bulk;
	struct gpiod_line *line;
	int offset;
	const char *string;

	fprintf(stdout, "%s - %s - %d lines\n",
		gpiod_chip_name(chip),
		gpiod_chip_label(chip),
		gpiod_chip_num_lines(chip));

	if (gpiod_chip_get_all_lines(chip, &bulk) != 0) {
		perror("gpiod_chip_get_all_lines()");
		return -1;
	}

	gpiod_line_bulk_foreach_line_off(&bulk, line, offset) {
		fprintf(stdout, "  %2d: ", offset);

		string = gpiod_line_name(line);
		if (string == NULL)
			fprintf(stdout, "- ");
		else
			fprintf(stdout, "%s ", string);

		string = gpiod_line_consumer(line);
		if (string == NULL)
			fprintf(stdout, "() ");
		else
			fprintf(stdout, "(%s) ", string);;

		if (gpiod_line_direction(line) == GPIOD_LINE_DIRECTION_OUTPUT)
			fprintf(stdout, "out ");
		else
			fprintf(stdout, "in  ");

		if (gpiod_line_active_state(line) == GPIOD_LINE_ACTIVE_STATE_LOW)
			fprintf(stdout, "active-low ");

		if (gpiod_line_is_open_drain(line))
			fprintf(stdout, "open-drain ");

		if (gpiod_line_is_open_source(line))
			fprintf(stdout, "open-source ");

		if (gpiod_line_is_used(line))
			fprintf(stdout, "* ");

		fprintf(stdout, "\n");
	}

	return 0;
}

int main(int argc, char *argv[])
{
	struct gpiod_chip_iter *iter;
	struct gpiod_chip *chip;
	int i;

	if (argc == 1) {
		// No argument: iterate on every GPIO chip.
		iter = gpiod_chip_iter_new();
		if (iter == NULL) {
			perror("gpiod_chip_iter");
			exit(EXIT_FAILURE);
		}
		while ((chip = gpiod_chip_iter_next(iter)) != NULL)
			list_gpio_lines(chip);
		gpiod_chip_iter_free(iter);

	} else {
		for (i = 1; i < argc; i ++) {
			chip = gpiod_chip_open_lookup(argv[i]);
			if (chip == NULL)
				perror(argv[i]);
			else
				list_gpio_lines(chip);
		}
	}

	return EXIT_SUCCESS;
}