SatCat5
polling.h
Go to the documentation of this file.
1 // Copyright 2021-2025 The Aerospace Corporation.
3 // This file is a part of SatCat5, licensed under CERN-OHL-W v2 or later.
47 
48 
49 #pragma once
50 
51 #include <satcat5/timeref.h>
52 #include <satcat5/types.h>
53 
54 namespace satcat5 {
55  namespace poll {
56  // Prototype for internal helper objects.
57  class OnDemandHelper;
58 
65  void service();
66 
80  void service_all(unsigned limit = 100);
81 
88  bool pre_test_reset();
89 
96  class Always {
97  public:
100  virtual void poll_always() = 0;
101 
103  static unsigned count_always();
104 
105  protected:
108  explicit Always(bool auto_register=true);
109  ~Always() SATCAT5_OPTIONAL_DTOR;
110 
112  void poll_register();
113 
115  void poll_unregister();
116 
117  private:
118  friend satcat5::poll::OnDemandHelper;
119  friend satcat5::util::ListCore;
120  friend void satcat5::poll::service();
121  satcat5::poll::Always* m_next; // Linked list to next object
122  };
123 
131  class OnDemand {
132  public:
135  void request_poll();
136 
138  void request_cancel();
139 
141  static unsigned count_ondemand();
142 
143  protected:
147  virtual void poll_demand() = 0;
148 
151  constexpr OnDemand() : m_next(0), m_idle(1) {}
152  ~OnDemand() SATCAT5_OPTIONAL_DTOR;
153 
154  private:
155  friend satcat5::util::ListCore;
156  friend satcat5::poll::OnDemandHelper;
157  friend satcat5::ptp::Interface;
158  satcat5::poll::OnDemand* m_next; // Linked list to next object
159  bool m_idle; // Item currently idle?
160  };
161 
168  class Timekeeper : public satcat5::poll::OnDemand {
169  public:
170  Timekeeper();
171 
175  satcat5::util::TimeRef* get_clock() const;
176 
178  bool clock_ready() const;
179 
182  void set_clock(satcat5::util::TimeRef* timer);
183 
187  void suggest_clock(satcat5::util::TimeRef* timer);
188 
191  bool pre_test_reset();
192 
193  protected:
194  void poll_demand() override;
195  satcat5::util::TimeVal m_tref;
196  };
197 
204  extern Timekeeper timekeeper;
205 
213  class Timer {
214  public:
216  static unsigned count_timer();
217 
219  void timer_once(unsigned msec);
221  void timer_every(unsigned msec);
223  void timer_stop();
224 
226  inline unsigned timer_interval() const {return m_tnext;}
227 
229  inline unsigned timer_remaining() const {return m_trem;}
230 
231  protected:
234  Timer();
235  ~Timer() SATCAT5_OPTIONAL_DTOR;
236 
238  virtual void timer_event() = 0;
239 
240  private:
241  // Event handler called by Timekeeper class.
242  void query(unsigned elapsed_msec);
243 
244  friend satcat5::util::ListCore;
245  friend satcat5::poll::Timekeeper;
246  satcat5::poll::Timer* m_next; // Linked list to next object
247  unsigned m_trem; // Milliseconds to next event
248  unsigned m_tnext; // Recurring timer interval
249  };
250 
256  class TimerAdapter : public satcat5::poll::Timer {
257  public:
258  explicit TimerAdapter(satcat5::poll::OnDemand* target);
259  protected:
260  void timer_event() override;
261  satcat5::poll::OnDemand* const m_target;
262  };
263  }
264 
265  namespace irq {
275  public:
278  VirtualTimer(
279  satcat5::poll::OnDemand* obj, unsigned usec = 1000);
280 
281  protected:
282  void poll_always() override;
283 
284  satcat5::poll::OnDemand* const m_target;
285  unsigned const m_interval;
286  satcat5::util::TimeVal m_tref;
287  };
288  }
289 }
Poll any OnDemand object using a TimeRef.
Definition: polling.h:274
An "Always" object is polled whenever service() is called.
Definition: polling.h:96
virtual void poll_always()=0
Child class MUST override this method.
static unsigned count_always()
Count active objects of this type.
Definition: polling.cc:178
void poll_register()
Register this pollable object, called by the constructor.
Definition: polling.cc:183
void poll_unregister()
Unregister this pollable object, called by the destructor.
Definition: polling.cc:189
Always(bool auto_register=true)
Registers this pollable object unless auto_register = false.
Definition: polling.cc:168
Global helper object that services all OnDemand objects.
Definition: polling.cc:64
An "OnDemand" object is polled only on request.
Definition: polling.h:131
virtual void poll_demand()=0
Deferred event handler, called after request().
constexpr OnDemand()
Register this pollable object.
Definition: polling.h:151
Global coordinator for multiple Timer objects.
Definition: polling.h:168
Connect a Timer to any OnDemand object.
Definition: polling.h:256
Timer objects are polled after a fixed delay or at a regular interval.
Definition: polling.h:213
unsigned timer_interval() const
Accessor for recurring timer interval, if one is set.
Definition: polling.h:226
unsigned timer_remaining() const
Accessor for time to next event, if one is set.
Definition: polling.h:229
The TimeRef API provides access to a monotonic time-counter.
Definition: timeref.h:142
Timekeeper timekeeper
There is a single global instance of the Timekeeper class.
Definition: polling.cc:50
void service()
Single-pass service loop.
Definition: polling.cc:143
void service_all(unsigned limit=100)
Multi-pass service loop Calling this function regularly is required for SatCat5 operation.
Definition: polling.cc:157
bool pre_test_reset()
Reset the global PRNG state used for rand_*(), below.
Definition: sim_utils.cc:32
Timestamp for measuring elapsed time.
Definition: timeref.h:48
TimeRef and TimeVal define the API for monotonic timers.
Basic type aliases and prototypes used throughout SatCat5.