Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Wrong WiFi driver detection in Snap package and Ubuntu Core #267

Closed
andi-h opened this issue Nov 24, 2022 · 5 comments · Fixed by #446
Closed

Wrong WiFi driver detection in Snap package and Ubuntu Core #267

andi-h opened this issue Nov 24, 2022 · 5 comments · Fixed by #446
Assignees
Labels
bug Something isn't working demos Relating to demos

Comments

@andi-h
Copy link

andi-h commented Nov 24, 2022

Component
What is the bug in?

  • python sdk

Describe the bug
I'm trying to connect to a GoPro via WiFi with a Raspberry CM4 using the Python SDK and Ubuntu Core 22.
The Python SDK is packed in a snap package using the python plugin.
Connections via USB and BLE are working and when trying to connect via WiFi the SSID and password are correctly read via BLE.
The WiFi is also listed in the found SSID's (nmcli device wifi list).

When I'm trying to specify the interface manually I get the error
Failed to connect. Requested WiFi interface [wlan0] not found among [sudo:]

Without the interface specified I get

Wifi connection failed. Retrying #1
Wifi connection failed. Retrying #2
Wifi connection failed. Retrying #3
Wifi connection failed. Retrying #4
Error while opening: Wifi Connection failed connection failed to establish after 5 retries with timeout 10

Reason
I found the reason in the python SDK already. The which commend here results to false, but should be true. Calling cmd(f"sudo nmcli device wifi list") works as expected in the same python script, so the network manager tool is working. Due to this, the wpa_supplicant driver is selected. The /tmp/wpa_supplicant.conf file is created correctly but it cannot connect because the interface is not set. This is because the iwconfig command here is not working in this environment. This also explains the available interfaces list [sudo:] when trying to specify it manually.

To Reproduce
Steps to reproduce the behavior:

  1. Generate a Snap package with the Python SDK
  2. Install network-manager
  3. Try to connect via WiFi

Expected behavior
which should return true.

Hardware

  • Camera: Hero10
  • Raspberry Pi CM4, Ubuntu Core 22 64 Bit
  • open-gopro 0.11.2

Python script

import time
import subprocess
from open_gopro import GoPro, Params
import requests

def cmd(command):
    response = (
        subprocess.Popen(command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
        .stdout.read()
        .decode(errors="ignore")
    )
    return response

def start():
      print('Starting WiFi test ...')
      response = cmd(f"sudo nmcli dev")
      interfaces = []
      for line in response.splitlines():
          if "wifi" in line:
              # this line has our interface name in the first column
              interfaces.append(line.split()[0])
  
      for device in interfaces:
          print("Trying interface " + device + "...")
          try:
              with GoPro(wifi_interface=device, sudo_password="***") as gopro:
                  print("Start test...")
                  gopro.ble_setting.fps.set(Params.FPS.FPS_30)
                  
                  # Get the media list before
                  media_set_before = set(x["n"] for x in gopro.wifi_command.get_media_list().flatten)
  
                  gopro.ble_command.set_shutter(Params.Toggle.ENABLE)
                  time.sleep(2)  # Record for 2 seconds
                  gopro.ble_command.set_shutter(Params.Toggle.DISABLE)
                  
                  # Get the media list after
                  media_set_after = set(x["n"] for x in gopro.wifi_command.get_media_list().flatten)
                  # The photo (is most likely) the difference between the two sets
                  photo = media_set_after.difference(media_set_before).pop()
                  # Download the photo
                  gopro.wifi_command.download_file(camera_file=photo, local_file="./photo.jpg")
                  print(f"Success!! :smiley: File has been downloaded to ./photo.jpg")
                  print("WiFi test OK.")
                  break
          except Exception as e:
              print("Failed to connect. " + str(e))
      
      print("Trying finding interface automatically...")
      try:
          with GoPro(sudo_password="***") as gopro:
              print("Start test...")
              gopro.ble_setting.fps.set(Params.FPS.FPS_30)
              
              # Get the media list before
              media_set_before = set(x["n"] for x in gopro.wifi_command.get_media_list().flatten)
  
              gopro.ble_command.set_shutter(Params.Toggle.ENABLE)
              time.sleep(2)  # Record for 2 seconds
              gopro.ble_command.set_shutter(Params.Toggle.DISABLE)
              
              # Get the media list after
              media_set_after = set(x["n"] for x in gopro.wifi_command.get_media_list().flatten)
              # The photo (is most likely) the difference between the two sets
              photo = media_set_after.difference(media_set_before).pop()
              # Download the photo
              gopro.wifi_command.download_file(camera_file=photo, local_file="./photo.jpg")
              print(f"Success!! :smiley: File has been downloaded to ./photo.jpg")
              print("WiFi test OK.")
      except Exception as e:
              print("Failed to connect. " + str(e))
@andi-h andi-h added the bug Something isn't working label Nov 24, 2022
@tcamise-gpsw tcamise-gpsw self-assigned this Nov 28, 2022
@tcamise-gpsw
Copy link
Collaborator

Hello. Thanks for the investigation here. As a quick disclaimer, I am not familiar with creating Snap packages so I do not know how this changes the behavior.

If I'm understand this correctly, you expect the which nmcli command to succeed and thus use the Nmcli0990Wireless driver, correct?
But why is it returning false? Does which nmcli work from your CLI?
This is using the which command from the Python standard library. I'm not sure I can do anything about that failing. I was originally sending which manually but this was causing other problems so I moved to the standard library implementation.

@andi-h
Copy link
Author

andi-h commented Dec 6, 2022

I did some further tests.
The problem seems to be that the nmcli executable is located in /snap/bin when the network manager is installed as snap package, but the PATH does not contain this location.
According to the snapcraft documentation the PATH is rewritten inside snap packages.

I was not able to modify the PATH inside the snap:

cmd("export PATH=\"$PATH:/snap/bin\"")
cmd("sudo export PATH=\"$PATH:/snap/bin\"")
sys.path.append("/snap/bin")
print("Path: " + os.environ['PATH'])
# returns Path: /snap/goprotest/x55/usr/sbin:/snap/goprotest/x55/usr/bin:/snap/goprotest/x55/sbin:/snap/goprotest/x55/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games

Some tests how to check if nmcli exists:

cmd("which nmcli") # returns nmcli: not found
cmd("sudo which nmcli") # returns /snap/bin/nmcli
cmd("sudo nmcli --version").split()[-1] # returns 1.22.10
which("nmcli") # returns false
which("nmcli", path="/snap/bin/") # returns true

@tcamise-gpsw
Copy link
Collaborator

I'm not sure if there is anything I can do about this, at least in the scope of this project. I suppose the module just does not work in the snap environment.

@andi-h
Copy link
Author

andi-h commented Dec 15, 2022

I think changing this line form

if which("nmcli"):

to

if which("nmcli") or which("nmcli", path="/snap/bin/"):

would solve the problem.

And reading the version in the next line must have sudo privileges (like in all the subsequent nmcli calls).

@tcamise-gpsw
Copy link
Collaborator

Sounds good to me. Feel free to make a Pull Request.

@tcamise-gpsw tcamise-gpsw added demos Relating to demos and removed python_sdk labels Jan 4, 2023
@tcamise-gpsw tcamise-gpsw linked a pull request Dec 6, 2023 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working demos Relating to demos
Projects
Status: Done
Development

Successfully merging a pull request may close this issue.

2 participants