Skip to content

Commit

Permalink
Merge branch 'master' into add-redirect2
Browse files Browse the repository at this point in the history
  • Loading branch information
hmueller01 authored Dec 17, 2023
2 parents 14b8c43 + d5eb265 commit 10294df
Show file tree
Hide file tree
Showing 9 changed files with 88 additions and 11 deletions.
31 changes: 27 additions & 4 deletions cores/esp8266/LwipIntfDev.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,17 @@ class LwipIntfDev: public LwipIntf, public RawDev
memset(&_netif, 0, sizeof(_netif));
}

//The argument order for ESP is not the same as for Arduino. However, there is compatibility code under the hood
//to detect Arduino arg order, and handle it correctly.
boolean config(const IPAddress& local_ip, const IPAddress& arg1, const IPAddress& arg2,
const IPAddress& arg3 = IPADDR_NONE, const IPAddress& dns2 = IPADDR_NONE);

// two and one parameter version. 2nd parameter is DNS like in Arduino. IPv4 only
[[deprecated("It is discouraged to use this 1 or 2 parameters network configuration legacy "
"function config(ip[,dns]) as chosen defaults may not match the local network "
"configuration")]] boolean
config(IPAddress local_ip, IPAddress dns = INADDR_ANY);

// default mac-address is inferred from esp8266's STA interface
boolean begin(const uint8_t* macAddress = nullptr, const uint16_t mtu = DEFAULT_MTU);
void end();
Expand Down Expand Up @@ -96,7 +104,7 @@ class LwipIntfDev: public LwipIntf, public RawDev
{
return IPAddress(ip4_addr_get_u32(ip_2_ip4(&_netif.gw)));
}
IPAddress dnsIP(int n) const // WiFi lib way
IPAddress dnsIP(int n = 0) const // WiFi lib way
{
return IPAddress(dns_getserver(n));
}
Expand Down Expand Up @@ -209,6 +217,24 @@ boolean LwipIntfDev<RawDev>::config(const IPAddress& localIP, const IPAddress& g
return true;
}

template<class RawDev>
boolean LwipIntfDev<RawDev>::config(IPAddress local_ip, IPAddress dns)
{
if (!local_ip.isSet())
return config(INADDR_ANY, INADDR_ANY, INADDR_ANY);

if (!local_ip.isV4())
return false;

IPAddress gw(local_ip);
gw[3] = 1;
if (!dns.isSet())
{
dns = gw;
}
return config(local_ip, gw, IPAddress(255, 255, 255, 0), dns);
}

template<class RawDev>
boolean LwipIntfDev<RawDev>::begin(const uint8_t* macAddress, const uint16_t mtu)
{
Expand Down Expand Up @@ -336,9 +362,6 @@ template<class RawDev>
void LwipIntfDev<RawDev>::end()
{
netif_remove(&_netif);
ip_addr_copy(_netif.ip_addr, ip_addr_any); // to allow DHCP at next begin
ip_addr_copy(_netif.netmask, ip_addr_any);
ip_addr_copy(_netif.gw, ip_addr_any);
_started = false;
RawDev::end();
}
Expand Down
4 changes: 3 additions & 1 deletion cores/esp8266/core_esp8266_postmortem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,9 @@ static void ets_printf_P(const char *str, ...) {
}

static void cut_here() {
ets_putc('\n');
// https://tinyurl.com/8266dcdr => https://arduino-esp8266.readthedocs.io/en/latest/faq/a02-my-esp-crashes.html#exception
ets_printf_P(PSTR("\nTo make this dump useful, DECODE IT - https://tinyurl.com/8266dcdr\n"));

for (auto i = 0; i < 15; i++ ) {
ets_putc('-');
}
Expand Down
24 changes: 22 additions & 2 deletions doc/esp8266wifi/station-class.rst
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ The number of features provided by ESP8266 in the station mode is far more exten

Description of station class has been broken down into four parts. First discusses methods to establish connection to an access point. Second provides methods to manage connection like e.g. ``reconnect`` or ``isConnected``. Third covers properties to obtain information about connection like MAC or IP address. Finally the fourth section provides alternate methods to connect like e.g. Wi-Fi Protected Setup (WPS).

An effort to unify such network device class accross several Arduino core implementations has been made. Recommandations are located at `Arduino-Networking-API <https://github.com/JAndrassy/Arduino-Networking-API>`__ and tested with `NetApiHelpers <https://github.com/JAndrassy/NetApiHelpers>`__. Esp8266 Arduino core's station class is also following these guidelines.

Table of Contents
-----------------

Expand Down Expand Up @@ -97,8 +99,12 @@ config

Disable `DHCP <https://en.wikipedia.org/wiki/Dynamic_Host_Configuration_Protocol>`__ client (Dynamic Host Configuration Protocol) and set the IP configuration of station interface to user defined arbitrary values. The interface will be a static IP configuration instead of values provided by DHCP.

Note that to reenable DHCP, all three parameters (local_ip, gateway and subnet) as IPv4 ``0U`` (= 0.0.0.0) must be passed back to config() and re-connecting is needed.

.. code:: cpp
WiFi.config(local_ip, gateway, subnet) (for Arduino API portability, discouraged as chosen defaults may not match the local network configuration)
WiFi.config(local_ip, gateway, subnet, dns1)
WiFi.config(local_ip, gateway, subnet, dns1, dns2)
Function will return ``true`` if configuration change is applied successfully. If configuration can not be applied, because e.g. module is not in station or station + soft access point mode, then ``false`` will be returned.
Expand All @@ -116,6 +122,21 @@ The following IP configuration may be provided:
(like e.g. *www.google.co.uk*) and translate them for us to IP
addresses

For Arduino networking API compatibility, the ESP8266WiFi library supports IPv4-only additional versions of the ``config`` function:

.. code:: cpp
WiFi.config(local_ip) (for Arduino API portability, discouraged as chosen defaults may not match the local network configuration)
WiFi.config(local_ip, dns) (for Arduino API portability, discouraged as chosen defaults may not match the local network configuration)
WiFi.config(local_ip, dns, gateway) (for Arduino API portability, discouraged as chosen defaults may not match the local network configuration)
WiFi.config(local_ip, dns, gateway, subnet)
Versions where some of ``dns``, ``gateway`` and ``subnet`` parameters are not specified use a default value. Default ``subnet`` is 255.255.255.0. Default ``gateway`` and ``dns`` are derived from ``local_ip`` by changing the last number to 1. It is discouraged to use these default values as they may not apply to every network configuration.

Reminder : To reenable DHCP you can use ``WiFi.config(0U, 0U, 0U);``.

**Warning: The default values for dns, gateway and subnet may not match your router's settings.** Also please note, that ``config(local_ip, gateway)`` is not supported and ``WiFi.config(local_ip, gateway, subnet)`` doesn't set the DNS server IP.

*Example code:*

.. code:: cpp
Expand Down Expand Up @@ -157,8 +178,7 @@ The following IP configuration may be provided:
.
Connected, IP address: 192.168.1.22

Please note that station with static IP configuration usually connects to the network faster. In the above example it took about 500ms (one dot `.` displayed). This is because obtaining of IP configuration by DHCP client takes time and in this case this step is skipped. If you pass all three parameter as 0.0.0.0 (local_ip, gateway and subnet), it will re enable DHCP. You need to re-connect the device to get new IPs.

Please note that station with static IP configuration usually connects to the network faster. In the above example it took about 500ms (one dot `.` displayed). This is because obtaining of IP configuration by DHCP client takes time and in this case this step is skipped. Reminder: If you pass all three parameters as 0.0.0.0 (local_ip, gateway and subnet), it will re enable DHCP. You need to re-connect the device to get new IPs.

Manage Connection
~~~~~~~~~~~~~~~~~
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include <DNSServer.h>
#include <ESP8266mDNS.h>
#include <EEPROM.h>
#include <uri/UriGlob.h>

/*
This example serves a "hello world" on a WLAN and a SoftAP at the same time.
Expand Down Expand Up @@ -74,8 +75,8 @@ void setup() {
server.on("/", handleRoot);
server.on("/wifi", handleWifi);
server.on("/wifisave", handleWifiSave);
server.on("/generate_204", handleRoot); // Android captive portal. Maybe not needed. Might be handled by notFound handler.
server.on("/fwlink", handleRoot); // Microsoft captive portal. Maybe not needed. Might be handled by notFound handler.
server.on(UriGlob("/generate_204*"), handleRoot); // Android captive portal. Handle "/generate_204_<uuid>"-like requests. Might be handled by notFound handler.
server.on("/fwlink", handleRoot); // Microsoft captive portal. Maybe not needed. Might be handled by notFound handler.
server.onNotFound(handleNotFound);
server.begin(); // Web server start
Serial.println("HTTP server started");
Expand Down
16 changes: 16 additions & 0 deletions libraries/ESP8266WiFi/src/ESP8266WiFiSTA.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -339,6 +339,22 @@ bool ESP8266WiFiSTAClass::config(IPAddress local_ip, IPAddress arg1, IPAddress a
return true;
}

bool ESP8266WiFiSTAClass::config(IPAddress local_ip, IPAddress dns) {

if (!local_ip.isSet())
return config(INADDR_ANY, INADDR_ANY, INADDR_ANY);

if (!local_ip.isV4())
return false;

IPAddress gw(local_ip);
gw[3] = 1;
if (!dns.isSet()) {
dns = gw;
}
return config(local_ip, dns, gw);
}

/**
* Change DNS for static IP configuration
* @param dns1 Static DNS server 1
Expand Down
6 changes: 6 additions & 0 deletions libraries/ESP8266WiFi/src/ESP8266WiFiSTA.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,12 @@ class ESP8266WiFiSTAClass: public LwipIntf {
//to detect Arduino arg order, and handle it correctly. Be aware that the Arduino default value handling doesn't
//work here (see Arduino docs for gway/subnet defaults). In other words: at least 3 args must always be given.
bool config(IPAddress local_ip, IPAddress gateway, IPAddress subnet, IPAddress dns1 = INADDR_ANY, IPAddress dns2 = INADDR_ANY);

// two and one parameter version. 2nd parameter is DNS like in Arduino
// IPv4 only
[[deprecated("It is discouraged to use this 1 or 2 parameters network configuration legacy function config(ip[,dns]) as chosen defaults may not match the local network configuration")]]
bool config(IPAddress local_ip, IPAddress dns = INADDR_ANY);

bool setDNS(IPAddress dns1, IPAddress dns2 = INADDR_ANY);

bool reconnect();
Expand Down
2 changes: 2 additions & 0 deletions libraries/ESP8266WiFi/src/WiFi.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@

#include "ESP8266WiFi.h"
7 changes: 7 additions & 0 deletions libraries/lwIP_Ethernet/src/EthernetCompat.h
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,13 @@ class ArduinoEthernet: public LwipIntfDev<RawDev>
return ret;
}

void end()
{
ip_addr_copy(LwipIntfDev<RawDev>::_netif.ip_addr,
ip_addr_any); // to allow DHCP at next begin
LwipIntfDev<RawDev>::end();
}

HardwareStatus hardwareStatus() const
{
return _hardwareStatus;
Expand Down
4 changes: 2 additions & 2 deletions tools/elf2bin.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ def get_elf_entry(elf, path):
lines = io.StringIO(result.stdout)
for line in lines.readlines():
if 'Entry point address' in line:
words = re.split('\s+', line)
words = re.split(r'\s+', line)
entry_point = words[-2]
return int(entry_point, 16)

Expand All @@ -70,7 +70,7 @@ def get_segment_size_addr(elf, segment, path):
lines = io.StringIO(result.stdout)
for line in lines.readlines():
if segment in line:
words = re.split('\s+', line)
words = re.split(r'\s+', line)
size = int(words[3], 16)
addr = int(words[4], 16)
return [ size, addr ]
Expand Down

0 comments on commit 10294df

Please sign in to comment.