Customize Rest bus simulation behavior

Configuring a node

Configuring a node is an essential step before handling its events, this is done through:
  • Dragging and dropping a node from a database to the nodes section of the project explorer.

  • Initializing the needed properties of the node, this is done either from the properties panel of the node or from scripts.

../_images/initialize_node.gif

Handling node events

Once a node is initialized, its events are accessible as regular events from scripts using the global variable name of the node.

Note

Node events are:
  • on_start: event fired when ecu starts.

  • on_stop: event fired when ecu stops.

  • on_reset_ecu: fired raised when an ecu is reset.

Ethernet nodes have three extra events:
  • on_sending_sd: event fired when the ecu sends a someip-sd message.

  • on_sending_someip: event fired when the ecu sends a someip message.

  • on_receiving: event fired when the ecu receives a message.

Example of how to use events

# appending event callback function that will execute when the event fires.
node_name.on_sending_sd += event_function_defined

# some code to execute while the event is needed

# removing the event callback function so that it will no longer execute.
node_name.on_sending_sd -= event_function_defined

Important

When appending a method to an event it is good practice to remove that event once it's no longer needed (usually at the end of the script).
This way the script is cleaned and the method is no longer called with the event.
Appending and removing event methods is done through the operators += (for appending) and -= (for removing).
Event callback functions are regular functions defined in script with optional event related argument.
To know more about event callback functions arguments check EcuNodeEthernet or EcuNodeCAN.

Example for defining callback function used for on_sending_someip event, this function requires two arguments, a node object representing the node that fired the event, and an event_args that holds the someip message that was sent.
# defining event callback function
def event_function_defined(node, event_args):
        #code to execute when the event fires

Important

Not respecting the number of arguments defined for an event when appending an event callback function would raise an exception in the script.
The name of the arguments is irrelevant for the functions.

Example script

from time import sleep

def on_sending_sd_msg(node, event_args):
    print("sending someip-sd message")
    # customizing message before sending it
    # send original message
    event_args.message.send()
    # changing message options before sending new message
    event_args.message.ipv4_header.ip_address_source = "192.168.0.100"
    # setting wrong checksum
    event_args.message.udp_header.checksum = 0
    # message will be sent again after this event is handled
    # other code to execute
    
def on_ecu_start():
    print("ecu started")
    # other code to execute
    
def on_ecu_stop():
    print("ecu stopped")
    # other code to execute
    
def on_ecu_reset():
    print("ecu reset")
    # other code to execute
    
def on_sending_someip_msg(node, event_args):
    print("sending someip message")
    # dropping the someip message
    event_args.message = None
    # other code to execute
    
# appending event callback functions  
ATM.on_sending_sd += on_sending_sd_msg
ATM.on_start += on_ecu_start
ATM.on_stop += on_ecu_stop
ATM.on_reset_ecu += on_ecu_reset
ATM.on_sending_someip += on_sending_someip_msg

# starting the ecu, this will cause on_start event to fire
ATM.start()

# sleeping for 5 seconds
sleep(5)

# reseting the ecu, this will cause on_reset_ecu event to fire
ATM.reset()

# sleeping for 5 seconds
sleep(5)

# stopping the ecu, this will cause on_stop event to fire
ATM.stop()

# removing event callback functions  
ATM.on_sending_sd -= on_sending_sd_msg
ATM.on_start -= on_ecu_start
ATM.on_stop -= on_ecu_stop
ATM.on_reset_ecu -= on_ecu_reset
ATM.on_sending_someip -= on_sending_someip_msg

# on_sending_sd and on_sending_someip are special event that fire
# when the ecu is about to send a someip-sd or someip message