IP Protection via Static-Library-Based S-Function

This example shows how to generate code for a Simulink® model that contains an S-Function that relies on a static library. Static libraries can encapsulate proprietary algorithms and provide an interface to be used in generated code and Simulink. Using a static library is one technique to protect your model intellectual property (IP). Alternatively, you may use S-Function objects. Shared libraries, or a Simulink® Protected model for IP protection, etc.

Example Static Library

Example Requirements

Given that the model code must run on the SimWB real-time host, which uses a Linux platform, this example focuses on the Linux workflow. You must work through this example on an instance of MATLAB® running on a MATLAB and SimWB-supported Linux platform.

Library Source Code

Suppose that your proprietary library calculates the hypotenuse of a right-angled triangle, given the two sides. The following are the contents of <MLTOOLKITROOT>/examples/use_lib/pythagoras.c.

                #include <stdio.h>
                #include <math.h>
                double hypotenuse(double a, double b){
                    return sqrt(pow(a,2)+pow(b,2));
                }

pythagoras.h is a companion header file for pythagoras.c , provided as an interface to the library. Its contents are as follows:

                #ifndef HYPOTENUSE_H_
                #define HYPOTENUSE_H_
                double hypotenuse(double a, double b);
                #endif

Build Static Library

Using the GCC compiler and the ar tool, you can create the static library for pythagoras.c by executing the following at the MATLAB command prompt:

                % using the bang operator (!) as a shell escape
                !gcc -c -o pythagoras.o pythagoras.c -fPIC
                !ar rsv libpythagoras.a pythagoras.o

For more information regarding these commands, type man gcc or man ar at a shell terminal.

Use Static Library in Simulink Model via S-Function

Create S-Function

Write an S-Function that uses the hypotenuse method of the library. For an example, see <MLTOOLKITROOT>/examples/use_lib/use_lib.c. Create a MEX-file for simulation using the following command at the MATLAB command prompt:

                mex use_lib.c libpythagoras.a

The model <MLTOOLKITROOT>/examples/use_lib/ccur_calc_hyp.mdl (developed in MATLAB version 7.11 (R2010b)) contains an S-Function block that uses the use_lib S-Function. Constant blocks (SW_a, SW_b) supply the required inputs and the output can be viewed via the Display block (SW_out). You can run this model in Simulink® for different values of SW_a and SW_b to verify the algorithm behavior.

Distribution Files

To distribute this S-Function to a third party, you must provide your user with the following files. Notice that the list excludes the pythagoras.c file, which contains the IP to be protected.

Create RTDB and Generate Code

The process to create the RTDB and generate SimWB-compliant code for this model is described in this example.

Note: To ensure that the static library is ported to the real-time host along with the model source files, you must modify the Simulink model's Configuration Parameters > Code Generation > Custom Code > Include list of additional > Libraries list to include libpythagoras.a.

See Also

Simulink®, developed by MathWorks, is a graphical modeling environment.