SatCat5
io_override.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_override.h>
7 
12 
13 Override::Override(Writeable* dst, Readable* src, CopyMode mode)
14  : ReadableRedirect(src)
15  , WriteableRedirect(dst)
16  , m_dev_rd(src)
17  , m_ovr_rd(nullptr)
18  , m_dev_wr(dst)
19  , m_ovr_wr(nullptr)
20  , m_mode(mode)
21  , m_remote(false)
22  , m_timeout(30000)
23 {
24  if (m_dev_rd) m_dev_rd->set_callback(this);
25 }
26 
27 #if SATCAT5_ALLOW_DELETION
28 Override::~Override() {
29  if (m_dev_rd) m_dev_rd->set_callback(0);
30  if (m_ovr_rd) m_ovr_rd->set_callback(0);
31 }
32 #endif
33 
34 void Override::set_override(bool remote) {
35  // Set new mode, then reconfigure timer if applicable.
36  m_remote = remote;
37  if (remote) {
38  write_dst(0);
39  } else {
40  write_dst(m_dev_wr);
41  }
42  watchdog_reset();
43 }
44 
46  // Cleanup previous connection if applicable.
47  if (m_ovr_rd) m_ovr_rd->set_callback(0);
48  // Link to the new connection.
49  m_ovr_rd = rx;
50  m_ovr_wr = tx;
51  if (m_ovr_rd) m_ovr_rd->set_callback(this);
52  // If there's already data available, enter override mode.
53  if (m_ovr_rd && m_ovr_rd->get_read_ready()) set_override(true);
54 }
55 
56 void Override::set_timeout(unsigned msec) {
57  // Set new timeout, then reset timer if applicable.
58  m_timeout = msec;
59  watchdog_reset();
60 }
61 
63  if (m_ovr_rd == src) {
64  // New data from the remote controller.
65  set_override(true);
66  src->copy_and_finalize(m_dev_wr, m_mode);
67  } else if (m_remote) {
68  // New data from the I/O device (remote mode).
69  src->copy_and_finalize(m_ovr_wr, m_mode);
70  } else {
71  // New data from the I/O device (local mode).
72  read_notify();
73  }
74 }
75 
77  if (m_dev_rd == src) {m_dev_rd = 0; read_src(&null_read);}
78  if (m_ovr_rd == src) {m_ovr_rd = 0;}
79 }
80 
82  // Timeout elapsed, revert to local mode.
83  set_override(false);
84 }
85 
86 void Override::watchdog_reset() {
87  if (m_remote && m_timeout) {
88  timer_once(m_timeout);
89  } else {
90  timer_stop();
91  }
92 }
Remote-control override of an I/O device.
Definition: io_override.h:59
void set_remote(satcat5::io::Writeable *tx, satcat5::io::Readable *rx)
Attach the remote-control interface.
Definition: io_override.cc:45
void data_rcvd(satcat5::io::Readable *src) override
The data_rcvd() callback is polled whenever data is available.
Definition: io_override.cc:62
void timer_event() override
Child class MUST override this method.
Definition: io_override.cc:81
void set_override(bool remote)
Manually set local or remote mode.
Definition: io_override.cc:34
void data_unlink(satcat5::io::Readable *src) override
Unlink this EventListener from the designated source, because the designated Readable object is being...
Definition: io_override.cc:76
void set_timeout(unsigned msec)
Set timeout for automatic return to local mode.
Definition: io_override.cc:56
Abstract API for reading byte-streams and packets.
Definition: io_readable.h:68
void read_notify()
Attempt notification by calling m_callback->data_rcvd().
Definition: io_readable.cc:254
bool copy_and_finalize(satcat5::io::Writeable *dst, satcat5::io::CopyMode mode=CopyMode::PACKET)
Copy data to a Writeable object, then finalize.
Definition: io_readable.cc:234
virtual void set_callback(satcat5::io::EventListener *callback)
Update registered callback for data_rcvd() events.
Definition: io_readable.cc:39
virtual unsigned get_read_ready() const =0
How many bytes can be read without blocking?
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
void timer_stop()
Stop all future notifications.
Definition: polling.cc:326
void timer_once(unsigned msec)
Configure a one-time notification after X milliseconds.
Definition: polling.cc:316
CopyMode
Specify read and write behavior for Readable::copy_and_finalize().
Definition: io_readable.h:23
satcat5::io::NullRead null_read
Global instances of the basic NullRead and NullSink objects.
Definition: io_readable.cc:29