FreeRTOS-Cpp
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 
34 namespace 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, void* ptr) { return ptr; }
69  static void* operator new[](size_t, void* ptr) { return ptr; }
70  static void* operator new(size_t) = delete;
71  static void* operator new[](size_t) = delete;
72 
83  inline bool isValid() const { return (handle != NULL); }
84 
129  inline size_t send(const void* data, const size_t length,
130  const TickType_t ticksToWait = portMAX_DELAY) const {
131  return xMessageBufferSend(handle, data, length, ticksToWait);
132  }
133 
178  inline size_t sendFromISR(bool& higherPriorityTaskWoken, const void* data,
179  const size_t length) const {
180  BaseType_t taskWoken = pdFALSE;
181  size_t result = xMessageBufferSendFromISR(handle, data, length, &taskWoken);
182  if (taskWoken == pdTRUE) {
183  higherPriorityTaskWoken = true;
184  }
185  return result;
186  }
187 
199  inline size_t sendFromISR(const void* data, const size_t length) const {
200  return xMessageBufferSendFromISR(handle, data, length, NULL);
201  }
202 
241  inline size_t receive(void* buffer, const size_t bufferLength,
242  const TickType_t ticksToWait = portMAX_DELAY) const {
243  return xMessageBufferReceive(handle, buffer, bufferLength, ticksToWait);
244  }
245 
284  inline size_t receiveFromISR(bool& higherPriorityTaskWoken, void* buffer,
285  const size_t bufferLength) const {
286  BaseType_t taskWoken = pdFALSE;
287  size_t result =
288  xMessageBufferReceiveFromISR(handle, buffer, bufferLength, &taskWoken);
289  if (taskWoken == pdTRUE) {
290  higherPriorityTaskWoken = true;
291  }
292  return result;
293  }
294 
306  inline size_t receiveFromISR(void* buffer, const size_t bufferLength) const {
307  return xMessageBufferReceiveFromISR(handle, buffer, bufferLength, NULL);
308  }
309 
330  inline size_t spacesAvailable() const {
331  return xMessageBufferSpacesAvailable(handle);
332  }
333 
351  inline bool reset() const { return (xMessageBufferReset(handle) == pdPASS); }
352 
367  inline bool isEmpty() const {
368  return (xMessageBufferIsEmpty(handle) == pdTRUE);
369  }
370 
386  inline bool isFull() const {
387  return (xMessageBufferIsFull(handle) == pdTRUE);
388  }
389 
390  private:
391  MessageBufferBase() = default;
392 
404  ~MessageBufferBase() { vMessageBufferDelete(this->handle); }
405 
406  MessageBufferBase(MessageBufferBase&&) noexcept = default;
407  MessageBufferBase& operator=(MessageBufferBase&&) noexcept = default;
408 
409  MessageBufferHandle_t handle = NULL;
410 };
411 
412 #if (configSUPPORT_DYNAMIC_ALLOCATION == 1)
413 
425  public:
449  explicit MessageBuffer(size_t size) {
450  this->handle = xMessageBufferCreate(size);
451  }
452  ~MessageBuffer() = default;
453 
454  MessageBuffer(const MessageBuffer&) = delete;
455  MessageBuffer& operator=(const MessageBuffer&) = delete;
456 
457  MessageBuffer(MessageBuffer&&) noexcept = default;
458  MessageBuffer& operator=(MessageBuffer&&) noexcept = default;
459 };
460 
461 #endif /* configSUPPORT_DYNAMIC_ALLOCATION */
462 
463 #if (configSUPPORT_STATIC_ALLOCATION == 1)
464 
477 template <size_t N>
479  public:
498  this->handle = xMessageBufferCreateStatic(sizeof(storage), storage,
499  &staticMessageBuffer);
500  }
501  ~StaticMessageBuffer() = default;
502 
503  StaticMessageBuffer(const StaticMessageBuffer&) = delete;
504  StaticMessageBuffer& operator=(const StaticMessageBuffer&) = delete;
505 
506  StaticMessageBuffer(StaticMessageBuffer&&) noexcept = default;
507  StaticMessageBuffer& operator=(StaticMessageBuffer&&) noexcept = default;
508 
509  private:
510  StaticMessageBuffer_t staticMessageBuffer;
511  uint8_t storage[N] = {0};
512 };
513 
514 #endif /* configSUPPORT_STATIC_ALLOCATION */
515 
516 } // namespace FreeRTOS
517 
518 #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:351
size_t spacesAvailable() const
Function that calls size_t xMessageBufferSpacesAvailable( MessageBufferHandle_t xMessageBuffer )
Definition: MessageBuffer.hpp:330
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:129
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:178
size_t receiveFromISR(void *buffer, const size_t bufferLength) const
Function that calls size_t xMessageBufferReceiveFromISR( MessageBufferHandle_t xMessageBuffer,...
Definition: MessageBuffer.hpp:306
bool isFull() const
Function that calls BaseType_t xMessageBufferIsFull( MessageBufferHandle_t xMessageBuffer )
Definition: MessageBuffer.hpp:386
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:241
bool isValid() const
Function that checks if the underlying message buffer handle is not NULL. This should be used to ensu...
Definition: MessageBuffer.hpp:83
size_t sendFromISR(const void *data, const size_t length) const
Function that calls size_t xMessageBufferSendFromISR( MessageBufferHandle_t xMessageBuffer,...
Definition: MessageBuffer.hpp:199
size_t receiveFromISR(bool &higherPriorityTaskWoken, void *buffer, const size_t bufferLength) const
Function that calls size_t xMessageBufferReceiveFromISR( MessageBufferHandle_t xMessageBuffer,...
Definition: MessageBuffer.hpp:284
~MessageBufferBase()
Destroy the MessageBufferBase object by calling void vMessageBufferDelete( MessageBufferHandle_t xMes...
Definition: MessageBuffer.hpp:404
bool isEmpty() const
Function that calls BaseType_t xMessageBufferIsEmpty( MessageBufferHandle_t xMessageBuffer )
Definition: MessageBuffer.hpp:367
Class that encapsulates the functionality of a FreeRTOS message buffer.
Definition: MessageBuffer.hpp:424
MessageBuffer(size_t size)
Construct a new MessageBuffer object by calling MessageBufferHandle_t xMessageBufferCreate( size_t xB...
Definition: MessageBuffer.hpp:449
Class that encapsulates the functionality of a FreeRTOS message buffer.
Definition: MessageBuffer.hpp:478
StaticMessageBuffer()
Construct a new StaticMessageBuffer object by calling MessageBufferHandle_t xMessageBufferCreateStati...
Definition: MessageBuffer.hpp:497