monitor.set_sample_time(0.48) api.connect_monitor(sim_env, monitor, 0.5) # create a Result Reader that is used to export the # simulation results to the GUI or to a file result_reader = ResultReader() save_path_cp = os.path.join(os.path.dirname(__file__), "logs/checkpoints.csv") save_path_buf = os.path.join(os.path.dirname(__file__), "logs/buffer.csv") save_path_can = os.path.join(os.path.dirname(__file__), "logs/can_bus.csv") # enable certain handlers to define which export has to be made # a result reader receives the interpreter to be used and the InterpreterOptions enum # defining how the export should be performed result_reader.enable_handler(BufferInterpreter, [InterpreterOptions.CONNECTION, InterpreterOptions.CSV_FILE], save_path_buf) result_reader.enable_handler(CheckpointInterpreter, [ InterpreterOptions.TIMING_FILE], save_path_cp) result_reader.enable_handler(EventlineInterpreter, [InterpreterOptions.CSV_FILE], save_path_cp) # CSV Live Tracking result_reader.enable_handler(CanBusInterpreter, [InterpreterOptions.CSV_MSG_FILE, InterpreterOptions.CSV_DR_FILE], save_path_can) # connect the result reader to the monitor api.connect_result_reader(sim_env, monitor, result_reader) #=========================================================================== # Build and run simulation #=========================================================================== # run the simulation api.build_simulation(sim_env) api.run_simulation(sim_env)
# Create a Monitor and Reader my_moni = Monitor() my_moni.set_sample_time(0.48) api.connect_monitor(sim_env, my_moni, 0.5) # Connect monitor to environment my_reader = ResultReader() # enable handlers save_path_cp = os.path.join(os.path.dirname(__file__), "logs/checkpoints.csv") save_path_cp_2 = os.path.join(os.path.dirname(__file__), "logs/checkpoints2.csv") save_path_buf = os.path.join(os.path.dirname(__file__), "logs/buffer.csv") save_path_can = os.path.join(os.path.dirname(__file__), "logs/can_bus.csv") my_reader.enable_handler(BufferInterpreter, [InterpreterOptions.CONNECTION, InterpreterOptions.CSV_FILE], save_path_buf) # If connected to GUI this step is done automatically by GUI my_reader.enable_handler(CheckpointInterpreter, [ InterpreterOptions.TIMING_FILE], save_path_cp) my_reader.enable_handler(EventlineInterpreter, [InterpreterOptions.CSV_FILE], save_path_cp_2) # CSV Live Tracking my_reader.enable_handler(CanBusInterpreter, [InterpreterOptions.CSV_FILE], save_path_can) api.connect_result_reader(sim_env, my_moni, my_reader) # Connect result reader #=============================================================================== # Connect Reader to GUI #=============================================================================== # Create a GUI and connect it to the monior direct_view = gui.direct_view_window.DirectViewer() # available Plugins: BufferViewPlugin, CanBusStateViewPlugin, CanBusViewPlugin, CheckpointViewPlugin, # ConstellationViewPlugin, ECUMessagesViewPlugin, EventlineViewPlugin, MessageCountViewPlugin sim_env.gui_lock_sync.acquire() # optionally ensure thread synchronization direct_view.run(my_reader, ['EventlineViewPlugin', 'MessageCountViewPlugin'], sim_env.gui_lock_sync)
my_moni = Monitor() my_moni.set_sample_time(0.48) api.connect_monitor(sim_env, my_moni, 0.5) # Connect monitor to environment my_reader = ResultReader() # enable handlers save_path_cp = os.path.join(os.path.dirname(__file__), "logs/checkpoints.csv") save_path_cp_2 = os.path.join(os.path.dirname(__file__), "logs/checkpoints2.csv") save_path_buf = os.path.join(os.path.dirname(__file__), "logs/buffer.csv") save_path_can = os.path.join(os.path.dirname(__file__), "logs/can_bus.csv") my_reader.enable_handler(BufferInterpreter, [InterpreterOptions.CONNECTION, InterpreterOptions.CSV_FILE], save_path_buf) # If connected to GUI this step is done automatically by GUI my_reader.enable_handler(CheckpointInterpreter, [ InterpreterOptions.TIMING_FILE], save_path_cp) my_reader.enable_handler(EventlineInterpreter, [InterpreterOptions.CSV_FILE], save_path_cp_2) # CSV Live Tracking my_reader.enable_handler(CanBusInterpreter, [InterpreterOptions.CSV_FILE], save_path_can) api.connect_result_reader(sim_env, my_moni, my_reader) # Connect result reader #=============================================================================== # Connect Reader to GUI #=============================================================================== # Create a GUI and connect it to the monior direct_view = gui.direct_view_window.DirectViewer() # available Plugins: BufferViewPlugin, CanBusStateViewPlugin, CanBusViewPlugin, CheckpointViewPlugin, # ConstellationViewPlugin, ECUMessagesViewPlugin, EventlineViewPlugin, MessageCountViewPlugin sim_env.gui_lock_sync.acquire() # optionally ensure thread synchronization direct_view.run(my_reader, ['EventlineViewPlugin', 'MessageCountViewPlugin'], sim_env.gui_lock_sync) # start simulation api.open_simulation_stop_button(sim_env)
def prepare_configuration(self, simpy_env, life_time): ''' this method prepares the configuration on the side of the ECU simulation. It sets up the defined environment by mapping the ECU Ids as they are defined in the battery management system on the ECUs and the defined specs. So the constellation used is implemented here. Input: simpy_env simpy.Environment environment used in the battery management system life_time float life time of the simulation Output: - ''' # lifetime life_time = 50000 # logging # api_log_path = os.path.join(os.path.dirname(__file__), "../logs/api.log") # api.show_logging(logging.INFO, api_log_path, True) # create environment sim_env = api.create_environment(life_time) sim_env.set_env(simpy_env) ecu_list = [] # generate a ecu from the ECU specs setting the adapter for ecu_id in self.available_can_adapters(): # logging.info("ECU ID %s: " % ecu_id) # define individual ECU Spec ecu_spec = copy.deepcopy(self._ecu_spec) ecu_class_name = self._ecu_class_name if ecu_id in self._individual_spec: ecu_spec = self._individual_spec[ecu_id][0] ecu_class_name = self._individual_spec[ecu_id][1] ecu_spec.ecu_id_list = [str(ecu_id)] # create ecu ecu = api.set_ecus(sim_env, 1, ecu_class_name, ecu_spec)[0] ecu_list += [ecu] # connect ecu to adapter ecu.connect_adapter(self.can_bus_adapter[ecu_id]) # add security module # create ECU specification ecu_spec = SimpleECUSpec(['SEC 1'], 200000, 200000) # 200 KB ecu_spec.set_ecu_setting('t_ecu_auth_trigger_process', 0) ecu_spec.set_ecu_setting('t_ecu_auth_trigger_intervall', 80000) ecu_spec.set_apply_jitter(0.0001) sec_mod_group = api.set_ecus(sim_env, 1, 'SecLwAuthSecurityModule', ecu_spec) security_module = sec_mod_group[0] # connect to one bus # create the bus specifications bus_spec = SimpleBusSpec(['CAN_0']) api.set_busses(sim_env, 1, 'StdCANBus', bus_spec) api.connect_bus_by_obj(sim_env, 'CAN_0', ecu_list + sec_mod_group) # security constellation all_ecu_groups = [ecu_list] api.register_ecu_groups_to_secmod(sim_env, sec_mod_group[0].ecu_id, all_ecu_groups) certificate_manager = api.create_cert_manager() all_created_ecus = api.ecu_list_from_groups([all_ecu_groups]) ecu_ids = [str(ecu.ecu_id) for ecu in all_created_ecus] for ecu_id in ecu_ids: api.generate_valid_ecu_cert_cfg(certificate_manager, ecu_id, CAEnum.CA_L313, security_module.ecu_id, 0, float('inf')) api.generate_valid_sec_mod_cert_cfg(certificate_manager, security_module.ecu_id, CAEnum.CA_L313, ecu_ids, 0, float('inf')) api.apply_certification(sim_env, certificate_manager) # define allowed streams -------------------------------------------------------------- TODO very IMPORTANT for broadcast_stream_id in [0x0080, 0x0081, 0x0082, 0x0083, 0x0012, 0x0013, 0x0020, 0x00A0, 0x00A1 ]: for ecu_id in ecu_ids: lst = copy.deepcopy(ecu_ids) lst.remove(ecu_id) stream = MessageStream(ecu_id, lst, broadcast_stream_id, float('inf'), 0, float('inf')) api.add_allowed_stream(sim_env, security_module.ecu_id, stream) # -------------------------------------------------------------- TODO very IMPORTANT # set gateways api.autoset_gateway_filters(sim_env, sec_mod_group[0].ecu_id) # set timing functions function_set = TimingFunctionSet() ecu_func_set = StdSecurLwSecModTimingFunctions(main_library_tag='CyaSSL') ecu_func_set.library_tags['t_ecu_auth_reg_msg_validate_cert'] = 'Crypto_Lib_SW' function_set.set_mapping_from_function_set(security_module.ecu_id, ecu_func_set) api.apply_timing_functions_set(sim_env, security_module.ecu_id, function_set) function_set_2 = TimingFunctionSet() ecu_func_set = StdSecurECUTimingFunctions(main_library_tag='CyaSSL') ecu_func_set.library_tags['t_adv_msg_secmodcert_enc'] = 'Crypto_Lib_SW' for ecu_id in ecu_ids: function_set_2.set_mapping_from_function_set(ecu_id, ecu_func_set) api.apply_timing_functions_set(sim_env, ecu_id, function_set_2) # add monitor if self._monitor and self._result_reader: api.connect_monitor(sim_env, self._monitor, 0.5) api.connect_result_reader(sim_env, self._monitor, self._result_reader) # run view if defined if self._view: self._view.show(self._result_reader, self._view_options, self.q_application) # run simulation api.open_simulation_stop_button(sim_env) api.build_simulation(sim_env) api.run_simulation(sim_env)