25static constexpr uint32_t LOOP_BUDGET_MS = 20;
26static constexpr uint32_t HANDSHAKE_TIMEOUT_MS = 15000;
27static constexpr uint32_t STABLE_SESSION_MS = 60000;
28static constexpr uint32_t MAX_BACKOFF_MS = 60000;
29static constexpr uint32_t CONFIG_RETRY_MS = 300000;
30static constexpr uint32_t AUTH_AMBIGUOUS_HOLD_MS = 60000;
31static constexpr uint32_t HEARTBEAT_MS = 60000;
32static constexpr uint32_t MIN_REQUEST_SPACING_MS = 50;
34static constexpr uint32_t DISPLAY_START_MS = 5000;
36static constexpr uint32_t DISPLAY_BUSY_TIMEOUT_MS = 120000;
37static constexpr uint16_t MAX_CURSOR_SIDE = 1024;
38static constexpr uint32_t MAX_TEXT_LEN = 63;
40static constexpr uint8_t SECURITY_NONE = 1;
41static constexpr uint8_t SECURITY_VNC_AUTH = 2;
43static constexpr int32_t ENCODING_RAW = 0;
44static constexpr int32_t ENCODING_HEXTILE = 5;
45static constexpr int32_t ENCODING_RICH_CURSOR = -239;
47static constexpr int32_t ENCODINGS[] = {ENCODING_HEXTILE, ENCODING_RAW, ENCODING_RICH_CURSOR};
52static constexpr uint8_t HEXTILE_RAW = 0x01;
53static constexpr uint8_t HEXTILE_BACKGROUND = 0x02;
54static constexpr uint8_t HEXTILE_FOREGROUND = 0x04;
55static constexpr uint8_t HEXTILE_ANY_SUBRECTS = 0x08;
56static constexpr uint8_t HEXTILE_SUBRECTS_COLOURED = 0x10;
58static constexpr uint8_t MSG_FRAMEBUFFER_UPDATE = 0;
59static constexpr uint8_t MSG_SET_COLOUR_MAP = 1;
60static constexpr uint8_t MSG_BELL = 2;
61static constexpr uint8_t MSG_SERVER_CUT_TEXT = 3;
64static constexpr uint8_t MSG_KEY_EVENT = 4;
65static constexpr uint8_t MSG_POINTER_EVENT = 5;
67static constexpr uint32_t KEYSYM_BACKSPACE = 0xFF08;
68static constexpr uint32_t KEYSYM_TAB = 0xFF09;
69static constexpr uint32_t KEYSYM_RETURN = 0xFF0D;
71static constexpr uint32_t KEYSYM_UNICODE = 0x01000000;
74static inline uint16_t read_be16(
const uint8_t *p) {
return encode_uint16(p[0], p[1]); }
77static int parse_three_digits(
const uint8_t *p) {
79 for (
int i = 0; i < 3; i++) {
80 if (p[i] <
'0' || p[i] >
'9') {
97 return KEYSYM_BACKSPACE;
101 if (cp < 0x20 || (cp >= 0x7F && cp < 0xA0)) {
104 return cp <= 0xFF ? cp : (KEYSYM_UNICODE | cp);
108#ifdef USE_VNC_TOUCHSCREEN
109static constexpr uint16_t TOUCH_SLOP_PX = 8;
111static bool is_down(
const touchscreen::TouchPoint &tp) {
113 return (tp.state & (touchscreen::STATE_PRESSED | touchscreen::STATE_UPDATED)) != 0 &&
114 (tp.state & touchscreen::STATE_RELEASING) == 0;
125 for (
const auto &tp : tpoints) {
145 const uint32_t cells = std::min<uint32_t>(wanted, UINT16_MAX);
146 if (cells < wanted) {
147 ESP_LOGW(TAG,
"Display has %" PRIu32
" tiles, more than the %u the cache holds", wanted, UINT16_MAX);
152 ESP_LOGW(TAG,
"Not enough memory to skip unchanged tiles");
174 " Reconnect Interval: %" PRIu32
"ms\n"
175 " Update Delay: %" PRIu32
"ms\n"
176 " Max Update Delay: %" PRIu32
"ms\n"
177 " Pixel Format: %s\n"
178 " Tile Cache: %u tiles",
179 this->
link_.
host(), this->link_.port(), this->link_.reconnect_interval(), this->update_delay_ms_,
180 this->max_update_delay_ms_,
181 LOG_STR_ARG(PixelFormatStrings::get_log_str(
static_cast<uint8_t
>(this->
pixel_format_), 0)),
184 ESP_LOGCONFIG(TAG,
" Password: %s", this->
auth_key_ !=
nullptr ? LOG_STR_LITERAL(
"set") : LOG_STR_LITERAL(
"none"));
186#ifdef USE_VNC_TOUCHSCREEN
187 ESP_LOGCONFIG(TAG,
" Touchscreen: %s", this->
has_touchscreen_ ? LOG_STR_LITERAL(
"yes") : LOG_STR_LITERAL(
"no"));
266 const bool display_starting =
336 ESP_LOGI(TAG,
"Disconnected");
359 if (!this->
link_.
connecting() && (now - this->hold_start_ms_ < this->hold_ms_ || this->link_.in_backoff())) {
364 if (!this->
link_.
connected() && !this->link_.connecting() && this->link_.in_backoff()) {
399#ifdef USE_VNC_TOUCHSCREEN
458 ESP_LOGW(TAG,
"%s after %" PRIu32
"s", LOG_STR_ARG(reason), lasted / 1000);
460 if (lasted >= STABLE_SESSION_MS) {
475 this->
hold_ms_ = AUTH_AMBIGUOUS_HOLD_MS;
476 ESP_LOGW(TAG,
"%s before the password was accepted; retrying in %" PRIu32
"s", LOG_STR_ARG(reason),
484 "%s after the password twice. Not retrying: on some servers repeated failures lock out every user. "
485 "Check the password and the server, then reboot or call vnc.connect",
486 LOG_STR_ARG(reason));
487 this->
status_set_error(LOG_STR(
"Server dropped the connection after the password twice"));
491 ESP_LOGE(TAG,
"Authentication failed. Not retrying: on some servers repeated failures lock out every user. "
492 "Fix the password, then reboot or call vnc.connect");
498 ESP_LOGE(TAG,
"%s; retrying in %" PRIu32
"s", LOG_STR_ARG(reason), this->
hold_ms_ / 1000);
507 ESP_LOGW(TAG,
"%s; retrying in %" PRIu32
"s", LOG_STR_ARG(reason), this->
hold_ms_ / 1000);
559 const uint8_t shared = 1;
560 return this->
send_(&shared, 1);
566 if (type == SECURITY_VNC_AUTH) {
583 switch (this->
step_) {
588 const int major = parse_three_digits(p + 4);
589 const int minor = parse_three_digits(p + 8);
590 if (std::memcmp(p,
"RFB ", 4) != 0 || p[7] !=
'.' || p[11] !=
'\n' || major < 0 || minor < 0) {
596 this->
minor_ = (major > 3 || minor >= 8) ? 8 : (minor == 7 ? 7 : 3);
598 uint8_t reply[12] = {
'R',
'F',
'B',
' ',
'0',
'0',
'3',
'.',
'0',
'0',
'8',
'\n'};
599 reply[10] =
'0' + this->
minor_;
600 if (!this->
send_(reply,
sizeof(reply))) {
626 bool wants_password =
false;
627 for (uint8_t i = 0; i < this->
sec_count_; i++) {
628 if (p[i] == SECURITY_NONE || (p[i] == SECURITY_VNC_AUTH && has_key)) {
632 wants_password |= p[i] == SECURITY_VNC_AUTH;
637 : LOG_STR(
"No supported security type"));
652 }
else if (type == SECURITY_NONE || (
type == SECURITY_VNC_AUTH && this->
has_auth_key_())) {
658 ? LOG_STR(
"The server requires a password")
659 : LOG_STR(
"No supported security type"));
668 uint8_t *challenge = this->
rx_ + this->
rx_pos_;
670 if (!this->
send_(challenge, 16)) {
682 const uint32_t result = read_be32(p);
686 if (this->
sec_type_ == SECURITY_VNC_AUTH)
712 this->
text_len_ =
static_cast<uint8_t
>(std::min(read_be32(p), MAX_TEXT_LEN));
721 ESP_LOGE(TAG,
"Server: %.*s",
static_cast<int>(this->text_len_),
reinterpret_cast<const char *
>(p));
728 this->
fb_w_ = read_be16(p);
729 this->
fb_h_ = read_be16(p + 2);
744 ESP_LOGI(TAG,
"Server '%.*s', %ux%u, RFB 3.%u",
static_cast<int>(this->text_len_),
761 case MSG_FRAMEBUFFER_UPDATE:
773 case MSG_SET_COLOUR_MAP:
783 case MSG_SERVER_CUT_TEXT:
791 ESP_LOGW(TAG,
"Unknown message type %u", p[0]);
804 this->
rect_y_ = read_be16(p + 2);
805 this->
rect_w_ = read_be16(p + 4);
806 this->
rect_h_ = read_be16(p + 6);
807 const auto encoding =
static_cast<int32_t
>(read_be32(p + 8));
810 if (encoding == ENCODING_RAW || encoding == ENCODING_HEXTILE) {
813 ESP_LOGW(TAG,
"Rectangle %ux%u at %u,%u is outside the %ux%u screen", this->
rect_w_, this->
rect_h_,
819 }
else if (encoding == ENCODING_RAW) {
829 }
else if (encoding == ENCODING_RICH_CURSOR) {
830 if (this->
rect_w_ > MAX_CURSOR_SIDE || this->
rect_h_ > MAX_CURSOR_SIDE) {
837 ESP_LOGW(TAG,
"Unsupported encoding %" PRId32, encoding);
849 const size_t pixels = avail / this->
bpp_;
856 const auto count =
static_cast<uint16_t
>(std::min<size_t>(pixels, this->
rect_w_ - this->
pos_x_));
878 const uint8_t
flags = p[0];
883 if (flags & HEXTILE_RAW) {
889 const size_t need = 1 + ((
flags & HEXTILE_BACKGROUND) ? this->
bpp_ : 0) +
890 ((flags & HEXTILE_FOREGROUND) ? this->
bpp_ : 0) + ((flags & HEXTILE_ANY_SUBRECTS) ? 1 : 0);
894 const uint8_t *q = p + 1;
895 if (
flags & HEXTILE_BACKGROUND) {
896 std::memcpy(this->
hex_bg_, q, this->bpp_);
899 if (flags & HEXTILE_FOREGROUND) {
900 std::memcpy(this->
hex_fg_, q, this->bpp_);
905 this->rx_pos_ += need;
917 this->rx_pos_ +=
count;
935 const uint8_t *pixel = this->
hex_fg_;
936 const uint8_t *q = p;
941 this->
fill_tile_(q[0] >> 4, q[0] & 0x0F, (q[1] >> 4) + 1, (q[1] & 0x0F) + 1, pixel);
942 this->rx_pos_ += need;
948 this->rx_pos_ +=
count;
994 ESP_LOGD(TAG,
"Update: %" PRIu32
" bytes in %" PRIu32
"ms, %u hextile tiles drawn",
1004 const int visible = std::min<int>(
count, this->
vis_w_ - x);
1024 if (*cell == hash) {
1033 if (cell !=
nullptr) {
1044 for (; i + 4 <=
len; i += 4) {
1046 std::memcpy(&word, this->
tile_ + i,
sizeof(word));
1050 for (; i <
len; i++) {
1053 return hash == 0 ? 1 : hash;
1060 const uint16_t col_end = std::min<uint16_t>((
x + w - 1) / 16, this->
hash_cols_ - 1);
1061 const uint16_t row_end = std::min<uint16_t>((
y +
h - 1) / 16, this->
hash_rows_ - 1);
1062 for (uint16_t row =
y / 16; row <= row_end; row++) {
1063 for (uint16_t col =
x / 16; col <= col_end; col++) {
1073 w = std::min<uint8_t>(w, this->
tile_w_ - x);
1074 h = std::min<uint8_t>(
h, this->
tile_h_ - y);
1075 const size_t stride =
static_cast<size_t>(this->
tile_w_) * this->
bpp_;
1076 const size_t row_len =
static_cast<size_t>(w) * this->
bpp_;
1077 uint8_t *first = this->
tile_ + y * stride +
x * this->
bpp_;
1078 if (this->bpp_ == 1) {
1079 std::memset(first, pixel[0], w);
1081 for (
size_t i = 0; i < row_len; i += 2) {
1082 first[i] = pixel[0];
1083 first[i + 1] = pixel[1];
1086 for (uint8_t row = 1; row <
h; row++) {
1087 std::memcpy(first + row * stride, first, row_len);
1099 this->
bpp_ = rgb565 ? 2 : 1;
1104 this->
vis_w_ = std::min<int>(this->
fb_w_, disp_w);
1105 this->
vis_h_ = std::min<int>(this->
fb_h_, disp_h);
1106 if (this->
fb_w_ != disp_w || this->
fb_h_ != disp_h) {
1107 ESP_LOGW(TAG,
"Server screen is %ux%u but the display is %dx%d; showing the top-left %ux%u", this->
fb_w_,
1110 const uint16_t cols = (this->
vis_w_ + 15) / 16;
1111 const uint16_t rows = (this->
vis_h_ + 15) / 16;
1119 ESP_LOGW(TAG,
"Screen has %u tiles, more than the %u cached; every tile is drawn", cols * rows,
1130 uint8_t pixel_format[20] = {};
1131 pixel_format[4] = rgb565 ? 16 : 8;
1132 pixel_format[5] = rgb565 ? 16 : 8;
1134 pixel_format[7] = 1;
1135 put_be16(pixel_format + 8, rgb565 ? 31 : 7);
1136 put_be16(pixel_format + 10, rgb565 ? 63 : 7);
1137 put_be16(pixel_format + 12, rgb565 ? 31 : 3);
1138 pixel_format[14] = rgb565 ? 11 : 5;
1139 pixel_format[15] = rgb565 ? 5 : 2;
1140 if (!this->
send_(pixel_format,
sizeof(pixel_format))) {
1145 uint8_t encodings[4 + 4 * std::size(ENCODINGS)] = {2};
1146 uint8_t *p =
put_be16(encodings + 2, std::size(ENCODINGS));
1147 for (
auto encoding : ENCODINGS) {
1193 uint8_t msg[10] = {3,
static_cast<uint8_t
>(incremental ? 1 : 0)};
1196 if (!this->
send_(msg,
sizeof(msg))) {
1236 const uint8_t *msg = this->
pending_[0];
1249 ESP_LOGV(TAG,
"Not connected, pointer event dropped");
1252 x = std::min<uint16_t>(
x, this->
fb_w_ - 1);
1253 y = std::min<uint16_t>(
y, this->
fb_h_ - 1);
1257 const bool release = buttons != this->
ptr_buttons_ && (buttons & ~this->ptr_buttons_) == 0;
1259 ESP_LOGW(TAG,
"Send buffer full, pointer event dropped");
1272 ESP_LOGV(TAG,
"Not connected, key dropped");
1276 ESP_LOGW(TAG,
"Send buffer full, key dropped");
1282 ESP_LOGV(TAG,
"Not connected, text dropped");
1286 for (
size_t i = 0; i <
len;) {
1288 char sequence[5] = {};
1289 std::memcpy(sequence, text + i, std::min<size_t>(4,
len - i));
1293 i += used == 0 ? 1 : used;
1294 const uint32_t keysym = keysym_for_codepoint(cp);
1299 ESP_LOGW(TAG,
"Send buffer full, text cut after %u characters",
static_cast<unsigned>(typed));
1307#ifdef USE_VNC_TOUCHSCREEN
1322 x = std::min<uint16_t>(
x, this->
vis_w_ - 1);
1323 y = std::min<uint16_t>(
y, this->
vis_h_ - 1);
1326 if (std::abs(
x - this->
press_x_) <= TOUCH_SLOP_PX && std::abs(
y - this->
press_y_) <= TOUCH_SLOP_PX) {
uint32_t IRAM_ATTR HOT get_loop_component_start_time() const
Get the cached time in milliseconds from when the current component started its loop execution.
bool has_overridden_loop() const
void status_set_warning()
void status_clear_error()
void enable_loop()
Enable this component's loop.
bool status_has_warning() const
void disable_loop()
Disable this component's loop.
void status_clear_warning()
bool is_idle() const
Check if this component is idle.
void stop()
Stop running the loop continuously.
void start()
Start running the loop continuously.
An STL allocator that uses SPI or internal RAM.
virtual int get_height()
Get the calculated height of the display in pixels with rotation applied.
virtual DisplayRotation get_rotation() const
virtual int get_width()
Get the calculated width of the display in pixels with rotation applied.
virtual DisplayType get_display_type()=0
Get the type of display that the buffer corresponds to.
void draw_pixels_at(int x_start, int y_start, int w, int h, const uint8_t *ptr, ColorBitness bitness, bool big_endian, int x_offset, int y_offset, int x_pad)
Given an array of pixels encoded in the nominated format, draw these into the display's buffer.
const char * host() const
bool flush_tx()
Send the front of the buffer; true once it is empty.
uint32_t reconnect_interval() const
void close()
Close without scheduling a reconnect (shutdown).
ssize_t read(uint8_t *buf, size_t len)
Returns bytes moved, 0 when nothing can move now, -1 when the link dropped.
void begin(const char *tag)
Call from setup(). tag names this link's log lines.
void note_attempt()
Shared retry clock, also usable for a listen socket.
bool connecting() const
A connect attempt is in flight.
size_t queue(const uint8_t *data, size_t len)
Copy into the outgoing buffer; returns how many bytes fit.
void poll()
Connect state machine; call every loop while acting as a client.
static constexpr size_t KEY_EVENT_SIZE
LazyCallbackManager< void()> disconnect_callback_
void disconnect()
Close the session and stop retrying until connect().
VncFailure lost_failure_() const
bool select_security_(uint8_t type)
uint8_t tile_[TILE_BUFFER_SIZE]
static constexpr uint32_t INPUT_BOOST_MS
uint32_t update_start_bytes_
static constexpr size_t POINTER_EVENT_SIZE
uint32_t first_change_ms_
LazyCallbackManager< void()> connect_callback_
void send_key(uint32_t keysym, VncKeyAction action)
void session_lost_(uint32_t now)
uint32_t max_update_delay_ms_
void forget_tiles_(uint16_t x, uint16_t y, uint16_t w, uint16_t h)
VncParse fail_(VncFailure failure, const LogString *reason)
uint32_t update_start_ms_
void handle_touch_(uint16_t x, uint16_t y)
Touch input in display coordinates.
display::ColorBitness bitness_
display::Display * display_
socket::TcpClientLink link_
const uint8_t * auth_key_
void draw_row_(uint16_t x, uint16_t y, uint16_t count, const uint8_t *pixels)
bool is_connected() const
void dump_config() override
bool send_(const uint8_t *data, size_t len)
bool start_session_(uint32_t now)
void refresh()
Ask the server for the whole screen again.
uint32_t session_start_ms_
HighFrequencyLoopRequester high_freq_
uint32_t last_request_ms_
void connect()
Retry now, also after an authentication failure or disconnect().
void clear_link_warning_()
bool has_auth_key_() const
void service_rx_(uint32_t now)
void type_text(const char *text, size_t len)
uint32_t tile_hash_() const
void update(const touchscreen::TouchPoints_t &tpoints) override
bool send_input_(const uint8_t *msg, size_t len, bool release)
bool request_outstanding_
void start_handshake_(uint32_t now)
static constexpr size_t RX_BUFFER_SIZE
void session_end_(uint32_t now, VncFailure failure, const LogString *reason)
void connect_step_(uint32_t now)
VncPixelFormat pixel_format_
VncFailure reason_failure_
static constexpr size_t MAX_PENDING_RELEASES
bool budget_exhausted_() const
uint8_t pending_[MAX_PENDING_RELEASES][KEY_EVENT_SIZE]
bool send_pointer(uint16_t x, uint16_t y, uint8_t buttons)
bool send_key_event_(uint32_t keysym, VncKeyAction action)
void on_shutdown() override
bool send_update_request_(bool incremental, uint32_t now)
void schedule_retry_(uint32_t now, VncFailure failure, const LogString *reason, bool was_running)
void skip_(uint32_t count, VncStep next)
void touch(touchscreen::TouchPoint tp) override
uint8_t rx_[RX_BUFFER_SIZE]
uint32_t update_delay_ms_
void fill_tile_(uint8_t x, uint8_t y, uint8_t w, uint8_t h, const uint8_t *pixel)
ESPHOME_ALWAYS_INLINE bool is_connected()
Return whether the node is connected to the network (through wifi, eth, ...)
std::vector< TouchPoint > TouchPoints_t
uint32_t extract_unicode_codepoint(const char *utf8_str, size_t *length)
Attempt to extract a 32 bit Unicode codepoint from a UTF-8 string.
ESPHOME_LOG_TAG(TAG, "vnc")
void vnc_auth_response(const uint8_t *key_progmem, uint8_t challenge[16])
VNC Authentication response: encrypts both 8-byte halves of the challenge in place.
@ VNC_FAILURE_AUTH_AMBIGUOUS
@ VNC_PIXEL_FORMAT_RGB565
@ VNC_PIXEL_FORMAT_RGB332
@ VNC_STEP_SECURITY_COUNT
@ VNC_STEP_SECURITY_TYPE_33
@ VNC_STEP_SECURITY_RESULT
PROGMEM_STRING_TABLE(PixelFormatStrings, "auto", "RGB565", "RGB332")
constexpr uint32_t fnv1a_hash_extend(uint32_t hash, const char *str)
Extend a FNV-1a hash with additional string data.
constexpr uint32_t FNV1_OFFSET_BASIS
FNV-1 32-bit offset basis.
constexpr uint32_t encode_uint32(uint8_t byte1, uint8_t byte2, uint8_t byte3, uint8_t byte4)
Encode a 32-bit value given four bytes in most to least significant byte order.
constexpr uint8_t * put_be16(uint8_t *dst, uint16_t value)
Write the low 16, 24 or 32 bits of value to dst, most significant byte first (be) or least significan...
constexpr uint16_t encode_uint16(uint8_t msb, uint8_t lsb)
Encode a 16-bit value given the most and least significant byte.
uint32_t IRAM_ATTR HOT millis()
Application App
Global storage of Application pointer - only one Application can exist.
constexpr uint8_t * put_be32(uint8_t *dst, uint32_t value)