| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
#include "helpers.h" |
| 5 |
|
| 6 |
gint gpiodbus_id_cmp(gconstpointer a, gconstpointer b, |
| 7 |
gpointer user_data G_GNUC_UNUSED) |
| 8 |
{ |
| 9 |
const gint *id_a = a; |
| 10 |
const gint *id_b = b; |
| 11 |
|
| 12 |
if (*id_a < *id_b) |
| 13 |
return -1; |
| 14 |
else if (*id_a > *id_b) |
| 15 |
return 1; |
| 16 |
|
| 17 |
return 0; |
| 18 |
} |
| 19 |
|
| 20 |
static gboolean find_lowest(gpointer key, gpointer value G_GNUC_UNUSED, |
| 21 |
gpointer data) |
| 22 |
{ |
| 23 |
gint *lowest = data, *curr = key; |
| 24 |
|
| 25 |
if (*lowest == *curr) |
| 26 |
(*lowest)++; |
| 27 |
|
| 28 |
return FALSE; |
| 29 |
} |
| 30 |
|
| 31 |
gint gpiodbus_id_alloc(GTree *id_root) |
| 32 |
{ |
| 33 |
gint lowest = 0, *key; |
| 34 |
|
| 35 |
g_tree_foreach(id_root, find_lowest, &lowest); |
| 36 |
|
| 37 |
key = g_malloc(sizeof(*key)); |
| 38 |
*key = lowest; |
| 39 |
g_tree_insert(id_root, key, NULL); |
| 40 |
|
| 41 |
return lowest; |
| 42 |
} |
| 43 |
|
| 44 |
void gpiodbus_id_free(GTree *id_root, gint id) |
| 45 |
{ |
| 46 |
gboolean removed; |
| 47 |
|
| 48 |
removed = g_tree_remove(id_root, &id); |
| 49 |
g_assert(removed); |
| 50 |
} |
| 51 |
|
| 52 |
gboolean |
| 53 |
gpiodbus_chip_set_props(GpiodbusChip *skeleton, GpiodglibChip *chip, |
| 54 |
GError **err) |
| 55 |
{ |
| 56 |
g_autoptr(GpiodglibChipInfo) info = NULL; |
| 57 |
g_autofree gchar *label = NULL; |
| 58 |
g_autofree gchar *path = NULL; |
| 59 |
g_autofree gchar *name = NULL; |
| 60 |
|
| 61 |
info = gpiodglib_chip_get_info(chip, err); |
| 62 |
if (!info) |
| 63 |
return FALSE; |
| 64 |
|
| 65 |
name = gpiodglib_chip_info_dup_name(info); |
| 66 |
label = gpiodglib_chip_info_dup_label(info); |
| 67 |
|
| 68 |
gpiodbus_chip_set_name(skeleton, name); |
| 69 |
gpiodbus_chip_set_label(skeleton, label); |
| 70 |
gpiodbus_chip_set_num_lines(skeleton, |
| 71 |
gpiodglib_chip_info_get_num_lines(info)); |
| 72 |
path = gpiodglib_chip_dup_path(chip); |
| 73 |
gpiodbus_chip_set_path(skeleton, path); |
| 74 |
g_dbus_interface_skeleton_flush(G_DBUS_INTERFACE_SKELETON(skeleton)); |
| 75 |
|
| 76 |
return TRUE; |
| 77 |
} |
| 78 |
|
| 79 |
static const gchar *map_direction(GpiodglibLineDirection direction) |
| 80 |
{ |
| 81 |
switch (direction) { |
| 82 |
case GPIODGLIB_LINE_DIRECTION_INPUT: |
| 83 |
return "input"; |
| 84 |
case GPIODGLIB_LINE_DIRECTION_OUTPUT: |
| 85 |
return "output"; |
| 86 |
default: |
| 87 |
g_error("invalid direction value returned by libgpiod-glib"); |
| 88 |
} |
| 89 |
} |
| 90 |
|
| 91 |
static const gchar *map_edge(GpiodglibLineEdge edge) |
| 92 |
{ |
| 93 |
switch (edge) { |
| 94 |
case GPIODGLIB_LINE_EDGE_NONE: |
| 95 |
return "none"; |
| 96 |
case GPIODGLIB_LINE_EDGE_FALLING: |
| 97 |
return "falling"; |
| 98 |
case GPIODGLIB_LINE_EDGE_RISING: |
| 99 |
return "rising"; |
| 100 |
case GPIODGLIB_LINE_EDGE_BOTH: |
| 101 |
return "both"; |
| 102 |
default: |
| 103 |
g_error("invalid edge value returned by libgpiod-glib"); |
| 104 |
} |
| 105 |
} |
| 106 |
|
| 107 |
static const gchar *map_bias(GpiodglibLineBias bias) |
| 108 |
{ |
| 109 |
switch (bias) { |
| 110 |
case GPIODGLIB_LINE_BIAS_UNKNOWN: |
| 111 |
return "unknown"; |
| 112 |
case GPIODGLIB_LINE_BIAS_DISABLED: |
| 113 |
return "disabled"; |
| 114 |
case GPIODGLIB_LINE_BIAS_PULL_UP: |
| 115 |
return "pull-up"; |
| 116 |
case GPIODGLIB_LINE_BIAS_PULL_DOWN: |
| 117 |
return "pull-down"; |
| 118 |
default: |
| 119 |
g_error("invalid bias value returned by libgpiod-glib"); |
| 120 |
} |
| 121 |
} |
| 122 |
|
| 123 |
static const gchar *map_drive(GpiodglibLineDrive drive) |
| 124 |
{ |
| 125 |
switch (drive) { |
| 126 |
case GPIODGLIB_LINE_DRIVE_PUSH_PULL: |
| 127 |
return "push-pull"; |
| 128 |
case GPIODGLIB_LINE_DRIVE_OPEN_DRAIN: |
| 129 |
return "open-drain"; |
| 130 |
case GPIODGLIB_LINE_DRIVE_OPEN_SOURCE: |
| 131 |
return "open-source"; |
| 132 |
default: |
| 133 |
g_error("invalid drive value returned by libgpiod-glib"); |
| 134 |
} |
| 135 |
} |
| 136 |
|
| 137 |
static const gchar *map_clock(GpiodglibLineClock event_clock) |
| 138 |
{ |
| 139 |
switch (event_clock) { |
| 140 |
case GPIODGLIB_LINE_CLOCK_MONOTONIC: |
| 141 |
return "monotonic"; |
| 142 |
case GPIODGLIB_LINE_CLOCK_REALTIME: |
| 143 |
return "realtime"; |
| 144 |
case GPIODGLIB_LINE_CLOCK_HTE: |
| 145 |
return "hte"; |
| 146 |
default: |
| 147 |
g_error("invalid event clock value returned by libgpiod-glib"); |
| 148 |
} |
| 149 |
} |
| 150 |
|
| 151 |
void gpiodbus_line_set_props(GpiodbusLine *skeleton, GpiodglibLineInfo *info) |
| 152 |
{ |
| 153 |
g_autofree gchar *consumer = gpiodglib_line_info_dup_consumer(info); |
| 154 |
g_autofree gchar *name = gpiodglib_line_info_dup_name(info); |
| 155 |
|
| 156 |
gpiodbus_line_set_offset(skeleton, |
| 157 |
gpiodglib_line_info_get_offset(info)); |
| 158 |
gpiodbus_line_set_name(skeleton, name); |
| 159 |
gpiodbus_line_set_used(skeleton, gpiodglib_line_info_is_used(info)); |
| 160 |
gpiodbus_line_set_consumer(skeleton, consumer); |
| 161 |
gpiodbus_line_set_direction(skeleton, |
| 162 |
map_direction(gpiodglib_line_info_get_direction(info))); |
| 163 |
gpiodbus_line_set_edge_detection(skeleton, |
| 164 |
map_edge(gpiodglib_line_info_get_edge_detection(info))); |
| 165 |
gpiodbus_line_set_bias(skeleton, |
| 166 |
map_bias(gpiodglib_line_info_get_bias(info))); |
| 167 |
gpiodbus_line_set_drive(skeleton, |
| 168 |
map_drive(gpiodglib_line_info_get_drive(info))); |
| 169 |
gpiodbus_line_set_active_low(skeleton, |
| 170 |
gpiodglib_line_info_is_active_low(info)); |
| 171 |
gpiodbus_line_set_debounced(skeleton, |
| 172 |
gpiodglib_line_info_is_debounced(info)); |
| 173 |
gpiodbus_line_set_debounce_period_us(skeleton, |
| 174 |
gpiodglib_line_info_get_debounce_period_us(info)); |
| 175 |
gpiodbus_line_set_event_clock(skeleton, |
| 176 |
map_clock(gpiodglib_line_info_get_event_clock(info))); |
| 177 |
g_dbus_interface_skeleton_flush(G_DBUS_INTERFACE_SKELETON(skeleton)); |
| 178 |
} |
| 179 |
|
| 180 |
static gint line_offset_cmp(gconstpointer a, gconstpointer b) |
| 181 |
{ |
| 182 |
GpiodbusObject *line_obj = (GpiodbusObject *)a; |
| 183 |
GpiodbusLine *line; |
| 184 |
const guint *offset = b; |
| 185 |
|
| 186 |
line = gpiodbus_object_peek_line(line_obj); |
| 187 |
|
| 188 |
return gpiodbus_line_get_offset(line) != *offset; |
| 189 |
} |
| 190 |
|
| 191 |
void gpiodbus_request_set_props(GpiodbusRequest *skeleton, |
| 192 |
GpiodglibLineRequest *request, GpiodbusChip *chip, |
| 193 |
GDBusObjectManager *line_manager) |
| 194 |
{ |
| 195 |
g_autolist(GpiodbusObject) line_objs = NULL; |
| 196 |
g_autoptr(GStrvBuilder) builder = NULL; |
| 197 |
g_autoptr(GArray) offsets = NULL; |
| 198 |
g_auto(GStrv) paths = NULL; |
| 199 |
GList *found; |
| 200 |
guint i; |
| 201 |
|
| 202 |
offsets = gpiodglib_line_request_get_requested_offsets(request); |
| 203 |
line_objs = g_dbus_object_manager_get_objects(line_manager); |
| 204 |
builder = g_strv_builder_new(); |
| 205 |
|
| 206 |
for (i = 0; i < offsets->len; i++) { |
| 207 |
found = g_list_find_custom(line_objs, |
| 208 |
&g_array_index(offsets, guint, i), |
| 209 |
line_offset_cmp); |
| 210 |
if (found) |
| 211 |
g_strv_builder_add(builder, |
| 212 |
g_dbus_object_get_object_path( |
| 213 |
G_DBUS_OBJECT(found->data))); |
| 214 |
} |
| 215 |
|
| 216 |
paths = g_strv_builder_end(builder); |
| 217 |
|
| 218 |
gpiodbus_request_set_chip_path(skeleton, |
| 219 |
g_dbus_interface_skeleton_get_object_path( |
| 220 |
G_DBUS_INTERFACE_SKELETON(chip))); |
| 221 |
gpiodbus_request_set_line_paths(skeleton, (const gchar *const *)paths); |
| 222 |
g_dbus_interface_skeleton_flush(G_DBUS_INTERFACE_SKELETON(skeleton)); |
| 223 |
} |
| 224 |
|
| 225 |
static gboolean |
| 226 |
set_settings_from_variant(GpiodglibLineSettings *settings, const gchar *key, |
| 227 |
GVariant *val) |
| 228 |
{ |
| 229 |
GpiodglibLineDirection direction; |
| 230 |
GpiodglibLineClock event_clock; |
| 231 |
GpiodglibLineDrive drive; |
| 232 |
GpiodglibLineEdge edge; |
| 233 |
GpiodglibLineBias bias; |
| 234 |
const gchar *str; |
| 235 |
|
| 236 |
|
| 237 |
if (g_strcmp0(key, "direction") == 0) { |
| 238 |
str = g_variant_get_string(val, NULL); |
| 239 |
|
| 240 |
if (g_strcmp0(str, "input") == 0) { |
| 241 |
direction = GPIODGLIB_LINE_DIRECTION_INPUT; |
| 242 |
} else if (g_strcmp0(str, "output") == 0) { |
| 243 |
direction = GPIODGLIB_LINE_DIRECTION_OUTPUT; |
| 244 |
} else if (g_strcmp0(str, "as-is") == 0) { |
| 245 |
direction = GPIODGLIB_LINE_DIRECTION_AS_IS; |
| 246 |
} else { |
| 247 |
g_critical("invalid direction value received: '%s'", |
| 248 |
str); |
| 249 |
return FALSE; |
| 250 |
} |
| 251 |
|
| 252 |
gpiodglib_line_settings_set_direction(settings, direction); |
| 253 |
} else if (g_strcmp0(key, "edge") == 0) { |
| 254 |
str = g_variant_get_string(val, NULL); |
| 255 |
|
| 256 |
if (g_strcmp0(str, "falling") == 0) { |
| 257 |
edge = GPIODGLIB_LINE_EDGE_FALLING; |
| 258 |
} else if (g_strcmp0(str, "rising") == 0) { |
| 259 |
edge = GPIODGLIB_LINE_EDGE_RISING; |
| 260 |
} else if (g_strcmp0(str, "both") == 0) { |
| 261 |
edge = GPIODGLIB_LINE_EDGE_BOTH; |
| 262 |
} else { |
| 263 |
g_critical("invalid edge value received: '%s'", str); |
| 264 |
return FALSE; |
| 265 |
} |
| 266 |
|
| 267 |
gpiodglib_line_settings_set_edge_detection(settings, edge); |
| 268 |
} else if (g_strcmp0(key, "active-low") == 0) { |
| 269 |
if (g_variant_get_boolean(val)) |
| 270 |
gpiodglib_line_settings_set_active_low(settings, TRUE); |
| 271 |
} else if (g_strcmp0(key, "bias") == 0) { |
| 272 |
str = g_variant_get_string(val, NULL); |
| 273 |
|
| 274 |
if (g_strcmp0(str, "as-is") == 0) { |
| 275 |
bias = GPIODGLIB_LINE_BIAS_AS_IS; |
| 276 |
} else if (g_strcmp0(str, "pull-up") == 0) { |
| 277 |
bias = GPIODGLIB_LINE_BIAS_PULL_UP; |
| 278 |
} else if (g_strcmp0(str, "pull-down") == 0) { |
| 279 |
bias = GPIODGLIB_LINE_BIAS_PULL_DOWN; |
| 280 |
} else if (g_strcmp0(str, "disabled") == 0) { |
| 281 |
bias = GPIODGLIB_LINE_BIAS_DISABLED; |
| 282 |
} else { |
| 283 |
g_critical("invalid bias value received: '%s'", str); |
| 284 |
return FALSE; |
| 285 |
} |
| 286 |
|
| 287 |
gpiodglib_line_settings_set_bias(settings, bias); |
| 288 |
} else if (g_strcmp0(key, "drive") == 0) { |
| 289 |
str = g_variant_get_string(val, NULL); |
| 290 |
|
| 291 |
if (g_strcmp0(str, "push-pull") == 0) { |
| 292 |
drive = GPIODGLIB_LINE_DRIVE_PUSH_PULL; |
| 293 |
} else if (g_strcmp0(str, "open-drain") == 0) { |
| 294 |
drive = GPIODGLIB_LINE_DRIVE_OPEN_DRAIN; |
| 295 |
} else if (g_strcmp0(str, "open-source") == 0) { |
| 296 |
drive = GPIODGLIB_LINE_DRIVE_OPEN_SOURCE; |
| 297 |
} else { |
| 298 |
g_critical("invalid drive value received: '%s'", str); |
| 299 |
return FALSE; |
| 300 |
} |
| 301 |
|
| 302 |
gpiodglib_line_settings_set_drive(settings, drive); |
| 303 |
} else if (g_strcmp0(key, "debounce-period") == 0) { |
| 304 |
gpiodglib_line_settings_set_debounce_period_us(settings, |
| 305 |
g_variant_get_int64(val)); |
| 306 |
} else if (g_strcmp0(key, "event-clock") == 0) { |
| 307 |
str = g_variant_get_string(val, NULL); |
| 308 |
|
| 309 |
if (g_strcmp0(str, "monotonic") == 0) { |
| 310 |
event_clock = GPIODGLIB_LINE_CLOCK_MONOTONIC; |
| 311 |
} else if (g_strcmp0(str, "realtime") == 0) { |
| 312 |
event_clock = GPIODGLIB_LINE_CLOCK_REALTIME; |
| 313 |
} else if (g_strcmp0(str, "hte") == 0) { |
| 314 |
event_clock = GPIODGLIB_LINE_CLOCK_HTE; |
| 315 |
} else { |
| 316 |
g_critical("invalid event clock value received: '%s'", |
| 317 |
str); |
| 318 |
return FALSE; |
| 319 |
} |
| 320 |
|
| 321 |
gpiodglib_line_settings_set_event_clock(settings, event_clock); |
| 322 |
} else { |
| 323 |
g_critical("invalid config option received: '%s'", key); |
| 324 |
return FALSE; |
| 325 |
} |
| 326 |
|
| 327 |
return TRUE; |
| 328 |
} |
| 329 |
|
| 330 |
GpiodglibLineConfig *gpiodbus_line_config_from_variant(GVariant *variant) |
| 331 |
{ |
| 332 |
g_autoptr(GpiodglibLineSettings) settings = NULL; |
| 333 |
g_autoptr(GpiodglibLineConfig) config = NULL; |
| 334 |
g_autoptr(GVariant) output_values_v = NULL; |
| 335 |
g_autoptr(GVariant) line_configs_v = NULL; |
| 336 |
g_autoptr(GArray) values = NULL; |
| 337 |
g_autoptr(GError) err = NULL; |
| 338 |
GVariantIter iter0, iter1; |
| 339 |
guint offset; |
| 340 |
gboolean ret; |
| 341 |
GVariant *v; |
| 342 |
gchar *k; |
| 343 |
gint val; |
| 344 |
|
| 345 |
line_configs_v = g_variant_get_child_value(variant, 0); |
| 346 |
output_values_v = g_variant_get_child_value(variant, 1); |
| 347 |
|
| 348 |
config = gpiodglib_line_config_new(); |
| 349 |
settings = gpiodglib_line_settings_new(NULL); |
| 350 |
|
| 351 |
g_variant_iter_init(&iter0, line_configs_v); |
| 352 |
while ((v = g_variant_iter_next_value(&iter0))) { |
| 353 |
g_autoptr(GVariant) line_settings_v = NULL; |
| 354 |
g_autoptr(GVariant) line_config_v = v; |
| 355 |
g_autoptr(GVariant) offsets_v = NULL; |
| 356 |
g_autoptr(GArray) offsets = NULL; |
| 357 |
|
| 358 |
offsets_v = g_variant_get_child_value(line_config_v, 0); |
| 359 |
line_settings_v = g_variant_get_child_value(line_config_v, 1); |
| 360 |
|
| 361 |
gpiodglib_line_settings_reset(settings); |
| 362 |
g_variant_iter_init(&iter1, line_settings_v); |
| 363 |
while (g_variant_iter_next(&iter1, "{sv}", &k, &v)) { |
| 364 |
g_autoptr(GVariant) val = v; |
| 365 |
g_autofree gchar *key = k; |
| 366 |
|
| 367 |
ret = set_settings_from_variant(settings, key, val); |
| 368 |
if (!ret) |
| 369 |
return NULL; |
| 370 |
} |
| 371 |
|
| 372 |
offsets = g_array_sized_new(FALSE, TRUE, sizeof(guint), |
| 373 |
g_variant_n_children(offsets_v)); |
| 374 |
g_variant_iter_init(&iter1, offsets_v); |
| 375 |
while (g_variant_iter_next(&iter1, "u", &offset)) |
| 376 |
g_array_append_val(offsets, offset); |
| 377 |
|
| 378 |
ret = gpiodglib_line_config_add_line_settings(config, offsets, |
| 379 |
settings, &err); |
| 380 |
if (!ret) { |
| 381 |
g_critical("failed to add line settings: %s", |
| 382 |
err->message); |
| 383 |
return NULL; |
| 384 |
} |
| 385 |
} |
| 386 |
|
| 387 |
values = g_array_sized_new(FALSE, TRUE, sizeof(gint), |
| 388 |
g_variant_n_children(output_values_v)); |
| 389 |
g_variant_iter_init(&iter0, output_values_v); |
| 390 |
while (g_variant_iter_next(&iter0, "i", &val)) |
| 391 |
g_array_append_val(values, val); |
| 392 |
|
| 393 |
if (values->len > 0) { |
| 394 |
ret = gpiodglib_line_config_set_output_values(config, values, |
| 395 |
&err); |
| 396 |
if (!ret) { |
| 397 |
g_critical("failed to set output values: %s", |
| 398 |
err->message); |
| 399 |
return NULL; |
| 400 |
} |
| 401 |
} |
| 402 |
|
| 403 |
return g_object_ref(config); |
| 404 |
} |
| 405 |
|
| 406 |
GpiodglibRequestConfig *gpiodbus_request_config_from_variant(GVariant *variant) |
| 407 |
{ |
| 408 |
g_autoptr(GpiodglibRequestConfig) config = NULL; |
| 409 |
GVariantIter iter; |
| 410 |
GVariant *v; |
| 411 |
gchar *k; |
| 412 |
|
| 413 |
config = gpiodglib_request_config_new(NULL); |
| 414 |
|
| 415 |
g_variant_iter_init(&iter, variant); |
| 416 |
while (g_variant_iter_next(&iter, "{sv}", &k, &v)) { |
| 417 |
g_autoptr(GVariant) val = v; |
| 418 |
g_autofree gchar *key = k; |
| 419 |
|
| 420 |
if (g_strcmp0(key, "consumer") == 0) { |
| 421 |
gpiodglib_request_config_set_consumer(config, |
| 422 |
g_variant_get_string(val, NULL)); |
| 423 |
} else if (g_strcmp0(key, "event-buffer-size") == 0) { |
| 424 |
gpiodglib_request_config_set_event_buffer_size(config, |
| 425 |
g_variant_get_uint32(val)); |
| 426 |
} else { |
| 427 |
g_critical("invalid request config option received: '%s'", |
| 428 |
key); |
| 429 |
return NULL; |
| 430 |
} |
| 431 |
} |
| 432 |
|
| 433 |
return g_object_ref(config); |
| 434 |
} |