Debian Codegraph @

bindings/glib/chip.c

raw symbols SCIP
1 // SPDX-License-Identifier: LGPL-2.1-or-later
2 // SPDX-FileCopyrightText: 2022-2024 Bartosz Golaszewski <[email protected]>
3
4 #include <gio/gio.h>
5
6 #include "internal.h"
7
8 /**
9 * GpiodglibChip:
10 *
11 * Represents a single GPIO chip.
12 */
13 struct _GpiodglibChip {
14 GObject parent_instance;
15 GString *path;
16 GError *construct_err;
17 struct gpiod_chip *handle;
18 GSource *info_event_src;
19 guint info_event_src_id;
20 };
21
22 typedef enum {
23 GPIODGLIB_CHIP_PROP_PATH = 1,
24 } GpiodglibChipProp;
25
26 enum {
27 GPIODGLIB_CHIP_SIGNAL_INFO_EVENT,
28 GPIODGLIB_CHIP_SIGNAL_LAST,
29 };
30
31 static guint signals[GPIODGLIB_CHIP_SIGNAL_LAST];
32
33 static void g_string_free_complete(GString *str)
34 {
35 g_string_free(str, TRUE);
36 }
37
38 static gboolean
39 gpiodglib_chip_on_info_event(GIOChannel *source G_GNUC_UNUSED,
40 GIOCondition condition G_GNUC_UNUSED,
41 gpointer data)
42 {
43 g_autoptr(GpiodglibInfoEvent) event = NULL;
44 struct gpiod_info_event *event_handle;
45 GpiodglibChip *self = data;
46
47 event_handle = gpiod_chip_read_info_event(self->handle);
48 if (!event_handle)
49 return TRUE;
50
51 event = _gpiodglib_info_event_new(event_handle);
52
53 g_signal_emit(self, signals[GPIODGLIB_CHIP_SIGNAL_INFO_EVENT], 0,
54 event);
55
56 return TRUE;
57 }
58
59 static gboolean
60 gpiodglib_chip_initable_init(GInitable *initable,
61 GCancellable *cancellable G_GNUC_UNUSED,
62 GError **err)
63 {
64 GpiodglibChip *self = GPIODGLIB_CHIP_OBJ(initable);
65
66 if (self->construct_err) {
67 g_propagate_error(err, g_steal_pointer(&self->construct_err));
68 return FALSE;
69 }
70
71 return TRUE;
72 }
73
74 static void gpiodglib_chip_initable_iface_init(GInitableIface *iface)
75 {
76 iface->init = gpiodglib_chip_initable_init;
77 }
78
79 G_DEFINE_TYPE_WITH_CODE(GpiodglibChip, gpiodglib_chip, G_TYPE_OBJECT,
80 G_IMPLEMENT_INTERFACE(
81 G_TYPE_INITABLE,
82 gpiodglib_chip_initable_iface_init));
83
84 static void gpiodglib_chip_constructed(GObject *obj)
85 {
86 GpiodglibChip *self = GPIODGLIB_CHIP_OBJ(obj);
87 g_autoptr(GIOChannel) channel = NULL;
88
89 g_assert(!self->handle);
90 g_assert(self->path);
91
92 self->handle = gpiod_chip_open(self->path->str);
93 if (!self->handle) {
94 _gpiodglib_set_error_from_errno(&self->construct_err,
95 "unable to open GPIO chip '%s'",
96 self->path->str);
97 return;
98 }
99
100 channel = g_io_channel_unix_new(gpiod_chip_get_fd(self->handle));
101 self->info_event_src = g_io_create_watch(channel, G_IO_IN);
102 g_source_set_callback(self->info_event_src,
103 G_SOURCE_FUNC(gpiodglib_chip_on_info_event),
104 self, NULL);
105 self->info_event_src_id = g_source_attach(self->info_event_src, NULL);
106
107 G_OBJECT_CLASS(gpiodglib_chip_parent_class)->constructed(obj);
108 }
109
110 static void gpiodglib_chip_get_property(GObject *obj, guint prop_id,
111 GValue *val, GParamSpec *pspec)
112 {
113 GpiodglibChip *self = GPIODGLIB_CHIP_OBJ(obj);
114
115 switch ((GpiodglibChipProp)prop_id) {
116 case GPIODGLIB_CHIP_PROP_PATH:
117 g_value_set_string(val, self->path->str);
118 break;
119 default:
120 G_OBJECT_WARN_INVALID_PROPERTY_ID(obj, prop_id, pspec);
121 }
122 }
123
124 static void gpiodglib_chip_set_property(GObject *obj, guint prop_id,
125 const GValue *val, GParamSpec *pspec)
126 {
127 GpiodglibChip *self = GPIODGLIB_CHIP_OBJ(obj);
128
129 switch ((GpiodglibChipProp)prop_id) {
130 case GPIODGLIB_CHIP_PROP_PATH:
131 self->path = g_string_new(g_value_get_string(val));
132 break;
133 default:
134 G_OBJECT_WARN_INVALID_PROPERTY_ID(obj, prop_id, pspec);
135 }
136 }
137
138 void gpiodglib_chip_close(GpiodglibChip *self)
139 {
140 g_clear_pointer(&self->info_event_src, g_source_unref);
141 g_clear_pointer(&self->handle, gpiod_chip_close);
142 }
143
144 static void gpiodglib_chip_dispose(GObject *obj)
145 {
146 GpiodglibChip *self = GPIODGLIB_CHIP_OBJ(obj);
147
148 if (self->info_event_src_id)
149 g_source_remove(self->info_event_src_id);
150
151 gpiodglib_chip_close(self);
152
153 G_OBJECT_CLASS(gpiodglib_chip_parent_class)->dispose(obj);
154 }
155
156 static void gpiodglib_chip_finalize(GObject *obj)
157 {
158 GpiodglibChip *self = GPIODGLIB_CHIP_OBJ(obj);
159
160 g_clear_error(&self->construct_err);
161 g_clear_pointer(&self->path, g_string_free_complete);
162
163 G_OBJECT_CLASS(gpiodglib_chip_parent_class)->finalize(obj);
164 }
165
166 static void gpiodglib_chip_class_init(GpiodglibChipClass *chip_class)
167 {
168 GObjectClass *class = G_OBJECT_CLASS(chip_class);
169
170 class->constructed = gpiodglib_chip_constructed;
171 class->get_property = gpiodglib_chip_get_property;
172 class->set_property = gpiodglib_chip_set_property;
173 class->dispose = gpiodglib_chip_dispose;
174 class->finalize = gpiodglib_chip_finalize;
175
176 /**
177 * GpiodglibChip:path:
178 *
179 * Path that was used to open this GPIO chip.
180 */
181 g_object_class_install_property(class, GPIODGLIB_CHIP_PROP_PATH,
182 g_param_spec_string("path", "Path",
183 "Path to the GPIO chip device used to create this chip.",
184 NULL,
185 G_PARAM_CONSTRUCT_ONLY |
186 G_PARAM_READWRITE |
187 G_PARAM_STATIC_STRINGS));
188
189 /**
190 * GpiodglibChip::info-event:
191 * @chip: #GpiodglibChip receiving the event
192 * @event: The #GpiodglibInfoEvent
193 *
194 * Emitted when the state of a watched GPIO line changes.
195 */
196 signals[GPIODGLIB_CHIP_SIGNAL_INFO_EVENT] =
197 g_signal_new("info-event",
198 G_TYPE_FROM_CLASS(chip_class),
199 G_SIGNAL_RUN_LAST,
200 0,
201 NULL,
202 NULL,
203 g_cclosure_marshal_generic,
204 G_TYPE_NONE,
205 1,
206 GPIODGLIB_INFO_EVENT_TYPE);
207 }
208
209 static void gpiodglib_chip_init(GpiodglibChip *self)
210 {
211 self->path = NULL;
212 self->construct_err = NULL;
213 self->handle = NULL;
214 self->info_event_src = NULL;
215 self->info_event_src_id = 0;
216 }
217
218 GpiodglibChip *gpiodglib_chip_new(const gchar *path, GError **err)
219 {
220 return GPIODGLIB_CHIP_OBJ(g_initable_new(GPIODGLIB_CHIP_TYPE, NULL, err,
221 "path", path, NULL));
222 }
223
224 gboolean gpiodglib_chip_is_closed(GpiodglibChip *self)
225 {
226 return !self->handle;
227 }
228
229 gchar *gpiodglib_chip_dup_path(GpiodglibChip *self)
230 {
231 return _gpiodglib_dup_prop_string(G_OBJECT(self), "path");
232 }
233
234 static void set_err_chip_closed(GError **err)
235 {
236 g_set_error(err, GPIODGLIB_ERROR, GPIODGLIB_ERR_CHIP_CLOSED,
237 "Chip was closed and cannot be used");
238 }
239
240 GpiodglibChipInfo *gpiodglib_chip_get_info(GpiodglibChip *self, GError **err)
241 {
242 struct gpiod_chip_info *info;
243
244 g_assert(self);
245
246 if (gpiodglib_chip_is_closed(self)) {
247 set_err_chip_closed(err);
248 return NULL;
249 }
250
251 info = gpiod_chip_get_info(self->handle);
252 if (!info) {
253 _gpiodglib_set_error_from_errno(err,
254 "unable to retrieve GPIO chip information");
255 return NULL;
256 }
257
258 return _gpiodglib_chip_info_new(info);
259 }
260
261 static GpiodglibLineInfo *
262 gpiodglib_chip_do_get_line_info(GpiodglibChip *self, guint offset, GError **err,
263 struct gpiod_line_info *(*func)(struct gpiod_chip *,
264 unsigned int),
265 const gchar *err_action)
266 {
267 struct gpiod_line_info *info;
268
269 g_assert(self);
270
271 if (gpiodglib_chip_is_closed(self)) {
272 set_err_chip_closed(err);
273 return NULL;
274 }
275
276 info = func(self->handle, offset);
277 if (!info) {
278 _gpiodglib_set_error_from_errno(err, "unable to %s for offset %u",
279 err_action, offset);
280 return NULL;
281 }
282
283 return _gpiodglib_line_info_new(info);
284 }
285
286 GpiodglibLineInfo *
287 gpiodglib_chip_get_line_info(GpiodglibChip *self, guint offset, GError **err)
288 {
289 return gpiodglib_chip_do_get_line_info(self, offset, err,
290 gpiod_chip_get_line_info,
291 "retrieve GPIO line-info");
292 }
293
294 GpiodglibLineInfo *
295 gpiodglib_chip_watch_line_info(GpiodglibChip *self, guint offset, GError **err)
296 {
297 return gpiodglib_chip_do_get_line_info(self, offset, err,
298 gpiod_chip_watch_line_info,
299 "setup a line-info watch");
300 }
301
302 gboolean
303 gpiodglib_chip_unwatch_line_info(GpiodglibChip *self, guint offset,
304 GError **err)
305 {
306 int ret;
307
308 g_assert(self);
309
310 if (gpiodglib_chip_is_closed(self)) {
311 set_err_chip_closed(err);
312 return FALSE;
313 }
314
315 ret = gpiod_chip_unwatch_line_info(self->handle, offset);
316 if (ret) {
317 _gpiodglib_set_error_from_errno(err,
318 "unable to unwatch line-info events for offset %u",
319 offset);
320 return FALSE;
321 }
322
323 return TRUE;
324 }
325
326 gboolean
327 gpiodglib_chip_get_line_offset_from_name(GpiodglibChip *self, const gchar *name,
328 guint *offset, GError **err)
329 {
330 gint ret;
331
332 g_assert(self);
333
334 if (gpiodglib_chip_is_closed(self)) {
335 set_err_chip_closed(err);
336 return FALSE;
337 }
338
339 if (!name) {
340 g_set_error(err, GPIODGLIB_ERROR, GPIODGLIB_ERR_INVAL,
341 "name must not be NULL");
342 return FALSE;
343 }
344
345 ret = gpiod_chip_get_line_offset_from_name(self->handle, name);
346 if (ret < 0) {
347 if (errno != ENOENT)
348 _gpiodglib_set_error_from_errno(err,
349 "failed to map line name to offset");
350 else
351 errno = 0;
352
353 return FALSE;
354 }
355
356 if (offset)
357 *offset = ret;
358
359 return TRUE;
360 }
361
362 GpiodglibLineRequest *
363 gpiodglib_chip_request_lines(GpiodglibChip *self,
364 GpiodglibRequestConfig *req_cfg,
365 GpiodglibLineConfig *line_cfg, GError **err)
366 {
367 struct gpiod_request_config *req_cfg_handle;
368 struct gpiod_line_config *line_cfg_handle;
369 struct gpiod_line_request *req;
370
371 g_assert(self);
372
373 if (gpiodglib_chip_is_closed(self)) {
374 set_err_chip_closed(err);
375 return NULL;
376 }
377
378 if (!line_cfg) {
379 g_set_error(err, GPIODGLIB_ERROR, GPIODGLIB_ERR_INVAL,
380 "line-config is required for request");
381 return NULL;
382 }
383
384 req_cfg_handle = req_cfg ?
385 _gpiodglib_request_config_get_handle(req_cfg) : NULL;
386 line_cfg_handle = _gpiodglib_line_config_get_handle(line_cfg);
387
388 req = gpiod_chip_request_lines(self->handle,
389 req_cfg_handle, line_cfg_handle);
390 if (!req) {
391 _gpiodglib_set_error_from_errno(err,
392 "failed to request GPIO lines");
393 return NULL;
394 }
395
396 return _gpiodglib_line_request_new(req);
397 }