Skip to content

Commit a2880a4

Browse files
feat(ap): Add support for DHCP Captive Portal (opt 114) (#11412)
* feat(ap): Add support for DHCP Captive Portal (opt 114) * feat(ap): No need to guard the function * ci(pre-commit): Apply automatic fixes --------- Co-authored-by: pre-commit-ci-lite[bot] <117423508+pre-commit-ci-lite[bot]@users.noreply.github.com>
1 parent b5c5655 commit a2880a4

File tree

3 files changed

+43
-2
lines changed

3 files changed

+43
-2
lines changed

libraries/DNSServer/examples/CaptivePortal/CaptivePortal.ino

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,9 @@ void handleNotFound() {
3434

3535
void setup() {
3636
Serial.begin(115200);
37-
WiFi.mode(WIFI_AP);
38-
WiFi.softAP("ESP32-DNSServer");
37+
WiFi.AP.begin();
38+
WiFi.AP.create("ESP32-DNSServer");
39+
WiFi.AP.enableDhcpCaptivePortal();
3940

4041
// by default DNSServer is started serving any "*" domain name. It will reply
4142
// AccessPoint's IP to all DNS request (this is required for Captive Portal detection)

libraries/WiFi/src/AP.cpp

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -305,6 +305,45 @@ bool APClass::enableNAPT(bool enable) {
305305
return true;
306306
}
307307

308+
bool APClass::enableDhcpCaptivePortal() {
309+
esp_err_t err = ESP_OK;
310+
static char captiveportal_uri[32] = {
311+
0,
312+
};
313+
314+
if (!started()) {
315+
log_e("AP must be first started to enable DHCP Captive Portal");
316+
return false;
317+
}
318+
319+
// Create Captive Portal URL: http://192.168.0.4
320+
strcpy(captiveportal_uri, "http://");
321+
strcat(captiveportal_uri, String(localIP()).c_str());
322+
323+
// Stop DHCPS
324+
err = esp_netif_dhcps_stop(_esp_netif);
325+
if (err && err != ESP_ERR_ESP_NETIF_DHCP_ALREADY_STOPPED) {
326+
log_e("DHCPS Stop Failed! 0x%04x: %s", err, esp_err_to_name(err));
327+
return false;
328+
}
329+
330+
// Enable DHCP Captive Portal
331+
err = esp_netif_dhcps_option(_esp_netif, ESP_NETIF_OP_SET, ESP_NETIF_CAPTIVEPORTAL_URI, captiveportal_uri, strlen(captiveportal_uri));
332+
if (err) {
333+
log_e("Could not set enable DHCP Captive Portal! 0x%x: %s", err, esp_err_to_name(err));
334+
return false;
335+
}
336+
337+
// Start DHCPS
338+
err = esp_netif_dhcps_start(_esp_netif);
339+
if (err) {
340+
log_e("DHCPS Start Failed! 0x%04x: %s", err, esp_err_to_name(err));
341+
return false;
342+
}
343+
344+
return true;
345+
}
346+
308347
String APClass::SSID(void) const {
309348
if (!started()) {
310349
return String();

libraries/WiFi/src/WiFiAP.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ class APClass : public NetworkInterface {
5353

5454
bool bandwidth(wifi_bandwidth_t bandwidth);
5555
bool enableNAPT(bool enable = true);
56+
bool enableDhcpCaptivePortal();
5657

5758
String SSID(void) const;
5859
uint8_t stationCount();

0 commit comments

Comments
 (0)