From a6a5d2a80f285bd4276aa9430c993bc3519f72d2 Mon Sep 17 00:00:00 2001 From: Kevin Schoedel <67607049+kpschoedel@users.noreply.github.com> Date: Tue, 9 Nov 2021 08:54:11 -0500 Subject: [PATCH] Split Inet::EndPoint into per-implemention files (#11539) #### Problem This is a step toward #7715 _Virtualize System and Inet interfaces_. #### Change overview Move each `Inet::EndPointBasis` implementation into its own file. Each inherits from `Inet::EndPointBase`, but transitionally each implementation is statically named `EndPointBasis`, and initialization continues to be done in `InitEndPointBasis()` rather than a constructor. #### Testing CI; no changes to functionality. --- src/inet/BUILD.gn | 4 +- src/inet/EndPointBasis.cpp | 66 -------------------- src/inet/EndPointBasis.h | 64 +++++-------------- src/inet/EndPointBasisImplLwIP.h | 64 +++++++++++++++++++ src/inet/EndPointBasisImplNetworkFramework.h | 46 ++++++++++++++ src/inet/EndPointBasisImplSockets.h | 47 ++++++++++++++ src/inet/inet.gni | 7 +++ 7 files changed, 182 insertions(+), 116 deletions(-) delete mode 100644 src/inet/EndPointBasis.cpp create mode 100644 src/inet/EndPointBasisImplLwIP.h create mode 100644 src/inet/EndPointBasisImplNetworkFramework.h create mode 100644 src/inet/EndPointBasisImplSockets.h diff --git a/src/inet/BUILD.gn b/src/inet/BUILD.gn index 3c2caced20f164..ebffb373509ae3 100644 --- a/src/inet/BUILD.gn +++ b/src/inet/BUILD.gn @@ -49,6 +49,8 @@ buildconfig_header("inet_buildconfig") { defines += [ "INET_PLATFORM_CONFIG_INCLUDE=${chip_inet_platform_config_include}" ] } + + defines += [ "CHIP_INET_END_POINT_IMPL_CONFIG_FILE=" ] } source_set("inet_config_header") { @@ -66,8 +68,8 @@ static_library("inet") { output_name = "libInetLayer" sources = [ - "EndPointBasis.cpp", "EndPointBasis.h", + "EndPointBasisImpl${chip_system_config_inet}.h", "IANAConstants.h", "IPAddress-StringFuncts.cpp", "IPAddress.cpp", diff --git a/src/inet/EndPointBasis.cpp b/src/inet/EndPointBasis.cpp deleted file mode 100644 index a4a457bad41b9a..00000000000000 --- a/src/inet/EndPointBasis.cpp +++ /dev/null @@ -1,66 +0,0 @@ -/* - * - * Copyright (c) 2020-2021 Project CHIP Authors - * Copyright (c) 2015-2017 Nest Labs, Inc. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -/** - * @file - * This file contains the external implementations of methods in - * the basis class for all the various transport endpoint classes - * in the Inet layer, i.e. TCP, UDP, Raw and Tun. - */ - -#include - -#include - -namespace chip { -namespace Inet { - -#if CHIP_SYSTEM_CONFIG_USE_LWIP - -void EndPointBasis::InitEndPointBasis(InetLayer & aInetLayer, void * aAppState) -{ - AppState = aAppState; - mInetLayer = &aInetLayer; - mLwIPEndPointType = LwIPEndPointType::Unknown; -} - -#endif // CHIP_SYSTEM_CONFIG_USE_LWIP - -#if CHIP_SYSTEM_CONFIG_USE_SOCKETS - -void EndPointBasis::InitEndPointBasis(InetLayer & aInetLayer, void * aAppState) -{ - AppState = aAppState; - mInetLayer = &aInetLayer; - mSocket = kInvalidSocketFd; -} - -#endif // CHIP_SYSTEM_CONFIG_USE_SOCKETS - -#if CHIP_SYSTEM_CONFIG_USE_NETWORK_FRAMEWORK - -void EndPointBasis::InitEndPointBasis(InetLayer & aInetLayer, void * aAppState) -{ - AppState = aAppState; - mInetLayer = &aInetLayer; -} - -#endif // CHIP_SYSTEM_CONFIG_USE_NETWORK_FRAMEWORK - -} // namespace Inet -} // namespace chip diff --git a/src/inet/EndPointBasis.h b/src/inet/EndPointBasis.h index 0a68bf5c29924a..c1d503d8eec883 100644 --- a/src/inet/EndPointBasis.h +++ b/src/inet/EndPointBasis.h @@ -19,29 +19,14 @@ /** * @file * This file contains the basis class for all the various transport - * endpoint classes in the Inet layer, i.e. TCP, UDP, Raw and Tun. + * endpoint classes in the Inet layer, i.e. TCP and UDP. */ #pragma once #include - -#include #include -#if CHIP_SYSTEM_CONFIG_USE_SOCKETS -#include -#endif // CHIP_SYSTEM_CONFIG_USE_SOCKETS - -#if CHIP_SYSTEM_CONFIG_USE_NETWORK_FRAMEWORK -#include -#endif // CHIP_SYSTEM_CONFIG_USE_NETWORK_FRAMEWORK - -#if CHIP_SYSTEM_CONFIG_USE_LWIP -struct udp_pcb; -struct tcp_pcb; -#endif // CHIP_SYSTEM_CONFIG_USE_LWIP - namespace chip { namespace Inet { @@ -50,7 +35,7 @@ class InetLayer; /** * Basis of internet transport endpoint classes. */ -class DLL_EXPORT EndPointBasis +class DLL_EXPORT EndPointBase { public: /** @@ -73,42 +58,23 @@ class DLL_EXPORT EndPointBasis InetLayer * mInetLayer; /**< Pointer to the InetLayer object that owns this object. */ protected: - void InitEndPointBasis(InetLayer & aInetLayer, void * aAppState = nullptr); + virtual ~EndPointBase() = default; -#if CHIP_SYSTEM_CONFIG_USE_NETWORK_FRAMEWORK - nw_parameters_t mParameters; - IPAddressType mAddrType; /**< Protocol family, i.e. IPv4 or IPv6. */ -#endif - -#if CHIP_SYSTEM_CONFIG_USE_SOCKETS - static constexpr int kInvalidSocketFd = -1; - int mSocket; /**< Encapsulated socket descriptor. */ - IPAddressType mAddrType; /**< Protocol family, i.e. IPv4 or IPv6. */ - System::SocketWatchToken mWatch; /**< Socket event watcher */ -#endif // CHIP_SYSTEM_CONFIG_USE_SOCKETS - -#if CHIP_SYSTEM_CONFIG_USE_LWIP - /** Encapsulated LwIP protocol control block */ - union + void InitEndPointBasis(InetLayer & aInetLayer, void * aAppState = nullptr) { -#if INET_CONFIG_ENABLE_UDP_ENDPOINT - udp_pcb * mUDP; /**< User datagram protocol (UDP) control */ -#endif // INET_CONFIG_ENABLE_UDP_ENDPOINT -#if INET_CONFIG_ENABLE_TCP_ENDPOINT - tcp_pcb * mTCP; /**< Transmission control protocol (TCP) control */ -#endif // INET_CONFIG_ENABLE_TCP_ENDPOINT - }; + AppState = aAppState; + mInetLayer = &aInetLayer; + InitEndPointBasisImpl(); + } - enum class LwIPEndPointType : uint8_t - { - Unknown = 0, - Raw = 1, - UDP = 2, - UCP = 3, - TCP = 4 - } mLwIPEndPointType; -#endif // CHIP_SYSTEM_CONFIG_USE_LWIP + virtual void InitEndPointBasisImpl() = 0; }; } // namespace Inet } // namespace chip + +#ifdef CHIP_INET_END_POINT_IMPL_CONFIG_FILE +#include CHIP_INET_END_POINT_IMPL_CONFIG_FILE +#else // CHIP_INET_END_POINT_IMPL_CONFIG_FILE +#include +#endif // CHIP_INET_END_POINT_IMPL_CONFIG_FILE diff --git a/src/inet/EndPointBasisImplLwIP.h b/src/inet/EndPointBasisImplLwIP.h new file mode 100644 index 00000000000000..e8df0ed4baed1b --- /dev/null +++ b/src/inet/EndPointBasisImplLwIP.h @@ -0,0 +1,64 @@ +/* + * + * Copyright (c) 2020-2021 Project CHIP Authors + * Copyright (c) 2015-2017 Nest Labs, Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/** + * LwIP implementation of EndPointBase. + */ + +#pragma once + +#include + +#include + +struct udp_pcb; +struct tcp_pcb; + +namespace chip { +namespace Inet { + +class DLL_EXPORT EndPointImplLwIP : public EndPointBase +{ +protected: + // EndPointBase overrides + void InitEndPointBasisImpl() override { mLwIPEndPointType = LwIPEndPointType::Unknown; } + + /** Encapsulated LwIP protocol control block */ + union + { + const void * mVoid; /**< An untyped protocol control buffer reference */ +#if INET_CONFIG_ENABLE_UDP_ENDPOINT + udp_pcb * mUDP; /**< User datagram protocol (UDP) control */ +#endif // INET_CONFIG_ENABLE_UDP_ENDPOINT +#if INET_CONFIG_ENABLE_TCP_ENDPOINT + tcp_pcb * mTCP; /**< Transmission control protocol (TCP) control */ +#endif // INET_CONFIG_ENABLE_TCP_ENDPOINT + }; + + enum class LwIPEndPointType : uint8_t + { + Unknown = 0, + UDP = 1, + TCP = 2 + } mLwIPEndPointType; +}; + +using EndPointBasis = EndPointImplLwIP; + +} // namespace Inet +} // namespace chip diff --git a/src/inet/EndPointBasisImplNetworkFramework.h b/src/inet/EndPointBasisImplNetworkFramework.h new file mode 100644 index 00000000000000..b38344cb472600 --- /dev/null +++ b/src/inet/EndPointBasisImplNetworkFramework.h @@ -0,0 +1,46 @@ +/* + * + * Copyright (c) 2020-2021 Project CHIP Authors + * Copyright (c) 2015-2017 Nest Labs, Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/** + * Network Framework implementation of EndPointBase. + */ + +#pragma once + +#include + +#include + +#include + +namespace chip { +namespace Inet { + +class DLL_EXPORT EndPointImplNetworkFramework : public EndPointBase +{ +protected: + void InitEndPointBasisImpl() override {} + + nw_parameters_t mParameters; + IPAddressType mAddrType; /**< Protocol family, i.e. IPv4 or IPv6. */ +}; + +using EndPointBasis = EndPointImplNetworkFramework; + +} // namespace Inet +} // namespace chip diff --git a/src/inet/EndPointBasisImplSockets.h b/src/inet/EndPointBasisImplSockets.h new file mode 100644 index 00000000000000..d4db35cebc7dec --- /dev/null +++ b/src/inet/EndPointBasisImplSockets.h @@ -0,0 +1,47 @@ +/* + * + * Copyright (c) 2020-2021 Project CHIP Authors + * Copyright (c) 2015-2017 Nest Labs, Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/** + * Sockets implementation of EndPointBase. + */ + +#pragma once + +#include + +#include +#include + +namespace chip { +namespace Inet { + +class DLL_EXPORT EndPointImplSockets : public EndPointBase +{ +protected: + void InitEndPointBasisImpl() override { mSocket = kInvalidSocketFd; } + + static constexpr int kInvalidSocketFd = -1; + int mSocket; /**< Encapsulated socket descriptor. */ + IPAddressType mAddrType; /**< Protocol family, i.e. IPv4 or IPv6. */ + System::SocketWatchToken mWatch; /**< Socket event watcher */ +}; + +using EndPointBasis = EndPointImplSockets; + +} // namespace Inet +} // namespace chip diff --git a/src/inet/inet.gni b/src/inet/inet.gni index 382ba477c7351b..997ae034c8eaa4 100644 --- a/src/inet/inet.gni +++ b/src/inet/inet.gni @@ -25,4 +25,11 @@ declare_args() { # Enable TCP endpoint. chip_inet_config_enable_tcp_endpoint = true + + # Inet implementation type. + if (chip_system_config_use_lwip) { + chip_system_config_inet = "LwIP" + } else { + chip_system_config_inet = "Sockets" + } }