FreeRTOS-Cpp
Loading...
Searching...
No Matches
MessageBuffer.hpp
1/*
2 * FreeRTOS-Cpp
3 * Copyright (C) 2021 Jon Enz. All Rights Reserved.
4 *
5 * SPDX-License-Identifier: MIT
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining a copy
8 * of this software and associated documentation files (the "Software"), to deal
9 * in the Software without restriction, including without limitation the rights
10 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 * copies of the Software, and to permit persons to whom the Software is
12 * furnished to do so, subject to the following conditions:
13 *
14 * The above copyright notice and this permission notice shall be included in
15 * all 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,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23 * SOFTWARE.
24 *
25 * https://github.com/jonenz/FreeRTOS-Cpp
26 */
27
28#ifndef FREERTOS_MESSAGEBUFFER_HPP
29#define FREERTOS_MESSAGEBUFFER_HPP
30
31#include "FreeRTOS.h"
32#include "message_buffer.h"
33
34namespace FreeRTOS {
35
60 public:
61 friend class MessageBuffer;
62 template <size_t>
63 friend class StaticMessageBuffer;
64
65 MessageBufferBase(const MessageBufferBase&) = delete;
66 MessageBufferBase& operator=(const MessageBufferBase&) = delete;
67
68 static void* operator new(size_t) = delete;
69 static void* operator new[](size_t) = delete;
70
71 static void* operator new(size_t, void* ptr) {
72 return ptr;
73 }
74
75 static void* operator new[](size_t, void* ptr) {
76 return ptr;
77 }
78
89 inline bool isValid() const {
90 return (handle != NULL);
91 }
92
137 inline size_t send(const void* data, const size_t length,
138 const TickType_t ticksToWait = portMAX_DELAY) const {
139 return xMessageBufferSend(handle, data, length, ticksToWait);
140 }
141
186 inline size_t sendFromISR(bool& higherPriorityTaskWoken, const void* data,
187 const size_t length) const {
188 BaseType_t taskWoken = pdFALSE;
189 const size_t result =
190 xMessageBufferSendFromISR(handle, data, length, &taskWoken);
191 if (taskWoken == pdTRUE) {
192 higherPriorityTaskWoken = true;
193 }
194 return result;
195 }
196
208 inline size_t sendFromISR(const void* data, const size_t length) const {
209 return xMessageBufferSendFromISR(handle, data, length, NULL);
210 }
211
250 inline size_t receive(void* buffer, const size_t bufferLength,
251 const TickType_t ticksToWait = portMAX_DELAY) const {
252 return xMessageBufferReceive(handle, buffer, bufferLength, ticksToWait);
253 }
254
293 inline size_t receiveFromISR(bool& higherPriorityTaskWoken, void* buffer,
294 const size_t bufferLength) const {
295 BaseType_t taskWoken = pdFALSE;
296 const size_t result =
297 xMessageBufferReceiveFromISR(handle, buffer, bufferLength, &taskWoken);
298 if (taskWoken == pdTRUE) {
299 higherPriorityTaskWoken = true;
300 }
301 return result;
302 }
303
315 inline size_t receiveFromISR(void* buffer, const size_t bufferLength) const {
316 return xMessageBufferReceiveFromISR(handle, buffer, bufferLength, NULL);
317 }
318
339 inline size_t spacesAvailable() const {
340 return xMessageBufferSpacesAvailable(handle);
341 }
342
360 inline bool reset() const {
361 return (xMessageBufferReset(handle) == pdPASS);
362 }
363
378 inline bool isEmpty() const {
379 return (xMessageBufferIsEmpty(handle) == pdTRUE);
380 }
381
397 inline bool isFull() const {
398 return (xMessageBufferIsFull(handle) == pdTRUE);
399 }
400
401 private:
402 MessageBufferBase() = default;
403
416 vMessageBufferDelete(this->handle);
417 }
418
419 MessageBufferBase(MessageBufferBase&&) noexcept = default;
420 MessageBufferBase& operator=(MessageBufferBase&&) noexcept = default;
421
422 MessageBufferHandle_t handle = NULL;
423};
424
425#if (configSUPPORT_DYNAMIC_ALLOCATION == 1)
426
438 public:
462 explicit MessageBuffer(size_t size) {
463 this->handle = xMessageBufferCreate(size);
464 }
465 ~MessageBuffer() = default;
466
467 MessageBuffer(const MessageBuffer&) = delete;
468 MessageBuffer& operator=(const MessageBuffer&) = delete;
469
470 MessageBuffer(MessageBuffer&&) noexcept = default;
471 MessageBuffer& operator=(MessageBuffer&&) noexcept = default;
472};
473
474#endif /* configSUPPORT_DYNAMIC_ALLOCATION */
475
476#if (configSUPPORT_STATIC_ALLOCATION == 1)
477
490template <size_t N>
492 public:
511 this->handle = xMessageBufferCreateStatic(sizeof(storage), storage,
512 &staticMessageBuffer);
513 }
514 ~StaticMessageBuffer() = default;
515
517 StaticMessageBuffer& operator=(const StaticMessageBuffer&) = delete;
518
519 StaticMessageBuffer(StaticMessageBuffer&&) noexcept = default;
520 StaticMessageBuffer& operator=(StaticMessageBuffer&&) noexcept = default;
521
522 private:
523 StaticMessageBuffer_t staticMessageBuffer;
524 uint8_t storage[N] = {0};
525};
526
527#endif /* configSUPPORT_STATIC_ALLOCATION */
528
529} // namespace FreeRTOS
530
531#endif // FREERTOS_MESSAGEBUFFER_HPP
Base class that provides the standard message buffer interface to FreeRTOS::MessageBuffer and FreeRTO...
Definition MessageBuffer.hpp:59
bool reset() const
Function that calls BaseType_t xMessageBufferReset( MessageBufferHandle_t xMessageBuffer )
Definition MessageBuffer.hpp:360
size_t spacesAvailable() const
Function that calls size_t xMessageBufferSpacesAvailable( MessageBufferHandle_t xMessageBuffer )
Definition MessageBuffer.hpp:339
size_t send(const void *data, const size_t length, const TickType_t ticksToWait=portMAX_DELAY) const
Function that calls size_t xMessageBufferSend( MessageBufferHandle_t xMessageBuffer,...
Definition MessageBuffer.hpp:137
size_t sendFromISR(bool &higherPriorityTaskWoken, const void *data, const size_t length) const
Function that calls size_t xMessageBufferSendFromISR( MessageBufferHandle_t xMessageBuffer,...
Definition MessageBuffer.hpp:186
size_t receiveFromISR(void *buffer, const size_t bufferLength) const
Function that calls size_t xMessageBufferReceiveFromISR( MessageBufferHandle_t xMessageBuffer,...
Definition MessageBuffer.hpp:315
bool isFull() const
Function that calls BaseType_t xMessageBufferIsFull( MessageBufferHandle_t xMessageBuffer )
Definition MessageBuffer.hpp:397
size_t receive(void *buffer, const size_t bufferLength, const TickType_t ticksToWait=portMAX_DELAY) const
Function that calls size_t xMessageBufferReceive( MessageBufferHandle_t xMessageBuffer,...
Definition MessageBuffer.hpp:250
bool isValid() const
Function that checks if the underlying message buffer handle is not NULL. This should be used to ensu...
Definition MessageBuffer.hpp:89
size_t sendFromISR(const void *data, const size_t length) const
Function that calls size_t xMessageBufferSendFromISR( MessageBufferHandle_t xMessageBuffer,...
Definition MessageBuffer.hpp:208
size_t receiveFromISR(bool &higherPriorityTaskWoken, void *buffer, const size_t bufferLength) const
Function that calls size_t xMessageBufferReceiveFromISR( MessageBufferHandle_t xMessageBuffer,...
Definition MessageBuffer.hpp:293
~MessageBufferBase()
Destroy the MessageBufferBase object by calling void vMessageBufferDelete( MessageBufferHandle_t xMes...
Definition MessageBuffer.hpp:415
bool isEmpty() const
Function that calls BaseType_t xMessageBufferIsEmpty( MessageBufferHandle_t xMessageBuffer )
Definition MessageBuffer.hpp:378
Class that encapsulates the functionality of a FreeRTOS message buffer.
Definition MessageBuffer.hpp:437
MessageBuffer(size_t size)
Construct a new MessageBuffer object by calling MessageBufferHandle_t xMessageBufferCreate( size_t xB...
Definition MessageBuffer.hpp:462
Class that encapsulates the functionality of a FreeRTOS message buffer.
Definition MessageBuffer.hpp:491
StaticMessageBuffer()
Construct a new StaticMessageBuffer object by calling MessageBufferHandle_t xMessageBufferCreateStati...
Definition MessageBuffer.hpp:510