Deep C integration: Integrating your C source code with your state machines

Introduction

The YAKINDU Statechart Tools Professional Edition comes with a Deep C Integration feature which allows for using C types, variables, and operations directly within the statechart model. C header files located in your workspace are automatically recognized by the tool and all contained type and operation declarations are made accessible in the statechart editor with all its editing features like code completion and validation. In addition to your custom C types, the C99 standard primitive types, like int16_t, are also available out of the box.

Making your self-defined C types, structs, and unions available in your YAKINDU statecharts saves you a lot of time and hassle that would otherwise be needed to map data from your C-type variables to statechart variables and vice versa. The Deep C Integration, however, allows you to directly create and manipulate your data structures in their native C types.

Please note:

Instead of „types, structs, and unions”, subsequently we will speak of „types” only. While this does not precisely confirm with the C programming language nomenclature, it is much easier to read in a text. From a statechart’s point of view, there is no relevant difference between types, structs, and unions anyway. Should it become necessary to differentiate between types, structs, and unions, we will do so explicitly.

The subsequent sections will explain how to use the C integration in practice, using a sample project. In this example, we will define some geometry types like Point or Triangle in C header files and demonstrate how to make them available and use them in a statechart model.

Creating a new C project

  1. In the Eclipse main menu, select File → New → Project…. The New Project wizard opens.
  2. Select C/C++ → C Project.
    Creating a new C project
  3. Click Next >. The C Project dialog appears.
  4. Enter the name of your project into the Project name field. For the sake of this example, we call the project Geometry.
  5. Specify the location of the project folder by either using the default location or by explicitly specifying a directory in the Location field.
  6. Select the Project type. In order to keep things plain and simple, for this example we create an Empty Project.
  7. Select the toolchain you want to work with. It contains the necessary tools for C development. By default only the toolchains supporting your local platform are displayed. Since this example has been created on a Linux machine, the toolchain Linux GCC is shown.
    Specifying the C project's properties
  8. Click Next >.
  9. Specify platforms, configurations, and project settings. This is more specific to C than to YAKINDU Statechart Tools, so we won’t go into any details here.
    Specifying platforms, configurations, and project settings
  10. Click Finish.
  11. Eclipse asks whether it should associate this kind of project with the C/C++ perspective. Usually this is what you want, so set a checkmark at Remember my decision and click Yes.
  12. Eclipse creates the C project, here Geometry.

Creating a C header file

Now we can create a C header file specifying our own C type definitions which we can use in a state machine later. In order to create the file, let’s proceed as follows:

  1. In the project explorer view, right-click on the project. The context menu opens.
  2. In the context menu, select New → Header File.
    Creating a C header file
  3. The dialog New Header File is shown. Specify the name of the header file. Here we choose point.h.
    Selecting a header filename
  4. Click Finish.
  5. The header file point.h is created.

Defining a C struct

In the created header file we define a struct type named Point, which we will later use in a statechart. A (two-dimensional) point consists of an x and a y coordinate. We choose int16_t to represent a coordinate, i.e. a 16-bit signed integer. The complete header file containing the struct definition looks like this:

/*
 * point.h
 *
 */

#ifndef POINT_H_
#define POINT_H_

#include <stdint.h>

typedef struct {
    int16_t x;
    int16_t y;
} Point;

#endif /* POINT_H_ */

Please note:

In C it is possible to define structs, unions and enums without a typedef. They can be referenced by using the corresponding qualifying keyword ( struct, union, or enum, respectively). As the statechart language does not support these qualifiers, the usage of struct, union and enumeration types is currently restricted to those defined by a typedef.

Using C types in a statechart

Creating a statechart model

Let’s create a statechart model now to make use of the C type Point we have just defined.

  1. Right-click on the project. The context menu opens.
  2. Select New → C Statechart Model …. The New YAKINDU Statechart wizard is shown.
  3. In the dialog, specify the directory and the filename for the new statechart model file. The filename should end with .sct.
  4. Click Finish. If Eclipse asks you whether to switch to the YAKINDU Modeling perspective, please confirm.
  5. The new statechart model is created:
    New statechart model

Defining a C-type variable in a statechart

Variables are defined in the definition section on the left-hand side of the statechart editor. Double-click into the definition section to edit it.

In order to make use of the struct defined above we have to import the point.h header file:

import: "point.h"

With the definitions from point.h at hand, we can declare a variable pointA of the Point type. In the statechart’s definition section, enter the following text:

interface:
	var pointA:

On the right-hand side of the colon in the variable declaration, the variable’s type must follow. In order to see which types are available, press [Ctrl+Space]. The content assist opens and shows the C types available, depending on the headers imported within your statechart, i.e.

  • the C basic standard types,
  • the C99 types, provided by including stdint.h,
  • the self-defined Point type, provided by including point.h,
  • the qualifier pointer.

Using content assist to display available types

Using content assist to display available types

Selecting the Point menu entry completes the variable definition:

A "Point" variable

A Point variable

Using a C-type variable in a statechart

A statechart variable with a C type can be used everywhere a „normal” statechart variable can be used.

Let’s consider the above example extended by an additional count variable of the C99 int8_t standard type. Additionally, we add an event that will be used as a trigger to switch between states.

import: "point.h"
interface:
    var count: int8_t
    var pointA: Point
    in event tick

The statechart below uses these variables in various places, i.e. in transition actions, in internal actions, and in guard conditions.

Using C-type variables

Using C-type variables

Variables of primitive types like var count: int8_t are accessed as expected, e.g. count = 0 or count += 1;

The dot notation is used to access structure elements. For example, pointA.x = 0; pointA.y = 0 sets pointA to the origin of the coordinate system.

The statechart type system

When parsing a C header file YAKINDU Statechart Tools are mapping the C data types to an internal type system. You can open a C header file in Eclipse with the Sample Reflective Ecore Model Editor to see how the mapping result looks like.

In case you are interested in the EMF model underlying SCT’s type system, you can find it in the source code of the YAKINDU Statechart Tools open edition at /org.yakindu.base.types/model/types.ecore.

Imports and includes

Importing a C header

YAKINDU Statechart Tools gives you direct access to C header files within the statechart model. This saves time during development, especially while integrating your state machine with your C program. In general you can import (include) all C header files residing in the same project as well as those header files that are available on one of the CDT project’s include paths, see Properties → C/C++ General → Paths and Symbols in the context menu of your C project.

To import a C header file, go to the beginning of your statechart’s definition section, enter import: and hit [Ctrl]+[Space]. The content assist now shows all C header files you can import (besides other syntactical elements that would be valid here). In our example one of the first includes provided by the content assist is point.h, which is found in /Geometry/point.h. Other imports shown by the content assist are provided by the various include paths configured in your CDT project. For example, figure "Selecting a C header to import" shows headers on the basic cygwin toolchain on a Windows system.

Please note:

Contrary to a C compiler, YAKINDU Statechart Tools do not support transitive imports. That is to say, if a header file a.h imports another header file b.h using the #include directive, b.h will not be „seen” in YAKINDU Statechart Tools unless you import it explicitly in your statechart’s definition section.

The following picture for example .

Selecting a namespace to import

Selecting a C header to import

If we had more than a single header file in the project, we would see them all. The content assist shows all header files in a project, including those in subdirectories. A C header’s path is relative to the statechart it is imported into.

Click on the point.h entry in the menu to complete the import statement. The result looks like this:

import: "point.h"

interface:
	var pointA: Point




Data structure traversal via dot notation

The dot notation to access structure members can traverse an arbitrary number of stages. As an example, let’s define a datatype named Triangle. A triangle is defined by three points. Using dot notation in a statechart, you can navigate from a triangle to its individual points and further on to the points' coordinates.

The C header file triangle.h specifies the Triangle type:

#ifndef TRIANGLE_H_
#define TRIANGLE_H_

#include "./point.h"

typedef struct {
    Point a, b, c;
} Triangle;

#endif /* TRIANGLE_H_ */

A Triangle consists of the three Points a, b, and c. Let’s define a Triangle t in a statechart’s definition section as follows:

import: triangle

interface:
    var t: Triangle

With this definition we can use expressions like t.a.x, see the image below. Regardless of where you are currently editing an expression, you can always use the code assist to explore which fields are available at that very point and of which types these fields are. Example:

Content assist in data structure traversal

Content assist in data structure traversal

Pointers

Pointers are a core feature of the C programming language. YAKINDU Statechart Tools' Deep C Integration is making C pointers available to you in your statecharts. In particular, you can

  • declare typed pointer variables,
  • assign a pointer to a pointer variable of the same type,
  • retrieve the pointer pointing to a given variable,
  • pass pointers as parameters to functions, and
  • receive a pointer as a functions return value.

Declaring pointer variables

Pointer variables are declared in a statechart’s definition section as shown in the following example:

var n: int32_t
var pInt: pointer<int32_t>
var ppInt: pointer<pointer<int32_t> >
var pTriangle: pointer<Triangle>;

The declarations above declare

  • n as a (non-pointer) variable of type int32_t,
  • pInt as a pointer variable that is pointing to a variable of type int32_t,
  • ppInt as a pointer that is pointing to a pointer that is pointing to a variable of type int32_t, and
  • pTriangle as a pointer to a variable of the self-defined type Triangle.

Please note:

When closing the type specification in a pointer declaration with angle brackets, e. g. pointer<pointer<int32_t> >, the > characters must be separated from each other by one or more white space characters. Writing e. g. pointer<pointer<int32_t>> would result in an error. This restrictions pertains to the current release candidate of YAKINDU Statechart Tools PRO only and will be fixed in the final release.

Using pointer variables

In order to actually assign a pointer to a pointer variable, you have to get hold of that pointer. To retrieve the pointer to a variable v, use v's extension function pointer. That is, for a variable v, the expression v.pointer evaluates to a pointer to v. Each variable has the pointer extension function.

Example: Let’s say the pointer variable pInt (declared in the example above) should point to the variable n. The following assignment accomplishes this:

pInt = n.pointer

Similarly, a pointer to a pointer to a base type can be retrieved as follows:

ppInt = pInt.pointer;

Or even:

ppInt = n.pointer.pointer

In order to deference a pointer, i. e. to retrieve the value of what the pointer is pointing to, use the value extension function, which is available on all pointer-type variables.

Example: Let’s say the pointer variable pInt (declared in the example above) is pointing to some int32_t variable. The value of the variable pInt is pointing to should be assigned to the int32_t variable n. The following assignment accomplishes this:

n = pInt.value;

Similarly, if ppInt points to a pointer pointing to some int32_t variable, the following statement retrieves the latter’s value:

n = ppInt.value.value;

Passing pointer parameters to C functions is straightforward. Let’s say you have a C function to rotate a triangle around a center point by a given angle. The C function is defined like this:

Triangle* rotateTriangle(Triangle* triangle, Point* centerPoint, float angle) { … }

Provided the function is declared in an imported C header file, you can call it directly like this:

pTriangle2 = rotateTriangle(pTriangle, pCenterPoint, 45.0);

Please note:

Assigning a pointer to a pointer variable is only possible if the pointer types are the same.

Arrays

Unlike other variables, arrays are not defined in a statechart’s definition section, but rather on the C side in header files. Importing a C header containing an array definition makes the array available to a statechart.

While YAKINDU Statechart Tool’s Deep C Integration provides mechanisms for accessing individual elements of an existent array, arrays must be allocated statically or dynamically in C. Initializing the array elements is possible in C as well as in the statechart. However, depending on the concrete application it might generally be easier in C.

The header file sample_arrays.h defines a couple of sample arrays:

#ifndef SAMPLE_ARRAYS_H_
#define SAMPLE_ARRAYS_H_

#include <stdint.h>
#include "triangle.h"

int32_t coordinates[] = {0, 0, 10, 0, 5, 5};

Triangle manyTriangles[200];

int32_t * pArray[10];

#endif /* SAMPLE_ARRAYS_H_ */

The following arrays are defined:

  • coordinates is statically allocated to hold six int32_t elements and it is initialized with values for all six of them. More precisely, the number of elements in the initializer determines the size of the array.
  • manyTriangles is statically allocated with enough memory to hold 200 elements of the self-defined Triangle type. However, these elements are not initialized. This can and should be done either in C or in the state machine. An example is given below.
  • pArray is of size 10 and holds pointers to int32_t values.

As mentioned above, importing a header file containing array definitions into the statechart’s definition section is sufficient to make the arrays available in a statechart. Example:

import: sample_arrays

With this import, you can access the arrays in statechart language expressions, for example in a state’s local reactions:

entry /
coordinates[2] = 42

Writing to array elements is as straightforward as you would expect. Examples:

coordinates[0] = coordinates[0] + 1;
pArray[3] = n.pointer;
pArray[4] = coordinates[0].pointer

Passing arrays as parameters to C functions is straightforward. Let’s say you have a C function sort to sort the elements of a one-dimensional int32_t array and return a pointer to the sorted array:

int32_t* sort(int32_t data[], int size) {…}

Please note that in C a function cannot return an array as such, but only a pointer to it. Analogously you cannot pass an array by value as a parameter to a function, i. e. the data bytes the array is consisting of are not copied into the function’s formal parameter. Instead a pointer to the array is passed to the function, or – to be more exact – a pointer to the array’s first element. To express this in the function’s formal parameter type, you can specify the sort function equivalently to the above definition as follows. The data parameter is now specified as int32_t* data instead of int32_t data[], but the meaning is exactly the same.

int32_t* sort(int32_t* data, int size) {…}

Provided the function is declared in an imported C header file, you can call it directly like this:

sort(coordinates, 6)

Please note:

The current YAKINDU Statecharts Tools release only supports statically allocated arrays. Arrays dynamically allocated using malloc() or calloc() will be supported in a later version.

Simulation

During a statechart simulation full access to the C data structures is possible on all layers. The user can inspect them as well as modify them in the simulation view.

The state machine below exemplifies this. Initially it defines two rectangles a and b with certain widths and heights. The state machine calculates the rectangles' respective area size, stores their sizes in two int32_t variables named area_a and area_b, and compares them. Depending on the result, it proceeds to state A is larger or to A is smaller. Only if both a and b have the same area – not necessarily the same width and height –, the state machine proceeds to its final state.

When one of the states A is larger or A is smaller is active, the rectangles' properties can be changed. Triggering the compare_size event transitions to the Check state which repeats the area size comparison as described above.

The rectangle comparison statechart

The rectangle comparison statechart

The state machine’s definitions are as follows:

import: rectangle

interface:
    var a: Rectangle
    var b: Rectangle
    var area_a: int16_t
    var area_b: int16_t

internal:
    event compare_size

The Rectangle datatype is defined in a new header file rectangle.h with the following contents:

#include "./point.h"

typedef struct {
    Point lowerLeft;
    int16_t width, height;
} Rectangle;

In order to simulate the statechart, right-click on the statechart file in the project explorer and select Run As → Statechart Simulation from the context menu.

The statechart simulation

  • starts,
  • performs the initializing actions specified in the transition from the initial state to the Check state,
  • in the Check state, calculates the rectangles' areas and stores the results in the area_a and area_b variables,
  • transitions to the A is larger state, because its guard condition is fulfilled, and
  • stops, waiting for the compare_size event to occur.

Inspecting C data structures

Inspecting C data structures

Inspecting C data structures

The simulation view in the screenshot above is showing the state machine’s variables and their values. Click to open or close the nested data structures. The image below shows in particular

  • the rectangles' respective width and height values as have been set initially,
  • the calculated areas of the a and b rectangles,
  • the coordinates of the points defining the respective lower left corner of the rectangles.

Warning:

Simple C variables and fields in C data structure are not initialized. Never try to read a variable or field you haven’t written before, because it might contain arbitrary values.

Even if the Point data structures in the example above look like having been initialized to defined values, they are not. Without going into details, in C, variables are generally not initialized. This also holds for statechart variables from the C integration. If you are reading a variable, make sure you have written to it before. Otherwise you might get surprising and non-deterministic results.

Modifying C data structures

Change a variable’s or field’s value as follows:

  1. Click on the value displayed in the simulation view.
  2. Enter the new value into the text field, see figure "Modifying C data values" where a.height is being edited and the previous value 7 is replaced by 3.
  3. Press the [Enter] key to quit editing and to write the new value to the variable or field. Giving the input focus to another window has the same effect.
  4. You can cancel editing by pressing the [Esc] key. The variable’s or field’s value remains unchanged.

Modifying C data values

Modifying C data values

In the example, click compare_size to trigger the event. The state machine transitions to the Check state, recalculates the areas, and behaves as explained above.

Rectangle areas modified and rechecked

Rectangle areas modified and rechecked

Looking up a type definition

Given a variable definition in a statechart’s definition section, you can lookup the corresponding type definition. The definition section must be in editing mode, i.e. you must have double-clicked into it. Now press the [Ctrl] key and move the mouse pointer over the type name. The former changes its shape into a hand symbol and the latter changes into a hyperlink:

Looking up a C type

Looking up a C type

Click on the hyperlink to open the header file containing the respective type declaration.

Showing the C type definition

Showing the C type definition

Enums

Besides specifying structure types, it is also possible to declare enumeration types in a C header. Here’s the header file color.h which defines the Color enumeration type:

#ifndef COLOR_H_
#define COLOR_H_

typedef enum {
	RED, GREEN, BLUE, YELLOW, BLACK, WHITE
} Color;

#endif /* COLOR_H_ */

Now let’s extend the Triangle defined above by a fill color:

#ifndef TRIANGLE_H_
#define TRIANGLE_H_

#include "./point.h"
#include "./color.h"

typedef struct {
	Point a, b, c;
	Color fillColor;

} Triangle;

#endif /* TRIANGLE_H_ */


Similar to the Triangle type or any other C type, the Color enumeration type can be used in the statechart, e.g. by declaring an additional interface variable:

import: color
import: triangle

interface:
	var t: Triangle
	var c: Color = Color.BLUE

Please note that in contrast to structured types, enumeration variables can be initialized directly in their definitions.

In order to see which values are available the content assist, triggered by [Ctrl+Space], is helpful again.

Using content assist to select an enumeration value

Using content assist to select an enumeration value

Once initialized, the c variable can now be used e.g. in an assignment to the triangle t's fill color:

t.fillColor = c;

Accordingly, during simulation the values of enum variables are displayed in the simulation view. It is also possible to modify them manually.

Using content assist to select an enumeration value

Using content assist to select an enumeration value

Operations

Functions declared in a C header file become available in a statechart. The state machine can call it as an operation.

Let’s say our rectangle.h header file not only defines the data type, but also declares one or more C functions to operate on them. The following line declares a function named area, taking a Rectangle parameter by value and returning an int32_t result.

extern int32_t area(Rectangle r);

For the sake of the example, let’s assume the function calculates the area of the given rectangle. Of course we could also do this with means built into the statechart language. However, in the general case you neither can nor want to do that.

  • Implementing the functionality in the statechart language might not be possible, because the latter does not provide the necessary means, e.g. to pull some data from an external source.
  • Even if it would be possible to implement the functionality in the statechart language, it might still not be desirable, if the functionality has been developed and fully tested in C already. You will neither want to re-invent the wheel nor would you want to introduce any new errors into an alternative implementation.

YAKINDU Statechart Tools parses function declarations in header files and makes the functions available in the statechart language. It doesn’t care where those functions are defined – or whether they are defined at all – nor what they do. Questions like these will become relevant later when the state machine is generated as C source code, compiled and linked to the functions' implementations.

For now, once the statechart knows about the area function’s declaration in the C header file, the function can be used immediately in statechart language operations. Any operation declaration in the statechart’s definition section is not needed. Example:

Using content assist to enter a C function call

Using content assist to enter a C function call

Here’s the complete example with the area calculations done by the area function:

Example calling the "area" function

Example calling the „area” function

Please note:

State machines calling C functions as operations are debarred from simulation and debugging. The simulator is not yet capable to call C functions.

Generating C source code

Code generation, i.e. turning a statechart model into source code of a programming language, is explained in the section Generating state machine code. Therefore we won’t go into the details here, but instead only put some emphasis on code generation specialties of the Deep C Integration.

Creating a generator model

In the statechart model introduced above, do the following:

  1. In the project view, right-click on the project’s name. The context menu opens.
  2. In the context menu, select New → Code Generator Model. The YAKINDU Generator Model wizard opens.
  3. Enter a filename into the File name field, e.g. c.sgen, and click Next >.
  4. In the Generator drop-down menu, select YAKINDU SCT C Code Generator.
  5. Select the statechart models to generate C source code for. Click Finish.

YAKINDU Statechart Tools creates the following generator model in the file c.sgen:

GeneratorModel for yakindu::c {

    statechart statechart {

        feature Outlet {
            targetProject = "Geometry"
            targetFolder = "src-gen"
            libraryTargetFolder = "src"
        }
    }
}

YAKINDU Statechart Tools creates the target folders src and src-gen and generates the C source representing the statemachine into them.

The generated C code

Particularly interesting are the files Statechart.h and Statechart.c.

Statechart.h first includes the sc_types.h header followed by very same C header files that have been included in the statechart:

#include "sc_types.h"
#include "rectangle.h"

The generated code in Statechart.h then uses the native standard and user-defined C data types. For example, the statechart implementation defines the type StatechartIface as follows:

/*! Type definition of the data structure for the StatechartIface interface scope. */
typedef struct
{
	Rectangle a;
	Rectangle b;
	int32_t area_a;
	int32_t area_b;
	Point p;
} StatechartIface;

By including Statechart.h all definitions are available in Statechart.c, too. For example, a getter and a setter function for the Rectangle variable a are defined as follows:

Rectangle statechartIface_get_a(const Statechart* handle)
{
	return handle->iface.a;
}
void statechartIface_set_a(Statechart* handle, Rectangle value)
{
	handle->iface.a = value;
}

The external area function is called in the entry actions section of state Check:

/* Entry action for state 'Check'. */
static void statechart_enact_main_region_Check(Statechart* handle)
{
	/* Entry action for state 'Check'. */
	handle->iface.area_a = area(handle->iface.a);
	handle->iface.area_b = area(handle->iface.b);
}

Currently supported primitive types

The Deep C Integration natively supports the following primitive C types. That is, in a statechart without any additional data type definitions, the following types are readily available:

  • bool
  • double
  • float
  • int16_t
  • int32_t
  • int64_t
  • int8_t
  • string
  • uint16_t
  • uint32_t
  • uint64_t
  • uint8_t
  • void

Current restrictions

The current release candidate of YAKINDU Statechart Tools PRO is still missing some C functionalities that will be approached as soon as possible by subsequent releases. Among others, the following issues are known to be not available yet:

Type range checks

Type range validations are currently not implemented. As a consequence, it is possible to e.g. assign an int32_t value to an int8_t variable one without any warning.

Plain struct, union, and enum types

In C it is possible to define structs, unions and enums without a typedef. They can be referenced by using the corresponding qualifying keyword ( struct, union, or enum, respectively). As the statechart language does not support these qualifiers, the usage of struct, union and enumeration types is currently restricted to those defined by a typedef.

Please get in touch with us

Please note that the preceding list of restrictions might not be complete. If you discover any further problems, please do not hesitate to contact us! Your feedback is highly appreciated!