Skip to content

Commit

Permalink
Refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
aBozowski committed Nov 3, 2023
1 parent b9dca9d commit b7d115d
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 28 deletions.
2 changes: 2 additions & 0 deletions src/tools/interop/idt/capture/pcap/pcap.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import time

from utils.artifact import create_standard_log_name, log
from utils.host_platform import verify_host_dependencies
from utils.shell import Bash

logger = log.get_logger(__file__)
Expand All @@ -27,6 +28,7 @@
class PacketCaptureRunner:

def __init__(self, artifact_dir: str, interface: str) -> None:
verify_host_dependencies(["tcpdump"])
self.logger = logger
self.artifact_dir = artifact_dir
self.output_path = str(
Expand Down
2 changes: 2 additions & 0 deletions src/tools/interop/idt/capture/platform/android/android.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import typing

from capture.base import PlatformLogStreamer
from utils.host_platform import verify_host_dependencies
from utils.shell import Bash, log

from . import config, streams
Expand All @@ -32,6 +33,7 @@
class Android(PlatformLogStreamer):

def __init__(self, artifact_dir: str) -> None:
verify_host_dependencies(["adb"])
self.logger = logger
self.artifact_dir = artifact_dir
self.device_id: str | None = None
Expand Down
3 changes: 1 addition & 2 deletions src/tools/interop/idt/capture/platform/android/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,5 @@
#

enable_build_push_tcpdump = True
# TODO: Replace
enable_bug_report = False
enable_bug_report = True
hci_log_level = "full"
2 changes: 0 additions & 2 deletions src/tools/interop/idt/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,3 @@

enable_color = True
log_level = logging.INFO
# TODO: RE-Enable
enable_generic_prober_in_capture = False
27 changes: 3 additions & 24 deletions src/tools/interop/idt/idt.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@

from capture import EcosystemController, EcosystemFactory, PacketCaptureRunner, PlatformFactory
from utils.artifact import create_file_timestamp, safe_mkdir
from utils.host_platform import get_available_interfaces
from utils.shell import Bash
from discovery import MatterBleScanner, MatterDnssdListener
from utils.log import border_print
Expand Down Expand Up @@ -82,12 +83,8 @@ def __init__(self) -> None:
self.available_ecosystems_default = 'ALL'
self.available_ecosystems.append(self.available_ecosystems_default)

net_interface_path = "/sys/class/net/"
self.available_net_interfaces = os.listdir(net_interface_path) \
if os.path.exists(net_interface_path) \
else []
self.available_net_interfaces.append("any")
self.available_net_interfaces_default = "any"
self.available_net_interfaces = get_available_interfaces()
self.available_net_interfaces_default = "any" if "any" in self.available_net_interfaces else None
self.pcap_artifact_dir = os.path.join(self.artifact_dir, "pcap")
self.net_interface_required = self.available_net_interfaces_default is None

Expand All @@ -97,25 +94,7 @@ def __init__(self) -> None:

self.process_args()

@staticmethod
def command_is_available(cmd_name) -> bool:
cmd = Bash(f"which {cmd_name}", sync=True, capture_output=True)
cmd.start_command()
return cmd.finished_success()

def verify_host_dependencies(self) -> None:
deps = ["tcpdump", "adb"]
missing_deps = []
for dep in deps:
if not self.command_is_available(dep):
missing_deps.append(dep)
if len(missing_deps) > 0:
for missing_dep in missing_deps:
border_print(f"Missing dependency, please install {missing_dep}!")
sys.exit(1)

def process_args(self) -> None:
self.verify_host_dependencies() # TODO: host dependency should be checked *per feature*
parser = argparse.ArgumentParser(
prog="idt",
description="Interop Debugging Tool for Matter")
Expand Down
41 changes: 41 additions & 0 deletions src/tools/interop/idt/utils/host_platform.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,15 @@

import os
import platform as host_platform
import sys

import psutil

from utils import log
from utils.log import border_print
from utils.shell import Bash

logger = log.get_logger(__file__)

def is_mac():
p = host_platform.platform().lower()
Expand All @@ -35,3 +43,36 @@ def get_ll_interface():
for interface in available_net_interfaces:
if "wl" in interface:
return interface


def get_available_interfaces():
net_interface_path = "/sys/class/net/"
available_net_interfaces = os.listdir(net_interface_path) \
if os.path.exists(net_interface_path) \
else []
available_net_interfaces.append("any")
return available_net_interfaces


def command_is_available(cmd_name) -> bool:
cmd = Bash(f"which {cmd_name}", sync=True, capture_output=True)
cmd.start_command()
return cmd.finished_success()


def verify_host_dependencies(deps: [str]) -> None:
missing_deps = []
for dep in deps:
logger.info(f"Verifying host dependency {dep}")
if not command_is_available(dep):
missing_deps.append(dep)
if len(missing_deps) > 0:
for missing_dep in missing_deps:
border_print(f"Missing dependency, please install {missing_dep}!", important=True)
kill_idt_children()
sys.exit(1)


def kill_idt_children() -> None:
for child_proc in psutil.Process(os.getpid()).children(recursive=True):
Bash("").stop_single_proc(child_proc)

0 comments on commit b7d115d

Please sign in to comment.