Skip to content

Commit

Permalink
wifi: softap dhcp leases (#2320)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
mcspr committed Aug 13, 2020
1 parent f849dc4 commit 69c65a6
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
5 changes: 5 additions & 0 deletions code/espurna/config/general.h
Expand Up @@ -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
Expand Down
19 changes: 19 additions & 0 deletions code/espurna/wifi.cpp
Expand Up @@ -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
Expand Down

1 comment on commit 69c65a6

@mcspr
Copy link
Collaborator Author

@mcspr mcspr commented on 69c65a6 Sep 3, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does not actually work, as it turns out.

Because of the way DHCP is integrated into the Core logic, what actually needs to happen is:

diff --git a/code/espurna/wifi.cpp b/code/espurna/wifi.cpp
index 51cb963f..0f842562 100644
--- a/code/espurna/wifi.cpp
+++ b/code/espurna/wifi.cpp
@@ -759,15 +759,13 @@ void wifiSetup() {

     _wifiConfigure();

-    if (WiFiApMode::Enabled ==_wifi_ap_mode) {
-        jw.enableAP(true);
-        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
+#if WIFI_AP_LEASES_SUPPORT
+    wifiRegister([](justwifi_messages_t code, char*) {
+        if (MESSAGE_ACCESSPOINT_CREATING != code) return;
+
         for (unsigned char index = 0; index < 4; ++index) {
             auto lease = getSetting({"wifiApLease", index});
             if (12 != lease.length()) {
@@ -781,7 +779,13 @@ void wifiSetup() {

             wifi_softap_add_dhcps_lease(mac);
         }
-    #endif
+    });
+#endif
+
+    if (WiFiApMode::Enabled ==_wifi_ap_mode) {
+        jw.enableAP(true);
+        jw.enableSTA(true);
+    }

     #if JUSTWIFI_ENABLE_SMARTCONFIG
         if (_wifi_smartconfig_initial) jw.startSmartConfig();

Plus, JustWifi needs to have it's event callback moved inbetween softapconfig and softap calls:

diff --git a/src/JustWifi.cpp b/src/JustWifi.cpp
index d52f826..5232052 100644
--- a/src/JustWifi.cpp
+++ b/src/JustWifi.cpp
@@ -328,8 +328,6 @@ bool JustWifi::_doAP() {
         _softap.ssid = strdup(_hostname);
     }

-    _doCallback(MESSAGE_ACCESSPOINT_CREATING);
-
     WiFi.enableAP(true);

     // Configure static options
@@ -337,6 +335,8 @@ bool JustWifi::_doAP() {
         WiFi.softAPConfig(_softap.ip, _softap.gw, _softap.netmask);
     }

+    _doCallback(MESSAGE_ACCESSPOINT_CREATING);
+
     if (_softap.pass) {
         WiFi.softAP(_softap.ssid, _softap.pass);
     } else {

(idk how it did work initially in testing...)

Will need to patch this + refactor jw runtime a bit

Please sign in to comment.