Skip to content

Commit 773d248

Browse files
authored
Merge pull request #209 from JAndrassy/cnetif_config
CNetIf - added config method to apply new static IP settings for WiFi and Ethernet
2 parents 9bf6e99 + 86b8b09 commit 773d248

File tree

4 files changed

+28
-8
lines changed

4 files changed

+28
-8
lines changed

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

+7-3
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,13 @@ int CEthernet::begin(IPAddress local_ip, IPAddress dns_server, IPAddress gateway
5656
int CEthernet::begin(IPAddress local_ip, IPAddress dns_server, IPAddress gateway, IPAddress subnet) {
5757
/* -------------------------------------------------------------------------- */
5858

59-
ni = CLwipIf::getInstance().get(NI_ETHERNET, local_ip, gateway, subnet);
60-
if(ni == nullptr) {
61-
return 0;
59+
if (ni != nullptr) {
60+
ni->config(local_ip, gateway, subnet);
61+
} else {
62+
ni = CLwipIf::getInstance().get(NI_ETHERNET, local_ip, gateway, subnet);
63+
if (ni == nullptr) {
64+
return 0;
65+
}
6266
}
6367

6468
/* If there is a local DHCP informs it of our manual IP configuration to prevent IP conflict */

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

+18
Original file line numberDiff line numberDiff line change
@@ -1271,6 +1271,24 @@ void CNetIf::setLinkDown()
12711271
netif_set_down(&ni);
12721272
}
12731273

1274+
/* -------------------------------------------------------------------------- */
1275+
void CNetIf::config(IPAddress _ip, IPAddress _gw, IPAddress _nm)
1276+
{
1277+
DhcpStop();
1278+
DhcpNotUsed();
1279+
1280+
IP_ADDR4(&ip, _ip[0], _ip[1], _ip[2], _ip[3]);
1281+
IP_ADDR4(&nm, _nm[0], _nm[1], _nm[2], _nm[3]);
1282+
IP_ADDR4(&gw, _gw[0], _gw[1], _gw[2], _gw[3]);
1283+
1284+
netif_set_addr(&ni, &ip, &nm, &gw);
1285+
1286+
if (netif_is_link_up(&ni)) {
1287+
netif_set_down(&ni);
1288+
netif_set_up(&ni);
1289+
}
1290+
}
1291+
12741292
/* ########################################################################## */
12751293
/* ETHERNET NETWORK INTERFACE CLASS */
12761294
/* ########################################################################## */

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)