summaryrefslogtreecommitdiff
path: root/kvmd
diff options
context:
space:
mode:
authorDevaev Maxim <[email protected]>2020-11-22 14:33:18 +0300
committerDevaev Maxim <[email protected]>2020-11-22 14:33:18 +0300
commitb7e0ee3300ddca0b4c7049ee9bfb172996b56ff2 (patch)
treed5ac6caf34cf0eeef3e464b7f1102cc8e3ddf469 /kvmd
parentd8a631ab84de53ad387c452f55b6060bcc2300c0 (diff)
parent48550d2e78acb7e2669f8bd9595330c4001302f1 (diff)
Merge branch 'multihid'
Diffstat (limited to 'kvmd')
-rw-r--r--kvmd/aiomulti.py19
-rw-r--r--kvmd/apps/__init__.py5
-rw-r--r--kvmd/apps/kvmd/api/hid.py20
-rw-r--r--kvmd/plugins/hid/__init__.py12
-rw-r--r--kvmd/plugins/hid/_mcu/__init__.py251
-rw-r--r--kvmd/plugins/hid/_mcu/proto.py202
-rw-r--r--kvmd/plugins/hid/bt/__init__.py9
-rw-r--r--kvmd/plugins/hid/otg/__init__.py6
-rw-r--r--kvmd/plugins/hid/serial.py5
-rw-r--r--kvmd/plugins/hid/spi.py10
-rw-r--r--kvmd/tools.py4
-rw-r--r--kvmd/validators/hid.py56
-rw-r--r--kvmd/validators/kvm.py21
13 files changed, 413 insertions, 207 deletions
diff --git a/kvmd/aiomulti.py b/kvmd/aiomulti.py
index c0fca419..490b787a 100644
--- a/kvmd/aiomulti.py
+++ b/kvmd/aiomulti.py
@@ -25,7 +25,9 @@ import queue
from typing import Tuple
from typing import Dict
+from typing import Type
from typing import TypeVar
+from typing import Generic
from typing import Optional
from . import aiotools
@@ -71,14 +73,19 @@ class AioProcessNotifier:
# =====
-class AioSharedFlags:
+_SharedFlagT = TypeVar("_SharedFlagT", int, bool)
+
+
+class AioSharedFlags(Generic[_SharedFlagT]):
def __init__(
self,
- initial: Dict[str, bool],
+ initial: Dict[str, _SharedFlagT],
notifier: AioProcessNotifier,
+ type: Type[_SharedFlagT]=bool, # pylint: disable=redefined-builtin
) -> None:
self.__notifier = notifier
+ self.__type: Type[_SharedFlagT] = type
self.__flags = {
key: multiprocessing.RawValue("i", int(value)) # type: ignore
@@ -87,7 +94,7 @@ class AioSharedFlags:
self.__lock = multiprocessing.Lock()
- def update(self, **kwargs: bool) -> None:
+ def update(self, **kwargs: _SharedFlagT) -> None:
changed = False
with self.__lock:
for (key, value) in kwargs.items():
@@ -98,12 +105,12 @@ class AioSharedFlags:
if changed:
self.__notifier.notify()
- async def get(self) -> Dict[str, bool]:
+ async def get(self) -> Dict[str, _SharedFlagT]:
return (await aiotools.run_async(self.__inner_get))
- def __inner_get(self) -> Dict[str, bool]:
+ def __inner_get(self) -> Dict[str, _SharedFlagT]:
with self.__lock:
return {
- key: bool(shared.value)
+ key: self.__type(shared.value)
for (key, shared) in self.__flags.items()
}
diff --git a/kvmd/apps/__init__.py b/kvmd/apps/__init__.py
index b8a4c160..3190f62c 100644
--- a/kvmd/apps/__init__.py
+++ b/kvmd/apps/__init__.py
@@ -84,11 +84,12 @@ from ..validators.net import valid_ports_list
from ..validators.net import valid_mac
from ..validators.net import valid_ssl_ciphers
+from ..validators.hid import valid_hid_key
+from ..validators.hid import valid_hid_mouse_move
+
from ..validators.kvm import valid_stream_quality
from ..validators.kvm import valid_stream_fps
from ..validators.kvm import valid_stream_resolution
-from ..validators.kvm import valid_hid_key
-from ..validators.kvm import valid_hid_mouse_move
from ..validators.kvm import valid_ugpio_driver
from ..validators.kvm import valid_ugpio_channel
from ..validators.kvm import valid_ugpio_mode
diff --git a/kvmd/apps/kvmd/api/hid.py b/kvmd/apps/kvmd/api/hid.py
index 1f51a0e4..a988ab34 100644
--- a/kvmd/apps/kvmd/api/hid.py
+++ b/kvmd/apps/kvmd/api/hid.py
@@ -38,10 +38,12 @@ from ....validators import raise_error
from ....validators.basic import valid_bool
from ....validators.basic import valid_int_f0
from ....validators.os import valid_printable_filename
-from ....validators.kvm import valid_hid_key
-from ....validators.kvm import valid_hid_mouse_move
-from ....validators.kvm import valid_hid_mouse_button
-from ....validators.kvm import valid_hid_mouse_delta
+from ....validators.hid import valid_hid_keyboard_output
+from ....validators.hid import valid_hid_mouse_output
+from ....validators.hid import valid_hid_key
+from ....validators.hid import valid_hid_mouse_move
+from ....validators.hid import valid_hid_mouse_button
+from ....validators.hid import valid_hid_mouse_delta
from ....keyboard.keysym import build_symmap
from ....keyboard.printer import text_to_web_keys
@@ -67,6 +69,16 @@ class HidApi:
async def __state_handler(self, _: Request) -> Response:
return make_json_response(await self.__hid.get_state())
+ @exposed_http("POST", "/hid/keyboard/set_params")
+ async def __keyboard_set_params_handler(self, request: Request) -> Response:
+ self.__hid.set_keyboard_output(valid_hid_keyboard_output(request.query.get("output")))
+ return make_json_response()
+
+ @exposed_http("POST", "/hid/mouse/set_params")
+ async def __mouse_set_params_handler(self, request: Request) -> Response:
+ self.__hid.set_mouse_output(valid_hid_mouse_output(request.query.get("output")))
+ return make_json_response()
+
@exposed_http("POST", "/hid/reset")
async def __reset_handler(self, _: Request) -> Response:
await self.__hid.reset()
diff --git a/kvmd/plugins/hid/__init__.py b/kvmd/plugins/hid/__init__.py
index f57379aa..8baceb28 100644
--- a/kvmd/plugins/hid/__init__.py
+++ b/kvmd/plugins/hid/__init__.py
@@ -57,14 +57,22 @@ class BaseHid(BasePlugin):
raise NotImplementedError
def send_mouse_move_event(self, to_x: int, to_y: int) -> None:
- raise NotImplementedError
+ _ = to_x
+ _ = to_y
def send_mouse_relative_event(self, delta_x: int, delta_y: int) -> None:
- raise NotImplementedError
+ _ = delta_x
+ _ = delta_y
def send_mouse_wheel_event(self, delta_x: int, delta_y: int) -> None:
raise NotImplementedError
+ def set_keyboard_output(self, output: str) -> None:
+ _ = output
+
+ def set_mouse_output(self, output: str) -> None:
+ _ = output
+
def clear_events(self) -> None:
raise NotImplementedError
diff --git a/kvmd/plugins/hid/_mcu/__init__.py b/kvmd/plugins/hid/_mcu/__init__.py
index b46c876d..6e0f902e 100644
--- a/kvmd/plugins/hid/_mcu/__init__.py
+++ b/kvmd/plugins/hid/_mcu/__init__.py
@@ -22,10 +22,8 @@
import os
import multiprocessing
-import dataclasses
import contextlib
import queue
-import struct
import time
from typing import Tuple
@@ -37,8 +35,6 @@ from typing import AsyncGenerator
from ....logging import get_logger
-from ....keyboard.mappings import KEYMAP
-
from .... import tools
from .... import aiotools
from .... import aiomulti
@@ -56,6 +52,22 @@ from .. import BaseHid
from .gpio import Gpio
+from .proto import REQUEST_PING
+from .proto import REQUEST_REPEAT
+from .proto import RESPONSE_LEGACY_OK
+from .proto import BaseEvent
+from .proto import SetKeyboardOutputEvent
+from .proto import SetMouseOutputEvent
+from .proto import ClearEvent
+from .proto import KeyEvent
+from .proto import MouseButtonEvent
+from .proto import MouseMoveEvent
+from .proto import MouseRelativeEvent
+from .proto import MouseWheelEvent
+from .proto import get_active_keyboard
+from .proto import get_active_mouse
+from .proto import check_response
+
# =====
class _RequestError(Exception):
@@ -73,84 +85,6 @@ class _TempRequestError(_RequestError):
# =====
-class _BaseEvent:
- def make_command(self) -> bytes:
- raise NotImplementedError
-
-
-class _ClearEvent(_BaseEvent):
- def make_command(self) -> bytes:
- return b"\x10\x00\x00\x00\x00"
-
-
[email protected](frozen=True)
-class _KeyEvent(_BaseEvent):
- name: str
- state: bool
-
- def __post_init__(self) -> None:
- assert self.name in KEYMAP
-
- def make_command(self) -> bytes:
- code = KEYMAP[self.name].mcu.code
- return struct.pack(">BBBxx", 0x11, code, int(self.state))
-
-
[email protected](frozen=True)
-class _MouseButtonEvent(_BaseEvent):
- name: str
- state: bool
-
- def __post_init__(self) -> None:
- assert self.name in ["left", "right", "middle", "up", "down"]
-
- def make_command(self) -> bytes:
- (code, state_pressed, is_main) = {
- "left": (0b10000000, 0b00001000, True),
- "right": (0b01000000, 0b00000100, True),
- "middle": (0b00100000, 0b00000010, True),
- "up": (0b10000000, 0b00001000, False), # Back
- "down": (0b01000000, 0b00000100, False), # Forward
- }[self.name]
- if self.state:
- code |= state_pressed
- if is_main:
- main_code = code
- extra_code = 0
- else:
- main_code = 0
- extra_code = code
- return struct.pack(">BBBxx", 0x13, main_code, extra_code)
-
-
[email protected](frozen=True)
-class _MouseMoveEvent(_BaseEvent):
- to_x: int
- to_y: int
-
- def __post_init__(self) -> None:
- assert -32768 <= self.to_x <= 32767
- assert -32768 <= self.to_y <= 32767
-
- def make_command(self) -> bytes:
- return struct.pack(">Bhh", 0x12, self.to_x, self.to_y)
-
-
[email protected](frozen=True)
-class _MouseWheelEvent(_BaseEvent):
- delta_x: int
- delta_y: int
-
- def __post_init__(self) -> None:
- assert -127 <= self.delta_x <= 127
- assert -127 <= self.delta_y <= 127
-
- def make_command(self) -> bytes:
- # Горизонтальная прокрутка пока не поддерживается
- return struct.pack(">Bxbxx", 0x14, self.delta_y)
-
-
-# =====
class BasePhyConnection:
def send(self, request: bytes) -> bytes:
raise NotImplementedError
@@ -192,16 +126,13 @@ class BaseMcuHid(BaseHid, multiprocessing.Process): # pylint: disable=too-many-
self.__phy = phy
self.__gpio = Gpio(reset_pin, reset_inverted, reset_delay)
- self.__events_queue: "multiprocessing.Queue[_BaseEvent]" = multiprocessing.Queue()
+ self.__events_queue: "multiprocessing.Queue[BaseEvent]" = multiprocessing.Queue()
self.__notifier = aiomulti.AioProcessNotifier()
self.__state_flags = aiomulti.AioSharedFlags({
- "keyboard_online": True,
- "mouse_online": True,
- "caps": False,
- "scroll": False,
- "num": False,
- }, self.__notifier)
+ "online": 0,
+ "status": 0,
+ }, self.__notifier, type=int)
self.__stop_event = multiprocessing.Event()
@@ -226,19 +157,51 @@ class BaseMcuHid(BaseHid, multiprocessing.Process): # pylint: disable=too-many-
async def get_state(self) -> Dict:
state = await self.__state_flags.get()
+ online = bool(state["online"])
+ pong = (state["status"] >> 16) & 0xFF
+ outputs = (state["status"] >> 8) & 0xFF
+ features = state["status"] & 0xFF
+
+ absolute = True
+ active_mouse = get_active_mouse(outputs)
+ if online and active_mouse in ["usb_rel", "ps2"]:
+ absolute = False
+
+ keyboard_outputs: Dict = {"available": {}, "active": ""}
+ mouse_outputs: Dict = {"available": {}, "active": ""}
+
+ if outputs & 0b10000000: # Dynamic
+ if features & 0b00000001: # USB
+ keyboard_outputs["available"]["usb"] = {"name": "USB"}
+ mouse_outputs["available"]["usb"] = {"name": "USB", "absolute": True}
+ mouse_outputs["available"]["usb_rel"] = {"name": "USB Relative", "absolute": False}
+
+ if features & 0b00000010: # PS/2
+ keyboard_outputs["available"]["ps2"] = {"name": "PS/2"}
+ mouse_outputs["available"]["ps2"] = {"name": "PS/2"}
+
+ active_keyboard = get_active_keyboard(outputs)
+ if active_keyboard in keyboard_outputs["available"]:
+ keyboard_outputs["active"] = active_keyboard
+
+ if active_mouse in mouse_outputs["available"]:
+ mouse_outputs["active"] = active_mouse
+
return {
- "online": (state["keyboard_online"] and state["mouse_online"]),
+ "online": online,
"keyboard": {
- "online": state["keyboard_online"],
+ "online": (online and not (pong & 0b00001000)),
"leds": {
- "caps": state["caps"],
- "scroll": state["scroll"],
- "num": state["num"],
+ "caps": bool(pong & 0b00000001),
+ "scroll": bool(pong & 0b00000010),
+ "num": bool(pong & 0b00000100),
},
+ "outputs": keyboard_outputs,
},
"mouse": {
- "online": state["mouse_online"],
- "absolute": True,
+ "online": (online and not (pong & 0b00010000)),
+ "absolute": absolute,
+ "outputs": mouse_outputs,
},
}
@@ -268,7 +231,7 @@ class BaseMcuHid(BaseHid, multiprocessing.Process): # pylint: disable=too-many-
get_logger().info("Clearing HID events ...")
try:
with self.__phy.connected() as conn:
- self.__process_command(conn, b"\x10\x00\x00\x00\x00")
+ self.__process_request(conn, ClearEvent().make_request())
except Exception:
logger.exception("Can't clear HID events")
finally:
@@ -278,30 +241,36 @@ class BaseMcuHid(BaseHid, multiprocessing.Process): # pylint: disable=too-many-
def send_key_events(self, keys: Iterable[Tuple[str, bool]]) -> None:
for (key, state) in keys:
- self.__queue_event(_KeyEvent(key, state))
+ self.__queue_event(KeyEvent(key, state))
def send_mouse_button_event(self, button: str, state: bool) -> None:
- self.__queue_event(_MouseButtonEvent(button, state))
+ self.__queue_event(MouseButtonEvent(button, state))
def send_mouse_move_event(self, to_x: int, to_y: int) -> None:
- self.__queue_event(_MouseMoveEvent(to_x, to_y))
+ self.__queue_event(MouseMoveEvent(to_x, to_y))
def send_mouse_relative_event(self, delta_x: int, delta_y: int) -> None:
- _ = delta_x # No relative events yet
- _ = delta_y
+ self.__queue_event(MouseRelativeEvent(delta_x, delta_y))
def send_mouse_wheel_event(self, delta_x: int, delta_y: int) -> None:
- self.__queue_event(_MouseWheelEvent(delta_x, delta_y))
+ self.__queue_event(MouseWheelEvent(delta_x, delta_y))
+
+ def set_keyboard_output(self, output: str) -> None:
+ self.__queue_event(SetKeyboardOutputEvent(output), clear=True)
+
+ def set_mouse_output(self, output: str) -> None:
+ self.__queue_event(SetMouseOutputEvent(output), clear=True)
def clear_events(self) -> None:
- # FIXME: Если очистка производится со стороны процесса хида, то возможна гонка между
- # очисткой и добавлением события _ClearEvent. Неприятно, но не смертельно.
- # Починить блокировкой после перехода на асинхронные очереди.
- tools.clear_queue(self.__events_queue)
- self.__queue_event(_ClearEvent())
+ self.__queue_event(ClearEvent(), clear=True)
- def __queue_event(self, event: _BaseEvent) -> None:
+ def __queue_event(self, event: BaseEvent, clear: bool=False) -> None:
if not self.__stop_event.is_set():
+ if clear:
+ # FIXME: Если очистка производится со стороны процесса хида, то возможна гонка между
+ # очисткой и добавлением нового события. Неприятно, но не смертельно.
+ # Починить блокировкой после перехода на асинхронные очереди.
+ tools.clear_queue(self.__events_queue)
self.__events_queue.put_nowait(event)
def run(self) -> None: # pylint: disable=too-many-branches
@@ -319,9 +288,9 @@ class BaseMcuHid(BaseHid, multiprocessing.Process): # pylint: disable=too-many-
try:
event = self.__events_queue.get(timeout=0.1)
except queue.Empty:
- self.__process_command(conn, b"\x01\x00\x00\x00\x00") # Ping
+ self.__process_request(conn, REQUEST_PING)
else:
- if not self.__process_command(conn, event.make_command()):
+ if not self.__process_request(conn, event.make_request()):
self.clear_events()
else:
logger.error("Missing HID device")
@@ -331,9 +300,6 @@ class BaseMcuHid(BaseHid, multiprocessing.Process): # pylint: disable=too-many-
logger.exception("Unexpected HID error")
time.sleep(1)
- def __process_command(self, conn: BasePhyConnection, command: bytes) -> bool:
- return self.__process_request(conn, self.__make_request(command))
-
def __process_request(self, conn: BasePhyConnection, request: bytes) -> bool: # pylint: disable=too-many-branches
logger = get_logger()
error_messages: List[str] = []
@@ -344,15 +310,14 @@ class BaseMcuHid(BaseHid, multiprocessing.Process): # pylint: disable=too-many-
error_retval = False
while common_retries and read_retries:
- response = self.__send_request(conn, request)
+ response = (RESPONSE_LEGACY_OK if self.__noop else conn.send(request))
try:
if len(response) < 4:
read_retries -= 1
raise _TempRequestError(f"No response from HID: request={request!r}")
- assert len(response) == 4, response
- if self.__make_crc16(response[-4:-2]) != struct.unpack(">H", response[-2:])[0]:
- request = self.__make_request(b"\x02\x00\x00\x00\x00") # Repeat an answer
+ if not check_response(response):
+ request = REQUEST_REPEAT
raise _TempRequestError("Invalid response CRC; requesting response again ...")
code = response[1]
@@ -368,9 +333,9 @@ class BaseMcuHid(BaseHid, multiprocessing.Process): # pylint: disable=too-many-
self.__set_state_online(True)
return True
elif code & 0x80: # Pong/Done with state
- self.__set_state_code(code)
+ self.__set_state_pong(response)
return True
- raise _TempRequestError(f"Invalid response from HID: request={request!r}; code=0x{code:02X}")
+ raise _TempRequestError(f"Invalid response from HID: request={request!r}, response=0x{response!r}")
except _RequestError as err:
common_retries -= 1
@@ -401,42 +366,10 @@ class BaseMcuHid(BaseHid, multiprocessing.Process): # pylint: disable=too-many-
return error_retval
def __set_state_online(self, online: bool) -> None:
- self.__state_flags.update(
- keyboard_online=online,
- mouse_online=online,
- )
-
- def __set_state_code(self, code: int) -> None:
- self.__state_flags.update(
- keyboard_online=(not (code & 0b00001000)),
- mouse_online=(not (code & 0b00010000)),
- caps=bool(code & 0b00000001),
- scroll=bool(code & 0b00000010),
- num=bool(code & 0b00000100),
- )
-
- def __send_request(self, conn: BasePhyConnection, request: bytes) -> bytes:
- if not self.__noop:
- response = conn.send(request)
- else:
- response = b"\x33\x20" # Magic + OK
- response += struct.pack(">H", self.__make_crc16(response))
- return response
-
- def __make_request(self, command: bytes) -> bytes:
- request = b"\x33" + command
- request += struct.pack(">H", self.__make_crc16(request))
- assert len(request) == 8, (request, command)
- return request
-
- def __make_crc16(self, data: bytes) -> int:
- crc = 0xFFFF
- for byte in data:
- crc = crc ^ byte
- for _ in range(8):
- if crc & 0x0001 == 0:
- crc = crc >> 1
- else:
- crc = crc >> 1
- crc = crc ^ 0xA001
- return crc
+ self.__state_flags.update(online=int(online))
+
+ def __set_state_pong(self, response: bytes) -> None:
+ status = response[1] << 16
+ if len(response) > 4:
+ status |= (response[2] << 8) | response[3]
+ self.__state_flags.update(online=1, status=status)
diff --git a/kvmd/plugins/hid/_mcu/proto.py b/kvmd/plugins/hid/_mcu/proto.py
new file mode 100644
index 00000000..7cfc3fd7
--- /dev/null
+++ b/kvmd/plugins/hid/_mcu/proto.py
@@ -0,0 +1,202 @@
+# ========================================================================== #
+# #
+# KVMD - The main Pi-KVM daemon. #
+# #
+# Copyright (C) 2018 Maxim Devaev <[email protected]> #
+# #
+# This program is free software: you can redistribute it and/or modify #
+# it under the terms of the GNU General Public License as published by #
+# the Free Software Foundation, either version 3 of the License, or #
+# (at your option) any later version. #
+# #
+# This program is distributed in the hope that it will be useful, #
+# but WITHOUT ANY WARRANTY; without even the implied warranty of #
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
+# GNU General Public License for more details. #
+# #
+# You should have received a copy of the GNU General Public License #
+# along with this program. If not, see <https://www.gnu.org/licenses/>. #
+# #
+# ========================================================================== #
+
+
+import dataclasses
+import struct
+
+from ....keyboard.mappings import KEYMAP
+
+from .... import tools
+
+
+# =====
+class BaseEvent:
+ def make_request(self) -> bytes:
+ raise NotImplementedError
+
+
+# =====
+_KEYBOARD_NAMES_TO_CODES = {
+ "usb": 0b00000001,
+ "ps2": 0b00000011,
+}
+_KEYBOARD_CODES_TO_NAMES = tools.swapped_kvs(_KEYBOARD_NAMES_TO_CODES)
+
+
+def get_active_keyboard(outputs: int) -> str:
+ return _KEYBOARD_CODES_TO_NAMES.get(outputs & 0b00000111, "")
+
+
[email protected](frozen=True)
+class SetKeyboardOutputEvent(BaseEvent):
+ keyboard: str
+
+ def __post_init__(self) -> None:
+ assert not self.keyboard or self.keyboard in _KEYBOARD_NAMES_TO_CODES
+
+ def make_request(self) -> bytes:
+ code = _KEYBOARD_NAMES_TO_CODES.get(self.keyboard, 0)
+ return _make_request(struct.pack(">BBxxx", 0x03, code))
+
+
+# =====
+_MOUSE_NAMES_TO_CODES = {
+ "usb": 0b00001000,
+ "usb_rel": 0b00010000,
+ "ps2": 0b00011000,
+}
+_MOUSE_CODES_TO_NAMES = tools.swapped_kvs(_MOUSE_NAMES_TO_CODES)
+
+
+def get_active_mouse(outputs: int) -> str:
+ return _MOUSE_CODES_TO_NAMES.get(outputs & 0b00111000, "")
+
+
[email protected](frozen=True)
+class SetMouseOutputEvent(BaseEvent):
+ mouse: str
+
+ def __post_init__(self) -> None:
+ assert not self.mouse or self.mouse in _MOUSE_NAMES_TO_CODES
+
+ def make_request(self) -> bytes:
+ return _make_request(struct.pack(">BBxxx", 0x04, _MOUSE_NAMES_TO_CODES.get(self.mouse, 0)))
+
+
+# =====
+class ClearEvent(BaseEvent):
+ def make_request(self) -> bytes:
+ return _make_request(b"\x10\x00\x00\x00\x00")
+
+
[email protected](frozen=True)
+class KeyEvent(BaseEvent):
+ name: str
+ state: bool
+
+ def __post_init__(self) -> None:
+ assert self.name in KEYMAP
+
+ def make_request(self) -> bytes:
+ code = KEYMAP[self.name].mcu.code
+ return _make_request(struct.pack(">BBBxx", 0x11, code, int(self.state)))
+
+
[email protected](frozen=True)
+class MouseButtonEvent(BaseEvent):
+ name: str
+ state: bool
+
+ def __post_init__(self) -> None:
+ assert self.name in ["left", "right", "middle", "up", "down"]
+
+ def make_request(self) -> bytes:
+ (code, state_pressed, is_main) = {
+ "left": (0b10000000, 0b00001000, True),
+ "right": (0b01000000, 0b00000100, True),
+ "middle": (0b00100000, 0b00000010, True),
+ "up": (0b10000000, 0b00001000, False), # Back
+ "down": (0b01000000, 0b00000100, False), # Forward
+ }[self.name]
+ if self.state:
+ code |= state_pressed
+ if is_main:
+ main_code = code
+ extra_code = 0
+ else:
+ main_code = 0
+ extra_code = code
+ return _make_request(struct.pack(">BBBxx", 0x13, main_code, extra_code))
+
+
[email protected](frozen=True)
+class MouseMoveEvent(BaseEvent):
+ to_x: int
+ to_y: int
+
+ def __post_init__(self) -> None:
+ assert -32768 <= self.to_x <= 32767
+ assert -32768 <= self.to_y <= 32767
+
+ def make_request(self) -> bytes:
+ return _make_request(struct.pack(">Bhh", 0x12, self.to_x, self.to_y))
+
+
[email protected](frozen=True)
+class MouseRelativeEvent(BaseEvent):
+ delta_x: int
+ delta_y: int
+
+ def __post_init__(self) -> None:
+ assert -127 <= self.delta_x <= 127
+ assert -127 <= self.delta_y <= 127
+
+ def make_request(self) -> bytes:
+ return _make_request(struct.pack(">Bbbxx", 0x15, self.delta_x, self.delta_y))
+
+
[email protected](frozen=True)
+class MouseWheelEvent(BaseEvent):
+ delta_x: int
+ delta_y: int
+
+ def __post_init__(self) -> None:
+ assert -127 <= self.delta_x <= 127
+ assert -127 <= self.delta_y <= 127
+
+ def make_request(self) -> bytes:
+ # Горизонтальная прокрутка пока не поддерживается
+ return _make_request(struct.pack(">Bxbxx", 0x14, self.delta_y))
+
+
+# =====
+def check_response(response: bytes) -> bool:
+ assert len(response) in (4, 8), response
+ return (_make_crc16(response[:-2]) == struct.unpack(">H", response[-2:])[0])
+
+
+def _make_request(command: bytes) -> bytes:
+ assert len(command) == 5, command
+ request = b"\x33" + command
+ request += struct.pack(">H", _make_crc16(request))
+ assert len(request) == 8, request
+ return request
+
+
+def _make_crc16(data: bytes) -> int:
+ crc = 0xFFFF
+ for byte in data:
+ crc = crc ^ byte
+ for _ in range(8):
+ if crc & 0x0001 == 0:
+ crc = crc >> 1
+ else:
+ crc = crc >> 1
+ crc = crc ^ 0xA001
+ return crc
+
+
+# =====
+REQUEST_PING = _make_request(b"\x01\x00\x00\x00\x00")
+REQUEST_REPEAT = _make_request(b"\x02\x00\x00\x00\x00")
+
+RESPONSE_LEGACY_OK = b"\x33\x20" + struct.pack(">H", _make_crc16(b"\x33\x20"))
diff --git a/kvmd/plugins/hid/bt/__init__.py b/kvmd/plugins/hid/bt/__init__.py
index 6c8dc46b..c13e2a34 100644
--- a/kvmd/plugins/hid/bt/__init__.py
+++ b/kvmd/plugins/hid/bt/__init__.py
@@ -131,8 +131,9 @@ class Plugin(BaseHid): # pylint: disable=too-many-instance-attributes
async def get_state(self) -> Dict:
state = await self.__server.get_state()
+ outputs: Dict = {"available": {}, "active": ""}
return {
- "online": state["online"],
+ "online": True,
"keyboard": {
"online": state["online"],
"leds": {
@@ -140,10 +141,12 @@ class Plugin(BaseHid): # pylint: disable=too-many-instance-attributes
"scroll": state["scroll"],
"num": state["num"],
},
+ "outputs": outputs,
},
"mouse": {
"online": state["online"],
"absolute": False,
+ "outputs": outputs,
},
}
@@ -178,10 +181,6 @@ class Plugin(BaseHid): # pylint: disable=too-many-instance-attributes
def send_mouse_button_event(self, button: str, state: bool) -> None:
self.__server.queue_event(MouseButtonEvent(button, state))
- def send_mouse_move_event(self, to_x: int, to_y: int) -> None:
- _ = to_x # No absolute events
- _ = to_y
-
def send_mouse_relative_event(self, delta_x: int, delta_y: int) -> None:
self.__server.queue_event(MouseRelativeEvent(delta_x, delta_y))
diff --git a/kvmd/plugins/hid/otg/__init__.py b/kvmd/plugins/hid/otg/__init__.py
index aa3d79b2..e685226b 100644
--- a/kvmd/plugins/hid/otg/__init__.py
+++ b/kvmd/plugins/hid/otg/__init__.py
@@ -89,8 +89,9 @@ class Plugin(BaseHid):
async def get_state(self) -> Dict:
keyboard_state = await self.__keyboard_proc.get_state()
mouse_state = await self.__mouse_proc.get_state()
+ outputs: Dict = {"available": {}, "active": ""}
return {
- "online": (keyboard_state["online"] and mouse_state["online"]),
+ "online": True,
"keyboard": {
"online": keyboard_state["online"],
"leds": {
@@ -98,8 +99,9 @@ class Plugin(BaseHid):
"scroll": keyboard_state["scroll"],
"num": keyboard_state["num"],
},
+ "outputs": outputs,
},
- "mouse": mouse_state,
+ "mouse": {**mouse_state, "outputs": outputs},
}
async def poll_state(self) -> AsyncGenerator[Dict, None]:
diff --git a/kvmd/plugins/hid/serial.py b/kvmd/plugins/hid/serial.py
index dee992b6..da61f2c6 100644
--- a/kvmd/plugins/hid/serial.py
+++ b/kvmd/plugins/hid/serial.py
@@ -51,7 +51,10 @@ class _SerialPhyConnection(BasePhyConnection):
if self.__tty.in_waiting:
self.__tty.read_all()
assert self.__tty.write(request) == 8
- return self.__tty.read(4)
+ data = self.__tty.read(4)
+ if data[0] == 0x34: # New response protocol
+ data += self.__tty.read(4)
+ return data
class _SerialPhy(BasePhy):
diff --git a/kvmd/plugins/hid/spi.py b/kvmd/plugins/hid/spi.py
index 7dadac0e..785ca779 100644
--- a/kvmd/plugins/hid/spi.py
+++ b/kvmd/plugins/hid/spi.py
@@ -67,7 +67,7 @@ class _SpiPhyConnection(BasePhyConnection):
assert request[0] == 0x33
deadline_ts = time.monotonic() + self.__read_timeout
- dummy = b"\x00" * 8
+ dummy = b"\x00" * 10
while time.monotonic() < deadline_ts:
if bytes(self.__xfer(dummy)) == dummy:
break
@@ -81,15 +81,15 @@ class _SpiPhyConnection(BasePhyConnection):
deadline_ts = time.monotonic() + self.__read_timeout
found = False
while time.monotonic() < deadline_ts:
- for byte in self.__xfer(b"\x00" * (5 - len(response))):
+ for byte in self.__xfer(b"\x00" * (9 - len(response))):
if not found:
- if byte != 0x33:
+ if byte == 0:
continue
found = True
response.append(byte)
- if len(response) == 4:
+ if len(response) == 8:
break
- if len(response) == 4:
+ if len(response) == 8:
break
else:
get_logger(0).error("SPI timeout reached while responce waiting")
diff --git a/kvmd/tools.py b/kvmd/tools.py
index 555ddc3e..40db9b9c 100644
--- a/kvmd/tools.py
+++ b/kvmd/tools.py
@@ -57,6 +57,10 @@ def sorted_kvs(dct: Dict[_DictKeyT, _DictValueT]) -> List[Tuple[_DictKeyT, _Dict
return sorted(dct.items(), key=operator.itemgetter(0))
+def swapped_kvs(dct: Dict[_DictKeyT, _DictValueT]) -> Dict[_DictValueT, _DictKeyT]:
+ return {value: key for (key, value) in dct.items()}
+
+
# =====
def clear_queue(q: multiprocessing.queues.Queue) -> None: # pylint: disable=invalid-name
for _ in range(q.qsize()):
diff --git a/kvmd/validators/hid.py b/kvmd/validators/hid.py
new file mode 100644
index 00000000..4f6ca4be
--- /dev/null
+++ b/kvmd/validators/hid.py
@@ -0,0 +1,56 @@
+# ========================================================================== #
+# #
+# KVMD - The main Pi-KVM daemon. #
+# #
+# Copyright (C) 2018 Maxim Devaev <[email protected]> #
+# #
+# This program is free software: you can redistribute it and/or modify #
+# it under the terms of the GNU General Public License as published by #
+# the Free Software Foundation, either version 3 of the License, or #
+# (at your option) any later version. #
+# #
+# This program is distributed in the hope that it will be useful, #
+# but WITHOUT ANY WARRANTY; without even the implied warranty of #
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
+# GNU General Public License for more details. #
+# #
+# You should have received a copy of the GNU General Public License #
+# along with this program. If not, see <https://www.gnu.org/licenses/>. #
+# #
+# ========================================================================== #
+
+
+from typing import Any
+
+from ..keyboard.mappings import KEYMAP
+
+from . import check_string_in_list
+
+from .basic import valid_number
+
+
+# =====
+def valid_hid_keyboard_output(arg: Any) -> str:
+ return check_string_in_list(arg, "Keyboard output", ["usb", "ps2", ""])
+
+
+def valid_hid_mouse_output(arg: Any) -> str:
+ return check_string_in_list(arg, "Mouse output", ["usb", "usb_rel", "ps2", ""])
+
+
+def valid_hid_key(arg: Any) -> str:
+ return check_string_in_list(arg, "Keyboard key", KEYMAP, lower=False)
+
+
+def valid_hid_mouse_move(arg: Any) -> int:
+ arg = valid_number(arg, name="Mouse move")
+ return min(max(-32768, arg), 32767)
+
+
+def valid_hid_mouse_button(arg: Any) -> str:
+ return check_string_in_list(arg, "Mouse button", ["left", "right", "middle", "up", "down"])
+
+
+def valid_hid_mouse_delta(arg: Any) -> int:
+ arg = valid_number(arg, name="Mouse delta")
+ return min(max(-127, arg), 127)
diff --git a/kvmd/validators/kvm.py b/kvmd/validators/kvm.py
index 6ae92fa4..16f8e59b 100644
--- a/kvmd/validators/kvm.py
+++ b/kvmd/validators/kvm.py
@@ -25,8 +25,6 @@ from typing import Set
from typing import Optional
from typing import Any
-from ..keyboard.mappings import KEYMAP
-
from . import raise_error
from . import check_string_in_list
from . import check_re_match
@@ -83,25 +81,6 @@ def valid_stream_resolution(arg: Any) -> str:
# =====
-def valid_hid_key(arg: Any) -> str:
- return check_string_in_list(arg, "HID key", KEYMAP, lower=False)
-
-
-def valid_hid_mouse_move(arg: Any) -> int:
- arg = valid_number(arg, name="HID mouse move")
- return min(max(-32768, arg), 32767)
-
-
-def valid_hid_mouse_button(arg: Any) -> str:
- return check_string_in_list(arg, "HID mouse button", ["left", "right", "middle", "up", "down"])
-
-
-def valid_hid_mouse_delta(arg: Any) -> int:
- arg = valid_number(arg, name="HID mouse delta")
- return min(max(-127, arg), 127)
-
-
-# =====
def valid_ugpio_driver(arg: Any, variants: Optional[Set[str]]=None) -> str:
name = "GPIO driver"
arg = check_len(check_re_match(arg, name, r"^[a-zA-Z_][a-zA-Z0-9_-]*$"), name, 255)