Skip to content

CNetIf - added config method to apply new static IP settings for WiFi and Ethernet #209

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions libraries/Ethernet/src/Ethernet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,13 @@ int CEthernet::begin(IPAddress local_ip, IPAddress subnet, IPAddress gateway) {
int CEthernet::begin(IPAddress local_ip, IPAddress subnet, IPAddress gateway, IPAddress dns_server) {
/* -------------------------------------------------------------------------- */

ni = CLwipIf::getInstance().get(NI_ETHERNET, local_ip, gateway, subnet);
if(ni == nullptr) {
return 0;
if (ni != nullptr) {
ni->config(local_ip, gateway, subnet);
} else {
ni = CLwipIf::getInstance().get(NI_ETHERNET, local_ip, gateway, subnet);
if (ni == nullptr) {
return 0;
}
}

/* If there is a local DHCP informs it of our manual IP configuration to prevent IP conflict */
Expand Down
6 changes: 1 addition & 5 deletions libraries/WiFi/src/WiFi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -98,11 +98,7 @@ void CWifi::_config(IPAddress local_ip, IPAddress gateway, IPAddress subnet) {
/* -------------------------------------------------------------------------- */
_useStaticIp = local_ip != INADDR_NONE;
if(ni != nullptr) {
ni->DhcpStop();
ni->DhcpNotUsed();
IP_ADDR4(&ni->ip, local_ip[0], local_ip[1], local_ip[2], local_ip[3]);
IP_ADDR4(&ni->gw, gateway[0], gateway[1], gateway[2], gateway[3]);
IP_ADDR4(&ni->nm, subnet[0], subnet[1], subnet[2], subnet[3]);
ni->config(local_ip, gateway, subnet);
}
else {
CNetIf::default_ip = local_ip;
Expand Down
18 changes: 18 additions & 0 deletions libraries/lwIpWrapper/src/CNetIf.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1268,6 +1268,24 @@ void CNetIf::setLinkDown()
netif_set_down(&ni);
}

/* -------------------------------------------------------------------------- */
void CNetIf::config(IPAddress _ip, IPAddress _gw, IPAddress _nm)
{
DhcpStop();
DhcpNotUsed();

IP_ADDR4(&ip, _ip[0], _ip[1], _ip[2], _ip[3]);
IP_ADDR4(&nm, _nm[0], _nm[1], _nm[2], _nm[3]);
IP_ADDR4(&gw, _gw[0], _gw[1], _gw[2], _gw[3]);

netif_set_addr(&ni, &ip, &nm, &gw);

if (netif_is_link_up(&ni)) {
netif_set_down(&ni);
netif_set_up(&ni);
}
}

/* ########################################################################## */
/* ETHERNET NETWORK INTERFACE CLASS */
/* ########################################################################## */
Expand Down
2 changes: 2 additions & 0 deletions libraries/lwIpWrapper/src/CNetIf.h
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,8 @@ class CNetIf {
uint32_t getNmAdd() { return ip4_addr_get_u32(&(ni.netmask)); }
uint32_t getGwAdd() { return ip4_addr_get_u32(&(ni.gw)); }

void config(IPAddress _ip, IPAddress _gw, IPAddress _nm);

void setHostname(const char* name)
{
memset(hostname, 0x00, MAX_HOSTNAME_DIM);
Expand Down