Skip to content

Commit

Permalink
[Linux] Implement Ethernet network driver
Browse files Browse the repository at this point in the history
  • Loading branch information
erjiaqing committed Apr 27, 2022
1 parent 13ef4a3 commit cfbcd38
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 2 deletions.
11 changes: 9 additions & 2 deletions examples/all-clusters-app/linux/main-common.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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
}
}

Expand Down
1 change: 1 addition & 0 deletions src/platform/Linux/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ static_library("Linux") {
"KeyValueStoreManagerImpl.h",
"Logging.cpp",
"NetworkCommissioningDriver.h",
"NetworkCommissioningEthernetDriver.cpp",
"NetworkCommissioningThreadDriver.cpp",
"NetworkCommissioningWiFiDriver.cpp",
"PlatformManagerImpl.cpp",
Expand Down
32 changes: 32 additions & 0 deletions src/platform/Linux/NetworkCommissioningDriver.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
43 changes: 43 additions & 0 deletions src/platform/Linux/NetworkCommissioningEthernetDriver.cpp
Original file line number Diff line number Diff line change
@@ -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 <lib/support/SafePointerCast.h>
#include <platform/CHIPDeviceLayer.h>
#include <platform/Linux/ConnectivityUtils.h>
#include <platform/Linux/NetworkCommissioningDriver.h>

#include <limits>
#include <string>
#include <vector>

using namespace chip::DeviceLayer::Internal;

namespace chip {
namespace DeviceLayer {
namespace NetworkCommissioning {

NetworkIterator * LinuxEthernetDriver::GetNetworks()
{
auto ret = new EthernetNetworkIterator();
ConnectivityUtils::GetEthInterfaceName(SafePointerCast<char *>(ret->interfaceName), sizeof(ret->interfaceName));
ret->interfaceNameLen = strnlen(SafePointerCast<char *>(ret->interfaceName), sizeof(ret->interfaceName));
return ret;
}

} // namespace NetworkCommissioning
} // namespace DeviceLayer
} // namespace chip

0 comments on commit cfbcd38

Please sign in to comment.