7 #include <satcat5/ethernet.h>
8 #include <satcat5/ip_core.h>
21 #include <sys/ioctl.h>
52 BufferedTee::BufferedTee(
unsigned nbytes)
53 : HeapAllocator(nbytes)
63 for (
auto ptr = m_list.begin() ; ptr != m_list.end() ; ++ptr) {
65 if ((*ptr)->write_finalize()) ++count;
72 BufferedWriterHeap::BufferedWriterHeap(
Writeable* dst,
unsigned nbytes)
73 : HeapAllocator(nbytes)
79 KeyboardStream::KeyboardStream(
Writeable* dst,
bool line_buffer)
81 , m_line_buffer(line_buffer)
90 term.c_lflag &= ~(ICANON | ECHO);
91 tcsetattr(0, TCSANOW, &term);
95 KeyboardStream::~KeyboardStream() {
100 tcflush(0, TCIFLUSH);
103 term.c_lflag |= ICANON | ECHO;
104 tcsetattr(0, TCSANOW, &term);
117 ioctl(0, FIONREAD, &byteswaiting);
118 if (byteswaiting < 1)
break;
119 write_key(getchar());
124 void KeyboardStream::write_key(
int ch) {
125 if (m_line_buffer && (ch ==
'\r' || ch ==
'\n')) {
127 }
else if (0 < ch && ch < 128) {
133 ToConsole::ToConsole(s8 threshold)
134 : m_threshold(threshold)
136 , m_tref(m_timer.now())
142 return (
m_last_msg.find(msg) != std::string::npos);
147 m_suppress.push_back(std::string(msg));
161 for (
auto filter = m_suppress.begin() ; filter != m_suppress.end() ; ++filter) {
162 if (
m_last_msg.find(*filter) != std::string::npos)
return;
170 fprintf(stderr,
"Log (ERROR) @%04u: %s\n", now, msg);
172 fprintf(stdout,
"Log (WARN) @%04u: %s\n", now, msg);
174 fprintf(stdout,
"Log (INFO) @%04u: %s\n", now, msg);
176 fprintf(stdout,
"Log (DEBUG) @%04u: %s\n", now, msg);
180 MultiBufferHeap::MultiBufferHeap(
unsigned nbytes)
181 : HeapAllocator(nbytes)
182 , MultiBuffer(m_buffptr, nbytes)
187 PacketBufferHeap::PacketBufferHeap(
unsigned nbytes)
188 : HeapAllocator(nbytes)
189 , PacketBuffer(m_buffptr, nbytes, nbytes/64)
194 PacketBufferTee::PacketBufferTee(
unsigned nbytes)
195 : ReadableRedirect(&m_buff)
201 StreamBufferHeap::StreamBufferHeap(
unsigned nbytes)
202 : HeapAllocator(nbytes)
203 , PacketBuffer(m_buffptr, nbytes, 0)
208 StreamBufferTee::StreamBufferTee(
unsigned nbytes)
209 : ReadableRedirect(&m_buff)
215 PosixTimer::PosixTimer()
221 u32 PosixTimer::raw() {
223 int errcode = clock_gettime(CLOCK_MONOTONIC, &tv);
226 const unsigned SCALE = 1000000 / CLOCKS_PER_SEC;
227 return u32(clock() * SCALE);
230 u32 usec1 = (u32)(tv.tv_sec * 1000000);
231 u32 usec2 = (u32)(tv.tv_nsec / 1000);
232 return usec1 + usec2;
236 s64 PosixTimer::gps()
const {
240 int errcode = clock_gettime(CLOCK_REALTIME, &tv);
241 if (errcode)
return 0;
242 s64 msec1 = s64(tv.tv_sec) * 1000;
243 s64 msec2 = s64(tv.tv_nsec) / 1000000;
249 constexpr s64 GPS_EPOCH = (1000LL) * (315964800 - 18);
250 return msec1 + msec2 - GPS_EPOCH;
253 PosixTimekeeper::PosixTimekeeper()
255 , m_adapter(&timekeeper)
260 PosixTimekeeper::~PosixTimekeeper()
265 SwitchCoreHeap::SwitchCoreHeap(
unsigned nbytes)
266 : HeapAllocator(nbytes)
267 , SwitchCore(m_buffptr, nbytes)
282 auto tref = timer.checkpoint_msec(total_msec);
285 if (tref.checkpoint_elapsed())
break;
292 snprintf(tmp,
sizeof(tmp),
293 "%02X:%02X:%02X:%02X:%02X:%02X",
296 return std::string(tmp);
302 write_be_u32(addr_bytes, addr.
value);
304 std::stringstream tmp;
305 tmp << (unsigned)addr_bytes[0] <<
"."
306 << (
unsigned)addr_bytes[1] <<
"."
307 << (unsigned)addr_bytes[2] <<
"."
308 << (
unsigned)addr_bytes[3];
Heap-allocated variant of eth::SwitchCore.
Ephemeral Writeable interface for a simple array.
unsigned written_len() const
Report total length after write_finalize() is called.
bool write_finalize() override
Mark end of frame and release temporary working data.
BufferedTee copies incoming data to any number of destinations.
bool write_finalize() override
Override end-of-packet handling.
BufferedWriter with heap allocation.
BufferedWriter adds an inline buffer to any Writeable interface.
Stream keyboard input to a Writeable interface.
void poll_always() override
Child class MUST override this method.
MultiBuffer with heap allocation.
PacketBuffer with heap allocation (in packet mode).
PacketBuffer with optional wiretap(s).
Abstract API for reading byte-streams and packets.
u8 read_u8()
One of many functions for reading integer/floating point values, see details.
virtual void read_finalize()
Consume any remaining bytes in this frame, if applicable.
virtual unsigned get_read_ready() const =0
How many bytes can be read without blocking?
PacketBuffer with heap allocation (in stream mode).
PacketBuffer with optional wiretap(s).
Abstract API for writing byte-streams and packets.
void write_u8(u8 data)
One of many functions for writing integer/floating point values, see details.
virtual bool write_finalize()
Mark end of frame and release temporary working data.
Helper object that prints log::Log messages to console.
std::string m_last_msg
Most recent message (ignores threshold)
void suppress(const char *msg)
Suppress messages containing a specific string.
void log_event(s8 priority, unsigned nbytes, const char *msg) override
Callback for each formatted Log message.
bool contains(const char *msg)
Does the last logged message contain the provided substring?
s8 m_threshold
Print only if priority >= threshold.
void set_clock(satcat5::util::TimeRef *timer)
Immediately set the system time reference.
u8 *const m_buffptr
Pointer to the underlying buffer.
Link a PosixTimer to the main polling timekeeper.
Timer object using ctime::clock().
constexpr s8 WARNING
Define basic priority codes for log messages.
constexpr s8 ERROR
Define basic priority codes for log messages.
constexpr s8 INFO
Define basic priority codes for log messages.
Core event-processing loop for SatCat5 software.
Timekeeper timekeeper
There is a single global instance of the Timekeeper class.
Miscellaneous POSIX wrappers (e.g., heap allocation, log to console...)
std::string format(const satcat5::eth::MacAddr &addr)
Human-readable formatting for an Ethernet address.
std::string read_str(satcat5::io::Readable *src)
Read contents of a SatCat5 buffer as a string.
void sleep_msec(unsigned msec)
Cross-platform wrapper for sleep()/Sleep()/etc.
void service_msec(unsigned total_msec, unsigned msec_per_iter=10)
Alternate between sleep_msec() and poll::service_all().
An Ethernet MAC address (with serializable interface).
u8 addr[6]
Byte array in network order (Index 0 = MSB)
IPv4 address is a 32-bit unsigned integer.
u32 value
Raw access to the underlying representation.
Timestamp for measuring elapsed time.
unsigned elapsed_msec() const
Elapsed time in milliseconds.
Miscellaneous mathematical utility functions.
void write_be_u32(u8 *dst, u32 val)
Store fields into a big-endian byte array.