SatCat5
io_broadcast.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_broadcast.h>
7 #include <satcat5/utils.h>
8 
13 
14 
15 // Available write space is the minimum of all Writeables.
16 unsigned WriteableBroadcast::get_write_space() const {
17  unsigned ws = UINT32_MAX; // Large default if no open Writeables.
18  for (unsigned i = 0; i < m_size; ++i) {
19  if (m_dsts[i]) { ws = min_unsigned(ws, m_dsts[i]->get_write_space()); }
20  }
21  return ws;
22 }
23 
24 // Broadcast call.
26  for (unsigned i = 0; i < m_size; ++i) {
27  if (m_dsts[i]) { m_dsts[i]->write_abort(); }
28  }
29 }
30 
31 // Broadcast call.
32 void WriteableBroadcast::write_bytes(unsigned nbytes, const void* src) {
33  if (nbytes <= get_write_space()) {
34  for (unsigned i = 0; i < m_size; ++i) {
35  if (m_dsts[i]) { m_dsts[i]->write_bytes(nbytes, src); }
36  }
37  } else {
39  }
40 }
41 
42 // Return OK only if all write_finalize() calls were successful.
44  unsigned n_ok = 0;
45  for (unsigned i = 0; i < m_size; ++i) {
46  if (!m_dsts[i] || m_dsts[i]->write_finalize()) { ++n_ok; }
47  }
48  return (n_ok == m_size);
49 }
50 
51 // Broadcast call.
53  for (unsigned i = 0; i < m_size; ++i) {
54  if (m_dsts[i]) { m_dsts[i]->write_next(data); }
55  }
56 }
57 
58 // Broadcast call.
60  for (unsigned i = 0; i < m_size; ++i) {
61  if (m_dsts[i]) { m_dsts[i]->write_overflow(); }
62  }
63 }
64 
65 // WriteableBroadcastAny changes to maximum available write space.
67  unsigned ws = 0; // Default if no open Writeables.
68  for (unsigned i = 0; i < m_size; ++i) {
69  if (m_dsts[i]) { ws = max_unsigned(ws, m_dsts[i]->get_write_space()); }
70  }
71  return ws;
72 }
73 
74 // WriteableBroadcastAny changes to return OK if any write_finalize succeeds.
76  bool any_ok = false;
77  for (unsigned i = 0; i < m_size; ++i) {
78  if (!m_dsts[i] || m_dsts[i]->write_finalize()) { any_ok = true; }
79  }
80  return any_ok;
81 }
Version of io::WriteableBroadcast that considers a write successful if ANY of the downstream Writeabl...
Definition: io_broadcast.h:82
bool write_finalize() override
Finalize the in-progress write for all downstream Writeables.
Definition: io_broadcast.cc:75
unsigned get_write_space() const override
Get the max available write space of any downstream Writeable.
Definition: io_broadcast.cc:66
Forwards incoming writes to multiple downstream Writeables.
Definition: io_broadcast.h:35
void write_bytes(unsigned nbytes, const void *src) override
Write a byte sequence to all downstream Writeables.
Definition: io_broadcast.cc:32
satcat5::io::Writeable **const m_dsts
Array of Writeables.
Definition: io_broadcast.h:74
const unsigned m_size
Size of m_dsts.
Definition: io_broadcast.h:73
void write_abort() override
Abort writes for all downstream Writeables.
Definition: io_broadcast.cc:25
bool write_finalize() override
Finalize the in-progress write for all downstream Writeables.
Definition: io_broadcast.cc:43
void write_overflow() override
Optional error handling for write overflow.
Definition: io_broadcast.cc:59
unsigned get_write_space() const override
Get the min available write space of downstream Writeables.
Definition: io_broadcast.cc:16
void write_next(u8 data) override
Write the next byte to the underlying buffer or device.
Definition: io_broadcast.cc:52
virtual void write_bytes(unsigned nbytes, const void *src)
Write 0 or more bytes from a buffer.
virtual void write_abort()
If possible, abort the current partially-written packet.
virtual void write_overflow()
Optional error handling for write overflow.
virtual void write_next(u8 data)=0
Write the next byte to the underlying buffer or device.
Writeable API that broadcasts copies to multiple destinations.
Miscellaneous mathematical utility functions.
constexpr unsigned min_unsigned(unsigned a, unsigned b)
Min and max functions.
Definition: utils.h:111
constexpr unsigned max_unsigned(unsigned a, unsigned b)
Min and max functions.
Definition: utils.h:126