-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbuild.rs
More file actions
257 lines (214 loc) · 8.05 KB
/
build.rs
File metadata and controls
257 lines (214 loc) · 8.05 KB
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
use std::collections::HashSet;
use std::env;
use std::fs::File;
use std::io::Write;
use std::net::Ipv4Addr;
use std::path::Path;
fn main() {
let _ = dotenvy::from_filename(".env");
println!("cargo:rerun-if-changed=.env");
for key in ["AP_SSID", "AP_PASS"] {
if let Ok(val) = std::env::var(key) {
println!("cargo:rustc-env={key}={val}");
}
}
for key in ["AP_SSID", "AP_PASS"] {
if let Ok(val) = std::env::var(key) {
println!("cargo:rustc-env={key}={val}");
}
}
// Handle multiple Wi-Fi networks (ST_SSID_1, ST_PASS_1, etc.)
let mut wifi_networks = Vec::new();
for i in 1..=10 {
// Support up to 10 networks
let ssid_key = format!("ST_SSID_{}", i);
let pass_key = format!("ST_PASS_{}", i);
if let (Ok(ssid), Ok(pass)) = (std::env::var(&ssid_key), std::env::var(&pass_key)) {
wifi_networks.push((ssid, pass));
println!(
"cargo:rustc-env={}={}",
ssid_key,
std::env::var(&ssid_key).unwrap()
);
println!(
"cargo:rustc-env={}={}",
pass_key,
std::env::var(&pass_key).unwrap()
);
}
}
// Also support legacy single ST_SSID/ST_PASS for backwards compatibility
for key in ["ST_SSID", "ST_PASS"] {
if let Ok(val) = std::env::var(key) {
println!("cargo:rustc-env={key}={val}");
}
}
// Generate Wi-Fi networks configuration
generate_wifi_networks(&wifi_networks);
// Collect and generate static DHCP leases (MAC -> IP reservations)
let static_leases = collect_static_leases();
generate_static_leases(&static_leases);
// Generate device names for MAC address mapping
generate_device_names();
embuild::espidf::sysenv::output();
}
fn generate_wifi_networks(wifi_networks: &[(String, String)]) {
let out_dir = env::var("OUT_DIR").unwrap();
let dest_path = Path::new(&out_dir).join("wifi_networks.rs");
let mut f = File::create(&dest_path).unwrap();
writeln!(f, "// Auto-generated Wi-Fi networks configuration").unwrap();
writeln!(f, "").unwrap();
writeln!(f, "#[derive(Debug, Clone)]").unwrap();
writeln!(f, "pub struct WifiCredentials {{").unwrap();
writeln!(f, " pub ssid: &'static str,").unwrap();
writeln!(f, " pub password: &'static str,").unwrap();
writeln!(f, "}}").unwrap();
writeln!(f, "").unwrap();
writeln!(f, "pub const WIFI_NETWORKS: &[WifiCredentials] = &[").unwrap();
for (ssid, pass) in wifi_networks {
writeln!(f, " WifiCredentials {{").unwrap();
writeln!(f, " ssid: \"{}\",", ssid).unwrap();
writeln!(f, " password: \"{}\",", pass).unwrap();
writeln!(f, " }},").unwrap();
}
writeln!(f, "];").unwrap();
writeln!(f, "").unwrap();
writeln!(f, "pub fn get_network_count() -> usize {{").unwrap();
writeln!(f, " WIFI_NETWORKS.len()").unwrap();
writeln!(f, "}}").unwrap();
writeln!(f, "").unwrap();
writeln!(
f,
"pub fn get_network(index: usize) -> Option<&'static WifiCredentials> {{"
)
.unwrap();
writeln!(f, " WIFI_NETWORKS.get(index)").unwrap();
writeln!(f, "}}").unwrap();
writeln!(f, "").unwrap();
writeln!(
f,
"pub fn cycle_to_next_network(current_index: usize) -> usize {{"
)
.unwrap();
writeln!(f, " if WIFI_NETWORKS.is_empty() {{").unwrap();
writeln!(f, " 0").unwrap();
writeln!(f, " }} else {{").unwrap();
writeln!(f, " (current_index + 1) % WIFI_NETWORKS.len()").unwrap();
writeln!(f, " }}").unwrap();
writeln!(f, "}}").unwrap();
println!("cargo:rerun-if-changed=build.rs");
}
fn generate_device_names() {
let out_dir = env::var("OUT_DIR").unwrap();
let dest_path = Path::new(&out_dir).join("device_names.rs");
let mut f = File::create(&dest_path).unwrap();
// Generate 100 friendly device names
let mut device_names = Vec::new();
for _i in 0..100 {
let name = names::Generator::default().next().unwrap();
device_names.push(name);
}
writeln!(f, "// Auto-generated device names").unwrap();
writeln!(f, "pub const DEVICE_NAMES: &[&str] = &[").unwrap();
for name in &device_names {
writeln!(f, " \"{}\",", name).unwrap();
}
writeln!(f, "];").unwrap();
writeln!(f, "").unwrap();
writeln!(f, "/// Map MAC address to a friendly device name").unwrap();
writeln!(f, "pub fn mac_to_name(mac: &[u8; 6]) -> &'static str {{").unwrap();
writeln!(f, " let hash = (mac[5] as usize) % DEVICE_NAMES.len();").unwrap();
writeln!(f, " DEVICE_NAMES[hash]").unwrap();
writeln!(f, "}}").unwrap();
println!("cargo:rerun-if-changed=build.rs");
}
#[derive(Debug)]
struct StaticLeaseConfig {
mac: [u8; 6],
ip: Ipv4Addr,
}
fn format_mac(mac: &[u8; 6]) -> String {
let mut formatted = String::with_capacity(17);
for (idx, byte) in mac.iter().enumerate() {
if idx > 0 {
formatted.push(':');
}
formatted.push_str(&format!("{:02X}", byte));
}
formatted
}
fn collect_static_leases() -> Vec<StaticLeaseConfig> {
let mut leases = Vec::new();
let mut seen_macs = HashSet::new();
for (key, value) in env::vars() {
let stripped = key
.strip_prefix("DHCP_")
.or_else(|| key.strip_prefix("MAC_"));
let Some(raw_mac) = stripped else {
continue;
};
let mac = parse_mac(raw_mac).unwrap_or_else(|err| {
panic!(
"Invalid MAC identifier `{}` in environment variable `{}`: {}. Expected formats include AA:BB:CC:DD:EE:FF or aa-bb-cc-dd-ee-ff",
raw_mac, key, err
)
});
if !seen_macs.insert(mac) {
panic!(
"Duplicate static DHCP reservation for MAC {}; variable `{}`",
format_mac(&mac),
key
);
}
let ip: Ipv4Addr = value
.trim()
.parse()
.unwrap_or_else(|_| panic!("Invalid IPv4 address `{}` for `{}`", value, key));
leases.push(StaticLeaseConfig { mac, ip });
}
leases.sort_by(|a, b| a.mac.cmp(&b.mac));
leases
}
fn parse_mac(input: &str) -> Result<[u8; 6], &'static str> {
let filtered: String = input
.chars()
.filter(|c| !matches!(c, ':' | '-' | '_'))
.collect();
if filtered.len() != 12 {
return Err("expected 12 hexadecimal characters for MAC (after removing separators)");
}
let mut mac = [0u8; 6];
for (idx, chunk) in filtered.as_bytes().chunks(2).enumerate() {
let slice = std::str::from_utf8(chunk).map_err(|_| "non-hex character in MAC")?;
mac[idx] = u8::from_str_radix(slice, 16).map_err(|_| "invalid hex in MAC")?;
}
Ok(mac)
}
fn generate_static_leases(leases: &[StaticLeaseConfig]) {
let out_dir = env::var("OUT_DIR").unwrap();
let dest_path = Path::new(&out_dir).join("dhcp_leases.rs");
let mut f = File::create(&dest_path).unwrap();
writeln!(f, "// Auto-generated static DHCP reservations").unwrap();
writeln!(f, "#[derive(Debug, Clone, Copy)]").unwrap();
writeln!(f, "pub struct StaticLease {{").unwrap();
writeln!(f, " pub mac: [u8; 6],").unwrap();
writeln!(f, " pub ip: [u8; 4],").unwrap();
writeln!(f, "}}").unwrap();
writeln!(f, "").unwrap();
writeln!(f, "pub const STATIC_LEASES: &[StaticLease] = &[").unwrap();
for lease in leases {
let mac_bytes: Vec<String> = lease.mac.iter().map(|b| format!("0x{:02X}", b)).collect();
let ip_octets = lease.ip.octets();
writeln!(f, " StaticLease {{").unwrap();
writeln!(f, " mac: [{}],", mac_bytes.join(", ")).unwrap();
writeln!(
f,
" ip: [{}, {}, {}, {}],",
ip_octets[0], ip_octets[1], ip_octets[2], ip_octets[3]
)
.unwrap();
writeln!(f, " }},").unwrap();
}
writeln!(f, "];").unwrap();
println!("cargo:rerun-if-changed=build.rs");
}