SatCat5
posix_utils.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.
14 
15 #pragma once
16 
17 #include <satcat5/eth_switch.h>
18 #include <satcat5/io_buffer.h>
19 #include <satcat5/io_core.h>
20 #include <satcat5/log.h>
21 #include <satcat5/multi_buffer.h>
22 #include <satcat5/pkt_buffer.h>
23 #include <satcat5/timeref.h>
24 #include <list>
25 #include <string>
26 #include <vector>
27 
28 #ifdef _WIN32
29  #define SATCAT5_WIN32 1
30 #else
31  #define SATCAT5_WIN32 0
32 #endif
33 
34 namespace satcat5 {
35  namespace util {
37  class HeapAllocator {
38  protected:
40  explicit HeapAllocator(unsigned nbytes)
41  : m_buffptr(new u8[nbytes]) {}
42  ~HeapAllocator()
43  { delete[] m_buffptr; }
44 
47  HeapAllocator(const HeapAllocator&) = delete;
48  void operator=(const HeapAllocator&) = delete;
50 
52  u8* const m_buffptr;
53  };
54  }
55 
56  namespace eth {
58  class SwitchCoreHeap final
60  , public satcat5::eth::SwitchCore {
61  public:
62  explicit SwitchCoreHeap(unsigned nbytes = 65536);
63  };
64  }
65 
66  namespace io {
68  std::string read_str(satcat5::io::Readable* src);
69 
73  , public satcat5::io::ArrayWrite {
74  public:
76  explicit BufferedTee(unsigned nbytes = 4096);
77 
79  inline void add(satcat5::io::Writeable* dst)
80  { m_list.push_back(dst); }
81  inline void remove(satcat5::io::Writeable* dst)
82  { m_list.remove(dst); }
83 
85  bool write_finalize() override;
86 
87  protected:
88  std::list<satcat5::io::Writeable*> m_list;
89  };
90 
92  class BufferedWriterHeap final
95  public:
96  explicit BufferedWriterHeap(
98  unsigned nbytes = 4096);
99  };
100 
103  public:
104  explicit KeyboardStream(
106  bool line_buffer = true);
107  virtual ~KeyboardStream();
108 
109  protected:
110  void poll_always() override;
111  void write_key(int ch);
112  satcat5::io::Writeable* const m_dst;
113  const bool m_line_buffer;
114  };
115 
119  , public satcat5::io::MultiBuffer {
120  public:
121  explicit MultiBufferHeap(unsigned nbytes=65536);
122  virtual ~MultiBufferHeap() {}
123  };
124 
126  class PacketBufferHeap final
128  , public satcat5::io::PacketBuffer {
129  public:
130  explicit PacketBufferHeap(unsigned nbytes=4096);
131  };
132 
136  class PacketBufferTee final
138  , public satcat5::io::BufferedTee {
139  public:
140  explicit PacketBufferTee(unsigned nbytes=4096);
141  inline void clear() {m_buff.clear();}
142  protected:
144  };
145 
148  class StreamBufferHeap final
150  , public satcat5::io::PacketBuffer {
151  public:
152  explicit StreamBufferHeap(unsigned nbytes=4096);
153  };
154 
158  class StreamBufferTee final
160  , public satcat5::io::BufferedTee {
161  public:
162  explicit StreamBufferTee(unsigned nbytes=4096);
163  inline void clear() {m_buff.clear();}
164  protected:
166  };
167  }
168 
169  namespace util {
173  public:
174  PosixTimer();
175  u32 raw() override; // Monotonic millisecond counter
176  s64 gps() const; // Milliseconds since GPS epoch
177  };
178 
182  public:
183  PosixTimekeeper();
184  virtual ~PosixTimekeeper();
185 
186  inline s64 gps() {return m_timer.gps();}
187  inline TimeVal now() {return m_timer.now();}
188  inline u32 raw() {return m_timer.raw();}
189 
190  satcat5::util::TimeRef* timer() {return &m_timer;}
191 
192  protected:
194  satcat5::irq::VirtualTimer m_adapter;
195  };
196 
198  void sleep_msec(unsigned msec);
199 
201  void service_msec(unsigned total_msec, unsigned msec_per_iter = 10);
202  }
203 
204  namespace log {
206  std::string format(const satcat5::eth::MacAddr& addr);
208  std::string format(const satcat5::ip::Addr& addr);
209 
213  class ToConsole final : public satcat5::log::EventHandler {
214  public:
216  explicit ToConsole(s8 threshold=satcat5::log::DEBUG);
217 
219  void disable() {m_threshold = INT8_MAX;}
220 
223  void suppress(const char* msg);
224 
226  bool contains(const char* msg);
227 
229  void clear() {m_last_msg.clear();}
231  bool empty() {return m_last_msg.empty();}
232 
233  // Publically accessible members:
235  std::string m_last_msg;
236 
237  protected:
238  void log_event(s8 priority, unsigned nbytes, const char* msg) override;
239  std::vector<std::string> m_suppress;
241  satcat5::util::TimeVal m_tref;
242  };
243  }
244 }
Heap-allocated variant of eth::SwitchCore.
Definition: posix_utils.h:60
A shared-memory Ethernet switch based on the MultiBuffer class.
Definition: eth_switch.h:93
Ephemeral Writeable interface for a simple array.
Definition: io_writeable.h:127
BufferedTee copies incoming data to any number of destinations.
Definition: posix_utils.h:73
BufferedTee(unsigned nbytes=4096)
Create this object and set its buffer size.
Definition: posix_utils.cc:52
void add(satcat5::io::Writeable *dst)
Update the list of destination objects.
Definition: posix_utils.h:79
bool write_finalize() override
Override end-of-packet handling.
Definition: posix_utils.cc:59
BufferedWriter with heap allocation.
Definition: posix_utils.h:94
BufferedWriter adds an inline buffer to any Writeable interface.
Definition: io_buffer.h:187
Stream keyboard input to a Writeable interface.
Definition: posix_utils.h:102
void poll_always() override
Child class MUST override this method.
Definition: posix_utils.cc:108
MultiBuffer with heap allocation.
Definition: posix_utils.h:119
A multi-source, multi-sink packet buffer.
Definition: multi_buffer.h:198
PacketBuffer with heap allocation (in packet mode).
Definition: posix_utils.h:128
The PacketBuffer class is a wrapper for a circular buffer, with optional logic to support retention o...
Definition: pkt_buffer.h:66
void clear()
Reset buffer contents.
Definition: pkt_buffer.cc:22
PacketBuffer with optional wiretap(s).
Definition: posix_utils.h:138
Abstract API for reading byte-streams and packets.
Definition: io_readable.h:68
Wrapper class for forwarding reads to another object.
Definition: io_readable.h:299
PacketBuffer with heap allocation (in stream mode).
Definition: posix_utils.h:150
PacketBuffer with optional wiretap(s).
Definition: posix_utils.h:160
Abstract API for writing byte-streams and packets.
Definition: io_writeable.h:24
Poll any OnDemand object using a TimeRef.
Definition: polling.h:274
Defines the interface for accepting Log messages.
Definition: log.h:72
Helper object that prints log::Log messages to console.
Definition: posix_utils.h:213
ToConsole(s8 threshold=satcat5::log::DEBUG)
On creation, optionally specify the minimum priority to print.
Definition: posix_utils.cc:133
std::string m_last_msg
Most recent message (ignores threshold)
Definition: posix_utils.h:235
void suppress(const char *msg)
Suppress messages containing a specific string.
Definition: posix_utils.cc:145
void log_event(s8 priority, unsigned nbytes, const char *msg) override
Callback for each formatted Log message.
Definition: posix_utils.cc:153
void disable()
Disable all output messages until threshold is lowered.
Definition: posix_utils.h:219
bool contains(const char *msg)
Does the last logged message contain the provided substring?
Definition: posix_utils.cc:141
void clear()
Clear the stored copy of the most recent log message.
Definition: posix_utils.h:229
bool empty()
Is there a stored log message?
Definition: posix_utils.h:231
s8 m_threshold
Print only if priority >= threshold.
Definition: posix_utils.h:234
An "Always" object is polled whenever service() is called.
Definition: polling.h:96
Helper object for heap allocation.
Definition: posix_utils.h:37
void operator=(const HeapAllocator &)=delete
Disable auto-generated assignment and copy methods.
u8 *const m_buffptr
Pointer to the underlying buffer.
Definition: posix_utils.h:52
HeapAllocator(unsigned nbytes)
Constructor/destructor through child class only.
Definition: posix_utils.h:40
HeapAllocator(const HeapAllocator &)=delete
Disable auto-generated assignment and copy methods.
Link a PosixTimer to the main polling timekeeper.
Definition: posix_utils.h:181
Timer object using ctime::clock().
Definition: posix_utils.h:172
u32 raw() override
Read current time in arbitrary "ticks".
Definition: posix_utils.cc:221
The TimeRef API provides access to a monotonic time-counter.
Definition: timeref.h:142
TimeVal now()
Create a TimeVal object using the tick-count from raw().
Definition: timeref.cc:65
Software-defined Ethernet switch.
Buffered I/O wrappers for PacketBuffer.
I/O interface core definitions.
Diagnostic logging to UART and/or Ethernet ports.
constexpr s8 DEBUG
Define basic priority codes for log messages.
Definition: log.h:109
Multi-source, multi-sink packet buffer.
void sleep_msec(unsigned msec)
Cross-platform wrapper for sleep()/Sleep()/etc.
Definition: posix_utils.cc:272
void service_msec(unsigned total_msec, unsigned msec_per_iter=10)
Alternate between sleep_msec() and poll::service_all().
Definition: posix_utils.cc:280
An Ethernet MAC address (with serializable interface).
Definition: eth_header.h:29
IPv4 address is a 32-bit unsigned integer.
Definition: ip_core.h:15
Timestamp for measuring elapsed time.
Definition: timeref.h:48
TimeRef and TimeVal define the API for monotonic timers.