Compare commits

..
Author SHA1 Message Date
clyde 547dbf18d4 Merge branch 'runtime-image-jpeg-scale' into runtime-image-integer-draw 2026-10-22 22:03:58 +11:00
clyde ba3af07a23 Merge branch 'online-image-progress' into runtime-image-jpeg-scale 2026-10-22 22:03:58 +11:00
clyde 9c6ae9c615 Merge branch 'online-image-jpeg-stream' into online-image-progress 2026-10-22 22:03:58 +11:00
clyde de02ef8555 Merge branch 'runtime-image-jpeg-steps' into online-image-jpeg-stream 2026-10-22 22:03:57 +11:00
clydeandClaude Opus 5.5 38b23152f4 [runtime_image] Put the is_finished() doc back above is_finished()
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-22 22:03:51 +11:00
clyde 375e8bc39b Merge branch 'runtime-image-jpeg-scale' into runtime-image-integer-draw 2026-10-22 21:44:49 +11:00
clyde 16a91ab760 Merge branch 'online-image-progress' into runtime-image-jpeg-scale 2026-10-22 21:44:44 +11:00
clydeandClaude Opus 5.5 2456e8d686 [online_image] Use emplace_back in the download started test
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-22 21:44:39 +11:00
clyde 0c8e1c3f1c Merge branch 'runtime-image-jpeg-scale' into runtime-image-integer-draw 2026-10-22 21:35:06 +11:00
clyde a3c575d643 Merge branch 'online-image-progress' into runtime-image-jpeg-scale 2026-10-22 21:31:12 +11:00
clydeandClaude Opus 5.5 0573c8b568 [online_image] Add on_download_started
Fires when the server has accepted the request and the image starts to
download, before the first on_download_progress; not for a 304 or an
HTTP error.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-22 21:24:05 +11:00
clyde d01205fa62 Merge branch 'runtime-image-jpeg-steps' into runtime-image-jpeg-scale 2026-10-22 21:17:21 +11:00
clyde de7ea89d0b Merge branch 'online-image-jpeg-stream' into online-image-progress 2026-10-22 21:17:19 +11:00
clyde 0077f68158 Merge branch 'runtime-image-jpeg-steps' into online-image-jpeg-stream 2026-10-22 21:17:18 +11:00
clydeandClaude Opus 5.5 7458f8c7d8 [runtime_image] Pin JPEGDEC to 1.8.4-esphome.3, built with -O2
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-22 21:17:13 +11:00
clyde 0e5e0a8ff6 Merge remote-tracking branch 'upstream/dev' into runtime-image-jpeg-steps 2026-10-22 21:15:57 +11:00
clydeandClaude Opus 5.5 752c230405 [runtime_image] Check once per draw() call whether the image is scaled
An image drawn at its own size needs no span maths at all.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-22 21:14:33 +11:00
clydeandClaude Opus 5.5 cfc92657a2 [runtime_image] Say the generic draw() path keeps the same pixels as the RGB565 path
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-22 20:51:14 +11:00
clydeandClaude Opus 5.5 32f32f144d [runtime_image] Skip the span division for an image drawn at its own size
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-22 20:44:48 +11:00
clydeandClaude Opus 5.5 9b60e7cff2 [runtime_image] Decode a JPEG at the smallest JPEGDEC scale that covers the buffer
JPEGDEC can decode at 1/2, 1/4 or 1/8 size for much less work, but
every JPEG was decoded at full size and then shrunk to the buffer. A
3596 x 2840 image shown at 281 x 222 took 7.1 s on an ESP32-S3; at
1/8 size it takes 2.0 s.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-22 20:19:44 +11:00
clydeandClaude Opus 5.5 02b8db1f3f [runtime_image] Draw each target pixel once, with integer maths
ImageDecoder::draw() painted every source pixel into the buffer, so a
shrunk image wrote each target pixel many times, and worked out the
spans with double multiplications and ceil() for every pixel. The S3
has no double FPU, so drawing took about 6 us a pixel: 3.7 s of a 1000
x 617 PNG's 6 s. Give each source pixel the span from ceil(start *
scale) to ceil(end * scale), worked out with integers, so neighbouring
spans meet without overlapping. The PNG now loads in 3.2 s.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-22 20:17:27 +11:00
clyde f860c8a8d9 Merge branch 'online-image-jpeg-stream' into online-image-progress 2026-10-22 20:13:39 +11:00
clyde bb7a060138 Merge branch 'runtime-image-jpeg-steps' into online-image-jpeg-stream 2026-10-22 20:13:38 +11:00
clydeandClaude Opus 5.5 84839507bd [runtime_image] Pin JPEGDEC to 1.8.4-esphome.2, with the ESP32-S3 SIMD under ESP-IDF
Full-size JPEG decodes are 12-19% faster on an ESP32-S3 with ESP-IDF.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-22 20:13:33 +11:00
18348b8794 [vnc] Add VNC client component (#20623)
Co-authored-by: J. Nick Koston <nick@home-assistant.io>
Co-authored-by: pre-commit-ci-lite[bot] <117423508+pre-commit-ci-lite[bot]@users.noreply.github.com>
2026-10-21 21:14:37 -10:00
f7dc2ecb51 [wifi] [ethernet] Bridge the WISP access point to an ethernet upstream (#7147)
Co-authored-by: J. Nick Koston <nick@home-assistant.io>
Co-authored-by: J. Nick Koston <nick@koston.org>
2026-10-22 07:02:25 +00:00
ddcc5874ed [api] Let a media_player ask for a constant bitrate (#19962)
Co-authored-by: J. Nick Koston <nick@home-assistant.io>
Co-authored-by: J. Nick Koston <nick@koston.org>
2026-10-22 06:17:59 +00:00
clyde ef54eb14d8 Merge branch 'online-image-jpeg-stream' into online-image-progress 2026-10-22 16:29:02 +11:00
clydeandClaude Opus 5.5 60aa3e24f1 [online_image] Brace the whole-file fallback for clang-tidy
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-22 16:29:01 +11:00
clyde 63566999bd Merge branch 'runtime-image-jpeg-steps' into online-image-jpeg-stream 2026-10-22 16:28:45 +11:00
clydeandClaude Opus 5.5 1728d3326b [runtime_image] Log a failed open from a source at debug level, as the caller may recover
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-22 16:28:44 +11:00
clyde 2b99c56754 Merge branch 'online-image-jpeg-stream' into online-image-progress 2026-10-22 16:21:09 +11:00
clydeandClaude Opus 5.5 d79cd9a7eb [online_image] Download the whole JPEG when its headers do not fit the buffer
Before JPEG images were decoded as they download, the buffer grew to
hold the whole file, so an image with large EXIF or ICC data decoded
with any buffer_size. Keep that working: when the headers do not fit,
grow the buffer to the file size, keeping what has arrived, and decode
it at the end as before.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-22 16:21:08 +11:00
esphome[bot] 28013a60e1 Bump aioesphomeapi from 46.19.0 to 46.20.0 (#21362) 2026-10-21 19:19:35 -10:00
clyde fa18994fab Merge branch 'runtime-image-jpeg-steps' into online-image-jpeg-stream 2026-10-22 16:19:20 +11:00
clydeandClaude Opus 5.5 1090706216 [runtime_image] Count the bytes of a stepped decode, and say when begin_source() can be called
Report any failed read of the source as DECODE_ERROR_READ from one place.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-22 16:19:01 +11:00
J. Nick Koston 966193cb09 [core] Emit generated lambda code after the user includes (#21307) 2026-10-21 18:28:04 -10:00
clydeandClaude Opus 5.5 ced497995d [online_image] Add on_download_progress, to show how far a download has got
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-22 14:35:37 +11:00
clydeandClaude Opus 5.5 228c553f60 [online_image] Also give the buffer_size hint when unreadable headers end in a read error
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-22 14:34:15 +11:00
clydeandClaude Opus 5.5 563e197c6d [online_image] Open a streamed JPEG in a pass of its own, and only blame the buffer for unreadable headers
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-22 14:29:07 +11:00
clyde 9d07f1f400 Merge branch 'runtime-image-jpeg-steps' into online-image-jpeg-stream 2026-10-22 14:27:47 +11:00
clydeandClaude Opus 5.5 846fc70bdf [runtime_image] Report a progressive JPEG as an unsupported format
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-22 14:27:46 +11:00
clyde 74212d8d04 Merge branch 'runtime-image-jpeg-steps' into online-image-jpeg-stream 2026-10-22 13:41:53 +11:00
clydeandClaude Opus 5.5 101c9cd840 [runtime_image] Take JPEGDEC from esphome-libs
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-22 13:41:48 +11:00
clyde 8152dcf224 Merge branch 'runtime-image-jpeg-steps' into online-image-jpeg-stream 2026-10-22 13:37:13 +11:00
clydeandClaude Opus 5.5 f2d50b4aa7 [runtime_image] Drop the trailing underscore from the static JPEGDEC callbacks
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-22 13:37:13 +11:00
clydeandClaude Opus 5.5 f75b3d6ded [online_image] Test decode and read errors part way through a streamed JPEG
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-22 13:17:00 +11:00
clyde 3cf29bf4fb Merge branch 'runtime-image-jpeg-steps' into online-image-jpeg-stream 2026-10-22 13:14:54 +11:00
clydeandClaude Opus 5.5 f752e3b150 [runtime_image] Report a failed seek to the start as a read error
Also limit the JPEGDEC pin comment to the fixes ESPHome uses: it never
crops, and refuses progressive images.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-22 13:14:51 +11:00
clyde a5bc7070d1 Merge branch 'runtime-image-jpeg-steps' into online-image-jpeg-stream 2026-10-22 13:06:41 +11:00
clydeandClaude Opus 5.5 b2eebfb010 [runtime_image] Pin JPEGDEC to the 1.8.4-esphome.1 release
Adds the upstream fix that skips building AC Huffman tables for
progressive images.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-22 13:06:37 +11:00
clyde a596d1d3c8 Merge branch 'runtime-image-jpeg-steps' into online-image-jpeg-stream 2026-10-22 09:09:13 +11:00
clydeandClaude Opus 5.5 6701aedae2 [runtime_image] Pin JPEGDEC with the grayscale, crop and progressive fixes
A grayscale JPEG decoded for any image type but direct RGB565 came out garbled:
JPEGDEC wrote RGB565 words where it was asked for RGB8888. The new pin fixes
that, along with cropping and a crash on progressive images with skipped blocks,
which ESPHome does not use.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-22 09:09:09 +11:00
clydeandClaude Opus 5.5 6fddbd1d48 [online_image] Decode a JPEG as it downloads
A JPEG had to be downloaded whole, into a buffer grown to the file size, and was
then decoded in one call. When the server gives the size, the JPEG decoder now
reads the download from the buffer itself and decodes a row of MCUs at a time,
within a time budget per loop pass. The buffer only has to hold the headers, so
buffer_size bounds the RAM used, and a slow link makes the decoder wait for the
rest rather than decode past what has arrived. Downloads without a size keep the
old path.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-22 07:23:28 +11:00
clydeandClaude Opus 5.5 03aaa00d75 [runtime_image] Refuse a source decode without a usable image size
JPEGDEC needs the file size when it opens a source, and holds it in an int.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-22 07:21:14 +11:00
clydeandClaude Opus 5.5 b612eb2929 [runtime_image] Let a source-reading decoder wait for data that has not arrived
A source that is still arriving, such as a download, may have nothing to give
when the decoder asks for more. The pinned JPEGDEC now pauses before an MCU when
less input is buffered than it needs and the source has no more yet, and
decode_step() reports that as DECODE_STEP_NEED_DATA; the next call carries on.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-22 07:04:54 +11:00
clydeandClaude Opus 5.5 63c041a95b [runtime_image] Pin JPEGDEC with the ARM NEON clipped MCU fix
Upstream's fix for the NEON path writing past the end of a row on clipped MCUs,
which overflows a framebuffer whose width is not a multiple of 16.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-21 22:36:44 +11:00
clydeandClaude Opus 5.5 f8d5096093 [runtime_image] Pin JPEGDEC with the ARM NEON colour order fix
The NEON 4:2:0 RGB8888 path swapped red and blue (JPEGDEC PR 121), which shows on
host builds on 64-bit ARM.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-21 22:26:39 +11:00
clydeandClaude Opus 5.5 f835600fa6 [runtime_image] Let a decoder read its input itself and decode JPEG a row at a time
JPEGDEC decodes a whole image in one call, from a copy of the whole file in RAM.
A JPEGDEC that can stop between rows (1.8.4 plus decodeBegin() and decodeRows(),
pinned from a fork for now) lets the JPEG decoder read a DecodeSource itself,
through JPEGDEC's own 2 KB buffer, and decode one row of MCUs per decode_step().
A component that can read its input on demand, such as a file, no longer needs a
buffer for the whole file or a main loop pass long enough to decode the image.

Decoders that cannot read a source say so, and the fed decode() path is unchanged.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-21 22:08:01 +11:00
79 changed files with 6942 additions and 1078 deletions
+1
View File
@@ -700,6 +700,7 @@ esphome/components/veml7700/* @latonita
esphome/components/version/* @esphome/core
esphome/components/vevor_dtx/* @Bofr5555
esphome/components/vl53l1x/* @will-tm
esphome/components/vnc/* @MindFreeze
esphome/components/voice_assistant/* @jesserockz @kahrendt
esphome/components/wake_on_lan/* @clydebarrow @willwill2will54
esphome/components/watchdog/* @oarcher
+2
View File
@@ -44,6 +44,7 @@ from esphome.platformio.library import (
normalize_dependencies,
parse_library_json,
parse_library_properties,
warn_properties_depends,
)
_LOGGER = logging.getLogger(__name__)
@@ -282,6 +283,7 @@ def _bundled_library(framework_path: Path, name: str) -> ArduinoLibrary:
"resolved automatically; add them with add_library() if needed",
name,
)
warn_properties_depends(name, data)
build = data.get("build")
if isinstance(build, dict) and build.get("extraScript"):
# Scripts only run on the converted path; building without
+2
View File
@@ -1785,6 +1785,8 @@ message MediaPlayerSupportedFormat {
uint32 num_channels = 3;
MediaPlayerFormatPurpose purpose = 4;
uint32 sample_bytes = 5;
// Constant bitrate in kbps for lossy formats; 0 means no preference
uint32 bitrate = 6;
}
message ListEntitiesMediaPlayerResponse {
option (id) = 63;
@@ -1131,6 +1131,7 @@ uint16_t APIConnection::try_send_media_player_info(EntityBase *entity, APIConnec
media_format.num_channels = supported_format.num_channels;
media_format.purpose = static_cast<enums::MediaPlayerFormatPurpose>(supported_format.purpose);
media_format.sample_bytes = supported_format.sample_bytes;
media_format.bitrate = supported_format.bitrate;
}
return fill_and_encode_entity_info(media_player, msg, conn, remaining_size);
}
+2
View File
@@ -2373,6 +2373,7 @@ uint8_t *MediaPlayerSupportedFormat::encode_msg(const void *self, uint8_t *__res
pos = ProtoEncode::encode_uint32(pos PROTO_ENCODE_DEBUG_ARG, 3, msg.num_channels);
pos = ProtoEncode::encode_uint32(pos PROTO_ENCODE_DEBUG_ARG, 4, static_cast<uint32_t>(msg.purpose));
pos = ProtoEncode::encode_uint32(pos PROTO_ENCODE_DEBUG_ARG, 5, msg.sample_bytes);
pos = ProtoEncode::encode_uint32(pos PROTO_ENCODE_DEBUG_ARG, 6, msg.bitrate);
return pos;
}
uint32_t MediaPlayerSupportedFormat::calc_size_msg(const void *self) {
@@ -2383,6 +2384,7 @@ uint32_t MediaPlayerSupportedFormat::calc_size_msg(const void *self) {
size += ProtoSize::calc_uint32(1, msg.num_channels);
size += msg.purpose ? 2 : 0;
size += ProtoSize::calc_uint32(1, msg.sample_bytes);
size += ProtoSize::calc_uint32(1, msg.bitrate);
return size;
}
uint8_t *ListEntitiesMediaPlayerResponse::encode_msg(const void *self,
+1
View File
@@ -2263,6 +2263,7 @@ class MediaPlayerSupportedFormat final : public ProtoMessage {
uint32_t num_channels{0};
enums::MediaPlayerFormatPurpose purpose{};
uint32_t sample_bytes{0};
uint32_t bitrate{0};
static uint8_t *encode_msg(const void *self, uint8_t *pos PROTO_ENCODE_DEBUG_PARAM);
uint8_t *encode(ProtoWriteBuffer &buffer PROTO_ENCODE_DEBUG_PARAM) const {
return encode_msg(this, buffer.get_pos() PROTO_ENCODE_DEBUG_ARG);
+1
View File
@@ -2099,6 +2099,7 @@ const char *MediaPlayerSupportedFormat::dump_to(DumpBuffer &out) const {
dump_field(out, ESPHOME_PSTR("num_channels"), this->num_channels);
dump_field(out, ESPHOME_PSTR("purpose"), static_cast<enums::MediaPlayerFormatPurpose>(this->purpose));
dump_field(out, ESPHOME_PSTR("sample_bytes"), this->sample_bytes);
dump_field(out, ESPHOME_PSTR("bitrate"), this->bitrate);
return out.c_str();
}
const char *ListEntitiesMediaPlayerResponse::dump_to(DumpBuffer &out) const {
+1 -19
View File
@@ -3813,20 +3813,6 @@ def _write_sdkconfig():
clean_build(clear_pio_cache=False)
def _arduino_provides(name: str) -> bool:
"""Whether arduino-esp32 bundles the library a manifest depends on, so it is
never fetched by name; it still has to be enabled to build."""
if name not in ARDUINO_DISABLED_LIBRARIES:
return False
if name not in CORE.data[KEY_ESP32][KEY_ARDUINO_LIBRARIES]:
_LOGGER.warning(
"A library depends on the Arduino library %s, which is not enabled; "
"add it under esphome: libraries:",
name,
)
return True
def _write_idf_component_yml():
yml_path = CORE.relative_build_path("src/idf_component.yml")
dependencies: dict[str, dict] = {}
@@ -3848,11 +3834,7 @@ def _write_idf_component_yml():
# IDF sees the same requirement twice; converted components reach it through
# ${ESPHOME_PROJECT_MANAGED_COMPONENTS}.
managed = set(CORE.data[KEY_ESP32].get(KEY_COMPONENTS, {}))
converted = generate_idf_components(
libraries,
managed=managed,
provides=_arduino_provides if CORE.using_arduino else None,
)
converted = generate_idf_components(libraries, managed=managed)
# IDF names a component after its directory and a later registration of the
# same name replaces the earlier one, so a stub beside a converted library of
# the same name (espressif/libsodium vs esphome/libsodium) would win or lose
+21 -16
View File
@@ -46,6 +46,7 @@ from esphome.const import (
CONF_TYPE,
CONF_USE_ADDRESS,
CONF_VALUE,
CONF_WIFI,
KEY_CORE,
KEY_FRAMEWORK_VERSION,
Platform,
@@ -562,6 +563,25 @@ CONFIG_SCHEMA = cv.All(
)
def _final_validate_wifi(config: ConfigType) -> None:
"""A wifi station competes with ethernet for the primary interface, so both must be in network: priority:."""
full_config = fv.full_config.get()
if (wifi_config := full_config.get(CONF_WIFI)) is None:
return
from esphome.components.wifi import wifi_stays_ap_only
if wifi_stays_ap_only(wifi_config, full_config):
return # an access point does not claim the primary interface
priority_ifaces = get_priority_interfaces_from_full_config(full_config)
if missing := [i for i in (DOMAIN, CONF_WIFI) if i not in priority_ifaces]:
raise cv.Invalid(
"When ethernet and a wifi station are used together, 'network: priority:' "
f"must list both interfaces; missing: {', '.join(missing)}"
)
def _final_validate_spi(config: ConfigType) -> None:
if not CORE.is_esp32:
return # SPI interface validation is ESP32-only
@@ -846,22 +866,7 @@ def _final_validate_rmii_pins(config: ConfigType) -> None:
def _final_validate(config: ConfigType) -> None:
"""Final validation for Ethernet component."""
# Allow ethernet + wifi coexistence only when both are declared in network: priority:.
if "wifi" in fv.full_config.get():
priority_ifaces = get_priority_interfaces_from_full_config(fv.full_config.get())
missing = [i for i in ("ethernet", "wifi") if i not in priority_ifaces]
if missing and priority_ifaces:
# A priority list exists but is incomplete: point at what to add.
raise cv.Invalid(
"When ethernet and wifi are used together, 'network: priority:' must "
f"list both interfaces; missing: {', '.join(missing)}"
)
if missing:
raise cv.Invalid(
"Component ethernet cannot be used together with component wifi "
"unless both are listed under 'network: priority:'"
)
_final_validate_wifi(config)
_final_validate_spi(config)
_final_validate_rmii_pins(config)
@@ -3,6 +3,7 @@ from collections.abc import Callable
from esphome import automation
import esphome.codegen as cg
from esphome.components import audio
from esphome.components.const import CONF_BITRATE
import esphome.config_validation as cv
from esphome.const import (
CONF_ENTITY_CATEGORY,
@@ -55,6 +56,30 @@ FORMAT_MAPPING = {
}
# MP3 constant bitrates in kbps; the encoder rounds anything else to the nearest one
MP3_BITRATES = (
8,
16,
24,
32,
40,
48,
56,
64,
80,
96,
112,
128,
144, # MPEG-2 only, at 16, 22.05 and 24 kHz
160,
192,
224,
256,
320,
)
validate_mp3_bitrate = cv.one_of(*MP3_BITRATES, int=True)
def build_supported_format_struct(
format_config: ConfigType, purpose: MockObj
) -> cg.StructInitializer:
@@ -75,6 +100,10 @@ def build_supported_format_struct(
if format_config[CONF_FORMAT] != "MP3":
args.append(("sample_bytes", 2))
# Omitted means no preference, and the client keeps encoding as it always has.
if bitrate := format_config.get(CONF_BITRATE):
args.append(("bitrate", bitrate))
return cg.StructInitializer(*args)
@@ -91,6 +120,14 @@ def validate_preferred_format(
inherit_property_from(CONF_NUM_CHANNELS, audio_device_key)(config)
inherit_property_from(CONF_SAMPLE_RATE, audio_device_key)(config)
# A constant bitrate is only meaningful for the one lossy format whose
# encoder Home Assistant can be told to fix
if CONF_BITRATE in config and config.get(CONF_FORMAT) != "MP3":
raise cv.Invalid(
f"{CONF_BITRATE} is only supported for the MP3 format",
path=[CONF_BITRATE],
)
# Opus only supports 48 kHz
if config.get(CONF_FORMAT) == "OPUS" and config.get(CONF_SAMPLE_RATE) != 48000:
raise cv.Invalid("Opus only supports a sample rate of 48000 Hz")
@@ -77,6 +77,7 @@ struct MediaPlayerSupportedFormat {
uint32_t num_channels;
MediaPlayerFormatPurpose purpose;
uint32_t sample_bytes;
uint32_t bitrate;
};
// Base features always reported for all media players
@@ -1,5 +1,6 @@
#include "download_buffer.h"
#include "esphome/core/log.h"
#include <algorithm>
#include <cstring>
namespace esphome::online_image {
@@ -34,6 +35,46 @@ size_t DownloadBuffer::read(size_t len) {
return this->unread_;
}
void DownloadSource::compact() {
if (this->keep_read_ || this->pos_ == this->start_)
return;
this->buffer_.read(this->pos_ - this->start_);
this->start_ = this->pos_;
}
int32_t DownloadSource::read(uint8_t *buf, uint32_t len) {
const size_t offset = this->pos_ - this->start_;
const size_t n = std::min<size_t>(len, this->buffer_.unread() - offset);
memcpy(buf, this->buffer_.data(offset), n);
this->pos_ += n;
return static_cast<int32_t>(n);
}
bool DownloadSource::seek(uint32_t position) {
// Only what is still in the buffer can be read again; a decoder never skips ahead of the download
if (position < this->start_ || position > this->start_ + this->buffer_.unread())
return false;
this->pos_ = position;
return true;
}
bool DownloadBuffer::grow(size_t size) {
if (this->size_ >= size)
return true;
RAMAllocator<uint8_t> allocator;
uint8_t *buffer = allocator.allocate(size);
if (buffer == nullptr) {
ESP_LOGE(TAG, "allocation of %zu bytes failed. Biggest block in heap: %zu Bytes", size,
allocator.get_max_free_block_size());
return false;
}
memcpy(buffer, this->buffer_, this->unread_);
allocator.deallocate(this->buffer_, this->size_);
this->buffer_ = buffer;
this->size_ = size;
return true;
}
size_t DownloadBuffer::resize(size_t size) {
if (this->size_ >= size) {
// Avoid useless reallocations; if the buffer is big enough, don't reallocate.
@@ -1,5 +1,6 @@
#pragma once
#include "esphome/components/runtime_image/image_decoder.h"
#include "esphome/core/helpers.h"
#include <cstddef>
#include <cstdint>
@@ -35,6 +36,8 @@ class DownloadBuffer {
void reset() { this->unread_ = 0; }
size_t resize(size_t size);
/// Make the buffer at least size bytes, keeping the unread data. Returns false if the allocation failed.
bool grow(size_t size);
protected:
uint8_t *buffer_;
@@ -43,4 +46,35 @@ class DownloadBuffer {
size_t unread_;
};
/**
* @brief Lets a decoder read a download as it arrives, from the download buffer.
*
* The decoder reads at its own position in the buffer. Bytes it has read stay there until compact() drops
* them, and compact() keeps everything while set_keep_read(true) is in effect, so the decoder can seek back
* while it reads the headers.
*/
class DownloadSource : public runtime_image::DecodeSource {
public:
explicit DownloadSource(DownloadBuffer &buffer) : buffer_(buffer) {}
/// Start again at the beginning of a new download, keeping what is read
void reset() {
this->start_ = 0;
this->pos_ = 0;
this->keep_read_ = true;
}
void set_keep_read(bool keep) { this->keep_read_ = keep; }
/// Drop the bytes the decoder has read, to make room for more of the download
void compact();
int32_t read(uint8_t *buf, uint32_t len) override;
bool seek(uint32_t position) override;
protected:
DownloadBuffer &buffer_;
size_t start_{0}; // Position in the download of the first byte in the buffer
size_t pos_{0}; // Position in the download the decoder reads next
bool keep_read_{true};
};
} // namespace esphome::online_image
+12
View File
@@ -22,6 +22,8 @@ DEPENDENCIES = ["http_request"]
CODEOWNERS = ["@guillempages", "@clydebarrow"]
CONF_ON_DOWNLOAD_FINISHED = "on_download_finished"
CONF_ON_DOWNLOAD_STARTED = "on_download_started"
CONF_ON_DOWNLOAD_PROGRESS = "on_download_progress"
CONF_UPDATE = "update"
online_image_ns = cg.esphome_ns.namespace("online_image")
@@ -43,7 +45,9 @@ ONLINE_IMAGE_SCHEMA = (
cv.Optional(CONF_REQUEST_HEADERS): cv.All(
cv.Schema({cv.string: cv.templatable(cv.string)})
),
cv.Optional(CONF_ON_DOWNLOAD_STARTED): automation.validate_automation({}),
cv.Optional(CONF_ON_DOWNLOAD_FINISHED): automation.validate_automation({}),
cv.Optional(CONF_ON_DOWNLOAD_PROGRESS): automation.validate_automation({}),
cv.Optional(CONF_ON_ERROR): automation.validate_automation({}),
}
)
@@ -96,9 +100,17 @@ automation.register_apply_action(
_CALLBACK_AUTOMATIONS = (
automation.CallbackAutomation(
CONF_ON_DOWNLOAD_STARTED, "add_on_download_started_callback"
),
automation.CallbackAutomation(
CONF_ON_DOWNLOAD_FINISHED, "add_on_finished_callback", [(bool, "cached")]
),
automation.CallbackAutomation(
CONF_ON_DOWNLOAD_PROGRESS,
"add_on_progress_callback",
[(float, "progress"), (cg.size_t, "bytes_read")],
),
automation.CallbackAutomation(CONF_ON_ERROR, "add_on_error_callback"),
)
@@ -4,6 +4,7 @@
#include "esphome/core/helpers.h"
#include "esphome/core/log.h"
#include <algorithm>
#include <cmath>
#include <cstdio>
ESPHOME_LOG_TAG(TAG, "online_image");
@@ -15,6 +16,11 @@ static const char *const IF_MODIFIED_SINCE_HEADER_NAME = "if-modified-since";
namespace esphome::online_image {
// Time one loop pass may spend decoding a JPEG as it downloads
static constexpr uint32_t STREAM_PASS_BUDGET_MS = 20;
// Shortest time between two on_download_progress calls
static constexpr uint32_t PROGRESS_INTERVAL_MS = 100;
OnlineImage::OnlineImage(const std::string &url, int width, int height, runtime_image::ImageFormat format,
image::ImageType type, image::Transparency transparency, image::Image *placeholder,
uint32_t buffer_size, bool is_big_endian)
@@ -140,8 +146,12 @@ void OnlineImage::update() {
return;
}
// JPEG requires the complete image in the download buffer before decoding
if (format == runtime_image::JPEG && total_size > this->download_buffer_.size() &&
// A JPEG of known size is decoded as it arrives, so the download buffer only has to hold its headers.
// Otherwise the decoder needs the complete image in the buffer.
this->streaming_ = total_size > 0 && total_size <= INT32_MAX && this->decoder_supports_source();
this->stream_started_ = false;
this->source_.reset();
if (!this->streaming_ && format == runtime_image::JPEG && total_size > this->download_buffer_.size() &&
this->download_buffer_.resize(total_size) == 0) {
// resize() logged the failed allocation; the next update() allocates the configured buffer again
this->end_connection_();
@@ -152,6 +162,8 @@ void OnlineImage::update() {
ESP_LOGI(TAG, "Downloading image (Size: %zu)", total_size);
this->start_time_ = millis();
this->last_data_time_ = this->start_time_;
this->download_started_callback_.call();
this->report_progress_(true);
this->enable_loop();
}
@@ -169,6 +181,11 @@ void OnlineImage::loop() {
return;
}
if (this->streaming_) {
this->loop_stream_();
return;
}
// Check if download is complete — use decoder's format-specific completion check
// to handle both known content-length and chunked transfer encoding
if (this->is_decode_finished() || (this->downloader_->content_length > 0 &&
@@ -188,6 +205,7 @@ void OnlineImage::loop() {
if (len > 0) {
this->last_data_time_ = App.get_loop_component_start_time();
this->download_buffer_.write(len);
this->report_progress_(false);
// Feed data to decoder
auto consumed = this->feed_data(this->download_buffer_.data(), this->download_buffer_.unread());
@@ -255,6 +273,104 @@ void OnlineImage::loop() {
}
}
void OnlineImage::loop_stream_() {
const uint32_t now = App.get_loop_component_start_time();
const size_t total = this->downloader_->content_length;
if (this->stream_started_)
this->source_.compact();
if (this->downloader_->get_bytes_read() < total) {
const size_t available = std::min(this->download_buffer_.free_capacity(), this->download_buffer_initial_size_);
if (available == 0) {
// Waiting for the decoder, not the network
this->last_data_time_ = now;
} else {
const int len = this->downloader_->read(this->download_buffer_.append(), available);
if (len > 0) {
this->last_data_time_ = now;
this->download_buffer_.write(len);
this->report_progress_(false);
} else if (len < 0) {
ESP_LOGE(TAG, "Error downloading image: %d", len);
this->fail_download_();
return;
} else if (now - this->last_data_time_ >= this->parent_->get_timeout()) {
ESP_LOGE(TAG, "No data received for %" PRIu32 " ms", this->parent_->get_timeout());
this->fail_download_();
return;
}
}
}
const bool arrived = this->downloader_->get_bytes_read() >= total;
if (!this->stream_started_) {
// The decoder reads the headers when it opens the image, so wait until they are in the buffer
if (!arrived && this->download_buffer_.free_capacity() != 0)
return;
const int err = this->begin_source(&this->source_);
if (err != 0) {
// Headers cut short by a full buffer fail to parse, or make the decoder seek past the buffer. Such an image
// still decodes once it is all in memory, as it did before JPEG images were decoded as they download.
if (!arrived && (err == runtime_image::DECODE_ERROR_INVALID_TYPE || err == runtime_image::DECODE_ERROR_READ)) {
ESP_LOGW(TAG,
"The image headers do not fit the %zu byte download buffer, so the whole image is downloaded "
"before it is decoded; increase buffer_size to decode it as it downloads",
this->download_buffer_.size());
// grow() logs a failed allocation
if (this->download_buffer_.grow(total)) {
this->streaming_ = false;
} else {
this->fail_download_();
}
return;
}
ESP_LOGE(TAG, "Error decoding image: %s", runtime_image::decode_error_to_string(err));
this->fail_download_();
return;
}
this->source_.set_keep_read(false);
this->stream_started_ = true;
// Opening the image and allocating its buffer is enough for one pass
return;
}
do {
const int result = this->decode_step();
if (result == runtime_image::DECODE_STEP_MORE)
continue;
if (result == runtime_image::DECODE_STEP_DONE) {
this->finish_download_();
} else if (result == runtime_image::DECODE_STEP_NEED_DATA) {
if (!arrived)
return;
// JPEGDEC only waits for bytes short of the size it was given, so this guards against a hang
ESP_LOGE(TAG, "Download ended before the image was complete");
this->fail_download_();
} else {
ESP_LOGE(TAG, "Error decoding image: %s", runtime_image::decode_error_to_string(result));
this->fail_download_();
}
return;
} while (millis() - now < STREAM_PASS_BUDGET_MS);
}
void OnlineImage::report_progress_(bool force) {
const uint32_t now = App.get_loop_component_start_time();
const size_t bytes = this->downloader_->get_bytes_read();
const size_t total = this->downloader_->content_length;
// The last bytes are always reported, so a progress bar reaches 100%
if (!force && now - this->last_progress_time_ < PROGRESS_INTERVAL_MS && (total == 0 || bytes < total))
return;
this->last_progress_time_ = now;
const float progress = total > 0 ? std::min(100.0f, bytes * 100.0f / total) : NAN;
this->download_progress_callback_.call(progress, bytes);
}
void OnlineImage::fail_download_() {
this->end_connection_();
this->download_error_callback_.call();
}
void OnlineImage::finish_download_() {
// Must be read before end_decode() ends the decoding session
bool decoder_finished = this->is_decode_finished();
@@ -288,6 +404,8 @@ void OnlineImage::end_connection_() {
this->downloader_ = nullptr;
}
this->download_buffer_.reset();
this->streaming_ = false;
this->stream_started_ = false;
this->disable_loop();
}
@@ -70,9 +70,17 @@ class OnlineImage final : public PollingComponent,
*/
void release();
/// Called when the server has accepted the request and the image starts to download
template<typename F> void add_on_download_started_callback(F &&callback) {
this->download_started_callback_.add(std::forward<F>(callback));
}
template<typename F> void add_on_finished_callback(F &&callback) {
this->download_finished_callback_.add(std::forward<F>(callback));
}
/// Called with the percentage downloaded (NAN when the server did not send the size) and the bytes read so far
template<typename F> void add_on_progress_callback(F &&callback) {
this->download_progress_callback_.add(std::forward<F>(callback));
}
template<typename F> void add_on_error_callback(F &&callback) {
this->download_error_callback_.add(std::forward<F>(callback));
}
@@ -81,12 +89,20 @@ class OnlineImage final : public PollingComponent,
bool validate_url_(const std::string &url);
void end_connection_();
void finish_download_();
/// loop() for a JPEG that is decoded as it downloads
void loop_stream_();
void fail_download_();
/// Reports download progress, at most every PROGRESS_INTERVAL_MS unless force is set
void report_progress_(bool force);
CallbackManager<void(bool)> download_finished_callback_{};
CallbackManager<void()> download_error_callback_{};
LazyCallbackManager<void()> download_started_callback_{};
LazyCallbackManager<void(float, size_t)> download_progress_callback_{};
std::shared_ptr<http_request::HttpContainer> downloader_{nullptr};
DownloadBuffer download_buffer_;
DownloadSource source_{download_buffer_};
/**
* This is the *initial* size of the download buffer, not the current size.
* The download buffer can be resized at runtime; the download_buffer_initial_size_
@@ -109,6 +125,9 @@ class OnlineImage final : public PollingComponent,
uint32_t start_time_{0};
uint32_t last_data_time_{0};
uint32_t last_progress_time_{0};
bool streaming_{false}; // The decoder reads the download itself, as it arrives
bool stream_started_{false}; // The decoder has read the headers
};
} // namespace esphome::online_image
+11 -1
View File
@@ -92,7 +92,17 @@ class JPEGFormat(Format):
def actions(self) -> None:
cg.add_define("USE_RUNTIME_IMAGE_JPEG")
cg.add_library("JPEGDEC", "1.8.4", "https://github.com/bitbank2/JPEGDEC#1.8.4")
# 1.8.4 plus decodeBegin()/decodeRows(), so an image can be decoded a few rows at a time and
# can wait for input that has not arrived yet, and fixes for: input that arrives in short
# reads, garbled RGB8888 output of grayscale images, and two ARM NEON faults seen on arm64 hosts
# (swapped red and blue in the 4:2:0 RGB8888 path, writes past the row on clipped MCUs). It also
# uses the ESP32-S3 SIMD colour conversion in ESP-IDF builds, not only Arduino ones, and is built
# with -O2.
cg.add_library(
"JPEGDEC",
"1.8.4",
"https://github.com/esphome-libs/JPEGDEC#1.8.4-esphome.3",
)
if CORE.is_host:
# JPEGDEC's host detection checks __MACH__/__LINUX__, but gcc only
# predefines the lowercase __linux__; without this a Linux host
@@ -2,7 +2,6 @@
#include "runtime_image.h"
#include "esphome/core/log.h"
#include <algorithm>
#include <cmath>
namespace esphome::runtime_image {
@@ -11,19 +10,48 @@ ESPHOME_LOG_TAG(TAG, "image_decoder");
bool ImageDecoder::set_size(int width, int height) {
bool success = this->image_->resize(width, height) > 0;
this->size_valid_ = success;
this->x_scale_ = static_cast<double>(this->image_->get_buffer_width()) / width;
this->y_scale_ = static_cast<double>(this->image_->get_buffer_height()) / height;
this->draw_width_ = width;
this->draw_height_ = height;
return success;
}
int ImageDecoder::scaled_x_(int x) const {
if (this->draw_width_ <= 0)
return 0;
const uint32_t buffer_width = this->image_->get_buffer_width();
return static_cast<int>((static_cast<uint32_t>(x) * buffer_width + this->draw_width_ - 1) / this->draw_width_);
}
int ImageDecoder::scaled_y_(int y) const {
if (this->draw_height_ <= 0)
return 0;
const uint32_t buffer_height = this->image_->get_buffer_height();
return static_cast<int>((static_cast<uint32_t>(y) * buffer_height + this->draw_height_ - 1) / this->draw_height_);
}
void ImageDecoder::draw(int x, int y, int w, int h, const Color &color) {
if (!this->size_valid_) {
return;
}
auto width = std::min(this->image_->get_buffer_width(), static_cast<int>(std::ceil((x + w) * this->x_scale_)));
auto height = std::min(this->image_->get_buffer_height(), static_cast<int>(std::ceil((y + h) * this->y_scale_)));
for (int i = x * this->x_scale_; i < width; i++) {
for (int j = y * this->y_scale_; j < height; j++) {
// A source pixel paints the target pixels from ceil(start * scale) up to ceil(end * scale). The spans of neighbouring
// pixels meet without overlapping, so when an image is shrunk each target pixel is painted once, by the first
// source pixel that reaches it, rather than by every pixel that lands on it. Integer maths: the S3 has no double FPU.
const int buffer_width = this->image_->get_buffer_width();
const int buffer_height = this->image_->get_buffer_height();
int x1 = x;
int y1 = y;
int x2 = x + w;
int y2 = y + h;
if (buffer_width != this->draw_width_ || buffer_height != this->draw_height_) {
x1 = this->scaled_x_(x);
y1 = this->scaled_y_(y);
x2 = this->scaled_x_(x + w);
y2 = this->scaled_y_(y + h);
}
x2 = std::min(buffer_width, x2);
y2 = std::min(buffer_height, y2);
for (int i = x1; i < x2; i++) {
for (int j = y1; j < y2; j++) {
this->image_->draw_pixel(i, j, color);
}
}
@@ -1,4 +1,6 @@
#pragma once
#include <cstdint>
#include "esphome/core/color.h"
#include "image_format.h"
@@ -9,6 +11,14 @@ enum DecodeError : int {
DECODE_ERROR_UNSUPPORTED_FORMAT = -2,
DECODE_ERROR_OUT_OF_MEMORY = -3,
DECODE_ERROR_INTERNAL_DECODER_ERROR = -4,
DECODE_ERROR_READ = -5,
};
/// Results of ImageDecoder::decode_step() other than a DecodeError
enum DecodeStep : int {
DECODE_STEP_DONE = 0,
DECODE_STEP_MORE = 1,
DECODE_STEP_NEED_DATA = 2, // The source has nothing more yet; call again once more has arrived
};
constexpr const char *decode_error_to_string(int error) {
@@ -21,6 +31,8 @@ constexpr const char *decode_error_to_string(int error) {
return "Out of memory";
case DECODE_ERROR_INTERNAL_DECODER_ERROR:
return "Internal decoder error";
case DECODE_ERROR_READ:
return "Read error";
default:
return "Unknown error";
}
@@ -28,6 +40,19 @@ constexpr const char *decode_error_to_string(int error) {
class RuntimeImage;
/// Input that a decoder reads itself as it goes, so the whole image never has to be in memory at once
class DecodeSource {
public:
virtual ~DecodeSource() = default;
/**
* Read up to len bytes at the current position. Returns the number read, which is less than len, even 0, when
* no more has arrived yet or at the end of the input, or negative on error.
*/
virtual int32_t read(uint8_t *buf, uint32_t len) = 0;
/// Move to a position counted from the start. Returns false on error.
virtual bool seek(uint32_t position) = 0;
};
/**
* @brief Class to abstract decoding different image formats.
*/
@@ -58,8 +83,8 @@ class ImageDecoder {
this->expected_size_ = 0;
this->decoded_bytes_ = 0;
this->size_valid_ = true;
this->x_scale_ = 1.0;
this->y_scale_ = 1.0;
this->draw_width_ = 0;
this->draw_height_ = 0;
}
/**
@@ -88,6 +113,28 @@ class ImageDecoder {
*/
virtual int decode(uint8_t *buffer, size_t size) = 0;
/// Whether this decoder can read a DecodeSource itself, a step at a time, instead of being fed data.
virtual bool supports_source() const { return false; }
/**
* @brief Start decoding from a source the decoder reads itself. Call after prepare(), in place of decode().
*
* The image headers (everything before the image data) must be readable from the source when this is called;
* only decode_step() can wait for more to arrive. The source must hold expected_size bytes in the end, and stay
* valid until the session ends.
* @return int 0 on success, or a {@see DecodeError} value.
*/
virtual int begin_source(DecodeSource *source) { return DECODE_ERROR_UNSUPPORTED_FORMAT; }
/**
* @brief Decode a little more of an image started with begin_source(), keeping each call short.
*
* @return int DECODE_STEP_MORE while there is more to do, DECODE_STEP_NEED_DATA when the source has not
* delivered enough to go on, DECODE_STEP_DONE when the image is complete, or a {@see DecodeError}
* value.
*/
virtual int decode_step() { return DECODE_ERROR_UNSUPPORTED_FORMAT; }
/**
* @brief Request the image to be resized once the actual dimensions are known.
* Called by the callback functions, to be able to access the parent Image class.
@@ -112,6 +159,9 @@ class ImageDecoder {
*/
void draw(int x, int y, int w, int h, const Color &color);
/// Bytes of the image the decoder has used so far
size_t get_decoded_bytes() const { return this->decoded_bytes_; }
/**
* @brief Check if the decoder has finished processing.
*
@@ -134,8 +184,13 @@ class ImageDecoder {
uint8_t *image_buffer_() const;
bool image_is_big_endian_() const;
double x_scale_ = 1.0;
double y_scale_ = 1.0;
/// The first buffer column, or row, that source column x, or row y, paints: ceil(x * buffer size / draw size)
int scaled_x_(int x) const;
int scaled_y_(int y) const;
// Size of the image as the decoder draws it, which draw() spreads over the buffer
int draw_width_ = 0;
int draw_height_ = 0;
RuntimeImage *image_;
size_t expected_size_ = 0; // Expected data size (0 if unknown)
size_t decoded_bytes_ = 0; // Bytes processed so far
+136 -24
View File
@@ -31,8 +31,8 @@ bool JpegDecoder::prepare_rgb565_scaling_(int width, int height) {
const uint32_t target_width = this->image_->get_buffer_width();
const uint32_t target_height = this->image_->get_buffer_height();
// Boundary n is the first target pixel source pixel n covers, rounded up. When shrinking, the source
// pixel whose span lands on a target pixel paints it, so an 8 to 4 scale keeps columns 0, 2, 4 and 6;
// the generic draw() path rounds down and keeps the odd ones instead
// pixel whose span lands on a target pixel paints it, so an 8 to 4 scale keeps columns 0, 2, 4 and 6,
// as the generic draw() path does
for (uint32_t x = 0; x <= static_cast<uint32_t>(width); x++) {
this->x_boundaries_.push_back(static_cast<uint16_t>((x * target_width + width - 1) / width));
}
@@ -135,13 +135,24 @@ int HOT JpegDecoder::decode(uint8_t *buffer, size_t size) {
ESP_LOGE(TAG, "Could not open image for decoding: %d", this->jpeg_.getLastError());
return DECODE_ERROR_INVALID_TYPE;
}
const int err = this->start_();
if (err != 0)
return err;
if (!this->jpeg_.decode(0, 0, this->scale_option_()))
return this->fail_();
this->decoded_bytes_ = size;
this->jpeg_.close();
return size;
}
int JpegDecoder::start_() {
auto jpeg_type = this->jpeg_.getJPEGType();
if (jpeg_type == JPEG_MODE_INVALID) {
ESP_LOGE(TAG, "Unsupported JPEG image");
return DECODE_ERROR_INVALID_TYPE;
} else if (jpeg_type == JPEG_MODE_PROGRESSIVE) {
ESP_LOGE(TAG, "Progressive JPEG images not supported");
return DECODE_ERROR_INVALID_TYPE;
return DECODE_ERROR_UNSUPPORTED_FORMAT;
}
ESP_LOGD(TAG, "Image size: %d x %d, bpp: %d", this->jpeg_.getWidth(), this->jpeg_.getHeight(), this->jpeg_.getBpp());
@@ -154,32 +165,133 @@ int HOT JpegDecoder::decode(uint8_t *buffer, size_t size) {
} else {
this->jpeg_.setPixelType(RGB8888);
}
if (!this->set_size(this->jpeg_.getWidth(), this->jpeg_.getHeight())) {
const int width = this->jpeg_.getWidth();
const int height = this->jpeg_.getHeight();
if (!this->set_size(width, height)) {
return DECODE_ERROR_OUT_OF_MEMORY;
}
if (this->direct_rgb565_ && !this->prepare_rgb565_scaling_(this->jpeg_.getWidth(), this->jpeg_.getHeight())) {
// JPEGDEC can decode at 1/2, 1/4 or 1/8 size for much less work. Use the smallest size that still has at least as
// many pixels as the buffer, so a large image shown small is not decoded in full only to be thrown away.
const int buffer_width = this->image_->get_buffer_width();
const int buffer_height = this->image_->get_buffer_height();
this->scale_ = 1;
while (this->scale_ < JPEG_SCALE_EIGHTH && width / (this->scale_ * 2) >= buffer_width &&
height / (this->scale_ * 2) >= buffer_height) {
this->scale_ *= 2;
}
const int decoded_width = (width + this->scale_ - 1) / this->scale_;
const int decoded_height = (height + this->scale_ - 1) / this->scale_;
this->draw_width_ = decoded_width;
this->draw_height_ = decoded_height;
if (this->scale_ != 1) {
ESP_LOGD(TAG, "Decoding at 1/%d size, %d x %d", this->scale_, decoded_width, decoded_height);
}
if (this->direct_rgb565_ && !this->prepare_rgb565_scaling_(decoded_width, decoded_height)) {
return DECODE_ERROR_OUT_OF_MEMORY;
}
if (!this->jpeg_.decode(0, 0, 0)) {
auto error = this->jpeg_.getLastError();
ESP_LOGE(TAG, "Error while decoding: %d", error);
this->jpeg_.close();
switch (error) {
case JPEG_ERROR_MEMORY:
return DECODE_ERROR_OUT_OF_MEMORY;
case JPEG_UNSUPPORTED_FEATURE:
return DECODE_ERROR_UNSUPPORTED_FORMAT;
case JPEG_INVALID_FILE:
case JPEG_INVALID_PARAMETER:
return DECODE_ERROR_INVALID_TYPE;
case JPEG_DECODE_ERROR:
default:
return DECODE_ERROR_INTERNAL_DECODER_ERROR;
}
}
this->decoded_bytes_ = size;
return 0;
}
int JpegDecoder::fail_() {
const bool read_failed = this->source_ != nullptr && this->source_failed_;
const int error = this->jpeg_.getLastError();
this->jpeg_.close();
return size;
this->source_ = nullptr;
if (read_failed) {
ESP_LOGE(TAG, "Reading the image failed");
return DECODE_ERROR_READ;
}
ESP_LOGE(TAG, "Error while decoding: %d", error);
switch (error) {
case JPEG_ERROR_MEMORY:
return DECODE_ERROR_OUT_OF_MEMORY;
case JPEG_UNSUPPORTED_FEATURE:
return DECODE_ERROR_UNSUPPORTED_FORMAT;
case JPEG_INVALID_FILE:
case JPEG_INVALID_PARAMETER:
return DECODE_ERROR_INVALID_TYPE;
case JPEG_DECODE_ERROR:
default:
return DECODE_ERROR_INTERNAL_DECODER_ERROR;
}
}
void JpegDecoder::reset() {
// A decode from a source that was given up part way still has the image open
if (this->source_ != nullptr) {
this->jpeg_.close();
this->source_ = nullptr;
}
ImageDecoder::reset();
}
int32_t JpegDecoder::source_read(JPEGFILE *file, uint8_t *buf, int32_t len) {
auto *decoder = static_cast<JpegDecoder *>(file->fHandle);
const int32_t n = decoder->source_->read(buf, len);
if (n < 0) {
// JPEGDEC has no way to report a failed read, so remember it and give it no data
decoder->source_failed_ = true;
return 0;
}
file->iPos += n;
return n;
}
int32_t JpegDecoder::source_seek(JPEGFILE *file, int32_t position) {
auto *decoder = static_cast<JpegDecoder *>(file->fHandle);
if (position < 0 || !decoder->source_->seek(position)) {
decoder->source_failed_ = true;
return file->iPos;
}
file->iPos = position;
return position;
}
int JpegDecoder::begin_source(DecodeSource *source) {
if (this->expected_size_ == 0 || this->expected_size_ > INT32_MAX) {
ESP_LOGE(TAG, "Image size %zu cannot be read from a source", this->expected_size_);
return DECODE_ERROR_INVALID_TYPE;
}
if (!source->seek(0)) {
ESP_LOGE(TAG, "Could not read the image");
return DECODE_ERROR_READ;
}
this->source_ = source;
this->source_failed_ = false;
if (!this->jpeg_.open(this, this->expected_size_, source_close, source_read, source_seek, draw_callback)) {
// The caller may recover, for example by reading the whole image first, so it reports the failure
ESP_LOGD(TAG, "Could not open image for decoding: %d", this->jpeg_.getLastError());
this->source_ = nullptr;
return this->source_failed_ ? DECODE_ERROR_READ : DECODE_ERROR_INVALID_TYPE;
}
const int err = this->start_();
if (err != 0) {
this->jpeg_.close();
this->source_ = nullptr;
return err;
}
if (!this->jpeg_.decodeBegin(0, 0, this->scale_option_()))
return this->fail_();
return 0;
}
int HOT JpegDecoder::decode_step() {
if (this->source_ == nullptr)
return DECODE_ERROR_INVALID_TYPE;
const int result = this->jpeg_.decodeRows(1);
// JPEGDEC may decode on with the missing data, so check the read before its result
if (this->source_failed_)
return this->fail_();
if (result == JPEG_DECODE_MORE)
return DECODE_STEP_MORE;
if (result == JPEG_DECODE_NEED_DATA)
return DECODE_STEP_NEED_DATA;
if (result == 0)
return this->fail_();
this->decoded_bytes_ = this->expected_size_;
this->jpeg_.close();
this->source_ = nullptr;
return DECODE_STEP_DONE;
}
} // namespace esphome::runtime_image
@@ -22,19 +22,41 @@ class JpegDecoder : public ImageDecoder {
~JpegDecoder() override {}
int HOT decode(uint8_t *buffer, size_t size) override;
void reset() override;
/// The source is read as the decode needs it, through JPEGDEC's own 2 KB buffer
bool supports_source() const override { return true; }
int begin_source(DecodeSource *source) override;
/// Decodes one row of MCUs (8 or 16 pixel rows), or waits part way through one for more of the source
int HOT decode_step() override;
bool uses_direct_rgb565() const { return this->direct_rgb565_; }
/// JPEGDEC decodes the image at 1/get_scale() of its size: 1, 2, 4 or 8
int get_scale() const { return this->scale_; }
void draw_rgb565(int x, int y, int w, int h, const uint8_t *pixels);
protected:
bool prepare_rgb565_scaling_(int width, int height);
/// Set up an opened image for decoding. Returns 0, or a DecodeError.
int start_();
/// The JPEGDEC option for scale_: JPEG_SCALE_HALF, _QUARTER and _EIGHTH are the divisors themselves
int scale_option_() const { return this->scale_ == 1 ? 0 : this->scale_; }
/// The DecodeError for a failed read of the source or JPEGDEC's last error, closing the image
int fail_();
static int32_t source_read(JPEGFILE *file, uint8_t *buf, int32_t len);
static int32_t source_seek(JPEGFILE *file, int32_t position);
static void source_close(void *handle) {}
JPEGDEC jpeg_{};
FixedVector<uint16_t> x_boundaries_{};
FixedVector<uint16_t> y_boundaries_{};
int source_width_{0};
int source_height_{0};
int scale_{1}; // JPEGDEC decodes at 1/scale_ of the image size
DecodeSource *source_{nullptr}; // Set while a begin_source() decode is in progress
bool direct_rgb565_{false};
bool source_failed_{false};
};
} // namespace esphome::runtime_image
@@ -239,6 +239,25 @@ int RuntimeImage::feed_data(uint8_t *data, size_t len) {
return consumed;
}
int RuntimeImage::begin_source(DecodeSource *source) {
if (!this->decoder_supports_source()) {
ESP_LOGE(TAG, "Decoder cannot read a source");
return DECODE_ERROR_UNSUPPORTED_FORMAT;
}
return this->decoder_->begin_source(source);
}
int RuntimeImage::decode_step() {
if (!this->is_decoding()) {
ESP_LOGE(TAG, "No decoder initialized");
return DECODE_ERROR_INVALID_TYPE;
}
const int result = this->decoder_->decode_step();
if (result == DECODE_STEP_DONE)
this->decoded_bytes_ = this->decoder_->get_decoded_bytes();
return result;
}
bool RuntimeImage::end_decode() {
if (!this->is_decoding()) {
return false;
@@ -76,6 +76,22 @@ class RuntimeImage : public image::Image {
*/
int feed_data(uint8_t *data, size_t len);
/// Whether the active decoder can read a DecodeSource itself (see begin_source()).
bool decoder_supports_source() const { return this->is_decoding() && this->decoder_->supports_source(); }
/**
* @brief Decode from a source the decoder reads itself, in place of feed_data().
*
* Call after begin_decode() when decoder_supports_source() is true, once the image headers can be read from the
* source, then call decode_step() until it
* returns DECODE_STEP_DONE, and finish with end_decode().
* @return 0 on success, or a DecodeError value.
*/
int begin_source(DecodeSource *source);
/// Decode a little more from the source; see ImageDecoder::decode_step().
int decode_step();
/**
* @brief Complete the decoding process.
*
@@ -13,6 +13,7 @@ from esphome.components import (
)
from esphome.components.const import (
CONF_ANNOUNCEMENT_VOLUME,
CONF_BITRATE,
CONF_VOLUME_INCREMENT,
CONF_VOLUME_INITIAL,
CONF_VOLUME_MAX,
@@ -107,6 +108,7 @@ PIPELINE_SCHEMA = cv.Schema(
cv.Optional(CONF_FORMAT, default="FLAC"): cv.enum(audio.AUDIO_FILE_TYPE_ENUM),
cv.Optional(CONF_SAMPLE_RATE): cv.int_range(min=1),
cv.Optional(CONF_NUM_CHANNELS): cv.int_range(1, 2),
cv.Optional(CONF_BITRATE): media_player.validate_mp3_bitrate,
}
)
@@ -3,6 +3,7 @@ import esphome.codegen as cg
from esphome.components import audio, media_player, media_source, speaker
from esphome.components.const import (
CONF_ANNOUNCEMENT_VOLUME,
CONF_BITRATE,
CONF_SOURCES,
CONF_VOLUME_INCREMENT,
CONF_VOLUME_INITIAL,
@@ -78,6 +79,7 @@ PIPELINE_SCHEMA = cv.Schema(
cv.Optional(CONF_FORMAT, default="FLAC"): cv.enum(audio.AUDIO_FILE_TYPE_ENUM),
cv.Optional(CONF_SAMPLE_RATE): cv.int_range(min=1),
cv.Optional(CONF_NUM_CHANNELS): cv.int_range(1, 2),
cv.Optional(CONF_BITRATE): media_player.validate_mp3_bitrate,
}
)
+138
View File
@@ -0,0 +1,138 @@
"""VNC (RFB) client that shows a remote screen on a display."""
from esphome import automation
import esphome.codegen as cg
from esphome.components import display, network, socket
from esphome.components.const import (
BYTE_ORDER_LITTLE,
CONF_HOST,
CONF_RECONNECT_INTERVAL,
)
import esphome.config_validation as cv
from esphome.const import (
CONF_AUTO_CLEAR_ENABLED,
CONF_DISPLAY_ID,
CONF_ID,
CONF_ON_CONNECT,
CONF_ON_DISCONNECT,
CONF_PLATFORM,
CONF_PORT,
)
import esphome.final_validate as fv
from esphome.types import ConfigType
CODEOWNERS = ["@MindFreeze"]
DEPENDENCIES = ["display", "network"]
AUTO_LOAD = ["socket"]
DOMAIN = "vnc"
MULTI_CONF = True
CONF_MAX_UPDATE_DELAY = "max_update_delay"
CONF_UPDATE_DELAY = "update_delay"
vnc_ns = cg.esphome_ns.namespace("vnc")
VncClient = vnc_ns.class_("VncClient", cg.Component)
# Drivers that cannot take another component's pixels yet.
UNSUPPORTED_DISPLAYS = dict.fromkeys(
("mipi_dsi", "mipi_rgb", "mipi_spi"), "it is not supported by vnc yet"
) | {
"waveshare_epaper": "it draws every colour except pure black as ink; use epaper_spi"
}
def _register_writer(config: ConfigType) -> ConfigType:
display.register_external_writer(config[CONF_DISPLAY_ID], DOMAIN)
return config
CONFIG_SCHEMA = cv.All(
cv.Schema(
{
cv.GenerateID(): cv.declare_id(VncClient),
cv.GenerateID(CONF_DISPLAY_ID): cv.use_id(display.Display),
cv.Required(CONF_HOST): socket.ipv4_host,
cv.Optional(CONF_PORT, default=5900): cv.port,
cv.Optional(CONF_RECONNECT_INTERVAL, default="5s"): cv.All(
cv.positive_time_period_milliseconds,
cv.Range(min=cv.TimePeriod(seconds=1)),
),
cv.Optional(
CONF_UPDATE_DELAY, default="200ms"
): cv.positive_time_period_milliseconds,
cv.Optional(
CONF_MAX_UPDATE_DELAY, default="1s"
): cv.positive_time_period_milliseconds,
cv.Optional(CONF_ON_CONNECT): automation.validate_automation({}),
cv.Optional(CONF_ON_DISCONNECT): automation.validate_automation({}),
}
).extend(cv.COMPONENT_SCHEMA),
_register_writer,
socket.consume_sockets(1, DOMAIN),
network.require_ipv4(DOMAIN),
)
def _final_validate(config: ConfigType) -> None:
full_config = fv.full_config.get()
disp_id = config[CONF_DISPLAY_ID]
disp_conf = fv.get_declaring_config(disp_id)
if (platform := disp_conf.get(CONF_PLATFORM)) in UNSUPPORTED_DISPLAYS:
raise cv.Invalid(
f"Display '{disp_id}' uses the {platform} platform: "
f"{UNSUPPORTED_DISPLAYS[platform]}",
path=[CONF_DISPLAY_ID],
)
if (
display.get_display_metadata(disp_id).has_writer
or disp_conf.get(CONF_AUTO_CLEAR_ENABLED) is True
):
raise cv.Invalid(
f"vnc draws the whole of display '{disp_id}'; remove lambda, pages, "
"auto_clear_enabled and show_test_card from it",
path=[CONF_DISPLAY_ID],
)
if len(writers := display.get_external_writers(disp_id)) > 1:
raise cv.Invalid(
f"Display '{disp_id}' is drawn by more than one component: {', '.join(writers)}",
path=[CONF_DISPLAY_ID],
)
lvgl_configs = full_config.get("lvgl") or []
if isinstance(lvgl_configs, dict):
lvgl_configs = [lvgl_configs]
for lvgl_config in lvgl_configs:
if any(d.id == disp_id.id for d in lvgl_config.get("displays", [])):
raise cv.Invalid(
f"Display '{disp_id}' is also used by LVGL", path=[CONF_DISPLAY_ID]
)
FINAL_VALIDATE_SCHEMA = _final_validate
_CALLBACK_AUTOMATIONS = (
automation.CallbackAutomation(CONF_ON_CONNECT, "add_on_connect_callback"),
automation.CallbackAutomation(CONF_ON_DISCONNECT, "add_on_disconnect_callback"),
)
async def to_code(config: ConfigType) -> None:
disp = await cg.get_variable(config[CONF_DISPLAY_ID])
# Ask the server for the display's own byte order, so block-copying drivers skip the per-pixel path
meta = display.get_display_metadata(config[CONF_DISPLAY_ID])
var = cg.new_Pvariable(config[CONF_ID], disp, meta.byte_order != BYTE_ORDER_LITTLE)
await cg.register_component(var, config)
socket.require_tcp_client_link()
cg.add(var.set_host(config[CONF_HOST]))
cg.add(var.set_port(config[CONF_PORT]))
cg.add(var.set_reconnect_interval(config[CONF_RECONNECT_INTERVAL]))
cg.add(var.set_update_delay(config[CONF_UPDATE_DELAY]))
cg.add(var.set_max_update_delay(config[CONF_MAX_UPDATE_DELAY]))
await automation.build_callback_automations(var, config, _CALLBACK_AUTOMATIONS)
VNC_ID_SCHEMA = automation.maybe_simple_id(
{cv.GenerateID(): cv.use_id(VncClient)},
)
automation.register_apply_condition("vnc.is_connected", VNC_ID_SCHEMA, "is_connected()")
+702
View File
@@ -0,0 +1,702 @@
#include "vnc_client.h"
#include "esphome/components/network/util.h"
#include "esphome/core/application.h"
#include "esphome/core/hal.h"
#include "esphome/core/log.h"
#include <algorithm>
#include <cinttypes>
#include <cstring>
namespace esphome::vnc {
ESPHOME_LOG_TAG(TAG, "vnc");
// Below the 50 ms blocking warning, and the same slice it8951 uses for its transfers.
static constexpr uint32_t LOOP_BUDGET_MS = 20;
static constexpr uint32_t HANDSHAKE_TIMEOUT_MS = 15000;
static constexpr uint32_t STABLE_SESSION_MS = 60000;
static constexpr uint32_t MAX_BACKOFF_MS = 60000;
static constexpr uint32_t CONFIG_RETRY_MS = 300000;
static constexpr uint32_t HEARTBEAT_MS = 60000;
static constexpr uint32_t MIN_REQUEST_SPACING_MS = 50;
// The longest the first refresh waits for a display with a loop to park once after boot.
static constexpr uint32_t DISPLAY_START_MS = 5000;
// Longer than any driver's own busy timeout: a panel that stays busy this long is stuck
static constexpr uint32_t DISPLAY_BUSY_TIMEOUT_MS = 120000;
static constexpr uint16_t MAX_CURSOR_SIDE = 1024;
static constexpr uint32_t MAX_TEXT_LEN = 63;
static constexpr uint8_t SECURITY_NONE = 1;
static constexpr uint8_t SECURITY_VNC_AUTH = 2;
static constexpr int32_t ENCODING_RAW = 0;
static constexpr int32_t ENCODING_RICH_CURSOR = -239;
static constexpr uint8_t MSG_FRAMEBUFFER_UPDATE = 0;
static constexpr uint8_t MSG_SET_COLOUR_MAP = 1;
static constexpr uint8_t MSG_BELL = 2;
static constexpr uint8_t MSG_SERVER_CUT_TEXT = 3;
static inline uint16_t read_be16(const uint8_t *p) { return encode_uint16(p[0], p[1]); }
static inline uint32_t read_be32(const uint8_t *p) { return encode_uint32(p[0], p[1], p[2], p[3]); }
static inline void write_be16(uint8_t *p, uint16_t v) {
p[0] = v >> 8;
p[1] = v;
}
static inline void write_be32(uint8_t *p, uint32_t v) {
p[0] = v >> 24;
p[1] = v >> 16;
p[2] = v >> 8;
p[3] = v;
}
static int parse_three_digits(const uint8_t *p) {
int value = 0;
for (int i = 0; i < 3; i++) {
if (p[i] < '0' || p[i] > '9') {
return -1;
}
value = value * 10 + (p[i] - '0');
}
return value;
}
void VncClient::setup() {
this->setup_ms_ = App.get_loop_component_start_time();
this->link_.begin(TAG);
}
void VncClient::dump_config() {
ESP_LOGCONFIG(TAG,
"VNC:\n"
" Server: %s:%u\n"
" Reconnect Interval: %" PRIu32 "ms\n"
" Update Delay: %" PRIu32 "ms\n"
" Max Update Delay: %" PRIu32 "ms",
this->link_.host(), this->link_.port(), this->link_.reconnect_interval(), this->update_delay_ms_,
this->max_update_delay_ms_);
}
void VncClient::on_shutdown() { this->link_.close(); }
void VncClient::loop() {
const uint32_t now = App.get_loop_component_start_time();
this->pass_start_ms_ = millis();
if (this->display_->is_failed()) {
this->link_.close(); // a connect still in flight would otherwise keep its socket for good
if (this->state_ != VncState::VNC_STATE_IDLE) {
this->session_end_(now, VncFailure::VNC_FAILURE_TRANSIENT, LOG_STR("Display failed"));
}
this->status_set_error(LOG_STR("Display failed"));
this->disable_loop();
return;
}
// A display that parks its loop when idle is busy while that loop runs. Displays that never
// park (no loop, or one that always runs) update synchronously, so they are never busy.
if (this->display_->is_idle()) {
this->display_parks_ = true;
this->display_busy_ = false;
} else {
this->display_busy_ = this->display_parks_;
}
// A stuck panel, or another owner of the display's loop, would otherwise freeze the screen silently
if (!this->display_busy_) {
this->busy_since_ms_ = now;
if (this->busy_warned_) {
this->busy_warned_ = false;
// The connection path owns the warning while no session runs
if (this->state_ == VncState::VNC_STATE_RUNNING) {
this->status_clear_warning();
}
}
} else if (!this->busy_warned_ && now - this->busy_since_ms_ >= DISPLAY_BUSY_TIMEOUT_MS) {
this->busy_warned_ = true;
this->status_set_warning(LOG_STR("Display busy too long"));
}
if (!this->link_.connected()) {
if (this->state_ != VncState::VNC_STATE_IDLE) {
this->session_lost_(now);
}
this->connect_step_(now);
if (!this->link_.connected()) {
return;
}
this->start_handshake_(now);
}
if (this->state_ == VncState::VNC_STATE_HANDSHAKE && now - this->session_start_ms_ >= HANDSHAKE_TIMEOUT_MS) {
this->fail_(this->lost_failure_(), LOG_STR("Handshake timed out"));
return;
}
const bool blocked = this->in_update_ && this->display_busy_;
// A display still starting up (it8951) can go from its start-up straight into a refresh without parking.
const bool display_starting =
!this->display_parks_ && this->display_->has_overridden_loop() && now - this->setup_ms_ < DISPLAY_START_MS;
// Wait until the screen stops changing, so an animation becomes one refresh showing where it ended.
if (this->refresh_pending_ && !this->in_update_ && !this->display_busy_ && !display_starting &&
(now - this->last_change_ms_ >= this->update_delay_ms_ ||
now - this->first_change_ms_ >= this->max_update_delay_ms_)) {
// Synchronous displays flush inside update(), so this pass does nothing else.
this->refresh_pending_ = false;
this->display_->update();
this->display_busy_ = this->display_parks_ && !this->display_->is_idle();
} else if (!blocked && (this->rx_pending_ || this->link_.ready())) {
this->service_rx_(now);
}
if (this->state_ == VncState::VNC_STATE_RUNNING) {
// Mid-frame there is nothing to send, so a vanished server would never fail a write.
if (this->in_update_ && !blocked && !this->rx_pending_ && now - this->last_rx_ms_ >= HEARTBEAT_MS) {
this->fail_(VncFailure::VNC_FAILURE_TRANSIENT, LOG_STR("Server stopped sending"));
return;
}
this->pace_(now);
}
this->link_.flush_tx();
if (this->rx_pending_ && !(this->in_update_ && this->display_busy_)) {
this->high_freq_.start();
} else {
this->high_freq_.stop();
}
}
void VncClient::connect_step_(uint32_t now) {
const bool up = network::is_connected();
if (up && !this->network_was_up_) {
this->backoff_ms_ = 0;
this->hold_ms_ = 0;
}
this->network_was_up_ = up;
if (!up) {
return;
}
if (!this->link_.connecting() && (now - this->hold_start_ms_ < this->hold_ms_ || this->link_.in_backoff())) {
return;
}
this->link_.poll();
// DNS failures, refused connects and connect timeouts all leave no socket and start the link's backoff.
if (!this->link_.connected() && !this->link_.connecting() && this->link_.in_backoff()) {
this->session_end_(now, VncFailure::VNC_FAILURE_TRANSIENT, LOG_STR("Cannot reach the server"));
}
}
void VncClient::reset_session_() {
this->rx_pos_ = 0;
this->rx_len_ = 0;
this->rx_pending_ = false;
this->in_update_ = false;
this->request_outstanding_ = false;
this->refresh_pending_ = false;
this->frame_drawn_ = false;
this->step_ = VncStep::VNC_STEP_VERSION;
this->reason_failure_ = VncFailure::VNC_FAILURE_TRANSIENT;
this->high_freq_.stop();
}
void VncClient::start_handshake_(uint32_t now) {
this->reset_session_();
this->state_ = VncState::VNC_STATE_HANDSHAKE;
this->session_start_ms_ = now;
this->last_rx_ms_ = now;
}
VncFailure VncClient::lost_failure_() const {
if (this->state_ == VncState::VNC_STATE_HANDSHAKE) {
if (this->step_ == VncStep::VNC_STEP_REASON_LENGTH || this->step_ == VncStep::VNC_STEP_REASON) {
return this->reason_failure_;
}
}
return VncFailure::VNC_FAILURE_TRANSIENT;
}
void VncClient::session_lost_(uint32_t now) {
this->session_end_(now, this->lost_failure_(), LOG_STR("Connection lost"));
}
VncParse VncClient::fail_(VncFailure failure, const LogString *reason) {
this->link_.close();
this->link_.note_attempt();
this->session_end_(App.get_loop_component_start_time(), failure, reason);
return VncParse::VNC_PARSE_STOP;
}
void VncClient::session_end_(uint32_t now, VncFailure failure, const LogString *reason) {
const bool was_running = this->state_ == VncState::VNC_STATE_RUNNING;
this->reset_session_();
this->state_ = VncState::VNC_STATE_IDLE;
this->hold_start_ms_ = now;
if (was_running) {
const uint32_t lasted = now - this->session_start_ms_;
ESP_LOGW(TAG, "%s after %" PRIu32 "s", LOG_STR_ARG(reason), lasted / 1000);
this->disconnect_callback_.call();
this->status_set_warning();
if (lasted >= STABLE_SESSION_MS) {
this->backoff_ms_ = 0;
this->hold_ms_ = 0;
return;
}
failure = VncFailure::VNC_FAILURE_TRANSIENT;
}
switch (failure) {
case VncFailure::VNC_FAILURE_CONFIG:
this->hold_ms_ = CONFIG_RETRY_MS;
ESP_LOGE(TAG, "%s; retrying in %" PRIu32 "s", LOG_STR_ARG(reason), this->hold_ms_ / 1000);
this->status_set_error(reason);
return;
default: {
const uint32_t base = this->link_.reconnect_interval();
this->backoff_ms_ =
this->backoff_ms_ == 0 ? base : std::min(this->backoff_ms_ * 2, std::max(MAX_BACKOFF_MS, base));
this->hold_ms_ = this->backoff_ms_;
if (!was_running) {
ESP_LOGW(TAG, "%s; retrying in %" PRIu32 "s", LOG_STR_ARG(reason), this->hold_ms_ / 1000);
}
this->status_set_warning();
return;
}
}
}
bool VncClient::budget_exhausted_() const { return millis() - this->pass_start_ms_ >= LOOP_BUDGET_MS; }
void VncClient::service_rx_(uint32_t now) {
for (;;) {
VncParse result = this->parse_();
if (result == VncParse::VNC_PARSE_STOP) {
return;
}
if (result == VncParse::VNC_PARSE_PAUSE) {
this->rx_pending_ = true;
return;
}
// Less than one item is left, so the buffer always has room after compacting.
if (this->rx_pos_ != 0) {
this->rx_len_ -= this->rx_pos_;
std::memmove(this->rx_, this->rx_ + this->rx_pos_, this->rx_len_);
this->rx_pos_ = 0;
}
if (this->budget_exhausted_()) {
this->rx_pending_ = true;
return;
}
ssize_t count = this->link_.read(this->rx_ + this->rx_len_, RX_BUFFER_SIZE - this->rx_len_);
if (count < 0) {
this->session_lost_(now);
return;
}
if (count == 0) {
this->rx_pending_ = false;
return;
}
this->rx_len_ += static_cast<uint16_t>(count);
this->rx_total_ += count;
this->last_rx_ms_ = now;
}
}
void VncClient::skip_(uint32_t count, VncStep next) {
this->skip_left_ = count;
this->after_skip_ = next;
this->step_ = VncStep::VNC_STEP_SKIP;
}
bool VncClient::send_client_init_() {
const uint8_t shared = 1;
return this->send_(&shared, 1);
}
VncParse VncClient::parse_() {
for (;;) {
const uint8_t *p = this->rx_ + this->rx_pos_;
const size_t avail = this->rx_len_ - this->rx_pos_;
switch (this->step_) {
case VncStep::VNC_STEP_VERSION: {
if (avail < 12) {
return VncParse::VNC_PARSE_MORE;
}
const int major = parse_three_digits(p + 4);
const int minor = parse_three_digits(p + 8);
if (std::memcmp(p, "RFB ", 4) != 0 || p[7] != '.' || p[11] != '\n' || major < 0 || minor < 0) {
return this->fail_(VncFailure::VNC_FAILURE_CONFIG, LOG_STR("Not a VNC server"));
}
if (major < 3) {
return this->fail_(VncFailure::VNC_FAILURE_CONFIG, LOG_STR("Unsupported RFB version"));
}
this->minor_ = (major > 3 || minor >= 8) ? 8 : (minor == 7 ? 7 : 3);
this->rx_pos_ += 12;
uint8_t reply[12] = {'R', 'F', 'B', ' ', '0', '0', '3', '.', '0', '0', '8', '\n'};
reply[10] = '0' + this->minor_;
if (!this->send_(reply, sizeof(reply))) {
return this->fail_send_();
}
this->step_ = this->minor_ == 3 ? VncStep::VNC_STEP_SECURITY_TYPE_33 : VncStep::VNC_STEP_SECURITY_COUNT;
break;
}
case VncStep::VNC_STEP_SECURITY_COUNT: {
if (avail < 1) {
return VncParse::VNC_PARSE_MORE;
}
this->sec_count_ = p[0];
this->rx_pos_ += 1;
if (this->sec_count_ == 0) {
// A refusal before authentication, such as a lockout caused by another client.
this->step_ = VncStep::VNC_STEP_REASON_LENGTH;
} else {
this->step_ = VncStep::VNC_STEP_SECURITY_LIST;
}
break;
}
case VncStep::VNC_STEP_SECURITY_LIST: {
if (avail < this->sec_count_) {
return VncParse::VNC_PARSE_MORE;
}
uint8_t chosen = 0;
bool wants_password = false;
for (uint8_t i = 0; i < this->sec_count_; i++) {
if (p[i] == SECURITY_NONE) {
chosen = p[i];
break;
}
wants_password |= p[i] == SECURITY_VNC_AUTH;
}
this->rx_pos_ += this->sec_count_;
if (chosen == 0) {
return this->fail_(VncFailure::VNC_FAILURE_CONFIG, wants_password ? LOG_STR("The server requires a password")
: LOG_STR("No supported security type"));
}
if (!this->send_(&chosen, 1)) {
return this->fail_send_();
}
if (this->minor_ == 8) {
this->step_ = VncStep::VNC_STEP_SECURITY_RESULT;
} else {
if (!this->send_client_init_()) {
return this->fail_send_();
}
this->step_ = VncStep::VNC_STEP_SERVER_INIT;
}
break;
}
case VncStep::VNC_STEP_SECURITY_TYPE_33: {
if (avail < 4) {
return VncParse::VNC_PARSE_MORE;
}
const uint32_t type = read_be32(p);
this->rx_pos_ += 4;
if (type == 0) {
this->step_ = VncStep::VNC_STEP_REASON_LENGTH;
} else if (type == SECURITY_NONE) {
if (!this->send_client_init_()) {
return this->fail_send_();
}
this->step_ = VncStep::VNC_STEP_SERVER_INIT;
} else {
return this->fail_(VncFailure::VNC_FAILURE_CONFIG, type == SECURITY_VNC_AUTH
? LOG_STR("The server requires a password")
: LOG_STR("No supported security type"));
}
break;
}
case VncStep::VNC_STEP_SECURITY_RESULT: {
if (avail < 4) {
return VncParse::VNC_PARSE_MORE;
}
const uint32_t result = read_be32(p);
this->rx_pos_ += 4;
if (result == 0) {
if (!this->send_client_init_()) {
return this->fail_send_();
}
this->step_ = VncStep::VNC_STEP_SERVER_INIT;
break;
}
this->reason_failure_ = VncFailure::VNC_FAILURE_CONFIG;
this->step_ = VncStep::VNC_STEP_REASON_LENGTH;
break;
}
case VncStep::VNC_STEP_REASON_LENGTH: {
if (avail < 4) {
return VncParse::VNC_PARSE_MORE;
}
this->text_len_ = static_cast<uint8_t>(std::min(read_be32(p), MAX_TEXT_LEN));
this->rx_pos_ += 4;
this->step_ = VncStep::VNC_STEP_REASON;
break;
}
case VncStep::VNC_STEP_REASON: {
if (avail < this->text_len_) {
return VncParse::VNC_PARSE_MORE;
}
ESP_LOGE(TAG, "Server: %.*s", static_cast<int>(this->text_len_), reinterpret_cast<const char *>(p));
return this->fail_(this->reason_failure_, LOG_STR("Connection refused"));
}
case VncStep::VNC_STEP_SERVER_INIT: {
if (avail < 24) {
return VncParse::VNC_PARSE_MORE;
}
this->fb_w_ = read_be16(p);
this->fb_h_ = read_be16(p + 2);
// Reused for the length of the name that follows.
this->skip_left_ = read_be32(p + 20);
this->rx_pos_ += 24;
if (this->fb_w_ == 0 || this->fb_h_ == 0) {
return this->fail_(VncFailure::VNC_FAILURE_TRANSIENT, LOG_STR("The server screen is empty"));
}
this->text_len_ = static_cast<uint8_t>(std::min(this->skip_left_, MAX_TEXT_LEN));
this->step_ = VncStep::VNC_STEP_SERVER_NAME;
break;
}
case VncStep::VNC_STEP_SERVER_NAME: {
if (avail < this->text_len_) {
return VncParse::VNC_PARSE_MORE;
}
ESP_LOGI(TAG, "Server '%.*s', %ux%u, RFB 3.%u", static_cast<int>(this->text_len_),
reinterpret_cast<const char *>(p), this->fb_w_, this->fb_h_, this->minor_);
this->rx_pos_ += this->text_len_;
this->skip_(this->skip_left_ - this->text_len_, VncStep::VNC_STEP_SESSION_START);
break;
}
case VncStep::VNC_STEP_SESSION_START:
if (!this->start_session_(App.get_loop_component_start_time())) {
return this->fail_send_();
}
this->step_ = VncStep::VNC_STEP_MESSAGE;
break;
case VncStep::VNC_STEP_MESSAGE: {
if (avail < 1) {
return VncParse::VNC_PARSE_MORE;
}
switch (p[0]) {
case MSG_FRAMEBUFFER_UPDATE:
if (avail < 4) {
return VncParse::VNC_PARSE_MORE;
}
this->rects_left_ = read_be16(p + 2);
this->rx_pos_ += 4;
this->in_update_ = true;
this->update_start_ms_ = millis();
this->update_start_bytes_ = this->rx_total_ - (this->rx_len_ - this->rx_pos_);
this->rect_done_();
break;
case MSG_SET_COLOUR_MAP:
if (avail < 6) {
return VncParse::VNC_PARSE_MORE;
}
this->rx_pos_ += 6;
this->skip_(6u * read_be16(p + 4), VncStep::VNC_STEP_MESSAGE);
break;
case MSG_BELL:
this->rx_pos_ += 1;
break;
case MSG_SERVER_CUT_TEXT:
if (avail < 8) {
return VncParse::VNC_PARSE_MORE;
}
this->rx_pos_ += 8;
this->skip_(read_be32(p + 4), VncStep::VNC_STEP_MESSAGE);
break;
default:
ESP_LOGW(TAG, "Unknown message type %u", p[0]);
return this->fail_(VncFailure::VNC_FAILURE_TRANSIENT, LOG_STR("Protocol error"));
}
break;
}
case VncStep::VNC_STEP_RECT: {
if (this->display_busy_) {
return VncParse::VNC_PARSE_PAUSE;
}
if (avail < 12) {
return VncParse::VNC_PARSE_MORE;
}
this->rect_x_ = read_be16(p);
this->rect_y_ = read_be16(p + 2);
this->rect_w_ = read_be16(p + 4);
this->rect_h_ = read_be16(p + 6);
const auto encoding = static_cast<int32_t>(read_be32(p + 8));
this->rx_pos_ += 12;
this->rects_left_--;
if (encoding == ENCODING_RAW) {
if (static_cast<uint32_t>(this->rect_x_) + this->rect_w_ > this->fb_w_ ||
static_cast<uint32_t>(this->rect_y_) + this->rect_h_ > this->fb_h_) {
ESP_LOGW(TAG, "Rectangle %ux%u at %u,%u is outside the %ux%u screen", this->rect_w_, this->rect_h_,
this->rect_x_, this->rect_y_, this->fb_w_, this->fb_h_);
return this->fail_(VncFailure::VNC_FAILURE_TRANSIENT, LOG_STR("Protocol error"));
}
if (this->rect_w_ == 0 || this->rect_h_ == 0) {
this->rect_done_();
} else {
this->pos_x_ = 0;
this->pos_y_ = 0;
this->step_ = VncStep::VNC_STEP_RAW;
}
} else if (encoding == ENCODING_RICH_CURSOR) {
if (this->rect_w_ > MAX_CURSOR_SIDE || this->rect_h_ > MAX_CURSOR_SIDE) {
return this->fail_(VncFailure::VNC_FAILURE_TRANSIENT, LOG_STR("Protocol error"));
}
const uint32_t size = static_cast<uint32_t>(this->rect_w_) * this->rect_h_ * this->bpp_ +
((this->rect_w_ + 7u) / 8u) * this->rect_h_;
this->skip_(size, VncStep::VNC_STEP_RECT_DONE);
} else {
ESP_LOGW(TAG, "Unsupported encoding %" PRId32, encoding);
return this->fail_(VncFailure::VNC_FAILURE_TRANSIENT, LOG_STR("Protocol error"));
}
break;
}
case VncStep::VNC_STEP_RECT_DONE:
this->rect_done_();
break;
case VncStep::VNC_STEP_RAW: {
if (this->display_busy_) {
return VncParse::VNC_PARSE_PAUSE;
}
const size_t pixels = avail / this->bpp_;
if (pixels == 0) {
return VncParse::VNC_PARSE_MORE;
}
if (this->budget_exhausted_()) {
return VncParse::VNC_PARSE_PAUSE;
}
const auto count = static_cast<uint16_t>(std::min<size_t>(pixels, this->rect_w_ - this->pos_x_));
this->draw_row_(this->rect_x_ + this->pos_x_, this->rect_y_ + this->pos_y_, count, p);
this->rx_pos_ += count * this->bpp_;
this->pos_x_ += count;
if (this->pos_x_ == this->rect_w_) {
this->pos_x_ = 0;
if (++this->pos_y_ == this->rect_h_) {
this->rect_done_();
}
}
break;
}
case VncStep::VNC_STEP_SKIP: {
const auto count = static_cast<uint32_t>(std::min<size_t>(avail, this->skip_left_));
this->rx_pos_ += count;
this->skip_left_ -= count;
if (this->skip_left_ != 0) {
return VncParse::VNC_PARSE_MORE;
}
this->step_ = this->after_skip_;
break;
}
}
}
}
void VncClient::rect_done_() {
if (this->rects_left_ == 0) {
this->frame_done_();
} else {
this->step_ = VncStep::VNC_STEP_RECT;
}
}
void VncClient::frame_done_() {
this->in_update_ = false;
this->request_outstanding_ = false;
this->step_ = VncStep::VNC_STEP_MESSAGE;
if (this->frame_drawn_) {
const uint32_t now = App.get_loop_component_start_time();
this->frame_drawn_ = false;
if (!this->refresh_pending_) {
this->first_change_ms_ = now;
}
this->last_change_ms_ = now;
this->refresh_pending_ = true;
ESP_LOGD(TAG, "Update: %" PRIu32 " bytes in %" PRIu32 "ms",
this->rx_total_ - (this->rx_len_ - this->rx_pos_) - this->update_start_bytes_,
millis() - this->update_start_ms_);
}
}
void VncClient::draw_row_(uint16_t x, uint16_t y, uint16_t count, const uint8_t *pixels) {
if (y >= this->vis_h_ || x >= this->vis_w_) {
return;
}
const int visible = std::min<int>(count, this->vis_w_ - x);
this->display_->draw_pixels_at(x, y, visible, 1, pixels, this->bitness_, this->big_endian_);
this->frame_drawn_ = true;
}
bool VncClient::start_session_(uint32_t now) {
const bool rgb565 = this->display_->get_display_type() != display::DISPLAY_TYPE_BINARY;
this->bpp_ = rgb565 ? 2 : 1;
this->bitness_ = rgb565 ? display::COLOR_BITNESS_565 : display::COLOR_BITNESS_332;
const int disp_w = this->display_->get_width();
const int disp_h = this->display_->get_height();
this->vis_w_ = std::min<int>(this->fb_w_, disp_w);
this->vis_h_ = std::min<int>(this->fb_h_, disp_h);
if (this->fb_w_ != disp_w || this->fb_h_ != disp_h) {
ESP_LOGW(TAG, "Server screen is %ux%u but the display is %dx%d; showing the top-left %ux%u", this->fb_w_,
this->fb_h_, disp_w, disp_h, this->vis_w_, this->vis_h_);
}
// SetPixelFormat: true colour, in the byte order the display takes
uint8_t pixel_format[20] = {};
pixel_format[4] = rgb565 ? 16 : 8;
pixel_format[5] = rgb565 ? 16 : 8;
pixel_format[6] = this->big_endian_;
pixel_format[7] = 1;
write_be16(pixel_format + 8, rgb565 ? 31 : 7);
write_be16(pixel_format + 10, rgb565 ? 63 : 7);
write_be16(pixel_format + 12, rgb565 ? 31 : 3);
pixel_format[14] = rgb565 ? 11 : 5;
pixel_format[15] = rgb565 ? 5 : 2;
if (!this->send_(pixel_format, sizeof(pixel_format))) {
return false;
}
// SetEncodings: TightVNC uses the first real encoding it supports for every rectangle.
uint8_t encodings[12] = {2, 0, 0, 2};
write_be32(encodings + 4, ENCODING_RAW);
write_be32(encodings + 8, static_cast<uint32_t>(ENCODING_RICH_CURSOR));
if (!this->send_(encodings, sizeof(encodings)) || !this->send_update_request_(false, now)) {
return false;
}
this->state_ = VncState::VNC_STATE_RUNNING;
this->session_start_ms_ = now;
if (!this->busy_warned_) {
this->status_clear_warning(); // a stuck display keeps its warning through a reconnect
}
this->status_clear_error();
this->connect_callback_.call();
return true;
}
void VncClient::pace_(uint32_t now) {
if (this->display_busy_ || this->in_update_ || now - this->last_request_ms_ < MIN_REQUEST_SPACING_MS) {
return;
}
// Repeating a parked request costs nothing on the server and detects a dead link.
if (!this->request_outstanding_ ||
(now - this->last_rx_ms_ >= HEARTBEAT_MS && now - this->last_request_ms_ >= HEARTBEAT_MS)) {
this->send_update_request_(true, now);
}
}
bool VncClient::send_(const uint8_t *data, size_t len) {
if (this->link_.tx_free() < len) {
this->link_.flush_tx();
if (this->link_.tx_free() < len) {
return false;
}
}
this->link_.queue(data, len);
return true;
}
bool VncClient::send_update_request_(bool incremental, uint32_t now) {
uint8_t msg[10] = {3, static_cast<uint8_t>(incremental ? 1 : 0)};
write_be16(msg + 6, this->vis_w_);
write_be16(msg + 8, this->vis_h_);
if (!this->send_(msg, sizeof(msg))) {
return false;
}
this->request_outstanding_ = true;
this->last_request_ms_ = now;
return true;
}
} // namespace esphome::vnc
+162
View File
@@ -0,0 +1,162 @@
#pragma once
#include "esphome/components/display/display.h"
#include "esphome/components/socket/tcp_client_link.h"
#include "esphome/core/component.h"
#include "esphome/core/helpers.h"
#include <cstddef>
#include <cstdint>
namespace esphome::vnc {
enum class VncState : uint8_t {
VNC_STATE_IDLE,
VNC_STATE_HANDSHAKE,
VNC_STATE_RUNNING,
};
enum class VncStep : uint8_t {
VNC_STEP_VERSION,
VNC_STEP_SECURITY_COUNT,
VNC_STEP_SECURITY_LIST,
VNC_STEP_SECURITY_TYPE_33,
VNC_STEP_SECURITY_RESULT,
VNC_STEP_REASON_LENGTH,
VNC_STEP_REASON,
VNC_STEP_SERVER_INIT,
VNC_STEP_SERVER_NAME,
VNC_STEP_SESSION_START,
VNC_STEP_MESSAGE,
VNC_STEP_RECT,
VNC_STEP_RECT_DONE,
VNC_STEP_RAW,
VNC_STEP_SKIP,
};
enum class VncParse : uint8_t {
VNC_PARSE_MORE, // the next step needs bytes that have not arrived
VNC_PARSE_PAUSE, // out of time, or the display is busy; resume without new data
VNC_PARSE_STOP, // the session ended
};
enum class VncFailure : uint8_t {
VNC_FAILURE_TRANSIENT,
VNC_FAILURE_CONFIG,
};
/// RFB (VNC) client that draws the remote framebuffer into a display.
class VncClient : public Component {
public:
explicit VncClient(display::Display *display, bool big_endian = false) : display_(display), big_endian_(big_endian) {}
void set_host(const char *host) { this->link_.set_host(host); }
void set_port(uint16_t port) { this->link_.set_port(port); }
void set_reconnect_interval(uint32_t ms) { this->link_.set_reconnect_interval(ms); }
void set_update_delay(uint32_t ms) { this->update_delay_ms_ = ms; }
void set_max_update_delay(uint32_t ms) { this->max_update_delay_ms_ = ms; }
void setup() override;
void loop() override;
void dump_config() override;
void on_shutdown() override;
float get_setup_priority() const override { return setup_priority::AFTER_WIFI; }
bool is_connected() const { return this->state_ == VncState::VNC_STATE_RUNNING; }
template<typename F> void add_on_connect_callback(F &&callback) {
this->connect_callback_.add(std::forward<F>(callback));
}
template<typename F> void add_on_disconnect_callback(F &&callback) {
this->disconnect_callback_.add(std::forward<F>(callback));
}
protected:
static constexpr size_t RX_BUFFER_SIZE = 512;
void connect_step_(uint32_t now);
void start_handshake_(uint32_t now);
void service_rx_(uint32_t now);
VncParse parse_();
void pace_(uint32_t now);
bool start_session_(uint32_t now);
bool send_client_init_();
void frame_done_();
void rect_done_();
void draw_row_(uint16_t x, uint16_t y, uint16_t count, const uint8_t *pixels);
void skip_(uint32_t count, VncStep next);
bool budget_exhausted_() const;
bool send_(const uint8_t *data, size_t len);
bool send_update_request_(bool incremental, uint32_t now);
VncParse fail_(VncFailure failure, const LogString *reason);
VncParse fail_send_() { return this->fail_(VncFailure::VNC_FAILURE_TRANSIENT, LOG_STR("Send failed")); }
VncFailure lost_failure_() const;
void session_lost_(uint32_t now);
void session_end_(uint32_t now, VncFailure failure, const LogString *reason);
void reset_session_();
display::Display *display_;
socket::TcpClientLink link_;
LazyCallbackManager<void()> connect_callback_;
LazyCallbackManager<void()> disconnect_callback_;
HighFrequencyLoopRequester high_freq_;
uint32_t update_delay_ms_{200};
uint32_t max_update_delay_ms_{1000};
uint32_t backoff_ms_{0};
uint32_t hold_ms_{0};
uint32_t hold_start_ms_{0};
uint32_t setup_ms_{0};
uint32_t busy_since_ms_{0};
uint32_t session_start_ms_{0};
uint32_t update_start_ms_{0};
// The first and the latest frame drawn since the display last refreshed.
uint32_t first_change_ms_{0};
uint32_t last_change_ms_{0};
uint32_t rx_total_{0};
uint32_t update_start_bytes_{0};
uint32_t last_request_ms_{0};
uint32_t last_rx_ms_{0};
uint32_t pass_start_ms_{0};
uint32_t skip_left_{0};
uint16_t rx_pos_{0};
uint16_t rx_len_{0};
uint16_t fb_w_{0};
uint16_t fb_h_{0};
uint16_t vis_w_{0};
uint16_t vis_h_{0};
uint16_t rects_left_{0};
uint16_t rect_x_{0};
uint16_t rect_y_{0};
uint16_t rect_w_{0};
uint16_t rect_h_{0};
// Raw: column and row inside the rect.
uint16_t pos_x_{0};
uint16_t pos_y_{0};
uint8_t bpp_{2};
uint8_t minor_{8};
uint8_t sec_count_{0};
uint8_t text_len_{0}; // clamped to MAX_TEXT_LEN
display::ColorBitness bitness_{display::COLOR_BITNESS_565};
VncState state_{VncState::VNC_STATE_IDLE};
VncStep step_{VncStep::VNC_STEP_VERSION};
VncStep after_skip_{VncStep::VNC_STEP_MESSAGE};
VncFailure reason_failure_{VncFailure::VNC_FAILURE_TRANSIENT};
bool rx_pending_{false};
bool in_update_{false};
bool request_outstanding_{false};
bool refresh_pending_{false};
bool frame_drawn_{false};
bool display_parks_{false};
bool display_busy_{false};
bool busy_warned_{false};
bool network_was_up_{false};
bool big_endian_; // the pixel order the display takes
uint8_t rx_[RX_BUFFER_SIZE];
};
} // namespace esphome::vnc
+43 -9
View File
@@ -39,6 +39,7 @@ from esphome.const import (
CONF_ENABLE_BTM,
CONF_ENABLE_ON_BOOT,
CONF_ENABLE_RRM,
CONF_ETHERNET,
CONF_FAST_CONNECT,
CONF_GATEWAY,
CONF_HIDDEN,
@@ -431,8 +432,26 @@ def _apply_min_auth_mode_default(config):
return config
def wifi_has_sta(config: ConfigType) -> bool:
"""Whether the validated config has at least one station network."""
return bool(config.get(CONF_NETWORKS))
# Components that save station credentials at run time
_PROVISIONERS = ("captive_portal", "improv_ble", "improv_serial")
def wifi_stays_ap_only(config: ConfigType, full_config: ConfigType) -> bool:
"""Whether no station can appear at run time: no networks, no DPP, no provisioner."""
return not (
wifi_has_sta(config)
or CONF_DPP in config
or any(c in full_config for c in _PROVISIONERS)
)
def final_validate(config):
has_sta = bool(config.get(CONF_NETWORKS, True))
has_sta = wifi_has_sta(config)
has_ap = CONF_AP in config
has_dpp = CONF_DPP in config
full_config = fv.full_config.get()
@@ -447,9 +466,11 @@ def final_validate(config):
ap_is_wisp = has_ap and config[CONF_AP][CONF_AP_MODE] == WIFI_AP_MODE_WISP
if ap_is_wisp:
if not has_sta:
# The clients are routed through the station, or through ethernet
if not has_sta and CONF_ETHERNET not in full_config:
raise cv.Invalid(
"WISP mode requires at least one STA network (wifi.ssid or wifi.networks)"
"WISP mode needs a station network (wifi.ssid or wifi.networks), "
"or ethernet as the upstream interface"
)
if has_captive_portal:
raise cv.Invalid(
@@ -458,14 +479,27 @@ def final_validate(config):
# Every client packet passes through the WiFi driver, none through the device's own sockets
require_high_performance_wifi()
if CONF_DNS_SERVERS not in config[CONF_AP]:
# The access point passes the station's resolvers on, which a static address has to name
for network in [config, *config.get(CONF_NETWORKS, [])]:
# The access point passes the upstream's resolvers on, which a static address has to name
upstreams = [
([], config),
*(
([CONF_NETWORKS, i], network)
for i, network in enumerate(config.get(CONF_NETWORKS, []))
),
(None, full_config.get(CONF_ETHERNET, {})),
]
for path, network in upstreams:
static = network.get(CONF_MANUAL_IP)
if static and static[CONF_DNS1].is_unspecified:
if not static or not static[CONF_DNS1].is_unspecified:
continue
if path is None:
raise cv.Invalid(
"manual_ip needs dns1 in WISP mode unless ap.dns_servers is set",
path=[CONF_MANUAL_IP, CONF_DNS1],
"ethernet manual_ip needs dns1 in WISP mode unless ap.dns_servers is set"
)
raise cv.Invalid(
"manual_ip needs dns1 in WISP mode unless ap.dns_servers is set",
path=[*path, CONF_MANUAL_IP, CONF_DNS1],
)
if has_ap and not has_captive_portal and not has_web_server and not ap_is_wisp:
_LOGGER.warning(
@@ -565,7 +599,7 @@ def _report_provisioning_credentials(config):
"""
from esphome.components import provisioning
if config.get(CONF_NETWORKS):
if wifi_has_sta(config):
provisioning.report_hardcoded_credentials("wifi")
elif CONF_AP in config:
# An access point with no station credentials: the AP shuts down when the
+20 -16
View File
@@ -783,12 +783,9 @@ void WiFiComponent::start() {
// Without fast_connect: go straight to scanning (or hidden mode if all networks are hidden)
this->start_initial_connection_();
#endif
#ifdef USE_WIFI_AP
} else if (this->has_ap()) {
#if defined(USE_WIFI_AP) && !defined(USE_WIFI_AP_WISP)
} else if (this->has_ap()) { // a bridge waits in loop() for the upstream's resolver
this->setup_ap_config_();
if (!std::isnan(this->output_power_) && !this->wifi_apply_output_power_(this->output_power_)) {
ESP_LOGV(TAG, "Setting Output Power Option failed");
}
#ifdef USE_CAPTIVE_PORTAL
// Where the radio scans alongside the AP, the portal can list networks.
if (captive_portal::global_captive_portal != nullptr && !WIFI_AP_EXCLUSIVE) {
@@ -797,7 +794,7 @@ void WiFiComponent::start() {
}
#endif
this->start_ap_portal_();
#endif // USE_WIFI_AP
#endif // USE_WIFI_AP && !USE_WIFI_AP_WISP
}
#ifdef USE_WIFI_DPP
// Without networks there is nothing to try first, so listen for a configurator right away
@@ -937,15 +934,7 @@ void WiFiComponent::loop() {
return;
}
#ifdef USE_WIFI_AP_WISP
// The bridge comes up once it has a resolver to offer; a start that failed is tried again after ap_timeout
if (!this->ap_setup_ && this->ap_dns_known_() &&
(this->ap_changed_ == 0 || now - this->ap_changed_ > this->ap_timeout_)) {
ESP_LOGI(TAG, "Starting AP");
this->ap_changed_ = now;
this->setup_ap_config_();
}
#elif defined(USE_WIFI_AP)
#if defined(USE_WIFI_AP) && !defined(USE_WIFI_AP_WISP)
// Once the provisioning window has closed, don't bring up the fallback AP (or
// the captive portal on it) - the device must stay unprovisionable until it is
// power-cycled.
@@ -975,7 +964,7 @@ void WiFiComponent::loop() {
this->start_ap_portal_();
}
}
#endif // USE_WIFI_AP
#endif // USE_WIFI_AP && !USE_WIFI_AP_WISP
#ifdef USE_IMPROV_BLE
if (improv_ble::global_improv_component != nullptr && !improv_ble::global_improv_component->is_active() &&
@@ -1013,6 +1002,16 @@ void WiFiComponent::loop() {
}
}
#ifdef USE_WIFI_AP_WISP
// The bridge comes up once it has a resolver to offer; a start that failed is tried again after ap_timeout
if (!this->ap_setup_ && this->state_ != WIFI_COMPONENT_STATE_DISABLED && this->ap_dns_known_() &&
(this->ap_changed_ == 0 || now - this->ap_changed_ > this->ap_timeout_)) {
ESP_LOGI(TAG, "Starting AP");
this->ap_changed_ = now;
this->setup_ap_config_();
}
#endif
#ifdef USE_WIFI_DPP
// Without networks no cooldown starts the listen, so a failed or abandoned enrollment is listened for again
// here, at most once per timeout
@@ -1098,6 +1097,11 @@ void WiFiComponent::setup_ap_config_() {
// Reset before the AP exists: afterwards only the platform's event path writes the counter
this->ap_clients_ = 0;
this->ap_setup_ = this->wifi_start_ap_(this->ap_);
// The station path applied it in start(); without a station the radio only exists from here
if (this->ap_setup_ && !this->has_sta() && !std::isnan(this->output_power_) &&
!this->wifi_apply_output_power_(this->output_power_)) {
ESP_LOGV(TAG, "Setting Output Power Option failed");
}
#if defined(USE_WIFI_AP_EXCLUSIVE) || defined(USE_WIFI_AP_WISP)
// A failed start must not leave the radio in AP mode: on an exclusive radio it would refuse
// the STA attempts until the next try, and a bridge would take clients it cannot serve.
@@ -127,6 +127,10 @@ void event_handler(void *arg, esp_event_base_t event_base, int32_t event_id, voi
#if USE_NETWORK_IPV6
} else if (event_base == IP_EVENT && event_id == IP_EVENT_GOT_IP6) {
memcpy(&event.data.ip_got_ip6, event_data, sizeof(ip_event_got_ip6_t));
#endif
#if defined(USE_WIFI_AP_WISP) && !defined(USE_WIFI_AP_WISP_STATIC_DNS)
} else if (event_base == IP_EVENT && event_id == IP_EVENT_ETH_GOT_IP) { // NOLINT(bugprone-branch-clone)
// no data: the bridge rereads the resolvers an ethernet upstream just received
#endif
} else if (event_base == IP_EVENT && event_id == IP_EVENT_STA_LOST_IP) { // NOLINT(bugprone-branch-clone)
// no data
@@ -1123,6 +1127,10 @@ void WiFiComponent::wifi_process_event_(IDFWiFiEvent *data) {
this->notify_disconnect_state_listeners_();
#endif
#if defined(USE_WIFI_AP_WISP) && !defined(USE_WIFI_AP_WISP_STATIC_DNS)
} else if (data->event_base == IP_EVENT && data->event_id == IP_EVENT_ETH_GOT_IP) {
this->apply_ap_dns_();
#endif
} else if (data->event_base == IP_EVENT && data->event_id == IP_EVENT_STA_GOT_IP) {
const auto &it = data->data.ip_got_ip;
ESP_LOGV(TAG, "static_ip=" IPSTR " gateway=" IPSTR, IP2STR(&it.ip_info.ip), IP2STR(&it.ip_info.gw));
@@ -1397,10 +1405,6 @@ bool WiFiComponent::wifi_scan_start_(bool passive) {
bool WiFiComponent::wifi_ap_ip_config_(const optional<ManualIP> &manual_ip) {
esp_err_t err;
// enable AP
if (!this->wifi_mode_({}, true))
return false;
// Check if the AP interface is initialized before using it
if (s_ap_netif == nullptr) {
ESP_LOGW(TAG, "AP interface not initialized");
@@ -1478,8 +1482,8 @@ bool WiFiComponent::wifi_ap_ip_config_(const optional<ManualIP> &manual_ip) {
#endif
err = esp_netif_dhcps_start(s_ap_netif);
if (err != ESP_OK) {
// Before the access point is up, its start event brings the server up with these settings
if (err != ESP_OK && err != ESP_ERR_ESP_NETIF_IF_NOT_READY) {
ESP_LOGE(TAG, "esp_netif_dhcps_start failed: %d", err);
return false;
}
@@ -1488,6 +1492,17 @@ bool WiFiComponent::wifi_ap_ip_config_(const optional<ManualIP> &manual_ip) {
}
bool WiFiComponent::wifi_start_ap_(const WiFiAP &ap) {
// The address and DHCP settings go in before the access point starts: its start event starts the DHCP
// server on the event task, and settings written after that race it.
#ifdef USE_WIFI_MANUAL_IP
if (!this->wifi_ap_ip_config_(ap.get_manual_ip())) {
#else
if (!this->wifi_ap_ip_config_({})) {
#endif
ESP_LOGE(TAG, "wifi_ap_ip_config_ failed:");
return false;
}
// enable AP
if (!this->wifi_mode_({}, true))
return false;
@@ -1524,19 +1539,6 @@ bool WiFiComponent::wifi_start_ap_(const WiFiAP &ap) {
ESP_LOGE(TAG, "esp_wifi_set_config failed: %d", err);
return false;
}
#ifdef USE_WIFI_MANUAL_IP
if (!this->wifi_ap_ip_config_(ap.get_manual_ip())) {
ESP_LOGE(TAG, "wifi_ap_ip_config_ failed:");
return false;
}
#else
if (!this->wifi_ap_ip_config_({})) {
ESP_LOGE(TAG, "wifi_ap_ip_config_ failed:");
return false;
}
#endif
return true;
}
@@ -1681,7 +1683,7 @@ bool WiFiComponent::write_ap_dns_() {
#ifndef USE_WIFI_AP_WISP_STATIC_DNS
void WiFiComponent::apply_ap_dns_() {
if (!this->ap_setup_)
return; // loop() brings the bridge up once the station has a resolver, and its setup writes them
return; // loop() brings the bridge up once the upstream has a resolver, and its setup writes them
// The DHCP server holds what it offers, so there is nothing to cache; a failed update is noticed again with the
// next lease. A resolver the station lost stays offered: set_dns_info rejects an empty address.
bool changed = false;
+18 -11
View File
@@ -658,6 +658,8 @@ class EsphomeCore:
self.main_statements: list[Statement] = []
# A list of statements to insert in the global block (includes and global variables)
self.global_statements: list[Statement] = []
# Generated code emitted after the user includes, so it can use what they declare
self.global_statements_after_includes: list[Statement] = []
# A map of platformio libraries to add to the project (shortname: (name, version, repository))
self.platformio_libraries: dict[str, Library] = {}
# A set of build flags to set in the platformio project
@@ -728,6 +730,7 @@ class EsphomeCore:
self.variables = {}
self.main_statements = []
self.global_statements = []
self.global_statements_after_includes = []
self.platformio_libraries = {}
self.build_flags = set()
self.cmake_args = {}
@@ -1045,8 +1048,8 @@ class EsphomeCore:
except RuntimeError as e:
raise EsphomeError(str(e)) from e
def add(self, expression, prepend=False) -> "Statement":
"""Add an expression or statement to the main setup() block."""
@staticmethod
def _as_statement(expression) -> "Statement":
from esphome.cpp_generator import Expression, Statement, statement
if isinstance(expression, Expression):
@@ -1055,7 +1058,11 @@ class EsphomeCore:
raise ValueError(
f"Add '{expression}' must be expression or statement, not {type(expression)}"
)
return expression
def add(self, expression, prepend=False) -> "Statement":
"""Add an expression or statement to the main setup() block."""
expression = self._as_statement(expression)
if prepend:
self.main_statements.insert(0, expression)
else:
@@ -1064,14 +1071,7 @@ class EsphomeCore:
return expression
def add_global(self, expression, prepend=False) -> "Statement":
from esphome.cpp_generator import Expression, Statement, statement
if isinstance(expression, Expression):
expression = statement(expression)
if not isinstance(expression, Statement):
raise ValueError(
f"Add '{expression}' must be expression or statement, not {type(expression)}"
)
expression = self._as_statement(expression)
if prepend:
self.global_statements.insert(0, expression)
else:
@@ -1079,6 +1079,13 @@ class EsphomeCore:
_LOGGER.debug("Adding global: %s", expression)
return expression
def add_global_after_includes(self, expression) -> "Statement":
"""Add a global after the user includes, for generated code that may use them."""
expression = self._as_statement(expression)
self.global_statements_after_includes.append(expression)
_LOGGER.debug("Adding global after includes: %s", expression)
return expression
def add_library(self, library: Library):
if not isinstance(library, Library):
raise TypeError(
@@ -1258,7 +1265,7 @@ class EsphomeCore:
from esphome.cpp_generator import statement
global_code = []
for exp in self.global_statements:
for exp in (*self.global_statements, *self.global_statements_after_includes):
text = str(statement(exp))
text = text.rstrip()
global_code.append(text)
+2 -1
View File
@@ -590,7 +590,8 @@ async def add_arduino_global_workaround():
@coroutine_with_priority(CoroPriority.FINAL)
async def add_includes(includes: list[str], is_c_header: bool = False) -> None:
# Add includes at the very end, so that the included files can access global variables
# After the id declarations, so the headers can use id(x); the generated function bodies
# and flash tables come later still, so their lambdas can use the headers.
for include in includes:
path = CORE.relative_config_path(include)
if path.is_dir():
+20 -10
View File
@@ -536,8 +536,10 @@ def shared_progmem_array(
used |= {str(i) for i in CORE.variables}
used |= set(RESERVED_IDS) | CORE.loaded_integrations
id_ = ID(ensure_unique_string(name, used), is_declaration=True, type=type_)
# Global, so any scope can use it; anything a lambda references is already declared.
CORE.add_global(ProgmemAssignmentExpression(type_, id_, rhs, constexpr))
# After the user includes, so a lambda in the table can use what their headers declare.
CORE.add_global_after_includes(
ProgmemAssignmentExpression(type_, id_, rhs, constexpr)
)
array = MockObj(id_, ".")
CORE.register_variable(id_, array)
if share:
@@ -789,15 +791,16 @@ def static_function(
"""Emit ``static <return_type> <name>(parameters) { body }`` at global scope and return an
expression naming it, for use as a template argument or a function pointer.
Every id the body names must already be declared, which holds when the statements were
rendered through ``get_variable`` or ``process_lambda``.
The declaration goes with the other globals, so a template argument can name the function
at once; the body goes after the user includes, where every id and everything those
headers declare is in scope.
"""
params = ParameterListExpression(*parameters)
add_global(
RawStatement(
f"static {safe_exp(return_type)} {name}({params}) {{\n"
f"{indent(chr(10).join(body))}\n}}"
)
signature = (
f"static {safe_exp(return_type)} {name}({ParameterListExpression(*parameters)})"
)
add_global(RawStatement(f"{signature};"))
add_global_after_includes(
RawStatement(f"{signature} {{\n{indent(chr(10).join(body))}\n}}")
)
return RawExpression(name)
@@ -816,6 +819,13 @@ def add_global(expression: SafeExpType | Statement, prepend: bool = False):
CORE.add_global(expression, prepend)
def add_global_after_includes(expression: SafeExpType | Statement):
"""Add a global after the user's ``includes:`` headers, for generated code whose
lambdas may use what those headers declare.
"""
CORE.add_global_after_includes(expression)
def add_library(name: str, version: str | None, repository: str | None = None):
"""Add a library to the codegen library storage.
+2 -12
View File
@@ -266,24 +266,14 @@ def _convert_pio_libs(
in the real esp32 build; converting them too would give IDF the same requirement
twice. Arduino below IDF 6.0 has them rule-gated off, so there they convert.
"""
from esphome.components.esp32 import (
ARDUINO_DISABLED_LIBRARIES,
arduino_bundles_libsodium,
)
from esphome.components.esp32 import arduino_bundles_libsodium
from esphome.espidf.component import generate_idf_components
libraries = _parse_lib_deps(platformio_ini, framework)
# Same rule as noise._use_managed_components and the manifest
managed = set() if arduino_bundles_libsodium() else _esphome_manifest_deps()
deps: dict[str, dict[str, str]] = {}
# As in the real esp32 build, a dependency on an Arduino-bundled library is
# left to arduino-esp32 instead of being fetched from the registry
provides = (
ARDUINO_DISABLED_LIBRARIES.__contains__ if framework == "arduino" else None
)
for component in generate_idf_components(
libraries, managed=managed, provides=provides
):
for component in generate_idf_components(libraries, managed=managed):
deps[component.get_sanitized_name()] = {"override_path": str(component.path)}
return deps
+1 -7
View File
@@ -7,7 +7,6 @@ for each resolved library, running any PlatformIO ``extraScript``, and the
ESP-IDF platform/framework compatibility defaults.
"""
from collections.abc import Callable
import logging
import os
from pathlib import Path
@@ -289,23 +288,18 @@ def _emit_idf_component(component: IDFComponent) -> None:
def generate_idf_components(
libraries: list[Library],
managed: set[str] | None = None,
provides: Callable[[str], bool] | None = None,
libraries: list[Library], managed: set[str] | None = None
) -> list[IDFComponent]:
"""Resolve and convert a batch of PlatformIO libraries to IDF components.
``managed`` names registry components already in the project manifest; they
are not converted, since IDF rejects the same requirement from two components.
Converted components reach them through ``${ESPHOME_PROJECT_MANAGED_COMPONENTS}``.
``provides`` names the libraries the framework bundles, so a manifest dependency on
one is never fetched from the registry.
"""
backend = LibraryBackend(
platform=ESP32_PLATFORM,
framework=_idf_framework(),
emit=_emit_idf_component,
cache_key="idf",
provides=provides,
)
return convert_libraries(libraries, backend, provided=managed)
+101 -236
View File
@@ -609,34 +609,9 @@ def parse_library_properties(library_properties_path: PathType):
if not value.strip():
continue
data[key.strip()] = value.strip()
if depends := data.pop("depends", None):
data["dependencies"] = parse_properties_depends(depends)
return data
def parse_properties_depends(depends: str) -> list[dict[str, str]]:
"""The Arduino ``depends=Name, Other (>=1.0)`` line as name-only dependency entries.
An Arduino constraint is not PlatformIO syntax, so it is dropped; the name is
satisfied by a listed library or the registry's current version, as the walk
does for every version-less dependency.
"""
entries: list[dict[str, str]] = []
for item in depends.split(","):
name, _, constraint = item.strip().partition("(")
if not (name := name.strip()):
continue
if constraint:
# A plain lower bound is met by the registry's current version; an
# exact or capped one may not be, so say so
log = (
_LOGGER.debug if constraint.lstrip().startswith(">=") else _LOGGER.info
)
log("Ignoring version constraint on depends=%s", item.strip())
entries.append({"name": name})
return entries
def _make_registry_client() -> Any:
"""Create a minimal PlatformIO registry client with no system filtering.
@@ -755,6 +730,20 @@ def join_flag_args(tokens: Iterable[str], owner: str) -> list[str]:
return out
def warn_properties_depends(name: str, data: object) -> None:
"""Warn for ``depends=``-only manifests; the walk reads only the JSON
``dependencies`` key, so they would otherwise drop silently."""
if isinstance(data, dict) and not data.get("dependencies") and data.get("depends"):
# INFO: common and unactionable for transitive libraries; a WARNING
# on every build would train users to ignore the stream
_LOGGER.info(
"Library %s declares dependencies via library.properties "
"depends=, which are not resolved automatically; add them with "
"add_library() if needed",
name,
)
def dependency_is_usable(
dep: dict, platform: str | None, framework: str, requester: str
) -> bool:
@@ -773,20 +762,6 @@ def dependency_is_usable(
return True
def _split_owner(entry: dict) -> dict:
"""``owner/Name`` written as one name becomes owner and name, as in the dict form,
so the name matches the package the registry resolves it to."""
name, owner = entry.get("name"), entry.get("owner")
if isinstance(name, str) and owner is None and "://" not in name:
head, sep, tail = name.partition("/")
# A registry owner is a plain identifier; anything else is left to the
# backend's own checks
owner_ok = head.replace("-", "").replace("_", "").isalnum()
if sep and owner_ok and tail and "/" not in tail:
return {**entry, "owner": head, "name": tail}
return entry
def _valid_dependency_entry(entry: dict, manifest_name: str) -> bool:
"""Whether a normalized entry carries a usable name (non-empty string),
version (string, if present), and owner (string, if present); invalid
@@ -815,12 +790,16 @@ def normalize_dependencies(
so callers see a uniform list. ``manifest_name`` names the manifest in the
warning for entries that cannot be normalized.
Bare-name spellings carry no version; the walk settles them once the graph is known.
Bare-name spellings carry no version; the dependency walk later drops
version-less entries at DEBUG, since they are usually names of bundled
framework libraries (Wire, SPI) that need no registry install.
"""
if dependencies is None:
return []
if isinstance(dependencies, str):
return [_split_owner(entry) for entry in parse_properties_depends(dependencies)]
# A plain string is one or more comma-separated names; iterating it
# as a list would shred it into one-character "libraries"
return [{"name": n.strip()} for n in dependencies.split(",") if n.strip()]
if isinstance(dependencies, dict):
normalized = []
for raw_name, spec in dependencies.items():
@@ -847,10 +826,10 @@ def normalize_dependencies(
for entry in dependencies:
if isinstance(entry, dict):
if _valid_dependency_entry(entry, manifest_name):
normalized.append(_split_owner(entry))
normalized.append(entry)
elif isinstance(entry, str) and entry:
# PIO also accepts a bare list of names ("dependencies": ["Wire"])
normalized.append(_split_owner({"name": entry}))
normalized.append({"name": entry})
else:
_LOGGER.warning(
"Ignoring unrecognized dependency entry %r of %s",
@@ -992,55 +971,54 @@ def is_lib_ignored(name: str | None, lib_ignore: set[str]) -> bool:
)
def _satisfying_key(
name: str,
owner: str | None,
requester: str,
nodes: dict[str, _LibNode],
def _reconcile_versionless_skips(
skipped_versionless: list[tuple[Any, Any, str]],
components: dict[str, ConvertedLibrary],
) -> tuple[str, bool] | None:
"""The key of a resolved library a version-less dependency names, and whether
only its manifest name matched: the registry package name (with the owner,
when the dependency gives one) is checked first; never the requester itself."""
wanted = name.lower()
def owner_of(key: str, component: ConvertedLibrary) -> str:
# The owner as requested, else the one the registry (or a git path) gave
head, sep, _ = component.name.partition("/")
return nodes[key].owner or (head if sep else "")
candidates = [
(key, component)
for key, component in components.items()
if key != requester
and (not owner or owner_of(key, component).lower() == owner.lower())
]
for key, _ in candidates:
if (nodes[key].pkgname or key).lower() == wanted:
return key, False
if owner:
return None
for key, component in candidates:
manifest_name = component.data.get("name")
if isinstance(manifest_name, str) and manifest_name.lower() == wanted:
return key, True
return None
def _own_names(
key: str, nodes: dict[str, _LibNode], components: dict[str, ConvertedLibrary]
) -> set[str]:
"""The lower-case key, requested and resolved package names and the manifest
name a resolved library goes by."""
names = {
key.lower(),
(nodes[key].pkgname or key).lower(),
components[key].name.lower(),
}
manifest_name = components[key].data.get("name")
if isinstance(manifest_name, str):
names.add(manifest_name.lower())
return names
backend: LibraryBackend,
) -> None:
"""Warn for version-less deps nothing satisfied, and record the
backend-provided ones in ``backend.provided_requests`` for its
post-emit reconciliation; a silent drop surfaces as link errors far
from the cause."""
resolved_manifest_names = {c.data.get("name") for c in components.values()}
# A treeless backend can never supply a bundled name; noise for it
log = _LOGGER.warning if backend.provides is not None else _LOGGER.debug
warned: set[str] = set()
for dep_name, dep_owner, requester in skipped_versionless:
if not isinstance(dep_name, str) or not dep_name or dep_name in warned:
continue
if dep_name in components:
# A version-less dep's request key is the name itself
continue
if (
not dep_owner
and backend.provides is not None
and backend.provides(dep_name)
):
# provides() only satisfies owner-less names (same guard as
# the walk's skip); record for the post-emit reconciliation.
# Checked before the manifest-name evidence so the overlap
# case warns once, in the backend's own suppression loop
backend.provided_requests.add(dep_name)
continue
if dep_name in resolved_manifest_names:
# Name-only evidence: a coincidental collision must stay
# visible where the user could pin it
warned.add(dep_name)
log(
"Version-less dependency %s of %s assumed satisfied by a "
"resolved library's manifest name only",
dep_name,
requester,
)
continue
warned.add(dep_name)
log(
"Dependency %s of %s has no version to resolve and nothing "
"provides it; skipping",
dep_name,
requester,
)
def _fetch_source(
@@ -1169,12 +1147,7 @@ def convert_libraries(
managed components from ``add_idf_component``); unlike ``backend.provides`` they
carry an owner. They are excluded like ``lib_ignore`` so nothing is both
converted and managed, which ESP-IDF refuses to build.
A dependency named without a version is settled once the graph is known: wired
to a library already in it, left to ``backend.provides``, or fetched by name.
"""
from platformio.package.exception import UnknownPackageError
nodes: dict[str, _LibNode] = {}
# Folded into one set so every is_lib_ignored() call site honors both.
@@ -1192,16 +1165,10 @@ def convert_libraries(
else ""
)
resolved_requirements: dict[str, frozenset[str]] = {}
def add_spec(name: str | None, version: str | None, repository: str | None) -> str:
key, kind, locator = _node_key(name, version, repository)
node = nodes.get(key) or _LibNode(key=key, is_git=kind == "git")
nodes[key] = node
if kind != "registry" and not (node.is_git or node.is_local):
# A registry node (perhaps one the registry did not know) becomes a
# git or local source: resolve it again from that source
resolved_requirements.pop(key, None)
# The same key requested from two different kinds of source (or two
# different local paths) is a config mistake: one silently wins. Warn so
# it isn't a surprise. (git-vs-registry is reported separately below.)
@@ -1253,69 +1220,10 @@ def convert_libraries(
# requirement set has grown since the last time, so every requirement in the
# graph is accounted for before conversion.
components: dict[str, ConvertedLibrary] = {}
resolved_requirements: dict[str, frozenset[str]] = {}
top_level_keys = set(top_level)
def fetched_by_name(key: str) -> bool:
"""A registry node only version-less manifest dependencies asked for: a
miss skips it, and an explicit library of the same name replaces it."""
node = nodes[key]
return (
key not in top_level_keys
and not (node.is_git or node.is_local)
and not node.requirements
)
def backend_provides(name: str, owner: str | None) -> bool:
"""Whether the framework bundles an owner-less name; asked once per name,
since the backend may warn, and recorded in ``provided_requests``."""
if owner or backend.provides is None:
return False
if name in backend.provided_requests or backend.provides(name):
backend.provided_requests.add(name)
return True
return False
# Version-less dependencies per requester key, kept for the whole walk and
# wired once the graph is final; a re-walked requester replaces its entries
pending_versionless: dict[str, list[dict]] = {}
# Names fetched by name so far, whatever their case: each is asked for once
fetched: dict[tuple[str, str], str] = {}
def settle_versionless() -> list[str]:
"""Fetch by name the deferred dependencies nothing in the graph or the
framework satisfies yet; return the new keys to walk. Only a dependency
on the requester itself or with a bad name needs no wiring later."""
new_keys: list[str] = []
for requester, dependencies in pending_versionless.items():
kept: list[dict] = []
for dependency in dependencies:
name, owner = dependency["name"], dependency.get("owner")
if _owner_pkgname_to_name(owner, name).lower() in _own_names(
requester, nodes, components
):
_LOGGER.debug("Dependency %s of %s names itself", name, requester)
continue
# A library in the graph beats a bundled copy, as on the Arduino backend
if _satisfying_key(
name, owner, requester, nodes, components
) is None and not backend_provides(name, owner):
spelling = (name.lower(), (owner or "").lower())
if spelling not in fetched:
try:
dep_key = add_spec(
_owner_pkgname_to_name(owner, name), None, None
)
except RuntimeError as e:
_LOGGER.warning(
"Skipping dependency %s of %s: %s", name, requester, e
)
continue
fetched[spelling] = dep_key
new_keys.append(dep_key)
kept.append(dependency)
dependencies[:] = kept
return new_keys
# (name, owner, requester) reconciled against the final resolution set
skipped_versionless: list[tuple[Any, Any, str]] = []
worklist = deque(dict.fromkeys(top_level))
while worklist:
# Drain the frontier sequentially (spec resolution mutates shared
@@ -1337,17 +1245,9 @@ def convert_libraries(
elif node.is_local:
component = ConvertedLibrary(key, "*", LocalSource(node.local_path))
else:
try:
owner, name, version, url, size = _resolve_registry_version(
node.owner, node.pkgname, node.requirements
)
except UnknownPackageError as e:
# A requirement conflict is a RuntimeError and always raises
if not fetched_by_name(key):
raise
# Reported per requester once the graph is final
_LOGGER.debug("Library %s is not in the registry: %s", key, e)
continue
owner, name, version, url, size = _resolve_registry_version(
node.owner, node.pkgname, node.requirements
)
component = ConvertedLibrary(
_owner_pkgname_to_name(owner, name), version, URLSource(url, size)
)
@@ -1355,9 +1255,9 @@ def convert_libraries(
_prefetch_wave(wave, salt, backend.cache_key)
for key, component in wave:
node = nodes[key]
if frozenset(node.requirements) != resolved_requirements.get(key):
# Requirements grew (or the source changed) mid-wave: skip parsing
# a manifest the next wave will re-resolve and replace
if frozenset(node.requirements) != resolved_requirements[key]:
# Requirements grew mid-wave: skip parsing a manifest the
# next wave will re-resolve and replace
worklist.append(key)
continue
component.download(salt=salt, namespace=backend.cache_key)
@@ -1413,6 +1313,8 @@ def convert_libraries(
raise EsphomeError(f"Library {key} has a malformed manifest")
_LOGGER.warning("Skipping dependency %s: malformed manifest", key)
continue
warn_properties_depends(component.name, component.data)
try:
check_library_data(component.data, backend.platform, backend.framework)
except InvalidLibrary as e:
@@ -1435,19 +1337,27 @@ def convert_libraries(
# Requirements changed (we got past the short-circuit above), so
# (re)walk this component's dependencies.
node.edges = set()
pending = pending_versionless[key] = []
for dependency in normalize_dependencies(
component.data.get("dependencies"), component.name
):
if not dependency.get("version"):
# An empty version is as good as none; it settles the same way
if "version" not in dependency:
# Cannot resolve from the registry; the post-emit
# reconciliation owns the drop warning
dep_name = dependency.get("name")
_LOGGER.debug(
"Skip version-less dependency %r of %s",
dep_name,
component.name,
)
if not is_lib_ignored(
dependency.get("name"), lib_ignore
dep_name, lib_ignore
) and dependency_is_usable(
dependency, backend.platform, backend.framework, component.name
):
# Filtered or ignored deps are deliberately absent
pending.append(dependency)
skipped_versionless.append(
(dep_name, dependency.get("owner"), component.name)
)
continue
if not dependency_is_usable(
dependency, backend.platform, backend.framework, component.name
@@ -1465,7 +1375,11 @@ def convert_libraries(
dep_url = _url_or_none(dep_version)
if dep_url is not None:
dep_version = None
elif backend_provides(dep_name, dependency.get("owner")):
elif (
backend.provides is not None
and not dependency.get("owner")
and backend.provides(dep_name)
):
# The backend adds it from its own tree; resolving here
# would fetch a same-named registry package
if dep_version and dep_version != "*":
@@ -1478,12 +1392,11 @@ def convert_libraries(
)
else:
_LOGGER.debug("Skip backend-provided dependency %s", dep_name)
backend.provided_requests.add(dep_name)
continue
dep_key = add_spec(dep_name, dep_version, dep_url)
node.edges.add(dep_key)
worklist.append(dep_key)
if not worklist:
worklist.extend(settle_versionless())
# A git or local source wins over the same component requested from the
# registry. That's intentional, but warn so the dropped registry spec isn't
@@ -1507,56 +1420,6 @@ def convert_libraries(
source,
)
# Every version-less request resolves to the explicit library that goes by
# its name if the final graph has one, else to the node fetched by name for
# that package, else to the framework's bundled copy, else to nothing
explicit = {k: c for k, c in components.items() if not fetched_by_name(k)}
by_name = {k: c for k, c in components.items() if fetched_by_name(k)}
unresolved: dict[tuple[str, str], None] = {}
for requester, dependencies in pending_versionless.items():
for dependency in dependencies:
name, owner = dependency["name"], dependency.get("owner")
found = _satisfying_key(
name, owner, requester, nodes, explicit
) or _satisfying_key(name, owner, requester, nodes, by_name)
if found is None:
if not backend_provides(name, owner):
unresolved[(requester, name)] = None
continue
key, by_manifest_name = found
if by_manifest_name:
# Visible, so a chance name collision can be pinned to the right package
_LOGGER.info(
"Dependency %s of %s satisfied by %s through its manifest name",
name,
requester,
key,
)
else:
_LOGGER.debug(
"Dependency %s of %s satisfied by %s", name, requester, key
)
nodes[requester].edges.add(key)
# What no listed library needs any more is not built: a node fetched by name
# that an explicit library replaced, and whatever only it pulled in
reachable: set[str] = set()
stack = [key for key in top_level if key in components]
while stack:
if (key := stack.pop()) in reachable:
continue
reachable.add(key)
stack.extend(edge for edge in nodes[key].edges if edge in components)
for key in [key for key in components if key not in reachable]:
_LOGGER.debug("Library %s is not needed; dropping it", key)
del components[key]
for requester, name in unresolved:
if requester in components:
_LOGGER.warning(
"Dependency %s of %s was not resolved; skipping", name, requester
)
# Two graph nodes that resolve to the same component name (e.g. a package
# referenced both bare and as ``owner/name``) are not deduplicated and can
# produce conflicting component definitions. Warn so it's not silent.
@@ -1584,4 +1447,6 @@ def convert_libraries(
for component in components.values():
backend.emit(component)
_reconcile_versionless_skips(skipped_versionless, components, backend)
return [components[key] for key in top_level if key in components]
+2 -2
View File
@@ -48,7 +48,7 @@ lib_deps =
improv/Improv@1.2.7 ; improv_serial / improv_ble
kikuchan98/pngle@1.1.0 ; runtime_image
; Using the repository directly, otherwise ESP-IDF can't use the library
https://github.com/bitbank2/JPEGDEC.git#1.8.4 ; runtime_image
https://github.com/esphome-libs/JPEGDEC.git#1.8.4-esphome.3 ; runtime_image
; This dependency is used only in unit tests.
; Must coincide with PLATFORMIO_GOOGLE_TEST_LIB in scripts/cpp_unit_test.py
; See scripts/cpp_unit_test.py and tests/components/README.md
@@ -253,7 +253,7 @@ lib_deps =
heman/AsyncMqttClient-esphome@2.0.0 ; mqtt
improv/Improv@1.2.7 ; improv_serial
kikuchan98/pngle@1.1.0 ; runtime_image
https://github.com/bitbank2/JPEGDEC.git#1.8.4 ; runtime_image
https://github.com/esphome-libs/JPEGDEC.git#1.8.4-esphome.3 ; runtime_image
build_flags =
${common:arduino.build_flags}
-DUSE_LIBRETINY
+1 -1
View File
@@ -12,7 +12,7 @@ pyserial==3.5
platformio==6.2.0
esptool==5.6.0
click==8.5.0
aioesphomeapi==46.19.0
aioesphomeapi==46.20.0
aiohappyeyeballs==2.7.1 # Happy Eyeballs for requests downloads; already pulled in by aioesphomeapi
zeroconf==0.151.5
puremagic==2.2.0
+35 -18
View File
@@ -11,9 +11,13 @@ from esphome.components.esp32 import (
VARIANT_ESP32,
VARIANT_ESP32S3,
)
from esphome.components.ethernet import CONF_CLOCK_SPEED, CONFIG_SCHEMA, _final_validate
from esphome.components.ethernet import (
CONF_CLOCK_SPEED,
CONFIG_SCHEMA,
_final_validate_wifi,
)
from esphome.components.network import _validate_priority_list
from esphome.const import CONF_PRIORITY, PlatformFramework
from esphome.const import CONF_AP, CONF_NETWORKS, CONF_PRIORITY, PlatformFramework
from esphome.core import CORE
import esphome.final_validate as fv
@@ -28,24 +32,37 @@ _CH390_CONFIG = {
}
def test_rejects_wifi_and_ethernet_without_priority() -> None:
"""Wi-Fi + ethernet without a network: priority: list must be rejected."""
fv.full_config.set({"wifi": {}, "ethernet": {}})
with pytest.raises(Invalid, match="cannot be used together with component wifi"):
_final_validate({})
_WIFI_STA_CONFIG = {CONF_NETWORKS: [{"ssid": "MySSID", "password": "password1"}]}
_WIFI_AP_CONFIG = {CONF_AP: {"ssid": "espap", "password": "mypassword"}}
def test_rejects_wifi_and_ethernet_with_incomplete_priority() -> None:
"""A priority list missing an interface is rejected and names what's missing."""
fv.full_config.set(
{
"wifi": {},
"ethernet": {},
"network": {CONF_PRIORITY: _validate_priority_list(["ethernet"])},
}
)
with pytest.raises(Invalid, match=r"must.*list both interfaces; missing: wifi"):
_final_validate({})
@pytest.mark.parametrize(
("wifi", "priority", "extra", "match"),
[
(_WIFI_STA_CONFIG, None, {}, "missing: ethernet, wifi"),
(_WIFI_STA_CONFIG, ["ethernet"], {}, "missing: wifi"),
(_WIFI_STA_CONFIG, ["wifi", "ethernet"], {}, None),
(_WIFI_AP_CONFIG, None, {}, None),
# A station can still appear at run time: DPP or a provisioner saves credentials
({"dpp": {}}, None, {}, "missing: ethernet, wifi"),
({"dpp": {}, **_WIFI_AP_CONFIG}, None, {}, "missing: ethernet, wifi"),
(_WIFI_AP_CONFIG, None, {"captive_portal": {}}, "missing: ethernet, wifi"),
(_WIFI_AP_CONFIG, None, {"improv_serial": {}}, "missing: ethernet, wifi"),
],
)
def test_wifi_station_needs_a_full_priority_list_but_an_access_point_does_not(
wifi: dict, priority: list[str] | None, extra: dict, match: str | None
) -> None:
"""A station, configured or provisioned later, competes with ethernet; an access point does not."""
full = {"wifi": wifi, "ethernet": {}, **extra}
if priority:
full["network"] = {CONF_PRIORITY: _validate_priority_list(priority)}
fv.full_config.set(full)
if match is None:
_final_validate_wifi({})
else:
with pytest.raises(Invalid, match=match):
_final_validate_wifi({})
@pytest.mark.parametrize("clock_speed", ["26.67MHz", "72MHz"])
@@ -1,14 +1,22 @@
"""Tests for the speaker_source media player announcement volume validation."""
"""Tests for the speaker_source media player pipeline validation."""
from typing import Any
import pytest
from esphome import config_validation as cv
from esphome.components.const import CONF_ANNOUNCEMENT_VOLUME
from esphome.components import audio
from esphome.components.const import CONF_ANNOUNCEMENT_VOLUME, CONF_BITRATE
from esphome.components.speaker_source.media_player import (
CONF_ANNOUNCEMENT_PIPELINE,
CONF_MEDIA_PIPELINE,
PIPELINE_SCHEMA,
_validate_announcement_volume,
_validate_pipeline,
)
from esphome.const import CONF_FORMAT, CONF_NUM_CHANNELS, CONF_SAMPLE_RATE
PIPELINE = {"speaker": "spk", "sources": ["src"]}
def test_announcement_volume_with_both_pipelines() -> None:
@@ -29,3 +37,49 @@ def test_no_announcement_volume_with_one_pipeline() -> None:
def test_announcement_volume_needs_both_pipelines(pipeline: str) -> None:
with pytest.raises(cv.Invalid, match="requires both"):
_validate_announcement_volume({CONF_ANNOUNCEMENT_VOLUME: 0.6, pipeline: {}})
@pytest.mark.parametrize("bitrate", [8, 48, 144, 320])
def test_pipeline_accepts_mp3_bitrates(bitrate: int) -> None:
config = PIPELINE_SCHEMA({**PIPELINE, CONF_FORMAT: "MP3", CONF_BITRATE: bitrate})
assert config[CONF_BITRATE] == bitrate
def test_pipeline_without_bitrate() -> None:
assert CONF_BITRATE not in PIPELINE_SCHEMA(PIPELINE)
@pytest.mark.parametrize("bitrate", [0, -48, 50, 48000])
def test_pipeline_rejects_bitrates_the_encoder_cannot_use(bitrate: int) -> None:
with pytest.raises(cv.Invalid, match="Unknown value"):
PIPELINE_SCHEMA({**PIPELINE, CONF_FORMAT: "MP3", CONF_BITRATE: bitrate})
def _final_validate(monkeypatch: pytest.MonkeyPatch, **overrides: Any) -> dict:
"""Run the pipeline's final validator with the speaker checks isolated."""
monkeypatch.setattr(
audio, "final_validate_audio_schema", lambda *args, **kwargs: lambda c: c
)
# Both audio properties are set, so nothing is inherited from the speaker.
config = PIPELINE_SCHEMA(
{**PIPELINE, CONF_SAMPLE_RATE: 48000, CONF_NUM_CHANNELS: 1, **overrides}
)
return _validate_pipeline(config)
def test_final_validation_keeps_mp3_bitrate(monkeypatch: pytest.MonkeyPatch) -> None:
config = _final_validate(monkeypatch, **{CONF_FORMAT: "MP3", CONF_BITRATE: 48})
assert config[CONF_BITRATE] == 48
def test_final_validation_without_bitrate(monkeypatch: pytest.MonkeyPatch) -> None:
assert CONF_BITRATE not in _final_validate(monkeypatch, **{CONF_FORMAT: "FLAC"})
@pytest.mark.parametrize("format_", ["FLAC", "NONE", "WAV"])
def test_final_validation_rejects_bitrate_for_other_formats(
format_: str, monkeypatch: pytest.MonkeyPatch
) -> None:
with pytest.raises(cv.Invalid, match="only supported for the MP3 format") as exc:
_final_validate(monkeypatch, **{CONF_FORMAT: format_, CONF_BITRATE: 48})
assert exc.value.path == [CONF_BITRATE]
+406
View File
@@ -0,0 +1,406 @@
"""Code generation and validation tests for the vnc component."""
from collections.abc import Callable
from pathlib import Path
import re
import pytest
from esphome.config import read_config
from esphome.core import CORE
TEST_CARD = "show_test_card()"
UPDATE_NEVER = "set_update_interval(4294967295UL)"
BASE = """
esphome:
name: vnc-test
esp32:
board: esp32dev
logger:
wifi:
ssid: MySSID
password: password1
spi:
clk_pin: GPIO14
mosi_pin: GPIO12
miso_pin: GPIO13
"""
IT8951 = """
display:
- platform: it8951
id: epd
model: m5stack-m5paper
"""
VNC = """
vnc:
- host: 192.168.1.20
"""
EPAPER_SPI = """
- platform: epaper_spi
id: other
model: ssd1677
dimensions:
width: 200
height: 200
cs_pin: GPIO5
dc_pin: GPIO21
busy_pin: GPIO22
reset_pin: GPIO25
"""
S3_BASE = """
esphome:
name: vnc-test
esp32:
board: esp32-s3-devkitc-1
framework:
type: esp-idf
psram:
mode: octal
speed: 80MHz
wifi:
ssid: MySSID
password: password1
"""
RPI_DPI_RGB = """
display:
- platform: rpi_dpi_rgb
id: rgb
dimensions:
width: 800
height: 480
de_pin: 40
hsync_pin: 39
vsync_pin: 41
pclk_pin: 42
data_pins:
red: [45, 48, 47, 21, 14]
green: [5, 6, 7, 15, 16, 4]
blue: [8, 3, 46, 9, 1]
"""
OPENTHREAD_BASE = """
esphome:
name: vnc-test
esp32:
variant: esp32c6
framework:
type: esp-idf
openthread:
tlv: 0e080000000000010000000300001035060004001fffe00208e227ac6a7f24052f0708fdb753eb517cb4d3051062b2442a928d9ea3b947a1618fc4085a030f4f70656e5468726561642d393837330102987304105330d857354330133c05e1fd7ae81a910c0402a0f7f8
spi:
clk_pin: GPIO6
mosi_pin: GPIO7
"""
C6_EPAPER = """
display:
- platform: epaper_spi
id: epd
model: ssd1677
dimensions:
width: 200
height: 200
cs_pin: GPIO5
dc_pin: GPIO4
busy_pin: GPIO3
reset_pin: GPIO2
"""
@pytest.fixture(autouse=True)
def _clear_toolchain() -> None:
# A host build earlier in the same session leaves the host toolchain selected.
CORE.toolchain = None
@pytest.fixture
def write_yaml(tmp_path: Path) -> Callable[..., Path]:
def writer(*parts: str, base: str = BASE) -> Path:
path = tmp_path / "vnc.yaml"
path.write_text(base + "".join(parts), encoding="utf-8")
return path
return writer
@pytest.fixture
def config_error(
write_yaml: Callable[..., Path], capsys: pytest.CaptureFixture[str]
) -> Callable[..., str]:
"""Validate a config that must fail and return the printed errors."""
def validate(*parts: str, base: str = BASE) -> str:
CORE.config_path = write_yaml(*parts, base=base)
assert read_config({}) is None
return capsys.readouterr().out
return validate
def test_it8951_owned_by_vnc(
generate_main: Callable[[str | Path], str], write_yaml: Callable[..., Path]
) -> None:
main_cpp = generate_main(write_yaml(IT8951, VNC))
assert TEST_CARD not in main_cpp
assert f"epd->{UPDATE_NEVER}" in main_cpp
assert re.search(r"new\(\w+\) vnc::VncClient\(epd, \w+\);", main_cpp)
assert 'set_host("192.168.1.20");' in main_cpp
assert "set_port(5900);" in main_cpp
assert "set_reconnect_interval(5000);" in main_cpp
assert "set_update_delay(200);" in main_cpp
assert "set_max_update_delay(1000);" in main_cpp
def test_epaper_spi_owned_by_vnc(
generate_main: Callable[[str | Path], str], write_yaml: Callable[..., Path]
) -> None:
main_cpp = generate_main(
write_yaml("display:", EPAPER_SPI, "\nvnc:\n - host: 192.168.1.20\n")
)
assert TEST_CARD not in main_cpp
assert f"other->{UPDATE_NEVER}" in main_cpp
assert re.search(r"new\(\w+\) vnc::VncClient\(other, \w+\);", main_cpp)
def test_display_with_generated_id(
generate_main: Callable[[str | Path], str], write_yaml: Callable[..., Path]
) -> None:
main_cpp = generate_main(
write_yaml("display:\n - platform: it8951\n model: m5stack-m5paper\n", VNC)
)
assert TEST_CARD not in main_cpp
assert UPDATE_NEVER in main_cpp
assert "vnc::VncClient(" in main_cpp
def test_only_the_vnc_display_loses_test_card(
generate_main: Callable[[str | Path], str], write_yaml: Callable[..., Path]
) -> None:
main_cpp = generate_main(
write_yaml(
IT8951, EPAPER_SPI, "\nvnc:\n - host: 192.168.1.20\n display_id: epd\n"
)
)
assert f"other->{TEST_CARD}" in main_cpp
assert f"epd->{TEST_CARD}" not in main_cpp
assert f"epd->{UPDATE_NEVER}" in main_cpp
def test_options(
generate_main: Callable[[str | Path], str], write_yaml: Callable[..., Path]
) -> None:
main_cpp = generate_main(
write_yaml(
IT8951,
"""
vnc:
- host: vnc.local
port: 5901
reconnect_interval: 2s
update_delay: 0ms
max_update_delay: 3s
on_connect:
- logger.log: up
on_disconnect:
- logger.log: down
""",
)
)
assert 'set_host("vnc.local");' in main_cpp
assert "set_port(5901);" in main_cpp
assert "set_reconnect_interval(2000);" in main_cpp
assert "set_update_delay(0);" in main_cpp
assert "set_max_update_delay(3000);" in main_cpp
assert "add_on_connect_callback(" in main_cpp
assert "add_on_disconnect_callback(" in main_cpp
def test_two_clients_on_two_displays(
generate_main: Callable[[str | Path], str], write_yaml: Callable[..., Path]
) -> None:
main_cpp = generate_main(
write_yaml(
IT8951,
EPAPER_SPI,
"""
vnc:
- host: 192.168.1.20
display_id: epd
- host: 192.168.1.21
display_id: other
""",
)
)
assert re.search(r"vnc::VncClient\(epd, \w+\);", main_cpp)
assert re.search(r"vnc::VncClient\(other, \w+\);", main_cpp)
assert TEST_CARD not in main_cpp
@pytest.mark.parametrize(
("display_options", "error"),
[
pytest.param(
" lambda: |-\n it.fill(Color::BLACK);\n",
"vnc draws the whole of display 'epd'",
id="lambda",
),
pytest.param(
" pages:\n - id: page1\n lambda: |-\n"
" it.fill(Color::BLACK);\n",
"vnc draws the whole of display 'epd'",
id="pages",
),
pytest.param(
" show_test_card: true\n",
"vnc draws the whole of display 'epd'",
id="show_test_card",
),
pytest.param(
" auto_clear_enabled: true\n",
"vnc draws the whole of display 'epd'",
id="auto_clear_enabled",
),
],
)
def test_display_with_own_writer_rejected(
display_options: str, error: str, config_error: Callable[..., str]
) -> None:
assert error in config_error(IT8951, display_options, VNC)
def test_lvgl_on_same_display_rejected(config_error: Callable[..., str]) -> None:
out = config_error(IT8951, VNC, "\nlvgl:\n displays: [epd]\n")
assert "Display 'epd' is also used by LVGL" in out
def test_lvgl_on_other_display_accepted(
write_yaml: Callable[..., Path],
) -> None:
CORE.config_path = write_yaml(
IT8951,
EPAPER_SPI,
"\nvnc:\n - host: 192.168.1.20\n display_id: epd\n",
"\nlvgl:\n displays: [other]\n",
)
assert read_config({}) is not None
def test_two_clients_on_one_display_rejected(
config_error: Callable[..., str],
) -> None:
out = config_error(
IT8951, "\nvnc:\n - host: 192.168.1.20\n - host: 192.168.1.21\n"
)
assert "Display 'epd' is drawn by more than one component: vnc, vnc" in out
def test_mipi_spi_rejected(config_error: Callable[..., str]) -> None:
out = config_error(
"""
display:
- platform: mipi_spi
id: epd
model: st7735
cs_pin: GPIO15
dc_pin: GPIO27
reset_pin: GPIO26
""",
VNC,
)
assert "Display 'epd' uses the mipi_spi platform" in out
def test_rotated_rpi_dpi_rgb_accepted(write_yaml: Callable[..., Path]) -> None:
CORE.config_path = write_yaml(RPI_DPI_RGB, " rotation: 90\n", VNC, base=S3_BASE)
assert read_config({}) is not None
def test_ipv4_kept_on_with_openthread(
generate_main: Callable[[str | Path], str], write_yaml: Callable[..., Path]
) -> None:
generate_main(write_yaml(C6_EPAPER, VNC, base=OPENTHREAD_BASE))
define = next(d for d in CORE.defines if d.name == "USE_NETWORK_IPV4")
assert str(define.value) == "true"
def test_ipv4_disabled_rejected(config_error: Callable[..., str]) -> None:
out = config_error(
"network:\n enable_ipv4: false\n", C6_EPAPER, VNC, base=OPENTHREAD_BASE
)
assert "IPv4 is required by vnc" in out
def test_waveshare_epaper_rejected(config_error: Callable[..., str]) -> None:
out = config_error(
"""
display:
- platform: waveshare_epaper
id: epd
model: 1.54in
cs_pin: GPIO15
dc_pin: GPIO27
busy_pin: GPIO26
reset_pin: GPIO25
""",
VNC,
)
assert "Display 'epd' uses the waveshare_epaper platform" in out
assert "use epaper_spi" in out
@pytest.mark.parametrize(
("vnc_options", "error"),
[
pytest.param(
" reconnect_interval: 500ms\n",
"value must be at least 1s",
id="reconnect",
),
pytest.param(" port: 0\n", "value must be at least 1", id="port"),
],
)
def test_option_rejected(
vnc_options: str, error: str, config_error: Callable[..., str]
) -> None:
out = config_error(IT8951, VNC, vnc_options)
assert error in out
@pytest.mark.parametrize("host", ["::1", "fe80::1", "2001:db8::10"])
def test_ipv6_host_rejected(host: str, config_error: Callable[..., str]) -> None:
out = config_error(IT8951, f'\nvnc:\n - host: "{host}"\n')
assert "IPv6 addresses are not supported" in out
@@ -0,0 +1,15 @@
ethernet:
type: LAN8720
mdc_pin: 23
mdio_pin: 32
clk:
pin: 0
mode: CLK_EXT_IN
phy_addr: 0
power_pin: 33
wifi:
ap:
ssid: WISP AP
password: wisp12345678
ap_mode: wisp
@@ -19,6 +19,11 @@ image:
} else {
ESP_LOGD("online_image", "Cache miss: fresh download");
}
on_download_started:
- logger.log: Download started
on_download_progress:
lambda: |-
ESP_LOGD("online_image", "Downloaded %.0f%% (%zu bytes)", progress, bytes_read);
- platform: online_image
id: online_binary_transparent_image
url: http://www.libpng.org/pub/png/img_png/pnglogo-blk-tiny.png
@@ -2,6 +2,7 @@
#include <algorithm>
#include <array>
#include <cmath>
#include <cstdint>
#include <cstring>
#include <memory>
@@ -27,6 +28,8 @@ class FakeContainer : public http_request::HttpContainer {
}
int read(uint8_t *buf, size_t max_len) override {
if (this->read_error_)
return -1;
size_t len = std::min(max_len, this->available_ - this->bytes_read_);
std::memcpy(buf, this->body_.data() + this->bytes_read_, len);
this->bytes_read_ += len;
@@ -42,6 +45,10 @@ class FakeContainer : public http_request::HttpContainer {
}
void set_content_length(size_t length) { this->content_length = length; }
/// Make every read from now on fail, like a dropped connection
void fail_reads() { this->read_error_ = true; }
/// Let len more bytes of the body arrive
void deliver(size_t len) { this->available_ = std::min(this->body_.size(), this->available_ + len); }
void add_response_header(const std::string &name, const std::string &value) {
this->response_headers_.push_back({name, value});
}
@@ -49,6 +56,7 @@ class FakeContainer : public http_request::HttpContainer {
protected:
std::vector<uint8_t> body_;
size_t available_;
bool read_error_{false};
};
class FakeHttpRequest : public http_request::HttpRequestComponent {
@@ -123,13 +131,173 @@ static constexpr std::array<uint8_t, 69> PNG_1X1{
0x0C, 0x49, 0x44, 0x41, 0x54, 0x78, 0x9C, 0x63, 0xF8, 0xCF, 0xC0, 0x00, 0x00, 0x03, 0x01, 0x01, 0x00, 0xC9,
0xFE, 0x92, 0xEF, 0x00, 0x00, 0x00, 0x00, 0x49, 0x45, 0x4E, 0x44, 0xAE, 0x42, 0x60, 0x82};
// 8x8 progressive JPEG, 521 bytes
static const uint8_t JPEG_PROGRESSIVE[] = {
0xFF, 0xD8, 0xFF, 0xE0, 0x00, 0x10, 0x4A, 0x46, 0x49, 0x46, 0x00, 0x01, 0x01, 0x00, 0x00, 0x01, 0x00, 0x01, 0x00,
0x00, 0xFF, 0xDB, 0x00, 0x43, 0x00, 0x10, 0x0B, 0x0C, 0x0E, 0x0C, 0x0A, 0x10, 0x0E, 0x0D, 0x0E, 0x12, 0x11, 0x10,
0x13, 0x18, 0x28, 0x1A, 0x18, 0x16, 0x16, 0x18, 0x31, 0x23, 0x25, 0x1D, 0x28, 0x3A, 0x33, 0x3D, 0x3C, 0x39, 0x33,
0x38, 0x37, 0x40, 0x48, 0x5C, 0x4E, 0x40, 0x44, 0x57, 0x45, 0x37, 0x38, 0x50, 0x6D, 0x51, 0x57, 0x5F, 0x62, 0x67,
0x68, 0x67, 0x3E, 0x4D, 0x71, 0x79, 0x70, 0x64, 0x78, 0x5C, 0x65, 0x67, 0x63, 0xFF, 0xDB, 0x00, 0x43, 0x01, 0x11,
0x12, 0x12, 0x18, 0x15, 0x18, 0x2F, 0x1A, 0x1A, 0x2F, 0x63, 0x42, 0x38, 0x42, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63,
0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63,
0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63,
0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0xFF, 0xC2, 0x00, 0x11, 0x08, 0x00, 0x08, 0x00, 0x08, 0x03, 0x01, 0x22, 0x00,
0x02, 0x11, 0x01, 0x03, 0x11, 0x01, 0xFF, 0xC4, 0x00, 0x15, 0x00, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x04, 0xFF, 0xC4, 0x00, 0x15, 0x01, 0x01, 0x01, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0x05, 0xFF, 0xDA, 0x00, 0x0C, 0x03,
0x01, 0x00, 0x02, 0x10, 0x03, 0x10, 0x00, 0x00, 0x01, 0x94, 0x05, 0x3F, 0xFF, 0xC4, 0x00, 0x14, 0x10, 0x01, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFF, 0xDA, 0x00, 0x08,
0x01, 0x01, 0x00, 0x01, 0x05, 0x02, 0x7F, 0xFF, 0xC4, 0x00, 0x14, 0x11, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFF, 0xDA, 0x00, 0x08, 0x01, 0x03, 0x01, 0x01, 0x3F,
0x01, 0x7F, 0xFF, 0xC4, 0x00, 0x14, 0x11, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0xFF, 0xDA, 0x00, 0x08, 0x01, 0x02, 0x01, 0x01, 0x3F, 0x01, 0x7F, 0xFF, 0xC4, 0x00,
0x14, 0x10, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0xFF, 0xDA, 0x00, 0x08, 0x01, 0x01, 0x00, 0x06, 0x3F, 0x02, 0x7F, 0xFF, 0xC4, 0x00, 0x14, 0x10, 0x01, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFF, 0xDA, 0x00, 0x08, 0x01,
0x01, 0x00, 0x01, 0x3F, 0x21, 0x7F, 0xFF, 0xDA, 0x00, 0x0C, 0x03, 0x01, 0x00, 0x02, 0x00, 0x03, 0x00, 0x00, 0x00,
0x10, 0x0B, 0xFF, 0xC4, 0x00, 0x14, 0x11, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0xFF, 0xDA, 0x00, 0x08, 0x01, 0x03, 0x01, 0x01, 0x3F, 0x10, 0x7F, 0xFF, 0xC4, 0x00,
0x14, 0x11, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0xFF, 0xDA, 0x00, 0x08, 0x01, 0x02, 0x01, 0x01, 0x3F, 0x10, 0x7F, 0xFF, 0xC4, 0x00, 0x14, 0x10, 0x01, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFF, 0xDA, 0x00, 0x08, 0x01,
0x01, 0x00, 0x01, 0x3F, 0x10, 0x7F, 0xFF, 0xD9,
};
// 48x40 4:2:0 JPEG (quality 95), 2344 bytes: three rows of 16 pixel MCUs, with the scan starting at byte 609
static const uint8_t JPEG_48X40[] = {
0xFF, 0xD8, 0xFF, 0xE0, 0x00, 0x10, 0x4A, 0x46, 0x49, 0x46, 0x00, 0x01, 0x01, 0x00, 0x00, 0x01, 0x00, 0x01, 0x00,
0x00, 0xFF, 0xDB, 0x00, 0x43, 0x00, 0x02, 0x01, 0x01, 0x01, 0x01, 0x01, 0x02, 0x01, 0x01, 0x01, 0x02, 0x02, 0x02,
0x02, 0x02, 0x04, 0x03, 0x02, 0x02, 0x02, 0x02, 0x05, 0x04, 0x04, 0x03, 0x04, 0x06, 0x05, 0x06, 0x06, 0x06, 0x05,
0x06, 0x06, 0x06, 0x07, 0x09, 0x08, 0x06, 0x07, 0x09, 0x07, 0x06, 0x06, 0x08, 0x0B, 0x08, 0x09, 0x0A, 0x0A, 0x0A,
0x0A, 0x0A, 0x06, 0x08, 0x0B, 0x0C, 0x0B, 0x0A, 0x0C, 0x09, 0x0A, 0x0A, 0x0A, 0xFF, 0xDB, 0x00, 0x43, 0x01, 0x02,
0x02, 0x02, 0x02, 0x02, 0x02, 0x05, 0x03, 0x03, 0x05, 0x0A, 0x07, 0x06, 0x07, 0x0A, 0x0A, 0x0A, 0x0A, 0x0A, 0x0A,
0x0A, 0x0A, 0x0A, 0x0A, 0x0A, 0x0A, 0x0A, 0x0A, 0x0A, 0x0A, 0x0A, 0x0A, 0x0A, 0x0A, 0x0A, 0x0A, 0x0A, 0x0A, 0x0A,
0x0A, 0x0A, 0x0A, 0x0A, 0x0A, 0x0A, 0x0A, 0x0A, 0x0A, 0x0A, 0x0A, 0x0A, 0x0A, 0x0A, 0x0A, 0x0A, 0x0A, 0x0A, 0x0A,
0x0A, 0x0A, 0x0A, 0x0A, 0x0A, 0x0A, 0xFF, 0xC0, 0x00, 0x11, 0x08, 0x00, 0x28, 0x00, 0x30, 0x03, 0x01, 0x22, 0x00,
0x02, 0x11, 0x01, 0x03, 0x11, 0x01, 0xFF, 0xC4, 0x00, 0x1F, 0x00, 0x00, 0x01, 0x05, 0x01, 0x01, 0x01, 0x01, 0x01,
0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A,
0x0B, 0xFF, 0xC4, 0x00, 0xB5, 0x10, 0x00, 0x02, 0x01, 0x03, 0x03, 0x02, 0x04, 0x03, 0x05, 0x05, 0x04, 0x04, 0x00,
0x00, 0x01, 0x7D, 0x01, 0x02, 0x03, 0x00, 0x04, 0x11, 0x05, 0x12, 0x21, 0x31, 0x41, 0x06, 0x13, 0x51, 0x61, 0x07,
0x22, 0x71, 0x14, 0x32, 0x81, 0x91, 0xA1, 0x08, 0x23, 0x42, 0xB1, 0xC1, 0x15, 0x52, 0xD1, 0xF0, 0x24, 0x33, 0x62,
0x72, 0x82, 0x09, 0x0A, 0x16, 0x17, 0x18, 0x19, 0x1A, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2A, 0x34, 0x35, 0x36, 0x37,
0x38, 0x39, 0x3A, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0x4A, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59, 0x5A,
0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 0x6A, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79, 0x7A, 0x83, 0x84, 0x85,
0x86, 0x87, 0x88, 0x89, 0x8A, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98, 0x99, 0x9A, 0xA2, 0xA3, 0xA4, 0xA5, 0xA6,
0xA7, 0xA8, 0xA9, 0xAA, 0xB2, 0xB3, 0xB4, 0xB5, 0xB6, 0xB7, 0xB8, 0xB9, 0xBA, 0xC2, 0xC3, 0xC4, 0xC5, 0xC6, 0xC7,
0xC8, 0xC9, 0xCA, 0xD2, 0xD3, 0xD4, 0xD5, 0xD6, 0xD7, 0xD8, 0xD9, 0xDA, 0xE1, 0xE2, 0xE3, 0xE4, 0xE5, 0xE6, 0xE7,
0xE8, 0xE9, 0xEA, 0xF1, 0xF2, 0xF3, 0xF4, 0xF5, 0xF6, 0xF7, 0xF8, 0xF9, 0xFA, 0xFF, 0xC4, 0x00, 0x1F, 0x01, 0x00,
0x03, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x02, 0x03,
0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0xFF, 0xC4, 0x00, 0xB5, 0x11, 0x00, 0x02, 0x01, 0x02, 0x04, 0x04,
0x03, 0x04, 0x07, 0x05, 0x04, 0x04, 0x00, 0x01, 0x02, 0x77, 0x00, 0x01, 0x02, 0x03, 0x11, 0x04, 0x05, 0x21, 0x31,
0x06, 0x12, 0x41, 0x51, 0x07, 0x61, 0x71, 0x13, 0x22, 0x32, 0x81, 0x08, 0x14, 0x42, 0x91, 0xA1, 0xB1, 0xC1, 0x09,
0x23, 0x33, 0x52, 0xF0, 0x15, 0x62, 0x72, 0xD1, 0x0A, 0x16, 0x24, 0x34, 0xE1, 0x25, 0xF1, 0x17, 0x18, 0x19, 0x1A,
0x26, 0x27, 0x28, 0x29, 0x2A, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3A, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0x4A,
0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59, 0x5A, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 0x6A, 0x73, 0x74, 0x75,
0x76, 0x77, 0x78, 0x79, 0x7A, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87, 0x88, 0x89, 0x8A, 0x92, 0x93, 0x94, 0x95, 0x96,
0x97, 0x98, 0x99, 0x9A, 0xA2, 0xA3, 0xA4, 0xA5, 0xA6, 0xA7, 0xA8, 0xA9, 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B,
0x5E, 0x8E, 0x4F, 0x7D, 0xFA, 0xDB, 0x57, 0xB2, 0x56, 0xD6, 0xED, 0xF7, 0x3E, 0x10, 0xB2, 0x82, 0xF6, 0xC2, 0xF2,
0xF3, 0x4E, 0xD4, 0xAF, 0xEE, 0x5A, 0xCA, 0xCE, 0xD2, 0xF1, 0xA7, 0xB8, 0x9D, 0xD4, 0x42, 0x8C, 0xF2, 0x3C, 0x8E,
0x42, 0x02, 0x24, 0xFD, 0xDA, 0x96, 0x60, 0xAD, 0xB0, 0xC9, 0x0E, 0x00, 0xC8, 0xC8, 0xEA, 0xEE, 0x12, 0x7B, 0x09,
0x6C, 0x6E, 0xCE, 0xA2, 0x0B, 0xAA, 0x32, 0xD9, 0xB9, 0x49, 0x66, 0xDF, 0x74, 0xCC, 0x31, 0x1B, 0xE4, 0xE6, 0x63,
0xF2, 0x91, 0x95, 0x42, 0x15, 0x8B, 0x7D, 0xE0, 0xAC, 0xB4, 0x51, 0x5F, 0x33, 0x3A, 0xD8, 0x8C, 0x46, 0x7A, 0x9C,
0xA6, 0xD7, 0x2C, 0x13, 0x49, 0x25, 0x6E, 0x6A, 0x73, 0xE4, 0x84, 0x9E, 0x9A, 0xC9, 0x29, 0x24, 0x9E, 0xE9, 0x42,
0x11, 0x8D, 0xA2, 0x9C, 0x5F, 0xF5, 0x37, 0x0D, 0x36, 0xF8, 0x7E, 0x9A, 0xEC, 0xE5, 0xE5, 0x7E, 0x67, 0x77, 0x7B,
0x7A, 0xBF, 0x5E, 0xB7, 0x3F, 0xFF, 0xD9,
};
static void set_loop_time(uint32_t now) { LoopBlockingGuard clock(nullptr, LOG_STR("test"), now); }
class OnlineImageDownloadEnd : public ::testing::Test {
protected:
std::unique_ptr<OnlineImage> make_image_(runtime_image::ImageFormat format) {
std::unique_ptr<OnlineImage> make_image_(runtime_image::ImageFormat format, uint32_t buffer_size = 256) {
auto img = std::make_unique<OnlineImage>("http://example.com/image", 0, 0, format, image::IMAGE_TYPE_RGB,
image::TRANSPARENCY_OPAQUE, nullptr, 256);
image::TRANSPARENCY_OPAQUE, nullptr, buffer_size);
img->set_parent(&this->http_);
return img;
}
@@ -286,4 +454,260 @@ TEST_F(OnlineImageDownloadEnd, JpegBufferAllocationFailureReportsError) {
EXPECT_EQ(this->finished_, 0);
}
// The JPEG decoded with all of it in memory at once, to compare the downloaded images with
static std::vector<uint8_t> reference_jpeg_pixels() {
runtime_image::RuntimeImage ref(runtime_image::JPEG, image::IMAGE_TYPE_RGB, image::TRANSPARENCY_OPAQUE);
std::vector<uint8_t> data(JPEG_48X40, JPEG_48X40 + sizeof(JPEG_48X40));
EXPECT_TRUE(ref.begin_decode(data.size()));
EXPECT_EQ(ref.feed_data(data.data(), data.size()), static_cast<int>(data.size()));
EXPECT_TRUE(ref.end_decode());
return std::vector<uint8_t>(ref.get_data_start(), ref.get_data_start() + ref.get_width_stride() * ref.get_height());
}
static std::vector<uint8_t> image_pixels(OnlineImage &image) {
return std::vector<uint8_t>(image.get_data_start(),
image.get_data_start() + image.get_width_stride() * image.get_height());
}
// A JPEG of known size is decoded as it arrives, so a buffer smaller than the file only has to hold the headers
TEST_F(OnlineImageDownloadEnd, JpegDecodesThroughASmallBuffer) {
auto image = this->make_image_(runtime_image::JPEG, 1024);
// Bytes after the image are never needed. Waiting for the whole file before decoding would read them all.
std::vector<uint8_t> body(JPEG_48X40, JPEG_48X40 + sizeof(JPEG_48X40));
body.resize(body.size() + 8192, 0);
auto container = std::make_shared<FakeContainer>(body, body.size(), false);
container->add_response_header("etag", "\"abc\"");
this->start_(*image, container);
for (int i = 0; i < 100 && this->finished_ + this->errors_ == 0; i++)
image->loop();
ASSERT_EQ(this->finished_, 1);
EXPECT_LT(container->get_bytes_read(), body.size()) << "the image was not decoded as it arrived";
EXPECT_EQ(this->errors_, 0);
ASSERT_EQ(image->get_width(), 48);
ASSERT_EQ(image->get_height(), 40);
EXPECT_EQ(image_pixels(*image), reference_jpeg_pixels());
// A complete image is cached
this->http_.next = std::make_shared<FakeContainer>(std::vector<uint8_t>{}, 0, false);
image->update();
EXPECT_TRUE(this->http_.sent_header("if-none-match"));
}
// A slow link: the decoder waits part way through the image for the rest to arrive
TEST_F(OnlineImageDownloadEnd, JpegWaitsForASlowDownload) {
auto image = this->make_image_(runtime_image::JPEG, 1024);
auto container =
std::make_shared<FakeContainer>(std::vector<uint8_t>(JPEG_48X40, JPEG_48X40 + sizeof(JPEG_48X40)), 1024, false);
this->start_(*image, container);
int passes = 0;
for (; passes < 1000 && this->finished_ + this->errors_ == 0; passes++) {
image->loop();
if (passes == 5) {
EXPECT_EQ(this->finished_, 0) << "the image cannot be finished before the rest of it arrives";
}
container->deliver(40);
}
ASSERT_EQ(this->finished_, 1);
EXPECT_EQ(this->errors_, 0);
EXPECT_GT(passes, 10);
EXPECT_EQ(image_pixels(*image), reference_jpeg_pixels());
}
// A download that stops part way through times out rather than waiting for ever
TEST_F(OnlineImageDownloadEnd, JpegDownloadThatStopsTimesOut) {
auto image = this->make_image_(runtime_image::JPEG, 1024);
auto container =
std::make_shared<FakeContainer>(std::vector<uint8_t>(JPEG_48X40, JPEG_48X40 + sizeof(JPEG_48X40)), 1500, false);
set_loop_time(1000);
this->start_(*image, container);
for (int i = 0; i < 10; i++)
image->loop();
EXPECT_EQ(this->errors_, 0) << "still within the timeout";
set_loop_time(1000 + this->http_.get_timeout());
image->loop();
EXPECT_EQ(this->errors_, 1);
EXPECT_EQ(this->finished_, 0);
}
// Headers that do not fit the download buffer cannot be read as the image arrives, so the whole image is
// downloaded first, keeping the bytes already read
TEST_F(OnlineImageDownloadEnd, JpegHeadersLargerThanTheBufferDownloadTheWholeImage) {
auto image = this->make_image_(runtime_image::JPEG, 256);
auto container = std::make_shared<FakeContainer>(std::vector<uint8_t>(JPEG_48X40, JPEG_48X40 + sizeof(JPEG_48X40)),
sizeof(JPEG_48X40), false);
this->start_(*image, container);
for (int i = 0; i < 100 && this->finished_ + this->errors_ == 0; i++) {
image->loop();
container->deliver(256);
}
ASSERT_EQ(this->finished_, 1);
EXPECT_EQ(this->errors_, 0);
EXPECT_EQ(container->get_bytes_read(), sizeof(JPEG_48X40));
EXPECT_EQ(image_pixels(*image), reference_jpeg_pixels());
}
// The pass that opens the image does not decode any of it
TEST_F(OnlineImageDownloadEnd, JpegOpensInAPassOfItsOwn) {
auto image = this->make_image_(runtime_image::JPEG, 1024);
auto container = std::make_shared<FakeContainer>(std::vector<uint8_t>(JPEG_48X40, JPEG_48X40 + sizeof(JPEG_48X40)),
sizeof(JPEG_48X40), false);
this->start_(*image, container);
image->loop();
EXPECT_EQ(this->finished_ + this->errors_, 0);
for (int i = 0; i < 100 && this->finished_ + this->errors_ == 0; i++)
image->loop();
EXPECT_EQ(this->finished_, 1);
}
// A progressive JPEG is refused while it downloads, without blaming the buffer size
TEST_F(OnlineImageDownloadEnd, JpegProgressiveReportsError) {
auto image = this->make_image_(runtime_image::JPEG, 256);
std::vector<uint8_t> body(JPEG_PROGRESSIVE, JPEG_PROGRESSIVE + sizeof(JPEG_PROGRESSIVE));
auto container = std::make_shared<FakeContainer>(body, 256, false);
this->start_(*image, container);
for (int i = 0; i < 10 && this->finished_ + this->errors_ == 0; i++)
image->loop();
EXPECT_EQ(this->errors_, 1);
EXPECT_EQ(this->finished_, 0);
}
// on_download_started fires once the server has accepted the request, before the first progress report, and not
// for a response that has no image to download
TEST_F(OnlineImageDownloadEnd, StartedFiresOnlyWhenTheDownloadBegins) {
auto image = this->make_image_(runtime_image::JPEG, 1024);
std::vector<std::string> events;
image->add_on_download_started_callback([&events]() { events.emplace_back("started"); });
image->add_on_progress_callback([&events](float, size_t) { events.emplace_back("progress"); });
this->start_(*image,
std::make_shared<FakeContainer>(std::vector<uint8_t>(JPEG_48X40, JPEG_48X40 + sizeof(JPEG_48X40)),
sizeof(JPEG_48X40), false));
ASSERT_GE(events.size(), 2u);
EXPECT_EQ(events[0], "started");
EXPECT_EQ(events[1], "progress");
for (int i = 0; i < 100 && this->finished_ + this->errors_ == 0; i++)
image->loop();
EXPECT_EQ(std::count(events.begin(), events.end(), std::string("started")), 1);
for (int status : {304, 404}) {
events.clear();
auto container = std::make_shared<FakeContainer>(std::vector<uint8_t>{}, 0, false);
container->status_code = status;
this->http_.next = container;
image->update();
EXPECT_TRUE(events.empty()) << "HTTP " << status;
}
}
// Progress starts at 0, is reported at most every 100 ms, and always reaches 100%
TEST_F(OnlineImageDownloadEnd, ProgressIsReportedAsTheImageDownloads) {
auto image = this->make_image_(runtime_image::JPEG, 1024);
std::vector<float> reports;
image->add_on_progress_callback([&reports](float progress, size_t) { reports.push_back(progress); });
auto container =
std::make_shared<FakeContainer>(std::vector<uint8_t>(JPEG_48X40, JPEG_48X40 + sizeof(JPEG_48X40)), 100, false);
uint32_t now = 1000;
set_loop_time(now);
this->start_(*image, container);
ASSERT_EQ(reports.size(), 1u);
EXPECT_EQ(reports[0], 0.0f);
int passes = 0;
for (; passes < 1000 && this->finished_ + this->errors_ == 0; passes++) {
now += 50;
set_loop_time(now);
container->deliver(100);
image->loop();
}
ASSERT_EQ(this->finished_, 1);
EXPECT_EQ(reports.back(), 100.0f);
EXPECT_TRUE(std::is_sorted(reports.begin(), reports.end()));
// A report every 100 ms of a 50 ms loop, plus the start and the end
EXPECT_LE(reports.size(), static_cast<size_t>(passes / 2 + 2));
EXPECT_GT(reports.size(), 3u);
}
// Without a Content-Length the percentage is unknown, but the bytes read are still reported
TEST_F(OnlineImageDownloadEnd, ProgressOfAChunkedDownloadHasNoPercentage) {
auto image = this->make_image_(runtime_image::PNG);
std::vector<std::pair<float, size_t>> reports;
image->add_on_progress_callback(
[&reports](float progress, size_t bytes_read) { reports.emplace_back(progress, bytes_read); });
auto container =
std::make_shared<FakeContainer>(std::vector<uint8_t>(PNG_1X1.begin(), PNG_1X1.end()), PNG_1X1.size(), true);
set_loop_time(1000);
this->start_(*image, container);
set_loop_time(2000);
for (int i = 0; i < 10 && this->finished_ + this->errors_ == 0; i++)
image->loop();
ASSERT_GE(reports.size(), 2u);
EXPECT_TRUE(std::isnan(reports.back().first));
EXPECT_EQ(reports.back().second, PNG_1X1.size());
}
// Scan data that cannot be decoded fails the download part way through
TEST_F(OnlineImageDownloadEnd, JpegDecodeErrorFailsTheDownload) {
auto image = this->make_image_(runtime_image::JPEG, 1024);
std::vector<uint8_t> body(JPEG_48X40, JPEG_48X40 + sizeof(JPEG_48X40));
std::fill(body.begin() + 700, body.end() - 2, 0xFE);
auto container = std::make_shared<FakeContainer>(body, 1024, false);
this->start_(*image, container);
for (int i = 0; i < 100 && this->finished_ + this->errors_ == 0; i++) {
image->loop();
container->deliver(256);
}
EXPECT_EQ(this->errors_, 1);
EXPECT_EQ(this->finished_, 0);
}
// A read that fails part way through the download ends it with an error
TEST_F(OnlineImageDownloadEnd, JpegReadErrorFailsTheDownload) {
auto image = this->make_image_(runtime_image::JPEG, 1024);
auto container =
std::make_shared<FakeContainer>(std::vector<uint8_t>(JPEG_48X40, JPEG_48X40 + sizeof(JPEG_48X40)), 1024, false);
this->start_(*image, container);
for (int i = 0; i < 5; i++)
image->loop();
ASSERT_EQ(this->errors_, 0);
container->fail_reads();
image->loop();
EXPECT_EQ(this->errors_, 1);
EXPECT_EQ(this->finished_, 0);
}
TEST(DownloadBufferGrow, KeepsTheUnreadData) {
DownloadBuffer buffer(16);
std::memcpy(buffer.append(), "abcdef", 6);
buffer.write(6);
buffer.read(2);
ASSERT_TRUE(buffer.grow(64));
EXPECT_EQ(buffer.size(), 64u);
ASSERT_EQ(buffer.unread(), 4u);
EXPECT_EQ(std::memcmp(buffer.data(), "cdef", 4), 0);
// A failed allocation leaves the buffer as it was
EXPECT_FALSE(buffer.grow(SIZE_MAX / 2));
EXPECT_EQ(buffer.size(), 64u);
EXPECT_EQ(std::memcmp(buffer.data(), "cdef", 4), 0);
// Growing to a smaller size changes nothing
EXPECT_TRUE(buffer.grow(8));
EXPECT_EQ(buffer.size(), 64u);
}
} // namespace esphome::online_image::testing
@@ -4,9 +4,11 @@
#include <algorithm>
#include <array>
#include <cstring>
#include <utility>
#include <vector>
#include "esphome/components/runtime_image/image_decoder.h"
#include "esphome/components/runtime_image/jpeg_decoder.h"
#include "esphome/components/runtime_image/runtime_image.h"
namespace esphome::runtime_image::testing {
@@ -362,6 +364,23 @@ static std::vector<uint8_t> pixel_bytes(TestableRuntimeImage &img) {
return std::vector<uint8_t>(start, start + img.get_width_stride() * img.get_height());
}
// Shrinking a PNG keeps the first source pixel of each span, as the JPEG path does: the even rows and columns
TEST(RuntimeImageDecoder, ShrunkPngKeepsTheFirstPixelOfEachSpan) {
TestableRuntimeImage img(PNG, image::IMAGE_TYPE_RGB, false, 2, 2);
ASSERT_TRUE(decode_all(img, PNG_RGB, sizeof(PNG_RGB), PNG));
ASSERT_EQ(img.get_width(), 2);
ASSERT_EQ(img.get_height(), 2);
for (int row = 0; row < 2; row++) {
for (int col = 0; col < 2; col++) {
const Color pixel = img.get_pixel(col, row);
const uint8_t *expected = PNG_RGB_EXPECTED[row * 2][col * 2];
EXPECT_EQ(pixel.r, expected[0]) << col << "," << row;
EXPECT_EQ(pixel.g, expected[1]) << col << "," << row;
EXPECT_EQ(pixel.b, expected[2]) << col << "," << row;
}
}
}
TEST(RuntimeImageDecoder, JpegDecoderStaysWarmAcrossDecodes) {
TestableRuntimeImage img(JPEG);
@@ -377,6 +396,12 @@ TEST(RuntimeImageDecoder, JpegDecoderStaysWarmAcrossDecodes) {
EXPECT_EQ(pixel_bytes(img), first_pixels) << "reused decoder must reproduce identical pixels";
}
// The RGB565 word at (x, y) of a decoded image, in its own byte order
static uint16_t rgb565_word(const TestableRuntimeImage &img, int x, int y) {
const uint8_t *p = img.get_data_start() + (y * img.get_width() + x) * 2;
return p[0] | (p[1] << 8);
}
// JPEGDEC's colour conversion differs by one step between its SIMD and scalar builds, so each
// channel is checked within one step; the byte order is still exact
static void expect_rgb565_near(uint16_t actual, uint16_t expected, const char *order, size_t pixel) {
@@ -386,16 +411,12 @@ static void expect_rgb565_near(uint16_t actual, uint16_t expected, const char *o
}
TEST(RuntimeImageDecoder, JpegDecodesDirectlyToScaledRgb565WithBothByteOrders) {
// Halving the 8x8 gradient keeps its even rows and columns. The words are JPEGDEC's output for those
// pixels, red in the top five bits: a decode of the fixture with PIL gives (51, 7, 30) for source
// pixel (2, 0), which is word 0x3803 here.
static const uint16_t EXPECTED[4 * 4] = {
0x0000, 0x3803, 0x8008, 0xB80B, 0x0203, 0x3A07, 0x81EC, 0xBA0F,
0x03E7, 0x3C0B, 0x83F0, 0xBC13, 0x05EC, 0x3E0F, 0x85F4, 0xBE17,
};
// Halving the 8x8 gradient makes JPEGDEC decode it at half size, which averages each 2x2 block of pixels
TestableRuntimeImage full(JPEG, image::IMAGE_TYPE_RGB565, false, 8, 8);
TestableRuntimeImage rgb565_little_endian(JPEG, image::IMAGE_TYPE_RGB565, false, 4, 4);
TestableRuntimeImage rgb565_big_endian(JPEG, image::IMAGE_TYPE_RGB565, true, 4, 4);
ASSERT_TRUE(decode_all(full, JPEG_GRADIENT, sizeof(JPEG_GRADIENT)));
ASSERT_TRUE(decode_all(rgb565_little_endian, JPEG_GRADIENT, sizeof(JPEG_GRADIENT)));
ASSERT_TRUE(decode_all(rgb565_big_endian, JPEG_GRADIENT, sizeof(JPEG_GRADIENT)));
ASSERT_EQ(rgb565_little_endian.get_width(), 4);
@@ -405,9 +426,24 @@ TEST(RuntimeImageDecoder, JpegDecodesDirectlyToScaledRgb565WithBothByteOrders) {
const uint8_t *little = rgb565_little_endian.get_data_start();
const uint8_t *big = rgb565_big_endian.get_data_start();
for (size_t i = 0; i < 4 * 4; i++) {
expect_rgb565_near(little[i * 2] | (little[i * 2 + 1] << 8), EXPECTED[i], "little-endian", i);
expect_rgb565_near((big[i * 2] << 8) | big[i * 2 + 1], EXPECTED[i], "big-endian", i);
for (int row = 0; row < 4; row++) {
for (int col = 0; col < 4; col++) {
int red = 0;
int green = 0;
int blue = 0;
for (int dy = 0; dy < 2; dy++) {
for (int dx = 0; dx < 2; dx++) {
const uint16_t word = rgb565_word(full, col * 2 + dx, row * 2 + dy);
red += word >> 11;
green += (word >> 5) & 0x3F;
blue += word & 0x1F;
}
}
const uint16_t expected = ((red + 2) / 4) << 11 | ((green + 2) / 4) << 5 | (blue + 2) / 4;
const size_t i = row * 4 + col;
expect_rgb565_near(little[i * 2] | (little[i * 2 + 1] << 8), expected, "little-endian", i);
expect_rgb565_near((big[i * 2] << 8) | big[i * 2 + 1], expected, "big-endian", i);
}
}
}
@@ -415,19 +451,25 @@ TEST(RuntimeImageDecoder, JpegDecodesDirectlyToScaledRgb565WithBothByteOrders) {
// prepare_rgb565_scaling_ rounds: source n covers targets ceil(n * target / source) up to the next start
static uint16_t scaled_boundary(int n, int source, int target) { return (n * target + source - 1) / source; }
// The RGB565 word at (x, y) of a decoded image, in its own byte order
static uint16_t rgb565_word(const TestableRuntimeImage &img, int x, int y) {
const uint8_t *p = img.get_data_start() + (y * img.get_width() + x) * 2;
return p[0] | (p[1] << 8);
}
// Every scaled direct decode must equal the unscaled one sampled through the boundary table,
// for shrinking, growing and a ratio that is not a whole number
// JPEGDEC decodes at the smallest of full, 1/2, 1/4 and 1/8 size that still covers the buffer
TEST(RuntimeImageDecoder, JpegDecodesAtTheSmallestSizeThatCoversTheBuffer) {
for (auto [size, scale] :
{std::pair{8, 1}, std::pair{5, 1}, std::pair{4, 2}, std::pair{3, 2}, std::pair{2, 4}, std::pair{1, 8}}) {
TestableRuntimeImage img(JPEG, image::IMAGE_TYPE_RGB565, false, size, size);
ASSERT_TRUE(decode_all(img, JPEG_GRADIENT, sizeof(JPEG_GRADIENT)));
EXPECT_EQ(img.get_width(), size);
EXPECT_EQ(static_cast<JpegDecoder *>(img.decoder())->get_scale(), scale) << size << "x" << size;
}
}
TEST(RuntimeImageDecoder, JpegDirectRgb565ScalesBySourcePixelSpans) {
TestableRuntimeImage full(JPEG, image::IMAGE_TYPE_RGB565, false, 8, 8);
ASSERT_TRUE(decode_all(full, JPEG_GRADIENT, sizeof(JPEG_GRADIENT)));
for (int size : {4, 6, 16}) {
// Sizes JPEGDEC cannot reach by scaling, so the full size decode is spread over the buffer
for (int size : {5, 6, 16}) {
TestableRuntimeImage scaled(JPEG, image::IMAGE_TYPE_RGB565, false, size, size);
ASSERT_TRUE(decode_all(scaled, JPEG_GRADIENT, sizeof(JPEG_GRADIENT)));
ASSERT_EQ(scaled.get_width(), size);
@@ -0,0 +1,83 @@
#include <gtest/gtest.h>
#include <cstdlib>
#include <vector>
#include "esphome/components/runtime_image/runtime_image.h"
#ifdef USE_RUNTIME_IMAGE_JPEG
namespace esphome::runtime_image::testing {
// 16x8 grayscale JPEG (quality 95) of a smooth gradient, from 60 at the top left to 185 at the bottom right
static const uint8_t JPEG_GRAY[] = {
0xFF, 0xD8, 0xFF, 0xE0, 0x00, 0x10, 0x4A, 0x46, 0x49, 0x46, 0x00, 0x01, 0x01, 0x00, 0x00, 0x01, 0x00, 0x01, 0x00,
0x00, 0xFF, 0xDB, 0x00, 0x43, 0x00, 0x02, 0x01, 0x01, 0x01, 0x01, 0x01, 0x02, 0x01, 0x01, 0x01, 0x02, 0x02, 0x02,
0x02, 0x02, 0x04, 0x03, 0x02, 0x02, 0x02, 0x02, 0x05, 0x04, 0x04, 0x03, 0x04, 0x06, 0x05, 0x06, 0x06, 0x06, 0x05,
0x06, 0x06, 0x06, 0x07, 0x09, 0x08, 0x06, 0x07, 0x09, 0x07, 0x06, 0x06, 0x08, 0x0B, 0x08, 0x09, 0x0A, 0x0A, 0x0A,
0x0A, 0x0A, 0x06, 0x08, 0x0B, 0x0C, 0x0B, 0x0A, 0x0C, 0x09, 0x0A, 0x0A, 0x0A, 0xFF, 0xC0, 0x00, 0x0B, 0x08, 0x00,
0x08, 0x00, 0x10, 0x01, 0x01, 0x11, 0x00, 0xFF, 0xC4, 0x00, 0x1F, 0x00, 0x00, 0x01, 0x05, 0x01, 0x01, 0x01, 0x01,
0x01, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09,
0x0A, 0x0B, 0xFF, 0xC4, 0x00, 0xB5, 0x10, 0x00, 0x02, 0x01, 0x03, 0x03, 0x02, 0x04, 0x03, 0x05, 0x05, 0x04, 0x04,
0x00, 0x00, 0x01, 0x7D, 0x01, 0x02, 0x03, 0x00, 0x04, 0x11, 0x05, 0x12, 0x21, 0x31, 0x41, 0x06, 0x13, 0x51, 0x61,
0x07, 0x22, 0x71, 0x14, 0x32, 0x81, 0x91, 0xA1, 0x08, 0x23, 0x42, 0xB1, 0xC1, 0x15, 0x52, 0xD1, 0xF0, 0x24, 0x33,
0x62, 0x72, 0x82, 0x09, 0x0A, 0x16, 0x17, 0x18, 0x19, 0x1A, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2A, 0x34, 0x35, 0x36,
0x37, 0x38, 0x39, 0x3A, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0x4A, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59,
0x5A, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 0x6A, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79, 0x7A, 0x83, 0x84,
0x85, 0x86, 0x87, 0x88, 0x89, 0x8A, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98, 0x99, 0x9A, 0xA2, 0xA3, 0xA4, 0xA5,
0xA6, 0xA7, 0xA8, 0xA9, 0xAA, 0xB2, 0xB3, 0xB4, 0xB5, 0xB6, 0xB7, 0xB8, 0xB9, 0xBA, 0xC2, 0xC3, 0xC4, 0xC5, 0xC6,
0xC7, 0xC8, 0xC9, 0xCA, 0xD2, 0xD3, 0xD4, 0xD5, 0xD6, 0xD7, 0xD8, 0xD9, 0xDA, 0xE1, 0xE2, 0xE3, 0xE4, 0xE5, 0xE6,
0xE7, 0xE8, 0xE9, 0xEA, 0xF1, 0xF2, 0xF3, 0xF4, 0xF5, 0xF6, 0xF7, 0xF8, 0xF9, 0xFA, 0xFF, 0xDA, 0x00, 0x08, 0x01,
0x01, 0x00, 0x00, 0x3F, 0x00, 0xF0, 0x9F, 0x82, 0x5F, 0x09, 0x3F, 0xD4, 0xFF, 0x00, 0xA2, 0xFA, 0x76, 0xAF, 0xB0,
0x3E, 0x09, 0x7C, 0x24, 0xFF, 0x00, 0x53, 0xFE, 0x8B, 0xE9, 0xDA, 0xBF, 0xFF, 0xD9,
};
// The pixels Pillow decodes from it
static const uint8_t JPEG_GRAY_PIXELS[8 * 16] = {
60, 66, 72, 78, 84, 90, 96, 102, 108, 114, 120, 126, 132, 138, 144, 150, 65, 71, 77, 83, 89, 95,
101, 107, 113, 119, 125, 131, 137, 143, 149, 155, 70, 76, 82, 88, 94, 100, 106, 112, 118, 124, 130, 136,
142, 148, 154, 160, 75, 81, 87, 93, 99, 105, 111, 117, 123, 129, 135, 141, 147, 153, 159, 165, 80, 86,
92, 98, 104, 110, 116, 122, 128, 134, 140, 146, 152, 158, 164, 170, 85, 91, 97, 103, 109, 115, 121, 127,
133, 139, 145, 151, 157, 163, 169, 175, 90, 96, 102, 108, 114, 120, 126, 132, 138, 144, 150, 156, 162, 168,
174, 180, 95, 101, 107, 113, 119, 125, 131, 137, 143, 149, 155, 161, 167, 173, 179, 185,
};
// JPEGDEC's faster inverse DCT is up to 4 steps from Pillow's on this image; the garbled output was off by hundreds
static constexpr int TOLERANCE = 5;
static bool decode(RuntimeImage &img) {
std::vector<uint8_t> data(JPEG_GRAY, JPEG_GRAY + sizeof(JPEG_GRAY));
if (!img.begin_decode(data.size()))
return false;
if (img.feed_data(data.data(), data.size()) != static_cast<int>(data.size()))
return false;
return img.end_decode();
}
// A grayscale JPEG decoded for a colour image goes through JPEGDEC's RGB8888 output, which used to be garbled
TEST(JpegGrayscale, DecodesToRgb) {
RuntimeImage img(JPEG, image::IMAGE_TYPE_RGB, image::TRANSPARENCY_ALPHA_CHANNEL);
ASSERT_TRUE(decode(img));
ASSERT_EQ(img.get_width(), 16);
ASSERT_EQ(img.get_height(), 8);
const uint8_t *data = img.get_data_start();
for (int i = 0; i < 16 * 8; i++) {
const uint8_t *pixel = data + i * 4; // blue, green, red, alpha
EXPECT_NEAR(pixel[0], JPEG_GRAY_PIXELS[i], TOLERANCE) << "blue of pixel " << i;
EXPECT_NEAR(pixel[1], JPEG_GRAY_PIXELS[i], TOLERANCE) << "green of pixel " << i;
EXPECT_NEAR(pixel[2], JPEG_GRAY_PIXELS[i], TOLERANCE) << "red of pixel " << i;
EXPECT_EQ(pixel[3], 255) << "alpha of pixel " << i;
}
}
TEST(JpegGrayscale, DecodesToGrayscale) {
RuntimeImage img(JPEG, image::IMAGE_TYPE_GRAYSCALE, image::TRANSPARENCY_OPAQUE);
ASSERT_TRUE(decode(img));
const uint8_t *data = img.get_data_start();
for (int i = 0; i < 16 * 8; i++)
EXPECT_NEAR(data[i], JPEG_GRAY_PIXELS[i], TOLERANCE) << "pixel " << i;
}
} // namespace esphome::runtime_image::testing
#endif // USE_RUNTIME_IMAGE_JPEG
@@ -0,0 +1,434 @@
#include <gtest/gtest.h>
#include <algorithm>
#include <cstring>
#include <vector>
#include "esphome/components/runtime_image/image_decoder.h"
#include "esphome/components/runtime_image/runtime_image.h"
#ifdef USE_RUNTIME_IMAGE_JPEG
namespace esphome::runtime_image::testing {
// 48x40 4:2:0 JPEG of a noisy gradient (quality 95): three rows of 16 pixel MCUs, so a stepped decode takes
// three steps. At 2344 bytes it is larger than JPEGDEC's 2048 byte read buffer, so the source is read again
// part way through the image.
static const uint8_t JPEG_ROWS[] = {
0xFF, 0xD8, 0xFF, 0xE0, 0x00, 0x10, 0x4A, 0x46, 0x49, 0x46, 0x00, 0x01, 0x01, 0x00, 0x00, 0x01, 0x00, 0x01, 0x00,
0x00, 0xFF, 0xDB, 0x00, 0x43, 0x00, 0x02, 0x01, 0x01, 0x01, 0x01, 0x01, 0x02, 0x01, 0x01, 0x01, 0x02, 0x02, 0x02,
0x02, 0x02, 0x04, 0x03, 0x02, 0x02, 0x02, 0x02, 0x05, 0x04, 0x04, 0x03, 0x04, 0x06, 0x05, 0x06, 0x06, 0x06, 0x05,
0x06, 0x06, 0x06, 0x07, 0x09, 0x08, 0x06, 0x07, 0x09, 0x07, 0x06, 0x06, 0x08, 0x0B, 0x08, 0x09, 0x0A, 0x0A, 0x0A,
0x0A, 0x0A, 0x06, 0x08, 0x0B, 0x0C, 0x0B, 0x0A, 0x0C, 0x09, 0x0A, 0x0A, 0x0A, 0xFF, 0xDB, 0x00, 0x43, 0x01, 0x02,
0x02, 0x02, 0x02, 0x02, 0x02, 0x05, 0x03, 0x03, 0x05, 0x0A, 0x07, 0x06, 0x07, 0x0A, 0x0A, 0x0A, 0x0A, 0x0A, 0x0A,
0x0A, 0x0A, 0x0A, 0x0A, 0x0A, 0x0A, 0x0A, 0x0A, 0x0A, 0x0A, 0x0A, 0x0A, 0x0A, 0x0A, 0x0A, 0x0A, 0x0A, 0x0A, 0x0A,
0x0A, 0x0A, 0x0A, 0x0A, 0x0A, 0x0A, 0x0A, 0x0A, 0x0A, 0x0A, 0x0A, 0x0A, 0x0A, 0x0A, 0x0A, 0x0A, 0x0A, 0x0A, 0x0A,
0x0A, 0x0A, 0x0A, 0x0A, 0x0A, 0x0A, 0xFF, 0xC0, 0x00, 0x11, 0x08, 0x00, 0x28, 0x00, 0x30, 0x03, 0x01, 0x22, 0x00,
0x02, 0x11, 0x01, 0x03, 0x11, 0x01, 0xFF, 0xC4, 0x00, 0x1F, 0x00, 0x00, 0x01, 0x05, 0x01, 0x01, 0x01, 0x01, 0x01,
0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A,
0x0B, 0xFF, 0xC4, 0x00, 0xB5, 0x10, 0x00, 0x02, 0x01, 0x03, 0x03, 0x02, 0x04, 0x03, 0x05, 0x05, 0x04, 0x04, 0x00,
0x00, 0x01, 0x7D, 0x01, 0x02, 0x03, 0x00, 0x04, 0x11, 0x05, 0x12, 0x21, 0x31, 0x41, 0x06, 0x13, 0x51, 0x61, 0x07,
0x22, 0x71, 0x14, 0x32, 0x81, 0x91, 0xA1, 0x08, 0x23, 0x42, 0xB1, 0xC1, 0x15, 0x52, 0xD1, 0xF0, 0x24, 0x33, 0x62,
0x72, 0x82, 0x09, 0x0A, 0x16, 0x17, 0x18, 0x19, 0x1A, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2A, 0x34, 0x35, 0x36, 0x37,
0x38, 0x39, 0x3A, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0x4A, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59, 0x5A,
0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 0x6A, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79, 0x7A, 0x83, 0x84, 0x85,
0x86, 0x87, 0x88, 0x89, 0x8A, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98, 0x99, 0x9A, 0xA2, 0xA3, 0xA4, 0xA5, 0xA6,
0xA7, 0xA8, 0xA9, 0xAA, 0xB2, 0xB3, 0xB4, 0xB5, 0xB6, 0xB7, 0xB8, 0xB9, 0xBA, 0xC2, 0xC3, 0xC4, 0xC5, 0xC6, 0xC7,
0xC8, 0xC9, 0xCA, 0xD2, 0xD3, 0xD4, 0xD5, 0xD6, 0xD7, 0xD8, 0xD9, 0xDA, 0xE1, 0xE2, 0xE3, 0xE4, 0xE5, 0xE6, 0xE7,
0xE8, 0xE9, 0xEA, 0xF1, 0xF2, 0xF3, 0xF4, 0xF5, 0xF6, 0xF7, 0xF8, 0xF9, 0xFA, 0xFF, 0xC4, 0x00, 0x1F, 0x01, 0x00,
0x03, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x02, 0x03,
0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0xFF, 0xC4, 0x00, 0xB5, 0x11, 0x00, 0x02, 0x01, 0x02, 0x04, 0x04,
0x03, 0x04, 0x07, 0x05, 0x04, 0x04, 0x00, 0x01, 0x02, 0x77, 0x00, 0x01, 0x02, 0x03, 0x11, 0x04, 0x05, 0x21, 0x31,
0x06, 0x12, 0x41, 0x51, 0x07, 0x61, 0x71, 0x13, 0x22, 0x32, 0x81, 0x08, 0x14, 0x42, 0x91, 0xA1, 0xB1, 0xC1, 0x09,
0x23, 0x33, 0x52, 0xF0, 0x15, 0x62, 0x72, 0xD1, 0x0A, 0x16, 0x24, 0x34, 0xE1, 0x25, 0xF1, 0x17, 0x18, 0x19, 0x1A,
0x26, 0x27, 0x28, 0x29, 0x2A, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3A, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0x4A,
0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59, 0x5A, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 0x6A, 0x73, 0x74, 0x75,
0x76, 0x77, 0x78, 0x79, 0x7A, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87, 0x88, 0x89, 0x8A, 0x92, 0x93, 0x94, 0x95, 0x96,
0x97, 0x98, 0x99, 0x9A, 0xA2, 0xA3, 0xA4, 0xA5, 0xA6, 0xA7, 0xA8, 0xA9, 0xAA, 0xB2, 0xB3, 0xB4, 0xB5, 0xB6, 0xB7,
0xB8, 0xB9, 0xBA, 0xC2, 0xC3, 0xC4, 0xC5, 0xC6, 0xC7, 0xC8, 0xC9, 0xCA, 0xD2, 0xD3, 0xD4, 0xD5, 0xD6, 0xD7, 0xD8,
0xD9, 0xDA, 0xE2, 0xE3, 0xE4, 0xE5, 0xE6, 0xE7, 0xE8, 0xE9, 0xEA, 0xF2, 0xF3, 0xF4, 0xF5, 0xF6, 0xF7, 0xF8, 0xF9,
0xFA, 0xFF,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x0E, 0x00, 0xC8, 0xC8, 0xEA, 0xEE, 0x12, 0x7B, 0x09,
0x6C, 0x6E, 0xCE, 0xA2, 0x0B, 0xAA, 0x32, 0xD9, 0xB9, 0x49, 0x66, 0xDF, 0x74, 0xCC, 0x31, 0x1B, 0xE4, 0xE6, 0x63,
0xF2, 0x91, 0x95, 0x42, 0x15, 0x8B, 0x7D, 0xE0, 0xAC, 0xB4, 0x51, 0x5F, 0x33, 0x3A, 0xD8, 0x8C, 0x46, 0x7A, 0x9C,
0xA6, 0xD7, 0x2C, 0x13, 0x49, 0x25, 0x6E, 0x6A, 0x73, 0xE4, 0x84, 0x9E, 0x9A, 0xC9, 0x29, 0x24, 0x9E, 0xE9, 0x42,
0x11, 0x8D, 0xA2, 0x9C, 0x5F, 0xF5, 0x37, 0x0D, 0x36, 0xF8, 0x7E, 0x9A, 0xEC, 0xE5, 0xE5, 0x7E, 0x67, 0x77, 0x7B,
0x7A, 0xBF, 0x5E, 0xB7, 0x3F, 0xFF, 0xD9,
};
// 8x8 progressive JPEG, 521 bytes
static const uint8_t JPEG_PROGRESSIVE[] = {
0xFF, 0xD8, 0xFF, 0xE0, 0x00, 0x10, 0x4A, 0x46, 0x49, 0x46, 0x00, 0x01, 0x01, 0x00, 0x00, 0x01, 0x00, 0x01, 0x00,
0x00, 0xFF, 0xDB, 0x00, 0x43, 0x00, 0x10, 0x0B, 0x0C, 0x0E, 0x0C, 0x0A, 0x10, 0x0E, 0x0D, 0x0E, 0x12, 0x11, 0x10,
0x13, 0x18, 0x28, 0x1A, 0x18, 0x16, 0x16, 0x18, 0x31, 0x23, 0x25, 0x1D, 0x28, 0x3A, 0x33, 0x3D, 0x3C, 0x39, 0x33,
0x38, 0x37, 0x40, 0x48, 0x5C, 0x4E, 0x40, 0x44, 0x57, 0x45, 0x37, 0x38, 0x50, 0x6D, 0x51, 0x57, 0x5F, 0x62, 0x67,
0x68, 0x67, 0x3E, 0x4D, 0x71, 0x79, 0x70, 0x64, 0x78, 0x5C, 0x65, 0x67, 0x63, 0xFF, 0xDB, 0x00, 0x43, 0x01, 0x11,
0x12, 0x12, 0x18, 0x15, 0x18, 0x2F, 0x1A, 0x1A, 0x2F, 0x63, 0x42, 0x38, 0x42, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63,
0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63,
0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63,
0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0xFF, 0xC2, 0x00, 0x11, 0x08, 0x00, 0x08, 0x00, 0x08, 0x03, 0x01, 0x22, 0x00,
0x02, 0x11, 0x01, 0x03, 0x11, 0x01, 0xFF, 0xC4, 0x00, 0x15, 0x00, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x04, 0xFF, 0xC4, 0x00, 0x15, 0x01, 0x01, 0x01, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0x05, 0xFF, 0xDA, 0x00, 0x0C, 0x03,
0x01, 0x00, 0x02, 0x10, 0x03, 0x10, 0x00, 0x00, 0x01, 0x94, 0x05, 0x3F, 0xFF, 0xC4, 0x00, 0x14, 0x10, 0x01, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFF, 0xDA, 0x00, 0x08,
0x01, 0x01, 0x00, 0x01, 0x05, 0x02, 0x7F, 0xFF, 0xC4, 0x00, 0x14, 0x11, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFF, 0xDA, 0x00, 0x08, 0x01, 0x03, 0x01, 0x01, 0x3F,
0x01, 0x7F, 0xFF, 0xC4, 0x00, 0x14, 0x11, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0xFF, 0xDA, 0x00, 0x08, 0x01, 0x02, 0x01, 0x01, 0x3F, 0x01, 0x7F, 0xFF, 0xC4, 0x00,
0x14, 0x10, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0xFF, 0xDA, 0x00, 0x08, 0x01, 0x01, 0x00, 0x06, 0x3F, 0x02, 0x7F, 0xFF, 0xC4, 0x00, 0x14, 0x10, 0x01, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFF, 0xDA, 0x00, 0x08, 0x01,
0x01, 0x00, 0x01, 0x3F, 0x21, 0x7F, 0xFF, 0xDA, 0x00, 0x0C, 0x03, 0x01, 0x00, 0x02, 0x00, 0x03, 0x00, 0x00, 0x00,
0x10, 0x0B, 0xFF, 0xC4, 0x00, 0x14, 0x11, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0xFF, 0xDA, 0x00, 0x08, 0x01, 0x03, 0x01, 0x01, 0x3F, 0x10, 0x7F, 0xFF, 0xC4, 0x00,
0x14, 0x11, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0xFF, 0xDA, 0x00, 0x08, 0x01, 0x02, 0x01, 0x01, 0x3F, 0x10, 0x7F, 0xFF, 0xC4, 0x00, 0x14, 0x10, 0x01, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFF, 0xDA, 0x00, 0x08, 0x01,
0x01, 0x00, 0x01, 0x3F, 0x10, 0x7F, 0xFF, 0xD9,
};
class SourceImage : public RuntimeImage {
public:
explicit SourceImage(image::ImageType type = image::IMAGE_TYPE_RGB, int width = 0, int height = 0)
: RuntimeImage(JPEG, type, image::TRANSPARENCY_OPAQUE, nullptr, false, width, height) {}
size_t decoded_bytes() const { return this->decoded_bytes_; }
};
/// The file in memory; fails reads once fail_at bytes have been read
class MemorySource : public DecodeSource {
public:
MemorySource(const uint8_t *data, size_t size, size_t fail_at = SIZE_MAX)
: data_(data), size_(size), fail_at_(fail_at) {}
int32_t read(uint8_t *buf, uint32_t len) override {
if (this->pos_ >= this->fail_at_)
return -1;
const size_t n = std::min<size_t>(len, this->size_ - this->pos_);
memcpy(buf, this->data_ + this->pos_, n);
this->pos_ += n;
this->reads++;
return static_cast<int32_t>(n);
}
bool seek(uint32_t position) override {
if (position > this->size_)
return false;
this->pos_ = position;
return true;
}
int reads{0};
protected:
const uint8_t *data_;
size_t size_;
size_t pos_{0};
size_t fail_at_;
};
/// Hands out only the bytes delivered so far, like a download that is still arriving
class TrickleSource : public DecodeSource {
public:
TrickleSource(const uint8_t *data, size_t size, size_t delivered) : data_(data), size_(size), delivered_(delivered) {}
int32_t read(uint8_t *buf, uint32_t len) override {
const size_t n = std::min<size_t>(len, this->delivered_ - this->pos_);
memcpy(buf, this->data_ + this->pos_, n);
this->pos_ += n;
return static_cast<int32_t>(n);
}
bool seek(uint32_t position) override {
if (position > this->delivered_)
return false;
this->pos_ = position;
return true;
}
void deliver(size_t len) { this->delivered_ = std::min(this->size_, this->delivered_ + len); }
protected:
const uint8_t *data_;
size_t size_;
size_t delivered_;
size_t pos_{0};
};
static bool decode_fed(RuntimeImage &img) {
std::vector<uint8_t> buffer(JPEG_ROWS, JPEG_ROWS + sizeof(JPEG_ROWS));
if (!img.begin_decode(buffer.size()))
return false;
if (img.feed_data(buffer.data(), buffer.size()) != static_cast<int>(buffer.size()))
return false;
return img.end_decode();
}
/// Decodes from a source a step at a time; returns the last result and counts the steps
static int decode_stepped(RuntimeImage &img, DecodeSource &source, int &steps) {
steps = 0;
if (!img.begin_decode(sizeof(JPEG_ROWS)) || !img.decoder_supports_source())
return DECODE_ERROR_UNSUPPORTED_FORMAT;
int result = img.begin_source(&source);
if (result != 0)
return result;
do {
result = img.decode_step();
steps++;
} while (result == DECODE_STEP_MORE);
if (result == DECODE_STEP_DONE && !img.end_decode())
return DECODE_ERROR_INTERNAL_DECODER_ERROR;
return result;
}
static std::vector<uint8_t> pixels(const RuntimeImage &img) {
const uint8_t *start = img.get_data_start();
return std::vector<uint8_t>(start, start + img.get_width_stride() * img.get_height());
}
TEST(JpegSourceDecode, SteppedDecodeMatchesFedDecode) {
SourceImage fed;
ASSERT_TRUE(decode_fed(fed));
SourceImage stepped;
MemorySource source(JPEG_ROWS, sizeof(JPEG_ROWS));
int steps;
ASSERT_EQ(decode_stepped(stepped, source, steps), DECODE_STEP_DONE);
EXPECT_EQ(steps, 3);
ASSERT_EQ(stepped.get_width(), 48);
ASSERT_EQ(stepped.get_height(), 40);
EXPECT_EQ(pixels(stepped), pixels(fed));
}
TEST(JpegSourceDecode, SteppedDirectRgb565MatchesFedDecode) {
// A fixed size that is not a whole multiple of the source exercises the scaling tables across steps
SourceImage fed(image::IMAGE_TYPE_RGB565, 30, 25);
ASSERT_TRUE(decode_fed(fed));
SourceImage stepped(image::IMAGE_TYPE_RGB565, 30, 25);
MemorySource source(JPEG_ROWS, sizeof(JPEG_ROWS));
int steps;
ASSERT_EQ(decode_stepped(stepped, source, steps), DECODE_STEP_DONE);
EXPECT_EQ(pixels(stepped), pixels(fed));
}
TEST(JpegSourceDecode, WaitsForDataThatHasNotArrived) {
SourceImage fed;
ASSERT_TRUE(decode_fed(fed));
// Enough for the headers (the scan starts at byte 609), then 50 bytes between steps
SourceImage img;
TrickleSource source(JPEG_ROWS, sizeof(JPEG_ROWS), 700);
ASSERT_TRUE(img.begin_decode(sizeof(JPEG_ROWS)));
ASSERT_EQ(img.begin_source(&source), 0);
int waits = 0;
int result;
while ((result = img.decode_step()) == DECODE_STEP_MORE || result == DECODE_STEP_NEED_DATA) {
if (result == DECODE_STEP_NEED_DATA)
waits++;
source.deliver(50);
ASSERT_LT(waits, 1000);
}
ASSERT_EQ(result, DECODE_STEP_DONE);
ASSERT_TRUE(img.end_decode());
EXPECT_GT(waits, 0);
EXPECT_EQ(pixels(img), pixels(fed));
}
TEST(JpegSourceDecode, ImageIsHiddenUntilTheLastStep) {
SourceImage img;
MemorySource source(JPEG_ROWS, sizeof(JPEG_ROWS));
ASSERT_TRUE(img.begin_decode(sizeof(JPEG_ROWS)));
ASSERT_EQ(img.begin_source(&source), 0);
EXPECT_EQ(img.decode_step(), DECODE_STEP_MORE);
EXPECT_EQ(img.get_width(), 0);
EXPECT_EQ(img.get_data_start(), nullptr);
}
TEST(JpegSourceDecode, ReadFailureIsReportedAndTheDecoderRecovers) {
SourceImage img;
// The first read fills JPEGDEC's buffer, so the failure comes part way through the image data
MemorySource failing(JPEG_ROWS, sizeof(JPEG_ROWS), 2048);
int steps;
EXPECT_EQ(decode_stepped(img, failing, steps), DECODE_ERROR_READ);
EXPECT_GE(steps, 1);
img.release();
MemorySource source(JPEG_ROWS, sizeof(JPEG_ROWS));
EXPECT_EQ(decode_stepped(img, source, steps), DECODE_STEP_DONE);
}
TEST(JpegSourceDecode, ReleasePartWayThroughLetsTheNextDecodeStart) {
SourceImage img;
MemorySource first(JPEG_ROWS, sizeof(JPEG_ROWS));
ASSERT_TRUE(img.begin_decode(sizeof(JPEG_ROWS)));
ASSERT_EQ(img.begin_source(&first), 0);
ASSERT_EQ(img.decode_step(), DECODE_STEP_MORE);
img.release();
EXPECT_FALSE(img.is_decoding());
MemorySource second(JPEG_ROWS, sizeof(JPEG_ROWS));
int steps;
EXPECT_EQ(decode_stepped(img, second, steps), DECODE_STEP_DONE);
EXPECT_EQ(steps, 3);
}
TEST(JpegSourceDecode, FailedFirstReadIsAReadError) {
SourceImage img;
MemorySource failing(JPEG_ROWS, sizeof(JPEG_ROWS), 0);
ASSERT_TRUE(img.begin_decode(sizeof(JPEG_ROWS)));
EXPECT_EQ(img.begin_source(&failing), DECODE_ERROR_READ);
}
TEST(JpegSourceDecode, FailedSeekToTheStartIsAReadError) {
class NoSeekSource : public MemorySource {
public:
using MemorySource::MemorySource;
bool seek(uint32_t position) override { return false; }
};
SourceImage img;
NoSeekSource source(JPEG_ROWS, sizeof(JPEG_ROWS));
ASSERT_TRUE(img.begin_decode(sizeof(JPEG_ROWS)));
EXPECT_EQ(img.begin_source(&source), DECODE_ERROR_READ);
EXPECT_EQ(img.decode_step(), DECODE_ERROR_INVALID_TYPE);
}
TEST(JpegSourceDecode, ProgressiveImageIsUnsupported) {
SourceImage img;
MemorySource source(JPEG_PROGRESSIVE, sizeof(JPEG_PROGRESSIVE));
ASSERT_TRUE(img.begin_decode(sizeof(JPEG_PROGRESSIVE)));
EXPECT_EQ(img.begin_source(&source), DECODE_ERROR_UNSUPPORTED_FORMAT);
EXPECT_EQ(img.decode_step(), DECODE_ERROR_INVALID_TYPE);
}
TEST(JpegSourceDecode, CompletedDecodeCountsTheWholeImage) {
SourceImage img;
MemorySource source(JPEG_ROWS, sizeof(JPEG_ROWS));
ASSERT_TRUE(img.begin_decode(sizeof(JPEG_ROWS)));
ASSERT_EQ(img.begin_source(&source), 0);
int result;
while ((result = img.decode_step()) == DECODE_STEP_MORE) {
EXPECT_EQ(img.decoded_bytes(), 0u);
}
ASSERT_EQ(result, DECODE_STEP_DONE);
EXPECT_EQ(img.decoded_bytes(), sizeof(JPEG_ROWS));
}
TEST(JpegSourceDecode, SourceIsReadAsTheDecodeNeedsIt) {
SourceImage img;
MemorySource source(JPEG_ROWS, sizeof(JPEG_ROWS));
int steps;
ASSERT_EQ(decode_stepped(img, source, steps), DECODE_STEP_DONE);
EXPECT_GE(source.reads, 2);
}
TEST(JpegSourceDecode, DecodersThatCannotReadASourceSaySo) {
RuntimeImage img(BMP, image::IMAGE_TYPE_RGB, image::TRANSPARENCY_OPAQUE);
ASSERT_TRUE(img.begin_decode(100));
EXPECT_FALSE(img.decoder_supports_source());
MemorySource source(JPEG_ROWS, sizeof(JPEG_ROWS));
EXPECT_EQ(img.begin_source(&source), DECODE_ERROR_UNSUPPORTED_FORMAT);
EXPECT_EQ(img.decode_step(), DECODE_ERROR_UNSUPPORTED_FORMAT);
}
TEST(JpegSourceDecode, SourceNeedsTheImageSize) {
SourceImage img;
MemorySource source(JPEG_ROWS, sizeof(JPEG_ROWS));
// JPEGDEC needs the file size up front, and holds it in an int
ASSERT_TRUE(img.begin_decode(0));
EXPECT_EQ(img.begin_source(&source), DECODE_ERROR_INVALID_TYPE);
img.release();
ASSERT_TRUE(img.begin_decode(static_cast<size_t>(INT32_MAX) + 1));
EXPECT_EQ(img.begin_source(&source), DECODE_ERROR_INVALID_TYPE);
}
TEST(JpegSourceDecode, StepWithoutADecodeIsRefused) {
SourceImage img;
EXPECT_EQ(img.decode_step(), DECODE_ERROR_INVALID_TYPE);
ASSERT_TRUE(img.begin_decode(sizeof(JPEG_ROWS)));
// begin_source() was never called
EXPECT_EQ(img.decode_step(), DECODE_ERROR_INVALID_TYPE);
}
} // namespace esphome::runtime_image::testing
#endif // USE_RUNTIME_IMAGE_JPEG
+2 -1
View File
@@ -35,7 +35,8 @@ media_player:
announcement_volume: 60%
announcement_pipeline:
speaker: announcement_mixer_speaker_id
format: FLAC
format: MP3
bitrate: 48
num_channels: 1
sources:
- announcement_audio_file_source
+15
View File
@@ -0,0 +1,15 @@
# This file's presence makes pytest treat this directory as a package named
# "vnc"; required for cpp unit testing.
from esphome.components import socket as socket_component
from esphome.types import ConfigType
from tests.testing_helpers import ComponentManifestOverride
def override_manifest(manifest: ComponentManifestOverride) -> None:
# MULTI_CONF components get no config in a unit test build, so to_code would never run.
manifest.multi_conf = False
async def to_code_testing(config: ConfigType) -> None:
socket_component.require_tcp_client_link()
manifest.to_code = to_code_testing
+27
View File
@@ -0,0 +1,27 @@
vnc:
- id: vnc_client
display_id: test_display_screen
host: 192.0.2.10
port: 5901
reconnect_interval: 10s
update_delay: 300ms
max_update_delay: 2s
on_connect:
- logger.log: VNC connected
on_disconnect:
- logger.log: VNC disconnected
interval:
- interval: 60s
then:
- if:
condition:
vnc.is_connected: vnc_client
then:
- logger.log: VNC is connected
- if:
condition:
vnc.is_connected:
id: vnc_client
then:
- logger.log: VNC is still connected
@@ -0,0 +1,7 @@
packages:
test_display: !include ../../test_build_components/common/test_display/test_display.yaml
vnc: !include common.yaml
wifi:
ssid: MySSID
password: password1
+7
View File
@@ -0,0 +1,7 @@
packages:
test_display: !include ../../test_build_components/common/test_display/test_display.yaml
vnc: !include common.yaml
wifi:
ssid: MySSID
password: password1
@@ -0,0 +1,7 @@
packages:
test_display: !include ../../test_build_components/common/test_display/test_display.yaml
vnc: !include common.yaml
wifi:
ssid: MySSID
password: password1
+3
View File
@@ -0,0 +1,3 @@
packages:
test_display: !include ../../test_build_components/common/test_display/test_display.yaml
vnc: !include common.yaml
@@ -0,0 +1,7 @@
packages:
test_display: !include ../../test_build_components/common/test_display/test_display.yaml
vnc: !include common.yaml
wifi:
ssid: MySSID
password: password1
+164
View File
@@ -0,0 +1,164 @@
#include "vnc_test_helpers.h"
#ifdef USE_HOST
namespace esphome::vnc::testing {
/// Runs one pass at time t, then lets an asynchronous connect finish before time moves on.
static void settle_at(Session &s, uint32_t t) {
set_now(t);
s.loop();
for (int i = 0; i < 10000 && s.client.link().connecting(); i++) {
usleep(100);
s.loop();
}
}
/// Times at which refused connect attempts ended, starting at the current time.
static std::vector<uint32_t> refused_attempts(Session &s, size_t count, uint32_t step = 100) {
std::vector<uint32_t> times;
uint32_t last_start = s.client.hold_start_ms();
const uint32_t start = now_ms();
for (uint32_t t = start; times.size() < count && t - start < 3600000; t += step) {
settle_at(s, t);
if (s.client.hold_start_ms() != last_start) {
last_start = s.client.hold_start_ms();
times.push_back(t);
}
}
return times;
}
static std::vector<uint32_t> gaps(const std::vector<uint32_t> &times) {
std::vector<uint32_t> out;
for (size_t i = 1; i < times.size(); i++) {
out.push_back(times[i] - times[i - 1]);
}
return out;
}
TEST(VncBackoff, RefusedConnectsBackOff) {
FakeDisplay d;
Session s(&d);
auto times = refused_attempts(s, 7);
ASSERT_EQ(times.size(), 7u);
EXPECT_EQ(times[0], 1000u);
EXPECT_EQ(gaps(times), std::vector<uint32_t>({5000, 10000, 20000, 40000, 60000, 60000}));
EXPECT_EQ(s.client.state(), VncState::VNC_STATE_IDLE);
EXPECT_TRUE(s.client.status_has_warning());
EXPECT_FALSE(s.client.status_has_error());
}
TEST(VncBackoff, ShortReconnectInterval) {
FakeDisplay d;
Session s(&d);
s.client.set_reconnect_interval(1000);
auto times = refused_attempts(s, 9);
EXPECT_EQ(gaps(times), std::vector<uint32_t>({1000, 2000, 4000, 8000, 16000, 32000, 60000, 60000}));
}
TEST(VncBackoff, ReconnectIntervalAboveTheCap) {
FakeDisplay d;
Session s(&d);
s.client.set_reconnect_interval(90000);
auto times = refused_attempts(s, 4, 1000);
EXPECT_EQ(gaps(times), std::vector<uint32_t>({90000, 90000, 90000}));
}
TEST(VncBackoff, StableSessionResetsBackoff) {
FakeDisplay d;
Session s(&d);
auto times = refused_attempts(s, 3);
ASSERT_EQ(s.client.backoff_ms(), 20000u);
set_now(times.back() + 1000);
s.start(64, 48);
s.run_for(60000, 1000);
ASSERT_TRUE(s.client.is_connected());
int disconnects = 0;
s.client.add_on_disconnect_callback([&disconnects]() { disconnects++; });
s.close_peer();
s.loop();
EXPECT_EQ(disconnects, 1);
EXPECT_EQ(s.client.state(), VncState::VNC_STATE_IDLE);
EXPECT_EQ(s.client.backoff_ms(), 0u);
EXPECT_EQ(s.client.hold_ms(), 0u);
const uint32_t dropped_at = now_ms();
auto after = refused_attempts(s, 2);
ASSERT_EQ(after.size(), 2u);
// The link's own interval spaces the first retry; the backoff then starts over.
EXPECT_EQ(after[0] - dropped_at, 5000u);
EXPECT_EQ(after[1] - after[0], 5000u);
}
TEST(VncBackoff, ShortSessionKeepsEscalating) {
FakeDisplay d;
Session s(&d);
auto times = refused_attempts(s, 3);
ASSERT_EQ(s.client.backoff_ms(), 20000u);
set_now(times.back() + 1000);
s.start(64, 48);
s.run_for(10000, 1000);
s.close_peer();
s.loop();
EXPECT_EQ(s.client.state(), VncState::VNC_STATE_IDLE);
EXPECT_EQ(s.client.hold_ms(), 40000u);
}
TEST(VncBackoff, PeerCloseWhileRunningRetries) {
FakeDisplay d;
Session s(&d);
int disconnects = 0;
s.client.add_on_disconnect_callback([&disconnects]() { disconnects++; });
s.start(64, 48);
s.close_peer();
s.loop();
EXPECT_FALSE(s.client.is_connected());
EXPECT_EQ(disconnects, 1);
EXPECT_EQ(s.client.hold_ms(), 5000u);
EXPECT_TRUE(s.client.status_has_warning());
const uint32_t dropped_at = now_ms();
auto after = refused_attempts(s, 1);
ASSERT_EQ(after.size(), 1u);
EXPECT_EQ(after[0] - dropped_at, 5000u);
}
TEST(VncBackoff, NextSessionStartsClean) {
FakeDisplay d;
Session s(&d);
s.start(64, 48);
set_now(now_ms() + 100);
// Leave a frame half parsed and a refresh pending, then lose the link.
s.deliver(Rfb().fbu(1).raw_rect(0, 0, 2, 1, {1, 2}));
s.deliver(Rfb().fbu(1).rect(0, 0, 4, 4, 0).u8(0x02));
ASSERT_TRUE(s.client.in_update());
s.close_peer();
s.loop();
EXPECT_FALSE(s.client.in_update());
EXPECT_FALSE(s.client.refresh_pending());
s.attach();
EXPECT_EQ(s.client.step(), VncStep::VNC_STEP_VERSION);
EXPECT_EQ(s.client.rx_buffered(), 0);
s.deliver(handshake_38(64, 48));
ASSERT_TRUE(s.client.is_connected());
s.take();
set_now(now_ms() + 100);
s.deliver(Rfb().fbu(1).raw_rect(3, 3, 1, 1, {0x7777}));
EXPECT_EQ(d.pixel(3, 3), expect_px(0x7777, 2));
EXPECT_EQ(hex(s.take()), hex(fbur(true, 64, 48)));
}
TEST(VncBackoff, ShutdownClosesTheLink) {
FakeDisplay d;
Session s(&d);
s.start(64, 48);
s.client.on_shutdown();
EXPECT_FALSE(s.client.link().connected());
}
} // namespace esphome::vnc::testing
#endif // USE_HOST
+586
View File
@@ -0,0 +1,586 @@
#include "vnc_test_helpers.h"
#include <random>
#ifdef USE_HOST
namespace esphome::vnc::testing {
static constexpr int32_t RAW = 0;
static std::vector<uint16_t> pattern(size_t count, uint16_t seed) {
std::vector<uint16_t> out(count);
for (size_t i = 0; i < count; i++) {
out[i] = static_cast<uint16_t>(seed + i * 0x0841u);
}
return out;
}
/// Checks that a w x h block at x,y shows the given wire pixels, row-major.
static void expect_block(const FakeDisplay &d, int x, int y, int w, int h, const std::vector<uint16_t> &px,
uint8_t bpp) {
for (int row = 0; row < h; row++) {
for (int col = 0; col < w; col++) {
ASSERT_EQ(d.pixel(x + col, y + row), expect_px(px[row * w + col], bpp)) << "at " << x + col << "," << y + row;
}
}
}
static void expect_solid(const FakeDisplay &d, int x, int y, int w, int h, uint16_t px, uint8_t bpp) {
expect_block(d, x, y, w, h, std::vector<uint16_t>(static_cast<size_t>(w) * h, px), bpp);
}
class VncDecode : public ::testing::Test {
protected:
void start(FakeDisplay &display, uint16_t fb_w, uint16_t fb_h) {
this->session_ = std::make_unique<Session>(&display);
this->session_->start(fb_w, fb_h);
// Past the 50 ms request spacing, so the next request goes out as soon as a frame ends.
set_now(now_ms() + 100);
}
Session &s() { return *this->session_; }
std::unique_ptr<Session> session_;
};
TEST_F(VncDecode, Raw16bpp) {
FakeDisplay d(64, 48);
this->start(d, 64, 48);
auto px = pattern(12, 0x1234);
s().deliver(Rfb().fbu(1).raw_rect(3, 2, 4, 3, px));
expect_block(d, 3, 2, 4, 3, px, 2);
EXPECT_EQ(d.drawn_count(), 12u);
EXPECT_EQ(hex(s().take()), hex(fbur(true, 64, 48)));
EXPECT_EQ(d.updates(), 0);
s().run_for(300);
EXPECT_EQ(d.updates(), 1);
}
TEST_F(VncDecode, Raw8bpp) {
FakeDisplay d(64, 48, display::DISPLAY_TYPE_BINARY);
this->start(d, 64, 48);
std::vector<uint16_t> px = {0x00, 0xFF, 0xE0, 0x1C, 0x03, 0x92, 0x49, 0x24, 0xB6, 0x6D};
s().deliver(Rfb().fbu(1).raw_rect(10, 20, 5, 2, px, 1));
expect_block(d, 10, 20, 5, 2, px, 1);
EXPECT_EQ(d.drawn_count(), 10u);
EXPECT_TRUE(s().client.is_connected());
}
TEST_F(VncDecode, RawSeveralRectsInOneUpdate) {
FakeDisplay d(64, 48);
this->start(d, 64, 48);
auto a = pattern(6, 0x0101);
auto b = pattern(4, 0xF00F);
s().deliver(Rfb().fbu(2).raw_rect(0, 0, 3, 2, a).raw_rect(60, 46, 4, 1, b));
expect_block(d, 0, 0, 3, 2, a, 2);
expect_block(d, 60, 46, 4, 1, b, 2);
EXPECT_EQ(hex(s().take()), hex(fbur(true, 64, 48)));
}
TEST_F(VncDecode, EmptyUpdateRequestsAgainWithoutRefresh) {
FakeDisplay d(64, 48);
this->start(d, 64, 48);
s().deliver(Rfb().fbu(0));
EXPECT_EQ(hex(s().take()), hex(fbur(true, 64, 48)));
s().run_for(2000);
EXPECT_EQ(d.updates(), 0);
}
TEST_F(VncDecode, RichCursorIsSkippedAndNeverRefreshes) {
FakeDisplay d(64, 48);
this->start(d, 64, 48);
s().deliver(Rfb().fbu(1).cursor(3, 4, 5, 4));
EXPECT_EQ(d.draw_calls(), 0);
EXPECT_EQ(hex(s().take()), hex(fbur(true, 64, 48)));
s().run_for(3000);
EXPECT_EQ(d.updates(), 0);
EXPECT_TRUE(s().client.is_connected());
}
class VncCursorSkip : public ::testing::TestWithParam<display::DisplayType> {};
TEST_P(VncCursorSkip, PayloadLengthMatchesThePixelFormat) {
FakeDisplay d(64, 48, GetParam());
Session s(&d);
s.start(64, 48);
const uint8_t bpp = s.client.bpp();
// 9 wide needs a 2-byte mask row; the raw rect after it only lands right if the skip was exact.
s.deliver(Rfb().fbu(3).cursor(0, 0, 9, 3, bpp).raw_rect(7, 7, 2, 1, {0x00E0, 0x001C}, bpp).cursor(1, 1, 0, 0, bpp));
EXPECT_EQ(d.pixel(7, 7), expect_px(0x00E0, bpp));
EXPECT_EQ(d.pixel(8, 7), expect_px(0x001C, bpp));
EXPECT_EQ(d.drawn_count(), 2u);
EXPECT_TRUE(s.client.is_connected());
EXPECT_FALSE(s.client.in_update());
}
INSTANTIATE_TEST_SUITE_P(Formats, VncCursorSkip,
::testing::Values(display::DISPLAY_TYPE_COLOR, display::DISPLAY_TYPE_BINARY));
TEST_F(VncDecode, ZeroSizeCursorEndsTheFrame) {
FakeDisplay d(64, 48);
this->start(d, 64, 48);
s().deliver(Rfb().fbu(1).cursor(0, 0, 0, 0));
EXPECT_FALSE(s().client.in_update());
EXPECT_EQ(hex(s().take()), hex(fbur(true, 64, 48)));
}
TEST_F(VncDecode, OversizedCursorReconnects) {
FakeDisplay d(64, 48);
this->start(d, 64, 48);
s().deliver(Rfb().fbu(1).rect(0, 0, 1025, 1, -239));
EXPECT_EQ(s().client.state(), VncState::VNC_STATE_IDLE);
}
TEST_F(VncDecode, LargeCutTextIsSkipped) {
FakeDisplay d(64, 48);
this->start(d, 64, 48);
s().deliver(Rfb().cut_text(200 * 1024).fbu(1).raw_rect(1, 1, 1, 1, {0x4321}));
EXPECT_TRUE(s().client.is_connected());
EXPECT_EQ(d.pixel(1, 1), expect_px(0x4321, 2));
EXPECT_EQ(d.drawn_count(), 1u);
}
TEST_F(VncDecode, BellAndColourMapAreSkipped) {
FakeDisplay d(64, 48);
this->start(d, 64, 48);
s().deliver(Rfb().bell().colour_map(5, 3).bell().colour_map(0, 0).fbu(1).raw_rect(2, 2, 1, 1, {0x0F0F}));
EXPECT_TRUE(s().client.is_connected());
EXPECT_EQ(d.pixel(2, 2), expect_px(0x0F0F, 2));
EXPECT_EQ(d.drawn_count(), 1u);
}
TEST(VncDecodeClip, ServerLargerThanDisplay) {
FakeDisplay d(64, 48);
Session s(&d);
s.attach();
s.deliver(handshake_38(100, 80));
ASSERT_TRUE(s.client.is_connected());
Bytes tx = s.take();
EXPECT_EQ(hex(Bytes(tx.end() - 10, tx.end())), hex(fbur(false, 64, 48)));
set_now(now_ms() + 100);
auto raw = pattern(90 * 70, 0x0021);
Rfb f;
f.fbu(3).raw_rect(5, 5, 90, 70, raw);
// A raw rect straddling both edges, and one entirely off screen.
f.raw_rect(56, 40, 20, 20, std::vector<uint16_t>(20 * 20, 0x5000));
f.raw_rect(70, 50, 16, 16, pattern(256, 1));
s.deliver(f);
EXPECT_EQ(d.out_of_bounds(), 0);
for (int y = 5; y < 40; y++) {
for (int x = 5; x < 56; x++) {
ASSERT_EQ(d.pixel(x, y), expect_px(raw[(y - 5) * 90 + (x - 5)], 2)) << x << "," << y;
}
}
expect_solid(d, 56, 40, 8, 8, 0x5000, 2);
EXPECT_EQ(d.pixel(4, 4), UNSET);
EXPECT_TRUE(s.client.is_connected());
EXPECT_EQ(hex(s.take()), hex(fbur(true, 64, 48)));
}
TEST(VncDecodeClip, ServerSmallerThanDisplay) {
FakeDisplay d(64, 48);
Session s(&d);
s.start(20, 10);
EXPECT_EQ(s.client.vis_w(), 20);
EXPECT_EQ(s.client.vis_h(), 10);
s.deliver(Rfb().fbu(1).raw_rect(19, 9, 1, 1, {0x1111}));
EXPECT_EQ(d.pixel(19, 9), expect_px(0x1111, 2));
s.deliver(Rfb().fbu(1).raw_rect(20, 0, 1, 1, {0x1111}));
EXPECT_EQ(s.client.state(), VncState::VNC_STATE_IDLE);
}
TEST_F(VncDecode, UnknownMessageTypeReconnects) {
FakeDisplay d(64, 48);
this->start(d, 64, 48);
int disconnects = 0;
s().client.add_on_disconnect_callback([&disconnects]() { disconnects++; });
s().deliver(Rfb().u8(7));
EXPECT_EQ(s().client.state(), VncState::VNC_STATE_IDLE);
EXPECT_FALSE(s().client.link().connected());
EXPECT_EQ(s().client.hold_ms(), 5000u);
EXPECT_EQ(disconnects, 1);
}
TEST_F(VncDecode, UnsupportedEncodingReconnects) {
FakeDisplay d(64, 48);
this->start(d, 64, 48);
// CopyRect, which the client never asked for.
s().deliver(Rfb().fbu(1).rect(0, 0, 4, 4, 1).u16(0).u16(0));
EXPECT_EQ(s().client.state(), VncState::VNC_STATE_IDLE);
}
struct OutsideCase {
uint16_t x, y, w, h;
int32_t encoding;
};
class VncRectOutside : public ::testing::TestWithParam<OutsideCase> {};
TEST_P(VncRectOutside, Reconnects) {
FakeDisplay d(64, 48);
Session s(&d);
s.start(64, 48);
const auto &c = GetParam();
s.deliver(Rfb().fbu(1).rect(c.x, c.y, c.w, c.h, c.encoding).fill(64, 0));
EXPECT_EQ(s.client.state(), VncState::VNC_STATE_IDLE);
EXPECT_EQ(d.draw_calls(), 0);
}
INSTANTIATE_TEST_SUITE_P(Rects, VncRectOutside,
::testing::Values(OutsideCase{60, 0, 5, 1, RAW}, OutsideCase{0, 47, 1, 2, RAW},
OutsideCase{65535, 0, 2, 1, RAW}, OutsideCase{0, 65535, 1, 2, RAW},
OutsideCase{64, 0, 1, 1, RAW}, OutsideCase{0, 0, 65, 1, RAW}));
// --- A frame whose last step needs no bytes must still finish when its last byte arrives.
class VncZeroByteEnd : public VncDecode {
protected:
void deliver_bytewise(const Bytes &stream, size_t frame_end) {
for (size_t i = 0; i < stream.size(); i++) {
s().deliver(Bytes{stream[i]});
Bytes tx = s().take();
if (i + 1 == frame_end) {
EXPECT_EQ(hex(tx), hex(fbur(true, 64, 48))) << "no request after the final byte";
} else {
EXPECT_EQ(tx.size(), 0u) << "unexpected bytes after byte " << i;
}
}
EXPECT_FALSE(s().client.in_update());
EXPECT_EQ(s().client.step(), VncStep::VNC_STEP_MESSAGE);
EXPECT_EQ(s().client.rx_buffered(), 0);
}
};
TEST_F(VncZeroByteEnd, ZeroSizeCursor) {
FakeDisplay d(64, 48);
this->start(d, 64, 48);
Rfb f;
f.fbu(2).raw_rect(0, 0, 2, 1, {0x7001, 0x7002}).cursor(4, 4, 0, 0);
this->deliver_bytewise(f.bytes, f.bytes.size());
s().run_for(300);
EXPECT_EQ(d.updates(), 1);
}
TEST_F(VncZeroByteEnd, ZeroSizeRawRect) {
FakeDisplay d(64, 48);
this->start(d, 64, 48);
Rfb f;
f.fbu(2).raw_rect(0, 0, 1, 1, {0x7001}).rect(64, 48, 0, 0, RAW);
this->deliver_bytewise(f.bytes, f.bytes.size());
s().run_for(300);
EXPECT_EQ(d.updates(), 1);
}
TEST_F(VncZeroByteEnd, EmptyCutTextAfterFrame) {
FakeDisplay d(64, 48);
this->start(d, 64, 48);
Rfb f;
f.fbu(1).raw_rect(0, 0, 1, 1, {0x7001});
const size_t frame_end = f.bytes.size();
f.cut_text(0).colour_map(0, 0);
this->deliver_bytewise(f.bytes, frame_end);
s().run_for(300);
EXPECT_EQ(d.updates(), 1);
s().deliver(Rfb().fbu(1).raw_rect(1, 0, 1, 1, {0x7002}));
EXPECT_EQ(d.pixel(1, 0), expect_px(0x7002, 2));
}
// --- The 20 ms budget: with a slow display, one row per pass.
TEST_F(VncDecode, BudgetPausesBetweenRectsAndFinishesOnTheLastOne) {
FakeDisplay d(64, 48);
this->start(d, 64, 48);
d.set_draw_delay_us(25000);
Rfb f;
f.fbu(3)
.raw_rect(0, 0, 16, 1, std::vector<uint16_t>(16, 0x0A0A))
.raw_rect(16, 0, 16, 1, std::vector<uint16_t>(16, 0x0B0B))
.raw_rect(32, 0, 16, 1, std::vector<uint16_t>(16, 0x0C0C));
s().send(f);
s().loop();
EXPECT_EQ(d.draw_calls(), 1);
EXPECT_TRUE(s().client.in_update());
EXPECT_TRUE(s().client.rx_pending());
EXPECT_TRUE(HighFrequencyLoopRequester::is_high_frequency());
s().loop();
EXPECT_EQ(d.draw_calls(), 2);
s().loop();
EXPECT_EQ(d.draw_calls(), 3);
EXPECT_FALSE(s().client.in_update());
EXPECT_EQ(hex(s().take()), hex(fbur(true, 64, 48)));
d.set_draw_delay_us(0);
s().loop(2);
EXPECT_FALSE(s().client.rx_pending());
EXPECT_FALSE(HighFrequencyLoopRequester::is_high_frequency());
expect_solid(d, 32, 0, 16, 1, 0x0C0C, 2);
}
TEST_F(VncDecode, BudgetPausesInsideARawRect) {
FakeDisplay d(64, 48);
this->start(d, 64, 48);
d.set_draw_delay_us(25000);
auto px = pattern(4 * 3, 0x2000);
s().send(Rfb().fbu(1).raw_rect(0, 0, 4, 3, px));
s().loop();
EXPECT_EQ(d.draw_calls(), 1);
EXPECT_TRUE(s().client.in_update());
s().loop(2);
EXPECT_EQ(d.draw_calls(), 3);
EXPECT_FALSE(s().client.in_update());
d.set_draw_delay_us(0);
expect_block(d, 0, 0, 4, 3, px, 2);
}
// --- Split-feed property: however the bytes are cut, the result is the same and matches a model.
struct Model {
int fb_w{0};
int fb_h{0};
uint8_t bpp{2};
std::vector<int32_t> px;
void set(int x, int y, uint16_t v) { this->px[static_cast<size_t>(y) * this->fb_w + x] = v; }
};
struct Generated {
int disp_w{0};
int disp_h{0};
display::DisplayType type{display::DISPLAY_TYPE_COLOR};
uint16_t vis_w{0};
uint16_t vis_h{0};
Bytes handshake;
Bytes handshake_tx;
std::vector<Bytes> frames;
Model model;
};
static Generated generate(uint32_t seed) {
std::mt19937 rng(seed);
auto rnd = [&rng](int lo, int hi) { return std::uniform_int_distribution<int>(lo, hi)(rng); };
Generated g;
g.disp_w = rnd(4, 70);
g.disp_h = rnd(4, 50);
const int type = rnd(0, 2);
g.type = type == 0 ? display::DISPLAY_TYPE_COLOR
: (type == 1 ? display::DISPLAY_TYPE_GRAYSCALE : display::DISPLAY_TYPE_BINARY);
const uint8_t bpp = g.type == display::DISPLAY_TYPE_BINARY ? 1 : 2;
const int fb_w = rnd(1, 90);
const int fb_h = rnd(1, 64);
g.vis_w = std::min(fb_w, g.disp_w);
g.vis_h = std::min(fb_h, g.disp_h);
g.model.fb_w = fb_w;
g.model.fb_h = fb_h;
g.model.bpp = bpp;
g.model.px.assign(static_cast<size_t>(fb_w) * fb_h, -1);
auto pixel = [&]() { return static_cast<uint16_t>(bpp == 2 ? rnd(0, 0xFFFF) : rnd(0, 0xFF)); };
Rfb hs;
switch (rnd(0, 2)) {
case 0:
hs.version("RFB 003.003\n").security_33(1);
g.handshake_tx = cat({version_reply('3'), {1}});
break;
case 1:
hs.version("RFB 003.007\n").security_list({1});
g.handshake_tx = cat({version_reply('7'), {1}, {1}});
break;
default:
hs.version().security_list({16, 1}).security_result(0);
g.handshake_tx = cat({version_reply('8'), {1}, {1}});
break;
}
hs.server_init(fb_w, fb_h, std::string(rnd(0, 80), 'n'));
g.handshake = hs.bytes;
g.handshake_tx = cat({g.handshake_tx, bpp == 2 ? set_pixel_format_565() : set_pixel_format_332(), set_encodings(),
fbur(false, g.vis_w, g.vis_h)});
const int frames = rnd(3, 8);
for (int frame = 0; frame < frames; frame++) {
Rfb f;
for (int extra = rnd(0, 2); extra > 0; extra--) {
switch (rnd(0, 2)) {
case 0:
f.bell();
break;
case 1:
f.cut_text(rnd(0, 700));
break;
default:
f.colour_map(rnd(0, 255), rnd(0, 4));
break;
}
}
const int rects = rnd(0, 5);
f.fbu(rects);
for (int r = 0; r < rects; r++) {
const int kind = rnd(0, 9);
if (kind == 8) {
f.cursor(rnd(0, 100), rnd(0, 100), rnd(0, 9), rnd(0, 9), bpp);
continue;
}
if (kind == 9) {
const int x = rnd(0, fb_w);
const int y = rnd(0, fb_h);
f.rect(x, y, rnd(0, 1) ? 0 : rnd(0, fb_w - x), 0, RAW);
continue;
}
const int x = rnd(0, fb_w - 1);
const int y = rnd(0, fb_h - 1);
const int w = rnd(1, std::min(fb_w - x, 48));
const int h = rnd(1, std::min(fb_h - y, 40));
f.rect(x, y, w, h, RAW);
for (int yy = 0; yy < h; yy++) {
for (int xx = 0; xx < w; xx++) {
uint16_t p = pixel();
f.px(p, bpp);
g.model.set(x + xx, y + yy, p);
}
}
}
g.frames.push_back(f.bytes);
}
return g;
}
static void expect_matches_model(const FakeDisplay &d, const Generated &g, uint32_t seed) {
for (int y = 0; y < g.disp_h; y++) {
for (int x = 0; x < g.disp_w; x++) {
uint32_t expected = UNSET;
if (x < g.vis_w && y < g.vis_h) {
const int32_t v = g.model.px[static_cast<size_t>(y) * g.model.fb_w + x];
if (v >= 0) {
expected = expect_px(static_cast<uint16_t>(v), g.model.bpp);
}
}
ASSERT_EQ(d.pixel(x, y), expected) << "seed " << seed << " at " << x << "," << y;
}
}
}
enum class Cut { WHOLE, BYTES, RANDOM };
static std::vector<Bytes> cut(const Bytes &stream, Cut how, std::mt19937 &rng) {
std::vector<Bytes> chunks;
size_t pos = 0;
while (pos < stream.size()) {
size_t len = stream.size() - pos;
if (how == Cut::BYTES) {
len = 1;
} else if (how == Cut::RANDOM) {
len = std::min<size_t>(len, std::uniform_int_distribution<size_t>(1, 700)(rng));
}
chunks.emplace_back(stream.begin() + pos, stream.begin() + pos + len);
pos += len;
}
return chunks;
}
struct FeedResult {
std::vector<uint32_t> pixels;
Bytes tx;
};
static FeedResult run_parser(const Generated &g, Cut how, uint32_t seed) {
FakeDisplay d(g.disp_w, g.disp_h, g.type);
Session s(&d);
s.attach();
Bytes stream = g.handshake;
for (const auto &frame : g.frames) {
stream.insert(stream.end(), frame.begin(), frame.end());
}
std::mt19937 rng(seed * 7919u + static_cast<uint32_t>(how));
for (const auto &chunk : cut(stream, how, rng)) {
EXPECT_EQ(s.client.feed(chunk), VncParse::VNC_PARSE_MORE) << "seed " << seed;
}
EXPECT_EQ(s.client.state(), VncState::VNC_STATE_RUNNING) << "seed " << seed;
EXPECT_EQ(s.client.step(), VncStep::VNC_STEP_MESSAGE) << "seed " << seed;
EXPECT_EQ(s.client.rx_buffered(), 0) << "seed " << seed;
EXPECT_FALSE(s.client.in_update()) << "seed " << seed;
EXPECT_EQ(d.out_of_bounds(), 0);
expect_matches_model(d, g, seed);
return {d.pixels(), s.take()};
}
TEST(VncSplitFeed, ParserGivesTheSameResultHoweverTheBytesAreCut) {
for (uint32_t seed = 1; seed <= 50; seed++) {
const Generated g = generate(seed);
const FeedResult whole = run_parser(g, Cut::WHOLE, seed);
EXPECT_EQ(hex(whole.tx), hex(g.handshake_tx)) << "seed " << seed;
const FeedResult bytes = run_parser(g, Cut::BYTES, seed);
const FeedResult random = run_parser(g, Cut::RANDOM, seed);
EXPECT_EQ(whole.pixels, bytes.pixels) << "seed " << seed;
EXPECT_EQ(whole.pixels, random.pixels) << "seed " << seed;
EXPECT_EQ(hex(whole.tx), hex(bytes.tx)) << "seed " << seed;
EXPECT_EQ(hex(whole.tx), hex(random.tx)) << "seed " << seed;
if (::testing::Test::HasFailure()) {
break;
}
}
}
/// End to end over the socket: the server sends each frame only after the client asked for it.
static FeedResult run_socket(const Generated &g, Cut how, uint32_t seed) {
FakeDisplay d(g.disp_w, g.disp_h, g.type);
Session s(&d);
s.attach();
s.deliver(g.handshake);
EXPECT_EQ(s.client.state(), VncState::VNC_STATE_RUNNING) << "seed " << seed;
EXPECT_EQ(hex(s.take()), hex(g.handshake_tx)) << "seed " << seed;
std::mt19937 rng(seed * 104729u + static_cast<uint32_t>(how));
Bytes tx;
auto collect = [&]() {
Bytes more = s.take();
tx.insert(tx.end(), more.begin(), more.end());
};
auto wait_for_requests = [&](int count) {
for (int i = 0; i < 100 && count_fburs(tx, true) < count; i++) {
s.tick(10);
collect();
}
EXPECT_EQ(count_fburs(tx, true), count) << "seed " << seed;
};
for (size_t k = 0; k < g.frames.size(); k++) {
if (k > 0) {
wait_for_requests(static_cast<int>(k));
}
for (const auto &chunk : cut(g.frames[k], how, rng)) {
s.send(chunk);
for (int i = 0; i < 1000 && (s.server_bytes_waiting() || s.unread() != 0 || s.client.rx_pending()); i++) {
s.tick(1);
}
collect();
}
}
wait_for_requests(static_cast<int>(g.frames.size()));
s.run_for(2000);
collect();
EXPECT_EQ(s.client.state(), VncState::VNC_STATE_RUNNING) << "seed " << seed;
EXPECT_EQ(d.out_of_bounds(), 0);
expect_matches_model(d, g, seed);
if (d.drawn_count() != 0) {
EXPECT_GE(d.updates(), 1) << "seed " << seed;
}
Bytes expected_tx;
for (size_t i = 0; i < g.frames.size(); i++) {
expected_tx = cat({expected_tx, fbur(true, g.vis_w, g.vis_h)});
}
EXPECT_EQ(hex(tx), hex(expected_tx)) << "seed " << seed;
return {d.pixels(), tx};
}
TEST(VncSplitFeed, SocketGivesTheSameResultHoweverTheBytesAreCut) {
for (uint32_t seed = 101; seed <= 130; seed++) {
const Generated g = generate(seed);
const FeedResult whole = run_socket(g, Cut::WHOLE, seed);
const FeedResult bytes = run_socket(g, Cut::BYTES, seed);
const FeedResult random = run_socket(g, Cut::RANDOM, seed);
EXPECT_EQ(whole.pixels, bytes.pixels) << "seed " << seed;
EXPECT_EQ(whole.pixels, random.pixels) << "seed " << seed;
if (::testing::Test::HasFailure()) {
break;
}
}
}
} // namespace esphome::vnc::testing
#endif // USE_HOST
@@ -0,0 +1,342 @@
#include "vnc_test_helpers.h"
#ifdef USE_HOST
namespace esphome::vnc::testing {
static Bytes running_tail(const Bytes &pixel_format, uint16_t w, uint16_t h) {
return cat({pixel_format, set_encodings(), fbur(false, w, h)});
}
TEST(VncHandshake, Rfb38NoneExactBytes) {
FakeDisplay display(64, 48);
Session s(&display);
int connects = 0;
s.client.add_on_connect_callback([&connects]() { connects++; });
s.attach();
EXPECT_EQ(s.client.state(), VncState::VNC_STATE_HANDSHAKE);
s.deliver(Rfb().version());
EXPECT_EQ(hex(s.take()), hex(version_reply('8')));
s.deliver(Rfb().security_list({1}));
EXPECT_EQ(hex(s.take()), hex({1}));
s.deliver(Rfb().security_result(0));
EXPECT_EQ(hex(s.take()), hex({1})); // ClientInit, shared
EXPECT_FALSE(s.client.is_connected());
s.deliver(Rfb().server_init(80, 40));
// The first request covers only what the display can show.
EXPECT_EQ(hex(s.take()), hex(running_tail(set_pixel_format_565(), 64, 40)));
EXPECT_TRUE(s.client.is_connected());
EXPECT_EQ(connects, 1);
}
TEST(VncHandshake, Rfb38NonePipelined) {
FakeDisplay display(64, 48);
Session s(&display);
s.attach();
s.deliver(handshake_38(32, 100));
EXPECT_EQ(hex(s.take()), hex(cat({version_reply('8'), {1}, {1}, running_tail(set_pixel_format_565(), 32, 48)})));
EXPECT_TRUE(s.client.is_connected());
}
struct PixelFormatCase {
display::DisplayType type;
bool rgb565;
};
class VncPixelFormatSelection : public ::testing::TestWithParam<PixelFormatCase> {};
TEST_P(VncPixelFormatSelection, SetPixelFormatMatchesTheDisplay) {
const auto &param = GetParam();
FakeDisplay display(64, 48, param.type);
Session s(&display);
s.attach();
s.deliver(handshake_38(64, 48));
Bytes tx = s.take();
const Bytes expected = running_tail(param.rgb565 ? set_pixel_format_565() : set_pixel_format_332(), 64, 48);
ASSERT_GE(tx.size(), expected.size());
EXPECT_EQ(hex(Bytes(tx.end() - expected.size(), tx.end())), hex(expected));
EXPECT_EQ(s.client.bpp(), param.rgb565 ? 2 : 1);
}
INSTANTIATE_TEST_SUITE_P(Formats, VncPixelFormatSelection,
::testing::Values(PixelFormatCase{display::DISPLAY_TYPE_COLOR, true},
PixelFormatCase{display::DISPLAY_TYPE_GRAYSCALE, true},
PixelFormatCase{display::DISPLAY_TYPE_BINARY, false}));
TEST(VncHandshake, Rfb37NoneHasNoSecurityResult) {
FakeDisplay display(64, 48);
Session s(&display);
s.attach();
s.deliver(Rfb().version("RFB 003.007\n"));
EXPECT_EQ(hex(s.take()), hex(version_reply('7')));
s.deliver(Rfb().security_list({1}));
// Type and ClientInit go out together: 3.7 sends no SecurityResult for None.
EXPECT_EQ(hex(s.take()), hex({1, 1}));
s.deliver(Rfb().server_init(64, 48));
EXPECT_EQ(hex(s.take()), hex(running_tail(set_pixel_format_565(), 64, 48)));
EXPECT_TRUE(s.client.is_connected());
}
TEST(VncHandshake, Rfb33None) {
FakeDisplay display(64, 48);
Session s(&display);
s.attach();
s.deliver(Rfb().version("RFB 003.003\n"));
EXPECT_EQ(hex(s.take()), hex(version_reply('3')));
s.deliver(Rfb().security_33(1));
EXPECT_EQ(hex(s.take()), hex({1}));
s.deliver(Rfb().server_init(64, 48));
EXPECT_EQ(hex(s.take()), hex(running_tail(set_pixel_format_565(), 64, 48)));
EXPECT_TRUE(s.client.is_connected());
}
TEST(VncHandshake, Rfb33RefusalIsTransient) {
FakeDisplay display(64, 48);
Session s(&display);
s.attach();
s.deliver(Rfb().version("RFB 003.003\n").security_33(0).reason("Too many security failures"));
EXPECT_EQ(s.client.state(), VncState::VNC_STATE_IDLE);
EXPECT_EQ(s.client.hold_ms(), 5000u);
EXPECT_FALSE(s.client.status_has_error());
}
TEST(VncHandshake, Rfb33PasswordWithoutKeyIsConfig) {
FakeDisplay display(64, 48);
Session s(&display);
s.attach();
s.deliver(Rfb().version("RFB 003.003\n"));
EXPECT_EQ(hex(s.take()), hex(version_reply('3')));
s.deliver(Rfb().security_33(2).challenge());
EXPECT_EQ(s.take().size(), 0u);
EXPECT_EQ(s.client.state(), VncState::VNC_STATE_IDLE);
EXPECT_EQ(s.client.hold_ms(), 300000u);
EXPECT_TRUE(s.client.status_has_error());
}
struct VersionCase {
const char *server;
char reply_minor;
};
class VncVersionMapping : public ::testing::TestWithParam<VersionCase> {};
TEST_P(VncVersionMapping, RepliesWithTheMappedVersion) {
FakeDisplay display(64, 48);
Session s(&display);
s.attach();
s.deliver(text(GetParam().server));
EXPECT_EQ(hex(s.take()), hex(version_reply(GetParam().reply_minor)));
EXPECT_EQ(s.client.state(), VncState::VNC_STATE_HANDSHAKE);
EXPECT_EQ(s.client.step(),
GetParam().reply_minor == '3' ? VncStep::VNC_STEP_SECURITY_TYPE_33 : VncStep::VNC_STEP_SECURITY_COUNT);
}
INSTANTIATE_TEST_SUITE_P(Versions, VncVersionMapping,
::testing::Values(VersionCase{"RFB 003.008\n", '8'}, VersionCase{"RFB 003.889\n", '8'},
VersionCase{"RFB 004.001\n", '8'}, VersionCase{"RFB 005.000\n", '8'},
VersionCase{"RFB 003.007\n", '7'}, VersionCase{"RFB 003.005\n", '3'},
VersionCase{"RFB 003.003\n", '3'}));
class VncBadVersion : public ::testing::TestWithParam<const char *> {};
TEST_P(VncBadVersion, IsAConfigFailureHeldFor300s) {
FakeDisplay display(64, 48);
Listener listener;
Session s(&display);
s.client.set_port(listener.port());
s.attach();
s.deliver(text(GetParam()));
EXPECT_EQ(s.take().size(), 0u);
EXPECT_EQ(s.client.state(), VncState::VNC_STATE_IDLE);
EXPECT_EQ(s.client.hold_ms(), 300000u);
EXPECT_TRUE(s.client.status_has_error());
const uint32_t failed_at = now_ms();
// No attempt while the hold runs.
while (now_ms() - failed_at < 299000) {
s.tick(1000);
}
EXPECT_EQ(listener.accepted(), 0);
EXPECT_FALSE(s.client.link().connecting());
set_now(failed_at + 300000);
EXPECT_TRUE(s.wait_until([&]() { return listener.accepted() == 1; }));
}
INSTANTIATE_TEST_SUITE_P(Versions, VncBadVersion,
::testing::Values("HTTP/1.1 400", "RFB 002.000\n", "RFB 003,008\n", "RFB 0x3.008\n",
"SSH-2.0-Open"));
TEST(VncHandshake, PasswordRequiredWithoutKeyIsConfig) {
FakeDisplay display(64, 48);
Session s(&display);
s.attach();
s.deliver(Rfb().version());
EXPECT_EQ(hex(s.take()), hex(version_reply('8')));
s.deliver(Rfb().security_list({2, 16}));
// Never pick 16, never answer type 2 without a key.
EXPECT_EQ(s.take().size(), 0u);
EXPECT_EQ(s.client.state(), VncState::VNC_STATE_IDLE);
EXPECT_EQ(s.client.hold_ms(), 300000u);
EXPECT_TRUE(s.client.status_has_error());
EXPECT_FALSE(s.client.link().connected());
}
TEST(VncHandshake, OnlyUnsupportedTypesIsConfig) {
FakeDisplay display(64, 48);
Session s(&display);
s.attach();
s.deliver(Rfb().version());
EXPECT_EQ(hex(s.take()), hex(version_reply('8')));
s.deliver(Rfb().security_list({16}));
EXPECT_EQ(s.take().size(), 0u);
EXPECT_EQ(s.client.state(), VncState::VNC_STATE_IDLE);
EXPECT_EQ(s.client.hold_ms(), 300000u);
EXPECT_TRUE(s.client.status_has_error());
}
TEST(VncHandshake, EmptyListWithReasonIsTransient) {
FakeDisplay display(64, 48);
Session s(&display);
s.attach();
s.deliver(Rfb().version());
EXPECT_EQ(hex(s.take()), hex(version_reply('8')));
s.deliver(Rfb().security_list({}).reason("Too many authentication failures"));
EXPECT_EQ(s.take().size(), 0u);
EXPECT_EQ(s.client.state(), VncState::VNC_STATE_IDLE);
EXPECT_EQ(s.client.hold_ms(), 5000u);
EXPECT_EQ(s.client.backoff_ms(), 5000u);
EXPECT_TRUE(s.client.status_has_warning());
EXPECT_FALSE(s.client.status_has_error());
}
TEST(VncHandshake, EmptyReasonEndsAtOnce) {
FakeDisplay display(64, 48);
Session s(&display);
s.attach();
s.deliver(Rfb().version());
s.deliver(Rfb().security_list({}).u32(0));
EXPECT_EQ(s.client.state(), VncState::VNC_STATE_IDLE);
EXPECT_EQ(s.client.hold_ms(), 5000u);
}
TEST(VncHandshake, LongReasonIsCutWithoutWaitingForAllOfIt) {
FakeDisplay display(64, 48);
Session s(&display);
s.attach();
s.deliver(Rfb().version());
// Announces 100000 bytes but sends only the first 63 the client logs.
s.deliver(Rfb().security_list({}).u32(100000).fill(63, 'r'));
EXPECT_EQ(s.client.state(), VncState::VNC_STATE_IDLE);
EXPECT_EQ(s.client.hold_ms(), 5000u);
}
TEST(VncHandshake, FullSecurityListWithoutAUsableType) {
FakeDisplay display(64, 48);
Session s(&display);
s.attach();
s.deliver(Rfb().version());
Rfb list;
list.u8(255);
for (int i = 0; i < 255; i++) {
list.u8(static_cast<uint8_t>(i == 1 || i == 2 ? 16 : i));
}
s.deliver(list);
EXPECT_EQ(s.client.state(), VncState::VNC_STATE_IDLE);
EXPECT_EQ(s.client.hold_ms(), 300000u);
}
TEST(VncHandshake, DumpConfig) {
FakeDisplay display(64, 48);
Session s(&display);
s.client.dump_config();
}
TEST(VncHandshake, HandshakeTimeout) {
FakeDisplay display(64, 48);
Session s(&display);
s.attach();
const uint32_t start = now_ms();
s.deliver(Rfb().version());
s.at(start + 14999);
EXPECT_EQ(s.client.state(), VncState::VNC_STATE_HANDSHAKE);
EXPECT_TRUE(s.client.link().connected());
s.at(start + 15000);
EXPECT_EQ(s.client.state(), VncState::VNC_STATE_IDLE);
EXPECT_EQ(s.client.hold_ms(), 5000u);
EXPECT_FALSE(s.client.link().connected());
EXPECT_FALSE(s.client.status_has_error());
}
TEST(VncHandshake, NoneSecurityFailureIsConfig) {
FakeDisplay display(64, 48);
Session s(&display);
s.attach();
s.deliver(Rfb().version().security_list({1, 16}));
EXPECT_EQ(hex(s.take()), hex(cat({version_reply('8'), {1}})));
s.deliver(Rfb().security_result(1).reason("Server is busy"));
EXPECT_EQ(s.client.state(), VncState::VNC_STATE_IDLE);
EXPECT_EQ(s.client.hold_ms(), 300000u);
}
TEST(VncHandshake, EmptyServerName) {
FakeDisplay display(64, 48);
Session s(&display);
s.attach();
s.deliver(Rfb().version().security_list({1}).security_result(0).server_init(64, 48, ""));
EXPECT_TRUE(s.client.is_connected());
EXPECT_EQ(hex(s.take()), hex(cat({version_reply('8'), {1}, {1}, running_tail(set_pixel_format_565(), 64, 48)})));
}
TEST(VncHandshake, LongServerNameIsSkipped) {
FakeDisplay display(64, 48);
Session s(&display);
s.attach();
s.deliver(Rfb().version().security_list({1}).security_result(0).server_init(64, 48, std::string(700, 'n')));
ASSERT_TRUE(s.client.is_connected());
s.take();
s.deliver(Rfb().fbu(1).raw_rect(0, 0, 1, 1, {0xF800}));
EXPECT_EQ(display.pixel(0, 0), expect_px(0xF800, 2));
EXPECT_TRUE(s.client.is_connected());
}
TEST(VncHandshake, RunningOnlyAfterTheWholeServerName) {
FakeDisplay display(64, 48);
Session s(&display);
s.attach();
Bytes init = Rfb().version().security_list({1}).security_result(0).server_init(64, 48, std::string(700, 'n')).bytes;
s.deliver(Bytes(init.begin(), init.end() - 1));
EXPECT_EQ(s.client.state(), VncState::VNC_STATE_HANDSHAKE);
s.deliver(Bytes(init.end() - 1, init.end()));
EXPECT_TRUE(s.client.is_connected());
}
TEST(VncHandshake, EmptyServerScreenIsTransient) {
FakeDisplay display(64, 48);
Session s(&display);
s.attach();
s.deliver(Rfb().version().security_list({1}).security_result(0).server_init(0, 48));
EXPECT_EQ(s.client.state(), VncState::VNC_STATE_IDLE);
EXPECT_EQ(s.client.hold_ms(), 5000u);
}
TEST(VncHandshake, DisconnectCallbackOnlyAfterRunning) {
FakeDisplay display(64, 48);
Session s(&display);
int disconnects = 0;
s.client.add_on_disconnect_callback([&disconnects]() { disconnects++; });
s.attach();
s.deliver(Rfb().version());
s.close_peer();
s.loop(2);
EXPECT_EQ(disconnects, 0);
s.start(64, 48);
s.close_peer();
s.loop(2);
EXPECT_EQ(disconnects, 1);
EXPECT_FALSE(s.client.is_connected());
}
} // namespace esphome::vnc::testing
#endif // USE_HOST
+385
View File
@@ -0,0 +1,385 @@
#include "vnc_test_helpers.h"
#ifdef USE_HOST
namespace esphome::vnc::testing {
static Bytes frame(uint16_t x = 0, uint16_t pixel = 0x1234) {
return Rfb().fbu(1).raw_rect(x, 0, 2, 2, {pixel, pixel, pixel, pixel}).bytes;
}
static const Bytes INCREMENTAL = fbur(true, 64, 48);
// The session starts at t=1000, when the first (full) request goes out.
class VncPacing : public ::testing::Test {
protected:
void SetUp() override {
this->s_ = std::make_unique<Session>(&this->d_);
this->s_->start(64, 48);
}
Session &s() { return *this->s_; }
FakeDisplay d_;
std::unique_ptr<Session> s_;
};
TEST_F(VncPacing, UpdateOnlyAfterTheScreenStopsChanging) {
set_now(1100);
s().deliver(frame());
EXPECT_EQ(d_.updates(), 0);
s().at(1299);
EXPECT_EQ(d_.updates(), 0);
s().at(1300);
EXPECT_EQ(d_.updates(), 1);
s().run_for(5000);
EXPECT_EQ(d_.updates(), 1);
}
TEST_F(VncPacing, UpdateHasItsOwnPass) {
set_now(1100);
s().deliver(frame(0));
s().send(frame(10, 0x4321));
ASSERT_GT(s().unread(), 0);
s().at(1300);
EXPECT_EQ(d_.updates(), 1);
// Nothing was read in the pass that refreshed the display.
EXPECT_GT(s().unread(), 0);
EXPECT_EQ(d_.pixel(10, 0), UNSET);
s().loop();
EXPECT_EQ(s().unread(), 0);
EXPECT_EQ(d_.pixel(10, 0), expect_px(0x4321, 2));
}
TEST_F(VncPacing, FastFramesPostponeTheUpdateUntilTheMaximumDelay) {
// A frame every 100 ms: never quiet for 200 ms, so only the 1 s limit forces a refresh.
std::vector<uint32_t> update_times;
int last = 0;
for (uint32_t t = 1100; t <= 2500; t += 100) {
s().send(frame(t % 60));
s().at(t);
if (d_.updates() != last) {
update_times.push_back(t);
last = d_.updates();
s().loop();
}
EXPECT_EQ(s().unread(), 0);
}
EXPECT_EQ(update_times, std::vector<uint32_t>({2100}));
s().at(2699);
EXPECT_EQ(d_.updates(), 1);
s().at(2700);
EXPECT_EQ(d_.updates(), 2);
}
TEST_F(VncPacing, NoUpdateInTheMiddleOfAFrameEvenPastTheMaximumDelay) {
set_now(1100);
s().deliver(frame());
Bytes second = Rfb().fbu(1).raw_rect(0, 10, 4, 1, {1, 2, 3, 4}).bytes;
set_now(1200);
s().deliver(Bytes(second.begin(), second.end() - 3));
ASSERT_TRUE(s().client.in_update());
s().run_for(1400);
EXPECT_EQ(d_.updates(), 0);
s().deliver(Bytes(second.end() - 3, second.end()));
EXPECT_FALSE(s().client.in_update());
EXPECT_EQ(d_.updates(), 0);
s().tick(10);
EXPECT_EQ(d_.updates(), 1);
}
TEST(VncPacingDelays, CustomDelays) {
FakeDisplay d;
Session s(&d);
s.client.set_update_delay(50);
s.client.set_max_update_delay(300);
s.start(64, 48);
set_now(1100);
s.deliver(frame());
s.at(1149);
EXPECT_EQ(d.updates(), 0);
s.at(1150);
EXPECT_EQ(d.updates(), 1);
// Frames every 40 ms: the 300 ms limit applies.
int updates = 1;
uint32_t first_forced = 0;
for (uint32_t t = 1200; t < 1700; t += 40) {
s.send(frame(t % 60));
s.at(t);
if (d.updates() != updates && first_forced == 0) {
first_forced = t;
}
updates = d.updates();
s.loop();
}
EXPECT_EQ(first_forced, 1520u);
}
TEST(VncPacingDelays, ZeroDelayRefreshesOnTheNextPass) {
FakeDisplay d;
Session s(&d);
s.client.set_update_delay(0);
s.start(64, 48);
set_now(1100);
s.deliver(frame());
s.loop();
EXPECT_EQ(d.updates(), 1);
}
TEST_F(VncPacing, NextRequestRightAfterEachFrameWithA50msFloor) {
set_now(1010);
s().deliver(frame());
EXPECT_EQ(s().take().size(), 0u);
s().at(1049);
EXPECT_EQ(s().take().size(), 0u);
s().at(1050);
EXPECT_EQ(hex(s().take()), hex(INCREMENTAL));
set_now(1500);
s().deliver(frame());
EXPECT_EQ(hex(s().take()), hex(INCREMENTAL));
// One request stays outstanding; nothing more until the next frame or the heartbeat.
s().run_for(50000, 100);
EXPECT_EQ(s().take().size(), 0u);
}
TEST_F(VncPacing, CursorOnlyFrameNeverRefreshes) {
set_now(1100);
s().deliver(Rfb().fbu(1).cursor(1, 1, 4, 4));
EXPECT_EQ(hex(s().take()), hex(INCREMENTAL));
s().run_for(5000);
EXPECT_EQ(d_.updates(), 0);
}
TEST_F(VncPacing, HeartbeatAfterAMinuteOfSilence) {
set_now(1100);
s().deliver(frame());
EXPECT_EQ(hex(s().take()), hex(INCREMENTAL));
while (now_ms() + 1000 < 61099) {
s().tick(1000);
}
EXPECT_EQ(s().take().size(), 0u);
s().at(61099);
EXPECT_EQ(s().take().size(), 0u);
s().at(61100);
EXPECT_EQ(hex(s().take()), hex(INCREMENTAL));
s().at(121099);
EXPECT_EQ(s().take().size(), 0u);
s().at(121100);
EXPECT_EQ(hex(s().take()), hex(INCREMENTAL));
}
TEST_F(VncPacing, DataResetsTheHeartbeat) {
set_now(1100);
s().deliver(frame());
s().take();
set_now(40000);
s().deliver(Rfb().bell());
s().at(61100);
EXPECT_EQ(s().take().size(), 0u);
s().at(100000);
EXPECT_EQ(hex(s().take()), hex(INCREMENTAL));
}
TEST_F(VncPacing, ServerSilentMidFrameEndsTheSession) {
set_now(1100);
s().deliver(frame());
s().take();
Bytes partial = frame();
partial.pop_back();
s().deliver(partial);
ASSERT_TRUE(s().client.in_update());
s().at(1100 + 59999);
EXPECT_EQ(s().client.state(), VncState::VNC_STATE_RUNNING);
s().at(1100 + 60000);
EXPECT_EQ(s().client.state(), VncState::VNC_STATE_IDLE);
EXPECT_FALSE(s().client.link().connected());
}
// --- A display that parks its loop when idle (it8951, epaper_spi).
class VncPacingParked : public ::testing::Test {
protected:
void SetUp() override {
this->s_ = std::make_unique<Session>(&this->d_);
this->s_->start(64, 48);
}
Session &s() { return *this->s_; }
FakeParkingDisplay d_;
std::unique_ptr<Session> s_;
};
TEST_F(VncPacingParked, BusyDisplayHoldsDrawsAndRequests) {
set_now(1100);
s().deliver(frame());
EXPECT_EQ(hex(s().take()), hex(INCREMENTAL));
s().at(1300);
ASSERT_EQ(d_.updates(), 1);
ASSERT_TRUE(d_.busy());
// The server answers the outstanding request while the panel refreshes.
s().send(frame(20, 0x5555));
s().at(1310);
EXPECT_TRUE(s().client.display_busy());
EXPECT_TRUE(s().client.in_update());
EXPECT_FALSE(HighFrequencyLoopRequester::is_high_frequency());
s().run_for(3000);
EXPECT_EQ(d_.draws_while_busy(), 0);
EXPECT_EQ(d_.pixel(20, 0), UNSET);
EXPECT_EQ(s().take().size(), 0u);
d_.finish();
s().tick(10);
EXPECT_EQ(d_.pixel(20, 0), expect_px(0x5555, 2));
EXPECT_EQ(hex(s().take()), hex(INCREMENTAL));
s().run_for(300);
EXPECT_EQ(d_.updates(), 2);
}
TEST(VncPacingParkedDelay, NoRequestInTheRefreshPassWithZeroDelay) {
FakeParkingDisplay d;
Session s(&d);
s.client.set_update_delay(0);
s.start(64, 48);
// The frame ends 20 ms after the request, inside the 50 ms floor, so the next request waits.
set_now(1020);
s.deliver(frame());
EXPECT_EQ(s.take().size(), 0u);
s.at(1060);
ASSERT_EQ(d.updates(), 1);
ASSERT_TRUE(d.busy());
EXPECT_EQ(s.take().size(), 0u) << "request sent while the panel refreshes";
d.finish();
s.tick(10);
EXPECT_EQ(hex(s.take()), hex(INCREMENTAL));
}
TEST_F(VncPacingParked, FailedDisplayEndsTheSessionAndStops) {
set_now(1100);
s().deliver(frame());
s().take();
s().at(1300);
ASSERT_EQ(d_.updates(), 1);
d_.mark_failed();
s().tick(10);
EXPECT_EQ(s().client.state(), VncState::VNC_STATE_IDLE);
EXPECT_FALSE(s().client.link().connected());
EXPECT_TRUE(s().client.status_has_error());
EXPECT_TRUE(s().client.is_idle());
}
TEST_F(VncPacingParked, NoRequestWhileBusyBetweenFrames) {
// Inside the 50 ms floor, so the next request is still due when the panel turns busy.
set_now(1010);
s().deliver(Rfb().fbu(0));
EXPECT_EQ(s().take().size(), 0u);
d_.external_update();
s().run_for(2000);
EXPECT_EQ(s().take().size(), 0u);
d_.finish();
s().tick(10);
EXPECT_EQ(hex(s().take()), hex(INCREMENTAL));
}
TEST_F(VncPacingParked, ExternalUpdateMidFrameStopsReading) {
Bytes f = Rfb().fbu(2).raw_rect(0, 0, 2, 1, {1, 2}).raw_rect(0, 5, 2, 1, {3, 4}).bytes;
set_now(1100);
s().deliver(Bytes(f.begin(), f.begin() + 4 + 12 + 4 + 6));
ASSERT_TRUE(s().client.in_update());
d_.external_update();
s().send(Bytes(f.begin() + 4 + 12 + 4 + 6, f.end()));
s().run_for(1000);
EXPECT_GT(s().unread(), 0);
EXPECT_EQ(d_.draws_while_busy(), 0);
EXPECT_FALSE(HighFrequencyLoopRequester::is_high_frequency());
d_.finish();
s().tick(10);
EXPECT_EQ(s().unread(), 0);
EXPECT_EQ(d_.pixel(1, 5), expect_px(4, 2));
EXPECT_FALSE(s().client.in_update());
}
// --- Displays that never report busy must never stall the client.
template<typename D> static void runs_frames_without_deadlock() {
D d;
Session s(&d);
s.start(64, 48);
for (int i = 0; i < 5; i++) {
set_now(now_ms() + 300);
s.deliver(frame(i * 4, 0x1000 + i));
EXPECT_EQ(hex(s.take()), hex(INCREMENTAL)) << "frame " << i;
s.run_for(300);
EXPECT_EQ(d.updates(), i + 1) << "frame " << i;
EXPECT_EQ(d.pixel(i * 4, 0), expect_px(0x1000 + i, 2));
}
}
TEST(VncPacingDisplays, PlainDisplay) { runs_frames_without_deadlock<FakeDisplay>(); }
TEST(VncPacingStart, FirstRefreshWaitsForAStartingDisplayToPark) {
FakeStartingDisplay d;
Session s(&d);
s.start(64, 48);
set_now(1100);
s.deliver(frame());
s.at(1300);
EXPECT_EQ(d.updates(), 0);
d.finish_start();
s.tick(10);
ASSERT_EQ(d.updates(), 1);
// Known to park now, so nothing is drawn until this refresh is over.
s.deliver(frame(10));
EXPECT_EQ(d.draws_while_busy(), 0);
d.finish();
s.tick(10);
EXPECT_EQ(d.pixel(10, 0), expect_px(0x1234, 2));
}
TEST(VncPacingStart, LoopingDisplayRefreshesAfterTheStartWindow) {
FakeLoopingDisplay d;
Session s(&d);
s.start(64, 48);
set_now(1100);
s.deliver(frame());
s.at(1000 + 4999);
EXPECT_EQ(d.updates(), 0);
s.at(1000 + 5000);
EXPECT_EQ(d.updates(), 1);
}
TEST(VncPacingDisplays, FreeRunningDisplay) { runs_frames_without_deadlock<FakeFreeRunDisplay>(); }
TEST(VncPacingDisplays, SynchronousParkedDisplay) { runs_frames_without_deadlock<FakeSyncParkedDisplay>(); }
TEST_F(VncPacingParked, FailedDisplayClosesAConnectStillInFlight) {
s().client.forget_session();
ASSERT_TRUE(s().client.link().connected());
d_.mark_failed();
s().tick(10);
EXPECT_FALSE(s().client.link().connected());
EXPECT_TRUE(s().client.status_has_error());
}
TEST_F(VncPacingParked, DisplayBusyTooLongSetsAWarningUntilItParks) {
d_.external_update();
s().run_for(120000, 1000);
EXPECT_TRUE(s().client.status_has_warning());
// A new session does not hide the stuck display
s().start(64, 48);
EXPECT_TRUE(s().client.status_has_warning());
d_.finish();
s().tick(10);
EXPECT_FALSE(s().client.status_has_warning());
}
TEST_F(VncPacingParked, DisplayParkingKeepsAConnectionWarning) {
d_.external_update();
s().run_for(120000, 1000);
s().close_peer();
s().tick(10);
ASSERT_EQ(s().client.state(), VncState::VNC_STATE_IDLE);
EXPECT_TRUE(s().client.status_has_warning());
d_.finish();
s().tick(10);
EXPECT_TRUE(s().client.status_has_warning());
}
} // namespace esphome::vnc::testing
#endif // USE_HOST
@@ -0,0 +1,13 @@
packages:
test_display: !include ../../test_build_components/common/test_display/test_display.yaml
vnc:
- host: 192.0.2.10
interval:
- interval: 60s
then:
- if:
condition: vnc.is_connected
then:
- logger.log: VNC is connected
@@ -0,0 +1,18 @@
packages:
spi: !include ../../test_build_components/common/spi/esp32-idf.yaml
wifi:
ssid: MySSID
password: password1
display:
- platform: it8951
id: m5paper_display
model: m5stack-m5paper
cs_pin: GPIO22
reset_pin: GPIO21
busy_pin: GPIO27
sleep_when_done: false
vnc:
- host: vnc.local
+581
View File
@@ -0,0 +1,581 @@
#pragma once
#include <gtest/gtest.h>
#include <algorithm>
#include <cerrno>
#include <csignal>
#include <cstdint>
#include <cstdio>
#include <cstring>
#include <fcntl.h>
#include <functional>
#include <initializer_list>
#include <memory>
#include <netinet/in.h>
#include <string>
#include <sys/ioctl.h>
#include <sys/socket.h>
#include <unistd.h>
#include <vector>
#include "esphome/components/display/display.h"
#include "esphome/components/socket/socket.h"
#include "esphome/components/vnc/vnc_client.h"
#include "esphome/core/application.h"
#include "esphome/core/hal.h"
#include "esphome/core/helpers.h"
#ifdef USE_HOST
namespace esphome::vnc::testing {
using Bytes = std::vector<uint8_t>;
inline void set_now(uint32_t ms) { LoopBlockingGuard guard(nullptr, nullptr, ms); }
inline uint32_t now_ms() { return App.get_loop_component_start_time(); }
inline std::string hex(const Bytes &bytes) {
std::string out;
char buf[4];
for (size_t i = 0; i < bytes.size(); i++) {
snprintf(buf, sizeof(buf), i == 0 ? "%02X" : " %02X", bytes[i]);
out += buf;
}
return out;
}
inline Bytes cat(std::initializer_list<Bytes> parts) {
Bytes out;
for (const auto &part : parts) {
out.insert(out.end(), part.begin(), part.end());
}
return out;
}
inline Bytes text(const std::string &s) { return Bytes(s.begin(), s.end()); }
// ---------------------------------------------------------------------------
// Displays
inline constexpr uint32_t UNSET = 0xFF000000;
inline uint32_t pack(Color c) { return (static_cast<uint32_t>(c.r) << 16) | (static_cast<uint32_t>(c.g) << 8) | c.b; }
/// The colour a wire pixel must show as, decoded the way the display API decodes it.
inline uint32_t expect_px(uint16_t wire, uint8_t bpp) {
return pack(display::ColorUtil::to_color(wire, bpp == 2 ? display::COLOR_BITNESS_565 : display::COLOR_BITNESS_332));
}
class FakeDisplay : public display::Display {
public:
FakeDisplay(int width = 64, int height = 48, display::DisplayType type = display::DISPLAY_TYPE_COLOR)
: width_(width), height_(height), type_(type), pixels_(static_cast<size_t>(width) * height, UNSET) {}
using display::Display::draw_pixels_at;
void update() override { this->updates_++; }
display::DisplayType get_display_type() override { return this->type_; }
int get_width_internal() override { return this->width_; }
int get_height_internal() override { return this->height_; }
void draw_pixel_at(int x, int y, Color color) override {
if (x < 0 || y < 0 || x >= this->width_ || y >= this->height_) {
this->out_of_bounds_++;
ADD_FAILURE() << "pixel drawn outside the display at " << x << "," << y;
return;
}
this->pixels_[static_cast<size_t>(y) * this->width_ + x] = pack(color);
}
void draw_pixels_internal(int x_start, int y_start, int w, int h, const uint8_t *ptr, display::ColorBitness bitness,
bool big_endian, int x_offset, int y_offset, int x_pad) override {
this->draw_calls_++;
if (w <= 0 || h <= 0 || x_start < 0 || y_start < 0 || x_start + w > this->width_ || y_start + h > this->height_) {
this->out_of_bounds_++;
ADD_FAILURE() << "block " << w << "x" << h << " at " << x_start << "," << y_start << " does not fit "
<< this->width_ << "x" << this->height_;
}
if (this->busy()) {
this->draws_while_busy_++;
ADD_FAILURE() << "drawn while the display is busy";
}
if (this->draw_delay_us_ != 0) {
usleep(this->draw_delay_us_);
}
display::Display::draw_pixels_internal(x_start, y_start, w, h, ptr, bitness, big_endian, x_offset, y_offset, x_pad);
}
virtual bool busy() const { return false; }
uint32_t pixel(int x, int y) const { return this->pixels_[static_cast<size_t>(y) * this->width_ + x]; }
const std::vector<uint32_t> &pixels() const { return this->pixels_; }
size_t drawn_count() const {
return static_cast<size_t>(
std::count_if(this->pixels_.begin(), this->pixels_.end(), [](uint32_t p) { return p != UNSET; }));
}
int updates() const { return this->updates_; }
int draw_calls() const { return this->draw_calls_; }
int out_of_bounds() const { return this->out_of_bounds_; }
int draws_while_busy() const { return this->draws_while_busy_; }
void set_draw_delay_us(uint32_t us) { this->draw_delay_us_ = us; }
protected:
int width_;
int height_;
display::DisplayType type_;
std::vector<uint32_t> pixels_;
int updates_{0};
int draw_calls_{0};
int out_of_bounds_{0};
int draws_while_busy_{0};
uint32_t draw_delay_us_{0};
};
/// Behaves like it8951 and epaper_spi: parked (loop disabled) while idle,
/// update() starts a refresh that runs in its loop until finish().
class FakeParkingDisplay : public FakeDisplay {
public:
FakeParkingDisplay(int width = 64, int height = 48, display::DisplayType type = display::DISPLAY_TYPE_GRAYSCALE)
: FakeDisplay(width, height, type) {
this->disable_loop();
}
void update() override {
this->updates_++;
this->enable_loop();
}
/// The refresh is over and the driver parks its loop again.
void finish() { this->disable_loop(); }
/// Something other than the VNC client started a refresh.
void external_update() { this->enable_loop(); }
bool busy() const override { return !this->is_idle(); }
};
/// Starts with its loop running and parks once its start-up is done, like it8951 at boot.
class FakeStartingDisplay : public FakeParkingDisplay {
public:
FakeStartingDisplay() {
this->component_state_ |= COMPONENT_HAS_LOOP;
this->enable_loop();
}
void finish_start() {
this->started_ = true;
this->finish();
}
bool busy() const override { return this->started_ && FakeParkingDisplay::busy(); }
protected:
bool started_{false};
};
/// A display whose loop always runs (never reports idle).
class FakeFreeRunDisplay : public FakeDisplay {
public:
FakeFreeRunDisplay(int width = 64, int height = 48) : FakeDisplay(width, height) {
this->disable_loop();
this->enable_loop();
}
};
/// A display with its own loop that never parks.
class FakeLoopingDisplay : public FakeFreeRunDisplay {
public:
FakeLoopingDisplay() { this->component_state_ |= COMPONENT_HAS_LOOP; }
};
/// Parked from setup and refreshes synchronously inside update(), like headless SDL.
class FakeSyncParkedDisplay : public FakeDisplay {
public:
FakeSyncParkedDisplay(int width = 64, int height = 48) : FakeDisplay(width, height) { this->disable_loop(); }
};
// ---------------------------------------------------------------------------
// Client under test
class TestVncClient : public VncClient {
public:
using VncClient::VncClient;
~TestVncClient() {
this->high_freq_.stop();
this->link_.close();
}
/// The state a connect still in flight has: a socket, but no session yet.
void forget_session() { this->state_ = VncState::VNC_STATE_IDLE; }
/// Hand the client a connected stream, as connect_step_() does after a successful connect.
void adopt(int fd) {
// connect_step_() has seen the network up before any connect succeeds.
this->network_was_up_ = true;
this->link_.adopt(std::make_unique<socket::Socket>(fd));
this->start_handshake_(now_ms());
}
/// Parse bytes straight from memory, the way service_rx_() would, without a socket read.
VncParse feed(const uint8_t *data, size_t len) {
VncParse result = VncParse::VNC_PARSE_MORE;
size_t offset = 0;
do {
const size_t room = RX_BUFFER_SIZE - this->rx_len_;
const size_t count = std::min(room, len - offset);
std::memcpy(this->rx_ + this->rx_len_, data + offset, count);
this->rx_len_ += static_cast<uint16_t>(count);
this->rx_total_ += count;
offset += count;
for (int guard = 0;; guard++) {
this->pass_start_ms_ = millis();
result = this->parse_();
if (result != VncParse::VNC_PARSE_PAUSE || this->display_busy_ || guard > 100000) {
break;
}
}
if (result != VncParse::VNC_PARSE_MORE) {
return result;
}
if (this->rx_pos_ != 0) {
this->rx_len_ -= this->rx_pos_;
std::memmove(this->rx_, this->rx_ + this->rx_pos_, this->rx_len_);
this->rx_pos_ = 0;
} else if (count == 0) {
ADD_FAILURE() << "parser made no progress with a full buffer";
return result;
}
} while (offset < len);
return result;
}
VncParse feed(const Bytes &bytes) { return this->feed(bytes.data(), bytes.size()); }
socket::TcpClientLink &link() { return this->link_; }
VncState state() const { return this->state_; }
VncStep step() const { return this->step_; }
uint32_t hold_ms() const { return this->hold_ms_; }
uint32_t hold_start_ms() const { return this->hold_start_ms_; }
uint32_t backoff_ms() const { return this->backoff_ms_; }
bool rx_pending() const { return this->rx_pending_; }
bool in_update() const { return this->in_update_; }
bool refresh_pending() const { return this->refresh_pending_; }
bool display_busy() const { return this->display_busy_; }
uint16_t rx_buffered() const { return this->rx_len_ - this->rx_pos_; }
uint16_t vis_w() const { return this->vis_w_; }
uint16_t vis_h() const { return this->vis_h_; }
uint8_t bpp() const { return this->bpp_; }
};
// ---------------------------------------------------------------------------
// Server-side byte builder (RFB 3.x, server to client)
class Rfb {
public:
Bytes bytes;
Rfb &u8(uint8_t v) {
this->bytes.push_back(v);
return *this;
}
Rfb &u16(uint16_t v) { return this->u8(v >> 8).u8(v & 0xFF); }
Rfb &u32(uint32_t v) { return this->u16(v >> 16).u16(v & 0xFFFF); }
Rfb &s32(int32_t v) { return this->u32(static_cast<uint32_t>(v)); }
Rfb &raw(const Bytes &b) {
this->bytes.insert(this->bytes.end(), b.begin(), b.end());
return *this;
}
Rfb &str(const std::string &s) { return this->raw(text(s)); }
Rfb &fill(size_t count, uint8_t value) {
this->bytes.insert(this->bytes.end(), count, value);
return *this;
}
/// One pixel in the client's format: little-endian RGB565 (bpp 2) or RGB332 (bpp 1).
Rfb &px(uint16_t v, uint8_t bpp = 2) {
this->u8(v & 0xFF);
if (bpp == 2) {
this->u8(v >> 8);
}
return *this;
}
Rfb &version(const std::string &v = "RFB 003.008\n") { return this->str(v); }
Rfb &security_list(std::initializer_list<uint8_t> types) {
this->u8(static_cast<uint8_t>(types.size()));
for (uint8_t t : types) {
this->u8(t);
}
return *this;
}
Rfb &security_33(uint32_t type) { return this->u32(type); }
Rfb &security_result(uint32_t result) { return this->u32(result); }
Rfb &reason(const std::string &s) { return this->u32(s.size()).str(s); }
Rfb &challenge() {
for (uint8_t i = 0; i < 16; i++) {
this->u8(i);
}
return *this;
}
Rfb &server_init(uint16_t w, uint16_t h, const std::string &name = "esphome-test") {
// The server's own pixel format; the client replaces it with SetPixelFormat.
this->u16(w).u16(h).u8(32).u8(24).u8(0).u8(1).u16(255).u16(255).u16(255).u8(16).u8(8).u8(0).fill(3, 0);
return this->u32(name.size()).str(name);
}
Rfb &fbu(uint16_t rects) { return this->u8(0).u8(0).u16(rects); }
Rfb &rect(uint16_t x, uint16_t y, uint16_t w, uint16_t h, int32_t encoding) {
return this->u16(x).u16(y).u16(w).u16(h).s32(encoding);
}
Rfb &raw_rect(uint16_t x, uint16_t y, uint16_t w, uint16_t h, const std::vector<uint16_t> &pixels, uint8_t bpp = 2) {
this->rect(x, y, w, h, 0);
for (uint16_t p : pixels) {
this->px(p, bpp);
}
return *this;
}
Rfb &cursor(uint16_t x, uint16_t y, uint16_t w, uint16_t h, uint8_t bpp = 2) {
this->rect(x, y, w, h, -239);
for (uint32_t i = 0; i < static_cast<uint32_t>(w) * h; i++) {
this->px(0xA5A5, bpp);
}
return this->fill(((w + 7u) / 8u) * h, 0xFF);
}
Rfb &bell() { return this->u8(2); }
Rfb &cut_text(uint32_t len, uint8_t value = 'x') { return this->u8(3).fill(3, 0).u32(len).fill(len, value); }
Rfb &colour_map(uint16_t first, uint16_t count) {
this->u8(1).u8(0).u16(first).u16(count);
for (uint16_t i = 0; i < count; i++) {
this->u16(i).u16(i).u16(i);
}
return *this;
}
};
/// A full RFB 3.8 None handshake up to and including ServerInit.
inline Bytes handshake_38(uint16_t w, uint16_t h) {
return Rfb().version().security_list({1}).security_result(0).server_init(w, h).bytes;
}
// ---------------------------------------------------------------------------
// Client-side messages the tests expect
inline Bytes fbur(bool incremental, uint16_t w, uint16_t h) {
return Rfb().u8(3).u8(incremental ? 1 : 0).u16(0).u16(0).u16(w).u16(h).bytes;
}
inline Bytes set_pixel_format_565() { return {0, 0, 0, 0, 16, 16, 0, 1, 0, 31, 0, 63, 0, 31, 11, 5, 0, 0, 0, 0}; }
inline Bytes set_pixel_format_332() { return {0, 0, 0, 0, 8, 8, 0, 1, 0, 7, 0, 7, 0, 3, 5, 2, 0, 0, 0, 0}; }
inline Bytes set_encodings() { return {2, 0, 0, 2, 0, 0, 0, 0, 0xFF, 0xFF, 0xFF, 0x11}; }
inline Bytes version_reply(char minor) { return text(std::string("RFB 003.00") + minor + "\n"); }
/// Splits a stream of client messages that are all FramebufferUpdateRequests.
inline int count_fburs(const Bytes &tx, bool incremental) {
int count = 0;
for (size_t i = 0; i + 10 <= tx.size(); i += 10) {
if (tx[i] == 3 && (tx[i + 1] != 0) == incremental) {
count++;
}
}
return count;
}
// ---------------------------------------------------------------------------
// Ports
/// A loopback port that was free a moment ago, so a connect to it is refused.
inline uint16_t refused_port() {
int probe = ::socket(AF_INET, SOCK_STREAM, 0);
struct sockaddr_in addr{};
addr.sin_family = AF_INET;
addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
socklen_t len = sizeof(addr);
EXPECT_EQ(::bind(probe, reinterpret_cast<struct sockaddr *>(&addr), len), 0);
EXPECT_EQ(::getsockname(probe, reinterpret_cast<struct sockaddr *>(&addr), &len), 0);
::close(probe);
return ntohs(addr.sin_port);
}
/// A loopback listener that counts the connects that reach it.
class Listener {
public:
Listener() {
this->fd_ = ::socket(AF_INET, SOCK_STREAM, 0);
struct sockaddr_in addr{};
addr.sin_family = AF_INET;
addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
socklen_t len = sizeof(addr);
EXPECT_EQ(::bind(this->fd_, reinterpret_cast<struct sockaddr *>(&addr), len), 0);
EXPECT_EQ(::listen(this->fd_, 16), 0);
EXPECT_EQ(::getsockname(this->fd_, reinterpret_cast<struct sockaddr *>(&addr), &len), 0);
fcntl(this->fd_, F_SETFL, fcntl(this->fd_, F_GETFL, 0) | O_NONBLOCK);
this->port_ = ntohs(addr.sin_port);
}
~Listener() {
for (int fd : this->accepted_) {
if (fd >= 0) {
::close(fd);
}
}
::close(this->fd_);
}
uint16_t port() const { return this->port_; }
/// Connections accepted so far, including any waiting now.
int accepted() {
for (;;) {
int fd = ::accept(this->fd_, nullptr, nullptr);
if (fd >= 0) {
this->accepted_.push_back(fd);
} else if (errno == ECONNABORTED) {
// The client gave up on it already; it still was an attempt.
this->accepted_.push_back(-1);
} else if (errno != EINTR) {
break;
}
}
return static_cast<int>(this->accepted_.size());
}
protected:
int fd_{-1};
uint16_t port_{0};
std::vector<int> accepted_;
};
// ---------------------------------------------------------------------------
// Test harness: the test plays the server on the far end of a socketpair.
class Session {
public:
explicit Session(display::Display *display, uint32_t start_ms = 1000) : client(display) {
signal(SIGPIPE, SIG_IGN);
set_now(start_ms);
this->client.set_host("127.0.0.1");
this->client.set_port(refused_port());
this->client.setup();
}
~Session() { this->close_peer(); }
/// A new TCP session: the client gets one end, the test keeps the other.
void attach() {
this->close_peer();
int fds[2];
ASSERT_EQ(socketpair(AF_UNIX, SOCK_STREAM, 0, fds), 0);
fcntl(fds[1], F_SETFL, fcntl(fds[1], F_GETFL, 0) | O_NONBLOCK);
this->peer_fd = fds[1];
this->client_fd = fds[0];
this->out_.clear();
this->out_pos_ = 0;
this->client.adopt(fds[0]);
}
void close_peer() {
if (this->peer_fd >= 0) {
::close(this->peer_fd);
this->peer_fd = -1;
}
}
/// Queue server bytes and write as many as the socket takes now.
void send(const Bytes &bytes) {
this->out_.insert(this->out_.end(), bytes.begin(), bytes.end());
this->pump();
}
void send(const Rfb &rfb) { this->send(rfb.bytes); }
void pump() {
while (this->out_pos_ < this->out_.size() && this->peer_fd >= 0) {
ssize_t n = ::write(this->peer_fd, this->out_.data() + this->out_pos_, this->out_.size() - this->out_pos_);
if (n <= 0) {
break;
}
this->out_pos_ += n;
}
if (this->out_pos_ == this->out_.size()) {
this->out_.clear();
this->out_pos_ = 0;
}
}
bool server_bytes_waiting() const { return this->out_pos_ < this->out_.size(); }
/// Bytes the client has not read from its socket yet.
int unread() {
int count = 0;
if (this->client.link().connected()) {
ioctl(this->client_fd, FIONREAD, &count);
}
return count;
}
/// Everything the client sent since the last call.
Bytes take() {
this->client.link().flush_tx();
Bytes out;
if (this->peer_fd < 0) {
return out;
}
uint8_t buf[4096];
for (;;) {
ssize_t n = ::read(this->peer_fd, buf, sizeof(buf));
if (n <= 0) {
break;
}
out.insert(out.end(), buf, buf + n);
}
return out;
}
void loop(int count = 1) {
for (int i = 0; i < count; i++) {
this->pump();
this->client.loop();
}
}
/// Move the virtual clock to an absolute time and run one pass.
void at(uint32_t ms) {
set_now(ms);
this->loop();
}
/// Advance the virtual clock and run one pass.
void tick(uint32_t ms) { this->at(now_ms() + ms); }
/// Advance in steps, running one pass per step.
void run_for(uint32_t ms, uint32_t step = 10) {
const uint32_t end = now_ms() + ms;
while (static_cast<int32_t>(end - now_ms()) > 0) {
this->tick(std::min(step, end - now_ms()));
}
}
bool loop_until(const std::function<bool()> &done, int max_loops = 1000) {
for (int i = 0; i < max_loops && !done(); i++) {
this->loop();
}
return done();
}
/// Runs passes at the current virtual time until done, sleeping briefly so the kernel can finish a connect.
bool wait_until(const std::function<bool()> &done, int max_ms = 2000) {
for (int i = 0; i < max_ms * 5 && !done(); i++) {
this->loop();
if (!done()) {
usleep(200);
}
}
return done();
}
/// Send and keep running passes at the current time until the client has read and parsed it all.
void deliver(const Bytes &bytes, int max_loops = 100000) {
this->send(bytes);
this->loop_until(
[this]() { return !this->server_bytes_waiting() && this->unread() == 0 && !this->client.rx_pending(); },
max_loops);
}
void deliver(const Rfb &rfb) { this->deliver(rfb.bytes); }
/// Full RFB 3.8 None handshake up to RUNNING; the client's handshake bytes are discarded.
void start(uint16_t fb_w, uint16_t fb_h) {
this->attach();
this->deliver(handshake_38(fb_w, fb_h));
ASSERT_EQ(this->client.state(), VncState::VNC_STATE_RUNNING);
this->take();
}
TestVncClient client;
int peer_fd{-1};
int client_fd{-1};
protected:
Bytes out_;
size_t out_pos_{0};
};
} // namespace esphome::vnc::testing
#endif // USE_HOST
+4
View File
@@ -236,6 +236,10 @@ async def yaml_config(request: pytest.FixtureRequest, unused_tcp_port: int) -> s
external_components_path = str(FIXTURES_DIR / "external_components")
content = content.replace("EXTERNAL_COMPONENT_PATH", external_components_path)
# Replace the user includes path placeholder if present
if "INCLUDES_PATH" in content:
content = content.replace("INCLUDES_PATH", str(FIXTURES_DIR / "includes"))
if shared_name is not None:
# _compile verifies the marked test compiles this content unmodified
request.node._shared_yaml_content = content
@@ -0,0 +1,6 @@
#pragma once
static constexpr int HEADER_CONSTANT = 42;
// Names the global by its id, so the header must come after the id declarations.
inline int header_reads_global() { return header_value->value() * 2; }
@@ -0,0 +1,27 @@
esphome:
name: includes-lambda-order
includes:
- INCLUDES_PATH/lambda_order.h
on_boot:
# An apply action value is rendered at file scope.
- globals.set:
id: header_value
value: !lambda return HEADER_CONSTANT;
host:
api:
logger:
globals:
- id: header_value
type: int
initial_value: "0"
sensor:
- platform: template
name: Header Value
update_interval: 100ms
lambda: return header_reads_global();
filters:
# A lambda in a flash table.
- filter_out: !lambda return HEADER_CONSTANT - 1;
@@ -0,0 +1,40 @@
esphome:
name: vnc-client-test
host:
api:
actions:
- action: take_snapshot
variables:
filename: string
then:
- snapshot.take:
id: disp
filename: !lambda return filename;
logger:
level: DEBUG
display:
- platform: snapshot
id: disp
update_interval: never
dimensions:
width: 64
height: 48
vnc:
id: remote
display_id: disp
host: 127.0.0.1
# Rewritten by the test to the fake server's port
port: 15900
reconnect_interval: 1s
# Longer than the time the next request may take, so the two can be told apart
update_delay: 300ms
max_update_delay: 500ms
on_connect:
- logger.log: "VNC_TEST_CONNECTED"
on_disconnect:
- logger.log: "VNC_TEST_DISCONNECTED"
+629
View File
@@ -0,0 +1,629 @@
"""A deterministic fake RFB (VNC) server for the vnc integration tests.
It behaves like the TightVNC 1.3.10 Xvnc that the Home Assistant VNC add-on runs: the same
handshake for RFB 3.3, 3.7 and 3.8, the same security lists and failure messages, and a
connection that closes on any client message type it does not know.
The framebuffer is an RGB888 image the test draws into. Updates are translated into the pixel
format the client asked for and encoded as Raw or a RichCursor shape. Every client
message is recorded, and the test decides when to answer: ``wait_for("fbur")`` then
``send_update(...)``.
"""
from __future__ import annotations
import asyncio
from collections.abc import Callable, Iterable, Sequence
import contextlib
from dataclasses import asdict, dataclass
import logging
import random
import re
import struct
from typing import Any, Literal, Self
_LOGGER = logging.getLogger(__name__)
SECURITY_NONE = 1
SECURITY_TIGHT = 16
ENCODING_RAW = 0
ENCODING_RICH_CURSOR = -239
#: ``chunk`` value that writes in random 1-7 byte pieces.
HOSTILE_CHUNK = -1
#: TightVNC closes the connection on a longer ClientCutText.
MAX_CLIENT_CUT_TEXT = 1 << 20
RGB = tuple[int, int, int]
Encoding = Literal["raw", "rich-cursor"]
Rect = tuple[int, int, int, int, Encoding]
Message = dict[str, Any]
_PIXEL_FORMAT = struct.Struct(">BBBBHHHBBB3x")
_VERSION_RE = re.compile(rb"RFB (\d{3})\.(\d{3})\n")
@dataclass(frozen=True)
class PixelFormat:
"""An RFB PIXEL_FORMAT. Field names match the keys of a set_pixel_format message."""
bpp: int
depth: int
big_endian: bool
true_colour: bool
red_max: int
green_max: int
blue_max: int
red_shift: int
green_shift: int
blue_shift: int
@classmethod
def unpack(cls, data: bytes) -> PixelFormat:
"""Decode the 16 byte wire form."""
bpp, depth, big_endian, true_colour, *rest = _PIXEL_FORMAT.unpack(data)
return cls(bpp, depth, bool(big_endian), bool(true_colour), *rest)
def pack(self) -> bytes:
"""Encode the 16 byte wire form."""
return _PIXEL_FORMAT.pack(*asdict(self).values())
@property
def bytes_per_pixel(self) -> int:
"""Bytes per pixel on the wire."""
return self.bpp // 8
def value_of(self, rgb: RGB) -> int:
"""The pixel value a real server sends for an RGB888 colour."""
r, g, b = rgb
value = (
(((r * self.red_max + 127) // 255) << self.red_shift)
| (((g * self.green_max + 127) // 255) << self.green_shift)
| (((b * self.blue_max + 127) // 255) << self.blue_shift)
)
return value & ((1 << self.bpp) - 1)
def to_bytes(self, value: int) -> bytes:
"""A pixel value in wire byte order."""
return value.to_bytes(
self.bytes_per_pixel, "big" if self.big_endian else "little"
)
#: TightVNC's native format at ``-depth 16`` on a little-endian host.
RGB565_LE = PixelFormat(16, 16, False, True, 31, 63, 31, 11, 5, 0)
RGB565_BE = PixelFormat(16, 16, True, True, 31, 63, 31, 11, 5, 0)
RGB332 = PixelFormat(8, 8, False, True, 7, 7, 3, 5, 2, 0)
def _string(text: str) -> bytes:
data = text.encode()
return struct.pack(">I", len(data)) + data
class _ProtocolError(Exception):
"""The client broke the protocol; TightVNC would close the connection."""
@dataclass(eq=False)
class _Session:
reader: asyncio.StreamReader
writer: asyncio.StreamWriter
pixel_format: PixelFormat
task: asyncio.Task[None] | None
minor: int = 8
class FakeRfbServer:
"""An RFB server with a scripted framebuffer, serving one client at a time.
A new connection closes the previous one, as the add-on's ``-nevershared`` does.
"""
def __init__(
self,
width: int,
height: int,
*,
version: bytes = b"RFB 003.008\n",
security_types: list[int] | None = None,
name: str = "fake rfb",
native_format: PixelFormat = RGB565_LE,
refuse_with_reason: str | None = None,
handshake_chunk: int | None = None,
seed: int = 0,
) -> None:
"""Set up the server; nothing listens until ``start()``.
Args:
width: Framebuffer width.
height: Framebuffer height.
version: The 12 byte ProtocolVersion the server sends.
security_types: The 3.7+ list. Defaults to TightVNC's ``[1, 16]``.
name: Desktop name in ServerInit.
native_format: Pixel format in ServerInit, used until SetPixelFormat.
refuse_with_reason: Refuse every connection before authentication with this
reason, as TightVNC does after too many failures.
handshake_chunk: ``chunk`` used for the server's handshake writes.
seed: Seed for the hostile chunk sizes.
"""
self.width = width
self.height = height
self.version = version
self.security_types = (
list(security_types)
if security_types is not None
else [SECURITY_NONE, SECURITY_TIGHT]
)
self.name = name
self.native_format = native_format
self.refuse_with_reason = refuse_with_reason
self.handshake_chunk = handshake_chunk
self.image = bytearray(width * height * 3)
#: Every parsed client message, from every connection, in arrival order.
self.messages: list[Message] = []
self.protocol_errors: list[str] = []
self.client_version: bytes | None = None
self.shared_flag: int | None = None
self.encodings: list[int] = []
self._rng = random.Random(seed)
self._server: asyncio.Server | None = None
self._session: _Session | None = None
self._open: set[_Session] = set()
self._pending: list[Message] = []
self._changed = asyncio.Event()
self._write_lock = asyncio.Lock()
self._port = 0
self._connections = 0
self._sessions = 0
async def __aenter__(self) -> Self:
"""Start on a free loopback port if not started yet."""
if self._server is None:
await self.start()
return self
async def __aexit__(self, *exc_info: object) -> None:
"""Stop the server."""
await self.stop()
@property
def port(self) -> int:
"""The bound port, 0 before ``start()``."""
return self._port
@property
def connections(self) -> int:
"""TCP connections accepted so far."""
return self._connections
@property
def sessions(self) -> int:
"""Connections that got as far as ServerInit."""
return self._sessions
@property
def client_connected(self) -> bool:
"""True while a client connection is open."""
return self._session is not None
@property
def pixel_format(self) -> PixelFormat:
"""The current client's pixel format, or the native one with no client."""
return self._session.pixel_format if self._session else self.native_format
async def start(self, host: str = "127.0.0.1", port: int = 0) -> int:
"""Listen and return the bound port."""
self._server = await asyncio.start_server(self._handle, host, port)
self._port = self._server.sockets[0].getsockname()[1]
return self._port
async def stop(self) -> None:
"""Close the listener and every connection, and wait for their handlers."""
if (server := self._server) is None:
return
self._server = None
server.close()
for session in list(self._open):
session.writer.transport.abort()
if tasks := [s.task for s in self._open if s.task is not None]:
_, pending = await asyncio.wait(tasks, timeout=5.0)
for task in pending:
task.cancel()
if pending:
await asyncio.wait(pending)
await server.wait_closed()
def set_pixel(self, x: int, y: int, rgb: RGB) -> None:
"""Set one framebuffer pixel."""
self.fill_rect(x, y, 1, 1, rgb)
def get_pixel(self, x: int, y: int) -> RGB:
"""Read one framebuffer pixel."""
self._check_rect(x, y, 1, 1)
offset = (y * self.width + x) * 3
r, g, b = self.image[offset : offset + 3]
return r, g, b
def fill_rect(self, x: int, y: int, w: int, h: int, rgb: RGB) -> None:
"""Fill a framebuffer rectangle with one colour."""
self._check_rect(x, y, w, h)
row = bytes(rgb) * w
for line in range(y, y + h):
offset = (line * self.width + x) * 3
self.image[offset : offset + w * 3] = row
async def wait_until(
self,
predicate: Callable[[], bool],
timeout: float = 5.0,
what: str = "condition",
) -> None:
"""Wait until ``predicate()`` is true; it is checked after every server event.
Raises:
AssertionError: If it is still false after ``timeout``.
"""
if not await self._wait(lambda: True if predicate() else None, timeout):
raise AssertionError(f"{what} not met within {timeout}s; {self._summary()}")
async def wait_for(
self,
type_name: str,
timeout: float = 5.0,
predicate: Callable[[Message], bool] | None = None,
) -> Message:
"""Return and consume the oldest unconsumed message of this type.
Messages of other types stay queued, so the order of calls does not matter. Only the
current connection's messages are queued: a new connection drops the old ones.
Raises:
AssertionError: If no matching message arrives within ``timeout``.
"""
def take() -> Message | None:
for index, message in enumerate(self._pending):
if message["type"] == type_name and (
predicate is None or predicate(message)
):
return self._pending.pop(index)
return None
if (message := await self._wait(take, timeout)) is None:
raise AssertionError(
f"no {type_name} message within {timeout}s; {self._summary()}"
)
return message
def pending(self) -> list[Message]:
"""Messages not consumed by ``wait_for()`` yet."""
return list(self._pending)
def discard_pending(self) -> None:
"""Drop every unconsumed message."""
self._pending.clear()
def build_update(
self,
rects: Sequence[Rect],
*,
pixel_format: PixelFormat | None = None,
) -> bytes:
"""Encode one FramebufferUpdate from the image.
Each rect is ``(x, y, w, h, encoding)``. For ``rich-cursor`` x and y are the hotspot
and the shape is a fixed pattern.
Args:
rects: The rectangles, in order.
pixel_format: Format to encode in; defaults to the current client's.
"""
fmt = pixel_format or self.pixel_format
out = bytearray(struct.pack(">BxH", 0, len(rects)))
for x, y, w, h, encoding in rects:
if encoding == "rich-cursor":
out += struct.pack(">HHHHi", x, y, w, h, ENCODING_RICH_CURSOR)
out += _cursor_shape(fmt, w, h)
continue
self._check_rect(x, y, w, h)
values = self._values(fmt, x, y, w, h)
if encoding == "raw":
out += struct.pack(">HHHHi", x, y, w, h, ENCODING_RAW)
out += _pixels(fmt, (v for row in values for v in row))
else:
raise ValueError(f"unknown encoding {encoding!r}")
return bytes(out)
async def send_update(
self,
rects: Sequence[Rect],
chunk: int | None = None,
*,
delay: float = 0.0,
) -> None:
"""Send one FramebufferUpdate built by ``build_update()``.
Args:
rects: The rectangles, in order.
chunk: Split the write into pieces of at most this many bytes; ``HOSTILE_CHUNK``
uses seeded random 1-7 byte pieces.
delay: Sleep between pieces.
"""
session = self._require_session()
data = self.build_update(rects, pixel_format=session.pixel_format)
await self._write(session, data, chunk, delay)
async def send_cut_text(
self, text: bytes, chunk: int | None = None, *, delay: float = 0.0
) -> None:
"""Send ServerCutText."""
await self.send_raw(
struct.pack(">B3xI", 3, len(text)) + text, chunk, delay=delay
)
async def send_bell(self) -> None:
"""Send Bell."""
await self.send_raw(b"\x02")
async def send_colour_map(
self,
first: int = 0,
colours: Sequence[RGB] = (),
chunk: int | None = None,
) -> None:
"""Send SetColourMapEntries; colours are 16 bit per channel."""
data = struct.pack(">BxHH", 1, first, len(colours))
data += b"".join(struct.pack(">HHH", *colour) for colour in colours)
await self.send_raw(data, chunk)
async def send_raw(
self, data: bytes, chunk: int | None = None, *, delay: float = 0.0
) -> None:
"""Send arbitrary bytes to the current client."""
await self._write(self._require_session(), data, chunk, delay)
async def close_client(self, *, abort: bool = False) -> None:
"""Close the current client connection and wait for its handler to finish."""
if (session := self._session) is None:
return
if abort:
session.writer.transport.abort()
else:
session.writer.close()
if session.task is not None:
await asyncio.wait([session.task], timeout=5.0)
def _check_rect(self, x: int, y: int, w: int, h: int) -> None:
if min(x, y, w, h) < 0 or x + w > self.width or y + h > self.height:
raise ValueError(
f"rect {w}x{h}+{x}+{y} is outside the {self.width}x{self.height} framebuffer"
)
def _values(
self, fmt: PixelFormat, x: int, y: int, w: int, h: int
) -> list[list[int]]:
cache: dict[bytes, int] = {}
rows = []
for line in range(y, y + h):
offset = (line * self.width + x) * 3
data = bytes(self.image[offset : offset + w * 3])
row = []
for i in range(0, len(data), 3):
rgb = data[i : i + 3]
if (value := cache.get(rgb)) is None:
value = cache[rgb] = fmt.value_of((rgb[0], rgb[1], rgb[2]))
row.append(value)
rows.append(row)
return rows
def _require_session(self) -> _Session:
if self._session is None:
raise RuntimeError("no RFB client is connected")
return self._session
def _summary(self) -> str:
return (
f"connections={self._connections}, sessions={self._sessions}, "
f"pending={[m['type'] for m in self._pending]}, "
f"protocol_errors={self.protocol_errors}"
)
def _bump(self) -> None:
self._changed.set()
self._changed = asyncio.Event()
async def _wait[T](self, check: Callable[[], T | None], timeout: float) -> T | None:
loop = asyncio.get_running_loop()
deadline = loop.time() + timeout
while (result := check()) is None:
if (remaining := deadline - loop.time()) <= 0:
return None
with contextlib.suppress(TimeoutError):
await asyncio.wait_for(self._changed.wait(), remaining)
return result
async def _write(
self, session: _Session, data: bytes, chunk: int | None, delay: float = 0.0
) -> None:
if chunk is not None and chunk != HOSTILE_CHUNK and chunk < 1:
raise ValueError(f"bad chunk size {chunk}")
writer = session.writer
async with self._write_lock:
if chunk is None:
writer.write(data)
await writer.drain()
return
pos = 0
while pos < len(data):
size = self._rng.randint(1, 7) if chunk == HOSTILE_CHUNK else chunk
writer.write(data[pos : pos + size])
await writer.drain()
pos += size
await asyncio.sleep(delay)
async def _reply(self, session: _Session, data: bytes) -> None:
await self._write(session, data, self.handshake_chunk)
def _record(self, session: _Session, message: Message) -> None:
self.messages.append(message)
if session is self._session:
self._pending.append(message)
self._bump()
async def _handle(
self, reader: asyncio.StreamReader, writer: asyncio.StreamWriter
) -> None:
session = _Session(reader, writer, self.native_format, asyncio.current_task())
self._connections += 1
if (previous := self._session) is not None:
previous.writer.close()
self._session = session
self._open.add(session)
self._pending.clear()
self._bump()
try:
await self._serve(session)
except (asyncio.IncompleteReadError, ConnectionError):
pass
except _ProtocolError as err:
self.protocol_errors.append(str(err))
except Exception as err: # noqa: BLE001 - reported through protocol_errors
_LOGGER.exception("Fake RFB server failed")
self.protocol_errors.append(f"internal error: {err!r}")
finally:
if self._session is session:
self._session = None
writer.close()
with contextlib.suppress(OSError):
await writer.wait_closed()
self._open.discard(session)
self._bump()
async def _serve(self, session: _Session) -> None:
await self._reply(session, self.version)
session.minor = self._negotiate_version(await session.reader.readexactly(12))
if not await self._negotiate_security(session):
return
self.shared_flag = (await session.reader.readexactly(1))[0]
server_init = (
struct.pack(">HH", self.width, self.height)
+ self.native_format.pack()
+ _string(self.name)
)
await self._reply(session, server_init)
self._sessions += 1
self._bump()
while True:
await self._read_message(session)
def _negotiate_version(self, data: bytes) -> int:
self.client_version = data
if (match := _VERSION_RE.fullmatch(data)) is None:
raise _ProtocolError(f"bad client version {data!r}")
if int(match[1]) != 3:
raise _ProtocolError(f"unsupported client version {data!r}")
minor = int(match[2])
if minor > 8:
return 8
return minor if minor in (7, 8) else 3
async def _negotiate_security(self, session: _Session) -> bool:
reason = self.refuse_with_reason
if reason is None and not self.security_types:
reason = ""
if session.minor == 3:
if reason is not None:
await self._reply(session, struct.pack(">I", 0) + _string(reason))
return False
chosen = next(
(t for t in self.security_types if t == SECURITY_NONE),
self.security_types[0],
)
await self._reply(session, struct.pack(">I", chosen))
else:
if reason is not None:
await self._reply(session, b"\0" + _string(reason))
return False
offer = bytes([len(self.security_types), *self.security_types])
await self._reply(session, offer)
chosen = (await session.reader.readexactly(1))[0]
if chosen not in self.security_types:
raise _ProtocolError(
f"client chose security type {chosen}, offered {self.security_types}"
)
if chosen == SECURITY_NONE:
if session.minor >= 8:
await self._reply(session, struct.pack(">I", 0))
return True
raise _ProtocolError(f"security type {chosen} is not emulated")
async def _read_message(self, session: _Session) -> None:
read = session.reader.readexactly
match kind := (await read(1))[0]:
case 0:
fmt = PixelFormat.unpack((await read(19))[3:])
self._record(session, {"type": "set_pixel_format", **asdict(fmt)})
if fmt.bpp not in (8, 16, 32):
raise _ProtocolError(f"unsupported bits per pixel {fmt.bpp}")
if not fmt.true_colour:
raise _ProtocolError("colour map formats are not emulated")
session.pixel_format = fmt
case 2:
(count,) = struct.unpack(">xH", await read(3))
self.encodings = list(
struct.unpack(f">{count}i", await read(4 * count))
)
self._record(
session, {"type": "set_encodings", "encodings": self.encodings}
)
case 3:
incremental, x, y, w, h = struct.unpack(">BHHHH", await read(9))
self._record(
session,
{
"type": "fbur",
"incremental": bool(incremental),
"x": x,
"y": y,
"w": w,
"h": h,
},
)
case 4:
down, keysym = struct.unpack(">B2xI", await read(7))
self._record(
session, {"type": "key", "down": bool(down), "keysym": keysym}
)
case 5:
mask, x, y = struct.unpack(">BHH", await read(5))
self._record(session, {"type": "pointer", "mask": mask, "x": x, "y": y})
case 6:
(length,) = struct.unpack(">3xI", await read(7))
if length > MAX_CLIENT_CUT_TEXT:
raise _ProtocolError(f"client cut text of {length} bytes")
self._record(session, {"type": "cut_text", "text": await read(length)})
case _:
raise _ProtocolError(f"unknown client message type {kind}")
def _pixels(fmt: PixelFormat, values: Iterable[int]) -> bytes:
cache: dict[int, bytes] = {}
out = bytearray()
for value in values:
if (data := cache.get(value)) is None:
data = cache[value] = fmt.to_bytes(value)
out += data
return bytes(out)
def _cursor_shape(fmt: PixelFormat, w: int, h: int) -> bytes:
light = fmt.to_bytes(fmt.value_of((255, 255, 255)))
dark = fmt.to_bytes(fmt.value_of((0, 0, 0)))
pixels = b"".join(
light if (x + y) % 2 else dark for y in range(h) for x in range(w)
)
return pixels + b"\x5a" * (((w + 7) // 8) * h)
@@ -0,0 +1,31 @@
"""Lambdas rendered at file scope can use the user's includes, which can name ids."""
from __future__ import annotations
from aioesphomeapi import SensorInfo, SensorState
import pytest
from .state_utils import require_entity, wait_for_state
from .types import APIClientConnectedFactory, RunCompiledFunction
@pytest.mark.asyncio
async def test_includes_lambda_order(
yaml_config: str,
run_compiled: RunCompiledFunction,
api_client_connected: APIClientConnectedFactory,
) -> None:
"""The apply action and the sensor filter use the header, the header reads the global."""
async with run_compiled(yaml_config), api_client_connected() as client:
entities, _ = await client.list_entities_services()
sensor = require_entity(entities, "header_value", SensorInfo)
state = await wait_for_state(
client,
lambda s: (
isinstance(s, SensorState)
and s.key == sensor.key
and not s.missing_state
),
)
# globals.set stored HEADER_CONSTANT and the header read it back doubled.
assert state.state == 84.0
+466
View File
@@ -0,0 +1,466 @@
"""Integration test for the vnc component against a fake RFB server.
One compiled device runs every step in order against one server: the first frame, request
pacing, a reconnect after the server closes the link, a stream cut into tiny pieces, and an
unknown message type that makes the client drop the link.
The client asks for the next frame only once the previous one is fully drawn, so snapshots are
taken after that request arrives. The snapshot display draws as soon as pixels arrive, so the
picture does not depend on when the client refreshes the display.
"""
from __future__ import annotations
import asyncio
from collections.abc import Callable
import contextlib
from dataclasses import asdict
from itertools import pairwise
from pathlib import Path
import random
import re
import struct
from aioesphomeapi import APIClient, UserService
import pytest
from .bmp_utils import Bmp, wait_for_bmp
from .rfb_fake_server import (
HOSTILE_CHUNK,
RGB,
RGB565_BE,
RGB565_LE,
FakeRfbServer,
Message,
PixelFormat,
Rect,
)
from .types import APIClientConnectedFactory, RunCompiledFunction
DISPLAY_WIDTH = 64
DISPLAY_HEIGHT = 48
SERVER_WIDTH = 80
SERVER_HEIGHT = 60
FIXTURE_PORT = "port: 15900"
CONNECTED = "VNC_TEST_CONNECTED"
DISCONNECTED = "VNC_TEST_DISCONNECTED"
FRAME_LOG = re.compile(r"Update: (\d+) bytes in \d+ ?ms")
LONG_OPERATION = "vnc took a long time"
# Well under the fixture's 300 ms update_delay: the next request must not wait for the refresh.
QUICK_REQUEST_S = 0.25
# Requests are at least 50 ms apart; leave room for jitter in when they arrive.
REQUEST_SPACING_S = 0.035
# The fixture's reconnect_interval.
RECONNECT_S = 1.0
# Long enough for a frame that drew something to be logged.
LOG_SETTLE_S = 0.5
# Raw rects that cross the right and bottom edges of the display.
FULL_FRAME: list[Rect] = [
(0, 0, SERVER_WIDTH, 13, "raw"),
(0, 13, 37, SERVER_HEIGHT - 13, "raw"),
(37, 13, SERVER_WIDTH - 37, SERVER_HEIGHT - 13, "raw"),
]
LogMatch = str | re.Pattern[str] | Callable[[str], bool]
class LogWatch:
"""Device log lines with the time each one arrived."""
def __init__(self) -> None:
self.lines: list[tuple[float, str]] = []
self._changed = asyncio.Event()
def callback(self, line: str) -> None:
self.lines.append((asyncio.get_running_loop().time(), line))
self._changed.set()
self._changed = asyncio.Event()
def times(self, match: LogMatch) -> list[float]:
"""When each matching line came."""
if isinstance(match, str):
text = match
def test(line: str) -> bool:
return text in line
elif isinstance(match, re.Pattern):
pattern = match
def test(line: str) -> bool:
return pattern.search(line) is not None
else:
test = match
return [when for when, line in self.lines if test(line)]
def count(self, match: LogMatch) -> int:
return len(self.times(match))
def frame_sizes(self) -> list[int]:
"""The byte counts of the 'Update:' lines, in order."""
return [
int(found[1]) for _, line in self.lines if (found := FRAME_LOG.search(line))
]
async def wait_count(
self, match: LogMatch, count: int, timeout: float = 10.0
) -> float:
"""Wait until ``match`` was logged ``count`` times and return when the last one came."""
loop = asyncio.get_running_loop()
deadline = loop.time() + timeout
while len(times := self.times(match)) < count:
if (remaining := deadline - loop.time()) <= 0:
raise AssertionError(
f"{match!r} logged {len(times)} times within {timeout}s, "
f"expected {count}"
)
with contextlib.suppress(TimeoutError):
await asyncio.wait_for(self._changed.wait(), remaining)
return times[count - 1]
def paint(server: FakeRfbServer, seed: int) -> None:
"""Draw solid bands, boxes and noise.
Noise also runs along the right and bottom edges of the display, so clipping errors show.
"""
rng = random.Random(seed)
def colour() -> RGB:
return rng.randrange(256), rng.randrange(256), rng.randrange(256)
width, height = server.width, server.height
for x in range(0, width, 20):
server.fill_rect(x, 0, min(20, width - x), height, colour())
accent = colour()
for _ in range(10):
server.fill_rect(
rng.randrange(width - 8),
rng.randrange(height - 8),
rng.randint(1, 8),
rng.randint(1, 8),
accent,
)
for _ in range(10):
server.fill_rect(
rng.randrange(width - 4),
rng.randrange(height - 4),
rng.randint(1, 4),
rng.randint(1, 4),
colour(),
)
noise = [
(x, y)
for x in range(DISPLAY_WIDTH - 6, min(width, DISPLAY_WIDTH + 6))
for y in range(height)
]
noise += [
(x, y)
for y in range(DISPLAY_HEIGHT - 5, min(height, DISPLAY_HEIGHT + 5))
for x in range(width)
]
noise += [(x, y) for x in range(8, 24) for y in range(16, 32)]
for x, y in noise:
server.set_pixel(x, y, colour())
def shown_colour(fmt: PixelFormat, rgb: RGB) -> RGB:
"""The colour a snapshot holds for a server pixel, after the trip through RGB565."""
value = fmt.value_of(rgb)
return (
((value >> fmt.red_shift) & fmt.red_max) * 255 // fmt.red_max,
((value >> fmt.green_shift) & fmt.green_max) * 255 // fmt.green_max,
((value >> fmt.blue_shift) & fmt.blue_max) * 255 // fmt.blue_max,
)
def assert_shows(bmp: Bmp, server: FakeRfbServer) -> None:
"""Assert the snapshot is the top-left of the server screen, pixel for pixel."""
assert (bmp.width, bmp.height, bmp.bits) == (DISPLAY_WIDTH, DISPLAY_HEIGHT, 24)
mismatches = []
for y in range(DISPLAY_HEIGHT):
row = (DISPLAY_HEIGHT - 1 - y) * DISPLAY_WIDTH * 3
for x in range(DISPLAY_WIDTH):
b, g, r = bmp.pixels[row + x * 3 : row + x * 3 + 3]
expected = shown_colour(RGB565_LE, server.get_pixel(x, y))
if (r, g, b) != expected:
mismatches.append(((x, y), (r, g, b), expected))
assert not mismatches, (
f"{len(mismatches)} pixels differ; (x, y), shown, expected: {mismatches[:10]}"
)
def full_request(incremental: bool) -> Message:
return {
"type": "fbur",
"incremental": incremental,
"x": 0,
"y": 0,
"w": DISPLAY_WIDTH,
"h": DISPLAY_HEIGHT,
}
def cut_text(text: bytes) -> bytes:
return struct.pack(">B3xI", 3, len(text)) + text
class Device:
"""The API actions of the fixture."""
def __init__(
self, client: APIClient, services: list[UserService], snapshot_dir: Path
) -> None:
self.client = client
self.actions = {service.name: service for service in services}
self.snapshot_dir = snapshot_dir
async def run(self, action: str, **data: int | str) -> None:
await self.client.execute_service(self.actions[action], data)
async def snapshot(self, name: str) -> Bmp:
await self.run("take_snapshot", filename=name)
return await wait_for_bmp(self.snapshot_dir / f"{name}.bmp")
async def next_request(
server: FakeRfbServer, timeout: float = 5.0
) -> tuple[Message, float]:
"""Wait for the next FramebufferUpdateRequest and return it and when it came."""
request = await server.wait_for("fbur", timeout=timeout)
return request, asyncio.get_running_loop().time()
async def exchange(
server: FakeRfbServer,
data: bytes,
*,
log: LogWatch | None = None,
frame: bytes | None = None,
chunk: int | None = None,
timeout: float = 5.0,
) -> tuple[float, float]:
"""Send bytes that end a frame and wait for the incremental request that follows.
With ``log``, also wait for the frame's 'Update:' line and check its byte count; ``frame``
is the FramebufferUpdate inside ``data`` when other messages surround it.
Returns how long after the write the request came, and when it came.
"""
logged = log.count(FRAME_LOG) if log is not None else 0
await server.send_raw(data, chunk)
sent = asyncio.get_running_loop().time()
request, requested = await next_request(server, timeout)
assert request == full_request(incremental=True)
if log is not None:
await expect_logged(log, data if frame is None else frame, logged)
return requested - sent, requested
async def expect_logged(log: LogWatch, frame: bytes, before: int) -> None:
"""Wait for the 'Update:' line after the first ``before`` and check its byte count."""
await log.wait_count(FRAME_LOG, before + 1)
sizes = log.frame_sizes()
assert len(sizes) == before + 1, f"expected {before + 1} logged frames: {sizes}"
# The four byte message header may or may not be counted.
assert sizes[-1] in (len(frame) - 4, len(frame)), (
f"logged {sizes[-1]} bytes for a {len(frame)} byte update"
)
@pytest.mark.asyncio
async def test_vnc_client(
yaml_config: str,
run_compiled: RunCompiledFunction,
api_client_connected: APIClientConnectedFactory,
tmp_path: Path,
monkeypatch: pytest.MonkeyPatch,
) -> None:
"""The client renders, paces and recovers from drops and bad streams."""
snapshot_dir = tmp_path / "snapshots"
monkeypatch.setenv("ESPHOME_SNAPSHOT_DIR", str(snapshot_dir))
loop = asyncio.get_running_loop()
log = LogWatch()
server = FakeRfbServer(SERVER_WIDTH, SERVER_HEIGHT, name="vnc client test")
paint(server, seed=1)
async with server:
assert FIXTURE_PORT in yaml_config
yaml_config = yaml_config.replace(FIXTURE_PORT, f"port: {server.port}")
async with (
run_compiled(yaml_config, line_callback=log.callback),
api_client_connected() as client,
):
_, services = await client.list_entities_services()
device = Device(client, services, snapshot_dir)
# Render: the session setup is exact and the first frame is pixel-exact.
pixel_format = await server.wait_for("set_pixel_format", timeout=15.0)
# The snapshot display declares no byte order, so the client asks for big-endian pixels
assert pixel_format == {"type": "set_pixel_format", **asdict(RGB565_BE)}
encodings = await server.wait_for("set_encodings")
assert encodings["encodings"] == [0, -239]
request, _ = await next_request(server)
assert request == full_request(incremental=False)
assert [m["type"] for m in server.messages[:3]] == [
"set_pixel_format",
"set_encodings",
"fbur",
]
assert server.client_version == b"RFB 003.008\n"
assert server.shared_flag == 1
await log.wait_count(CONNECTED, 1)
assert log.count("Server 'vnc client test', 80x60, RFB 3.8") == 1
assert log.count("showing the top-left 64x48") == 1
elapsed, _ = await exchange(
server, server.build_update(FULL_FRAME), log=log
)
assert elapsed < QUICK_REQUEST_S, f"next request after {elapsed:.3f}s"
assert_shows(await device.snapshot("render"), server)
# Pacing: one request stays parked while nothing changes.
await asyncio.sleep(1.0)
assert not [m for m in server.pending() if m["type"] == "fbur"]
# The next request goes out as soon as a frame is drawn, before the refresh.
server.fill_rect(30, 10, 10, 10, (250, 10, 10))
server.fill_rect(58, 40, 22, 20, (10, 250, 10))
frame = server.build_update(
[(30, 10, 10, 10, "raw"), (58, 40, 22, 20, "raw")]
)
elapsed, _ = await exchange(server, frame, log=log)
assert elapsed < QUICK_REQUEST_S, f"next request after {elapsed:.3f}s"
assert_shows(await device.snapshot("pacing"), server)
# A frame that only changes what the display cannot show draws nothing.
logged = log.count(FRAME_LOG)
server.fill_rect(
DISPLAY_WIDTH, 0, SERVER_WIDTH - DISPLAY_WIDTH, SERVER_HEIGHT, (1, 2, 3)
)
server.fill_rect(
0,
DISPLAY_HEIGHT,
SERVER_WIDTH,
SERVER_HEIGHT - DISPLAY_HEIGHT,
(200, 100, 50),
)
hidden = server.build_update(
[
(
DISPLAY_WIDTH,
0,
SERVER_WIDTH - DISPLAY_WIDTH,
SERVER_HEIGHT,
"raw",
),
(
0,
DISPLAY_HEIGHT,
SERVER_WIDTH,
SERVER_HEIGHT - DISPLAY_HEIGHT,
"raw",
),
]
)
elapsed, requested = await exchange(server, hidden)
assert elapsed < QUICK_REQUEST_S, f"next request after {elapsed:.3f}s"
# Empty and cursor-only updates draw nothing either, and requests stay 50 ms
# apart.
requested_at = [requested]
for update in (
server.build_update([]),
*(
server.build_update([(2 + i, 3, 10, 16, "rich-cursor")])
for i in range(3)
),
):
elapsed, requested = await exchange(server, update)
assert elapsed < QUICK_REQUEST_S, f"next request after {elapsed:.3f}s"
requested_at.append(requested)
gaps = [later - earlier for earlier, later in pairwise(requested_at)]
assert min(gaps) >= REQUEST_SPACING_S, f"requests {gaps} apart"
await asyncio.sleep(LOG_SETTLE_S)
assert log.count(FRAME_LOG) == logged, (
"a frame that drew nothing was logged"
)
assert_shows(await device.snapshot("hidden"), server)
# Reconnect: the server closes the link; the client comes back after the backoff
# and sets the session up again.
server.discard_pending()
closed = loop.time()
await server.close_client()
await log.wait_count(DISCONNECTED, 1)
await server.wait_until(
lambda: server.connections == 2, timeout=10.0, what="second connection"
)
reconnected = loop.time()
assert reconnected - closed >= RECONNECT_S - 0.05, (
f"reconnected after {reconnected - closed:.3f}s"
)
await log.wait_count(CONNECTED, 2)
assert await server.wait_for("set_pixel_format") == pixel_format
assert (await server.wait_for("set_encodings"))["encodings"] == [0, -239]
request, _ = await next_request(server)
assert request == full_request(incremental=False)
assert server.sessions == 2
# Hostile stream: 1-7 byte pieces, a long cut text, bells, a colour map and
# cursor rects around a frame.
paint(server, seed=2)
frame = server.build_update(
[
(3, 4, 12, 18, "rich-cursor"),
(0, 0, DISPLAY_WIDTH, 20, "raw"),
(DISPLAY_WIDTH, 0, SERVER_WIDTH - DISPLAY_WIDTH, 60, "raw"),
(0, 0, 0, 0, "rich-cursor"),
(0, 20, 30, SERVER_HEIGHT - 20, "raw"),
(30, 20, SERVER_WIDTH - 30, SERVER_HEIGHT - 20, "raw"),
(1, 1, 7, 9, "rich-cursor"),
]
)
colour_map = struct.pack(">BxHH", 1, 0, 2) + bytes(12)
long_cut = bytes(
random.Random(3).randrange(32, 127) for _ in range(200_000)
)
payload = (
cut_text(long_cut)
+ b"\x02"
+ colour_map
+ frame
+ b"\x02"
+ cut_text(b"")
)
await exchange(
server, payload, log=log, frame=frame, chunk=HOSTILE_CHUNK, timeout=30.0
)
assert_shows(await device.snapshot("hostile"), server)
# An unknown message type drops the link. This session was too short to count as
# stable, so the backoff has doubled.
server.discard_pending()
sent = loop.time()
await server.send_raw(b"\x09")
await log.wait_count(DISCONNECTED, 2)
assert log.count("Unknown message type 9") == 1
await server.wait_until(
lambda: server.connections == 3, timeout=15.0, what="third connection"
)
assert loop.time() - sent >= 2 * RECONNECT_S - 0.1, (
f"reconnected after {loop.time() - sent:.3f}s"
)
await log.wait_count(CONNECTED, 3)
request, _ = await next_request(server)
assert request == full_request(incremental=False)
paint(server, seed=6)
await exchange(server, server.build_update(FULL_FRAME), log=log)
assert_shows(await device.snapshot("third"), server)
assert server.protocol_errors == []
assert log.count(LONG_OPERATION) == 0
@@ -62,9 +62,8 @@ def test_write_idf_component_yml_passes_managed_components(
converted.get_sanitized_name.return_value = "esphome/other-lib"
converted.path = tmp_path / "pio_components" / "other-lib"
def fake_generate_idf_components(libraries, managed=None, provides=None):
def fake_generate_idf_components(libraries, managed=None):
captured["managed"] = managed
captured["provides"] = provides
return [converted]
monkeypatch.setattr(esp32, "generate_idf_components", fake_generate_idf_components)
@@ -72,8 +71,6 @@ def test_write_idf_component_yml_passes_managed_components(
esp32._write_idf_component_yml()
assert captured["managed"] == {"esphome/noise-c"}
# ESP-IDF bundles no Arduino libraries, so every name may be fetched
assert captured["provides"] is None
# The managed component itself is still written into the manifest deps
# directly (from KEY_COMPONENTS), just not converted a second time.
yml_path = tmp_path / "src" / "idf_component.yml"
@@ -96,7 +93,7 @@ def test_write_idf_component_yml_empty_managed_when_no_components(
captured: dict[str, set[str] | None] = {}
def fake_generate_idf_components(libraries, managed=None, provides=None):
def fake_generate_idf_components(libraries, managed=None):
captured["managed"] = managed
return []
@@ -107,50 +104,6 @@ def test_write_idf_component_yml_empty_managed_when_no_components(
assert captured["managed"] == set()
def test_write_idf_component_yml_marks_arduino_bundled_libraries_provided(
tmp_path: Path,
monkeypatch: pytest.MonkeyPatch,
) -> None:
"""Under Arduino the converter is told which names the core bundles, so a
manifest dependency on Wire or SPI is never fetched from the registry."""
_setup_core(tmp_path)
CORE.data[KEY_CORE][KEY_TARGET_FRAMEWORK] = str(Framework.ARDUINO)
CORE.data[KEY_CORE][KEY_FRAMEWORK_VERSION] = cv.Version(3, 3, 12)
CORE.data[esp32.KEY_ESP32] = {
esp32.KEY_COMPONENTS: {},
esp32.KEY_IDF_VERSION: cv.Version(5, 5, 4),
esp32.KEY_ARDUINO_LIBRARIES: {"Wire", "SPI"},
}
captured: dict[str, object] = {}
def fake_generate_idf_components(libraries, managed=None, provides=None):
captured["provides"] = provides
return []
monkeypatch.setattr(esp32, "generate_idf_components", fake_generate_idf_components)
esp32._write_idf_component_yml()
provides = captured["provides"]
assert provides("Wire") and provides("SPI")
assert not provides("Adafruit BusIO")
def test_arduino_provides_warns_for_a_bundled_library_not_enabled(
caplog: pytest.LogCaptureFixture,
) -> None:
"""A manifest dependency on a bundled Arduino library is never fetched, but
the library still has to be enabled, so an unenabled one is called out."""
CORE.reset()
CORE.data[esp32.KEY_ESP32] = {esp32.KEY_ARDUINO_LIBRARIES: {"Wire"}}
assert esp32._arduino_provides("Wire")
assert "Wire" not in caplog.text
assert esp32._arduino_provides("SPI")
assert "Arduino library SPI, which is not enabled" in caplog.text
assert not esp32._arduino_provides("Adafruit BusIO")
@pytest.mark.parametrize(
("version", "libsodium_stubbed"),
[
@@ -247,9 +200,7 @@ def test_write_idf_component_yml_stub_yields_to_converted_library_of_same_name(
converted.get_sanitized_name.return_value = "esphome/libsodium"
converted.path = tmp_path / "pio_components" / "esphome" / "libsodium"
monkeypatch.setattr(
esp32,
"generate_idf_components",
lambda libraries, managed=None, provides=None: [converted],
esp32, "generate_idf_components", lambda libraries, managed=None: [converted]
)
esp32._write_idf_component_yml()
+8 -4
View File
@@ -327,10 +327,14 @@ def test_wisp_requires_idf_5_1(wisp_core: None) -> None:
@pytest.mark.usefixtures("wisp_core")
def test_wisp_requires_sta_network() -> None:
"""WISP has nothing to share without an upstream network."""
with pytest.raises(cv.Invalid, match="at least one STA network"):
_final_validate({CONF_NETWORKS: [], CONF_AP: _wisp_ap()})
def test_wisp_requires_sta_network_or_ethernet() -> None:
"""WISP has nothing to share without an upstream network; ethernet is one too."""
ap_only = {CONF_NETWORKS: [], CONF_AP: _wisp_ap()}
with pytest.raises(cv.Invalid, match="ethernet as the upstream"):
_final_validate(ap_only)
_final_validate(ap_only, {"ethernet": {}})
with pytest.raises(cv.Invalid, match="ethernet manual_ip needs dns1"):
_final_validate(ap_only, {"ethernet": {CONF_MANUAL_IP: _manual_ip("0.0.0.0")}})
@pytest.mark.usefixtures("wisp_core")
+35 -1
View File
@@ -26,7 +26,7 @@ from esphome.const import (
KEY_TARGET_PLATFORM,
Toolchain,
)
from esphome.core import CORE, KEY_CONTROLLER_REGISTRY_CONTROLLERS, config
from esphome.core import CORE, ID, KEY_CONTROLLER_REGISTRY_CONTROLLERS, config
from esphome.core.config import (
CONF_RELATION,
CONF_SUSPEND_LOOP,
@@ -1114,6 +1114,40 @@ async def test_add_includes_with_single_file(
assert any('#include "my_header.h"' in inc for inc in includes_added)
@pytest.mark.asyncio
async def test_add_includes_sit_between_declarations_and_generated_code(
tmp_path: Path, mock_copy_file_if_changed: Mock
) -> None:
"""A header can name an id, and generated functions and tables can use the header."""
CORE.config_path = tmp_path / "config.yaml"
CORE.build_path = tmp_path / "build"
CORE.build_path.mkdir(parents=True, exist_ok=True)
header = tmp_path / "my_header.h"
header.write_text("static constexpr int MY_CONSTANT = 42;")
CORE.config = {}
value = cg.new_Pvariable(ID("my_value", is_declaration=True, type=cg.int32))
cg.static_function(
"esphome__my_action__fn", cg.void, [], [f"*{value} = MY_CONSTANT;"]
)
cg.shared_progmem_array("my_table", cg.int32, [cg.RawExpression("MY_CONSTANT")])
await config.add_includes([str(header)])
# A job that runs after the includes still lands before the generated code.
cg.add_global(cg.RawStatement("static constexpr int FINAL_JOB = 1;"))
section = CORE.cpp_global_section
expected_order = [
"*const my_value",
"static void esphome__my_action__fn();",
'#include "my_header.h"',
"static constexpr int FINAL_JOB = 1;",
"static void esphome__my_action__fn() {",
"my_table[] PROGMEM",
]
positions = [section.index(text) for text in expected_order]
assert positions == sorted(positions), section
@pytest.mark.asyncio
@pytest.mark.skipif(os.name == "nt", reason="Unix-specific test")
async def test_add_includes_with_directory_unix(
+4 -3
View File
@@ -846,14 +846,15 @@ def test_library_info_dot_a_linkage_malformed_raises(tmp_path: Path) -> None:
def test_bundled_library_properties_depends_warns(
tmp_path: Path, caplog: pytest.LogCaptureFixture
) -> None:
"""The library.properties depends= spelling is a dependency list too, so a
bundled manifest using it gets the same visibility warning."""
"""The library.properties depends= spelling reaches the visibility
warning too; the shared parser returns it raw."""
framework = _make_framework(tmp_path)
wire = framework / "libraries" / "Wire"
(wire / "library.properties").write_text("name=Wire\nversion=1.0\ndepends=SPI\n")
_add_library("Wire", None)
caplog.set_level("INFO")
_resolve(framework)
assert "Bundled library Wire declares dependencies" in caplog.text
assert "Library Wire declares dependencies via library.properties" in caplog.text
def test_bundled_library_extra_script_raises(tmp_path: Path) -> None:
+24 -5
View File
@@ -785,8 +785,9 @@ class MockCodegen(NamedTuple):
new_pvariable: MagicMock
register_parented: AsyncMock
add_global: MagicMock
add_global_after_includes: MagicMock
add: MagicMock
calls: MagicMock # new_pvariable and add_global attached, to check their order
calls: MagicMock # the global emitters and new_pvariable attached, to check their order
@pytest.fixture
@@ -848,6 +849,9 @@ def mock_cg() -> Generator[MockCodegen]:
"esphome.codegen.register_parented", new_callable=AsyncMock
) as register_parented,
patch("esphome.cpp_generator.add_global") as add_global,
patch(
"esphome.cpp_generator.add_global_after_includes"
) as add_global_after_includes,
patch("esphome.codegen.add") as add,
):
get_variable.return_value = PARENT_OBJ
@@ -855,8 +859,15 @@ def mock_cg() -> Generator[MockCodegen]:
calls = MagicMock()
calls.attach_mock(new_pvariable, "new_pvariable")
calls.attach_mock(add_global, "add_global")
calls.attach_mock(add_global_after_includes, "add_global_after_includes")
yield MockCodegen(
get_variable, new_pvariable, register_parented, add_global, add, calls
get_variable,
new_pvariable,
register_parented,
add_global,
add_global_after_includes,
add,
calls,
)
@@ -984,7 +995,7 @@ async def _run_apply_condition(
def _apply_definition(mock_cg: MockCodegen) -> str:
return str(mock_cg.add_global.call_args.args[0])
return str(mock_cg.add_global_after_includes.call_args.args[0])
@pytest.mark.asyncio
@@ -998,8 +1009,16 @@ async def test_register_apply_action_entry(
action_id, template_arg = mock_cg.new_pvariable.call_args.args
assert action_id == ID("obj_1")
assert str(template_arg) == "<esphome__obj_1__fn, int32_t>"
# The definition must precede the storage line that names the function.
assert [c[0] for c in mock_cg.calls.mock_calls] == ["add_global", "new_pvariable"]
# The declaration must precede the storage line that names the function; the body
# goes after the user includes.
assert [c[0] for c in mock_cg.calls.mock_calls] == [
"add_global",
"add_global_after_includes",
"new_pvariable",
]
assert str(mock_cg.add_global.call_args.args[0]) == (
"static void esphome__obj_1__fn(const std::remove_cvref_t<int32_t> & x);"
)
assert _apply_definition(mock_cg).startswith(
"static void esphome__obj_1__fn(const std::remove_cvref_t<int32_t> & x) {"
)
+25 -4
View File
@@ -866,7 +866,8 @@ class TestSharedProgmemArray:
c = cg.shared_progmem_array("table", ct.uint8, [4])
assert a is b
assert str(a) != str(c)
assert sum("PROGMEM" in str(st) for st in CORE.global_statements) == 2
late = CORE.global_statements_after_includes
assert sum("PROGMEM" in str(st) for st in late) == 2
assert not any("PROGMEM" in str(st) for st in CORE.main_statements)
def test_share_false_always_emits_a_new_array(self) -> None:
@@ -874,7 +875,8 @@ class TestSharedProgmemArray:
a = cg.shared_progmem_array("table", ct.uint8, [1], share=False)
b = cg.shared_progmem_array("table", ct.uint8, [1], share=False)
assert str(a) != str(b)
assert sum("PROGMEM" in str(st) for st in CORE.global_statements) == 2
late = CORE.global_statements_after_includes
assert sum("PROGMEM" in str(st) for st in late) == 2
def test_is_static_pointer_only_for_placement_new(self) -> None:
CORE.config = {}
@@ -910,7 +912,9 @@ class TestSharedProgmemArray:
CORE.config = {}
cg.shared_progmem_array("table", ct.uint8, [1], constexpr=False)
(statement,) = (
str(st) for st in CORE.global_statements if "PROGMEM" in str(st)
str(st)
for st in CORE.global_statements_after_includes
if "PROGMEM" in str(st)
)
assert statement.startswith(
"ESPHOME_FLASH_CONSTINIT static uint8_t const table[] PROGMEM = {1}"
@@ -948,7 +952,7 @@ class TestProgmemString:
assert a is b
assert str(a) == "progmem_str"
assert str(c) == "progmem_str_2"
assert [str(st) for st in CORE.global_statements] == [
assert [str(st) for st in CORE.global_statements_after_includes] == [
'static constexpr char progmem_str[] PROGMEM = "level";',
'static constexpr char progmem_str_2[] PROGMEM = "say \\042hi\\042";',
]
@@ -957,6 +961,7 @@ class TestProgmemString:
self._set_platform(PLATFORM_ESP32)
assert str(cg.shared_progmem_string('say "hi"')) == '"say \\042hi\\042"'
assert not CORE.global_statements
assert not CORE.global_statements_after_includes
def test_extern_progmem_array_is_an_externally_linked_global() -> None:
@@ -967,6 +972,22 @@ def test_extern_progmem_array_is_an_externally_linked_global() -> None:
]
def test_static_function_declares_early_and_defines_after_includes() -> None:
fn = cg.static_function("esphome__act__fn", ct.void, [(ct.int32, "x")], ["return;"])
assert str(fn) == "esphome__act__fn"
assert [str(st) for st in CORE.global_statements] == [
"static void esphome__act__fn(int32_t x);"
]
assert [str(st) for st in CORE.global_statements_after_includes] == [
"static void esphome__act__fn(int32_t x) {\n return;\n}"
]
def test_add_global_after_includes_rejects_non_statements() -> None:
with pytest.raises(ValueError, match="must be expression or statement"):
CORE.add_global_after_includes("not a statement")
class TestEnumOptions:
def test_class_enum_with_prefix(self) -> None:
mode = cg.MockObj("esphome::foo", "::").enum("Mode", is_class=True)
+3 -9
View File
@@ -215,9 +215,8 @@ def test_convert_pio_libs_arduino_framework_passes_empty_managed(
path=tmp_path / "other-lib",
)
def fake_generate_idf_components(libraries, managed=None, provides=None):
def fake_generate_idf_components(libraries, managed=None):
captured["managed"] = managed
captured["provides"] = provides
return [converted]
monkeypatch.setattr(
@@ -227,8 +226,6 @@ def test_convert_pio_libs_arduino_framework_passes_empty_managed(
result = _convert_pio_libs(tmp_path / "platformio.ini", "arduino")
assert captured["managed"] == set()
# Bundled Arduino names are left to arduino-esp32, as in the real build
assert captured["provides"]("Wire") and not captured["provides"]("Adafruit BusIO")
assert result == {
"esphome/other-lib": {"override_path": str(tmp_path / "other-lib")}
}
@@ -245,7 +242,7 @@ def test_convert_pio_libs_arduino_idf_6_passes_manifest_deps(
captured: dict[str, set[str] | None] = {}
def fake_generate_idf_components(libraries, managed=None, provides=None):
def fake_generate_idf_components(libraries, managed=None):
captured["managed"] = managed
return []
@@ -268,9 +265,8 @@ def test_convert_pio_libs_espidf_framework_passes_manifest_deps(
captured: dict[str, set[str] | None] = {}
def fake_generate_idf_components(libraries, managed=None, provides=None):
def fake_generate_idf_components(libraries, managed=None):
captured["managed"] = managed
captured["provides"] = provides
return []
monkeypatch.setattr(
@@ -280,8 +276,6 @@ def test_convert_pio_libs_espidf_framework_passes_manifest_deps(
result = _convert_pio_libs(tmp_path / "platformio.ini", "espidf")
assert captured["managed"] == _esphome_manifest_deps()
# ESP-IDF bundles no Arduino libraries
assert captured["provides"] is None
assert "esphome/noise-c" in captured["managed"]
assert result == {}
+122 -562
View File
@@ -4,13 +4,11 @@ Covers the shared download/parse/resolve/dependency-walk paths in
``esphome.platformio.library`` directly (the ESP-IDF and Zephyr backends are
exercised in their own test modules)."""
from collections.abc import Collection
import json
import logging
from pathlib import Path
from unittest.mock import Mock
from platformio.package.exception import UnknownPackageError
import pytest
from esphome.core import CORE, EsphomeError, Library
@@ -263,37 +261,23 @@ def test_make_registry_client_creates_http_cache_dir(
lib._make_registry_client()
def _patch_registry_resolve(
monkeypatch: pytest.MonkeyPatch,
*,
unknown: Collection[str] = (),
owner: str | None = None,
) -> list[tuple[str | None, str]]:
"""Stub the registry lookup so tests never touch the network; returns the
(owner, name) pairs looked up. ``unknown`` names miss; ``owner`` is what the
registry answers for an owner-less name."""
asked: list[tuple[str | None, str]] = []
def resolve(requested_owner, pkgname, requirements):
asked.append((requested_owner, pkgname))
if pkgname in unknown:
raise UnknownPackageError(pkgname)
return _resolution(requested_owner or owner, pkgname)
monkeypatch.setattr(lib, "_resolve_registry_version", resolve)
return asked
def _patch_registry_resolve(monkeypatch: pytest.MonkeyPatch) -> None:
"""Stub the registry lookup so tests never touch the network."""
monkeypatch.setattr(
lib,
"_resolve_registry_version",
lambda owner, pkgname, requirements: (
owner,
pkgname,
"1.0.0",
f"http://x/{pkgname}.tar.gz",
None,
),
)
def _resolution(owner, pkgname):
"""What the stubbed registry answers for a package."""
return owner, pkgname, "1.0.0", f"http://x/{pkgname}.tar.gz", None
def _patch_download_with_manifests(
monkeypatch, tmp_path, manifests, *, properties=(), unknown=(), owner=None
):
"""Fake ConvertedLibrary.download to materialize canned manifests on disk;
returns the registry lookups made (see _patch_registry_resolve)."""
def _patch_download_with_manifests(monkeypatch, tmp_path, manifests, *, properties=()):
"""Fake ConvertedLibrary.download to materialize canned manifests on disk."""
def fake_download(self, force=False, salt="", namespace=""):
self.path = tmp_path / self.get_require_name()
@@ -304,7 +288,7 @@ def _patch_download_with_manifests(
(self.path / "library.json").write_text(json.dumps(manifests[self.name]))
monkeypatch.setattr(ConvertedLibrary, "download", fake_download)
return _patch_registry_resolve(monkeypatch, unknown=unknown, owner=owner)
_patch_registry_resolve(monkeypatch)
def test_wave_requirement_growth_defers_the_superseded_download(tmp_path, monkeypatch):
@@ -359,22 +343,18 @@ def test_convert_libraries_parses_library_properties(tmp_path, monkeypatch):
assert emitted[0].data["version"] == "1.0"
def test_convert_libraries_fetches_dependency_without_version(tmp_path, monkeypatch):
# A dependency entry lacking a version resolves by name alone, as PlatformIO did.
asked = _patch_download_with_manifests(
def test_convert_libraries_skips_dependency_without_version(tmp_path, monkeypatch):
# A dependency entry lacking a version is malformed and silently skipped.
_patch_download_with_manifests(
monkeypatch,
tmp_path,
{
"esphome/A": {"name": "A", "dependencies": [{"name": "C"}]},
"C": {"name": "C"},
},
{"esphome/A": {"name": "A", "dependencies": [{"name": "C"}]}},
)
# No version on the top-level spec exercises the "no requirement" path too.
top = convert_libraries([Library("esphome/A", None, None)], _backend())
assert [d.name for d in top[0].dependencies] == ["C"]
assert asked == [("esphome", "A"), (None, "C")]
assert top[0].dependencies == []
def test_convert_libraries_handles_unparsable_dependency_version(tmp_path, monkeypatch):
@@ -1033,24 +1013,20 @@ def test_convert_libraries_malformed_transitive_dep_skips(
assert "Skipping dependency esphome/B: malformed manifest" in caplog.text
def test_walk_resolves_properties_only_depends(tmp_path, monkeypatch) -> None:
"""A library.properties depends= line is a dependency list: a bundled name
goes to the backend, the rest resolve by name."""
asked = _patch_download_with_manifests(
def test_walk_warns_for_properties_only_depends(
tmp_path, monkeypatch, caplog: pytest.LogCaptureFixture
) -> None:
"""A manifest declaring dependencies only as library.properties depends=
warns in the shared walk, so every backend reports the drop."""
_patch_download_with_manifests(
monkeypatch,
tmp_path,
{
"esphome/A": "name=A\nversion=1.0\ndepends=Wire, SPI\n",
"SPI": {"name": "SPI"},
},
{"esphome/A": "name=A\nversion=1.0\ndepends=Wire, SPI\n"},
properties=("esphome/A",),
)
backend = _backend(provides=lambda name: name == "Wire")
top = convert_libraries([Library("esphome/A", "1.0.0", None)], backend)
assert [d.name for d in top[0].dependencies] == ["SPI"]
assert "depends" not in top[0].data
assert backend.provided_requests == {"Wire"}
assert (None, "SPI") in asked
caplog.set_level("INFO")
convert_libraries([Library("esphome/A", "1.0.0", None)], _backend())
assert "declares dependencies via library.properties" in caplog.text
def test_walk_warns_for_nonplatform_invalid_library(
@@ -1117,12 +1093,12 @@ def test_source_kind_map_shape() -> None:
assert SOURCE_KIND_FOR_SUFFIX[".C++"] == "cxx"
def test_versionless_platform_filtered_dependency_is_not_fetched(
tmp_path, monkeypatch
def test_versionless_platform_filtered_dependency_stays_quiet(
tmp_path, monkeypatch, caplog: pytest.LogCaptureFixture
) -> None:
"""A version-less dependency the platform filter excludes is deliberately
absent, not fetched by name."""
asked = _patch_download_with_manifests(
"""A version-less dependency the platform filter excludes is
deliberately absent, not a drop to warn about."""
_patch_download_with_manifests(
monkeypatch,
tmp_path,
{
@@ -1132,52 +1108,59 @@ def test_versionless_platform_filtered_dependency_is_not_fetched(
}
},
)
top = convert_libraries([Library("esphome/A", None, None)], _backend())
assert top[0].dependencies == []
assert (None, "Hash") not in asked
convert_libraries([Library("esphome/A", None, None)], _backend())
assert "has no version to resolve" not in caplog.text
def test_versionless_ignored_dependency_is_not_fetched(tmp_path, monkeypatch) -> None:
def test_versionless_ignored_dependency_stays_quiet(
tmp_path, monkeypatch, caplog: pytest.LogCaptureFixture
) -> None:
"""A lib_ignore'd version-less dependency is deliberately excluded, not
fetched by name."""
asked = _patch_download_with_manifests(
a drop; no reconciliation warning."""
_patch_download_with_manifests(
monkeypatch,
tmp_path,
{"esphome/A": {"name": "A", "dependencies": [{"name": "Hash"}]}},
)
CORE.platformio_options = {"lib_ignore": ["Hash"]}
top = convert_libraries([Library("esphome/A", None, None)], _backend())
assert top[0].dependencies == []
assert (None, "Hash") not in asked
convert_libraries([Library("esphome/A", None, None)], _backend())
assert "has no version to resolve" not in caplog.text
def test_versionless_dependency_registry_miss_is_skipped(
def test_versionless_dependency_without_provider_warns(
tmp_path, monkeypatch, caplog: pytest.LogCaptureFixture
) -> None:
"""A name-only dependency the registry does not know is skipped with one
warning naming the requester, and the requester still converts; a listed
library the registry does not know still fails the build."""
"""A backend whose tree could supply the name warns on the drop; one
without provides() can never act on it, so it stays at debug."""
_patch_download_with_manifests(
monkeypatch,
tmp_path,
{
"esphome/A": {
"name": "A",
# The duplicate entry is one node, so it warns once
# The duplicate entry warns once (reconciliation dedup)
"dependencies": [{"name": "Hash"}, {"name": "Hash"}],
}
},
unknown={"Hash"},
)
top = convert_libraries([Library("esphome/A", None, None)], _backend())
assert top[0].dependencies == []
convert_libraries(
[Library("esphome/A", None, None)], _backend(provides=lambda name: False)
)
assert (
caplog.text.count("Dependency Hash of esphome/A was not resolved; skipping")
caplog.text.count(
"Hash of esphome/A has no version to resolve and nothing provides it"
)
== 1
)
with pytest.raises(UnknownPackageError):
convert_libraries([Library("Hash", None, None)], _backend())
caplog.clear()
with caplog.at_level(logging.DEBUG):
convert_libraries([Library("esphome/A", None, None)], _backend())
records = [
r
for r in caplog.records
if "has no version to resolve and nothing provides it" in r.message
]
assert records and all(r.levelno == logging.DEBUG for r in records)
def test_url_version_dependency_is_not_substituted_by_provides(
@@ -1208,525 +1191,102 @@ def test_url_version_dependency_is_not_substituted_by_provides(
assert any("o/hash" in n.lower() for n in emitted)
def test_versionless_owner_qualified_dependency_is_fetched_despite_provides(
tmp_path, monkeypatch
def test_versionless_owner_qualified_dependency_warns_despite_provides(
tmp_path, monkeypatch, caplog: pytest.LogCaptureFixture
) -> None:
"""An owner-qualified version-less dependency names a registry package,
so provides() does not stand in for it."""
asked = _patch_download_with_manifests(
"""An owner-qualified version-less dependency is not satisfied by
provides(); it must still warn."""
_patch_download_with_manifests(
monkeypatch,
tmp_path,
{
"esphome/A": {
"name": "A",
"dependencies": [{"name": "Wire", "owner": "Foo"}],
},
"Foo/Wire": {"name": "Wire"},
}
},
)
backend = _backend(provides=lambda name: name == "Wire")
top = convert_libraries([Library("esphome/A", None, None)], backend)
assert [d.name for d in top[0].dependencies] == ["Foo/Wire"]
assert ("Foo", "Wire") in asked
assert not backend.provided_requests
convert_libraries(
[Library("esphome/A", None, None)],
_backend(provides=lambda name: name == "Wire"),
)
assert "Wire of esphome/A has no version to resolve" in caplog.text
def test_versionless_provided_dependency_stays_quiet(
tmp_path, monkeypatch, caplog: pytest.LogCaptureFixture
) -> None:
"""An owner-less version-less dependency the backend provides is added
by the backend after emit; no reconciliation warning."""
_patch_download_with_manifests(
monkeypatch,
tmp_path,
{"esphome/A": {"name": "A", "dependencies": [{"name": "Wire"}]}},
)
convert_libraries(
[Library("esphome/A", None, None)],
_backend(provides=lambda name: name == "Wire"),
)
assert "has no version to resolve" not in caplog.text
def test_versionless_dependency_requested_top_level_stays_quiet(
tmp_path, monkeypatch, caplog: pytest.LogCaptureFixture
) -> None:
"""A version-less dependency the config also requests top-level is in
the build; no drop warning even without a provides backend."""
_patch_download_with_manifests(
monkeypatch,
tmp_path,
{
"esphome/A": {"name": "A", "dependencies": [{"name": "Hash"}]},
"Hash": {"name": "Hash"},
},
)
convert_libraries(
[Library("esphome/A", None, None), Library("Hash", None, None)],
_backend(),
)
assert "has no version to resolve" not in caplog.text
def test_versionless_url_ish_dependency_name_warns_cleanly(
tmp_path, monkeypatch, caplog: pytest.LogCaptureFixture
) -> None:
"""A malformed URL-ish dependency name is skipped with a warning, never
"""A malformed URL-ish dependency name falls to the drop warning, never
a RuntimeError out of the key parser."""
_patch_download_with_manifests(
monkeypatch,
tmp_path,
{"esphome/A": {"name": "A", "dependencies": [{"name": "file://"}]}},
)
top = convert_libraries([Library("esphome/A", None, None)], _backend())
assert top[0].dependencies == []
assert "Skipping dependency file:// of esphome/A" in caplog.text
def test_versionless_dependency_matching_resolved_manifest_name_is_wired(
tmp_path, monkeypatch, caplog: pytest.LogCaptureFixture
) -> None:
"""A bare name no package name matches is satisfied by a listed component's
manifest name (a fork published under another package name)."""
asked = _patch_download_with_manifests(
monkeypatch,
tmp_path,
{
"esphome/A": {"name": "A", "dependencies": [{"name": "B"}]},
"esphome/B-fork": {"name": "B"},
},
convert_libraries(
[Library("esphome/A", None, None)], _backend(provides=lambda name: False)
)
with caplog.at_level(logging.INFO):
top = convert_libraries(
[Library("esphome/A", None, None), Library("esphome/B-fork", None, None)],
_backend(),
)
assert [d.name for d in top[0].dependencies] == ["esphome/B-fork"]
assert asked == [("esphome", "A"), ("esphome", "B-fork")]
assert (
"Dependency B of esphome/A satisfied by esphome/B-fork through its manifest name"
"file:// of esphome/A has no version to resolve and nothing provides it"
in caplog.text
)
def test_dependency_naming_the_requester_itself_is_ignored(
tmp_path, monkeypatch
) -> None:
"""A manifest that lists its own name as a dependency neither fetches a
second copy of itself nor gains a self edge."""
asked = _patch_download_with_manifests(
monkeypatch,
tmp_path,
{
"esphome/A": "name=A\ndepends=a, esphome/A\n",
# No manifest name: the requested and resolved package names still
# identify it, with or without the owner
"esphome/C": {
"dependencies": [{"name": "C"}, {"name": "C", "owner": "esphome"}]
},
},
properties=("esphome/A",),
)
top = convert_libraries(
[Library("esphome/A", "1.0.0", None), Library("esphome/C", "1.0.0", None)],
_backend(),
)
assert [c.dependencies for c in top] == [[], []]
assert asked == [("esphome", "A"), ("esphome", "C")]
def test_versionless_dependency_settles_after_the_graph(
def test_versionless_dependency_matching_resolved_manifest_name_stays_quiet(
tmp_path, monkeypatch, caplog: pytest.LogCaptureFixture
) -> None:
"""The issue 21308 shape: a library.properties depends= naming a library the
config lists owner-qualified is wired to that one package, case-insensitively
and whatever constraint it carries, without a second node for the bare name
and ahead of a same-named bundled copy, as on the Arduino backend."""
asked = _patch_download_with_manifests(
monkeypatch,
tmp_path,
{
"adafruit/Adafruit GFX Library": (
"name=Adafruit GFX Library\ndepends=adafruit busio (>=1.0)\n"
),
"adafruit/Adafruit BusIO": {"name": "Adafruit BusIO"},
},
properties=("adafruit/Adafruit GFX Library",),
)
backend = _backend(provides=lambda name: name == "adafruit busio")
top = convert_libraries(
[
Library("adafruit/Adafruit GFX Library", "^1.13.0", None),
Library("adafruit/Adafruit BusIO", "^1.17.4", None),
],
backend,
)
assert [d.name for d in top[0].dependencies] == ["adafruit/Adafruit BusIO"]
assert len(asked) == 2
assert not backend.provided_requests
assert "multiple names" not in caplog.text
def test_by_name_node_folds_into_a_later_pin_of_the_same_package(
tmp_path, monkeypatch, caplog: pytest.LogCaptureFixture
) -> None:
"""A library fetched by name alone may pull in a manifest that pins the same
package owner-qualified; the by-name node folds into that one instead of
converting the package twice."""
# Manifests are keyed by the resolved name, so both B nodes read the same one:
# B pins C, and C pins owner/B, which arrives after B was fetched by name
manifests = {
"esphome/A": {"name": "A", "dependencies": [{"name": "B"}]},
"esphome/B": {"name": "B", "dependencies": {"esphome/C": "1.0.0"}},
"esphome/C": {"name": "C", "dependencies": {"esphome/B": "1.0.0"}},
}
_patch_download_with_manifests(monkeypatch, tmp_path, manifests, owner="esphome")
emitted: list[str] = []
top = convert_libraries(
[Library("esphome/A", None, None)],
_backend(emit=lambda c: emitted.append(c.name)),
)
assert [d.name for d in top[0].dependencies] == ["esphome/B"]
assert emitted.count("esphome/B") == 1
assert "multiple names" not in caplog.text
def test_two_spellings_of_a_name_in_one_round_share_a_node(
tmp_path, monkeypatch, caplog: pytest.LogCaptureFixture
) -> None:
"""Requesters spelling one unlisted name differently get a single by-name
fetch and the same node."""
asked = _patch_download_with_manifests(
monkeypatch,
tmp_path,
{
"esphome/A": {"name": "A", "dependencies": [{"name": "Foo"}]},
"esphome/B": {"name": "B", "dependencies": [{"name": "foo"}]},
"Foo": {"name": "Foo"},
},
)
top = convert_libraries(
[Library("esphome/A", None, None), Library("esphome/B", None, None)],
_backend(),
)
assert [d.name for d in top[0].dependencies] == ["Foo"]
assert [d.name for d in top[1].dependencies] == ["Foo"]
assert asked.count((None, "Foo")) + asked.count((None, "foo")) == 1
assert "multiple names" not in caplog.text
def test_parse_properties_depends_reads_names_and_drops_constraints(
caplog: pytest.LogCaptureFixture,
) -> None:
"""Arduino constraints are not PlatformIO syntax, so only the names survive;
an unclosed parenthesis loses nothing more than the constraint."""
with caplog.at_level(logging.DEBUG):
entries = lib.parse_properties_depends(
"Adafruit BusIO, Foo (>=1.2), ,Bar( <2 ), Baz (=1.0, Qux ()"
)
levels = {
r.message.removeprefix("Ignoring version constraint on depends="): r.levelno
for r in caplog.records
if "Ignoring version constraint" in r.message
}
# A lower bound is met by the current version; a cap or pin may not be
assert levels == {
"Foo (>=1.2)": logging.DEBUG,
"Bar( <2 )": logging.INFO,
"Baz (=1.0": logging.INFO,
"Qux ()": logging.INFO,
}
assert entries == [
{"name": "Adafruit BusIO"},
{"name": "Foo"},
{"name": "Bar"},
{"name": "Baz"},
{"name": "Qux"},
]
assert "Ignoring version constraint on depends=Foo (>=1.2)" in caplog.text
def test_by_name_node_pinned_later_reports_a_requirement_conflict(
tmp_path, monkeypatch
) -> None:
"""A node fetched by name that a later manifest pins under the same spelling
is resolved again against the pin; a conflict is an error, not a miss."""
"""A bare name satisfied by an owner-qualified component's manifest
name is not a drop."""
_patch_download_with_manifests(
monkeypatch,
tmp_path,
{
"esphome/A": {"name": "A", "dependencies": [{"name": "B"}]},
"B": {"name": "B", "dependencies": {"C": "1.0.0"}},
"C": {"name": "C", "dependencies": {"B": "^2.0"}},
"esphome/B": {"name": "B"},
},
)
def resolve(owner, pkgname, requirements):
if pkgname == "B" and requirements:
raise RuntimeError(f"No version of {pkgname} satisfies {requirements}")
return _resolution(owner, pkgname)
monkeypatch.setattr(lib, "_resolve_registry_version", resolve)
with pytest.raises(RuntimeError, match="No version of B satisfies"):
convert_libraries([Library("esphome/A", None, None)], _backend())
def test_by_name_fetch_yields_to_a_fork_pulled_in_later(
tmp_path, monkeypatch, caplog: pytest.LogCaptureFixture
) -> None:
"""A name fetched from the registry yields to a library another round
pins explicitly under the same manifest name, so only the fork is built."""
_patch_download_with_manifests(
monkeypatch,
tmp_path,
{
"esphome/A": {"name": "A", "dependencies": [{"name": "B"}]},
"esphome/X": {"name": "X", "dependencies": [{"name": "C"}]},
# The upstream B pulls in D, which nothing else needs; D asks for a
# name the registry does not know
"B": {"name": "B", "dependencies": {"esphome/D": "1.0.0"}},
"esphome/D": {"name": "D", "dependencies": [{"name": "Zed"}]},
"C": {"name": "C", "dependencies": {"custom/B-fork": "1.0.0"}},
"custom/B-fork": {"name": "B"},
},
unknown={"Zed"},
)
emitted: list[str] = []
top = convert_libraries(
[Library("esphome/A", None, None), Library("esphome/X", None, None)],
_backend(emit=lambda c: emitted.append(c.name)),
)
assert [d.name for d in top[0].dependencies] == ["custom/B-fork"]
assert sorted(emitted) == ["C", "custom/B-fork", "esphome/A", "esphome/X"]
# The miss belongs to a library that is not built, so it is not reported
assert "Zed" not in caplog.text
def test_git_source_replacing_a_registry_key_in_the_same_wave(
tmp_path, monkeypatch
) -> None:
"""A manifest in one wave naming a key of the same wave as a git source
defers that key to the next wave instead of tripping over its stale record."""
_patch_download_with_manifests(
monkeypatch,
tmp_path,
{
"o/A": {"name": "A", "dependencies": {"B": "https://github.com/o/B.git"}},
"o/B": {"name": "B"},
},
)
emitted: dict[str, ConvertedLibrary] = {}
top = convert_libraries(
[Library("o/A", "1.0.0", None), Library("o/B", "1.0.0", None)],
_backend(emit=lambda c: emitted.__setitem__(c.name, c)),
)
assert [c.name for c in top] == ["o/A", "o/B"]
assert isinstance(emitted["o/B"].source, GitSource)
assert [d.name for d in top[0].dependencies] == ["o/B"]
def test_owner_qualified_name_matches_the_resolved_owner(tmp_path, monkeypatch) -> None:
"""A version-less ``o/B`` is satisfied by a bare ``B`` pin the registry
resolved to owner ``o``, so the package is not fetched a second time."""
asked = _patch_download_with_manifests(
monkeypatch,
tmp_path,
{
"esphome/A": {
"name": "A",
"dependencies": [
{"name": "B", "version": "1.0.0"},
{"name": "B", "owner": "o"},
],
},
"o/B": {"name": "B"},
},
owner="o",
)
top = convert_libraries([Library("esphome/A", None, None)], _backend())
assert [d.name for d in top[0].dependencies] == ["o/B"]
assert asked.count((None, "B")) == 1
assert ("o", "B") not in asked
def test_two_spellings_fetched_by_name_fold_into_one_node(
tmp_path, monkeypatch, caplog: pytest.LogCaptureFixture
) -> None:
"""Version-less ``B`` and ``o/B`` from two requesters resolve to one
package; only one copy is built and both requesters depend on it."""
_patch_download_with_manifests(
monkeypatch,
tmp_path,
{
"esphome/A": {"name": "A", "dependencies": [{"name": "B"}]},
"esphome/X": {"name": "X", "dependencies": [{"name": "B", "owner": "o"}]},
"o/B": {"name": "B"},
},
owner="o",
)
emitted: list[str] = []
top = convert_libraries(
[Library("esphome/A", None, None), Library("esphome/X", None, None)],
_backend(emit=lambda c: emitted.append(c.name)),
)
assert emitted.count("o/B") == 1
kept = top[0].dependencies[0]
assert top[1].dependencies == [kept]
assert "multiple names" not in caplog.text
def test_registry_miss_resolves_again_as_a_git_source(tmp_path, monkeypatch) -> None:
"""A key the registry missed is resolved again when a later manifest names
the same key as a git source."""
_patch_download_with_manifests(
monkeypatch,
tmp_path,
{
"esphome/A": {"name": "A", "dependencies": [{"name": "B", "owner": "o"}]},
"esphome/X": {"name": "X", "dependencies": [{"name": "C"}]},
"C": {"name": "C", "dependencies": {"B": "https://github.com/o/B.git"}},
"o/B": {"name": "B"},
},
unknown={"B"},
)
emitted: dict[str, ConvertedLibrary] = {}
top = convert_libraries(
[Library("esphome/A", None, None), Library("esphome/X", None, None)],
_backend(emit=lambda c: emitted.__setitem__(c.name, c)),
)
assert [d.name for d in top[0].dependencies] == ["o/B"]
assert isinstance(emitted["o/B"].source, GitSource)
assert [d.name for d in emitted["C"].dependencies] == ["o/B"]
def test_normalize_dependencies_splits_an_embedded_owner() -> None:
"""``owner/Name`` as one string, in a list or a dict entry, normalizes like
the dict form; a URL or a deeper path is left alone."""
assert lib.normalize_dependencies(
["o/B", {"name": "o/C", "version": "1.0"}, "x://y", "a/b/c", "../escape"],
"libx",
) == [
{"name": "B", "owner": "o"},
{"name": "C", "owner": "o", "version": "1.0"},
{"name": "x://y"},
{"name": "a/b/c"},
{"name": "../escape"},
]
assert lib.normalize_dependencies("o/B, D", "libx") == [
{"name": "B", "owner": "o"},
{"name": "D"},
]
def test_owner_qualified_string_dependency_is_fetched_and_wired(
tmp_path, monkeypatch
) -> None:
"""``"dependencies": ["o/B"]`` fetches ``o/B`` and wires the requester to it."""
asked = _patch_download_with_manifests(
monkeypatch,
tmp_path,
{
"esphome/A": {"name": "A", "dependencies": ["o/B"]},
"o/B": {"name": "B"},
},
)
top = convert_libraries([Library("esphome/A", None, None)], _backend())
assert [d.name for d in top[0].dependencies] == ["o/B"]
assert asked == [("esphome", "A"), ("o", "B")]
def test_empty_version_is_settled_like_a_missing_one(
tmp_path, monkeypatch, caplog: pytest.LogCaptureFixture
) -> None:
"""``{"Foo": ""}`` carries no version either, so it is matched, fetched by
name or reported like a version-less entry rather than failing or vanishing."""
asked = _patch_download_with_manifests(
monkeypatch,
tmp_path,
{
"esphome/A": {"name": "A", "dependencies": {"Foo": "", "Bar": ""}},
"Bar": {"name": "Bar"},
},
unknown={"Foo"},
)
top = convert_libraries([Library("esphome/A", None, None)], _backend())
assert [d.name for d in top[0].dependencies] == ["Bar"]
assert (None, "Foo") in asked
assert "Dependency Foo of esphome/A was not resolved; skipping" in caplog.text
def test_bundled_name_is_satisfied_by_a_replacement_pulled_in_later(
tmp_path, monkeypatch
) -> None:
"""A name the framework bundles is still wired to an explicit replacement
of the same manifest name that a later round pulls in, as it would be had
the replacement been listed up front."""
_patch_download_with_manifests(
monkeypatch,
tmp_path,
{
"esphome/A": {"name": "A", "dependencies": [{"name": "Ethernet"}]},
"esphome/X": {"name": "X", "dependencies": [{"name": "C"}]},
"C": {"name": "C", "dependencies": {"vendor/Ethernet": "1.0.0"}},
"vendor/Ethernet": {"name": "Ethernet"},
},
)
backend = _backend(provides=lambda name: name == "Ethernet")
top = convert_libraries(
[Library("esphome/A", None, None), Library("esphome/X", None, None)],
backend,
)
assert [d.name for d in top[0].dependencies] == ["vendor/Ethernet"]
def test_missed_name_is_satisfied_by_a_library_pulled_in_later(
tmp_path, monkeypatch, caplog: pytest.LogCaptureFixture
) -> None:
"""A bare name the registry does not know is wired to a library a later
round pulls in under another package name but the same manifest name."""
asked = _patch_download_with_manifests(
monkeypatch,
tmp_path,
{
"esphome/A": {"name": "A", "dependencies": [{"name": "B"}]},
"esphome/X": {"name": "X", "dependencies": [{"name": "C"}]},
# Fetched by name in the same round as the miss; pins the fork
"C": {"name": "C", "dependencies": {"owner/B-fork": "1.0.0"}},
"owner/B-fork": {"name": "B"},
},
unknown={"B"},
)
with caplog.at_level(logging.INFO):
top = convert_libraries(
[Library("esphome/A", None, None), Library("esphome/X", None, None)],
_backend(),
)
assert [d.name for d in top[0].dependencies] == ["owner/B-fork"]
assert asked.count((None, "B")) == 1
assert (
"Dependency B of esphome/A satisfied by owner/B-fork through its manifest name"
in caplog.text
)
assert "skipping" not in caplog.text
def test_by_name_request_does_not_make_an_existing_node_optional(
tmp_path, monkeypatch, caplog: pytest.LogCaptureFixture
) -> None:
"""A version-less request for a node the walk already created (here one
skipped for its platform) neither re-fetches it nor turns a later miss
into a warning."""
asked = _patch_download_with_manifests(
monkeypatch,
tmp_path,
{
"esphome/A": {
"name": "A",
"dependencies": [{"name": "X", "version": "1.0"}, {"name": "X"}],
},
"X": {"name": "X", "platforms": "espressif8266"},
},
)
top = convert_libraries([Library("esphome/A", None, None)], _backend())
assert top[0].dependencies == []
assert asked == [("esphome", "A"), (None, "X")]
assert "not in the registry" not in caplog.text
def test_provided_dependency_asks_the_backend_once_per_name(
tmp_path, monkeypatch
) -> None:
"""A bundled name is recorded for the backend and never fetched; two
requesters cost one provides() call, so a backend warning there is not
repeated. A pinned request on the same name shares the memo."""
asked = _patch_download_with_manifests(
monkeypatch,
tmp_path,
{
"esphome/A": {"name": "A", "dependencies": [{"name": "Wire"}]},
"esphome/B": {"name": "B", "dependencies": {"Wire": "*"}},
},
)
calls: list[str] = []
def provides(name: str) -> bool:
calls.append(name)
return name == "Wire"
backend = _backend(provides=provides)
convert_libraries(
[Library("esphome/A", None, None), Library("esphome/B", None, None)],
backend,
_backend(),
)
assert calls == ["Wire"]
assert backend.provided_requests == {"Wire"}
assert (None, "Wire") not in asked
assert "has no version to resolve" not in caplog.text
def test_convert_libraries_symlink_url_resolves_as_local(setup_core: Path) -> None: