Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "sdwire"
version = "0.2.2"
version = "0.2.3"
description = "CLI application to interact with Badgerd SDWire devices"
authors = ["Talha Can Havadar <havadartalha@gmail.com>"]
license = "GPL-3"
Expand All @@ -14,6 +14,7 @@ adafruit-board-toolkit = "^1.1.1"
semver = "^3.0.2"
pyusb = "^1.2.1"
pyftdi = "^0.55.4"
pyudev = "^0.24.3"


[tool.poetry.group.dev.dependencies]
Expand Down
27 changes: 18 additions & 9 deletions sdwire/backend/detect.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from .device.sdwirec import SDWireC
from .device.usb_device import PortInfo

import pyudev
import usb.core
import usb.util
from usb.core import Device
Expand Down Expand Up @@ -52,7 +53,11 @@ def get_sdwire_devices() -> List[SDWire]:
# Badgerd SDWireC
# VID = 0x04e8 PID = 0x6001
result = []
devices: List[Device] = usb.core.find(find_all=True)
devices: List[Device] = pyudev.Context().list_devices(
subsystem="usb",
ID_VENDOR_ID=f"{constants.SDWIRE3_VID:04x}",
ID_MODEL_ID=f"{constants.SDWIRE3_PID:04x}",
)
if not devices:
log.info("no usb devices found while searching for SDWire..")
return []
Expand All @@ -62,24 +67,28 @@ def get_sdwire_devices() -> List[SDWire]:
product = None
serial = None
manufacturer = None
bus = None
address = None
try:
product = device.idProduct
vendor = device.idVendor
serial = (
usb.util.get_string(device, device.iSerialNumber, None)
+ f"{device.bus}{device.port_number}"
)
manufacturer = device.manufacturer
product = int(f"0x{device.get("ID_MODEL_ID")}", 16)
vendor = int(f"0x{device.get("ID_VENDOR_ID")}", 16)
bus = int(device.get("BUSNUM"))
address = int(device.get("DEVNUM"))
serial = f"{device.get("ID_USB_SERIAL_SHORT")}:{bus}.{address}"
manufacturer = ""
except Exception as e:
log.debug(
"not able to get usb product, serial_number and manufacturer information, err: %s",
e,
)

if product == constants.SDWIRE3_PID and vendor == constants.SDWIRE3_VID:
usb_device: List[Device] = usb.core.find(
idVendor=vendor, idProduct=product, bus=bus, address=address
)
result.append(
SDWire(
port_info=PortInfo(device, product, vendor, serial, device),
port_info=PortInfo(device, product, vendor, serial, usb_device),
generation=SDWIRE_GENERATION_SDWIRE3,
)
)
Expand Down
14 changes: 13 additions & 1 deletion sdwire/backend/device/sdwire.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,18 @@


class SDWire(USBDevice):
__block_dev = None

def __init__(self, port_info: PortInfo, generation: int):
super().__init__(port_info)
self.generation = generation
for sibling in self.dev_string.parent.children:
if (
self.dev_string.device_path != sibling.device_path
and sibling.device_type == "disk"
):
self.__block_dev = f"/dev/{sibling.device_path.split("/")[-1]}"
break

def switch_ts(self):
try:
Expand All @@ -33,8 +41,12 @@ def switch_dut(self):
e,
)

@property
def block_dev(self):
return self.__block_dev

def __str__(self):
return f"{self.serial_string}\t[{int(self.manufacturer_string):04x}::{int(self.product_string):04x}]"
return f"{self.serial_string}\t[{int(self.manufacturer_string):04x}::{int(self.product_string):04x}]\t\t{self.block_dev}"

def __repr__(self):
return self.__str__()
20 changes: 17 additions & 3 deletions sdwire/backend/device/sdwirec.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,32 @@


class SDWireC(USBDevice):
__block_dev = None

def __init__(self, port_info: PortInfo):
super().__init__(port_info)
for d in self._pyudev_context.list_devices(ID_MODEL="sd-wire"):
d_serial = d.get("ID_USB_SERIAL_SHORT", None)
if d_serial is not None and d_serial == self.serial_string:
for sibling in d.parent.children:
if (
d.device_path != sibling.device_path
and sibling.device_type == "disk"
):
self.__block_dev = f"/dev/{sibling.device_path.split("/")[-1]}"
break
break

def __str__(self):
return (
f"{self.serial_string}\t[{self.product_string}::{self.manufacturer_string}]"
)
return f"{self.serial_string}\t[{self.product_string}::{self.manufacturer_string}]\t{self.block_dev}"

def __repr__(self):
return self.__str__()

@property
def block_dev(self):
return self.__block_dev

def switch_ts(self):
self._set_sdwire(1)

Expand Down
3 changes: 3 additions & 0 deletions sdwire/backend/device/usb_device.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from collections import namedtuple
import pyudev
from serial.tools.list_ports_common import ListPortInfo

PortInfo = namedtuple(
Expand All @@ -8,9 +9,11 @@

class USBDevice:
__port_info = None
_pyudev_context = None

def __init__(self, port_info: PortInfo):
self.__port_info = port_info
self._pyudev_context = pyudev.Context()

@property
def usb_device(self):
Expand Down
10 changes: 6 additions & 4 deletions sdwire/main.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,21 @@
#!/usr/bin/env python
import logging
import click
from .backend import utils
from .backend import detect
from .backend.device.sdwire import SDWire


@click.group()
def main():
pass
@click.option("--debug", required=False, is_flag=True, help="Enable debug output")
def main(debug=None):
if debug:
logging.basicConfig(level=logging.DEBUG)


@main.command()
def list():

print(f"Serial\t\t\tProduct Info")
print(f"Serial\t\t\tProduct Info\t\tBlock Dev")
for sdwire in detect.get_sdwire_devices():
print(sdwire)

Expand Down
Loading