CAPL is an acronym for Communication Access Programming Language, which is a programming language used in Vector testing tools chain. It is used to create:
Simulation network nodes
Measurement, analysis components
Testing modules
*NOTE: All of the images in this article are captured from Vector CANoe tool and their example projects.
Some characteristics of CAPL script are:
C like syntax.
Event-driven (individual functions(evaluate, set signal values, send messages) that react to received messages, expired timers event, change of a signals or system variables in the environment).
CAPL program can be developed(.can, .cin) and compiled(.cbf) using Vector tool CAPL Browser(IDE).
Direct access to messages, signals, system variables and diagnostic parameters(via database file such as DBC, CDD, ODX-d) because CAPL Browser is integrated with CANoe,CANalyzer tool chain.
Can link to user dll(e.g. diagnostic operation: encryption API, security key/seed calculation).
.can: is execution code and compiled .cin: is for common/library function, and shall be included by .can
I. Program Structure
Sample CAPL script - simulation node that monitor CAN message and update the sysvar value to be displayed on CANoe Panel
Above sample script shall be stored in a *.can file, which then shall be complied with CAPL Compiler into executable file in CBF(CAPL Binary Format *.cbf). For a test node, main section of CAPL test script are:
I.1 include
Include files contain arbitrary but complete sections of a CAPL program: includes, variables and functions. All variables and functions that are included via the Include files will be available as global variables and functions.
The sequence of inclusion is irrelevant. The compiler reports any duplicate symbols as an error between included files and between included files and the main program. Code and data from included and parent files may use each other mutually.
One exception to the prohibition of duplicate symbols is that on start, on preStart, on preStop and on stopMeasurement may coexist in both the included file and the parent file. In these functions, the code is executed sequentially: first the code from the included file and then the code from the parent file. This means that the Include files are used to perform three tasks: declare data types, define variables and provide an (inline) function library.
I.2 variable
CAPL program variables can be declared in each function as local variable or under variables section as global variables.
NOTE: CAPL only allow declare local variable at the beginning section of the function.
Struct, array, associative field, object, when used as function parameter will be passed as reference by default.
Other primitive data type has to used & to mark that it shall be passed as reference. E.g. void Function(byte& ref); Function(var);, and implicit conversion, e.g., from byte to long, is not possible.
Some C concept, function such as align, memcpy, memcmp are also available and greatly help the testing operation(e.g. parse datastream to/from data structure) for user defined data type. > memcpy, memcpy_h2n, memcpy_n2h, memcpy_off, memcmp > _align(1) struct streamListener{...}, __size_of(struct streamListener), __alignment_of(struct streamListener), __offset_of(struct streamListener, sink_ID)
sysvar or System Variables are defined under tab Environment/System Variables, it can be accessed everywhere in CANoe configuration. We define user sysvar to shared, route data around testing environment between test nodes, simulated nodes, measurement nodes, CANoe panel.
CANoe also provide sysvar so test nodes can interact with other component in a testing environment such as VT System.
signal representation of the bus signals. Access to the signals is carried out with the syntax $signal, in addition you can specify the read/write operation either be raw or phys $signal.raw, $signal.phys
keep in mind, signal value does not change immediately, only after the signal is being transmitted again on the network then it can be said to be updated, and read out value is the last value transmitted on the network.
CAPL ECU(CANoeIL) is a restbus simulation node(generated from DBC file, in which message/signals information of each nodes in the network are defined). It shall stimulate the message transmission on the bus, which we can update message signal value in test node and create stimulated input for the DUT.
In case of your configuration is created manually, no CANoeIL simulation node and either with or without DBC, you have to implement the callback functions as a signal transmission/receive driver by your own if you want to manipulate signal as below.
I.3 on <event>
Except for the testcases, which can be called on test execution, all of the CANoe nodes are operated base on event processing. In a test nodes most of the time, you don’t need to define the event handler, because CANoe provide a very well defined API libraries, that we can use to create a sequential testing operation (e.g. TestWaitFor* APIs to wait for certain event to occur).
But some time, you might need access to a lower layer level of data, which can not be provided by CANoe APIs or you just want to create a custom procedure to filter event, in that cases define these on <event> handle and a notification mechanism(e.g. using sysvar, text event notification) will help you a lot during testing process.
Above is a sample on how to create/handle your event and integrate it into your testing sequence.
I.4 functions
Syntactically, function definition and usage in CAPL is the same as C. Beside that CAPL provides some advanced programming mechanism such as:
Overloading of functions (i.e. multiple functions with the same name but different parameter lists).
Function delegate: Instead of passing the name of a CAPL function explicitly defined elsewhere in the program as a parameter to a predefined function (e.g. consumedMethodRef::CallAsync), you can also define a small function directly at the position of the parameter. This function has no name and is called a delegate (in other programming languages you will also find the terms lambda expression or anonymous function).
A test function/control function is a predefined test procedure that is parameterized with concrete parameter values for execution. These are the most frequently used test procedures that are occupied with values in an XML test module and brought to execution. The execution of the function is noted in the test report and eventually leads to a verdict change of the surrounding test case.
CAPL also come with a library of predefined(intrinsic) functions. For the selection of a predefined function, the CAPL browser makes available the CAPL Function Explorer.
I.5 testcases
Above is a sample testcase structure
testcase description and traceability information
testcase precondition setup
test steps
testcase post-run clean up
These testcase can be lated called by CAPL test node MainTest or XML test node.
II. Execution Concept
A key difference between CAPL and C or C++ relates to when and how program elements are called. In C, for example, all processing sequences begin with the central start function main(). In CAPL, on the other hand, a program contains an entire assortment of procedures of equal standing, each of which reacts to external events.
Above is a typical test environment CANoe configuration. 1. Simulated peer node: generated directly from input database with add-on “Model Generation Wizard” tool or can be created manually. It provide signal simulation base on database via provided CANoeIL DLL. 2. User define test node: this is where we implement test script, trigger test signal via simulated peer node signal driver(CANoeIL) and CANoe VT system. Then verify response of ECU via its feedback signal on the bus or measurement provided by VT system.
For stimulation and examination purposes the test modules/test units can access
the complete remaining bus simulation
the connected buses (e.g. CAN, LIN, FlexRay, IP)
the digital and analog input and output lines of the Device Under Test using general purpose I/O cards or VT System via system variables
other external real time systems (e.g. HIL systems and LabVIEW models) using the FDX interface
other external measurement systems (e.g. GPIB and Ethernet) using appropriate interfaces
An ECU may be tested by itself, or as part of a network consisting of various ECUs where the object of testing is called a System Under Test or SUT. CANoe’s options are available to the test modules/test units , e.g. panels for user interaction or writing of outputs to the Write Window.
III. Actual Project Example
Refer to following example github repos, it contain a CANoe simulation configuration (base on CANoe sample UDS config - to reuse the diagnostic database because without license you can not create new .cdd file and on the other hand I don’t have access to ODX format standard specification either):
DUT ECU - Door: simulated real ECU in a project
SGateway: simulated peer node ECU, which is defined in network description dbc.
Door.cdd: diagnostic description database file
NwSample.dbc: CAN database for CAN network simulation.
Normally in a project, we should have a network dbc file, where relation between target developed ECU and other neighbor ECUs is defined.
Target developed ECU shall be our DUT ECU.
Other neighbor ECUs shall be simulated by CANoe(peer node) to provide stimulated inputs.
Hardware configuration VN hardware device and bus characteristic.
Another input for testing environment is diagnostic description database file either in Vector defined .cdd or ODX standard format(.odx-d, .pdx). ⟹ These database file shall be imported to CANoe tool and help with test script implementation.
If your test setup have VT system, some additional configuration for it is needed as well.
During CAPL test script development, you can also debug CAPL as below.
In general, CAPL provide a lot of CANoe environment specific supported APIs(create stimulation, validation, and report) but for complex automation action, data processing such as: directory, file manipulate, XML/json parsing, cryptography operation,... Higher level programming language with rich libraries support such as C# .NET should be preferred. My suggestion for optimal setup of CANoe automation test environment is: - XML test node: easier for management/display/control during testing (regression test, manual/automation,...) - CAPL script: core implementation of testcases/testfunction to be called in XML test node, due to well supported/native Vector CANoe test APIs - C# .NET user library .DLL: can be easy implemented, built into .DLL and included to/called by main CAPL script, help expand automation functionalities. Ease the verification step in case of dealing with complex data and environment manipulation, which is very much limited with CAPL.