Skip to content

Commit 50cbdc0

Browse files
d-a-vdevyte
authored andcommitted
update AddrList and examples (#5422)
1 parent 31bee50 commit 50cbdc0

File tree

6 files changed

+82
-50
lines changed

6 files changed

+82
-50
lines changed

cores/esp8266/AddrList.h

+64-40
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
for (auto a: addrList)
2525
out.printf("IF='%s' index=%d legacy=%d IPv4=%d local=%d hostname='%s' addr= %s\n",
2626
a.iface().c_str(),
27-
a.number(),
27+
a.ifnumber(),
2828
a.addr().isLegacy(),
2929
a.addr().isV4(),
3030
a.addr().isLocal(),
@@ -65,7 +65,7 @@
6565
for (auto iface: addrList)
6666
if ((configured = ( !iface.addr().isV4()
6767
&& !iface.addr().isLocal()
68-
&& iface.number() == STATION_IF)))
68+
&& iface.ifnumber() == STATION_IF)))
6969
break;
7070
Serial.print('.');
7171
delay(500);
@@ -94,28 +94,40 @@ namespace AddressListImplementation
9494

9595
struct netifWrapper
9696
{
97-
netifWrapper(netif * netif) : _netif(netif), _num(-1) {}
98-
netifWrapper(const netifWrapper & o) : _netif(o._netif), _num(o._num) {}
97+
netifWrapper (netif* netif) : _netif(netif), _num(-1) {}
98+
netifWrapper (const netifWrapper& o) : _netif(o._netif), _num(o._num) {}
9999

100-
netifWrapper& operator=(const netifWrapper & o) {_netif = o._netif; _num = o._num; return *this;}
100+
netifWrapper& operator= (const netifWrapper& o)
101+
{
102+
_netif = o._netif;
103+
_num = o._num;
104+
return *this;
105+
}
101106

102-
bool equal (const netifWrapper & o)
107+
bool equal(const netifWrapper& o)
103108
{
104109
return _netif == o._netif && (!_netif || _num == o._num);
105110
}
106111

107-
108-
bool isLegacy() const { return _num == 0; }
109-
bool isLocal() const { return addr().isLocal(); }
110-
IPAddress addr () const { return ipFromNetifNum(); }
111-
IPAddress netmask () const { return _netif->netmask; }
112-
IPAddress gw () const { return _netif->gw; }
113-
String iface () const { return String(_netif->name[0]) + _netif->name[1]; }
114-
const char* hostname () const { return _netif->hostname?: emptyString.c_str(); }
115-
const char* mac () const { return (const char*)_netif->hwaddr; }
116-
int number () const { return _netif->num; }
117-
118-
const ip_addr_t* ipFromNetifNum () const
112+
// address properties
113+
IPAddress addr () const { return ipFromNetifNum(); }
114+
bool isLegacy () const { return _num == 0; }
115+
bool isLocal () const { return addr().isLocal(); }
116+
bool isV4 () const { return addr().isV4(); }
117+
bool isV6 () const { return !addr().isV4(); }
118+
String toString() const { return addr().toString(); }
119+
120+
// related to legacy address (_num=0, ipv4)
121+
IPAddress netmask () const { return _netif->netmask; }
122+
IPAddress gw () const { return _netif->gw; }
123+
124+
// common to all addresses of this interface
125+
String ifname () const { return String(_netif->name[0]) + _netif->name[1]; }
126+
const char* ifhostname () const { return _netif->hostname?: emptyString.c_str(); }
127+
const char* ifmac () const { return (const char*)_netif->hwaddr; }
128+
int ifnumber () const { return _netif->num; }
129+
130+
const ip_addr_t* ipFromNetifNum () const
119131
{
120132
#if LWIP_IPV6
121133
return _num ? &_netif->ip6_addr[_num - 1] : &_netif->ip_addr;
@@ -124,44 +136,57 @@ struct netifWrapper
124136
#endif
125137
}
126138

139+
// lwIP interface
140+
netif* _netif;
127141

128-
netif * _netif;
142+
// address index within interface
143+
// 0: legacy address (IPv4)
144+
// n>0: (_num-1) is IPv6 index for netif->ip6_addr[]
129145
int _num;
130146
};
131147

132148

133-
134149
class AddressListIterator
135150
{
136151
public:
137-
AddressListIterator(const netifWrapper &o) : netIf(o) {}
138-
AddressListIterator(netif * netif) : netIf(netif) {}
152+
AddressListIterator (const netifWrapper& o) : netIf(o) {}
153+
AddressListIterator (netif* netif) : netIf(netif)
154+
{
155+
// This constructor is called with lwIP's global netif_list, or
156+
// nullptr. operator++() is designed to loop through _configured_
157+
// addresses. That's why netIf's _num is initialized to -1 to allow
158+
// returning the first usable address to AddressList::begin().
159+
(void)operator++();
160+
}
139161

140-
const netifWrapper& operator* () const {return netIf;}
141-
const netifWrapper* operator->() const {return &netIf;}
162+
const netifWrapper& operator* () const { return netIf; }
163+
const netifWrapper* operator-> () const { return &netIf; }
142164

143-
bool operator==(AddressListIterator & o) {return netIf.equal(*o);}
144-
bool operator!=(AddressListIterator & o) {return !netIf.equal(*o);}
165+
bool operator== (AddressListIterator& o) { return netIf.equal(*o); }
166+
bool operator!= (AddressListIterator& o) { return !netIf.equal(*o); }
145167

146-
AddressListIterator & operator= (const AddressListIterator& o) {netIf = o.netIf; return *this; }
168+
AddressListIterator& operator= (const AddressListIterator& o) { netIf = o.netIf; return *this; }
147169

148-
AddressListIterator operator++(int)
170+
AddressListIterator operator++ (int)
149171
{
150172
AddressListIterator ret = *this;
151-
++(*this);
173+
(void)operator++();
152174
return ret;
153175
}
154176

155-
AddressListIterator & operator++()
177+
AddressListIterator& operator++ ()
156178
{
157-
while (netIf._netif)
179+
while (netIf._netif)
158180
{
159-
if (++netIf._num == IF_NUM_ADDRESSES)
181+
if (++netIf._num == IF_NUM_ADDRESSES)
160182
{
161-
netIf = netifWrapper(netIf._netif->next); //num is inited to -1
183+
// all addresses from current interface were iterated,
184+
// switching to next interface
185+
netIf = netifWrapper(netIf._netif->next);
162186
continue;
163187
}
164188
if (!ip_addr_isany(netIf.ipFromNetifNum()))
189+
// found an initialized address
165190
break;
166191
}
167192
return *this;
@@ -171,24 +196,23 @@ class AddressListIterator
171196
};
172197

173198

174-
175199
class AddressList
176200
{
177201
public:
178202
using const_iterator = const AddressListIterator;
179203

180-
const_iterator begin() const {return const_iterator(netif_list);}
181-
const_iterator end() const {return const_iterator(nullptr);}
204+
const_iterator begin () const { return const_iterator(netif_list); }
205+
const_iterator end () const { return const_iterator(nullptr); }
182206

183207
};
184208

209+
inline AddressList::const_iterator begin (const AddressList& a) { return a.begin(); }
210+
inline AddressList::const_iterator end (const AddressList& a) { return a.end(); }
185211

186-
inline AddressList::const_iterator begin(const AddressList &a) {return a.begin();}
187-
inline AddressList::const_iterator end(const AddressList &a) {return a.end();}
188212

189-
} //AddressListImplementation
213+
} // AddressListImplementation
190214

191-
} //esp8266
215+
} // esp8266
192216

193217
extern esp8266::AddressListImplementation::AddressList addrList;
194218

libraries/esp8266/examples/IPv6/IPv6.ino renamed to libraries/ESP8266WiFi/examples/IPv6/IPv6.ino

+9-6
Original file line numberDiff line numberDiff line change
@@ -66,11 +66,11 @@ void status(Print& out) {
6666
out.println(F("(with 'telnet <addr> or 'nc -u <addr> 23')"));
6767
for (auto a : addrList) {
6868
out.printf("IF='%s' IPv6=%d local=%d hostname='%s' addr= %s",
69-
a.iface().c_str(),
70-
!a.addr().isV4(),
71-
a.addr().isLocal(),
72-
a.hostname(),
73-
a.addr().toString().c_str());
69+
a.ifname().c_str(),
70+
a.isV6(),
71+
a.isLocal(),
72+
a.ifhostname(),
73+
a.toString().c_str());
7474

7575
if (a.isLegacy()) {
7676
out.printf(" / mask:%s / gw:%s",
@@ -79,6 +79,7 @@ void status(Print& out) {
7979
}
8080

8181
out.println();
82+
8283
}
8384

8485
// lwIP's dns client will ask for IPv4 first (by default)
@@ -96,12 +97,14 @@ void setup() {
9697
Serial.println();
9798
Serial.println(ESP.getFullVersion());
9899

100+
Serial.printf("IPV6 is%s enabled\n", LWIP_IPV6 ? emptyString.c_str() : " NOT");
101+
99102
WiFi.mode(WIFI_STA);
100103
WiFi.begin(STASSID, STAPSK);
101104

102105
status(Serial);
103106

104-
#if 0
107+
#if 0 // 0: legacy connecting loop - 1: wait for IPv6
105108

106109
// legacy loop (still valid with IPv4 only)
107110

libraries/ESP8266httpUpdate/examples/httpUpdateSigned/httpUpdateSigned.ino

+6-1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,11 @@
2121
#include <ESP8266HTTPClient.h>
2222
#include <ESP8266httpUpdate.h>
2323

24+
#ifndef STASSID
25+
#define STASSID "your-ssid"
26+
#define STAPSK "your-password"
27+
#endif
28+
2429
ESP8266WiFiMulti WiFiMulti;
2530

2631
#define MANUAL_SIGNING 0
@@ -66,7 +71,7 @@ void setup() {
6671
}
6772

6873
WiFi.mode(WIFI_STA);
69-
WiFiMulti.addAP("SSID", "PASS");
74+
WiFiMulti.addAP(STASSID, STAPSK);
7075

7176
#if MANUAL_SIGNING
7277
signPubKey = new BearSSL::PublicKey(pubkey);

tests/common.sh

+2-2
Original file line numberDiff line numberDiff line change
@@ -116,8 +116,8 @@ function install_ide()
116116
debug_flags="-DDEBUG_ESP_PORT=Serial -DDEBUG_ESP_SSL -DDEBUG_ESP_TLS_MEM -DDEBUG_ESP_HTTP_CLIENT -DDEBUG_ESP_HTTP_SERVER -DDEBUG_ESP_CORE -DDEBUG_ESP_WIFI -DDEBUG_ESP_HTTP_UPDATE -DDEBUG_ESP_UPDATER -DDEBUG_ESP_OTA -DDEBUG_ESP_OOM"
117117
fi
118118
# Set custom warnings for all builds (i.e. could add -Wextra at some point)
119-
echo "compiler.c.extra_flags=-Wall -Wextra -Werror $debug_flags" > esp8266/platform.local.txt
120-
echo "compiler.cpp.extra_flags=-Wall -Wextra -Werror $debug_flags" >> esp8266/platform.local.txt
119+
echo "compiler.c.extra_flags=-Wall -Wextra -Werror -DLWIP_IPV6=0 $debug_flags" > esp8266/platform.local.txt
120+
echo "compiler.cpp.extra_flags=-Wall -Wextra -Werror -DLWIP_IPV6=0 $debug_flags" >> esp8266/platform.local.txt
121121
echo -e "\n----platform.local.txt----"
122122
cat esp8266/platform.local.txt
123123
echo -e "\n----\n"

tests/run_CI_locally.sh

+1-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ if [ -d ${TMPCI} ]; then
3333
echo ""
3434
echo " -- updating CI directory in ${TMPCI} --"
3535
echo ""
36-
(cd ${TMPCI}; git checkout ${branch}; git pull)
36+
(cd ${TMPCI}; git checkout master; git branch -D ${branch} || true; git checkout -b ${branch}; git pull origin ${branch})
3737
else
3838
echo ""
3939
echo " -- installing CI directory in ${TMPCI} --"

0 commit comments

Comments
 (0)