File: chip.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 (350 lines) | stat: -rw-r--r-- 7,959 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
// SPDX-License-Identifier: LGPL-2.1-or-later
// SPDX-FileCopyrightText: 2022 Bartosz Golaszewski <brgl@bgdev.pl>

#include "internal.h"

typedef struct {
	PyObject_HEAD;
	struct gpiod_chip *chip;
} chip_object;

static int
chip_init(chip_object *self, PyObject *args, PyObject *Py_UNUSED(ignored))
{
	struct gpiod_chip *chip;
	char *path;
	int ret;

	ret = PyArg_ParseTuple(args, "s", &path);
	if (!ret)
		return -1;

	Py_BEGIN_ALLOW_THREADS;
	chip = gpiod_chip_open(path);
	Py_END_ALLOW_THREADS;
	if (!chip) {
		Py_gpiod_SetErrFromErrno();
		return -1;
	}

	self->chip = chip;

	return 0;
}

static void chip_finalize(chip_object *self)
{
	if (self->chip)
		PyObject_CallMethod((PyObject *)self, "close", "");
}

static PyObject *chip_path(chip_object *self, void *Py_UNUSED(ignored))
{
	return PyUnicode_FromString(gpiod_chip_get_path(self->chip));
}

static PyObject *chip_fd(chip_object *self, void *Py_UNUSED(ignored))
{
	return PyLong_FromLong(gpiod_chip_get_fd(self->chip));
}

static PyGetSetDef chip_getset[] = {
	{
		.name = "path",
		.get = (getter)chip_path,
	},
	{
		.name = "fd",
		.get = (getter)chip_fd,
	},
	{ }
};

static PyObject *chip_close(chip_object *self, PyObject *Py_UNUSED(ignored))
{
	Py_BEGIN_ALLOW_THREADS;
	gpiod_chip_close(self->chip);
	Py_END_ALLOW_THREADS;
	self->chip = NULL;

	Py_RETURN_NONE;
}

static PyObject *chip_get_info(chip_object *self, PyObject *Py_UNUSED(ignored))
{
	struct gpiod_chip_info *info;
	PyObject *type, *ret;

	type = Py_gpiod_GetModuleAttrString("gpiod.chip_info", "ChipInfo");
	if (!type)
		return NULL;

	info = gpiod_chip_get_info(self->chip);
	if (!info) {
		Py_DECREF(type);
		return PyErr_SetFromErrno(PyExc_OSError);
	}

	ret = PyObject_CallFunction(type, "ssI",
				    gpiod_chip_info_get_name(info),
				    gpiod_chip_info_get_label(info),
				    gpiod_chip_info_get_num_lines(info));
	gpiod_chip_info_free(info);
	Py_DECREF(type);
	return ret;
}

static PyObject *make_line_info(struct gpiod_line_info *info)
{
	PyObject *type, *ret;

	type = Py_gpiod_GetModuleAttrString("gpiod.line_info", "LineInfo");
	if (!type)
		return NULL;

	ret = PyObject_CallFunction(type, "IsOsiOiiiiOk",
				gpiod_line_info_get_offset(info),
				gpiod_line_info_get_name(info),
				gpiod_line_info_is_used(info) ?
							Py_True : Py_False,
				gpiod_line_info_get_consumer(info),
				gpiod_line_info_get_direction(info),
				gpiod_line_info_is_active_low(info) ?
							Py_True : Py_False,
				gpiod_line_info_get_bias(info),
				gpiod_line_info_get_drive(info),
				gpiod_line_info_get_edge_detection(info),
				gpiod_line_info_get_event_clock(info),
				gpiod_line_info_is_debounced(info) ?
							Py_True : Py_False,
				gpiod_line_info_get_debounce_period_us(info));
	Py_DECREF(type);
	return ret;
}

static PyObject *chip_get_line_info(chip_object *self, PyObject *args)
{
	struct gpiod_line_info *info;
	unsigned int offset;
	PyObject *info_obj;
	int ret, watch;

	ret = PyArg_ParseTuple(args, "Ip", &offset, &watch);
	if (!ret)
		return NULL;

	Py_BEGIN_ALLOW_THREADS;
	if (watch)
		info = gpiod_chip_watch_line_info(self->chip, offset);
	else
		info = gpiod_chip_get_line_info(self->chip, offset);
	Py_END_ALLOW_THREADS;
	if (!info)
		return Py_gpiod_SetErrFromErrno();

	info_obj = make_line_info(info);
	gpiod_line_info_free(info);
	return info_obj;
}

static PyObject *
chip_unwatch_line_info(chip_object *self, PyObject *args)
{
	unsigned int offset;
	int ret;

	ret = PyArg_ParseTuple(args, "I", &offset);
	if (!ret)
		return NULL;

	Py_BEGIN_ALLOW_THREADS;
	ret = gpiod_chip_unwatch_line_info(self->chip, offset);
	Py_END_ALLOW_THREADS;
	if (ret)
		return Py_gpiod_SetErrFromErrno();

	Py_RETURN_NONE;
}

static PyObject *
chip_read_info_event(chip_object *self, PyObject *Py_UNUSED(ignored))
{
	PyObject *type, *info_obj, *event_obj;
	struct gpiod_info_event *event;
	struct gpiod_line_info *info;

	Py_BEGIN_ALLOW_THREADS;
	event = gpiod_chip_read_info_event(self->chip);
	Py_END_ALLOW_THREADS;
	if (!event)
		return Py_gpiod_SetErrFromErrno();

	info = gpiod_info_event_get_line_info(event);

	info_obj = make_line_info(info);
	if (!info_obj) {
		gpiod_info_event_free(event);
		return NULL;
	}

	type = Py_gpiod_GetModuleAttrString("gpiod.info_event", "InfoEvent");
	if (!type) {
		Py_DECREF(info_obj);
		gpiod_info_event_free(event);
		return NULL;
	}

	event_obj = PyObject_CallFunction(type, "iKO",
				gpiod_info_event_get_event_type(event),
				gpiod_info_event_get_timestamp_ns(event),
				info_obj);
	Py_DECREF(info_obj);
	Py_DECREF(type);
	gpiod_info_event_free(event);
	return event_obj;
}

static PyObject *chip_line_offset_from_id(chip_object *self, PyObject *args)
{
	int ret, offset;
	char *name;

	ret = PyArg_ParseTuple(args, "s", &name);
	if (!ret)
		return NULL;

	Py_BEGIN_ALLOW_THREADS;
	offset = gpiod_chip_get_line_offset_from_name(self->chip, name);
	Py_END_ALLOW_THREADS;
	if (offset < 0)
		return Py_gpiod_SetErrFromErrno();

	return PyLong_FromLong(offset);
}

static struct gpiod_request_config *
make_request_config(PyObject *consumer_obj, PyObject *event_buffer_size_obj)
{
	struct gpiod_request_config *req_cfg;
	size_t event_buffer_size;
	const char *consumer;

	req_cfg = gpiod_request_config_new();
	if (!req_cfg) {
		Py_gpiod_SetErrFromErrno();
		return NULL;
	}

	if (consumer_obj != Py_None) {
		consumer = PyUnicode_AsUTF8(consumer_obj);
		if (!consumer) {
			gpiod_request_config_free(req_cfg);
			return NULL;
		}

		gpiod_request_config_set_consumer(req_cfg, consumer);
	}

	if (event_buffer_size_obj != Py_None) {
		event_buffer_size = PyLong_AsSize_t(event_buffer_size_obj);
		if (PyErr_Occurred()) {
			gpiod_request_config_free(req_cfg);
			return NULL;
		}

		gpiod_request_config_set_event_buffer_size(req_cfg,
							   event_buffer_size);
	}

	return req_cfg;
}

static PyObject *chip_request_lines(chip_object *self, PyObject *args)
{
	PyObject *line_config, *consumer, *event_buffer_size, *req_obj;
	struct gpiod_request_config *req_cfg;
	struct gpiod_line_config *line_cfg;
	struct gpiod_line_request *request;
	int ret;

	ret = PyArg_ParseTuple(args, "OOO",
			       &line_config, &consumer, &event_buffer_size);
	if (!ret)
		return NULL;

	line_cfg = Py_gpiod_LineConfigGetData(line_config);
	if (!line_cfg)
		return NULL;

	req_cfg = make_request_config(consumer, event_buffer_size);
	if (!req_cfg)
		return NULL;

	Py_BEGIN_ALLOW_THREADS;
	request = gpiod_chip_request_lines(self->chip, req_cfg, line_cfg);
	Py_END_ALLOW_THREADS;
	if (!request) {
		gpiod_request_config_free(req_cfg);
		return Py_gpiod_SetErrFromErrno();
	}

	req_obj = Py_gpiod_MakeRequestObject(request,
			gpiod_request_config_get_event_buffer_size(req_cfg));
	if (!req_obj)
		gpiod_line_request_release(request);
	gpiod_request_config_free(req_cfg);

	return req_obj;
}

static PyMethodDef chip_methods[] = {
	{
		.ml_name = "close",
		.ml_meth = (PyCFunction)chip_close,
		.ml_flags = METH_NOARGS,
	},
	{
		.ml_name = "get_info",
		.ml_meth = (PyCFunction)chip_get_info,
		.ml_flags = METH_NOARGS,
	},
	{
		.ml_name = "get_line_info",
		.ml_meth = (PyCFunction)chip_get_line_info,
		.ml_flags = METH_VARARGS,
	},
	{
		.ml_name = "unwatch_line_info",
		.ml_meth = (PyCFunction)chip_unwatch_line_info,
		.ml_flags = METH_VARARGS,
	},
	{
		.ml_name = "read_info_event",
		.ml_meth = (PyCFunction)chip_read_info_event,
		.ml_flags = METH_NOARGS,
	},
	{
		.ml_name = "line_offset_from_id",
		.ml_meth = (PyCFunction)chip_line_offset_from_id,
		.ml_flags = METH_VARARGS,
	},
	{
		.ml_name = "request_lines",
		.ml_meth = (PyCFunction)chip_request_lines,
		.ml_flags = METH_VARARGS,
	},
	{ }
};

PyTypeObject chip_type = {
	PyVarObject_HEAD_INIT(NULL, 0)
	.tp_name = "gpiod._ext.Chip",
	.tp_basicsize = sizeof(chip_object),
	.tp_flags = Py_TPFLAGS_DEFAULT,
	.tp_new = PyType_GenericNew,
	.tp_init = (initproc)chip_init,
	.tp_finalize = (destructor)chip_finalize,
	.tp_dealloc = (destructor)Py_gpiod_dealloc,
	.tp_getset = chip_getset,
	.tp_methods = chip_methods,
};