// Copyright (C) 2026 Douglas Quigg (dstroy0) <dquigg123@gmail.com>
// SPDX-License-Identifier: AGPL-3.0-or-later

/**
 * @file netadapt.cpp
 * @brief Network adaptation decision core (see netadapt.h).
 */

#include "services/netadapt/netadapt.h"

#if DETWS_ENABLE_NETADAPT

uint32_t detws_netadapt_window(uint32_t free_heap, uint32_t reserve, uint32_t min_win, uint32_t max_win)
{
    uint32_t ceil_win = max_win < min_win ? min_win : max_win;
    if (free_heap <= reserve)
        return min_win; // no spare heap: stay at the floor

    // A quarter of the heap above the reserve - leaves headroom for TX buffers, TLS, app state.
    uint32_t win = (free_heap - reserve) / 4;
    if (win < min_win)
        win = min_win;
    if (win > ceil_win)
        win = ceil_win;
    return win;
}

bool detws_netadapt_dhcp_fallback(uint32_t elapsed_ms, uint32_t attempts, uint32_t timeout_ms, uint32_t max_attempts)
{
    if (elapsed_ms >= timeout_ms)
        return true;
    if (max_attempts > 0 && attempts >= max_attempts)
        return true;
    return false;
}

#endif // DETWS_ENABLE_NETADAPT
