Skip to content

Commit

Permalink
Improve port-related convenience methods
Browse files Browse the repository at this point in the history
- Rework com-spec creation methods
- Move package map handling into Workspace
- Create Composition SWC example
- Fix: Unable to write ClientComSpec XML
- Fix: ClientServerOperation.ref returns wrong type
  • Loading branch information
cogu committed Mar 11, 2024
1 parent 334a0df commit 1002330
Show file tree
Hide file tree
Showing 11 changed files with 640 additions and 147 deletions.
120 changes: 65 additions & 55 deletions examples/xml/component/application_component.py
Original file line number Diff line number Diff line change
@@ -1,96 +1,109 @@
"""
Sender-receiver port interface examples
Application component example
"""
import os
import autosar
import autosar.xml.element as ar_element


def create_platform_types(packages: dict[str, ar_element.Package]):
def create_platform_types(workspace: autosar.xml.Workspace):
"""
Creates necessary platform types
"""
boolean_base_type = ar_element.SwBaseType('boolean', size=8, encoding="BOOLEAN")
packages["PlatformBaseTypes"].append(boolean_base_type)
workspace.add_element("PlatformBaseTypes", boolean_base_type)
uint8_base_type = ar_element.SwBaseType('uint8', size=8)
packages["PlatformBaseTypes"].append(uint8_base_type)
workspace.add_element("PlatformBaseTypes", uint8_base_type)
uint16_base_type = ar_element.SwBaseType('uint16', size=16)
packages["PlatformBaseTypes"].append(uint16_base_type)
workspace.add_element("PlatformBaseTypes", uint16_base_type)
uint32_base_type = ar_element.SwBaseType('uint32', size=32)
packages["PlatformBaseTypes"].append(uint32_base_type)
workspace.add_element("PlatformBaseTypes", uint32_base_type)
boolean_data_constr = ar_element.DataConstraint.make_internal("boolean_DataConstr", 0, 1)
packages["PlatformDataConstraints"].append(boolean_data_constr)
workspace.add_element("PlatformDataConstraints", boolean_data_constr)
computation = ar_element.Computation.make_value_table(["FALSE", "TRUE"])
boolean_compu_method = ar_element.CompuMethod(name="boolean_CompuMethod",
category="TEXTTABLE",
int_to_phys=computation)
packages["PlatformCompuMethods"].append(boolean_compu_method)
workspace.add_element("PlatformCompuMethods", boolean_compu_method)
sw_data_def_props = ar_element.SwDataDefPropsConditional(base_type_ref=uint8_base_type.ref(),
data_constraint_ref=boolean_data_constr.ref(),
compu_method_ref=boolean_compu_method.ref())
boolean_impl_type = ar_element.ImplementationDataType("boolean",
category="VALUE",
sw_data_def_props=sw_data_def_props)
packages["PlatformImplementationDataTypes"].append(boolean_impl_type)
workspace.add_element("PlatformImplementationDataTypes", boolean_impl_type)
sw_data_def_props = ar_element.SwDataDefPropsConditional(base_type_ref=uint8_base_type.ref())
uint8_impl_type = ar_element.ImplementationDataType("uint8",
category="VALUE",
sw_data_def_props=sw_data_def_props)
packages["PlatformImplementationDataTypes"].append(uint8_impl_type)
workspace.add_element("PlatformImplementationDataTypes", uint8_impl_type)
sw_data_def_props = ar_element.SwDataDefPropsConditional(base_type_ref=uint16_base_type.ref())
uint16_impl_type = ar_element.ImplementationDataType("uint16",
category="VALUE",
sw_data_def_props=sw_data_def_props)
packages["PlatformImplementationDataTypes"].append(uint16_impl_type)
workspace.add_element("PlatformImplementationDataTypes", uint16_impl_type)
sw_data_def_props = ar_element.SwDataDefPropsConditional(base_type_ref=uint32_base_type.ref())
uint32_impl_type = ar_element.ImplementationDataType("uint32",
category="VALUE",
sw_data_def_props=sw_data_def_props)
packages["PlatformImplementationDataTypes"].append(uint32_impl_type)
workspace.add_element("PlatformImplementationDataTypes", uint32_impl_type)


def create_sender_receiver_port_interfaces(packages: dict[str, ar_element.Package]):
def create_sender_receiver_port_interfaces(workspace: autosar.xml.Workspace):
"""
Create sender-receiver port interfaces used in example
Creates necessary sender-receiver port interfaces
"""
uint16_impl_t = packages["PlatformImplementationDataTypes"].find("uint16")
uint16_impl_t = workspace.find_element("PlatformImplementationDataTypes", "uint16")
port_interface = ar_element.SenderReceiverInterface("VehicleSpeed_I")
port_interface.create_data_element("VehicleSpeed", type_ref=uint16_impl_t.ref())
packages["PortInterfaces"].append(port_interface)
workspace.add_element("PortInterfaces", port_interface)
port_interface = ar_element.SenderReceiverInterface("EngineSpeed_I")
port_interface.create_data_element("EngineSpeed", type_ref=uint16_impl_t.ref())
workspace.add_element("PortInterfaces", port_interface)


def create_client_server_interfaces(packages: dict[str, ar_element.Package]):
def create_constants(workspace: autosar.xml.Workspace):
"""
Creates init values in Constants package
"""
constant = ar_element.ConstantSpecification.make_constant("VehicleSpeed_IV", 65535)
workspace.add_element("Constants", constant)
constant = ar_element.ConstantSpecification.make_constant("EngineSpeed_IV", 65535)
workspace.add_element("Constants", constant)


def create_client_server_interfaces(workspace: autosar.xml.Workspace):
"""
Creates client-server interface with one operation
"""
uint32_impl_type = packages["PlatformImplementationDataTypes"].find("uint32")
boolean_impl_type = packages["PlatformImplementationDataTypes"].find("boolean")
interface = ar_element.ClientServerInterface("FreeRunningTimer_I")
operation = interface.create_operation("GetTime")
uint32_impl_type = workspace.find_element("PlatformImplementationDataTypes", "uint32")
boolean_impl_type = workspace.find_element("PlatformImplementationDataTypes", "boolean")
port_interface = ar_element.ClientServerInterface("FreeRunningTimer_I")
operation = port_interface.create_operation("GetTime")
operation.create_out_argument("value", type_ref=uint32_impl_type.ref())
operation = interface.create_operation("IsTimerElapsed")
operation = port_interface.create_operation("IsTimerElapsed")
operation.create_in_argument("startTime", type_ref=uint32_impl_type.ref())
operation.create_in_argument("duration", type_ref=uint32_impl_type.ref())
operation.create_out_argument("result", type_ref=boolean_impl_type.ref())
packages["PortInterfaces"].append(interface)
workspace.add_element("PortInterfaces", port_interface)


def create_application_component(packages: dict[str, ar_element.Package]):
def create_application_component(workspace: autosar.xml.Workspace):
"""
Creates application component with one sender-receiver port and one
client-server port
Creates application SWC with two sender ports
"""
timer_interface = packages["PortInterfaces"].find("FreeRunningTimer_I")
vehicle_speed_interface = packages["PortInterfaces"].find("VehicleSpeed_I")
swc = ar_element.ApplicationSoftwareComponentType("MyApplication")
swc.create_require_port("VehicleSpeed", vehicle_speed_interface, com_spec={"init_value": 65535,
'alive_timeout': 0,
'enable_update': False,
'uses_end_to_end_protection': False,
'handle_never_received': False
vehicle_speed_interface = workspace.find_element("PortInterfaces", "VehicleSpeed_I")
vehicle_speed_init = workspace.find_element("Constants", "VehicleSpeed_IV")
engine_speed_interface = workspace.find_element("PortInterfaces", "EngineSpeed_I")
engine_speed_init = workspace.find_element("Constants", "EngineSpeed_IV")
swc = ar_element.ApplicationSoftwareComponentType("SenderComponent")
swc.create_provide_port("VehicleSpeed", vehicle_speed_interface, com_spec={"init_value": vehicle_speed_init.ref(),
"uses_end_to_end_protection": False,
})
swc.create_require_port("FreeRunningTimer", timer_interface)
packages["ComponentTypes"].append(swc)
swc.create_provide_port("EngineSpeed", engine_speed_interface, com_spec={"init_value": engine_speed_init.ref(),
"uses_end_to_end_protection": False,
})
workspace.add_element("ComponentTypes", swc)


def save_xml_files(workspace: autosar.xml.Workspace):
Expand All @@ -102,8 +115,11 @@ def save_xml_files(workspace: autosar.xml.Workspace):
platform_document_path = os.path.abspath(os.path.join(os.path.dirname(
__file__), 'data', 'platform.arxml'))
component_document_path = os.path.abspath(os.path.join(os.path.dirname(
__file__), 'data', 'MyApplication.arxml'))
__file__), 'data', 'sender_component.arxml'))
constant_document_path = os.path.abspath(os.path.join(os.path.dirname(
__file__), 'data', 'constants.arxml'))
workspace.create_document(interface_document_path, packages="/PortInterfaces")
workspace.create_document(constant_document_path, packages="/Constants")
workspace.create_document(platform_document_path, packages="/AUTOSAR_Platform")
workspace.create_document(component_document_path, packages="/ComponentTypes")
workspace.write_documents()
Expand All @@ -114,24 +130,18 @@ def main():
Main
"""
workspace = autosar.xml.Workspace()
packages = dict(zip(["PlatformBaseTypes",
"PlatformImplementationDataTypes",
"PlatformDataConstraints",
"PlatformCompuMethods",
"ImplementationDataTypes",
"PortInterfaces",
"ComponentTypes"],
workspace.make_packages("AUTOSAR_Platform/BaseTypes",
"AUTOSAR_Platform/ImplementationDataTypes",
"AUTOSAR_Platform/DataConstraints",
"AUTOSAR_Platform/CompuMethods",
"DataTypes/ImplementationDataTypes",
"PortInterfaces",
"ComponentTypes")))
create_platform_types(packages)
create_sender_receiver_port_interfaces(packages)
create_client_server_interfaces(packages)
create_application_component(packages)
workspace.init_package_map({"PlatformBaseTypes": "AUTOSAR_Platform/BaseTypes",
"PlatformImplementationDataTypes": "AUTOSAR_Platform/ImplementationDataTypes",
"PlatformDataConstraints": "AUTOSAR_Platform/DataConstraints",
"PlatformCompuMethods": "AUTOSAR_Platform/CompuMethods",
"Constants": "Constants",
"PortInterfaces": "PortInterfaces",
"ComponentTypes": "ComponentTypes"})
create_platform_types(workspace)
create_sender_receiver_port_interfaces(workspace)
create_client_server_interfaces(workspace)
create_constants(workspace)
create_application_component(workspace)
save_xml_files(workspace)


Expand Down
Loading

0 comments on commit 1002330

Please sign in to comment.