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
2 changes: 1 addition & 1 deletion docs/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ while loop.time() < end:
| `set_hdmi_idle_timeout` | PCIe 2.5.0 |
| `set_stream_mode`, `set_stream_quality`, `set_gop` | Pro |
| `get_hdmi_capture`, `set_hdmi_capture`, `get_hdmi_passthrough`, `set_hdmi_passthrough` | Pro |
| `get_edid`, `switch_edid` | Pro |
| `get_edid`, `switch_edid` | Pro; `WQUXGA_3840X2400_30HZ` below 1.2.16 |
| `get_custom_edid_list`, `upload_edid`, `delete_edid` | Pro 1.2.3 |
| `set_rate_control_mode` | Pro 1.2.6 |
| `set_fps` | Pro 1.2.8 |
Expand Down
4 changes: 3 additions & 1 deletion nanokvm/components/services.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,9 @@ async def update_application(

The device answers once the update is installed, then restarts its
server, which ends the session. Non-Pro firmware before 2.5.1 restarts
before its answer is sent, so it drops the connection instead.
before its answer is sent, so it drops the connection instead. Pro
1.2.16 and later answer once the package is downloaded and install it
in the background, so a failed install shows only as the old version.
"""
drops_connection = await self._restarts_before_answering()
try:
Expand Down
18 changes: 17 additions & 1 deletion nanokvm/components/video.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@
StopFrameDetectReq,
)
from ..models.pro import (
_LEGACY_EDID_NAMES,
DeleteEdidReq,
EdidPreset,
EdidValue,
GetCustomEdidListRsp,
GetEdidRsp,
Expand All @@ -41,6 +43,7 @@
StreamMode,
SwitchEdidReq,
UploadEdidRsp,
_normalize_edid_value,
)
from .session import Controller

Expand Down Expand Up @@ -203,7 +206,20 @@ async def get_edid(self) -> GetEdidRsp:

@require_hardware(HWFamily.PRO)
async def switch_edid(self, edid: EdidValue) -> None:
"""Switch EDID."""
"""Switch EDID.

A preset, given by either of its names, is sent under the name the
device's firmware uses. Pro 1.2.16 dropped ``WQUXGA_3840X2400_30HZ``.
"""
edid = _normalize_edid_value(edid)
if isinstance(edid, EdidPreset):
if not await self._session.application_version_at_least(pro="1.2.16"):
edid = _LEGACY_EDID_NAMES.get(edid, edid)
elif edid is EdidPreset.WQUXGA_3840X2400_30HZ:
raise NanoKVMNotSupportedError(
"the 3840x2400 EDID preset requires Pro application version "
f"< 1.2.16 (detected: {self._session._application_version})"
)
await self._session.api_request_json(
hdrs.METH_POST,
"/vm/edid",
Expand Down
35 changes: 23 additions & 12 deletions nanokvm/models/pro.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,25 +81,40 @@ class LcdTimeFormat(_CaseInsensitiveStrEnum):


class EdidPreset(StrEnum):
"""Built-in NanoKVM Pro EDID presets."""
"""Built-in NanoKVM Pro EDID presets, named as Pro 1.2.16 names them.

UHD_4K_30HZ = "E18-4K30FPS"
UHD_4K_39HZ = "E48-4K39FPS"
QHD_1440P_60HZ = "E56-2K60FPS"
FHD_1080P_60HZ = "E54-1080P60FPS"
Pro 1.2.15 and older use other names, and only they have
``WQUXGA_3840X2400_30HZ``.
"""

UHD_4K_30HZ = "nanokvm-pro-3840x2160-30"
UHD_4K_39HZ = "nanokvm-pro-3840x2160-39"
QHD_1440P_60HZ = "nanokvm-pro-2560x1440-60"
FHD_1080P_60HZ = "nanokvm-pro-1920x1080-60"
WQUXGA_3840X2400_30HZ = "E58-4K16-10"
ULTRAWIDE_3440X1440_60HZ = "E63-Ultrawide"
ULTRAWIDE_3440X1440_60HZ = "nanokvm-pro-3440x1440-60"


EdidValue = EdidPreset | str

# Pro 1.2.15 and older names of the presets that 1.2.16 renamed.
_LEGACY_EDID_NAMES: dict[EdidPreset, str] = {
EdidPreset.UHD_4K_30HZ: "E18-4K30FPS",
EdidPreset.UHD_4K_39HZ: "E48-4K39FPS",
EdidPreset.QHD_1440P_60HZ: "E56-2K60FPS",
EdidPreset.FHD_1080P_60HZ: "E54-1080P60FPS",
EdidPreset.ULTRAWIDE_3440X1440_60HZ: "E63-Ultrawide",
}
_PRESETS_BY_LEGACY_NAME = {name: preset for preset, name in _LEGACY_EDID_NAMES.items()}


def _normalize_edid_value(value: Any) -> Any:
"""Return the preset for either name of a built-in EDID, else ``value``."""
if isinstance(value, str):
try:
return EdidPreset(value)
except ValueError:
return value
return _PRESETS_BY_LEGACY_NAME.get(value, value)
return value


Expand All @@ -125,13 +140,9 @@ def _normalize_edid(cls, value: Any) -> Any:


class SwitchEdidReq(BaseModel):
# The client picks the name the firmware uses, so it is sent unchanged.
edid: EdidValue

@field_validator("edid", mode="before")
@classmethod
def _normalize_edid(cls, value: Any) -> Any:
return _normalize_edid_value(value)


class GetCustomEdidListRsp(BaseModel):
model_config = ConfigDict(populate_by_name=True)
Expand Down
35 changes: 34 additions & 1 deletion tests/test_video.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

from nanokvm.client import NanoKVMClient
from nanokvm.exceptions import NanoKVMNotSupportedError
from nanokvm.models import HWVersion, ScreenSettingType, StreamMode
from nanokvm.models import EdidPreset, HWVersion, ScreenSettingType, StreamMode

from .common import BASE_URL, mark_detected

Expand All @@ -28,6 +28,39 @@ async def test_set_stream_mode_accepts_existing_string_values(
assert calls[0].kwargs.get("json") == {"mode": "h264-direct"}


async def test_edid_presets_follow_the_1_2_16_rename(
client: NanoKVMClient, mocked: aioresponses
) -> None:
"""Test presets are sent as the firmware names them and read under both names."""
url = f"{BASE_URL}vm/edid"
mocked.post(url, payload={"code": 0, "msg": "success", "data": None}, repeat=True)

mark_detected(client, HWVersion.PRO, "1.2.15")
await client.switch_edid(EdidPreset.QHD_1440P_60HZ)
await client.switch_edid("nanokvm-pro-1920x1080-60")
await client.switch_edid(EdidPreset.WQUXGA_3840X2400_30HZ)

mark_detected(client, HWVersion.PRO, "1.2.16")
await client.switch_edid("E56-2K60FPS")
await client.switch_edid("custom.bin")
with pytest.raises(NanoKVMNotSupportedError):
await client.switch_edid(EdidPreset.WQUXGA_3840X2400_30HZ)

calls = mocked.requests[("POST", yarl.URL(url))]
assert [call.kwargs["json"]["edid"] for call in calls] == [
"E56-2K60FPS",
"E54-1080P60FPS",
"E58-4K16-10",
"nanokvm-pro-2560x1440-60",
"custom.bin",
]

for name in ("E63-Ultrawide", "nanokvm-pro-3440x1440-60"):
mocked.get(url, payload={"code": 0, "msg": "success", "data": {"edid": name}})
rsp = await client.get_edid()
assert rsp.edid is EdidPreset.ULTRAWIDE_3440X1440_60HZ


async def test_pause_frame_detect(client: NanoKVMClient, mocked: aioresponses) -> None:
"""Test the pause sends its duration and rejects non-positive ones."""
mark_detected(client, HWVersion.PCIE)
Expand Down
Loading