Update preset.
[bertos.git] / bertos / io / 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 Bernie Innocenti <bernie@codewiz.org>
31  *
32  * -->
33  *
34  * \defgroup io_kfile KFile interface
35  * \ingroup core
36  * \{
37  *
38  * \brief Virtual KFile I/O interface.
39  *
40  * KFile is a simple, generic interface for file I/O.  It uses an
41  * object-oriented model to supply a device-neutral interface to
42  * communicate with drivers.
43  *
44  * This module contains only definitions, the instance structure
45  * and the common API.
46  * Each KFile subclass can override one or more methods of the interface,
47  * and can extend the base KFile structure with its own private data.
48  * For instance, a serial driver might implement the KFile interface by
49  * declaring a context structure like this:
50  *
51  * \code
52  * typedef struct Serial
53  * {
54  *      // base class instance
55  *      KFile fd;
56  *
57  *      // private instance data
58  *      FIFOBuffer txfifo, rxfifo;
59  * } Serial;
60  * \endcode
61  *
62  * You should also supply a macro for casting KFile to Serial:
63  *
64  * \code
65  * INLINE Serial * SERIAL_CAST(KFile *fd)
66  * {
67  *              ASSERT(fd->_type == KFT_SERIAL);
68  *              return (Serial *)fd;
69  * }
70  * \endcode
71  *
72  * Then you can implement as many interface functions as needed
73  * and leave the rest to NULL.
74  *
75  * Example implementation of the close KFile method for Serial:
76  *
77  * \code
78  * static int ser_kfile_close(struct KFile *fd)
79  * {
80  *              Serial *fds = SERIAL_CAST(fd);
81  *      // [driver specific code here]
82  *              return 0;
83  * }
84  * \endcode
85  *
86  * The SERIAL_CAST() macro helps ensure that the passed object is
87  * really of type Serial.
88  *
89  * The KFile interface does not supply an open function: this is deliberate,
90  * because in embedded systems each device has its own init parameters.
91  * For the same reason, specific device settings like, for example,
92  * the baudrate, are not part of interface and should be handled by the
93  * driver-specific API.
94  *
95  * \author Bernie Innocenti <bernie@codewiz.org>
96  * \author Francesco Sacchi <batt@develer.com>
97  * \author Daniele Basile <asterix@develer.com>
98  *
99  * $WIZ$ module_name = "kfile"
100  * $WIZ$ module_configuration = "bertos/cfg/cfg_kfile.h"
101  * $WIZ$ module_depends = "timer", "formatwr"
102  */
103
104 #ifndef KERN_KFILE_H
105 #define KERN_KFILE_H
106
107 #include <cfg/compiler.h>
108 #include <cfg/debug.h>
109 #include <cfg/macros.h>
110
111 /* fwd decl */
112 struct KFile;
113
114 typedef int32_t kfile_off_t;     ///< KFile offset type, used by kfile_seek().
115
116 /**
117  * Costants for repositioning read/write file offset.
118  * These are needed because on some embedded platforms
119  * ANSI I/O library may not be present.
120  */
121 typedef enum KSeekMode
122 {
123         KSM_SEEK_SET, ///< Seek from file beginning.
124         KSM_SEEK_CUR, ///< Seek from file current position.
125         KSM_SEEK_END, ///< Seek from file end.
126 } KSeekMode;
127
128 /*
129  * Prototypes for KFile access functions.
130  * I/O file functions must be ANSI compliant.
131  * \note A KFile user can choose which function subset to implement,
132  *       but has to set to NULL unimplemented features.
133  */
134
135 /*
136  * Read from file.
137  * \return the number of bytes read.
138  */
139 typedef size_t (*ReadFunc_t) (struct KFile *fd, void *buf, size_t size);
140
141 /*
142  * Write to file.
143  * \return the number of bytes written.
144  */
145 typedef size_t (*WriteFunc_t) (struct KFile *fd, const void *buf, size_t size);
146
147 /*
148  * Seek into file (if seekable).
149  * \return the new file offset or EOF on errors.
150  */
151 typedef kfile_off_t (*SeekFunc_t) (struct KFile *fd, kfile_off_t offset, KSeekMode whence);
152
153 /*
154  * Close and reopen file \a fd.
155  * The reopening is done with the former file parameters and access modes.
156  */
157 typedef struct KFile * (*ReOpenFunc_t) (struct KFile *fd);
158
159 /*
160  * Close file.
161  * \return 0 on success, EOF on errors.
162  */
163 typedef int (*CloseFunc_t) (struct KFile *fd);
164
165 /*
166  * Flush file I/O.
167  * \return 0 on success, EOF on errors.
168  */
169 typedef int (*FlushFunc_t) (struct KFile *fd);
170
171 /*
172  * Get file error mask.
173  * \return 0 on success or file error code, device specific.
174  */
175 typedef int (*ErrorFunc_t) (struct KFile *fd);
176
177 /*
178  * Clear errors.
179  */
180 typedef void (*ClearErrFunc_t) (struct KFile *fd);
181
182 /**
183  * Context data for callback functions which operate on
184  * pseudo files.
185  *
186  * \note Remember to add the corresponding accessor functions
187  *       when extending this interface.
188  */
189 typedef struct KFile
190 {
191         ReadFunc_t     read;
192         WriteFunc_t    write;
193         ReOpenFunc_t   reopen;
194         CloseFunc_t    close;
195         SeekFunc_t     seek;
196         FlushFunc_t    flush;
197         ErrorFunc_t    error;
198         ClearErrFunc_t clearerr;
199         DB(id_t _type); // Used to keep track, at runtime, of the class type.
200
201         /* NOTE: these must _NOT_ be size_t on 16bit CPUs! */
202         kfile_off_t    seek_pos;
203         kfile_off_t    size;
204 } KFile;
205
206 /*
207  * Generic implementation of kfile_seek.
208  */
209 kfile_off_t kfile_genericSeek(struct KFile *fd, kfile_off_t offset, KSeekMode whence);
210
211 /*
212  * Generic implementation of kfile_reopen.
213  */
214 struct KFile * kfile_genericReopen(struct KFile *fd);
215
216 int kfile_genericClose(struct KFile *fd);
217
218 /** @name KFile access functions
219  * Interface functions for KFile access.
220  * @{
221  */
222
223 /**
224  * Read \a size bytes from file \a fd into \a buf.
225  *
226  * This function reads at most the number of requested bytes into the
227  * provided buffer.
228  * The value returned may be less than the requested bytes in case EOF is
229  * reached OR an error occurred. You need to check the error conditions
230  * using kfile_error() to understand which case happened.
231  *
232  * \note This function will block if there are less than \a size bytes
233  *       to read.
234  *
235  * \param fd KFile context.
236  * \param buf User provided buffer.
237  * \param size Number of bytes to read.
238  * \return Number of bytes read.
239  */
240 INLINE size_t kfile_read(struct KFile *fd, void *buf, size_t size)
241 {
242         ASSERT(fd->read);
243         return fd->read(fd, buf, size);
244 }
245 int kfile_gets(struct KFile *fd, char *buf, int size);
246 int kfile_gets_echo(struct KFile *fd, char *buf, int size, bool echo);
247
248 /**
249  * Copy \a size bytes from file \a src to \a dst.
250  *
251  * \param src Source KFile.
252  * \param dst Destionation KFile.
253  * \param size number of bytes to copy.
254  * \return the number of bytes copied.
255  */
256 kfile_off_t kfile_copy(KFile *src, KFile *dst, kfile_off_t size);
257
258 /**
259  * Write \a size bytes from buffer \a buf into KFile \a fd.
260  *
261  * Return value may be less than \a size.
262  *
263  * \param fd KFile context.
264  * \param buf User provided data.
265  * \param size Number of bytes to write.
266  * \return Number of bytes written.
267  */
268 INLINE size_t kfile_write(struct KFile *fd, const void *buf, size_t size)
269 {
270         ASSERT(fd->write);
271         return fd->write(fd, buf, size);
272 }
273
274 int kfile_printf(struct KFile *fd, const char *format, ...);
275 int kfile_print(struct KFile *fd, const char *s);
276
277 /**
278  * Seek into file (if seekable).
279  *
280  * Move \a fd file seek position of \a offset bytes from \a whence.
281  *
282  * \param fd KFile context.
283  * \param offset Offset bytes to move from position \a whence.
284  * \param whence Position where to start the seek.
285  * \return Current postion in the file.
286  */
287 INLINE kfile_off_t kfile_seek(struct KFile *fd, kfile_off_t offset, KSeekMode whence)
288 {
289         ASSERT(fd->seek);
290         return fd->seek(fd, offset, whence);
291 }
292
293 /**
294  * Close and reopen file \a fd.
295  * The reopening is done with the former file parameters and access modes.
296  */
297 INLINE KFile * kfile_reopen(struct KFile *fd)
298 {
299         ASSERT(fd->reopen);
300         return fd->reopen(fd);
301 }
302
303 /**
304  * Close file.
305  * \return 0 on success, EOF on errors.
306  */
307 INLINE int kfile_close(struct KFile *fd)
308 {
309         ASSERT(fd->close);
310         return fd->close(fd);
311 }
312
313 /**
314  * Flush file I/O.
315  * \return 0 on success, EOF on errors.
316  */
317 INLINE int kfile_flush(struct KFile *fd)
318 {
319         ASSERT(fd->flush);
320         return fd->flush(fd);
321 }
322
323 /**
324  * Get file error mask.
325  * \return 0 on success or file error code, device specific.
326  */
327 INLINE int kfile_error(struct KFile *fd)
328 {
329         ASSERT(fd->error);
330         return fd->error(fd);
331 }
332
333 /**
334  * Clear errors.
335  */
336 INLINE void kfile_clearerr(struct KFile *fd)
337 {
338         ASSERT(fd->clearerr);
339         fd->clearerr(fd);
340 }
341
342 int kfile_putc(int c, struct KFile *fd); ///< Generic putc implementation using kfile_write.
343 int kfile_getc(struct KFile *fd);  ///< Generic getc implementation using kfile_read.
344 void kfile_resync(KFile *fd, mtime_t delay);
345 void kfile_init(struct KFile *fd);
346 /* @} */
347
348 /** \} */ //Defgroup io_kfile
349
350 /*
351  * Kfile test function.
352  */
353 int kfile_testSetup(void);
354 int kfile_testRun(void);
355 int kfile_testRunGeneric(KFile *fd, uint8_t *test_buf, uint8_t *save_buf, size_t size);
356 int kfile_testTearDown(void);
357
358
359 #endif /* KERN_KFILE_H */