Skip to content

Commit

Permalink
main: warn once about USB permissions on scan
Browse files Browse the repository at this point in the history
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 bitcraze#444
  • Loading branch information
jonasdn committed Sep 8, 2021
1 parent 96d00f9 commit e5dadc1
Showing 1 changed file with 30 additions and 1 deletion.
31 changes: 30 additions & 1 deletion src/cfclient/ui/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
"""
import logging
import sys
import usb

import cfclient
from cfclient.ui.pose_logger import PoseLogger
Expand All @@ -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
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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 = "<a href='https://www.bitcraze.io/documentation/repository/crazyflie-lib-python/master/installation/usb_permissions/'>Install USB Permissions</a>" # 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)
Expand Down

0 comments on commit e5dadc1

Please sign in to comment.