ChibiOS/HAL 9.0.0
hal_streams.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_streams.h
19 * @brief Data streams.
20 * @details This header defines abstract interfaces useful to access generic
21 * data streams in a standardized way.
22 *
23 * @addtogroup HAL_STREAMS
24 * @details This module define an abstract interface for generic data streams.
25 * Note that no code is present, just abstract interfaces-like
26 * structures, you should look at the system as to a set of
27 * abstract C++ classes (even if written in C). This system
28 * has then advantage to make the access to data streams
29 * independent from the implementation logic.<br>
30 * The stream interface can be used as base class for high level
31 * object types such as files, sockets, serial ports, pipes etc.
32 * @{
33 */
34
35#ifndef HAL_STREAMS_H
36#define HAL_STREAMS_H
37
38
39/* Temporary, in order to avoid conflicts with the same definitions in new
40 OOP streams.*/
41#undef STM_OK
42#undef STM_TIMEOUT
43#undef STM_RESET
44
45/**
46 * @name Streams return codes
47 * @{
48 */
49#define STM_OK MSG_OK
50#define STM_TIMEOUT MSG_TIMEOUT
51#define STM_RESET MSG_RESET
52/** @} */
53
54/**
55 * @brief BaseSequentialStream specific methods.
56 */
57#define _base_sequential_stream_methods \
58 _base_object_methods \
59 /* Stream write buffer method.*/ \
60 size_t (*write)(void *instance, const uint8_t *bp, size_t n); \
61 /* Stream read buffer method.*/ \
62 size_t (*read)(void *instance, uint8_t *bp, size_t n); \
63 /* Channel put method, blocking.*/ \
64 msg_t (*put)(void *instance, uint8_t b); \
65 /* Channel get method, blocking.*/ \
66 msg_t (*get)(void *instance);
67
68/**
69 * @brief @p BaseSequentialStream specific data.
70 * @note It is empty because @p BaseSequentialStream is only an interface
71 * without implementation.
72 */
73#define _base_sequential_stream_data \
74 _base_object_data
75
76/**
77 * @brief @p BaseSequentialStream virtual methods table.
78 */
82
83/**
84 * @extends BaseObject
85 *
86 * @brief Base stream class.
87 * @details This class represents a generic blocking unbuffered sequential
88 * data stream.
89 */
90typedef struct {
91 /** @brief Virtual Methods Table.*/
95
96/**
97 * @name Macro Functions (BaseSequentialStream)
98 * @{
99 */
100/**
101 * @brief Sequential Stream write.
102 * @details The function writes data from a buffer to a stream.
103 *
104 * @param[in] ip pointer to a @p BaseSequentialStream or derived class
105 * @param[in] bp pointer to the data buffer
106 * @param[in] n the maximum amount of data to be transferred
107 * @return The number of bytes transferred. The return value can
108 * be less than the specified number of bytes if an
109 * end-of-file condition has been met.
110 *
111 * @api
112 */
113#define streamWrite(ip, bp, n) ((ip)->vmt->write(ip, bp, n))
114
115/**
116 * @brief Sequential Stream read.
117 * @details The function reads data from a stream into a buffer.
118 *
119 * @param[in] ip pointer to a @p BaseSequentialStream or derived class
120 * @param[out] bp pointer to the data buffer
121 * @param[in] n the maximum amount of data to be transferred
122 * @return The number of bytes transferred. The return value can
123 * be less than the specified number of bytes if an
124 * end-of-file condition has been met.
125 *
126 * @api
127 */
128#define streamRead(ip, bp, n) ((ip)->vmt->read(ip, bp, n))
129
130/**
131 * @brief Sequential Stream blocking byte write.
132 * @details This function writes a byte value to a channel. If the channel
133 * is not ready to accept data then the calling thread is suspended.
134 *
135 * @param[in] ip pointer to a @p BaseChannel or derived class
136 * @param[in] b the byte value to be written to the channel
137 *
138 * @return The operation status.
139 * @retval STM_OK if the operation succeeded.
140 * @retval STM_RESET if an end-of-file condition has been met.
141 *
142 * @api
143 */
144#define streamPut(ip, b) ((ip)->vmt->put(ip, b))
145
146/**
147 * @brief Sequential Stream blocking byte read.
148 * @details This function reads a byte value from a channel. If the data
149 * is not available then the calling thread is suspended.
150 *
151 * @param[in] ip pointer to a @p BaseChannel or derived class
152 *
153 * @return A byte value from the queue.
154 * @retval STM_RESET if an end-of-file condition has been met.
155 *
156 * @api
157 */
158#define streamGet(ip) ((ip)->vmt->get(ip))
159/** @} */
160
161/**
162 * @brief @p BaseBufferedStream specific methods.
163 */
164#define _base_buffered_stream_methods \
165 _base_sequential_stream_methods \
166 /* Channel unget method */ \
167 msg_t (*unget)(void *instance, uint8_t b);
168
169/**
170 * @brief @p BaseBufferedStream specific data.
171 * @note It is empty because @p BaseBufferedStream is only an interface
172 * without implementation.
173 */
174#define _base_buffered_stream_data \
175 _base_sequential_stream_data
176
177/**
178 * @extends BaseSequentialStreamVMT
179 *
180 * @brief @p BaseBufferedStream virtual methods table.
181 */
185
186/**
187 * @extends BaseSequentialStream
188 *
189 * @brief Buffered stream class.
190 * @details This class @p extends BaseSequentialStream to represent a generic
191 * blocking buffered sequential data stream.
192 */
193typedef struct {
194 /** @brief Virtual Methods Table. */
198
199/**
200 * @name Macro Functions (BaseBufferedStream)
201 * @{
202 */
203/**
204 * @brief Buffered Stream unget.
205 * @details This function replaces a byte value to a stream. streamUnget
206 * only guarantees a single byte can be replaced, and multiple
207 * calls without intervening calls to streamGet or streamRead may fail
208 *
209 * @param[in] ip pointer to a @p BaseBufferedStream or derived class
210 * @param[in] b the byte value to be written to the channel
211 *
212 * @post
213 *
214 * @return The operation status.
215 * @retval STM_OK if the operation succeeded.
216 * @retval STM_RESET if the operation failed
217 *
218 * @api
219 */
220#define streamUnget(ip, b) ((ip)->vmt->unget(ip, b))
221/** @} */
222
223#endif /* HAL_STREAMS_H */
224
225/** @} */
#define _base_buffered_stream_data
BaseBufferedStream specific data.
#define _base_sequential_stream_methods
BaseSequentialStream specific methods.
Definition hal_streams.h:57
#define _base_buffered_stream_methods
BaseBufferedStream specific methods.
#define _base_sequential_stream_data
BaseSequentialStream specific data.
Definition hal_streams.h:73
Buffered stream class.
const struct BaseBufferedStreamVMT * vmt
Virtual Methods Table.
BaseBufferedStream virtual methods table.
Base stream class.
Definition hal_streams.h:90
const struct BaseSequentialStreamVMT * vmt
Virtual Methods Table.
Definition hal_streams.h:92
BaseSequentialStream virtual methods table.
Definition hal_streams.h:79