Skip to content

Commit

Permalink
Adds send_message helper. (#484)
Browse files Browse the repository at this point in the history
* Adds a helper function similar to send_event that sends an arbitrary OpenLCB
message to the bus using synchronous allocation.
  • Loading branch information
balazsracz authored Dec 7, 2020
1 parent 9741a0d commit ed1b12c
Showing 1 changed file with 22 additions and 0 deletions.
22 changes: 22 additions & 0 deletions src/openlcb/If.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -471,6 +471,28 @@ public:
}
};

/// Sends an OpenLCB message to the bus. Performs synchronous (dynamic) memory
/// allocation so use it sparingly and when there is sufficient amount of RAM
/// available.
/// @param src_node A local virtual node from which to send the message.
/// @param mti message type indicator
/// @param args either a Payload to send a global message, or a NodeHandle dst
/// and a Payload to send an addressed message.
template <typename... Args> void send_message(Node *src_node, Defs::MTI mti, Args &&...args)
{
Buffer<GenMessage> *msg;
mainBufferPool->alloc(&msg);
msg->data()->reset(mti, src_node->node_id(), std::forward<Args>(args)...);
if (msg->data()->dst == NodeHandle())
{
src_node->iface()->global_message_write_flow()->send(msg);
}
else
{
src_node->iface()->addressed_message_write_flow()->send(msg);
}
}

} // namespace openlcb

#endif // _OPENLCB_IF_HXX_

0 comments on commit ed1b12c

Please sign in to comment.