ProtoCore v0.0.2
Deterministic, zero-heap network stack for embedded targets
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
11
12#if PC_ENABLE_RADIO_POWER
13
14#if defined(ARDUINO)
15#include "esp_wifi.h"
16#endif
17const char *pc_radio_ps_name(uint8_t mode)
18{
19 switch (mode)
20 {
21 case pc_radio_ps::PC_PS_MIN_MODEM:
22 return "min_modem";
23 case pc_radio_ps::PC_PS_MAX_MODEM:
24 return "max_modem";
25 case pc_radio_ps::PC_PS_NONE:
26 return "none";
27 default:
28 return "none";
29 }
30}
31
32#ifdef ARDUINO
33
34namespace
35{
36// Service vocabulary -> L1 vocabulary. Both are ours; neither is the vendor's.
37pc_phy_ps to_phy_ps(uint8_t mode)
38{
39 if (mode == pc_radio_ps::PC_PS_MIN_MODEM)
40 {
42 }
43 if (mode == pc_radio_ps::PC_PS_MAX_MODEM)
44 {
46 }
48}
49
50// Bulk-transfer keep-awake refcount, owned in one context (owner-context guard).
51struct RadioBusyCtx
52{
53 int held;
54};
55RadioBusyCtx s_busy;
56} // namespace
57
58void pc_radio_power_apply(void)
59{
61#if PC_RADIO_MAX_TX_DBM > 0
62 pc_phy_tx_power_set((int8_t)PC_RADIO_MAX_TX_DBM); // whole dBm; the backend owns the unit
63#endif
64}
65
66uint8_t pc_radio_ps_get(void)
67{
68 const pc_phy_ps m = pc_phy_ps_get();
70 {
71 return pc_radio_ps::PC_PS_MIN_MODEM;
72 }
74 {
75 return pc_radio_ps::PC_PS_MAX_MODEM;
76 }
77 return pc_radio_ps::PC_PS_NONE;
78}
79
80void pc_radio_busy_hold(void)
81{
82 if (s_busy.held++ == 0)
83 {
84 pc_phy_ps_set(pc_phy_ps::PC_PHY_PS_NONE); // modem sleep off during a bulk transfer
85 }
86}
87
88void pc_radio_busy_release(void)
89{
90 if (s_busy.held > 0 && --s_busy.held == 0)
91 {
92 pc_radio_power_apply(); // last transfer done: restore the configured mode
93 }
94}
95
96#else // host build - no radio
97
98void pc_radio_power_apply(void)
99{
100}
101uint8_t pc_radio_ps_get(void)
102{
103 return pc_radio_ps::PC_PS_NONE;
104}
105void pc_radio_busy_hold(void)
106{
107 // no-op on the host build: there is no radio to keep awake (the ESP32 branch above holds the
108 // modem-sleep refcount).
109}
110void pc_radio_busy_release(void)
111{
112 // no-op on the host build (see pc_radio_busy_hold).
113}
114
115#endif // ARDUINO
116
117#endif // PC_ENABLE_RADIO_POWER
pc_phy_ps pc_phy_ps_get(void)
Read the active power-save mode (PC_PHY_PS_NONE when unsupported).
Definition physical.cpp:108
bool pc_phy_tx_power_set(int8_t)
Cap transmit power.
Definition physical.cpp:112
bool pc_phy_ps_set(pc_phy_ps)
Apply a power-save mode.
Definition physical.cpp:104
Layer 1 (Physical) - link bring-up and live egress-interface reporting.
pc_phy_ps
Radio power-save mode, in the library's own vocabulary.
Definition physical.h:191
@ PC_PHY_PS_MIN_MODEM
Wake on every DTIM beacon.
@ PC_PHY_PS_NONE
Radio always on: lowest latency, highest average draw.
@ PC_PHY_PS_MAX_MODEM
Wake on a longer listen interval: lowest draw, highest latency.
#define PC_RADIO_WIFI_PS
WiFi modem-sleep mode: 0 = none (max perf), 1 = min modem, 2 = max modem.
#define PC_RADIO_MAX_TX_DBM
Max TX power cap in dBm (2..20); 0 = leave the platform default.
WiFi radio power controls (PC_ENABLE_RADIO_POWER).