From e7085e439c0edf9ba3d80c8920d726166ff68de3 Mon Sep 17 00:00:00 2001 From: David Crocker Date: Wed, 18 Sep 2024 13:22:58 +0100 Subject: [PATCH] Removed unused client code from Duet 2 builds to save flash space --- src/Networking/Network.cpp | 44 ++++++++++++++++---------------- src/Networking/Network.h | 7 ++--- src/Networking/NetworkClient.cpp | 7 +++++ src/Networking/NetworkClient.h | 4 +++ src/Networking/NetworkDefs.h | 3 +++ 5 files changed, 40 insertions(+), 25 deletions(-) diff --git a/src/Networking/Network.cpp b/src/Networking/Network.cpp index df7940b92..7fbf14432 100644 --- a/src/Networking/Network.cpp +++ b/src/Networking/Network.cpp @@ -86,7 +86,10 @@ void MacAddress::SetFromBytes(const uint8_t mb[6]) noexcept // Network members Network::Network(Platform& p) noexcept : platform(p) #if HAS_RESPONDERS - , responders(nullptr), clients(nullptr), nextResponderToPoll(nullptr) + , responders(nullptr), nextResponderToPoll(nullptr) +#endif +#if HAS_CLIENTS + , clients(nullptr) #endif { #if HAS_NETWORKING @@ -239,6 +242,7 @@ GCodeResult Network::EnableProtocol(unsigned int interface, NetworkProtocol prot #if HAS_NETWORKING if (interface < GetNumNetworkInterfaces()) { +# if HAS_CLIENTS bool hasFree = false; bool client = false; // Check if there are enough clients to accomodate enabling the protocol. Check if @@ -262,7 +266,7 @@ GCodeResult Network::EnableProtocol(unsigned int interface, NetworkProtocol prot reply.printf("No more instances for client protocol.\n"); return GCodeResult::error; } - +# endif return interfaces[interface]->EnableProtocol(protocol, port, ip, secure, reply); } @@ -281,6 +285,7 @@ GCodeResult Network::DisableProtocol(unsigned int interface, NetworkProtocol pro { bool client = false; +# if HAS_CLIENTS // Check if a client handles the protocol. If so, termination is handled // by the client itself, after attempting to disconnect gracefully. for (NetworkClient *c = clients; c != nullptr; c = c->GetNext()) @@ -291,13 +296,14 @@ GCodeResult Network::DisableProtocol(unsigned int interface, NetworkProtocol pro break; } } +# endif NetworkInterface * const iface = interfaces[interface]; const GCodeResult ret = iface->DisableProtocol(protocol, reply, !client); if (ret == GCodeResult::ok) { -#if HAS_RESPONDERS +# if HAS_RESPONDERS if (!client) { TerminateResponders(iface, protocol); @@ -305,43 +311,43 @@ GCodeResult Network::DisableProtocol(unsigned int interface, NetworkProtocol pro switch (protocol) { -#if SUPPORT_HTTP +# if SUPPORT_HTTP case HttpProtocol: HttpResponder::DisableInterface(iface); // free up output buffers etc. break; -#endif +# endif -#if SUPPORT_FTP +# if SUPPORT_FTP case FtpProtocol: // TODO the following isn't quite right, because we shouldn't free up output buffers if another network interface is still serving this protocol. FtpResponder::Disable(); break; -#endif +# endif -#if SUPPORT_TELNET +# if SUPPORT_TELNET case TelnetProtocol: // TODO the following isn't quite right, because we shouldn't free up output buffers if another network interface is still serving this protocol. TelnetResponder::Disable(); break; -#endif +# endif -#if SUPPORT_MULTICAST_DISCOVERY +# if SUPPORT_MULTICAST_DISCOVERY // TODO the following isn't quite right, because we shouldn't free up output buffers if another network interface is still serving this protocol. case MulticastDiscoveryProtocol: break; -#endif +# endif -#if SUPPORT_MQTT +# if SUPPORT_MQTT case MqttProtocol: // TODO the following isn't quite right, because we shouldn't free up output buffers if another network interface is still serving this protocol. MqttClient::Disable(); break; -#endif +# endif default: break; } -#endif // HAS_RESPONDERS +# endif // HAS_RESPONDERS } return ret; } @@ -740,11 +746,8 @@ bool Network::UsingDhcp(unsigned int interface) const noexcept return interface < GetNumNetworkInterfaces() && interfaces[interface]->UsingDhcp(); } -#endif - void Network::SetHostname(const char *name) noexcept { -#if HAS_NETWORKING size_t i = 0; while (*name && i < ARRAY_UPB(hostname)) { @@ -776,11 +779,8 @@ void Network::SetHostname(const char *name) noexcept iface->UpdateHostname(hostname); } } -#endif } -#if HAS_NETWORKING - // Net the MAC address. Pass -1 as the interface number to set the default MAC address for interfaces that don't have one. GCodeResult Network::SetMacAddress(unsigned int interface, const MacAddress& mac, const StringRef& reply) noexcept { @@ -820,7 +820,7 @@ bool Network::FindResponder(Socket *skt, NetworkProtocol protocol) noexcept bool Network::StartClient(NetworkInterface *interface, NetworkProtocol protocol) noexcept { -#if HAS_RESPONDERS +#if HAS_CLIENTS for (NetworkClient *c = clients; c != nullptr; c = c->GetNext()) { if (c->Start(protocol, interface)) @@ -834,7 +834,7 @@ bool Network::StartClient(NetworkInterface *interface, NetworkProtocol protocol) void Network::StopClient(NetworkInterface *interface, NetworkProtocol protocol) noexcept { -#if HAS_RESPONDERS +#if HAS_CLIENTS for (NetworkClient *c = clients; c != nullptr; c = c->GetNext()) { c->Stop(protocol, interface); diff --git a/src/Networking/Network.h b/src/Networking/Network.h index 150f494fd..7a815928c 100644 --- a/src/Networking/Network.h +++ b/src/Networking/Network.h @@ -38,8 +38,6 @@ const size_t NumTelnetResponders = 1; // the number of concurrent Telnet session const size_t NumFtpResponders = 1; // the number of concurrent FTP sessions we support -#define HAS_RESPONDERS (SUPPORT_HTTP || SUPPORT_FTP || SUPPORT_TELNET) - // Forward declarations class NetworkResponder; class NetworkClient; @@ -146,10 +144,13 @@ class Network INHERIT_OBJECT_MODEL #if HAS_RESPONDERS NetworkResponder *responders; - NetworkClient *clients; NetworkResponder *nextResponderToPoll; #endif +#if HAS_CLIENTS + NetworkClient *clients; +#endif + #if SUPPORT_HTTP Mutex httpMutex; #endif diff --git a/src/Networking/NetworkClient.cpp b/src/Networking/NetworkClient.cpp index de09ec3ee..7108ee808 100644 --- a/src/Networking/NetworkClient.cpp +++ b/src/Networking/NetworkClient.cpp @@ -6,6 +6,9 @@ */ #include "NetworkClient.h" + +#if HAS_CLIENTS + #include "Socket.h" #include @@ -106,3 +109,7 @@ void NetworkClient::ConnectionLost() noexcept } NetworkClient *NetworkClient::clients = nullptr; + +#endif + +// End diff --git a/src/Networking/NetworkClient.h b/src/Networking/NetworkClient.h index 7331a8e4b..d3c5d9713 100644 --- a/src/Networking/NetworkClient.h +++ b/src/Networking/NetworkClient.h @@ -11,6 +11,8 @@ #include #include "NetworkResponder.h" +#if HAS_CLIENTS + // Forward declarations class NetworkClient; class NetworkInterface; @@ -51,4 +53,6 @@ class NetworkClient : public NetworkResponder static NetworkClient *clients; // Head of the list of all network clients }; +#endif + #endif /* SRC_NETWORKING_NETWORKCLIENT_H_ */ diff --git a/src/Networking/NetworkDefs.h b/src/Networking/NetworkDefs.h index 788bfd05b..240965b53 100644 --- a/src/Networking/NetworkDefs.h +++ b/src/Networking/NetworkDefs.h @@ -10,6 +10,9 @@ #include +#define HAS_RESPONDERS (SUPPORT_HTTP || SUPPORT_FTP || SUPPORT_TELNET) +#define HAS_CLIENTS (SUPPORT_MQTT) + class NetworkBuffer; typedef uint8_t SocketNumber;