File: edge-event.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 (212 lines) | stat: -rw-r--r-- 4,264 bytes parent folder | download | duplicates (3)
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
// SPDX-License-Identifier: LGPL-2.1-or-later
// SPDX-FileCopyrightText: 2021 Bartosz Golaszewski <brgl@bgdev.pl>

#include <assert.h>
#include <errno.h>
#include <gpiod.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#include "internal.h"

/* As defined in the kernel. */
#define EVENT_BUFFER_MAX_CAPACITY (GPIO_V2_LINES_MAX * 16)

struct gpiod_edge_event {
	enum gpiod_edge_event_type event_type;
	uint64_t timestamp;
	unsigned int line_offset;
	unsigned long global_seqno;
	unsigned long line_seqno;
};

struct gpiod_edge_event_buffer {
	size_t capacity;
	size_t num_events;
	struct gpiod_edge_event *events;
	struct gpio_v2_line_event *event_data;
};

GPIOD_API void gpiod_edge_event_free(struct gpiod_edge_event *event)
{
	free(event);
}

GPIOD_API struct gpiod_edge_event *
gpiod_edge_event_copy(struct gpiod_edge_event *event)
{
	struct gpiod_edge_event *copy;

	assert(event);

	copy = malloc(sizeof(*event));
	if (!copy)
		return NULL;

	memcpy(copy, event, sizeof(*event));

	return copy;
}

GPIOD_API enum gpiod_edge_event_type
gpiod_edge_event_get_event_type(struct gpiod_edge_event *event)
{
	assert(event);

	return event->event_type;
}

GPIOD_API uint64_t
gpiod_edge_event_get_timestamp_ns(struct gpiod_edge_event *event)
{
	assert(event);

	return event->timestamp;
}

GPIOD_API unsigned int
gpiod_edge_event_get_line_offset(struct gpiod_edge_event *event)
{
	assert(event);

	return event->line_offset;
}

GPIOD_API unsigned long
gpiod_edge_event_get_global_seqno(struct gpiod_edge_event *event)
{
	assert(event);

	return event->global_seqno;
}

GPIOD_API unsigned long
gpiod_edge_event_get_line_seqno(struct gpiod_edge_event *event)
{
	assert(event);

	return event->line_seqno;
}

GPIOD_API struct gpiod_edge_event_buffer *
gpiod_edge_event_buffer_new(size_t capacity)
{
	struct gpiod_edge_event_buffer *buf;

	if (capacity == 0)
		capacity = 64;
	if (capacity > EVENT_BUFFER_MAX_CAPACITY)
		capacity = EVENT_BUFFER_MAX_CAPACITY;

	buf = malloc(sizeof(*buf));
	if (!buf)
		return NULL;

	memset(buf, 0, sizeof(*buf));
	buf->capacity = capacity;

	buf->events = calloc(capacity, sizeof(*buf->events));
	if (!buf->events) {
		free(buf);
		return NULL;
	}

	buf->event_data = calloc(capacity, sizeof(*buf->event_data));
	if (!buf->event_data) {
		free(buf->events);
		free(buf);
		return NULL;
	}

	return buf;
}

GPIOD_API size_t
gpiod_edge_event_buffer_get_capacity(struct gpiod_edge_event_buffer *buffer)
{
	assert(buffer);

	return buffer->capacity;
}

GPIOD_API void
gpiod_edge_event_buffer_free(struct gpiod_edge_event_buffer *buffer)
{
	if (!buffer)
		return;

	free(buffer->events);
	free(buffer->event_data);
	free(buffer);
}

GPIOD_API struct gpiod_edge_event *
gpiod_edge_event_buffer_get_event(struct gpiod_edge_event_buffer *buffer,
				  unsigned long index)
{
	assert(buffer);

	if (index >= buffer->num_events) {
		errno = EINVAL;
		return NULL;
	}

	return &buffer->events[index];
}

GPIOD_API size_t
gpiod_edge_event_buffer_get_num_events(struct gpiod_edge_event_buffer *buffer)
{
	assert(buffer);

	return buffer->num_events;
}

int gpiod_edge_event_buffer_read_fd(int fd,
				    struct gpiod_edge_event_buffer *buffer,
				    size_t max_events)
{
	struct gpio_v2_line_event *curr;
	struct gpiod_edge_event *event;
	size_t i;
	ssize_t rd;

	if (!buffer) {
		errno = EINVAL;
		return -1;
	}

	memset(buffer->event_data, 0,
	       sizeof(*buffer->event_data) * buffer->capacity);
	memset(buffer->events, 0, sizeof(*buffer->events) * buffer->capacity);

	if (max_events > buffer->capacity)
		max_events = buffer->capacity;

	rd = read(fd, buffer->event_data,
		  max_events * sizeof(*buffer->event_data));
	if (rd < 0) {
		return -1;
	} else if ((unsigned int)rd < sizeof(*buffer->event_data)) {
		errno = EIO;
		return -1;
	}

	buffer->num_events = rd / sizeof(*buffer->event_data);

	for (i = 0; i < buffer->num_events; i++) {
		curr = &buffer->event_data[i];
		event = &buffer->events[i];

		event->line_offset = curr->offset;
		event->event_type = curr->id == GPIO_V2_LINE_EVENT_RISING_EDGE ?
					    GPIOD_EDGE_EVENT_RISING_EDGE :
					    GPIOD_EDGE_EVENT_FALLING_EDGE;
		event->timestamp = curr->timestamp_ns;
		event->global_seqno = curr->seqno;
		event->line_seqno = curr->line_seqno;
	}

	return i;
}