DeterministicESPAsyncWebServer v6.28.0
Zero-allocation, bounded-execution async HTTP server for ESP32
Loading...
Searching...
No Matches
radio_power.cpp
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 radio_power.cpp
6 * @brief Modem-sleep mode names (pure) + esp_wifi apply/readback (ESP32).
7 */
8
10
11#if DETWS_ENABLE_RADIO_POWER
12
13const char *detws_radio_ps_name(uint8_t mode)
14{
15 switch (mode)
16 {
17 case DetwsRadioPs::DETWS_PS_MIN_MODEM:
18 return "min_modem";
19 case DetwsRadioPs::DETWS_PS_MAX_MODEM:
20 return "max_modem";
21 case DetwsRadioPs::DETWS_PS_NONE:
22 return "none";
23 default:
24 return "none";
25 }
26}
27
28#ifdef ARDUINO
29
30#include "esp_wifi.h"
31
32namespace
33{
34wifi_ps_type_t to_esp_ps(uint8_t mode)
35{
36 if (mode == DetwsRadioPs::DETWS_PS_MIN_MODEM)
37 return WIFI_PS_MIN_MODEM;
38 if (mode == DetwsRadioPs::DETWS_PS_MAX_MODEM)
39 return WIFI_PS_MAX_MODEM;
40 return WIFI_PS_NONE;
41}
42
43// Bulk-transfer keep-awake refcount, owned in one context (owner-context guard).
44struct RadioBusyCtx
45{
46 int held;
47};
48RadioBusyCtx s_busy;
49} // namespace
50
51void detws_radio_power_apply(void)
52{
53 esp_wifi_set_ps(to_esp_ps(DETWS_RADIO_WIFI_PS));
54#if DETWS_RADIO_MAX_TX_DBM > 0
55 esp_wifi_set_max_tx_power((int8_t)(DETWS_RADIO_MAX_TX_DBM * 4)); // API unit: 0.25 dBm
56#endif
57}
58
59uint8_t detws_radio_ps_get(void)
60{
61 wifi_ps_type_t m = WIFI_PS_NONE;
62 if (esp_wifi_get_ps(&m) != ESP_OK)
63 return DetwsRadioPs::DETWS_PS_NONE;
64 if (m == WIFI_PS_MIN_MODEM)
65 return DetwsRadioPs::DETWS_PS_MIN_MODEM;
66 if (m == WIFI_PS_MAX_MODEM)
67 return DetwsRadioPs::DETWS_PS_MAX_MODEM;
68 return DetwsRadioPs::DETWS_PS_NONE;
69}
70
71void detws_radio_busy_hold(void)
72{
73 if (s_busy.held++ == 0)
74 esp_wifi_set_ps(WIFI_PS_NONE); // modem sleep off while a bulk transfer is in flight
75}
76
77void detws_radio_busy_release(void)
78{
79 if (s_busy.held > 0 && --s_busy.held == 0)
80 detws_radio_power_apply(); // last transfer done: restore the configured mode
81}
82
83#else // host build - no radio
84
85void detws_radio_power_apply(void)
86{
87}
88uint8_t detws_radio_ps_get(void)
89{
90 return DetwsRadioPs::DETWS_PS_NONE;
91}
92void detws_radio_busy_hold(void)
93{
94 // no-op on the host build: there is no radio to keep awake (the ESP32 branch above holds the
95 // modem-sleep refcount).
96}
97void detws_radio_busy_release(void)
98{
99 // no-op on the host build (see detws_radio_busy_hold).
100}
101
102#endif // ARDUINO
103
104#endif // DETWS_ENABLE_RADIO_POWER
#define DETWS_RADIO_MAX_TX_DBM
Max TX power cap in dBm (2..20); 0 = leave the platform default.
#define DETWS_RADIO_WIFI_PS
WiFi modem-sleep mode: 0 = none (max perf), 1 = min modem, 2 = max modem.
WiFi radio power controls (DETWS_ENABLE_RADIO_POWER).