Add wifi hostname change and persistence between reboot - #171
Conversation
|
Ok, I resolved all my previous issues and I think this is a clean way. |
|
Thanks for the contrib, Could you first clarify the intended use case: setting a persistent hostname before connecting in Wi-Fi Client/Web UI mode, changing it at runtime, or both ? void WifiController::handleHostname(const TerminalCommand &cmd)
{
const std::string hostname = cmd.getSubcommand();
if (hostname.empty()) {
terminalView.println("Usage: hostname <name>");
return;
}
if (hostname.length() > 32) {
terminalView.println("Invalid hostname. Maximum 32 characters.");
return;
}
if (hostname.front() == '-' || hostname.back() == '-') {
terminalView.println("Invalid hostname. Cannot start or end with '-'.");
return;
}
for (char c : hostname) {
if (!(argTransformer.isValidAlphanumeric(std::string(1, c)) || c == '-')) {
terminalView.println("Invalid hostname. Use letters, numbers, and '-' only, ");
return;
}
}
// Save hostname to NVS and set it
nvsService.open();
nvsService.saveString(GlobalState::getInstance().getNvsHostnameField(), hostname);
nvsService.close();
WiFi.setHostname(hostname.c_str());
terminalView.println("WiFi: Hostname set to: " + wifiService.getHostname());
terminalView.println("\n [⚠️ WARNING] ");
terminalView.println(" Restart your device for hostname change to take effect.*\n");
}WifiController should not call the Arduino WiFi API directly or know which library implements Wi-Fi. It should only interact with IWifiService Also, it would be better to handle the hostname parsing and validation with something like argTransformer.isValidHostname`, even if the current way (validating in the controller) is also acceptable. One last question: would it make more sense to configure the hostname when entering Wi-Fi mode or through Since the hostname must be applied before connecting, changing it while using the Web CLI or when already connected would most likely require a reconnection to take effect |
Added the hostname handler in Wifi controller
Added logic in main.cpp to fetch hostname from NVS and apply it before the wifi is used
Added a argtransformer function to check if the string is alphanumeric only
Added the help description
Added globalstate for hostname