ChibiOS/HAL  6.1.0
hal_files.h
Go to the documentation of this file.
1 /*
2  ChibiOS - Copyright (C) 2006..2018 Giovanni Di Sirio
3 
4  Licensed under the Apache License, Version 2.0 (the "License");
5  you may not use this file except in compliance with the License.
6  You may obtain a copy of the License at
7 
8  http://www.apache.org/licenses/LICENSE-2.0
9 
10  Unless required by applicable law or agreed to in writing, software
11  distributed under the License is distributed on an "AS IS" BASIS,
12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  See the License for the specific language governing permissions and
14  limitations under the License.
15 */
16 
17 /**
18  * @file hal_files.h
19  * @brief Data files.
20  * @details This header defines abstract interfaces useful to access generic
21  * data files in a standardized way.
22  *
23  * @addtogroup HAL_FILES
24  * @details This module define an abstract interface for generic data files by
25  * extending the @p BaseSequentialStream interface. Note that no code
26  * is present, data files are just abstract interface-like structures,
27  * you should look at the systems as to a set of abstract C++ classes
28  * (even if written in C). This system has the advantage to make the
29  * access to streams independent from the implementation logic.<br>
30  * The data files interface can be used as base class for high level
31  * object types such as an API for a File System implementation.
32  * @{
33  */
34 
35 #ifndef HAL_FILES_H
36 #define HAL_FILES_H
37 
38 /**
39  * @name Files return codes
40  * @{
41  */
42 /**
43  * @brief No error return code.
44  */
45 #define FILE_OK STM_OK
46 
47 /**
48  * @brief Error code from the file stream methods.
49  */
50 #define FILE_ERROR STM_TIMEOUT
51 
52 /**
53  * @brief End-of-file condition for file get/put methods.
54  */
55 #define FILE_EOF STM_RESET
56 /** @} */
57 
58 /**
59  * @brief File offset type.
60  */
61 typedef uint32_t fileoffset_t;
62 
63 /**
64  * @brief FileStream specific methods.
65  */
66 #define _file_stream_methods \
67  _base_sequential_stream_methods \
68  /* File close method.*/ \
69  msg_t (*close)(void *instance); \
70  /* Get last error code method.*/ \
71  msg_t (*geterror)(void *instance); \
72  /* File get size method.*/ \
73  msg_t (*getsize)(void *instance); \
74  /* File get current position method.*/ \
75  msg_t (*getposition)(void *instance); \
76  /* File seek method.*/ \
77  msg_t (*lseek)(void *instance, fileoffset_t offset);
78 
79 /**
80  * @brief @p FileStream specific data.
81  * @note It is empty because @p FileStream is only an interface
82  * without implementation.
83  */
84 #define _file_stream_data \
85  _base_sequential_stream_data
86 
87 /**
88  * @extends BaseSequentialStreamVMT
89  *
90  * @brief @p FileStream virtual methods table.
91  */
92 struct FileStreamVMT {
94 };
95 
96 /**
97  * @extends BaseSequentialStream
98  *
99  * @brief Base file stream class.
100  * @details This class represents a generic file data stream.
101  */
102 typedef struct {
103  /** @brief Virtual Methods Table.*/
104  const struct FileStreamVMT *vmt;
106 } FileStream;
107 
108 /**
109  * @name Macro Functions (FileStream)
110  * @{
111  */
112 /**
113  * @brief File stream write.
114  * @details The function writes data from a buffer to a file stream.
115  *
116  * @param[in] ip pointer to a @p FileStream or derived class
117  * @param[in] bp pointer to the data buffer
118  * @param[in] n the maximum amount of data to be transferred
119  * @return The number of bytes transferred. The return value can
120  * be less than the specified number of bytes if an
121  * end-of-file condition has been met.
122  * @retval FILE_ERROR operation failed.
123  *
124  * @api
125  */
126 #define fileStreamWrite(ip, bp, n) streamWrite(ip, bp, n)
127 
128 /**
129  * @brief File stream read.
130  * @details The function reads data from a file stream into a buffer.
131  *
132  * @param[in] ip pointer to a @p FileStream or derived class
133  * @param[out] bp pointer to the data buffer
134  * @param[in] n the maximum amount of data to be transferred
135  * @return The number of bytes transferred. The return value can
136  * be less than the specified number of bytes if an
137  * end-of-file condition has been met.
138  * @retval FILE_ERROR operation failed.
139  *
140  * @api
141  */
142 #define fileStreamRead(ip, bp, n) streamRead(ip, bp, n)
143 
144 /**
145  * @brief File stream blocking byte write.
146  * @details This function writes a byte value to a channel. If the channel
147  * is not ready to accept data then the calling thread is suspended.
148  *
149  * @param[in] ip pointer to a @p FileStream or derived class
150  * @param[in] b the byte value to be written to the channel
151  *
152  * @return The operation status.
153  * @retval FILE_OK if the operation succeeded.
154  * @retval FILE_ERROR operation failed.
155  * @retval FILE_EOF if an end-of-file condition has been met.
156  *
157  * @api
158  */
159 #define fileStreamPut(ip, b) streamPut(ip, b)
160 
161 /**
162  * @brief File stream blocking byte read.
163  * @details This function reads a byte value from a channel. If the data
164  * is not available then the calling thread is suspended.
165  *
166  * @param[in] ip pointer to a @p FileStream or derived class
167  *
168  * @return A byte value from the queue.
169  * @retval FILE_ERROR operation failed.
170  * @retval FILE_EOF if an end-of-file condition has been met.
171  *
172  * @api
173  */
174 #define fileStreamGet(ip) streamGet(ip)
175 
176 /**
177  * @brief File Stream close.
178  * @details The function closes a file stream.
179  *
180  * @param[in] ip pointer to a @p FileStream or derived class
181  * @return The operation status.
182  * @retval FILE_OK no error.
183  * @retval FILE_ERROR operation failed.
184  *
185  * @api
186  */
187 #define fileStreamClose(ip) ((ip)->vmt->close(ip))
188 
189 /**
190  * @brief Returns an implementation dependent error code.
191  * @pre The previously called function must have returned @p FILE_ERROR.
192  *
193  * @param[in] ip pointer to a @p FileStream or derived class
194  * @return Implementation dependent error code.
195  *
196  * @api
197  */
198 #define fileStreamGetError(ip) ((ip)->vmt->geterror(ip))
199 
200 /**
201  * @brief Returns the current file size.
202  *
203  * @param[in] ip pointer to a @p FileStream or derived class
204  * @return The file size.
205  * @retval FILE_ERROR operation failed.
206  *
207  * @api
208  */
209 #define fileStreamGetSize(ip) ((ip)->vmt->getsize(ip))
210 
211 /**
212  * @brief Returns the current file pointer position.
213  *
214  * @param[in] ip pointer to a @p FileStream or derived class
215  * @return The current position inside the file.
216  * @retval FILE_ERROR operation failed.
217  *
218  * @api
219  */
220 #define fileStreamGetPosition(ip) ((ip)->vmt->getposition(ip))
221 
222 /**
223  * @brief Moves the file current pointer to an absolute position.
224  *
225  * @param[in] ip pointer to a @p FileStream or derived class
226  * @param[in] offset new absolute position
227  * @return The operation status.
228  * @retval FILE_OK no error.
229  * @retval FILE_ERROR operation failed.
230  *
231  * @api
232  */
233 #define fileStreamSeek(ip, offset) ((ip)->vmt->lseek(ip, offset))
234 /** @} */
235 
236 #endif /* HAL_FILES_H */
237 
238 /** @} */
uint32_t fileoffset_t
File offset type.
Definition: hal_files.h:61
FileStream virtual methods table.
Definition: hal_files.h:92
#define _file_stream_data
FileStream specific data.
Definition: hal_files.h:84
const struct FileStreamVMT * vmt
Virtual Methods Table.
Definition: hal_files.h:104
Base file stream class.
Definition: hal_files.h:102
#define _file_stream_methods
FileStream specific methods.
Definition: hal_files.h:66