Added prints to verify Client connections and debugging prints

This commit is contained in:
2026-07-15 11:00:25 -05:00
parent 85280d721b
commit 50ca869180

View File

@@ -17,6 +17,7 @@
#include "esp_spiffs.h" #include "esp_spiffs.h"
#include "esp_heap_caps.h" #include "esp_heap_caps.h"
#include "esp_timer.h" #include "esp_timer.h"
#include "sdkconfig.h"
#include "lwip/sockets.h" #include "lwip/sockets.h"
#include "lwip/netdb.h" #include "lwip/netdb.h"
@@ -45,6 +46,57 @@ typedef struct
static volatile int g_active_modbus_clients = 0; static volatile int g_active_modbus_clients = 0;
/*
* Socket diagnostics are intentionally event-driven so the terminal stays quiet.
* A summary is printed only when the server starts, a client connects or
* disconnects, or accept() fails.
*/
static int count_active_client_slots(const int client_socks[], uint8_t max_clients)
{
int active = 0;
if (client_socks == NULL)
return 0;
for (uint8_t i = 0; i < max_clients; i++)
{
if (client_socks[i] >= 0)
active++;
}
return active;
}
static void log_socket_summary(const char *event,
int listen_sock,
const int client_socks[],
uint8_t max_clients,
int event_sock,
int event_errno)
{
const int active = count_active_client_slots(client_socks, max_clients);
if (event_errno != 0)
{
ESP_LOGW(TAG,
"Socket status [%s]: listen_sock=%d event_sock=%d active=%d/%u "
"lwip_max=%d free_heap=%u errno=%d (%s)",
event, listen_sock, event_sock, active, (unsigned)max_clients,
CONFIG_LWIP_MAX_SOCKETS,
(unsigned)heap_caps_get_free_size(MALLOC_CAP_8BIT),
event_errno, strerror(event_errno));
}
else
{
ESP_LOGI(TAG,
"Socket status [%s]: listen_sock=%d event_sock=%d active=%d/%u "
"lwip_max=%d free_heap=%u",
event, listen_sock, event_sock, active, (unsigned)max_clients,
CONFIG_LWIP_MAX_SOCKETS,
(unsigned)heap_caps_get_free_size(MALLOC_CAP_8BIT));
}
}
typedef struct typedef struct
{ {
uint8_t unit_id; uint8_t unit_id;
@@ -285,20 +337,33 @@ static void modbus_client_task(void *arg)
vTaskDelete(NULL); vTaskDelete(NULL);
} }
static void close_client_slot(int client_socks[], struct sockaddr_in client_addrs[], int index) static void close_client_slot(int listen_sock,
int client_socks[],
struct sockaddr_in client_addrs[],
uint8_t max_clients,
int index)
{ {
if (client_socks[index] >= 0) if (client_socks[index] >= 0)
{ {
const int closed_sock = client_socks[index];
ESP_LOGI(TAG, "Client disconnected: %s:%d", ESP_LOGI(TAG, "Client disconnected: %s:%d",
inet_ntoa(client_addrs[index].sin_addr), inet_ntoa(client_addrs[index].sin_addr),
ntohs(client_addrs[index].sin_port)); ntohs(client_addrs[index].sin_port));
shutdown(client_socks[index], 0); shutdown(closed_sock, 0);
close(client_socks[index]); close(closed_sock);
client_socks[index] = -1; client_socks[index] = -1;
if (g_active_modbus_clients > 0) if (g_active_modbus_clients > 0)
g_active_modbus_clients--; g_active_modbus_clients--;
log_socket_summary("client disconnected",
listen_sock,
client_socks,
max_clients,
closed_sock,
0);
} }
} }
@@ -367,6 +432,13 @@ static void modbus_server_task(void *arg)
(unsigned)g_device_cfg.modbus.port, (unsigned)g_device_cfg.modbus.port,
(unsigned)max_clients); (unsigned)max_clients);
log_socket_summary("server started",
listen_sock,
client_socks,
max_clients,
-1,
0);
while (1) while (1)
{ {
fd_set read_fds; fd_set read_fds;
@@ -410,20 +482,25 @@ static void modbus_server_task(void *arg)
if (client_sock < 0) if (client_sock < 0)
{ {
if (errno == 23 || errno == EMFILE || errno == ENFILE || errno == ENOMEM) const int accept_errno = errno;
/* One concise diagnostic line per failed accept. */
log_socket_summary("accept failed",
listen_sock,
client_socks,
max_clients,
-1,
accept_errno);
if (accept_errno == 23 ||
accept_errno == EMFILE ||
accept_errno == ENFILE ||
accept_errno == ENOMEM)
{ {
ESP_LOGW(TAG,
"accept failed: socket resources exhausted errno=%d active=%d max=%u; backing off",
errno,
g_active_modbus_clients,
(unsigned)max_clients);
vTaskDelay(pdMS_TO_TICKS(500)); vTaskDelay(pdMS_TO_TICKS(500));
} }
else else
{ {
ESP_LOGW(TAG, "accept failed: errno=%d", errno);
vTaskDelay(pdMS_TO_TICKS(50)); vTaskDelay(pdMS_TO_TICKS(50));
} }
} }
@@ -460,6 +537,13 @@ static void modbus_server_task(void *arg)
inet_ntoa(client_addr.sin_addr), inet_ntoa(client_addr.sin_addr),
ntohs(client_addr.sin_port), ntohs(client_addr.sin_port),
g_active_modbus_clients); g_active_modbus_clients);
log_socket_summary("client connected",
listen_sock,
client_socks,
max_clients,
client_sock,
0);
} }
} }
} }
@@ -481,14 +565,14 @@ static void modbus_server_task(void *arg)
ntohs(client_addrs[i].sin_port), ntohs(client_addrs[i].sin_port),
errno); errno);
close_client_slot(client_socks, client_addrs, i); close_client_slot(listen_sock, client_socks, client_addrs, max_clients, i);
client_last_seen[i] = 0; client_last_seen[i] = 0;
continue; continue;
} }
if (len == 0) if (len == 0)
{ {
close_client_slot(client_socks, client_addrs, i); close_client_slot(listen_sock, client_socks, client_addrs, max_clients, i);
client_last_seen[i] = 0; client_last_seen[i] = 0;
continue; continue;
} }
@@ -541,7 +625,7 @@ static void modbus_server_task(void *arg)
ntohs(client_addrs[i].sin_port), ntohs(client_addrs[i].sin_port),
errno); errno);
close_client_slot(client_socks, client_addrs, i); close_client_slot(listen_sock, client_socks, client_addrs, max_clients, i);
continue; continue;
} }
} }
@@ -555,7 +639,7 @@ static void modbus_server_task(void *arg)
inet_ntoa(client_addrs[i].sin_addr), inet_ntoa(client_addrs[i].sin_addr),
ntohs(client_addrs[i].sin_port)); ntohs(client_addrs[i].sin_port));
close_client_slot(client_socks, client_addrs, i); close_client_slot(listen_sock, client_socks, client_addrs, max_clients, i);
client_last_seen[i] = 0; client_last_seen[i] = 0;
} }
} }
@@ -883,6 +967,10 @@ void app_main(void)
ESP_LOGI(TAG, "Modbus Port: %u", (unsigned)g_device_cfg.modbus.port); ESP_LOGI(TAG, "Modbus Port: %u", (unsigned)g_device_cfg.modbus.port);
ESP_LOGI(TAG, "Configured device_count = %u", (unsigned)g_device_cfg.modbus.device_count); ESP_LOGI(TAG, "Configured device_count = %u", (unsigned)g_device_cfg.modbus.device_count);
ESP_LOGI(TAG, "Configured max_points per device = %u", (unsigned)g_device_cfg.modbus.max_points); ESP_LOGI(TAG, "Configured max_points per device = %u", (unsigned)g_device_cfg.modbus.max_points);
ESP_LOGI(TAG,
"Socket configuration: configured_modbus_clients=%u lwip_max_sockets=%d",
(unsigned)g_device_cfg.modbus.max_clients,
CONFIG_LWIP_MAX_SOCKETS);
for (i = 0; i < g_device_cfg.modbus.device_count; i++) for (i = 0; i < g_device_cfg.modbus.device_count; i++)
{ {
@@ -955,11 +1043,10 @@ void app_main(void)
if (dev == NULL) if (dev == NULL)
{ {
ESP_LOGE(TAG, ESP_LOGE(TAG,
"Failed to allocate Modbus device %u: size=%u free_heap=%u largest_block=%u", "Failed to allocate Modbus device %u: size=%u free_heap=%u",
(unsigned)(i + 1U), (unsigned)(i + 1U),
(unsigned)sizeof(modbus_device_t), (unsigned)sizeof(modbus_device_t),
(unsigned)heap_caps_get_free_size(MALLOC_CAP_8BIT), (unsigned)heap_caps_get_free_size(MALLOC_CAP_8BIT));
(unsigned)heap_caps_get_largest_free_block(MALLOC_CAP_8BIT));
continue; continue;
} }
@@ -1060,18 +1147,14 @@ void app_main(void)
size_t total_points_remaining = 0; size_t total_points_remaining = 0;
size_t free_heap = heap_caps_get_free_size(MALLOC_CAP_8BIT); size_t free_heap = heap_caps_get_free_size(MALLOC_CAP_8BIT);
size_t largest_block = heap_caps_get_largest_free_block(MALLOC_CAP_8BIT);
size_t point_struct_size = sizeof(modbus_point_t); size_t point_struct_size = sizeof(modbus_point_t);
size_t configured_points_per_device = g_device_cfg.modbus.max_points; size_t configured_points_per_device = g_device_cfg.modbus.max_points;
size_t configured_db_bytes_per_device = configured_points_per_device * point_struct_size; size_t configured_db_bytes_per_device = configured_points_per_device * point_struct_size;
size_t estimated_additional_devices_free_heap = 0; size_t estimated_additional_devices_free_heap = 0;
size_t estimated_additional_devices_largest_block = 0;
if (configured_db_bytes_per_device > 0U) if (configured_db_bytes_per_device > 0U)
{ {
estimated_additional_devices_free_heap = free_heap / configured_db_bytes_per_device; estimated_additional_devices_free_heap = free_heap / configured_db_bytes_per_device;
estimated_additional_devices_largest_block = largest_block / configured_db_bytes_per_device;
} }
for (i = 0; i < g_device_count; i++) for (i = 0; i < g_device_count; i++)
@@ -1116,14 +1199,12 @@ void app_main(void)
(unsigned)configured_db_bytes_per_device); (unsigned)configured_db_bytes_per_device);
ESP_LOGI(TAG, ESP_LOGI(TAG,
"Free heap=%u bytes, largest block=%u bytes", "Free heap=%u bytes",
(unsigned)free_heap, (unsigned)free_heap);
(unsigned)largest_block);
ESP_LOGI(TAG, ESP_LOGI(TAG,
"Estimated additional devices possible with current max_points: free_heap=%u, largest_block=%u", "Estimated additional devices possible with current max_points: %u",
(unsigned)estimated_additional_devices_free_heap, (unsigned)estimated_additional_devices_free_heap);
(unsigned)estimated_additional_devices_largest_block);
} }
ESP_LOGI(TAG, "---------------------------------"); ESP_LOGI(TAG, "---------------------------------");
@@ -1223,9 +1304,8 @@ void app_main(void)
if (server_ok != pdPASS) if (server_ok != pdPASS)
{ {
ESP_LOGE(TAG, ESP_LOGE(TAG,
"Failed to create modbus_server_task: free_heap=%u largest_block=%u", "Failed to create modbus_server_task: free_heap=%u",
(unsigned)heap_caps_get_free_size(MALLOC_CAP_8BIT), (unsigned)heap_caps_get_free_size(MALLOC_CAP_8BIT));
(unsigned)heap_caps_get_largest_free_block(MALLOC_CAP_8BIT));
return; return;
} }
@@ -1239,15 +1319,13 @@ void app_main(void)
if (override_ok != pdPASS) if (override_ok != pdPASS)
{ {
ESP_LOGE(TAG, ESP_LOGE(TAG,
"Failed to create modbus_override_task: free_heap=%u largest_block=%u", "Failed to create modbus_override_task: free_heap=%u",
(unsigned)heap_caps_get_free_size(MALLOC_CAP_8BIT), (unsigned)heap_caps_get_free_size(MALLOC_CAP_8BIT));
(unsigned)heap_caps_get_largest_free_block(MALLOC_CAP_8BIT));
return; return;
} }
ESP_LOGI(TAG, ESP_LOGI(TAG,
"Modbus tasks started: free_heap=%u largest_block=%u", "Modbus tasks started: free_heap=%u",
(unsigned)heap_caps_get_free_size(MALLOC_CAP_8BIT), (unsigned)heap_caps_get_free_size(MALLOC_CAP_8BIT));
(unsigned)heap_caps_get_largest_free_block(MALLOC_CAP_8BIT));
} }