FreeRTOScpp
Loading...
Searching...
No Matches
MessageBufferCPP.h
Go to the documentation of this file.
1/**
2 * @file MessageBufferCPP.h
3 * @brief FreeRTOS MessageBuffer wrapper
4 *
5 * Wrapper for FreeRTOS MessageBuffers
6 *
7 * @copyright (c) 2024 Richard Damon
8 * @author Richard Damon <richard.damon@gmail.com>
9 * @parblock
10 * MIT License:
11 *
12 * Permission is hereby granted, free of charge, to any person obtaining a copy
13 * of this software and associated documentation files (the "Software"), to deal
14 * in the Software without restriction, including without limitation the rights
15 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
16 * copies of the Software, and to permit persons to whom the Software is
17 * furnished to do so, subject to the following conditions:
18 *
19 * The above copyright notice and this permission notice shall be included in
20 * all copies or substantial portions of the Software.
21 *
22 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
25 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
27 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
28 * THE SOFTWARE.
29 *
30 * It is requested (but not required by license) that any bugs found or
31 * improvements made be shared, preferably to the author.
32 * @endparblock
33 *
34 * @ingroup FreeRTOSCpp
35 */
36
37#ifndef MESSAGEBUFFER_CPP_H
38#define MESSAGEBUFFER_CPP_H
39
40#include "FreeRTOScpp.h"
41
42#include "message_buffer.h"
43
44#if FREERTOSCPP_USE_NAMESPACE
45namespace FreeRTOScpp {
46#endif
47
48/**
49 * Base Wrapper Class for MessageBuffer
50 *
51 * Base Class does all the operations, there is then a derived class to build the buffer,
52 * or the base class can be a wrapper around an elsewhere created handle.
53 */
55public:
56 MessageBufferBase(MessageBufferHandle_t mbHandle) : msgHandle(mbHandle) {}
57 virtual ~MessageBufferBase() { }
58
59 size_t send(const void* data, size_t len, TickType_t delay = portMAX_DELAY)
60 {return xMessageBufferSend(msgHandle, data, len, delay);}
61#if FREERTOSCPP_USE_CHRONO
62 size_t send(const void* data, size_t len, Time_ms delay)
63 {return xMessageBufferSend(msgHandle, data, len, ms2ticks(delay));}
64#endif
65 size_t send_ISR(const void* data, size_t len, BaseType_t &wasWoken)
66 {return xMessageBufferSendFromISR(msgHandle, data, len, &wasWoken);}
67
68 size_t read(void* data, size_t len, TickType_t delay = portMAX_DELAY)
69 {return xMessageBufferReceive(msgHandle, data, len, delay);}
70#if FREERTOSCPP_USE_CHRONO
71 size_t read(void* data, size_t len, Time_ms delay)
72 {return xMessageBufferReceive(msgHandle, data, len, ms2ticks(delay));}
73#endif
74 size_t read_ISR(void* data, size_t len, BaseType_t &wasWoken)
75 {return xMessageBufferReceiveFromISR(msgHandle, data, len, &wasWoken);}
76
77 // Message Buffers do not provide "Bytes Available"
78
79 /// @brief Get the amount of available space open in the MessageBuffer
80 /// @return The number of bytes that can be sent before the buffer is full
81 size_t available() const { return xMessageBufferSpacesAvailable(msgHandle);}
82
83 bool isEmpty() const { return xMessageBufferIsEmpty(msgHandle);}
84
85 bool isFull() const { return xMessageBufferIsFull(msgHandle);}
86
87 /// @brief Resets the buffer to empty
88 /// @return True if done, stream can not be reset if a task is waiting on the MessageBuffer.
89 bool reset() { return xMessageBufferReset(msgHandle);}
90
91 MessageBufferHandle_t msgHandle;
92};
93
94/**
95 * Template to implement a Message Buffer of a given size.
96 *
97 * MessageBuffer will be created statically if possible.
98 *
99 * @tparam size The number of bytes to store in the buffer, 0 = dynamically created
100 */
101
102template <size_t size
103#if( configSUPPORT_DYNAMIC_ALLOCATION == 1 )
104 = 0
105#endif
106>
108public:
111#if( configSUPPORT_STATIC_ALLOCATION == 1 )
112 xMessageBufferCreateStatic(size, storage, msgBuff)
113#else
114 xMessageBufferCreate(size)
115#endif
116 ) {}
117
118 virtual ~MessageBuffer() { vMessageBufferDelete(msgHandle);}
119
120#if configUSE_SB_COMPLETED_CALLBACK
121 MessageBuffer(StreamBufferCallbackFunction_t sendCallback, StreamBufferCallbackFunction_t recvCallback) :
123#if( configSUPPORT_STATIC_ALLOCATION == 1 )
124 xMessageBufferCreateStaticWithCallback(size, storage, msgBuff, sendCallback, recvCallback)
125#else
126 xMessageBufferCreateWithCallback(size, sendCallback. recvCallBack)
127#endif
128 )
129 {}
130#endif //configUSE_SB_COMPLETED_CALLBACK
131
132#if( configSUPPORT_STATIC_ALLOCATION == 1 )
133 uint8_t storage[size+1];
134 StaticMessageBuffer_t msgBuff;
135#endif
136};
137
138#if( configSUPPORT_DYNAMIC_ALLOCATION == 1 )
139
140template <>
141class MessageBuffer<0> : public MessageBufferBase {
142public:
143 MessageBuffer(size_t size) :
144 MessageBufferBase(xMessageBufferCreate(size))
145 {}
146 virtual ~MessageBuffer() { vMessageBufferDelete(msgHandle);}
147
148#if configUSE_SB_COMPLETED_CALLBACK
149 MessageBuffer(size_t size, StreamBufferCallbackFunction_t sendCallback, StreamBufferCallbackFunction_t recvCallback) :
150 MessageBufferBase(xMessageBufferCreateWithCallback(size, sendCallback, recvCallback))
151 {}
152#endif // configUSE_SB_COMPLETED_CALLBACK
153};
154#endif
155
156#if FREERTOSCPP_USE_NAMESPACE
157}
158#endif
159
160#endif
FreeRTOS Wrapper.
Base Wrapper Class for MessageBuffer.
Definition MessageBufferCPP.h:54
virtual ~MessageBufferBase()
Definition MessageBufferCPP.h:57
size_t read_ISR(void *data, size_t len, BaseType_t &wasWoken)
Definition MessageBufferCPP.h:74
size_t send(const void *data, size_t len, TickType_t delay=portMAX_DELAY)
Definition MessageBufferCPP.h:59
size_t send_ISR(const void *data, size_t len, BaseType_t &wasWoken)
Definition MessageBufferCPP.h:65
size_t available() const
Get the amount of available space open in the MessageBuffer.
Definition MessageBufferCPP.h:81
MessageBufferBase(MessageBufferHandle_t mbHandle)
Definition MessageBufferCPP.h:56
bool isEmpty() const
Definition MessageBufferCPP.h:83
size_t read(void *data, size_t len, TickType_t delay=portMAX_DELAY)
Definition MessageBufferCPP.h:68
bool reset()
Resets the buffer to empty.
Definition MessageBufferCPP.h:89
bool isFull() const
Definition MessageBufferCPP.h:85
MessageBufferHandle_t msgHandle
Definition MessageBufferCPP.h:91
Template to implement a Message Buffer of a given size.
Definition MessageBufferCPP.h:107
MessageBuffer()
Definition MessageBufferCPP.h:109
virtual ~MessageBuffer()
Definition MessageBufferCPP.h:118
Definition FreeRTOScpp.h:74