Skip to content

Commit 68ebda1

Browse files
committed
CNetIf - added config method to apply new static IP settings
in CWifi::_config netif_set_address call was missing. in Ethernet::begin static IP was not applied if invoked second time now the new CNetIf.config does the static IP change for both.
1 parent 05feca5 commit 68ebda1

File tree

4 files changed

+24
-9
lines changed

4 files changed

+24
-9
lines changed

Diff for: libraries/Ethernet/src/Ethernet.cpp

+8-4
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,13 @@ int CEthernet::begin(IPAddress local_ip, IPAddress subnet, IPAddress gateway) {
5353
int CEthernet::begin(IPAddress local_ip, IPAddress subnet, IPAddress gateway, IPAddress dns_server) {
5454
/* -------------------------------------------------------------------------- */
5555

56-
ni = CLwipIf::getInstance().get(NI_ETHERNET, local_ip, gateway, subnet);
57-
if(ni == nullptr) {
58-
return 0;
56+
if (ni != nullptr) {
57+
ni->config(local_ip, gateway, subnet);
58+
} else {
59+
ni = CLwipIf::getInstance().get(NI_ETHERNET, local_ip, gateway, subnet);
60+
if (ni == nullptr) {
61+
return 0;
62+
}
5963
}
6064

6165
/* If there is a local DHCP informs it of our manual IP configuration to prevent IP conflict */
@@ -108,7 +112,7 @@ int CEthernet::begin(uint8_t *mac_address, IPAddress local_ip, IPAddress dns_ser
108112
/* -------------------------------------------------------------------------- */
109113
int CEthernet::begin(uint8_t *mac, IPAddress local_ip, IPAddress dns_server, IPAddress gateway, IPAddress subnet, unsigned long timeout, unsigned long responseTimeout) {
110114
/* -------------------------------------------------------------------------- */
111-
CLwipIf::getInstance().setMacAddress(NI_ETHERNET, mac_address);
115+
CLwipIf::getInstance().setMacAddress(NI_ETHERNET, mac);
112116
return begin(local_ip, subnet, gateway, dns_server);
113117
}
114118

Diff for: libraries/WiFi/src/WiFi.cpp

+1-5
Original file line numberDiff line numberDiff line change
@@ -98,11 +98,7 @@ void CWifi::_config(IPAddress local_ip, IPAddress gateway, IPAddress subnet) {
9898
/* -------------------------------------------------------------------------- */
9999
_useStaticIp = local_ip != INADDR_NONE;
100100
if(ni != nullptr) {
101-
ni->DhcpStop();
102-
ni->DhcpNotUsed();
103-
IP_ADDR4(&ni->ip, local_ip[0], local_ip[1], local_ip[2], local_ip[3]);
104-
IP_ADDR4(&ni->gw, gateway[0], gateway[1], gateway[2], gateway[3]);
105-
IP_ADDR4(&ni->nm, subnet[0], subnet[1], subnet[2], subnet[3]);
101+
ni->config(local_ip, gateway, subnet);
106102
}
107103
else {
108104
CNetIf::default_ip = local_ip;

Diff for: libraries/lwIpWrapper/src/CNetIf.cpp

+13
Original file line numberDiff line numberDiff line change
@@ -1268,6 +1268,19 @@ void CNetIf::setLinkDown()
12681268
netif_set_down(&ni);
12691269
}
12701270

1271+
/* -------------------------------------------------------------------------- */
1272+
void CNetIf::config(IPAddress _ip, IPAddress _gw, IPAddress _nm)
1273+
{
1274+
DhcpStop();
1275+
DhcpNotUsed();
1276+
1277+
IP_ADDR4(&ip, _ip[0], _ip[1], _ip[2], _ip[3]);
1278+
IP_ADDR4(&nm, _nm[0], _nm[1], _nm[2], _nm[3]);
1279+
IP_ADDR4(&gw, _gw[0], _gw[1], _gw[2], _gw[3]);
1280+
1281+
netif_set_addr(&ni, &ip, &nm, &gw);
1282+
}
1283+
12711284
/* ########################################################################## */
12721285
/* ETHERNET NETWORK INTERFACE CLASS */
12731286
/* ########################################################################## */

Diff for: libraries/lwIpWrapper/src/CNetIf.h

+2
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,8 @@ class CNetIf {
189189
uint32_t getNmAdd() { return ip4_addr_get_u32(&(ni.netmask)); }
190190
uint32_t getGwAdd() { return ip4_addr_get_u32(&(ni.gw)); }
191191

192+
void config(IPAddress _ip, IPAddress _gw, IPAddress _nm);
193+
192194
void setHostname(const char* name)
193195
{
194196
memset(hostname, 0x00, MAX_HOSTNAME_DIM);

0 commit comments

Comments
 (0)