From 1262182c1c03c21c972b6f38965c931f64dbbdab Mon Sep 17 00:00:00 2001 From: Song GUO Date: Wed, 27 Apr 2022 22:52:39 +0800 Subject: [PATCH] [Linux] Implement Ethernet network driver (#17802) --- .../all-clusters-app/linux/main-common.cpp | 11 ++++- src/platform/Linux/BUILD.gn | 1 + .../Linux/NetworkCommissioningDriver.h | 32 ++++++++++++++ .../NetworkCommissioningEthernetDriver.cpp | 43 +++++++++++++++++++ 4 files changed, 85 insertions(+), 2 deletions(-) create mode 100644 src/platform/Linux/NetworkCommissioningEthernetDriver.cpp diff --git a/examples/all-clusters-app/linux/main-common.cpp b/examples/all-clusters-app/linux/main-common.cpp index a9b09291304849..7829f3a6c1894a 100644 --- a/examples/all-clusters-app/linux/main-common.cpp +++ b/examples/all-clusters-app/linux/main-common.cpp @@ -107,10 +107,13 @@ NetworkCommissioning::LinuxWiFiDriver sLinuxWiFiDriver; Clusters::NetworkCommissioning::Instance sWiFiNetworkCommissioningInstance(kNetworkCommissioningEndpointSecondary, &sLinuxWiFiDriver); #endif -#endif // CHIP_DEVICE_LAYER_TARGET_LINUX - +NetworkCommissioning::LinuxEthernetDriver sLinuxEthernetDriver; +Clusters::NetworkCommissioning::Instance sEthernetNetworkCommissioningInstance(kNetworkCommissioningEndpointMain, + &sLinuxEthernetDriver); +#else // CHIP_DEVICE_LAYER_TARGET_LINUX Clusters::NetworkCommissioning::NullNetworkDriver sNullNetworkDriver; Clusters::NetworkCommissioning::Instance sNullNetworkCommissioningInstance(kNetworkCommissioningEndpointMain, &sNullNetworkDriver); +#endif // CHIP_DEVICE_LAYER_TARGET_LINUX } // namespace void ApplicationInit() @@ -166,8 +169,12 @@ void ApplicationInit() else #endif // CHIP_DEVICE_LAYER_TARGET_LINUX { +#if CHIP_DEVICE_LAYER_TARGET_LINUX + sEthernetNetworkCommissioningInstance.Init(); +#else // Use NullNetworkCommissioningInstance to disable the network commissioning functions. sNullNetworkCommissioningInstance.Init(); +#endif } } diff --git a/src/platform/Linux/BUILD.gn b/src/platform/Linux/BUILD.gn index 0e42fb227e9ae0..1095bcebb93084 100644 --- a/src/platform/Linux/BUILD.gn +++ b/src/platform/Linux/BUILD.gn @@ -64,6 +64,7 @@ static_library("Linux") { "KeyValueStoreManagerImpl.h", "Logging.cpp", "NetworkCommissioningDriver.h", + "NetworkCommissioningEthernetDriver.cpp", "NetworkCommissioningThreadDriver.cpp", "NetworkCommissioningWiFiDriver.cpp", "PlatformManagerImpl.cpp", diff --git a/src/platform/Linux/NetworkCommissioningDriver.h b/src/platform/Linux/NetworkCommissioningDriver.h index a474535cd2e3ff..f0bce1902799dd 100644 --- a/src/platform/Linux/NetworkCommissioningDriver.h +++ b/src/platform/Linux/NetworkCommissioningDriver.h @@ -156,6 +156,38 @@ class LinuxThreadDriver final : public ThreadDriver #endif // CHIP_DEVICE_CONFIG_ENABLE_THREAD +class LinuxEthernetDriver final : public EthernetDriver +{ +public: + struct EthernetNetworkIterator final : public NetworkIterator + { + EthernetNetworkIterator() = default; + size_t Count() override { return interfaceNameLen > 0 ? 1 : 0; } + bool Next(Network & item) override + { + if (exhausted) + { + return false; + } + exhausted = true; + memcpy(item.networkID, interfaceName, interfaceNameLen); + item.networkIDLen = interfaceNameLen; + item.connected = true; + return true; + } + void Release() override { delete this; } + ~EthernetNetworkIterator() override = default; + + // Public, but cannot be accessed via NetworkIterator interface. + uint8_t interfaceName[kMaxNetworkIDLen]; + uint8_t interfaceNameLen = 0; + bool exhausted = false; + }; + + uint8_t GetMaxNetworks() override { return 1; }; + NetworkIterator * GetNetworks() override; +}; + } // namespace NetworkCommissioning } // namespace DeviceLayer } // namespace chip diff --git a/src/platform/Linux/NetworkCommissioningEthernetDriver.cpp b/src/platform/Linux/NetworkCommissioningEthernetDriver.cpp new file mode 100644 index 00000000000000..00c619c3605be3 --- /dev/null +++ b/src/platform/Linux/NetworkCommissioningEthernetDriver.cpp @@ -0,0 +1,43 @@ +/* + * + * Copyright (c) 2021 Project CHIP Authors + * + * 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. + */ + +#include +#include +#include +#include + +#include +#include +#include + +using namespace chip::DeviceLayer::Internal; + +namespace chip { +namespace DeviceLayer { +namespace NetworkCommissioning { + +NetworkIterator * LinuxEthernetDriver::GetNetworks() +{ + auto ret = new EthernetNetworkIterator(); + ConnectivityUtils::GetEthInterfaceName(SafePointerCast(ret->interfaceName), sizeof(ret->interfaceName)); + ret->interfaceNameLen = strnlen(SafePointerCast(ret->interfaceName), sizeof(ret->interfaceName)); + return ret; +} + +} // namespace NetworkCommissioning +} // namespace DeviceLayer +} // namespace chip