Skip to content

Commit

Permalink
Merge pull request #11 from meulemanelectronics/TSBM-231
Browse files Browse the repository at this point in the history
 added code for reading network interface
  • Loading branch information
NeethuVisw authored May 31, 2024
2 parents 3a03806 + 5432167 commit 9074c51
Show file tree
Hide file tree
Showing 3 changed files with 138 additions and 1 deletion.
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

0 comments on commit 9074c51

Please sign in to comment.