simulink를이용한 효율적인레거시코드...and unit design s-functions from handed c/c++...

34
1 © 2015 The MathWorks, Inc. Simulink를 이용한 효율적인 레거시 코드 검증 방안 류성연

Upload: others

Post on 07-Jul-2020

0 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Simulink를이용한 효율적인레거시코드...and unit design S-functions from Handed C/C++ code Coverage analysis Unit(/Integration) testing Code review and Static analysis

1© 2015 The MathWorks, Inc.

Simulink를이용한효율적인레거시코드검증방안

류성연

Page 2: Simulink를이용한 효율적인레거시코드...and unit design S-functions from Handed C/C++ code Coverage analysis Unit(/Integration) testing Code review and Static analysis

2

Agenda

▪ Overview to V&V in Model-Based Design

▪ Legacy code integration using Simulink

▪ Workflow for legacy code verification

Page 3: Simulink를이용한 효율적인레거시코드...and unit design S-functions from Handed C/C++ code Coverage analysis Unit(/Integration) testing Code review and Static analysis

3

Model-Based Design With Legacy C/C++ Code?

Hand Coding Full MBD

Page 4: Simulink를이용한 효율적인레거시코드...and unit design S-functions from Handed C/C++ code Coverage analysis Unit(/Integration) testing Code review and Static analysis

4

Why Using Simulink for Legacy Code Testing?

Input

Display

Algorithm

Simulink

Simulink

StateflowMATLAB

Function

C/C++

Vehicle Dynamics Blockset(R2018a)

Page 5: Simulink를이용한 효율적인레거시코드...and unit design S-functions from Handed C/C++ code Coverage analysis Unit(/Integration) testing Code review and Static analysis

5

ISO26262 “Road Vehicles - Functional Safety”

▪ Functional safety standard for passenger cars

– Concerned with avoidance of unreasonable

risks due to hazards caused by E/E systems

– Recommends tool certification, but offers little guidance

▪ Serves as an umbrella standard for industry specific adaptions

including:

– ISO 26262 - Automotive

– EN 50128 - Rail

– IEC 62304 - Medical

– IEC 61511 - Process Control

Page 6: Simulink를이용한 효율적인레거시코드...and unit design S-functions from Handed C/C++ code Coverage analysis Unit(/Integration) testing Code review and Static analysis

6

Software Development Workflow for Embedded Applications

Textual

Requirements

Executable

Specification

Modelling

Object

code

Compilation

and Linking

Generated

C/C++ code

Code

Generation

Model used for

production code

generation

Requirements

Authoring

Software architecture

and design

Handwritten

C/C++ code

Requirements Trace

Documentation

Version Control

Tool Qualification

Page 7: Simulink를이용한 효율적인레거시코드...and unit design S-functions from Handed C/C++ code Coverage analysis Unit(/Integration) testing Code review and Static analysis

7

Legacy Code Verification Overview

Textual

Requirements

Executable

Specification

Modelling

Object

code

Compilation

and Linking

Generated

C/C++ code

Code

Generation

Model used for

production code

generation

Requirements

Authoring

Software architecture

and unit design

S-functions from

Handed C/C++ code

Coverage analysis

Unit(/Integration) testing

Code review and

Static analysis

/ Test case generation

For Model-Based Design

For legacy code development

Page 8: Simulink를이용한 효율적인레거시코드...and unit design S-functions from Handed C/C++ code Coverage analysis Unit(/Integration) testing Code review and Static analysis

8

Agenda

▪ Overview to V&V in Model-Based Design

▪ Legacy code integration using Simulink

▪ Workflow for legacy code verification

Page 9: Simulink를이용한 효율적인레거시코드...and unit design S-functions from Handed C/C++ code Coverage analysis Unit(/Integration) testing Code review and Static analysis

9

How to Import Legacy Code

▪ Legacy Code Tool

▪ C Caller Block

▪ Legacy code integration in Stateflow

Page 10: Simulink를이용한 효율적인레거시코드...and unit design S-functions from Handed C/C++ code Coverage analysis Unit(/Integration) testing Code review and Static analysis

10

What legacy C code integration in Simulink means?

▪ Legacy Code Tool enables existing C code to be used in Simulink models

External C

Functions

Generating

S-function

Page 11: Simulink를이용한 효율적인레거시코드...and unit design S-functions from Handed C/C++ code Coverage analysis Unit(/Integration) testing Code review and Static analysis

11

How to use Legacy Code Tool?

▪ General procedure for using Legacy Code Tool

S-function block

Modeling with S-Function

Page 12: Simulink를이용한 효율적인레거시코드...and unit design S-functions from Handed C/C++ code Coverage analysis Unit(/Integration) testing Code review and Static analysis

12

Prerequisite to use Legacy Code Tool

▪ What is wrapper code?

– Root-level C function having in/output variables for S-Function block’s in/out ports

double doubleit(double u1)

{

MainInp = u1;

double_main();

y1 = MainOutp;

return y1;

}

void doubleit(double u1, double *y1)

{

MainInp = u1;

double_main();

*y1 = MainOutp;

}

or

Main function of

legacy code

Wrapper

code

Page 13: Simulink를이용한 효율적인레거시코드...and unit design S-functions from Handed C/C++ code Coverage analysis Unit(/Integration) testing Code review and Static analysis

13

MATLAB Script to Build and Generate S-Function Block

▪ m-script file: compiling C files and generate a S-Function Block

Simulink.importExternalCTypes('ex_myTypes_LCT.h');

def = legacy_code('initialize');

def.SFunctionName = 'sfun_ex_mySrc_LCT';def.SourceFiles = {'ex_mySrc_LCT.c'};def.HeaderFiles = {'ex_myTypes_LCT.h'};

def.OutputFcnSpec = 'void myFcn(sigStructType u1[1], paramStructType p1[1], sigStructType y1[1])';

def.IncPaths = {'rtwdemo_lct_src'}; def.SrcPaths = {'rtwdemo_lct_src'};

legacy_code('sfcn_cmex_generate', def);

legacy_code('compile', def);

legacy_code(‘slblock_generate', def);

① C files to integrate in Simulink

② S-Function block

specification

③ Include folders

④ Compile and s-function generation

https://www.mathworks.com/help/releases/R2018a/simulink/sfg/integrating-existing-c-functions-into-simulink-models-with-the-legacy-code-tool.html?s_tid=doc_srchtitle

Page 14: Simulink를이용한 효율적인레거시코드...and unit design S-functions from Handed C/C++ code Coverage analysis Unit(/Integration) testing Code review and Static analysis

14

Generate Simulink Representations from C or C++ Code

▪ Import external C header file and generate available Simulink data types

Simulink.importExternalCTypes('ex_myTypes_LCT.h’);

Automatically generating

to Simulink Bus

Selecting generated

Simulink Bus

Page 15: Simulink를이용한 효율적인레거시코드...and unit design S-functions from Handed C/C++ code Coverage analysis Unit(/Integration) testing Code review and Static analysis

15

Issues for Legacy Code Tool

▪ There are still technical challenges to make S-Function Block

Conventional C code

verification

C code verification

with Simulink

• Difficult to build test cases

• Limited input variation

• Long lead time from development to test

• Hard work to improve test results

• No unified interfaces to interact with legacy code

• Hard to build S-Function Block

• No auto sync with custom C code change

• Still maintenance problem

Legacy Code Tool

Page 16: Simulink를이용한 효율적인레거시코드...and unit design S-functions from Handed C/C++ code Coverage analysis Unit(/Integration) testing Code review and Static analysis

16

Example Issue: Too Many Function Arguments

Legacy code

Wrapper code

Script file

- Too many interface variables

- Nested structure

- Bitfield

- etc.

Page 17: Simulink를이용한 효율적인레거시코드...and unit design S-functions from Handed C/C++ code Coverage analysis Unit(/Integration) testing Code review and Static analysis

17

Maintenance Problem…

Legacy codeWrapper code

Modeling

Script file

Page 18: Simulink를이용한 효율적인레거시코드...and unit design S-functions from Handed C/C++ code Coverage analysis Unit(/Integration) testing Code review and Static analysis

18

Introducing C Caller Block

C Caller Block makes it easier to call C Functions in Simulink

It works for simulation and Code Generation

Page 19: Simulink를이용한 효율적인레거시코드...and unit design S-functions from Handed C/C++ code Coverage analysis Unit(/Integration) testing Code review and Static analysis

19

▪ Automate the process

▪ Synchronize with custom code changes

Key Features

C/C++ Code

functions*

types *

globals

Define Block

Interface

Build Simulation

MEX

Write Codegen

TLC

Automate

• Tedious

• Error prone

• Hard to maintain

Page 20: Simulink를이용한 효율적인레거시코드...and unit design S-functions from Handed C/C++ code Coverage analysis Unit(/Integration) testing Code review and Static analysis

20

Using C Caller Block

1. Specify Custom Code in the Configuration Parameters

▪ Custom code is specified on the Configuration Parameters.

– The Header file section: Any code that needs to be inserted into the header file

– The Source files section: List of source files that needs to be compiled

include custom header file

add custom source file

Page 21: Simulink를이용한 효율적인레거시코드...and unit design S-functions from Handed C/C++ code Coverage analysis Unit(/Integration) testing Code review and Static analysis

21

Using C Caller Block

2. Select the function that you want to call

Page 22: Simulink를이용한 효율적인레거시코드...and unit design S-functions from Handed C/C++ code Coverage analysis Unit(/Integration) testing Code review and Static analysis

22

Using C Caller Block

3. Customize the function that you want to call

▪ Mapping inputs, outputs or parameters to C Caller Block

1) Change argument

scope to “Output” 2) (Optional) Override with a

better output name

3) Complete the test model with connecting signal ports

Page 23: Simulink를이용한 효율적인레거시코드...and unit design S-functions from Handed C/C++ code Coverage analysis Unit(/Integration) testing Code review and Static analysis

23

Demo: Simple C Caller

Page 24: Simulink를이용한 효율적인레거시코드...and unit design S-functions from Handed C/C++ code Coverage analysis Unit(/Integration) testing Code review and Static analysis

24

Library Workflow

include custom header file

add custom source file

▪ C Caller block can be configured as a library model

– Custom Code Settings can be accessed from View Menu Library Custom Code Settings

Page 25: Simulink를이용한 효율적인레거시코드...and unit design S-functions from Handed C/C++ code Coverage analysis Unit(/Integration) testing Code review and Static analysis

26

Demo: Reusable Library Workflow with OpenCV

Page 26: Simulink를이용한 효율적인레거시코드...and unit design S-functions from Handed C/C++ code Coverage analysis Unit(/Integration) testing Code review and Static analysis

27

Legacy Code Evaluation in Stateflow

▪ Using legacy code in Stateflow chart

Step 1: Have C code Step 2: Put on Config. Set Step 3: Use in Stateflow

Page 27: Simulink를이용한 효율적인레거시코드...and unit design S-functions from Handed C/C++ code Coverage analysis Unit(/Integration) testing Code review and Static analysis

28

Agenda

▪ Overview to V&V in Model-Based Design

▪ Legacy code integration using Simulink

▪ Workflow for legacy code verification

Page 28: Simulink를이용한 효율적인레거시코드...and unit design S-functions from Handed C/C++ code Coverage analysis Unit(/Integration) testing Code review and Static analysis

29

Legacy Code Verification using Simulink V&V

Test harness model

S-Function or C Caller

Code coverage analysis

Test Cases

Test case

generation

Page 29: Simulink를이용한 효율적인레거시코드...and unit design S-functions from Handed C/C++ code Coverage analysis Unit(/Integration) testing Code review and Static analysis

30

Demo: Legacy C Code Verification

Page 30: Simulink를이용한 효율적인레거시코드...and unit design S-functions from Handed C/C++ code Coverage analysis Unit(/Integration) testing Code review and Static analysis

31

Needs for Test Automation

Test harness model

• Test automation/management

• Code coverage analysis

• Function/Function call coverage

• Report generation

Page 31: Simulink를이용한 효율적인레거시코드...and unit design S-functions from Handed C/C++ code Coverage analysis Unit(/Integration) testing Code review and Static analysis

32

Test Automation with Test Manager

• Test automation

• Test case creation from template

• Customization

• View, share, report results

Page 32: Simulink를이용한 효율적인레거시코드...and unit design S-functions from Handed C/C++ code Coverage analysis Unit(/Integration) testing Code review and Static analysis

33

Static Code Analysis

• Run time error / MISRA rule check

• Polyspace report from Simulink

• Reducing Polyspace set-up efforts

Page 33: Simulink를이용한 효율적인레거시코드...and unit design S-functions from Handed C/C++ code Coverage analysis Unit(/Integration) testing Code review and Static analysis

34

Key Takeaways

Textual

Requirements

Executable

Specification

Modelling

Object

code

Compilation

and Linking

Generated

C/C++ code

Code

Generation

Model used for

production code

generation

Requirements

Authoring

Software architecture

and unit design

C Caller for

legacy code

Coverage analysis

Unit(/Integration) testing

Review and

Static analysis

/ Test case generation

Simulink Requirement

Simulink Test

SL Requirement

Simulink Design Verifier

Simulink Coverage

Simulink Simulink

Simulink Coder

Embedded Coder

Polyspace

Page 34: Simulink를이용한 효율적인레거시코드...and unit design S-functions from Handed C/C++ code Coverage analysis Unit(/Integration) testing Code review and Static analysis

36

Thank You!