forked from arduino/ArduinoCore-renesas
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEthernet.cpp
206 lines (174 loc) · 7.08 KB
/
Ethernet.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
#include <EthernetC33.h>
/*
* The old implementation of the begin set a default mac address:
* this does not make any sense.
* Default mac address is in the hardware, when lwip start that mac
* address is passed to lwip
* If mac address needs to be changed then call the appropriate function
* of lwIpIf before to get the interface
*/
/* -------------------------------------------------------------------------- */
int CEthernet::begin(unsigned long timeout, unsigned long responseTimeout) {
/* -------------------------------------------------------------------------- */
(void)responseTimeout;
int rv = 0;
ni = CLwipIf::getInstance().get(NI_ETHERNET);
if(ni != nullptr) {
ni->DhcpSetTimeout(timeout);
rv = (int)ni->DhcpStart();
}
return rv;
}
/* -------------------------------------------------------------------------- */
int CEthernet::begin(IPAddress local_ip) {
/* -------------------------------------------------------------------------- */
IPAddress subnet(255, 255, 255, 0);
return begin(local_ip, subnet);
}
/* -------------------------------------------------------------------------- */
int CEthernet::begin(IPAddress local_ip, IPAddress subnet) {
/* -------------------------------------------------------------------------- */
// Assume the gateway will be the machine on the same network as the local IP
// but with last octet being '1'
IPAddress gateway = local_ip;
gateway[3] = 1;
return begin(local_ip, subnet, gateway);
}
/* -------------------------------------------------------------------------- */
int CEthernet::begin(IPAddress local_ip, IPAddress subnet, IPAddress gateway) {
/* -------------------------------------------------------------------------- */
// Assume the DNS server will be the same machine than gateway
return begin(local_ip, subnet, gateway, 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 there is a local DHCP informs it of our manual IP configuration to prevent IP conflict */
ni->DhcpNotUsed();
CLwipIf::getInstance().addDns(dns_server);
return 1;
}
/* -------------------------------------------------------------------------- */
void CEthernet::setDNS(IPAddress dns_server) {
/* -------------------------------------------------------------------------- */
CLwipIf::getInstance().addDns(dns_server);
}
/* -------------------------------------------------------------------------- */
int CEthernet::begin(uint8_t *mac, unsigned long timeout, unsigned long responseTimeout) {
/* -------------------------------------------------------------------------- */
int ret = (int)CLwipIf::getInstance().setMacAddress(NI_ETHERNET, mac);
begin(timeout, responseTimeout);
return ret;
}
/* -------------------------------------------------------------------------- */
int CEthernet::begin(uint8_t *mac_address, IPAddress local_ip) {
/* -------------------------------------------------------------------------- */
// Assume the DNS server will be the machine on the same network as the local IP
// but with last octet being '1'
IPAddress dns_server = local_ip;
dns_server[3] = 1;
return begin(mac_address, local_ip, dns_server);
}
/* -------------------------------------------------------------------------- */
int CEthernet::begin(uint8_t *mac_address, IPAddress local_ip, IPAddress dns_server) {
/* -------------------------------------------------------------------------- */
// Assume the gateway will be the machine on the same network as the local IP
// but with last octet being '1'
IPAddress gateway = local_ip;
gateway[3] = 1;
return begin(mac_address, local_ip, dns_server, gateway);
}
/* -------------------------------------------------------------------------- */
int CEthernet::begin(uint8_t *mac_address, IPAddress local_ip, IPAddress dns_server, IPAddress gateway) {
/* -------------------------------------------------------------------------- */
IPAddress subnet(255, 255, 255, 0);
return begin(mac_address, local_ip, dns_server, gateway, subnet);
}
/* -------------------------------------------------------------------------- */
int CEthernet::begin(uint8_t *mac, IPAddress local_ip, IPAddress dns_server, IPAddress gateway, IPAddress subnet, unsigned long timeout, unsigned long responseTimeout) {
/* -------------------------------------------------------------------------- */
CLwipIf::getInstance().setMacAddress(NI_ETHERNET, mac_address);
return begin(local_ip, subnet, gateway, dns_server);
}
/* -------------------------------------------------------------------------- */
EthernetLinkStatus CEthernet::linkStatus() {
/* -------------------------------------------------------------------------- */
if(ni != nullptr) {
return (!CLwipIf::getInstance().isEthInitialized()) ? Unknown : (ni->isLinkUp() ? LinkON : LinkOFF);
}
return Unknown;
}
/* -------------------------------------------------------------------------- */
EthernetHardwareStatus CEthernet::hardwareStatus() {
/* -------------------------------------------------------------------------- */
return EthernetLwip;
}
/* -------------------------------------------------------------------------- */
int CEthernet::disconnect() {
/* -------------------------------------------------------------------------- */
return 1;
}
/* -------------------------------------------------------------------------- */
int CEthernet::maintain() {
/* -------------------------------------------------------------------------- */
int rc = DHCP_CHECK_NONE;
if (ni != NULL) {
//we have a pointer to dhcp, use it
rc = ni->checkLease();
switch (rc) {
case DHCP_CHECK_NONE:
//nothing done
break;
case DHCP_CHECK_RENEW_OK:
case DHCP_CHECK_REBIND_OK:
//_dnsServerAddress = _dhcp->getDnsServerIp();
break;
default:
//this is actually a error, it will retry though
break;
}
}
return rc;
}
/*
* This function updates the LwIP stack and can be called to be sure to update
* the stack (e.g. in case of a long loop).
*/
void CEthernet::schedule(void) {
if (ni != NULL) {
ni->task();
}
}
uint8_t *CEthernet::MACAddress(void) {
CLwipIf::getInstance().getMacAddress(NI_ETHERNET, mac_address);
return mac_address;
}
void CEthernet::MACAddress(uint8_t *mac) {
CLwipIf::getInstance().getMacAddress(NI_ETHERNET, mac);
}
IPAddress CEthernet::localIP() {
if(ni != nullptr) {
return IPAddress(ni->getIpAdd());
}
return IPAddress((uint32_t)0);
}
IPAddress CEthernet::subnetMask() {
if(ni != nullptr) {
return IPAddress(ni->getNmAdd());
}
return IPAddress((uint32_t)0);
}
IPAddress CEthernet::gatewayIP() {
if(ni != nullptr) {
return IPAddress(ni->getGwAdd());
}
return IPAddress((uint32_t)0);
}
IPAddress CEthernet::dnsServerIP() {
return CLwipIf::getInstance().getDns();
}
CEthernet Ethernet;