Debian Codegraph @

tests/gpiosim-glib/gpiosim-glib.c

raw symbols SCIP
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2 /* SPDX-FileCopyrightText: 2022 Bartosz Golaszewski <[email protected]> */
3
4 #include <errno.h>
5 #include <gpiosim.h>
6 #include <stdlib.h>
7 #include <unistd.h>
8
9 #include "gpiosim-glib.h"
10
11 G_DEFINE_QUARK(g-gpiosim-error, g_gpiosim_error);
12
13 struct _GPIOSimChip {
14 GObject parent_instance;
15 struct gpiosim_bank *bank;
16 GError *construct_err;
17 guint num_lines;
18 gchar *label;
19 GVariant *line_names;
20 GVariant *hogs;
21 };
22
23 enum {
24 G_GPIOSIM_CHIP_PROP_DEV_PATH = 1,
25 G_GPIOSIM_CHIP_PROP_NAME,
26 G_GPIOSIM_CHIP_PROP_NUM_LINES,
27 G_GPIOSIM_CHIP_PROP_LABEL,
28 G_GPIOSIM_CHIP_PROP_LINE_NAMES,
29 G_GPIOSIM_CHIP_PROP_HOGS,
30 };
31
32 static struct gpiosim_ctx *sim_ctx;
33
34 static gboolean
35 g_gpiosim_chip_initable_init(GInitable *initable,
36 GCancellable *cancellable G_GNUC_UNUSED,
37 GError **err)
38 {
39 GPIOSimChip *self = G_GPIOSIM_CHIP_OBJ(initable);
40
41 if (self->construct_err) {
42 g_propagate_error(err, self->construct_err);
43 self->construct_err = NULL;
44 return FALSE;
45 }
46
47 return TRUE;
48 }
49
50 static void g_gpiosim_chip_initable_iface_init(GInitableIface *iface)
51 {
52 iface->init = g_gpiosim_chip_initable_init;
53 }
54
55 G_DEFINE_TYPE_WITH_CODE(GPIOSimChip, g_gpiosim_chip, G_TYPE_OBJECT,
56 G_IMPLEMENT_INTERFACE(
57 G_TYPE_INITABLE,
58 g_gpiosim_chip_initable_iface_init));
59
60 static void g_gpiosim_ctx_unref(void)
61 {
62 gpiosim_ctx_unref(sim_ctx);
63 }
64
65 static gboolean g_gpiosim_chip_apply_line_names(GPIOSimChip *self)
66 {
67 g_autoptr(GVariantIter) iter = NULL;
68 guint offset;
69 gchar *name;
70 int ret;
71
72 if (!self->line_names)
73 return TRUE;
74
75 iter = g_variant_iter_new(self->line_names);
76
77 while (g_variant_iter_loop(iter, "(us)", &offset, &name)) {
78 ret = gpiosim_bank_set_line_name(self->bank, offset, name);
79 if (ret) {
80 g_set_error(&self->construct_err, G_GPIOSIM_ERROR,
81 G_GPIOSIM_ERR_CHIP_INIT_FAILED,
82 "Unable to set the name of the simulated GPIO line: %s",
83 g_strerror(errno));
84 return FALSE;
85 }
86 }
87
88 return TRUE;
89 }
90
91 static gboolean g_gpiosim_chip_apply_hogs(GPIOSimChip *self)
92 {
93 g_autoptr(GVariantIter) iter = NULL;
94 enum gpiosim_direction dir;
95 guint offset;
96 gchar *name;
97 gint vdir;
98 int ret;
99
100 if (!self->hogs)
101 return TRUE;
102
103 iter = g_variant_iter_new(self->hogs);
104
105 while (g_variant_iter_loop(iter, "(usi)", &offset, &name, &vdir)) {
106 switch (vdir) {
107 case G_GPIOSIM_DIRECTION_INPUT:
108 dir = GPIOSIM_DIRECTION_INPUT;
109 break;
110 case G_GPIOSIM_DIRECTION_OUTPUT_HIGH:
111 dir = GPIOSIM_DIRECTION_OUTPUT_HIGH;
112 break;
113 case G_GPIOSIM_DIRECTION_OUTPUT_LOW:
114 dir = GPIOSIM_DIRECTION_OUTPUT_LOW;
115 break;
116 default:
117 g_error("Invalid hog direction value: %d", vdir);
118 }
119
120 ret = gpiosim_bank_hog_line(self->bank, offset, name, dir);
121 if (ret) {
122 g_set_error(&self->construct_err, G_GPIOSIM_ERROR,
123 G_GPIOSIM_ERR_CHIP_INIT_FAILED,
124 "Unable to hog the simulated GPIO line: %s",
125 g_strerror(errno));
126 return FALSE;
127 }
128 }
129
130 return TRUE;
131 }
132
133 static gboolean g_gpiosim_chip_apply_properties(GPIOSimChip *self)
134 {
135 gboolean err;
136 int ret;
137
138 ret = gpiosim_bank_set_num_lines(self->bank, self->num_lines);
139 if (ret) {
140 g_set_error(&self->construct_err, G_GPIOSIM_ERROR,
141 G_GPIOSIM_ERR_CHIP_INIT_FAILED,
142 "Unable to set the number of lines exposed by the simulated chip: %s",
143 g_strerror(errno));
144 return FALSE;
145 }
146
147 if (self->label) {
148 ret = gpiosim_bank_set_label(self->bank, self->label);
149 if (ret) {
150 g_set_error(&self->construct_err, G_GPIOSIM_ERROR,
151 G_GPIOSIM_ERR_CHIP_INIT_FAILED,
152 "Unable to set the label of the simulated chip: %s",
153 g_strerror(errno));
154 return FALSE;
155 }
156 }
157
158 err = g_gpiosim_chip_apply_line_names(self);
159 if (!err)
160 return FALSE;
161
162 return g_gpiosim_chip_apply_hogs(self);
163 }
164
165 static gboolean g_gpiosim_chip_enable(GPIOSimChip *self)
166 {
167 struct gpiosim_dev *dev;
168 int ret;
169
170 dev = gpiosim_bank_get_dev(self->bank);
171 ret = gpiosim_dev_enable(dev);
172 gpiosim_dev_unref(dev);
173 if (ret) {
174 g_set_error(&self->construct_err, G_GPIOSIM_ERROR,
175 G_GPIOSIM_ERR_CHIP_ENABLE_FAILED,
176 "Error while trying to enable the simulated GPIO device: %s",
177 g_strerror(errno));
178 return FALSE;
179 }
180
181 return TRUE;
182 }
183
184 static gboolean g_gpiosim_ctx_init(GError **err)
185 {
186 sim_ctx = gpiosim_ctx_new();
187 if (!sim_ctx) {
188 g_set_error(err, G_GPIOSIM_ERROR,
189 G_GPIOSIM_ERR_CTX_INIT_FAILED,
190 "Unable to initialize libgpiosim: %s",
191 g_strerror(errno));
192 return FALSE;
193 }
194
195 atexit(g_gpiosim_ctx_unref);
196
197 return TRUE;
198 }
199
200 static void g_gpiosim_chip_constructed(GObject *obj)
201 {
202 GPIOSimChip *self = G_GPIOSIM_CHIP(obj);
203 struct gpiosim_dev *dev;
204 gboolean ret;
205
206 if (!sim_ctx) {
207 ret = g_gpiosim_ctx_init(&self->construct_err);
208 if (!ret)
209 return;
210 }
211
212 dev = gpiosim_dev_new(sim_ctx);
213 if (!dev) {
214 g_set_error(&self->construct_err, G_GPIOSIM_ERROR,
215 G_GPIOSIM_ERR_CHIP_INIT_FAILED,
216 "Unable to instantiate new GPIO device: %s",
217 g_strerror(errno));
218 return;
219 }
220
221 self->bank = gpiosim_bank_new(dev);
222 gpiosim_dev_unref(dev);
223 if (!self->bank) {
224 g_set_error(&self->construct_err, G_GPIOSIM_ERROR,
225 G_GPIOSIM_ERR_CHIP_INIT_FAILED,
226 "Unable to instantiate new GPIO bank: %s",
227 g_strerror(errno));
228 return;
229 }
230
231 ret = g_gpiosim_chip_apply_properties(self);
232 if (!ret)
233 return;
234
235 ret = g_gpiosim_chip_enable(self);
236 if (!ret)
237 return;
238
239 G_OBJECT_CLASS(g_gpiosim_chip_parent_class)->constructed(obj);
240 }
241
242 static void g_gpiosim_chip_get_property(GObject *obj, guint prop_id,
243 GValue *val, GParamSpec *pspec)
244 {
245 GPIOSimChip *self = G_GPIOSIM_CHIP(obj);
246
247 switch (prop_id) {
248 case G_GPIOSIM_CHIP_PROP_DEV_PATH:
249 g_value_set_string(val, gpiosim_bank_get_dev_path(self->bank));
250 break;
251 case G_GPIOSIM_CHIP_PROP_NAME:
252 g_value_set_string(val,
253 gpiosim_bank_get_chip_name(self->bank));
254 break;
255 default:
256 G_OBJECT_WARN_INVALID_PROPERTY_ID(obj, prop_id, pspec);
257 break;
258 }
259 }
260
261 static void set_variant_prop(GVariant **prop, const GValue *val)
262 {
263 GVariant *variant = g_value_get_variant(val);
264
265 g_clear_pointer(prop, g_variant_unref);
266 if (variant)
267 *prop = g_variant_ref(variant);
268 }
269
270 static void g_gpiosim_chip_set_property(GObject *obj, guint prop_id,
271 const GValue *val, GParamSpec *pspec)
272 {
273 GPIOSimChip *self = G_GPIOSIM_CHIP(obj);
274 const gchar *label;
275
276 switch (prop_id) {
277 case G_GPIOSIM_CHIP_PROP_NUM_LINES:
278 self->num_lines = g_value_get_uint(val);
279 break;
280 case G_GPIOSIM_CHIP_PROP_LABEL:
281 g_clear_pointer(&self->label, g_free);
282 label = g_value_get_string(val);
283 if (label)
284 self->label = g_strdup(label);
285 break;
286 case G_GPIOSIM_CHIP_PROP_LINE_NAMES:
287 set_variant_prop(&self->line_names, val);
288 break;
289 case G_GPIOSIM_CHIP_PROP_HOGS:
290 set_variant_prop(&self->hogs, val);
291 break;
292 default:
293 G_OBJECT_WARN_INVALID_PROPERTY_ID(obj, prop_id, pspec);
294 break;
295 }
296 }
297
298 static void g_gpiosim_chip_dispose(GObject *obj)
299 {
300 GPIOSimChip *self = G_GPIOSIM_CHIP(obj);
301
302 g_clear_pointer(&self->line_names, g_variant_unref);
303 g_clear_pointer(&self->hogs, g_variant_unref);
304
305 G_OBJECT_CLASS(g_gpiosim_chip_parent_class)->dispose(obj);
306 }
307
308 static void g_gpiosim_disable_and_cleanup(struct gpiosim_bank *bank)
309 {
310 struct gpiosim_dev *dev;
311 gint ret;
312
313 dev = gpiosim_bank_get_dev(bank);
314
315 if (gpiosim_dev_is_live(dev)) {
316 ret = gpiosim_dev_disable(dev);
317 if (ret)
318 g_warning("Error while trying to disable the simulated GPIO device: %s",
319 g_strerror(errno));
320 }
321
322 gpiosim_dev_unref(dev);
323 gpiosim_bank_unref(bank);
324 }
325
326 static void g_gpiosim_chip_finalize(GObject *obj)
327 {
328 GPIOSimChip *self = G_GPIOSIM_CHIP(obj);
329
330 g_clear_error(&self->construct_err);
331 g_clear_pointer(&self->label, g_free);
332 g_clear_pointer(&self->bank, g_gpiosim_disable_and_cleanup);
333
334 G_OBJECT_CLASS(g_gpiosim_chip_parent_class)->finalize(obj);
335 }
336
337 static void g_gpiosim_chip_class_init(GPIOSimChipClass *chip_class)
338 {
339 GObjectClass *class = G_OBJECT_CLASS(chip_class);
340
341 class->constructed = g_gpiosim_chip_constructed;
342 class->get_property = g_gpiosim_chip_get_property;
343 class->set_property = g_gpiosim_chip_set_property;
344 class->dispose = g_gpiosim_chip_dispose;
345 class->finalize = g_gpiosim_chip_finalize;
346
347 g_object_class_install_property(
348 class, G_GPIOSIM_CHIP_PROP_DEV_PATH,
349 g_param_spec_string("dev-path", "Device path",
350 "Character device filesystem path.", NULL,
351 G_PARAM_READABLE));
352
353 g_object_class_install_property(
354 class, G_GPIOSIM_CHIP_PROP_NAME,
355 g_param_spec_string(
356 "name", "Chip name",
357 "Name of this chip device as set by the kernel.", NULL,
358 G_PARAM_READABLE));
359
360 g_object_class_install_property(
361 class, G_GPIOSIM_CHIP_PROP_NUM_LINES,
362 g_param_spec_uint("num-lines", "Number of lines",
363 "Number of lines this simulated chip exposes.",
364 1, G_MAXUINT, 1,
365 G_PARAM_WRITABLE | G_PARAM_CONSTRUCT_ONLY));
366
367 g_object_class_install_property(
368 class, G_GPIOSIM_CHIP_PROP_LABEL,
369 g_param_spec_string("label", "Chip label",
370 "Label of this simulated chip.", NULL,
371 G_PARAM_WRITABLE | G_PARAM_CONSTRUCT_ONLY));
372
373 g_object_class_install_property(
374 class, G_GPIOSIM_CHIP_PROP_LINE_NAMES,
375 g_param_spec_variant(
376 "line-names", "Line names",
377 "List of names of the lines exposed by this chip",
378 (GVariantType *)"a(us)", NULL,
379 G_PARAM_WRITABLE | G_PARAM_CONSTRUCT_ONLY));
380
381 g_object_class_install_property(
382 class, G_GPIOSIM_CHIP_PROP_HOGS,
383 g_param_spec_variant(
384 "hogs", "Line hogs",
385 "List of hogged lines and their directions.",
386 (GVariantType *)"a(usi)", NULL,
387 G_PARAM_WRITABLE | G_PARAM_CONSTRUCT_ONLY));
388 }
389
390 static void g_gpiosim_chip_init(GPIOSimChip *self)
391 {
392 self->construct_err = NULL;
393 self->num_lines = 1;
394 self->label = NULL;
395 self->line_names = NULL;
396 self->hogs = NULL;
397 }
398
399 const gchar *g_gpiosim_chip_get_dev_path(GPIOSimChip *self)
400 {
401 return gpiosim_bank_get_dev_path(self->bank);
402 }
403
404 const gchar *g_gpiosim_chip_get_name(GPIOSimChip *self)
405 {
406 return gpiosim_bank_get_chip_name(self->bank);
407 }
408
409 GPIOSimValue
410 _g_gpiosim_chip_get_value(GPIOSimChip *chip, guint offset, GError **err)
411 {
412 enum gpiosim_value val;
413
414 val = gpiosim_bank_get_value(chip->bank, offset);
415 switch (val) {
416 case GPIOSIM_VALUE_ERROR:
417 g_set_error(err, G_GPIOSIM_ERROR,
418 G_GPIOSIM_ERR_GET_VALUE_FAILED,
419 "Unable to read the line value: %s",
420 g_strerror(errno));
421 return G_GPIOSIM_VALUE_ERROR;
422 case GPIOSIM_VALUE_INACTIVE:
423 return G_GPIOSIM_VALUE_INACTIVE;
424 case GPIOSIM_VALUE_ACTIVE:
425 return G_GPIOSIM_VALUE_ACTIVE;
426 }
427
428 g_error("Invalid line value returned by gpiosim");
429 }
430
431 void g_gpiosim_chip_set_pull(GPIOSimChip *chip, guint offset, GPIOSimPull pull)
432 {
433 enum gpiosim_pull sim_pull;
434 gint ret;
435
436 switch (pull) {
437 case G_GPIOSIM_PULL_DOWN:
438 sim_pull = GPIOSIM_PULL_DOWN;
439 break;
440 case G_GPIOSIM_PULL_UP:
441 sim_pull = GPIOSIM_PULL_UP;
442 break;
443 default:
444 g_error("invalid pull value");
445 }
446
447 ret = gpiosim_bank_set_pull(chip->bank, offset, sim_pull);
448 if (ret)
449 g_critical("Unable to set the pull setting for simulated line: %s",
450 g_strerror(errno));
451 }
452
453 GVariant *g_gpiosim_package_line_names(const GPIOSimLineName *names)
454 {
455 g_autoptr(GVariantBuilder) builder = NULL;
456 const GPIOSimLineName *name;
457
458 builder = g_variant_builder_new(G_VARIANT_TYPE("a(us)"));
459
460 for (name = &names[0]; name->name; name++)
461 g_variant_builder_add(builder, "(us)",
462 name->offset, name->name);
463
464 return g_variant_ref_sink(g_variant_new("a(us)", builder));
465 }
466
467 GVariant *g_gpiosim_package_hogs(const GPIOSimHog *hogs)
468 {
469 g_autoptr(GVariantBuilder) builder = NULL;
470 const GPIOSimHog *hog;
471
472 builder = g_variant_builder_new(G_VARIANT_TYPE("a(usi)"));
473
474 for (hog = &hogs[0]; hog->name; hog++)
475 g_variant_builder_add(builder, "(usi)",
476 hog->offset, hog->name, hog->direction);
477
478 return g_variant_ref_sink(g_variant_new("a(usi)", builder));
479 }