1
1
#include < EthernetC33.h>
2
2
#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
+
11
17
12
18
/* -------------------------------------------------------------------------- */
13
19
int CEthernet::begin (unsigned long timeout, unsigned long responseTimeout) {
14
20
/* -------------------------------------------------------------------------- */
15
21
16
- ethernetTimer = new EthernetClock ();
17
- ethernetTimer->start ();
18
- delay (2 );
19
- (void )responseTimeout;
22
+ initializeTimer ();
23
+ (void )responseTimeout;
20
24
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
+ }
22
32
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
+ }
23
44
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 ;
31
48
}
32
49
33
50
/* -------------------------------------------------------------------------- */
34
- int CEthernet::begin (IPAddress local_ip) {
51
+ int CEthernet::begin (const IPAddress& local_ip) {
35
52
/* -------------------------------------------------------------------------- */
36
53
// Assume the DNS server will be the machine on the same network as the local IP
37
54
// but with last octet being '1'
@@ -41,7 +58,7 @@ int CEthernet::begin(IPAddress local_ip) {
41
58
}
42
59
43
60
/* -------------------------------------------------------------------------- */
44
- int CEthernet::begin (IPAddress local_ip, IPAddress dns_server) {
61
+ int CEthernet::begin (const IPAddress& local_ip, const IPAddress& dns_server) {
45
62
/* -------------------------------------------------------------------------- */
46
63
// Assume the gateway will be the machine on the same network as the local IP
47
64
// but with last octet being '1'
@@ -51,56 +68,42 @@ int CEthernet::begin(IPAddress local_ip, IPAddress dns_server) {
51
68
}
52
69
53
70
/* -------------------------------------------------------------------------- */
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) {
55
72
/* -------------------------------------------------------------------------- */
56
73
IPAddress subnet (255 , 255 , 255 , 0 );
57
74
return begin (local_ip, dns_server, gateway, subnet);
58
75
}
59
76
60
77
/* -------------------------------------------------------------------------- */
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) {
62
79
/* -------------------------------------------------------------------------- */
63
80
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);
81
82
}
82
83
83
84
/* -------------------------------------------------------------------------- */
84
- void CEthernet::setDNS (IPAddress dns_server) {
85
+ inline void CEthernet::setDNS (const IPAddress& dns_server) {
85
86
/* -------------------------------------------------------------------------- */
86
87
CLwipIf::getInstance ().addDns (dns_server);
87
88
}
88
89
89
90
/* -------------------------------------------------------------------------- */
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) {
91
92
/* -------------------------------------------------------------------------- */
92
93
CLwipIf::getInstance ().setMacAddress (NI_ETHERNET, mac);
93
94
return begin (timeout, responseTimeout);
94
95
}
95
96
96
97
/* -------------------------------------------------------------------------- */
97
- int CEthernet::begin (uint8_t * mac_address, IPAddress local_ip) {
98
+ int CEthernet::begin (const uint8_t * mac_address, const IPAddress& local_ip) {
98
99
/* -------------------------------------------------------------------------- */
99
100
// Assume the DNS server will be the machine on the same network as the local IP
100
101
// 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);
104
107
}
105
108
106
109
/* -------------------------------------------------------------------------- */
@@ -114,111 +117,92 @@ int CEthernet::begin(uint8_t *mac_address, IPAddress local_ip, IPAddress dns_ser
114
117
}
115
118
116
119
/* -------------------------------------------------------------------------- */
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) {
118
121
/* -------------------------------------------------------------------------- */
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 );
121
124
}
122
125
123
126
/* -------------------------------------------------------------------------- */
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) {
125
128
/* -------------------------------------------------------------------------- */
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);
128
131
}
129
132
130
133
/* -------------------------------------------------------------------------- */
131
- EthernetLinkStatus CEthernet::linkStatus () {
134
+ EthernetLinkStatus CEthernet::linkStatus () const {
132
135
/* -------------------------------------------------------------------------- */
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;
137
139
}
138
140
139
141
/* -------------------------------------------------------------------------- */
140
- EthernetHardwareStatus CEthernet::hardwareStatus () {
142
+ EthernetHardwareStatus CEthernet::hardwareStatus () const {
141
143
/* -------------------------------------------------------------------------- */
142
144
return EthernetLwip;
143
145
}
144
146
145
147
/* -------------------------------------------------------------------------- */
146
148
int CEthernet::disconnect () {
147
149
/* -------------------------------------------------------------------------- */
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 ;
152
157
}
153
158
154
159
/* -------------------------------------------------------------------------- */
155
160
int CEthernet::maintain () {
156
161
/* -------------------------------------------------------------------------- */
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;
176
163
}
177
164
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
+ /* -------------------------------------------------------------------------- */
182
166
void CEthernet::schedule (void ) {
183
- if (ni != NULL ) {
184
- ni->task ();
185
- }
167
+ /* -------------------------------------------------------------------------- */
168
+ if (ni) ni->task ();
186
169
}
187
170
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;
193
176
}
194
177
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);
197
182
}
198
183
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 ));
204
188
}
205
189
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 ));
211
194
}
212
195
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 ));
218
200
}
219
201
220
- IPAddress CEthernet::dnsServerIP () {
221
- return CLwipIf::getInstance ().getDns ();
202
+ /* -------------------------------------------------------------------------- */
203
+ IPAddress CEthernet::dnsServerIP () const {
204
+ /* -------------------------------------------------------------------------- */
205
+ return CLwipIf::getInstance ().getDns ();
222
206
}
223
207
224
208
CEthernet Ethernet;
0 commit comments