Skip to content

Commit 24fdbf0

Browse files
Ethernet lib: add set IP, Subnet, Gateway functions
1 parent 554d34f commit 24fdbf0

File tree

4 files changed

+80
-13
lines changed

4 files changed

+80
-13
lines changed

libraries/SocketWrapper/Ethernet.cpp

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ int EthernetClass::begin(uint8_t *mac, IPAddress ip, IPAddress dns, IPAddress ga
3232
int EthernetClass::begin(uint8_t *mac, IPAddress ip, IPAddress dns, IPAddress gateway, IPAddress subnet, unsigned long timeout, unsigned long responseTimeout) {
3333
setMACAddress(mac);
3434

35-
if (!NetworkInterface::setLocalIP(ip, subnet, gateway)) {
35+
if (!NetworkInterface::setLocalIPFull(ip, subnet, gateway)) {
3636
return 0;
3737
}
3838

@@ -72,9 +72,41 @@ void EthernetClass::setMACAddress(const uint8_t *mac_address) {
7272
}
7373
}
7474

75+
void EthernetClass::MACAddress(uint8_t *mac_address) {
76+
setMACAddress(mac_address);
77+
}
78+
7579
IPAddress EthernetClass::localIP() {
7680
return NetworkInterface::localIP();
7781
}
7882

83+
IPAddress EthernetClass::subnetMask() {
84+
return NetworkInterface::subnetMask();
85+
}
86+
87+
IPAddress EthernetClass::gatewayIP() {
88+
return NetworkInterface::gatewayIP();
89+
}
90+
91+
IPAddress EthernetClass::dnsServerIP() {
92+
return NetworkInterface::dnsServerIP();
93+
}
94+
95+
void EthernetClass::setLocalIP(const IPAddress local_ip) {
96+
NetworkInterface::setLocalIP(local_ip);
97+
}
98+
99+
void EthernetClass::setSubnetMask(const IPAddress subnet) {
100+
NetworkInterface::setSubnetMask(subnet);
101+
}
102+
103+
void EthernetClass::setGatewayIP(const IPAddress gateway) {
104+
NetworkInterface::setGatewayIP(gateway);
105+
}
106+
107+
void EthernetClass::setDnsServerIP(const IPAddress dns_server) {
108+
NetworkInterface::setDnsServerIP(dns_server);
109+
}
110+
79111
EthernetClass Ethernet;
80112
#endif

libraries/SocketWrapper/Ethernet.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ class EthernetClass: public NetworkInterface
3131
int begin(uint8_t *mac, IPAddress ip, IPAddress dns);
3232
int begin(uint8_t *mac, IPAddress ip, IPAddress dns, IPAddress gateway);
3333
int begin(uint8_t *mac, IPAddress ip, IPAddress dns, IPAddress gateway, IPAddress subnet, unsigned long timeout = 60000, unsigned long responseTimeout = 4000);
34+
3435
void init(uint8_t sspin = 10);
3536

3637
void MACAddress(uint8_t *mac_address);
@@ -44,6 +45,7 @@ class EthernetClass: public NetworkInterface
4445
void setSubnetMask(const IPAddress subnet);
4546
void setGatewayIP(const IPAddress gateway);
4647
void setDnsServerIP(const IPAddress dns_server);
48+
4749
void setRetransmissionTimeout(uint16_t milliseconds);
4850
void setRetransmissionCount(uint8_t num);
4951
};

libraries/SocketWrapper/SocketHelpers.cpp

Lines changed: 40 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -119,10 +119,6 @@ IPAddress NetworkInterface::dnsServerIP() {
119119
return arduino::INADDR_NONE;
120120
}
121121

122-
IPAddress NetworkInterface::dnsIP(int n) {
123-
//TODO
124-
}
125-
126122
void NetworkInterface::setMACAddress(const uint8_t* mac) {
127123
struct net_eth_addr new_mac;
128124
struct ethernet_req_params params = { 0 };
@@ -153,7 +149,7 @@ bool NetworkInterface::disconnect() {
153149
return (net_if_down(netif) == 0);
154150
}
155151

156-
bool NetworkInterface::setLocalIP(IPAddress ip, IPAddress subnet, IPAddress gateway) {
152+
bool NetworkInterface::setLocalIPFull(IPAddress ip, IPAddress subnet, IPAddress gateway) {
157153
struct in_addr ip_addr, subnet_addr, gw_addr;
158154

159155
ip_addr.s_addr = ip;
@@ -173,4 +169,43 @@ bool NetworkInterface::setLocalIP(IPAddress ip, IPAddress subnet, IPAddress gate
173169
net_if_ipv4_set_gw(netif, &gw_addr);
174170
LOG_INF("Static IP configured");
175171
return true;
172+
}
173+
174+
bool NetworkInterface::setLocalIP(IPAddress ip) {
175+
struct in_addr addr;
176+
addr.s_addr = ip;
177+
178+
if (!net_if_ipv4_addr_add(netif, &addr, NET_ADDR_MANUAL, 0)) {
179+
LOG_ERR("Failed to set local IP address");
180+
return false;
181+
}
182+
183+
LOG_INF("Local IP address set: %s", ip.toString().c_str());
184+
return true;
185+
}
186+
187+
bool NetworkInterface::setSubnetMask(IPAddress subnet) {
188+
struct in_addr netmask_addr;
189+
netmask_addr.s_addr = subnet;
190+
191+
if (!net_if_ipv4_set_netmask_by_addr(netif, &netmask_addr, &netmask_addr)) {
192+
LOG_ERR("Failed to set subnet mask");
193+
return false;
194+
}
195+
196+
LOG_INF("Subnet mask set: %s", subnet.toString().c_str());
197+
return true;
198+
}
199+
200+
bool NetworkInterface::setGatewayIP(IPAddress gateway) {
201+
struct in_addr gw_addr;
202+
gw_addr.s_addr = gateway;
203+
204+
net_if_ipv4_set_gw(netif, &gw_addr);
205+
LOG_INF("Gateway IP set: %s", gateway.toString().c_str());
206+
return true;
207+
}
208+
209+
bool NetworkInterface::setDnsServerIP(IPAddress dns_server) {
210+
return false; // DNS server dynamic configuration is not supported
176211
}

libraries/SocketWrapper/SocketHelpers.h

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -49,16 +49,14 @@ class NetworkInterface {
4949
IPAddress gatewayIP();
5050
IPAddress dnsServerIP();
5151

52-
IPAddress dnsIP(int n = 0);
53-
5452
void setMACAddress(const uint8_t* mac);
55-
bool setLocalIP(IPAddress ip, IPAddress subnet, IPAddress gateway);
53+
bool setLocalIPFull(IPAddress ip, IPAddress subnet, IPAddress gateway);
54+
bool setLocalIP(IPAddress ip);
55+
bool setSubnetMask(IPAddress subnet);
56+
bool setGatewayIP(IPAddress gateway);
57+
bool setDnsServerIP(IPAddress dns_server);
5658

5759
int begin(bool blocking = true, uint32_t additional_event_mask = 0);
5860

5961
bool disconnect();
60-
61-
// TODO: manual functions for setting IP address, subnet mask, gateway, etc.
62-
// net_if_ipv4_set_netmask_by_addr(iface, &addr4, &nm);
63-
// net_if_ipv4_addr_add(iface, &addr4, NET_ADDR_MANUAL, 0);
6462
};

0 commit comments

Comments
 (0)