IP Protection via Distribution of S-Function Object File

This example shows how to generate code for a Simulink model that contains an S-Function for which you have the object file. Using the S-Function object file is one technique to protect your model intellectual property (IP). Alternatively, you may use shared libraries, static libraries, or a Simulink® Protected model for IP protection, etc.

Example S-Function

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.

Example Subsystem/S-Function

In this example, we use the Simulink® Coder feature that generates an S-Function out of a subsystem. The model used is <MLTOOLKITROOT>/examples/pythagoras.slx. This model was created using MATLAB 8.2 (R2013b). The steps from this example were also verified in MATLAB 9.1 (R2016b).

Vendor-side Workflow

Requirements

  1. A machine or virtual machine that uses either the same or an older distribution of Linux as the SimWB server. (RedHawk is not required.)

    To check the Linux distribution installed on the SimWB server, execute the following at a terminal: lsb_release -a
    The end-user must provide the output of these commands to the vendor, so they can prepare their system appropriately.

    Some options for creating a virtual machine for free are as follows:

  2. MATLAB / Simulink on the machine / virtual machine. Use the same version of MATLAB as the end-user.

Steps

  1. Open the model <MLTOOLKITROOT>/examples/pythagoras.slx.
  2. Right-click the subsystem calc_hypotenuse and select C/C++ > Generate S-Function.
  3. A dialog opens to pick tunable parameters. Check the Tunable box for myGain.
  4. Click Build.
  5. A new model opens, containing the generated S-Function. Look inside the mask of this subsystem. You will see a green subsystem called calc_hypotenuse_sfcn.
  6. Replace the calc_hypotenuse subsystem in the pythagoras model with the newly generated calc_hypotenuse_sfcnsubsystem.
  7. Double-click the green block and change the value of the myGain parameter from sfcnParam1 to myGain (MATLAB base workspace variable with value 10).
  8. Run the model to ensure the values are as expected.
  9. For the pythagoras model, open the Configuration Parameters dialog. Click Code Generation. Set System target file to grt.tlc.
  10. Type Ctrl + B to generate code for the model.
  11. At this point, you have to package a small subset of files. The SimWB command packSFuncIP bundles the necessary files and creates a zipped file. For more information on packSFuncIP, type the following at the MATLAB command prompt: help packSFuncIP.
    Execute the following at the MATLAB command prompt: packSFuncIP('pythagoras','calc_hypotenuse'); A file called pythagoras_distributable.zip will be created in the current work directory. The vendor provides this zipped file to the end-user. The file does not contain the source code for S-Function, but instead packages:

End-user-side Steps

Upon receiving the pythagoras_distributable.zip file, use the following steps to simulate the S-Function in Simulink and generate SimWB-compatible code for the model.

  1. Create a new folder and unzip the contents of pythagoras_distributable into this folder.
  2. Create a copy of the pythagoras model called test.
  3. Open the test model and run it to verify that it simulates as expected.
  4. For the test model, open the Configuration Parameters dialog. Select Code Generation > Custom Code > Libraries. Add the following entry (note the leading period to indicate a relative path): ./pythagoras_grt_rtw/calc_hypotenuse_sf.o
  5. Execute the following at the MATLAB command prompt: !touch calc_hypotenuse_sf.c !touch pythagoras_grt_rtw/calc_hypotenuse_sf.o
  6. Create the RTDB and generate SimWB-compliant code for the test model as described in this example.

See Also

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