36#ifndef _OPENLCB_EVENTHANDLERTEMPLATES_HXX_
37#define _OPENLCB_EVENTHANDLERTEMPLATES_HXX_
72#define DEFPROXYFN(FN) \
73 virtual void FN(const EventRegistryEntry ®istry_entry, \
74 EventReport *event, BarrierNotifiable *done) override \
76 HandlerFn(&EventHandler::FN, registry_entry, event, done); \
100#define IGNOREFN(FN) \
101 virtual void FN(const EventRegistryEntry ®istry_entry, \
102 EventReport *event, BarrierNotifiable *done) override \
129template <u
int64_t EVENT_ID>
150 if (event->dst_node && event->dst_node != node_)
152 return done->notify();
154 event->event_write_helper<1>()->WriteAsync(node_,
237 ,
state_(EventState::UNKNOWN)
272 state_ = new_value ? EventState::VALID : EventState::INVALID;
279 requested_ = new_value ? EventState::VALID : EventState::INVALID;
319 bool default_local_state)
338 if (!
isKnown_)
return EventState::UNKNOWN;
340 return EventState::INVALID;
401 uint64_t
event_off,
bool default_local_state)
470 return ((*ptr_) & mask_) ? EventState::VALID : EventState::INVALID;
512 return gpio_->
is_set() ? EventState::VALID : EventState::INVALID;
516 gpio_->
write(new_value);
766 bool Get(
unsigned bit)
const;
784 unsigned size() {
return size_; }
789 void GetBitAndMask(
unsigned bit, uint32_t **data, uint32_t *mask)
const;
791 uint64_t event_base_;
875 bool DecodeEventId(uint64_t event_id, uint8_t **data, uint8_t *value);
877 uint64_t event_base_;
#define DEFPROXYFN(FN)
Proxies an event handler function to a gmock function.
#define IGNOREFN(FN)
Defines an event handler function as ignored.
A BarrierNotifiable allows to create a number of child Notifiable and wait for all of them to finish.
void notify() override
Implementation of the barrier semantics.
OS-independent abstraction for GPIO.
virtual void write(Value new_state) const =0
Writes a GPIO output pin (set or clear to a specific state).
bool is_set() const
Tests the GPIO input pin to see if it is set.
An object that can schedule itself on an executor to run.
static EventRegistry * instance()
Event handler for a single-bit consumer, e.g.
void handle_identify_global(const EventRegistryEntry &entry, EventReport *event, BarrierNotifiable *done) override
Called on the need of sending out identification messages.
void handle_event_report(const EventRegistryEntry &entry, EventReport *event, BarrierNotifiable *done) override
Called on incoming EventReport messages.
void handle_identify_consumer(const EventRegistryEntry &entry, EventReport *event, BarrierNotifiable *done) override
Called on another node sending IdentifyConsumer.
void SendQuery(WriteHelper *writer, BarrierNotifiable *done)
Queries producers and acquires the current state of the bit.
void handle_producer_identified(const EventRegistryEntry &entry, EventReport *event, BarrierNotifiable *done) override
Called on another node sending ProducerIdentified for this event.
Base class for single-bit producer and consumer objects.
void SendConsumerIdentified(EventReport *event, BarrierNotifiable *done)
Sends off two packets using event_write_helper{3,4} of ConsumerIdentified for handling a global ident...
void unregister_handler()
Removes this event handler from the global event manager.
void register_handler(uint64_t event_on, uint64_t event_off)
Registers this event handler with the global event manager.
void SendEventReport(WriteHelper *writer, Notifiable *done)
Requests the event associated with the current value of the bit to be produced (unconditionally): sen...
void SendProducerIdentified(EventReport *event, BarrierNotifiable *done)
Sends off two packets using event_write_helper{1,2} of ProducerIdentified for handling a global ident...
void HandlePCIdentify(Defs::MTI mti_valid, EventReport *event, BarrierNotifiable *done)
Checks if the event in the report is something we are interested in, and if so, sends off a {Producer...
@ BOTH_ON_IS_ZERO
This registration is for two events, and the lower numbered is the event on.
@ EVENT_ON
This registration is for a single event_on.
@ EVENT_OFF
This registration is for a single event_off.
@ BOTH_OFF_IS_ZERO
This registration is for two events, and the lower numbered is the event off.
Represents a bit of state using two events.
uint64_t event_on()
returns the event ID for representing the state transition OFF->ON.
virtual void set_state(bool new_value)=0
Updates the hardware for the new event state.
virtual EventState get_requested_state()
Get the requested state.
uint64_t event_off()
returns the event ID for representing the state transition ON->OFF.
virtual Node * node()=0
returns the OpenLCB virtual node from which to send the respective events when the bit changes.
virtual EventState get_current_state()=0
returns the current hardware state: true for ON, false for OFF.
Producer-Consumer event handler for a single bit represented by two event IDs.
void handle_identify_global(const EventRegistryEntry &entry, EventReport *event, BarrierNotifiable *done) override
Called on the need of sending out identification messages.
void SendQueryConsumer(WriteHelper *writer, BarrierNotifiable *done)
Queries consumer and acquires the current state of the bit.
void handle_identify_producer(const EventRegistryEntry &entry, EventReport *event, BarrierNotifiable *done) override
Called on another node sending IdentifyProducer.
void SendQueryProducer(WriteHelper *writer, BarrierNotifiable *done)
Queries producers and acquires the current state of the bit.
void handle_consumer_identified(const EventRegistryEntry &entry, EventReport *event, BarrierNotifiable *done) override
Called on another node sending ConsumerIdentified for this event.
BitEventPC(BitEventInterface *bit)
Event handler for a single-bit producer, e.g.
void Update(WriteHelper *writer, Notifiable *done)
Requests the event associated with the current value of the bit to be produced (unconditionally).
void SendQuery(WriteHelper *writer, BarrierNotifiable *done)
Queries consumers and acquires the current state of the bit.
void handle_identify_producer(const EventRegistryEntry &entry, EventReport *event, BarrierNotifiable *done) override
Called on another node sending IdentifyProducer.
void handle_identify_global(const EventRegistryEntry &entry, EventReport *event, BarrierNotifiable *done) override
Called on the need of sending out identification messages.
BitEventProducer(BitEventInterface *bit)
Producer-Consumer event handler for a sequence of bits represented by a dense block of consecutive ev...
void handle_identify_producer(const EventRegistryEntry &entry, EventReport *event, BarrierNotifiable *done) override
Called on another node sending IdentifyProducer.
void handle_identify_consumer(const EventRegistryEntry &entry, EventReport *event, BarrierNotifiable *done) override
Called on another node sending IdentifyConsumer.
void handle_identify_global(const EventRegistryEntry &entry, EventReport *event, BarrierNotifiable *done) override
Called on the need of sending out identification messages.
bool Get(unsigned bit) const
void SendIdentified(WriteHelper *writer, BarrierNotifiable *done)
Sends out a ProducerRangeIdentified.
void handle_event_report(const EventRegistryEntry &entry, EventReport *event, BarrierNotifiable *done) override
Called on incoming EventReport messages.
void Set(unsigned bit, bool new_value, WriteHelper *writer, BarrierNotifiable *done)
Requests the event associated with the current value of the bit to be produced (unconditionally).
Producer event handler for a sequence of bits represented by a dense block of consecutive event IDs.
virtual ~BitRangeEventP()
Destructor.
void handle_event_report(const EventRegistryEntry &entry, EventReport *event, BarrierNotifiable *done) override
Called on incoming EventReport messages.
BitRangeEventP(Node *node, uint64_t event_base, uint32_t *backing_store, unsigned size)
Creates a new bit range producer.
void handle_identify_consumer(const EventRegistryEntry &entry, EventReport *event, BarrierNotifiable *done) override
Called on another node sending IdentifyConsumer.
void handle_identify_global(const EventRegistryEntry &entry, EventReport *event, BarrierNotifiable *done) override
Called on the need of sending out identification messages.
Consumer event handler for a sequence of bytes represented by a dense block of consecutive event IDs.
void handle_event_report(const EventRegistryEntry &entry, EventReport *event, BarrierNotifiable *done) override
Called on incoming EventReport messages.
void handle_identify_consumer(const EventRegistryEntry &entry, EventReport *event, BarrierNotifiable *done) override
Called on another node sending IdentifyConsumer.
bool DecodeEventId(uint64_t event_id, uint8_t **data, uint8_t *value)
takes an event ID and checks if we are responsible for it.
void handle_identify_global(const EventRegistryEntry &entry, EventReport *event, BarrierNotifiable *done) override
Called on the need of sending out identification messages.
void SendIdentified(WriteHelper *writer, BarrierNotifiable *done)
Sends out a ConsumerRangeIdentified.
virtual void notify_changed(unsigned offset)
This function is called by the handler when a data value overwrite event arrives.
TODO(balazs.racz): Add another class here, ByteRangeEventPC.
void handle_identify_consumer(const EventRegistryEntry &entry, EventReport *event, BarrierNotifiable *done) override
Called on another node sending IdentifyConsumer.
void handle_identify_global(const EventRegistryEntry &entry, EventReport *event, BarrierNotifiable *done) override
Called on the need of sending out identification messages.
void Update(unsigned byte, WriteHelper *writer, BarrierNotifiable *done)
Requests the event associated with the current value of a specific byte to be produced (unconditional...
uint64_t CurrentEventId(unsigned byte)
Creates the eventid of the currently valid value of a given byte.
void SendIdentified(WriteHelper *writer, BarrierNotifiable *done)
Sends out a ProducerRangeIdentified.
void handle_identify_producer(const EventRegistryEntry &entry, EventReport *event, BarrierNotifiable *done) override
Called on another node sending IdentifyProducer.
void handle_consumer_identified(const EventRegistryEntry &entry, EventReport *event, BarrierNotifiable *done) override
Called on another node sending ConsumerIdentified for this event.
void handle_event_report(const EventRegistryEntry &entry, EventReport *event, BarrierNotifiable *done) override
Called on incoming EventReport messages.
void handle_consumer_range_identified(const EventRegistryEntry &entry, EventReport *event, BarrierNotifiable *done) override
Called on another node sending ConsumerRangeIdentified.
Speciallization of NetworkInitializedBit that adds callback support when the state changes.
void reset()
Call this function in order to reset the network state to unknown.
void set_state(bool new_value) override
Call from the network stack (or the client before notifying the network stack) to set the state.
std::function< void()> callback_
This function is invoked when the state of the bit changes.
void set_change_callback(std::function< void()> cb)
Specifies the change notifier.
CallbackNetworkInitializedBit(openlcb::Node *node, uint64_t event_on, uint64_t event_off, bool default_local_state)
Constructor.
Implementation of the BitEventInterface that has accessors to the desired and actual state,...
EventState get_requested_state() override
Get the requested state.
EventState get_current_state() override
Get the current state.
void set_state(bool new_value) override
Set the current state.
~DistributedBit()
Destructor.
Node * node_
node that this interface is bound to
EventState requested_
Event state reauested.
void toggle_state()
Invert the requested state from the current state.
void set_requested_state(bool new_value)
Set the requested state.
EventState state_
Event state.
Node * node() override
Get a refference to the owning Node.
Abstract base class for all event handlers.
virtual void handle_identify_global(const EventRegistryEntry ®istry_entry, EventReport *event, BarrierNotifiable *done)=0
Called on the need of sending out identification messages.
virtual void handle_consumer_range_identified(const EventRegistryEntry ®istry_entry, EventReport *event, BarrierNotifiable *done)
Called on another node sending ConsumerRangeIdentified.
virtual void handle_producer_range_identified(const EventRegistryEntry ®istry_entry, EventReport *event, BarrierNotifiable *done)
Called on another node sending ProducerRangeIdentified for this event.
virtual void handle_event_report(const EventRegistryEntry ®istry_entry, EventReport *event, BarrierNotifiable *done)=0
Called on incoming EventReport messages.
virtual void handle_identify_consumer(const EventRegistryEntry ®istry_entry, EventReport *event, BarrierNotifiable *done)=0
Called on another node sending IdentifyConsumer.
virtual void handle_producer_identified(const EventRegistryEntry ®istry_entry, EventReport *event, BarrierNotifiable *done)
Called on another node sending ProducerIdentified for this event.
virtual void handle_consumer_identified(const EventRegistryEntry ®istry_entry, EventReport *event, BarrierNotifiable *done)
Called on another node sending ConsumerIdentified for this event.
virtual void handle_identify_producer(const EventRegistryEntry ®istry_entry, EventReport *event, BarrierNotifiable *done)=0
Called on another node sending IdentifyProducer.
Structure used in registering event handlers.
Class that advertises an event ID to be produced.
void handle_identify_producer(const EventRegistryEntry ®istry_entry, EventReport *event, BarrierNotifiable *done) OVERRIDE
Called on another node sending IdentifyProducer.
void handle_identify_global(const EventRegistryEntry ®istry_entry, EventReport *event, BarrierNotifiable *done) OVERRIDE
Called on the need of sending out identification messages.
FixedEventProducer(Node *node)
Simple implementation of the BitEventInterface for going through GPIO ports.
void set_state(bool new_value) OVERRIDE
Updates the hardware for the new event state.
EventState get_current_state() OVERRIDE
returns the current hardware state: true for ON, false for OFF.
Node * node() OVERRIDE
returns the OpenLCB virtual node from which to send the respective events when the bit changes.
Simple implementation of a BitEventInterface when the true state ofthe variable is mapped in memory (...
MemoryBit(Node *node, uint64_t event_on, uint64_t event_off, T *ptr, T mask)
EventState get_current_state() override
returns the current hardware state: true for ON, false for OFF.
void set_state(bool new_value) override
Updates the hardware for the new event state.
Node * node() override
returns the OpenLCB virtual node from which to send the respective events when the bit changes.
A network-initialized bit is a tri-state bit implementation that starts up in the UNKNOWN state until...
void set_state(bool new_value) override
Call from the network stack (or the client before notifying the network stack) to set the state.
NetworkInitializedBit(Node *node, uint64_t event_on, uint64_t event_off, bool default_local_state)
Constructs a NetworkInitializedBit.
bool is_network_state_known()
EventState get_current_state() override
Accessor from the network stack to return the current state.
Node * node() override
Get a reference to the owning Node.
void toggle_state()
Invert the current state.
uint8_t localState_
local state; either matches the network state or is the constructor-default local state.
uint8_t isKnown_
true when we knowthe network state
Base class for NMRAnet nodes conforming to the asynchronous interface.
A proxy event handler has a single helper function that gets every event handler call with an indicat...
virtual void HandlerFn(EventHandlerFunction fn, const EventRegistryEntry ®istry_entry, EventReport *event, BarrierNotifiable *done)=0
This function will be called for any other incoming event handler function.
SimpleEventHandler ignores all non-essential callbacks.
A statically allocated buffer for sending one message to the OpenLCB bus.
#define OVERRIDE
Function attribute for virtual functions declaring that this funciton is overriding a funciton that s...
#define DISALLOW_COPY_AND_ASSIGN(TypeName)
Removes default copy-constructor and assignment added by C++.
uint64_t EncodeRange(uint64_t begin, unsigned size)
Creates a single encoded event range from the beginning of the range and the number fo events to cove...
EventState invert_event_state(EventState state)
Returns the inverted event state, switching valid and invalid, but not changing unknown and reserved.
EventState
Allowed states of producers and consumers.
Payload eventid_to_buffer(uint64_t eventid)
Converts an Event ID to a Payload suitable to be sent as an event report.
MTI
Known Message type indicators.
@ MTI_PRODUCER_IDENTIFIED_UNKNOWN
producer broadcast, validity unknown
Shared notification structure that is assembled for each incoming event-related message,...