From b3dba5526e676af32d42eb66a1127e50cfa69dbe Mon Sep 17 00:00:00 2001 From: Arjan van Vught Date: Sat, 16 Nov 2024 19:36:36 +0100 Subject: [PATCH] Moved RDM E1.37-2 code from network.cpp to rdmhandlere137.cpp --- lib-network/include/emac/network.h | 47 +-------- lib-network/include/linux/network.h | 44 -------- lib-network/include/network.h | 10 -- lib-network/src/emac/network.cpp | 90 +--------------- lib-network/src/linux/network.cpp | 89 ---------------- lib-rdm/src/llrp/rdmhandlere1372.cpp | 151 ++++++++++++++++++++++++--- 6 files changed, 143 insertions(+), 288 deletions(-) diff --git a/lib-network/include/emac/network.h b/lib-network/include/emac/network.h index c64a250fe..cb521fd7b 100755 --- a/lib-network/include/emac/network.h +++ b/lib-network/include/emac/network.h @@ -102,22 +102,13 @@ class Network { void EnableDhcp(); bool IsDhcpUsed() { - const auto b = net::netif_dhcp(); - return b; + return net::netif_dhcp(); } bool IsDhcpKnown() const { return true; } - network::dhcp::Mode GetDhcpMode() { - if (IsDhcpUsed()) { - return network::dhcp::Mode::ACTIVE; - } - - return network::dhcp::Mode::INACTIVE; - } - /* * Zeroconf / autoip */ @@ -197,7 +188,7 @@ class Network { } void SendTo(int32_t nHandle, const void *pBuffer, uint32_t nLength, uint32_t to_ip, uint16_t remote_port) { - if (__builtin_expect((GetIp() != 0), 1)) { + if (__builtin_expect((GetIp() != 0), 1)) { //FIXME net::udp_send(nHandle, reinterpret_cast(pBuffer), nLength, to_ip, remote_port); } } @@ -238,18 +229,6 @@ class Network { net::igmp_leave(nIp); } - void SetQueuedStaticIp(const uint32_t nStaticIp, const uint32_t nNetmask); - void SetQueuedDefaultRoute(const uint32_t nGatewayIp); - void SetQueuedDhcp(const network::dhcp::Mode mode) { - m_QueuedConfig.mode = mode; - m_QueuedConfig.nMask |= QueuedConfig::DHCP; - } - void SetQueuedZeroconf() { - m_QueuedConfig.nMask |= QueuedConfig::ZEROCONF; - } - - bool ApplyQueuedConfig(); - uint32_t GetNetmaskCIDR() { return static_cast(__builtin_popcount(GetNetmask())); } @@ -303,27 +282,7 @@ class Network { char m_aDomainName[network::DOMAINNAME_SIZE]; uint32_t m_nNameservers[network::NAMESERVERS_COUNT]; - struct QueuedConfig { - static constexpr uint32_t NONE = 0; - static constexpr uint32_t STATIC_IP = (1U << 0); - static constexpr uint32_t NETMASK = (1U << 1); - static constexpr uint32_t GW = (1U << 2); - static constexpr uint32_t DHCP = (1U << 3); - static constexpr uint32_t ZEROCONF = (1U << 4); - uint32_t nMask = QueuedConfig::NONE; - uint32_t nStaticIp; - uint32_t nNetmask; - uint32_t nGateway; - network::dhcp::Mode mode; - }; - - QueuedConfig m_QueuedConfig; - - bool isQueuedMaskSet(const uint32_t nMask) { - return (m_QueuedConfig.nMask & nMask) == nMask; - } - - static Network *s_pThis; + static inline Network *s_pThis; }; #endif /* EMAC_NETWORK_H_ */ diff --git a/lib-network/include/linux/network.h b/lib-network/include/linux/network.h index 629981d58..8db846852 100644 --- a/lib-network/include/linux/network.h +++ b/lib-network/include/linux/network.h @@ -108,18 +108,6 @@ class Network { return false; } - void SetQueuedStaticIp(const uint32_t nStaticIp, const uint32_t nNetmask); - void SetQueuedDefaultRoute(const uint32_t nGatewayIp); - void SetQueuedDhcp(const network::dhcp::Mode mode) { - m_QueuedConfig.mode = mode; - m_QueuedConfig.nMask |= QueuedConfig::DHCP; - } - void SetQueuedZeroconf() { - m_QueuedConfig.nMask |= QueuedConfig::ZEROCONF; - } - - bool ApplyQueuedConfig(); - uint32_t GetGatewayIp() const { return m_nGatewayIp; } @@ -174,18 +162,6 @@ class Network { #endif } - network::dhcp::Mode GetDhcpMode() const { - if (IsDhcpKnown()) { - if (m_IsDhcpUsed) { - return network::dhcp::Mode::ACTIVE; - } - - return network::dhcp::Mode::INACTIVE; - } - - return network::dhcp::Mode::UNKNOWN; - } - const char *GetIfName() const { return m_aIfName; } @@ -247,26 +223,6 @@ class Network { uint8_t m_aNetMacaddr[network::MAC_SIZE]; char m_aIfName[IFNAMSIZ]; - struct QueuedConfig { - static constexpr uint32_t NONE = 0; - static constexpr uint32_t STATIC_IP = (1U << 0); - static constexpr uint32_t NETMASK = (1U << 1); - static constexpr uint32_t GW = (1U << 2); - static constexpr uint32_t DHCP = (1U << 3); - static constexpr uint32_t ZEROCONF = (1U << 4); - uint32_t nMask = QueuedConfig::NONE; - uint32_t nStaticIp; - uint32_t nNetmask; - uint32_t nGateway; - network::dhcp::Mode mode; - }; - - QueuedConfig m_QueuedConfig; - - bool isQueuedMaskSet(uint32_t nMask) { - return (m_QueuedConfig.nMask & nMask) == nMask; - } - static Network *s_pThis; }; diff --git a/lib-network/include/network.h b/lib-network/include/network.h index 7fe09f459..53875905a 100644 --- a/lib-network/include/network.h +++ b/lib-network/include/network.h @@ -30,16 +30,6 @@ #include "ip4_address.h" -namespace network { -namespace dhcp { -enum class Mode: uint8_t { - INACTIVE = 0x00, ///< The IP address was not obtained via DHCP - ACTIVE = 0x01, ///< The IP address was obtained via DHCP - UNKNOWN = 0x02 ///< The system cannot determine if the address was obtained via DHCP -}; -} // namespace dhcp -} // namespace network - #if defined(__linux__) || defined (__APPLE__) # if defined (CONFIG_NETWORK_USE_MINIMUM) # include "linux/minimum/network.h" diff --git a/lib-network/src/emac/network.cpp b/lib-network/src/emac/network.cpp index 63e0eedec..3698fc5a5 100755 --- a/lib-network/src/emac/network.cpp +++ b/lib-network/src/emac/network.cpp @@ -100,8 +100,6 @@ static void netif_ext_callback(const uint16_t reason, [[maybe_unused]] const net DEBUG_EXIT } -Network *Network::s_pThis; - Network::Network() { DEBUG_ENTRY assert(s_pThis == nullptr); @@ -242,6 +240,7 @@ void Network::SetHostName(const char *pHostName) { m_aHostName[network::HOSTNAME_SIZE - 1] = '\0'; NetworkStore::SaveHostName(m_aHostName, static_cast(strlen(m_aHostName))); + #if !defined(CONFIG_NET_APPS_NO_MDNS) mdns_send_announcement(mdns::MDNS_RESPONSE_TTL); #endif @@ -270,93 +269,6 @@ void Network::EnableDhcp() { DEBUG_EXIT } -void Network::SetQueuedStaticIp(const uint32_t nStaticIp, const uint32_t nNetmask) { - DEBUG_ENTRY - DEBUG_PRINTF(IPSTR ", nNetmask=" IPSTR, IP2STR(nStaticIp), IP2STR(nNetmask)); - - if (nStaticIp != 0) { - m_QueuedConfig.nStaticIp = nStaticIp; - } else { - m_QueuedConfig.nStaticIp = GetIp(); - } - - if (nNetmask != 0) { - m_QueuedConfig.nNetmask = nNetmask; - } else { - m_QueuedConfig.nNetmask = GetNetmask(); - } - - m_QueuedConfig.nMask |= QueuedConfig::STATIC_IP; - m_QueuedConfig.nMask |= QueuedConfig::NETMASK; - - DEBUG_EXIT -} - -void Network::SetQueuedDefaultRoute(const uint32_t nGatewayIp) { - if (nGatewayIp != 0) { - m_QueuedConfig.nGateway = nGatewayIp; - } else { - m_QueuedConfig.nGateway = GetGatewayIp(); - } - - m_QueuedConfig.nMask |= QueuedConfig::GW; -} - -bool Network::ApplyQueuedConfig() { - DEBUG_ENTRY - DEBUG_PRINTF("m_QueuedConfig.nMask=%x, " IPSTR ", " IPSTR, m_QueuedConfig.nMask, IP2STR(m_QueuedConfig.nStaticIp), IP2STR(m_QueuedConfig.nNetmask)); - - if (m_QueuedConfig.nMask == QueuedConfig::NONE) { - DEBUG_EXIT - return false; - } - - if ((isQueuedMaskSet(QueuedConfig::STATIC_IP)) || (isQueuedMaskSet(QueuedConfig::NETMASK)) || (isQueuedMaskSet(QueuedConfig::GW))) { - // After SetIp all ip address might be zero. - if (isQueuedMaskSet(QueuedConfig::STATIC_IP)) { - SetIp(m_QueuedConfig.nStaticIp); - } - - if (isQueuedMaskSet(QueuedConfig::NETMASK)) { - SetNetmask(m_QueuedConfig.nNetmask); - } - - if (isQueuedMaskSet(QueuedConfig::GW)) { - SetGatewayIp(m_QueuedConfig.nGateway); - } - - m_QueuedConfig.nMask = QueuedConfig::NONE; - - DEBUG_EXIT - return true; - } - - if (isQueuedMaskSet(QueuedConfig::DHCP)) { - if (m_QueuedConfig.mode == network::dhcp::Mode::ACTIVE) { - EnableDhcp(); - } else if (m_QueuedConfig.mode == network::dhcp::Mode::INACTIVE) { - - } - - m_QueuedConfig.mode = network::dhcp::Mode::UNKNOWN; - m_QueuedConfig.nMask = QueuedConfig::NONE; - - DEBUG_EXIT - return true; - } - - if (isQueuedMaskSet(QueuedConfig::ZEROCONF)) { - SetZeroconf(); - m_QueuedConfig.nMask = QueuedConfig::NONE; - - DEBUG_EXIT - return true; - } - - DEBUG_EXIT - return false; -} - void Network::Print() { printf("Network [%c]\n", GetAddressingMode()); printf(" Hostname : %s\n", m_aHostName); diff --git a/lib-network/src/linux/network.cpp b/lib-network/src/linux/network.cpp index 3256daa3a..45dfa0c55 100644 --- a/lib-network/src/linux/network.cpp +++ b/lib-network/src/linux/network.cpp @@ -581,95 +581,6 @@ void Network::SetHostName(const char *pHostName) { } -// COMMON - -void Network::SetQueuedStaticIp(const uint32_t nStaticIp, const uint32_t nNetmask) { - DEBUG_ENTRY - DEBUG_PRINTF(IPSTR ", nNetmask=" IPSTR, IP2STR(nStaticIp), IP2STR(nNetmask)); - - if (nStaticIp != 0) { - m_QueuedConfig.nStaticIp = nStaticIp; - } else { - m_QueuedConfig.nStaticIp = GetIp(); - } - - if (nNetmask != 0) { - m_QueuedConfig.nNetmask = nNetmask; - } else { - m_QueuedConfig.nNetmask = GetNetmask(); - } - - m_QueuedConfig.nMask |= QueuedConfig::STATIC_IP; - m_QueuedConfig.nMask |= QueuedConfig::NETMASK; - - DEBUG_EXIT -} - -void Network::SetQueuedDefaultRoute(const uint32_t nGatewayIp) { - if (nGatewayIp != 0) { - m_QueuedConfig.nGateway = nGatewayIp; - } else { - m_QueuedConfig.nGateway = GetGatewayIp(); - } - - m_QueuedConfig.nMask |= QueuedConfig::GW; -} - -bool Network::ApplyQueuedConfig() { - DEBUG_ENTRY - DEBUG_PRINTF("m_QueuedConfig.nMask=%x, " IPSTR ", " IPSTR, m_QueuedConfig.nMask, IP2STR(m_QueuedConfig.nStaticIp), IP2STR(m_QueuedConfig.nNetmask)); - - if (m_QueuedConfig.nMask == QueuedConfig::NONE) { - DEBUG_EXIT - return false; - } - - if ((isQueuedMaskSet(QueuedConfig::STATIC_IP)) || (isQueuedMaskSet(QueuedConfig::NETMASK)) || (isQueuedMaskSet(QueuedConfig::GW))) { - // After SetIp all ip address might be zero. - if (isQueuedMaskSet(QueuedConfig::STATIC_IP)) { - SetIp(m_QueuedConfig.nStaticIp); - } - - if (isQueuedMaskSet(QueuedConfig::NETMASK)) { - SetNetmask(m_QueuedConfig.nNetmask); - } - - if (isQueuedMaskSet(QueuedConfig::GW)) { - SetGatewayIp(m_QueuedConfig.nGateway); - } - - m_QueuedConfig.nMask = QueuedConfig::NONE; - - DEBUG_EXIT - return true; - } - - if (isQueuedMaskSet(QueuedConfig::DHCP)) { - if (m_QueuedConfig.mode == network::dhcp::Mode::ACTIVE) { - EnableDhcp(); - } else if (m_QueuedConfig.mode == network::dhcp::Mode::INACTIVE) { - - } - - m_QueuedConfig.mode = network::dhcp::Mode::UNKNOWN; - m_QueuedConfig.nMask = QueuedConfig::NONE; - - DEBUG_EXIT - return true; - } - - if (isQueuedMaskSet(QueuedConfig::ZEROCONF)) { - SetZeroconf(); - m_QueuedConfig.nMask = QueuedConfig::NONE; - - DEBUG_EXIT - return true; - } - - DEBUG_EXIT - return false; -} - void Network::Print() { printf("Network\n"); printf(" Hostname : %s\n", m_aHostName); diff --git a/lib-rdm/src/llrp/rdmhandlere1372.cpp b/lib-rdm/src/llrp/rdmhandlere1372.cpp index b2fa97c65..8c938f543 100755 --- a/lib-rdm/src/llrp/rdmhandlere1372.cpp +++ b/lib-rdm/src/llrp/rdmhandlere1372.cpp @@ -36,10 +36,137 @@ #include "debug.h" -enum { - IPV4_UNCONFIGURED = 0x00000000, - NO_DEFAULT_ROUTE = 0x00000000 +namespace dhcp { +enum class Mode: uint8_t { + INACTIVE = 0x00, ///< The IP address was not obtained via DHCP + ACTIVE = 0x01, ///< The IP address was obtained via DHCP + UNKNOWN = 0x02 ///< The system cannot determine if the address was obtained via DHCP }; +} // namespace dhcp + +static dhcp::Mode get_dhcp_mode() { + if (Network::Get()->IsDhcpUsed()) { + return dhcp::Mode::ACTIVE; + } + + return dhcp::Mode::INACTIVE; +} + +struct QueuedConfig { + static constexpr uint32_t NONE = 0; + static constexpr uint32_t STATIC_IP = (1U << 0); + static constexpr uint32_t NETMASK = (1U << 1); + static constexpr uint32_t GW = (1U << 2); + static constexpr uint32_t DHCP = (1U << 3); + static constexpr uint32_t ZEROCONF = (1U << 4); + uint32_t nMask = QueuedConfig::NONE; + uint32_t nStaticIp; + uint32_t nNetmask; + uint32_t nGateway; + dhcp::Mode mode; +}; + +static QueuedConfig s_QueuedConfig; + +static bool is_queued_mask_set(const uint32_t nMask) { + return (s_QueuedConfig.nMask & nMask) == nMask; +} + +static void set_queued_static_ip(const uint32_t nStaticIp, const uint32_t nNetmask) { + DEBUG_ENTRY + DEBUG_PRINTF(IPSTR ", nNetmask=" IPSTR, IP2STR(nStaticIp), IP2STR(nNetmask)); + + if (nStaticIp != 0) { + s_QueuedConfig.nStaticIp = nStaticIp; + } else { + s_QueuedConfig.nStaticIp = Network::Get()->GetIp(); + } + + if (nNetmask != 0) { + s_QueuedConfig.nNetmask = nNetmask; + } else { + s_QueuedConfig.nNetmask = Network::Get()->GetNetmask(); + } + + s_QueuedConfig.nMask |= QueuedConfig::STATIC_IP; + s_QueuedConfig.nMask |= QueuedConfig::NETMASK; + + DEBUG_EXIT +} + +static void set_queued_default_route(const uint32_t nGatewayIp) { + if (nGatewayIp != 0) { + s_QueuedConfig.nGateway = nGatewayIp; + } else { + s_QueuedConfig.nGateway = Network::Get()->GetGatewayIp(); + } + + s_QueuedConfig.nMask |= QueuedConfig::GW; +} + +static void set_queued_dhcp(const dhcp::Mode mode) { + s_QueuedConfig.mode = mode; + s_QueuedConfig.nMask |= QueuedConfig::DHCP; +} + +static void set_queued_zeroconf() { + s_QueuedConfig.nMask |= QueuedConfig::ZEROCONF; +} + +static bool apply_queued_config() { + DEBUG_ENTRY + DEBUG_PRINTF("s_QueuedConfig.nMask=%x, " IPSTR ", " IPSTR, s_QueuedConfig.nMask, IP2STR(s_QueuedConfig.nStaticIp), IP2STR(s_QueuedConfig.nNetmask)); + + if (s_QueuedConfig.nMask == QueuedConfig::NONE) { + DEBUG_EXIT + return false; + } + + if ((is_queued_mask_set(QueuedConfig::STATIC_IP)) || (is_queued_mask_set(QueuedConfig::NETMASK)) || (is_queued_mask_set(QueuedConfig::GW))) { + // After SetIp all ip address might be zero. + if (is_queued_mask_set(QueuedConfig::STATIC_IP)) { + Network::Get()->SetIp(s_QueuedConfig.nStaticIp); + } + + if (is_queued_mask_set(QueuedConfig::NETMASK)) { + Network::Get()->SetNetmask(s_QueuedConfig.nNetmask); + } + + if (is_queued_mask_set(QueuedConfig::GW)) { + Network::Get()->SetGatewayIp(s_QueuedConfig.nGateway); + } + + s_QueuedConfig.nMask = QueuedConfig::NONE; + + DEBUG_EXIT + return true; + } + + if (is_queued_mask_set(QueuedConfig::DHCP)) { + if (s_QueuedConfig.mode == dhcp::Mode::ACTIVE) { + Network::Get()->EnableDhcp(); + } else if (s_QueuedConfig.mode == dhcp::Mode::INACTIVE) { + + } + + s_QueuedConfig.mode = dhcp::Mode::UNKNOWN; + s_QueuedConfig.nMask = QueuedConfig::NONE; + + DEBUG_EXIT + return true; + } + + if (is_queued_mask_set(QueuedConfig::ZEROCONF)) { + Network::Get()->SetZeroconf(); + s_QueuedConfig.nMask = QueuedConfig::NONE; + + DEBUG_EXIT + return true; + } + + DEBUG_EXIT + return false; +} /* * ANSI E1.37-2 @@ -169,10 +296,10 @@ void RDMHandler::SetDHCPMode([[maybe_unused]] bool IsBroadcast, [[maybe_unused]] return; } - const auto mode = static_cast(pRdmDataIn->param_data[4]); + const auto mode = static_cast(pRdmDataIn->param_data[4]); - if ((mode == network::dhcp::Mode::ACTIVE) || mode == network::dhcp::Mode::INACTIVE) { - Network::Get()->SetQueuedDhcp(mode); + if ((mode == dhcp::Mode::ACTIVE) || mode == dhcp::Mode::INACTIVE) { + set_queued_dhcp(mode); RespondMessageAck(); DEBUG_EXIT @@ -254,7 +381,7 @@ void RDMHandler::SetZeroconf([[maybe_unused]] bool IsBroadcast, [[maybe_unused]] } if (pRdmDataIn->param_data[4] == 1) { - Network::Get()->SetQueuedZeroconf(); + set_queued_zeroconf(); RespondMessageAck(); DEBUG_EXIT @@ -262,7 +389,7 @@ void RDMHandler::SetZeroconf([[maybe_unused]] bool IsBroadcast, [[maybe_unused]] } if (pRdmDataIn->param_data[4] == 0) { - Network::Get()->SetQueuedStaticIp(0, 0); + set_queued_static_ip(0, 0); RespondMessageAck(); DEBUG_EXIT @@ -325,7 +452,7 @@ void RDMHandler::GetAddressNetmask([[maybe_unused]] uint16_t nSubDevice) { memcpy(&pRdmDataOut->param_data[0], &pRdmDataIn->param_data[0], 4); memcpy(&pRdmDataOut->param_data[4], p, 4); pRdmDataOut->param_data[8] = static_cast(Network::Get()->GetNetmaskCIDR()); - pRdmDataOut->param_data[9] = static_cast(Network::Get()->GetDhcpMode()); + pRdmDataOut->param_data[9] = static_cast(get_dhcp_mode()); pRdmDataOut->param_data_length = 10; @@ -383,7 +510,7 @@ void RDMHandler::SetStaticAddress([[maybe_unused]] bool IsBroadcast, [[maybe_unu auto *p = reinterpret_cast(&nIpAddress); memcpy(p, &pRdmDataIn->param_data[4], 4); - Network::Get()->SetQueuedStaticIp(nIpAddress, network::cidr_to_netmask(pRdmDataIn->param_data[8])); + set_queued_static_ip(nIpAddress, network::cidr_to_netmask(pRdmDataIn->param_data[8])); RespondMessageAck(); @@ -401,7 +528,7 @@ void RDMHandler::ApplyConfiguration([[maybe_unused]] bool IsBroadcast, [[maybe_u return; } - if (Network::Get()->ApplyQueuedConfig()) { // Not Queuing -> Apply + if (apply_queued_config()) { // Not Queuing -> Apply RespondMessageAck(); DEBUG_EXIT @@ -461,7 +588,7 @@ void RDMHandler::SetDefaultRoute([[maybe_unused]] bool IsBroadcast, [[maybe_unus auto *p = reinterpret_cast(&nIpAddress); memcpy(p, &pRdmDataIn->param_data[4], 4); - Network::Get()->SetQueuedDefaultRoute(nIpAddress); + set_queued_default_route(nIpAddress); RespondMessageAck();