File: request-config.c

package info (click to toggle)
libgpiod 2.2.3-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 6,172 kB
  • sloc: ansic: 26,661; sh: 8,090; cpp: 4,944; python: 2,426; makefile: 811; xml: 49
file content (170 lines) | stat: -rw-r--r-- 4,689 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
// SPDX-License-Identifier: LGPL-2.1-or-later
// SPDX-FileCopyrightText: 2023-2024 Bartosz Golaszewski <bartosz.golaszewski@linaro.org>

#include <gio/gio.h>
#include <stdarg.h>

#include "internal.h"

/**
 * GpiodglibRequestConfig:
 *
 * Request config objects are used to pass a set of options to the kernel at
 * the time of the line request.
 */
struct _GpiodglibRequestConfig {
	GObject parent_instance;
	struct gpiod_request_config *handle;
};

typedef enum {
	GPIODGLIB_REQUEST_CONFIG_PROP_CONSUMER = 1,
	GPIODGLIB_REQUEST_CONFIG_PROP_EVENT_BUFFER_SIZE,
} GpiodglibRequestConfigProp;

G_DEFINE_TYPE(GpiodglibRequestConfig, gpiodglib_request_config, G_TYPE_OBJECT);

static void gpiodglib_request_config_get_property(GObject *obj, guint prop_id,
						  GValue *val,
						  GParamSpec *pspec)
{
	GpiodglibRequestConfig *self = GPIODGLIB_REQUEST_CONFIG_OBJ(obj);

	switch ((GpiodglibRequestConfigProp)prop_id) {
	case GPIODGLIB_REQUEST_CONFIG_PROP_CONSUMER:
		g_value_set_string(val,
			gpiod_request_config_get_consumer(self->handle));
		break;
	case GPIODGLIB_REQUEST_CONFIG_PROP_EVENT_BUFFER_SIZE:
		g_value_set_uint(val,
			gpiod_request_config_get_event_buffer_size(
				self->handle));
		break;
	default:
		G_OBJECT_WARN_INVALID_PROPERTY_ID(obj, prop_id, pspec);
	}
}

static void gpiodglib_request_config_set_property(GObject *obj, guint prop_id,
						  const GValue *val,
						  GParamSpec *pspec)
{
	GpiodglibRequestConfig *self = GPIODGLIB_REQUEST_CONFIG_OBJ(obj);

	switch ((GpiodglibRequestConfigProp)prop_id) {
	case GPIODGLIB_REQUEST_CONFIG_PROP_CONSUMER:
		gpiod_request_config_set_consumer(self->handle,
						  g_value_get_string(val));
		break;
	case GPIODGLIB_REQUEST_CONFIG_PROP_EVENT_BUFFER_SIZE:
		gpiod_request_config_set_event_buffer_size(self->handle,
							g_value_get_uint(val));
		break;
	default:
		G_OBJECT_WARN_INVALID_PROPERTY_ID(obj, prop_id, pspec);
	}
}

static void gpiodglib_request_config_finalize(GObject *obj)
{
	GpiodglibRequestConfig *self = GPIODGLIB_REQUEST_CONFIG_OBJ(obj);

	g_clear_pointer(&self->handle, gpiod_request_config_free);

	G_OBJECT_CLASS(gpiodglib_request_config_parent_class)->finalize(obj);
}

static void gpiodglib_request_config_class_init(
			GpiodglibRequestConfigClass *request_config_class)
{
	GObjectClass *class = G_OBJECT_CLASS(request_config_class);

	class->set_property = gpiodglib_request_config_set_property;
	class->get_property = gpiodglib_request_config_get_property;
	class->finalize = gpiodglib_request_config_finalize;

	/**
	 * GpiodglibRequestConfig:consumer:
	 *
	 * Name of the request consumer.
	 */
	g_object_class_install_property(class,
					GPIODGLIB_REQUEST_CONFIG_PROP_CONSUMER,
		g_param_spec_string("consumer", "Consumer",
			"Name of the request consumer.",
			NULL, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));

	/**
	 * GpiodglibRequestConfig:event-buffer-size:
	 *
	 * Size of the kernel event buffer size of the request.
	 */
	g_object_class_install_property(class,
				GPIODGLIB_REQUEST_CONFIG_PROP_EVENT_BUFFER_SIZE,
		g_param_spec_uint("event-buffer-size", "Event Buffer Size",
			"Size of the kernel event buffer size of the request.",
			0, G_MAXUINT, 64,
			G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
}

static void gpiodglib_request_config_init(GpiodglibRequestConfig *self)
{
	self->handle = gpiod_request_config_new();
	if (!self->handle)
		/* The only possible error is ENOMEM. */
		g_error("Failed to allocate memory for the request-config object.");
}

GpiodglibRequestConfig *
gpiodglib_request_config_new(const gchar *first_prop, ...)
{
	GpiodglibRequestConfig *settings;
	va_list va;

	va_start(va, first_prop);
	settings = GPIODGLIB_REQUEST_CONFIG_OBJ(
			g_object_new_valist(GPIODGLIB_REQUEST_CONFIG_TYPE,
					    first_prop, va));
	va_end(va);

	return settings;
}

void gpiodglib_request_config_set_consumer(GpiodglibRequestConfig *self,
					   const gchar *consumer)
{
	g_assert(self);

	_gpiodglib_set_prop_string(G_OBJECT(self), "consumer", consumer);
}

gchar *gpiodglib_request_config_dup_consumer(GpiodglibRequestConfig *self)
{
	g_assert(self);

	return _gpiodglib_dup_prop_string(G_OBJECT(self), "consumer");
}

void
gpiodglib_request_config_set_event_buffer_size(GpiodglibRequestConfig *self,
					       guint event_buffer_size)
{
	g_assert(self);

	_gpiodglib_set_prop_uint(G_OBJECT(self), "event-buffer-size",
				 event_buffer_size);
}

guint
gpiodglib_request_config_get_event_buffer_size(GpiodglibRequestConfig *self)
{
	g_assert(self);

	return _gpiodglib_get_prop_uint(G_OBJECT(self), "event-buffer-size");
}

struct gpiod_request_config *
_gpiodglib_request_config_get_handle(GpiodglibRequestConfig *req_cfg)
{
	return req_cfg->handle;
}