]> begriffs open source - freertos/blob - include/message_buffer.h
Normalize line endings and whitespace in source files
[freertos] / include / message_buffer.h
1 /*
2  * FreeRTOS Kernel <DEVELOPMENT BRANCH>
3  * Copyright (C) 2021 Amazon.com, Inc. or its affiliates.  All Rights Reserved.
4  *
5  * SPDX-License-Identifier: MIT
6  *
7  * Permission is hereby granted, free of charge, to any person obtaining a copy of
8  * this software and associated documentation files (the "Software"), to deal in
9  * the Software without restriction, including without limitation the rights to
10  * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
11  * the Software, and to permit persons to whom the Software is furnished to do so,
12  * subject to the following conditions:
13  *
14  * The above copyright notice and this permission notice shall be included in all
15  * copies or substantial portions of the Software.
16  *
17  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
19  * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
20  * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
21  * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23  *
24  * https://www.FreeRTOS.org
25  * https://github.com/FreeRTOS
26  *
27  */
28
29
30 /*
31  * Message buffers build functionality on top of FreeRTOS stream buffers.
32  * Whereas stream buffers are used to send a continuous stream of data from one
33  * task or interrupt to another, message buffers are used to send variable
34  * length discrete messages from one task or interrupt to another.  Their
35  * implementation is light weight, making them particularly suited for interrupt
36  * to task and core to core communication scenarios.
37  *
38  * ***NOTE***:  Uniquely among FreeRTOS objects, the stream buffer
39  * implementation (so also the message buffer implementation, as message buffers
40  * are built on top of stream buffers) assumes there is only one task or
41  * interrupt that will write to the buffer (the writer), and only one task or
42  * interrupt that will read from the buffer (the reader).  It is safe for the
43  * writer and reader to be different tasks or interrupts, but, unlike other
44  * FreeRTOS objects, it is not safe to have multiple different writers or
45  * multiple different readers.  If there are to be multiple different writers
46  * then the application writer must place each call to a writing API function
47  * (such as xMessageBufferSend()) inside a critical section and set the send
48  * block time to 0.  Likewise, if there are to be multiple different readers
49  * then the application writer must place each call to a reading API function
50  * (such as xMessageBufferRead()) inside a critical section and set the receive
51  * timeout to 0.
52  *
53  * Message buffers hold variable length messages.  To enable that, when a
54  * message is written to the message buffer an additional sizeof( size_t ) bytes
55  * are also written to store the message's length (that happens internally, with
56  * the API function).  sizeof( size_t ) is typically 4 bytes on a 32-bit
57  * architecture, so writing a 10 byte message to a message buffer on a 32-bit
58  * architecture will actually reduce the available space in the message buffer
59  * by 14 bytes (10 byte are used by the message, and 4 bytes to hold the length
60  * of the message).
61  */
62
63 #ifndef FREERTOS_MESSAGE_BUFFER_H
64 #define FREERTOS_MESSAGE_BUFFER_H
65
66 #ifndef INC_FREERTOS_H
67     #error "include FreeRTOS.h must appear in source files before include message_buffer.h"
68 #endif
69
70 /* Message buffers are built onto of stream buffers. */
71 #include "stream_buffer.h"
72
73 /* *INDENT-OFF* */
74 #if defined( __cplusplus )
75     extern "C" {
76 #endif
77 /* *INDENT-ON* */
78
79 /**
80  * Type by which message buffers are referenced.  For example, a call to
81  * xMessageBufferCreate() returns an MessageBufferHandle_t variable that can
82  * then be used as a parameter to xMessageBufferSend(), xMessageBufferReceive(),
83  * etc. Message buffer is essentially built as a stream buffer hence its handle
84  * is also set to same type as a stream buffer handle.
85  */
86 typedef StreamBufferHandle_t MessageBufferHandle_t;
87
88 /*-----------------------------------------------------------*/
89
90 /**
91  * message_buffer.h
92  *
93  * @code{c}
94  * MessageBufferHandle_t xMessageBufferCreate( size_t xBufferSizeBytes );
95  * @endcode
96  *
97  * Creates a new message buffer using dynamically allocated memory.  See
98  * xMessageBufferCreateStatic() for a version that uses statically allocated
99  * memory (memory that is allocated at compile time).
100  *
101  * configSUPPORT_DYNAMIC_ALLOCATION must be set to 1 or left undefined in
102  * FreeRTOSConfig.h for xMessageBufferCreate() to be available.
103  *
104  * @param xBufferSizeBytes The total number of bytes (not messages) the message
105  * buffer will be able to hold at any one time.  When a message is written to
106  * the message buffer an additional sizeof( size_t ) bytes are also written to
107  * store the message's length.  sizeof( size_t ) is typically 4 bytes on a
108  * 32-bit architecture, so on most 32-bit architectures a 10 byte message will
109  * take up 14 bytes of message buffer space.
110  *
111  * @param pxSendCompletedCallback Callback invoked when a send operation to the
112  * message buffer is complete. If the parameter is NULL or xMessageBufferCreate()
113  * is called without the parameter, then it will use the default implementation
114  * provided by sbSEND_COMPLETED macro. To enable the callback,
115  * configUSE_SB_COMPLETED_CALLBACK must be set to 1 in FreeRTOSConfig.h.
116  *
117  * @param pxReceiveCompletedCallback Callback invoked when a receive operation from
118  * the message buffer is complete. If the parameter is NULL or xMessageBufferCreate()
119  * is called without the parameter, it will use the default implementation provided
120  * by sbRECEIVE_COMPLETED macro. To enable the callback,
121  * configUSE_SB_COMPLETED_CALLBACK must be set to 1 in FreeRTOSConfig.h.
122  *
123  * @return If NULL is returned, then the message buffer cannot be created
124  * because there is insufficient heap memory available for FreeRTOS to allocate
125  * the message buffer data structures and storage area.  A non-NULL value being
126  * returned indicates that the message buffer has been created successfully -
127  * the returned value should be stored as the handle to the created message
128  * buffer.
129  *
130  * Example use:
131  * @code{c}
132  *
133  * void vAFunction( void )
134  * {
135  * MessageBufferHandle_t xMessageBuffer;
136  * const size_t xMessageBufferSizeBytes = 100;
137  *
138  *  // Create a message buffer that can hold 100 bytes.  The memory used to hold
139  *  // both the message buffer structure and the messages themselves is allocated
140  *  // dynamically.  Each message added to the buffer consumes an additional 4
141  *  // bytes which are used to hold the length of the message.
142  *  xMessageBuffer = xMessageBufferCreate( xMessageBufferSizeBytes );
143  *
144  *  if( xMessageBuffer == NULL )
145  *  {
146  *      // There was not enough heap memory space available to create the
147  *      // message buffer.
148  *  }
149  *  else
150  *  {
151  *      // The message buffer was created successfully and can now be used.
152  *  }
153  *
154  * @endcode
155  * \defgroup xMessageBufferCreate xMessageBufferCreate
156  * \ingroup MessageBufferManagement
157  */
158 #define xMessageBufferCreate( xBufferSizeBytes ) \
159     xStreamBufferGenericCreate( ( xBufferSizeBytes ), ( size_t ) 0, pdTRUE, NULL, NULL )
160
161 #if ( configUSE_SB_COMPLETED_CALLBACK == 1 )
162     #define xMessageBufferCreateWithCallback( xBufferSizeBytes, pxSendCompletedCallback, pxReceiveCompletedCallback ) \
163     xStreamBufferGenericCreate( ( xBufferSizeBytes ), ( size_t ) 0, pdTRUE, ( pxSendCompletedCallback ), ( pxReceiveCompletedCallback ) )
164 #endif
165
166 /**
167  * message_buffer.h
168  *
169  * @code{c}
170  * MessageBufferHandle_t xMessageBufferCreateStatic( size_t xBufferSizeBytes,
171  *                                                uint8_t *pucMessageBufferStorageArea,
172  *                                                StaticMessageBuffer_t *pxStaticMessageBuffer );
173  * @endcode
174  * Creates a new message buffer using statically allocated memory.  See
175  * xMessageBufferCreate() for a version that uses dynamically allocated memory.
176  *
177  * @param xBufferSizeBytes The size, in bytes, of the buffer pointed to by the
178  * pucMessageBufferStorageArea parameter.  When a message is written to the
179  * message buffer an additional sizeof( size_t ) bytes are also written to store
180  * the message's length.  sizeof( size_t ) is typically 4 bytes on a 32-bit
181  * architecture, so on most 32-bit architecture a 10 byte message will take up
182  * 14 bytes of message buffer space.  The maximum number of bytes that can be
183  * stored in the message buffer is actually (xBufferSizeBytes - 1).
184  *
185  * @param pucMessageBufferStorageArea Must point to a uint8_t array that is at
186  * least xBufferSizeBytes big.  This is the array to which messages are
187  * copied when they are written to the message buffer.
188  *
189  * @param pxStaticMessageBuffer Must point to a variable of type
190  * StaticMessageBuffer_t, which will be used to hold the message buffer's data
191  * structure.
192  *
193  * @param pxSendCompletedCallback Callback invoked when a new message is sent to the message buffer.
194  * If the parameter is NULL or xMessageBufferCreate() is called without the parameter, then it will use the default
195  * implementation provided by sbSEND_COMPLETED macro. To enable the callback,
196  * configUSE_SB_COMPLETED_CALLBACK must be set to 1 in FreeRTOSConfig.h.
197  *
198  * @param pxReceiveCompletedCallback Callback invoked when a message is read from a
199  * message buffer. If the parameter is NULL or xMessageBufferCreate() is called without the parameter, it will
200  * use the default implementation provided by sbRECEIVE_COMPLETED macro. To enable the callback,
201  * configUSE_SB_COMPLETED_CALLBACK must be set to 1 in FreeRTOSConfig.h.
202  *
203  * @return If the message buffer is created successfully then a handle to the
204  * created message buffer is returned. If either pucMessageBufferStorageArea or
205  * pxStaticmessageBuffer are NULL then NULL is returned.
206  *
207  * Example use:
208  * @code{c}
209  *
210  * // Used to dimension the array used to hold the messages.  The available space
211  * // will actually be one less than this, so 999.
212  #define STORAGE_SIZE_BYTES 1000
213  *
214  * // Defines the memory that will actually hold the messages within the message
215  * // buffer.
216  * static uint8_t ucStorageBuffer[ STORAGE_SIZE_BYTES ];
217  *
218  * // The variable used to hold the message buffer structure.
219  * StaticMessageBuffer_t xMessageBufferStruct;
220  *
221  * void MyFunction( void )
222  * {
223  * MessageBufferHandle_t xMessageBuffer;
224  *
225  *  xMessageBuffer = xMessageBufferCreateStatic( sizeof( ucStorageBuffer ),
226  *                                               ucStorageBuffer,
227  *                                               &xMessageBufferStruct );
228  *
229  *  // As neither the pucMessageBufferStorageArea or pxStaticMessageBuffer
230  *  // parameters were NULL, xMessageBuffer will not be NULL, and can be used to
231  *  // reference the created message buffer in other message buffer API calls.
232  *
233  *  // Other code that uses the message buffer can go here.
234  * }
235  *
236  * @endcode
237  * \defgroup xMessageBufferCreateStatic xMessageBufferCreateStatic
238  * \ingroup MessageBufferManagement
239  */
240 #define xMessageBufferCreateStatic( xBufferSizeBytes, pucMessageBufferStorageArea, pxStaticMessageBuffer ) \
241     xStreamBufferGenericCreateStatic( ( xBufferSizeBytes ), 0, pdTRUE, ( pucMessageBufferStorageArea ), ( pxStaticMessageBuffer ), NULL, NULL )
242
243 #if ( configUSE_SB_COMPLETED_CALLBACK == 1 )
244     #define xMessageBufferCreateStaticWithCallback( xBufferSizeBytes, pucMessageBufferStorageArea, pxStaticMessageBuffer, pxSendCompletedCallback, pxReceiveCompletedCallback ) \
245     xStreamBufferGenericCreateStatic( ( xBufferSizeBytes ), 0, pdTRUE, ( pucMessageBufferStorageArea ), ( pxStaticMessageBuffer ), ( pxSendCompletedCallback ), ( pxReceiveCompletedCallback ) )
246 #endif
247
248 /**
249  * message_buffer.h
250  *
251  * @code{c}
252  * size_t xMessageBufferSend( MessageBufferHandle_t xMessageBuffer,
253  *                         const void *pvTxData,
254  *                         size_t xDataLengthBytes,
255  *                         TickType_t xTicksToWait );
256  * @endcode
257  *
258  * Sends a discrete message to the message buffer.  The message can be any
259  * length that fits within the buffer's free space, and is copied into the
260  * buffer.
261  *
262  * ***NOTE***:  Uniquely among FreeRTOS objects, the stream buffer
263  * implementation (so also the message buffer implementation, as message buffers
264  * are built on top of stream buffers) assumes there is only one task or
265  * interrupt that will write to the buffer (the writer), and only one task or
266  * interrupt that will read from the buffer (the reader).  It is safe for the
267  * writer and reader to be different tasks or interrupts, but, unlike other
268  * FreeRTOS objects, it is not safe to have multiple different writers or
269  * multiple different readers.  If there are to be multiple different writers
270  * then the application writer must place each call to a writing API function
271  * (such as xMessageBufferSend()) inside a critical section and set the send
272  * block time to 0.  Likewise, if there are to be multiple different readers
273  * then the application writer must place each call to a reading API function
274  * (such as xMessageBufferRead()) inside a critical section and set the receive
275  * block time to 0.
276  *
277  * Use xMessageBufferSend() to write to a message buffer from a task.  Use
278  * xMessageBufferSendFromISR() to write to a message buffer from an interrupt
279  * service routine (ISR).
280  *
281  * @param xMessageBuffer The handle of the message buffer to which a message is
282  * being sent.
283  *
284  * @param pvTxData A pointer to the message that is to be copied into the
285  * message buffer.
286  *
287  * @param xDataLengthBytes The length of the message.  That is, the number of
288  * bytes to copy from pvTxData into the message buffer.  When a message is
289  * written to the message buffer an additional sizeof( size_t ) bytes are also
290  * written to store the message's length.  sizeof( size_t ) is typically 4 bytes
291  * on a 32-bit architecture, so on most 32-bit architecture setting
292  * xDataLengthBytes to 20 will reduce the free space in the message buffer by 24
293  * bytes (20 bytes of message data and 4 bytes to hold the message length).
294  *
295  * @param xTicksToWait The maximum amount of time the calling task should remain
296  * in the Blocked state to wait for enough space to become available in the
297  * message buffer, should the message buffer have insufficient space when
298  * xMessageBufferSend() is called.  The calling task will never block if
299  * xTicksToWait is zero.  The block time is specified in tick periods, so the
300  * absolute time it represents is dependent on the tick frequency.  The macro
301  * pdMS_TO_TICKS() can be used to convert a time specified in milliseconds into
302  * a time specified in ticks.  Setting xTicksToWait to portMAX_DELAY will cause
303  * the task to wait indefinitely (without timing out), provided
304  * INCLUDE_vTaskSuspend is set to 1 in FreeRTOSConfig.h.  Tasks do not use any
305  * CPU time when they are in the Blocked state.
306  *
307  * @return The number of bytes written to the message buffer.  If the call to
308  * xMessageBufferSend() times out before there was enough space to write the
309  * message into the message buffer then zero is returned.  If the call did not
310  * time out then xDataLengthBytes is returned.
311  *
312  * Example use:
313  * @code{c}
314  * void vAFunction( MessageBufferHandle_t xMessageBuffer )
315  * {
316  * size_t xBytesSent;
317  * uint8_t ucArrayToSend[] = { 0, 1, 2, 3 };
318  * char *pcStringToSend = "String to send";
319  * const TickType_t x100ms = pdMS_TO_TICKS( 100 );
320  *
321  *  // Send an array to the message buffer, blocking for a maximum of 100ms to
322  *  // wait for enough space to be available in the message buffer.
323  *  xBytesSent = xMessageBufferSend( xMessageBuffer, ( void * ) ucArrayToSend, sizeof( ucArrayToSend ), x100ms );
324  *
325  *  if( xBytesSent != sizeof( ucArrayToSend ) )
326  *  {
327  *      // The call to xMessageBufferSend() times out before there was enough
328  *      // space in the buffer for the data to be written.
329  *  }
330  *
331  *  // Send the string to the message buffer.  Return immediately if there is
332  *  // not enough space in the buffer.
333  *  xBytesSent = xMessageBufferSend( xMessageBuffer, ( void * ) pcStringToSend, strlen( pcStringToSend ), 0 );
334  *
335  *  if( xBytesSent != strlen( pcStringToSend ) )
336  *  {
337  *      // The string could not be added to the message buffer because there was
338  *      // not enough free space in the buffer.
339  *  }
340  * }
341  * @endcode
342  * \defgroup xMessageBufferSend xMessageBufferSend
343  * \ingroup MessageBufferManagement
344  */
345 #define xMessageBufferSend( xMessageBuffer, pvTxData, xDataLengthBytes, xTicksToWait ) \
346     xStreamBufferSend( ( xMessageBuffer ), ( pvTxData ), ( xDataLengthBytes ), ( xTicksToWait ) )
347
348 /**
349  * message_buffer.h
350  *
351  * @code{c}
352  * size_t xMessageBufferSendFromISR( MessageBufferHandle_t xMessageBuffer,
353  *                                const void *pvTxData,
354  *                                size_t xDataLengthBytes,
355  *                                BaseType_t *pxHigherPriorityTaskWoken );
356  * @endcode
357  *
358  * Interrupt safe version of the API function that sends a discrete message to
359  * the message buffer.  The message can be any length that fits within the
360  * buffer's free space, and is copied into the buffer.
361  *
362  * ***NOTE***:  Uniquely among FreeRTOS objects, the stream buffer
363  * implementation (so also the message buffer implementation, as message buffers
364  * are built on top of stream buffers) assumes there is only one task or
365  * interrupt that will write to the buffer (the writer), and only one task or
366  * interrupt that will read from the buffer (the reader).  It is safe for the
367  * writer and reader to be different tasks or interrupts, but, unlike other
368  * FreeRTOS objects, it is not safe to have multiple different writers or
369  * multiple different readers.  If there are to be multiple different writers
370  * then the application writer must place each call to a writing API function
371  * (such as xMessageBufferSend()) inside a critical section and set the send
372  * block time to 0.  Likewise, if there are to be multiple different readers
373  * then the application writer must place each call to a reading API function
374  * (such as xMessageBufferRead()) inside a critical section and set the receive
375  * block time to 0.
376  *
377  * Use xMessageBufferSend() to write to a message buffer from a task.  Use
378  * xMessageBufferSendFromISR() to write to a message buffer from an interrupt
379  * service routine (ISR).
380  *
381  * @param xMessageBuffer The handle of the message buffer to which a message is
382  * being sent.
383  *
384  * @param pvTxData A pointer to the message that is to be copied into the
385  * message buffer.
386  *
387  * @param xDataLengthBytes The length of the message.  That is, the number of
388  * bytes to copy from pvTxData into the message buffer.  When a message is
389  * written to the message buffer an additional sizeof( size_t ) bytes are also
390  * written to store the message's length.  sizeof( size_t ) is typically 4 bytes
391  * on a 32-bit architecture, so on most 32-bit architecture setting
392  * xDataLengthBytes to 20 will reduce the free space in the message buffer by 24
393  * bytes (20 bytes of message data and 4 bytes to hold the message length).
394  *
395  * @param pxHigherPriorityTaskWoken  It is possible that a message buffer will
396  * have a task blocked on it waiting for data.  Calling
397  * xMessageBufferSendFromISR() can make data available, and so cause a task that
398  * was waiting for data to leave the Blocked state.  If calling
399  * xMessageBufferSendFromISR() causes a task to leave the Blocked state, and the
400  * unblocked task has a priority higher than the currently executing task (the
401  * task that was interrupted), then, internally, xMessageBufferSendFromISR()
402  * will set *pxHigherPriorityTaskWoken to pdTRUE.  If
403  * xMessageBufferSendFromISR() sets this value to pdTRUE, then normally a
404  * context switch should be performed before the interrupt is exited.  This will
405  * ensure that the interrupt returns directly to the highest priority Ready
406  * state task.  *pxHigherPriorityTaskWoken should be set to pdFALSE before it
407  * is passed into the function.  See the code example below for an example.
408  *
409  * @return The number of bytes actually written to the message buffer.  If the
410  * message buffer didn't have enough free space for the message to be stored
411  * then 0 is returned, otherwise xDataLengthBytes is returned.
412  *
413  * Example use:
414  * @code{c}
415  * // A message buffer that has already been created.
416  * MessageBufferHandle_t xMessageBuffer;
417  *
418  * void vAnInterruptServiceRoutine( void )
419  * {
420  * size_t xBytesSent;
421  * char *pcStringToSend = "String to send";
422  * BaseType_t xHigherPriorityTaskWoken = pdFALSE; // Initialised to pdFALSE.
423  *
424  *  // Attempt to send the string to the message buffer.
425  *  xBytesSent = xMessageBufferSendFromISR( xMessageBuffer,
426  *                                          ( void * ) pcStringToSend,
427  *                                          strlen( pcStringToSend ),
428  *                                          &xHigherPriorityTaskWoken );
429  *
430  *  if( xBytesSent != strlen( pcStringToSend ) )
431  *  {
432  *      // The string could not be added to the message buffer because there was
433  *      // not enough free space in the buffer.
434  *  }
435  *
436  *  // If xHigherPriorityTaskWoken was set to pdTRUE inside
437  *  // xMessageBufferSendFromISR() then a task that has a priority above the
438  *  // priority of the currently executing task was unblocked and a context
439  *  // switch should be performed to ensure the ISR returns to the unblocked
440  *  // task.  In most FreeRTOS ports this is done by simply passing
441  *  // xHigherPriorityTaskWoken into portYIELD_FROM_ISR(), which will test the
442  *  // variables value, and perform the context switch if necessary.  Check the
443  *  // documentation for the port in use for port specific instructions.
444  *  portYIELD_FROM_ISR( xHigherPriorityTaskWoken );
445  * }
446  * @endcode
447  * \defgroup xMessageBufferSendFromISR xMessageBufferSendFromISR
448  * \ingroup MessageBufferManagement
449  */
450 #define xMessageBufferSendFromISR( xMessageBuffer, pvTxData, xDataLengthBytes, pxHigherPriorityTaskWoken ) \
451     xStreamBufferSendFromISR( ( xMessageBuffer ), ( pvTxData ), ( xDataLengthBytes ), ( pxHigherPriorityTaskWoken ) )
452
453 /**
454  * message_buffer.h
455  *
456  * @code{c}
457  * size_t xMessageBufferReceive( MessageBufferHandle_t xMessageBuffer,
458  *                            void *pvRxData,
459  *                            size_t xBufferLengthBytes,
460  *                            TickType_t xTicksToWait );
461  * @endcode
462  *
463  * Receives a discrete message from a message buffer.  Messages can be of
464  * variable length and are copied out of the buffer.
465  *
466  * ***NOTE***:  Uniquely among FreeRTOS objects, the stream buffer
467  * implementation (so also the message buffer implementation, as message buffers
468  * are built on top of stream buffers) assumes there is only one task or
469  * interrupt that will write to the buffer (the writer), and only one task or
470  * interrupt that will read from the buffer (the reader).  It is safe for the
471  * writer and reader to be different tasks or interrupts, but, unlike other
472  * FreeRTOS objects, it is not safe to have multiple different writers or
473  * multiple different readers.  If there are to be multiple different writers
474  * then the application writer must place each call to a writing API function
475  * (such as xMessageBufferSend()) inside a critical section and set the send
476  * block time to 0.  Likewise, if there are to be multiple different readers
477  * then the application writer must place each call to a reading API function
478  * (such as xMessageBufferRead()) inside a critical section and set the receive
479  * block time to 0.
480  *
481  * Use xMessageBufferReceive() to read from a message buffer from a task.  Use
482  * xMessageBufferReceiveFromISR() to read from a message buffer from an
483  * interrupt service routine (ISR).
484  *
485  * @param xMessageBuffer The handle of the message buffer from which a message
486  * is being received.
487  *
488  * @param pvRxData A pointer to the buffer into which the received message is
489  * to be copied.
490  *
491  * @param xBufferLengthBytes The length of the buffer pointed to by the pvRxData
492  * parameter.  This sets the maximum length of the message that can be received.
493  * If xBufferLengthBytes is too small to hold the next message then the message
494  * will be left in the message buffer and 0 will be returned.
495  *
496  * @param xTicksToWait The maximum amount of time the task should remain in the
497  * Blocked state to wait for a message, should the message buffer be empty.
498  * xMessageBufferReceive() will return immediately if xTicksToWait is zero and
499  * the message buffer is empty.  The block time is specified in tick periods, so
500  * the absolute time it represents is dependent on the tick frequency.  The
501  * macro pdMS_TO_TICKS() can be used to convert a time specified in milliseconds
502  * into a time specified in ticks.  Setting xTicksToWait to portMAX_DELAY will
503  * cause the task to wait indefinitely (without timing out), provided
504  * INCLUDE_vTaskSuspend is set to 1 in FreeRTOSConfig.h.  Tasks do not use any
505  * CPU time when they are in the Blocked state.
506  *
507  * @return The length, in bytes, of the message read from the message buffer, if
508  * any.  If xMessageBufferReceive() times out before a message became available
509  * then zero is returned.  If the length of the message is greater than
510  * xBufferLengthBytes then the message will be left in the message buffer and
511  * zero is returned.
512  *
513  * Example use:
514  * @code{c}
515  * void vAFunction( MessageBuffer_t xMessageBuffer )
516  * {
517  * uint8_t ucRxData[ 20 ];
518  * size_t xReceivedBytes;
519  * const TickType_t xBlockTime = pdMS_TO_TICKS( 20 );
520  *
521  *  // Receive the next message from the message buffer.  Wait in the Blocked
522  *  // state (so not using any CPU processing time) for a maximum of 100ms for
523  *  // a message to become available.
524  *  xReceivedBytes = xMessageBufferReceive( xMessageBuffer,
525  *                                          ( void * ) ucRxData,
526  *                                          sizeof( ucRxData ),
527  *                                          xBlockTime );
528  *
529  *  if( xReceivedBytes > 0 )
530  *  {
531  *      // A ucRxData contains a message that is xReceivedBytes long.  Process
532  *      // the message here....
533  *  }
534  * }
535  * @endcode
536  * \defgroup xMessageBufferReceive xMessageBufferReceive
537  * \ingroup MessageBufferManagement
538  */
539 #define xMessageBufferReceive( xMessageBuffer, pvRxData, xBufferLengthBytes, xTicksToWait ) \
540     xStreamBufferReceive( ( xMessageBuffer ), ( pvRxData ), ( xBufferLengthBytes ), ( xTicksToWait ) )
541
542
543 /**
544  * message_buffer.h
545  *
546  * @code{c}
547  * size_t xMessageBufferReceiveFromISR( MessageBufferHandle_t xMessageBuffer,
548  *                                   void *pvRxData,
549  *                                   size_t xBufferLengthBytes,
550  *                                   BaseType_t *pxHigherPriorityTaskWoken );
551  * @endcode
552  *
553  * An interrupt safe version of the API function that receives a discrete
554  * message from a message buffer.  Messages can be of variable length and are
555  * copied out of the buffer.
556  *
557  * ***NOTE***:  Uniquely among FreeRTOS objects, the stream buffer
558  * implementation (so also the message buffer implementation, as message buffers
559  * are built on top of stream buffers) assumes there is only one task or
560  * interrupt that will write to the buffer (the writer), and only one task or
561  * interrupt that will read from the buffer (the reader).  It is safe for the
562  * writer and reader to be different tasks or interrupts, but, unlike other
563  * FreeRTOS objects, it is not safe to have multiple different writers or
564  * multiple different readers.  If there are to be multiple different writers
565  * then the application writer must place each call to a writing API function
566  * (such as xMessageBufferSend()) inside a critical section and set the send
567  * block time to 0.  Likewise, if there are to be multiple different readers
568  * then the application writer must place each call to a reading API function
569  * (such as xMessageBufferRead()) inside a critical section and set the receive
570  * block time to 0.
571  *
572  * Use xMessageBufferReceive() to read from a message buffer from a task.  Use
573  * xMessageBufferReceiveFromISR() to read from a message buffer from an
574  * interrupt service routine (ISR).
575  *
576  * @param xMessageBuffer The handle of the message buffer from which a message
577  * is being received.
578  *
579  * @param pvRxData A pointer to the buffer into which the received message is
580  * to be copied.
581  *
582  * @param xBufferLengthBytes The length of the buffer pointed to by the pvRxData
583  * parameter.  This sets the maximum length of the message that can be received.
584  * If xBufferLengthBytes is too small to hold the next message then the message
585  * will be left in the message buffer and 0 will be returned.
586  *
587  * @param pxHigherPriorityTaskWoken  It is possible that a message buffer will
588  * have a task blocked on it waiting for space to become available.  Calling
589  * xMessageBufferReceiveFromISR() can make space available, and so cause a task
590  * that is waiting for space to leave the Blocked state.  If calling
591  * xMessageBufferReceiveFromISR() causes a task to leave the Blocked state, and
592  * the unblocked task has a priority higher than the currently executing task
593  * (the task that was interrupted), then, internally,
594  * xMessageBufferReceiveFromISR() will set *pxHigherPriorityTaskWoken to pdTRUE.
595  * If xMessageBufferReceiveFromISR() sets this value to pdTRUE, then normally a
596  * context switch should be performed before the interrupt is exited.  That will
597  * ensure the interrupt returns directly to the highest priority Ready state
598  * task.  *pxHigherPriorityTaskWoken should be set to pdFALSE before it is
599  * passed into the function.  See the code example below for an example.
600  *
601  * @return The length, in bytes, of the message read from the message buffer, if
602  * any.
603  *
604  * Example use:
605  * @code{c}
606  * // A message buffer that has already been created.
607  * MessageBuffer_t xMessageBuffer;
608  *
609  * void vAnInterruptServiceRoutine( void )
610  * {
611  * uint8_t ucRxData[ 20 ];
612  * size_t xReceivedBytes;
613  * BaseType_t xHigherPriorityTaskWoken = pdFALSE;  // Initialised to pdFALSE.
614  *
615  *  // Receive the next message from the message buffer.
616  *  xReceivedBytes = xMessageBufferReceiveFromISR( xMessageBuffer,
617  *                                                ( void * ) ucRxData,
618  *                                                sizeof( ucRxData ),
619  *                                                &xHigherPriorityTaskWoken );
620  *
621  *  if( xReceivedBytes > 0 )
622  *  {
623  *      // A ucRxData contains a message that is xReceivedBytes long.  Process
624  *      // the message here....
625  *  }
626  *
627  *  // If xHigherPriorityTaskWoken was set to pdTRUE inside
628  *  // xMessageBufferReceiveFromISR() then a task that has a priority above the
629  *  // priority of the currently executing task was unblocked and a context
630  *  // switch should be performed to ensure the ISR returns to the unblocked
631  *  // task.  In most FreeRTOS ports this is done by simply passing
632  *  // xHigherPriorityTaskWoken into portYIELD_FROM_ISR(), which will test the
633  *  // variables value, and perform the context switch if necessary.  Check the
634  *  // documentation for the port in use for port specific instructions.
635  *  portYIELD_FROM_ISR( xHigherPriorityTaskWoken );
636  * }
637  * @endcode
638  * \defgroup xMessageBufferReceiveFromISR xMessageBufferReceiveFromISR
639  * \ingroup MessageBufferManagement
640  */
641 #define xMessageBufferReceiveFromISR( xMessageBuffer, pvRxData, xBufferLengthBytes, pxHigherPriorityTaskWoken ) \
642     xStreamBufferReceiveFromISR( ( xMessageBuffer ), ( pvRxData ), ( xBufferLengthBytes ), ( pxHigherPriorityTaskWoken ) )
643
644 /**
645  * message_buffer.h
646  *
647  * @code{c}
648  * void vMessageBufferDelete( MessageBufferHandle_t xMessageBuffer );
649  * @endcode
650  *
651  * Deletes a message buffer that was previously created using a call to
652  * xMessageBufferCreate() or xMessageBufferCreateStatic().  If the message
653  * buffer was created using dynamic memory (that is, by xMessageBufferCreate()),
654  * then the allocated memory is freed.
655  *
656  * A message buffer handle must not be used after the message buffer has been
657  * deleted.
658  *
659  * @param xMessageBuffer The handle of the message buffer to be deleted.
660  *
661  */
662 #define vMessageBufferDelete( xMessageBuffer ) \
663     vStreamBufferDelete( xMessageBuffer )
664
665 /**
666  * message_buffer.h
667  * @code{c}
668  * BaseType_t xMessageBufferIsFull( MessageBufferHandle_t xMessageBuffer );
669  * @endcode
670  *
671  * Tests to see if a message buffer is full.  A message buffer is full if it
672  * cannot accept any more messages, of any size, until space is made available
673  * by a message being removed from the message buffer.
674  *
675  * @param xMessageBuffer The handle of the message buffer being queried.
676  *
677  * @return If the message buffer referenced by xMessageBuffer is full then
678  * pdTRUE is returned.  Otherwise pdFALSE is returned.
679  */
680 #define xMessageBufferIsFull( xMessageBuffer ) \
681     xStreamBufferIsFull( xMessageBuffer )
682
683 /**
684  * message_buffer.h
685  * @code{c}
686  * BaseType_t xMessageBufferIsEmpty( MessageBufferHandle_t xMessageBuffer );
687  * @endcode
688  *
689  * Tests to see if a message buffer is empty (does not contain any messages).
690  *
691  * @param xMessageBuffer The handle of the message buffer being queried.
692  *
693  * @return If the message buffer referenced by xMessageBuffer is empty then
694  * pdTRUE is returned.  Otherwise pdFALSE is returned.
695  *
696  */
697 #define xMessageBufferIsEmpty( xMessageBuffer ) \
698     xStreamBufferIsEmpty( xMessageBuffer )
699
700 /**
701  * message_buffer.h
702  * @code{c}
703  * BaseType_t xMessageBufferReset( MessageBufferHandle_t xMessageBuffer );
704  * @endcode
705  *
706  * Resets a message buffer to its initial empty state, discarding any message it
707  * contained.
708  *
709  * A message buffer can only be reset if there are no tasks blocked on it.
710  *
711  * @param xMessageBuffer The handle of the message buffer being reset.
712  *
713  * @return If the message buffer was reset then pdPASS is returned.  If the
714  * message buffer could not be reset because either there was a task blocked on
715  * the message queue to wait for space to become available, or to wait for a
716  * a message to be available, then pdFAIL is returned.
717  *
718  * \defgroup xMessageBufferReset xMessageBufferReset
719  * \ingroup MessageBufferManagement
720  */
721 #define xMessageBufferReset( xMessageBuffer ) \
722     xStreamBufferReset( xMessageBuffer )
723
724
725 /**
726  * message_buffer.h
727  * @code{c}
728  * size_t xMessageBufferSpaceAvailable( MessageBufferHandle_t xMessageBuffer );
729  * @endcode
730  * Returns the number of bytes of free space in the message buffer.
731  *
732  * @param xMessageBuffer The handle of the message buffer being queried.
733  *
734  * @return The number of bytes that can be written to the message buffer before
735  * the message buffer would be full.  When a message is written to the message
736  * buffer an additional sizeof( size_t ) bytes are also written to store the
737  * message's length.  sizeof( size_t ) is typically 4 bytes on a 32-bit
738  * architecture, so if xMessageBufferSpacesAvailable() returns 10, then the size
739  * of the largest message that can be written to the message buffer is 6 bytes.
740  *
741  * \defgroup xMessageBufferSpaceAvailable xMessageBufferSpaceAvailable
742  * \ingroup MessageBufferManagement
743  */
744 #define xMessageBufferSpaceAvailable( xMessageBuffer ) \
745     xStreamBufferSpacesAvailable( xMessageBuffer )
746 #define xMessageBufferSpacesAvailable( xMessageBuffer ) \
747     xStreamBufferSpacesAvailable( xMessageBuffer ) /* Corrects typo in original macro name. */
748
749 /**
750  * message_buffer.h
751  * @code{c}
752  * size_t xMessageBufferNextLengthBytes( MessageBufferHandle_t xMessageBuffer );
753  * @endcode
754  * Returns the length (in bytes) of the next message in a message buffer.
755  * Useful if xMessageBufferReceive() returned 0 because the size of the buffer
756  * passed into xMessageBufferReceive() was too small to hold the next message.
757  *
758  * @param xMessageBuffer The handle of the message buffer being queried.
759  *
760  * @return The length (in bytes) of the next message in the message buffer, or 0
761  * if the message buffer is empty.
762  *
763  * \defgroup xMessageBufferNextLengthBytes xMessageBufferNextLengthBytes
764  * \ingroup MessageBufferManagement
765  */
766 #define xMessageBufferNextLengthBytes( xMessageBuffer ) \
767     xStreamBufferNextMessageLengthBytes( xMessageBuffer ) PRIVILEGED_FUNCTION;
768
769 /**
770  * message_buffer.h
771  *
772  * @code{c}
773  * BaseType_t xMessageBufferSendCompletedFromISR( MessageBufferHandle_t xMessageBuffer, BaseType_t *pxHigherPriorityTaskWoken );
774  * @endcode
775  *
776  * For advanced users only.
777  *
778  * The sbSEND_COMPLETED() macro is called from within the FreeRTOS APIs when
779  * data is sent to a message buffer or stream buffer.  If there was a task that
780  * was blocked on the message or stream buffer waiting for data to arrive then
781  * the sbSEND_COMPLETED() macro sends a notification to the task to remove it
782  * from the Blocked state.  xMessageBufferSendCompletedFromISR() does the same
783  * thing.  It is provided to enable application writers to implement their own
784  * version of sbSEND_COMPLETED(), and MUST NOT BE USED AT ANY OTHER TIME.
785  *
786  * See the example implemented in FreeRTOS/Demo/Minimal/MessageBufferAMP.c for
787  * additional information.
788  *
789  * @param xMessageBuffer The handle of the stream buffer to which data was
790  * written.
791  *
792  * @param pxHigherPriorityTaskWoken *pxHigherPriorityTaskWoken should be
793  * initialised to pdFALSE before it is passed into
794  * xMessageBufferSendCompletedFromISR().  If calling
795  * xMessageBufferSendCompletedFromISR() removes a task from the Blocked state,
796  * and the task has a priority above the priority of the currently running task,
797  * then *pxHigherPriorityTaskWoken will get set to pdTRUE indicating that a
798  * context switch should be performed before exiting the ISR.
799  *
800  * @return If a task was removed from the Blocked state then pdTRUE is returned.
801  * Otherwise pdFALSE is returned.
802  *
803  * \defgroup xMessageBufferSendCompletedFromISR xMessageBufferSendCompletedFromISR
804  * \ingroup StreamBufferManagement
805  */
806 #define xMessageBufferSendCompletedFromISR( xMessageBuffer, pxHigherPriorityTaskWoken ) \
807     xStreamBufferSendCompletedFromISR( ( xMessageBuffer ), ( pxHigherPriorityTaskWoken ) )
808
809 /**
810  * message_buffer.h
811  *
812  * @code{c}
813  * BaseType_t xMessageBufferReceiveCompletedFromISR( MessageBufferHandle_t xMessageBuffer, BaseType_t *pxHigherPriorityTaskWoken );
814  * @endcode
815  *
816  * For advanced users only.
817  *
818  * The sbRECEIVE_COMPLETED() macro is called from within the FreeRTOS APIs when
819  * data is read out of a message buffer or stream buffer.  If there was a task
820  * that was blocked on the message or stream buffer waiting for data to arrive
821  * then the sbRECEIVE_COMPLETED() macro sends a notification to the task to
822  * remove it from the Blocked state.  xMessageBufferReceiveCompletedFromISR()
823  * does the same thing.  It is provided to enable application writers to
824  * implement their own version of sbRECEIVE_COMPLETED(), and MUST NOT BE USED AT
825  * ANY OTHER TIME.
826  *
827  * See the example implemented in FreeRTOS/Demo/Minimal/MessageBufferAMP.c for
828  * additional information.
829  *
830  * @param xMessageBuffer The handle of the stream buffer from which data was
831  * read.
832  *
833  * @param pxHigherPriorityTaskWoken *pxHigherPriorityTaskWoken should be
834  * initialised to pdFALSE before it is passed into
835  * xMessageBufferReceiveCompletedFromISR().  If calling
836  * xMessageBufferReceiveCompletedFromISR() removes a task from the Blocked state,
837  * and the task has a priority above the priority of the currently running task,
838  * then *pxHigherPriorityTaskWoken will get set to pdTRUE indicating that a
839  * context switch should be performed before exiting the ISR.
840  *
841  * @return If a task was removed from the Blocked state then pdTRUE is returned.
842  * Otherwise pdFALSE is returned.
843  *
844  * \defgroup xMessageBufferReceiveCompletedFromISR xMessageBufferReceiveCompletedFromISR
845  * \ingroup StreamBufferManagement
846  */
847 #define xMessageBufferReceiveCompletedFromISR( xMessageBuffer, pxHigherPriorityTaskWoken ) \
848     xStreamBufferReceiveCompletedFromISR( ( xMessageBuffer ), ( pxHigherPriorityTaskWoken ) )
849
850 /* *INDENT-OFF* */
851 #if defined( __cplusplus )
852     } /* extern "C" */
853 #endif
854 /* *INDENT-ON* */
855
856 #endif /* !defined( FREERTOS_MESSAGE_BUFFER_H ) */