SatCat5
io_multiplexer.h
Go to the documentation of this file.
1 // Copyright 2024 The Aerospace Corporation.
3 // This file is a part of SatCat5, licensed under CERN-OHL-W v2 or later.
17 
18 #pragma once
19 
20 #include <satcat5/io_readable.h>
21 #include <satcat5/io_writeable.h>
22 
23 namespace satcat5 {
24  namespace io {
26  class MuxPort
29  {
30  public:
32  constexpr MuxPort()
34 
36  void set_callback(satcat5::io::EventListener* callback) override
38 
39  protected:
40  friend class satcat5::io::MuxDown;
41 
43  void attach(Readable* src, Writeable* dst)
44  { read_src(src); write_dst(dst); }
45  };
46 
52  public:
55  inline satcat5::io::Readable* port_rd(unsigned idx)
56  { return (idx < m_size) ? (m_ports + idx) : 0; }
57  inline satcat5::io::Writeable* port_wr(unsigned idx)
58  { return (idx < m_size) ? (m_ports + idx) : 0; }
60 
62  void select(unsigned idx);
63 
64  protected:
67  MuxDown(
68  unsigned size, satcat5::io::MuxPort* ports,
70  ~MuxDown() SATCAT5_OPTIONAL_DTOR;
72 
73  // Implement the EventListener API.
74  void data_rcvd(satcat5::io::Readable* src) override;
75  void data_unlink(satcat5::io::Readable* src) override;
76 
77  // Pointers to the backing array and the shared port interface.
78  const unsigned m_size;
79  unsigned m_index;
80  satcat5::io::MuxPort* const m_ports;
81  satcat5::io::Readable* m_src;
82  satcat5::io::Writeable* const m_dst;
83  };
84 
89  class MuxUp
90  : public satcat5::io::EventListener
91  , public satcat5::io::MuxPort
92  {
93  public:
95  void port_set(unsigned idx, Readable* src, Writeable* dst);
96 
98  void select(unsigned idx);
99 
100  protected:
103  constexpr MuxUp(unsigned size,
105  : m_size(size), m_index(-1), m_src(src), m_dst(dst) {}
106  ~MuxUp() SATCAT5_OPTIONAL_DTOR;
108 
109  // Implement the EventListener API.
110  void data_rcvd(satcat5::io::Readable* src) override;
111  void data_unlink(satcat5::io::Readable* src) override;
112 
113  // Pointers to the backing array of port interfaces.
114  const unsigned m_size;
115  unsigned m_index;
116  satcat5::io::Readable** const m_src;
117  satcat5::io::Writeable** const m_dst;
118  };
119 
122  template <unsigned SIZE>
123  class MuxDownStatic final : public satcat5::io::MuxDown {
124  public:
126  : MuxDown(SIZE, m_port_array, src, dst) {}
127  private:
128  satcat5::io::MuxPort m_port_array[SIZE];
129  };
130 
133  template <unsigned SIZE>
134  class MuxUpStatic final : public satcat5::io::MuxUp {
135  public:
136  MuxUpStatic()
137  : MuxUp(SIZE, m_src_array, m_dst_array), m_src_array{0}, m_dst_array{0} {}
138  private:
139  satcat5::io::Readable* m_src_array[SIZE];
140  satcat5::io::Writeable* m_dst_array[SIZE];
141  };
142  }
143 }
Event-handler interface for newly received data.
Definition: io_readable.h:43
Multiplexer connecting a port to one of several controllers.
void data_rcvd(satcat5::io::Readable *src) override
The data_rcvd() callback is polled whenever data is available.
satcat5::io::Writeable * port_wr(unsigned idx)
Fetch the interface pointer for attaching the Nth controller.
satcat5::io::Readable * port_rd(unsigned idx)
Fetch the interface pointer for attaching the Nth controller.
void data_unlink(satcat5::io::Readable *src) override
Unlink this EventListener from the designated source, because the designated Readable object is being...
void select(unsigned idx)
Select the active controller index, or UINT_MAX for none.
~MuxDown() SATCAT5_OPTIONAL_DTOR
Constructor and destructor should only be called by children.
MuxDown(unsigned size, satcat5::io::MuxPort *ports, satcat5::io::Readable *src, satcat5::io::Writeable *dst)
Constructor and destructor should only be called by children.
Static allocator for io::MuxDown.
Helper object used inside io::MuxDown.
void attach(Readable *src, Writeable *dst)
Update the redirect configuration to attach or detach this port.
constexpr MuxPort()
Create an unattached port object.
void set_callback(satcat5::io::EventListener *callback) override
Override set_callback() to set the internal variable only.
Multiplexer connecting a controller to one of several ports.
constexpr MuxUp(unsigned size, satcat5::io::Readable **src, satcat5::io::Writeable **dst)
Constructor and destructor should only be called by children.
Static allocator for io::MuxUp.
Abstract API for reading byte-streams and packets.
Definition: io_readable.h:68
virtual void set_callback(satcat5::io::EventListener *callback)
Update registered callback for data_rcvd() events.
Definition: io_readable.cc:39
Wrapper class for forwarding reads to another object.
Definition: io_readable.h:299
void read_src(satcat5::io::Readable *src)
Children may reset the source object as needed.
Definition: io_readable.h:319
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
"Readable" I/O interface core definitions
"Writeable" I/O interface core definitions
satcat5::io::NullWrite null_write
Global instance of the basic NullWrite object.