2009-08-16 13:07:24 +00:00
|
|
|
/*
|
2010-02-21 07:24:53 +00:00
|
|
|
ChibiOS/RT - Copyright (C) 2006,2007,2008,2009,2010 Giovanni Di Sirio.
|
2009-08-16 13:07:24 +00:00
|
|
|
|
|
|
|
This file is part of ChibiOS/RT.
|
|
|
|
|
|
|
|
ChibiOS/RT is free software; you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation; either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
ChibiOS/RT is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
2010-02-27 08:54:22 +00:00
|
|
|
* @file chioch.h
|
2010-02-06 10:55:53 +00:00
|
|
|
* @brief I/O channels.
|
2010-02-06 12:31:09 +00:00
|
|
|
* @details This header defines abstract interfaces useful to access generic
|
|
|
|
* I/O resources in a standardized way.
|
2010-02-06 10:55:53 +00:00
|
|
|
*
|
2009-08-30 06:59:43 +00:00
|
|
|
* @addtogroup io_channels
|
2010-03-16 19:36:21 +00:00
|
|
|
* @details This module defines an abstract interface for I/O channels by
|
|
|
|
* extending the @p BaseSequentialStream interface. Note that no code
|
|
|
|
* is present, I/O channels are just abstract interface like
|
|
|
|
* structures, you should look at the systems as to a set of abstract
|
|
|
|
* C++ classes (even if written in C). Specific device drivers can
|
|
|
|
* use/extend the interface and implement them.<br>
|
|
|
|
* This system has the advantage to make the access to channels
|
|
|
|
* independent from the implementation logic.
|
2009-08-16 13:07:24 +00:00
|
|
|
* @{
|
|
|
|
*/
|
|
|
|
|
2010-02-27 08:54:22 +00:00
|
|
|
#ifndef _CHIOCH_H_
|
|
|
|
#define _CHIOCH_H_
|
2009-08-16 13:07:24 +00:00
|
|
|
|
|
|
|
/**
|
2010-02-06 12:31:09 +00:00
|
|
|
* @brief @p BaseChannel specific methods.
|
2009-08-16 13:07:24 +00:00
|
|
|
*/
|
2010-01-22 14:51:54 +00:00
|
|
|
#define _base_channel_methods \
|
2010-07-19 10:16:23 +00:00
|
|
|
_base_sequential_stream_methods \
|
2010-01-23 09:51:18 +00:00
|
|
|
/* Channel output check.*/ \
|
2010-01-22 14:51:54 +00:00
|
|
|
bool_t (*putwouldblock)(void *instance); \
|
2010-01-23 09:51:18 +00:00
|
|
|
/* Channel input check.*/ \
|
2010-01-22 14:51:54 +00:00
|
|
|
bool_t (*getwouldblock)(void *instance); \
|
2010-01-23 09:51:18 +00:00
|
|
|
/* Channel put method with timeout specification.*/ \
|
2010-01-22 14:51:54 +00:00
|
|
|
msg_t (*put)(void *instance, uint8_t b, systime_t time); \
|
2010-01-23 09:51:18 +00:00
|
|
|
/* Channel get method with timeout specification.*/ \
|
2010-01-22 14:51:54 +00:00
|
|
|
msg_t (*get)(void *instance, systime_t time); \
|
2010-01-23 09:51:18 +00:00
|
|
|
/* Channel write method with timeout specification.*/ \
|
2010-01-22 14:51:54 +00:00
|
|
|
size_t (*writet)(void *instance, const uint8_t *bp, \
|
|
|
|
size_t n, systime_t time); \
|
2010-01-23 09:51:18 +00:00
|
|
|
/* Channel read method with timeout specification.*/ \
|
2010-01-25 18:50:35 +00:00
|
|
|
size_t (*readt)(void *instance, uint8_t *bp, size_t n, systime_t time);
|
2009-08-16 13:07:24 +00:00
|
|
|
|
|
|
|
/**
|
2010-02-06 12:31:09 +00:00
|
|
|
* @brief @p BaseChannel specific data.
|
|
|
|
* @note It is empty because @p BaseChannel is only an interface without
|
|
|
|
* implementation.
|
2009-08-16 13:07:24 +00:00
|
|
|
*/
|
2010-01-22 14:51:54 +00:00
|
|
|
#define _base_channel_data \
|
2010-07-19 10:16:23 +00:00
|
|
|
_base_sequential_stream_data
|
2009-08-16 13:07:24 +00:00
|
|
|
|
|
|
|
/**
|
2010-02-06 12:31:09 +00:00
|
|
|
* @brief @p BaseChannel virtual methods table.
|
2009-08-16 13:07:24 +00:00
|
|
|
*/
|
2010-01-22 14:51:54 +00:00
|
|
|
struct BaseChannelVMT { \
|
2010-01-30 11:59:30 +00:00
|
|
|
_base_channel_methods \
|
2009-08-16 13:07:24 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
2010-01-05 17:14:09 +00:00
|
|
|
* @extends BaseSequentialStream
|
|
|
|
*
|
2010-02-06 12:31:09 +00:00
|
|
|
* @brief Base channel class.
|
2010-01-05 17:14:09 +00:00
|
|
|
* @details This class represents a generic, byte-wide, I/O channel. This class
|
2010-01-25 18:50:35 +00:00
|
|
|
* introduces generic I/O primitives with timeout specification.
|
2009-08-16 13:07:24 +00:00
|
|
|
*/
|
|
|
|
typedef struct {
|
2010-02-06 12:31:09 +00:00
|
|
|
/** @brief Virtual Methods Table.*/
|
2009-08-16 13:07:24 +00:00
|
|
|
const struct BaseChannelVMT *vmt;
|
2010-01-25 18:50:35 +00:00
|
|
|
_base_channel_data
|
2009-08-16 13:07:24 +00:00
|
|
|
} BaseChannel;
|
|
|
|
|
|
|
|
/**
|
2010-02-06 12:31:09 +00:00
|
|
|
* @brief Channel output check.
|
2010-01-05 17:14:09 +00:00
|
|
|
* @details This function verifies if a subsequent put/write operation would
|
|
|
|
* block.
|
2009-08-16 13:07:24 +00:00
|
|
|
*
|
2010-02-06 12:31:09 +00:00
|
|
|
* @param[in] ip pointer to a @p BaseChannel or derived class
|
2010-09-21 10:22:06 +00:00
|
|
|
* @return The output queue status.
|
2010-02-06 12:31:09 +00:00
|
|
|
* @retval FALSE if the output queue has space and would not block a
|
|
|
|
* write operation.
|
|
|
|
* @retval TRUE if the output queue is full and would block a write
|
|
|
|
* operation.
|
2010-09-21 10:22:06 +00:00
|
|
|
*
|
|
|
|
* @api
|
2009-08-16 13:07:24 +00:00
|
|
|
*/
|
2010-01-22 14:51:54 +00:00
|
|
|
#define chIOPutWouldBlock(ip) ((ip)->vmt->putwouldblock(ip))
|
2009-08-16 13:07:24 +00:00
|
|
|
|
|
|
|
/**
|
2010-02-06 12:31:09 +00:00
|
|
|
* @brief Channel input check.
|
2010-01-05 17:14:09 +00:00
|
|
|
* @details This function verifies if a subsequent get/read operation would
|
|
|
|
* block.
|
2009-08-16 13:07:24 +00:00
|
|
|
*
|
2010-02-06 12:31:09 +00:00
|
|
|
* @param[in] ip pointer to a @p BaseChannel or derived class
|
2010-09-21 10:22:06 +00:00
|
|
|
* @return The input queue status.
|
2010-02-06 12:31:09 +00:00
|
|
|
* @retval FALSE if the input queue contains data and would not block a
|
|
|
|
* read operation.
|
|
|
|
* @retval TRUE if the input queue is empty and would block a read
|
|
|
|
* operation.
|
2010-09-21 10:22:06 +00:00
|
|
|
*
|
|
|
|
* @api
|
2009-08-16 13:07:24 +00:00
|
|
|
*/
|
2010-01-22 14:51:54 +00:00
|
|
|
#define chIOGetWouldBlock(ip) ((ip)->vmt->getwouldblock(ip))
|
2009-08-16 13:07:24 +00:00
|
|
|
|
|
|
|
/**
|
2010-02-06 12:31:09 +00:00
|
|
|
* @brief Channel blocking byte write.
|
2009-08-16 13:07:24 +00:00
|
|
|
* @details This function writes a byte value to a channel. If the channel
|
|
|
|
* is not ready to accept data then the calling thread is suspended.
|
|
|
|
*
|
2010-02-06 12:31:09 +00:00
|
|
|
* @param[in] ip pointer to a @p BaseChannel or derived class
|
|
|
|
* @param[in] b the byte value to be written to the channel
|
2010-09-21 10:22:06 +00:00
|
|
|
* @return The operation status.
|
2010-02-06 12:31:09 +00:00
|
|
|
* @retval Q_OK if the operation succeeded.
|
|
|
|
* @retval Q_RESET if the channel associated queue (if any) was reset.
|
2010-09-21 10:22:06 +00:00
|
|
|
*
|
|
|
|
* @api
|
2009-08-16 13:07:24 +00:00
|
|
|
*/
|
2010-01-22 14:51:54 +00:00
|
|
|
#define chIOPut(ip, b) ((ip)->vmt->put(ip, b, TIME_INFINITE))
|
2009-08-16 13:07:24 +00:00
|
|
|
|
|
|
|
/**
|
2010-02-06 12:31:09 +00:00
|
|
|
* @brief Channel blocking byte write with timeout.
|
2009-08-16 13:07:24 +00:00
|
|
|
* @details This function writes a byte value to a channel. If the channel
|
|
|
|
* is not ready to accept data then the calling thread is suspended.
|
|
|
|
*
|
2010-02-06 12:31:09 +00:00
|
|
|
* @param[in] ip pointer to a @p BaseChannel or derived class
|
|
|
|
* @param[in] b the byte value to be written to the channel
|
|
|
|
* @param[in] time the number of ticks before the operation timeouts,
|
|
|
|
* the following special values are allowed:
|
|
|
|
* - @a TIME_IMMEDIATE immediate timeout.
|
|
|
|
* - @a TIME_INFINITE no timeout.
|
|
|
|
* .
|
2010-09-21 10:22:06 +00:00
|
|
|
* @return The operation status.
|
2010-02-06 12:31:09 +00:00
|
|
|
* @retval Q_OK if the operation succeeded.
|
|
|
|
* @retval Q_TIMEOUT if the specified time expired.
|
|
|
|
* @retval Q_RESET if the channel associated queue (if any) was reset.
|
2010-09-21 10:22:06 +00:00
|
|
|
*
|
|
|
|
* @api
|
2009-08-16 13:07:24 +00:00
|
|
|
*/
|
2010-01-22 14:51:54 +00:00
|
|
|
#define chIOPutTimeout(ip, b, time) ((ip)->vmt->put(ip, b, time))
|
2009-08-16 13:07:24 +00:00
|
|
|
|
|
|
|
/**
|
2010-02-06 12:31:09 +00:00
|
|
|
* @brief Channel blocking byte read.
|
2009-08-16 13:07:24 +00:00
|
|
|
* @details This function reads a byte value from a channel. If the data
|
|
|
|
* is not available then the calling thread is suspended.
|
|
|
|
*
|
2010-02-06 12:31:09 +00:00
|
|
|
* @param[in] ip pointer to a @p BaseChannel or derived class
|
2010-09-21 10:22:06 +00:00
|
|
|
* @return A byte value from the queue.
|
|
|
|
* @retval Q_RESET if the channel associated queue (if any) has been
|
|
|
|
* reset.
|
|
|
|
*
|
|
|
|
* @api
|
2009-08-16 13:07:24 +00:00
|
|
|
*/
|
2010-01-22 14:51:54 +00:00
|
|
|
#define chIOGet(ip) ((ip)->vmt->get(ip, TIME_INFINITE))
|
2009-08-16 13:07:24 +00:00
|
|
|
|
|
|
|
/**
|
2010-02-06 12:31:09 +00:00
|
|
|
* @brief Channel blocking byte read with timeout.
|
2009-08-16 13:07:24 +00:00
|
|
|
* @details This function reads a byte value from a channel. If the data
|
|
|
|
* is not available then the calling thread is suspended.
|
|
|
|
*
|
2010-02-06 12:31:09 +00:00
|
|
|
* @param[in] ip pointer to a @p BaseChannel or derived class
|
|
|
|
* @param[in] time the number of ticks before the operation timeouts,
|
|
|
|
* the following special values are allowed:
|
|
|
|
* - @a TIME_IMMEDIATE immediate timeout.
|
|
|
|
* - @a TIME_INFINITE no timeout.
|
|
|
|
* .
|
2010-09-21 10:22:06 +00:00
|
|
|
* @return A byte value from the queue.
|
2010-02-06 12:31:09 +00:00
|
|
|
* @retval Q_TIMEOUT if the specified time expired.
|
2010-09-21 10:22:06 +00:00
|
|
|
* @retval Q_RESET if the channel associated queue (if any) has been
|
|
|
|
* reset.
|
|
|
|
*
|
|
|
|
* @api
|
2009-08-16 13:07:24 +00:00
|
|
|
*/
|
2010-01-22 14:51:54 +00:00
|
|
|
#define chIOGetTimeout(ip, time) ((ip)->vmt->get(ip, time))
|
2010-01-05 17:14:09 +00:00
|
|
|
|
|
|
|
/**
|
2010-02-06 12:31:09 +00:00
|
|
|
* @brief Channel blocking write with timeout.
|
2010-01-05 17:14:09 +00:00
|
|
|
* @details The function writes data from a buffer to a channel. If the channel
|
|
|
|
* is not ready to accept data then the calling thread is suspended.
|
|
|
|
*
|
2010-02-06 12:31:09 +00:00
|
|
|
* @param[in] ip pointer to a @p BaseChannel or derived class
|
|
|
|
* @param[out] bp pointer to the data buffer
|
|
|
|
* @param[in] n the maximum amount of data to be transferred
|
|
|
|
* @param[in] time the number of ticks before the operation timeouts,
|
|
|
|
* the following special values are allowed:
|
|
|
|
* - @a TIME_IMMEDIATE immediate timeout.
|
|
|
|
* - @a TIME_INFINITE no timeout.
|
|
|
|
* .
|
|
|
|
* @return The number of bytes transferred.
|
2010-09-21 10:22:06 +00:00
|
|
|
*
|
|
|
|
* @api
|
2010-01-05 17:14:09 +00:00
|
|
|
*/
|
|
|
|
#define chIOWriteTimeout(ip, bp, n, time) \
|
2010-04-19 16:54:43 +00:00
|
|
|
((ip)->vmt->writet(ip, bp, n, time))
|
2010-01-05 17:14:09 +00:00
|
|
|
|
|
|
|
/**
|
2010-02-06 12:31:09 +00:00
|
|
|
* @brief Channel blocking read with timeout.
|
2010-01-05 17:14:09 +00:00
|
|
|
* @details The function reads data from a channel into a buffer. If the data
|
|
|
|
* is not available then the calling thread is suspended.
|
|
|
|
*
|
2010-02-06 12:31:09 +00:00
|
|
|
* @param[in] ip pointer to a @p BaseChannel or derived class
|
|
|
|
* @param[in] bp pointer to the data buffer
|
|
|
|
* @param[in] n the maximum amount of data to be transferred
|
|
|
|
* @param[in] time the number of ticks before the operation timeouts,
|
|
|
|
* the following special values are allowed:
|
|
|
|
* - @a TIME_IMMEDIATE immediate timeout.
|
|
|
|
* - @a TIME_INFINITE no timeout.
|
|
|
|
* .
|
|
|
|
* @return The number of bytes transferred.
|
2010-09-21 10:22:06 +00:00
|
|
|
*
|
|
|
|
* @api
|
2010-01-05 17:14:09 +00:00
|
|
|
*/
|
|
|
|
#define chIOReadTimeout(ip, bp, n, time) \
|
2010-04-19 16:54:43 +00:00
|
|
|
((ip)->vmt->readt(ip, bp, n, time))
|
2009-08-16 13:07:24 +00:00
|
|
|
|
|
|
|
#if CH_USE_EVENTS
|
2011-01-09 10:10:39 +00:00
|
|
|
|
|
|
|
/** @brief No pending conditions.*/
|
|
|
|
#define IO_NO_ERROR 0
|
|
|
|
/** @brief Connection happened.*/
|
|
|
|
#define IO_CONNECTED 1
|
|
|
|
/** @brief Disconnection happened.*/
|
|
|
|
#define IO_DISCONNECTED 2
|
|
|
|
/** @brief Data available in the input queue.*/
|
|
|
|
#define IO_INPUT_AVAILABLE 4
|
|
|
|
/** @brief Output queue empty.*/
|
|
|
|
#define IO_OUTPUT_EMPTY 8
|
2011-01-30 16:18:24 +00:00
|
|
|
/** @brief Transmission end.*/
|
|
|
|
#define IO_TRANSMISSION_END 16
|
2011-01-09 10:10:39 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Type of an I/O condition flags mask.
|
|
|
|
*/
|
|
|
|
typedef uint_fast16_t ioflags_t;
|
|
|
|
|
2009-08-16 13:07:24 +00:00
|
|
|
/**
|
2010-02-06 12:31:09 +00:00
|
|
|
* @brief @p BaseAsynchronousChannel specific methods.
|
2009-08-16 13:07:24 +00:00
|
|
|
*/
|
2010-01-22 14:51:54 +00:00
|
|
|
#define _base_asynchronous_channel_methods \
|
2011-01-09 10:10:39 +00:00
|
|
|
_base_channel_methods \
|
|
|
|
/* Channel read method with timeout specification.*/ \
|
|
|
|
ioflags_t (*getflags)(void *instance);
|
2009-08-16 13:07:24 +00:00
|
|
|
|
|
|
|
/**
|
2010-02-06 12:31:09 +00:00
|
|
|
* @brief @p BaseAsynchronousChannel specific data.
|
2009-08-16 13:07:24 +00:00
|
|
|
*/
|
2010-01-22 14:51:54 +00:00
|
|
|
#define _base_asynchronous_channel_data \
|
2010-01-30 11:59:30 +00:00
|
|
|
_base_channel_data \
|
2011-01-09 10:10:39 +00:00
|
|
|
/* I/O condition event source.*/ \
|
|
|
|
EventSource event; \
|
|
|
|
/* I/O condition flags.*/ \
|
|
|
|
ioflags_t flags;
|
2009-08-16 13:07:24 +00:00
|
|
|
|
|
|
|
/**
|
2010-02-06 12:31:09 +00:00
|
|
|
* @brief @p BaseAsynchronousChannel virtual methods table.
|
2009-08-16 13:07:24 +00:00
|
|
|
*/
|
|
|
|
struct BaseAsynchronousChannelVMT {
|
2010-01-25 18:50:35 +00:00
|
|
|
_base_asynchronous_channel_methods
|
2009-08-16 13:07:24 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @extends BaseChannel
|
|
|
|
*
|
2010-02-06 12:31:09 +00:00
|
|
|
* @brief Base asynchronous channel class.
|
2010-01-05 17:14:09 +00:00
|
|
|
* @details This class extends @p BaseChannel by adding event sources fields
|
2010-01-25 18:50:35 +00:00
|
|
|
* for asynchronous I/O for use in an event-driven environment.
|
2009-08-16 13:07:24 +00:00
|
|
|
*/
|
|
|
|
typedef struct {
|
2010-02-06 12:31:09 +00:00
|
|
|
/** @brief Virtual Methods Table.*/
|
2009-08-16 13:07:24 +00:00
|
|
|
const struct BaseAsynchronousChannelVMT *vmt;
|
2010-01-25 18:50:35 +00:00
|
|
|
_base_asynchronous_channel_data
|
2009-08-16 13:07:24 +00:00
|
|
|
} BaseAsynchronousChannel;
|
|
|
|
|
|
|
|
/**
|
2011-01-09 10:10:39 +00:00
|
|
|
* @brief Returns the I/O condition event source.
|
|
|
|
* @details The event source is broadcasted when an I/O condition happens.
|
2010-02-06 12:31:09 +00:00
|
|
|
*
|
|
|
|
* @param[in] ip pointer to a @p BaseAsynchronousChannel or derived
|
|
|
|
* class
|
|
|
|
* @return A pointer to an @p EventSource object.
|
2010-09-21 10:22:06 +00:00
|
|
|
*
|
|
|
|
* @api
|
2009-08-16 13:07:24 +00:00
|
|
|
*/
|
2011-01-09 10:10:39 +00:00
|
|
|
#define chIOGetEventSource(ip) (&((ip)->event))
|
2009-08-16 13:07:24 +00:00
|
|
|
|
|
|
|
/**
|
2011-01-09 10:10:39 +00:00
|
|
|
* @brief Adds condition flags to the channel's mask.
|
|
|
|
* @details This function is usually called from the I/O ISTs in order to
|
2011-01-23 09:36:19 +00:00
|
|
|
* notify I/O conditions such as data events, errors, signal
|
2011-01-09 10:10:39 +00:00
|
|
|
* changes etc.
|
2010-02-06 12:31:09 +00:00
|
|
|
*
|
|
|
|
* @param[in] ip pointer to a @p BaseAsynchronousChannel or derived
|
|
|
|
* class
|
2011-01-09 10:10:39 +00:00
|
|
|
* @param[in] mask condition flags to be added to the mask
|
|
|
|
*
|
|
|
|
* @iclass
|
|
|
|
*/
|
|
|
|
#define chIOAddFlagsI(ip, mask) { \
|
|
|
|
(ip)->flags |= (mask); \
|
|
|
|
chEvtBroadcastI(&(ip)->event); \
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Returns and clears the errors mask associated to the channel.
|
|
|
|
*
|
|
|
|
* @param[in] ip pointer to a @p BaseAsynchronousChannel or derived
|
|
|
|
* class
|
|
|
|
* @return The condition flags modified since last time this
|
|
|
|
* function was invoked.
|
2010-09-21 10:22:06 +00:00
|
|
|
*
|
|
|
|
* @api
|
2009-08-16 13:07:24 +00:00
|
|
|
*/
|
2011-01-09 10:10:39 +00:00
|
|
|
#define chIOGetAndClearFlags(ip) ((ip)->vmt->getflags(ip))
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Default implementation of the @p getflags virtual method.
|
|
|
|
*
|
|
|
|
* @param[in] ip pointer to a @p BaseAsynchronousChannel or derived
|
|
|
|
* class
|
|
|
|
* @return The condition flags modified since last time this
|
|
|
|
* function was invoked.
|
|
|
|
*
|
|
|
|
* @notapi
|
|
|
|
*/
|
|
|
|
#define _ch_get_and_clear_flags_impl(ip) \
|
|
|
|
ioflags_t mask; \
|
|
|
|
chSysLock(); \
|
|
|
|
mask = ((BaseAsynchronousChannel *)(ip))->flags; \
|
|
|
|
((BaseAsynchronousChannel *)(ip))->flags = IO_NO_ERROR; \
|
|
|
|
chSysUnlock(); \
|
|
|
|
return mask
|
2009-08-16 13:07:24 +00:00
|
|
|
|
|
|
|
#endif /* CH_USE_EVENTS */
|
|
|
|
|
2010-02-27 08:54:22 +00:00
|
|
|
#endif /* _CHIOCH_H_ */
|
2009-08-16 13:07:24 +00:00
|
|
|
|
|
|
|
/** @} */
|