Using YAKINDU State Machine Trace Callbacks

Typically state machines generated from YAKINDU SCT state charts are used as black boxes. The code that embeds the state machines does not care about the internal execution of the state machine. But there are various use cases where information about internal execution events, especially state changes, must be observed. These kind of execution traces can be implemented manually by adding entry and exit actions that call operations or set variables. But this is very cumbersome and pollutes the statecharts with tracing code. Since version 2.6 the C code generator supports an option to enable tracing callbacks.

Statechart-Trace.jpg

You can enable this feature by adding the Tracing feature to your sgen generator config model:

GeneratorModel for yakindu::c {

    statechart CoffeeMachine {

        feature Outlet {

            targetProject = "coffee_machine"

            targetFolder = "src"

            libraryTargetFolder = "src/sc"

        }

        feature Tracing {

            enterState = true

            exitState = true

        }

}}

Simply copy the code from above or press ctrl-space in the sgen file editor to get feature template proposals. After saving and running the generator you'll notice two additional function prototypes in the generated required header file (CoffeeMachineRequired.h in this example).

/*! This function is called when a state is entered. */

extern void coffeeMachine_stateEntered(CoffeeMachine* handle, const CoffeeMachineStates state);

/*! This function is called when a state is exited. */

extern void coffeeMachine_stateExited(CoffeeMachine* handle, const CoffeeMachineStates state);

 

You only have to implement these callbacks. They will be invoked with the state machine handle and the enum value of the current state. You can do whatever you need to do in the implementation. But be careful that these are no long running actions. 

This feature is currently only available for the C code generator but we will also provide it for Java and C++ code generators. 

 

Learn more about YAKINDU Statechart Tools

 

Share this blog post

    

About The Author

Axel is working with itemis since 2006. He is head of R&D embedded systems and project lead of YAKINDU Statechart Tools.