From: batt Date: Sun, 27 Sep 2009 13:22:15 +0000 (+0000) Subject: Add new module kfile_mem: kfile access over a plain memory buffer. X-Git-Tag: 2.3.0~104 X-Git-Url: https://codewiz.org/gitweb?a=commitdiff_plain;h=e4b96c973d54ffbd7950253295c6686a9db08b0a;p=bertos.git Add new module kfile_mem: kfile access over a plain memory buffer. git-svn-id: https://src.develer.com/svnoss/bertos/trunk@2976 38d2e660-2303-0410-9eaa-f027e97ec537 --- diff --git a/bertos/struct/kfile_mem.c b/bertos/struct/kfile_mem.c new file mode 100644 index 00000000..5560fdc6 --- /dev/null +++ b/bertos/struct/kfile_mem.c @@ -0,0 +1,83 @@ +/** + * \file + * + * + * \brief KFile interface over a memory buffer. + * + * \version $Id: cfg_adc.h 2348 2009-02-16 13:43:44Z duplo $ + * \author Francesco Sacchi + */ + +#include "kfile_mem.h" + +#include + +#include + +static size_t kfilemem_read(struct KFile *_fd, void *buf, size_t size) +{ + KFileMem *fd = KFILEMEM_CAST(_fd); + + size = MIN((kfile_off_t)size, fd->fd.size - fd->fd.seek_pos); + uint8_t *mem = (uint8_t *)fd->mem; + memcpy(buf, mem + fd->fd.seek_pos, size); + fd->fd.seek_pos += size; + + return size; +} + +static size_t kfilemem_write(struct KFile *_fd, const void *buf, size_t size) +{ + KFileMem *fd = KFILEMEM_CAST(_fd); + + size = MIN((kfile_off_t)size, fd->fd.size - fd->fd.seek_pos); + uint8_t *mem = (uint8_t *)fd->mem; + memcpy(mem + fd->fd.seek_pos, buf, size); + fd->fd.seek_pos += size; + + return size; +} + +void kfilemem_init(KFileMem *km, void *mem, size_t len) +{ + ASSERT(km); + ASSERT(mem); + ASSERT(len); + + memset(km, 0, sizeof(*km)); + + km->mem = mem; + kfile_init(&km->fd); + km->fd.read = kfilemem_read; + km->fd.write = kfilemem_write; + km->fd.size = len; + DB(km->fd._type = KFT_KFILEMEM); +} diff --git a/bertos/struct/kfile_mem.h b/bertos/struct/kfile_mem.h new file mode 100644 index 00000000..695e6648 --- /dev/null +++ b/bertos/struct/kfile_mem.h @@ -0,0 +1,79 @@ +/** + * \file + * + * + * \brief KFile interface over a memory buffer. + * + * Convenient way to access a memory region using the KFile interface. + * + * + * \version $Id: cfg_adc.h 2348 2009-02-16 13:43:44Z duplo $ + * \author Francesco Sacchi + * + * $WIZ$ module_name = "kfilemem" + * $WIZ$ module_depends = "kfile" + */ + +#ifndef STRUCT_KFILE_MEM +#define STRUCT_KFILE_MEM + +#include + +typedef struct KFileMem +{ + KFile fd; + void *mem; +} KFileMem; + +/** + * ID for KFile Mem. + */ +#define KFT_KFILEMEM MAKE_ID('M', 'E', 'M', '0') + +/** + * Convert + ASSERT from generic KFile to KFileMem. + */ +INLINE KFileMem * KFILEMEM_CAST(KFile *fd) +{ + ASSERT(fd->_type == KFT_KFILEMEM); + return (KFileMem *)fd; +} + +/** + * Initialize KFileMem struct. + * + * \param kf Interface to initialize. + * \param mem Pointer to the memory buffer to operate on. + * \param len Size of the buffer + */ +void kfilemem_init(KFileMem *kf, void *mem, size_t len); + +#endif /* STRUCT_KFILE_MEM */