From e5dadc1b378ac0ef1fb6f08d985c8f77bd7e16b1 Mon Sep 17 00:00:00 2001 From: Jonas Danielsson Date: Wed, 8 Sep 2021 09:36:30 +0200 Subject: [PATCH] main: warn once about USB permissions on scan When the user press the scan button and we get a Permissions Denied error from the USB core then we know that the Crazyradio dongle is present, but we cannot access it. We will warn, once, about this with a message box that gives a link to instructions on how to install the udev files. Closes #444 --- src/cfclient/ui/main.py | 31 ++++++++++++++++++++++++++++++- 1 file changed, 30 insertions(+), 1 deletion(-) diff --git a/src/cfclient/ui/main.py b/src/cfclient/ui/main.py index b25bb5a76f..fbdc345c22 100644 --- a/src/cfclient/ui/main.py +++ b/src/cfclient/ui/main.py @@ -28,6 +28,7 @@ """ import logging import sys +import usb import cfclient from cfclient.ui.pose_logger import PoseLogger @@ -54,6 +55,7 @@ from PyQt5.QtCore import QDir from PyQt5.QtCore import QThread from PyQt5.QtCore import QUrl +from PyQt5.QtCore import Qt from PyQt5.QtWidgets import QAction from PyQt5.QtWidgets import QActionGroup from PyQt5.QtWidgets import QShortcut @@ -185,7 +187,7 @@ def __init__(self, *args): scan_button=self.scanButton)) self._connectivity_manager.connect_button_clicked.connect(self._connect) - self._connectivity_manager.scan_button_clicked.connect(self._scan) + self._connectivity_manager.scan_button_clicked.connect(self._scan_from_button) self._disable_input = False @@ -359,6 +361,9 @@ def __init__(self, *args): self._theme_group.addAction(node) self.menuThemes.addAction(node) + # We only want to warn about USB permission once + self._permission_warned = False + def _set_address(self): address = 0xE7E7E7E7E7 try: @@ -598,6 +603,30 @@ def _scan(self, address): self._update_ui_state() self.scanner.scanSignal.emit(address) + def _scan_from_button(self, address): + # + # Below we check if we can open the Crazyradio device. + # If it is there, but we have no permissions we inform the user, once, + # about how to install the udev rules. + # + if not self._permission_warned: + try: + radio = cflib.crtp.radiodriver.RadioManager.open(0) + radio.close() + except usb.core.USBError as e: + if e.errno == 13: # Permission denied + link = "Install USB Permissions" # noqa + msg = QMessageBox() + msg.setIcon(QMessageBox.Information) + msg.setTextFormat(Qt.RichText) + msg.setText("Could not access Crazyradio") + msg.setInformativeText(link) + msg.setWindowTitle("Crazyradio permissions") + msg.exec() + self._permission_warned = True + + self._scan(address) + def _display_input_device_error(self, error): self.cf.close_link() QMessageBox.critical(self, "Input device error", error)