Main Content

Service Interfaces Overview

When your applications or software components need to communicate with each other through a functional interface, use service-oriented communication where services are provided and requested by different components depending on the target platform. You can use service-oriented architectures to decide the implementation of a function in your component after deployment. A service-oriented architecture allows you to think about your components as micro-services that enable powerful capabilities such as over-the-air updates and cloud functionality. System Composer allows you to model these scenarios using service interfaces.

You can model your service-oriented communication at the architecture level using software architectures in System Composer™. Starting at the architecture level allows you to design your service interface and components independent of the implementation of their behavior. Ports and interfaces in an architecture describe 'what' the system should do and implementation of behavior in linked Simulink® models describe 'how' the component should do it.

If you start from the architecture level, you can use these methods to define Simulink behavior for your components:

  • Use a preconfigured Simulink template.

    System Composer can provide you with preconfigured Simulink template.

  • Manually model your behavior in Simulink and attach the model to your software architecture components. For more information, see Model Client-Server Communication Using Function Ports.

This topic shows how to model service-oriented communication starting from a System Composer software architecture and implementing component behavior with Simulink.

Structure of Service-Oriented Architectures

Software architecture called myClientServerArchitectureModel with two client components and a server component.

A service-oriented architecture consists of these elements:

  • A software component is a specialization of a component for software entities, including its interfaces.

    • A server is a component that defines and provides a function.

    • A client is a component that sends a request to the server.

  • A service interface defines the functional interface between client and server components. Each service interface consists of one or more function elements.

    • A service is a unit of functionality.

    • A function element describes the attributes of a function in a client-server interface.

Clients and servers are often used to distribute functionality across a distributed system or to access services offered by cloud APIs. Therefore, calling a service may require resources and you may want to specify if the execution should be synchronous or asynchronous.

  • Synchronous execution is when the client sends a request to the server, the function runs immediately, and the function returns the output arguments to the client.

  • Asynchronous execution is when the client sends a request to the server, the function executes asynchronously based on the priority order, and the function returns the output arguments to the client.

Modeling Service-Oriented Communication Using System Composer

In System Composer, to connect your client and server components, use client and server ports.

Client port connected to a server port.

Once you connect the client and server components, you can specify function elements by creating a service interface. To create a service interface, open the Interface Editor, click the down arrow, and select Service Interface.

Add interface menu is open and the user is selecting Service Interface option to add a service interface.

You can create functions in your software architecture using the Interface Editor. For each function in the service interface, add a function element. Specify if the function is asynchronous by selecting the Asynchronous check box. To define the name and parameters of the function, edit the function prototype. Below the function prototype are the parameters of the function represented as function arguments. For each function argument, you can specify values in the Type, Dimensions, Units, Complexity, and Asynchronous columns.

Interface Editor with a service interface definition displayed. The service interface has a function element with function signature "y = f0(u)".

Once you define and select a service interface, you can assign the interface to client and server ports in your architecture by right-clicking the port and selecting Apply selected interface.

Right-click menu for client port with 'Apply selected interface: ServiceInterface0' option highlighted.

Implement Behavior of Functions Using Simulink

To implement behaviors for your functions, you can create a Simulink behavior model. To be able to generate code for your components with a Simulink behavior, use a Simulink export-function model. For more information about export-function models, see Export-Function Models Overview.

To create a Simulink behavior, right-click the component and select Create a Simulink behavior and set the Type of the Simulink behavior to Model Reference: Export-Function.

Create Simulink behavior model window with model type set to Model Reference: Export-Function and New file name set to 'NewComponentBehaviorModel'.

The created model is a template export-function model that includes the service interface you created in System Composer. In your software architecture, you see the export-function model as a model reference in the component. The implementation of the behavior depends on the synchronicity of the function execution.

The server model is where you define the function. To implement a function behavior, double-click the Simulink Function block. Inside the Simulink Function block is a Trigger block, an ArgIn block, and an ArgOut block. Add any necessary blocks to the function subsystem and connect elements to the ArgIn block and the ArgOut block.

Reference model, ServerModel, with Simulink Function block labeled "y = f0(u)" and a Function Element block labeled Server.f0.

The client model is where you call the function. To implement the function call inside the Function-Call Subsystem block, double-click the Function-Call Subsystem block. Add any necessary blocks to the function call subsystem to represent an input and an output. For an asynchronous function call, the output message is a message payload.

Reference model, ClientModel, with Function-Call Subsystem block labeled callf0, a Function Element block labeled Client.f0, an Inport block labeled callf0Port, and a message payload.

For more information on implementing function behavior, see Model Client-Server Communication Using Function Ports.

Simulate Your Client-Server Communication Using System Composer

You can simulate service-oriented communication by compiling and running your service-oriented architecture model.

To run your architecture model, click the Run button or use the sim function. For example, if your model is called myServiceModel, enter this command to run your model

sim('myServiceModel')

Once the model runs, you can analyze and visualize signals using the Simulation Data Inspector and Sequence Viewer tools.

  • To visualize the logged signals, go to the Simulation tab, and in the Review Results section, select Simulation Data Inspector.

  • To view the execution order of the functions calls, go to the Simulation tab, and in the Review Results section, select Sequence Viewer.

You can modify the execution order of functions in the service interface in Functions Editor.

For more information, see Model Service-Oriented Communication Between Sensors.

Test Your Service Interface Using Simulink Test

You can test your service interface by creating a test harness for models or components with client and server ports.

A test harness is a model that isolates the component under test with inputs, outputs, and verification blocks configured for testing scenarios. You can create a test harness for a model component or for a full model. A test harness gives you a separate testing environment for a model or a model component.

A Simulink Test™ license is required to create a test harness.

When you create a test harness, System Composer and Simulink Test autogenerate these models:

  • The mock architecture model contains a Reference Component referencing the component under test.

  • A test harness model containing a Model block referencing the mock architecture model and a Test Sequence (Simulink Test) block for defining test scenarios.

Use the Simulink Test Manager (Simulink Test) with the test harness to create test files and test cases. For more information, see Test Harness and Model Relationship (Simulink Test) and Create or Import Test Harnesses and Select Properties (Simulink Test).

For an example, see Model Service-Oriented Communication Between Sensors.

Generate Code Using Embedded Coder

If you have an Embedded Coder® license, you can generate code from a software architecture model that includes a service interface by using the rtwbuild (Embedded Coder) function.

Embedded Coder generates the service interface as an abstract class, which enables implementation to be separate from the interface.

The generated code contains an entry-point for each function of the component. For more information, see Generate Code for Export-Function Model.

See Also

Blocks

Functions

Related Topics