IP Protection via Shared-Library-Based S-Function

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

The advantage of using a shared library to protect your IP over a static library is that you may update your shared library without having to recompile your model, because shared libraries are dynamically linked at run-time. In contrast, static libraries are linked during compile-time, thereby requiring a recompilation of the model for any changes in the libraries. For large models or models that are distributed to a third party, avoiding recompilation can be an important consideration.

Example Shared 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 Shared Library

Using the GCC compiler, you can create the shared 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
            !gcc -shared -o libpythagoras.so pythagoras.o

For more information regarding GCC, type man gcc at a shell terminal.

Use Shared 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.so

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. Before you generate code for the model, however, you must:

See Also

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