From: batt <batt@38d2e660-2303-0410-9eaa-f027e97ec537>
Date: Fri, 1 May 2009 12:13:10 +0000 (+0000)
Subject: Add KFile interface over a fifo buffer.
X-Git-Tag: 2.2.0~280
X-Git-Url: https://codewiz.org/gitweb?a=commitdiff_plain;h=034d37aa87fba63475b9c5cd6f00d800ab10ed46;p=bertos.git

Add KFile interface over a fifo buffer.

git-svn-id: https://src.develer.com/svnoss/bertos/trunk@2685 38d2e660-2303-0410-9eaa-f027e97ec537
---

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
+ * <!--
+ * This file is part of BeRTOS.
+ *
+ * Bertos is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
+ *
+ * As a special exception, you may use this file as part of a free software
+ * library without restriction.  Specifically, if other files instantiate
+ * templates or use macros or inline functions from this file, or you compile
+ * this file and link it with other files to produce an executable, this
+ * file does not by itself cause the resulting executable to be covered by
+ * the GNU General Public License.  This exception does not however
+ * invalidate any other reasons why the executable file might be covered by
+ * the GNU General Public License.
+ *
+ * Copyright 2009 Develer S.r.l. (http://www.develer.com/)
+ *
+ * -->
+ *
+ * \brief KFile interface over a FIFO buffer.
+ *
+ * \version $Id: cfg_adc.h 2348 2009-02-16 13:43:44Z duplo $
+ * \author Francesco Sacchi <asterix@develer.com>
+ */
+
+#include "kfile_fifo.h"
+#include "fifobuf.h"
+
+#include <kern/kfile.h>
+
+#include <string.h>
+
+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
+ * <!--
+ * This file is part of BeRTOS.
+ *
+ * Bertos is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
+ *
+ * As a special exception, you may use this file as part of a free software
+ * library without restriction.  Specifically, if other files instantiate
+ * templates or use macros or inline functions from this file, or you compile
+ * this file and link it with other files to produce an executable, this
+ * file does not by itself cause the resulting executable to be covered by
+ * the GNU General Public License.  This exception does not however
+ * invalidate any other reasons why the executable file might be covered by
+ * the GNU General Public License.
+ *
+ * Copyright 2009 Develer S.r.l. (http://www.develer.com/)
+ *
+ * -->
+ *
+ * \brief KFile interface over a FIFO buffer.
+ *
+ * \version $Id: cfg_adc.h 2348 2009-02-16 13:43:44Z duplo $
+ * \author Francesco Sacchi <asterix@develer.com>
+ *
+ * $WIZ$ module_name = "kfilefifo"
+ * $WIZ$ module_depends = "kfile"
+ */
+
+#ifndef STRUCT_KFILE_FIFO
+#define STRUCT_KFILE_FIFO
+
+#include "fifobuf.h"
+#include <kern/kfile.h>
+
+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
+ * <!--
+ * This file is part of BeRTOS.
+ *
+ * Bertos is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
+ *
+ * As a special exception, you may use this file as part of a free software
+ * library without restriction.  Specifically, if other files instantiate
+ * templates or use macros or inline functions from this file, or you compile
+ * this file and link it with other files to produce an executable, this
+ * file does not by itself cause the resulting executable to be covered by
+ * the GNU General Public License.  This exception does not however
+ * invalidate any other reasons why the executable file might be covered by
+ * the GNU General Public License.
+ *
+ * Copyright 2009 Develer S.r.l. (http://www.develer.com/)
+ *
+ * -->
+ *
+ * \brief FIFO and KFileFifo test.
+ *
+ * \version $Id: kfilefifo_test.c 1962 2008-12-02 15:37:17Z batt $
+ * \author Bernie Innocenti <bernie@codewiz.org>
+ */
+
+
+#include <struct/fifobuf.h>
+#include <struct/kfile_fifo.h>
+
+#include <cfg/compiler.h>
+#include <cfg/test.h>
+#include <cfg/debug.h>
+
+
+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'