Skip to content

Commit

Permalink
add IPAddresses for DiagnosticDataProviderImpl on android (#16557)
Browse files Browse the repository at this point in the history
* add IPAddresses for DiagnosticDataProviderImpl on android

* fix restyled-io and ci errors

* add ip adder size check

* fix restyled-io and ci errors
  • Loading branch information
xylophone21 authored and pull[bot] committed Oct 18, 2023
1 parent 94dfefe commit 27eef36
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 0 deletions.
36 changes: 36 additions & 0 deletions src/platform/android/DiagnosticDataProviderImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,42 @@ CHIP_ERROR DiagnosticDataProviderImpl::GetNetworkInterfaces(NetworkInterface **
jfieldID getTypeField = env->GetFieldID(nifClass, "type", "I");
ifp->type = static_cast<InterfaceType>(env->GetIntField(nifObject, getTypeField));

jfieldID ipv4AddressField = env->GetFieldID(nifClass, "ipv4Address", "[B");
jbyteArray jIpv4AddressObj = static_cast<jbyteArray>(env->GetObjectField(nifObject, ipv4AddressField));
if (jIpv4AddressObj != nullptr)
{
JniByteArray Ipv4ByteArray(env, jIpv4AddressObj);

if (Ipv4ByteArray.size() == kMaxIPv4AddrSize)
{
memcpy(ifp->Ipv4AddressesBuffer[0], reinterpret_cast<const uint8_t *>(Ipv4ByteArray.data()), kMaxIPv4AddrSize);
ifp->Ipv4AddressSpans[0] = ByteSpan(ifp->Ipv4AddressesBuffer[0], kMaxIPv4AddrSize);
ifp->IPv4Addresses = chip::app::DataModel::List<chip::ByteSpan>(ifp->Ipv4AddressSpans, 1);
}
else
{
ChipLogError(DeviceLayer, "ipv4Address size (%d) not equal to kMaxIPv4AddrSize", Ipv4ByteArray.size());
}
}

jfieldID ipv6AddressField = env->GetFieldID(nifClass, "ipv6Address", "[B");
jbyteArray jIpv6AddressObj = static_cast<jbyteArray>(env->GetObjectField(nifObject, ipv6AddressField));
if (jIpv6AddressObj != nullptr)
{
JniByteArray Ipv6ByteArray(env, jIpv6AddressObj);

if (Ipv6ByteArray.size() == kMaxIPv6AddrSize)
{
memcpy(ifp->Ipv6AddressesBuffer[0], reinterpret_cast<const uint8_t *>(Ipv6ByteArray.data()), kMaxIPv6AddrSize);
ifp->Ipv6AddressSpans[0] = ByteSpan(ifp->Ipv6AddressesBuffer[0], kMaxIPv6AddrSize);
ifp->IPv6Addresses = chip::app::DataModel::List<chip::ByteSpan>(ifp->Ipv6AddressSpans, 1);
}
else
{
ChipLogError(DeviceLayer, "ipv6Address size (%d) not equal to kMaxIPv6AddrSize", Ipv6ByteArray.size());
}
}

ifp->Next = head;
head = ifp;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,12 @@

import android.content.Context;
import android.util.Log;
import java.net.Inet4Address;
import java.net.InetAddress;
import java.net.SocketException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Enumeration;
import java.util.List;

public class DiagnosticDataProviderImpl implements DiagnosticDataProvider {
Expand Down Expand Up @@ -64,6 +67,22 @@ public NetworkInterface[] getNetworkInterfaces() {
name.startsWith("wlan")
? NetworkInterface.INTERFACE_TYPE_WI_FI
: NetworkInterface.INTERFACE_TYPE_ETHERNET;

Enumeration<InetAddress> inetAddress = nif.getInetAddresses();
while (inetAddress.hasMoreElements()) {
InetAddress ip = inetAddress.nextElement();

if (ip instanceof Inet4Address) {
if (anInterface.ipv4Address == null) {
anInterface.ipv4Address = ip.getAddress();
}
} else if (ip instanceof InetAddress) {
if (anInterface.ipv6Address == null) {
anInterface.ipv6Address = ip.getAddress();
}
}
}

destInterfaces.add(anInterface);
}
}
Expand Down
2 changes: 2 additions & 0 deletions src/platform/android/java/chip/platform/NetworkInterface.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,5 +33,7 @@ public class NetworkInterface {
@Nullable public Boolean offPremiseServicesReachableIPv4;
@Nullable public Boolean offPremiseServicesReachableIPv6;
public byte[] hardwareAddress;
public byte[] ipv4Address;
public byte[] ipv6Address;
public int type;
}

0 comments on commit 27eef36

Please sign in to comment.