Simplify
This commit is contained in:
@@ -64,7 +64,7 @@ typedef enum {
|
|||||||
* Shared state
|
* Shared state
|
||||||
* ---------------------------------------------------------------------- */
|
* ---------------------------------------------------------------------- */
|
||||||
static volatile led_mode_t s_led_mode = LED_MODE_WIFI_STATUS;
|
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 QueueHandle_t s_event_queue;
|
||||||
static TimerHandle_t s_action_timer;
|
static TimerHandle_t s_action_timer;
|
||||||
@@ -265,7 +265,7 @@ void app_main(void)
|
|||||||
/* GPIO and event infrastructure */
|
/* GPIO and event infrastructure */
|
||||||
gpio_init();
|
gpio_init();
|
||||||
|
|
||||||
s_event_queue = xQueueCreate(8, sizeof(event_type_t));
|
s_event_queue = xQueueCreate(4, sizeof(event_type_t));
|
||||||
configASSERT(s_event_queue);
|
configASSERT(s_event_queue);
|
||||||
|
|
||||||
/* 5-second one-shot timer (auto-reload disabled) */
|
/* 5-second one-shot timer (auto-reload disabled) */
|
||||||
|
|||||||
@@ -14,7 +14,6 @@
|
|||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <sys/select.h>
|
|
||||||
#include <sys/socket.h>
|
#include <sys/socket.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
|
|
||||||
@@ -128,24 +127,6 @@ static int sign_callback(LIBSSH2_SESSION *session,
|
|||||||
return 0;
|
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
|
* Public API
|
||||||
* ---------------------------------------------------------------------- */
|
* ---------------------------------------------------------------------- */
|
||||||
@@ -258,23 +239,14 @@ bool ssh_execute_command(const char *cmd)
|
|||||||
ESP_LOGI(TAG, "SSH authenticated as '%s'", CONFIG_SSH_USERNAME);
|
ESP_LOGI(TAG, "SSH authenticated as '%s'", CONFIG_SSH_USERNAME);
|
||||||
|
|
||||||
/* --- Open channel and execute command --------------------------------- */
|
/* --- Open channel and execute command --------------------------------- */
|
||||||
LIBSSH2_CHANNEL *channel;
|
LIBSSH2_CHANNEL *channel = libssh2_channel_open_session(session);
|
||||||
do {
|
|
||||||
channel = libssh2_channel_open_session(session);
|
|
||||||
if (!channel && libssh2_session_last_errno(session) == LIBSSH2_ERROR_EAGAIN) {
|
|
||||||
waitsocket(sock, session);
|
|
||||||
}
|
|
||||||
} while (!channel);
|
|
||||||
|
|
||||||
if (!channel) {
|
if (!channel) {
|
||||||
ESP_LOGE(TAG, "Failed to open SSH channel");
|
ESP_LOGE(TAG, "Failed to open SSH channel");
|
||||||
goto cleanup_session;
|
goto cleanup_session;
|
||||||
}
|
}
|
||||||
|
|
||||||
ESP_LOGI(TAG, "Executing: %s", cmd);
|
ESP_LOGI(TAG, "Executing: %s", cmd);
|
||||||
while ((rc = libssh2_channel_exec(channel, cmd)) == LIBSSH2_ERROR_EAGAIN) {
|
rc = libssh2_channel_exec(channel, cmd);
|
||||||
waitsocket(sock, session);
|
|
||||||
}
|
|
||||||
if (rc != 0) {
|
if (rc != 0) {
|
||||||
ESP_LOGE(TAG, "channel_exec failed: %d", rc);
|
ESP_LOGE(TAG, "channel_exec failed: %d", rc);
|
||||||
libssh2_channel_free(channel);
|
libssh2_channel_free(channel);
|
||||||
@@ -285,28 +257,14 @@ bool ssh_execute_command(const char *cmd)
|
|||||||
char buf[256];
|
char buf[256];
|
||||||
int bytes;
|
int bytes;
|
||||||
ESP_LOGI(TAG, "--- command output ---");
|
ESP_LOGI(TAG, "--- command output ---");
|
||||||
for (;;) {
|
while ((bytes = libssh2_channel_read(channel, buf, sizeof(buf) - 1)) > 0) {
|
||||||
do {
|
|
||||||
bytes = libssh2_channel_read(channel, buf, sizeof(buf) - 1);
|
|
||||||
} while (bytes == LIBSSH2_ERROR_EAGAIN && (waitsocket(sock, session), 1));
|
|
||||||
|
|
||||||
if (bytes <= 0) break;
|
|
||||||
buf[bytes] = '\0';
|
buf[bytes] = '\0';
|
||||||
printf("%s", buf);
|
printf("%s", buf);
|
||||||
}
|
}
|
||||||
ESP_LOGI(TAG, "--- end output ---");
|
ESP_LOGI(TAG, "--- end output ---");
|
||||||
|
|
||||||
/* --- Shutdown --------------------------------------------------------- */
|
/* --- Shutdown --------------------------------------------------------- */
|
||||||
while ((rc = libssh2_channel_send_eof(channel)) == LIBSSH2_ERROR_EAGAIN) {
|
libssh2_channel_close(channel);
|
||||||
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);
|
|
||||||
}
|
|
||||||
|
|
||||||
int exit_status = libssh2_channel_get_exit_status(channel);
|
int exit_status = libssh2_channel_get_exit_status(channel);
|
||||||
ESP_LOGI(TAG, "Command exited with status: %d", exit_status);
|
ESP_LOGI(TAG, "Command exited with status: %d", exit_status);
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user