Welcome to mirror list, hosted at ThFree Co, Russian Federation.

WiFiInterface.h « ESP8266WiFi « Networking « src - github.com/Duet3D/RepRapFirmware.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 5776f1e616dc37fe30a615b77a2dafd37faa0258 (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
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
/*
 * WiFiInterface.h
 *
 *  Created on: 27 Nov 2017
 *      Authors: Christian and David
 */

#ifndef SRC_NETWORKING_WIFIINTERFACE_H_
#define SRC_NETWORKING_WIFIINTERFACE_H_

#include <RepRapFirmware.h>

#if HAS_WIFI_NETWORKING

#include <Networking/NetworkDefs.h>
#include <Networking/NetworkInterface.h>
#include <MessageFormats.h>

// Forward declarations
class WiFiSocket;
class WifiFirmwareUploader;

// Class to allow us to receive some data allowing for some extra bytes being stored by the DMAC
template<class T> class Receiver
{
public:
	void *DmaPointer() noexcept { return &object; }
	size_t Size() const noexcept { return sizeof(T); }
	T& Value() noexcept { return object; }
private:
	T object;
	uint32_t padding;
};

// The main network class that drives the network.
class WiFiInterface : public NetworkInterface
{
public:
	friend class WiFiSocket;

	WiFiInterface(Platform& p) noexcept;

	void Init() noexcept override;
	void Activate() noexcept override;
	void Exit() noexcept override;
	void Spin() noexcept override;
	void Diagnostics(MessageType mtype) noexcept override;
	void Start() noexcept;
	void Stop() noexcept;

	GCodeResult EnableInterface(int mode, const StringRef& ssid, const StringRef& reply) noexcept override;			// enable or disable the network
	GCodeResult EnableProtocol(NetworkProtocol protocol, int port, int secure, const StringRef& reply) noexcept override;
	GCodeResult DisableProtocol(NetworkProtocol protocol, const StringRef& reply) noexcept override;
	GCodeResult ReportProtocols(const StringRef& reply) const noexcept override;

	GCodeResult GetNetworkState(const StringRef& reply) noexcept override;
	int EnableState() const noexcept override;
	bool IsWiFiInterface() const noexcept override { return true; }

	void UpdateHostname(const char *hostname) noexcept override;

	IPAddress GetIPAddress() const noexcept override { return ipAddress; }
	IPAddress GetNetmask() const noexcept override { return netmask; }
	IPAddress GetGateway() const noexcept override { return gateway; }
	bool UsingDhcp() const noexcept override { return usingDhcp; }
	void SetIPAddress(IPAddress p_ip, IPAddress p_netmask, IPAddress p_gateway) noexcept override;
	GCodeResult SetMacAddress(const MacAddress& mac, const StringRef& reply) noexcept override;
	const MacAddress& GetMacAddress() const noexcept override { return macAddress; }

	void OpenDataPort(TcpPort port) noexcept override;
	void TerminateDataPort() noexcept override;

	// The remaining functions are specific to the WiFi version
	GCodeResult HandleWiFiCode(int mcode, GCodeBuffer &gb, const StringRef& reply, OutputBuffer*& longReply) THROWS(GCodeException);
	WifiFirmwareUploader *GetWifiUploader() const noexcept { return uploader; }
	void StartWiFi() noexcept;
	void ResetWiFi() noexcept;
	void ResetWiFiForUpload(bool external) noexcept;
	const char *GetWiFiServerVersion() const noexcept { return wiFiServerVersion; }
	static const char* TranslateWiFiState(WiFiState w) noexcept;
	void SpiInterrupt() noexcept;
	void EspRequestsTransfer() noexcept;
	void UpdateSocketStatus(uint16_t connectedSockets, uint16_t otherEndClosedSockets) noexcept;

protected:
	DECLARE_OBJECT_MODEL

private:
	void InitSockets() noexcept;
	void TerminateSockets() noexcept;
	void TerminateSockets(TcpPort port) noexcept;
	void StopListening(TcpPort port) noexcept;

	void StartProtocol(NetworkProtocol protocol) noexcept
	pre(protocol < NumProtocols);

	void ShutdownProtocol(NetworkProtocol protocol) noexcept
	pre(protocol < NumProtocols);

	void ReportOneProtocol(NetworkProtocol protocol, const StringRef& reply) const noexcept
	pre(protocol < NumProtocols);

	NetworkProtocol GetProtocolByLocalPort(TcpPort port) const noexcept;

	void SetupSpi() noexcept;

	int32_t SendCommand(NetworkCommand cmd, SocketNumber socket, uint8_t flags, uint32_t param32, const void *dataOut, size_t dataOutLength, void* dataIn, size_t dataInLength) noexcept;

	template<class T> int32_t SendCommand(NetworkCommand cmd, SocketNumber socket, uint8_t flags, const void *dataOut, size_t dataOutLength, Receiver<T>& recvr) noexcept
	{
		return SendCommand(cmd, socket, flags, 0, dataOut, dataOutLength, recvr.DmaPointer(), recvr.Size());
	}

	void SendListenCommand(TcpPort port, NetworkProtocol protocol, unsigned int maxConnections) noexcept;
	void GetNewStatus() noexcept;
	void spi_slave_dma_setup(uint32_t dataOutSize, uint32_t dataInSize) noexcept;

	static const char* TranslateWiFiResponse(int32_t response) noexcept;
	static const char* TranslateEspResetReason(uint32_t reason) noexcept;

	Platform& platform;
	uint32_t lastTickMillis;
	bool lastDataReadyPinState;
	uint8_t risingEdges;

	struct MessageBufferOut
	{
		MessageHeaderSamToEsp hdr;
		uint8_t data[MaxDataLength];	// data to send
	};

	struct alignas(16) MessageBufferIn
	{
		MessageHeaderEspToSam hdr;
		uint8_t data[MaxDataLength];	// data to send
	};

	MessageBufferOut *bufferOut;
	MessageBufferIn *bufferIn;

	WifiFirmwareUploader *uploader;
	TaskHandle espWaitingTask;

	WiFiSocket *sockets[NumWiFiTcpSockets];
	size_t currentSocket;

	TcpPort portNumbers[NumProtocols];				// port number used for each protocol
	TcpPort ftpDataPort;
	bool closeDataPort;
	bool protocolEnabled[NumProtocols];				// whether each protocol is enabled

	WiFiState requestedMode;
	WiFiState currentMode;
	bool activated;
	volatile bool espStatusChanged;

	IPAddress ipAddress;
	IPAddress netmask;
	IPAddress gateway;
	MacAddress macAddress;
	char requestedSsid[SsidLength + 1];
	char actualSsid[SsidLength + 1];

	unsigned int spiTxUnderruns;
	unsigned int spiRxOverruns;
	unsigned int reconnectCount;
	unsigned int transferAlreadyPendingCount;
	unsigned int readyTimeoutCount;
	unsigned int responseTimeoutCount;

	char wiFiServerVersion[16];

	bool usingDhcp = true;

	// For processing debug messages from the WiFi module
	bool serialRunning;
	bool debugPrintPending;
	char debugMessageBuffer[200];
	size_t debugMessageChars;
};

#endif	// HAS_WIFI_NETWORKING

#endif