diff options
author | Devaev Maxim <[email protected]> | 2020-10-29 13:51:58 +0300 |
---|---|---|
committer | Devaev Maxim <[email protected]> | 2020-11-11 22:24:25 +0300 |
commit | a5dbc1adeaa0d790206580d5f94c7503808ea1f0 (patch) | |
tree | ddb8d85b841cf075eafffbdf24e98f7cbfc0be70 /kvmd/plugins | |
parent | aaef672ac2ce5dd2cee17ad571e6cab35f284ee4 (diff) |
refactoring
Diffstat (limited to 'kvmd/plugins')
-rw-r--r-- | kvmd/plugins/hid/_mcu/__init__.py | 4 | ||||
-rw-r--r-- | kvmd/plugins/hid/serial.py | 7 | ||||
-rw-r--r-- | kvmd/plugins/hid/spi.py | 33 |
3 files changed, 23 insertions, 21 deletions
diff --git a/kvmd/plugins/hid/_mcu/__init__.py b/kvmd/plugins/hid/_mcu/__init__.py index 71981e0d..f762567b 100644 --- a/kvmd/plugins/hid/_mcu/__init__.py +++ b/kvmd/plugins/hid/_mcu/__init__.py @@ -153,7 +153,7 @@ class _MouseWheelEvent(_BaseEvent): # ===== class BasePhyConnection: - def send(self, request: bytes, receive: int) -> bytes: + def send(self, request: bytes) -> bytes: raise NotImplementedError @@ -396,7 +396,7 @@ class BaseMcuHid(BaseHid, multiprocessing.Process): # pylint: disable=too-many- def __send_request(self, conn: BasePhyConnection, request: bytes) -> bytes: if not self.__noop: - response = conn.send(request, 4) + response = conn.send(request) else: response = b"\x33\x20" # Magic + OK response += struct.pack(">H", self.__make_crc16(response)) diff --git a/kvmd/plugins/hid/serial.py b/kvmd/plugins/hid/serial.py index d5de1cc7..9bfafc69 100644 --- a/kvmd/plugins/hid/serial.py +++ b/kvmd/plugins/hid/serial.py @@ -45,11 +45,12 @@ class _SerialPhyConnection(BasePhyConnection): def __init__(self, tty: serial.Serial) -> None: self.__tty = tty - def send(self, request: bytes, receive: int) -> bytes: + def send(self, request: bytes) -> bytes: + assert len(request) == 8 if self.__tty.in_waiting: self.__tty.read_all() - assert self.__tty.write(request) == len(request) - return self.__tty.read(receive) + assert self.__tty.write(request) == 8 + return self.__tty.read(4) class _SerialPhy(BasePhy): diff --git a/kvmd/plugins/hid/spi.py b/kvmd/plugins/hid/spi.py index 77d2f34c..6020e04e 100644 --- a/kvmd/plugins/hid/spi.py +++ b/kvmd/plugins/hid/spi.py @@ -31,6 +31,8 @@ from typing import Any import spidev +from ...logging import get_logger + from ...yamlconf import Option from ...validators.basic import valid_int_f0 @@ -44,10 +46,6 @@ from ._mcu import BaseMcuHid # ===== -class SpiPhyError(Exception): - pass - - class _SpiPhyConnection(BasePhyConnection): def __init__( self, @@ -60,41 +58,44 @@ class _SpiPhyConnection(BasePhyConnection): self.__read_timeout = read_timeout self.__read_delay = read_delay - def send(self, request: bytes, receive: int) -> bytes: - assert 0 < receive <= len(request) + self.__empty8 = b"\x00" * 8 + self.__empty4 = b"\x00" * 4 + + def send(self, request: bytes) -> bytes: + assert len(request) == 8 - dummy = b"\x00" * len(request) deadline_ts = time.time() + self.__read_timeout while time.time() < deadline_ts: - garbage = bytes(self.__spi.xfer(dummy)) - if garbage == dummy: + garbage = bytes(self.__spi.xfer(self.__empty8)) + if garbage == self.__empty8: break else: - raise SpiPhyError("Timeout reached while reading a garbage") + get_logger(0).error("SPI timeout reached while reading the a garbage") + return b"" self.__spi.xfer(request) response: List[int] = [] - dummy = b"\x00" * receive deadline_ts = time.time() + self.__read_timeout found = False while time.time() < deadline_ts: if not found: time.sleep(self.__read_delay) - for byte in self.__spi.xfer(dummy): + for byte in self.__spi.xfer(self.__empty4): if not found: if byte == 0: continue found = True response.append(byte) - if len(response) >= receive: + if len(response) >= 4: break - if len(response) >= receive: + if len(response) >= 4: break else: - raise SpiPhyError("Timeout reached while responce waiting") + get_logger(0).error("SPI timeout reached while responce waiting") + return b"" - assert len(response) == receive + assert len(response) == 4 return bytes(response) |