4 * This file is part of BeRTOS.
6 * Bertos is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 * As a special exception, you may use this file as part of a free software
21 * library without restriction. Specifically, if other files instantiate
22 * templates or use macros or inline functions from this file, or you compile
23 * this file and link it with other files to produce an executable, this
24 * file does not by itself cause the resulting executable to be covered by
25 * the GNU General Public License. This exception does not however
26 * invalidate any other reasons why the executable file might be covered by
27 * the GNU General Public License.
29 * Copyright 2004 Develer S.r.l. (http://www.develer.com/)
30 * Copyright 1999, 2000, 2001, 2003 Bernie Innocenti <bernie@codewiz.org>
34 * \brief Virtual KFile I/O interface.
36 * KFile is a simple, generic interface for file I/O. It uses an
37 * object-oriented model to supply a device-neutral interface to
38 * communicate with drivers.
40 * This module contains only definitions, the instance structure
42 * Each KFile subclass can override one or more methods of the interface,
43 * and can extend the base KFile structure with its own private data.
44 * For instance, a serial driver might implement the KFile interface by
45 * declaring a context structure like this:
48 * typedef struct Serial
50 * // base class instance
53 * // private instance data
54 * FIFOBuffer txfifo, rxfifo;
58 * You should also supply a macro for casting KFile to Serial:
61 * INLINE Serial * SERIAL_CAST(KFile *fd)
63 * ASSERT(fd->_type == KFT_SERIAL);
64 * return (Serial *)fd;
68 * Then you can implement as many interface functions as needed
69 * and leave the rest to NULL.
71 * Example implementation of the close KFile method for Serial:
74 * static int ser_kfile_close(struct KFile *fd)
76 * Serial *fds = SERIAL_CAST(fd);
77 * // [driver specific code here]
82 * The SERIAL_CAST() macro helps ensure that the passed object is
83 * really of type Serial.
85 * The KFile interface does not supply an open function: this is deliberate,
86 * because in embedded systems each device has its own init parameters.
87 * For the same reason, specific device settings like, for example,
88 * the baudrate, are not part of interface and should be handled by the
89 * driver-specific API.
92 * \author Bernie Innocenti <bernie@codewiz.org>
93 * \author Francesco Sacchi <batt@develer.com>
94 * \author Daniele Basile <asterix@develer.com>
96 * $WIZ$ module_name = "kfile"
97 * $WIZ$ module_configuration = "bertos/cfg/cfg_kfile.h"
98 * $WIZ$ module_depends = "timer", "formatwr"
104 #include <cfg/compiler.h>
105 #include <cfg/debug.h>
106 #include <cfg/macros.h>
111 typedef int32_t kfile_off_t; ///< KFile offset type, used by kfile_seek().
114 * Costants for repositioning read/write file offset.
115 * These are needed because on some embedded platforms
116 * ANSI I/O library may not be present.
118 typedef enum KSeekMode
120 KSM_SEEK_SET, ///< Seek from file beginning.
121 KSM_SEEK_CUR, ///< Seek from file current position.
122 KSM_SEEK_END, ///< Seek from file end.
126 * Prototypes for KFile access functions.
127 * I/O file functions must be ANSI compliant.
128 * \note A KFile user can choose which function subset to implement,
129 * but has to set to NULL unimplemented features.
135 * \return the number of bytes read.
137 typedef size_t (*ReadFunc_t) (struct KFile *fd, void *buf, size_t size);
141 * \return the number of bytes written.
143 typedef size_t (*WriteFunc_t) (struct KFile *fd, const void *buf, size_t size);
146 * Seek into file (if seekable).
147 * \return the new file offset or EOF on errors.
149 typedef kfile_off_t (*SeekFunc_t) (struct KFile *fd, kfile_off_t offset, KSeekMode whence);
152 * Close and reopen file \a fd.
153 * The reopening is done with the former file parameters and access modes.
155 typedef struct KFile * (*ReOpenFunc_t) (struct KFile *fd);
159 * \return 0 on success, EOF on errors.
161 typedef int (*CloseFunc_t) (struct KFile *fd);
165 * \return 0 on success, EOF on errors.
167 typedef int (*FlushFunc_t) (struct KFile *fd);
170 * Get file error mask.
171 * \return 0 on success or file error code, device specific.
173 typedef int (*ErrorFunc_t) (struct KFile *fd);
178 typedef void (*ClearErrFunc_t) (struct KFile *fd);
182 * Context data for callback functions which operate on
185 * \note Remember to add the corresponding accessor functions
186 * when extending this interface.
197 ClearErrFunc_t clearerr;
198 DB(id_t _type); ///< Used to keep track, at runtime, of the class type.
200 /* NOTE: these must _NOT_ be size_t on 16bit CPUs! */
201 kfile_off_t seek_pos;
206 * Generic implementation of kfile_seek.
208 kfile_off_t kfile_genericSeek(struct KFile *fd, kfile_off_t offset, KSeekMode whence);
211 * Generic implementation of kfile_reopen.
213 struct KFile * kfile_genericReopen(struct KFile *fd);
215 int kfile_genericClose(struct KFile *fd);
217 int kfile_putc(int c, struct KFile *fd); ///< Generic putc implementation using kfile_write.
218 int kfile_getc(struct KFile *fd); ///< Generic getc implementation using kfile_read.
219 int kfile_printf(struct KFile *fd, const char *format, ...);
220 int kfile_print(struct KFile *fd, const char *s);
221 int kfile_gets(struct KFile *fd, char *buf, int size);
222 int kfile_gets_echo(struct KFile *fd, char *buf, int size, bool echo);
223 void kfile_resync(KFile *fd, mtime_t delay);
226 * Interface functions for KFile access.
227 * \note Remember to change following functions if KFile interface changes.
230 INLINE size_t kfile_read(struct KFile *fd, void *buf, size_t size)
233 return fd->read(fd, buf, size);
236 INLINE size_t kfile_write(struct KFile *fd, const void *buf, size_t size)
239 return fd->write(fd, buf, size);
242 INLINE KFile * kfile_reopen(struct KFile *fd)
245 return fd->reopen(fd);
248 INLINE int kfile_close(struct KFile *fd)
251 return fd->close(fd);
254 INLINE kfile_off_t kfile_seek(struct KFile *fd, kfile_off_t offset, KSeekMode whence)
257 return fd->seek(fd, offset, whence);
260 INLINE int kfile_flush(struct KFile *fd)
263 return fd->flush(fd);
266 INLINE int kfile_error(struct KFile *fd)
269 return fd->error(fd);
272 INLINE void kfile_clearerr(struct KFile *fd)
274 ASSERT(fd->clearerr);
280 * Kfile test function.
282 int kfile_testSetup(void);
283 int kfile_testRun(void);
284 int kfile_testRunGeneric(KFile *fd, uint8_t *test_buf, uint8_t *save_buf, size_t size);
285 int kfile_testTearDown(void);
287 #endif /* KERN_KFILE_H */