blob: b8cd5c6c4b1589cc2cbf1ae3eb5e3add2011d86a [file] [log] [blame]
Christina Quast4db82e02015-04-11 18:14:41 +02001#include "ringbuffer.h"
2#include "trace.h"
Harald Welte7abdb512016-03-03 17:48:32 +01003#include "utils.h"
Christina Quast542f9182015-04-11 09:31:15 +02004
Harald Welte05cc7f62018-07-04 04:39:40 +02005/* WARNINGI: Since console output is internally using this ringbuffer to implement
6 * buffered writes, we cannot use any TRACE_*() or printf() style functions here,
7 * as it would create infinite recursion! */
8
Harald Welte7dd3dfd2016-03-03 12:32:04 +01009void rbuf_reset(volatile ringbuf * rb)
Christina Quast542f9182015-04-11 09:31:15 +020010{
Harald Welte7abdb512016-03-03 17:48:32 +010011 unsigned long state;
12
13 local_irq_save(state);
Harald Welte7dd3dfd2016-03-03 12:32:04 +010014 rb->ird = 0;
15 rb->iwr = 0;
Harald Welte7abdb512016-03-03 17:48:32 +010016 local_irq_restore(state);
Christina Quast542f9182015-04-11 09:31:15 +020017}
18
Harald Welte7dd3dfd2016-03-03 12:32:04 +010019uint8_t rbuf_read(volatile ringbuf * rb)
Christina Quast542f9182015-04-11 09:31:15 +020020{
Harald Welte7abdb512016-03-03 17:48:32 +010021 unsigned long state;
22 uint8_t val;
23
24 local_irq_save(state);
25 val = rb->buf[rb->ird];
Harald Welte7dd3dfd2016-03-03 12:32:04 +010026 rb->ird = (rb->ird + 1) % RING_BUFLEN;
Harald Welte7abdb512016-03-03 17:48:32 +010027 local_irq_restore(state);
28
Harald Welte7dd3dfd2016-03-03 12:32:04 +010029 return val;
Christina Quast542f9182015-04-11 09:31:15 +020030}
31
Harald Welte7dd3dfd2016-03-03 12:32:04 +010032uint8_t rbuf_peek(volatile ringbuf * rb)
Christina Quast7a7f98c2015-05-02 14:10:13 +020033{
Harald Welte7dd3dfd2016-03-03 12:32:04 +010034 return rb->buf[rb->ird];
Christina Quast7a7f98c2015-05-02 14:10:13 +020035}
36
Harald Welte7dd3dfd2016-03-03 12:32:04 +010037bool rbuf_is_empty(volatile ringbuf * rb)
Christina Quast4db82e02015-04-11 18:14:41 +020038{
Harald Welte7dd3dfd2016-03-03 12:32:04 +010039 return rb->ird == rb->iwr;
Christina Quast4db82e02015-04-11 18:14:41 +020040}
41
Harald Welte7abdb512016-03-03 17:48:32 +010042static bool __rbuf_is_full(volatile ringbuf * rb)
Christina Quast4db82e02015-04-11 18:14:41 +020043{
Harald Welte7dd3dfd2016-03-03 12:32:04 +010044 return rb->ird == (rb->iwr + 1) % RING_BUFLEN;
Christina Quast4db82e02015-04-11 18:14:41 +020045}
Harald Welte7abdb512016-03-03 17:48:32 +010046
47bool rbuf_is_full(volatile ringbuf * rb)
48{
49 unsigned long state;
50 bool rc;
51
52 local_irq_save(state);
53 rc = rb->ird == (rb->iwr + 1) % RING_BUFLEN;
54 local_irq_restore(state);
55
56 return rc;
57}
58
Harald Welte05cc7f62018-07-04 04:39:40 +020059int rbuf_write(volatile ringbuf * rb, uint8_t item)
Harald Welte7abdb512016-03-03 17:48:32 +010060{
61 unsigned long state;
62
63 local_irq_save(state);
64 if (!__rbuf_is_full(rb)) {
65 rb->buf[rb->iwr] = item;
66 rb->iwr = (rb->iwr + 1) % RING_BUFLEN;
67 local_irq_restore(state);
Harald Welte05cc7f62018-07-04 04:39:40 +020068 return 0;
Harald Welte7abdb512016-03-03 17:48:32 +010069 } else {
70 local_irq_restore(state);
Harald Welte05cc7f62018-07-04 04:39:40 +020071 return -1;
Harald Welte7abdb512016-03-03 17:48:32 +010072 }
73}
74
75