Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit e8a5b4b

Browse files
authoredAug 6, 2024··
Update Ethernet.cpp
Code cleanup, standardize formatting, added nullptr checks to help stability, reorganized to eliminate local variables to save stack space.
1 parent a430d9f commit e8a5b4b

File tree

1 file changed

+97
-113
lines changed

1 file changed

+97
-113
lines changed
 

‎libraries/Ethernet/src/Ethernet.cpp

Lines changed: 97 additions & 113 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,54 @@
11
#include <EthernetC33.h>
22
#include <EthernetClock.h>
3-
/*
4-
* The old implementation of the begin set a default mac address:
5-
* this does not make any sense.
6-
* Default mac address is in the hardware, when lwip start that mac
7-
* address is passed to lwip
8-
* If mac address needs to be changed then call the appropriate function
9-
* of lwIpIf before to get the interface
10-
*/
3+
4+
/* -------------------------------------------------------------------------- */
5+
void CEthernet::initializeTimer() {
6+
/* -------------------------------------------------------------------------- */
7+
8+
if (!ethernetTimer) {
9+
ethernetTimer = new (std::nothrow) EthernetClock();
10+
if (ethernetTimer) {
11+
ethernetTimer->start();
12+
delay(2);
13+
}
14+
}
15+
}
16+
1117

1218
/* -------------------------------------------------------------------------- */
1319
int CEthernet::begin(unsigned long timeout, unsigned long responseTimeout) {
1420
/* -------------------------------------------------------------------------- */
1521

16-
ethernetTimer = new EthernetClock();
17-
ethernetTimer->start();
18-
delay(2);
19-
(void)responseTimeout;
22+
initializeTimer();
23+
(void)responseTimeout;
2024

21-
int rv = 0;
25+
ni = CLwipIf::getInstance().get(NI_ETHERNET);
26+
if (ni) {
27+
ni->DhcpSetTimeout(timeout);
28+
return static_cast<int>(ni->DhcpStart());
29+
}
30+
return 0;
31+
}
2232

33+
/* -------------------------------------------------------------------------- */
34+
int CEthernet::configureStaticIP(const IPAddress& local_ip, const IPAddress& dns_server, const IPAddress& gateway, const IPAddress& subnet) {
35+
/* -------------------------------------------------------------------------- */
36+
initializeTimer();
37+
38+
if (ni) {
39+
ni->config(local_ip, gateway, subnet);
40+
} else {
41+
ni = CLwipIf::getInstance().get(NI_ETHERNET, local_ip, gateway, subnet);
42+
if (!ni) return 0;
43+
}
2344

24-
ni = CLwipIf::getInstance().get(NI_ETHERNET);
25-
if(ni != nullptr) {
26-
ni->DhcpSetTimeout(timeout);
27-
rv = (int)ni->DhcpStart();
28-
}
29-
30-
return rv;
45+
ni->DhcpNotUsed();
46+
CLwipIf::getInstance().addDns(dns_server);
47+
return 1;
3148
}
3249

3350
/* -------------------------------------------------------------------------- */
34-
int CEthernet::begin(IPAddress local_ip) {
51+
int CEthernet::begin(const IPAddress& local_ip) {
3552
/* -------------------------------------------------------------------------- */
3653
// Assume the DNS server will be the machine on the same network as the local IP
3754
// but with last octet being '1'
@@ -41,7 +58,7 @@ int CEthernet::begin(IPAddress local_ip) {
4158
}
4259

4360
/* -------------------------------------------------------------------------- */
44-
int CEthernet::begin(IPAddress local_ip, IPAddress dns_server) {
61+
int CEthernet::begin(const IPAddress& local_ip, const IPAddress& dns_server) {
4562
/* -------------------------------------------------------------------------- */
4663
// Assume the gateway will be the machine on the same network as the local IP
4764
// but with last octet being '1'
@@ -51,56 +68,42 @@ int CEthernet::begin(IPAddress local_ip, IPAddress dns_server) {
5168
}
5269

5370
/* -------------------------------------------------------------------------- */
54-
int CEthernet::begin(IPAddress local_ip, IPAddress dns_server, IPAddress gateway) {
71+
int CEthernet::begin(const IPAddress& local_ip, const IPAddress& dns_server, const IPAddress& gateway) {
5572
/* -------------------------------------------------------------------------- */
5673
IPAddress subnet(255, 255, 255, 0);
5774
return begin(local_ip, dns_server, gateway, subnet);
5875
}
5976

6077
/* -------------------------------------------------------------------------- */
61-
int CEthernet::begin(IPAddress local_ip, IPAddress dns_server, IPAddress gateway, IPAddress subnet) {
78+
int CEthernet::begin(const IPAddress& local_ip, const IPAddress& dns_server, const IPAddress& gateway, const IPAddress& subnet) {
6279
/* -------------------------------------------------------------------------- */
6380

64-
ethernetTimer = new EthernetClock();
65-
ethernetTimer->start();
66-
delay(2);
67-
68-
if (ni != nullptr) {
69-
ni->config(local_ip, gateway, subnet);
70-
} else {
71-
ni = CLwipIf::getInstance().get(NI_ETHERNET, local_ip, gateway, subnet);
72-
if (ni == nullptr) {
73-
return 0;
74-
}
75-
}
76-
77-
/* If there is a local DHCP informs it of our manual IP configuration to prevent IP conflict */
78-
ni->DhcpNotUsed();
79-
CLwipIf::getInstance().addDns(dns_server);
80-
return 1;
81+
return configureStaticIP(local_ip, dns_server, gateway, subnet);
8182
}
8283

8384
/* -------------------------------------------------------------------------- */
84-
void CEthernet::setDNS(IPAddress dns_server) {
85+
inline void CEthernet::setDNS(const IPAddress& dns_server) {
8586
/* -------------------------------------------------------------------------- */
8687
CLwipIf::getInstance().addDns(dns_server);
8788
}
8889

8990
/* -------------------------------------------------------------------------- */
90-
int CEthernet::begin(uint8_t *mac, unsigned long timeout, unsigned long responseTimeout) {
91+
int CEthernet::begin(const uint8_t* mac, unsigned long timeout, unsigned long responseTimeout) {
9192
/* -------------------------------------------------------------------------- */
9293
CLwipIf::getInstance().setMacAddress(NI_ETHERNET, mac);
9394
return begin(timeout, responseTimeout);
9495
}
9596

9697
/* -------------------------------------------------------------------------- */
97-
int CEthernet::begin(uint8_t *mac_address, IPAddress local_ip) {
98+
int CEthernet::begin(const uint8_t* mac_address, const IPAddress& local_ip) {
9899
/* -------------------------------------------------------------------------- */
99100
// Assume the DNS server will be the machine on the same network as the local IP
100101
// but with last octet being '1'
101-
IPAddress dns_server = local_ip;
102-
dns_server[3] = 1;
103-
return begin(mac_address, local_ip, dns_server);
102+
103+
static const uint8_t GATEWAY_OFFSET = 1;
104+
IPAddress gateway(local_ip);
105+
gateway[3] = GATEWAY_OFFSET;
106+
return begin(mac_address, local_ip, dns_server, gateway);
104107
}
105108

106109
/* -------------------------------------------------------------------------- */
@@ -114,111 +117,92 @@ int CEthernet::begin(uint8_t *mac_address, IPAddress local_ip, IPAddress dns_ser
114117
}
115118

116119
/* -------------------------------------------------------------------------- */
117-
int CEthernet::begin(uint8_t *mac_address, IPAddress local_ip, IPAddress dns_server, IPAddress gateway) {
120+
CEthernet::begin(const uint8_t* mac_address, const IPAddress& local_ip, const IPAddress& dns_server, const IPAddress& gateway) {
118121
/* -------------------------------------------------------------------------- */
119-
IPAddress subnet(255, 255, 255, 0);
120-
return begin(mac_address, local_ip, dns_server, gateway, subnet);
122+
static const IPAddress DEFAULT_SUBNET(255, 255, 255, 0);
123+
return begin(mac_address, local_ip, dns_server, gateway, DEFAULT_SUBNET);
121124
}
122125

123126
/* -------------------------------------------------------------------------- */
124-
int CEthernet::begin(uint8_t *mac, IPAddress local_ip, IPAddress dns_server, IPAddress gateway, IPAddress subnet, unsigned long timeout, unsigned long responseTimeout) {
127+
int CEthernet::begin(const uint8_t* mac, const IPAddress& local_ip, const IPAddress& dns_server, const IPAddress& gateway, const IPAddress& subnet, unsigned long timeout, unsigned long responseTimeout) {
125128
/* -------------------------------------------------------------------------- */
126-
CLwipIf::getInstance().setMacAddress(NI_ETHERNET, mac);
127-
return begin(local_ip, dns_server, gateway, subnet);
129+
CLwipIf::getInstance().setMacAddress(NI_ETHERNET, mac);
130+
return begin(local_ip, dns_server, gateway, subnet);
128131
}
129132

130133
/* -------------------------------------------------------------------------- */
131-
EthernetLinkStatus CEthernet::linkStatus() {
134+
EthernetLinkStatus CEthernet::linkStatus() const {
132135
/* -------------------------------------------------------------------------- */
133-
if(ni != nullptr) {
134-
return (!CLwipIf::getInstance().isEthInitialized()) ? Unknown : (ni->isLinkUp() ? LinkON : LinkOFF);
135-
}
136-
return Unknown;
136+
if (!ni) return Unknown;
137+
if (!CLwipIf::getInstance().isEthInitialized()) return Unknown;
138+
return ni->isLinkUp() ? LinkON : LinkOFF;
137139
}
138140

139141
/* -------------------------------------------------------------------------- */
140-
EthernetHardwareStatus CEthernet::hardwareStatus() {
142+
EthernetHardwareStatus CEthernet::hardwareStatus() const {
141143
/* -------------------------------------------------------------------------- */
142144
return EthernetLwip;
143145
}
144146

145147
/* -------------------------------------------------------------------------- */
146148
int CEthernet::disconnect() {
147149
/* -------------------------------------------------------------------------- */
148-
ethernetTimer->stop();
149-
delete(ethernetTimer);
150-
ethernetTimer = NULL;
151-
return 1;
150+
if (ethernetTimer) {
151+
ethernetTimer->stop();
152+
delete ethernetTimer;
153+
ethernetTimer = nullptr;
154+
}
155+
ni = nullptr;
156+
return 1;
152157
}
153158

154159
/* -------------------------------------------------------------------------- */
155160
int CEthernet::maintain() {
156161
/* -------------------------------------------------------------------------- */
157-
int rc = DHCP_CHECK_NONE;
158-
159-
if (ni != NULL) {
160-
//we have a pointer to dhcp, use it
161-
rc = ni->checkLease();
162-
switch (rc) {
163-
case DHCP_CHECK_NONE:
164-
//nothing done
165-
break;
166-
case DHCP_CHECK_RENEW_OK:
167-
case DHCP_CHECK_REBIND_OK:
168-
//_dnsServerAddress = _dhcp->getDnsServerIp();
169-
break;
170-
default:
171-
//this is actually a error, it will retry though
172-
break;
173-
}
174-
}
175-
return rc;
162+
return ni ? ni->checkLease() : DHCP_CHECK_NONE;
176163
}
177164

178-
/*
179-
* This function updates the LwIP stack and can be called to be sure to update
180-
* the stack (e.g. in case of a long loop).
181-
*/
165+
/* -------------------------------------------------------------------------- */
182166
void CEthernet::schedule(void) {
183-
if (ni != NULL) {
184-
ni->task();
185-
}
167+
/* -------------------------------------------------------------------------- */
168+
if (ni) ni->task();
186169
}
187170

188-
189-
190-
uint8_t *CEthernet::MACAddress(void) {
191-
CLwipIf::getInstance().getMacAddress(NI_ETHERNET, mac_address);
192-
return mac_address;
171+
/* -------------------------------------------------------------------------- */
172+
const uint8_t* CEthernet::MACAddress() const {
173+
/* -------------------------------------------------------------------------- */
174+
CLwipIf::getInstance().getMacAddress(NI_ETHERNET, const_cast<uint8_t*>(mac_address));
175+
return mac_address;
193176
}
194177

195-
void CEthernet::MACAddress(uint8_t *mac) {
196-
CLwipIf::getInstance().getMacAddress(NI_ETHERNET, mac);
178+
/* -------------------------------------------------------------------------- */
179+
void CEthernet::MACAddress(uint8_t* mac) const {
180+
/* -------------------------------------------------------------------------- */
181+
CLwipIf::getInstance().getMacAddress(NI_ETHERNET, mac);
197182
}
198183

199-
IPAddress CEthernet::localIP() {
200-
if(ni != nullptr) {
201-
return IPAddress(ni->getIpAdd());
202-
}
203-
return IPAddress((uint32_t)0);
184+
/* -------------------------------------------------------------------------- */
185+
IPAddress CEthernet::localIP() const {
186+
/* -------------------------------------------------------------------------- */
187+
return ni ? IPAddress(ni->getIpAdd()) : IPAddress(static_cast<uint32_t>(0));
204188
}
205189

206-
IPAddress CEthernet::subnetMask() {
207-
if(ni != nullptr) {
208-
return IPAddress(ni->getNmAdd());
209-
}
210-
return IPAddress((uint32_t)0);
190+
/* -------------------------------------------------------------------------- */
191+
IPAddress CEthernet::subnetMask() const {
192+
/* -------------------------------------------------------------------------- */
193+
return ni ? IPAddress(ni->getNmAdd()) : IPAddress(static_cast<uint32_t>(0));
211194
}
212195

213-
IPAddress CEthernet::gatewayIP() {
214-
if(ni != nullptr) {
215-
return IPAddress(ni->getGwAdd());
216-
}
217-
return IPAddress((uint32_t)0);
196+
/* -------------------------------------------------------------------------- */
197+
IPAddress CEthernet::gatewayIP() const {
198+
/* -------------------------------------------------------------------------- */
199+
return ni ? IPAddress(ni->getGwAdd()) : IPAddress(static_cast<uint32_t>(0));
218200
}
219201

220-
IPAddress CEthernet::dnsServerIP() {
221-
return CLwipIf::getInstance().getDns();
202+
/* -------------------------------------------------------------------------- */
203+
IPAddress CEthernet::dnsServerIP() const {
204+
/* -------------------------------------------------------------------------- */
205+
return CLwipIf::getInstance().getDns();
222206
}
223207

224208
CEthernet Ethernet;

0 commit comments

Comments
 (0)
Please sign in to comment.