Debian Codegraph @

bindings/python/gpiod/ext/chip.c

raw symbols SCIP
1 // SPDX-License-Identifier: LGPL-2.1-or-later
2 // SPDX-FileCopyrightText: 2022 Bartosz Golaszewski <[email protected]>
3
4 #include "internal.h"
5
6 typedef struct {
7 PyObject_HEAD;
8 struct gpiod_chip *chip;
9 } chip_object;
10
11 static int
12 chip_init(chip_object *self, PyObject *args, PyObject *Py_UNUSED(ignored))
13 {
14 struct gpiod_chip *chip;
15 char *path;
16 int ret;
17
18 ret = PyArg_ParseTuple(args, "s", &path);
19 if (!ret)
20 return -1;
21
22 Py_BEGIN_ALLOW_THREADS;
23 chip = gpiod_chip_open(path);
24 Py_END_ALLOW_THREADS;
25 if (!chip) {
26 Py_gpiod_SetErrFromErrno();
27 return -1;
28 }
29
30 self->chip = chip;
31
32 return 0;
33 }
34
35 static void chip_finalize(chip_object *self)
36 {
37 if (self->chip)
38 PyObject_CallMethod((PyObject *)self, "close", "");
39 }
40
41 static PyObject *chip_path(chip_object *self, void *Py_UNUSED(ignored))
42 {
43 return PyUnicode_FromString(gpiod_chip_get_path(self->chip));
44 }
45
46 static PyObject *chip_fd(chip_object *self, void *Py_UNUSED(ignored))
47 {
48 return PyLong_FromLong(gpiod_chip_get_fd(self->chip));
49 }
50
51 static PyGetSetDef chip_getset[] = {
52 {
53 .name = "path",
54 .get = (getter)chip_path,
55 },
56 {
57 .name = "fd",
58 .get = (getter)chip_fd,
59 },
60 { }
61 };
62
63 static PyObject *chip_close(chip_object *self, PyObject *Py_UNUSED(ignored))
64 {
65 Py_BEGIN_ALLOW_THREADS;
66 gpiod_chip_close(self->chip);
67 Py_END_ALLOW_THREADS;
68 self->chip = NULL;
69
70 Py_RETURN_NONE;
71 }
72
73 static PyObject *chip_get_info(chip_object *self, PyObject *Py_UNUSED(ignored))
74 {
75 struct gpiod_chip_info *info;
76 PyObject *type, *ret;
77
78 type = Py_gpiod_GetModuleAttrString("gpiod.chip_info", "ChipInfo");
79 if (!type)
80 return NULL;
81
82 info = gpiod_chip_get_info(self->chip);
83 if (!info) {
84 Py_DECREF(type);
85 return PyErr_SetFromErrno(PyExc_OSError);
86 }
87
88 ret = PyObject_CallFunction(type, "ssI",
89 gpiod_chip_info_get_name(info),
90 gpiod_chip_info_get_label(info),
91 gpiod_chip_info_get_num_lines(info));
92 gpiod_chip_info_free(info);
93 Py_DECREF(type);
94 return ret;
95 }
96
97 static PyObject *make_line_info(struct gpiod_line_info *info)
98 {
99 PyObject *type, *ret;
100
101 type = Py_gpiod_GetModuleAttrString("gpiod.line_info", "LineInfo");
102 if (!type)
103 return NULL;
104
105 ret = PyObject_CallFunction(type, "IsOsiOiiiiOk",
106 gpiod_line_info_get_offset(info),
107 gpiod_line_info_get_name(info),
108 gpiod_line_info_is_used(info) ?
109 Py_True : Py_False,
110 gpiod_line_info_get_consumer(info),
111 gpiod_line_info_get_direction(info),
112 gpiod_line_info_is_active_low(info) ?
113 Py_True : Py_False,
114 gpiod_line_info_get_bias(info),
115 gpiod_line_info_get_drive(info),
116 gpiod_line_info_get_edge_detection(info),
117 gpiod_line_info_get_event_clock(info),
118 gpiod_line_info_is_debounced(info) ?
119 Py_True : Py_False,
120 gpiod_line_info_get_debounce_period_us(info));
121 Py_DECREF(type);
122 return ret;
123 }
124
125 static PyObject *chip_get_line_info(chip_object *self, PyObject *args)
126 {
127 struct gpiod_line_info *info;
128 unsigned int offset;
129 PyObject *info_obj;
130 int ret, watch;
131
132 ret = PyArg_ParseTuple(args, "Ip", &offset, &watch);
133 if (!ret)
134 return NULL;
135
136 Py_BEGIN_ALLOW_THREADS;
137 if (watch)
138 info = gpiod_chip_watch_line_info(self->chip, offset);
139 else
140 info = gpiod_chip_get_line_info(self->chip, offset);
141 Py_END_ALLOW_THREADS;
142 if (!info)
143 return Py_gpiod_SetErrFromErrno();
144
145 info_obj = make_line_info(info);
146 gpiod_line_info_free(info);
147 return info_obj;
148 }
149
150 static PyObject *
151 chip_unwatch_line_info(chip_object *self, PyObject *args)
152 {
153 unsigned int offset;
154 int ret;
155
156 ret = PyArg_ParseTuple(args, "I", &offset);
157 if (!ret)
158 return NULL;
159
160 Py_BEGIN_ALLOW_THREADS;
161 ret = gpiod_chip_unwatch_line_info(self->chip, offset);
162 Py_END_ALLOW_THREADS;
163 if (ret)
164 return Py_gpiod_SetErrFromErrno();
165
166 Py_RETURN_NONE;
167 }
168
169 static PyObject *
170 chip_read_info_event(chip_object *self, PyObject *Py_UNUSED(ignored))
171 {
172 PyObject *type, *info_obj, *event_obj;
173 struct gpiod_info_event *event;
174 struct gpiod_line_info *info;
175
176 Py_BEGIN_ALLOW_THREADS;
177 event = gpiod_chip_read_info_event(self->chip);
178 Py_END_ALLOW_THREADS;
179 if (!event)
180 return Py_gpiod_SetErrFromErrno();
181
182 info = gpiod_info_event_get_line_info(event);
183
184 info_obj = make_line_info(info);
185 if (!info_obj) {
186 gpiod_info_event_free(event);
187 return NULL;
188 }
189
190 type = Py_gpiod_GetModuleAttrString("gpiod.info_event", "InfoEvent");
191 if (!type) {
192 Py_DECREF(info_obj);
193 gpiod_info_event_free(event);
194 return NULL;
195 }
196
197 event_obj = PyObject_CallFunction(type, "iKO",
198 gpiod_info_event_get_event_type(event),
199 gpiod_info_event_get_timestamp_ns(event),
200 info_obj);
201 Py_DECREF(info_obj);
202 Py_DECREF(type);
203 gpiod_info_event_free(event);
204 return event_obj;
205 }
206
207 static PyObject *chip_line_offset_from_id(chip_object *self, PyObject *args)
208 {
209 int ret, offset;
210 char *name;
211
212 ret = PyArg_ParseTuple(args, "s", &name);
213 if (!ret)
214 return NULL;
215
216 Py_BEGIN_ALLOW_THREADS;
217 offset = gpiod_chip_get_line_offset_from_name(self->chip, name);
218 Py_END_ALLOW_THREADS;
219 if (offset < 0)
220 return Py_gpiod_SetErrFromErrno();
221
222 return PyLong_FromLong(offset);
223 }
224
225 static struct gpiod_request_config *
226 make_request_config(PyObject *consumer_obj, PyObject *event_buffer_size_obj)
227 {
228 struct gpiod_request_config *req_cfg;
229 size_t event_buffer_size;
230 const char *consumer;
231
232 req_cfg = gpiod_request_config_new();
233 if (!req_cfg) {
234 Py_gpiod_SetErrFromErrno();
235 return NULL;
236 }
237
238 if (consumer_obj != Py_None) {
239 consumer = PyUnicode_AsUTF8(consumer_obj);
240 if (!consumer) {
241 gpiod_request_config_free(req_cfg);
242 return NULL;
243 }
244
245 gpiod_request_config_set_consumer(req_cfg, consumer);
246 }
247
248 if (event_buffer_size_obj != Py_None) {
249 event_buffer_size = PyLong_AsSize_t(event_buffer_size_obj);
250 if (PyErr_Occurred()) {
251 gpiod_request_config_free(req_cfg);
252 return NULL;
253 }
254
255 gpiod_request_config_set_event_buffer_size(req_cfg,
256 event_buffer_size);
257 }
258
259 return req_cfg;
260 }
261
262 static PyObject *chip_request_lines(chip_object *self, PyObject *args)
263 {
264 PyObject *line_config, *consumer, *event_buffer_size, *req_obj;
265 struct gpiod_request_config *req_cfg;
266 struct gpiod_line_config *line_cfg;
267 struct gpiod_line_request *request;
268 int ret;
269
270 ret = PyArg_ParseTuple(args, "OOO",
271 &line_config, &consumer, &event_buffer_size);
272 if (!ret)
273 return NULL;
274
275 line_cfg = Py_gpiod_LineConfigGetData(line_config);
276 if (!line_cfg)
277 return NULL;
278
279 req_cfg = make_request_config(consumer, event_buffer_size);
280 if (!req_cfg)
281 return NULL;
282
283 Py_BEGIN_ALLOW_THREADS;
284 request = gpiod_chip_request_lines(self->chip, req_cfg, line_cfg);
285 Py_END_ALLOW_THREADS;
286 if (!request) {
287 gpiod_request_config_free(req_cfg);
288 return Py_gpiod_SetErrFromErrno();
289 }
290
291 req_obj = Py_gpiod_MakeRequestObject(request,
292 gpiod_request_config_get_event_buffer_size(req_cfg));
293 if (!req_obj)
294 gpiod_line_request_release(request);
295 gpiod_request_config_free(req_cfg);
296
297 return req_obj;
298 }
299
300 static PyMethodDef chip_methods[] = {
301 {
302 .ml_name = "close",
303 .ml_meth = (PyCFunction)chip_close,
304 .ml_flags = METH_NOARGS,
305 },
306 {
307 .ml_name = "get_info",
308 .ml_meth = (PyCFunction)chip_get_info,
309 .ml_flags = METH_NOARGS,
310 },
311 {
312 .ml_name = "get_line_info",
313 .ml_meth = (PyCFunction)chip_get_line_info,
314 .ml_flags = METH_VARARGS,
315 },
316 {
317 .ml_name = "unwatch_line_info",
318 .ml_meth = (PyCFunction)chip_unwatch_line_info,
319 .ml_flags = METH_VARARGS,
320 },
321 {
322 .ml_name = "read_info_event",
323 .ml_meth = (PyCFunction)chip_read_info_event,
324 .ml_flags = METH_NOARGS,
325 },
326 {
327 .ml_name = "line_offset_from_id",
328 .ml_meth = (PyCFunction)chip_line_offset_from_id,
329 .ml_flags = METH_VARARGS,
330 },
331 {
332 .ml_name = "request_lines",
333 .ml_meth = (PyCFunction)chip_request_lines,
334 .ml_flags = METH_VARARGS,
335 },
336 { }
337 };
338
339 PyTypeObject chip_type = {
340 PyVarObject_HEAD_INIT(NULL, 0)
341 .tp_name = "gpiod._ext.Chip",
342 .tp_basicsize = sizeof(chip_object),
343 .tp_flags = Py_TPFLAGS_DEFAULT,
344 .tp_new = PyType_GenericNew,
345 .tp_init = (initproc)chip_init,
346 .tp_finalize = (destructor)chip_finalize,
347 .tp_dealloc = (destructor)Py_gpiod_dealloc,
348 .tp_getset = chip_getset,
349 .tp_methods = chip_methods,
350 };