-
Notifications
You must be signed in to change notification settings - Fork 1
/
fbuf.c
472 lines (370 loc) · 11.2 KB
/
fbuf.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
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
/*
* Buffers for frames of data.
*/
#include "defines.h"
#include "fbuf.h"
#include <avr/signal.h>
#include <avr/pgmspace.h>
#include <string.h>
#include "config.h"
#include "kernel/kernel.h"
/***************************************************************
* Storage for packet buffer chains
* For each buffer slot we have
* - reference count.
* - Length of content in bytes
* - Index of next buffer in chain (NILPTR if this is the last)
* - Storage for actual content
*
***************************************************************/
uint8_t _fbuf_refcnt[FBUF_SLOTS];
uint8_t _fbuf_length[FBUF_SLOTS];
uint8_t _fbuf_next [FBUF_SLOTS];
char _fbuf_buf [FBUF_SLOTS][FBUF_SLOTSIZE];
static uint8_t _free_slots = FBUF_SLOTS;
static void(*memFullError)(void) = NULL;
static uint8_t _split(uint8_t islot, uint8_t pos);
uint8_t fbuf_freeSlots()
{ return _free_slots; }
/*
* Note: We assume that FBUF objects are not shared between
* ISRs/threads.
*
* We also assume that FBUF objects are not accessed from interrupt handlers.
* That may change later. In that case, check if we need to protect
* parts of code by disabling interrupts. slots may be shared between
* FBUF objects but it is problematic and this should be tested and
* analysed more. We should disallow writing to a FBUF that contains
* shared slots.
*/
void fbuf_errorHandler( void(*f)(void) )
{ memFullError = f; }
/******************************************************
Internal: Allocate a new buffer slot
******************************************************/
static uint8_t _fbuf_newslot ()
{
register uint8_t i;
for (i=0; i<FBUF_SLOTS; i++)
if (_fbuf_refcnt[i] == 0)
{
_fbuf_refcnt[i] = 1;
_fbuf_length[i] = 0;
_fbuf_next[i] = NILPTR;
_free_slots--;
return i;
}
return NILPTR;
}
/*******************************************************
initialise a buffer chain
*******************************************************/
void fbuf_new (FBUF* bb)
{
bb->head = bb->wslot = bb->rslot = _fbuf_newslot();
bb->rpos = 0;
bb->length = 0;
}
/*******************************************************
dispose the content of a buffer chain
*******************************************************/
void fbuf_release(FBUF* bb)
{
register uint8_t b = bb->head;
while (b != NILPTR)
{
if (_fbuf_refcnt[b] > 0) {
_fbuf_refcnt[b]--;
if (_fbuf_refcnt[b] == 0)
_free_slots++;
}
b = _fbuf_next[b];
}
bb->head = bb->wslot = bb->rslot = NILPTR;
bb->rpos = bb->length = 0;
}
/*******************************************************
* Create a new reference to a buffer chain
*******************************************************/
FBUF fbuf_newRef(FBUF* bb)
{
FBUF newb;
register uint8_t b = bb->head;
while (b != NILPTR)
{
_fbuf_refcnt[b]++;
b = _fbuf_next[b];
}
newb.head = bb->head;
newb.length = bb->length;
fbuf_reset(&newb);
newb.wslot = bb->wslot;
return newb;
}
/*******************************************************
reset or set read position of a buffer chain
*******************************************************/
void fbuf_reset(FBUF* b)
{
b->rslot = b->head;
b->rpos = 0;
}
void fbuf_rseek(FBUF* b, const uint8_t pos)
{
register uint8_t i=pos;
if (pos > b->length)
return;
fbuf_reset(b);
while (i > _fbuf_length[b->rslot]) {
i -= _fbuf_length[b->rslot];
b->rslot = _fbuf_next[b->rslot];
}
b->rpos = i;
}
/*******************************************************
Add a byte to a buffer chain.
Add new slots to it if necessary
*******************************************************/
void fbuf_putChar (FBUF* b, const char c)
{
/* if wslot is NIL it means that writing is not allowed */
if (b->wslot == NILPTR)
return;
register uint8_t pos = _fbuf_length[b->wslot];
if (pos == FBUF_SLOTSIZE || b->head == NILPTR)
{
pos = 0;
register uint8_t newslot = _fbuf_newslot();
if (newslot == NILPTR) {
if (memFullError != NULL)
(*memFullError)();
else
return;
}
b->wslot = _fbuf_next[b->wslot] = newslot;
if (b->head == NILPTR)
b->rslot = b->head = newslot;
}
_fbuf_buf[b->wslot] [pos] = c;
_fbuf_length[b->wslot]++;
b->length++;
}
/*******************************************************
* Insert a buffer chain x into another buffer chain b
* at position pos
*
* Note: After calling this, writing into the buffers
* are disallowed.
*******************************************************/
void fbuf_insert(FBUF* b, FBUF* x, uint8_t pos)
{
register uint8_t islot = b->head;
while (pos >= FBUF_SLOTSIZE) {
pos -= _fbuf_length[islot];
if (pos > 0)
islot = _fbuf_next[islot];
}
/* Find last slot in x chain and increment reference count*/
register uint8_t xlast = x->head;
_fbuf_refcnt[xlast]++;
while (_fbuf_next[xlast] != NILPTR) {
xlast = _fbuf_next[xlast];
_fbuf_refcnt[xlast]++;
}
/* Insert x chain after islot */
_fbuf_next[xlast] = _split(islot, pos);
_fbuf_next[islot] = x->head;
b->wslot = x->wslot = NILPTR; // Disallow writing
b->length += x->length;
}
/*****************************************************
* Connect b buffer to x, at position pos. In practice
* this mean that we get two buffers, with different
* headers but with a shared last part.
*
* Note: After calling this, writing into the buffers
* is not allowed.
*****************************************************/
void fbuf_connect(FBUF* b, FBUF* x, uint8_t pos)
{
register uint8_t islot = x->head;
register uint8_t p = pos;
while (p >= FBUF_SLOTSIZE) {
p -= _fbuf_length[islot];
if (p > 0)
islot = _fbuf_next[islot];
}
/* Find last slot of b and connect it to rest of x */
register uint8_t xlast = b->head;
while (_fbuf_next[xlast] != NILPTR)
xlast = _fbuf_next[xlast];
_fbuf_next[xlast] = _split(islot, p);
/* Increment reference count of rest of x */
while (_fbuf_next[xlast] != NILPTR) {
xlast = _fbuf_next[xlast];
_fbuf_refcnt[xlast]++;
}
b->wslot = x->wslot = NILPTR; // Disallow writing
b->length = b->length + x->length - pos;
}
static uint8_t _split(uint8_t islot, uint8_t pos)
{
if (pos == 0)
return _fbuf_next[islot];
register uint8_t newslot = _fbuf_newslot();
_fbuf_next[newslot] = _fbuf_next[islot];
_fbuf_next[islot] = newslot;
_fbuf_refcnt[newslot] = _fbuf_refcnt[islot];
/* Copy last part of slot to newslot */
for (uint8_t i = 0; i<_fbuf_length[islot]-pos; i++)
_fbuf_buf[newslot][i] = _fbuf_buf[islot][pos+i];
_fbuf_length[newslot] = _fbuf_length[islot] - pos;
_fbuf_length[islot] = pos;
return newslot;
}
/*******************************************************
Write a string to a buffer chain
*******************************************************/
void fbuf_write (FBUF* b, const char* data, const uint8_t size)
{
register uint8_t i;
for (i=0; i<size; i++)
fbuf_putChar(b, data[i]);
}
/*******************************************************
Write a null terminated string to a buffer chain
*******************************************************/
void fbuf_putstr(FBUF* b, const char *data)
{
while (*data != 0)
fbuf_putChar(b, *(data++));
}
/*******************************************************
Write a null terminated string in program memory
to a buffer chain
*******************************************************/
void fbuf_putstr_P(FBUF *b, const char * data)
{
char c;
while ((c = pgm_read_byte(data)) != 0)
{
fbuf_putChar(b, pgm_read_byte(data) );
data++;
}
}
/*******************************************************
Read a byte from a buffer chain.
(this will increment the read-position)
*******************************************************/
char fbuf_getChar(FBUF* b)
{
register char x = _fbuf_buf[b->rslot][b->rpos];
if (b->rpos == _fbuf_length[b->rslot]-1)
{
b->rslot = _fbuf_next[b->rslot];
b->rpos = 0;
}
else
b->rpos++;
return x;
}
/*******************************************************
Read a string of bytes from buffer chain.
(this will add 'size' to the read-position)
*******************************************************/
char* fbuf_read (FBUF* b, uint8_t size, char *buf)
{
register uint8_t n, bb, r=0;
if (b->length < size)
size = b->length;
bb = b->head;
while ( bb != NILPTR )
{
n = size - r;
if (n > _fbuf_length[bb])
n = _fbuf_length[bb];
strncpy(buf+r, _fbuf_buf[bb], n);
r += n;
bb = _fbuf_next[bb];
if (r >= size )
break;
}
buf[r] = '\0';
return buf;
}
/*******************************************************
Remove the last byte of a buffer chain.
*******************************************************/
void fbuf_removeLast(FBUF* x)
{
register uint8_t xlast = x->head;
register uint8_t prev = xlast;
while (_fbuf_next[xlast] != NILPTR) {
xlast = _fbuf_next[xlast];
if (_fbuf_next[xlast] != NILPTR)
prev = xlast;
}
_fbuf_length[xlast]--;
if (_fbuf_length[xlast] == 0 && prev != xlast) {
_fbuf_refcnt[xlast]--;
if (_fbuf_refcnt[xlast] == 0)
_free_slots++;
_fbuf_next[prev] = NILPTR;
}
}
/*
* FBQ: QUEUE OF BUFFER-CHAINS
*/
/*******************************************************
initialise a queue
*******************************************************/
void _fbq_init(FBQ* q, FBUF* buf, const uint16_t sz)
{
q->size = sz;
q->buf = buf;
q->index = 0;
sem_init(&q->length, 0);
sem_init(&q->capacity, sz);
}
void fbq_clear(FBQ* q)
{
register uint16_t i;
for (i = q->index; i < q->index + q->length.cnt; i++)
fbuf_release(&q->buf[(uint8_t) (i % q->size)]);
sem_set(&q->length, 0);
sem_set(&q->capacity, q->size);
q->index = 0;
}
/********************************************************
put a buffer chain into the queue
********************************************************/
void fbq_put(FBQ* q, FBUF b)
{
sem_down(&q->capacity);
register uint16_t i = q->index + q->length.cnt;
if (i >= q->size)
i -= q->size;
q->buf[(uint8_t) i] = b;
sem_up(&q->length);
}
/*********************************************************
get a buffer chain from the queue (block if empty)
*********************************************************/
FBUF fbq_get(FBQ* q)
{
sem_down(&q->length);
register uint8_t i = q->index;
if (++q->index >= q->size)
q->index = 0;
sem_up(&q->capacity);
return q->buf[i];
}
/**********************************************************
* put an empty buffer onto the queue.
**********************************************************/
void fbq_signal(FBQ* q)
{
FBUF b;
fbuf_new(&b);
fbq_put(q, b);
}