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