Skip to content

Commit df47418

Browse files
committed
Added ability to set start and end ip for softAP
Base on #3562 and discussed in #6031
1 parent e77f96c commit df47418

File tree

2 files changed

+42
-10
lines changed

2 files changed

+42
-10
lines changed

libraries/ESP8266WiFi/src/ESP8266WiFiAP.cpp

Lines changed: 41 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -194,8 +194,10 @@ bool ESP8266WiFiAPClass::softAP(const String& ssid, const String& passphrase, in
194194
* @param local_ip access point IP
195195
* @param gateway gateway IP (0.0.0.0 to disable)
196196
* @param subnet subnet mask
197+
* @param dhcp_start first IP assigned by DHCP
198+
* @param dhcp_end last IP assigned by DHCP
197199
*/
198-
bool ESP8266WiFiAPClass::softAPConfig(IPAddress local_ip, IPAddress gateway, IPAddress subnet) {
200+
bool ESP8266WiFiAPClass::softAPConfig(IPAddress local_ip, IPAddress gateway, IPAddress subnet, IPAddress dhcp_start, IPAddress dhcp_end) {
199201
DEBUG_WIFI("[APConfig] local_ip: %s gateway: %s subnet: %s\n", local_ip.toString().c_str(), gateway.toString().c_str(), subnet.toString().c_str());
200202
if(!WiFi.enableAP(true)) {
201203
// enable AP failed
@@ -214,10 +216,24 @@ bool ESP8266WiFiAPClass::softAPConfig(IPAddress local_ip, IPAddress gateway, IPA
214216
return false;
215217
}
216218
struct ip_info info;
219+
217220
info.ip.addr = local_ip.v4();
218221
info.gw.addr = gateway.v4();
219222
info.netmask.addr = subnet.v4();
220-
223+
224+
uint32_t softap_ip = htonl(info.ip.addr);
225+
uint32_t start_ip = htonl(dhcp_start.v4());
226+
uint32_t end_ip = htonl(dhcp_end.v4());
227+
softap_ip >>= 8;
228+
if (( start_ip >> 8 != softap_ip ) || ( end_ip >> 8 != softap_ip )) {
229+
DEBUG_WIFI("[APConfig] dhcp_start or dhcp_end isn't in range of local_ip Address!\n");
230+
DEBUG_WIFI("[APConfig] set dfault vaule\n");
231+
dhcp_start = local_ip;
232+
dhcp_start[3] += 99;
233+
dhcp_end = dhcp_start;
234+
dhcp_end[3] += 100;
235+
}
236+
221237
if(!wifi_softap_dhcps_stop()) {
222238
DEBUG_WIFI("[APConfig] wifi_softap_dhcps_stop failed!\n");
223239
}
@@ -228,14 +244,12 @@ bool ESP8266WiFiAPClass::softAPConfig(IPAddress local_ip, IPAddress gateway, IPA
228244
}
229245

230246
struct dhcps_lease dhcp_lease;
231-
IPAddress ip = local_ip;
232-
ip[3] += 99;
233-
dhcp_lease.start_ip.addr = ip.v4();
234-
DEBUG_WIFI("[APConfig] DHCP IP start: %s\n", ip.toString().c_str());
247+
dhcp_lease.enable = true;
248+
dhcp_lease.start_ip.addr = dhcp_start.v4();
249+
DEBUG_WIFI("[APConfig] DHCP IP start: %s\n", dhcp_start.toString().c_str());
235250

236-
ip[3] += 100;
237-
dhcp_lease.end_ip.addr = ip.v4();
238-
DEBUG_WIFI("[APConfig] DHCP IP end: %s\n", ip.toString().c_str());
251+
dhcp_lease.end_ip.addr = dhcp_end.v4();
252+
DEBUG_WIFI("[APConfig] DHCP IP end: %s\n", dhcp_end.toString().c_str());
239253

240254
if(!wifi_softap_set_dhcps_lease(&dhcp_lease)) {
241255
DEBUG_WIFI("[APConfig] wifi_set_ip_info failed!\n");
@@ -265,7 +279,7 @@ bool ESP8266WiFiAPClass::softAPConfig(IPAddress local_ip, IPAddress gateway, IPA
265279
DEBUG_WIFI("[APConfig] IP config Invalid?!\n");
266280
ret = false;
267281
} else if(local_ip.v4() != info.ip.addr) {
268-
ip = info.ip.addr;
282+
IPAddress ip = info.ip.addr;
269283
DEBUG_WIFI("[APConfig] IP config not set correct?! new IP: %s\n", ip.toString().c_str());
270284
ret = false;
271285
}
@@ -277,7 +291,24 @@ bool ESP8266WiFiAPClass::softAPConfig(IPAddress local_ip, IPAddress gateway, IPA
277291
return ret;
278292
}
279293

294+
/**
295+
* Configure access point
296+
* @param local_ip access point IP
297+
* @param gateway gateway IP
298+
* @param subnet subnet mask
299+
*/
300+
bool ESP8266WiFiAPClass::softAPConfig(IPAddress local_ip, IPAddress gateway, IPAddress subnet) {
301+
IPAddress dhcp_start;
302+
IPAddress dhcp_end;
303+
304+
dhcp_start = local_ip;
305+
dhcp_start[3] += 99;
306+
dhcp_end = dhcp_start;
307+
dhcp_end[3] += 100;
280308

309+
bool ret = softAPConfig(local_ip, gateway, subnet, dhcp_start, dhcp_end);
310+
return ret;
311+
}
281312

282313
/**
283314
* Disconnect from the network (close AP)

libraries/ESP8266WiFi/src/ESP8266WiFiAP.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ class ESP8266WiFiAPClass {
3838

3939
bool softAP(const char* ssid, const char* passphrase = NULL, int channel = 1, int ssid_hidden = 0, int max_connection = 4);
4040
bool softAP(const String& ssid,const String& passphrase = emptyString,int channel = 1,int ssid_hidden = 0,int max_connection = 4);
41+
bool softAPConfig(IPAddress local_ip, IPAddress gateway, IPAddress subnet, IPAddress dhcp_start, IPAddress dhcp_end);
4142
bool softAPConfig(IPAddress local_ip, IPAddress gateway, IPAddress subnet);
4243
bool softAPdisconnect(bool wifioff = false);
4344

0 commit comments

Comments
 (0)