File: helpers.c

package info (click to toggle)
libgpiod 2.2.1-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 6,108 kB
  • sloc: ansic: 26,612; sh: 7,554; cpp: 4,944; python: 2,426; makefile: 811; xml: 49
file content (431 lines) | stat: -rw-r--r-- 12,322 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
// SPDX-License-Identifier: GPL-2.0-or-later
// SPDX-FileCopyrightText: 2023-2024 Bartosz Golaszewski <bartosz.golaszewski@linaro.org>

#include "helpers.h"

gint gpiodbus_id_cmp(gconstpointer a, gconstpointer b,
		     gpointer user_data G_GNUC_UNUSED)
{
	const gint *id_a = a;
	const gint *id_b = b;

	if (*id_a < *id_b)
		return -1;
	else if (*id_a > *id_b)
		return 1;

	return 0;
}

static gboolean find_lowest(gpointer key, gpointer value G_GNUC_UNUSED,
			    gpointer data)
{
	gint *lowest = data, *curr = key;

	if (*lowest == *curr)
		(*lowest)++;

	return FALSE;
}

gint gpiodbus_id_alloc(GTree *id_root)
{
	gint lowest = 0, *key;

	g_tree_foreach(id_root, find_lowest, &lowest);

	key = g_malloc(sizeof(*key));
	*key = lowest;
	g_tree_insert(id_root, key, NULL);

	return lowest;
}

void gpiodbus_id_free(GTree *id_root, gint id)
{
	g_assert(g_tree_remove(id_root, &id));
}

gboolean
gpiodbus_chip_set_props(GpiodbusChip *skeleton, GpiodglibChip *chip,
			GError **err)
{
	g_autoptr(GpiodglibChipInfo) info = NULL;
	g_autofree gchar *label = NULL;
	g_autofree gchar *path = NULL;
	g_autofree gchar *name = NULL;

	info = gpiodglib_chip_get_info(chip, err);
	if (!info)
		return FALSE;

	name = gpiodglib_chip_info_dup_name(info);
	label = gpiodglib_chip_info_dup_label(info);

	gpiodbus_chip_set_name(skeleton, name);
	gpiodbus_chip_set_label(skeleton, label);
	gpiodbus_chip_set_num_lines(skeleton,
				    gpiodglib_chip_info_get_num_lines(info));
	path = gpiodglib_chip_dup_path(chip);
	gpiodbus_chip_set_path(skeleton, path);
	g_dbus_interface_skeleton_flush(G_DBUS_INTERFACE_SKELETON(skeleton));

	return TRUE;
}

static const gchar *map_direction(GpiodglibLineDirection direction)
{
	switch (direction) {
	case GPIODGLIB_LINE_DIRECTION_INPUT:
		return "input";
	case GPIODGLIB_LINE_DIRECTION_OUTPUT:
		return "output";
	default:
		g_error("invalid direction value returned by libgpiod-glib");
	}
}

static const gchar *map_edge(GpiodglibLineEdge edge)
{
	switch (edge) {
	case GPIODGLIB_LINE_EDGE_NONE:
		return "none";
	case GPIODGLIB_LINE_EDGE_FALLING:
		return "falling";
	case GPIODGLIB_LINE_EDGE_RISING:
		return "rising";
	case GPIODGLIB_LINE_EDGE_BOTH:
		return "both";
	default:
		g_error("invalid edge value returned by libgpiod-glib");
	}
}

static const gchar *map_bias(GpiodglibLineBias bias)
{
	switch (bias) {
	case GPIODGLIB_LINE_BIAS_UNKNOWN:
		return "unknown";
	case GPIODGLIB_LINE_BIAS_DISABLED:
		return "disabled";
	case GPIODGLIB_LINE_BIAS_PULL_UP:
		return "pull-up";
	case GPIODGLIB_LINE_BIAS_PULL_DOWN:
		return "pull-down";
	default:
		g_error("invalid bias value returned by libgpiod-glib");
	}
}

static const gchar *map_drive(GpiodglibLineDrive drive)
{
	switch (drive) {
	case GPIODGLIB_LINE_DRIVE_PUSH_PULL:
		return "push-pull";
	case GPIODGLIB_LINE_DRIVE_OPEN_DRAIN:
		return "open-drain";
	case GPIODGLIB_LINE_DRIVE_OPEN_SOURCE:
		return "open-source";
	default:
		g_error("invalid drive value returned by libgpiod-glib");
	}
}

static const gchar *map_clock(GpiodglibLineClock event_clock)
{
	switch (event_clock) {
	case GPIODGLIB_LINE_CLOCK_MONOTONIC:
		return "monotonic";
	case GPIODGLIB_LINE_CLOCK_REALTIME:
		return "realtime";
	case GPIODGLIB_LINE_CLOCK_HTE:
		return "hte";
	default:
		g_error("invalid event clock value returned by libgpiod-glib");
	}
}

void gpiodbus_line_set_props(GpiodbusLine *skeleton, GpiodglibLineInfo *info)
{
	g_autofree gchar *consumer = gpiodglib_line_info_dup_consumer(info);
	g_autofree gchar *name = gpiodglib_line_info_dup_name(info);

	gpiodbus_line_set_offset(skeleton,
				 gpiodglib_line_info_get_offset(info));
	gpiodbus_line_set_name(skeleton, name);
	gpiodbus_line_set_used(skeleton, gpiodglib_line_info_is_used(info));
	gpiodbus_line_set_consumer(skeleton, consumer);
	gpiodbus_line_set_direction(skeleton,
			map_direction(gpiodglib_line_info_get_direction(info)));
	gpiodbus_line_set_edge_detection(skeleton,
			map_edge(gpiodglib_line_info_get_edge_detection(info)));
	gpiodbus_line_set_bias(skeleton,
			       map_bias(gpiodglib_line_info_get_bias(info)));
	gpiodbus_line_set_drive(skeleton,
				map_drive(gpiodglib_line_info_get_drive(info)));
	gpiodbus_line_set_active_low(skeleton,
				     gpiodglib_line_info_is_active_low(info));
	gpiodbus_line_set_debounced(skeleton,
				    gpiodglib_line_info_is_debounced(info));
	gpiodbus_line_set_debounce_period_us(skeleton,
			gpiodglib_line_info_get_debounce_period_us(info));
	gpiodbus_line_set_event_clock(skeleton,
			map_clock(gpiodglib_line_info_get_event_clock(info)));
	g_dbus_interface_skeleton_flush(G_DBUS_INTERFACE_SKELETON(skeleton));
}

static gint line_offset_cmp(gconstpointer a, gconstpointer b)
{
	GpiodbusObject *line_obj = (GpiodbusObject *)a;
	GpiodbusLine *line;
	const guint *offset = b;

	line = gpiodbus_object_peek_line(line_obj);

	return gpiodbus_line_get_offset(line) != *offset;
}

void gpiodbus_request_set_props(GpiodbusRequest *skeleton,
				GpiodglibLineRequest *request, GpiodbusChip *chip,
				GDBusObjectManager *line_manager)
{
	g_autolist(GpiodbusObject) line_objs = NULL;
	g_autoptr(GStrvBuilder) builder = NULL;
	g_autoptr(GArray) offsets = NULL;
	g_auto(GStrv) paths = NULL;
	GList *found;
	guint i;

	offsets = gpiodglib_line_request_get_requested_offsets(request);
	line_objs = g_dbus_object_manager_get_objects(line_manager);
	builder = g_strv_builder_new();

	for (i = 0; i < offsets->len; i++) {
		found = g_list_find_custom(line_objs,
					   &g_array_index(offsets, guint, i),
					   line_offset_cmp);
		if (found)
			g_strv_builder_add(builder,
					   g_dbus_object_get_object_path(
						G_DBUS_OBJECT(found->data)));
	}

	paths = g_strv_builder_end(builder);

	gpiodbus_request_set_chip_path(skeleton,
			g_dbus_interface_skeleton_get_object_path(
					G_DBUS_INTERFACE_SKELETON(chip)));
	gpiodbus_request_set_line_paths(skeleton, (const gchar *const *)paths);
	g_dbus_interface_skeleton_flush(G_DBUS_INTERFACE_SKELETON(skeleton));
}

static gboolean
set_settings_from_variant(GpiodglibLineSettings *settings, const gchar *key,
			  GVariant *val)
{
	GpiodglibLineDirection direction;
	GpiodglibLineClock event_clock;
	GpiodglibLineDrive drive;
	GpiodglibLineEdge edge;
	GpiodglibLineBias bias;
	const gchar *str;

	/* FIXME: Make it into a nice set of hashmaps and callbacks. */
	if (g_strcmp0(key, "direction") == 0) {
		str = g_variant_get_string(val, NULL);

		if (g_strcmp0(str, "input") == 0) {
			direction = GPIODGLIB_LINE_DIRECTION_INPUT;
		} else if (g_strcmp0(str, "output") == 0) {
			direction = GPIODGLIB_LINE_DIRECTION_OUTPUT;
		} else if (g_strcmp0(str, "as-is") == 0) {
			direction = GPIODGLIB_LINE_DIRECTION_AS_IS;
		} else {
			g_critical("invalid direction value received: '%s'",
				   str);
			return FALSE;
		}

		gpiodglib_line_settings_set_direction(settings, direction);
	} else if (g_strcmp0(key, "edge") == 0) {
		str = g_variant_get_string(val, NULL);

		if (g_strcmp0(str, "falling") == 0) {
			edge = GPIODGLIB_LINE_EDGE_FALLING;
		} else if (g_strcmp0(str, "rising") == 0) {
			edge = GPIODGLIB_LINE_EDGE_RISING;
		} else if (g_strcmp0(str, "both") == 0) {
			edge = GPIODGLIB_LINE_EDGE_BOTH;
		} else {
			g_critical("invalid edge value received: '%s'", str);
			return FALSE;
		}

		gpiodglib_line_settings_set_edge_detection(settings, edge);
	} else if (g_strcmp0(key, "active-low") == 0) {
		if (g_variant_get_boolean(val))
			gpiodglib_line_settings_set_active_low(settings, TRUE);
	} else if (g_strcmp0(key, "bias") == 0) {
		str = g_variant_get_string(val, NULL);

		if (g_strcmp0(str, "as-is") == 0) {
			bias = GPIODGLIB_LINE_BIAS_AS_IS;
		} else if (g_strcmp0(str, "pull-up") == 0) {
			bias = GPIODGLIB_LINE_BIAS_PULL_UP;
		} else if (g_strcmp0(str, "pull-down") == 0) {
			bias = GPIODGLIB_LINE_BIAS_PULL_DOWN;
		} else if (g_strcmp0(str, "disabled") == 0) {
			bias = GPIODGLIB_LINE_BIAS_DISABLED;
		} else {
			g_critical("invalid bias value received: '%s'", str);
			return FALSE;
		}

		gpiodglib_line_settings_set_bias(settings, bias);
	} else if (g_strcmp0(key, "drive") == 0) {
		str = g_variant_get_string(val, NULL);

		if (g_strcmp0(str, "push-pull") == 0) {
			drive = GPIODGLIB_LINE_DRIVE_PUSH_PULL;
		} else if (g_strcmp0(str, "open-drain") == 0) {
			drive = GPIODGLIB_LINE_DRIVE_OPEN_DRAIN;
		} else if (g_strcmp0(str, "open-source") == 0) {
			drive = GPIODGLIB_LINE_DRIVE_OPEN_SOURCE;
		} else {
			g_critical("invalid drive value received: '%s'", str);
			return FALSE;
		}

		gpiodglib_line_settings_set_drive(settings, drive);
	} else if (g_strcmp0(key, "debounce-period") == 0) {
		gpiodglib_line_settings_set_debounce_period_us(settings,
						g_variant_get_int64(val));
	} else if (g_strcmp0(key, "event-clock") == 0) {
		str = g_variant_get_string(val, NULL);

		if (g_strcmp0(str, "monotonic") == 0) {
			event_clock = GPIODGLIB_LINE_CLOCK_MONOTONIC;
		} else if (g_strcmp0(str, "realtime") == 0) {
			event_clock = GPIODGLIB_LINE_CLOCK_REALTIME;
		} else if (g_strcmp0(str, "hte") == 0) {
			event_clock = GPIODGLIB_LINE_CLOCK_HTE;
		} else {
			g_critical("invalid event clock value received: '%s'",
				   str);
			return FALSE;
		}

		gpiodglib_line_settings_set_event_clock(settings, event_clock);
	} else {
		g_critical("invalid config option received: '%s'", key);
		return FALSE;
	}

	return TRUE;
}

GpiodglibLineConfig *gpiodbus_line_config_from_variant(GVariant *variant)
{
	g_autoptr(GpiodglibLineSettings) settings = NULL;
	g_autoptr(GpiodglibLineConfig) config = NULL;
	g_autoptr(GVariant) output_values_v = NULL;
	g_autoptr(GVariant) line_configs_v = NULL;
	g_autoptr(GArray) values = NULL;
	g_autoptr(GError) err = NULL;
	GVariantIter iter0, iter1;
	guint offset;
	gboolean ret;
	GVariant *v;
	gchar *k;
	gint val;

	line_configs_v = g_variant_get_child_value(variant, 0);
	output_values_v = g_variant_get_child_value(variant, 1);

	config = gpiodglib_line_config_new();
	settings = gpiodglib_line_settings_new(NULL);

	g_variant_iter_init(&iter0, line_configs_v);
	while ((v = g_variant_iter_next_value(&iter0))) {
		g_autoptr(GVariant) line_settings_v = NULL;
		g_autoptr(GVariant) line_config_v = v;
		g_autoptr(GVariant) offsets_v = NULL;
		g_autoptr(GArray) offsets = NULL;

		offsets_v = g_variant_get_child_value(line_config_v, 0);
		line_settings_v = g_variant_get_child_value(line_config_v, 1);

		gpiodglib_line_settings_reset(settings);
		g_variant_iter_init(&iter1, line_settings_v);
		while (g_variant_iter_next(&iter1, "{sv}", &k, &v)) {
			g_autoptr(GVariant) val = v;
			g_autofree gchar *key = k;

			ret = set_settings_from_variant(settings, key, val);
			if (!ret)
				return NULL;
		}

		offsets = g_array_sized_new(FALSE, TRUE, sizeof(guint),
					    g_variant_n_children(offsets_v));
		g_variant_iter_init(&iter1, offsets_v);
		while (g_variant_iter_next(&iter1, "u", &offset))
			g_array_append_val(offsets, offset);

		ret = gpiodglib_line_config_add_line_settings(config, offsets,
							      settings, &err);
		if (!ret) {
			g_critical("failed to add line settings: %s",
				   err->message);
			return NULL;
		}
	}

	values = g_array_sized_new(FALSE, TRUE, sizeof(gint),
				   g_variant_n_children(output_values_v));
	g_variant_iter_init(&iter0, output_values_v);
	while (g_variant_iter_next(&iter0, "i", &val))
		g_array_append_val(values, val);

	if (values->len > 0) {
		ret = gpiodglib_line_config_set_output_values(config, values,
							      &err);
		if (!ret) {
			g_critical("failed to set output values: %s",
				   err->message);
			return NULL;
		}
	}

	return g_object_ref(config);
}

GpiodglibRequestConfig *gpiodbus_request_config_from_variant(GVariant *variant)
{
	g_autoptr(GpiodglibRequestConfig) config = NULL;
	GVariantIter iter;
	GVariant *v;
	gchar *k;

	config = gpiodglib_request_config_new(NULL);

	g_variant_iter_init(&iter, variant);
	while (g_variant_iter_next(&iter, "{sv}", &k, &v)) {
		g_autoptr(GVariant) val = v;
		g_autofree gchar *key = k;

		if (g_strcmp0(key, "consumer") == 0) {
			gpiodglib_request_config_set_consumer(config,
					g_variant_get_string(val, NULL));
		} else if (g_strcmp0(key, "event-buffer-size") == 0) {
			gpiodglib_request_config_set_event_buffer_size(config,
						g_variant_get_uint32(val));
		} else {
			g_critical("invalid request config option received: '%s'",
				   key);
			return NULL;
		}
	}

	return g_object_ref(config);
}