b46207a091fb6ac7a9fcfded43e3e361e542666e
[bertos.git] / kern / kfile.h
1 /**
2  * \file
3  * <!--
4  * This file is part of BeRTOS.
5  *
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.
10  *
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.
15  *
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
19  *
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.
28  *
29  * Copyright 2004 Develer S.r.l. (http://www.develer.com/)
30  * Copyright 1999, 2000, 2001, 2003 Bernardo Innocenti <bernie@develer.com>
31  *
32  * -->
33  *
34  * \brief Virtual KFile I/O interface.
35  * KFile is a generic interface for file I/O.
36  * Uses an of object oriented model to supply
37  * a generic interface for drivers to communicate.
38  * This module contains only definitions,data structure
39  * and an API.
40  * Each KFile user should implement at least some core functions.
41  * E.G.
42  * If you have a serial driver and want to comply to KFile interface,
43  * you have to declare your context structure:
44  *
45  * \code
46  * typedef struct KFileSerial
47  * {
48  *      KFile fd;
49  *      Serial *ser;
50  * } KFileSerial;
51  * \endcode
52  *
53  * You should also supply a macro for casting KFile to KFileSerial:
54  *
55  * \code
56  * INLINE KFileSerial * KFILESERIAL(KFile *fd)
57  * {
58  *      ASSERT(fd->_type == KFT_SERIAL);
59  *      return (KFileSerial *)fd;
60  * }
61  * \endcode
62  *
63  * Then you can implement as much interface functions as you like
64  * and leave the others to NULL.
65  * ser_close implementation example:
66  *
67  * \code
68  * static int ser_kfile_close(struct KFile *fd)
69  * {
70  *      KFileSerial *fds = KFILESERIAL(fd);
71  *      ser_close(fds->ser);
72  *      return 0;
73  * }
74  * \endcode
75  * KFILESERIAL macro helps to ensure that obj passed is really a Serial.
76  *
77  * KFile interface do not supply the open function: this is done deliberately,
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.
81  *
82  * \version $Id$
83  * \author Bernardo Innocenti <bernie@develer.com>
84  * \author Francesco Sacchi <batt@develer.com>
85  * \author Daniele Basile <asterix@develer.com>
86  */
87
88 #ifndef KERN_KFILE_H
89 #define KERN_KFILE_H
90
91 #include <cfg/compiler.h>
92 #include <cfg/debug.h>
93
94 /* fwd decl */
95 struct KFile;
96
97 typedef int32_t kfile_off_t; ///< KFile offset type, used by kfile_seek function.
98
99 /**
100  * Costants for repositioning read/write file offset.
101  * These are needed because on some embedded platforms
102  * ANSI I/O library may not be present.
103  */
104 typedef enum KSeekMode
105 {
106         KSM_SEEK_SET, ///< Seek from file beginning.
107         KSM_SEEK_CUR, ///< Seek from file current position.
108         KSM_SEEK_END, ///< Seek from file end.
109 } KSeekMode;
110
111 /**
112  * Prototypes for KFile access functions.
113  * I/O file function must be ANSI compliant.
114  * \note A KFile user can choose which function subset to implement,
115  *       but has to set to NULL unimplemented features.
116  * \{
117  */
118
119 /**
120  * Read from file.
121  * \return the number of bytes read.
122  */
123 typedef size_t (*ReadFunc_t) (struct KFile *fd, void *buf, size_t size);
124
125 /**
126  * Write to file.
127  * \return the number of bytes written.
128  */
129 typedef size_t (*WriteFunc_t) (struct KFile *fd, const void *buf, size_t size);
130
131 /**
132  * Seek into file (if seekable).
133  * \return the new file offset or EOF on errors.
134  */
135 typedef kfile_off_t (*SeekFunc_t) (struct KFile *fd, kfile_off_t offset, KSeekMode whence);
136
137 /**
138  * Close and reopen file \a fd.
139  * The reopening is done with the former file parameters and access modes.
140  */
141 typedef struct KFile * (*ReOpenFunc_t) (struct KFile *fd);
142
143 /**
144  * Close file.
145  * \return 0 on success, EOF on errors.
146  */
147 typedef int (*CloseFunc_t) (struct KFile *fd);
148
149 /**
150  * Flush file I/O.
151  * \return 0 on success, EOF on errors.
152  */
153 typedef int (*FlushFunc_t) (struct KFile *fd);
154
155 /**
156  * Get file error mask.
157  * \return 0 on success or file error code, device specific.
158  */
159 typedef int (*ErrorFunc_t) (struct KFile *fd);
160
161 /**
162  * Clear errors.
163  */
164 typedef void (*ClearErrFunc_t) (struct KFile *fd);
165 /* \} */
166
167 /**
168  * KFile type.
169  * Used at runtime and in debug mode only to check
170  * "dynamic casts".
171  * \note Add here new KFile types.
172  */
173 typedef enum KFileType
174 {
175         KFT_GENERIC, ///< Generic
176         KFT_SERIAL,  ///< Serial driver
177         KFT_BATTFS,  ///< BattFS file
178         KFT_CNT
179 } KFileType;
180
181 /**
182  * Context data for callback functions which operate on
183  * pseudo files.
184  * \note If you change interface, remember to add corresponding access function.
185  */
186 typedef struct KFile
187 {
188         ReadFunc_t     read;
189         WriteFunc_t    write;
190         ReOpenFunc_t   reopen;
191         CloseFunc_t    close;
192         SeekFunc_t     seek;
193         FlushFunc_t    flush;
194         ErrorFunc_t    error;
195         ClearErrFunc_t clearerr;
196         DB(KFileType _type); ///< Used to keep trace, at runtime, of obj type.
197
198         /* NOTE: these must _NOT_ be size_t on 16bit CPUs! */
199         uint32_t seek_pos;
200         uint32_t size;
201 } KFile;
202
203 /**
204  * Check if \a fd is a generic KFile type.
205  */
206 #define KFILE_ASSERT_GENERIC(fd) ASSERT(fd->_type == KFT_GENERIC)
207
208 /**
209  * Generic implementation of kfile_seek.
210  */
211 kfile_off_t kfile_genericSeek(struct KFile *fd, kfile_off_t offset, KSeekMode whence);
212
213 /**
214  * Generic implementation of kfile_reopen.
215  */
216 struct KFile * kfile_genericReopen(struct KFile *fd);
217
218 int kfile_putc(int c, struct KFile *fd); ///< Generic putc implementation using kfile_write.
219 int kfile_getc(struct KFile *fd);  ///< Generic getc implementation using kfile_read.
220 int kfile_printf(struct KFile *fd, const char *format, ...);
221 int kfile_print(struct KFile *fd, const char *s);
222 int kfile_gets(struct KFile *fd, char *buf, int size);
223 int kfile_gets_echo(struct KFile *fd, char *buf, int size, bool echo);
224
225 /**
226  * Interface functions for KFile access.
227  * \note Remember to change following functions if KFile interface changes.
228  * \{
229  */
230 INLINE size_t kfile_read(struct KFile *fd, void *buf, size_t size)
231 {
232         ASSERT(fd->read);
233         return fd->read(fd, buf, size);
234 }
235
236 INLINE size_t kfile_write(struct KFile *fd, const void *buf, size_t size)
237 {
238         ASSERT(fd->write);
239         return fd->write(fd, buf, size);
240 }
241
242 INLINE KFile * kfile_reopen(struct KFile *fd)
243 {
244         ASSERT(fd->reopen);
245         return fd->reopen(fd);
246 }
247
248 INLINE int kfile_close(struct KFile *fd)
249 {
250         ASSERT(fd->close);
251         return fd->close(fd);
252 }
253
254 INLINE kfile_off_t kfile_seek(struct KFile *fd, kfile_off_t offset, KSeekMode whence)
255 {
256         ASSERT(fd->seek);
257         return fd->seek(fd, offset, whence);
258 }
259
260 INLINE int kfile_flush(struct KFile *fd)
261 {
262         ASSERT(fd->flush);
263         return fd->flush(fd);
264 }
265
266 INLINE int kfile_error(struct KFile *fd)
267 {
268         ASSERT(fd->error);
269         return fd->error(fd);
270 }
271
272 INLINE void kfile_clearerr(struct KFile *fd)
273 {
274         ASSERT(fd->clearerr);
275         fd->clearerr(fd);
276 }
277 /* \} */
278
279 /**
280  * Kfile test function.
281  */
282 bool kfile_test(KFile *fd, uint8_t *test_buf, uint8_t *save_buf, size_t size);
283
284 #endif /* KERN_KFILE_H */