Debian Codegraph @

lib/edge-event.c

raw symbols SCIP
1 // SPDX-License-Identifier: LGPL-2.1-or-later
2 // SPDX-FileCopyrightText: 2021 Bartosz Golaszewski <[email protected]>
3
4 #include <assert.h>
5 #include <errno.h>
6 #include <gpiod.h>
7 #include <stdlib.h>
8 #include <string.h>
9 #include <unistd.h>
10
11 #include "internal.h"
12
13 /* As defined in the kernel. */
14 #define EVENT_BUFFER_MAX_CAPACITY (GPIO_V2_LINES_MAX * 16)
15
16 struct gpiod_edge_event {
17 enum gpiod_edge_event_type event_type;
18 uint64_t timestamp;
19 unsigned int line_offset;
20 unsigned long global_seqno;
21 unsigned long line_seqno;
22 };
23
24 struct gpiod_edge_event_buffer {
25 size_t capacity;
26 size_t num_events;
27 struct gpiod_edge_event *events;
28 struct gpio_v2_line_event *event_data;
29 };
30
31 GPIOD_API void gpiod_edge_event_free(struct gpiod_edge_event *event)
32 {
33 free(event);
34 }
35
36 GPIOD_API struct gpiod_edge_event *
37 gpiod_edge_event_copy(struct gpiod_edge_event *event)
38 {
39 struct gpiod_edge_event *copy;
40
41 assert(event);
42
43 copy = malloc(sizeof(*event));
44 if (!copy)
45 return NULL;
46
47 memcpy(copy, event, sizeof(*event));
48
49 return copy;
50 }
51
52 GPIOD_API enum gpiod_edge_event_type
53 gpiod_edge_event_get_event_type(struct gpiod_edge_event *event)
54 {
55 assert(event);
56
57 return event->event_type;
58 }
59
60 GPIOD_API uint64_t
61 gpiod_edge_event_get_timestamp_ns(struct gpiod_edge_event *event)
62 {
63 assert(event);
64
65 return event->timestamp;
66 }
67
68 GPIOD_API unsigned int
69 gpiod_edge_event_get_line_offset(struct gpiod_edge_event *event)
70 {
71 assert(event);
72
73 return event->line_offset;
74 }
75
76 GPIOD_API unsigned long
77 gpiod_edge_event_get_global_seqno(struct gpiod_edge_event *event)
78 {
79 assert(event);
80
81 return event->global_seqno;
82 }
83
84 GPIOD_API unsigned long
85 gpiod_edge_event_get_line_seqno(struct gpiod_edge_event *event)
86 {
87 assert(event);
88
89 return event->line_seqno;
90 }
91
92 GPIOD_API struct gpiod_edge_event_buffer *
93 gpiod_edge_event_buffer_new(size_t capacity)
94 {
95 struct gpiod_edge_event_buffer *buf;
96
97 if (capacity == 0)
98 capacity = 64;
99 if (capacity > EVENT_BUFFER_MAX_CAPACITY)
100 capacity = EVENT_BUFFER_MAX_CAPACITY;
101
102 buf = malloc(sizeof(*buf));
103 if (!buf)
104 return NULL;
105
106 memset(buf, 0, sizeof(*buf));
107 buf->capacity = capacity;
108
109 buf->events = calloc(capacity, sizeof(*buf->events));
110 if (!buf->events) {
111 free(buf);
112 return NULL;
113 }
114
115 buf->event_data = calloc(capacity, sizeof(*buf->event_data));
116 if (!buf->event_data) {
117 free(buf->events);
118 free(buf);
119 return NULL;
120 }
121
122 return buf;
123 }
124
125 GPIOD_API size_t
126 gpiod_edge_event_buffer_get_capacity(struct gpiod_edge_event_buffer *buffer)
127 {
128 assert(buffer);
129
130 return buffer->capacity;
131 }
132
133 GPIOD_API void
134 gpiod_edge_event_buffer_free(struct gpiod_edge_event_buffer *buffer)
135 {
136 if (!buffer)
137 return;
138
139 free(buffer->events);
140 free(buffer->event_data);
141 free(buffer);
142 }
143
144 GPIOD_API struct gpiod_edge_event *
145 gpiod_edge_event_buffer_get_event(struct gpiod_edge_event_buffer *buffer,
146 unsigned long index)
147 {
148 assert(buffer);
149
150 if (index >= buffer->num_events) {
151 errno = EINVAL;
152 return NULL;
153 }
154
155 return &buffer->events[index];
156 }
157
158 GPIOD_API size_t
159 gpiod_edge_event_buffer_get_num_events(struct gpiod_edge_event_buffer *buffer)
160 {
161 assert(buffer);
162
163 return buffer->num_events;
164 }
165
166 int gpiod_edge_event_buffer_read_fd(int fd,
167 struct gpiod_edge_event_buffer *buffer,
168 size_t max_events)
169 {
170 struct gpio_v2_line_event *curr;
171 struct gpiod_edge_event *event;
172 size_t i;
173 ssize_t rd;
174
175 if (!buffer) {
176 errno = EINVAL;
177 return -1;
178 }
179
180 memset(buffer->event_data, 0,
181 sizeof(*buffer->event_data) * buffer->capacity);
182 memset(buffer->events, 0, sizeof(*buffer->events) * buffer->capacity);
183
184 if (max_events > buffer->capacity)
185 max_events = buffer->capacity;
186
187 if (max_events == 0) {
188 buffer->num_events = 0;
189 return 0;
190 }
191
192 rd = read(fd, buffer->event_data,
193 max_events * sizeof(*buffer->event_data));
194 if (rd < 0) {
195 return -1;
196 } else if ((unsigned int)rd < sizeof(*buffer->event_data)) {
197 errno = EIO;
198 return -1;
199 }
200
201 buffer->num_events = rd / sizeof(*buffer->event_data);
202
203 for (i = 0; i < buffer->num_events; i++) {
204 curr = &buffer->event_data[i];
205 event = &buffer->events[i];
206
207 event->line_offset = curr->offset;
208 event->event_type = curr->id == GPIO_V2_LINE_EVENT_RISING_EDGE ?
209 GPIOD_EDGE_EVENT_RISING_EDGE :
210 GPIOD_EDGE_EVENT_FALLING_EDGE;
211 event->timestamp = curr->timestamp_ns;
212 event->global_seqno = curr->seqno;
213 event->line_seqno = curr->line_seqno;
214 }
215
216 return i;
217 }