| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
#include <stdlib.h> |
| 5 |
|
| 6 |
#include "common.h" |
| 7 |
|
| 8 |
typedef struct { |
| 9 |
gboolean name_done; |
| 10 |
gboolean chip_done; |
| 11 |
const gchar *label; |
| 12 |
} WaitData; |
| 13 |
|
| 14 |
static void obj_match_label(GpiodbusObject *chip_obj, WaitData *data) |
| 15 |
{ |
| 16 |
GpiodbusChip *chip = gpiodbus_object_peek_chip(chip_obj); |
| 17 |
|
| 18 |
if (g_strcmp0(gpiodbus_chip_get_label(chip), data->label) == 0) |
| 19 |
data->chip_done = TRUE; |
| 20 |
} |
| 21 |
|
| 22 |
static void check_label(gpointer elem, gpointer user_data) |
| 23 |
{ |
| 24 |
WaitData *data = user_data; |
| 25 |
GpiodbusObject *obj = elem; |
| 26 |
|
| 27 |
obj_match_label(obj, data); |
| 28 |
} |
| 29 |
|
| 30 |
static void on_object_added(GDBusObjectManager *manager G_GNUC_UNUSED, |
| 31 |
GpiodbusObject *obj, gpointer user_data) |
| 32 |
{ |
| 33 |
WaitData *data = user_data; |
| 34 |
|
| 35 |
obj_match_label(GPIODBUS_OBJECT(obj), data); |
| 36 |
} |
| 37 |
|
| 38 |
static void wait_for_chip(WaitData *data) |
| 39 |
{ |
| 40 |
g_autoptr(GDBusObjectManager) manager = NULL; |
| 41 |
g_autolist(GpiodbusObject) objs = NULL; |
| 42 |
|
| 43 |
manager = get_object_manager_client("/io/gpiod1/chips"); |
| 44 |
|
| 45 |
g_signal_connect(manager, "object-added", |
| 46 |
G_CALLBACK(on_object_added), data); |
| 47 |
|
| 48 |
objs = g_dbus_object_manager_get_objects(manager); |
| 49 |
g_list_foreach(objs, check_label, data); |
| 50 |
|
| 51 |
while (!data->chip_done) |
| 52 |
g_main_context_iteration(NULL, TRUE); |
| 53 |
} |
| 54 |
|
| 55 |
static void on_name_appeared(GDBusConnection *con G_GNUC_UNUSED, |
| 56 |
const gchar *name G_GNUC_UNUSED, |
| 57 |
const gchar *name_owner G_GNUC_UNUSED, |
| 58 |
gpointer user_data) |
| 59 |
{ |
| 60 |
WaitData *data = user_data; |
| 61 |
|
| 62 |
data->name_done = TRUE; |
| 63 |
} |
| 64 |
|
| 65 |
static void on_name_vanished(GDBusConnection *con G_GNUC_UNUSED, |
| 66 |
const gchar *name G_GNUC_UNUSED, |
| 67 |
gpointer user_data) |
| 68 |
{ |
| 69 |
WaitData *data = user_data; |
| 70 |
|
| 71 |
if (data->label && data->chip_done) |
| 72 |
die("gpio-manager vanished while waiting for chip"); |
| 73 |
} |
| 74 |
|
| 75 |
static gboolean on_timeout(gpointer user_data G_GNUC_UNUSED) |
| 76 |
{ |
| 77 |
die("wait timed out!"); |
| 78 |
} |
| 79 |
|
| 80 |
static guint schedule_timeout(const gchar *timeout) |
| 81 |
{ |
| 82 |
gint64 period, multiplier = 0; |
| 83 |
gchar *end; |
| 84 |
|
| 85 |
period = g_ascii_strtoll(timeout, &end, 10); |
| 86 |
|
| 87 |
switch (*end) { |
| 88 |
case 'm': |
| 89 |
multiplier = 1; |
| 90 |
end++; |
| 91 |
break; |
| 92 |
case 's': |
| 93 |
multiplier = 1000; |
| 94 |
break; |
| 95 |
case '\0': |
| 96 |
break; |
| 97 |
default: |
| 98 |
goto invalid_timeout; |
| 99 |
} |
| 100 |
|
| 101 |
if (multiplier) { |
| 102 |
if (*end != 's') |
| 103 |
goto invalid_timeout; |
| 104 |
|
| 105 |
end++; |
| 106 |
} else { |
| 107 |
|
| 108 |
multiplier = 1; |
| 109 |
} |
| 110 |
|
| 111 |
period *= multiplier; |
| 112 |
if (period > G_MAXUINT) |
| 113 |
die("timeout must not exceed %u miliseconds\n", G_MAXUINT); |
| 114 |
|
| 115 |
return g_timeout_add(period, on_timeout, NULL); |
| 116 |
|
| 117 |
invalid_timeout: |
| 118 |
die("invalid timeout value: %s", timeout); |
| 119 |
} |
| 120 |
|
| 121 |
int gpiocli_wait_main(int argc, char **argv) |
| 122 |
{ |
| 123 |
static const gchar *const summary = |
| 124 |
"Wait for the gpio-manager interface to appear."; |
| 125 |
|
| 126 |
static const gchar *const description = |
| 127 |
"Timeout period defaults to miliseconds but can be given in seconds or miliseconds\n" |
| 128 |
"explicitly .e.g: --timeout=1000, --timeout=1000ms and --timeout=1s all specify\n" |
| 129 |
"the same period."; |
| 130 |
|
| 131 |
const gchar *timeout_str = NULL; |
| 132 |
guint watch_id, timeout_id = 0; |
| 133 |
g_auto(GStrv) remaining = NULL; |
| 134 |
WaitData data = {}; |
| 135 |
|
| 136 |
const GOptionEntry opts[] = { |
| 137 |
{ |
| 138 |
.long_name = "chip", |
| 139 |
.short_name = 'c', |
| 140 |
.flags = G_OPTION_FLAG_NONE, |
| 141 |
.arg = G_OPTION_ARG_STRING, |
| 142 |
.arg_data = &data.label, |
| 143 |
.description = "Wait for a specific chip to appear.", |
| 144 |
.arg_description = "<label>", |
| 145 |
}, |
| 146 |
{ |
| 147 |
.long_name = "timeout", |
| 148 |
.short_name = 't', |
| 149 |
.flags = G_OPTION_FLAG_NONE, |
| 150 |
.arg = G_OPTION_ARG_STRING, |
| 151 |
.arg_data = &timeout_str, |
| 152 |
.description = "Bail-out if timeout expires.", |
| 153 |
.arg_description = "<timeout_str>", |
| 154 |
}, |
| 155 |
{ |
| 156 |
.long_name = G_OPTION_REMAINING, |
| 157 |
.flags = G_OPTION_FLAG_NONE, |
| 158 |
.arg = G_OPTION_ARG_STRING_ARRAY, |
| 159 |
.arg_data = &remaining, |
| 160 |
}, |
| 161 |
{ } |
| 162 |
}; |
| 163 |
|
| 164 |
parse_options(opts, summary, description, &argc, &argv); |
| 165 |
|
| 166 |
if (remaining) |
| 167 |
die_parsing_opts("command doesn't take additional arguments"); |
| 168 |
|
| 169 |
watch_id = g_bus_watch_name(G_BUS_TYPE_SYSTEM, "io.gpiod1", |
| 170 |
G_BUS_NAME_WATCHER_FLAGS_NONE, |
| 171 |
on_name_appeared, on_name_vanished, |
| 172 |
&data, NULL); |
| 173 |
|
| 174 |
if (timeout_str) |
| 175 |
timeout_id = schedule_timeout(timeout_str); |
| 176 |
|
| 177 |
while (!data.name_done) |
| 178 |
g_main_context_iteration(NULL, TRUE); |
| 179 |
|
| 180 |
if (data.label) |
| 181 |
wait_for_chip(&data); |
| 182 |
|
| 183 |
g_bus_unwatch_name(watch_id); |
| 184 |
if (timeout_str) |
| 185 |
g_source_remove(timeout_id); |
| 186 |
|
| 187 |
return EXIT_SUCCESS; |
| 188 |
} |