ChibiOS/HAL  6.1.0
hal_channels.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_channels.h
19  * @brief I/O channels access.
20  * @details This header defines an abstract interface useful to access generic
21  * I/O serial devices in a standardized way.
22  *
23  * @addtogroup IO_CHANNEL
24  * @details This module defines an abstract interface for I/O channels by
25  * extending the @p BaseSequentialStream interface.<br>
26  * Note that no code is present, I/O channels are just abstract
27  * interface like structures, you should look at the systems as
28  * to a set of abstract C++ classes (even if written in C).
29  * Specific device drivers can use/extend the interface and
30  * implement them.<br>
31  * This system has the advantage to make the access to channels
32  * independent from the implementation logic.
33  * @{
34  */
35 
36 #ifndef HAL_CHANNELS_H
37 #define HAL_CHANNELS_H
38 
39 /**
40  * @name Default control operation codes.
41  * @{
42  */
43 #define CHN_CTL_INVALID 0 /** @brief Invalid operation code. */
44 #define CHN_CTL_NOP 1 /** @brief Does nothing. */
45 #define CHN_CTL_TX_WAIT 2 /** @brief Wait for TX completion. */
46 /** @} */
47 
48 /**
49  * @brief @p BaseChannel specific methods.
50  */
51 #define _base_channel_methods \
52  _base_sequential_stream_methods \
53  /* Channel put method with timeout specification.*/ \
54  msg_t (*putt)(void *instance, uint8_t b, sysinterval_t time); \
55  /* Channel get method with timeout specification.*/ \
56  msg_t (*gett)(void *instance, sysinterval_t time); \
57  /* Channel write method with timeout specification.*/ \
58  size_t (*writet)(void *instance, const uint8_t *bp, \
59  size_t n, sysinterval_t time); \
60  /* Channel read method with timeout specification.*/ \
61  size_t (*readt)(void *instance, uint8_t *bp, size_t n, \
62  sysinterval_t time); \
63  /* Channel control method.*/ \
64  msg_t (*ctl)(void *instance, unsigned int operation, void *arg);
65 
66 /**
67  * @brief @p BaseChannel specific data.
68  * @note It is empty because @p BaseChannel is only an interface without
69  * implementation.
70  */
71 #define _base_channel_data \
72  _base_sequential_stream_data
73 
74 /**
75  * @extends BaseSequentialStreamVMT
76  *
77  * @brief @p BaseChannel virtual methods table.
78  */
81 };
82 
83 /**
84  * @extends BaseSequentialStream
85  *
86  * @brief Base channel class.
87  * @details This class represents a generic, byte-wide, I/O channel. This class
88  * introduces generic I/O primitives with timeout specification.
89  */
90 typedef struct {
91  /** @brief Virtual Methods Table.*/
92  const struct BaseChannelVMT *vmt;
94 } BaseChannel;
95 
96 /**
97  * @name Macro Functions (BaseChannel)
98  * @{
99  */
100 /**
101  * @brief Channel blocking byte write with timeout.
102  * @details This function writes a byte value to a channel. If the channel
103  * is not ready to accept data then the calling thread is suspended.
104  *
105  * @param[in] ip pointer to a @p BaseChannel or derived class
106  * @param[in] b the byte value to be written to the channel
107  * @param[in] time the number of ticks before the operation timeouts,
108  * the following special values are allowed:
109  * - @a TIME_IMMEDIATE immediate timeout.
110  * - @a TIME_INFINITE no timeout.
111  * .
112  * @return The operation status.
113  * @retval STM_OK if the operation succeeded.
114  * @retval STM_TIMEOUT if the specified time expired.
115  * @retval STM_RESET if the channel associated queue (if any) was reset.
116  *
117  * @api
118  */
119 #define chnPutTimeout(ip, b, time) ((ip)->vmt->putt(ip, b, time))
120 
121 /**
122  * @brief Channel blocking byte read with timeout.
123  * @details This function reads a byte value from a channel. If the data
124  * is not available then the calling thread is suspended.
125  *
126  * @param[in] ip pointer to a @p BaseChannel or derived class
127  * @param[in] time the number of ticks before the operation timeouts,
128  * the following special values are allowed:
129  * - @a TIME_IMMEDIATE immediate timeout.
130  * - @a TIME_INFINITE no timeout.
131  * .
132  * @return A byte value from the queue.
133  * @retval STM_TIMEOUT if the specified time expired.
134  * @retval STM_RESET if the channel associated queue (if any) has been
135  * reset.
136  *
137  * @api
138  */
139 #define chnGetTimeout(ip, time) ((ip)->vmt->gett(ip, time))
140 
141 /**
142  * @brief Channel blocking write.
143  * @details The function writes data from a buffer to a channel. If the channel
144  * is not ready to accept data then the calling thread is suspended.
145  *
146  * @param[in] ip pointer to a @p BaseChannel or derived class
147  * @param[out] bp pointer to the data buffer
148  * @param[in] n the maximum amount of data to be transferred
149  *
150  * @return The number of bytes transferred.
151  *
152  * @api
153  */
154 #define chnWrite(ip, bp, n) streamWrite(ip, bp, n)
155 
156 /**
157  * @brief Channel blocking write with timeout.
158  * @details The function writes data from a buffer to a channel. If the channel
159  * is not ready to accept data then the calling thread is suspended.
160  *
161  * @param[in] ip pointer to a @p BaseChannel or derived class
162  * @param[out] bp pointer to the data buffer
163  * @param[in] n the maximum amount of data to be transferred
164  * @param[in] time the number of ticks before the operation timeouts,
165  * the following special values are allowed:
166  * - @a TIME_IMMEDIATE immediate timeout.
167  * - @a TIME_INFINITE no timeout.
168  * .
169  * @return The number of bytes transferred.
170  *
171  * @api
172  */
173 #define chnWriteTimeout(ip, bp, n, time) ((ip)->vmt->writet(ip, bp, n, time))
174 
175 /**
176  * @brief Channel blocking read.
177  * @details The function reads data from a channel into a buffer. If the data
178  * is not available then the calling thread is suspended.
179  *
180  * @param[in] ip pointer to a @p BaseChannel or derived class
181  * @param[in] bp pointer to the data buffer
182  * @param[in] n the maximum amount of data to be transferred
183  *
184  * @return The number of bytes transferred.
185  *
186  * @api
187  */
188 #define chnRead(ip, bp, n) streamRead(ip, bp, n)
189 
190 /**
191  * @brief Channel blocking read with timeout.
192  * @details The function reads data from a channel into a buffer. If the data
193  * is not available then the calling thread is suspended.
194  *
195  * @param[in] ip pointer to a @p BaseChannel or derived class
196  * @param[in] bp pointer to the data buffer
197  * @param[in] n the maximum amount of data to be transferred
198  * @param[in] time the number of ticks before the operation timeouts,
199  * the following special values are allowed:
200  * - @a TIME_IMMEDIATE immediate timeout.
201  * - @a TIME_INFINITE no timeout.
202  * .
203  * @return The number of bytes transferred.
204  *
205  * @api
206  */
207 #define chnReadTimeout(ip, bp, n, time) ((ip)->vmt->readt(ip, bp, n, time))
208 
209 /**
210  * @brief Control operation on a channel.
211  *
212  * @param[in] ip pointer to a @p BaseChannel or derived class
213  * @param[in] operation control operation code
214  * @param[in,out] arg operation argument
215  *
216  * @return The control operation status.
217  * @retval MSG_OK in case of success.
218  * @retval MSG_TIMEOUT in case of operation timeout.
219  * @retval MSG_RESET in case of operation reset.
220  *
221  * @api
222  */
223 #define chnControl(ip, operation, arg) ((ip)->vmt->ctl(ip, operation, arg))
224 /** @} */
225 
226 /**
227  * @name I/O status flags added to the event listener
228  * @{
229  */
230 /** @brief No pending conditions.*/
231 #define CHN_NO_ERROR (eventflags_t)0
232 /** @brief Connection happened.*/
233 #define CHN_CONNECTED (eventflags_t)1
234 /** @brief Disconnection happened.*/
235 #define CHN_DISCONNECTED (eventflags_t)2
236 /** @brief Data available in the input queue.*/
237 #define CHN_INPUT_AVAILABLE (eventflags_t)4
238 /** @brief Output queue empty.*/
239 #define CHN_OUTPUT_EMPTY (eventflags_t)8
240 /** @brief Transmission end.*/
241 #define CHN_TRANSMISSION_END (eventflags_t)16
242 /** @} */
243 
244 /**
245  * @brief @p BaseAsynchronousChannel specific methods.
246  */
247 #define _base_asynchronous_channel_methods \
248  _base_channel_methods \
249 
250 /**
251  * @brief @p BaseAsynchronousChannel specific data.
252  */
253 #define _base_asynchronous_channel_data \
254  _base_channel_data \
255  /* I/O condition event source.*/ \
256  event_source_t event;
257 
258 /**
259  * @extends BaseChannelVMT
260  *
261  * @brief @p BaseAsynchronousChannel virtual methods table.
262  */
265 };
266 
267 /**
268  * @extends BaseChannel
269  *
270  * @brief Base asynchronous channel class.
271  * @details This class extends @p BaseChannel by adding event sources fields
272  * for asynchronous I/O for use in an event-driven environment.
273  */
274 typedef struct {
275  /** @brief Virtual Methods Table.*/
279 
280 /**
281  * @name Macro Functions (BaseAsynchronousChannel)
282  * @{
283  */
284 /**
285  * @brief Returns the I/O condition event source.
286  * @details The event source is broadcasted when an I/O condition happens.
287  *
288  * @param[in] ip pointer to a @p BaseAsynchronousChannel or derived
289  * class
290  * @return A pointer to an @p EventSource object.
291  *
292  * @api
293  */
294 #define chnGetEventSource(ip) (&((ip)->event))
295 
296 /**
297  * @brief Adds status flags to the listeners's flags mask.
298  * @details This function is usually called from the I/O ISRs in order to
299  * notify I/O conditions such as data events, errors, signal
300  * changes etc.
301  *
302  * @param[in] ip pointer to a @p BaseAsynchronousChannel or derived
303  * class
304  * @param[in] flags condition flags to be added to the listener flags mask
305  *
306  * @iclass
307  */
308 #define chnAddFlagsI(ip, flags) { \
309  osalEventBroadcastFlagsI(&(ip)->event, flags); \
310 }
311 /** @} */
312 
313 #endif /* HAL_CHANNELS_H */
314 
315 /** @} */
#define _base_channel_methods
BaseChannel specific methods.
Definition: hal_channels.h:51
const struct BaseChannelVMT * vmt
Virtual Methods Table.
Definition: hal_channels.h:92
Base channel class.
Definition: hal_channels.h:90
BaseChannel virtual methods table.
Definition: hal_channels.h:79
#define _base_asynchronous_channel_data
BaseAsynchronousChannel specific data.
Definition: hal_channels.h:253
#define _base_channel_data
BaseChannel specific data.
Definition: hal_channels.h:71
#define _base_asynchronous_channel_methods
BaseAsynchronousChannel specific methods.
Definition: hal_channels.h:247
const struct BaseAsynchronousChannelVMT * vmt
Virtual Methods Table.
Definition: hal_channels.h:276
Base asynchronous channel class.
Definition: hal_channels.h:274
BaseAsynchronousChannel virtual methods table.
Definition: hal_channels.h:263