Skip to content

Commit

Permalink
Merge branch 'dcc-railcom-send' into bracz-st-decoder-update
Browse files Browse the repository at this point in the history
* dcc-railcom-send:
  Adds driver and hooks for sending railcom data out. (#551)
  Adds missing header guard. (#557)
  Reduce the number of times the config file is opened in SimpleStack (#555)
  • Loading branch information
balazsracz committed Aug 2, 2021
2 parents ee2f0d2 + 0c781dc commit 7074024
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 19 deletions.
14 changes: 4 additions & 10 deletions src/openlcb/ConfigUpdateFlow.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -42,16 +42,10 @@ namespace openlcb

int ConfigUpdateFlow::open_file(const char *path)
{
if (fd_ >= 0) return fd_;
if (!path)
{
fd_ = -1;
}
else
{
fd_ = ::open(path, O_RDWR);
HASSERT(fd_ >= 0);
}
HASSERT(fd_ < 0);
HASSERT(path);
fd_ = ::open(path, O_RDWR);
HASSERT(fd_ >= 0);
return fd_;
}

Expand Down
13 changes: 11 additions & 2 deletions src/openlcb/ConfigUpdateFlow.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -71,21 +71,30 @@ public:
{
}

/// Must be called once before calling anything else. Returns the file
/// descriptor.
/// Must be called once (only) before calling anything else. Returns the
/// file descriptor.
int open_file(const char *path);
/// Asynchronously invokes all update listeners with the config FD.
void init_flow();
/// Synchronously invokes all update listeners to factory reset.
void factory_reset();

/// @return the file descriptor of the configuration file, or -1 if the
/// configuration file has not yet been opened.
int get_fd()
{
return fd_;
}

#ifdef GTEST
void TEST_set_fd(int fd)
{
fd_ = fd;
}
bool TEST_is_terminated() {
return is_terminated();
}
#endif // GTEST

void trigger_update() override
{
Expand Down
26 changes: 19 additions & 7 deletions src/openlcb/SimpleStack.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,13 @@ void SimpleStackBase::start_stack(bool delay_start)
{
#if OPENMRN_HAVE_POSIX_FD
// Opens the eeprom file and sends configuration update commands to all
// listeners.
configUpdateFlow_.open_file(CONFIG_FILENAME);
// listeners. We must only call ConfigUpdateFlow::open_file() once and it
// may have been done by an earlier call to create_config_file_if_needed()
// or check_version_and_factory_reset().
if (configUpdateFlow_.get_fd() < 0)
{
configUpdateFlow_.open_file(CONFIG_FILENAME);
}
configUpdateFlow_.init_flow();
#endif // have posix fd

Expand Down Expand Up @@ -139,12 +144,12 @@ void SimpleStackBase::default_start_node()
#if OPENMRN_HAVE_POSIX_FD
{
auto *space = new FileMemorySpace(
SNIP_DYNAMIC_FILENAME, sizeof(SimpleNodeDynamicValues));
configUpdateFlow_.get_fd(), sizeof(SimpleNodeDynamicValues));
memoryConfigHandler_.registry()->insert(
node(), MemoryConfigDefs::SPACE_ACDI_USR, space);
additionalComponents_.emplace_back(space);
}
#endif // NOT ARDUINO, YES ESP32
#endif // OPENMRN_HAVE_POSIX_FD
size_t cdi_size = strlen(CDI_DATA);
if (cdi_size > 0)
{
Expand All @@ -157,12 +162,13 @@ void SimpleStackBase::default_start_node()
#if OPENMRN_HAVE_POSIX_FD
if (CONFIG_FILENAME != nullptr)
{
auto *space = new FileMemorySpace(CONFIG_FILENAME, CONFIG_FILE_SIZE);
auto *space =
new FileMemorySpace(configUpdateFlow_.get_fd(), CONFIG_FILE_SIZE);
memory_config_handler()->registry()->insert(
node(), openlcb::MemoryConfigDefs::SPACE_CONFIG, space);
additionalComponents_.emplace_back(space);
}
#endif // NOT ARDUINO, YES ESP32
#endif // OPENMRN_HAVE_POSIX_FD
}

SimpleTrainCanStack::SimpleTrainCanStack(
Expand Down Expand Up @@ -220,6 +226,7 @@ int SimpleStackBase::create_config_file_if_needed(const InternalConfigData &cfg,
uint16_t expected_version, unsigned file_size)
{
HASSERT(CONFIG_FILENAME);
HASSERT(configUpdateFlow_.get_fd() < 0);
struct stat statbuf;
bool reset = false;
bool extend = false;
Expand Down Expand Up @@ -318,7 +325,12 @@ int SimpleStackBase::check_version_and_factory_reset(
const InternalConfigData &cfg, uint16_t expected_version, bool force)
{
HASSERT(CONFIG_FILENAME);
int fd = configUpdateFlow_.open_file(CONFIG_FILENAME);
int fd = configUpdateFlow_.get_fd();
if (fd < 0)
{
fd = configUpdateFlow_.open_file(CONFIG_FILENAME);
}

if (cfg.version().read(fd) != expected_version)
{
/// @todo (balazs.racz): We need to clear the eeprom. Best would be if
Expand Down
5 changes: 5 additions & 0 deletions src/utils/Crc.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@
* @date 16 Dec 2014
*/

#ifndef _UTILS_CRC_HXX_
#define _UTILS_CRC_HXX_

#include <stdint.h>
#include <stddef.h>

Expand Down Expand Up @@ -186,3 +189,5 @@ private:
/// Current value of the state register for the CRC computation.
uint8_t state_;
};

#endif // _UTILS_CRC_HXX_

0 comments on commit 7074024

Please sign in to comment.