diff options
author | David Crocker <dcrocker@eschertech.com> | 2022-07-12 19:31:59 +0300 |
---|---|---|
committer | David Crocker <dcrocker@eschertech.com> | 2022-07-12 19:31:59 +0300 |
commit | 66af1b7897c61ba68c13e6d57bff51e1dfe81a79 (patch) | |
tree | 98f68eca610afd7621a0f68a81a40cdcbf7098ba | |
parent | 9e4445cac182e1521939456a68621b788701a485 (diff) |
More work on multicast discovery protocol
-rw-r--r-- | src/Networking/LwipEthernet/LwipEthernetInterface.cpp | 23 | ||||
-rw-r--r-- | src/Networking/MulticastResponder.cpp | 64 | ||||
-rw-r--r-- | src/Networking/MulticastResponder.h | 21 | ||||
-rw-r--r-- | src/Networking/Network.cpp | 3 | ||||
-rw-r--r-- | src/Networking/NetworkDefs.h | 27 | ||||
-rw-r--r-- | src/Networking/NetworkResponder.h | 4 |
6 files changed, 105 insertions, 37 deletions
diff --git a/src/Networking/LwipEthernet/LwipEthernetInterface.cpp b/src/Networking/LwipEthernet/LwipEthernetInterface.cpp index 9f35a201..63268c44 100644 --- a/src/Networking/LwipEthernet/LwipEthernetInterface.cpp +++ b/src/Networking/LwipEthernet/LwipEthernetInterface.cpp @@ -20,6 +20,10 @@ #include <Networking/HttpResponder.h> #include <Networking/FtpResponder.h> #include <Networking/TelnetResponder.h> +#if SUPPORT_MULTICAST_DISCOVERY +# include <Networking/MulticastResponder.h> +#endif + #include <General/IP4String.h> #include <Version.h> // version is reported by MDNS #include "GMAC/ethernet_sam.h" @@ -178,6 +182,7 @@ GCodeResult LwipEthernetInterface::EnableProtocol(NetworkProtocol protocol, int RebuildMdnsServices(); } } + ReportOneProtocol(protocol, reply); return GCodeResult::ok; } @@ -205,7 +210,11 @@ GCodeResult LwipEthernetInterface::DisableProtocol(NetworkProtocol protocol, con void LwipEthernetInterface::StartProtocol(NetworkProtocol protocol) noexcept { - if (listeningPcbs[protocol] == nullptr) + if ( listeningPcbs[protocol] == nullptr +#if SUPPORT_MULTICAST_DISCOVERY + && protocol != MulticastDiscoveryProtocol +#endif + ) { tcp_pcb *pcb = tcp_new(); if (pcb == nullptr) @@ -245,6 +254,12 @@ void LwipEthernetInterface::StartProtocol(NetworkProtocol protocol) noexcept sockets[TelnetSocketNumber]->Init(TelnetSocketNumber, portNumbers[protocol], protocol); break; +#if SUPPORT_MULTICAST_DISCOVERY + case MulticastDiscoveryProtocol: + MulticastResponder::Start(portNumbers[protocol]); + break; +#endif + default: break; } @@ -270,6 +285,12 @@ void LwipEthernetInterface::ShutdownProtocol(NetworkProtocol protocol) noexcept sockets[TelnetSocketNumber]->TerminateAndDisable(); break; +#if SUPPORT_MULTICAST_DISCOVERY + case MulticastDiscoveryProtocol: + MulticastResponder::Stop(); + break; +#endif + default: break; } diff --git a/src/Networking/MulticastResponder.cpp b/src/Networking/MulticastResponder.cpp index 8e6dae47..69077353 100644 --- a/src/Networking/MulticastResponder.cpp +++ b/src/Networking/MulticastResponder.cpp @@ -6,43 +6,81 @@ */ #include "MulticastResponder.h" +#include <Platform/RepRap.h> +#include <Platform/Platform.h> + +extern "C" { +#include "LwipEthernet/Lwip/src/include/lwip/udp.h" +} #if SUPPORT_MULTICAST_DISCOVERY -MulticastResponder::MulticastResponder(NetworkResponder *n) noexcept : NetworkResponder(n) +static udp_pcb *pcb = nullptr; + +void MulticastResponder::Init() noexcept { - // TODO Auto-generated constructor stub + // TODO Auto-generated stub } // Do some work, returning true if we did anything significant -bool MulticastResponder::Spin() noexcept +void MulticastResponder::Spin() noexcept { //TODO - return false; } -// Terminate the responder if it is serving the specified protocol on the specified interface -void MulticastResponder::Terminate(NetworkProtocol protocol, NetworkInterface *interface) noexcept +void MulticastResponder::Diagnostics(MessageType mtype) noexcept { //TODO } -void MulticastResponder::Diagnostics(MessageType mtype) const noexcept +void MulticastResponder::Start(TcpPort port) noexcept { - //TODO + if (pcb == nullptr) + { + pcb = udp_new(); + if (pcb == nullptr) + { + reprap.GetPlatform().Message(ErrorMessage, "unable to allocate a pcb\n"); + } + else + { + udp_bind(pcb, IP_ADDR_ANY, port); + //TODO +#if 0 + pcb = tcp_listen(pcb); + if (pcb == nullptr) + { + platform.Message(ErrorMessage, "tcp_listen call failed\n"); + } + else + { + listeningPcbs[protocol] = pcb; + tcp_accept(listeningPcbs[protocol], conn_accept); + } +#endif + } + } } -// Offer the responder a UDP packet, return true if it was accepted -bool MulticastResponder::AcceptUdp( /*TODO parameters*/ ) noexcept +void MulticastResponder::Stop() noexcept { //TODO - return false; } -void MulticastResponder::Disable() noexcept +#if 0 +// Offer the responder a UDP packet, return true if it was accepted +bool MulticastResponder::AcceptUdp(struct udp_pcb *pcb, struct pbuf *p, const ip_addr_t *addr, uint16_t port) noexcept { - //TODO + debugPrintf("Rx UDP: addr %u.%u.%u.%u port %u data", (unsigned int)((addr->addr >> 24) & 0xFF)); + for (size_t i = 0; i < qq; ++i) + { + debugPrintf(" %02x", qq); + } + debugPrintf("\n"); +//TODO + return true; } +#endif #endif diff --git a/src/Networking/MulticastResponder.h b/src/Networking/MulticastResponder.h index 774b409a..2d57581e 100644 --- a/src/Networking/MulticastResponder.h +++ b/src/Networking/MulticastResponder.h @@ -8,22 +8,19 @@ #ifndef SRC_NETWORKING_MULTICASTRESPONDER_H_ #define SRC_NETWORKING_MULTICASTRESPONDER_H_ -#include "NetworkResponder.h" +#include <RepRapFirmware.h> +#include <NetworkDefs.h> #if SUPPORT_MULTICAST_DISCOVERY -class MulticastResponder : public NetworkResponder +namespace MulticastResponder { -public: - MulticastResponder(NetworkResponder *n) noexcept; - bool Spin() noexcept override; // do some work, returning true if we did anything significant - bool Accept(Socket *s, NetworkProtocol protocol) noexcept override { return false; } // ask the responder to accept this connection, returns true if it did - void Terminate(NetworkProtocol protocol, NetworkInterface *interface) noexcept override; // terminate the responder if it is serving the specified protocol on the specified interface - void Diagnostics(MessageType mtype) const noexcept override; - virtual bool AcceptUdp(/*TODO parameters*/) noexcept override; // offer the responder a UDP packet, return true if it was accepted - - static void Disable() noexcept; -}; + void Init() noexcept; + void Spin() noexcept; + void Start(TcpPort port) noexcept; + void Stop() noexcept; + void Diagnostics(MessageType mtype) noexcept; +} #endif diff --git a/src/Networking/Network.cpp b/src/Networking/Network.cpp index a72cbba9..b8a4e7f1 100644 --- a/src/Networking/Network.cpp +++ b/src/Networking/Network.cpp @@ -255,7 +255,6 @@ GCodeResult Network::DisableProtocol(unsigned int interface, NetworkProtocol pro #if SUPPORT_MULTICAST_DISCOVERY case MulticastDiscoveryProtocol: - MulticastResponder::Disable(); break; #endif @@ -445,7 +444,7 @@ void Network::Activate() noexcept # endif #if SUPPORT_MULTICAST_DISCOVERY - responders = new MulticastResponder(responders); + MulticastResponder::Init(); #endif // Finally, create the network task diff --git a/src/Networking/NetworkDefs.h b/src/Networking/NetworkDefs.h index 0b290a22..2ef74aa4 100644 --- a/src/Networking/NetworkDefs.h +++ b/src/Networking/NetworkDefs.h @@ -36,21 +36,38 @@ constexpr IPAddress DefaultIpAddress; // will be initialised to 0 by construc constexpr IPAddress DefaultNetMask(0x00FFFFFF); // equivalent to 255.255.255.0. Use constexpr constructor to avoid it being allocated in RAM. constexpr IPAddress DefaultGateway; // will be initialised to 0 by constructor +constexpr size_t NumTcpProtocols = 3; + #if SUPPORT_MULTICAST_DISCOVERY -constexpr size_t NumProtocols = 4; // number of network protocols we support, not counting FtpDataProtocol, MdnsProtocol or AnyProtocol +constexpr size_t NumProtocols = NumTcpProtocols + 1; // number of network protocols we support, not counting FtpDataProtocol, MdnsProtocol or AnyProtocol #else -constexpr size_t NumProtocols = 3; // number of network protocols we support, not counting FtpDataProtocol, MdnsProtocol or AnyProtocol +constexpr size_t NumProtocols = NumTcpProtocols; // number of network protocols we support, not counting FtpDataProtocol, MdnsProtocol or AnyProtocol #endif constexpr NetworkProtocol HttpProtocol = 0, FtpProtocol = 1, TelnetProtocol = 2, MulticastDiscoveryProtocol = 3, FtpDataProtocol = 3, MdnsProtocol = 4, AnyProtocol = 255; -constexpr size_t NumTcpPorts = NumProtocols + 1; +constexpr size_t NumTcpPorts = NumTcpProtocols + 1; constexpr TcpPort DefaultHttpPort = 80; constexpr TcpPort DefaultFtpPort = 21; constexpr TcpPort DefaultTelnetPort = 23; +#if SUPPORT_MULTICAST_DISCOVERY +constexpr TcpPort DefaultMulticastDiscoveryPort = 10002; // this is actually a UDP port +#endif -constexpr TcpPort DefaultPortNumbers[NumProtocols] = { DefaultHttpPort, DefaultFtpPort, DefaultTelnetPort }; -constexpr const char *_ecv_array ProtocolNames[NumProtocols] = { "HTTP", "FTP", "TELNET" }; +constexpr TcpPort DefaultPortNumbers[NumProtocols] = +{ + DefaultHttpPort, DefaultFtpPort, DefaultTelnetPort, +#if SUPPORT_MULTICAST_DISCOVERY + DefaultMulticastDiscoveryPort +#endif +}; +constexpr const char *_ecv_array ProtocolNames[NumProtocols] = +{ + "HTTP", "FTP", "TELNET", +#if SUPPORT_MULTICAST_DISCOVERY + "Multicast Discovery" +#endif +}; constexpr uint8_t MdnsMacAddress[6] = { 0x01, 0x00, 0x5E, 0x00, 0x00, 0xFB }; constexpr uint8_t MdnsIPAddress[4] = { 224, 0, 0, 251 }; diff --git a/src/Networking/NetworkResponder.h b/src/Networking/NetworkResponder.h index 34aa6246..2cfb3c97 100644 --- a/src/Networking/NetworkResponder.h +++ b/src/Networking/NetworkResponder.h @@ -32,10 +32,6 @@ public: virtual void Terminate(NetworkProtocol protocol, NetworkInterface *interface) noexcept = 0; // terminate the responder if it is serving the specified protocol on the specified interface virtual void Diagnostics(MessageType mtype) const noexcept = 0; -#if SUPPORT_MULTICAST_DISCOVERY - virtual bool AcceptUdp(/*TODO parameters*/) noexcept { return false; } // most responders are TCP responders, so reject UDP packets by default -#endif - protected: // State machine control. Not all derived classes use all states. enum class ResponderState |