DeterministicESPAsyncWebServer v6.28.0
Zero-allocation, bounded-execution async HTTP server for ESP32
Loading...
Searching...
No Matches
netadapt.h
Go to the documentation of this file.
1// Copyright (C) 2026 Douglas Quigg (dstroy0) <dquigg123@gmail.com>
2// SPDX-License-Identifier: AGPL-3.0-or-later
3
4/**
5 * @file netadapt.h
6 * @brief Network adaptation decisions: TCP window sizing by free RAM + DHCP->static fallback
7 * (DETWS_ENABLE_NETADAPT).
8 *
9 * Two pure decisions a network manager needs on a memory-constrained, sometimes-headless device:
10 *
11 * - `detws_netadapt_window()` - size the TCP receive window / RX buffer from the free heap, so a device
12 * with RAM to spare uses a bigger window for throughput while a low-memory one shrinks to stay alive.
13 * Keeps a reserve untouched and clamps to a sane [min, max].
14 *
15 * - `detws_netadapt_dhcp_fallback()` - decide when to stop waiting on DHCP and configure a static IP, so
16 * a node on a network with no DHCP server still comes up. Triggers once the elapsed wait exceeds the
17 * timeout or the retry budget is spent.
18 *
19 * Pure, zero heap, no stdlib, host-testable; the app applies the results (lwIP window / netif config).
20 */
21
22#ifndef DETERMINISTICESPASYNCWEBSERVER_NETADAPT_H
23#define DETERMINISTICESPASYNCWEBSERVER_NETADAPT_H
24
25#include "ServerConfig.h"
26#include <stddef.h>
27#include <stdint.h>
28
29#if DETWS_ENABLE_NETADAPT
30
31/**
32 * @brief Recommend a TCP receive window / RX buffer size (bytes) from the free heap.
33 * @param free_heap current free heap (bytes).
34 * @param reserve heap to leave untouched for everything else (bytes).
35 * @param min_win floor for the returned window (always usable).
36 * @param max_win ceiling for the returned window.
37 * @return a window in [min_win, max_win]: min_win if the heap at/below the reserve, otherwise a quarter
38 * of the heap above the reserve (headroom for other buffers), clamped to the ceiling.
39 *
40 * If max_win < min_win the result is min_win.
41 */
42uint32_t detws_netadapt_window(uint32_t free_heap, uint32_t reserve, uint32_t min_win, uint32_t max_win);
43
44/**
45 * @brief Should we stop waiting on DHCP and switch to the configured static IP?
46 * @param elapsed_ms time since the DHCP attempt started (ms).
47 * @param attempts DHCP attempts made so far.
48 * @param timeout_ms per-run wait budget before falling back (ms).
49 * @param max_attempts attempt budget before falling back (0 = ignore the attempt count).
50 * @return true once the elapsed wait exceeds @p timeout_ms, or (when @p max_attempts > 0) the attempts
51 * reach @p max_attempts - i.e. DHCP has failed for long/often enough to fall back.
52 */
53bool detws_netadapt_dhcp_fallback(uint32_t elapsed_ms, uint32_t attempts, uint32_t timeout_ms, uint32_t max_attempts);
54
55#endif // DETWS_ENABLE_NETADAPT
56#endif // DETERMINISTICESPASYNCWEBSERVER_NETADAPT_H
User-facing configuration for DeterministicESPAsyncWebServer.