Debian Codegraph @

bindings/python/gpiod/ext/line-config.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_line_config *cfg;
9 } line_config_object;
10
11 static int line_config_init(line_config_object *self,
12 PyObject *Py_UNUSED(args), PyObject *Py_UNUSED(ignored))
13 {
14 self->cfg = gpiod_line_config_new();
15 if (!self->cfg) {
16 Py_gpiod_SetErrFromErrno();
17 return -1;
18 }
19
20 return 0;
21 }
22
23 static void line_config_finalize(line_config_object *self)
24 {
25 if (self->cfg)
26 gpiod_line_config_free(self->cfg);
27 }
28
29 static unsigned int *make_offsets(PyObject *obj, Py_ssize_t len)
30 {
31 unsigned int *offsets;
32 PyObject *offset;
33 Py_ssize_t i;
34
35 offsets = PyMem_Calloc(len, sizeof(unsigned int));
36 if (!offsets)
37 return (unsigned int *)PyErr_NoMemory();
38
39 for (i = 0; i < len; i++) {
40 offset = PyList_GetItem(obj, i);
41 if (!offset) {
42 PyMem_Free(offsets);
43 return NULL;
44 }
45
46 offsets[i] = Py_gpiod_PyLongAsUnsignedInt(offset);
47 if (PyErr_Occurred()) {
48 PyMem_Free(offsets);
49 return NULL;
50 }
51 }
52
53 return offsets;
54 }
55
56 static PyObject *
57 line_config_add_line_settings(line_config_object *self, PyObject *args)
58 {
59 PyObject *offsets_obj, *settings_obj;
60 struct gpiod_line_settings *settings;
61 unsigned int *offsets;
62 Py_ssize_t num_offsets;
63 int ret;
64
65 ret = PyArg_ParseTuple(args, "OO", &offsets_obj, &settings_obj);
66 if (!ret)
67 return NULL;
68
69 num_offsets = PyObject_Size(offsets_obj);
70 if (num_offsets < 0)
71 return NULL;
72
73 offsets = make_offsets(offsets_obj, num_offsets);
74 if (!offsets)
75 return NULL;
76
77 settings = Py_gpiod_LineSettingsGetData(settings_obj);
78 if (!settings) {
79 PyMem_Free(offsets);
80 return NULL;
81 }
82
83 ret = gpiod_line_config_add_line_settings(self->cfg, offsets,
84 num_offsets, settings);
85 PyMem_Free(offsets);
86 if (ret)
87 return Py_gpiod_SetErrFromErrno();
88
89 Py_RETURN_NONE;
90 }
91
92 static PyObject *
93 line_config_set_output_values(line_config_object *self, PyObject *args)
94 {
95 PyObject *values, *iter, *next, *val_stripped;
96 enum gpiod_line_value *valbuf;
97 Py_ssize_t num_values, pos;
98 int ret;
99
100 values = PyTuple_GetItem(args, 0);
101 if (!values)
102 return NULL;
103
104 num_values = PyObject_Size(values);
105 if (num_values < 0)
106 return NULL;
107
108 valbuf = PyMem_Calloc(num_values, sizeof(*valbuf));
109 if (!valbuf)
110 return PyErr_NoMemory();
111
112 iter = PyObject_GetIter(values);
113 if (!iter) {
114 PyMem_Free(valbuf);
115 return NULL;
116 }
117
118 for (pos = 0;; pos++) {
119 next = PyIter_Next(iter);
120 if (!next) {
121 Py_DECREF(iter);
122 break;
123 }
124
125 val_stripped = PyObject_GetAttrString(next, "value");
126 Py_DECREF(next);
127 if (!val_stripped) {
128 PyMem_Free(valbuf);
129 Py_DECREF(iter);
130 return NULL;
131 }
132
133 valbuf[pos] = PyLong_AsLong(val_stripped);
134 Py_DECREF(val_stripped);
135 if (PyErr_Occurred()) {
136 PyMem_Free(valbuf);
137 Py_DECREF(iter);
138 return NULL;
139 }
140 }
141
142 ret = gpiod_line_config_set_output_values(self->cfg,
143 valbuf, num_values);
144 PyMem_Free(valbuf);
145 if (ret)
146 return Py_gpiod_SetErrFromErrno();
147
148 Py_RETURN_NONE;
149 }
150
151 static PyMethodDef line_config_methods[] = {
152 {
153 .ml_name = "add_line_settings",
154 .ml_meth = (PyCFunction)line_config_add_line_settings,
155 .ml_flags = METH_VARARGS,
156 },
157 {
158 .ml_name = "set_output_values",
159 .ml_meth = (PyCFunction)line_config_set_output_values,
160 .ml_flags = METH_VARARGS,
161 },
162 { }
163 };
164
165 PyTypeObject line_config_type = {
166 PyVarObject_HEAD_INIT(NULL, 0)
167 .tp_name = "gpiod._ext.LineConfig",
168 .tp_basicsize = sizeof(line_config_object),
169 .tp_flags = Py_TPFLAGS_DEFAULT,
170 .tp_new = PyType_GenericNew,
171 .tp_init = (initproc)line_config_init,
172 .tp_finalize = (destructor)line_config_finalize,
173 .tp_dealloc = (destructor)Py_gpiod_dealloc,
174 .tp_methods = line_config_methods,
175 };
176
177 struct gpiod_line_config *Py_gpiod_LineConfigGetData(PyObject *obj)
178 {
179 line_config_object *line_cfg;
180 PyObject *type;
181
182 type = PyObject_Type(obj);
183 if (!type)
184 return NULL;
185
186 if ((PyTypeObject *)type != &line_config_type) {
187 PyErr_SetString(PyExc_TypeError,
188 "not a gpiod._ext.LineConfig object");
189 Py_DECREF(type);
190 return NULL;
191 }
192 Py_DECREF(type);
193
194 line_cfg = (line_config_object *)obj;
195
196 return line_cfg->cfg;
197 }