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

add IPAddresses for DiagnosticDataProviderImpl on android #16557

Merged
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
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
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;
}