Skip to content

Commit

Permalink
Removed unused client code from Duet 2 builds to save flash space
Browse files Browse the repository at this point in the history
  • Loading branch information
dc42 committed Sep 18, 2024
1 parent bc56400 commit e7085e4
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 25 deletions.
44 changes: 22 additions & 22 deletions src/Networking/Network.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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);
}

Expand All @@ -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())
Expand All @@ -291,57 +296,58 @@ 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);
}

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;
}
Expand Down Expand Up @@ -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))
{
Expand Down Expand Up @@ -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
{
Expand Down Expand Up @@ -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))
Expand All @@ -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);
Expand Down
7 changes: 4 additions & 3 deletions src/Networking/Network.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down
7 changes: 7 additions & 0 deletions src/Networking/NetworkClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
*/

#include "NetworkClient.h"

#if HAS_CLIENTS

#include "Socket.h"
#include <Platform/Platform.h>

Expand Down Expand Up @@ -106,3 +109,7 @@ void NetworkClient::ConnectionLost() noexcept
}

NetworkClient *NetworkClient::clients = nullptr;

#endif

// End
4 changes: 4 additions & 0 deletions src/Networking/NetworkClient.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
#include <RepRapFirmware.h>
#include "NetworkResponder.h"

#if HAS_CLIENTS

// Forward declarations
class NetworkClient;
class NetworkInterface;
Expand Down Expand Up @@ -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_ */
3 changes: 3 additions & 0 deletions src/Networking/NetworkDefs.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@

#include <General/IPAddress.h>

#define HAS_RESPONDERS (SUPPORT_HTTP || SUPPORT_FTP || SUPPORT_TELNET)
#define HAS_CLIENTS (SUPPORT_MQTT)

class NetworkBuffer;

typedef uint8_t SocketNumber;
Expand Down

0 comments on commit e7085e4

Please sign in to comment.