diff --git a/sys/include/ringbuffer.h b/sys/include/ringbuffer.h index 7122b95560..1564fc962c 100644 --- a/sys/include/ringbuffer.h +++ b/sys/include/ringbuffer.h @@ -27,8 +27,8 @@ typedef struct ringbuffer { } ringbuffer_t; void ringbuffer_init(ringbuffer_t *restrict rb, char *buffer, unsigned bufsize); -void ringbuffer_add_one(ringbuffer_t *restrict rb, char c); -void ringbuffer_add(ringbuffer_t *restrict rb, const char *buf, unsigned n); +int ringbuffer_add_one(ringbuffer_t *restrict rb, char c); +unsigned ringbuffer_add(ringbuffer_t *restrict rb, const char *buf, unsigned n); int ringbuffer_get_one(ringbuffer_t *restrict rb); unsigned ringbuffer_get(ringbuffer_t *restrict rb, char *buf, unsigned n); diff --git a/sys/lib/ringbuffer.c b/sys/lib/ringbuffer.c index 588a6aeec2..084ae48f24 100644 --- a/sys/lib/ringbuffer.c +++ b/sys/lib/ringbuffer.c @@ -44,19 +44,26 @@ static char get_head(ringbuffer_t *restrict rb) return result; } -void ringbuffer_add(ringbuffer_t *restrict rb, const char *buf, unsigned n) +unsigned ringbuffer_add(ringbuffer_t *restrict rb, const char *buf, unsigned n) { - for (unsigned i = 0; i < n; i++) { - ringbuffer_add_one(rb, buf[i]); + unsigned i; + for (i = 0; i < n; i++) { + if (ringbuffer_full(rb)) { + break; + } + add_tail(rb, buf[i]); } + return i; } -void ringbuffer_add_one(ringbuffer_t *restrict rb, char c) +int ringbuffer_add_one(ringbuffer_t *restrict rb, char c) { + int result = -1; if (ringbuffer_full(rb)) { - get_head(rb); + result = (unsigned char) get_head(rb); } add_tail(rb, c); + return result; } int ringbuffer_get_one(ringbuffer_t *restrict rb)