mirror of
https://github.com/markqvist/OpenModem.git
synced 2025-12-10 14:15:43 -05:00
Working
This commit is contained in:
commit
c898b090dd
1049 changed files with 288572 additions and 0 deletions
281
bertos/io/kblock.c
Normal file
281
bertos/io/kblock.c
Normal file
|
|
@ -0,0 +1,281 @@
|
|||
/**
|
||||
* \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 2010 Develer S.r.l. (http://www.develer.com/)
|
||||
*
|
||||
* -->
|
||||
*
|
||||
* \author Francesco Sacchi <batt@develer.com>
|
||||
*
|
||||
* \brief KBlock interface
|
||||
*/
|
||||
|
||||
|
||||
#include "kblock.h"
|
||||
|
||||
#define LOG_LEVEL LOG_LVL_ERR
|
||||
#define LOG_FORMAT LOG_FMT_VERBOSE
|
||||
|
||||
#include <cfg/log.h>
|
||||
#include <string.h>
|
||||
|
||||
INLINE size_t kblock_readDirect(struct KBlock *b, block_idx_t index, void *buf, size_t offset, size_t size)
|
||||
{
|
||||
KB_ASSERT_METHOD(b, readDirect);
|
||||
ASSERT(index < b->blk_cnt);
|
||||
return b->priv.vt->readDirect(b, b->priv.blk_start + index, buf, offset, size);
|
||||
}
|
||||
|
||||
INLINE size_t kblock_writeDirect(struct KBlock *b, block_idx_t index, const void *buf, size_t offset, size_t size)
|
||||
{
|
||||
KB_ASSERT_METHOD(b, writeDirect);
|
||||
ASSERT(index < b->blk_cnt);
|
||||
return b->priv.vt->writeDirect(b, b->priv.blk_start + index, buf, offset, size);
|
||||
}
|
||||
|
||||
INLINE size_t kblock_readBuf(struct KBlock *b, void *buf, size_t offset, size_t size)
|
||||
{
|
||||
KB_ASSERT_METHOD(b, readBuf);
|
||||
ASSERT(offset + size <= b->blk_size);
|
||||
|
||||
return b->priv.vt->readBuf(b, buf, offset, size);
|
||||
}
|
||||
|
||||
INLINE size_t kblock_writeBuf(struct KBlock *b, const void *buf, size_t offset, size_t size)
|
||||
{
|
||||
KB_ASSERT_METHOD(b, writeBuf);
|
||||
ASSERT(offset + size <= b->blk_size);
|
||||
return b->priv.vt->writeBuf(b, buf, offset, size);
|
||||
}
|
||||
|
||||
INLINE int kblock_load(struct KBlock *b, block_idx_t index)
|
||||
{
|
||||
KB_ASSERT_METHOD(b, load);
|
||||
ASSERT(index < b->blk_cnt);
|
||||
|
||||
LOG_INFO("index %ld\n", index);
|
||||
return b->priv.vt->load(b, b->priv.blk_start + index);
|
||||
}
|
||||
|
||||
INLINE int kblock_store(struct KBlock *b, block_idx_t index)
|
||||
{
|
||||
KB_ASSERT_METHOD(b, store);
|
||||
ASSERT(index < b->blk_cnt);
|
||||
|
||||
LOG_INFO("index %ld\n", index);
|
||||
return b->priv.vt->store(b, b->priv.blk_start + index);
|
||||
}
|
||||
|
||||
INLINE void kblock_setDirty(struct KBlock *b, bool dirty)
|
||||
{
|
||||
if (dirty)
|
||||
b->priv.flags |= KB_CACHE_DIRTY;
|
||||
else
|
||||
b->priv.flags &= ~KB_CACHE_DIRTY;
|
||||
}
|
||||
|
||||
|
||||
size_t kblock_read(struct KBlock *b, block_idx_t idx, void *buf, size_t offset, size_t size)
|
||||
{
|
||||
ASSERT(b);
|
||||
ASSERT(buf);
|
||||
ASSERT(offset + size <= b->blk_size);
|
||||
LOG_INFO("blk_idx %ld, offset %u, size %u\n", idx, offset, size);
|
||||
|
||||
if (kblock_buffered(b) && idx == b->priv.curr_blk)
|
||||
return kblock_readBuf(b, buf, offset, size);
|
||||
else
|
||||
return kblock_readDirect(b, idx, buf, offset, size);
|
||||
}
|
||||
|
||||
|
||||
int kblock_flush(struct KBlock *b)
|
||||
{
|
||||
ASSERT(b);
|
||||
|
||||
if (!kblock_buffered(b))
|
||||
return 0;
|
||||
|
||||
if (kblock_cacheDirty(b))
|
||||
{
|
||||
LOG_INFO("flushing block %ld\n", b->priv.curr_blk);
|
||||
if (kblock_store(b, b->priv.curr_blk) == 0)
|
||||
kblock_setDirty(b, false);
|
||||
else
|
||||
return EOF;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
static bool kblock_loadPage(struct KBlock *b, block_idx_t idx)
|
||||
{
|
||||
ASSERT(b);
|
||||
|
||||
if (idx != b->priv.curr_blk)
|
||||
{
|
||||
LOG_INFO("loading block %ld\n", idx);
|
||||
if (kblock_flush(b) != 0 || kblock_load(b, idx) != 0)
|
||||
return false;
|
||||
|
||||
b->priv.curr_blk = idx;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
int kblock_trim(struct KBlock *b, block_idx_t start, block_idx_t count)
|
||||
{
|
||||
ASSERT(start + count <= b->blk_cnt);
|
||||
|
||||
if (kblock_buffered(b))
|
||||
{
|
||||
if (!kblock_loadPage(b, start))
|
||||
return EOF;
|
||||
}
|
||||
|
||||
b->priv.blk_start += start;
|
||||
b->priv.curr_blk = 0; // adjust logical address
|
||||
b->blk_cnt = count;
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
size_t kblock_write(struct KBlock *b, block_idx_t idx, const void *buf, size_t offset, size_t size)
|
||||
{
|
||||
ASSERT(b);
|
||||
ASSERT(buf);
|
||||
ASSERT(idx < b->blk_cnt);
|
||||
ASSERT(offset + size <= b->blk_size);
|
||||
|
||||
LOG_INFO("blk_idx %ld, offset %u, size %u\n", idx, offset, size);
|
||||
|
||||
if (kblock_buffered(b))
|
||||
{
|
||||
if (!kblock_loadPage(b, idx))
|
||||
return 0;
|
||||
|
||||
kblock_setDirty(b, true);
|
||||
return kblock_writeBuf(b, buf, offset, size);
|
||||
}
|
||||
else
|
||||
{
|
||||
#ifdef _DEBUG
|
||||
if (offset != 0 || size != b->blk_size)
|
||||
ASSERT(kblock_partialWrite(b));
|
||||
#endif
|
||||
return kblock_writeDirect(b, idx, buf, offset, size);
|
||||
}
|
||||
}
|
||||
|
||||
int kblock_copy(struct KBlock *b, block_idx_t src, block_idx_t dest)
|
||||
{
|
||||
ASSERT(b);
|
||||
ASSERT(src < b->blk_cnt);
|
||||
ASSERT(dest < b->blk_cnt);
|
||||
|
||||
if (kblock_buffered(b))
|
||||
{
|
||||
if (!kblock_loadPage(b, src))
|
||||
return EOF;
|
||||
|
||||
b->priv.curr_blk = dest;
|
||||
kblock_setDirty(b, true);
|
||||
return 0;
|
||||
}
|
||||
else if (kblock_partialWrite(b))
|
||||
{
|
||||
uint8_t buf[16];
|
||||
size_t blk_size = b->blk_size;
|
||||
size_t offset = 0;
|
||||
|
||||
while (blk_size)
|
||||
{
|
||||
size_t size = MIN(sizeof(buf), blk_size);
|
||||
if (kblock_readDirect(b, src, buf, offset, size) != size)
|
||||
return EOF;
|
||||
if (kblock_writeDirect(b, dest, buf, offset, size) != size)
|
||||
return EOF;
|
||||
|
||||
blk_size -= size;
|
||||
offset += size;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
else
|
||||
{
|
||||
ASSERT(0);
|
||||
return EOF;
|
||||
}
|
||||
}
|
||||
|
||||
int kblock_swLoad(struct KBlock *b, block_idx_t index)
|
||||
{
|
||||
/*
|
||||
* Since this is a low level API, the index here is a fisical index.
|
||||
* If we call another low level API, logical to fisical translation
|
||||
* would be applied twice.
|
||||
* In order to avoid this we subtract the start block index.
|
||||
*/
|
||||
ASSERT(index >= b->priv.blk_start);
|
||||
return (kblock_readDirect(b, index - b->priv.blk_start, b->priv.buf, 0, b->blk_size) == b->blk_size) ? 0 : EOF;
|
||||
}
|
||||
|
||||
int kblock_swStore(struct KBlock *b, block_idx_t index)
|
||||
{
|
||||
/*
|
||||
* Since this is a low level API, the index here is a fisical index.
|
||||
* If we call another low level API, logical to fisical translation
|
||||
* would be applied twice.
|
||||
* In order to avoid this we subtract the start block index.
|
||||
*/
|
||||
ASSERT(index >= b->priv.blk_start);
|
||||
return (kblock_writeDirect(b, index - b->priv.blk_start, b->priv.buf, 0, b->blk_size) == b->blk_size) ? 0 : EOF;
|
||||
}
|
||||
|
||||
size_t kblock_swReadBuf(struct KBlock *b, void *buf, size_t offset, size_t size)
|
||||
{
|
||||
ASSERT(buf);
|
||||
ASSERT(offset + size <= b->blk_size);
|
||||
|
||||
memcpy(buf, (uint8_t *)b->priv.buf + offset, size);
|
||||
return size;
|
||||
}
|
||||
|
||||
size_t kblock_swWriteBuf(struct KBlock *b, const void *buf, size_t offset, size_t size)
|
||||
{
|
||||
ASSERT(buf);
|
||||
ASSERT(offset + size <= b->blk_size);
|
||||
memcpy((uint8_t *)b->priv.buf + offset, buf, size);
|
||||
return size;
|
||||
}
|
||||
|
||||
int kblock_swClose(UNUSED_ARG(struct KBlock, *b))
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
400
bertos/io/kblock.h
Normal file
400
bertos/io/kblock.h
Normal file
|
|
@ -0,0 +1,400 @@
|
|||
/**
|
||||
* \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/)
|
||||
*
|
||||
* -->
|
||||
*
|
||||
* \defgroup io_kblock KBlock interface
|
||||
* \ingroup core
|
||||
* \{
|
||||
*
|
||||
* \brief KBlock interface
|
||||
*
|
||||
* A block device is a device which can only be read/written
|
||||
* with data blocks of constant size: flash memories,
|
||||
* SD cards, hard disks, etc...
|
||||
* This interface is designed to adapt to most block devices and
|
||||
* use peculiar features in order to save CPU time and memory space.
|
||||
*
|
||||
* There is no init function because you do not have to use this
|
||||
* structure directly, specific implementations will supply their own init
|
||||
* functions.
|
||||
*
|
||||
* Error handling is done in a way similar to standard C library: whenever a
|
||||
* function (eg. kblock_flush()) returns error, you need to check the error
|
||||
* code, which is implementation specific.
|
||||
*
|
||||
* Example of code flow:
|
||||
* \code
|
||||
* // init a KBlock-derived class
|
||||
* Flash fls;
|
||||
* flash_init(&fls.blk, 0);
|
||||
*
|
||||
* // use kblock_* functions to access the derived class
|
||||
* kblock_write(&fls.blk, ...);
|
||||
* if (kblock_flush(&fls.blk) == EOF)
|
||||
* {
|
||||
* // oops, error occurred!
|
||||
* int err = kblock_error(&fls.blk);
|
||||
* // handle Flash specific error conditions
|
||||
* // ...
|
||||
* // clear error condition
|
||||
* kblock_clearerr(&fls.blk);
|
||||
* }
|
||||
* \endcode
|
||||
*
|
||||
* \note The KBlock interface is optimized for block reads. If you need a
|
||||
* file-like access, you can use \ref kfile_block.
|
||||
*
|
||||
* \author Francesco Sacchi <batt@develer.com>
|
||||
*
|
||||
* $WIZ$ module_name = "kblock"
|
||||
*/
|
||||
|
||||
#ifndef IO_KBLOCK_H
|
||||
#define IO_KBLOCK_H
|
||||
|
||||
#include <cfg/compiler.h>
|
||||
#include <cfg/debug.h>
|
||||
#include <cfg/macros.h>
|
||||
|
||||
/** Type for addressing blocks in the device. */
|
||||
typedef uint32_t block_idx_t;
|
||||
|
||||
// Fwd Declaration
|
||||
struct KBlock;
|
||||
|
||||
/**
|
||||
* \name Prototypes for KBlock low level access functions.
|
||||
*
|
||||
* When writing a driver implementing the KBlock interface you can choose which
|
||||
* function subset to implement, but you have to set to NULL unimplemented
|
||||
* features.
|
||||
*
|
||||
* \{
|
||||
*/
|
||||
typedef size_t (* kblock_read_direct_t) (struct KBlock *b, block_idx_t index, void *buf, size_t offset, size_t size);
|
||||
typedef size_t (* kblock_write_direct_t) (struct KBlock *b, block_idx_t index, const void *buf, size_t offset, size_t size);
|
||||
|
||||
typedef size_t (* kblock_read_t) (struct KBlock *b, void *buf, size_t offset, size_t size);
|
||||
typedef size_t (* kblock_write_t) (struct KBlock *b, const void *buf, size_t offset, size_t size);
|
||||
typedef int (* kblock_load_t) (struct KBlock *b, block_idx_t index);
|
||||
typedef int (* kblock_store_t) (struct KBlock *b, block_idx_t index);
|
||||
|
||||
typedef int (* kblock_error_t) (struct KBlock *b);
|
||||
typedef void (* kblock_clearerr_t) (struct KBlock *b);
|
||||
typedef int (* kblock_close_t) (struct KBlock *b);
|
||||
/* \} */
|
||||
|
||||
/*
|
||||
* Table of interface functions for a KBlock device.
|
||||
*/
|
||||
typedef struct KBlockVTable
|
||||
{
|
||||
kblock_read_direct_t readDirect;
|
||||
kblock_write_direct_t writeDirect;
|
||||
|
||||
kblock_read_t readBuf;
|
||||
kblock_write_t writeBuf;
|
||||
kblock_load_t load;
|
||||
kblock_store_t store;
|
||||
|
||||
kblock_error_t error; // \sa kblock_error()
|
||||
kblock_clearerr_t clearerr; // \sa kblock_clearerr()
|
||||
|
||||
kblock_close_t close; // \sa kblock_close()
|
||||
} KBlockVTable;
|
||||
|
||||
|
||||
#define KB_BUFFERED BV(0) ///< Internal flag: true if the KBlock has a buffer
|
||||
#define KB_CACHE_DIRTY BV(1) ///< Internal flag: true if the cache is dirty
|
||||
#define KB_PARTIAL_WRITE BV(2) ///< Internal flag: true if the device allows partial block write
|
||||
|
||||
|
||||
/*
|
||||
* KBlock private members.
|
||||
* These are the private members of the KBlock interface, please do not
|
||||
* access these directly, use the KBlock API.
|
||||
*/
|
||||
typedef struct KBlockPriv
|
||||
{
|
||||
DB(id_t type); // Used to keep track, at runtime, of the class type.
|
||||
int flags; // Status and error flags.
|
||||
void *buf; // Pointer to the page buffer for RAM-cached KBlocks.
|
||||
block_idx_t blk_start; // Start block number when the device is trimmed. \sa kblock_trim().
|
||||
block_idx_t curr_blk; // Current cached block number in cached KBlocks.
|
||||
|
||||
const struct KBlockVTable *vt; // Virtual table of interface functions.
|
||||
} KBlockPriv;
|
||||
|
||||
/**
|
||||
* KBlock: interface for a generic block device.
|
||||
*
|
||||
*/
|
||||
typedef struct KBlock
|
||||
{
|
||||
KBlockPriv priv; ///< Interface private data, do not use directly.
|
||||
|
||||
/* Public access members */
|
||||
size_t blk_size; ///< Block size.
|
||||
block_idx_t blk_cnt; ///< Number of blocks available in the device.
|
||||
} KBlock;
|
||||
|
||||
|
||||
/**
|
||||
* Use a subset of the blocks on the device.
|
||||
*
|
||||
* This function is useful for partitioning a device and use it for
|
||||
* different purposes at the same time.
|
||||
*
|
||||
* This function will limit the number of blocks used on the device by setting
|
||||
* a start index and a number of blocks to be used counting from that index.
|
||||
*
|
||||
* The blocks outside this range are no more accessible.
|
||||
*
|
||||
* Logical block indexes will be mapped to physical indexes inside this new
|
||||
* range automatically. Even following calls to kblock_trim() will use logical
|
||||
* indexes, so, once trimmed, access can only be limited further and never
|
||||
* expanded back.
|
||||
*
|
||||
* Example:
|
||||
* \code
|
||||
* //...init KBlock device dev
|
||||
* kblock_trim(dev, 200, 1500); // Restrict access to the 200-1700 physical block range.
|
||||
* kblock_read(dev, 0, buf, 0, dev->blk_size); // Read from physical block #200.
|
||||
* kblock_trim(dev, 0, 300); // Restrict access to the 200-500 physical block range.
|
||||
* \endcode
|
||||
*
|
||||
* \param b KBlock device.
|
||||
* \param start The index of the start block for the limiting window in logical addressing units.
|
||||
* \param count The number of blocks to be used.
|
||||
*
|
||||
* \return 0 if all is OK, EOF on errors.
|
||||
*/
|
||||
int kblock_trim(struct KBlock *b, block_idx_t start, block_idx_t count);
|
||||
|
||||
|
||||
#define KB_ASSERT_METHOD(b, method) \
|
||||
do \
|
||||
{ \
|
||||
ASSERT(b); \
|
||||
ASSERT((b)->priv.vt); \
|
||||
ASSERT((b)->priv.vt->method); \
|
||||
} \
|
||||
while (0)
|
||||
|
||||
|
||||
/**
|
||||
* Get the current errors for the device.
|
||||
*
|
||||
* \note Calling this function will not clear the errors.
|
||||
*
|
||||
* \param b KBlock device.
|
||||
*
|
||||
* \return 0 if no error is present, a driver specific mask of errors otherwise.
|
||||
*
|
||||
* \sa kblock_clearerr()
|
||||
*/
|
||||
INLINE int kblock_error(struct KBlock *b)
|
||||
{
|
||||
KB_ASSERT_METHOD(b, error);
|
||||
return b->priv.vt->error(b);
|
||||
}
|
||||
|
||||
/**
|
||||
* Clear the errors of the device.
|
||||
*
|
||||
* \param b KBlock device.
|
||||
*
|
||||
*
|
||||
* \sa kblock_error()
|
||||
*/
|
||||
INLINE void kblock_clearerr(struct KBlock *b)
|
||||
{
|
||||
KB_ASSERT_METHOD(b, clearerr);
|
||||
b->priv.vt->clearerr(b);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Flush the cache (if any) to the device.
|
||||
*
|
||||
* This function will write any pending modifications to the device.
|
||||
* If the device does not have a cache, this function will do nothing.
|
||||
*
|
||||
* \return 0 if all is OK, EOF on errors.
|
||||
* \sa kblock_read(), kblock_write(), kblock_buffered().
|
||||
*/
|
||||
int kblock_flush(struct KBlock *b);
|
||||
|
||||
/**
|
||||
* Close the device.
|
||||
*
|
||||
* \param b KBlock device.
|
||||
*
|
||||
* \return 0 on success, EOF on errors.
|
||||
*/
|
||||
INLINE int kblock_close(struct KBlock *b)
|
||||
{
|
||||
KB_ASSERT_METHOD(b, close);
|
||||
return kblock_flush(b) | b->priv.vt->close(b);
|
||||
}
|
||||
|
||||
/**
|
||||
* \return true if the device \a b is buffered, false otherwise.
|
||||
* \param b KBlock device.
|
||||
* \sa kblock_cachedBlock(), kblock_cacheDirty().
|
||||
*/
|
||||
INLINE bool kblock_buffered(struct KBlock *b)
|
||||
{
|
||||
ASSERT(b);
|
||||
return (b->priv.flags & KB_BUFFERED);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* \return The current cached block number if the device is buffered.
|
||||
* \param b KBlock device.
|
||||
* \note This function will throw an ASSERT if called on a non buffered KBlock.
|
||||
* \sa kblock_buffered(), kblock_cacheDirty().
|
||||
*/
|
||||
INLINE block_idx_t kblock_cachedBlock(struct KBlock *b)
|
||||
{
|
||||
ASSERT(kblock_buffered(b));
|
||||
return b->priv.curr_blk;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Return the status of the internal cache.
|
||||
*
|
||||
* \param b KBlock device.
|
||||
* \return If the device supports buffering, returns true if the cache is dirty,
|
||||
* false if the cache is clean and coherent with device content.
|
||||
* \note This function will throw an ASSERT if called on a non buffered KBlock.
|
||||
* \sa kblock_cachedBlock(), kblock_buffered().
|
||||
*/
|
||||
INLINE bool kblock_cacheDirty(struct KBlock *b)
|
||||
{
|
||||
ASSERT(kblock_buffered(b));
|
||||
return kblock_buffered(b) && (b->priv.flags & KB_CACHE_DIRTY);
|
||||
}
|
||||
|
||||
/**
|
||||
* \return true if the device \a b supports partial block write. That is, you
|
||||
* can call kblock_write() with a size which is lesser than the block
|
||||
* size.
|
||||
* \param b KBlock device.
|
||||
* \sa kblock_write().
|
||||
*/
|
||||
INLINE bool kblock_partialWrite(struct KBlock *b)
|
||||
{
|
||||
ASSERT(b);
|
||||
return (b->priv.flags & KB_PARTIAL_WRITE);
|
||||
}
|
||||
|
||||
/**
|
||||
* Read data from the block device.
|
||||
*
|
||||
* This function will read \a size bytes from block \a idx starting at
|
||||
* address \a offset inside the block.
|
||||
*
|
||||
* Most block devices (almost all flash memories, for instance),
|
||||
* can efficiently read even a part of the block.
|
||||
*
|
||||
* \note This function can be slow if you try to partial read a block from
|
||||
* a device which does not support partial block reads and is opened
|
||||
* in unbuffered mode.
|
||||
*
|
||||
* \param b KBlock device.
|
||||
* \param idx the block number where you want to read.
|
||||
* \param buf a buffer where the data will be read.
|
||||
* \param offset the offset inside the block from which data reading will start.
|
||||
* \param size the size of data to be read.
|
||||
*
|
||||
* \return the number of bytes read.
|
||||
*
|
||||
* \sa kblock_write().
|
||||
*/
|
||||
size_t kblock_read(struct KBlock *b, block_idx_t idx, void *buf, size_t offset, size_t size);
|
||||
|
||||
|
||||
/**
|
||||
* Write data to the block device.
|
||||
*
|
||||
* This function will write \a size bytes to block \a idx starting at
|
||||
* address \a offset inside the block.
|
||||
*
|
||||
* \note Partial block writes are supported only on certain devices.
|
||||
* You can use kblock_partialWrite() in order to check if the device
|
||||
* has this feature or not.
|
||||
*
|
||||
* \note If the device is opened in buffered mode, this function will use
|
||||
* efficiently and trasparently the cache provided.
|
||||
* In order to be sure that all modifications are actually written
|
||||
* to the device you have to call kblock_flush().
|
||||
*
|
||||
* \param b KBlock device.
|
||||
* \param idx the block number where you want to write.
|
||||
* \param buf a pointer to the data to be written.
|
||||
* \param offset the offset inside the block from which data writing will start.
|
||||
* \param size the size of data to be written.
|
||||
*
|
||||
* \return the number of bytes written.
|
||||
*
|
||||
* \sa kblock_read(), kblock_flush(), kblock_buffered(), kblock_partialWrite().
|
||||
*/
|
||||
size_t kblock_write(struct KBlock *b, block_idx_t idx, const void *buf, size_t offset, size_t size);
|
||||
|
||||
/**
|
||||
* Copy one block to another.
|
||||
*
|
||||
* This function will copy the content of block \a src to block \a dest.
|
||||
*
|
||||
* \note This function is available only on devices which support partial
|
||||
* block write or are opened in buffered mode.
|
||||
*
|
||||
* \param b KBlock device.
|
||||
* \param src source block number.
|
||||
* \param dest destination block number.
|
||||
*
|
||||
* \return 0 if all is OK, EOF on errors.
|
||||
*/
|
||||
int kblock_copy(struct KBlock *b, block_idx_t src, block_idx_t dest);
|
||||
|
||||
int kblock_swLoad(struct KBlock *b, block_idx_t index);
|
||||
int kblock_swStore(struct KBlock *b, block_idx_t index);
|
||||
size_t kblock_swReadBuf(struct KBlock *b, void *buf, size_t offset, size_t size);
|
||||
size_t kblock_swWriteBuf(struct KBlock *b, const void *buf, size_t offset, size_t size);
|
||||
int kblock_swClose(struct KBlock *b);
|
||||
|
||||
/** \} */ //defgroup io_kblock
|
||||
|
||||
|
||||
#endif /* IO_KBLOCK_H */
|
||||
182
bertos/io/kblock_posix.c
Normal file
182
bertos/io/kblock_posix.c
Normal file
|
|
@ -0,0 +1,182 @@
|
|||
/**
|
||||
* \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 2010 Develer S.r.l. (http://www.develer.com/)
|
||||
*
|
||||
* -->
|
||||
*
|
||||
* \author Francesco Sacchi <batt@develer.com>
|
||||
*
|
||||
* \brief KBlock interface over libc files.
|
||||
*
|
||||
* notest: avr
|
||||
* notest: arm
|
||||
*/
|
||||
|
||||
|
||||
#include "kblock_posix.h"
|
||||
#include <string.h>
|
||||
#include <stdio.h>
|
||||
|
||||
|
||||
static int kblockposix_load(KBlock *b, block_idx_t index)
|
||||
{
|
||||
KBlockPosix *f = KBLOCKPOSIX_CAST(b);
|
||||
fseek(f->fp, index * b->blk_size, SEEK_SET);
|
||||
return (fread(f->b.priv.buf, 1, b->blk_size, f->fp) == b->blk_size) ? 0 : EOF;
|
||||
}
|
||||
|
||||
static int kblockposix_store(struct KBlock *b, block_idx_t index)
|
||||
{
|
||||
KBlockPosix *f = KBLOCKPOSIX_CAST(b);
|
||||
fseek(f->fp, index * b->blk_size, SEEK_SET);
|
||||
return (fwrite(f->b.priv.buf, 1, b->blk_size, f->fp) == b->blk_size) ? 0 : EOF;
|
||||
}
|
||||
|
||||
static size_t kblockposix_readBuf(struct KBlock *b, void *buf, size_t offset, size_t size)
|
||||
{
|
||||
KBlockPosix *f = KBLOCKPOSIX_CAST(b);
|
||||
memcpy(buf, (uint8_t *)f->b.priv.buf + offset, size);
|
||||
return size;
|
||||
}
|
||||
|
||||
static size_t kblockposix_readDirect(struct KBlock *b, block_idx_t index, void *buf, size_t offset, size_t size)
|
||||
{
|
||||
KBlockPosix *f = KBLOCKPOSIX_CAST(b);
|
||||
fseek(f->fp, index * b->blk_size + offset, SEEK_SET);
|
||||
return fread(buf, 1, size, f->fp);
|
||||
}
|
||||
|
||||
static size_t kblockposix_writeBuf(struct KBlock *b, const void *buf, size_t offset, size_t size)
|
||||
{
|
||||
KBlockPosix *f = KBLOCKPOSIX_CAST(b);
|
||||
memcpy((uint8_t *)f->b.priv.buf + offset, buf, size);
|
||||
return size;
|
||||
}
|
||||
|
||||
static size_t kblockposix_writeDirect(struct KBlock *b, block_idx_t index, const void *buf, size_t offset, size_t size)
|
||||
{
|
||||
KBlockPosix *f = KBLOCKPOSIX_CAST(b);
|
||||
ASSERT(buf);
|
||||
ASSERT(index < b->blk_cnt);
|
||||
fseek(f->fp, index * b->blk_size + offset, SEEK_SET);
|
||||
return fwrite(buf, 1, size, f->fp);
|
||||
}
|
||||
|
||||
static int kblockposix_error(struct KBlock *b)
|
||||
{
|
||||
KBlockPosix *f = KBLOCKPOSIX_CAST(b);
|
||||
return ferror(f->fp);
|
||||
}
|
||||
|
||||
|
||||
static void kblockposix_claererr(struct KBlock *b)
|
||||
{
|
||||
KBlockPosix *f = KBLOCKPOSIX_CAST(b);
|
||||
clearerr(f->fp);
|
||||
}
|
||||
|
||||
|
||||
static int kblockposix_close(struct KBlock *b)
|
||||
{
|
||||
KBlockPosix *f = KBLOCKPOSIX_CAST(b);
|
||||
|
||||
return fflush(f->fp) | fclose(f->fp);
|
||||
}
|
||||
|
||||
|
||||
static const KBlockVTable kblockposix_hwbuffered_vt =
|
||||
{
|
||||
.readDirect = kblockposix_readDirect,
|
||||
|
||||
.readBuf = kblockposix_readBuf,
|
||||
.writeBuf = kblockposix_writeBuf,
|
||||
.load = kblockposix_load,
|
||||
.store = kblockposix_store,
|
||||
|
||||
.error = kblockposix_error,
|
||||
.clearerr = kblockposix_claererr,
|
||||
.close = kblockposix_close,
|
||||
};
|
||||
|
||||
static const KBlockVTable kblockposix_swbuffered_vt =
|
||||
{
|
||||
.readDirect = kblockposix_readDirect,
|
||||
.writeDirect =kblockposix_writeDirect,
|
||||
|
||||
.readBuf = kblock_swReadBuf,
|
||||
.writeBuf = kblock_swWriteBuf,
|
||||
.load = kblock_swLoad,
|
||||
.store = kblock_swStore,
|
||||
|
||||
.error = kblockposix_error,
|
||||
.clearerr = kblockposix_claererr,
|
||||
.close = kblockposix_close,
|
||||
};
|
||||
|
||||
static const KBlockVTable kblockposix_unbuffered_vt =
|
||||
{
|
||||
.readDirect = kblockposix_readDirect,
|
||||
.writeDirect =kblockposix_writeDirect,
|
||||
|
||||
.error = kblockposix_error,
|
||||
.clearerr = kblockposix_claererr,
|
||||
.close = kblockposix_close,
|
||||
};
|
||||
|
||||
|
||||
|
||||
void kblockposix_init(KBlockPosix *f, FILE *fp, bool hwbuf, void *buf, size_t block_size, block_idx_t block_count)
|
||||
{
|
||||
ASSERT(f);
|
||||
ASSERT(fp);
|
||||
ASSERT(block_size);
|
||||
|
||||
memset(f, 0, sizeof(*f));
|
||||
|
||||
DB(f->b.priv.type = KBT_KBLOCKPOSIX);
|
||||
|
||||
f->fp = fp;
|
||||
f->b.blk_size = block_size;
|
||||
f->b.blk_cnt = block_count;
|
||||
|
||||
f->b.priv.flags |= KB_PARTIAL_WRITE;
|
||||
if (buf)
|
||||
{
|
||||
f->b.priv.flags |= KB_BUFFERED;
|
||||
f->b.priv.buf = buf;
|
||||
if (hwbuf)
|
||||
f->b.priv.vt = &kblockposix_hwbuffered_vt;
|
||||
else
|
||||
f->b.priv.vt = &kblockposix_swbuffered_vt;
|
||||
kblockposix_load(&f->b, 0);
|
||||
f->b.priv.curr_blk = 0;
|
||||
}
|
||||
else
|
||||
f->b.priv.vt = &kblockposix_unbuffered_vt;
|
||||
}
|
||||
67
bertos/io/kblock_posix.h
Normal file
67
bertos/io/kblock_posix.h
Normal file
|
|
@ -0,0 +1,67 @@
|
|||
/**
|
||||
* \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/)
|
||||
*
|
||||
* -->
|
||||
*
|
||||
* \author Francesco Sacchi <batt@develer.com>
|
||||
*
|
||||
* \brief KBlock interface on POSIX file.
|
||||
*
|
||||
* \author Francesco Sacchi <batt@develer.com>
|
||||
*
|
||||
* $WIZ$ module_name = "kfile_posix"
|
||||
* $WIZ$ module_depends = "kblock"
|
||||
*/
|
||||
|
||||
#ifndef KBLOCK_POSIX_H
|
||||
#define KBLOCK_POSIX_H
|
||||
|
||||
#include "kblock.h"
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
typedef struct KBlockPosix
|
||||
{
|
||||
KBlock b;
|
||||
FILE *fp;
|
||||
} KBlockPosix;
|
||||
|
||||
#define KBT_KBLOCKPOSIX MAKE_ID('K', 'B', 'F', 'L')
|
||||
|
||||
|
||||
INLINE KBlockPosix *KBLOCKPOSIX_CAST(KBlock *b)
|
||||
{
|
||||
ASSERT(b->priv.type == KBT_KBLOCKPOSIX);
|
||||
return (KBlockPosix *)b;
|
||||
}
|
||||
|
||||
void kblockposix_init(KBlockPosix *f, FILE *fp, bool hwbuf, void *buf, size_t block_size, block_idx_t block_count);
|
||||
|
||||
#endif /* KBLOCK_POSIX_H */
|
||||
171
bertos/io/kblock_ram.c
Normal file
171
bertos/io/kblock_ram.c
Normal file
|
|
@ -0,0 +1,171 @@
|
|||
/**
|
||||
* \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 2010 Develer S.r.l. (http://www.develer.com/)
|
||||
*
|
||||
* -->
|
||||
*
|
||||
* \author Francesco Sacchi <batt@develer.com>
|
||||
*
|
||||
* \brief KBlock interface on RAM memory
|
||||
*
|
||||
* \author Francesco Sacchi <batt@develer.com>
|
||||
*
|
||||
* $WIZ$ module_name = "kfile_ram"
|
||||
* $WIZ$ module_depends = "kblock"
|
||||
*/
|
||||
|
||||
|
||||
#include "kblock_ram.h"
|
||||
#include <string.h>
|
||||
|
||||
|
||||
static int kblockram_load(KBlock *b, block_idx_t index)
|
||||
{
|
||||
KBlockRam *r = KBLOCKRAM_CAST(b);
|
||||
memcpy(r->b.priv.buf, r->membuf + index * r->b.blk_size, r->b.blk_size);
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int kblockram_store(struct KBlock *b, block_idx_t index)
|
||||
{
|
||||
KBlockRam *r = KBLOCKRAM_CAST(b);
|
||||
memcpy(r->membuf + index * r->b.blk_size, r->b.priv.buf, r->b.blk_size);
|
||||
return 0;
|
||||
}
|
||||
|
||||
static size_t kblockram_readBuf(struct KBlock *b, void *buf, size_t offset, size_t size)
|
||||
{
|
||||
KBlockRam *r = KBLOCKRAM_CAST(b);
|
||||
memcpy(buf, (uint8_t *)r->b.priv.buf + offset, size);
|
||||
return size;
|
||||
}
|
||||
|
||||
static size_t kblockram_readDirect(struct KBlock *b, block_idx_t index, void *buf, size_t offset, size_t size)
|
||||
{
|
||||
KBlockRam *r = KBLOCKRAM_CAST(b);
|
||||
memcpy(buf, r->membuf + index * r->b.blk_size + offset, size);
|
||||
return size;
|
||||
}
|
||||
|
||||
static size_t kblockram_writeBuf(struct KBlock *b, const void *buf, size_t offset, size_t size)
|
||||
{
|
||||
KBlockRam *r = KBLOCKRAM_CAST(b);
|
||||
memcpy((uint8_t *)r->b.priv.buf + offset, buf, size);
|
||||
return size;
|
||||
}
|
||||
|
||||
static size_t kblockram_writeDirect(struct KBlock *b, block_idx_t index, const void *buf, size_t offset, size_t size)
|
||||
{
|
||||
KBlockRam *r = KBLOCKRAM_CAST(b);
|
||||
ASSERT(buf);
|
||||
ASSERT(index < b->blk_cnt);
|
||||
|
||||
memcpy(r->membuf + index * r->b.blk_size + offset, buf, size);
|
||||
return size;
|
||||
}
|
||||
|
||||
static int kblockram_dummy(UNUSED_ARG(struct KBlock *,b))
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
static const KBlockVTable kblockram_hwbuffered_vt =
|
||||
{
|
||||
.readDirect = kblockram_readDirect,
|
||||
|
||||
.readBuf = kblockram_readBuf,
|
||||
.writeBuf = kblockram_writeBuf,
|
||||
.load = kblockram_load,
|
||||
.store = kblockram_store,
|
||||
|
||||
.error = kblockram_dummy,
|
||||
.clearerr = (kblock_clearerr_t)kblockram_dummy,
|
||||
.close = kblockram_dummy,
|
||||
};
|
||||
|
||||
|
||||
static const KBlockVTable kblockram_swbuffered_vt =
|
||||
{
|
||||
.readDirect = kblockram_readDirect,
|
||||
.writeDirect = kblockram_writeDirect,
|
||||
|
||||
.readBuf = kblock_swReadBuf,
|
||||
.writeBuf = kblock_swWriteBuf,
|
||||
.load = kblock_swLoad,
|
||||
.store = kblock_swStore,
|
||||
|
||||
.error = kblockram_dummy,
|
||||
.clearerr = (kblock_clearerr_t)kblockram_dummy,
|
||||
.close = kblockram_dummy,
|
||||
};
|
||||
|
||||
static const KBlockVTable kblockram_unbuffered_vt =
|
||||
{
|
||||
.readDirect = kblockram_readDirect,
|
||||
.writeDirect = kblockram_writeDirect,
|
||||
|
||||
.error = kblockram_dummy,
|
||||
.clearerr = (kblock_clearerr_t)kblockram_dummy,
|
||||
.close = kblockram_dummy,
|
||||
};
|
||||
|
||||
void kblockram_init(KBlockRam *ram, void *buf, size_t size, size_t block_size, bool buffered, bool hwbuffered)
|
||||
{
|
||||
ASSERT(buf);
|
||||
ASSERT(size);
|
||||
ASSERT(block_size);
|
||||
|
||||
memset(ram, 0, sizeof(*ram));
|
||||
|
||||
DB(ram->b.priv.type = KBT_KBLOCKRAM);
|
||||
ram->b.blk_size = block_size;
|
||||
ram->b.priv.flags |= KB_PARTIAL_WRITE;
|
||||
|
||||
if (buffered)
|
||||
{
|
||||
ram->b.priv.flags |= KB_BUFFERED;
|
||||
ram->b.blk_cnt = (size / block_size) - 1;
|
||||
ram->b.priv.buf = buf;
|
||||
// First page used as page buffer
|
||||
ram->membuf = (uint8_t *)buf + block_size;
|
||||
|
||||
if (hwbuffered)
|
||||
ram->b.priv.vt = &kblockram_hwbuffered_vt;
|
||||
else
|
||||
ram->b.priv.vt = &kblockram_swbuffered_vt;
|
||||
|
||||
kblockram_load(&ram->b, 0);
|
||||
}
|
||||
else
|
||||
{
|
||||
ram->b.blk_cnt = (size / block_size);
|
||||
ram->membuf = (uint8_t *)buf;
|
||||
ram->b.priv.vt = &kblockram_unbuffered_vt;
|
||||
}
|
||||
}
|
||||
61
bertos/io/kblock_ram.h
Normal file
61
bertos/io/kblock_ram.h
Normal file
|
|
@ -0,0 +1,61 @@
|
|||
/**
|
||||
* \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 KBlock interface
|
||||
*
|
||||
* \author Francesco Sacchi <batt@develer.com>
|
||||
*/
|
||||
|
||||
#ifndef KBLOCK_RAM_H
|
||||
#define KBLOCK_RAM_H
|
||||
|
||||
#include "kblock.h"
|
||||
|
||||
|
||||
typedef struct KBlockRam
|
||||
{
|
||||
KBlock b;
|
||||
uint8_t *membuf;
|
||||
} KBlockRam;
|
||||
|
||||
#define KBT_KBLOCKRAM MAKE_ID('K', 'B', 'R', 'M')
|
||||
|
||||
|
||||
INLINE KBlockRam *KBLOCKRAM_CAST(KBlock *b)
|
||||
{
|
||||
ASSERT(b->priv.type == KBT_KBLOCKRAM);
|
||||
return (KBlockRam *)b;
|
||||
}
|
||||
|
||||
void kblockram_init(KBlockRam *ram, void *buf, size_t size, size_t block_size, bool buffered, bool hwbuffered);
|
||||
|
||||
#endif /* KBLOCK_RAM_H */
|
||||
304
bertos/io/kfile.c
Normal file
304
bertos/io/kfile.c
Normal file
|
|
@ -0,0 +1,304 @@
|
|||
/**
|
||||
* \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 2007 Develer S.r.l. (http://www.develer.com/)
|
||||
* -->
|
||||
*
|
||||
* \brief Virtual KFile I/O interface.
|
||||
*
|
||||
* This module implements some generic I/O interfaces for kfile.
|
||||
*
|
||||
* \author Francesco Sacchi <batt@develer.com>
|
||||
* \author Daniele Basile <asterix@develer.com>
|
||||
*/
|
||||
|
||||
#include "kfile.h"
|
||||
|
||||
#include "cfg/cfg_kfile.h"
|
||||
#include <cfg/debug.h>
|
||||
#include <cfg/log.h>
|
||||
|
||||
#include <drv/timer.h>
|
||||
#include <mware/formatwr.h>
|
||||
|
||||
#include <string.h>
|
||||
|
||||
/*
|
||||
* 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 */
|
||||
|
||||
|
||||
kfile_off_t kfile_copy(KFile *src, KFile *dst, kfile_off_t size)
|
||||
{
|
||||
char buf[32];
|
||||
kfile_off_t cp_len = 0;
|
||||
|
||||
while (size)
|
||||
{
|
||||
size_t len = MIN(sizeof(buf), (size_t)size);
|
||||
if (kfile_read(src, buf, len) != len)
|
||||
break;
|
||||
|
||||
size_t wr_len = kfile_write(dst, buf, len);
|
||||
cp_len += wr_len;
|
||||
size -= len;
|
||||
|
||||
if (wr_len != len)
|
||||
break;
|
||||
}
|
||||
|
||||
return cp_len;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 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;
|
||||
}
|
||||
|
||||
/* Bound check */
|
||||
if (seek_pos + offset > fd->size)
|
||||
LOG_INFO("seek outside EOF\n");
|
||||
|
||||
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 flush the file.
|
||||
*/
|
||||
int kfile_genericClose(struct KFile *fd)
|
||||
{
|
||||
return kfile_flush(fd);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 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;
|
||||
}
|
||||
|
||||
359
bertos/io/kfile.h
Normal file
359
bertos/io/kfile.h
Normal file
|
|
@ -0,0 +1,359 @@
|
|||
/**
|
||||
* \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 2004 Develer S.r.l. (http://www.develer.com/)
|
||||
* Copyright 1999, 2000, 2001, 2003 Bernie Innocenti <bernie@codewiz.org>
|
||||
*
|
||||
* -->
|
||||
*
|
||||
* \defgroup io_kfile KFile interface
|
||||
* \ingroup core
|
||||
* \{
|
||||
*
|
||||
* \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.
|
||||
*
|
||||
* \author Bernie Innocenti <bernie@codewiz.org>
|
||||
* \author Francesco Sacchi <batt@develer.com>
|
||||
* \author Daniele Basile <asterix@develer.com>
|
||||
*
|
||||
* $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 <cfg/compiler.h>
|
||||
#include <cfg/debug.h>
|
||||
#include <cfg/macros.h>
|
||||
|
||||
/* 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);
|
||||
|
||||
/** @name KFile access functions
|
||||
* Interface functions for KFile access.
|
||||
* @{
|
||||
*/
|
||||
|
||||
/**
|
||||
* Read \a size bytes from file \a fd into \a buf.
|
||||
*
|
||||
* This function reads at most the number of requested bytes into the
|
||||
* provided buffer.
|
||||
* The value returned may be less than the requested bytes in case EOF is
|
||||
* reached OR an error occurred. You need to check the error conditions
|
||||
* using kfile_error() to understand which case happened.
|
||||
*
|
||||
* \note This function will block if there are less than \a size bytes
|
||||
* to read.
|
||||
*
|
||||
* \param fd KFile context.
|
||||
* \param buf User provided buffer.
|
||||
* \param size Number of bytes to read.
|
||||
* \return Number of bytes read.
|
||||
*/
|
||||
INLINE size_t kfile_read(struct KFile *fd, void *buf, size_t size)
|
||||
{
|
||||
ASSERT(fd->read);
|
||||
return fd->read(fd, buf, size);
|
||||
}
|
||||
int kfile_gets(struct KFile *fd, char *buf, int size);
|
||||
int kfile_gets_echo(struct KFile *fd, char *buf, int size, bool echo);
|
||||
|
||||
/**
|
||||
* Copy \a size bytes from file \a src to \a dst.
|
||||
*
|
||||
* \param src Source KFile.
|
||||
* \param dst Destionation KFile.
|
||||
* \param size number of bytes to copy.
|
||||
* \return the number of bytes copied.
|
||||
*/
|
||||
kfile_off_t kfile_copy(KFile *src, KFile *dst, kfile_off_t size);
|
||||
|
||||
/**
|
||||
* Write \a size bytes from buffer \a buf into KFile \a fd.
|
||||
*
|
||||
* Return value may be less than \a size.
|
||||
*
|
||||
* \param fd KFile context.
|
||||
* \param buf User provided data.
|
||||
* \param size Number of bytes to write.
|
||||
* \return Number of bytes written.
|
||||
*/
|
||||
INLINE size_t kfile_write(struct KFile *fd, const void *buf, size_t size)
|
||||
{
|
||||
ASSERT(fd->write);
|
||||
return fd->write(fd, buf, size);
|
||||
}
|
||||
|
||||
int kfile_printf(struct KFile *fd, const char *format, ...);
|
||||
int kfile_print(struct KFile *fd, const char *s);
|
||||
|
||||
/**
|
||||
* Seek into file (if seekable).
|
||||
*
|
||||
* Move \a fd file seek position of \a offset bytes from \a whence.
|
||||
*
|
||||
* \param fd KFile context.
|
||||
* \param offset Offset bytes to move from position \a whence.
|
||||
* \param whence Position where to start the seek.
|
||||
* \return Current postion in the file.
|
||||
*/
|
||||
INLINE kfile_off_t kfile_seek(struct KFile *fd, kfile_off_t offset, KSeekMode whence)
|
||||
{
|
||||
ASSERT(fd->seek);
|
||||
return fd->seek(fd, offset, whence);
|
||||
}
|
||||
|
||||
/**
|
||||
* Close and reopen file \a fd.
|
||||
* The reopening is done with the former file parameters and access modes.
|
||||
*/
|
||||
INLINE KFile * kfile_reopen(struct KFile *fd)
|
||||
{
|
||||
ASSERT(fd->reopen);
|
||||
return fd->reopen(fd);
|
||||
}
|
||||
|
||||
/**
|
||||
* Close file.
|
||||
* \return 0 on success, EOF on errors.
|
||||
*/
|
||||
INLINE int kfile_close(struct KFile *fd)
|
||||
{
|
||||
ASSERT(fd->close);
|
||||
return fd->close(fd);
|
||||
}
|
||||
|
||||
/**
|
||||
* Flush file I/O.
|
||||
* \return 0 on success, EOF on errors.
|
||||
*/
|
||||
INLINE int kfile_flush(struct KFile *fd)
|
||||
{
|
||||
ASSERT(fd->flush);
|
||||
return fd->flush(fd);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get file error mask.
|
||||
* \return 0 on success or file error code, device specific.
|
||||
*/
|
||||
INLINE int kfile_error(struct KFile *fd)
|
||||
{
|
||||
ASSERT(fd->error);
|
||||
return fd->error(fd);
|
||||
}
|
||||
|
||||
/**
|
||||
* Clear errors.
|
||||
*/
|
||||
INLINE void kfile_clearerr(struct KFile *fd)
|
||||
{
|
||||
ASSERT(fd->clearerr);
|
||||
fd->clearerr(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.
|
||||
void kfile_resync(KFile *fd, mtime_t delay);
|
||||
void kfile_init(struct KFile *fd);
|
||||
/* @} */
|
||||
|
||||
/** \} */ //Defgroup io_kfile
|
||||
|
||||
/*
|
||||
* 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 */
|
||||
131
bertos/io/kfile_block.c
Normal file
131
bertos/io/kfile_block.c
Normal file
|
|
@ -0,0 +1,131 @@
|
|||
/**
|
||||
* \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 2010 Develer S.r.l. (http://www.develer.com/)
|
||||
*
|
||||
* -->
|
||||
*
|
||||
* \brief KFile interface over a KBlock.
|
||||
*
|
||||
* \author Francesco Sacchi <batt@develer.com>
|
||||
* \author Daniele Basile <asterix@develer.com>
|
||||
*
|
||||
*/
|
||||
|
||||
#include "kfile_block.h"
|
||||
#include <string.h>
|
||||
|
||||
|
||||
/**
|
||||
* ID for KFile over a KBlock.
|
||||
*/
|
||||
#define KFT_KFILEBLOCK MAKE_ID('K', 'F', 'B', 'L')
|
||||
|
||||
/**
|
||||
* Convert + ASSERT from generic KFile to KFileBlock.
|
||||
*/
|
||||
INLINE KFileBlock * KFILEBLOCK_CAST(KFile *fd)
|
||||
{
|
||||
ASSERT(fd->_type == KFT_KFILEBLOCK);
|
||||
return (KFileBlock *)fd;
|
||||
}
|
||||
|
||||
#define KFILEBLOCK(dir, fd, buf, size) \
|
||||
({ \
|
||||
KFileBlock *fb = KFILEBLOCK_CAST(fd); \
|
||||
size_t len = 0; \
|
||||
while (size) \
|
||||
{ \
|
||||
block_idx_t id = (fd)->seek_pos / fb->blk->blk_size; \
|
||||
if (id >= fb->blk->blk_cnt) \
|
||||
break; \
|
||||
size_t offset = (fd)->seek_pos % fb->blk->blk_size; \
|
||||
size_t count = MIN(size, (size_t)(fb->blk->blk_size - offset)); \
|
||||
size_t ret_len = kblock_##dir(fb->blk, id, buf, offset, count); \
|
||||
size -= ret_len; \
|
||||
(fd)->seek_pos += ret_len; \
|
||||
buf = buf + ret_len; \
|
||||
len += ret_len; \
|
||||
if (ret_len != count) \
|
||||
break; \
|
||||
} \
|
||||
len; \
|
||||
})
|
||||
|
||||
static size_t kfileblock_read(struct KFile *fd, void *_buf, size_t size)
|
||||
{
|
||||
uint8_t *buf = (uint8_t *)_buf;
|
||||
return KFILEBLOCK(read, fd, buf, size);
|
||||
}
|
||||
|
||||
static size_t kfileblock_write(struct KFile *fd, const void *_buf, size_t size)
|
||||
{
|
||||
const uint8_t *buf = (const uint8_t *)_buf;
|
||||
return KFILEBLOCK(write, fd, buf, size);
|
||||
}
|
||||
|
||||
static int kfileblock_flush(struct KFile *fd)
|
||||
{
|
||||
KFileBlock *fb = KFILEBLOCK_CAST(fd);
|
||||
return kblock_flush(fb->blk);
|
||||
}
|
||||
|
||||
static int kfileblock_error(struct KFile *fd)
|
||||
{
|
||||
KFileBlock *fb = KFILEBLOCK_CAST(fd);
|
||||
return kblock_error(fb->blk);
|
||||
}
|
||||
|
||||
static void kfileblock_clearerr(struct KFile *fd)
|
||||
{
|
||||
KFileBlock *fb = KFILEBLOCK_CAST(fd);
|
||||
return kblock_clearerr(fb->blk);
|
||||
}
|
||||
|
||||
static int kfileblock_close(struct KFile *fd)
|
||||
{
|
||||
KFileBlock *fb = KFILEBLOCK_CAST(fd);
|
||||
return kblock_close(fb->blk);
|
||||
}
|
||||
|
||||
void kfileblock_init(KFileBlock *fb, KBlock *blk)
|
||||
{
|
||||
ASSERT(fb);
|
||||
ASSERT(blk);
|
||||
memset(fb, 0, sizeof(*fb));
|
||||
kfile_init(&fb->fd);
|
||||
DB(fb->fd._type = KFT_KFILEBLOCK);
|
||||
fb->blk = blk;
|
||||
fb->fd.size = blk->blk_cnt * blk->blk_size;
|
||||
fb->fd.read = kfileblock_read;
|
||||
fb->fd.write = kfileblock_write;
|
||||
fb->fd.flush = kfileblock_flush;
|
||||
fb->fd.error = kfileblock_error;
|
||||
fb->fd.clearerr = kfileblock_clearerr;
|
||||
fb->fd.close = kfileblock_close;
|
||||
}
|
||||
100
bertos/io/kfile_block.h
Normal file
100
bertos/io/kfile_block.h
Normal file
|
|
@ -0,0 +1,100 @@
|
|||
/**
|
||||
* \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 2010 Develer S.r.l. (http://www.develer.com/)
|
||||
*
|
||||
* -->
|
||||
*
|
||||
* \defgroup kfile_block KFile interface over KBlock
|
||||
* \ingroup core
|
||||
* \{
|
||||
*
|
||||
* \brief KFile interface over a KBlock.
|
||||
*
|
||||
* With this module, you can access a KBlock device
|
||||
* with the handy KFile interface.
|
||||
* In order to achieve this, the block device must support partial block write.
|
||||
*
|
||||
* Error codes returned by kfile_error() are specific of the underlying
|
||||
* KBlock implementation.
|
||||
*
|
||||
* Make sure you have trimmed the KBlock to avoid overwriting something.
|
||||
* Example:
|
||||
* \code
|
||||
* // init a derived instance of KBlock
|
||||
* // any will do.
|
||||
* Flash flash;
|
||||
* flash_init(&flash, 0);
|
||||
* kblock_trim(&flash.blk, trim_start, internal_flash.blk.blk_cnt - trim_start);
|
||||
*
|
||||
* // now create and initialize the kfile_block instance
|
||||
* KFileBlock kfb;
|
||||
* kfileblock_init(&kfb, &flash.blk);
|
||||
*
|
||||
* // now you can access the Flash in a file like fashion
|
||||
* kfile_read(&kfb.fd, buf, 20);
|
||||
* \endcode
|
||||
*
|
||||
* \author Francesco Sacchi <batt@develer.com>
|
||||
* \author Daniele Basile <asterix@develer.com>
|
||||
*
|
||||
* $WIZ$ module_name = "kfile_block"
|
||||
* $WIZ$ module_depends = "kfile", "kblock"
|
||||
*/
|
||||
|
||||
#ifndef IO_KFILE_BLOCK_H
|
||||
#define IO_KFILE_BLOCK_H
|
||||
|
||||
#include <cfg/compiler.h>
|
||||
#include <io/kblock.h>
|
||||
#include <io/kfile.h>
|
||||
|
||||
/**
|
||||
* KFileBlock context.
|
||||
*/
|
||||
typedef struct KFileBlock
|
||||
{
|
||||
KFile fd; ///< KFile context
|
||||
KBlock *blk; ///< KBlock device
|
||||
} KFileBlock;
|
||||
|
||||
/**
|
||||
* Init a KFile over KBlock.
|
||||
* After this you can access your KBlock device with a handy KFile interface.
|
||||
*
|
||||
* \note The block device must support partial block write in order to support
|
||||
* random write access.
|
||||
*
|
||||
* \param fb KFileBlock context.
|
||||
* \param blk block device to be accessed with a KFile interface.
|
||||
*/
|
||||
void kfileblock_init(KFileBlock *fb, KBlock *blk);
|
||||
|
||||
/** \} */ //defgroup kfile_block
|
||||
|
||||
#endif /* IO_KFILE_KBLOCK_H */
|
||||
334
bertos/io/kfile_test.c
Normal file
334
bertos/io/kfile_test.c
Normal file
|
|
@ -0,0 +1,334 @@
|
|||
/**
|
||||
* \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 2007 Develer S.r.l. (http://www.develer.com/)
|
||||
*
|
||||
* -->
|
||||
*
|
||||
* \brief Test suite for virtual KFile I/O interface.
|
||||
*
|
||||
* This module implements a test for some generic I/O interfaces for kfile.
|
||||
*
|
||||
* \author Francesco Sacchi <batt@develer.com>
|
||||
* \author Daniele Basile <asterix@develer.com>
|
||||
*/
|
||||
|
||||
|
||||
#include "kfile.h"
|
||||
#include <struct/kfile_mem.h>
|
||||
|
||||
#include "cfg/cfg_kfile.h"
|
||||
#include <cfg/debug.h>
|
||||
#include <cfg/test.h>
|
||||
#include <cfg/module.h>
|
||||
|
||||
// Define logging setting (for cfg/log.h module).
|
||||
#define LOG_LEVEL KFILE_LOG_LEVEL
|
||||
#define LOG_FORMAT KFILE_LOG_FORMAT
|
||||
#include <cfg/log.h>
|
||||
|
||||
#include <mware/formatwr.h>
|
||||
|
||||
#include <string.h>
|
||||
|
||||
MOD_DEFINE(kfile_test);
|
||||
|
||||
// Size of the "virtual" disk that
|
||||
// we want to test.
|
||||
#define BUF_TEST_LEN 3209
|
||||
|
||||
// Buffer for test
|
||||
uint8_t test_buf[BUF_TEST_LEN];
|
||||
uint8_t test_buf_save[BUF_TEST_LEN];
|
||||
|
||||
uint8_t test_disk[BUF_TEST_LEN];
|
||||
KFileMem mem;
|
||||
|
||||
/*
|
||||
* Help function to init disk and the buffers.
|
||||
*/
|
||||
static void init_testBuf(void)
|
||||
{
|
||||
|
||||
kprintf("Init fake buffer..\n");
|
||||
for (int i = 0; i < BUF_TEST_LEN; i++)
|
||||
{
|
||||
test_disk[i] = i;
|
||||
kprintf("%d ", test_disk[i]);
|
||||
}
|
||||
kprintf("\nend\n");
|
||||
|
||||
memset(test_buf, 0, sizeof(test_buf));
|
||||
memset(test_buf_save, 0, sizeof(test_buf_save));
|
||||
}
|
||||
|
||||
/**
|
||||
* KFile read/write subtest.
|
||||
* Try to write/read in the same \a f file location \a size bytes.
|
||||
* \return true if all is ok, false otherwise
|
||||
* \note Restore file position at exit (if no error)
|
||||
* \note Test buffer \a buf must be filled with
|
||||
* the following statement:
|
||||
* <pre>
|
||||
* buf[i] = i & 0xff
|
||||
* </pre>
|
||||
*/
|
||||
static bool kfile_rwTest(KFile *f, uint8_t *buf, size_t size)
|
||||
{
|
||||
/*
|
||||
* Write test buffer
|
||||
*/
|
||||
if (kfile_write(f, buf, size) != size)
|
||||
{
|
||||
LOG_ERR("error writing buf");
|
||||
return false;
|
||||
}
|
||||
|
||||
kfile_seek(f, -(kfile_off_t)size, KSM_SEEK_CUR);
|
||||
|
||||
/*
|
||||
* Reset test buffer
|
||||
*/
|
||||
memset(buf, 0, size);
|
||||
|
||||
/*
|
||||
* Read file in test buffer
|
||||
*/
|
||||
if (kfile_read(f, buf, size) != size)
|
||||
{
|
||||
LOG_ERR("error reading buf");
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
kfile_seek(f, -(kfile_off_t)size, KSM_SEEK_CUR);
|
||||
|
||||
/*
|
||||
* Check test result
|
||||
*/
|
||||
for (size_t i = 0; i < size; i++)
|
||||
{
|
||||
if (buf[i] != (i & 0xff))
|
||||
{
|
||||
LOG_ERR("error comparing at index [%d] read [%02x] expected [%02x]\n", i, buf[i], i);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* KFile read/write test.
|
||||
* This function write and read \a test_buf long \a size
|
||||
* on \a fd handler.
|
||||
* \a save_buf can be NULL or a buffer where to save previous file content.
|
||||
*/
|
||||
int kfile_testRunGeneric(KFile *fd, uint8_t *test_buf, uint8_t *save_buf, size_t size)
|
||||
{
|
||||
|
||||
/*
|
||||
* Part of test buf size that you would write.
|
||||
* This var is used in test 3 to check kfile_write
|
||||
* when writing beyond filesize limit.
|
||||
*/
|
||||
kfile_off_t len = size / 2;
|
||||
|
||||
|
||||
/* Fill test buffer */
|
||||
for (size_t i = 0; i < size; i++)
|
||||
test_buf[i] = (i & 0xff);
|
||||
|
||||
/*
|
||||
* If necessary, user can save content,
|
||||
* for later restore.
|
||||
*/
|
||||
if (save_buf)
|
||||
{
|
||||
LOG_INFO("Saved content..form [%ld] to [%ld]\n", fd->seek_pos, fd->seek_pos + size);
|
||||
kfile_read(fd, save_buf, size);
|
||||
}
|
||||
|
||||
/* TEST 1 BEGIN. */
|
||||
LOG_INFO("Test 1: write from pos 0 to [%ld]\n", (long)size);
|
||||
|
||||
/*
|
||||
* Seek to addr 0.
|
||||
*/
|
||||
if (kfile_seek(fd, 0, KSM_SEEK_SET) != 0)
|
||||
goto kfile_test_end;
|
||||
|
||||
/*
|
||||
* Test read/write to address 0..size
|
||||
*/
|
||||
if (!kfile_rwTest(fd, test_buf, size))
|
||||
goto kfile_test_end;
|
||||
|
||||
LOG_INFO("Test 1: ok!\n");
|
||||
|
||||
/*
|
||||
* Restore previous read content.
|
||||
*/
|
||||
if (save_buf)
|
||||
{
|
||||
kfile_seek(fd, 0, KSM_SEEK_SET);
|
||||
|
||||
if (kfile_write(fd, save_buf, size) != size)
|
||||
goto kfile_test_end;
|
||||
|
||||
LOG_INFO("Restore content..form [%ld] to [%ld]\n", fd->seek_pos, fd->seek_pos + size);
|
||||
}
|
||||
/* TEST 1 END. */
|
||||
|
||||
/* TEST 2 BEGIN. */
|
||||
LOG_INFO("Test 2: write from pos [%ld] to [%ld]\n", fd->size/2 , fd->size/2 + size);
|
||||
|
||||
/*
|
||||
* Go to half test size.
|
||||
*/
|
||||
kfile_seek(fd, (fd->size / 2), KSM_SEEK_SET);
|
||||
|
||||
/*
|
||||
* If necessary, user can save content
|
||||
* for later restore.
|
||||
*/
|
||||
if (save_buf)
|
||||
{
|
||||
kfile_read(fd, save_buf, size);
|
||||
kfile_seek(fd, -(kfile_off_t)size, KSM_SEEK_CUR);
|
||||
LOG_INFO("Saved content..form [%ld] to [%ld]\n", fd->seek_pos, fd->seek_pos + size);
|
||||
}
|
||||
|
||||
/*
|
||||
* Test read/write to address filesize/2 ... filesize/2 + size
|
||||
*/
|
||||
if (!kfile_rwTest(fd, test_buf, size))
|
||||
goto kfile_test_end;
|
||||
|
||||
LOG_INFO("Test 2: ok!\n");
|
||||
|
||||
/*
|
||||
* Restore previous content.
|
||||
*/
|
||||
if (save_buf)
|
||||
{
|
||||
kfile_seek(fd, -(kfile_off_t)size, KSM_SEEK_CUR);
|
||||
|
||||
if (kfile_write(fd, save_buf, size) != size)
|
||||
goto kfile_test_end;
|
||||
|
||||
LOG_INFO("Restore content..form [%ld] to [%ld]\n", fd->seek_pos, fd->seek_pos + size);
|
||||
}
|
||||
|
||||
/* TEST 2 END. */
|
||||
|
||||
/* TEST 3 BEGIN. */
|
||||
LOG_INFO("Test 3: write outside of fd->size limit [%ld]\n", fd->size);
|
||||
|
||||
/*
|
||||
* Go to the Flash end
|
||||
*/
|
||||
kfile_seek(fd, -len, KSM_SEEK_END);
|
||||
|
||||
/*
|
||||
* If necessary, user can save content,
|
||||
* for later restore.
|
||||
*/
|
||||
if (save_buf)
|
||||
{
|
||||
kfile_read(fd, save_buf, len);
|
||||
kfile_seek(fd, -len, KSM_SEEK_CUR);
|
||||
LOG_INFO("Saved content..form [%ld] to [%ld]\n", fd->seek_pos, fd->seek_pos + len);
|
||||
}
|
||||
|
||||
/*
|
||||
* Test read/write to address (filesize - size) ... filesize
|
||||
*/
|
||||
if (kfile_rwTest(fd, test_buf, size))
|
||||
goto kfile_test_end;
|
||||
|
||||
kprintf("Test 3: ok!\n");
|
||||
|
||||
/*
|
||||
* Restore previous read content
|
||||
*/
|
||||
if (save_buf)
|
||||
{
|
||||
kfile_seek(fd, -len, KSM_SEEK_END);
|
||||
|
||||
if ((kfile_off_t)kfile_write(fd, save_buf, len) != len)
|
||||
goto kfile_test_end;
|
||||
|
||||
LOG_INFO("Restore content..form [%ld] to [%ld]\n", fd->seek_pos, fd->seek_pos + len);
|
||||
}
|
||||
|
||||
/* TEST 3 END. */
|
||||
|
||||
kfile_close(fd);
|
||||
return 0;
|
||||
|
||||
kfile_test_end:
|
||||
kfile_close(fd);
|
||||
LOG_ERR("One kfile_test failed!\n");
|
||||
return EOF;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Setup all needed for kfile test
|
||||
*/
|
||||
int kfile_testSetup(void)
|
||||
{
|
||||
MOD_INIT(kfile_test);
|
||||
LOG_INFO("Mod init..ok\n");
|
||||
|
||||
// Init our backend and the buffers
|
||||
kfilemem_init(&mem, test_disk, BUF_TEST_LEN);
|
||||
init_testBuf();
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int kfile_testRun(void)
|
||||
{
|
||||
return kfile_testRunGeneric(&mem.fd, test_buf, test_buf_save, BUF_TEST_LEN);
|
||||
}
|
||||
|
||||
/**
|
||||
* End a dataflash Test.
|
||||
* (Unused)
|
||||
*/
|
||||
int kfile_testTearDown(void)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
TEST_MAIN(kfile);
|
||||
|
||||
126
bertos/io/reblock.c
Normal file
126
bertos/io/reblock.c
Normal file
|
|
@ -0,0 +1,126 @@
|
|||
/**
|
||||
* \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 2011 Develer S.r.l. (http://www.develer.com/)
|
||||
*
|
||||
* -->
|
||||
*
|
||||
* \brief KBlock block size reducer
|
||||
*
|
||||
* This module allows access to a KBlock device using a smaller block
|
||||
* size than the native one exported by the device.
|
||||
* Note that the device being remapped needs either to be opened in buffered
|
||||
* mode or to support partial writes.
|
||||
*
|
||||
* \author Stefano Fedrigo <aleph@develer.com>
|
||||
*
|
||||
* $WIZ$ module_depends = "kblock"
|
||||
*/
|
||||
|
||||
#include "reblock.h"
|
||||
#include <string.h> /* memset */
|
||||
|
||||
|
||||
static size_t reblock_readDirect(struct KBlock *b, block_idx_t idx, void *buf, size_t offset, size_t size)
|
||||
{
|
||||
Reblock *r = REBLOCK_CAST(b);
|
||||
|
||||
offset += idx % (r->native_fd->blk_size / r->fd.blk_size) * r->fd.blk_size;
|
||||
idx = idx / (r->native_fd->blk_size / r->fd.blk_size);
|
||||
|
||||
return kblock_read(r->native_fd, idx, buf, offset, size);
|
||||
}
|
||||
|
||||
|
||||
static size_t reblock_writeDirect(struct KBlock *b, block_idx_t idx, const void *buf, size_t offset, size_t size)
|
||||
{
|
||||
Reblock *r = REBLOCK_CAST(b);
|
||||
|
||||
offset += idx % (r->native_fd->blk_size / r->fd.blk_size) * r->fd.blk_size;
|
||||
idx = idx / (r->native_fd->blk_size / r->fd.blk_size);
|
||||
|
||||
return kblock_write(r->native_fd, idx, buf, offset, size);
|
||||
}
|
||||
|
||||
|
||||
static int reblock_error(struct KBlock *b)
|
||||
{
|
||||
return kblock_error(REBLOCK_CAST(b)->native_fd);
|
||||
}
|
||||
|
||||
static void reblock_clearerr(struct KBlock *b)
|
||||
{
|
||||
kblock_clearerr(REBLOCK_CAST(b)->native_fd);
|
||||
}
|
||||
|
||||
static int reblock_close(struct KBlock *b)
|
||||
{
|
||||
return kblock_close(REBLOCK_CAST(b)->native_fd);
|
||||
}
|
||||
|
||||
|
||||
static const KBlockVTable reblock_vt =
|
||||
{
|
||||
.readDirect = reblock_readDirect,
|
||||
.writeDirect = reblock_writeDirect,
|
||||
|
||||
.error = reblock_error,
|
||||
.clearerr = reblock_clearerr,
|
||||
.close = reblock_close,
|
||||
};
|
||||
|
||||
|
||||
/*
|
||||
* Initialize reblock device.
|
||||
*
|
||||
* \param rbl kblock reblock device
|
||||
* \param native_fd kblock descriptor of the reblocked device
|
||||
* \param new_blk_size new block size to export
|
||||
*
|
||||
* \note new block size is required to be a submultiple of the
|
||||
* native device block size.
|
||||
*/
|
||||
void reblock_init(Reblock *rbl, KBlock *native_fd, size_t new_blk_size)
|
||||
{
|
||||
ASSERT(new_blk_size);
|
||||
ASSERT(new_blk_size < native_fd->blk_size);
|
||||
ASSERT(native_fd->blk_size % new_blk_size == 0);
|
||||
ASSERT(kblock_buffered(native_fd) || kblock_partialWrite(native_fd));
|
||||
|
||||
memset(rbl, 0, sizeof(Reblock));
|
||||
|
||||
DB(rbl->fd.priv.type = KBT_REBLOCK);
|
||||
|
||||
rbl->fd.blk_size = new_blk_size;
|
||||
rbl->fd.blk_cnt = native_fd->blk_cnt * (native_fd->blk_size / new_blk_size);
|
||||
|
||||
rbl->fd.priv.flags |= KB_PARTIAL_WRITE;
|
||||
rbl->fd.priv.vt = &reblock_vt;
|
||||
|
||||
rbl->native_fd = native_fd;
|
||||
}
|
||||
64
bertos/io/reblock.h
Normal file
64
bertos/io/reblock.h
Normal file
|
|
@ -0,0 +1,64 @@
|
|||
/**
|
||||
* \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 2011 Develer S.r.l. (http://www.develer.com/)
|
||||
*
|
||||
* -->
|
||||
*
|
||||
* \brief KBlock block size reducer
|
||||
*
|
||||
* \author Stefano Fedrigo <aleph@develer.com>
|
||||
*
|
||||
* $WIZ$ module_name = "reblock"
|
||||
* $WIZ$ module_depends = "kblock"
|
||||
*/
|
||||
|
||||
#ifndef REBLOCK_H
|
||||
#define REBLOCK_H
|
||||
|
||||
#include "kblock.h"
|
||||
|
||||
|
||||
typedef struct Reblock
|
||||
{
|
||||
KBlock fd;
|
||||
KBlock *native_fd;
|
||||
} Reblock;
|
||||
|
||||
#define KBT_REBLOCK MAKE_ID('R', 'E', 'B', 'L')
|
||||
|
||||
|
||||
INLINE Reblock *REBLOCK_CAST(KBlock *b)
|
||||
{
|
||||
ASSERT(b->priv.type == KBT_REBLOCK);
|
||||
return (Reblock *)b;
|
||||
}
|
||||
|
||||
void reblock_init(Reblock *rbl, KBlock *native_fd, size_t native_blk_size);
|
||||
|
||||
#endif /* REBLOCK_H */
|
||||
Loading…
Add table
Add a link
Reference in a new issue