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

Network.cpp « DuetEthernet « DuetNG « src - github.com/Duet3D/RepRapFirmware.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 094795d796e75f72f7dd1fae3a848b818eb764f7 (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
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
/*
 * Network.cpp
 *
 *  Created on: 13 Dec 2016
 *      Author: David
 */

#include "RepRapFirmware.h"
#include "compiler.h"
#include "Pins.h"
#include "Ethernet3/Ethernet3.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), responseCode(0), responseBody(nullptr), responseText(nullptr), responseFile(nullptr),
	httpPort(DEFAULT_HTTP_PORT),
	state(disabled), activated(false)
{
	SetIPAddress(IP_ADDRESS, NET_MASK, GATE_WAY);
	strcpy(hostname, HOSTNAME);
}

void Network::Init()
{
	// Ensure that the chip is in the reset state
	pinMode(EspResetPin, OUTPUT_LOW);
	state = disabled;
}

// This is called at the end of config.g processing.
// Start the network if it was enabled
void Network::Activate()
{
	activated = true;
	if (state == enabled)
	{
		Start();
	}
}

void Network::Exit()
{
	Stop();
}

void Network::Spin()
{
#if 1
#if 0
	if (state == starting)
	{
		const int rc = Ethernet.begin(platform->MACAddress(), 12000, 5000);		// for now we always use DHCP
		if (rc == 1)
		{
			state = running;
		}
	}
	else if (state == running)
#else
	if (state == starting || state == running)
#endif
	{
		// Check DHCP
		const int rc = Ethernet.maintain();
		if (state == starting && (rc == DHCP_CHECK_RENEW_OK || rc == DHCP_CHECK_REBIND_OK))
		{
			state = running;
		}
	}
#endif
	platform->ClassReport(longWait);
}

void Network::Diagnostics(MessageType mtype)
{

}

void Network::Start()
{
	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
	state = starting;
#if 0
	w5500.init();
	w5500.setMACAddress(platform->MACAddress());
#endif
	const int rc = Ethernet.begin(platform->MACAddress(), 12000, 5000);		// for now we always use DHCP
	if (rc == 1)
	{
		state = running;
	}
	else
	{
#if 1
		{
			uint8_t tmp = w5500.readPHYCFGR();
			uint8_t version = w5500.readVERSIONR();
			uint8_t macBuf[6];
			w5500.readSHAR(macBuf);
			platform->MessageF(GENERIC_MESSAGE, "Phy %02x ver %02x Mac %02x:%02x:%02x:%02x:%02x:%02x\n",
					tmp, version, macBuf[0], macBuf[1], macBuf[2], macBuf[3], macBuf[4], macBuf[5]);
		}
#endif
		platform->Message(GENERIC_MESSAGE, "Failed to start Ethernet interface\n");
		Stop();
	}
}

void Network::Stop()
{
	if (state != disabled)
	{
		Ethernet.stop();
		digitalWrite(EspResetPin, LOW);	// put the ESP back into reset
		state = disabled;
	}
}

void Network::Enable()
{
	if (state == disabled)
	{
		state = enabled;
		if (activated)
		{
			Start();
		}
	}
#if 1
	else if (state == starting)
	{
		uint8_t tmp = w5500.readPHYCFGR();
		uint8_t version = w5500.readVERSIONR();
		uint8_t macBuf[6];
		w5500.readSHAR(macBuf);
		platform->MessageF(GENERIC_MESSAGE, "Phy %02x ver %02x Mac %02x:%02x:%02x:%02x:%02x:%02x\n",
				tmp, version, macBuf[0], macBuf[1], macBuf[2], macBuf[3], macBuf[4], macBuf[5]);
	}
#endif
}

void Network::Disable()
{
	if (activated && state != disabled)
	{
		Stop();
		platform->Message(GENERIC_MESSAGE, "WiFi server stopped\n");
	}
}

bool Network::IsEnabled() const
{
	return state != disabled;
}

const uint8_t *Network::GetIPAddress() const
{
	if (state == running)
	{
		static IPAddress ip;
		ip = Ethernet.localIP();
		return ip.GetRawAddress();
	}
	else
	{
		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);
	}
}

// End