blob: ef51dc7288819caca862cd9d6060c1d12ba79c20 (
plain)
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
|
/****************************************************************************************************
RepRapFirmware - Network: RepRapPro Ormerod with Duet controller
Separated out from Platform.h by dc42 and extended by zpl
****************************************************************************************************/
#ifndef NETWORK_H
#define NETWORK_H
#include "NetworkDefs.h"
#include "RepRapFirmware.h"
#include "MessageType.h"
#include "Socket.h"
// We have 8 sockets available on the W5500.
const size_t NumHttpSockets = 4; // sockets 0-3 are for HTTP
const SocketNumber FtpSocketNumber = 4;
const SocketNumber FtpDataSocketNumber = 5; // TODO can we allocate this dynamically when required, to allow more http sockets most of the time?
const SocketNumber TelnetSocketNumber = 6;
const size_t NumTcpSockets = 7;
const SocketNumber DhcpSocketNumber = 7; // TODO can we allocate this dynamically when required, to allow more http sockets most of the time?
class Platform;
// The main network class that drives the network.
class Network
{
public:
const uint8_t *GetIPAddress() const;
void SetIPAddress(const uint8_t p_ipAddress[], const uint8_t p_netmask[], const uint8_t p_gateway[]);
Network(Platform* p);
void Init();
void Activate();
void Exit();
void Spin(bool full);
void Diagnostics(MessageType mtype);
void Start();
void Stop();
bool Lock();
void Unlock();
bool InLwip() const;
void Enable();
void Disable();
bool IsEnabled() const;
void SetHttpPort(Port port);
Port GetHttpPort() const;
void SetHostname(const char *name);
// Interfaces for the Webserver
NetworkTransaction *GetTransaction(Connection conn = NoConnection);
void OpenDataPort(Port port);
Port GetDataPort() const;
void CloseDataPort();
void SaveDataConnection() {}
void SaveFTPConnection() {}
void SaveTelnetConnection() {}
bool AcquireFTPTransaction() { return AcquireTransaction(FtpSocketNumber); }
bool AcquireDataTransaction() { return AcquireTransaction(FtpDataSocketNumber); }
bool AcquireTelnetTransaction() { return AcquireTransaction(TelnetSocketNumber); }
void Defer(NetworkTransaction *tr);
static Port GetLocalPort(Connection conn);
static Port GetRemotePort(Connection conn);
static uint32_t GetRemoteIP(Connection conn);
static bool IsConnected(Connection conn);
static bool IsTerminated(Connection conn);
static void Terminate(Connection conn);
private:
enum class NetworkState
{
disabled, // WiFi not active
enabled, // WiFi enabled but not started yet
establishingLink, // starting up (waiting for initialisation)
obtainingIP,
active
};
void InitSockets();
void TerminateSockets();
bool AcquireTransaction(size_t socketNumber);
Platform *platform;
float longWait;
uint32_t lastTickMillis;
Socket sockets[NumTcpSockets];
size_t nextSocketToPoll; // next TCP socket number to poll for read/write operations
size_t currentTransactionSocketNumber; // the socket number of the last transaction we passed to the web server
Port httpPort;
uint8_t ipAddress[4];
uint8_t netmask[4];
uint8_t gateway[4];
char hostname[16]; // Limit DHCP hostname to 15 characters + terminating 0
NetworkState state;
bool activated;
bool usingDhcp;
};
#endif
|