From 911d2706a86d326786bfe721dcc3d63aeade7f28 Mon Sep 17 00:00:00 2001 From: batt Date: Fri, 2 Jul 2010 10:01:25 +0000 Subject: [PATCH] Move kfile interface to the io/ directory. git-svn-id: https://src.develer.com/svnoss/bertos/trunk@3985 38d2e660-2303-0410-9eaa-f027e97ec537 --- bertos/cfg/kfile_debug.c | 2 +- bertos/cfg/kfile_debug.h | 2 +- bertos/cpu/arm/drv/flash_at91.c | 2 +- bertos/cpu/arm/drv/flash_at91.h | 2 +- bertos/cpu/arm/drv/spi_dma_at91.c | 2 +- bertos/cpu/arm/drv/spi_dma_at91.h | 2 +- bertos/cpu/avr/drv/flash_avr.c | 2 +- bertos/cpu/avr/drv/flash_avr.h | 2 +- bertos/cpu/cortex-m3/drv/flash_lm3s.c | 2 +- bertos/cpu/cortex-m3/drv/flash_lm3s.h | 2 +- bertos/cpu/cortex-m3/drv/ssi_lm3s.h | 2 +- bertos/drv/dataflash.c | 2 +- bertos/drv/dataflash.h | 2 +- bertos/drv/dataflash_hwtest.c | 2 +- bertos/drv/eeprom.h | 2 +- bertos/drv/flash25.c | 2 +- bertos/drv/flash25.h | 2 +- bertos/drv/flash25_hwtest.c | 2 +- bertos/drv/ft245rl.c | 2 +- bertos/drv/ft245rl.h | 2 +- bertos/drv/mcp41.c | 2 +- bertos/drv/mcp41.h | 2 +- bertos/drv/sd.c | 2 +- bertos/drv/sd.h | 2 +- bertos/drv/ser.h | 2 +- bertos/drv/sipo.c | 2 +- bertos/drv/sipo.h | 2 +- bertos/drv/tlv5618.c | 2 +- bertos/drv/tlv5618.h | 2 +- bertos/drv/tmp123.c | 2 +- bertos/drv/tmp123.h | 2 +- bertos/emul/kfile_posix.h | 2 +- bertos/fs/battfs.h | 2 +- bertos/fs/fat.h | 2 +- bertos/io/kfile.c | 284 ++++++++++++++++++++ bertos/io/kfile.h | 289 ++++++++++++++++++++ bertos/{kern => io}/kfile_test.c | 0 bertos/kern/kfile.c | 287 +------------------- bertos/kern/kfile.h | 292 +-------------------- bertos/mware/ini_reader.h | 2 +- bertos/mware/parser.c | 2 +- bertos/net/afsk.h | 2 +- bertos/net/ax25.h | 2 +- bertos/net/keytag.c | 2 +- bertos/net/keytag.h | 2 +- bertos/net/nmea.h | 2 +- bertos/net/pocketbus.c | 2 +- bertos/net/pocketbus.h | 2 +- bertos/net/xmodem.h | 2 +- bertos/struct/kfile_fifo.c | 2 +- bertos/struct/kfile_fifo.h | 2 +- bertos/struct/kfile_mem.c | 2 +- bertos/struct/kfile_mem.h | 2 +- boards/triface/examples/triface/protocol.c | 2 +- boards/triface/examples/triface/protocol.h | 2 +- test/run_tests.sh | 2 +- 56 files changed, 631 insertions(+), 623 deletions(-) create mode 100644 bertos/io/kfile.c create mode 100644 bertos/io/kfile.h rename bertos/{kern => io}/kfile_test.c (100%) diff --git a/bertos/cfg/kfile_debug.c b/bertos/cfg/kfile_debug.c index 0a71af68..40233a02 100644 --- a/bertos/cfg/kfile_debug.c +++ b/bertos/cfg/kfile_debug.c @@ -37,7 +37,7 @@ #include "kfile_debug.h" -#include +#include #include diff --git a/bertos/cfg/kfile_debug.h b/bertos/cfg/kfile_debug.h index 4cd31ddf..0dce7e75 100644 --- a/bertos/cfg/kfile_debug.h +++ b/bertos/cfg/kfile_debug.h @@ -42,7 +42,7 @@ #ifndef CFG_KFILE_DEBUG #define CFG_KFILE_DEBUG -#include +#include /** * Context for KFile over debug console. diff --git a/bertos/cpu/arm/drv/flash_at91.c b/bertos/cpu/arm/drv/flash_at91.c index f46c4881..83cc73b7 100644 --- a/bertos/cpu/arm/drv/flash_at91.c +++ b/bertos/cpu/arm/drv/flash_at91.c @@ -54,7 +54,7 @@ #include #include -#include +#include #include diff --git a/bertos/cpu/arm/drv/flash_at91.h b/bertos/cpu/arm/drv/flash_at91.h index 0b6c8d66..1bb7d94b 100644 --- a/bertos/cpu/arm/drv/flash_at91.h +++ b/bertos/cpu/arm/drv/flash_at91.h @@ -42,7 +42,7 @@ #include -#include +#include #include diff --git a/bertos/cpu/arm/drv/spi_dma_at91.c b/bertos/cpu/arm/drv/spi_dma_at91.c index 8576fc07..2c6a9308 100644 --- a/bertos/cpu/arm/drv/spi_dma_at91.c +++ b/bertos/cpu/arm/drv/spi_dma_at91.c @@ -42,7 +42,7 @@ #include "spi_dma_at91.h" #include "hw/hw_spi_dma.h" -#include +#include #include #include #include diff --git a/bertos/cpu/arm/drv/spi_dma_at91.h b/bertos/cpu/arm/drv/spi_dma_at91.h index 6ceef4aa..23e64a91 100644 --- a/bertos/cpu/arm/drv/spi_dma_at91.h +++ b/bertos/cpu/arm/drv/spi_dma_at91.h @@ -42,7 +42,7 @@ #ifndef DRV_SPI_DMA_AT91_H #define DRV_SPI_DMA_AT91_H -#include +#include typedef struct SpiDmaAt91 { diff --git a/bertos/cpu/avr/drv/flash_avr.c b/bertos/cpu/avr/drv/flash_avr.c index 2c3d1840..af1d6de8 100644 --- a/bertos/cpu/avr/drv/flash_avr.c +++ b/bertos/cpu/avr/drv/flash_avr.c @@ -57,7 +57,7 @@ #include #include -#include +#include #include #include diff --git a/bertos/cpu/avr/drv/flash_avr.h b/bertos/cpu/avr/drv/flash_avr.h index 6c06eaf3..ecbe1b77 100644 --- a/bertos/cpu/avr/drv/flash_avr.h +++ b/bertos/cpu/avr/drv/flash_avr.h @@ -45,7 +45,7 @@ #include -#include +#include #include diff --git a/bertos/cpu/cortex-m3/drv/flash_lm3s.c b/bertos/cpu/cortex-m3/drv/flash_lm3s.c index 1bebb535..b7498cac 100644 --- a/bertos/cpu/cortex-m3/drv/flash_lm3s.c +++ b/bertos/cpu/cortex-m3/drv/flash_lm3s.c @@ -40,7 +40,7 @@ #include -#include +#include #include #include diff --git a/bertos/cpu/cortex-m3/drv/flash_lm3s.h b/bertos/cpu/cortex-m3/drv/flash_lm3s.h index 9d054716..e70a7919 100644 --- a/bertos/cpu/cortex-m3/drv/flash_lm3s.h +++ b/bertos/cpu/cortex-m3/drv/flash_lm3s.h @@ -39,7 +39,7 @@ #define FLASH_LM3S_H #include -#include +#include /* Flash memory mapping */ #define FLASH_MEM_SIZE 0x40000 //< 256KiB diff --git a/bertos/cpu/cortex-m3/drv/ssi_lm3s.h b/bertos/cpu/cortex-m3/drv/ssi_lm3s.h index d5f09e3d..d8ab39ea 100644 --- a/bertos/cpu/cortex-m3/drv/ssi_lm3s.h +++ b/bertos/cpu/cortex-m3/drv/ssi_lm3s.h @@ -38,7 +38,7 @@ #define SSI_LM3S_H #include /* cpu_relax() */ -#include /* KFile */ +#include /* KFile */ #include /** diff --git a/bertos/drv/dataflash.c b/bertos/drv/dataflash.c index 573bea1e..a9135968 100644 --- a/bertos/drv/dataflash.c +++ b/bertos/drv/dataflash.c @@ -52,7 +52,7 @@ #include -#include +#include #include /* cpu_relax() */ diff --git a/bertos/drv/dataflash.h b/bertos/drv/dataflash.h index dd805ad2..a60f30ea 100644 --- a/bertos/drv/dataflash.h +++ b/bertos/drv/dataflash.h @@ -48,7 +48,7 @@ #include -#include +#include #include /** diff --git a/bertos/drv/dataflash_hwtest.c b/bertos/drv/dataflash_hwtest.c index fa5f480e..7dcd9eb2 100644 --- a/bertos/drv/dataflash_hwtest.c +++ b/bertos/drv/dataflash_hwtest.c @@ -66,7 +66,7 @@ #include #include -#include +#include #include diff --git a/bertos/drv/eeprom.h b/bertos/drv/eeprom.h index 995919ae..5bd834af 100644 --- a/bertos/drv/eeprom.h +++ b/bertos/drv/eeprom.h @@ -44,7 +44,7 @@ #include -#include +#include /** diff --git a/bertos/drv/flash25.c b/bertos/drv/flash25.c index 0f67e3d4..3f84903b 100644 --- a/bertos/drv/flash25.c +++ b/bertos/drv/flash25.c @@ -52,7 +52,7 @@ #include #include -#include +#include #include /* cpu_relax() */ diff --git a/bertos/drv/flash25.h b/bertos/drv/flash25.h index 2712c70a..921e1b5c 100644 --- a/bertos/drv/flash25.h +++ b/bertos/drv/flash25.h @@ -47,7 +47,7 @@ #include "cfg/cfg_flash25.h" #include -#include +#include /** * Type definition for serial flash memory. diff --git a/bertos/drv/flash25_hwtest.c b/bertos/drv/flash25_hwtest.c index 1297feea..29534c64 100644 --- a/bertos/drv/flash25_hwtest.c +++ b/bertos/drv/flash25_hwtest.c @@ -43,7 +43,7 @@ */ -#include +#include #include diff --git a/bertos/drv/ft245rl.c b/bertos/drv/ft245rl.c index 3cfac12c..1042cd7b 100644 --- a/bertos/drv/ft245rl.c +++ b/bertos/drv/ft245rl.c @@ -49,7 +49,7 @@ #include -#include +#include #include diff --git a/bertos/drv/ft245rl.h b/bertos/drv/ft245rl.h index 258b0a4b..24dae6d6 100644 --- a/bertos/drv/ft245rl.h +++ b/bertos/drv/ft245rl.h @@ -45,7 +45,7 @@ #ifndef DRV_FT245RL_H #define DRV_FT245RL_H -#include +#include #include /** diff --git a/bertos/drv/mcp41.c b/bertos/drv/mcp41.c index bfa8fd4e..dfdcba6b 100644 --- a/bertos/drv/mcp41.c +++ b/bertos/drv/mcp41.c @@ -43,7 +43,7 @@ #include #include -#include +#include static KFile *ch; diff --git a/bertos/drv/mcp41.h b/bertos/drv/mcp41.h index 6f5cf366..698774a0 100644 --- a/bertos/drv/mcp41.h +++ b/bertos/drv/mcp41.h @@ -47,7 +47,7 @@ #include "hw/mcp41_map.h" #include -#include +#include #define MCP41_WRITE_DATA 0x11 #define MCP41_HW_MAX 255 diff --git a/bertos/drv/sd.c b/bertos/drv/sd.c index 0c151563..a3a39062 100644 --- a/bertos/drv/sd.c +++ b/bertos/drv/sd.c @@ -38,7 +38,7 @@ #include "sd.h" #include "hw/hw_sd.h" -#include +#include #include #include diff --git a/bertos/drv/sd.h b/bertos/drv/sd.h index 42c1d7df..207dcfd2 100644 --- a/bertos/drv/sd.h +++ b/bertos/drv/sd.h @@ -47,7 +47,7 @@ #include "cfg/cfg_fat.h" -#include +#include #include diff --git a/bertos/drv/ser.h b/bertos/drv/ser.h index 226d9e0c..cd81f5af 100644 --- a/bertos/drv/ser.h +++ b/bertos/drv/ser.h @@ -46,7 +46,7 @@ #ifndef DRV_SER_H #define DRV_SER_H -#include +#include #include #include diff --git a/bertos/drv/sipo.c b/bertos/drv/sipo.c index 2d3d64fd..58830bbb 100644 --- a/bertos/drv/sipo.c +++ b/bertos/drv/sipo.c @@ -47,7 +47,7 @@ #include #include -#include +#include #include diff --git a/bertos/drv/sipo.h b/bertos/drv/sipo.h index 940f8537..7dbc6cc8 100644 --- a/bertos/drv/sipo.h +++ b/bertos/drv/sipo.h @@ -52,7 +52,7 @@ #include "hw/hw_sipo.h" -#include +#include #define SIPO_DATAORDER_START_LSB 1 #define SIPO_DATAORDER_START_MSB 0x80 diff --git a/bertos/drv/tlv5618.c b/bertos/drv/tlv5618.c index 4639df6b..048e36ba 100644 --- a/bertos/drv/tlv5618.c +++ b/bertos/drv/tlv5618.c @@ -38,7 +38,7 @@ #include "tlv5618.h" #include "hw/hw_tlv5618.h" -#include +#include #include //memset diff --git a/bertos/drv/tlv5618.h b/bertos/drv/tlv5618.h index 7deb1eb7..197229e7 100644 --- a/bertos/drv/tlv5618.h +++ b/bertos/drv/tlv5618.h @@ -43,7 +43,7 @@ #define DRV_TLV5618_H #include -#include +#include typedef struct Tlv5618 { diff --git a/bertos/drv/tmp123.c b/bertos/drv/tmp123.c index cce2696a..7f8b4f09 100644 --- a/bertos/drv/tmp123.c +++ b/bertos/drv/tmp123.c @@ -44,7 +44,7 @@ #include -#include +#include #include // Macro and data type to manage celsius degree diff --git a/bertos/drv/tmp123.h b/bertos/drv/tmp123.h index 85859b25..015076bd 100644 --- a/bertos/drv/tmp123.h +++ b/bertos/drv/tmp123.h @@ -45,7 +45,7 @@ #include -#include +#include #include // Macro and data type to manage celsius degree diff --git a/bertos/emul/kfile_posix.h b/bertos/emul/kfile_posix.h index ba88ab78..7caa3003 100644 --- a/bertos/emul/kfile_posix.h +++ b/bertos/emul/kfile_posix.h @@ -39,7 +39,7 @@ #ifndef KFILE_POSIX_H #define KFILE_POSIX_H -#include +#include #include typedef struct KFilePosix diff --git a/bertos/fs/battfs.h b/bertos/fs/battfs.h index 921c59da..d38b4d4a 100644 --- a/bertos/fs/battfs.h +++ b/bertos/fs/battfs.h @@ -49,7 +49,7 @@ #include // CPU_BITS_PER_CHAR #include #include -#include +#include #include typedef uint16_t fill_t; ///< Type for keeping trace of space filled inside a page diff --git a/bertos/fs/fat.h b/bertos/fs/fat.h index 3e7dc787..26d0d1c0 100644 --- a/bertos/fs/fat.h +++ b/bertos/fs/fat.h @@ -48,7 +48,7 @@ #ifndef FS_FAT_H #define FS_FAT_H -#include +#include #include "fatfs/ff.h" typedef struct FatFile diff --git a/bertos/io/kfile.c b/bertos/io/kfile.c new file mode 100644 index 00000000..d98f1548 --- /dev/null +++ b/bertos/io/kfile.c @@ -0,0 +1,284 @@ +/** + * \file + * + * + * \brief Virtual KFile I/O interface. + * + * This module implements some generic I/O interfaces for kfile. + * + * \version $Id$ + * \author Francesco Sacchi + * \author Daniele Basile + */ + +#include "kfile.h" + +#include "cfg/cfg_kfile.h" +#include +#include + +#include +#include + +#include + +/* + * Sanity check for config parameters required by this module. + */ +#if !defined(CONFIG_KFILE_GETS) || ((CONFIG_KFILE_GETS != 0) && CONFIG_KFILE_GETS != 1) + #error CONFIG_KFILE_GETS must be set to either 0 or 1 in appconfig.h +#endif +#if !defined(CONFIG_PRINTF) + #error CONFIG_PRINTF missing in appconfig.h +#endif + + +/** + * Generic putc() implementation using \a fd->write. + */ +int kfile_putc(int _c, struct KFile *fd) +{ + unsigned char c = (unsigned char)_c; + + if (kfile_write(fd, &c, sizeof(c)) == sizeof(c)) + return (int)((unsigned char)_c); + else + return EOF; +} + +/** + * Generic getc() implementation using \a fd->read. + */ +int kfile_getc(struct KFile *fd) +{ + unsigned char c; + + if (kfile_read(fd, &c, sizeof(c)) == sizeof(c)) + return (int)((unsigned char)c); + else + return EOF; +} + +#if CONFIG_PRINTF +/** + * Formatted write. + */ +int kfile_printf(struct KFile *fd, const char *format, ...) +{ + va_list ap; + int len; + + va_start(ap, format); + len = _formatted_write(format, (void (*)(char, void *))kfile_putc, fd, ap); + va_end(ap); + + return len; +} +#endif /* CONFIG_PRINTF */ + +/** + * Write a string to kfile \a fd. + * \return 0 if OK, EOF in case of error. + */ +int kfile_print(struct KFile *fd, const char *s) +{ + while (*s) + { + if (kfile_putc(*s++, fd) == EOF) + return EOF; + } + return 0; +} + +#if CONFIG_KFILE_GETS +/** + * Read a line long at most as size and put it + * in buf. + * \return number of chars read or EOF in case + * of error. + */ +int kfile_gets(struct KFile *fd, char *buf, int size) +{ + return kfile_gets_echo(fd, buf, size, false); +} + + +/** + * Read a line long at most as size and put it + * in buf, with optional echo. + * + * \return number of chars read, or EOF in case + * of error. + */ +int kfile_gets_echo(struct KFile *fd, char *buf, int size, bool echo) +{ + int i = 0; + int c; + + for (;;) + { + if ((c = kfile_getc(fd)) == EOF) + { + buf[i] = '\0'; + return -1; + } + + /* FIXME */ + if (c == '\r' || c == '\n' || i >= size-1) + { + buf[i] = '\0'; + if (echo) + kfile_print(fd, "\r\n"); + break; + } + buf[i++] = c; + if (echo) + kfile_putc(c, fd); + } + + return i; +} +#endif /* !CONFIG_KFILE_GETS */ + + +/** + * Move \a fd file seek position of \a offset bytes from \a whence. + * + * This is a generic implementation of seek function, you can redefine + * it in your local module if needed. + */ +kfile_off_t kfile_genericSeek(struct KFile *fd, kfile_off_t offset, KSeekMode whence) +{ + kfile_off_t seek_pos; + + switch (whence) + { + + case KSM_SEEK_SET: + seek_pos = 0; + break; + case KSM_SEEK_END: + seek_pos = fd->size; + break; + case KSM_SEEK_CUR: + seek_pos = fd->seek_pos; + break; + default: + ASSERT(0); + return EOF; + break; + } + + #if LOG_LEVEL >= LOG_LVL_INFO + /* Bound check */ + if (seek_pos + offset > fd->size) + LOG_INFO("seek outside EOF\n"); + #endif + + fd->seek_pos = seek_pos + offset; + + return fd->seek_pos; +} + +/** + * Reopen file \a fd. + * This is a generic implementation that only flush file + * and reset seek_pos to 0. + */ +struct KFile * kfile_genericReopen(struct KFile *fd) +{ + kfile_flush(fd); + kfile_seek(fd, 0, KSM_SEEK_SET); + return fd; +} + +/** + * Close file \a fd. + * This is a generic implementation that only return 0. + */ +int kfile_genericClose(UNUSED_ARG(struct KFile *, fd)) +{ + return 0; +}; + + +/** + * Discard input to resynchronize with remote end. + * + * Discard incoming data until the kfile_getc stops receiving + * characters for at least \a delay milliseconds. + * + * \note If the timeout occur, we reset the error before to + * quit. + */ +void kfile_resync(KFile *fd, mtime_t delay) +{ + ticks_t start_time = timer_clock(); + for(;;) + { + if(kfile_getc(fd) != EOF) + start_time = timer_clock(); + + if ((timer_clock() - start_time) > ms_to_ticks(delay)) + { + kfile_clearerr(fd); + break; + } + + } +} + +/** + * Stub function that does nothing. + * This is a generic implementation that only return 0. + */ +static int kfile_generic(UNUSED_ARG(struct KFile *, fd)) +{ + return 0; +}; + + +/** + * Base class KFile constructor. + */ +void kfile_init(struct KFile *fd) +{ + ASSERT(fd); + memset(fd, 0, sizeof(*fd)); + fd->clearerr = (ClearErrFunc_t)kfile_generic; + fd->close = kfile_genericClose; + fd->error = kfile_generic; + fd->flush = kfile_generic; + fd->read = (ReadFunc_t)kfile_generic; + fd->reopen = kfile_genericReopen; + fd->seek = kfile_genericSeek; + fd->write = (WriteFunc_t)kfile_generic; +} + diff --git a/bertos/io/kfile.h b/bertos/io/kfile.h new file mode 100644 index 00000000..44c32b06 --- /dev/null +++ b/bertos/io/kfile.h @@ -0,0 +1,289 @@ +/** + * \file + * + * + * \brief Virtual KFile I/O interface. + * + * KFile is a simple, generic interface for file I/O. It uses an + * object-oriented model to supply a device-neutral interface to + * communicate with drivers. + * + * This module contains only definitions, the instance structure + * and the common API. + * Each KFile subclass can override one or more methods of the interface, + * and can extend the base KFile structure with its own private data. + * For instance, a serial driver might implement the KFile interface by + * declaring a context structure like this: + * + * \code + * typedef struct Serial + * { + * // base class instance + * KFile fd; + * + * // private instance data + * FIFOBuffer txfifo, rxfifo; + * } Serial; + * \endcode + * + * You should also supply a macro for casting KFile to Serial: + * + * \code + * INLINE Serial * SERIAL_CAST(KFile *fd) + * { + * ASSERT(fd->_type == KFT_SERIAL); + * return (Serial *)fd; + * } + * \endcode + * + * Then you can implement as many interface functions as needed + * and leave the rest to NULL. + * + * Example implementation of the close KFile method for Serial: + * + * \code + * static int ser_kfile_close(struct KFile *fd) + * { + * Serial *fds = SERIAL_CAST(fd); + * // [driver specific code here] + * return 0; + * } + * \endcode + * + * The SERIAL_CAST() macro helps ensure that the passed object is + * really of type Serial. + * + * The KFile interface does not supply an open function: this is deliberate, + * because in embedded systems each device has its own init parameters. + * For the same reason, specific device settings like, for example, + * the baudrate, are not part of interface and should be handled by the + * driver-specific API. + * + * \version $Id$ + * \author Bernie Innocenti + * \author Francesco Sacchi + * \author Daniele Basile + * + * $WIZ$ module_name = "kfile" + * $WIZ$ module_configuration = "bertos/cfg/cfg_kfile.h" + * $WIZ$ module_depends = "timer", "formatwr" + */ + +#ifndef KERN_KFILE_H +#define KERN_KFILE_H + +#include +#include +#include + +/* fwd decl */ +struct KFile; + +typedef int32_t kfile_off_t; ///< KFile offset type, used by kfile_seek(). + +/** + * Costants for repositioning read/write file offset. + * These are needed because on some embedded platforms + * ANSI I/O library may not be present. + */ +typedef enum KSeekMode +{ + KSM_SEEK_SET, ///< Seek from file beginning. + KSM_SEEK_CUR, ///< Seek from file current position. + KSM_SEEK_END, ///< Seek from file end. +} KSeekMode; + +/** + * Prototypes for KFile access functions. + * I/O file functions must be ANSI compliant. + * \note A KFile user can choose which function subset to implement, + * but has to set to NULL unimplemented features. + * \{ + */ + +/** + * Read from file. + * \return the number of bytes read. + */ +typedef size_t (*ReadFunc_t) (struct KFile *fd, void *buf, size_t size); + +/** + * Write to file. + * \return the number of bytes written. + */ +typedef size_t (*WriteFunc_t) (struct KFile *fd, const void *buf, size_t size); + +/** + * Seek into file (if seekable). + * \return the new file offset or EOF on errors. + */ +typedef kfile_off_t (*SeekFunc_t) (struct KFile *fd, kfile_off_t offset, KSeekMode whence); + +/** + * Close and reopen file \a fd. + * The reopening is done with the former file parameters and access modes. + */ +typedef struct KFile * (*ReOpenFunc_t) (struct KFile *fd); + +/** + * Close file. + * \return 0 on success, EOF on errors. + */ +typedef int (*CloseFunc_t) (struct KFile *fd); + +/** + * Flush file I/O. + * \return 0 on success, EOF on errors. + */ +typedef int (*FlushFunc_t) (struct KFile *fd); + +/** + * Get file error mask. + * \return 0 on success or file error code, device specific. + */ +typedef int (*ErrorFunc_t) (struct KFile *fd); + +/** + * Clear errors. + */ +typedef void (*ClearErrFunc_t) (struct KFile *fd); +/* \} */ + +/** + * Context data for callback functions which operate on + * pseudo files. + * + * \note Remember to add the corresponding accessor functions + * when extending this interface. + */ +typedef struct KFile +{ + ReadFunc_t read; + WriteFunc_t write; + ReOpenFunc_t reopen; + CloseFunc_t close; + SeekFunc_t seek; + FlushFunc_t flush; + ErrorFunc_t error; + ClearErrFunc_t clearerr; + DB(id_t _type); ///< Used to keep track, at runtime, of the class type. + + /* NOTE: these must _NOT_ be size_t on 16bit CPUs! */ + kfile_off_t seek_pos; + kfile_off_t size; +} KFile; + +/** + * Generic implementation of kfile_seek. + */ +kfile_off_t kfile_genericSeek(struct KFile *fd, kfile_off_t offset, KSeekMode whence); + +/** + * Generic implementation of kfile_reopen. + */ +struct KFile * kfile_genericReopen(struct KFile *fd); + +int kfile_genericClose(struct KFile *fd); + +int kfile_putc(int c, struct KFile *fd); ///< Generic putc implementation using kfile_write. +int kfile_getc(struct KFile *fd); ///< Generic getc implementation using kfile_read. +int kfile_printf(struct KFile *fd, const char *format, ...); +int kfile_print(struct KFile *fd, const char *s); +int kfile_gets(struct KFile *fd, char *buf, int size); +int kfile_gets_echo(struct KFile *fd, char *buf, int size, bool echo); +void kfile_resync(KFile *fd, mtime_t delay); +void kfile_init(struct KFile *fd); + +/** + * Interface functions for KFile access. + * \note Remember to change following functions if KFile interface changes. + * \{ + */ +INLINE size_t kfile_read(struct KFile *fd, void *buf, size_t size) +{ + ASSERT(fd->read); + return fd->read(fd, buf, size); +} + +INLINE size_t kfile_write(struct KFile *fd, const void *buf, size_t size) +{ + ASSERT(fd->write); + return fd->write(fd, buf, size); +} + +INLINE KFile * kfile_reopen(struct KFile *fd) +{ + ASSERT(fd->reopen); + return fd->reopen(fd); +} + +INLINE int kfile_close(struct KFile *fd) +{ + ASSERT(fd->close); + return fd->close(fd); +} + +INLINE kfile_off_t kfile_seek(struct KFile *fd, kfile_off_t offset, KSeekMode whence) +{ + ASSERT(fd->seek); + return fd->seek(fd, offset, whence); +} + +INLINE int kfile_flush(struct KFile *fd) +{ + ASSERT(fd->flush); + return fd->flush(fd); +} + +INLINE int kfile_error(struct KFile *fd) +{ + ASSERT(fd->error); + return fd->error(fd); +} + +INLINE void kfile_clearerr(struct KFile *fd) +{ + ASSERT(fd->clearerr); + fd->clearerr(fd); +} +/* \} */ + +/** + * Kfile test function. + */ +int kfile_testSetup(void); +int kfile_testRun(void); +int kfile_testRunGeneric(KFile *fd, uint8_t *test_buf, uint8_t *save_buf, size_t size); +int kfile_testTearDown(void); + + +#endif /* KERN_KFILE_H */ diff --git a/bertos/kern/kfile_test.c b/bertos/io/kfile_test.c similarity index 100% rename from bertos/kern/kfile_test.c rename to bertos/io/kfile_test.c diff --git a/bertos/kern/kfile.c b/bertos/kern/kfile.c index d98f1548..e8691163 100644 --- a/bertos/kern/kfile.c +++ b/bertos/kern/kfile.c @@ -1,284 +1,5 @@ -/** - * \file - * - * - * \brief Virtual KFile I/O interface. - * - * This module implements some generic I/O interfaces for kfile. - * - * \version $Id$ - * \author Francesco Sacchi - * \author Daniele Basile - */ - -#include "kfile.h" - -#include "cfg/cfg_kfile.h" -#include -#include - -#include -#include - -#include - -/* - * Sanity check for config parameters required by this module. - */ -#if !defined(CONFIG_KFILE_GETS) || ((CONFIG_KFILE_GETS != 0) && CONFIG_KFILE_GETS != 1) - #error CONFIG_KFILE_GETS must be set to either 0 or 1 in appconfig.h -#endif -#if !defined(CONFIG_PRINTF) - #error CONFIG_PRINTF missing in appconfig.h -#endif - - -/** - * Generic putc() implementation using \a fd->write. - */ -int kfile_putc(int _c, struct KFile *fd) -{ - unsigned char c = (unsigned char)_c; - - if (kfile_write(fd, &c, sizeof(c)) == sizeof(c)) - return (int)((unsigned char)_c); - else - return EOF; -} - -/** - * Generic getc() implementation using \a fd->read. - */ -int kfile_getc(struct KFile *fd) -{ - unsigned char c; - - if (kfile_read(fd, &c, sizeof(c)) == sizeof(c)) - return (int)((unsigned char)c); - else - return EOF; -} - -#if CONFIG_PRINTF -/** - * Formatted write. - */ -int kfile_printf(struct KFile *fd, const char *format, ...) -{ - va_list ap; - int len; - - va_start(ap, format); - len = _formatted_write(format, (void (*)(char, void *))kfile_putc, fd, ap); - va_end(ap); - - return len; -} -#endif /* CONFIG_PRINTF */ - -/** - * Write a string to kfile \a fd. - * \return 0 if OK, EOF in case of error. - */ -int kfile_print(struct KFile *fd, const char *s) -{ - while (*s) - { - if (kfile_putc(*s++, fd) == EOF) - return EOF; - } - return 0; -} - -#if CONFIG_KFILE_GETS -/** - * Read a line long at most as size and put it - * in buf. - * \return number of chars read or EOF in case - * of error. - */ -int kfile_gets(struct KFile *fd, char *buf, int size) -{ - return kfile_gets_echo(fd, buf, size, false); -} - - -/** - * Read a line long at most as size and put it - * in buf, with optional echo. - * - * \return number of chars read, or EOF in case - * of error. - */ -int kfile_gets_echo(struct KFile *fd, char *buf, int size, bool echo) -{ - int i = 0; - int c; - - for (;;) - { - if ((c = kfile_getc(fd)) == EOF) - { - buf[i] = '\0'; - return -1; - } - - /* FIXME */ - if (c == '\r' || c == '\n' || i >= size-1) - { - buf[i] = '\0'; - if (echo) - kfile_print(fd, "\r\n"); - break; - } - buf[i++] = c; - if (echo) - kfile_putc(c, fd); - } - - return i; -} -#endif /* !CONFIG_KFILE_GETS */ - - -/** - * Move \a fd file seek position of \a offset bytes from \a whence. - * - * This is a generic implementation of seek function, you can redefine - * it in your local module if needed. - */ -kfile_off_t kfile_genericSeek(struct KFile *fd, kfile_off_t offset, KSeekMode whence) -{ - kfile_off_t seek_pos; - - switch (whence) - { - - case KSM_SEEK_SET: - seek_pos = 0; - break; - case KSM_SEEK_END: - seek_pos = fd->size; - break; - case KSM_SEEK_CUR: - seek_pos = fd->seek_pos; - break; - default: - ASSERT(0); - return EOF; - break; - } - - #if LOG_LEVEL >= LOG_LVL_INFO - /* Bound check */ - if (seek_pos + offset > fd->size) - LOG_INFO("seek outside EOF\n"); - #endif - - fd->seek_pos = seek_pos + offset; - - return fd->seek_pos; -} - -/** - * Reopen file \a fd. - * This is a generic implementation that only flush file - * and reset seek_pos to 0. - */ -struct KFile * kfile_genericReopen(struct KFile *fd) -{ - kfile_flush(fd); - kfile_seek(fd, 0, KSM_SEEK_SET); - return fd; -} - -/** - * Close file \a fd. - * This is a generic implementation that only return 0. - */ -int kfile_genericClose(UNUSED_ARG(struct KFile *, fd)) -{ - return 0; -}; - - -/** - * Discard input to resynchronize with remote end. - * - * Discard incoming data until the kfile_getc stops receiving - * characters for at least \a delay milliseconds. - * - * \note If the timeout occur, we reset the error before to - * quit. - */ -void kfile_resync(KFile *fd, mtime_t delay) -{ - ticks_t start_time = timer_clock(); - for(;;) - { - if(kfile_getc(fd) != EOF) - start_time = timer_clock(); - - if ((timer_clock() - start_time) > ms_to_ticks(delay)) - { - kfile_clearerr(fd); - break; - } - - } -} - -/** - * Stub function that does nothing. - * This is a generic implementation that only return 0. - */ -static int kfile_generic(UNUSED_ARG(struct KFile *, fd)) -{ - return 0; -}; - - -/** - * Base class KFile constructor. - */ -void kfile_init(struct KFile *fd) -{ - ASSERT(fd); - memset(fd, 0, sizeof(*fd)); - fd->clearerr = (ClearErrFunc_t)kfile_generic; - fd->close = kfile_genericClose; - fd->error = kfile_generic; - fd->flush = kfile_generic; - fd->read = (ReadFunc_t)kfile_generic; - fd->reopen = kfile_genericReopen; - fd->seek = kfile_genericSeek; - fd->write = (WriteFunc_t)kfile_generic; -} +// notest: all +#warning "This file is deprecated, use the new io/kfile.c" +#error analc +#include diff --git a/bertos/kern/kfile.h b/bertos/kern/kfile.h index 44c32b06..24e07dd5 100644 --- a/bertos/kern/kfile.h +++ b/bertos/kern/kfile.h @@ -1,289 +1,3 @@ -/** - * \file - * - * - * \brief Virtual KFile I/O interface. - * - * KFile is a simple, generic interface for file I/O. It uses an - * object-oriented model to supply a device-neutral interface to - * communicate with drivers. - * - * This module contains only definitions, the instance structure - * and the common API. - * Each KFile subclass can override one or more methods of the interface, - * and can extend the base KFile structure with its own private data. - * For instance, a serial driver might implement the KFile interface by - * declaring a context structure like this: - * - * \code - * typedef struct Serial - * { - * // base class instance - * KFile fd; - * - * // private instance data - * FIFOBuffer txfifo, rxfifo; - * } Serial; - * \endcode - * - * You should also supply a macro for casting KFile to Serial: - * - * \code - * INLINE Serial * SERIAL_CAST(KFile *fd) - * { - * ASSERT(fd->_type == KFT_SERIAL); - * return (Serial *)fd; - * } - * \endcode - * - * Then you can implement as many interface functions as needed - * and leave the rest to NULL. - * - * Example implementation of the close KFile method for Serial: - * - * \code - * static int ser_kfile_close(struct KFile *fd) - * { - * Serial *fds = SERIAL_CAST(fd); - * // [driver specific code here] - * return 0; - * } - * \endcode - * - * The SERIAL_CAST() macro helps ensure that the passed object is - * really of type Serial. - * - * The KFile interface does not supply an open function: this is deliberate, - * because in embedded systems each device has its own init parameters. - * For the same reason, specific device settings like, for example, - * the baudrate, are not part of interface and should be handled by the - * driver-specific API. - * - * \version $Id$ - * \author Bernie Innocenti - * \author Francesco Sacchi - * \author Daniele Basile - * - * $WIZ$ module_name = "kfile" - * $WIZ$ module_configuration = "bertos/cfg/cfg_kfile.h" - * $WIZ$ module_depends = "timer", "formatwr" - */ - -#ifndef KERN_KFILE_H -#define KERN_KFILE_H - -#include -#include -#include - -/* fwd decl */ -struct KFile; - -typedef int32_t kfile_off_t; ///< KFile offset type, used by kfile_seek(). - -/** - * Costants for repositioning read/write file offset. - * These are needed because on some embedded platforms - * ANSI I/O library may not be present. - */ -typedef enum KSeekMode -{ - KSM_SEEK_SET, ///< Seek from file beginning. - KSM_SEEK_CUR, ///< Seek from file current position. - KSM_SEEK_END, ///< Seek from file end. -} KSeekMode; - -/** - * Prototypes for KFile access functions. - * I/O file functions must be ANSI compliant. - * \note A KFile user can choose which function subset to implement, - * but has to set to NULL unimplemented features. - * \{ - */ - -/** - * Read from file. - * \return the number of bytes read. - */ -typedef size_t (*ReadFunc_t) (struct KFile *fd, void *buf, size_t size); - -/** - * Write to file. - * \return the number of bytes written. - */ -typedef size_t (*WriteFunc_t) (struct KFile *fd, const void *buf, size_t size); - -/** - * Seek into file (if seekable). - * \return the new file offset or EOF on errors. - */ -typedef kfile_off_t (*SeekFunc_t) (struct KFile *fd, kfile_off_t offset, KSeekMode whence); - -/** - * Close and reopen file \a fd. - * The reopening is done with the former file parameters and access modes. - */ -typedef struct KFile * (*ReOpenFunc_t) (struct KFile *fd); - -/** - * Close file. - * \return 0 on success, EOF on errors. - */ -typedef int (*CloseFunc_t) (struct KFile *fd); - -/** - * Flush file I/O. - * \return 0 on success, EOF on errors. - */ -typedef int (*FlushFunc_t) (struct KFile *fd); - -/** - * Get file error mask. - * \return 0 on success or file error code, device specific. - */ -typedef int (*ErrorFunc_t) (struct KFile *fd); - -/** - * Clear errors. - */ -typedef void (*ClearErrFunc_t) (struct KFile *fd); -/* \} */ - -/** - * Context data for callback functions which operate on - * pseudo files. - * - * \note Remember to add the corresponding accessor functions - * when extending this interface. - */ -typedef struct KFile -{ - ReadFunc_t read; - WriteFunc_t write; - ReOpenFunc_t reopen; - CloseFunc_t close; - SeekFunc_t seek; - FlushFunc_t flush; - ErrorFunc_t error; - ClearErrFunc_t clearerr; - DB(id_t _type); ///< Used to keep track, at runtime, of the class type. - - /* NOTE: these must _NOT_ be size_t on 16bit CPUs! */ - kfile_off_t seek_pos; - kfile_off_t size; -} KFile; - -/** - * Generic implementation of kfile_seek. - */ -kfile_off_t kfile_genericSeek(struct KFile *fd, kfile_off_t offset, KSeekMode whence); - -/** - * Generic implementation of kfile_reopen. - */ -struct KFile * kfile_genericReopen(struct KFile *fd); - -int kfile_genericClose(struct KFile *fd); - -int kfile_putc(int c, struct KFile *fd); ///< Generic putc implementation using kfile_write. -int kfile_getc(struct KFile *fd); ///< Generic getc implementation using kfile_read. -int kfile_printf(struct KFile *fd, const char *format, ...); -int kfile_print(struct KFile *fd, const char *s); -int kfile_gets(struct KFile *fd, char *buf, int size); -int kfile_gets_echo(struct KFile *fd, char *buf, int size, bool echo); -void kfile_resync(KFile *fd, mtime_t delay); -void kfile_init(struct KFile *fd); - -/** - * Interface functions for KFile access. - * \note Remember to change following functions if KFile interface changes. - * \{ - */ -INLINE size_t kfile_read(struct KFile *fd, void *buf, size_t size) -{ - ASSERT(fd->read); - return fd->read(fd, buf, size); -} - -INLINE size_t kfile_write(struct KFile *fd, const void *buf, size_t size) -{ - ASSERT(fd->write); - return fd->write(fd, buf, size); -} - -INLINE KFile * kfile_reopen(struct KFile *fd) -{ - ASSERT(fd->reopen); - return fd->reopen(fd); -} - -INLINE int kfile_close(struct KFile *fd) -{ - ASSERT(fd->close); - return fd->close(fd); -} - -INLINE kfile_off_t kfile_seek(struct KFile *fd, kfile_off_t offset, KSeekMode whence) -{ - ASSERT(fd->seek); - return fd->seek(fd, offset, whence); -} - -INLINE int kfile_flush(struct KFile *fd) -{ - ASSERT(fd->flush); - return fd->flush(fd); -} - -INLINE int kfile_error(struct KFile *fd) -{ - ASSERT(fd->error); - return fd->error(fd); -} - -INLINE void kfile_clearerr(struct KFile *fd) -{ - ASSERT(fd->clearerr); - fd->clearerr(fd); -} -/* \} */ - -/** - * Kfile test function. - */ -int kfile_testSetup(void); -int kfile_testRun(void); -int kfile_testRunGeneric(KFile *fd, uint8_t *test_buf, uint8_t *save_buf, size_t size); -int kfile_testTearDown(void); - - -#endif /* KERN_KFILE_H */ +#warning "This file is deprecated, include the new " +#error anal +#include diff --git a/bertos/mware/ini_reader.h b/bertos/mware/ini_reader.h index ef5d9489..377659be 100644 --- a/bertos/mware/ini_reader.h +++ b/bertos/mware/ini_reader.h @@ -50,7 +50,7 @@ #ifndef INI_READER_H #define INI_READER_H -#include +#include /** * \brief Returns the value for the given string in char* format. diff --git a/bertos/mware/parser.c b/bertos/mware/parser.c index b06013c8..b6b40881 100644 --- a/bertos/mware/parser.c +++ b/bertos/mware/parser.c @@ -57,7 +57,7 @@ #include "cfg/cfg_parser.h" -#include +#include #include #include // atol(), NULL diff --git a/bertos/net/afsk.h b/bertos/net/afsk.h index 57d3da8e..d5eb272d 100644 --- a/bertos/net/afsk.h +++ b/bertos/net/afsk.h @@ -46,7 +46,7 @@ #include "cfg/cfg_afsk.h" #include "hw/hw_afsk.h" -#include +#include #include #include diff --git a/bertos/net/ax25.h b/bertos/net/ax25.h index 0e609136..1e75501e 100644 --- a/bertos/net/ax25.h +++ b/bertos/net/ax25.h @@ -49,7 +49,7 @@ #include "cfg/cfg_ax25.h" #include -#include +#include /** * Maximum size of a AX25 frame. diff --git a/bertos/net/keytag.c b/bertos/net/keytag.c index 32473ceb..2f75b3ee 100644 --- a/bertos/net/keytag.c +++ b/bertos/net/keytag.c @@ -55,7 +55,7 @@ #include #include -#include +#include #include /** diff --git a/bertos/net/keytag.h b/bertos/net/keytag.h index f086bc34..2a2c5fbf 100644 --- a/bertos/net/keytag.h +++ b/bertos/net/keytag.h @@ -45,7 +45,7 @@ #include -#include +#include /** diff --git a/bertos/net/nmea.h b/bertos/net/nmea.h index f0a8b3de..184a7a0a 100644 --- a/bertos/net/nmea.h +++ b/bertos/net/nmea.h @@ -45,7 +45,7 @@ #include -#include +#include #include diff --git a/bertos/net/pocketbus.c b/bertos/net/pocketbus.c index 117e40d0..bded2db0 100644 --- a/bertos/net/pocketbus.c +++ b/bertos/net/pocketbus.c @@ -87,7 +87,7 @@ #include #include -#include +#include #include diff --git a/bertos/net/pocketbus.h b/bertos/net/pocketbus.h index e67f2634..69635e95 100644 --- a/bertos/net/pocketbus.h +++ b/bertos/net/pocketbus.h @@ -48,7 +48,7 @@ #include -#include +#include /** * pocketBus special characters definitions. diff --git a/bertos/net/xmodem.h b/bertos/net/xmodem.h index db27cf41..2e864d6d 100644 --- a/bertos/net/xmodem.h +++ b/bertos/net/xmodem.h @@ -45,7 +45,7 @@ #define NET_XMODEM_H #include -#include +#include /** * Called to know if we want to abort data tranfer. diff --git a/bertos/struct/kfile_fifo.c b/bertos/struct/kfile_fifo.c index 9ebc59df..66264eb4 100644 --- a/bertos/struct/kfile_fifo.c +++ b/bertos/struct/kfile_fifo.c @@ -39,7 +39,7 @@ #include "kfile_fifo.h" #include "fifobuf.h" -#include +#include #include diff --git a/bertos/struct/kfile_fifo.h b/bertos/struct/kfile_fifo.h index 78c1f46d..ebf23960 100644 --- a/bertos/struct/kfile_fifo.h +++ b/bertos/struct/kfile_fifo.h @@ -64,7 +64,7 @@ #define STRUCT_KFILE_FIFO #include "fifobuf.h" -#include +#include typedef struct KFileFifo { diff --git a/bertos/struct/kfile_mem.c b/bertos/struct/kfile_mem.c index 5560fdc6..0ff03904 100644 --- a/bertos/struct/kfile_mem.c +++ b/bertos/struct/kfile_mem.c @@ -38,7 +38,7 @@ #include "kfile_mem.h" -#include +#include #include diff --git a/bertos/struct/kfile_mem.h b/bertos/struct/kfile_mem.h index c723de2f..b39eacbe 100644 --- a/bertos/struct/kfile_mem.h +++ b/bertos/struct/kfile_mem.h @@ -45,7 +45,7 @@ #ifndef STRUCT_KFILE_MEM #define STRUCT_KFILE_MEM -#include +#include /** * Context for KFile over memory buffer. diff --git a/boards/triface/examples/triface/protocol.c b/boards/triface/examples/triface/protocol.c index 900531e6..78d262e2 100644 --- a/boards/triface/examples/triface/protocol.c +++ b/boards/triface/examples/triface/protocol.c @@ -59,7 +59,7 @@ #include #include -#include +#include #include #include diff --git a/boards/triface/examples/triface/protocol.h b/boards/triface/examples/triface/protocol.h index 8075bc87..e3c95b4e 100644 --- a/boards/triface/examples/triface/protocol.h +++ b/boards/triface/examples/triface/protocol.h @@ -42,7 +42,7 @@ #include -#include +#include void protocol_init(KFile *fd); void protocol_run(KFile *fd); diff --git a/test/run_tests.sh b/test/run_tests.sh index be0ed430..9034ac92 100755 --- a/test/run_tests.sh +++ b/test/run_tests.sh @@ -33,7 +33,6 @@ SRC_LIST=" bertos/algo/ramp.c bertos/drv/kdebug.c bertos/drv/timer.c - bertos/kern/kfile.c bertos/kern/monitor.c bertos/kern/proc.c bertos/kern/signal.c @@ -65,6 +64,7 @@ SRC_LIST=" bertos/io/kblock.c bertos/io/kblock_ram.c bertos/io/kblock_file.c + bertos/io/kfile.c " buildout='/dev/null' -- 2.25.1