ChibiOS/RT
2.5.1
chstreams.h
Go to the documentation of this file.
00001 /*
00002     ChibiOS/RT - Copyright (C) 2006,2007,2008,2009,2010,
00003                  2011,2012 Giovanni Di Sirio.
00004 
00005     This file is part of ChibiOS/RT.
00006 
00007     ChibiOS/RT is free software; you can redistribute it and/or modify
00008     it under the terms of the GNU General Public License as published by
00009     the Free Software Foundation; either version 3 of the License, or
00010     (at your option) any later version.
00011 
00012     ChibiOS/RT is distributed in the hope that it will be useful,
00013     but WITHOUT ANY WARRANTY; without even the implied warranty of
00014     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015     GNU General Public License for more details.
00016 
00017     You should have received a copy of the GNU General Public License
00018     along with this program.  If not, see <http://www.gnu.org/licenses/>.
00019 */
00020 
00021 /**
00022  * @file    chstreams.h
00023  * @brief   Data streams.
00024  * @details This header defines abstract interfaces useful to access generic
00025  *          data streams in a standardized way.
00026  *
00027  * @addtogroup data_streams
00028  * @details This module define an abstract interface for generic data streams.
00029  *          Note that no code is present, just abstract interfaces-like
00030  *          structures, you should look at the system as to a set of
00031  *          abstract C++ classes (even if written in C). This system
00032  *          has then advantage to make the access to data streams
00033  *          independent from the implementation logic.<br>
00034  *          The stream interface can be used as base class for high level
00035  *          object types such as files, sockets, serial ports, pipes etc.
00036  * @{
00037  */
00038 
00039 #ifndef _CHSTREAMS_H_
00040 #define _CHSTREAMS_H_
00041 
00042 /**
00043  * @brief   BaseSequentialStream specific methods.
00044  */
00045 #define _base_sequential_stream_methods                                     \
00046   /* Stream write buffer method.*/                                          \
00047   size_t (*write)(void *instance, const uint8_t *bp, size_t n);             \
00048   /* Stream read buffer method.*/                                           \
00049   size_t (*read)(void *instance, uint8_t *bp, size_t n);                    \
00050   /* Channel put method, blocking.*/                                        \
00051   msg_t (*put)(void *instance, uint8_t b);                                  \
00052   /* Channel get method, blocking.*/                                        \
00053   msg_t (*get)(void *instance);                                             \
00054 
00055 /**
00056  * @brief   @p BaseSequentialStream specific data.
00057  * @note    It is empty because @p BaseSequentialStream is only an interface
00058  *          without implementation.
00059  */
00060 #define _base_sequential_stream_data
00061 
00062 /**
00063  * @brief   @p BaseSequentialStream virtual methods table.
00064  */
00065 struct BaseSequentialStreamVMT {
00066   _base_sequential_stream_methods
00067 };
00068 
00069 /**
00070  * @brief   Base stream class.
00071  * @details This class represents a generic blocking unbuffered sequential
00072  *          data stream.
00073  */
00074 typedef struct {
00075   /** @brief Virtual Methods Table.*/
00076   const struct BaseSequentialStreamVMT *vmt;
00077   _base_sequential_stream_data
00078 } BaseSequentialStream;
00079 
00080 /**
00081  * @name    Macro Functions (BaseSequentialStream)
00082  * @{
00083  */
00084 /**
00085  * @brief   Sequential Stream write.
00086  * @details The function writes data from a buffer to a stream.
00087  *
00088  * @param[in] ip        pointer to a @p BaseSequentialStream or derived class
00089  * @param[in] bp        pointer to the data buffer
00090  * @param[in] n         the maximum amount of data to be transferred
00091  * @return              The number of bytes transferred. The return value can
00092  *                      be less than the specified number of bytes if an
00093  *                      end-of-file condition has been met.
00094  *
00095  * @api
00096  */
00097 #define chSequentialStreamWrite(ip, bp, n) ((ip)->vmt->write(ip, bp, n))
00098 
00099 /**
00100  * @brief   Sequential Stream read.
00101  * @details The function reads data from a stream into a buffer.
00102  *
00103  * @param[in] ip        pointer to a @p BaseSequentialStream or derived class
00104  * @param[out] bp       pointer to the data buffer
00105  * @param[in] n         the maximum amount of data to be transferred
00106  * @return              The number of bytes transferred. The return value can
00107  *                      be less than the specified number of bytes if an
00108  *                      end-of-file condition has been met.
00109  *
00110  * @api
00111  */
00112 #define chSequentialStreamRead(ip, bp, n) ((ip)->vmt->read(ip, bp, n))
00113 
00114 /**
00115  * @brief   Sequential Stream blocking byte write.
00116  * @details This function writes a byte value to a channel. If the channel
00117  *          is not ready to accept data then the calling thread is suspended.
00118  *
00119  * @param[in] ip        pointer to a @p BaseChannel or derived class
00120  * @param[in] b         the byte value to be written to the channel
00121  *
00122  * @return              The operation status.
00123  * @retval Q_OK         if the operation succeeded.
00124  * @retval Q_RESET      if an end-of-file condition has been met.
00125  *
00126  * @api
00127  */
00128 #define chSequentialStreamPut(ip, b) ((ip)->vmt->put(ip, b))
00129 
00130 /**
00131  * @brief   Sequential Stream blocking byte read.
00132  * @details This function reads a byte value from a channel. If the data
00133  *          is not available then the calling thread is suspended.
00134  *
00135  * @param[in] ip        pointer to a @p BaseChannel or derived class
00136  *
00137  * @return              A byte value from the queue.
00138  * @retval Q_RESET      if an end-of-file condition has been met.
00139  *
00140  * @api
00141  */
00142 #define chSequentialStreamGet(ip) ((ip)->vmt->get(ip))
00143 /** @} */
00144 
00145 #endif /* _CHSTREAMS_H_ */
00146 
00147 /** @} */