zpp
Zephyr C++20 Framework
poll_event.hpp
Go to the documentation of this file.
1 //
2 // Copyright (c) 2019 Erwin Rol <erwin@erwinrol.com>
3 //
4 // SPDX-License-Identifier: Apache-2.0
5 //
6 
7 #ifndef ZPP_INCLUDE_ZPP_POLL_EVENT_HPP
8 #define ZPP_INCLUDE_ZPP_POLL_EVENT_HPP
9 
10 #ifdef CONFIG_POLL
11 
12 #include <zephyr/kernel.h>
13 #include <zephyr/sys/__assert.h>
14 
15 #include <chrono>
16 #include <limits>
17 #include <optional>
18 #include <array>
19 
20 #include <zpp/sem.hpp>
21 #include <zpp/fifo.hpp>
22 #include <zpp/poll_signal.hpp>
23 
24 namespace zpp {
25 
29 class poll_event {
30 public:
34  enum class type_tag : uint8_t {
36  type_sem,
37  type_fifo,
40  };
41 
47  poll_event(k_poll_event* event) noexcept
48  : m_event(event)
49  {
50  __ASSERT_NO_MSG(m_event != nullptr);
51  }
52 
56  void assign(std::nullptr_t) noexcept
57  {
58  __ASSERT_NO_MSG(m_event != nullptr);
59 
60  m_event->obj = nullptr;
61  m_event->state = K_POLL_STATE_NOT_READY;
62  m_event->mode = K_POLL_MODE_NOTIFY_ONLY;
63  m_event->type = K_POLL_TYPE_IGNORE;
64  m_event->tag = (int)type_tag::type_ignore;
65  }
66 
72  void assign(sem& s) noexcept
73  {
74  __ASSERT_NO_MSG(m_event != nullptr);
75  k_poll_event_init(m_event,
76  K_POLL_TYPE_SEM_AVAILABLE,
77  K_POLL_MODE_NOTIFY_ONLY,
78  s.native_handle());
79  m_event->tag = (int)type_tag::type_sem;
80  }
81 
87  void assign(sem_ref& s) noexcept
88  {
89  __ASSERT_NO_MSG(m_event != nullptr);
90  k_poll_event_init(m_event,
91  K_POLL_TYPE_SEM_AVAILABLE,
92  K_POLL_MODE_NOTIFY_ONLY,
93  s.native_handle());
94  m_event->tag = (int)type_tag::type_sem;
95  }
96 
102  template<typename T_FifoItem>
103  void assign(fifo<T_FifoItem>& f) noexcept
104  {
105  __ASSERT_NO_MSG(m_event != nullptr);
106  k_poll_event_init(m_event,
107  K_POLL_TYPE_FIFO_DATA_AVAILABLE,
108  K_POLL_MODE_NOTIFY_ONLY,
109  f.native_handle());
110  m_event->tag = (int)type_tag::type_fifo;
111  }
112 
118  template<typename T_FifoItem>
119  void assign(fifo_ref<T_FifoItem>& f) noexcept
120  {
121  __ASSERT_NO_MSG(m_event != nullptr);
122  k_poll_event_init(m_event,
123  K_POLL_TYPE_FIFO_DATA_AVAILABLE,
124  K_POLL_MODE_NOTIFY_ONLY,
125  f.native_handle());
126  m_event->tag = (int)type_tag::type_fifo;
127  }
128 
134  void assign(poll_signal& s) noexcept
135  {
136  __ASSERT_NO_MSG(m_event != nullptr);
137  k_poll_event_init(m_event,
138  K_POLL_TYPE_SIGNAL,
139  K_POLL_MODE_NOTIFY_ONLY,
140  s.native_handle());
141  m_event->tag = (int)type_tag::type_signal;
142  }
143 
149  void assign(poll_signal_ref& s) noexcept
150  {
151  __ASSERT_NO_MSG(m_event != nullptr);
152  k_poll_event_init(m_event,
153  K_POLL_TYPE_SIGNAL,
154  K_POLL_MODE_NOTIFY_ONLY,
155  s.native_handle());
156  m_event->tag = (int)type_tag::type_signal;
157  }
158 
159 
165  bool is_ready() noexcept
166  {
167  __ASSERT_NO_MSG(m_event != nullptr);
168  __ASSERT_NO_MSG(m_event->tag != (int)type_tag::type_unknown);
169 
170  switch((type_tag)m_event->tag) {
172  return false;
173  case type_tag::type_sem:
174  return (m_event->state & K_POLL_STATE_SEM_AVAILABLE);
175  case type_tag::type_fifo:
176  return (m_event->state & K_POLL_STATE_FIFO_DATA_AVAILABLE);
178  return (m_event->state & K_POLL_STATE_SIGNALED);
180  return false;
181  }
182 
183  return false;
184  }
185 
189  void reset() noexcept
190  {
191  __ASSERT_NO_MSG(m_event != nullptr);
192 
193  m_event->state = K_POLL_STATE_NOT_READY;
194  }
195 
201  bool is_cancelled() noexcept
202  {
203  __ASSERT_NO_MSG(m_event != nullptr);
204  __ASSERT_NO_MSG(m_event->tag != (int)type_tag::type_unknown);
205 
206  if (m_event->state & K_POLL_STATE_CANCELLED) {
207  return true;
208  } else {
209  return false;
210  }
211  }
212 
221  template<typename T_FifoItemType>
222  auto fifo() noexcept
223  {
224  __ASSERT_NO_MSG(m_event != nullptr);
225  __ASSERT_NO_MSG(m_event->tag == (int)type_tag::type_fifo);
226  __ASSERT_NO_MSG(m_event->fifo != nullptr);
227 
228  return fifo_ref<T_FifoItemType>(m_event->fifo);
229  }
230 
238  auto sem() noexcept
239  {
240  __ASSERT_NO_MSG(m_event != nullptr);
241  __ASSERT_NO_MSG(m_event->tag == (int)type_tag::type_sem);
242  __ASSERT_NO_MSG(m_event->sem != nullptr);
243 
244  return sem_ref(m_event->sem);
245  }
246 
254  auto signal() noexcept
255  {
256  __ASSERT_NO_MSG(m_event != nullptr);
257  __ASSERT_NO_MSG(m_event->tag == (int)type_tag::type_signal);
258  __ASSERT_NO_MSG(m_event->signal != nullptr);
259 
260  return poll_signal_ref(m_event->signal);
261  }
262 private:
263  k_poll_event* m_event{ nullptr };
264 public:
265  poll_event(const poll_event&) = delete;
266  poll_event(poll_event&&) = delete;
267  poll_event& operator=(const poll_event&) = delete;
269 };
270 
271 } // namespace zpp
272 
273 #endif // CONFIG_POLL
274 
275 #endif // ZPP_INCLUDE_ZPP_POLL_EVENT_HPP
fifo that references a k_fifo object
Definition: fifo.hpp:221
fifo that manages a k_fifo object
Definition: fifo.hpp:175
wrapper class around a k_poll_event
Definition: poll_event.hpp:29
void assign(fifo< T_FifoItem > &f) noexcept
assign a fifo to this event
Definition: poll_event.hpp:103
void assign(fifo_ref< T_FifoItem > &f) noexcept
assign a fifo to this event
Definition: poll_event.hpp:119
void assign(poll_signal &s) noexcept
assign a signal to this event
Definition: poll_event.hpp:134
poll_event & operator=(poll_event &&)=delete
auto fifo() noexcept
get access to the fifo of the event
Definition: poll_event.hpp:222
void assign(std::nullptr_t) noexcept
assign a null event (that will be ignored when polling)
Definition: poll_event.hpp:56
auto signal() noexcept
get access to the signal of the event
Definition: poll_event.hpp:254
void assign(sem &s) noexcept
assign a semaphore to this event
Definition: poll_event.hpp:72
type_tag
type of event sources
Definition: poll_event.hpp:34
bool is_ready() noexcept
check if this event is ready
Definition: poll_event.hpp:165
auto sem() noexcept
get access to the sem of the event
Definition: poll_event.hpp:238
void assign(poll_signal_ref &s) noexcept
assign a signal to this event
Definition: poll_event.hpp:149
poll_event(poll_event &&)=delete
poll_event(k_poll_event *event) noexcept
wrap a k_poll_event pointer
Definition: poll_event.hpp:47
bool is_cancelled() noexcept
check if this event is cancelled
Definition: poll_event.hpp:201
poll_event & operator=(const poll_event &)=delete
void reset() noexcept
reset event to non ready state
Definition: poll_event.hpp:189
poll_event(const poll_event &)=delete
void assign(sem_ref &s) noexcept
assign a semaphore to this event
Definition: poll_event.hpp:87
class wrapping a k_poll_signal
class owning a k_poll_signal
A counting semaphore class borrowing the native sem.
Definition: sem.hpp:264
A counting semaphore class.
Definition: sem.hpp:196