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