| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
#include <gio/gio.h> |
| 5 |
#include <glib-unix.h> |
| 6 |
#include <stdlib.h> |
| 7 |
#include <string.h> |
| 8 |
|
| 9 |
#include "common.h" |
| 10 |
|
| 11 |
typedef struct { |
| 12 |
GList *lines; |
| 13 |
} MonitorData; |
| 14 |
|
| 15 |
static void on_edge_event(GpiodbusLine *line, GVariant *args, |
| 16 |
gpointer user_data G_GNUC_UNUSED) |
| 17 |
{ |
| 18 |
const char *name = gpiodbus_line_get_name(line); |
| 19 |
guint64 global_seqno, line_seqno, timestamp; |
| 20 |
gint32 edge; |
| 21 |
|
| 22 |
g_variant_get(args, "(ittt)", &edge, ×tamp, |
| 23 |
&global_seqno, &line_seqno); |
| 24 |
|
| 25 |
g_print("%"G_GUINT64_FORMAT" %s ", |
| 26 |
timestamp, edge ? "rising " : "falling"); |
| 27 |
if (strlen(name)) |
| 28 |
g_print("\"%s\"\n", name); |
| 29 |
else |
| 30 |
g_print("%u\n", gpiodbus_line_get_offset(line)); |
| 31 |
} |
| 32 |
|
| 33 |
static void connect_edge_event(gpointer elem, gpointer user_data) |
| 34 |
{ |
| 35 |
GpiodbusObject *line_obj = elem; |
| 36 |
MonitorData *data = user_data; |
| 37 |
g_autoptr(GError) err = NULL; |
| 38 |
const gchar *line_obj_path; |
| 39 |
GpiodbusLine *line; |
| 40 |
|
| 41 |
line_obj_path = g_dbus_object_get_object_path(G_DBUS_OBJECT(line_obj)); |
| 42 |
|
| 43 |
line = gpiodbus_line_proxy_new_for_bus_sync(G_BUS_TYPE_SYSTEM, |
| 44 |
G_DBUS_PROXY_FLAGS_NONE, |
| 45 |
"io.gpiod1", line_obj_path, |
| 46 |
NULL, &err); |
| 47 |
if (err) |
| 48 |
die_gerror(err, "Failed to get D-Bus proxy for '%s'", |
| 49 |
line_obj_path); |
| 50 |
|
| 51 |
if (!gpiodbus_line_get_managed(line)) |
| 52 |
die("Line must be managed by gpio-manager in order to be monitored"); |
| 53 |
|
| 54 |
if (g_strcmp0(gpiodbus_line_get_edge_detection(line), "none") == 0) |
| 55 |
die("Edge detection must be enabled for monitored lines"); |
| 56 |
|
| 57 |
data->lines = g_list_append(data->lines, line); |
| 58 |
|
| 59 |
g_signal_connect(line, "edge-event", G_CALLBACK(on_edge_event), NULL); |
| 60 |
} |
| 61 |
|
| 62 |
int gpiocli_monitor_main(int argc, char **argv) |
| 63 |
{ |
| 64 |
static const gchar *const summary = |
| 65 |
"Get values of one or more GPIO lines."; |
| 66 |
|
| 67 |
static const gchar *const description = |
| 68 |
"If -r/--request is specified then all the lines must belong to the same\n" |
| 69 |
"request (and - by extension - the same chip).\n" |
| 70 |
"\n" |
| 71 |
"If no lines are specified but -r/--request was passed then all lines within\n" |
| 72 |
"the request will be used."; |
| 73 |
|
| 74 |
g_autoptr(GDBusObjectManager) manager = NULL; |
| 75 |
const gchar *request_name = NULL, *chip_path; |
| 76 |
g_autolist(GpiodbusObject) line_objs = NULL; |
| 77 |
g_autoptr(GpiodbusObject) chip_obj = NULL; |
| 78 |
g_autoptr(GpiodbusObject) req_obj = NULL; |
| 79 |
g_autoptr(GArray) offsets = NULL; |
| 80 |
g_autoptr(GMainLoop) loop = NULL; |
| 81 |
g_auto(GStrv) lines = NULL; |
| 82 |
GpiodbusRequest *request; |
| 83 |
MonitorData data = { }; |
| 84 |
gsize num_lines, i; |
| 85 |
guint watch_id; |
| 86 |
gboolean ret; |
| 87 |
|
| 88 |
const GOptionEntry opts[] = { |
| 89 |
{ |
| 90 |
.long_name = "request", |
| 91 |
.short_name = 'r', |
| 92 |
.flags = G_OPTION_FLAG_NONE, |
| 93 |
.arg = G_OPTION_ARG_STRING, |
| 94 |
.arg_data = &request_name, |
| 95 |
.description = "restrict scope to a particular request", |
| 96 |
.arg_description = "<request>", |
| 97 |
}, |
| 98 |
{ |
| 99 |
.long_name = G_OPTION_REMAINING, |
| 100 |
.flags = G_OPTION_FLAG_NONE, |
| 101 |
.arg = G_OPTION_ARG_STRING_ARRAY, |
| 102 |
.arg_data = &lines, |
| 103 |
.arg_description = "[line0] [line1]...", |
| 104 |
}, |
| 105 |
{ } |
| 106 |
}; |
| 107 |
|
| 108 |
parse_options(opts, summary, description, &argc, &argv); |
| 109 |
|
| 110 |
watch_id = g_bus_watch_name(G_BUS_TYPE_SYSTEM, "io.gpiod1", |
| 111 |
G_BUS_NAME_WATCHER_FLAGS_NONE, |
| 112 |
NULL, die_on_name_vanished, NULL, NULL); |
| 113 |
check_manager(); |
| 114 |
|
| 115 |
if (!lines && !request_name) |
| 116 |
die_parsing_opts("either at least one line or the request must be specified"); |
| 117 |
|
| 118 |
if (request_name) { |
| 119 |
req_obj = get_request_obj(request_name); |
| 120 |
request = gpiodbus_object_peek_request(req_obj); |
| 121 |
chip_path = gpiodbus_request_get_chip_path(request); |
| 122 |
chip_obj = get_chip_obj_by_path(chip_path); |
| 123 |
offsets = g_array_new(FALSE, TRUE, sizeof(guint)); |
| 124 |
|
| 125 |
if (lines) { |
| 126 |
num_lines = g_strv_length(lines); |
| 127 |
|
| 128 |
for (i = 0; i < num_lines; i++) { |
| 129 |
g_autoptr(GpiodbusObject) line_obj = NULL; |
| 130 |
|
| 131 |
line_obj = get_line_obj_by_name_for_chip( |
| 132 |
chip_obj, lines[i]); |
| 133 |
if (!line_obj) |
| 134 |
die("Line not found: %s\n", lines[i]); |
| 135 |
|
| 136 |
line_objs = g_list_append(line_objs, |
| 137 |
g_object_ref(line_obj)); |
| 138 |
} |
| 139 |
} else { |
| 140 |
offsets = get_request_offsets(request); |
| 141 |
manager = get_object_manager_client(chip_path); |
| 142 |
|
| 143 |
for (i = 0; i < offsets->len; i++) { |
| 144 |
g_autoptr(GpiodbusObject) line_obj = NULL; |
| 145 |
g_autofree char *obj_path = NULL; |
| 146 |
|
| 147 |
obj_path = g_strdup_printf("%s/line%u", |
| 148 |
chip_path, |
| 149 |
g_array_index( |
| 150 |
offsets, |
| 151 |
guint, i)); |
| 152 |
|
| 153 |
line_obj = GPIODBUS_OBJECT( |
| 154 |
g_dbus_object_manager_get_object( |
| 155 |
manager, |
| 156 |
obj_path)); |
| 157 |
if (!line_obj) |
| 158 |
die("Line not found: %u\n", |
| 159 |
g_array_index(offsets, guint, i)); |
| 160 |
|
| 161 |
line_objs = g_list_append(line_objs, |
| 162 |
g_object_ref(line_obj)); |
| 163 |
} |
| 164 |
} |
| 165 |
} else { |
| 166 |
num_lines = g_strv_length(lines); |
| 167 |
|
| 168 |
for (i = 0; i < num_lines; i++) { |
| 169 |
g_autoptr(GpiodbusObject) line_obj = NULL; |
| 170 |
|
| 171 |
ret = get_line_obj_by_name(lines[i], &line_obj, NULL); |
| 172 |
if (!ret) |
| 173 |
die("Line not found: %s\n", lines[i]); |
| 174 |
|
| 175 |
line_objs = g_list_append(line_objs, |
| 176 |
g_object_ref(line_obj)); |
| 177 |
} |
| 178 |
} |
| 179 |
|
| 180 |
g_list_foreach(line_objs, connect_edge_event, &data); |
| 181 |
|
| 182 |
loop = g_main_loop_new(NULL, FALSE); |
| 183 |
g_unix_signal_add(SIGTERM, quit_main_loop_on_signal, loop); |
| 184 |
g_unix_signal_add(SIGINT, quit_main_loop_on_signal, loop); |
| 185 |
|
| 186 |
g_main_loop_run(loop); |
| 187 |
|
| 188 |
g_list_free_full(data.lines, g_object_unref); |
| 189 |
g_bus_unwatch_name(watch_id); |
| 190 |
|
| 191 |
return EXIT_SUCCESS; |
| 192 |
} |