Main Content

Define Callbacks for Protected Models

When you create a protected model, you can customize its behavior by defining callbacks. Callbacks specify code that executes when you view, simulate, or generate code for the protected model. For instance, you can implement callbacks to control usage of protected models. You cannot have protected model callbacks with HDL code generation support enabled for a protected model. To learn more about HDL code generation limitations, see Protected Model Restrictions for HDL Code Generation (HDL Coder).

A protected model user cannot view or modify a callback. If a model references a protected model with callbacks, you cannot protect the model.

To create a protected model with callbacks:

  1. Define Simulink.ProtectedModel.Callback objects for each callback.

  2. To create your protected model, call the Simulink.ModelReference.protect function. Use the 'Callbacks' option to specify a cell array of callbacks to include in the protected model.

Creating Callbacks

To create and define a protected model callback, create a Simulink.ProtectedModel.Callback object. Callback objects specify:

  • The code to execute for the callback. The code can be a character vector of MATLAB® commands or a script on the MATLAB path.

  • The event that triggers the callback. The event can be 'PreAccess' or 'Build'.

  • The protected model functionality that the event applies to. The functionality can be 'CODEGEN', 'SIM', 'VIEW', or 'AUTO'. If you select 'AUTO', and the event is 'PreAccess', the callback applies to each functionality. If you select 'AUTO', and the event is 'Build', the callback applies only to 'CODEGEN' functionality. If you do not select a functionality, the default behavior is 'AUTO'.

  • The option to override the protected model build process. This option applies only to 'CODEGEN' functionality.

You can create only one callback per event and per functionality.

Defining Callback Code

You can define the code for a callback by using either a character vector of MATLAB commands or a script on the MATLAB path. When you write callback code, follow these guidelines:

  • Callbacks must use MATLAB code (.m or .p).

  • The code can include protected model functions or a MATLAB command that does not require loading the model.

  • Callback code must not call out to external utilities unless those utilities are available in the environment where the protected model is used.

  • Callback code cannot reference the source protected model unless you are using protected model functions.

You can use the Simulink.ProtectedModel.getCallbackInfo function in callback code to get information on the protected model. The function returns a Simulink.ProtectedModel.CallbackInfo object that provides the protected model name and the names of submodels. If the callback is specified for 'CODEGEN' functionality and 'Build' event, the object provides the target identifier and model code interface type ('Top model' or 'Model reference').

Create a Protected Model with Callbacks

This example creates a protected model with a callback for code generation.

  1. On the MATLAB path, create a callback script, pm_callback.m, containing:

    s1 = 'Code interface is: ';
    cbinfobj = Simulink.ProtectedModel.getCallbackInfo(...
    'sldemo_mdlref_counter','Build','CODEGEN');
    disp([s1 cbinfobj.CodeInterface]);
    
  2. Create a callback that uses the script.

    openExample('sldemo_mdlref_basic');
    pmCallback = Simulink.ProtectedModel.Callback('Build',...
    'CODEGEN', 'pm_callback.m');
    
  3. Create the protected model and specify the code generation callback.

    Simulink.ModelReference.protect('sldemo_mdlref_counter',...
    'Mode', 'CodeGeneration','Callbacks',{pmCallback})
  4. Build the protected model. Before the build, the callback displays the code interface.

    slbuild('sldemo_mdlref_basic')

Control Usage of Protected Models with Callbacks

You can define callbacks for protected models to control their usage, for instance by setting a model expiration date.

  1. On the MATLAB path, create the following callback script and save it in a file named pm_callback.m.

    if datetime("now") > datetime('11-Mar-2024')
        set_param(gcs,'SimulationCommand','stop');
        disp('Protected model has expired.');
    end

    The script uses the system time on your computer to control access.

  2. Create the callback object that uses the script.

    pmCallback = Simulink.ProtectedModel.Callback('PreAccess','Sim','pm_callback.m');
  3. Create the protected model and specify the code generation callback.

    Simulink.ModelReference.protect('sldemo_mdlref_counter','Callbacks',{pmCallback});
  4. Simulate the model sldemo_mdlref_basic that references the protected model you created.

    openExample('sldemo_mdlref_basic');
    sim('sldemo_mdlref_basic');
    Protected model has expired.
    Protected model has expired.
    Protected model has expired.

See Also

| |

Related Topics