8000 WiFi and Ethernet - config static IP auto gw,mask,dns as in Arduino l… · erpebe/arduino-pico@abd3547 · GitHub
[go: up one dir, main page]

Skip to content

Commit abd3547

Browse files
authored
WiFi and Ethernet - config static IP auto gw,mask,dns as in Arduino libs (earlephilhower#1862)
1 parent 9181ec0 commit abd3547

File tree

2 files changed

+26
-6
lines changed

2 files changed

+26
-6
lines changed

libraries/WiFi/src/WiFiClass.cpp

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ bool WiFiClass::connected() {
193193
param local_ip: Static ip configuration
194194
*/
195195
void WiFiClass::config(IPAddress local_ip) {
196-
ip4_addr_set_u32(ip_2_ip4(&_wifi.getNetIf()->ip_addr), local_ip.v4());
196+
_wifi.config(local_ip);
197197
}
198198

199199
/* Change Ip configuration settings disabling the dhcp client
@@ -202,8 +202,7 @@ void WiFiClass::config(IPAddress local_ip) {
202202
param dns_server: IP configuration for DNS server 1
203203
*/
204204
void WiFiClass::config(IPAddress local_ip, IPAddress dns_server) {
205-
ip4_addr_set_u32(ip_2_ip4(&_wifi.getNetIf()->ip_addr), local_ip.v4());
206-
dns_setserver(0, dns_server);
205+
_wifi.config(local_ip, dns_server);
207206
}
208207

209208
/* Change Ip configuration settings disabling the dhcp client
@@ -213,9 +212,7 @@ void WiFiClass::config(IPAddress local_ip, IPAddress dns_server) {
213212
param gateway : Static gateway configuration
214213
*/
215214
void WiFiClass::config(IPAddress local_ip, IPAddress dns_server, IPAddress gateway) {
216-
ip4_addr_set_u32(ip_2_ip4(&_wifi.getNetIf()->ip_addr), local_ip.v4());
217-
dns_setserver(0, dns_server);
218-
ip4_addr_set_u32(ip_2_ip4(&_wifi.getNetIf()->gw), gateway.v4());
215+
_wifi.config(local_ip, dns_server, gateway);
219216
}
220217

221218
/* Change Ip configuration settings disabling the dhcp client

libraries/lwIP_Ethernet/src/LwipIntfDev.h

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,14 @@ class LwipIntfDev: public LwipIntf, public RawDev {
6161
memset(&_netif, 0, sizeof(_netif));
6262
}
6363

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

69+
// two and one parameter version. 2nd parameter is DNS like in Arduino. IPv4 only
70+
boolean config(IPAddress local_ip, IPAddress dns = IPADDR_NONE);
71+
6772
// default mac-address is inferred from esp8266's STA interface
6873
bool begin(const uint8_t* macAddress = nullptr, const uint16_t mtu = DEFAULT_MTU);
6974

@@ -264,6 +269,24 @@ bool LwipIntfDev<RawDev>::config(const IPAddress& localIP, const IPAddress& gate
264269
}
265270
return true;
266271
}
272+
273+
template<class RawDev>
274+
boolean LwipIntfDev<RawDev>::config(IPAddress local_ip, IPAddress dns) {
275+
276+
if (!local_ip.isSet()) {
277+
return config(INADDR_ANY, INADDR_ANY, INADDR_ANY);
278+
}
279+
if (!local_ip.isV4()) {
280+
return false;
281+
}
282+
IPAddress gw(local_ip);
283+
gw[3] = 1;
284+
if (!dns.isSet()) {
285+
dns = gw;
286+
}
287+
return config(local_ip, gw, IPAddress(255, 255, 255, 0), dns);
288+
}
289+
267290
extern char wifi_station_hostname[];
268291
template<class RawDev>
269292
bool LwipIntfDev<RawDev>::begin(const uint8_t* macAddress, const uint16_t mtu) {

0 commit comments

Comments
 (0)
0