| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
#include <stdlib.h> |
| 5 |
#include <string.h> |
| 6 |
|
| 7 |
#include "common.h" |
| 8 |
|
| 9 |
static gchar *make_line_name(const gchar *name) |
| 10 |
{ |
| 11 |
if (!name) |
| 12 |
return g_strdup("unnamed"); |
| 13 |
|
| 14 |
return g_strdup_printf("\"%s\"", name); |
| 15 |
} |
| 16 |
|
| 17 |
static void do_print_line_info(GpiodbusObject *line_obj, |
| 18 |
GpiodbusObject *chip_obj) |
| 19 |
{ |
| 20 |
g_autoptr(LineProperties) props = NULL; |
| 21 |
g_autoptr(GString) attributes = NULL; |
| 22 |
g_autofree gchar *line_name = NULL; |
| 23 |
GpiodbusChip *chip; |
| 24 |
|
| 25 |
props = get_line_properties(gpiodbus_object_peek_line(line_obj)); |
| 26 |
line_name = make_line_name(props->name); |
| 27 |
|
| 28 |
attributes = g_string_new("["); |
| 29 |
|
| 30 |
if (props->used) |
| 31 |
g_string_append_printf(attributes, "used,consumer=\"%s\",", |
| 32 |
props->consumer); |
| 33 |
|
| 34 |
if (props->managed) |
| 35 |
g_string_append_printf(attributes, "managed=\"%s\",", |
| 36 |
props->request_name); |
| 37 |
|
| 38 |
if (props->edge) { |
| 39 |
g_string_append_printf(attributes, "edges=%s,event-clock=%s,", |
| 40 |
props->edge, props->event_clock); |
| 41 |
if (props->debounced) |
| 42 |
g_string_append_printf(attributes, |
| 43 |
"debounce-period=%"G_GUINT64_FORMAT",", |
| 44 |
props->debounce_period); |
| 45 |
} |
| 46 |
|
| 47 |
if (props->bias) |
| 48 |
g_string_append_printf(attributes, "bias=%s,", props->bias); |
| 49 |
|
| 50 |
if (props->active_low) |
| 51 |
attributes = g_string_append(attributes, "active-low,"); |
| 52 |
|
| 53 |
g_string_append_printf(attributes, "%s", props->direction); |
| 54 |
|
| 55 |
if (g_strcmp0(props->direction, "output") == 0) |
| 56 |
g_string_append_printf(attributes, ",%s", props->drive); |
| 57 |
|
| 58 |
attributes = g_string_append(attributes, "]"); |
| 59 |
|
| 60 |
if (chip_obj) { |
| 61 |
chip = gpiodbus_object_peek_chip(chip_obj); |
| 62 |
g_print("%s ", gpiodbus_chip_get_name(chip)); |
| 63 |
} else { |
| 64 |
g_print("\tline "); |
| 65 |
} |
| 66 |
|
| 67 |
g_print("%3u:\t%s\t\t%s\n", props->offset, line_name, attributes->str); |
| 68 |
} |
| 69 |
|
| 70 |
static void print_line_info(gpointer elem, gpointer user_data G_GNUC_UNUSED) |
| 71 |
{ |
| 72 |
GpiodbusObject *line_obj = elem; |
| 73 |
|
| 74 |
do_print_line_info(line_obj, NULL); |
| 75 |
} |
| 76 |
|
| 77 |
static void do_show_chip(GpiodbusObject *chip_obj) |
| 78 |
{ |
| 79 |
GpiodbusChip *chip = gpiodbus_object_peek_chip(chip_obj); |
| 80 |
g_autolist(GpiodbusObject) line_objs = NULL; |
| 81 |
|
| 82 |
g_print("%s - %u lines:\n", |
| 83 |
gpiodbus_chip_get_name(chip), |
| 84 |
gpiodbus_chip_get_num_lines(chip)); |
| 85 |
|
| 86 |
line_objs = get_all_line_objs_for_chip(chip_obj); |
| 87 |
g_list_foreach(line_objs, print_line_info, NULL); |
| 88 |
} |
| 89 |
|
| 90 |
static void show_chip(gpointer elem, gpointer user_data G_GNUC_UNUSED) |
| 91 |
{ |
| 92 |
GpiodbusObject *chip_obj = elem; |
| 93 |
|
| 94 |
do_show_chip(chip_obj); |
| 95 |
} |
| 96 |
|
| 97 |
static void show_line_with_chip(gpointer elem, gpointer user_data) |
| 98 |
{ |
| 99 |
g_autoptr(GpiodbusObject) line_obj = NULL; |
| 100 |
GpiodbusObject *chip_obj = user_data; |
| 101 |
g_autofree gchar *chip_name = NULL; |
| 102 |
GString *line_name = elem; |
| 103 |
|
| 104 |
line_obj = get_line_obj_by_name_for_chip(chip_obj, line_name->str); |
| 105 |
if (!line_obj) { |
| 106 |
chip_name = g_path_get_basename( |
| 107 |
g_dbus_object_get_object_path(G_DBUS_OBJECT(chip_obj))); |
| 108 |
die("no line '%s' on chip '%s'", line_name->str, chip_name); |
| 109 |
} |
| 110 |
|
| 111 |
do_print_line_info(line_obj, chip_obj); |
| 112 |
} |
| 113 |
|
| 114 |
static void show_line(gpointer elem, gpointer user_data G_GNUC_UNUSED) |
| 115 |
{ |
| 116 |
g_autoptr(GpiodbusObject) line_obj = NULL; |
| 117 |
g_autoptr(GpiodbusObject) chip_obj = NULL; |
| 118 |
GString *line_name = elem; |
| 119 |
gboolean ret; |
| 120 |
|
| 121 |
ret = get_line_obj_by_name(line_name->str, &line_obj, &chip_obj); |
| 122 |
if (!ret) |
| 123 |
die("line '%s' not found", line_name->str); |
| 124 |
|
| 125 |
do_print_line_info(line_obj, chip_obj); |
| 126 |
} |
| 127 |
|
| 128 |
int gpiocli_info_main(int argc, char **argv) |
| 129 |
{ |
| 130 |
static const gchar *const summary = |
| 131 |
"Print information about GPIO lines."; |
| 132 |
|
| 133 |
static const gchar *const description = |
| 134 |
"Lines are specified by name, or optionally by offset if the chip option\n" |
| 135 |
"is provided.\n"; |
| 136 |
|
| 137 |
g_autolist(GpiodbusObject) chip_objs = NULL; |
| 138 |
g_autolist(GString) line_name_list = NULL; |
| 139 |
g_autoptr(GpiodbusObject) chip_obj = NULL; |
| 140 |
g_auto(GStrv) line_names = NULL; |
| 141 |
const gchar *chip_name = NULL; |
| 142 |
|
| 143 |
const GOptionEntry opts[] = { |
| 144 |
{ |
| 145 |
.long_name = "chip", |
| 146 |
.short_name = 'c', |
| 147 |
.flags = G_OPTION_FLAG_NONE, |
| 148 |
.arg = G_OPTION_ARG_STRING, |
| 149 |
.arg_data = &chip_name, |
| 150 |
.description = "restrict scope to a particular chip", |
| 151 |
.arg_description = "<chip>", |
| 152 |
}, |
| 153 |
{ |
| 154 |
.long_name = G_OPTION_REMAINING, |
| 155 |
.flags = G_OPTION_FLAG_NONE, |
| 156 |
.arg = G_OPTION_ARG_STRING_ARRAY, |
| 157 |
.arg_data = &line_names, |
| 158 |
.arg_description = "[line1] [line2] ...", |
| 159 |
}, |
| 160 |
{ } |
| 161 |
}; |
| 162 |
|
| 163 |
parse_options(opts, summary, description, &argc, &argv); |
| 164 |
check_manager(); |
| 165 |
|
| 166 |
if (chip_name) |
| 167 |
chip_obj = get_chip_obj(chip_name); |
| 168 |
|
| 169 |
if (line_names) { |
| 170 |
line_name_list = strv_to_gstring_list(line_names); |
| 171 |
if (chip_obj) |
| 172 |
g_list_foreach(line_name_list, show_line_with_chip, |
| 173 |
chip_obj); |
| 174 |
else |
| 175 |
g_list_foreach(line_name_list, show_line, NULL); |
| 176 |
} else if (chip_obj) { |
| 177 |
do_show_chip(chip_obj); |
| 178 |
} else { |
| 179 |
chip_objs = get_chip_objs(NULL); |
| 180 |
g_list_foreach(chip_objs, show_chip, NULL); |
| 181 |
} |
| 182 |
|
| 183 |
return EXIT_SUCCESS; |
| 184 |
} |