DeterministicESPAsyncWebServer v6.28.0
Zero-allocation, bounded-execution async HTTP server for ESP32
Loading...
Searching...
No Matches
mdns_adaptive.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 mdns_adaptive.h
6 * @brief Adaptive mDNS beacon scheduling: RF-aware backoff, TTL refresher, auto-sleep beacon
7 * (DETWS_ENABLE_MDNS_ADAPTIVE).
8 *
9 * The mDNS service (shipped) announces records with a TTL; caches on the network evict a record when its
10 * TTL lapses, so a device must re-announce to stay discoverable. Two pressures shape *when* to announce:
11 *
12 * - **Crowded RF**: on a busy 2.4 GHz channel, hammering announces just adds collisions. So back the
13 * announce interval off (toward a ceiling) when contention is high, and recover it toward the nominal
14 * cadence when the air is quiet.
15 * - **A continuous refresher**: re-announce at ~half the record TTL (RFC 6762 cache eviction is at TTL)
16 * so caches never lapse in steady state.
17 * - **Auto-sleep beacons**: before entering a sleep window that would run past the next refresh, announce
18 * *now* so the record survives the sleep instead of lapsing while the radio is off.
19 *
20 * These are the pure scheduling decisions - what interval, and is an announce due (incl. before a sleep).
21 * The app owns the actual mDNS transmit. Wrap-safe time math, no heap, no stdlib, host-testable.
22 */
23
24#ifndef DETERMINISTICESPASYNCWEBSERVER_MDNS_ADAPTIVE_H
25#define DETERMINISTICESPASYNCWEBSERVER_MDNS_ADAPTIVE_H
26
27#include "ServerConfig.h"
28#include <stddef.h>
29#include <stdint.h>
30
31#if DETWS_ENABLE_MDNS_ADAPTIVE
32
33/** @brief Adaptive beacon state. */
34struct MdnsBeacon
35{
36 uint32_t base_ms; ///< nominal refresh cadence (e.g. TTL/2), and the backoff floor.
37 uint32_t max_ms; ///< backoff ceiling under heavy contention.
38 uint32_t cur_ms; ///< current adaptive interval.
39 uint16_t hi_thresh; ///< contention count at/above which the interval backs off.
40};
41
42/** @brief The continuous-refresher cadence for a record TTL: half the TTL, in milliseconds. */
43uint32_t detws_mdns_refresh_interval(uint32_t ttl_s);
44
45/** @brief Initialize a beacon. @p cur_ms starts at @p base_ms. */
46void detws_mdns_beacon_init(MdnsBeacon *b, uint32_t base_ms, uint32_t max_ms, uint16_t hi_thresh);
47
48/**
49 * @brief Adapt the interval to observed RF contention (announces/collisions seen in the last window).
50 * contention >= hi_thresh doubles the interval (capped at max_ms); contention == 0 halves it back
51 * toward base_ms; moderate contention holds.
52 * @return the new interval (ms).
53 */
54uint32_t detws_mdns_beacon_adapt(MdnsBeacon *b, uint16_t contention);
55
56/** @brief Is an announce due now? (wrap-safe: elapsed since @p last_ms >= the current interval). */
57bool detws_mdns_beacon_due(const MdnsBeacon *b, uint32_t last_ms, uint32_t now_ms);
58
59/**
60 * @brief Auto-sleep beacon: should we announce *before* sleeping for @p sleep_ms?
61 * True when the elapsed-since-last plus the sleep would meet/exceed the interval - i.e. the record
62 * would lapse mid-sleep - so we refresh proactively before the radio goes off.
63 */
64bool detws_mdns_beacon_presleep_due(const MdnsBeacon *b, uint32_t last_ms, uint32_t now_ms, uint32_t sleep_ms);
65
66#endif // DETWS_ENABLE_MDNS_ADAPTIVE
67#endif // DETERMINISTICESPASYNCWEBSERVER_MDNS_ADAPTIVE_H
User-facing configuration for DeterministicESPAsyncWebServer.