1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
//! # esp-net-easy
//! Copyright (C) 2026 Jorge Andre Castro
//!
//! `no_std` asynchronous abstraction that simplifies Wi-Fi setup and network
//! stack startup on ESP32 chips using the [Hazard3](https://github.com/jorgeandrecastro/Hazard3)
//! RISC-V ecosystem.
//!
//! This version targets **esp-hal 1.x / esp-radio 0.18**, together with the
//! Hazard3 packages `hazard3-executor`, `hazard3-net-async`, and `hazard3-time`.
//!
//! This crate encapsulates the setup required to start a Wi-Fi station client
//! with a static IPv4 address: network stack configuration, static resource
//! allocation, and background task startup.
//!
//! ## Application requirements
//!
//! Application code must initialize the ESP runtime (`esp_rtos::start`) because
//! it requires ownership of a hardware timer.
//!
//! Since `esp-radio` 0.18, there is no separate `esp_radio::init()` or
//! `esp_radio::Controller`. The radio controller is initialized through
//! `esp_radio::wifi::new`, which takes the HAL `WIFI` peripheral directly.
//!
//! ## Usage example
//!
//! ```rust,ignore
//! #![no_std]
//! #![no_main]
//!
//! use embassy_executor::Spawner;
//! use esp_hal::clock::CpuClock;
//! use esp_hal::rng::Rng;
//! use esp_hal::timer::timg::TimerGroup;
//! use esp_net_easy::{init_esp_net_easy, WifiEasyConfig};
//!
//! #[esp_rtos::main]
//! async fn main(spawner: Spawner) -> ! {
//! let config = esp_hal::Config::default().with_cpu_clock(CpuClock::max());
//! let peripherals = esp_hal::init(config);
//!
//! esp_alloc::heap_allocator!(size: 72 * 1024);
//!
//! let timg0 = TimerGroup::new(peripherals.TIMG0);
//! esp_rtos::start(timg0.timer0);
//!
//! let net_config = WifiEasyConfig {
//! ssid: "MonReseauWifi",
//! password: "MotDePasseSecret",
//! static_ip: "192.168.1.50",
//! gateway_ip: "192.168.1.1",
//! dns_server: Some("8.8.8.8"),
//! };
//!
//! let stack = init_esp_net_easy(&spawner, peripherals.WIFI, net_config).await;
//!
//! // The stack is ready for TCP and UDP sockets.
//! loop {
//! embassy_time::Timer::after(embassy_time::Duration::from_secs(1)).await;
//! }
//! }
//! ```
//!
//! ## Internal operation
//!
//! 1. `esp-radio` initializes the controller and station interface from the
//! application-provided `WIFI` peripheral.
//! 2. The static IPv4 address, `/24` mask, gateway, and optional DNS server are
//! parsed and prepared for the network interface configuration.
//! 3. The ESP32 hardware RNG generates a 64-bit seed for the network stack.
//! 4. Static stack storage and packet pool are allocated through [`static_cell`].
//! 5. Hazard3-compatible background tasks are spawned:
//! - [`net_task`] runs the network stack runner.
//! - [`wifi_connection_task`] configures station mode and reconnects after
//! disconnection.
//! 6. The function returns the ready [`net::Stack`].
//!
//! ## Known limitations
//!
//! - Only Wi-Fi Station mode with static **IPv4** is supported.
//! - DHCP, Access Point mode, and IPv6 are not implemented.
//! - The subnet mask is fixed at `/24`.
//! - `ssid`, `password`, `static_ip`, and `gateway_ip` must be `&'static str`.
//! - Invalid configuration values may panic.
use Ipv4Addr;
use FromStr;
use Spawner;
use embassy_net as net;
use ;
use Rng;
use ;
/// Public macro for simplifying static allocation required by the network stack.
///
/// It promotes a value to a `'static` reference without unsafe code by using
/// [`static_cell::StaticCell`]. [`init_esp_net_easy`] uses it internally to
/// allocate network stack resources, but it is also available to applications.
/// Hazard3-compatible task that keeps the network stack running in the background.
///
/// This task runs the [`net::Runner`] loop indefinitely while the
/// application needs TCP and UDP sockets.
async
/// Hazard3-compatible task that configures Wi-Fi Station mode and manages
/// connections and reconnections.
///
/// This task configures Station mode with the supplied SSID and password,
/// starts the Wi-Fi controller, connects to the access point, waits for
/// disconnections, and attempts automatic reconnection.
async
/// Network initialization configuration.
///
/// String fields use `&'static str` values, typically literals or constants,
/// because they configure hardware at startup and are not reallocated.
///
/// # Champs
///
/// - `ssid`: name of the Wi-Fi access point.
/// - `password`: password for the access point.
/// - `static_ip`: static IPv4 address assigned to the ESP32.
/// - `gateway_ip`: IPv4 address of the router.
/// - `dns_server`: optional DNS server address. `None` leaves it unconfigured;
/// an invalid address is silently ignored.
///
/// # Exemple
///
/// ```rust,ignore
/// let config = WifiEasyConfig {
/// ssid: "MyWiFiNetwork",
/// password: "SecretPassword",
/// static_ip: "192.168.1.50",
/// gateway_ip: "192.168.1.1",
/// dns_server: Some("8.8.8.8"),
/// };
/// ```
/// Initializes ESP32 Wi-Fi and starts the Hazard3-compatible network stack.
///
/// This function creates the controller and station interface, prepares static
/// IPv4 parameters, allocates stack storage and packet pool resources, starts
/// background tasks, and returns the network stack.
///
/// # Arguments
/// * `spawner` - Hazard3-compatible executor spawner for background tasks.
/// * `wifi_peripheral` - HAL `WIFI` peripheral.
/// * `config` - Network parameters (SSID, password, and static IP).
///
/// # Returns
/// Returns a static network stack reference ready for TCP and UDP sockets.
///
/// # Panics
///
/// This function panics if:
/// - `config.static_ip` or `config.gateway_ip` is not a valid IPv4 address.
/// - `esp-radio` Wi-Fi initialization fails.
/// - A background task cannot be spawned.
///
/// # Exemple
///
/// ```rust,ignore
/// let stack = init_esp_net_easy(&spawner, peripherals.WIFI, config).await;
/// // `stack` can now be used to create TCP and UDP sockets.
/// ```
pub async