Skip to content

Commit

Permalink
updated & enabled huaweicheck
Browse files Browse the repository at this point in the history
  • Loading branch information
jonashoechst committed May 31, 2023
1 parent a614476 commit f027b2c
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 16 deletions.
73 changes: 57 additions & 16 deletions home/pi/huaweicheck.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,33 +4,74 @@
import os
import logging
import subprocess
from typing import List, Tuple


def match_usb_manuf(target):
return [os.path.split(p)[0] for p in glob.glob("/sys/bus/usb/devices/*/manufacturer") if target in open(p).read()]
def match_usb_manuf(target) -> Tuple[str, str]:
return [os.path.split(p)[0] for p in glob.glob("/sys/bus/usb/devices/*/manufacturer") if target in open(p, encoding="ascii").read()]


if __name__ == "__main__":
logging.basicConfig(level=logging.INFO)
def get_paths(targets: Tuple[str] = ("HUAWEI_MOBILE", "Mobile")) -> List[Tuple[str, str]]:
huawei_paths = []
for target in targets:
huawei_paths += [os.path.split(p)[1].split(".") for p in match_usb_manuf(target)]

if huawei_paths:
logging.info("Found %s devices at %s", targets, huawei_paths)
return huawei_paths
else:
logging.warning("Did not find %s devices.", targets)
exit(1)


def cycle_paths(huawei_paths: List[Tuple[str, str]]):
# power cycle hub
for location, port in huawei_paths:
cmd = ["uhubctl", "--location", location, "--action", "2"]
logging.info("Power cycling cmd: `%s`", " ".join(cmd))
p = subprocess.Popen(cmd)
p.wait()
if p.returncode:
logging.warning("Device %s cycling failed (%s), please check if hardware is working", location, p.returncode)


def ping_find_iface(candidates: Tuple[str]) -> List[str]:
return [i for i in os.listdir('/sys/class/net/') if i in candidates]


target = "HUAWEI_MOBILE"
ping_target = "8.8.8.8"
huawei_paths = [os.path.split(p)[1].split(".") for p in match_usb_manuf(target)]
def ping_cmd(ping_target: str = "8.8.8.8", iface_candidates: Tuple[str] = ("eth1", "usb0")) -> List[str]:
# define basic ping command
cmd = ["ping", ping_target, "-c", "1"]

logging.info(f"Found '{target}' devices at {huawei_paths}")
# find huawei ethernet device
ifaces = ping_find_iface(iface_candidates)
if len(ifaces):
logging.info("Found Huawei interface candidates: %s, using first.", ifaces)
cmd += ["-I", ifaces[0]]
else:
logging.warning("Did not find Huawei interface candidates, ignoring binding.")

logging.info(f"Running ping test for {ping_target}")
p = subprocess.Popen(["ping", ping_target, "-c", "1"])
return cmd


def ping_evaluate(cmd: List[str]):
logging.info("Running ping test: `%s`", " ".join(cmd))
p = subprocess.Popen(cmd)
p.wait()
if not p.returncode:
logging.info("Ping test succeeded, exiting.")
exit(0)

logging.warning(f"Ping test failed ({p.returncode}), cycling {target} device.")
for location, port in huawei_paths:
p = subprocess.Popen(["uhubctl", "--location", location, "--ports", port, "--action", "2"])
p.wait()
if p.returncode:
logging.warning(f"Device {location}.{port} cycling failed ({p.returncode}), please check")
logging.info("Ping test failed...")


if __name__ == "__main__":
logging.basicConfig(level=logging.INFO)

# run ping test
ping_evaluate(ping_cmd())

# get huawei usb path
cycle_paths(get_paths())

logging.info("Finished.")
1 change: 1 addition & 0 deletions tRackIT-OS.Pifile
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,7 @@ RUN systemctl enable dmesgdump

# Install uhubctl (dependency) and enable huaweicheck
RUN bash -c 'cd /home/pi/uhubctl; make; make install'
RUN systemctl enable huaweicheck.timer

# Blacklist DVB-T driver
RUN tee -a /etc/modprobe.d/raspi-blacklist.conf <<<'blacklist dvb_usb_rtl28xxu'
Expand Down

0 comments on commit f027b2c

Please sign in to comment.