sdwire 0.2.1__tar.gz → 0.2.3__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,7 +1,7 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: sdwire
3
- Version: 0.2.1
4
- Summary: CLI application to interact with Badgerd SDWire Gen2 devices
3
+ Version: 0.2.3
4
+ Summary: CLI application to interact with Badgerd SDWire devices
5
5
  License: GPL-3
6
6
  Author: Talha Can Havadar
7
7
  Author-email: havadartalha@gmail.com
@@ -12,6 +12,7 @@ Classifier: Programming Language :: Python :: 3.12
12
12
  Requires-Dist: adafruit-board-toolkit (>=1.1.1,<2.0.0)
13
13
  Requires-Dist: click (>=8.1.7,<9.0.0)
14
14
  Requires-Dist: pyftdi (>=0.55.4,<0.56.0)
15
+ Requires-Dist: pyudev (>=0.24.3,<0.25.0)
15
16
  Requires-Dist: pyusb (>=1.2.1,<2.0.0)
16
17
  Requires-Dist: semver (>=3.0.2,<4.0.0)
17
18
  Description-Content-Type: text/markdown
@@ -1,7 +1,7 @@
1
1
  [tool.poetry]
2
2
  name = "sdwire"
3
- version = "0.2.1"
4
- description = "CLI application to interact with Badgerd SDWire Gen2 devices"
3
+ version = "0.2.3"
4
+ description = "CLI application to interact with Badgerd SDWire devices"
5
5
  authors = ["Talha Can Havadar <havadartalha@gmail.com>"]
6
6
  license = "GPL-3"
7
7
  readme = "README.md"
@@ -14,6 +14,7 @@ adafruit-board-toolkit = "^1.1.1"
14
14
  semver = "^3.0.2"
15
15
  pyusb = "^1.2.1"
16
16
  pyftdi = "^0.55.4"
17
+ pyudev = "^0.24.3"
17
18
 
18
19
 
19
20
  [tool.poetry.group.dev.dependencies]
@@ -4,10 +4,11 @@ from adafruit_board_toolkit import circuitpython_serial as cpserial
4
4
  from serial.tools.list_ports_common import ListPortInfo
5
5
 
6
6
  from sdwire import constants
7
- from .device.sdwire import SDWire
7
+ from .device.sdwire import SDWire, SDWIRE_GENERATION_SDWIRE3
8
8
  from .device.sdwirec import SDWireC
9
9
  from .device.usb_device import PortInfo
10
10
 
11
+ import pyudev
11
12
  import usb.core
12
13
  import usb.util
13
14
  from usb.core import Device
@@ -46,20 +47,49 @@ def get_sdwirec_devices() -> List[SDWireC]:
46
47
 
47
48
 
48
49
  def get_sdwire_devices() -> List[SDWire]:
49
- ports = cpserial.data_comports()
50
50
 
51
- # Badgerd SDWire Gen2
52
- # VID = 0x1209 PID = 0x2404
51
+ # Badgerd SDWire3
52
+ # VID = 0bda PID = 0316
53
53
  # Badgerd SDWireC
54
54
  # VID = 0x04e8 PID = 0x6001
55
55
  result = []
56
- for p in ports:
57
- if p.vid == constants.SDWIRE_GEN2_VID and p.pid == constants.SDWIRE_GEN2_PID:
56
+ devices: List[Device] = pyudev.Context().list_devices(
57
+ subsystem="usb",
58
+ ID_VENDOR_ID=f"{constants.SDWIRE3_VID:04x}",
59
+ ID_MODEL_ID=f"{constants.SDWIRE3_PID:04x}",
60
+ )
61
+ if not devices:
62
+ log.info("no usb devices found while searching for SDWire..")
63
+ return []
64
+
65
+ device_list = []
66
+ for device in devices:
67
+ product = None
68
+ serial = None
69
+ manufacturer = None
70
+ bus = None
71
+ address = None
72
+ try:
73
+ product = int(f"0x{device.get("ID_MODEL_ID")}", 16)
74
+ vendor = int(f"0x{device.get("ID_VENDOR_ID")}", 16)
75
+ bus = int(device.get("BUSNUM"))
76
+ address = int(device.get("DEVNUM"))
77
+ serial = f"{device.get("ID_USB_SERIAL_SHORT")}:{bus}.{address}"
78
+ manufacturer = ""
79
+ except Exception as e:
80
+ log.debug(
81
+ "not able to get usb product, serial_number and manufacturer information, err: %s",
82
+ e,
83
+ )
84
+
85
+ if product == constants.SDWIRE3_PID and vendor == constants.SDWIRE3_VID:
86
+ usb_device: List[Device] = usb.core.find(
87
+ idVendor=vendor, idProduct=product, bus=bus, address=address
88
+ )
58
89
  result.append(
59
90
  SDWire(
60
- port_info=PortInfo(
61
- p.device, p.product, p.manufacturer, p.serial_number, p
62
- )
91
+ port_info=PortInfo(device, product, vendor, serial, usb_device),
92
+ generation=SDWIRE_GENERATION_SDWIRE3,
63
93
  )
64
94
  )
65
95
  # Search for legacy SDWireC devices
@@ -0,0 +1,52 @@
1
+ import logging
2
+ from serial import Serial
3
+ from .usb_device import USBDevice, PortInfo
4
+
5
+ log = logging.getLogger(__name__)
6
+
7
+ SDWIRE_GENERATION_SDWIRE3 = 2
8
+
9
+
10
+ class SDWire(USBDevice):
11
+ __block_dev = None
12
+
13
+ def __init__(self, port_info: PortInfo, generation: int):
14
+ super().__init__(port_info)
15
+ self.generation = generation
16
+ for sibling in self.dev_string.parent.children:
17
+ if (
18
+ self.dev_string.device_path != sibling.device_path
19
+ and sibling.device_type == "disk"
20
+ ):
21
+ self.__block_dev = f"/dev/{sibling.device_path.split("/")[-1]}"
22
+ break
23
+
24
+ def switch_ts(self):
25
+ try:
26
+ self.usb_device.attach_kernel_driver(0)
27
+ self.usb_device.reset()
28
+ except Exception as e:
29
+ log.debug(
30
+ "not able to switch to ts mode. Device might be already in ts mode, err: %s",
31
+ e,
32
+ )
33
+
34
+ def switch_dut(self):
35
+ try:
36
+ self.usb_device.detach_kernel_driver(0)
37
+ self.usb_device.reset()
38
+ except Exception as e:
39
+ log.debug(
40
+ "not able to switch to dut mode. Device might be already in dut mode, err: %s",
41
+ e,
42
+ )
43
+
44
+ @property
45
+ def block_dev(self):
46
+ return self.__block_dev
47
+
48
+ def __str__(self):
49
+ return f"{self.serial_string}\t[{int(self.manufacturer_string):04x}::{int(self.product_string):04x}]\t\t{self.block_dev}"
50
+
51
+ def __repr__(self):
52
+ return self.__str__()
@@ -7,18 +7,32 @@ log = logging.getLogger(__name__)
7
7
 
8
8
 
9
9
  class SDWireC(USBDevice):
10
+ __block_dev = None
10
11
 
11
12
  def __init__(self, port_info: PortInfo):
12
13
  super().__init__(port_info)
14
+ for d in self._pyudev_context.list_devices(ID_MODEL="sd-wire"):
15
+ d_serial = d.get("ID_USB_SERIAL_SHORT", None)
16
+ if d_serial is not None and d_serial == self.serial_string:
17
+ for sibling in d.parent.children:
18
+ if (
19
+ d.device_path != sibling.device_path
20
+ and sibling.device_type == "disk"
21
+ ):
22
+ self.__block_dev = f"/dev/{sibling.device_path.split("/")[-1]}"
23
+ break
24
+ break
13
25
 
14
26
  def __str__(self):
15
- return (
16
- f"{self.serial_string}\t[{self.product_string}::{self.manufacturer_string}]"
17
- )
27
+ return f"{self.serial_string}\t[{self.product_string}::{self.manufacturer_string}]\t{self.block_dev}"
18
28
 
19
29
  def __repr__(self):
20
30
  return self.__str__()
21
31
 
32
+ @property
33
+ def block_dev(self):
34
+ return self.__block_dev
35
+
22
36
  def switch_ts(self):
23
37
  self._set_sdwire(1)
24
38
 
@@ -1,4 +1,5 @@
1
1
  from collections import namedtuple
2
+ import pyudev
2
3
  from serial.tools.list_ports_common import ListPortInfo
3
4
 
4
5
  PortInfo = namedtuple(
@@ -8,9 +9,11 @@ PortInfo = namedtuple(
8
9
 
9
10
  class USBDevice:
10
11
  __port_info = None
12
+ _pyudev_context = None
11
13
 
12
14
  def __init__(self, port_info: PortInfo):
13
15
  self.__port_info = port_info
16
+ self._pyudev_context = pyudev.Context()
14
17
 
15
18
  @property
16
19
  def usb_device(self):
@@ -10,14 +10,12 @@ log = logging.getLogger(__name__)
10
10
 
11
11
  def handle_switch_host_command(ctx):
12
12
  device = ctx.obj["device"]
13
- if isinstance(device, SDWireC):
14
- device.switch_ts()
13
+ device.switch_ts()
15
14
 
16
15
 
17
16
  def handle_switch_target_command(ctx):
18
17
  device = ctx.obj["device"]
19
- if isinstance(device, SDWireC):
20
- device.switch_dut()
18
+ device.switch_dut()
21
19
 
22
20
 
23
21
  def handle_switch_off_command(ctx):
@@ -50,7 +48,3 @@ def handle_switch_command(ctx, serial):
50
48
  raise click.UsageError(
51
49
  f"There is no such sdwire device connected with serial={serial}"
52
50
  )
53
-
54
- device = ctx.obj["device"]
55
- if isinstance(device, SDWire):
56
- device.invoke(" ".join(sys.argv[1:]))
@@ -1,5 +1,5 @@
1
- SDWIRE_GEN2_VID = 0x1209
2
- SDWIRE_GEN2_PID = 0x2404
1
+ SDWIRE3_VID = 0x0BDA
2
+ SDWIRE3_PID = 0x0316
3
3
  SDWIREC_VID = 0x04E8
4
4
  SDWIREC_PID = 0x6001
5
5
  SDWIREC_PRODUCT_STRING = "sd-wire"
@@ -1,4 +1,5 @@
1
1
  #!/usr/bin/env python
2
+ import logging
2
3
  import click
3
4
  from .backend import utils
4
5
  from .backend import detect
@@ -6,14 +7,15 @@ from .backend.device.sdwire import SDWire
6
7
 
7
8
 
8
9
  @click.group()
9
- def main():
10
- pass
10
+ @click.option("--debug", required=False, is_flag=True, help="Enable debug output")
11
+ def main(debug=None):
12
+ if debug:
13
+ logging.basicConfig(level=logging.DEBUG)
11
14
 
12
15
 
13
16
  @main.command()
14
17
  def list():
15
-
16
- print(f"Serial\t\t\tProduct Info")
18
+ print(f"Serial\t\t\tProduct Info\t\tBlock Dev")
17
19
  for sdwire in detect.get_sdwire_devices():
18
20
  print(sdwire)
19
21
 
@@ -1,25 +0,0 @@
1
- from serial import Serial
2
- from .usb_device import USBDevice, PortInfo
3
-
4
-
5
- class SDWire(USBDevice):
6
-
7
- def __init__(self, port_info: PortInfo):
8
- super().__init__(port_info)
9
-
10
- @property
11
- def serial_string(self) -> str:
12
- return "sdwire_gen2_101"
13
-
14
- def invoke(self, command: str) -> None:
15
- with Serial(self.dev_string) as channel:
16
- channel.timeout = 0.05
17
- data = command.encode() + b"\n"
18
- print(f"data={data}")
19
- channel.write(data)
20
-
21
- def __str__(self):
22
- return f"{self.serial_string}\t\t[{self.product_string}::{self.manufacturer_string}]"
23
-
24
- def __repr__(self):
25
- return self.__str__()
File without changes
File without changes
File without changes