/
chan.c
375 lines (296 loc) · 8.6 KB
/
chan.c
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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
/**
* @section License
*
* The MIT License (MIT)
*
* Copyright (c) 2014-2017, Erik Moqvist
*
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation
* files (the "Software"), to deal in the Software without
* restriction, including without limitation the rights to use, copy,
* modify, merge, publish, distribute, sublicense, and/or sell copies
* of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
* This file is part of the Simba project.
*/
#include "simba.h"
#define CHAN_LIST_POLLING 0x1
static const struct chan_t null = {
.read = chan_read_null,
.write = chan_write_null,
.write_isr = chan_write_null,
.size = chan_size_null,
.control = chan_control_null
};
int chan_module_init(void)
{
return (0);
}
int chan_init(struct chan_t *self_p,
chan_read_fn_t read,
chan_write_fn_t write,
chan_size_fn_t size)
{
ASSERTN(self_p != NULL, EINVAL);
ASSERTN(read != NULL, EINVAL);
ASSERTN(write != NULL, EINVAL);
ASSERTN(size != NULL, EINVAL);
self_p->read = read;
self_p->write = write;
self_p->write_isr = chan_write_null;
self_p->size = size;
self_p->control = chan_control_null;
self_p->write_filter_cb = NULL;
self_p->write_filter_isr_cb = NULL;
self_p->reader_p = NULL;
self_p->list_p = NULL;
return (0);
}
int chan_set_write_filter_cb(struct chan_t *self_p,
chan_write_filter_fn_t write_filter_cb)
{
self_p->write_filter_cb = write_filter_cb;
return (0);
}
int chan_set_write_filter_isr_cb(struct chan_t *self_p,
chan_write_filter_fn_t write_filter_isr_cb)
{
self_p->write_filter_isr_cb = write_filter_isr_cb;
return (0);
}
int chan_set_write_isr_cb(struct chan_t *self_p,
chan_write_fn_t write_isr_cb)
{
self_p->write_isr = write_isr_cb;
return (0);
}
int chan_list_init(struct chan_list_t *list_p,
void *workspace_p,
size_t size)
{
ASSERTN(list_p != NULL, EINVAL);
ASSERTN(workspace_p != NULL, EINVAL);
ASSERTN(size > 0, EINVAL);
ASSERTN((size / sizeof(list_p->chans_pp[0])) > 0, EINVAL);
list_p->max = (size / sizeof(list_p->chans_pp[0]));
list_p->chans_pp = workspace_p;
list_p->len = 0;
list_p->flags = 0;
return (0);
}
int chan_list_destroy(struct chan_list_t *list_p)
{
ASSERTN(list_p != NULL, EINVAL);
struct chan_t *chan_p = NULL;
size_t i;
sys_lock();
for (i = 0; i < list_p->len; i++) {
chan_p = list_p->chans_pp[i];
chan_p->list_p = NULL;
}
sys_unlock();
return (0);
}
int chan_set_control_cb(struct chan_t *self_p,
chan_control_fn_t control_cb)
{
ASSERTN(self_p != NULL, EINVAL);
if (control_cb == NULL) {
control_cb = chan_control_null;
}
self_p->control = control_cb;
return (0);
}
ssize_t chan_read(void *self_p,
void *buf_p,
size_t size)
{
ASSERTN(self_p != NULL, EINVAL);
ASSERTN(((struct chan_t *)self_p)->read != NULL, EINVAL);
ASSERTN(buf_p != NULL, EINVAL);
ASSERTN(size > 0, EINVAL);
return (((struct chan_t *)self_p)->read(self_p, buf_p, size));
}
ssize_t chan_write(void *v_self_p,
const void *buf_p,
size_t size)
{
ASSERTN(v_self_p != NULL, EINVAL);
ASSERTN(((struct chan_t *)v_self_p)->write != NULL, EINVAL);
ASSERTN(buf_p != NULL, EINVAL);
ASSERTN(size > 0, EINVAL);
struct chan_t *self_p;
self_p = v_self_p;
if (self_p->write_filter_cb != NULL) {
if (self_p->write_filter_cb(self_p, buf_p, size) == 1) {
return (size);
}
}
return (self_p->write(self_p, buf_p, size));
}
int chan_control(void *self_p,
int operation)
{
ASSERTN(self_p != NULL, EINVAL);
return (((struct chan_t *)self_p)->control(self_p, operation));
}
RAM_CODE ssize_t chan_write_isr(void *self_in_p,
const void *buf_p,
size_t size)
{
struct chan_t *self_p;
self_p = self_in_p;
if (self_p->write_filter_isr_cb != NULL) {
if (self_p->write_filter_isr_cb(self_p, buf_p, size) == 1) {
return (size);
}
}
return (self_p->write_isr(self_p, buf_p, size));
}
RAM_CODE size_t chan_size(void *self_p)
{
ASSERTN(self_p != NULL, EINVAL);
return (((struct chan_t *)self_p)->size(self_p));
}
int chan_list_add(struct chan_list_t *list_p, void *chan_p)
{
ASSERTN(list_p != NULL, EINVAL);
ASSERTN(chan_p != NULL, EINVAL);
int res = 0;
sys_lock();
if (list_p->len == list_p->max) {
res = -ENOMEM;
} else {
list_p->chans_pp[list_p->len] = chan_p;
list_p->len++;
((struct chan_t *)chan_p)->list_p = list_p;
}
sys_unlock();
return (res);
}
int chan_list_remove(struct chan_list_t *list_p, void *chan_p)
{
ASSERTN(list_p != NULL, EINVAL);
ASSERTN(chan_p != NULL, EINVAL);
int i;
int res = -1;
sys_lock();
for (i = 0; i < list_p->len; i++) {
if (list_p->chans_pp[i] == chan_p) {
list_p->len--;
list_p->chans_pp[i] = list_p->chans_pp[list_p->len];
((struct chan_t *)chan_p)->list_p = NULL;
res = 0;
break;
}
}
sys_unlock();
return (res);
}
void *chan_list_poll(struct chan_list_t *list_p,
const struct time_t *timeout_p)
{
ASSERTNRN(list_p != NULL, EINVAL);
struct chan_t *chan_p = NULL;
size_t i;
sys_lock();
while (1) {
/* Check if data is available on any channel. */
for (i = 0; i < list_p->len; i++) {
chan_p = list_p->chans_pp[i];
if (chan_p->size(chan_p) > 0) {
goto out;
}
}
/* Add the thread as a reader on all channels.*/
list_p->flags = CHAN_LIST_POLLING;
for (i = 0; i < list_p->len; i++) {
chan_p = list_p->chans_pp[i];
chan_p->reader_p = thrd_self();
}
/* No data was available, wait for data to be written to one
of the channels. */
if (thrd_suspend_isr(timeout_p) == -ETIMEDOUT) {
for (i = 0; i < list_p->len; i++) {
chan_p = list_p->chans_pp[i];
chan_p->reader_p = NULL;
}
list_p->flags = 0;
chan_p = NULL;
goto out;
}
}
out:
sys_unlock();
return (chan_p);
}
void *chan_poll(void *chan_p, const struct time_t *timeout_p)
{
void *res_p;
struct chan_list_t list;
struct chan_t *workspace_p;
chan_list_init(&list, &workspace_p, sizeof(workspace_p));
chan_list_add(&list, chan_p);
res_p = chan_list_poll(&list, timeout_p);
chan_list_remove(&list, chan_p);
return (res_p);
}
void *chan_null(void)
{
return ((void *)&null);
}
ssize_t chan_read_null(void *self_p,
void *buf_p,
size_t size)
{
return (-1);
}
ssize_t chan_write_null(void *self_p,
const void *buf_p,
size_t size)
{
return (size);
}
size_t chan_size_null(void *self_p)
{
/* Set to 1 because read will return immediately. */
return (1);
}
int chan_control_null(void *self_p, int operation)
{
return (0);
}
RAM_CODE int chan_is_polled_isr(struct chan_t *self_p)
{
int i;
int res;
struct chan_t *chan_p;
res = 0;
if (self_p->list_p != NULL) {
if (self_p->list_p->flags & CHAN_LIST_POLLING) {
res = 1;
self_p->list_p->flags = 0;
for (i = 0; i < self_p->list_p->len; i++) {
chan_p = self_p->list_p->chans_pp[i];
if (self_p != chan_p) {
chan_p->reader_p = NULL;
}
}
}
}
return (res);
}