root/trunk/bertos/kern/kfile.h @ 1798

Revision 1798, 7.5 KB (checked in by batt, 17 months ago)

Add generic kfile_close.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
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 * \version $Id$
92 * \author Bernie Innocenti <bernie@codewiz.org>
93 * \author Francesco Sacchi <batt@develer.com>
94 * \author Daniele Basile <asterix@develer.com>
95 */
96
97#ifndef KERN_KFILE_H
98#define KERN_KFILE_H
99
100#include <cfg/compiler.h>
101#include <cfg/debug.h>
102#include <cfg/macros.h>
103
104/* fwd decl */
105struct KFile;
106
107typedef int32_t kfile_off_t;     ///< KFile offset type, used by kfile_seek().
108
109/**
110 * Costants for repositioning read/write file offset.
111 * These are needed because on some embedded platforms
112 * ANSI I/O library may not be present.
113 */
114typedef enum KSeekMode
115{
116        KSM_SEEK_SET, ///< Seek from file beginning.
117        KSM_SEEK_CUR, ///< Seek from file current position.
118        KSM_SEEK_END, ///< Seek from file end.
119} KSeekMode;
120
121/**
122 * Prototypes for KFile access functions.
123 * I/O file functions must be ANSI compliant.
124 * \note A KFile user can choose which function subset to implement,
125 *       but has to set to NULL unimplemented features.
126 * \{
127 */
128
129/**
130 * Read from file.
131 * \return the number of bytes read.
132 */
133typedef size_t (*ReadFunc_t) (struct KFile *fd, void *buf, size_t size);
134
135/**
136 * Write to file.
137 * \return the number of bytes written.
138 */
139typedef size_t (*WriteFunc_t) (struct KFile *fd, const void *buf, size_t size);
140
141/**
142 * Seek into file (if seekable).
143 * \return the new file offset or EOF on errors.
144 */
145typedef kfile_off_t (*SeekFunc_t) (struct KFile *fd, kfile_off_t offset, KSeekMode whence);
146
147/**
148 * Close and reopen file \a fd.
149 * The reopening is done with the former file parameters and access modes.
150 */
151typedef struct KFile * (*ReOpenFunc_t) (struct KFile *fd);
152
153/**
154 * Close file.
155 * \return 0 on success, EOF on errors.
156 */
157typedef int (*CloseFunc_t) (struct KFile *fd);
158
159/**
160 * Flush file I/O.
161 * \return 0 on success, EOF on errors.
162 */
163typedef int (*FlushFunc_t) (struct KFile *fd);
164
165/**
166 * Get file error mask.
167 * \return 0 on success or file error code, device specific.
168 */
169typedef int (*ErrorFunc_t) (struct KFile *fd);
170
171/**
172 * Clear errors.
173 */
174typedef void (*ClearErrFunc_t) (struct KFile *fd);
175/* \} */
176
177/**
178 * Context data for callback functions which operate on
179 * pseudo files.
180 *
181 * \note Remember to add the corresponding accessor functions
182 *       when extending this interface.
183 */
184typedef struct KFile
185{
186        ReadFunc_t     read;
187        WriteFunc_t    write;
188        ReOpenFunc_t   reopen;
189        CloseFunc_t    close;
190        SeekFunc_t     seek;
191        FlushFunc_t    flush;
192        ErrorFunc_t    error;
193        ClearErrFunc_t clearerr;
194        DB(id_t _type); ///< Used to keep track, at runtime, of the class type.
195
196        /* NOTE: these must _NOT_ be size_t on 16bit CPUs! */
197        kfile_off_t    seek_pos;
198        kfile_off_t    size;
199} KFile;
200
201/**
202 * Generic implementation of kfile_seek.
203 */
204kfile_off_t kfile_genericSeek(struct KFile *fd, kfile_off_t offset, KSeekMode whence);
205
206/**
207 * Generic implementation of kfile_reopen.
208 */
209struct KFile * kfile_genericReopen(struct KFile *fd);
210
211int kfile_genericClose(struct KFile *fd);
212
213int kfile_putc(int c, struct KFile *fd); ///< Generic putc implementation using kfile_write.
214int kfile_getc(struct KFile *fd);  ///< Generic getc implementation using kfile_read.
215int kfile_printf(struct KFile *fd, const char *format, ...);
216int kfile_print(struct KFile *fd, const char *s);
217int kfile_gets(struct KFile *fd, char *buf, int size);
218int kfile_gets_echo(struct KFile *fd, char *buf, int size, bool echo);
219
220/**
221 * Interface functions for KFile access.
222 * \note Remember to change following functions if KFile interface changes.
223 * \{
224 */
225INLINE size_t kfile_read(struct KFile *fd, void *buf, size_t size)
226{
227        ASSERT(fd->read);
228        return fd->read(fd, buf, size);
229}
230
231INLINE size_t kfile_write(struct KFile *fd, const void *buf, size_t size)
232{
233        ASSERT(fd->write);
234        return fd->write(fd, buf, size);
235}
236
237INLINE KFile * kfile_reopen(struct KFile *fd)
238{
239        ASSERT(fd->reopen);
240        return fd->reopen(fd);
241}
242
243INLINE int kfile_close(struct KFile *fd)
244{
245        ASSERT(fd->close);
246        return fd->close(fd);
247}
248
249INLINE kfile_off_t kfile_seek(struct KFile *fd, kfile_off_t offset, KSeekMode whence)
250{
251        ASSERT(fd->seek);
252        return fd->seek(fd, offset, whence);
253}
254
255INLINE int kfile_flush(struct KFile *fd)
256{
257        ASSERT(fd->flush);
258        return fd->flush(fd);
259}
260
261INLINE int kfile_error(struct KFile *fd)
262{
263        ASSERT(fd->error);
264        return fd->error(fd);
265}
266
267INLINE void kfile_clearerr(struct KFile *fd)
268{
269        ASSERT(fd->clearerr);
270        fd->clearerr(fd);
271}
272/* \} */
273
274/**
275 * Kfile test function.
276 */
277int kfile_testSetUp(void);
278int kfile_testRun(KFile *fd, uint8_t *test_buf, uint8_t *save_buf, size_t size);
279int kfile_testTearDown(void);
280
281#endif /* KERN_KFILE_H */
Note: See TracBrowser for help on using the browser.