SatCat5
io_counter.h
Go to the documentation of this file.
1 // Copyright 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/io_writeable.h>
18 
19 namespace satcat5 {
20  namespace io {
26  class Counter {
27  public:
29  virtual unsigned byte_count(bool reset = true) = 0;
30 
32  virtual unsigned frame_count(bool reset = true) = 0;
33 
35  virtual unsigned error_count(bool reset = true) // GCOVR_EXCL_LINE
36  { return 0; } // GCOVR_EXCL_LINE
37  };
38 
48  public:
49  // Implement the Counter API.
50  unsigned byte_count(bool reset = true) override;
51  unsigned frame_count(bool reset = true) override;
52  unsigned error_count(bool reset = true) override;
53 
55  inline void byte_incr(unsigned incr)
56  { m_byte_ct += incr; }
57  inline void frame_incr(unsigned incr)
58  { m_frm_ct += incr; }
59  inline void error_incr(unsigned incr)
60  { m_err_ct += incr; }
61 
62  protected:
64  constexpr CounterSimple()
65  : m_byte_ct(0), m_err_ct(0), m_frm_ct(0), m_frm_len(0) {}
66  ~CounterSimple() {}
67 
68  // Counter variables are incremented by the child class.
69  unsigned m_byte_ct;
70  unsigned m_err_ct;
71  unsigned m_frm_ct;
72  unsigned m_frm_len;
73  };
74 
79  {
80  public:
82  explicit constexpr CounterInline(satcat5::io::Writeable* dst)
83  : WriteableRedirect(dst) {}
84 
85  // Implement the Writeable API.
86  void write_abort() override;
87  void write_bytes(unsigned nbytes, const void* src) override;
88  bool write_finalize() override;
89  protected:
90  void write_next(u8 data) override;
91  };
92 
94  struct TrafficStats {
95  unsigned sent_bytes;
96  unsigned sent_frames;
97  unsigned rcvd_bytes;
98  unsigned rcvd_frames;
99  unsigned rcvd_errors;
100 
101  TrafficStats() = default;
102  TrafficStats(const TrafficStats& t) = default;
103  TrafficStats& operator=(const TrafficStats& t) = default;
104 
106  bool any() const;
107 
110  static TrafficStats query(
113  };
114  }
115 }
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
constexpr CounterInline(satcat5::io::Writeable *dst)
Link this object to its ultimate destination.
Definition: io_counter.h:82
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
constexpr CounterSimple()
Constructor is only accessible to the child object.
Definition: io_counter.h:64
unsigned m_frm_len
Length of current frame.
Definition: io_counter.h:72
void byte_incr(unsigned incr)
Increment any of the internal counters.
Definition: io_counter.h:55
unsigned byte_count(bool reset=true) override
Bytes transferred since the last query.
Definition: io_counter.cc:14
unsigned m_byte_ct
Cumulative byte count.
Definition: io_counter.h:69
Abstract API for writing byte-streams and packets.
Definition: io_writeable.h:24
Wrapper class for forwarding writes to another object.
Definition: io_writeable.h:218
"Writeable" I/O interface core definitions
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