Skip to content

Commit 9f30dcb

Browse files
Ethernet lib: tentative set manual IP,subnet,gateway
1 parent 60f110c commit 9f30dcb

File tree

4 files changed

+34
-3
lines changed

4 files changed

+34
-3
lines changed

libraries/SocketWrapper/Ethernet.cpp

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,17 @@ int EthernetClass::begin(uint8_t *mac, IPAddress ip, IPAddress dns, IPAddress ga
3030
}
3131

3232
int EthernetClass::begin(uint8_t *mac, IPAddress ip, IPAddress dns, IPAddress gateway, IPAddress subnet, unsigned long timeout, unsigned long responseTimeout) {
33-
// TODO: Config the network interface with the provided IP, DNS, gateway, and subnet
33+
setMACAddress(mac);
34+
35+
if (!NetworkInterface::setLocalIP(ip, subnet, gateway)) {
36+
return 0;
37+
}
38+
39+
if (!net_if_is_up(netif)) {
40+
net_if_up(netif);
41+
}
3442

35-
return begin(mac, timeout, responseTimeout);
43+
return 1;
3644
}
3745

3846
EthernetLinkStatus EthernetClass::linkStatus() {

libraries/SocketWrapper/Ethernet.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ class EthernetClass: public NetworkInterface
2121
EthernetClass() {}
2222
virtual ~EthernetClass() {}
2323

24-
int begin(uint8_t *mac, unsigned long timeout = 60000, unsigned long responseTimeout = 4000);
24+
int begin(uint8_t *mac = nullptr, unsigned long timeout = 60000, unsigned long responseTimeout = 4000);
2525
int maintain();
2626
EthernetLinkStatus linkStatus();
2727
EthernetHardwareStatus hardwareStatus();

libraries/SocketWrapper/SocketHelpers.cpp

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,3 +152,25 @@ int NetworkInterface::begin(bool blocking, uint32_t additional_event_mask) {
152152
bool NetworkInterface::disconnect() {
153153
return (net_if_down(netif) == 0);
154154
}
155+
156+
bool NetworkInterface::setLocalIP(IPAddress ip, IPAddress subnet, IPAddress gateway) {
157+
struct in_addr ip_addr, subnet_addr, gw_addr;
158+
159+
ip_addr.s_addr = ip;
160+
subnet_addr.s_addr = subnet;
161+
gw_addr.s_addr = gateway;
162+
163+
if (!net_if_ipv4_addr_add(netif, &ip_addr, NET_ADDR_MANUAL, 0)) {
164+
LOG_ERR("Failed to set static IP address");
165+
return false;
166+
}
167+
168+
if (!net_if_ipv4_set_netmask_by_addr(netif, &ip_addr, &subnet_addr)) {
169+
LOG_ERR("Failed to set subnet mask");
170+
return false;
171+
}
172+
173+
net_if_ipv4_set_gw(netif, &gw_addr);
174+
LOG_INF("Static IP configured");
175+
return true;
176+
}

libraries/SocketWrapper/SocketHelpers.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ class NetworkInterface {
5252
IPAddress dnsIP(int n = 0);
5353

5454
void setMACAddress(const uint8_t* mac);
55+
bool setLocalIP(IPAddress ip, IPAddress subnet, IPAddress gateway);
5556

5657
int begin(bool blocking = true, uint32_t additional_event_mask = 0);
5758

0 commit comments

Comments
 (0)