I am designing a subsystem model (a child subsystem or a referenced model). I would like to share this with others, who may include the child subsystem into their models (parent model). If they are using the setup for simulation, I can send them a protected model (child.slxp) of the child subsystem, which they can insert in the parent model. However, if they are looking to generate code from the parent model, I would like to know what format I can send them the subsystem design in so that the contents are not visible?
MATLAB: How to protect the IP of the Simulink model when sharing it with others, who may include it in their models and generate code from them
inliningipmodelprotectprotections-functionsimulink coder
Related Solutions
You can use the following four approaches to protect the intellectual property of a Simulink model:
- Protected Model
- S-Function Target
- Shared Library
- Read/Write Access for Subsystems
Note that for approaches 1, 2 & 3, the receiving party must match the platform. For example, if a protected model has been created on Windows, it cannot be run by the third party if they use Linux or macOS.
1. Protected Model
The preferred way to share a Simulink model without revealing its contents is to use protected models. The third party receiving the protected model (.SLXP file) can reference it from their Simulink model, without being able to edit the contents. This feature is available from R2014a onwards. Note that creating a protected model requires a Simulink Coder license. For more information, see:
The protected model approach has many advantages:
- Easy to use. The protected model offers a very familiar interface to someone that is used to working with model references.
- AES-256 encryption can be applied to the file contents by enabling password protection.
- If enabled by the author, the third party can generate code from the protected model.
- The author can choose to share a read-only version (web view) of the protected model.
- From R2018b onwards, protected models are forward compatible.
- From R2020a onwards, you can attach a digital signature to a protected model.
2. S-Function Target
By using the S-Function target, you can generate S-Function code from your model or subsystem, and then build it into a MEX file. This used to be the preferred way to conceal model contents in R2013b and earlier. Note that generating such an S-Function also requires a Simulink Coder license. For more information, see:
The main drawback compared to protected models is that the S-Function target has no built-in option to apply encryption. However, if we don't consider encryption, both the protected model and the S-Function will provide the same level of protection.
3. Shared Library
Using the shared library system target file (ert_shrlib.tlc), you can generate a universal shared library (.DLL/.SO) for a model or subsystem. Note that creating a shared library requires an Embedded Coder license on top of a Simulink Coder license. For more information, see:
The main advantage of this approach is that a shared library can be run in a system simulation outside of Simulink.
4. Read/Write Access for Subsystems
This is the easiest of the methods listed and requires no additional license or matching platform. By setting the read/write permissions in the Subsystem block parameters to 'NoReadOrWrite', nothing will happen when a user double clicks on it. For more information, see the section "Control Access to Subsystems" on:
However, the permission can be changed by the user, so this is just a superficial level of protection and will not work if the contents need to stay protected.
The crash occurs in this case, because a necessary value stored in PWork was not initialized in the compiled S-Function. The initialization was performed in the mdlStart function of the original S-Function. However, mdlStart for this subsystem is never executed in the new S-Function, as a result of how the child S-Function is incorporated when generating the new S-Function from the parent model.
An S-Function may be either an inlined or a noninlined S-Function. The following explains the differences in using an inlined or noninlined child S-Function when generating code from a parent model:
a) Inlined S-Function:
In this case, the code for the parent model is combined with code for the child block into a single MEX-file. This MEX-file defines the behavior of both the parent model and the child block. However, the code used for the child block is NOT the same source code that was used to make the MEX-file for the child S-Function. Instead, this code is produced by the child block's TLC file. An S-function TLC file governs how RTW emits code for the S-function block.
This does not work in this case, because the TLC file did not define code to initialize the pointer used by the childname_Outputs_wrapper function.
b) Noninlined S-Function
In this case, code generation results in a MEX-file for the parent model. However, the child block's functionality is still defined by the MEX-file of the child S-Function and is called from the parent's MEX-file.
This works for your model, because the child S-Function's MEX-file performs the necessary initialization of the PWork data.
When using the rtwsfcn target, the presence or absence of a child.tlc file determines whether or not the child will become an inlined S-Function.
The following documents describe these two types of S-Functions and some of the tradeoffs involved in selecting either inlined or noninlined S-functions.
To clarify, if the new S-Function is generated with the child as a noninlined S-Function, the simulation will call the mdlInitializeSizes, mdlInitializeSampleTimes, mdlStart, mdlOutputs, etc. functions inside the child.mexw32 file. However, this MEX-file is not used if the child is incorporated as a noninlined S-Function. Instead, the child.tlc file is used to determine the code that is executed when each of these functions are called.
You can resolve this issue using either of following three options:
1) Use model referencing instead of generating an S-Function in order to incorporate parent.mdl in a larger simulation. This is the recommended way to reuse models in multiple simulations. You can read more about model referencing in the following documents:
2) If you would like to generate an S-Function from parent.mdl, the quickest method is to remove or rename the child.tlc file and then generate code. The child S-Function will be incorporated as a noninlined S-Function.
A benefit of using noninlined S-functions is that you do not have to write TLC code, however, non-inlined S-Functions are not suitable for embedded code.
If you remove the TLC file, you will notice that the following additional code
SimStruct *rts = ssGetSFunction(S, 0);
sfcnStart(rts);
if (ssGetErrorStatus(rts) != NULL)
return;
is generated within the mdlStart function defined in parent_sf.c. This code ensures that the mdlStart functions of all the S-Function blocks in your model will be executed. In your case, this will find and call the mdlStart function within the child.mexw32 file.
3) If you would like to use the child S-Function as in inlined S-Function, you will have to modify the child.tlc file to include all the custom functionality that you would like to execute in mdlStart and mdlOutputs. In addition, inlined S-functions do not allocate any Simstruct structures, therefore you cannot use work vectors with inlined S-Functions. Instead of storing the address to the communications port in PWork, you will have to create a global variable to store this. You will also have to modify childname_Outputs_wrapper to access this global variable instead of the PWork inside the SimStruct.
Related Question
- How to prepare a model containing an S-function to be referenced by another model
- What source file names should be included in the “sources” field when creating the rtwmakecfg.m function file to include custom source file information for the S-functions in the Real-Time Workshop build process
- Model Reference Target Update Error
- How to build the model that contains an automatically generated S-function, if I do not have the source code
- Code Gen with Embedded coder
Best Answer