RTOS Wrapper
API reference
Loading...
Searching...
No Matches
OsEventFlags.h
Go to the documentation of this file.
1
15#ifndef OS_EVENT_FLAGS_H_
16#define OS_EVENT_FLAGS_H_
17
18#include <cstdint>
19#include <climits>
20#include "OsAbstraction.h"
21#include "OsUtility.h"
22
27enum class WaitMode : uint8_t {
28 Any,
29 All,
30};
31
38template <size_t kReserved = 1>
40public:
41 explicit OsEventFlags(const char* groupName) noexcept
42 : name_(groupName)
43 {
45 }
46
47 OsEventFlags(const OsEventFlags&) = delete;
49
50 ~OsEventFlags() noexcept
51 {
52 if (created_) {
54 }
55 }
56
58 [[nodiscard]] bool IsValid() const noexcept { return created_; }
59
63 bool Set(uint32_t bits) noexcept
64 {
65 if (!created_) return false;
66 return os_event_flags_set_ex(group_, static_cast<OS_Ulong>(bits));
67 }
68
72 bool Clear(uint32_t bits) noexcept
73 {
74 if (!created_) return false;
75 return os_event_flags_clear_ex(group_, static_cast<OS_Ulong>(bits));
76 }
77
90 bool Wait(uint32_t bits, WaitMode mode, uint32_t timeout_ms,
91 uint32_t& actual_bits) noexcept
92 {
93 actual_bits = 0;
94 if (!created_) return false;
95 OS_Ulong actual = 0;
96 const OS_Uint option = (mode == WaitMode::All)
97 ? static_cast<OS_Uint>(OS_AND)
98 : static_cast<OS_Uint>(OS_OR);
99 const bool ok = os_event_flags_get_ex(group_, static_cast<OS_Ulong>(bits),
100 option, actual, ToTicks(timeout_ms));
101 actual_bits = static_cast<uint32_t>(actual);
102 return ok;
103 }
104
105private:
106 static OS_Ulong ToTicks(uint32_t timeout_ms) noexcept
107 {
108 return (timeout_ms == UINT32_MAX)
109 ? static_cast<OS_Ulong>(OS_WAIT_FOREVER)
110 : static_cast<OS_Ulong>(os_convert_msec_to_delay_ticks(timeout_ms));
111 }
112
113 const char* name_;
114 bool created_{false};
116};
117
118#endif /* OS_EVENT_FLAGS_H_ */
119
Generic RTOS abstraction — public header.
WaitMode
Selects ANY-of-bits vs ALL-of-bits semantics for Wait calls.
Definition OsEventFlags.h:27
@ All
Wake only when all of the requested bits are set.
@ Any
Wake when any of the requested bits are set.
C-facing helper functions for delays, cycle counters, and creation of RTOS handles (queues,...
bool os_event_flags_set_ex(OS_EventGroup &eventFlags, OS_Ulong flagsToSet, bool suppressVerbose=true) noexcept
Set event flags in the event flag group.
Definition OsUtility.cpp:900
constexpr uint32_t os_convert_msec_to_delay_ticks(uint32_t milliseconds)
Converts milliseconds to delay ticks, rounding down but never to zero.
Definition OsUtility.h:74
bool os_event_flags_delete_ex(OS_EventGroup &eventFlags, bool suppressVerbose=true) noexcept
Deletes the specified event flag group and logs any error.
Definition OsUtility.cpp:877
bool os_event_flags_get_ex(OS_EventGroup &eventFlags, OS_Ulong flagsToGet, OS_Uint getOption, OS_Ulong &actualFlags, OS_Ulong wait_option, bool suppressVerbose=true) noexcept
Get event flags from the event flag group.
Definition OsUtility.cpp:933
bool os_event_flags_clear_ex(OS_EventGroup &eventFlags, OS_Ulong flagsToClear, bool suppressVerbose=true) noexcept
Clears event flags in the event flag group.
Definition OsUtility.cpp:915
bool os_event_flags_create_ex(OS_EventGroup &eventFlags, const char *name, bool suppressVerbose=true) noexcept
Creates an event flag group with the specified parameters.
Definition OsUtility.cpp:851
Eager-init event-flag group with modern C++ public surface.
Definition OsEventFlags.h:39
bool Set(uint32_t bits) noexcept
Set bits in the group.
Definition OsEventFlags.h:63
OsEventFlags(const char *groupName) noexcept
Definition OsEventFlags.h:41
bool IsValid() const noexcept
True if the underlying RTOS handle was created successfully.
Definition OsEventFlags.h:58
OsEventFlags(const OsEventFlags &)=delete
static OS_Ulong ToTicks(uint32_t timeout_ms) noexcept
Definition OsEventFlags.h:106
const char * name_
Definition OsEventFlags.h:113
bool Wait(uint32_t bits, WaitMode mode, uint32_t timeout_ms, uint32_t &actual_bits) noexcept
Wait for bits to be set per mode, up to timeout_ms.
Definition OsEventFlags.h:90
OsEventFlags & operator=(const OsEventFlags &)=delete
bool created_
Definition OsEventFlags.h:114
OS_EventGroup group_
Definition OsEventFlags.h:115
~OsEventFlags() noexcept
Definition OsEventFlags.h:50
bool Clear(uint32_t bits) noexcept
Clear bits in the group.
Definition OsEventFlags.h:72
@ OS_OR
Definition os_cmsis.h:52
@ OS_WAIT_FOREVER
Definition os_cmsis.h:48
@ OS_AND
Definition os_cmsis.h:51
osEventFlagsId_t OS_EventGroup
Definition os_cmsis.h:31
unsigned int OS_Uint
Definition os_cmsis.h:36
unsigned long OS_Ulong
Definition os_cmsis.h:35