@@ -179,8 +179,10 @@ bool ESP8266WiFiAPClass::softAP(const char* ssid, const char* passphrase, int ch
179
179
* @param local_ip access point IP
180
180
* @param gateway gateway IP
181
181
* @param subnet subnet mask
182
+ * @param dhcp_start first IP assigned by DHCP
183
+ * @param dhcp_end last IP assigned by DHCP
182
184
*/
183
- bool ESP8266WiFiAPClass::softAPConfig (IPAddress local_ip, IPAddress gateway, IPAddress subnet) {
185
+ bool ESP8266WiFiAPClass::softAPConfig (IPAddress local_ip, IPAddress gateway, IPAddress subnet, IPAddress dhcp_start, IPAddress dhcp_end ) {
184
186
DEBUG_WIFI (" [APConfig] local_ip: %s gateway: %s subnet: %s\n " , local_ip.toString ().c_str (), gateway.toString ().c_str (), subnet.toString ().c_str ());
185
187
if (!WiFi.enableAP (true )) {
186
188
// enable AP failed
@@ -204,35 +206,52 @@ bool ESP8266WiFiAPClass::softAPConfig(IPAddress local_ip, IPAddress gateway, IPA
204
206
}
205
207
206
208
struct dhcps_lease dhcp_lease;
207
- IPAddress ip = local_ip;
208
- ip[3 ] += 99 ;
209
- dhcp_lease.start_ip .addr = static_cast <uint32_t >(ip);
210
- DEBUG_WIFI (" [APConfig] DHCP IP start: %s\n " , ip.toString ().c_str ());
211
209
212
- ip[3 ] += 100 ;
213
- dhcp_lease.end_ip .addr = static_cast <uint32_t >(ip);
214
- DEBUG_WIFI (" [APConfig] DHCP IP end: %s\n " , ip.toString ().c_str ());
210
+ uint32_t net_addr = info.ip .addr & info.netmask .addr ;
211
+ uint32_t bcast_addr = net_addr | !info.netmask .addr ;
215
212
216
- if (!wifi_softap_set_dhcps_lease (&dhcp_lease)) {
217
- DEBUG_WIFI (" [APConfig] wifi_set_ip_info failed!\n " );
218
- ret = false ;
219
- }
213
+ // Assign user-supplied range, checking its validity
214
+ IPAddress ip = (static_cast <uint32_t >(dhcp_start) & !info.netmask .addr ) | net_addr;
220
215
221
- // set lease time to 720min --> 12h
222
- if (!wifi_softap_set_dhcps_lease_time (720 )) {
223
- DEBUG_WIFI (" [APConfig] wifi_softap_set_dhcps_lease_time failed!\n " );
224
- ret = false ;
216
+ dhcp_lease.start_ip .addr = ip;
217
+ if (ip != net_addr && ip != bcast_addr && ip != info.ip .addr && ip != info.gw .addr ) {
218
+ DEBUG_WIFI (" [APConfig] DHCP IP start: %s\n " , ip.toString ().c_str ());
219
+ } else {
220
+ dhcp_lease.start_ip .addr =0 ;
225
221
}
226
222
227
- uint8 mode = 1 ;
228
- if (!wifi_softap_set_dhcps_offer_option (OFFER_ROUTER, &mode)) {
229
- DEBUG_WIFI (" [APConfig] wifi_softap_set_dhcps_offer_option failed!\n " );
230
- ret = false ;
223
+ ip = (static_cast <uint32_t >(dhcp_end) & !info.netmask .addr ) | net_addr;
224
+ dhcp_lease.end_ip .addr = static_cast <uint32_t >(ip);
225
+ if (ip != net_addr && ip != bcast_addr && ip != info.ip .addr && ip != info.gw .addr ) {
226
+ DEBUG_WIFI (" [APConfig] DHCP IP end: %s\n " , ip.toString ().c_str ());
227
+ } else {
228
+ dhcp_lease.end_ip .addr =0 ;
231
229
}
232
230
233
- if (!wifi_softap_dhcps_start ()) {
234
- DEBUG_WIFI (" [APConfig] wifi_softap_dhcps_start failed!\n " );
235
- ret = false ;
231
+ if (dhcp_lease.start_ip .addr && dhcp_lease.end_ip .addr ) {
232
+ if (!wifi_softap_set_dhcps_lease (&dhcp_lease)) {
233
+ DEBUG_WIFI (" [APConfig] wifi_set_ip_info failed!\n " );
234
+ ret = false ;
235
+ }
236
+
237
+ // set lease time to 720min --> 12h
238
+ if (!wifi_softap_set_dhcps_lease_time (720 )) {
239
+ DEBUG_WIFI (" [APConfig] wifi_softap_set_dhcps_lease_time failed!\n " );
240
+ ret = false ;
241
+ }
242
+
243
+ uint8 mode = 1 ;
244
+ if (!wifi_softap_set_dhcps_offer_option (OFFER_ROUTER, &mode)) {
245
+ DEBUG_WIFI (" [APConfig] wifi_softap_set_dhcps_offer_option failed!\n " );
246
+ ret = false ;
247
+ }
248
+
249
+ if (!wifi_softap_dhcps_start ()) {
250
+ DEBUG_WIFI (" [APConfig] wifi_softap_dhcps_start failed!\n " );
251
+ ret = false ;
252
+ }
253
+ } else {
254
+ DEBUG_WIFI (" [APConfig] DHCP daemon not started (range error or user request)\n " );
236
255
}
237
256
238
257
// check config
@@ -254,6 +273,25 @@ bool ESP8266WiFiAPClass::softAPConfig(IPAddress local_ip, IPAddress gateway, IPA
254
273
}
255
274
256
275
276
+ /* *
277
+ * Configure access point
278
+ * @param local_ip access point IP
279
+ * @param gateway gateway IP
280
+ * @param subnet subnet mask
281
+ */
282
+ bool ESP8266WiFiAPClass::softAPConfig (IPAddress local_ip, IPAddress gateway, IPAddress subnet) {
283
+ IPAddress dhcp_start;
284
+ IPAddress dhcp_end;
285
+
286
+ // calculate dhcp_start and DHCP_end as done in the old code
287
+ dhcp_start = local_ip;
288
+ dhcp_start[3 ] += 99 ;
289
+ dhcp_end = dhcp_start;
290
+ dhcp_end[3 ] += 100 ;
291
+
292
+ softAPConfig (local_ip, gateway, subnet, dhcp_start, dhcp_end);
293
+ }
294
+
257
295
258
296
/* *
259
297
* Disconnect from the network (close AP)
0 commit comments