From 69c65a6a4033ca47dad49c90c6d2f2c7c2b24b78 Mon Sep 17 00:00:00 2001 From: Max Prokhorov Date: Thu, 13 Aug 2020 17:42:38 +0300 Subject: [PATCH] wifi: softap dhcp leases (#2320) Remember MAC for the current IP sequence number (e.g. lease 0 -> xxx.xxx.xxx.2, lease 1 -> xxx.xxx.xxx.3 and etc.) ~500 bytes of code --- code/espurna/config/general.h | 5 +++++ code/espurna/wifi.cpp | 19 +++++++++++++++++++ 2 files changed, 24 insertions(+) diff --git a/code/espurna/config/general.h b/code/espurna/config/general.h index 425173eebd..62d98d2ad4 100644 --- a/code/espurna/config/general.h +++ b/code/espurna/config/general.h @@ -529,6 +529,11 @@ // By default or when empty, admin password is used instead. #endif +#ifndef WIFI_AP_LEASES_SUPPORT +#define WIFI_AP_LEASES_SUPPORT 0 // (optional) Specify softAp MAC<->IP DHCP reservations + // Use `set wifiApLease# MAC`, where MAC is a valid 12-byte HEX number without colons +#endif + #ifndef WIFI_SLEEP_MODE #define WIFI_SLEEP_MODE WIFI_NONE_SLEEP // WIFI_NONE_SLEEP, WIFI_LIGHT_SLEEP or WIFI_MODEM_SLEEP #endif diff --git a/code/espurna/wifi.cpp b/code/espurna/wifi.cpp index 59f8d9dba7..51cb963fb1 100644 --- a/code/espurna/wifi.cpp +++ b/code/espurna/wifi.cpp @@ -764,6 +764,25 @@ void wifiSetup() { jw.enableSTA(true); } + // Note that maximum amount of stations is set by `WiFi.softAP(...)` call, but justwifi handles that. + // Default is 4, which we use here. However, maximum is 8. ref: + // https://arduino-esp8266.readthedocs.io/en/latest/esp8266wifi/soft-access-point-class.html#softap + #if WIFI_AP_LEASES_SUPPORT + for (unsigned char index = 0; index < 4; ++index) { + auto lease = getSetting({"wifiApLease", index}); + if (12 != lease.length()) { + break; + } + + uint8_t mac[6] = {0}; + if (!hexDecode(lease.c_str(), lease.length(), mac, sizeof(mac))) { + break; + } + + wifi_softap_add_dhcps_lease(mac); + } + #endif + #if JUSTWIFI_ENABLE_SMARTCONFIG if (_wifi_smartconfig_initial) jw.startSmartConfig(); #endif