From 034d37aa87fba63475b9c5cd6f00d800ab10ed46 Mon Sep 17 00:00:00 2001 From: batt Date: Fri, 1 May 2009 12:13:10 +0000 Subject: [PATCH] Add KFile interface over a fifo buffer. git-svn-id: https://src.develer.com/svnoss/bertos/trunk@2685 38d2e660-2303-0410-9eaa-f027e97ec537 --- bertos/struct/kfile_fifo.c | 76 ++++++++++++++++ bertos/struct/kfile_fifo.h | 70 +++++++++++++++ bertos/struct/kfile_fifo_test.c | 148 ++++++++++++++++++++++++++++++++ test/run_tests.sh | 2 +- 4 files changed, 295 insertions(+), 1 deletion(-) create mode 100644 bertos/struct/kfile_fifo.c create mode 100644 bertos/struct/kfile_fifo.h create mode 100644 bertos/struct/kfile_fifo_test.c diff --git a/bertos/struct/kfile_fifo.c b/bertos/struct/kfile_fifo.c new file mode 100644 index 00000000..d09ae1e7 --- /dev/null +++ b/bertos/struct/kfile_fifo.c @@ -0,0 +1,76 @@ +/** + * \file + * + * + * \brief KFile interface over a FIFO buffer. + * + * \version $Id: cfg_adc.h 2348 2009-02-16 13:43:44Z duplo $ + * \author Francesco Sacchi + */ + +#include "kfile_fifo.h" +#include "fifobuf.h" + +#include + +#include + +static size_t kfilefifo_read(struct KFile *_fd, void *_buf, size_t size) +{ + KFileFifo *fd = KFILEFIFO_CAST(_fd); + uint8_t *buf = (uint8_t *)_buf; + + while (size-- && !fifo_isempty_locked(fd->fifo)) + *buf++ = fifo_pop_locked(fd->fifo); + + return (void *)buf - _buf; +} + +static size_t kfilefifo_write(struct KFile *_fd, const void *_buf, size_t size) +{ + KFileFifo *fd = KFILEFIFO_CAST(_fd); + const uint8_t *buf = (const uint8_t *)_buf; + + while (size-- && !fifo_isfull_locked(fd->fifo)) + fifo_push_locked(fd->fifo, *buf++); + + return (void *)buf - _buf; +} + +void kfilefifo_init(KFileFifo *kf, FIFOBuffer *fifo) +{ + memset(kf, 0, sizeof(*kf)); + + kf->fifo = fifo; + kf->fd.read = kfilefifo_read; + kf->fd.write = kfilefifo_write; + DB(kf->fd._type = KFT_KFILEFIFO); +} diff --git a/bertos/struct/kfile_fifo.h b/bertos/struct/kfile_fifo.h new file mode 100644 index 00000000..5d9ba5c2 --- /dev/null +++ b/bertos/struct/kfile_fifo.h @@ -0,0 +1,70 @@ +/** + * \file + * + * + * \brief KFile interface over a FIFO buffer. + * + * \version $Id: cfg_adc.h 2348 2009-02-16 13:43:44Z duplo $ + * \author Francesco Sacchi + * + * $WIZ$ module_name = "kfilefifo" + * $WIZ$ module_depends = "kfile" + */ + +#ifndef STRUCT_KFILE_FIFO +#define STRUCT_KFILE_FIFO + +#include "fifobuf.h" +#include + +typedef struct KFileFifo +{ + KFile fd; + FIFOBuffer *fifo; +} KFileFifo; + +/** + * ID for KFile FIFO. + */ +#define KFT_KFILEFIFO MAKE_ID('F', 'I', 'F', '0') + +/** + * Convert + ASSERT from generic KFile to KFileFifo. + */ +INLINE KFileFifo * KFILEFIFO_CAST(KFile *fd) +{ + ASSERT(fd->_type == KFT_KFILEFIFO); + return (KFileFifo *)fd; +} + +void kfilefifo_init(KFileFifo *kf, FIFOBuffer *fifo); + +#endif /* STRUCT_KFILE_FIFO */ diff --git a/bertos/struct/kfile_fifo_test.c b/bertos/struct/kfile_fifo_test.c new file mode 100644 index 00000000..8c2d04a3 --- /dev/null +++ b/bertos/struct/kfile_fifo_test.c @@ -0,0 +1,148 @@ +/** + * \file + * + * + * \brief FIFO and KFileFifo test. + * + * \version $Id: kfilefifo_test.c 1962 2008-12-02 15:37:17Z batt $ + * \author Bernie Innocenti + */ + + +#include +#include + +#include +#include +#include + + +int kfilefifo_testSetup(void) +{ + kdbg_init(); + return 0; +} + +int kfilefifo_testRun(void) +{ + #define FIFOBUF_LEN 256 + + uint8_t buf[FIFOBUF_LEN]; + + FIFOBuffer fifo; + fifo_init(&fifo, buf, sizeof(buf)); + + ASSERT(fifo_isempty(&fifo)); + ASSERT(!fifo_isfull(&fifo)); + + for (int i = 0; i < FIFOBUF_LEN - 1; i++) + { + ASSERT(!fifo_isfull(&fifo)); + fifo_push(&fifo, i); + } + + ASSERT(fifo_isfull(&fifo)); + + for (int i = 0; i < FIFOBUF_LEN - 1; i++) + { + ASSERT(!fifo_isempty(&fifo)); + ASSERT(fifo_pop(&fifo) == i); + } + + ASSERT(fifo_isempty(&fifo)); + + for (int i = 0; i < FIFOBUF_LEN - 1; i++) + { + ASSERT(!fifo_isfull(&fifo)); + fifo_push(&fifo, i); + } + ASSERT(fifo_isfull(&fifo)); + fifo_flush(&fifo); + ASSERT(!fifo_isfull(&fifo)); + ASSERT(fifo_isempty(&fifo)); + + KFileFifo kfifo; + kfilefifo_init(&kfifo, &fifo); + + for (int i = 0; i < FIFOBUF_LEN - 1; i++) + { + ASSERT(!fifo_isfull(&fifo)); + fifo_push(&fifo, i); + } + + for (int i = 0; i < FIFOBUF_LEN - 1; i++) + ASSERT(kfile_getc(&kfifo.fd) == i); + + ASSERT(kfile_getc(&kfifo.fd) == EOF); + ASSERT(fifo_isempty(&fifo)); + + for (int i = 0; i < FIFOBUF_LEN - 1; i++) + ASSERT(kfile_putc(i, &kfifo.fd) == i); + + ASSERT(fifo_isfull(&fifo)); + + for (int i = 0; i < FIFOBUF_LEN - 1; i++) + { + ASSERT(!fifo_isempty(&fifo)); + ASSERT(fifo_pop(&fifo) == i); + } + + ASSERT(fifo_isempty(&fifo)); + + for (int i = 0; i < FIFOBUF_LEN - 1; i++) + ASSERT(kfile_putc(i, &kfifo.fd) == i); + + ASSERT(fifo_isfull(&fifo)); + ASSERT(kfile_putc('a', &kfifo.fd) == EOF); + + fifo_flush(&fifo); + ASSERT(!fifo_isfull(&fifo)); + ASSERT(fifo_isempty(&fifo)); + ASSERT(kfile_getc(&kfifo.fd) == EOF); + + ASSERT(kfile_write(&kfifo.fd, "hello world", 11) == 11); + ASSERT(kfile_write(&kfifo.fd, "hello world", FIFOBUF_LEN) == FIFOBUF_LEN - 1 - 11); + + uint8_t test_buf[FIFOBUF_LEN]; + ASSERT(kfile_read(&kfifo.fd, test_buf, FIFOBUF_LEN + 20) == FIFOBUF_LEN - 1); + + ASSERT(!fifo_isfull(&fifo)); + ASSERT(fifo_isempty(&fifo)); + ASSERT(kfile_getc(&kfifo.fd) == EOF); + return 0; +} + +int kfilefifo_testTearDown(void) +{ + return 0; +} + +TEST_MAIN(kfilefifo); diff --git a/test/run_tests.sh b/test/run_tests.sh index 90de1f04..633525d2 100755 --- a/test/run_tests.sh +++ b/test/run_tests.sh @@ -27,7 +27,7 @@ TESTS=${TESTS:-`find . \ -o -name "*_test.c" -print` } TESTOUT="testout" -SRC_LIST="bertos/algo/ramp.c bertos/drv/kdebug.c bertos/drv/timer.c bertos/fs/battfs.c bertos/kern/coop.c bertos/kern/idle.c bertos/kern/kfile.c bertos/kern/monitor.c bertos/kern/proc.c bertos/kern/signal.c bertos/kern/sem.c bertos/mware/event.c bertos/mware/formatwr.c bertos/mware/hex.c bertos/mware/sprintf.c bertos/os/hptime.c bertos/emul/switch_ctx_emul.S" +SRC_LIST="bertos/algo/ramp.c bertos/drv/kdebug.c bertos/drv/timer.c bertos/fs/battfs.c bertos/kern/coop.c bertos/kern/idle.c bertos/kern/kfile.c bertos/kern/monitor.c bertos/kern/proc.c bertos/kern/signal.c bertos/kern/sem.c bertos/mware/event.c bertos/mware/formatwr.c bertos/mware/hex.c bertos/mware/sprintf.c bertos/os/hptime.c bertos/struct/kfile_fifo.c bertos/emul/switch_ctx_emul.S" buildout='/dev/null' runout='/dev/null' -- 2.25.1