forked from ruvnet/RuView
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstream_sender.c
More file actions
116 lines (100 loc) · 3.45 KB
/
stream_sender.c
File metadata and controls
116 lines (100 loc) · 3.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
/**
* @file stream_sender.c
* @brief UDP stream sender for CSI frames.
*
* Opens a UDP socket and sends serialized ADR-018 frames to the aggregator.
*/
#include "stream_sender.h"
#include <string.h>
#include "esp_log.h"
#include "esp_timer.h"
#include "lwip/sockets.h"
#include "lwip/netdb.h"
#include "sdkconfig.h"
static const char *TAG = "stream_sender";
static int s_sock = -1;
static struct sockaddr_in s_dest_addr;
/**
* ENOMEM backoff state.
* When sendto fails with ENOMEM (errno 12), we suppress further sends for
* a cooldown period to let lwIP reclaim packet buffers. Without this,
* rapid-fire CSI callbacks can exhaust the pbuf pool and crash the device.
*/
static int64_t s_backoff_until_us = 0; /* esp_timer timestamp to resume */
#define ENOMEM_COOLDOWN_MS 100 /* suppress sends for 100 ms */
#define ENOMEM_LOG_INTERVAL 50 /* log every Nth suppressed send */
static uint32_t s_enomem_suppressed = 0;
static int sender_init_internal(const char *ip, uint16_t port)
{
s_sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
if (s_sock < 0) {
ESP_LOGE(TAG, "Failed to create socket: errno %d", errno);
return -1;
}
memset(&s_dest_addr, 0, sizeof(s_dest_addr));
s_dest_addr.sin_family = AF_INET;
s_dest_addr.sin_port = htons(port);
if (inet_pton(AF_INET, ip, &s_dest_addr.sin_addr) <= 0) {
ESP_LOGE(TAG, "Invalid target IP: %s", ip);
close(s_sock);
s_sock = -1;
return -1;
}
ESP_LOGI(TAG, "UDP sender initialized: %s:%d", ip, port);
return 0;
}
int stream_sender_init(void)
{
return sender_init_internal(CONFIG_CSI_TARGET_IP, CONFIG_CSI_TARGET_PORT);
}
int stream_sender_init_with(const char *ip, uint16_t port)
{
return sender_init_internal(ip, port);
}
int stream_sender_send(const uint8_t *data, size_t len)
{
if (s_sock < 0) {
return -1;
}
/* ENOMEM backoff: if we recently exhausted lwIP buffers, skip sends
* until the cooldown expires. This prevents the cascade of failed
* sendto calls that leads to a guru meditation crash. */
if (s_backoff_until_us > 0) {
int64_t now = esp_timer_get_time();
if (now < s_backoff_until_us) {
s_enomem_suppressed++;
if ((s_enomem_suppressed % ENOMEM_LOG_INTERVAL) == 1) {
ESP_LOGW(TAG, "sendto suppressed (ENOMEM backoff, %lu dropped)",
(unsigned long)s_enomem_suppressed);
}
return -1;
}
/* Cooldown expired — resume sending */
ESP_LOGI(TAG, "ENOMEM backoff expired, resuming sends (%lu were suppressed)",
(unsigned long)s_enomem_suppressed);
s_backoff_until_us = 0;
s_enomem_suppressed = 0;
}
int sent = sendto(s_sock, data, len, 0,
(struct sockaddr *)&s_dest_addr, sizeof(s_dest_addr));
if (sent < 0) {
if (errno == ENOMEM) {
/* Start backoff to let lwIP reclaim buffers */
s_backoff_until_us = esp_timer_get_time() +
(int64_t)ENOMEM_COOLDOWN_MS * 1000;
ESP_LOGW(TAG, "sendto ENOMEM — backing off for %d ms", ENOMEM_COOLDOWN_MS);
} else {
ESP_LOGW(TAG, "sendto failed: errno %d", errno);
}
return -1;
}
return sent;
}
void stream_sender_deinit(void)
{
if (s_sock >= 0) {
close(s_sock);
s_sock = -1;
ESP_LOGI(TAG, "UDP sender closed");
}
}