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: a77573a26f70673f324c485b9a7a6bb2cef3ba75 (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
/*
 * WiFiInterface.h
 *
 *  Created on: 27 Nov 2017
 *      Authors: Christian and David
 */

#ifndef SRC_NETWORKING_WIFIINTERFACE_H_
#define SRC_NETWORKING_WIFIINTERFACE_H_

#include "Networking/NetworkDefs.h"
#include "RepRapFirmware.h"
#include "MessageType.h"
#include "Networking/NetworkInterface.h"
#include "GCodes/GCodeResult.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() { return &object; }
	size_t Size() const { return sizeof(T); }
	T& Value() { 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);

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

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

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

	void UpdateHostname(const char *hostname) override;
	const uint8_t *GetIPAddress() const override { return ipAddress; }
	void SetIPAddress(const uint8_t ipAddress[], const uint8_t netmask[], const uint8_t gateway[]) override;
	void SetMacAddress(const uint8_t mac[]) override;
	const uint8_t *GetMacAddress() const override { return macAddress; }

	void OpenDataPort(Port port) override;
	void TerminateDataPort() override;

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

private:
	enum class NetworkState
	{
		disabled,					// WiFi module disabled
		starting1,					// starting up
		starting2,					// starting up
		active,						// running, but not necessarily in the requested mode
		changingMode,				// running and in the process of switching between modes
	};

	void InitSockets();
	void TerminateSockets();
	void TerminateSockets(Port port);
	void StopListening(Port port);

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

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

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

	NetworkProtocol GetProtocolByLocalPort(Port port) const;

	void SetupSpi();

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

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

	void SendListenCommand(Port port, NetworkProtocol protocol, unsigned int maxConnections);
	void GetNewStatus();

	static const char* TranslateEspResetReason(uint32_t reason);

	Platform& platform;
	uint32_t lastTickMillis;

	WifiFirmwareUploader *uploader;

	WiFiSocket *sockets[NumWiFiTcpSockets];
	size_t currentSocket;

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

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

	uint8_t ipAddress[4];
	uint8_t netmask[4];
	uint8_t gateway[4];
	uint8_t macAddress[6];
	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];

	// For processing debug messages from the WiFi module
	bool serialRunning;
	bool debugPrintPending;
	String<100> debugMessageBuffer;
};

#endif