Skip to content

Commit 1f5b81d

Browse files
committed
More fixes
1 parent 9bd5205 commit 1f5b81d

File tree

8 files changed

+166
-73
lines changed

8 files changed

+166
-73
lines changed

libraries/Ethernet/examples/ETH_LAN8720/ETH_LAN8720.ino

+1-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ void onEvent(arduino_event_id_t event)
3333
break;
3434
case ARDUINO_EVENT_ETH_GOT_IP:
3535
Serial.println("ETH Got IP");
36-
ETH.printInfo(Serial);
36+
Serial.println(ETH);
3737
eth_connected = true;
3838
break;
3939
case ARDUINO_EVENT_ETH_LOST_IP:

libraries/Ethernet/examples/ETH_TLK110/ETH_TLK110.ino

+1-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ void onEvent(arduino_event_id_t event)
3131
break;
3232
case ARDUINO_EVENT_ETH_GOT_IP:
3333
Serial.println("ETH Got IP");
34-
ETH.printInfo(Serial);
34+
Serial.println(ETH);
3535
eth_connected = true;
3636
break;
3737
case ARDUINO_EVENT_ETH_LOST_IP:

libraries/Ethernet/examples/ETH_W5500_Arduino_SPI/ETH_W5500_Arduino_SPI.ino

+2-2
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,9 @@ void onEvent(arduino_event_id_t event, arduino_event_info_t info)
4949
break;
5050
case ARDUINO_EVENT_ETH_GOT_IP:
5151
Serial.printf("ETH Got IP: '%s'\n", esp_netif_get_desc(info.got_ip.esp_netif));
52-
ETH.printInfo(Serial);
52+
Serial.println(ETH);
5353
#if USE_TWO_ETH_PORTS
54-
ETH1.printInfo(Serial);
54+
Serial.println(ETH1);
5555
#endif
5656
eth_connected = true;
5757
break;

libraries/Ethernet/examples/ETH_W5500_IDF_SPI/ETH_W5500_IDF_SPI.ino

+2-2
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,9 @@ void onEvent(arduino_event_id_t event, arduino_event_info_t info)
4747
break;
4848
case ARDUINO_EVENT_ETH_GOT_IP:
4949
Serial.printf("ETH Got IP: '%s'\n", esp_netif_get_desc(info.got_ip.esp_netif));
50-
ETH.printInfo(Serial);
50+
Serial.println(ETH);
5151
#if USE_TWO_ETH_PORTS
52-
ETH1.printInfo(Serial);
52+
Serial.println(ETH1);
5353
#endif
5454
eth_connected = true;
5555
break;

libraries/Networking/src/ESP_Network_Interface.cpp

+142-27
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
#include "lwip/ip_addr.h"
66
#include "lwip/err.h"
77
#include "lwip/netif.h"
8+
#include "dhcpserver/dhcpserver.h"
9+
#include "dhcpserver/dhcpserver_options.h"
810
#include "esp32-hal-log.h"
911

1012
static ESP_Network_Interface * _interfaces[ESP_NETIF_ID_MAX] = { NULL, NULL, NULL, NULL, NULL, NULL};
@@ -137,6 +139,7 @@ ESP_Network_Interface::ESP_Network_Interface()
137139
, _got_ip_event_id(-1)
138140
, _lost_ip_event_id(-1)
139141
, _interface_id(ESP_NETIF_ID_MAX)
142+
, _is_server_if(false)
140143
{}
141144

142145
ESP_Network_Interface::~ESP_Network_Interface(){
@@ -238,10 +241,11 @@ void ESP_Network_Interface::destroyNetif() {
238241
}
239242
}
240243

241-
bool ESP_Network_Interface::initNetif(ESP_Network_Interface_ID interface_id) {
244+
bool ESP_Network_Interface::initNetif(ESP_Network_Interface_ID interface_id, bool server_interface) {
242245
if(_esp_netif == NULL || interface_id >= ESP_NETIF_ID_MAX){
243246
return false;
244247
}
248+
_is_server_if = server_interface;
245249
_interface_id = interface_id;
246250
_got_ip_event_id = esp_netif_get_event_id(_esp_netif, ESP_NETIF_IP_EVENT_GOT_IP);
247251
_lost_ip_event_id = esp_netif_get_event_id(_esp_netif, ESP_NETIF_IP_EVENT_LOST_IP);
@@ -293,6 +297,29 @@ bool ESP_Network_Interface::enableIPv6(bool en)
293297
return true;
294298
}
295299

300+
bool ESP_Network_Interface::dnsIP(uint8_t dns_no, IPAddress ip)
301+
{
302+
if(_esp_netif == NULL || dns_no > 2){
303+
return false;
304+
}
305+
if(_is_server_if && dns_no > 0){
306+
log_e("Server interfaces can have only one DNS server.");
307+
return false;
308+
}
309+
esp_netif_dns_info_t d;
310+
// ToDo: can this work with IPv6 addresses?
311+
d.ip.type = IPADDR_TYPE_V4;
312+
if(static_cast<uint32_t>(ip) != 0){
313+
d.ip.u_addr.ip4.addr = static_cast<uint32_t>(ip);
314+
} else {
315+
d.ip.u_addr.ip4.addr = 0;
316+
}
317+
if(esp_netif_set_dns_info(_esp_netif, (esp_netif_dns_type_t)dns_no, &d) != ESP_OK){
318+
return false;
319+
}
320+
return true;
321+
}
322+
296323
bool ESP_Network_Interface::config(IPAddress local_ip, IPAddress gateway, IPAddress subnet, IPAddress dns1, IPAddress dns2, IPAddress dns3)
297324
{
298325
if(_esp_netif == NULL){
@@ -323,36 +350,124 @@ bool ESP_Network_Interface::config(IPAddress local_ip, IPAddress gateway, IPAddr
323350
d3.ip.u_addr.ip4.addr = 0;
324351
}
325352

326-
// Stop DHCPC
327-
err = esp_netif_dhcpc_stop(_esp_netif);
328-
if(err != ESP_OK && err != ESP_ERR_ESP_NETIF_DHCP_ALREADY_STOPPED){
329-
log_e("DHCP could not be stopped! Error: %d", err);
330-
return false;
331-
}
332-
333-
clearStatusBits(ESP_NETIF_HAS_IP_BIT);
353+
if(_is_server_if){
354+
// Stop DHCPS
355+
err = esp_netif_dhcps_stop(_esp_netif);
356+
if(err && err != ESP_ERR_ESP_NETIF_DHCP_ALREADY_STOPPED){
357+
log_e("DHCPS Stop Failed! 0x%04x: %s", err, esp_err_to_name(err));
358+
return false;
359+
}
334360

335-
// Set IPv4, Netmask, Gateway
336-
err = esp_netif_set_ip_info(_esp_netif, &info);
337-
if(err != ERR_OK){
338-
log_e("ETH IP could not be configured! Error: %d", err);
339-
return false;
340-
}
341-
342-
// Set DNS Servers
343-
esp_netif_set_dns_info(_esp_netif, ESP_NETIF_DNS_MAIN, &d1);
344-
esp_netif_set_dns_info(_esp_netif, ESP_NETIF_DNS_BACKUP, &d2);
345-
esp_netif_set_dns_info(_esp_netif, ESP_NETIF_DNS_FALLBACK, &d3);
346-
347-
// Start DHCPC if static IP was set
348-
if(info.ip.addr == 0){
349-
err = esp_netif_dhcpc_start(_esp_netif);
350-
if(err != ESP_OK && err != ESP_ERR_ESP_NETIF_DHCP_ALREADY_STARTED){
351-
log_w("DHCP could not be started! Error: %d", err);
361+
// Set IPv4, Netmask, Gateway
362+
err = esp_netif_set_ip_info(_esp_netif, &info);
363+
if(err){
364+
log_e("Netif Set IP Failed! 0x%04x: %s", err, esp_err_to_name(err));
365+
return false;
366+
}
367+
368+
// Set DNS Server
369+
esp_netif_set_dns_info(_esp_netif, ESP_NETIF_DNS_MAIN, &d1);
370+
371+
dhcps_lease_t lease;
372+
lease.enable = true;
373+
uint8_t CIDR = calculateSubnetCIDR(subnet);
374+
log_v("SoftAP: %s | Gateway: %s | DHCP Start: %s | Netmask: %s", local_ip.toString().c_str(), gateway.toString().c_str(), dns2.toString().c_str(), subnet.toString().c_str());
375+
// netmask must have room for at least 12 IP addresses (AP + GW + 10 DHCP Leasing addresses)
376+
// netmask also must be limited to the last 8 bits of IPv4, otherwise this function won't work
377+
// IDF NETIF checks netmask for the 3rd byte: https://github.com/espressif/esp-idf/blob/master/components/esp_netif/lwip/esp_netif_lwip.c#L1857-L1862
378+
if (CIDR > 28 || CIDR < 24) {
379+
log_e("Bad netmask. It must be from /24 to /28 (255.255.255. 0<->240)");
380+
return false; // ESP_FAIL if initializing failed
381+
}
382+
#define _byte_swap32(num) (((num>>24)&0xff) | ((num<<8)&0xff0000) | ((num>>8)&0xff00) | ((num<<24)&0xff000000))
383+
// The code below is ready for any netmask, not limited to 255.255.255.0
384+
uint32_t netmask = _byte_swap32(info.netmask.addr);
385+
uint32_t ap_ipaddr = _byte_swap32(info.ip.addr);
386+
uint32_t dhcp_ipaddr = _byte_swap32(static_cast<uint32_t>(dns2));
387+
dhcp_ipaddr = dhcp_ipaddr == 0 ? ap_ipaddr + 1 : dhcp_ipaddr;
388+
uint32_t leaseStartMax = ~netmask - 10;
389+
// there will be 10 addresses for DHCP to lease
390+
lease.start_ip.addr = dhcp_ipaddr;
391+
lease.end_ip.addr = lease.start_ip.addr + 10;
392+
// Check if local_ip is in the same subnet as the dhcp leasing range initial address
393+
if ((ap_ipaddr & netmask) != (dhcp_ipaddr & netmask)) {
394+
log_e("The AP IP address (%s) and the DHCP start address (%s) must be in the same subnet",
395+
local_ip.toString().c_str(), IPAddress(_byte_swap32(dhcp_ipaddr)).toString().c_str());
396+
return false; // ESP_FAIL if initializing failed
397+
}
398+
// prevents DHCP lease range to overflow subnet range
399+
if ((dhcp_ipaddr & ~netmask) >= leaseStartMax) {
400+
// make first DHCP lease addr stay in the begining of the netmask range
401+
lease.start_ip.addr = (dhcp_ipaddr & netmask) + 1;
402+
lease.end_ip.addr = lease.start_ip.addr + 10;
403+
log_w("DHCP Lease out of range - Changing DHCP leasing start to %s", IPAddress(_byte_swap32(lease.start_ip.addr)).toString().c_str());
404+
}
405+
// Check if local_ip is within DHCP range
406+
if (ap_ipaddr >= lease.start_ip.addr && ap_ipaddr <= lease.end_ip.addr) {
407+
log_e("The AP IP address (%s) can't be within the DHCP range (%s -- %s)",
408+
local_ip.toString().c_str(), IPAddress(_byte_swap32(lease.start_ip.addr)).toString().c_str(), IPAddress(_byte_swap32(lease.end_ip.addr)).toString().c_str());
409+
return false; // ESP_FAIL if initializing failed
410+
}
411+
// Check if gateway is within DHCP range
412+
uint32_t gw_ipaddr = _byte_swap32(info.gw.addr);
413+
bool gw_in_same_subnet = (gw_ipaddr & netmask) == (ap_ipaddr & netmask);
414+
if (gw_in_same_subnet && gw_ipaddr >= lease.start_ip.addr && gw_ipaddr <= lease.end_ip.addr) {
415+
log_e("The GatewayP address (%s) can't be within the DHCP range (%s -- %s)",
416+
gateway.toString().c_str(), IPAddress(_byte_swap32(lease.start_ip.addr)).toString().c_str(), IPAddress(_byte_swap32(lease.end_ip.addr)).toString().c_str());
417+
return false; // ESP_FAIL if initializing failed
418+
}
419+
// all done, just revert back byte order of DHCP lease range
420+
lease.start_ip.addr = _byte_swap32(lease.start_ip.addr);
421+
lease.end_ip.addr = _byte_swap32(lease.end_ip.addr);
422+
log_v("DHCP Server Range: %s to %s", IPAddress(lease.start_ip.addr).toString().c_str(), IPAddress(lease.end_ip.addr).toString().c_str());
423+
err = esp_netif_dhcps_option(
424+
_esp_netif,
425+
ESP_NETIF_OP_SET,
426+
ESP_NETIF_REQUESTED_IP_ADDRESS,
427+
(void*)&lease, sizeof(dhcps_lease_t)
428+
);
429+
if(err){
430+
log_e("DHCPS Set Lease Failed! 0x%04x: %s", err, esp_err_to_name(err));
431+
return false;
432+
}
433+
// Start DHCPS
434+
err = esp_netif_dhcps_start(_esp_netif);
435+
if(err){
436+
log_e("DHCPS Start Failed! 0x%04x: %s", err, esp_err_to_name(err));
352437
return false;
353438
}
354439
} else {
355-
setStatusBits(ESP_NETIF_HAS_IP_BIT);
440+
// Stop DHCPC
441+
err = esp_netif_dhcpc_stop(_esp_netif);
442+
if(err != ESP_OK && err != ESP_ERR_ESP_NETIF_DHCP_ALREADY_STOPPED){
443+
log_e("DHCP could not be stopped! Error: 0x%04x: %s", err, esp_err_to_name(err));
444+
return false;
445+
}
446+
447+
clearStatusBits(ESP_NETIF_HAS_IP_BIT);
448+
449+
// Set IPv4, Netmask, Gateway
450+
err = esp_netif_set_ip_info(_esp_netif, &info);
451+
if(err != ERR_OK){
452+
log_e("ETH IP could not be configured! Error: 0x%04x: %s", err, esp_err_to_name(err));
453+
return false;
454+
}
455+
456+
// Set DNS Servers
457+
esp_netif_set_dns_info(_esp_netif, ESP_NETIF_DNS_MAIN, &d1);
458+
esp_netif_set_dns_info(_esp_netif, ESP_NETIF_DNS_BACKUP, &d2);
459+
esp_netif_set_dns_info(_esp_netif, ESP_NETIF_DNS_FALLBACK, &d3);
460+
461+
// Start DHCPC if static IP was set
462+
if(info.ip.addr == 0){
463+
err = esp_netif_dhcpc_start(_esp_netif);
464+
if(err != ESP_OK && err != ESP_ERR_ESP_NETIF_DHCP_ALREADY_STARTED){
465+
log_w("DHCP could not be started! Error: 0x%04x: %s", err, esp_err_to_name(err));
466+
return false;
467+
}
468+
} else {
469+
setStatusBits(ESP_NETIF_HAS_IP_BIT);
470+
}
356471
}
357472

358473
return true;

libraries/Networking/src/ESP_Network_Interface.h

+4-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,9 @@ class ESP_Network_Interface: public Printable {
3030
ESP_Network_Interface();
3131
virtual ~ESP_Network_Interface();
3232

33+
// For server interfaces (WiFi AP), dns1 is the DNS and dns2 is the lease range start
3334
bool config(IPAddress local_ip = (uint32_t)0x00000000, IPAddress gateway = (uint32_t)0x00000000, IPAddress subnet = (uint32_t)0x00000000, IPAddress dns1 = (uint32_t)0x00000000, IPAddress dns2 = (uint32_t)0x00000000, IPAddress dns3 = (uint32_t)0x00000000);
35+
bool dnsIP(uint8_t dns_no, IPAddress ip);
3436

3537
const char * getHostname() const;
3638
bool setHostname(const char * hostname) const;
@@ -72,8 +74,9 @@ class ESP_Network_Interface: public Printable {
7274
int32_t _got_ip_event_id;
7375
int32_t _lost_ip_event_id;
7476
ESP_Network_Interface_ID _interface_id;
77+
bool _is_server_if;
7578

76-
bool initNetif(ESP_Network_Interface_ID interface_id);
79+
bool initNetif(ESP_Network_Interface_ID interface_id, bool server_interface=false);
7780
void destroyNetif();
7881
int setStatusBits(int bits);
7982
int clearStatusBits(int bits);

libraries/WiFi/src/WiFiGeneric.cpp

+1-25
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,6 @@ extern "C" {
5353
#include <vector>
5454
#include "sdkconfig.h"
5555

56-
#define _byte_swap32(num) (((num>>24)&0xff) | ((num<<8)&0xff0000) | ((num>>8)&0xff00) | ((num<<24)&0xff000000))
5756
ESP_EVENT_DEFINE_BASE(ARDUINO_EVENTS);
5857
/*
5958
* Private (exposable) methods
@@ -175,6 +174,7 @@ esp_err_t set_esp_interface_ip(esp_interface_t interface, IPAddress local_ip=IPA
175174
log_e("Bad netmask. It must be from /24 to /28 (255.255.255. 0<->240)");
176175
return ESP_FAIL; // ESP_FAIL if initializing failed
177176
}
177+
#define _byte_swap32(num) (((num>>24)&0xff) | ((num<<8)&0xff0000) | ((num>>8)&0xff00) | ((num<<24)&0xff000000))
178178
// The code below is ready for any netmask, not limited to 255.255.255.0
179179
uint32_t netmask = _byte_swap32(info.netmask.addr);
180180
uint32_t ap_ipaddr = _byte_swap32(info.ip.addr);
@@ -245,30 +245,6 @@ esp_err_t set_esp_interface_ip(esp_interface_t interface, IPAddress local_ip=IPA
245245
return err;
246246
}
247247

248-
esp_err_t set_esp_interface_dns(esp_interface_t interface, IPAddress main_dns=IPAddress(), IPAddress backup_dns=IPAddress(), IPAddress fallback_dns=IPAddress()){
249-
esp_netif_t *esp_netif = esp_netifs[interface];
250-
esp_netif_dns_info_t dns;
251-
dns.ip.type = ESP_IPADDR_TYPE_V4;
252-
dns.ip.u_addr.ip4.addr = static_cast<uint32_t>(main_dns);
253-
if(dns.ip.u_addr.ip4.addr && esp_netif_set_dns_info(esp_netif, ESP_NETIF_DNS_MAIN, &dns) != ESP_OK){
254-
log_e("Set Main DNS Failed!");
255-
return ESP_FAIL;
256-
}
257-
if(interface != ESP_IF_WIFI_AP){
258-
dns.ip.u_addr.ip4.addr = static_cast<uint32_t>(backup_dns);
259-
if(dns.ip.u_addr.ip4.addr && esp_netif_set_dns_info(esp_netif, ESP_NETIF_DNS_BACKUP, &dns) != ESP_OK){
260-
log_e("Set Backup DNS Failed!");
261-
return ESP_FAIL;
262-
}
263-
dns.ip.u_addr.ip4.addr = static_cast<uint32_t>(fallback_dns);
264-
if(dns.ip.u_addr.ip4.addr && esp_netif_set_dns_info(esp_netif, ESP_NETIF_DNS_FALLBACK, &dns) != ESP_OK){
265-
log_e("Set Fallback DNS Failed!");
266-
return ESP_FAIL;
267-
}
268-
}
269-
return ESP_OK;
270-
}
271-
272248
static void _arduino_event_cb(void* arg, esp_event_base_t event_base, int32_t event_id, void* event_data) {
273249
arduino_event_t arduino_event;
274250
arduino_event.event_id = ARDUINO_EVENT_MAX;

libraries/WiFi/src/WiFiSTA.cpp

+13-14
Original file line numberDiff line numberDiff line change
@@ -54,20 +54,6 @@
5454
// ---------------------------------------------------- STA function -----------------------------------------------------
5555
// -----------------------------------------------------------------------------------------------------------------------
5656

57-
esp_err_t set_esp_interface_dns(esp_interface_t interface, IPAddress main_dns=IPAddress(), IPAddress backup_dns=IPAddress(), IPAddress fallback_dns=IPAddress());
58-
/**
59-
* Change DNS server for static IP configuration
60-
* @param dns1 Static DNS server 1
61-
* @param dns2 Static DNS server 2 (optional)
62-
*/
63-
bool WiFiSTAClass::setDNS(IPAddress dns1, IPAddress dns2)
64-
{
65-
if(WiFiGenericClass::getMode() == WIFI_MODE_NULL)
66-
return false;
67-
esp_err_t err = set_esp_interface_dns(ESP_IF_WIFI_STA, dns1, dns2);
68-
return err == ESP_OK;
69-
}
70-
7157
/**
7258
* Return Connection status.
7359
* @return one of the value defined in wl_status_t
@@ -186,6 +172,19 @@ bool WiFiSTAClass::config(IPAddress local_ip, IPAddress gateway, IPAddress subne
186172
return STA.config(local_ip, gateway, subnet, dns1, dns2);
187173
}
188174

175+
/**
176+
* Change DNS server for static IP configuration
177+
* @param dns1 Static DNS server 1
178+
* @param dns2 Static DNS server 2 (optional)
179+
*/
180+
bool WiFiSTAClass::setDNS(IPAddress dns1, IPAddress dns2)
181+
{
182+
if(!STA.started()){
183+
return false;
184+
}
185+
return STA.dnsIP(0, dns1) && STA.dnsIP(1, dns2);
186+
}
187+
189188
/**
190189
* Sets the working bandwidth of the STA mode
191190
* @param m wifi_bandwidth_t

0 commit comments

Comments
 (0)