SatCat5
io_counter.cc
1 // Copyright 2025 The Aerospace Corporation.
3 // This file is a part of SatCat5, licensed under CERN-OHL-W v2 or later.
5 
6 #include <satcat5/io_counter.h>
7 #include <satcat5/utils.h>
8 
13 
14 unsigned CounterSimple::byte_count(bool reset)
15  { return satcat5::util::poll_counter(m_byte_ct, reset); }
16 unsigned CounterSimple::frame_count(bool reset)
17  { return satcat5::util::poll_counter(m_frm_ct, reset); }
18 unsigned CounterSimple::error_count(bool reset)
19  { return satcat5::util::poll_counter(m_err_ct, reset); }
20 
22  if (m_frm_len) ++m_err_ct;
23  m_frm_len = 0;
25 }
26 
27 void CounterInline::write_bytes(unsigned nbytes, const void* src) {
28  m_frm_len += nbytes;
29  WriteableRedirect::write_bytes(nbytes, src);
30 }
31 
33  unsigned len = m_frm_len;
34  m_frm_len = 0;
36  if (ok) {
37  m_byte_ct += len;
38  ++m_frm_ct;
39  } else if (len) {
40  ++m_err_ct;
41  }
42  return ok;
43 }
44 
46  ++m_frm_len;
48 }
49 
50 bool TrafficStats::any() const {
51  return sent_bytes
52  || sent_frames
53  || rcvd_bytes
54  || rcvd_frames
55  || rcvd_errors;
56 }
57 
59  return TrafficStats {
60  tx.byte_count(),
61  tx.frame_count(),
62  rx.byte_count(),
63  rx.frame_count(),
64  rx.error_count(),
65  };
66 }
Define a generic API for reporting traffic statistics.
Definition: io_counter.h:26
virtual unsigned error_count(bool reset=true)
(Optional) Error events since the last query.
Definition: io_counter.h:35
virtual unsigned frame_count(bool reset=true)=0
Frames transferred since the last query.
virtual unsigned byte_count(bool reset=true)=0
Bytes transferred since the last query.
Overlay class for adding counters to any Writeable interface.
Definition: io_counter.h:79
void write_next(u8 data) override
Write the next byte to the underlying buffer or device.
Definition: io_counter.cc:45
bool write_finalize() override
Mark end of frame and release temporary working data.
Definition: io_counter.cc:32
void write_abort() override
If possible, abort the current partially-written packet.
Definition: io_counter.cc:21
void write_bytes(unsigned nbytes, const void *src) override
Write 0 or more bytes from a buffer.
Definition: io_counter.cc:27
Simple implementation of the Counter API.
Definition: io_counter.h:47
unsigned m_err_ct
Cumulative error count.
Definition: io_counter.h:70
unsigned error_count(bool reset=true) override
(Optional) Error events since the last query.
Definition: io_counter.cc:18
unsigned m_frm_ct
Cumulative frame count.
Definition: io_counter.h:71
unsigned frame_count(bool reset=true) override
Frames transferred since the last query.
Definition: io_counter.cc:16
unsigned m_frm_len
Length of current frame.
Definition: io_counter.h:72
unsigned m_byte_ct
Cumulative byte count.
Definition: io_counter.h:69
void write_next(u8 data) override
Write the next byte to the underlying buffer or device.
bool write_finalize() override
Mark end of frame and release temporary working data.
void write_bytes(unsigned nbytes, const void *src) override
Write 0 or more bytes from a buffer.
void write_abort() override
If possible, abort the current partially-written packet.
Inline byte and packet counters for statistics and diagnostics.
Data structure for combined transmit and receive statistics.
Definition: io_counter.h:94
bool any() const
Nonzero activity on any of the counters?
Definition: io_counter.cc:50
unsigned rcvd_bytes
Bytes received from remote device.
Definition: io_counter.h:97
static TrafficStats query(satcat5::io::Counter &rx, satcat5::io::Counter &tx)
Read updated statistics from a transmit/receive pair.
Definition: io_counter.cc:58
unsigned sent_frames
Frames sent to remote device.
Definition: io_counter.h:96
unsigned sent_bytes
Bytes sent to remote device.
Definition: io_counter.h:95
unsigned rcvd_errors
Invalid frames received from device.
Definition: io_counter.h:99
unsigned rcvd_frames
Valid frames received from device.
Definition: io_counter.h:98
Miscellaneous mathematical utility functions.