Skip to content

Commit

Permalink
start rx/tx only when needed in simulator
Browse files Browse the repository at this point in the history
  • Loading branch information
jopohl committed Mar 25, 2018
1 parent 7ef12c4 commit 2005607
Show file tree
Hide file tree
Showing 4 changed files with 171 additions and 92 deletions.
167 changes: 105 additions & 62 deletions src/urh/controller/dialogs/SimulatorDialog.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ def __init__(self, simulator_config, modulators,
self.setAttribute(Qt.WA_DeleteOnClose)

self.simulator_config = simulator_config # type: SimulatorConfiguration
self.rx_needed = self.simulator_config.rx_needed
self.tx_needed = self.simulator_config.tx_needed

self.current_transcript_index = 0

self.simulator_scene = SimulatorScene(mode=1,
Expand All @@ -54,68 +57,97 @@ def __init__(self, simulator_config, modulators,
self.timer = QTimer(self)

self.backend_handler = BackendHandler()
self.device_settings_rx_widget = DeviceSettingsWidget(project_manager,
is_tx=False,
backend_handler=self.backend_handler)
if self.rx_needed:
self.device_settings_rx_widget = DeviceSettingsWidget(project_manager,
is_tx=False,
backend_handler=self.backend_handler)

self.sniff_settings_widget = SniffSettingsWidget(self.device_settings_rx_widget.ui.cbDevice.currentText(),
project_manager,
signal=None,
backend_handler=self.backend_handler,
network_raw_mode=True, signals=signals)
self.sniff_settings_widget = SniffSettingsWidget(self.device_settings_rx_widget.ui.cbDevice.currentText(),
project_manager,
signal=None,
backend_handler=self.backend_handler,
network_raw_mode=True, signals=signals)

self.device_settings_rx_widget.device = self.sniff_settings_widget.sniffer.rcv_device
self.device_settings_rx_widget.device = self.sniff_settings_widget.sniffer.rcv_device

self.sniff_settings_widget.ui.lineEdit_sniff_OutputFile.hide()
self.sniff_settings_widget.ui.label_sniff_OutputFile.hide()
self.sniff_settings_widget.ui.label_sniff_viewtype.hide()
self.sniff_settings_widget.ui.checkBox_sniff_Timestamp.hide()
self.sniff_settings_widget.ui.comboBox_sniff_viewtype.hide()
self.sniff_settings_widget.ui.lineEdit_sniff_OutputFile.hide()
self.sniff_settings_widget.ui.label_sniff_OutputFile.hide()
self.sniff_settings_widget.ui.label_sniff_viewtype.hide()
self.sniff_settings_widget.ui.checkBox_sniff_Timestamp.hide()
self.sniff_settings_widget.ui.comboBox_sniff_viewtype.hide()

self.ui.scrollAreaWidgetContentsRX.layout().insertWidget(0, self.device_settings_rx_widget)
self.ui.scrollAreaWidgetContentsRX.layout().insertWidget(1, self.sniff_settings_widget)
self.ui.scrollAreaWidgetContentsRX.layout().insertWidget(0, self.device_settings_rx_widget)
self.ui.scrollAreaWidgetContentsRX.layout().insertWidget(1, self.sniff_settings_widget)

self.device_settings_tx_widget = DeviceSettingsWidget(project_manager, is_tx=True,
backend_handler=self.backend_handler,
continuous_send_mode=True)
self.device_settings_tx_widget.ui.spinBoxNRepeat.hide()
self.device_settings_tx_widget.ui.labelNRepeat.hide()
sniffer = self.sniff_settings_widget.sniffer

self.modulation_settings_widget = ModulationSettingsWidget(modulators, signal_tree_model=signal_tree_model,
parent=None)
self.scene_manager = SniffSceneManager(np.array([]), parent=self)
self.ui.graphicsViewPreview.setScene(self.scene_manager.scene)
else:
self.device_settings_rx_widget = self.sniff_settings_widget = self.scene_manager = None
self.ui.tabWidgetSimulatorSettings.setTabEnabled(1, False)
self.ui.graphicsViewPreview.hide()
self.ui.btnSaveRX.hide()
self.ui.checkBoxCaptureFullRX.hide()

sniffer = None

if self.tx_needed:
self.device_settings_tx_widget = DeviceSettingsWidget(project_manager, is_tx=True,
backend_handler=self.backend_handler,
continuous_send_mode=True)
self.device_settings_tx_widget.ui.spinBoxNRepeat.hide()
self.device_settings_tx_widget.ui.labelNRepeat.hide()

self.modulation_settings_widget = ModulationSettingsWidget(modulators, signal_tree_model=signal_tree_model,
parent=None)

self.ui.scrollAreaWidgetContentsTX.layout().insertWidget(0, self.device_settings_tx_widget)
self.ui.scrollAreaWidgetContentsTX.layout().insertWidget(1, self.modulation_settings_widget)
send_device = self.device_settings_tx_widget.ui.cbDevice.currentText()
sender = EndlessSender(self.backend_handler, send_device)
else:
self.device_settings_tx_widget = self.modulation_settings_widget = None
self.ui.tabWidgetSimulatorSettings.setTabEnabled(2, False)

self.ui.scrollAreaWidgetContentsTX.layout().insertWidget(0, self.device_settings_tx_widget)
self.ui.scrollAreaWidgetContentsTX.layout().insertWidget(1, self.modulation_settings_widget)
sender = None

send_device = self.device_settings_tx_widget.ui.cbDevice.currentText()
self.simulator = Simulator(self.simulator_config, modulators, expression_parser, project_manager,
sniffer=self.sniff_settings_widget.sniffer,
sender=EndlessSender(self.backend_handler, send_device))

self.device_settings_tx_widget.device = self.simulator.sender.device
sniffer=sniffer, sender=sender)

self.scene_manager = SniffSceneManager(np.array([]), parent=self)
self.ui.graphicsViewPreview.setScene(self.scene_manager.scene)
if self.device_settings_tx_widget:
self.device_settings_tx_widget.device = self.simulator.sender.device

self.update_buttons()
self.create_connects()

self.device_settings_rx_widget.bootstrap(project_manager.simulator_rx_conf)
self.device_settings_tx_widget.bootstrap(project_manager.simulator_tx_conf)
if self.device_settings_rx_widget:
self.device_settings_rx_widget.bootstrap(project_manager.simulator_rx_conf)

if self.device_settings_tx_widget:
self.device_settings_tx_widget.bootstrap(project_manager.simulator_tx_conf)

self.ui.textEditTranscript.setFont(util.get_monospace_font())

if constants.SETTINGS.value('default_view', 0, int) == 1:
self.ui.radioButtonTranscriptHex.setChecked(True)

def create_connects(self):
self.device_settings_rx_widget.selected_device_changed.connect(self.on_selected_rx_device_changed)
self.device_settings_rx_widget.device_parameters_changed.connect(self.rx_parameters_changed.emit)
if self.rx_needed:
self.device_settings_rx_widget.selected_device_changed.connect(self.on_selected_rx_device_changed)
self.device_settings_rx_widget.device_parameters_changed.connect(self.rx_parameters_changed.emit)

self.sniff_settings_widget.sniff_parameters_changed.connect(self.sniff_parameters_changed.emit)

self.device_settings_tx_widget.selected_device_changed.connect(self.on_selected_tx_device_changed)
self.device_settings_tx_widget.device_parameters_changed.connect(self.tx_parameters_changed.emit)
self.ui.btnSaveRX.clicked.connect(self.on_btn_save_rx_clicked)
self.ui.checkBoxCaptureFullRX.clicked.connect(self.on_checkbox_capture_full_rx_clicked)

self.sniff_settings_widget.sniff_parameters_changed.connect(self.sniff_parameters_changed.emit)
self.ui.btnTestSniffSettings.clicked.connect(self.on_btn_test_sniff_settings_clicked)
self.ui.btnOpenInAnalysis.clicked.connect(self.on_btn_open_in_analysis_clicked)

if self.tx_needed:
self.device_settings_tx_widget.selected_device_changed.connect(self.on_selected_tx_device_changed)
self.device_settings_tx_widget.device_parameters_changed.connect(self.tx_parameters_changed.emit)

self.ui.radioButtonTranscriptBit.clicked.connect(self.on_radio_button_transcript_bit_clicked)
self.ui.radioButtonTranscriptHex.clicked.connect(self.on_radio_button_transcript_hex_clicked)
Expand All @@ -134,13 +166,6 @@ def create_connects(self):
self.simulator.simulation_started.connect(self.on_simulation_started)
self.simulator.simulation_stopped.connect(self.on_simulation_stopped)

self.ui.btnSaveRX.clicked.connect(self.on_btn_save_rx_clicked)

self.ui.checkBoxCaptureFullRX.clicked.connect(self.on_checkbox_capture_full_rx_clicked)

self.ui.btnTestSniffSettings.clicked.connect(self.on_btn_test_sniff_settings_clicked)
self.ui.btnOpenInAnalysis.clicked.connect(self.on_btn_open_in_analysis_clicked)

def update_buttons(self):
selectable_items = self.simulator_scene.selectable_items()
all_items_selected = all(item.model_item.logging_active for item in selectable_items)
Expand All @@ -159,7 +184,8 @@ def update_view(self):
for source, destination, msg, msg_index in self.simulator.transcript[self.current_transcript_index:]:
try:
data = msg.plain_bits_str if self.ui.radioButtonTranscriptBit.isChecked() else msg.plain_hex_str
self.ui.textEditTranscript.append(self.TRANSCRIPT_FORMAT.format(msg_index, source.shortname, destination.shortname, data))
self.ui.textEditTranscript.append(
self.TRANSCRIPT_FORMAT.format(msg_index, source.shortname, destination.shortname, data))
except AttributeError:
self.ui.textEditTranscript.append("")

Expand All @@ -171,7 +197,7 @@ def update_view(self):
self.ui.lblCurrentItemValue.setText(current_item)

def update_rx_graphics_view(self):
if not self.ui.graphicsViewPreview.isEnabled():
if self.scene_manager is None or not self.ui.graphicsViewPreview.isEnabled():
return

self.scene_manager.end = self.simulator.sniffer.rcv_device.current_index
Expand All @@ -188,16 +214,22 @@ def reset(self):
self.ui.lblCurrentItemValue.setText("-")

def emit_editing_finished_signals(self):
self.device_settings_rx_widget.emit_editing_finished_signals()
self.device_settings_tx_widget.emit_editing_finished_signals()
self.sniff_settings_widget.emit_editing_finished_signals()
if self.device_settings_rx_widget:
self.device_settings_rx_widget.emit_editing_finished_signals()

if self.device_settings_tx_widget:
self.device_settings_tx_widget.emit_editing_finished_signals()

if self.sniff_settings_widget:
self.sniff_settings_widget.emit_editing_finished_signals()

def update_transcript_view(self):
transcript = []
for source, destination, msg, msg_index in self.simulator.transcript:
try:
data = msg.plain_bits_str if self.ui.radioButtonTranscriptBit.isChecked() else msg.plain_hex_str
transcript.append(self.TRANSCRIPT_FORMAT.format(msg_index, source.shortname, destination.shortname, data))
transcript.append(
self.TRANSCRIPT_FORMAT.format(msg_index, source.shortname, destination.shortname, data))
except AttributeError:
transcript.append("")
self.ui.textEditTranscript.setText("\n".join(transcript))
Expand All @@ -209,9 +241,12 @@ def closeEvent(self, event: QCloseEvent):
self.simulator.cleanup()

self.emit_editing_finished_signals()
self.device_settings_rx_widget.emit_device_parameters_changed()
self.device_settings_tx_widget.emit_device_parameters_changed()
self.sniff_settings_widget.emit_sniff_parameters_changed()
if self.device_settings_rx_widget:
self.device_settings_rx_widget.emit_device_parameters_changed()
if self.device_settings_tx_widget:
self.device_settings_tx_widget.emit_device_parameters_changed()
if self.sniff_settings_widget:
self.sniff_settings_widget.emit_sniff_parameters_changed()

super().closeEvent(event)

Expand All @@ -225,6 +260,9 @@ def on_simulation_started(self):
self.ui.btnStartStop.setIcon(QIcon.fromTheme("media-playback-stop"))
self.ui.btnStartStop.setText("Stop")

if not self.rx_needed:
return

rx_device = self.simulator.sniffer.rcv_device
for item in self.scene_manager.scene.items():
if isinstance(item, QGraphicsTextItem):
Expand All @@ -246,8 +284,10 @@ def on_simulation_started(self):

@pyqtSlot()
def on_simulation_stopped(self):
for i in range(3):
self.ui.tabWidgetSimulatorSettings.setTabEnabled(i, True)
self.ui.tabWidgetSimulatorSettings.setTabEnabled(0, True)
self.ui.tabWidgetSimulatorSettings.setTabEnabled(1, self.rx_needed)
self.ui.tabWidgetSimulatorSettings.setTabEnabled(2, self.tx_needed)

self.timer.stop()
self.update_view()
self.ui.btnStartStop.setIcon(QIcon.fromTheme("media-playback-start"))
Expand Down Expand Up @@ -301,12 +341,15 @@ def on_btn_start_stop_clicked(self):
if self.simulator.is_simulating:
self.simulator.stop()
else:
self.device_settings_rx_widget.emit_editing_finished_signals()
self.device_settings_tx_widget.emit_editing_finished_signals()
self.sniff_settings_widget.emit_editing_finished_signals()
if self.rx_needed:
self.device_settings_rx_widget.emit_editing_finished_signals()
self.sniff_settings_widget.emit_editing_finished_signals()

self.simulator.sniffer.rcv_device.current_index = 0
self.simulator.sniffer.rcv_device.resume_on_full_receive_buffer = not self.ui.checkBoxCaptureFullRX.isChecked()

self.simulator.sniffer.rcv_device.current_index = 0
self.simulator.sniffer.rcv_device.resume_on_full_receive_buffer = not self.ui.checkBoxCaptureFullRX.isChecked()
if self.tx_needed:
self.device_settings_tx_widget.emit_editing_finished_signals()

self.simulator.start()

Expand Down
44 changes: 33 additions & 11 deletions src/urh/simulator/Simulator.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,13 +77,19 @@ def start(self):
self.__initialize_counters()

# start devices
self.sniffer.rcv_device.fatal_error_occurred.connect(self.stop_on_error)
self.sniffer.rcv_device.ready_for_action.connect(self.on_sniffer_ready)
self.sender.device.fatal_error_occurred.connect(self.stop_on_error)
self.sender.device.ready_for_action.connect(self.on_sender_ready)
if self.sniffer:
self.sniffer.rcv_device.fatal_error_occurred.connect(self.stop_on_error)
self.sniffer.rcv_device.ready_for_action.connect(self.on_sniffer_ready)

self.sniffer.sniff()
self.sender.start()
if self.sender:
self.sender.device.fatal_error_occurred.connect(self.stop_on_error)
self.sender.device.ready_for_action.connect(self.on_sender_ready)

if self.sniffer:
self.sniffer.sniff()

if self.sender:
self.sender.start()

self._start_simulation_thread()

Expand Down Expand Up @@ -117,8 +123,11 @@ def stop(self, msg=""):
time.sleep(0.5)

# stop devices
self.sniffer.stop()
self.sender.stop()
if self.sniffer:
self.sniffer.stop()

if self.sender:
self.sender.stop()

self.simulation_stopped.emit()

Expand All @@ -131,7 +140,9 @@ def reset(self):
self.sniffer_ready = False
self.sender_ready = False
self.fatal_device_error_occurred = False
self.sniffer.clear()

if self.sniffer:
self.sniffer.clear()

self.current_item = self.simulator_config.rootItem

Expand All @@ -146,7 +157,11 @@ def reset(self):

@property
def devices(self):
result = [self.sniffer.rcv_device, self.sender.device]
result = []
if self.sniffer is not None:
result.append(self.sniffer.rcv_device)
if self.sender is not None:
result.append(self.sender.device)
return result

def device_messages(self) -> list:
Expand Down Expand Up @@ -184,7 +199,7 @@ def simulation_is_finished(self):

def __wait_for_devices(self):
for i in range(10):
if self.sniffer_ready and self.sender_ready:
if (self.sniffer is None or self.sniffer_ready) and (self.sender is None or self.sender_ready):
return True
if self.fatal_device_error_occurred:
return False
Expand Down Expand Up @@ -294,6 +309,9 @@ def process_message(self):
if msg.source.simulate:
# we have to send a message
sender = self.sender
if sender is None:
self.log_message("Fatal: No sender configured")
return

for lbl in new_message.message_type:
if isinstance(lbl.label, ChecksumLabel):
Expand All @@ -313,6 +331,10 @@ def process_message(self):
# we have to receive a message
self.log_message("<i>Waiting for message {}</i>".format(msg.index()))
sniffer = self.sniffer
if sniffer is None:
self.log_message("Fatal: No sniffer configured")
return

retry = 0

max_retries = self.project_manager.simulator_retries
Expand Down
Loading

0 comments on commit 2005607

Please sign in to comment.