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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
|
/*
* Network.cpp
*
* Created on: 13 Dec 2016
* Author: David
*/
#include "Network.h"
#include "NetworkTransaction.h"
#include "Platform.h"
#include "wizchip_conf.h"
#include "Wiznet/Internet/DHCP/dhcp.h"
void Network::SetIPAddress(const uint8_t p_ipAddress[], const uint8_t p_netmask[], const uint8_t p_gateway[])
{
memcpy(ipAddress, p_ipAddress, sizeof(ipAddress));
memcpy(netmask, p_netmask, sizeof(netmask));
memcpy(gateway, p_gateway, sizeof(gateway));
}
Network::Network(Platform* p)
: platform(p), lastTickMillis(0),
httpPort(DefaultHttpPort), state(NetworkState::disabled), activated(false)
{
}
void Network::Init()
{
// Ensure that the W5500 chip is in the reset state
pinMode(EspResetPin, OUTPUT_LOW);
state = NetworkState::disabled;
longWait = platform->Time();
lastTickMillis = millis();
NetworkBuffer::AllocateBuffers(NetworkBufferCount);
NetworkTransaction::AllocateTransactions(NetworkTransactionCount);
SetIPAddress(DefaultIpAddress, DefaultNetMask, DefaultGateway);
strcpy(hostname, HOSTNAME);
}
// This is called at the end of config.g processing.
// Start the network if it was enabled
void Network::Activate()
{
activated = true;
if (state == NetworkState::enabled)
{
Start();
}
}
void Network::Exit()
{
Stop();
}
// Main spin loop. If 'full' is true then we are being called from the main spin loop. If false then we are being called during HSMCI idle time.
void Network::Spin(bool full)
{
switch(state)
{
case NetworkState::enabled:
case NetworkState::disabled:
// Nothing to do
break;
case NetworkState::establishingLink:
if (full && wizphy_getphylink() == PHY_LINK_ON)
{
usingDhcp = (ipAddress[0] == 0 && ipAddress[1] == 0 && ipAddress[2] == 0 && ipAddress[3] == 0);
if (usingDhcp)
{
debugPrintf("Link established, getting IP address\n");
// IP address is all zeros, so use DHCP
DHCP_init(DhcpSocketNumber, hostname);
lastTickMillis = millis();
state = NetworkState::obtainingIP;
}
else
{
debugPrintf("Link established, network running\n");
InitSockets();
state = NetworkState::active;
}
}
break;
case NetworkState::obtainingIP:
if (full)
{
if (wizphy_getphylink() == PHY_LINK_ON)
{
const uint32_t now = millis();
if (now - lastTickMillis >= 1000)
{
lastTickMillis += 1000;
DHCP_time_handler();
}
const DhcpRunResult ret = DHCP_run();
if (ret == DhcpRunResult::DHCP_IP_ASSIGN)
{
debugPrintf("IP address obtained, network running\n");
getSIPR(ipAddress);
// Send mDNS announcement so that some routers can perform hostname mapping
// if this board is connected via a non-IGMP capable WiFi bridge (like the TP-Link WR701N)
//mdns_announce();
InitSockets();
state = NetworkState::active;
}
}
else
{
debugPrintf("Lost phy link\n");
DHCP_stop();
TerminateSockets();
state = NetworkState::establishingLink;
}
}
break;
case NetworkState::active:
// Check that the link is still up
if (wizphy_getphylink() == PHY_LINK_ON)
{
// Maintain DHCP
if (full && usingDhcp)
{
const uint32_t now = millis();
if (now - lastTickMillis >= 1000)
{
lastTickMillis += 1000;
DHCP_time_handler();
}
const DhcpRunResult ret = DHCP_run();
if (ret == DhcpRunResult::DHCP_IP_CHANGED)
{
debugPrintf("IP address changed\n");
getSIPR(ipAddress);
}
}
// Poll the next TCP socket
sockets[nextSocketToPoll].Poll(full);
// Move on to the next TCP socket for next time
++nextSocketToPoll;
if (nextSocketToPoll == NumTcpSockets)
{
nextSocketToPoll = 0;
}
}
else if (full)
{
debugPrintf("Lost phy link\n");
if (usingDhcp)
{
DHCP_stop();
}
TerminateSockets();
state = NetworkState::establishingLink;
}
break;
}
platform->ClassReport(longWait);
}
void Network::Diagnostics(MessageType mtype)
{
platform->Message(mtype, "=== Network ===\n");
platform->MessageF(mtype, "State: %d\n", (int)state);
}
void Network::Start()
{
SetIPAddress(platform->GetIPAddress(), platform->NetMask(), platform->GateWay());
pinMode(EspResetPin, OUTPUT_LOW);
delayMicroseconds(550); // W550 reset pulse must be at least 500us long
Platform::WriteDigital(EspResetPin, HIGH); // raise /Reset pin
delay(55); // W5500 needs 50ms to start up
static const uint8_t bufSizes[8] = { 2, 2, 2, 2, 2, 2, 2, 2 };
wizchip_init(bufSizes, bufSizes);
setSHAR(platform->MACAddress());
setSIPR(ipAddress);
setGAR(gateway);
setSUBR(netmask);
state = NetworkState::establishingLink;
}
void Network::Stop()
{
if (state != NetworkState::disabled)
{
//TODO Ethernet.stop();
if (usingDhcp)
{
DHCP_stop();
}
digitalWrite(EspResetPin, LOW); // put the ESP back into reset
state = NetworkState::disabled;
}
}
void Network::Enable()
{
if (state == NetworkState::disabled)
{
state = NetworkState::enabled;
if (activated)
{
Start();
}
}
}
void Network::Disable()
{
if (activated && state != NetworkState::disabled)
{
Stop();
platform->Message(GENERIC_MESSAGE, "Network stopped\n");
}
}
bool Network::IsEnabled() const
{
return state != NetworkState::disabled;
}
const uint8_t *Network::GetIPAddress() const
{
return ipAddress;
}
void Network::SetHttpPort(uint16_t port)
{
httpPort = port;
}
uint16_t Network::GetHttpPort() const
{
return httpPort;
}
void Network::SetHostname(const char *name)
{
size_t i = 0;
while (*name && i < ARRAY_UPB(hostname))
{
char c = *name++;
if (c >= 'A' && c <= 'Z')
{
c += 'a' - 'A';
}
if ((c >= 'a' && c <= 'z') || (c >= '0' && c <= '9') || (c == '-') || (c == '_'))
{
hostname[i++] = c;
}
}
if (i)
{
hostname[i] = 0;
}
else
{
strcpy(hostname, HOSTNAME);
}
}
bool Network::Lock()
{
return true;
}
void Network::Unlock()
{
}
bool Network::InLwip() const
{
return false;
}
// This is called by the web server to get the next networking transaction.
//
// If conn is NoConnection, the transaction from the head of readyTransactions will be retrieved.
// If conn is not NoConnection, the first transaction with the matching connection will be returned.
//
// This method also ensures that a subsequent call with a null connection parameter will return exactly the same instance.
NetworkTransaction *Network::GetTransaction(Connection conn)
{
if (state == NetworkState::active)
{
if (conn != NoConnection)
{
NetworkTransaction *tr = conn->GetTransaction();
if (tr != nullptr && !tr->IsSending())
{
currentTransactionSocketNumber = conn->GetNumber();
return tr;
}
return nullptr;
}
size_t socketNum = currentTransactionSocketNumber;
do
{
NetworkTransaction *tr = sockets[socketNum].GetTransaction();
if (tr != nullptr && !tr->IsSending())
{
currentTransactionSocketNumber = socketNum;
return tr;
}
++socketNum;
if (socketNum == NumTcpSockets)
{
socketNum = 0;
}
} while (socketNum != currentTransactionSocketNumber);
}
return nullptr;
}
void Network::OpenDataPort(Port port)
{
sockets[FtpSocketNumber].Init(FtpSocketNumber, port);
sockets[FtpSocketNumber].Poll(false);
}
Port Network::GetDataPort() const
{
return sockets[FtpSocketNumber].GetLocalPort();
}
// Close FTP data port and purge associated PCB
void Network::CloseDataPort()
{
sockets[FtpSocketNumber].Close();
}
bool Network::AcquireFTPTransaction()
{
return AcquireTransaction(FtpSocketNumber);
}
bool Network::AcquireDataTransaction()
{
return AcquireTransaction(FtpDataSocketNumber);
}
bool Network::AcquireTelnetTransaction()
{
return AcquireTransaction(TelnetSocketNumber);
}
bool Network::AcquireTransaction(SocketNumber skt)
{
if ( sockets[skt].GetTransaction() != nullptr
&& sockets[skt].GetTransaction()->GetStatus() != TransactionStatus::sending
&& sockets[skt].GetTransaction()->GetStatus() != TransactionStatus::finished
)
{
currentTransactionSocketNumber = skt;
return true;
}
return false;
}
void Network::InitSockets()
{
for (SocketNumber skt = 0; skt < NumHttpSockets; ++skt)
{
sockets[skt].Init(skt, httpPort);
}
sockets[FtpSocketNumber].Init(FtpSocketNumber, FTP_PORT);
sockets[FtpDataSocketNumber].Init(FtpDataSocketNumber, 0); // FTP data port is allocated dynamically
sockets[TelnetSocketNumber].Init(TelnetSocketNumber, TELNET_PORT);
nextSocketToPoll = currentTransactionSocketNumber = 0;
}
void Network::TerminateSockets()
{
for (SocketNumber skt = 0; skt < NumTcpSockets; ++skt)
{
sockets[skt].Terminate();
}
}
void Network::Defer(NetworkTransaction *tr)
{
const Socket *skt = tr->GetConnection();
if (skt != nullptr && skt->GetNumber() == currentTransactionSocketNumber)
{
++currentTransactionSocketNumber;
if (currentTransactionSocketNumber == NumTcpSockets)
{
currentTransactionSocketNumber = 0;
}
}
}
/*static*/ Port Network::GetLocalPort(Connection conn) { return conn->GetLocalPort(); }
/*static*/ Port Network::GetRemotePort(Connection conn) { return conn->GetRemotePort(); }
/*static*/ uint32_t Network::GetRemoteIP(Connection conn) { return conn->GetRemoteIP(); }
/*static*/ bool Network::IsConnected(Connection conn) { return conn->IsConnected(); }
/*static*/ bool Network::IsTerminated(Connection conn) { return conn->IsTerminated(); }
/*static*/ void Network::Terminate(Connection conn) { conn->Terminate(); }
// End
|