Main Content

Programmatically Configure AUTOSAR Client-Server Communication

This example provides a programmatic workflow for configuring client-server ports, interfaces, and runnables using the autosar.api.getAUTOSARProperties object functions. Programmatic workflows are also provided for mapping ports to Simulink® functions using the autosar.api.getSimulinkMapping object functions.

The first section of this example uses model mControllerWithInterface_client to configure client properties, and create mappings to function callers using the above objects and their functions.

The second section of this examples uses model mControllerWithInterface_server to configure server properties, runnables, and events using the above objects and their functions.

Configure Client Properties and Mappings

Open example model mControllerWithInterface_client and create the autosar.api.getAUTOSARProperties object.

clientModel = "mControllerWithInterface_client";
open_system(clientModel);
clientProps = autosar.api.getAUTOSARProperties(clientModel);

Add Client-Server Interface and Operation

Add an AUTOSAR Client-Server interface named myCsIf. You will need to use the autosar.api.getAUTOSARProperties object to retrieve the InterfacePackage property information. For information regarding AUTOSAR properties, see AUTOSAR Element Properties.

clientifName = 'myCsIf';
clientifPkg = get(clientProps,"XmlOptions","InterfacePackage");
addPackageableElement(clientProps,"ClientServerInterface",clientifPkg,clientifName,"IsService",false);
ifPaths = find(clientProps,[],"ClientServerInterface","PathType","FullyQualified");

Add an AUTOSAR client-server operation, readData, to client-server interface myCsIf.

cOp = 'readData';
add(clientProps, [clientifPkg '/' clientifName],"Operations",cOp);

Define AUTOSAR arguments for operation readData specifying the Direction and SwCalibrationAccess properties. For information regarding AUTOSAR properties, see AUTOSAR Element Properties.

args = {'Op','In'; 'Data','Out'; 'ERR','Out'; 'NegCode','Out'};
swCalibValue = 'ReadOnly';
for i=1:length(args)
    add(clientProps,[clientifPkg '/' clientifName '/' cOp],"Arguments",args{i,1},"Direction",args{i,2},...
        "SwCalibrationAccess",swCalibValue);
end
get(clientProps,[clientifPkg '/' clientifName '/' cOp],"Arguments")'
ans = 4x1 cell
    {'myCsIf/readData/Op'     }
    {'myCsIf/readData/Data'   }
    {'myCsIf/readData/ERR'    }
    {'myCsIf/readData/NegCode'}

Add Client Ports to Client-Server Interface

Add AUTOSAR client and server ports to interface myCSIf.

cPortName = "myCPort";
aswcPath = find(clientProps,[],"AtomicComponent","PathType","FullyQualified");
add(clientProps,aswcPath{1},"ClientPorts",cPortName,"Interface",clientifName);

Find all of the AUTOSAR client ports. You can find client or server ports by setting the category property to ClientPort and using the find function.

arPortType = "ClientPort";
clientPorts = find(clientProps,aswcPath{1},arPortType,"PathType","FullyQualified");

Using a for loop, cycle through the client ports present in the component and list their associated interfaces.

for ii=1:length(clientPorts)
    sPort = clientPorts{ii};
    portIf = get(clientProps,sPort,"Interface");
    fprintf("Port %s has C-S interface %s\n",sPort,portIf);
end
Port /pkg/swc/ASWC/cPort has C-S interface CsIf1
Port /pkg/swc/ASWC/myCPort has C-S interface myCsIf

Set the Interface property for the found AUTOSAR ports. For information regarding AUTOSAR properties, see AUTOSAR Element Properties.

set(clientProps,clientPorts{1},"Interface","myCsIf");
portIf = get(clientProps,clientPorts{1},"Interface");
fprintf("Port %s has C-S interface %s\n",clientPorts{1},portIf);
Port /pkg/swc/ASWC/cPort has C-S interface myCsIf

Map Simulink Functions to Client Ports and Operations

You can also map the Simulink function caller readData to AUTOSAR client port and operation: myCPort, and readData.

Create the autosar.api.getSimulinkMapping object and retrieve function caller readData.

slMap = autosar.api.getSimulinkMapping(clientModel);

You can get linked functions using the autosar.api.getSimulinkMapping object and the name of the function.

[arPort,arOp] = getFunctionCaller(slMap,'readData')
arPort = 
'cPort'
arOp =

  0x0 empty char array
mapFunctionCaller(slMap,'readData',cPortName,cOp);
[arPort,arOp] = getFunctionCaller(slMap,'readData')
arPort = 
'myCPort'
arOp = 
'readData'

Configure Server Properties and Mappings

Using example model mControllerWithInterface_server add and configure an AUTOSAR client-server (C-S) interface.

Open example model mControllerWithInterface_server and create the autosar.api.getAUTOSARProperties object.

serverModel = 'mControllerWithInterface_server';
open_system(serverModel);
serverProps = autosar.api.getAUTOSARProperties(serverModel);

Add Client-Server Interface and Operation

Add an AUTOSAR Client-Server interface named myCsIf. You will need to use the autosar.api.getAUTOSARProperties object to retrieve the InterfacePackage property information. For information regarding AUTOSAR properties, see AUTOSAR Element Properties.

serverifName = 'myCsIf';
serverifPkg = get(serverProps,"XmlOptions","InterfacePackage");
addPackageableElement(serverProps,"ClientServerInterface",serverifPkg,serverifName,"IsService",false);
ifPaths=find(serverProps,[],"ClientServerInterface","PathType","FullyQualified");

Add an AUTOSAR client-server operation, readData, to client-server interface, myCsIf.

sOp = 'readData';
add(serverProps, [serverifPkg '/' serverifName],"Operations",sOp);

Add AUTOSAR arguments to operation readData with Direction and SwCalibrationAccess properties specified. For information regarding AUTOSAR properties, see AUTOSAR Element Properties.

args = {'Op','In'; 'Data','Out'; 'ERR','Out'; 'NegCode','Out'};
swCalibValue = 'ReadOnly';
for i=1:length(args)
    add(serverProps,[serverifPkg '/' serverifName '/' sOp],"Arguments",args{i,1},"Direction",args{i,2},...
        "SwCalibrationAccess",swCalibValue);
end
get(serverProps,[serverifPkg '/' serverifName '/' sOp],"Arguments")'
ans = 4x1 cell
    {'myCsIf/readData/Op'     }
    {'myCsIf/readData/Data'   }
    {'myCsIf/readData/ERR'    }
    {'myCsIf/readData/NegCode'}

Add Server Ports to Client-Server Interface

Add AUTOSAR server port mySPort to interface myCSIf.

sPortName = 'mySPort';
saswcPath = find(serverProps,[],"AtomicComponent","PathType","FullyQualified");
add(serverProps,saswcPath{1},"ServerPorts",sPortName,"Interface",serverifName);

Add and Configure Server Runnables for Client-Server Communication

Add AUTOSAR server runnable with symbol name, read, matching the Simulink function name.

serverRunnable = 'Runnable_myReadData';
serverRunnableSymbol = 'readData';
swc = get(serverProps,"XmlOptions","ComponentQualifiedName");
ib = get(serverProps,swc,"Behavior");
runnables = get(serverProps,ib,"Runnables");

To avoid symbol conflict, remove any existing runnables with symbol name readData.

delete(serverProps,'SWC_Controller/ControllerWithInterface_ar/Runnable_readData')
add(serverProps,ib,"Runnables",serverRunnable,"symbol",serverRunnableSymbol);
runnables = get(serverProps,ib,"Runnables");

Add an AUTOSAR operation setting the Category property to OperationInvokedEvent.

oiEventName = 'Event_myRead';
add(serverProps,ib,"Events",oiEventName,"Category","OperationInvokedEvent",...
    "Trigger","mySPort.readData","StartOnEvent",[ib '/' serverRunnable])

Map Server Runnables to Simulink Functions

Create the autosar.api.getSimulinkMapping object and use the object functions to map Simulink function readData to the AUTOSAR runnable Runnable_myReadData.

serverMap = autosar.api.getSimulinkMapping(serverModel);
mapFunction(serverMap,"SimulinkFunction:readData",serverRunnable);
arRunnableName = getFunction(serverMap,"SimulinkFunction:readData")
arRunnableName = 
'Runnable_myReadData'

See Also

Objects

Functions

Properties

Related Topics