Skip to content

Commit

Permalink
fix open/close FD being unclean after successive open/closes, add ass…
Browse files Browse the repository at this point in the history
…ociated test
  • Loading branch information
willzhang05 committed Apr 30, 2024
1 parent a7e0f7d commit a9b1512
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 6 deletions.
18 changes: 13 additions & 5 deletions plin/device.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@
PLIOSETLEDSTATE = IOW(ord('u'), 40, PLINUSBLEDState)


class PLINException(Exception):
pass


class PLIN:
def __init__(self, interface: str):
self.interface = interface
Expand Down Expand Up @@ -72,19 +76,23 @@ def start(self, mode: PLINMode, baudrate: int = 19200):
self.mode = mode
self.baudrate = baudrate

if not self.fd:
if self.fd:
raise PLINException("Already connected to PLIN device!")
else:
self.fd = os.open(self.interface, os.O_RDWR)

self.reset()
buffer = PLINUSBInitHardware(self.baudrate, self.mode, 0)
self._ioctl(PLIOHWINIT, buffer)
self.reset()
buffer = PLINUSBInitHardware(self.baudrate, self.mode, 0)
self._ioctl(PLIOHWINIT, buffer)

def stop(self):
'''
Disconnects from the PLIN device by closing the file descriptor.
'''
if self.fd:
os.close(self.fd)
self.fd = None
else:
raise PLINException("Not connected to PLIN device!")

def set_frame_entry(self,
id: int,
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"

[project]
name = "plin-linux"
version = "0.0.2"
version = "0.0.3"
authors = [
{ name="William Zhang", email="williamzhang@rivian.com" },
]
Expand Down
19 changes: 19 additions & 0 deletions tests/integration/test_plin.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from plin.enums import PLINMode, PLINMessageType, PLINFrameDirection, PLINFrameChecksumType
from plin.structs import *


@pytest.fixture
def plin_interface():
return "/dev/plin0"
Expand Down Expand Up @@ -47,6 +48,24 @@ def test_start(plin_interface, plin):
plin.stop()


def test_start_stop_start(plin_interface, plin):
expected_baudrate = 1000
expected_mode = PLINMode.SLAVE

plin.start(mode=expected_mode, baudrate=expected_baudrate)
assert plin.get_baudrate() == expected_baudrate
assert plin.get_mode() == expected_mode
plin.stop()

expected_baudrate = 19200
expected_mode = PLINMode.MASTER

plin.start(mode=expected_mode, baudrate=expected_baudrate)
assert plin.get_baudrate() == expected_baudrate
assert plin.get_mode() == expected_mode
plin.stop()


def test_read_nonblocking(plin_master_19200):
assert None == plin_master_19200.read(block=False)

Expand Down

0 comments on commit a9b1512

Please sign in to comment.