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 Bernardo Innocenti <bernie@develer.com>
34 * \brief Virtual KFile I/O interface.
35 * KFile is a generic interface for file I/O.
36 * It uses an object-oriented model to supply
37 * a generic interface for drivers to communicate.
38 * This module contains only definitions, the instance structure
40 * Each KFile user should implement at least some methods.
42 * If you have a serial driver and want to comply to KFile interface,
43 * you have to declare your context structure:
46 * typedef struct KFileSerial
53 * You should also supply a macro for casting KFile to KFileSerial:
56 * INLINE KFileSerial * KFILESERIAL(KFile *fd)
58 * ASSERT(fd->_type == KFT_SERIAL);
59 * return (KFileSerial *)fd;
63 * Then you can implement as much interface functions as you like
64 * and leave the others to NULL.
65 * ser_close implementation example:
68 * static int ser_kfile_close(struct KFile *fd)
70 * KFileSerial *fds = KFILESERIAL(fd);
71 * ser_close(fds->ser);
75 * KFILESERIAL macro helps to ensure that obj passed is really a Serial.
77 * KFile interface do not supply the open function: this is deliberate,
78 * because in embedded systems each device has its own init parameters.
79 * For the same reason specific file settings (like baudrate for Serial, for example)
80 * are demanded to specific driver implementation.
84 * \author Bernardo Innocenti <bernie@develer.com>
85 * \author Francesco Sacchi <batt@develer.com>
86 * \author Daniele Basile <asterix@develer.com>
92 #include <cfg/compiler.h>
93 #include <cfg/debug.h>
94 #include <cfg/macros.h>
99 typedef int32_t kfile_off_t; ///< KFile offset type, used by kfile_seek function.
100 typedef uint32_t kfile_size_t; ///< KFile size type, used in kfile struct.
103 * Costants for repositioning read/write file offset.
104 * These are needed because on some embedded platforms
105 * ANSI I/O library may not be present.
107 typedef enum KSeekMode
109 KSM_SEEK_SET, ///< Seek from file beginning.
110 KSM_SEEK_CUR, ///< Seek from file current position.
111 KSM_SEEK_END, ///< Seek from file end.
115 * Prototypes for KFile access functions.
116 * I/O file functions must be ANSI compliant.
117 * \note A KFile user can choose which function subset to implement,
118 * but has to set to NULL unimplemented features.
124 * \return the number of bytes read.
126 typedef size_t (*ReadFunc_t) (struct KFile *fd, void *buf, size_t size);
130 * \return the number of bytes written.
132 typedef size_t (*WriteFunc_t) (struct KFile *fd, const void *buf, size_t size);
135 * Seek into file (if seekable).
136 * \return the new file offset or EOF on errors.
138 typedef kfile_off_t (*SeekFunc_t) (struct KFile *fd, kfile_off_t offset, KSeekMode whence);
141 * Close and reopen file \a fd.
142 * The reopening is done with the former file parameters and access modes.
144 typedef struct KFile * (*ReOpenFunc_t) (struct KFile *fd);
148 * \return 0 on success, EOF on errors.
150 typedef int (*CloseFunc_t) (struct KFile *fd);
154 * \return 0 on success, EOF on errors.
156 typedef int (*FlushFunc_t) (struct KFile *fd);
159 * Get file error mask.
160 * \return 0 on success or file error code, device specific.
162 typedef int (*ErrorFunc_t) (struct KFile *fd);
167 typedef void (*ClearErrFunc_t) (struct KFile *fd);
171 * Context data for callback functions which operate on
173 * \note If you change interface, remember to add corresponding access function.
184 ClearErrFunc_t clearerr;
185 DB(id_t _type); ///< Used to keep trace, at runtime, of obj type.
187 /* NOTE: these must _NOT_ be size_t on 16bit CPUs! */
188 kfile_off_t seek_pos;
193 * Generic implementation of kfile_seek.
195 kfile_off_t kfile_genericSeek(struct KFile *fd, kfile_off_t offset, KSeekMode whence);
198 * Generic implementation of kfile_reopen.
200 struct KFile * kfile_genericReopen(struct KFile *fd);
202 int kfile_putc(int c, struct KFile *fd); ///< Generic putc implementation using kfile_write.
203 int kfile_getc(struct KFile *fd); ///< Generic getc implementation using kfile_read.
204 int kfile_printf(struct KFile *fd, const char *format, ...);
205 int kfile_print(struct KFile *fd, const char *s);
206 int kfile_gets(struct KFile *fd, char *buf, int size);
207 int kfile_gets_echo(struct KFile *fd, char *buf, int size, bool echo);
210 * Interface functions for KFile access.
211 * \note Remember to change following functions if KFile interface changes.
214 INLINE size_t kfile_read(struct KFile *fd, void *buf, size_t size)
217 return fd->read(fd, buf, size);
220 INLINE size_t kfile_write(struct KFile *fd, const void *buf, size_t size)
223 return fd->write(fd, buf, size);
226 INLINE KFile * kfile_reopen(struct KFile *fd)
229 return fd->reopen(fd);
232 INLINE int kfile_close(struct KFile *fd)
235 return fd->close(fd);
238 INLINE kfile_off_t kfile_seek(struct KFile *fd, kfile_off_t offset, KSeekMode whence)
241 return fd->seek(fd, offset, whence);
244 INLINE int kfile_flush(struct KFile *fd)
247 return fd->flush(fd);
250 INLINE int kfile_error(struct KFile *fd)
253 return fd->error(fd);
256 INLINE void kfile_clearerr(struct KFile *fd)
258 ASSERT(fd->clearerr);
264 * Kfile test function.
266 int kfile_testSetUp(void);
267 int kfile_testRun(KFile *fd, uint8_t *test_buf, uint8_t *save_buf, size_t size);
268 int kfile_testTearDown(void);
270 #endif /* KERN_KFILE_H */