Open Model Railroad Network (OpenMRN)
Loading...
Searching...
No Matches
openlcb::SimpleStackBase Class Referenceabstract
Inheritance diagram for openlcb::SimpleStackBase:
openlcb::SimpleCanStackBase openlcb::SimpleTcpStackBase openlcb::SimpleCanStack openlcb::SimpleTrainCanStack openlcb::SimpleTcpStack

Classes

class  PhysicalIf
 Polymorphic class that can be implemented by CAN and TCP interfaces separately for appropriate construction order. More...
 

Public Member Functions

 SimpleStackBase (std::function< std::unique_ptr< PhysicalIf >()> create_if_helper)
 
Executor< EXECUTOR_PRIORITIES > * executor ()
 
Serviceservice ()
 
Ififace ()
 
DatagramServicedg_service ()
 
SimpleInfoFlowinfo_flow ()
 Accessor for clients that have their custom SNIP-like handler.
 
virtual Nodenode ()=0
 
MemoryConfigHandlermemory_config_handler ()
 
ConfigUpdateServiceconfig_service ()
 
void set_tx_activity_led (const Gpio *led, long long period=MSEC_TO_NSEC(33))
 Adds an activiy LED which will be flashed every time a message is sent from this node to the network.
 
void restart_stack ()
 Reinitializes the node.
 
void loop_executor (bool delay_start=false)
 Donates the current thread to the executor.
 
void start_after_delay ()
 Call this function when you used delay_start upon starting the executor.
 
void start_executor_thread (const char *name, int priority, size_t stack_size, bool delay_start=false)
 Instructs the executor to create a new thread and run in there.
 
int create_config_file_if_needed (const InternalConfigData &ofs, uint16_t expected_version, unsigned file_size)
 Tries to open the config file; if not existant, the size too small, or the version number is mismatched, then creates a new file of the given size with all 0xFF bytes inside.
 
int check_version_and_factory_reset (const InternalConfigData &ofs, uint16_t expected_version, bool force=false)
 Checks the version information in the EEPROM and performs a factory reset if incorrect or if force is set.
 
void send_event (uint64_t event_id)
 Helper function to send an event report to the bus.
 
void send_message_to (Defs::MTI mti, NodeHandle dst, const string &payload=EMPTY_PAYLOAD)
 Sends an addressed message to the bus.
 
virtual uint64_t get_pip ()
 Get the PIP response value.
 

Static Public Member Functions

static void factory_reset_all_events (const InternalConfigData &ofs, uint64_t node_id, int fd)
 Overwrites all events in the eeprom with a brand new event ID.
 
static void set_event_offsets (const vector< uint16_t > *offsets)
 Call this function at the beginning of appl_main, just before {} or { create_config_file_if_needed} if the list of event offsets are dyamically created instead of statically linked.
 

Static Public Attributes

static const unsigned EXECUTOR_PRIORITIES = 5
 

Protected Member Functions

void start_stack (bool delay_start)
 Call this function once after the actual IO ports are set up.
 
virtual void start_iface (bool restart)=0
 Hook for descendant classes to start the interface.
 
virtual void start_node ()=0
 Hook for clients to initialize the node-specific components.
 
void default_start_node ()
 Exports the memory config spaces that are typically used for a complex node.
 

Protected Attributes

Executor< EXECUTOR_PRIORITIES > executor_ {NO_THREAD()}
 This executor's threads will be handled.
 
Service service_ {&executor_}
 Default service on the particular executor.
 
std::unique_ptr< PhysicalIfifaceHolder_
 Pointer to the polymorphic implementation of the OpenLCB If.
 
Ififace_ {ifaceHolder_->iface()}
 The OpenLCB interface object. Owned by ifaceHolder_;.
 
DatagramServicedatagramService_ {ifaceHolder_->datagram_service()}
 The datagram service bound to the interface object.
 
ConfigUpdateFlow configUpdateFlow_ {iface()}
 Calls the config listeners with the configuration FD.
 
InitializeFlow initFlow_ {&service_}
 The initialization flow takes care for node startup duties.
 
EventService eventService_ {iface()}
 Dispatches event protocol requests to the event handlers.
 
SimpleInfoFlow infoFlow_ {iface()}
 General flow for simple info requests.
 
MemoryConfigHandler memoryConfigHandler_
 
std::unique_ptr< HubFlowgcHub_
 All packets are forwarded to this hub in gridconnect format, if needed.
 
std::unique_ptr< GCAdapterBasegcAdapter_
 Bridge between canHub_ and gcHub_. Lazily initialized.
 
std::vector< std::unique_ptr< Destructable > > additionalComponents_
 Stores and keeps ownership of optional components.
 

Friends

class ::openmrn_arduino::OpenMRN
 

Detailed Description

Definition at line 85 of file SimpleStack.hxx.

Constructor & Destructor Documentation

◆ SimpleStackBase()

openlcb::SimpleStackBase::SimpleStackBase ( std::function< std::unique_ptr< PhysicalIf >()>  create_if_helper)

Definition at line 63 of file SimpleStack.cxx.

Member Function Documentation

◆ check_version_and_factory_reset()

int openlcb::SimpleStackBase::check_version_and_factory_reset ( const InternalConfigData &  ofs,
uint16_t  expected_version,
bool  force = false 
)

Checks the version information in the EEPROM and performs a factory reset if incorrect or if force is set.

Returns
file descriptor for config file.
Todo:
(balazs.racz): We need to clear the eeprom. Best would be if there was an ioctl to return the eeprom to factory default state by just erasing the segments.

Definition at line 349 of file SimpleStack.cxx.

◆ config_service()

ConfigUpdateService * openlcb::SimpleStackBase::config_service ( )
inline

Definition at line 153 of file SimpleStack.hxx.

◆ create_config_file_if_needed()

int openlcb::SimpleStackBase::create_config_file_if_needed ( const InternalConfigData &  ofs,
uint16_t  expected_version,
unsigned  file_size 
)

Tries to open the config file; if not existant, the size too small, or the version number is mismatched, then creates a new file of the given size with all 0xFF bytes inside.

This will internally do everything done by the check_version_and_factory_reset call.

Parameters
ofstells where in the file the versioninfo structure lies.
expected_verisonis the correct version of the config file.
file_sizeis the minimum required size of the config file.
Returns
file descriptor for config file.

Definition at line 250 of file SimpleStack.cxx.

◆ default_start_node()

void openlcb::SimpleStackBase::default_start_node ( )
protected

Exports the memory config spaces that are typically used for a complex node.

Expected to be called from start_node().

Definition at line 146 of file SimpleStack.cxx.

◆ dg_service()

DatagramService * openlcb::SimpleStackBase::dg_service ( )
inline
Returns
the datagram service for registering new datagram handlers or acquiring datagram client objects.

Definition at line 131 of file SimpleStack.hxx.

◆ executor()

Executor< EXECUTOR_PRIORITIES > * openlcb::SimpleStackBase::executor ( )
inline
Returns
the executor that's controlling the main thread of the OpenLCB stack.

Definition at line 112 of file SimpleStack.hxx.

◆ factory_reset_all_events()

void openlcb::SimpleStackBase::factory_reset_all_events ( const InternalConfigData &  ofs,
uint64_t  node_id,
int  fd 
)
static

Overwrites all events in the eeprom with a brand new event ID.

Definition at line 394 of file SimpleStack.cxx.

◆ get_pip()

virtual uint64_t openlcb::SimpleStackBase::get_pip ( )
inlinevirtual

Get the PIP response value.

Returns
PIP value

Reimplemented in openlcb::SimpleCanStack, openlcb::SimpleTcpStack, and openlcb::SimpleTrainCanStack.

Definition at line 261 of file SimpleStack.hxx.

◆ iface()

If * openlcb::SimpleStackBase::iface ( )
inline
Returns
the openlcb Interface object.

Definition at line 124 of file SimpleStack.hxx.

◆ info_flow()

SimpleInfoFlow * openlcb::SimpleStackBase::info_flow ( )
inline

Accessor for clients that have their custom SNIP-like handler.

Definition at line 137 of file SimpleStack.hxx.

◆ loop_executor()

void openlcb::SimpleStackBase::loop_executor ( bool  delay_start = false)
inline

Donates the current thread to the executor.

Never returns.

Parameters
delay_startif true, then prevents sending traffic to the bus

Definition at line 177 of file SimpleStack.hxx.

◆ memory_config_handler()

MemoryConfigHandler * openlcb::SimpleStackBase::memory_config_handler ( )
inline
Returns
the handler for the memory configuration protocol. This is needed for registering additional memory spaces.

Definition at line 148 of file SimpleStack.hxx.

◆ node()

virtual Node * openlcb::SimpleStackBase::node ( )
pure virtual
Returns
the virtual node pointer of the main virtual node of the stack (as defined by the NodeID argument of the constructor).

Implemented in openlcb::SimpleCanStack, openlcb::SimpleTcpStack, and openlcb::SimpleTrainCanStack.

◆ restart_stack()

void openlcb::SimpleStackBase::restart_stack ( )

Reinitializes the node.

Useful to call after the connection has flapped (gone down and up).

Definition at line 239 of file SimpleStack.cxx.

◆ send_event()

void openlcb::SimpleStackBase::send_event ( uint64_t  event_id)
inline

Helper function to send an event report to the bus.

Performs synchronous (dynamic) memory allocation so use it sparingly and when there is sufficient amount of RAM available.

Parameters
event_idis the event to send off.

Definition at line 237 of file SimpleStack.hxx.

◆ send_message_to()

void openlcb::SimpleStackBase::send_message_to ( Defs::MTI  mti,
NodeHandle  dst,
const string &  payload = EMPTY_PAYLOAD 
)
inline

Sends an addressed message to the bus.

Performs synchronous (dynamic) memory allocation so use it sparingly and when there is sufficient amount of RAM available.

Parameters
mtiis the message to send
dstis the node to send message to.
payloadis the contents of the message

Definition at line 251 of file SimpleStack.hxx.

◆ service()

Service * openlcb::SimpleStackBase::service ( )
inline
Returns
a plain service bound to the main thread's executor.

Definition at line 118 of file SimpleStack.hxx.

◆ set_event_offsets()

void openlcb::SimpleStackBase::set_event_offsets ( const vector< uint16_t > *  offsets)
static

Call this function at the beginning of appl_main, just before {} or { create_config_file_if_needed} if the list of event offsets are dyamically created instead of statically linked.

Parameters
offsetsis a vector with the data. The last entry must be zero. This vector must outlive the SimpleStack object.

Definition at line 389 of file SimpleStack.cxx.

◆ set_tx_activity_led()

void openlcb::SimpleStackBase::set_tx_activity_led ( const Gpio led,
long long  period = MSEC_TO_NSEC(33) 
)
inline

Adds an activiy LED which will be flashed every time a message is sent from this node to the network.

Parameters
gpioLED that will be flashed on for each packet.
perioddefines in nanosecond the time to spend between updates.

Definition at line 162 of file SimpleStack.hxx.

◆ start_after_delay()

void openlcb::SimpleStackBase::start_after_delay ( )

Call this function when you used delay_start upon starting the executor.

Definition at line 211 of file SimpleStack.cxx.

◆ start_executor_thread()

void openlcb::SimpleStackBase::start_executor_thread ( const char *  name,
int  priority,
size_t  stack_size,
bool  delay_start = false 
)
inline

Instructs the executor to create a new thread and run in there.

Parameters
nameis the thread name for the executor thread
priorityis the executor thread priority (used only for freertos)
stack_sizeis the executor stack in bytes (used only for freertos)
delay_startif true, then prevents sending traffic to the bus

Definition at line 193 of file SimpleStack.hxx.

◆ start_iface()

virtual void openlcb::SimpleStackBase::start_iface ( bool  restart)
protectedpure virtual

Hook for descendant classes to start the interface.

Parameters
restartfalse upon initial start, true upon restart.

Implemented in openlcb::SimpleCanStackBase, and openlcb::SimpleTcpStackBase.

◆ start_node()

virtual void openlcb::SimpleStackBase::start_node ( )
protectedpure virtual

Hook for clients to initialize the node-specific components.

Implemented in openlcb::SimpleCanStack, openlcb::SimpleTcpStack, and openlcb::SimpleTrainCanStack.

◆ start_stack()

void openlcb::SimpleStackBase::start_stack ( bool  delay_start)
protected

Call this function once after the actual IO ports are set up.

Calling before the executor starts looping is okay.

Definition at line 114 of file SimpleStack.cxx.

Friends And Related Symbol Documentation

◆ ::openmrn_arduino::OpenMRN

friend class ::openmrn_arduino::OpenMRN
friend

Definition at line 173 of file SimpleStack.hxx.

Member Data Documentation

◆ additionalComponents_

std::vector<std::unique_ptr<Destructable> > openlcb::SimpleStackBase::additionalComponents_
protected

Stores and keeps ownership of optional components.

Definition at line 311 of file SimpleStack.hxx.

◆ configUpdateFlow_

ConfigUpdateFlow openlcb::SimpleStackBase::configUpdateFlow_ {iface()}
protected

Calls the config listeners with the configuration FD.

Definition at line 294 of file SimpleStack.hxx.

◆ datagramService_

DatagramService* openlcb::SimpleStackBase::datagramService_ {ifaceHolder_->datagram_service()}
protected

The datagram service bound to the interface object.

Owned by ifaceHolder_;

Definition at line 292 of file SimpleStack.hxx.

◆ eventService_

EventService openlcb::SimpleStackBase::eventService_ {iface()}
protected

Dispatches event protocol requests to the event handlers.

Definition at line 298 of file SimpleStack.hxx.

◆ executor_

Executor<EXECUTOR_PRIORITIES> openlcb::SimpleStackBase::executor_ {NO_THREAD()}
protected

This executor's threads will be handled.

Definition at line 283 of file SimpleStack.hxx.

◆ EXECUTOR_PRIORITIES

const unsigned openlcb::SimpleStackBase::EXECUTOR_PRIORITIES = 5
static

Definition at line 105 of file SimpleStack.hxx.

◆ gcAdapter_

std::unique_ptr<GCAdapterBase> openlcb::SimpleStackBase::gcAdapter_
protected

Bridge between canHub_ and gcHub_. Lazily initialized.

Definition at line 309 of file SimpleStack.hxx.

◆ gcHub_

std::unique_ptr<HubFlow> openlcb::SimpleStackBase::gcHub_
protected

All packets are forwarded to this hub in gridconnect format, if needed.

Will be initialized upon first use.

Definition at line 307 of file SimpleStack.hxx.

◆ iface_

If* openlcb::SimpleStackBase::iface_ {ifaceHolder_->iface()}
protected

The OpenLCB interface object. Owned by ifaceHolder_;.

Definition at line 289 of file SimpleStack.hxx.

◆ ifaceHolder_

std::unique_ptr<PhysicalIf> openlcb::SimpleStackBase::ifaceHolder_
protected

Pointer to the polymorphic implementation of the OpenLCB If.

Definition at line 287 of file SimpleStack.hxx.

◆ infoFlow_

SimpleInfoFlow openlcb::SimpleStackBase::infoFlow_ {iface()}
protected

General flow for simple info requests.

Definition at line 300 of file SimpleStack.hxx.

◆ initFlow_

InitializeFlow openlcb::SimpleStackBase::initFlow_ {&service_}
protected

The initialization flow takes care for node startup duties.

Definition at line 296 of file SimpleStack.hxx.

◆ memoryConfigHandler_

MemoryConfigHandler openlcb::SimpleStackBase::memoryConfigHandler_
protected
Initial value:
{
datagramService_, nullptr, config_num_memory_spaces()}
DatagramService * datagramService_
The datagram service bound to the interface object.

Definition at line 302 of file SimpleStack.hxx.

◆ service_

Service openlcb::SimpleStackBase::service_ {&executor_}
protected

Default service on the particular executor.

Definition at line 285 of file SimpleStack.hxx.


The documentation for this class was generated from the following files: