Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added code for reading network interface #11

Merged
merged 8 commits into from
May 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 15 additions & 1 deletion src/inet/InetInterface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,21 @@ bool InterfaceIterator::HasBroadcastAddress()

CHIP_ERROR InterfaceIterator::GetInterfaceType(InterfaceType & type)
{
return CHIP_ERROR_NOT_IMPLEMENTED;
VerifyOrReturnError(HasCurrent(), CHIP_ERROR_INCORRECT_STATE);

if(!mCurNetif)
return CHIP_ERROR_INCORRECT_STATE;

if (netif_is_flag_set(mCurNetif, NETIF_FLAG_ETHARP))
{
type = InterfaceType::Ethernet;
}
else
{
type = InterfaceType::WiFi;
}

return CHIP_NO_ERROR;
}

CHIP_ERROR InterfaceIterator::GetHardwareAddress(uint8_t * addressBuffer, uint8_t & addressSize, uint8_t addressBufferSize)
Expand Down
120 changes: 120 additions & 0 deletions src/platform/renesas/DiagnosticDataProviderImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include <platform/internal/CHIPDeviceLayerInternal.h>

#include <platform/DiagnosticDataProvider.h>
#include <app/data-model/List.h>
#include <platform/renesas/DiagnosticDataProviderImpl.h>

#include "FreeRTOS.h"
Expand Down Expand Up @@ -93,6 +94,125 @@ CHIP_ERROR DiagnosticDataProviderImpl::GetBootReason(BootReasonType & bootReason
return err;
}

CHIP_ERROR DiagnosticDataProviderImpl::GetNetworkInterfaces(NetworkInterface ** netifpp)
{
CHIP_ERROR err = CHIP_NO_ERROR;
NetworkInterface *head = nullptr;

for (Inet::InterfaceIterator interfaceIterator; interfaceIterator.HasCurrent(); interfaceIterator.Next())
{
if(NetworkInterface *ifp = new NetworkInterface())
{
interfaceIterator.GetInterfaceName(ifp->Name, Inet::InterfaceId::kMaxIfNameLength);

ifp->name = CharSpan::fromCharString(ifp->Name);
ifp->isOperational = interfaceIterator.IsUp();

Inet::InterfaceType interfaceType;
if (interfaceIterator.GetInterfaceType(interfaceType) == CHIP_NO_ERROR)
{
switch (interfaceType)
{
case Inet::InterfaceType::Unknown:
ifp->type = EMBER_ZCL_INTERFACE_TYPE_ENUM_UNSPECIFIED;
break;
case Inet::InterfaceType::WiFi:
ifp->type = EMBER_ZCL_INTERFACE_TYPE_ENUM_WI_FI;
break;
case Inet::InterfaceType::Ethernet:
ifp->type = EMBER_ZCL_INTERFACE_TYPE_ENUM_ETHERNET;
break;
case Inet::InterfaceType::Thread:
ifp->type = EMBER_ZCL_INTERFACE_TYPE_ENUM_THREAD;
break;
case Inet::InterfaceType::Cellular:
ifp->type = EMBER_ZCL_INTERFACE_TYPE_ENUM_CELLULAR;
break;
}
}
else
{
ChipLogError(DeviceLayer, "Failed to get interface type");
ifp->type = EMBER_ZCL_INTERFACE_TYPE_ENUM_UNSPECIFIED;
}
ifp->offPremiseServicesReachableIPv4.SetNull();
ifp->offPremiseServicesReachableIPv6.SetNull();

uint8_t addressSize = 0;
err = interfaceIterator.GetHardwareAddress(ifp->MacAddress, addressSize, sizeof(ifp->MacAddress));

if (err == CHIP_NO_ERROR)
{
ifp->hardwareAddress = ByteSpan(ifp->MacAddress, addressSize);
}
else
{
ChipLogError(DeviceLayer, "Failed to get network hardware address");
ifp->hardwareAddress = ByteSpan();
}

size_t ipv6AddressCount = 0;

Inet::InterfaceAddressIterator interfaceAddressIterator;
for(Inet::InterfaceAddressIterator interfaceAddressIterator; interfaceAddressIterator.HasCurrent(); interfaceAddressIterator.Next())
{
if (interfaceAddressIterator.GetInterfaceId() == interfaceIterator.GetInterfaceId())
{
Inet::IPAddress ipAddress;
if (interfaceAddressIterator.GetAddress(ipAddress) == CHIP_NO_ERROR)
{
if (ipAddress.IsIPv6() && ipv6AddressCount < kMaxIPv6AddrCount)
{
memcpy(ifp->Ipv6AddressesBuffer[ipv6AddressCount], ipAddress.Addr, sizeof(ipAddress.Addr));
ifp->Ipv6AddressSpans[ipv6AddressCount] = ByteSpan(ifp->Ipv6AddressesBuffer[ipv6AddressCount], sizeof(ipAddress.Addr));
ipv6AddressCount++;
}
}
}
}

if (ipv6AddressCount > 0)
{
ifp->IPv6Addresses = chip::app::DataModel::List<chip::ByteSpan>(ifp->Ipv6AddressSpans, ipv6AddressCount);
}

ifp->Next = head;
head = ifp;
}
else
{
err = CHIP_ERROR_NO_MEMORY;
break;
}

}

if (err!=CHIP_NO_ERROR)
{
ReleaseNetworkInterfaces(head);
return err;
}
else if (head == nullptr)
{
return CHIP_ERROR_NOT_FOUND;
}
else
{
*netifpp = head;
return CHIP_NO_ERROR;
}
}

void DiagnosticDataProviderImpl::ReleaseNetworkInterfaces(NetworkInterface * netifp)
{
while (netifp)
{
NetworkInterface * del = netifp;
netifp = netifp->Next;
delete del;
}
}

DiagnosticDataProvider & GetDiagnosticDataProviderImpl()
{
return DiagnosticDataProviderImpl::GetDefaultInstance();
Expand Down
3 changes: 3 additions & 0 deletions src/platform/renesas/DiagnosticDataProviderImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ class DiagnosticDataProviderImpl : public DiagnosticDataProvider
CHIP_ERROR GetRebootCount(uint16_t & rebootCount) override;
CHIP_ERROR GetBootReason(BootReasonType & bootReason) override;

CHIP_ERROR GetNetworkInterfaces(NetworkInterface ** netifpp);
void ReleaseNetworkInterfaces(NetworkInterface * netifp);

};

/**
Expand Down