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
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
|
/*
* 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),
freeTransactions(nullptr), readyTransactions(nullptr), writingTransactions(nullptr),
httpPort(DefaultHttpPort), state(NetworkState::disabled), activated(false)
{
SetIPAddress(DefaultIpAddress, DefaultNetMask, DefaultGateway);
strcpy(hostname, HOSTNAME);
}
void Network::Init()
{
// Ensure that the chip is in the reset state
pinMode(EspResetPin, OUTPUT_LOW);
state = NetworkState::disabled;
longWait = platform->Time();
lastTickMillis = millis();
InitSockets();
}
// 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();
}
void Network::Spin()
{
switch(state)
{
case NetworkState::enabled:
case NetworkState::disabled:
// Nothing to do
break;
case NetworkState::establishingLink:
if (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 (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
{
DHCP_stop();
TerminateSockets();
state = NetworkState::establishingLink;
}
break;
case NetworkState::active:
if (wizphy_getphylink() == PHY_LINK_ON)
{
if (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)
{
getSIPR(ipAddress);
}
}
sockets[nextSocketToPoll].Poll();
++nextSocketToPoll;
if (nextSocketToPoll == NumTcpSockets)
{
nextSocketToPoll = 0;
}
#if 0
// See if we can send anything
NetworkTransaction *transaction = writingTransactions;
if (transaction != nullptr /*&& sendingConnection == nullptr*/ )
{
if (transaction->GetNext() != nullptr)
{
// Data is supposed to be sent and the last packet has been acknowledged.
// Rotate the transactions so every client is served even while multiple files are sent
NetworkTransaction *next = transaction->GetNext();
writingTransactions = next;
AppendTransaction(&writingTransactions, transaction);
transaction = next;
}
if (transaction->Send())
{
// This transaction can be released, do this here
writingTransactions = transaction->GetNext();
PrependTransaction(&freeTransactions, transaction);
// If there is more data to write on this connection, do it sometime soon
NetworkTransaction *nextWrite = transaction->GetNextWrite();
if (nextWrite != nullptr)
{
PrependTransaction(&writingTransactions, nextWrite);
}
}
}
#endif
}
else
{
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, "WiFi server 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()
{
//TODO
return true;
}
void Network::Unlock()
{
//TODO
}
bool Network::InLwip() const
{
//TODO
return false;
}
// This is called by the web server to get the next networking transaction.
//
// If cs is NULL, the transaction from the head of readyTransactions will be retrieved.
// If cs is not NULL, the first transaction with the matching connection will be returned.
//
// This method also ensures that the retrieved transaction is moved to the first item of
// readyTransactions, so that a subsequent call with a NULL cs parameter will return exactly
// the same instance.
NetworkTransaction *Network::GetTransaction(Connection conn)
{
#if 1
return nullptr;
#else
// See if there is any transaction at all
NetworkTransaction *transaction = readyTransactions;
if (transaction == nullptr)
{
return nullptr;
}
// If no specific connection is specified or if the first item already matches the
// connection we are looking for, just return it
if (cs == nullptr || transaction->GetConnection() == cs)
{
return transaction;
}
// We are looking for a specific transaction, but it's not the first item.
// Search for it and move it to the head of readyTransactions
NetworkTransaction *previous = transaction;
for(NetworkTransaction *item = transaction->next; item != nullptr; item = item->next)
{
if (item->GetConnection() == cs)
{
previous->next = item->next;
item->next = readyTransactions;
readyTransactions = item;
return item;
}
previous = item;
}
// We failed to find a valid transaction for the given connection
return nullptr;
#endif
}
void Network::AppendTransaction(NetworkTransaction* * list, NetworkTransaction *r)
{
r->next = nullptr;
while (*list != nullptr)
{
list = &((*list)->next);
}
*list = r;
}
void Network::PrependTransaction(NetworkTransaction* * list, NetworkTransaction *r)
{
r->next = *list;
*list = r;
}
void Network::OpenDataPort(uint16_t port)
{
//TODO
#if 0
closingDataPort = false;
tcp_pcb* pcb = tcp_new();
tcp_bind(pcb, IP_ADDR_ANY, port);
ftp_pasv_pcb = tcp_listen(pcb);
tcp_accept(ftp_pasv_pcb, conn_accept);
#endif
}
uint16_t Network::GetDataPort() const
{
#if 1
return 0; //TODO
#else
return (closingDataPort || (ftp_pasv_pcb == nullptr) ? 0 : ftp_pasv_pcb->local_port);
#endif
}
// Close FTP data port and purge associated PCB
void Network::CloseDataPort()
{
//TODO
#if 0
// See if it's already being closed
if (closingDataPort)
{
return;
}
closingDataPort = true;
// Close remote connection of our data port or do it as soon as the last packet has been sent
if (dataCs != nullptr)
{
NetworkTransaction *mySendingTransaction = dataCs->sendingTransaction;
if (mySendingTransaction != nullptr)
{
mySendingTransaction->Close();
return;
}
}
// We can close it now, so do it here
if (ftp_pasv_pcb != nullptr)
{
tcp_accept(ftp_pasv_pcb, nullptr);
tcp_close(ftp_pasv_pcb);
ftp_pasv_pcb = nullptr;
}
closingDataPort = false;
#endif
}
// These methods keep track of our connections in case we need to send to one of them
void Network::SaveDataConnection()
{
// dataCs = readyTransactions->cs->GetNumber();
}
void Network::SaveFTPConnection()
{
// ftpCs = readyTransactions->cs->GetNumber();
}
void Network::SaveTelnetConnection()
{
// telnetCs = readyTransactions->cs->GetNumber();
}
bool Network::AcquireFTPTransaction()
{
#if 1
return false; //TODO
#else
return AcquireTransaction(ftpCs);
#endif
}
bool Network::AcquireDataTransaction()
{
#if 1
return false; //TODO
#else
return AcquireTransaction(dataCs);
#endif
}
bool Network::AcquireTelnetTransaction()
{
#if 1
return false; //TODO
#else
return AcquireTransaction(telnetCs);
#endif
}
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);
nextSocketToProcess = nextSocketToPoll = 0;
}
void Network::TerminateSockets()
{
for (SocketNumber skt = 0; skt < NumTcpSockets; ++skt)
{
sockets[skt].Terminate();
}
}
/*static*/ uint16_t Network::GetLocalPort(Connection conn) { return conn->GetLocalPort(); }
/*static*/ uint16_t 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
|