Improve retry and cleanup logic

This commit is contained in:
Kelvin Ly 2025-03-18 22:36:58 -04:00
parent be008a2876
commit 121d39bfae
1 changed files with 30 additions and 17 deletions

View File

@ -67,7 +67,14 @@ void main_task(void* args) {
// TODO panic
}
int retry_time = 5000;
while (true) {
bool failed = true;
bool connect_success = false;
libssh2_socket_t sock = LIBSSH2_INVALID_SOCKET;
LIBSSH2_SESSION* session = 0;
LIBSSH2_CHANNEL* channel = 0;
xEventGroupWaitBits(s_wifi_event_group, WIFI_CONNECTED_BIT, 0, pdTRUE, portMAX_DELAY);
if (!(xEventGroupGetBits(s_wifi_event_group) & WIFI_CONNECTED_BIT)) {
continue;
@ -81,27 +88,21 @@ void main_task(void* args) {
rc = getaddrinfo("batmanbatman.local", "22", &hints, &results);
if (rc) {
ESP_LOGE(TAG, "getaddrinfo failed: %d", rc);
break;
goto cleanup;
}
if (!results) {
ESP_LOGE(TAG, "getaddrinfo returned no results");
// TODO sleep and try again?
break;
goto cleanup;
}
bool connect_success = false;
libssh2_socket_t sock = LIBSSH2_INVALID_SOCKET;
LIBSSH2_SESSION* session = 0;
LIBSSH2_CHANNEL* channel = 0;
for (struct addrinfo* rp = results; rp; rp = rp->ai_next) {
ESP_LOGI(TAG, "creating socket fam %d type %d proto %d", rp->ai_family, rp->ai_socktype, rp->ai_protocol);
sock = socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol);
if (sock == LIBSSH2_INVALID_SOCKET) {
ESP_LOGE(TAG, "failed to create socket");
continue;
goto cleanup;
}
if (connect(sock, results->ai_addr, results->ai_addrlen) != -1) {
@ -116,8 +117,7 @@ void main_task(void* args) {
freeaddrinfo(results);
if (!connect_success) {
// TODO I guess sleep for a bit and retry?
break;
goto cleanup;
}
ESP_LOGI(TAG, "connection established, creating SSH session...");
@ -125,7 +125,7 @@ void main_task(void* args) {
session = libssh2_session_init();
if (!session) {
ESP_LOGE(TAG, "could not create libssh2 session");
break;
goto cleanup;
}
//libssh2_session_set_read_timeout(session, 20);
@ -164,26 +164,27 @@ void main_task(void* args) {
if (rc) {
ESP_LOGE(TAG, "could not authenticate, err %d", rc);
break;
goto cleanup;
}
ESP_LOGE(TAG, "authenticated, opening channel...");
channel = libssh2_channel_open_session(session);
if (!channel) {
ESP_LOGE(TAG, "unable to open channel");
break;
goto cleanup;
}
rc = libssh2_channel_request_pty(channel, "vanilla");
if (rc) {
ESP_LOGE(TAG, "unable to request pty %d", rc);
break;
goto cleanup;
}
ESP_LOGE(TAG, "executing command...");
rc = libssh2_channel_exec(channel, "./runtest");
if (rc) {
ESP_LOGE(TAG, "could not open channel");
goto cleanup;
}
libssh2_session_set_timeout(session, 100);
@ -202,10 +203,12 @@ void main_task(void* args) {
ESP_LOGI(TAG, "received %s", tmp);
}
failed = false;
ESP_LOGI(TAG, "ssh exiting");
//rc = libssh2_channel_get_get_exit_status(channel);
//cleanup:
cleanup:
if (libssh2_channel_close(channel)) {
ESP_LOGE(TAG, "could not close channel");
}
@ -223,7 +226,17 @@ void main_task(void* args) {
shutdown(sock, 2);
LIBSSH2_SOCKET_CLOSE(sock);
}
break;
if (failed) {
retry_time *= 2;
} else {
retry_time = 5000;
}
if (retry_time > 10*60*1000) {
retry_time = 10*60*1000;
}
ESP_LOGI(TAG, "retrying in %d ms...", retry_time);
vTaskDelay(pdMS_TO_TICKS(retry_time));
}
while (true);