diff options
author | David Crocker <dcrocker@eschertech.com> | 2020-03-10 20:00:16 +0300 |
---|---|---|
committer | David Crocker <dcrocker@eschertech.com> | 2020-03-10 20:00:16 +0300 |
commit | 13e8c170918a1d4f692ac4f10ffeb75fa114c0c2 (patch) | |
tree | 4304cd50f748bcaf99c38876d2aafe9154e90405 /src/Networking | |
parent | 58b1f531d1f462e558e5940109b0157cc5fd5bad (diff) |
Implemented OM fields network.interfaces[].state and seqs.network
Diffstat (limited to 'src/Networking')
-rw-r--r-- | src/Networking/ESP8266WiFi/WiFiInterface.cpp | 58 | ||||
-rw-r--r-- | src/Networking/ESP8266WiFi/WiFiInterface.h | 11 | ||||
-rw-r--r-- | src/Networking/LwipEthernet/LwipEthernetInterface.cpp | 84 | ||||
-rw-r--r-- | src/Networking/LwipEthernet/LwipEthernetInterface.h | 11 | ||||
-rw-r--r-- | src/Networking/Network.h | 23 | ||||
-rw-r--r-- | src/Networking/NetworkInterface.cpp | 17 | ||||
-rw-r--r-- | src/Networking/NetworkInterface.h | 9 | ||||
-rw-r--r-- | src/Networking/W5500Ethernet/W5500Interface.cpp | 57 | ||||
-rw-r--r-- | src/Networking/W5500Ethernet/W5500Interface.h | 11 |
9 files changed, 131 insertions, 150 deletions
diff --git a/src/Networking/ESP8266WiFi/WiFiInterface.cpp b/src/Networking/ESP8266WiFi/WiFiInterface.cpp index 2278d261..f7026e2f 100644 --- a/src/Networking/ESP8266WiFi/WiFiInterface.cpp +++ b/src/Networking/ESP8266WiFi/WiFiInterface.cpp @@ -142,7 +142,7 @@ static inline void DisableEspInterrupt() noexcept // WiFi interface class WiFiInterface::WiFiInterface(Platform& p) noexcept : platform(p), uploader(nullptr), espWaitingTask(nullptr), ftpDataPort(0), closeDataPort(false), - state(NetworkState::disabled), requestedMode(WiFiState::disabled), currentMode(WiFiState::disabled), activated(false), + requestedMode(WiFiState::disabled), currentMode(WiFiState::disabled), activated(false), espStatusChanged(false), spiTxUnderruns(0), spiRxOverruns(0), serialRunning(false), debugMessageChars(0) { wifiInterface = this; @@ -179,11 +179,12 @@ constexpr ObjectModelTableEntry WiFiInterface::objectModelTable[] = { "firmwareVersion", OBJECT_MODEL_FUNC(self->wiFiServerVersion), ObjectModelEntryFlags::none }, { "gateway", OBJECT_MODEL_FUNC(self->gateway), ObjectModelEntryFlags::none }, { "mac", OBJECT_MODEL_FUNC(self->macAddress), ObjectModelEntryFlags::none }, + { "state", OBJECT_MODEL_FUNC(self->GetStateName()), ObjectModelEntryFlags::none }, { "subnet", OBJECT_MODEL_FUNC(self->netmask), ObjectModelEntryFlags::none }, { "type", OBJECT_MODEL_FUNC_NOSELF("wifi"), ObjectModelEntryFlags::none }, }; -constexpr uint8_t WiFiInterface::objectModelTableDescriptor[] = { 1, 6 }; +constexpr uint8_t WiFiInterface::objectModelTableDescriptor[] = { 1, 7 }; DEFINE_GET_OBJECT_MODEL_TABLE(WiFiInterface) @@ -219,7 +220,7 @@ GCodeResult WiFiInterface::EnableProtocol(NetworkProtocol protocol, int port, in const Port portToUse = (port < 0) ? DefaultPortNumbers[protocol] : port; MutexLocker lock(interfaceMutex); - if (portToUse != portNumbers[protocol] && state == NetworkState::active) + if (portToUse != portNumbers[protocol] && GetState() == NetworkState::active) { // We need to shut down and restart the protocol if it is active because the port number has changed ShutdownProtocol(protocol); @@ -229,7 +230,7 @@ GCodeResult WiFiInterface::EnableProtocol(NetworkProtocol protocol, int port, in if (!protocolEnabled[protocol]) { protocolEnabled[protocol] = true; - if (state == NetworkState::active) + if (GetState() == NetworkState::active) { StartProtocol(protocol); // mDNS announcement is done by the WiFi Server firmware @@ -249,7 +250,7 @@ GCodeResult WiFiInterface::DisableProtocol(NetworkProtocol protocol, const Strin { MutexLocker lock(interfaceMutex); - if (state == NetworkState::active) + if (GetState() == NetworkState::active) { ShutdownProtocol(protocol); } @@ -383,7 +384,7 @@ void WiFiInterface::Exit() noexcept // Get the network state into the reply buffer, returning true if there is some sort of error GCodeResult WiFiInterface::GetNetworkState(const StringRef& reply) noexcept { - switch (state) + switch (GetState()) { case NetworkState::disabled: reply.copy("WiFi module is disabled"); @@ -465,13 +466,13 @@ void WiFiInterface::Start() noexcept transferAlreadyPendingCount = readyTimeoutCount = responseTimeoutCount = 0; lastTickMillis = millis(); - state = NetworkState::starting1; + SetState(NetworkState::starting1); } // Stop the ESP void WiFiInterface::Stop() noexcept { - if (state != NetworkState::disabled) + if (GetState() != NetworkState::disabled) { MutexLocker lock(interfaceMutex); @@ -485,7 +486,7 @@ void WiFiInterface::Stop() noexcept spi_dma_check_rx_complete(); spi_dma_disable(); - state = NetworkState::disabled; + SetState(NetworkState::disabled); currentMode = WiFiState::disabled; } } @@ -493,7 +494,7 @@ void WiFiInterface::Stop() noexcept void WiFiInterface::Spin() noexcept { // Main state machine. - switch (state) + switch (GetState()) { case NetworkState::starting1: { @@ -502,7 +503,7 @@ void WiFiInterface::Spin() noexcept if (now - lastTickMillis >= WiFiStartupMillis) { lastTickMillis = now; - state = NetworkState::starting2; + SetState(NetworkState::starting2); } } break; @@ -533,7 +534,7 @@ void WiFiInterface::Spin() noexcept reprap.GetPlatform().Message(NetworkInfoMessage, "Error: Could not set WiFi hostname\n"); } - state = NetworkState::active; + SetState(NetworkState::active); espStatusChanged = true; // make sure we fetch the current state and enable the ESP interrupt } else @@ -592,7 +593,7 @@ void WiFiInterface::Spin() noexcept if (rslt >= 0) { - state = NetworkState::changingMode; + SetState(NetworkState::changingMode); } else { @@ -657,7 +658,7 @@ void WiFiInterface::Spin() noexcept case WiFiState::connected: case WiFiState::runningAsAccessPoint: - state = NetworkState::active; + SetState(NetworkState::active); { // Get our IP address, this needs to be correct for FTP to work Receiver<NetworkStatusResponse> status; @@ -680,7 +681,7 @@ void WiFiInterface::Spin() noexcept { requestedMode = currentMode; // don't keep repeating the request if it failed and it wasn't a connect request } - state = NetworkState::active; + SetState(NetworkState::active); platform.MessageF(NetworkInfoMessage, "WiFi module is %s\n", TranslateWiFiState(currentMode)); break; } @@ -731,7 +732,6 @@ const char* WiFiInterface::TranslateEspResetReason(uint32_t reason) noexcept // Mapping from known ESP reset codes to reasons static const char * const resetReasonTexts[] = { - "Power on", "Hardware watchdog", "Exception", "Software watchdog", @@ -745,23 +745,9 @@ const char* WiFiInterface::TranslateEspResetReason(uint32_t reason) noexcept : "Unknown"; } -const char* WiFiInterface::TranslateNetworkState() const noexcept -{ - switch (state) - { - case NetworkState::disabled: return "disabled"; - case NetworkState::starting1: - case NetworkState::starting2: return "starting"; - case NetworkState::active: return "running"; - case NetworkState::changingMode: return "changing mode"; - default: return "unknown"; - } -} - void WiFiInterface::Diagnostics(MessageType mtype) noexcept { - platform.Message(mtype, "- WiFi -\n"); - platform.MessageF(mtype, "Network state is %s\n", TranslateNetworkState()); + platform.MessageF(mtype, "- WiFi -\nNetwork state is %s\n", GetStateName()); platform.MessageF(mtype, "WiFi module is %s\n", TranslateWiFiState(currentMode)); platform.MessageF(mtype, "Failed messages: pending %u, notready %u, noresp %u\n", transferAlreadyPendingCount, readyTimeoutCount, responseTimeoutCount); @@ -770,7 +756,7 @@ void WiFiInterface::Diagnostics(MessageType mtype) noexcept platform.MessageF(mtype, "SPI underruns %u, overruns %u\n", spiTxUnderruns, spiRxOverruns); #endif - if (state != NetworkState::disabled && state != NetworkState::starting1 && state != NetworkState::starting2) + if (GetState() != NetworkState::disabled && GetState() != NetworkState::starting1 && GetState() != NetworkState::starting2) { Receiver<NetworkStatusResponse> status; if (SendCommand(NetworkCommand::networkGetStatus, 0, 0, nullptr, 0, status) > 0) @@ -838,7 +824,7 @@ GCodeResult WiFiInterface::EnableInterface(int mode, const StringRef& ssid, cons { // Shut down WiFi module completely requestedMode = modeRequested; - if (state != NetworkState::disabled) + if (GetState() != NetworkState::disabled) { Stop(); platform.Message(GenericMessage, "WiFi module stopped\n"); @@ -846,7 +832,7 @@ GCodeResult WiFiInterface::EnableInterface(int mode, const StringRef& ssid, cons } else { - if (state == NetworkState::disabled) + if (GetState() == NetworkState::disabled) { requestedMode = modeRequested; Start(); @@ -1110,7 +1096,7 @@ GCodeResult WiFiInterface::HandleWiFiCode(int mcode, GCodeBuffer &gb, const Stri void WiFiInterface::UpdateHostname(const char *hostname) noexcept { // Update the hostname if possible - if (state == NetworkState::active) + if (GetState() == NetworkState::active) { if (SendCommand(NetworkCommand::networkSetHostName, 0, 0, hostname, HostNameLength, nullptr, 0) != ResponseEmpty) { @@ -1551,7 +1537,7 @@ void WiFiInterface::SetupSpi() noexcept // Send a command to the ESP and get the result int32_t WiFiInterface::SendCommand(NetworkCommand cmd, SocketNumber socketNum, uint8_t flags, const void *dataOut, size_t dataOutLength, void* dataIn, size_t dataInLength) noexcept { - if (state == NetworkState::disabled) + if (GetState() == NetworkState::disabled) { if (reprap.Debug(moduleNetwork)) { diff --git a/src/Networking/ESP8266WiFi/WiFiInterface.h b/src/Networking/ESP8266WiFi/WiFiInterface.h index 0f88f81f..7180c033 100644 --- a/src/Networking/ESP8266WiFi/WiFiInterface.h +++ b/src/Networking/ESP8266WiFi/WiFiInterface.h @@ -72,7 +72,6 @@ public: void ResetWiFi() noexcept; void ResetWiFiForUpload(bool external) noexcept; const char *GetWiFiServerVersion() const noexcept { return wiFiServerVersion; } - const char* TranslateNetworkState() const noexcept; static const char* TranslateWiFiState(WiFiState w) noexcept; void SpiInterrupt() noexcept; void EspRequestsTransfer() noexcept; @@ -82,15 +81,6 @@ protected: DECLARE_OBJECT_MODEL 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() noexcept; void TerminateSockets() noexcept; void TerminateSockets(Port port) noexcept; @@ -136,7 +126,6 @@ private: bool closeDataPort; bool protocolEnabled[NumProtocols]; // whether each protocol is enabled - NetworkState state; WiFiState requestedMode; WiFiState currentMode; bool activated; diff --git a/src/Networking/LwipEthernet/LwipEthernetInterface.cpp b/src/Networking/LwipEthernet/LwipEthernetInterface.cpp index 662d46a7..edefa6e8 100644 --- a/src/Networking/LwipEthernet/LwipEthernetInterface.cpp +++ b/src/Networking/LwipEthernet/LwipEthernetInterface.cpp @@ -41,7 +41,6 @@ const char * const MdnsServiceStrings[NumProtocols] = { "_http", "_ftp", "_telne const char * const MdnsTxtRecords[2] = { "product=" FIRMWARE_NAME, "version=" VERSION }; const unsigned int MdnsTtl = 10 * 60; // same value as on the Duet 0.6/0.8.5 - /*-----------------------------------------------------------------------------------*/ static LwipEthernetInterface *ethernetInterface; @@ -51,8 +50,6 @@ Mutex lwipMutex; extern "C" { - - // Task function to keep the GMAC and LwIP running void DoEthernetTask() { @@ -60,7 +57,6 @@ extern "C" } // Callback functions for LWIP (may be called from ISR) - static err_t conn_accept(void *arg, tcp_pcb *pcb, err_t err) { LWIP_UNUSED_ARG(arg); @@ -80,8 +76,8 @@ extern "C" /*-----------------------------------------------------------------------------------*/ -LwipEthernetInterface::LwipEthernetInterface(Platform& p) noexcept : platform(p), closeDataPort(false), state(NetworkState::disabled), - activated(false), initialised(false), usingDhcp(false) +LwipEthernetInterface::LwipEthernetInterface(Platform& p) noexcept + : platform(p), closeDataPort(false), activated(false), initialised(false), usingDhcp(false) { ethernetInterface = this; @@ -111,14 +107,15 @@ LwipEthernetInterface::LwipEthernetInterface(Platform& p) noexcept : platform(p) constexpr ObjectModelTableEntry LwipEthernetInterface::objectModelTable[] = { // These entries must be in alphabetical order - { "actualIP", OBJECT_MODEL_FUNC(self->ipAddress), ObjectModelEntryFlags::none }, - { "gateway", OBJECT_MODEL_FUNC(self->gateway), ObjectModelEntryFlags::none }, - { "mac", OBJECT_MODEL_FUNC(self->macAddress), ObjectModelEntryFlags::none }, - { "subnet", OBJECT_MODEL_FUNC(self->netmask), ObjectModelEntryFlags::none }, - { "type", OBJECT_MODEL_FUNC_NOSELF("ethernet"), ObjectModelEntryFlags::none }, + { "actualIP", OBJECT_MODEL_FUNC(self->ipAddress), ObjectModelEntryFlags::none }, + { "gateway", OBJECT_MODEL_FUNC(self->gateway), ObjectModelEntryFlags::none }, + { "mac", OBJECT_MODEL_FUNC(self->macAddress), ObjectModelEntryFlags::none }, + { "state", OBJECT_MODEL_FUNC(self->GetStateName()), ObjectModelEntryFlags::none }, + { "subnet", OBJECT_MODEL_FUNC(self->netmask), ObjectModelEntryFlags::none }, + { "type", OBJECT_MODEL_FUNC_NOSELF("ethernet"), ObjectModelEntryFlags::verbose }, }; -constexpr uint8_t LwipEthernetInterface::objectModelTableDescriptor[] = { 1, 5 }; +constexpr uint8_t LwipEthernetInterface::objectModelTableDescriptor[] = { 1, 6 }; DEFINE_GET_OBJECT_MODEL_TABLE(LwipEthernetInterface) @@ -151,7 +148,7 @@ GCodeResult LwipEthernetInterface::EnableProtocol(NetworkProtocol protocol, int if (protocol < NumProtocols) { const Port portToUse = (port < 0) ? DefaultPortNumbers[protocol] : port; - if (portToUse != portNumbers[protocol] && state == NetworkState::active) + if (portToUse != portNumbers[protocol] && GetState() == NetworkState::active) { // We need to shut down and restart the protocol if it is active because the port number has changed ShutdownProtocol(protocol); @@ -162,7 +159,7 @@ GCodeResult LwipEthernetInterface::EnableProtocol(NetworkProtocol protocol, int { // Enable the corresponding socket protocolEnabled[protocol] = true; - if (state == NetworkState::active) + if (GetState() == NetworkState::active) { StartProtocol(protocol); RebuildMdnsServices(); @@ -180,7 +177,7 @@ GCodeResult LwipEthernetInterface::DisableProtocol(NetworkProtocol protocol, con { if (protocol < NumProtocols) { - if (state == NetworkState::active) + if (GetState() == NetworkState::active) { ShutdownProtocol(protocol); } @@ -227,13 +224,6 @@ void LwipEthernetInterface::StartProtocol(NetworkProtocol protocol) noexcept void LwipEthernetInterface::ShutdownProtocol(NetworkProtocol protocol) noexcept { -#if 0 // chrishamm: Also see Network::Stop - for (NetworkResponder* r = responders; r != nullptr; r = r->GetNext()) - { - r->Terminate(protocol); - } -#endif - switch(protocol) { case HttpProtocol: @@ -293,7 +283,7 @@ void LwipEthernetInterface::Activate() noexcept if (!activated) { activated = true; - if (state == NetworkState::enabled) + if (GetState() == NetworkState::enabled) { Start(); } @@ -352,20 +342,20 @@ void LwipEthernetInterface::Start() noexcept initialised = true; } - state = NetworkState::establishingLink; + SetState(NetworkState::establishingLink); } // Stop the network void LwipEthernetInterface::Stop() noexcept { - if (state != NetworkState::disabled) + if (GetState() != NetworkState::disabled) { netif_set_down(&gs_net_if); #if defined(DUET3) - pinMode(PhyResetPin, OUTPUT_LOW); // hold the Ethernet Phy chip in reset + pinMode(PhyResetPin, OUTPUT_LOW); // hold the Ethernet Phy chip in reset #endif - state = NetworkState::disabled; + SetState(NetworkState::disabled); } } @@ -374,7 +364,7 @@ void LwipEthernetInterface::Spin() noexcept { MutexLocker lock(lwipMutex); - switch(state) + switch(GetState()) { case NetworkState::enabled: case NetworkState::disabled: @@ -389,7 +379,7 @@ void LwipEthernetInterface::Spin() noexcept if (usingDhcp) { // IP address is all zeros, so use DHCP - state = NetworkState::obtainingIP; + SetState(NetworkState::obtainingIP); // debugPrintf("Link established, getting IP address\n"); IPAddress nullAddress; ethernet_set_configuration(nullAddress, nullAddress, nullAddress); @@ -398,7 +388,7 @@ void LwipEthernetInterface::Spin() noexcept else { // Using static IP address - state = NetworkState::connected; + SetState(NetworkState::connected); // debugPrintf("Link established, network running\n"); ethernet_set_configuration(platform.GetIPAddress(), platform.NetMask(), platform.GateWay()); } @@ -416,7 +406,7 @@ void LwipEthernetInterface::Spin() noexcept if (!ipAddress.IsNull()) { // Notify the mDNS responder about this - state = NetworkState::connected; + SetState(NetworkState::connected); // debugPrintf("IP address obtained, network running\n"); } } @@ -424,7 +414,7 @@ void LwipEthernetInterface::Spin() noexcept { // debugPrintf("Lost phy link\n"); TerminateSockets(); - state = NetworkState::establishingLink; + SetState(NetworkState::establishingLink); } break; @@ -433,7 +423,8 @@ void LwipEthernetInterface::Spin() noexcept RebuildMdnsServices(); ethernet_get_ipaddress(ipAddress, netmask, gateway); platform.MessageF(NetworkInfoMessage, "Ethernet running, IP address = %s\n", IP4String(ipAddress).c_str()); - state = NetworkState::active; + SetState(NetworkState::active); + reprap.NetworkUpdated(); break; case NetworkState::active: @@ -463,7 +454,7 @@ void LwipEthernetInterface::Spin() noexcept { // debugPrintf("Lost phy link\n"); TerminateSockets(); - state = NetworkState::establishingLink; + SetState(NetworkState::establishingLink); } break; } @@ -471,10 +462,9 @@ void LwipEthernetInterface::Spin() noexcept void LwipEthernetInterface::Diagnostics(MessageType mtype) noexcept { - platform.Message(mtype, "- Ethernet -\n"); - platform.MessageF(mtype, "State: %d\n", (int)state); - platform.MessageF(mtype, "Error counts: %u %u %u %u %u\n", rxErrorCount, rxBuffersNotFullyPopulatedCount, txErrorCount, txBufferNotFreeCount, txBufferTooShortCount); - platform.Message(mtype, "Socket states:"); + platform.MessageF(mtype, "- Ethernet -\nState: %s\n", GetStateName()); + platform.MessageF(mtype, "Error counts: %u %u %u %u %u\nSocket states:", + rxErrorCount, rxBuffersNotFullyPopulatedCount, txErrorCount, txBufferNotFreeCount, txBufferTooShortCount); for (LwipSocket *s : sockets) { platform.MessageF(mtype, " %d", s->GetState()); @@ -495,20 +485,20 @@ GCodeResult LwipEthernetInterface::EnableInterface(int mode, const StringRef& ss { if (!activated) { - state = (mode == 0) ? NetworkState::disabled : NetworkState::enabled; + SetState((mode == 0) ? NetworkState::disabled : NetworkState::enabled); } else if (mode == 0) { - if (state != NetworkState::disabled) + if (GetState() != NetworkState::disabled) { Stop(); platform.Message(NetworkInfoMessage, "Network stopped\n"); } } - else if (state == NetworkState::disabled) + else if (GetState() == NetworkState::disabled) { - state = NetworkState::enabled; + SetState(NetworkState::enabled); Start(); } return GCodeResult::ok; @@ -516,7 +506,7 @@ GCodeResult LwipEthernetInterface::EnableInterface(int mode, const StringRef& ss int LwipEthernetInterface::EnableState() const noexcept { - return (state == NetworkState::disabled) ? 0 : 1; + return (GetState() == NetworkState::disabled) ? 0 : 1; } bool LwipEthernetInterface::ConnectionEstablished(tcp_pcb *pcb) noexcept @@ -541,7 +531,7 @@ IPAddress LwipEthernetInterface::GetIPAddress() const noexcept void LwipEthernetInterface::SetIPAddress(IPAddress p_ipAddress, IPAddress p_netmask, IPAddress p_gateway) noexcept { - if (state == NetworkState::obtainingIP || state == NetworkState::active) + if (GetState() == NetworkState::obtainingIP || GetState() == NetworkState::active) { const bool wantDhcp = p_ipAddress.IsNull(); if (wantDhcp) @@ -549,7 +539,7 @@ void LwipEthernetInterface::SetIPAddress(IPAddress p_ipAddress, IPAddress p_netm // Acquire dynamic IP address if (!usingDhcp) { - state = NetworkState::obtainingIP; + SetState(NetworkState::obtainingIP); IPAddress nullAddress; ethernet_set_configuration(nullAddress, nullAddress, nullAddress); dhcp_start(&gs_net_if); @@ -561,11 +551,11 @@ void LwipEthernetInterface::SetIPAddress(IPAddress p_ipAddress, IPAddress p_netm // Set static IP address if (usingDhcp) { - if (state == NetworkState::obtainingIP) + if (GetState() == NetworkState::obtainingIP) { dhcp_stop(&gs_net_if); } - state = NetworkState::active; + SetState(NetworkState::active); usingDhcp = false; } diff --git a/src/Networking/LwipEthernet/LwipEthernetInterface.h b/src/Networking/LwipEthernet/LwipEthernetInterface.h index 04c57947..57594139 100644 --- a/src/Networking/LwipEthernet/LwipEthernetInterface.h +++ b/src/Networking/LwipEthernet/LwipEthernetInterface.h @@ -61,16 +61,6 @@ protected: DECLARE_OBJECT_MODEL private: - enum class NetworkState - { - disabled, // Network disabled - enabled, // Network enabled but not started yet - establishingLink, // starting up, waiting for link - obtainingIP, // link established, waiting for DHCP - connected, // just established a connection - active // network running - }; - void Start() noexcept; void Stop() noexcept; void InitSockets() noexcept; @@ -97,7 +87,6 @@ private: bool closeDataPort; tcp_pcb *listeningPcbs[NumTcpPorts]; - NetworkState state; bool activated; bool initialised; bool usingDhcp; diff --git a/src/Networking/Network.h b/src/Networking/Network.h index fe7e568b..afead1c6 100644 --- a/src/Networking/Network.h +++ b/src/Networking/Network.h @@ -9,11 +9,12 @@ #define SRC_NETWORK_NETWORK_H_ #include "NetworkDefs.h" -#include "RepRapFirmware.h" -#include "MessageType.h" -#include "GCodes/GCodeResult.h" -#include "RTOSIface/RTOSIface.h" -#include "ObjectModel/ObjectModel.h" +#include <RepRapFirmware.h> +#include <MessageType.h> +#include <GCodes/GCodeResult.h> +#include <RTOSIface/RTOSIface.h> +#include <ObjectModel/ObjectModel.h> +#include <General/NamedEnum.h> #if defined(DUET3_V03) const size_t NumNetworkInterfaces = 2; @@ -49,6 +50,18 @@ class Socket; class WiFiInterface; class WifiFirmwareUploader; +NamedEnum(NetworkState, uint8_t, + disabled, // Network disabled + enabled, // Network enabled but not started yet + starting1, // starting up (used by WiFi networking) + starting2, // starting up (used by WiFi networking) + changingMode, // running and in the process of switching between modes (used by WiFi networking) + establishingLink, // starting up, waiting for link + obtainingIP, // link established, waiting for DHCP + connected, // just established a connection + active // network running +); + // The main network class that drives the network. class Network INHERIT_OBJECT_MODEL { diff --git a/src/Networking/NetworkInterface.cpp b/src/Networking/NetworkInterface.cpp new file mode 100644 index 00000000..f6c1655c --- /dev/null +++ b/src/Networking/NetworkInterface.cpp @@ -0,0 +1,17 @@ +/* + * NetworkInterface.cpp + * + * Created on: 10 Mar 2020 + * Author: David + */ + +#include "NetworkInterface.h" +#include <RepRap.h> + +void NetworkInterface::SetState(NetworkState::RawType newState) noexcept +{ + state = newState; + reprap.NetworkUpdated(); +} + +// End diff --git a/src/Networking/NetworkInterface.h b/src/Networking/NetworkInterface.h index 739f19c2..dcf531a1 100644 --- a/src/Networking/NetworkInterface.h +++ b/src/Networking/NetworkInterface.h @@ -14,7 +14,7 @@ class NetworkInterface INHERIT_OBJECT_MODEL { public: - NetworkInterface() { } + NetworkInterface() : state(NetworkState::disabled) { } NetworkInterface(const NetworkInterface&) = delete; virtual void Init() noexcept = 0; @@ -47,8 +47,15 @@ public: Mutex interfaceMutex; // mutex to protect against multiple tasks using the same interface concurrently. Public so that sockets can lock it. protected: + NetworkState::RawType GetState() const noexcept { return state.RawValue(); } + void SetState(NetworkState::RawType newState) noexcept; + const char *GetStateName() const noexcept { return state.ToString(); } + Port portNumbers[NumProtocols]; // port number used for each protocol bool protocolEnabled[NumProtocols]; // whether each protocol is enabled + +private: + NetworkState state; }; #endif /* SRC_NETWORKING_NETWORKINTERFACE_H_ */ diff --git a/src/Networking/W5500Ethernet/W5500Interface.cpp b/src/Networking/W5500Ethernet/W5500Interface.cpp index 2299d72b..78aec934 100644 --- a/src/Networking/W5500Ethernet/W5500Interface.cpp +++ b/src/Networking/W5500Ethernet/W5500Interface.cpp @@ -19,7 +19,7 @@ #include "General/IP4String.h" W5500Interface::W5500Interface(Platform& p) noexcept - : platform(p), lastTickMillis(0), ftpDataSocket(0), state(NetworkState::disabled), activated(false) + : platform(p), lastTickMillis(0), ftpDataSocket(0), activated(false) { // Create the sockets for (W5500Socket*& skt : sockets) @@ -49,14 +49,15 @@ W5500Interface::W5500Interface(Platform& p) noexcept constexpr ObjectModelTableEntry W5500Interface::objectModelTable[] = { // These entries must be in alphabetical order - { "actualIP", OBJECT_MODEL_FUNC(self->ipAddress), ObjectModelEntryFlags::none }, - { "gateway", OBJECT_MODEL_FUNC(self->gateway), ObjectModelEntryFlags::none }, - { "mac", OBJECT_MODEL_FUNC(self->macAddress), ObjectModelEntryFlags::none }, - { "subnet", OBJECT_MODEL_FUNC(self->netmask), ObjectModelEntryFlags::none }, - { "type", OBJECT_MODEL_FUNC_NOSELF("ethernet"), ObjectModelEntryFlags::none }, + { "actualIP", OBJECT_MODEL_FUNC(self->ipAddress), ObjectModelEntryFlags::none }, + { "gateway", OBJECT_MODEL_FUNC(self->gateway), ObjectModelEntryFlags::none }, + { "mac", OBJECT_MODEL_FUNC(self->macAddress), ObjectModelEntryFlags::none }, + { "state", OBJECT_MODEL_FUNC(self->GetStateName()), ObjectModelEntryFlags::none }, + { "subnet", OBJECT_MODEL_FUNC(self->netmask), ObjectModelEntryFlags::none }, + { "type", OBJECT_MODEL_FUNC_NOSELF("ethernet"), ObjectModelEntryFlags::none }, }; -constexpr uint8_t W5500Interface::objectModelTableDescriptor[] = { 1, 5 }; +constexpr uint8_t W5500Interface::objectModelTableDescriptor[] = { 1, 6 }; DEFINE_GET_OBJECT_MODEL_TABLE(W5500Interface) @@ -87,7 +88,7 @@ GCodeResult W5500Interface::EnableProtocol(NetworkProtocol protocol, int port, i MutexLocker lock(interfaceMutex); const Port portToUse = (port < 0) ? DefaultPortNumbers[protocol] : port; - if (portToUse != portNumbers[protocol] && state == NetworkState::active) + if (portToUse != portNumbers[protocol] && GetState() == NetworkState::active) { // We need to shut down and restart the protocol if it is active because the port number has changed protocolEnabled[protocol] = false; @@ -97,10 +98,10 @@ GCodeResult W5500Interface::EnableProtocol(NetworkProtocol protocol, int port, i if (!protocolEnabled[protocol]) { protocolEnabled[protocol] = true; - if (state == NetworkState::active) + if (GetState() == NetworkState::active) { ResetSockets(); - if (state == NetworkState::active) + if (GetState() == NetworkState::active) { mdnsResponder->Announce(); } @@ -125,7 +126,7 @@ GCodeResult W5500Interface::DisableProtocol(NetworkProtocol protocol, const Stri { MutexLocker lock(interfaceMutex); - if (state == NetworkState::active) + if (GetState() == NetworkState::active) { ResetSockets(); } @@ -168,7 +169,7 @@ void W5500Interface::Activate() noexcept if (!activated) { activated = true; - if (state == NetworkState::enabled) + if (GetState() == NetworkState::enabled) { Start(); } @@ -229,13 +230,13 @@ void W5500Interface::Start() noexcept setPHYCFGR(PHYCFGR_OPMD | PHYCFGR_OPMDC_ALLA | ~PHYCFGR_RST); // remove the reset - state = NetworkState::establishingLink; + SetState(NetworkState::establishingLink); } // Stop the network void W5500Interface::Stop() noexcept { - if (state != NetworkState::disabled) + if (GetState() != NetworkState::disabled) { MutexLocker lock(interfaceMutex); @@ -244,14 +245,14 @@ void W5500Interface::Stop() noexcept DHCP_stop(); } digitalWrite(W5500ResetPin, false); // put the W5500 back into reset - state = NetworkState::disabled; + SetState(NetworkState::disabled); } } // Main spin loop void W5500Interface::Spin() noexcept { - switch(state) + switch (GetState()) { case NetworkState::enabled: case NetworkState::disabled: @@ -272,12 +273,12 @@ void W5500Interface::Spin() noexcept // debugPrintf("Link established, getting IP address\n"); DHCP_init(DhcpSocketNumber, platform.Random(), reprap.GetNetwork().GetHostname()); lastTickMillis = millis(); - state = NetworkState::obtainingIP; + SetState(NetworkState::obtainingIP); } else { // debugPrintf("Link established, network running\n"); - state = NetworkState::connected; + SetState(NetworkState::connected); } } } @@ -300,7 +301,7 @@ void W5500Interface::Spin() noexcept { // debugPrintf("IP address obtained, network running\n"); getSIPR(ipAddress); - state = NetworkState::connected; + SetState(NetworkState::connected); } } else @@ -308,7 +309,7 @@ void W5500Interface::Spin() noexcept // debugPrintf("Lost phy link\n"); DHCP_stop(); TerminateSockets(); - state = NetworkState::establishingLink; + SetState(NetworkState::establishingLink); } } break; @@ -317,7 +318,7 @@ void W5500Interface::Spin() noexcept InitSockets(); platform.MessageF(NetworkInfoMessage, "Network running, IP address = %s\n", IP4String(ipAddress).c_str()); mdnsResponder->Announce(); - state = NetworkState::active; + SetState(NetworkState::active); break; case NetworkState::active: @@ -367,7 +368,7 @@ void W5500Interface::Spin() noexcept DHCP_stop(); } TerminateSockets(); - state = NetworkState::establishingLink; + SetState(NetworkState::establishingLink); } } break; @@ -383,7 +384,7 @@ void W5500Interface::Diagnostics(MessageType mtype) noexcept } const char * const linkSpeed = ((phycfgr & 1) == 0) ? "down" : ((phycfgr & 2) != 0) ? "100Mbps" : "10Mbps"; const char * const linkDuplex = ((phycfgr & 1) == 0) ? "" : ((phycfgr & 4) != 0) ? " full duplex" : " half duplex"; - platform.MessageF(mtype, "Interface state %d, link %s%s\n", (int)state, linkSpeed, linkDuplex); + platform.MessageF(mtype, "Interface state %s, link %s%s\n", GetStateName(), linkSpeed, linkDuplex); } // Enable or disable the network @@ -391,20 +392,20 @@ GCodeResult W5500Interface::EnableInterface(int mode, const StringRef& ssid, con { if (!activated) { - state = (mode <= 0) ? NetworkState::disabled : NetworkState::enabled; + SetState((mode <= 0) ? NetworkState::disabled : NetworkState::enabled); } else if (mode <= 0) { - if (state != NetworkState::disabled) + if (GetState() != NetworkState::disabled) { Stop(); platform.Message(NetworkInfoMessage, "Network stopped\n"); } } - else if (state == NetworkState::disabled) + else if (GetState() == NetworkState::disabled) { - state = NetworkState::enabled; + SetState(NetworkState::enabled); Start(); } return GCodeResult::ok; @@ -412,7 +413,7 @@ GCodeResult W5500Interface::EnableInterface(int mode, const StringRef& ssid, con int W5500Interface::EnableState() const noexcept { - return (state == NetworkState::disabled) ? 0 : 1; + return (GetState() == NetworkState::disabled) ? 0 : 1; } void W5500Interface::UpdateHostname(const char *name) noexcept /*override*/ diff --git a/src/Networking/W5500Ethernet/W5500Interface.h b/src/Networking/W5500Ethernet/W5500Interface.h index 4f83bbfb..3fe7a8f8 100644 --- a/src/Networking/W5500Ethernet/W5500Interface.h +++ b/src/Networking/W5500Ethernet/W5500Interface.h @@ -63,16 +63,6 @@ protected: DECLARE_OBJECT_MODEL private: - enum class NetworkState - { - disabled, // Network disabled - enabled, // Network enabled but not started yet - establishingLink, // starting up, waiting for link - obtainingIP, // link established, waiting for DHCP - connected, // just established a connection - active // network running - }; - void Start() noexcept; void Stop() noexcept; void InitSockets() noexcept; @@ -95,7 +85,6 @@ private: Port portNumbers[NumProtocols]; // port number used for each protocol bool protocolEnabled[NumProtocols]; // whether each protocol is enabled - NetworkState state; bool activated; bool usingDhcp; |