From bff0427f2f07eaf0d60888556bf12ca9717c7191 Mon Sep 17 00:00:00 2001 From: Jonathan Berrisch Date: Sun, 1 Mar 2026 20:26:41 +0100 Subject: [PATCH] Simplify --- main/main.c | 4 ++-- main/ssh_client.c | 50 ++++------------------------------------------- 2 files changed, 6 insertions(+), 48 deletions(-) diff --git a/main/main.c b/main/main.c index 96e6a45..5501103 100644 --- a/main/main.c +++ b/main/main.c @@ -64,7 +64,7 @@ typedef enum { * Shared state * ---------------------------------------------------------------------- */ static volatile led_mode_t s_led_mode = LED_MODE_WIFI_STATUS; -static volatile btn_state_t s_btn_state = BTN_STATE_IDLE; +static btn_state_t s_btn_state = BTN_STATE_IDLE; static QueueHandle_t s_event_queue; static TimerHandle_t s_action_timer; @@ -265,7 +265,7 @@ void app_main(void) /* GPIO and event infrastructure */ gpio_init(); - s_event_queue = xQueueCreate(8, sizeof(event_type_t)); + s_event_queue = xQueueCreate(4, sizeof(event_type_t)); configASSERT(s_event_queue); /* 5-second one-shot timer (auto-reload disabled) */ diff --git a/main/ssh_client.c b/main/ssh_client.c index a001028..a86218c 100644 --- a/main/ssh_client.c +++ b/main/ssh_client.c @@ -14,7 +14,6 @@ #include #include #include -#include #include #include @@ -128,24 +127,6 @@ static int sign_callback(LIBSSH2_SESSION *session, return 0; } -/* ------------------------------------------------------------------------- - * Helpers for non-blocking libssh2 - * ---------------------------------------------------------------------- */ -static int waitsocket(int sock_fd, LIBSSH2_SESSION *session) -{ - struct timeval tv = { .tv_sec = 10, .tv_usec = 0 }; - fd_set fd; - FD_ZERO(&fd); - FD_SET(sock_fd, &fd); - - fd_set *rfd = NULL, *wfd = NULL; - int dir = libssh2_session_block_directions(session); - if (dir & LIBSSH2_SESSION_BLOCK_INBOUND) rfd = &fd; - if (dir & LIBSSH2_SESSION_BLOCK_OUTBOUND) wfd = &fd; - - return select(sock_fd + 1, rfd, wfd, NULL, &tv); -} - /* ------------------------------------------------------------------------- * Public API * ---------------------------------------------------------------------- */ @@ -258,23 +239,14 @@ bool ssh_execute_command(const char *cmd) ESP_LOGI(TAG, "SSH authenticated as '%s'", CONFIG_SSH_USERNAME); /* --- Open channel and execute command --------------------------------- */ - LIBSSH2_CHANNEL *channel; - do { - channel = libssh2_channel_open_session(session); - if (!channel && libssh2_session_last_errno(session) == LIBSSH2_ERROR_EAGAIN) { - waitsocket(sock, session); - } - } while (!channel); - + LIBSSH2_CHANNEL *channel = libssh2_channel_open_session(session); if (!channel) { ESP_LOGE(TAG, "Failed to open SSH channel"); goto cleanup_session; } ESP_LOGI(TAG, "Executing: %s", cmd); - while ((rc = libssh2_channel_exec(channel, cmd)) == LIBSSH2_ERROR_EAGAIN) { - waitsocket(sock, session); - } + rc = libssh2_channel_exec(channel, cmd); if (rc != 0) { ESP_LOGE(TAG, "channel_exec failed: %d", rc); libssh2_channel_free(channel); @@ -285,28 +257,14 @@ bool ssh_execute_command(const char *cmd) char buf[256]; int bytes; ESP_LOGI(TAG, "--- command output ---"); - for (;;) { - do { - bytes = libssh2_channel_read(channel, buf, sizeof(buf) - 1); - } while (bytes == LIBSSH2_ERROR_EAGAIN && (waitsocket(sock, session), 1)); - - if (bytes <= 0) break; + while ((bytes = libssh2_channel_read(channel, buf, sizeof(buf) - 1)) > 0) { buf[bytes] = '\0'; printf("%s", buf); } ESP_LOGI(TAG, "--- end output ---"); /* --- Shutdown --------------------------------------------------------- */ - while ((rc = libssh2_channel_send_eof(channel)) == LIBSSH2_ERROR_EAGAIN) { - waitsocket(sock, session); - } - while (libssh2_channel_wait_eof(channel) == LIBSSH2_ERROR_EAGAIN) { - waitsocket(sock, session); - } - while (libssh2_channel_wait_closed(channel) == LIBSSH2_ERROR_EAGAIN) { - waitsocket(sock, session); - } - + libssh2_channel_close(channel); int exit_status = libssh2_channel_get_exit_status(channel); ESP_LOGI(TAG, "Command exited with status: %d", exit_status);