Open Model Railroad Network (OpenMRN)
Loading...
Searching...
No Matches
CDIXMLGenerator.hxx
Go to the documentation of this file.
1
35#ifndef _CDIXMLGENERATOR_HXX_
36#define _CDIXMLGENERATOR_HXX_
37
39#include "utils/FileUtils.hxx"
40
44{
45public:
64 template <class ConfigDef>
66 const ConfigDef &config, const char *filename,
67 openlcb::SimpleStackBase *stack = nullptr)
68 {
69 string cdi_string;
70 ConfigDef cfg(config.offset());
71 cfg.config_renderer().render_cdi(&cdi_string);
72
73 cdi_string += '\0';
74
75 bool need_write = false;
76 LOG(INFO, "[CDI] Checking %s...", filename);
77 FILE *ff = fopen(filename, "rb");
78 if (!ff)
79 {
80 LOG(INFO, "[CDI] File %s does not exist", filename);
81 need_write = true;
82 }
83 else
84 {
85 fclose(ff);
86 string current_str = read_file_to_string(filename);
87 if (current_str != cdi_string)
88 {
89 LOG(INFO, "[CDI] File %s is not up-to-date", filename);
90 need_write = true;
91 }
92 else
93 {
94 LOG(INFO, "[CDI] File %s appears up-to-date (len %u vs %u)",
95 filename, current_str.size(), cdi_string.size());
96 }
97 }
98 if (need_write)
99 {
100 LOG(INFO, "[CDI] Updating %s (len %u)", filename,
101 cdi_string.size());
102 write_string_to_file(filename, cdi_string);
103 }
104
105 if (stack)
106 {
107 LOG(INFO, "[CDI] Registering CDI with stack...");
108 // Creates list of event IDs for factory reset.
109 auto *v = new vector<uint16_t>();
110 cfg.handle_events([v](unsigned o) { v->push_back(o); });
111 v->push_back(0);
112 stack->set_event_offsets(v);
113 // We leak v because it has to stay alive for the entire lifetime
114 // of the stack.
115
116 // Add the file memory space to the stack.
117 openlcb::MemorySpace *space =
118 new openlcb::ROFileMemorySpace(filename);
119 stack->memory_config_handler()->registry()->insert(
120 stack->node(), openlcb::MemoryConfigDefs::SPACE_CDI, space);
121 }
122 return need_write;
123 }
124};
125
126#endif // _CDIXMLGENERATOR_HXX_
string read_file_to_string(const string &filename)
Opens a file, reads the entire contents, stores it in a c++ std::string and returns this string.
Definition FileUtils.cxx:78
void write_string_to_file(const string &filename, const string &data)
Opens (or creates) a file, truncates it and overwrites the contents with what is given in a string.
Standalone utility class for generating the XML representation of the node configuration structure.
static bool create_config_descriptor_xml(const ConfigDef &config, const char *filename, openlcb::SimpleStackBase *stack=nullptr)
Creates the XML representation of the configuration structure and saves it to a file on the filesyste...
Abstract base class for the address spaces exported via the Memory Config Protocol.
Memory space implementation that exports the contents of a file as a memory space.
#define LOG(level, message...)
Conditionally write a message to the logging output.
Definition logging.h:99
static const int INFO
Loglevel that is printed by default, reporting some status information.
Definition logging.h:57