OpenLcbCLib 1.0 Alpha
OpenSource C Library to create OpenLcb/Lcc Nodes
Loading...
Searching...
No Matches
protocol_train_handler.h File Reference

Train Control Protocol message handler (Layer 1) More...

Go to the source code of this file.

Data Structures

struct  interface_protocol_train_handler_t
 Callback interface for train protocol events. More...
 

Functions

void ProtocolTrainHandler_initialize (const interface_protocol_train_handler_t *interface)
 Initializes the Train Control Protocol handler.
 
void ProtocolTrainHandler_handle_train_command (openlcb_statemachine_info_t *statemachine_info)
 Handles an incoming Train Control Protocol command (MTI_TRAIN_PROTOCOL).
 
void ProtocolTrainHandler_handle_train_reply (openlcb_statemachine_info_t *statemachine_info)
 Handles an incoming Train Control Protocol reply (MTI_TRAIN_REPLY).
 
void ProtocolTrainHandler_handle_emergency_event (openlcb_statemachine_info_t *statemachine_info, event_id_t event_id)
 Handles a global or addressed emergency event for a train node.
 

Detailed Description

Train Control Protocol message handler (Layer 1)

Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:

  • Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
  • Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

Handles incoming MTI_TRAIN_PROTOCOL (0x05EB) commands and MTI_TRAIN_REPLY (0x01E9) replies. Automatically updates train state, builds protocol replies, and forwards consist commands to listeners. Fires optional notifier callbacks after state is updated.

Train-node side callbacks are split into:

  • Notifiers: fire AFTER state is updated (all optional, NULL = ignored)
  • Decision callbacks: return a value the handler uses to build a reply (optional, NULL = handler uses default behavior)

Throttle-side callbacks are all notifiers that fire when a reply is received.

Called from the main statemachine when a train protocol message is received.

Author
Jim Kueneman
Date
9 Mar 2026
See also
openlcb_application_train.h - Layer 2 state and API

Function Documentation

◆ ProtocolTrainHandler_initialize()

void ProtocolTrainHandler_initialize ( const interface_protocol_train_handler_t * interface)
extern

Initializes the Train Control Protocol handler.

Parameters
interfacePointer to interface_protocol_train_handler_t callbacks (may be NULL).

Initializes the Train Control Protocol handler.

* @param interface  Populated callback table (may be NULL).
* 

◆ ProtocolTrainHandler_handle_train_command()

void ProtocolTrainHandler_handle_train_command ( openlcb_statemachine_info_t * statemachine_info)
extern

Handles an incoming Train Control Protocol command (MTI_TRAIN_PROTOCOL).

Decodes the sub-command byte, updates train_state, builds the appropriate reply, and fires notifier or decision callbacks.

Parameters
statemachine_infoPointer to openlcb_statemachine_info_t context.

Handles an incoming Train Control Protocol command (MTI_TRAIN_PROTOCOL).

Masks the P bit (bit 7) from the instruction byte before dispatch so that forwarded commands (P=1) are processed identically to direct commands (P=0). After local processing of speed, function, or emergency stop commands, initiates listener forwarding enumeration if listeners are attached and the command is not itself a forwarded command (P=0).

* @param statemachine_info  Pointer to openlcb_statemachine_info_t context.
* 

◆ ProtocolTrainHandler_handle_train_reply()

void ProtocolTrainHandler_handle_train_reply ( openlcb_statemachine_info_t * statemachine_info)
extern

Handles an incoming Train Control Protocol reply (MTI_TRAIN_REPLY).

Decodes the reply sub-command and fires the matching throttle-side notifier callback.

Parameters
statemachine_infoPointer to openlcb_statemachine_info_t context.

Handles an incoming Train Control Protocol reply (MTI_TRAIN_REPLY).

* @param statemachine_info  Pointer to openlcb_statemachine_info_t context.
* 

◆ ProtocolTrainHandler_handle_emergency_event()

void ProtocolTrainHandler_handle_emergency_event ( openlcb_statemachine_info_t * statemachine_info,
event_id_t event_id )
extern

Handles a global or addressed emergency event for a train node.

Activates or clears the emergency state based on the well-known Event ID and fires the appropriate notifier callback.

Parameters
statemachine_infoPointer to openlcb_statemachine_info_t context.
event_idWell-known event_id_t for the emergency condition.

Sets or clears emergency flags based on the well-known Event ID and fires the appropriate notifier callback.

* @param statemachine_info  Pointer to openlcb_statemachine_info_t context.
* @param event_id           Well-known event_id_t for the emergency condition.
* 

Copyright (c) 2026 Jim Kueneman all rights reserved. See the License