ChibiOS  0.0.0
chpipes.c
Go to the documentation of this file.
1 /*
2  ChibiOS - Copyright (C) 2006..2018 Giovanni Di Sirio.
3 
4  This file is part of ChibiOS.
5 
6  ChibiOS is free software; you can redistribute it and/or modify
7  it under the terms of the GNU General Public License as published by
8  the Free Software Foundation; either version 3 of the License, or
9  (at your option) any later version.
10 
11  ChibiOS is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  GNU General Public License for more details.
15 
16  You should have received a copy of the GNU General Public License
17  along with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19 
20 /**
21  * @file chpipes.c
22  * @brief Pipes code.
23  * @details Byte pipes.
24  * <h2>Operation mode</h2>
25  * A pipe is an asynchronous communication mechanism.<br>
26  * Operations defined for mailboxes:
27  * - <b>Write</b>: Writes a buffer of data in the pipe in FIFO order.
28  * - <b>Read</b>: A buffer of data is read from the read and removed.
29  * - <b>Reset</b>: The pipe is emptied and all the stored data
30  * is lost.
31  * .
32  * @pre In order to use the pipes APIs the @p CH_CFG_USE_PIPES
33  * option must be enabled in @p chconf.h.
34  * @note Compatible with RT and NIL.
35  *
36  * @addtogroup oslib_pipes
37  * @{
38  */
39 
40 #include "ch.h"
41 
42 #if (CH_CFG_USE_PIPES == TRUE) || defined(__DOXYGEN__)
43 
44 /*===========================================================================*/
45 /* Module local definitions. */
46 /*===========================================================================*/
47 
48 /*
49  * Defaults on the best synchronization mechanism available.
50  */
51 #if (CH_CFG_USE_MUTEXES == TRUE) || defined(__DOXYGEN__)
52 #define P_LOCK(p) chMtxLock(&(p)->mtx)
53 #define P_UNLOCK(p) chMtxUnlock(&(p)->mtx)
54 #else
55 #define P_LOCK(p) (void) chSemWait(&(p)->sem)
56 #define P_UNLOCK(p) chSemSignal(&(p)->sem)
57 #endif
58 
59 /*===========================================================================*/
60 /* Module exported variables. */
61 /*===========================================================================*/
62 
63 /*===========================================================================*/
64 /* Module local types. */
65 /*===========================================================================*/
66 
67 /*===========================================================================*/
68 /* Module local variables. */
69 /*===========================================================================*/
70 
71 /*===========================================================================*/
72 /* Module local functions. */
73 /*===========================================================================*/
74 
75 /*===========================================================================*/
76 /* Module exported functions. */
77 /*===========================================================================*/
78 
79 /**
80  * @brief Initializes a @p mailbox_t object.
81  *
82  * @param[out] pp the pointer to the @p pipe_t structure to be
83  * initialized
84  * @param[in] buf pointer to the pipe buffer as an array of @p uint8_t
85  * @param[in] n number of elements in the buffer array
86  *
87  * @init
88  */
89 void chPipeObjectInit(pipe_t *pp, uint8_t *buf, size_t n) {
90 
91  chDbgCheck((pp != NULL) && (buf != NULL) && (n > (size_t)0));
92 
93  pp->buffer = buf;
94  pp->rdptr = buf;
95  pp->wrptr = buf;
96  pp->top = &buf[n];
97  pp->cnt = (size_t)0;
98  pp->reset = false;
100  chThdQueueObjectInit(&pp->qr);
101 }
102 
103 /**
104  * @brief Resets a @p pipe_t object.
105  * @details All the waiting threads are resumed with status @p MSG_RESET and
106  * the queued data is lost.
107  * @post The pipe is in reset state, all operations will fail and
108  * return @p MSG_RESET until the mailbox is enabled again using
109  * @p chPipeResumeX().
110  *
111  * @param[in] pp the pointer to an initialized @p pipe_t object
112  *
113  * @api
114  */
115 void chPipeReset(pipe_t *pp) {
116 
117  chDbgCheck(pp != NULL);
118 
119  P_LOCK(pp);
120  chSysLock();
123  pipe_t->cnt = (size_t)0;
124  pipe_t->reset = true;
128  chSysUnlock();
129  P_UNLOCK();
130 }
131 
132 /**
133  * @brief Pipe write with timeout.
134  * @details The function writes data from a buffer to a pipe. The
135  * operation completes when the specified amount of data has been
136  * transferred or after the specified timeout or if the pipe has
137  * been reset.
138  *
139  * @param[in] pp the pointer to an initialized @p pipe_t object
140  * @param[in] bp pointer to the data buffer
141  * @param[in] n the maximum amount of data to be transferred, the
142  * value 0 is reserved
143  * @param[in] timeout the number of ticks before the operation timeouts,
144  * the following special values are allowed:
145  * - @a TIME_IMMEDIATE immediate timeout.
146  * - @a TIME_INFINITE no timeout.
147  * .
148  * @return The number of bytes effectively transferred.
149  * @retval MSG_RESET if the mailbox has been reset.
150  * @retval MSG_TIMEOUT if the operation has timed out.
151  *
152  * @api
153  */
154 size_t chPipeWriteTimeout(pipe_t *pp, const uint8_t *bp,
155  size_t n, sysinterval_t timeout) {
156 
157 }
158 
159 /**
160  * @brief Pipe read with timeout.
161  * @details The function reads data from a pipe into a buffer. The
162  * operation completes when the specified amount of data has been
163  * transferred or after the specified timeout or if the pipe has
164  * been reset.
165  *
166  * @param[in] pp the pointer to an initialized @p pipe_t object
167  * @param[out] bp pointer to the data buffer
168  * @param[in] n the maximum amount of data to be transferred, the
169  * value 0 is reserved
170  * @param[in] timeout the number of ticks before the operation timeouts,
171  * the following special values are allowed:
172  * - @a TIME_IMMEDIATE immediate timeout.
173  * - @a TIME_INFINITE no timeout.
174  * .
175  * @return The number of bytes effectively transferred.
176  * @retval MSG_RESET if the mailbox has been reset.
177  * @retval MSG_TIMEOUT if the operation has timed out.
178  *
179  * @api
180  */
181 size_t chPipeReadTimeout(pipe_t *pp, uint8_t *bp,
182  size_t n, sysinterval_t timeout) {
183 
184 }
185 
186 #endif /* CH_CFG_USE_MAILBOXES == TRUE */
187 
188 /** @} */
threads_queue_t qw
Queued writers.
Definition: chpipes.h:61
bool reset
True if in reset state.
Definition: chpipes.h:60
#define chSysLock()
Enters the kernel lock state.
uint64_t sysinterval_t
Type of time interval.
Definition: chtime.h:119
uint8_t * rdptr
Read pointer.
Definition: chpipes.h:58
void chThdDequeueAllI(threads_queue_t *tqp, msg_t msg)
Dequeues and wakes up all threads from the threads queue object.
Definition: chthreads.c:899
size_t chPipeReadTimeout(pipe_t *pp, uint8_t *bp, size_t n, sysinterval_t timeout)
Pipe read with timeout.
Definition: chpipes.c:181
uint8_t * top
Pointer to the location after the buffer.
Definition: chpipes.h:55
size_t cnt
Messages in queue.
Definition: chpipes.h:59
Structure representing a pipe object.
Definition: chpipes.h:52
void chSchRescheduleS(void)
Performs a reschedule if a higher priority thread is runnable.
Definition: chschd.c:456
#define chSysUnlock()
Leaves the kernel lock state.
#define chDbgCheck(c)
Function parameters check.
Definition: chdebug.h:101
size_t chPipeWriteTimeout(pipe_t *pp, const uint8_t *bp, size_t n, sysinterval_t timeout)
Pipe write with timeout.
Definition: chpipes.c:154
uint8_t * buffer
Pointer to the pipe buffer.
Definition: chpipes.h:53
uint8_t * wrptr
Write pointer.
Definition: chpipes.h:57
void chPipeReset(pipe_t *pp)
Resets a pipe_t object.
Definition: chpipes.c:115
threads_queue_t qr
Queued readers.
Definition: chpipes.h:62
void chPipeObjectInit(pipe_t *pp, uint8_t *buf, size_t n)
Initializes a mailbox_t object.
Definition: chpipes.c:89
#define chThdQueueObjectInit(tqp)
Initializes a threads queue object.
#define MSG_RESET
Wakeup caused by a reset condition.
Definition: chschd.h:43