zpp
Zephyr C++20 Framework
poll_event_set.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_SET_HPP
8 #define ZPP_INCLUDE_ZPP_POLL_EVENT_SET_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 #include <utility>
20 
21 #include <zpp/clock.hpp>
22 
23 #include <zpp/sem.hpp>
24 #include <zpp/fifo.hpp>
25 
26 #include <zpp/poll_event.hpp>
27 #include <zpp/poll_signal.hpp>
28 
29 namespace zpp {
30 
36 template<int T_Size>
38 {
39 public:
40  auto begin() noexcept { return m_events.begin(); }
41  auto end() noexcept { return m_events.end(); }
42 public:
46  poll_event_set() noexcept
47  {
48  }
49 
55  template <class... T_Args>
56  poll_event_set(T_Args&&... t) noexcept
57  {
58  assign(0, std::forward<T_Args>(t)...);
59  }
60 
68  auto operator[](size_t idx) noexcept
69  {
70  __ASSERT_NO_MSG(idx < T_Size);
71  return poll_event(&m_events[idx]);
72  }
73 
79  auto poll() noexcept
80  {
81  return poll(K_FOREVER);
82  }
83 
89  auto try_poll() noexcept
90  {
91  return poll(K_NO_WAIT);
92  }
93 
101  template<class T_Rep, class T_Period>
102  auto try_poll_for(const std::chrono::duration<T_Rep, T_Period>&
103  timeout) noexcept
104  {
105  using namespace std::chrono;
106 
107  return poll(to_timeout(timeout));
108  }
109 private:
117  void assign(int index) noexcept
118  {
119  __ASSERT_NO_MSG(index == T_Size);
120  }
121 
129  template<class T_FirstArg, class... T_Args>
130  void assign(int index, T_FirstArg&& f, T_Args&&... t) noexcept
131  {
132  poll_event(&m_events[index]).assign(std::forward<T_FirstArg>(f));
133  assign(index+1, std::forward<T_Args>(t)...);
134  }
135 
143  auto poll(k_timeout_t timeout) noexcept
144  {
145  for (auto& e: m_events) {
146  e.state = K_POLL_STATE_NOT_READY;
147  if (e.tag == (int)poll_event::type_tag::type_signal) {
148  __ASSERT_NO_MSG(e.signal != nullptr);
149  e.signal->signaled = 0;
150  }
151  }
152 
153  auto rc = k_poll(m_events.data(), m_events.size(), timeout);
154 
155  if (rc == 0) {
156  return true;
157  } else {
158  return false;
159  }
160  }
161 private:
162  std::array<struct k_poll_event, T_Size> m_events;
163 };
164 
172 template <class... T_Args>
173 poll_event_set(T_Args&&... t) noexcept -> poll_event_set<sizeof...(T_Args)>;
174 
175 } // namespace zpp
176 
177 #endif // CONFIG_POLL
178 
179 #endif // ZPP_INCLUDE_ZPP_POLL_EVENT_SET_HPP
A set of poll events.
poll_event_set() noexcept
default constructor
poll_event_set(T_Args &&... t) noexcept
constructor that takes arguments for initialization
auto poll() noexcept
poll events waiting for ever
auto end() noexcept
auto try_poll() noexcept
try poll events without waiting
auto try_poll_for(const std::chrono::duration< T_Rep, T_Period > &timeout) noexcept
try poll events waiting for e certain time
auto begin() noexcept
auto operator[](size_t idx) noexcept
access an element of the set
wrapper class around a k_poll_event
Definition: poll_event.hpp:29
constexpr k_timeout_t to_timeout(const std::chrono::duration< T_Rep, T_Period > &d) noexcept
convert a duration to tick
Definition: clock.hpp:88