Open Model Railroad Network (OpenMRN)
Loading...
Searching...
No Matches
ConfigUpdateFlow.cxx
1
38#include <fcntl.h>
39
40namespace openlcb
41{
42
43int ConfigUpdateFlow::open_file(const char *path)
44{
45 HASSERT(fd_ < 0);
46 HASSERT(path);
47 fd_ = ::open(path, O_RDWR);
48 HASSERT(fd_ >= 0);
49 return fd_;
50}
51
56
58{
59 for (auto it = listeners_.begin(); it != listeners_.end(); ++it) {
60 it->factory_reset(fd_);
61 }
62 for (auto it = pendingListeners_.begin(); it != pendingListeners_.end();
63 ++it)
64 {
65 it->factory_reset(fd_);
66 }
67}
68
70{
71 AtomicHolder h(this);
73 if (is_state(exit().next_state()))
74 {
75 start_flow(STATE(do_initial_load));
76 }
77}
78
80 ConfigUpdateListener *listener)
81{
82 AtomicHolder h(this);
83 for (auto it = listeners_.begin(); it != listeners_.end();)
84 {
85 if (it.operator->() == listener)
86 {
87 listeners_.erase(it);
88 continue;
89 }
90 ++it;
91 }
92 for (auto it = pendingListeners_.begin(); it != pendingListeners_.end();)
93 {
94 if (it.operator->() == listener)
95 {
97 continue;
98 }
99 ++it;
100 }
101 // We invalidated the iterators due to the erase.
103}
104
105extern const char *const CONFIG_FILENAME __attribute__((weak)) = nullptr;
106extern const size_t CONFIG_FILE_SIZE __attribute__((weak)) = 0;
107
108} // namespace openlcb
#define STATE(_fn)
Turns a function name into an argument to be supplied to functions expecting a state.
Definition StateFlow.hxx:61
See OSMutexLock in os/OS.hxx.
Definition Atomic.hxx:153
Abstract class for components that need to receive configuration from EEPROM.
end_iterator end()
void erase(const iterator &position)
Removes the entry pointed to by the iterator.
Action exit()
Terminate current StateFlow activity.
void start_flow(Callback c)
Resets the flow to the specified state and starts it.
bool is_state(Callback c)
iterator begin()
void push_front(T *entry)
Inserts an entry to the front of the queue.
int open_file(const char *path)
Must be called once (only) before calling anything else.
queue_type pendingListeners_
All listeners that have not yet been added to listeners_ and their initial load needs to be called.
void trigger_update() override
Executes an update in response to the configuration having changed.
queue_type listeners_
All registered update listeners. Protected by Atomic *this.
void init_flow()
Asynchronously invokes all update listeners with the config FD.
void register_update_listener(ConfigUpdateListener *listener) override
Adds a config update listener to be called upon configuration updates.
void unregister_update_listener(ConfigUpdateListener *listener) override
Removes a config update listener.
void factory_reset()
Synchronously invokes all update listeners to factory reset.
queue_type::iterator nextRefresh_
Where are we in the refresh cycle.
#define HASSERT(x)
Checks that the value of expression x is true, else terminates the current process.
Definition macros.h:138
const char *const CONFIG_FILENAME
This symbol must be defined by the application to tell which file to open for the configuration liste...
const size_t CONFIG_FILE_SIZE
This symbol must be defined by the application.