From 7d81ef7f9a138d36103cc00bdc4d9d92004744c6 Mon Sep 17 00:00:00 2001 From: xylophone21 Date: Wed, 23 Mar 2022 17:04:47 +0800 Subject: [PATCH 1/4] add IPAddresses for DiagnosticDataProviderImpl on android --- .../android/DiagnosticDataProviderImpl.cpp | 21 +++++++++++++++++++ .../platform/DiagnosticDataProviderImpl.java | 20 ++++++++++++++++++ .../java/chip/platform/NetworkInterface.java | 2 ++ 3 files changed, 43 insertions(+) diff --git a/src/platform/android/DiagnosticDataProviderImpl.cpp b/src/platform/android/DiagnosticDataProviderImpl.cpp index 09a580062d71e0..70e76381483af5 100644 --- a/src/platform/android/DiagnosticDataProviderImpl.cpp +++ b/src/platform/android/DiagnosticDataProviderImpl.cpp @@ -170,6 +170,27 @@ CHIP_ERROR DiagnosticDataProviderImpl::GetNetworkInterfaces(NetworkInterface ** jfieldID getTypeField = env->GetFieldID(nifClass, "type", "I"); ifp->type = static_cast(env->GetIntField(nifObject, getTypeField)); + jfieldID ipv4AddressField = env->GetFieldID(nifClass, "ipv4Address", "[B"); + jbyteArray jIpv4AddressObj = static_cast(env->GetObjectField(nifObject, ipv4AddressField)); + if (jIpv4AddressObj != nullptr) + { + JniByteArray Ipv4ByteArray(env, jIpv4AddressObj); + memcpy(ifp->Ipv4AddressesBuffer[0], reinterpret_cast(Ipv4ByteArray.data()), kMaxIPv4AddrSize); + ifp->Ipv4AddressSpans[0] = ByteSpan(ifp->Ipv4AddressesBuffer[0], kMaxIPv4AddrSize); + ifp->IPv4Addresses = chip::app::DataModel::List(ifp->Ipv4AddressSpans, 1); + } + + jfieldID ipv6AddressField = env->GetFieldID(nifClass, "ipv6Address", "[B"); + jbyteArray jIpv6AddressObj = static_cast(env->GetObjectField(nifObject, ipv6AddressField)); + if (jIpv6AddressObj != nullptr) + { + JniByteArray Ipv6ByteArray(env, jIpv6AddressObj); + + memcpy(ifp->Ipv6AddressesBuffer[0], reinterpret_cast(Ipv6ByteArray.data()), kMaxIPv6AddrSize); + ifp->Ipv6AddressSpans[0] = ByteSpan(ifp->Ipv6AddressesBuffer[0], kMaxIPv6AddrSize); + ifp->IPv6Addresses = chip::app::DataModel::List(ifp->Ipv6AddressSpans, 1); + } + ifp->Next = head; head = ifp; } diff --git a/src/platform/android/java/chip/platform/DiagnosticDataProviderImpl.java b/src/platform/android/java/chip/platform/DiagnosticDataProviderImpl.java index 769b9070679c27..eb2ca5becd28bd 100644 --- a/src/platform/android/java/chip/platform/DiagnosticDataProviderImpl.java +++ b/src/platform/android/java/chip/platform/DiagnosticDataProviderImpl.java @@ -19,9 +19,13 @@ 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 { @@ -64,6 +68,22 @@ public NetworkInterface[] getNetworkInterfaces() { name.startsWith("wlan") ? NetworkInterface.INTERFACE_TYPE_WI_FI : NetworkInterface.INTERFACE_TYPE_ETHERNET; + + Enumeration 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); } } diff --git a/src/platform/android/java/chip/platform/NetworkInterface.java b/src/platform/android/java/chip/platform/NetworkInterface.java index faa33cf9a91143..6898d091e39135 100644 --- a/src/platform/android/java/chip/platform/NetworkInterface.java +++ b/src/platform/android/java/chip/platform/NetworkInterface.java @@ -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; } From 80247544d9bc2b0db1b831f52557a5ff12282764 Mon Sep 17 00:00:00 2001 From: xylophone21 Date: Wed, 23 Mar 2022 17:09:05 +0800 Subject: [PATCH 2/4] fix restyled-io and ci errors --- .../android/java/chip/platform/DiagnosticDataProviderImpl.java | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/platform/android/java/chip/platform/DiagnosticDataProviderImpl.java b/src/platform/android/java/chip/platform/DiagnosticDataProviderImpl.java index eb2ca5becd28bd..4a90a48848aaba 100644 --- a/src/platform/android/java/chip/platform/DiagnosticDataProviderImpl.java +++ b/src/platform/android/java/chip/platform/DiagnosticDataProviderImpl.java @@ -19,7 +19,6 @@ import android.content.Context; import android.util.Log; - import java.net.Inet4Address; import java.net.InetAddress; import java.net.SocketException; @@ -77,7 +76,7 @@ public NetworkInterface[] getNetworkInterfaces() { if (anInterface.ipv4Address == null) { anInterface.ipv4Address = ip.getAddress(); } - } else if (ip instanceof InetAddress) { + } else if (ip instanceof InetAddress) { if (anInterface.ipv6Address == null) { anInterface.ipv6Address = ip.getAddress(); } From 4499b1040acce6502de7da4e4228394cddacf31b Mon Sep 17 00:00:00 2001 From: xylophone21 Date: Thu, 24 Mar 2022 10:40:41 +0800 Subject: [PATCH 3/4] add ip adder size check --- .../android/DiagnosticDataProviderImpl.cpp | 27 ++++++++++++++----- 1 file changed, 21 insertions(+), 6 deletions(-) diff --git a/src/platform/android/DiagnosticDataProviderImpl.cpp b/src/platform/android/DiagnosticDataProviderImpl.cpp index 70e76381483af5..75a8a056e7cafc 100644 --- a/src/platform/android/DiagnosticDataProviderImpl.cpp +++ b/src/platform/android/DiagnosticDataProviderImpl.cpp @@ -175,9 +175,17 @@ CHIP_ERROR DiagnosticDataProviderImpl::GetNetworkInterfaces(NetworkInterface ** if (jIpv4AddressObj != nullptr) { JniByteArray Ipv4ByteArray(env, jIpv4AddressObj); - memcpy(ifp->Ipv4AddressesBuffer[0], reinterpret_cast(Ipv4ByteArray.data()), kMaxIPv4AddrSize); - ifp->Ipv4AddressSpans[0] = ByteSpan(ifp->Ipv4AddressesBuffer[0], kMaxIPv4AddrSize); - ifp->IPv4Addresses = chip::app::DataModel::List(ifp->Ipv4AddressSpans, 1); + + if(Ipv4ByteArray.size() == kMaxIPv4AddrSize) + { + memcpy(ifp->Ipv4AddressesBuffer[0], reinterpret_cast(Ipv4ByteArray.data()), kMaxIPv4AddrSize); + ifp->Ipv4AddressSpans[0] = ByteSpan(ifp->Ipv4AddressesBuffer[0], kMaxIPv4AddrSize); + ifp->IPv4Addresses = chip::app::DataModel::List(ifp->Ipv4AddressSpans, 1); + } + else + { + ChipLogError(DeviceLayer,"ipv4Address size (%d) not equal to kMaxIPv4AddrSize", Ipv4ByteArray.size()); + } } jfieldID ipv6AddressField = env->GetFieldID(nifClass, "ipv6Address", "[B"); @@ -186,9 +194,16 @@ CHIP_ERROR DiagnosticDataProviderImpl::GetNetworkInterfaces(NetworkInterface ** { JniByteArray Ipv6ByteArray(env, jIpv6AddressObj); - memcpy(ifp->Ipv6AddressesBuffer[0], reinterpret_cast(Ipv6ByteArray.data()), kMaxIPv6AddrSize); - ifp->Ipv6AddressSpans[0] = ByteSpan(ifp->Ipv6AddressesBuffer[0], kMaxIPv6AddrSize); - ifp->IPv6Addresses = chip::app::DataModel::List(ifp->Ipv6AddressSpans, 1); + if(Ipv6ByteArray.size() == kMaxIPv6AddrSize) + { + memcpy(ifp->Ipv6AddressesBuffer[0], reinterpret_cast(Ipv6ByteArray.data()), kMaxIPv6AddrSize); + ifp->Ipv6AddressSpans[0] = ByteSpan(ifp->Ipv6AddressesBuffer[0], kMaxIPv6AddrSize); + ifp->IPv6Addresses = chip::app::DataModel::List(ifp->Ipv6AddressSpans, 1); + } + else + { + ChipLogError(DeviceLayer,"ipv6Address size (%d) not equal to kMaxIPv6AddrSize", Ipv6ByteArray.size()); + } } ifp->Next = head; From 8e94020c75cbd094441b623bd89984150c8ed71b Mon Sep 17 00:00:00 2001 From: xylophone21 Date: Thu, 24 Mar 2022 11:03:18 +0800 Subject: [PATCH 4/4] fix restyled-io and ci errors --- src/platform/android/DiagnosticDataProviderImpl.cpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/platform/android/DiagnosticDataProviderImpl.cpp b/src/platform/android/DiagnosticDataProviderImpl.cpp index 75a8a056e7cafc..4af6611657b009 100644 --- a/src/platform/android/DiagnosticDataProviderImpl.cpp +++ b/src/platform/android/DiagnosticDataProviderImpl.cpp @@ -176,7 +176,7 @@ CHIP_ERROR DiagnosticDataProviderImpl::GetNetworkInterfaces(NetworkInterface ** { JniByteArray Ipv4ByteArray(env, jIpv4AddressObj); - if(Ipv4ByteArray.size() == kMaxIPv4AddrSize) + if (Ipv4ByteArray.size() == kMaxIPv4AddrSize) { memcpy(ifp->Ipv4AddressesBuffer[0], reinterpret_cast(Ipv4ByteArray.data()), kMaxIPv4AddrSize); ifp->Ipv4AddressSpans[0] = ByteSpan(ifp->Ipv4AddressesBuffer[0], kMaxIPv4AddrSize); @@ -184,7 +184,7 @@ CHIP_ERROR DiagnosticDataProviderImpl::GetNetworkInterfaces(NetworkInterface ** } else { - ChipLogError(DeviceLayer,"ipv4Address size (%d) not equal to kMaxIPv4AddrSize", Ipv4ByteArray.size()); + ChipLogError(DeviceLayer, "ipv4Address size (%d) not equal to kMaxIPv4AddrSize", Ipv4ByteArray.size()); } } @@ -194,7 +194,7 @@ CHIP_ERROR DiagnosticDataProviderImpl::GetNetworkInterfaces(NetworkInterface ** { JniByteArray Ipv6ByteArray(env, jIpv6AddressObj); - if(Ipv6ByteArray.size() == kMaxIPv6AddrSize) + if (Ipv6ByteArray.size() == kMaxIPv6AddrSize) { memcpy(ifp->Ipv6AddressesBuffer[0], reinterpret_cast(Ipv6ByteArray.data()), kMaxIPv6AddrSize); ifp->Ipv6AddressSpans[0] = ByteSpan(ifp->Ipv6AddressesBuffer[0], kMaxIPv6AddrSize); @@ -202,7 +202,7 @@ CHIP_ERROR DiagnosticDataProviderImpl::GetNetworkInterfaces(NetworkInterface ** } else { - ChipLogError(DeviceLayer,"ipv6Address size (%d) not equal to kMaxIPv6AddrSize", Ipv6ByteArray.size()); + ChipLogError(DeviceLayer, "ipv6Address size (%d) not equal to kMaxIPv6AddrSize", Ipv6ByteArray.size()); } }