Debian Codegraph @

bindings/glib/line-info.c

raw symbols SCIP
1 // SPDX-License-Identifier: LGPL-2.1-or-later
2 // SPDX-FileCopyrightText: 2023-2024 Bartosz Golaszewski <[email protected]>
3
4 #include <gio/gio.h>
5
6 #include "internal.h"
7
8 /**
9 * GpiodglibLineInfo:
10 *
11 * Line info object contains an immutable snapshot of a line's status.
12 *
13 * The line info contains all the publicly available information about a
14 * line, which does not include the line value. The line must be requested
15 * to access the line value.
16 */
17 struct _GpiodglibLineInfo {
18 GObject parent_instance;
19 struct gpiod_line_info *handle;
20 };
21
22 typedef enum {
23 GPIODGLIB_LINE_INFO_PROP_OFFSET = 1,
24 GPIODGLIB_LINE_INFO_PROP_NAME,
25 GPIODGLIB_LINE_INFO_PROP_USED,
26 GPIODGLIB_LINE_INFO_PROP_CONSUMER,
27 GPIODGLIB_LINE_INFO_PROP_DIRECTION,
28 GPIODGLIB_LINE_INFO_PROP_EDGE_DETECTION,
29 GPIODGLIB_LINE_INFO_PROP_BIAS,
30 GPIODGLIB_LINE_INFO_PROP_DRIVE,
31 GPIODGLIB_LINE_INFO_PROP_ACTIVE_LOW,
32 GPIODGLIB_LINE_INFO_PROP_DEBOUNCED,
33 GPIODGLIB_LINE_INFO_PROP_DEBOUNCE_PERIOD,
34 GPIODGLIB_LINE_INFO_PROP_EVENT_CLOCK,
35 } GpiodglibLineInfoProp;
36
37 G_DEFINE_TYPE(GpiodglibLineInfo, gpiodglib_line_info, G_TYPE_OBJECT);
38
39 static void gpiodglib_line_info_get_property(GObject *obj, guint prop_id,
40 GValue *val, GParamSpec *pspec)
41 {
42 GpiodglibLineInfo *self = GPIODGLIB_LINE_INFO_OBJ(obj);
43
44 g_assert(self->handle);
45
46 switch ((GpiodglibLineInfoProp)prop_id) {
47 case GPIODGLIB_LINE_INFO_PROP_OFFSET:
48 g_value_set_uint(val, gpiod_line_info_get_offset(self->handle));
49 break;
50 case GPIODGLIB_LINE_INFO_PROP_NAME:
51 g_value_set_string(val,
52 gpiod_line_info_get_name(self->handle));
53 break;
54 case GPIODGLIB_LINE_INFO_PROP_USED:
55 g_value_set_boolean(val, gpiod_line_info_is_used(self->handle));
56 break;
57 case GPIODGLIB_LINE_INFO_PROP_CONSUMER:
58 g_value_set_string(val,
59 gpiod_line_info_get_consumer(self->handle));
60 break;
61 case GPIODGLIB_LINE_INFO_PROP_DIRECTION:
62 g_value_set_enum(val,
63 _gpiodglib_line_direction_from_library(
64 gpiod_line_info_get_direction(self->handle),
65 FALSE));
66 break;
67 case GPIODGLIB_LINE_INFO_PROP_EDGE_DETECTION:
68 g_value_set_enum(val,
69 _gpiodglib_line_edge_from_library(
70 gpiod_line_info_get_edge_detection(
71 self->handle)));
72 break;
73 case GPIODGLIB_LINE_INFO_PROP_BIAS:
74 g_value_set_enum(val,
75 _gpiodglib_line_bias_from_library(
76 gpiod_line_info_get_bias(self->handle),
77 FALSE));
78 break;
79 case GPIODGLIB_LINE_INFO_PROP_DRIVE:
80 g_value_set_enum(val,
81 _gpiodglib_line_drive_from_library(
82 gpiod_line_info_get_drive(self->handle)));
83 break;
84 case GPIODGLIB_LINE_INFO_PROP_ACTIVE_LOW:
85 g_value_set_boolean(val,
86 gpiod_line_info_is_active_low(self->handle));
87 break;
88 case GPIODGLIB_LINE_INFO_PROP_DEBOUNCED:
89 g_value_set_boolean(val,
90 gpiod_line_info_is_debounced(self->handle));
91 break;
92 case GPIODGLIB_LINE_INFO_PROP_DEBOUNCE_PERIOD:
93 g_value_set_int64(val,
94 gpiod_line_info_get_debounce_period_us(self->handle));
95 break;
96 case GPIODGLIB_LINE_INFO_PROP_EVENT_CLOCK:
97 g_value_set_enum(val,
98 _gpiodglib_line_clock_from_library(
99 gpiod_line_info_get_event_clock(self->handle)));
100 break;
101 default:
102 G_OBJECT_WARN_INVALID_PROPERTY_ID(obj, prop_id, pspec);
103 }
104 }
105
106 static void gpiodglib_line_info_finalize(GObject *obj)
107 {
108 GpiodglibLineInfo *self = GPIODGLIB_LINE_INFO_OBJ(obj);
109
110 g_clear_pointer(&self->handle, gpiod_line_info_free);
111
112 G_OBJECT_CLASS(gpiodglib_line_info_parent_class)->finalize(obj);
113 }
114
115 static void
116 gpiodglib_line_info_class_init(GpiodglibLineInfoClass *line_info_class)
117 {
118 GObjectClass *class = G_OBJECT_CLASS(line_info_class);
119
120 class->get_property = gpiodglib_line_info_get_property;
121 class->finalize = gpiodglib_line_info_finalize;
122
123 /**
124 * GpiodglibLineInfo:offset:
125 *
126 * Offset of the GPIO line.
127 */
128 g_object_class_install_property(class, GPIODGLIB_LINE_INFO_PROP_OFFSET,
129 g_param_spec_uint("offset", "Offset",
130 "Offset of the GPIO line.",
131 0, G_MAXUINT, 0,
132 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
133
134 /**
135 * GpiodglibLineInfo:name:
136 *
137 * Name of the GPIO line, if named.
138 */
139 g_object_class_install_property(class, GPIODGLIB_LINE_INFO_PROP_NAME,
140 g_param_spec_string("name", "Name",
141 "Name of the GPIO line, if named.",
142 NULL, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
143
144 /**
145 * GpiodglibLineInfo:used:
146 *
147 * Indicates whether the GPIO line is requested for exclusive usage.
148 */
149 g_object_class_install_property(class, GPIODGLIB_LINE_INFO_PROP_USED,
150 g_param_spec_boolean("used", "Is Used",
151 "Indicates whether the GPIO line is requested for exclusive usage.",
152 FALSE, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
153
154 /**
155 * GpiodglibLineInfo:consumer:
156 *
157 * Name of the consumer of the GPIO line, if requested.
158 */
159 g_object_class_install_property(class,
160 GPIODGLIB_LINE_INFO_PROP_CONSUMER,
161 g_param_spec_string("consumer", "Consumer",
162 "Name of the consumer of the GPIO line, if requested.",
163 NULL, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
164
165 /**
166 * GpiodglibLineInfo:direction:
167 *
168 * Direction of the GPIO line.
169 */
170 g_object_class_install_property(class,
171 GPIODGLIB_LINE_INFO_PROP_DIRECTION,
172 g_param_spec_enum("direction", "Direction",
173 "Direction of the GPIO line.",
174 GPIODGLIB_LINE_DIRECTION_TYPE,
175 GPIODGLIB_LINE_DIRECTION_INPUT,
176 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
177
178 /**
179 * GpiodglibLineInfo:edge-detection:
180 *
181 * Edge detection setting of the GPIO line.
182 */
183 g_object_class_install_property(class,
184 GPIODGLIB_LINE_INFO_PROP_EDGE_DETECTION,
185 g_param_spec_enum("edge-detection", "Edge Detection",
186 "Edge detection setting of the GPIO line.",
187 GPIODGLIB_LINE_EDGE_TYPE,
188 GPIODGLIB_LINE_EDGE_NONE,
189 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
190
191 /**
192 * GpiodglibLineInfo:bias:
193 *
194 * Bias setting of the GPIO line.
195 */
196 g_object_class_install_property(class, GPIODGLIB_LINE_INFO_PROP_BIAS,
197 g_param_spec_enum("bias", "Bias",
198 "Bias setting of the GPIO line.",
199 GPIODGLIB_LINE_BIAS_TYPE,
200 GPIODGLIB_LINE_BIAS_UNKNOWN,
201 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
202
203 /**
204 * GpiodglibLineInfo:drive:
205 *
206 * Drive setting of the GPIO line.
207 */
208 g_object_class_install_property(class, GPIODGLIB_LINE_INFO_PROP_DRIVE,
209 g_param_spec_enum("drive", "Drive",
210 "Drive setting of the GPIO line.",
211 GPIODGLIB_LINE_DRIVE_TYPE,
212 GPIODGLIB_LINE_DRIVE_PUSH_PULL,
213 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
214
215 /**
216 * GpiodglibLineInfo:active-low:
217 *
218 * Indicates whether the signal of the line is inverted.
219 */
220 g_object_class_install_property(class,
221 GPIODGLIB_LINE_INFO_PROP_ACTIVE_LOW,
222 g_param_spec_boolean("active-low", "Is Active-Low",
223 "Indicates whether the signal of the line is inverted.",
224 FALSE, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
225
226 /**
227 * GpiodglibLineInfo:debounced:
228 *
229 * Indicates whether the line is debounced (by hardware or by the
230 * kernel software debouncer).
231 */
232 g_object_class_install_property(class,
233 GPIODGLIB_LINE_INFO_PROP_DEBOUNCED,
234 g_param_spec_boolean("debounced", "Is Debounced",
235 "Indicates whether the line is debounced (by hardware or by the kernel software debouncer).",
236 FALSE, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
237
238 /**
239 * GpiodglibLineInfo:debounce-period-us:
240 *
241 * Debounce period of the line (expressed in microseconds).
242 */
243 g_object_class_install_property(class,
244 GPIODGLIB_LINE_INFO_PROP_DEBOUNCE_PERIOD,
245 g_param_spec_int64("debounce-period-us",
246 "Debounce Period (in microseconds)",
247 "Debounce period of the line (expressed in microseconds).",
248 0, G_MAXINT64, 0,
249 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
250
251 /**
252 * GpiodglibLineInfo:event-clock:
253 *
254 * Event clock used to timestamp the edge events of the line.
255 */
256 g_object_class_install_property(class,
257 GPIODGLIB_LINE_INFO_PROP_EVENT_CLOCK,
258 g_param_spec_enum("event-clock", "Event Clock",
259 "Event clock used to timestamp the edge events of the line.",
260 GPIODGLIB_LINE_CLOCK_TYPE,
261 GPIODGLIB_LINE_CLOCK_MONOTONIC,
262 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
263 }
264
265 static void gpiodglib_line_info_init(GpiodglibLineInfo *self)
266 {
267 self->handle = NULL;
268 }
269
270 guint gpiodglib_line_info_get_offset(GpiodglibLineInfo *self)
271 {
272 return _gpiodglib_get_prop_uint(G_OBJECT(self), "offset");
273 }
274
275 gchar *gpiodglib_line_info_dup_name(GpiodglibLineInfo *self)
276 {
277 return _gpiodglib_dup_prop_string(G_OBJECT(self), "name");
278 }
279
280 gboolean gpiodglib_line_info_is_used(GpiodglibLineInfo *self)
281 {
282 return _gpiodglib_get_prop_bool(G_OBJECT(self), "used");
283 }
284
285 gchar *gpiodglib_line_info_dup_consumer(GpiodglibLineInfo *self)
286 {
287 return _gpiodglib_dup_prop_string(G_OBJECT(self), "consumer");
288 }
289
290 GpiodglibLineDirection
291 gpiodglib_line_info_get_direction(GpiodglibLineInfo *self)
292 {
293 return _gpiodglib_get_prop_enum(G_OBJECT(self), "direction");
294 }
295
296 GpiodglibLineEdge
297 gpiodglib_line_info_get_edge_detection(GpiodglibLineInfo *self)
298 {
299 return _gpiodglib_get_prop_enum(G_OBJECT(self), "edge-detection");
300 }
301
302 GpiodglibLineBias gpiodglib_line_info_get_bias(GpiodglibLineInfo *self)
303 {
304 return _gpiodglib_get_prop_enum(G_OBJECT(self), "bias");
305 }
306
307 GpiodglibLineDrive gpiodglib_line_info_get_drive(GpiodglibLineInfo *self)
308 {
309 return _gpiodglib_get_prop_enum(G_OBJECT(self), "drive");
310 }
311
312 gboolean gpiodglib_line_info_is_active_low(GpiodglibLineInfo *self)
313 {
314 return _gpiodglib_get_prop_bool(G_OBJECT(self), "active-low");
315 }
316
317 gboolean gpiodglib_line_info_is_debounced(GpiodglibLineInfo *self)
318 {
319 return _gpiodglib_get_prop_bool(G_OBJECT(self), "debounced");
320 }
321
322 GTimeSpan gpiodglib_line_info_get_debounce_period_us(GpiodglibLineInfo *self)
323 {
324 return _gpiodglib_get_prop_timespan(G_OBJECT(self),
325 "debounce-period-us");
326 }
327
328 GpiodglibLineClock gpiodglib_line_info_get_event_clock(GpiodglibLineInfo *self)
329 {
330 return _gpiodglib_get_prop_enum(G_OBJECT(self), "event-clock");
331 }
332
333 GpiodglibLineInfo *_gpiodglib_line_info_new(struct gpiod_line_info *handle)
334 {
335 GpiodglibLineInfo *info;
336
337 info = GPIODGLIB_LINE_INFO_OBJ(g_object_new(GPIODGLIB_LINE_INFO_TYPE,
338 NULL));
339 info->handle = handle;
340
341 return info;
342 }