From 24920727efc029c089125e5cefa1e130bacdd258 Mon Sep 17 00:00:00 2001 From: joonhaengHeo <85541460+joonhaengHeo@users.noreply.github.com> Date: Wed, 26 Jul 2023 01:43:52 +0900 Subject: [PATCH] [ Java] Decouple generated API in build option (#27624) * Decouple Java generated API * Add comment * restyle * remove tlvdecoder in controller library * Restyled by whitespace * Restyled by google-java-format * Restyled by clang-format * Restyled by gn --------- Co-authored-by: Restyled.io --- build/chip/java/config.gni | 4 + src/controller/java/AndroidCallbacks.cpp | 31 ++++--- .../java/AndroidDeviceControllerWrapper.cpp | 20 ++--- src/controller/java/BUILD.gn | 61 +++++++++----- .../ChipDeviceController.java | 12 +-- .../devicecontroller/ThreadScanResult.java | 82 +++++++++++++++++++ .../chip/devicecontroller/WiFiScanResult.java | 70 ++++++++++++++++ 7 files changed, 228 insertions(+), 52 deletions(-) create mode 100644 src/controller/java/src/chip/devicecontroller/ThreadScanResult.java create mode 100644 src/controller/java/src/chip/devicecontroller/WiFiScanResult.java diff --git a/build/chip/java/config.gni b/build/chip/java/config.gni index ca18c9231fef1d..fbc1d95755863f 100644 --- a/build/chip/java/config.gni +++ b/build/chip/java/config.gni @@ -15,6 +15,9 @@ java_path = getenv("JAVA_PATH") declare_args() { java_matter_controller_dependent_paths = [] + + # The class of each cluster created by ZAP is added to the library. (e.g., ChipClusters) + matter_enable_java_generated_api = true matter_enable_java_compilation = false if (java_path != "" && current_os == "linux") { java_matter_controller_dependent_paths += [ "${java_path}/include/" ] @@ -27,6 +30,7 @@ declare_args() { [ "${java_path}/include/linux/" ] } + matter_enable_java_generated_api = false matter_enable_java_compilation = true } } diff --git a/src/controller/java/AndroidCallbacks.cpp b/src/controller/java/AndroidCallbacks.cpp index e236aef95fc41a..d55dfdf953c91c 100644 --- a/src/controller/java/AndroidCallbacks.cpp +++ b/src/controller/java/AndroidCallbacks.cpp @@ -17,8 +17,10 @@ #include "AndroidCallbacks.h" #include #include +#if USE_JAVA_TLV_ENCODE_DECODE #include #include +#endif #include #include #include @@ -243,14 +245,17 @@ void ReportCallback::OnAttributeData(const app::ConcreteDataAttributePath & aPat return; } - TLV::TLVReader readerForJavaObject; TLV::TLVReader readerForJavaTLV; TLV::TLVReader readerForJson; - readerForJavaObject.Init(*apData); readerForJavaTLV.Init(*apData); readerForJson.Init(*apData); - jobject value = DecodeAttributeValue(aPath, readerForJavaObject, &err); + jobject value = nullptr; +#if USE_JAVA_TLV_ENCODE_DECODE + TLV::TLVReader readerForJavaObject; + readerForJavaObject.Init(*apData); + + value = DecodeAttributeValue(aPath, readerForJavaObject, &err); // If we don't know this attribute, suppress it. if (err == CHIP_ERROR_IM_MALFORMED_ATTRIBUTE_PATH_IB) { @@ -260,7 +265,7 @@ void ReportCallback::OnAttributeData(const app::ConcreteDataAttributePath & aPat VerifyOrReturn(err == CHIP_NO_ERROR, ReportError(attributePathObj, nullptr, err)); VerifyOrReturn(!env->ExceptionCheck(), env->ExceptionDescribe(), ReportError(attributePathObj, nullptr, CHIP_JNI_ERROR_EXCEPTION_THROWN)); - +#endif // Create TLV byte array to pass to Java layer size_t bufferLen = readerForJavaTLV.GetRemainingLength() + readerForJavaTLV.GetLengthRead(); std::unique_ptr buffer = std::unique_ptr(new uint8_t[bufferLen]); @@ -354,10 +359,8 @@ void ReportCallback::OnEventData(const app::EventHeader & aEventHeader, TLV::TLV return; } - TLV::TLVReader readerForJavaObject; TLV::TLVReader readerForJavaTLV; TLV::TLVReader readerForJson; - readerForJavaObject.Init(*apData); readerForJavaTLV.Init(*apData); readerForJson.Init(*apData); @@ -365,7 +368,11 @@ void ReportCallback::OnEventData(const app::EventHeader & aEventHeader, TLV::TLV jlong priorityLevel = static_cast(aEventHeader.mPriorityLevel); jlong timestamp = static_cast(aEventHeader.mTimestamp.mValue); - jobject value = DecodeEventValue(aEventHeader.mPath, readerForJavaObject, &err); + jobject value = nullptr; +#if USE_JAVA_TLV_ENCODE_DECODE + TLV::TLVReader readerForJavaObject; + readerForJavaObject.Init(*apData); + value = DecodeEventValue(aEventHeader.mPath, readerForJavaObject, &err); // If we don't know this event, just skip it. if (err == CHIP_ERROR_IM_MALFORMED_EVENT_PATH_IB) { @@ -374,6 +381,7 @@ void ReportCallback::OnEventData(const app::EventHeader & aEventHeader, TLV::TLV VerifyOrReturn(err == CHIP_NO_ERROR, ReportError(nullptr, eventPathObj, err)); VerifyOrReturn(!env->ExceptionCheck(), env->ExceptionDescribe(), ReportError(nullptr, eventPathObj, CHIP_JNI_ERROR_EXCEPTION_THROWN)); +#endif // Create TLV byte array to pass to Java layer size_t bufferLen = readerForJavaTLV.GetRemainingLength() + readerForJavaTLV.GetLengthRead(); @@ -699,10 +707,8 @@ void ReportEventCallback::OnEventData(const app::EventHeader & aEventHeader, TLV return; } - TLV::TLVReader readerForJavaObject; TLV::TLVReader readerForJavaTLV; TLV::TLVReader readerForJson; - readerForJavaObject.Init(*apData); readerForJavaTLV.Init(*apData); readerForJson.Init(*apData); @@ -710,11 +716,16 @@ void ReportEventCallback::OnEventData(const app::EventHeader & aEventHeader, TLV jlong priorityLevel = static_cast(aEventHeader.mPriorityLevel); jlong timestamp = static_cast(aEventHeader.mTimestamp.mValue); - jobject value = DecodeEventValue(aEventHeader.mPath, readerForJavaObject, &err); + jobject value = nullptr; +#if USE_JAVA_TLV_ENCODE_DECODE + TLV::TLVReader readerForJavaObject; + readerForJavaObject.Init(*apData); + value = DecodeEventValue(aEventHeader.mPath, readerForJavaObject, &err); // If we don't know this event, just skip it. VerifyOrReturn(err != CHIP_ERROR_IM_MALFORMED_EVENT_PATH_IB); VerifyOrReturn(err == CHIP_NO_ERROR, ReportError(eventPathObj, err)); VerifyOrReturn(!env->ExceptionCheck(), env->ExceptionDescribe(), ReportError(eventPathObj, CHIP_JNI_ERROR_EXCEPTION_THROWN)); +#endif // Create TLV byte array to pass to Java layer size_t bufferLen = readerForJavaTLV.GetRemainingLength() + readerForJavaTLV.GetLengthRead(); diff --git a/src/controller/java/AndroidDeviceControllerWrapper.cpp b/src/controller/java/AndroidDeviceControllerWrapper.cpp index 974cb882cd2a9e..110121d6e2e41c 100644 --- a/src/controller/java/AndroidDeviceControllerWrapper.cpp +++ b/src/controller/java/AndroidDeviceControllerWrapper.cpp @@ -653,12 +653,11 @@ void AndroidDeviceControllerWrapper::OnScanNetworksSuccess( chip::JniReferences::GetInstance().CreateBoxedObject("java/lang/Integer", "(I)V", entry.rssi, newElement_rssi); jclass wiFiInterfaceScanResultStructClass; - err = chip::JniReferences::GetInstance().GetClassRef( - env, "chip/devicecontroller/ChipStructs$NetworkCommissioningClusterWiFiInterfaceScanResultStruct", - wiFiInterfaceScanResultStructClass); + err = chip::JniReferences::GetInstance().GetClassRef(env, "chip/devicecontroller/WiFiScanResult", + wiFiInterfaceScanResultStructClass); if (err != CHIP_NO_ERROR) { - ChipLogError(Zcl, "Could not find class ChipStructs$NetworkCommissioningClusterWiFiInterfaceScanResultStruct"); + ChipLogError(Zcl, "Could not find class WiFiScanResult"); return; } jmethodID wiFiInterfaceScanResultStructCtor = @@ -666,8 +665,7 @@ void AndroidDeviceControllerWrapper::OnScanNetworksSuccess( "(Ljava/lang/Integer;[B[BLjava/lang/Integer;Ljava/lang/Integer;Ljava/lang/Integer;)V"); if (wiFiInterfaceScanResultStructCtor == nullptr) { - ChipLogError(Zcl, - "Could not find ChipStructs$NetworkCommissioningClusterWiFiInterfaceScanResultStruct constructor"); + ChipLogError(Zcl, "Could not find WiFiScanResult constructor"); return; } @@ -717,12 +715,11 @@ void AndroidDeviceControllerWrapper::OnScanNetworksSuccess( chip::JniReferences::GetInstance().CreateBoxedObject("java/lang/Integer", "(I)V", entry.lqi, newElement_lqi); jclass threadInterfaceScanResultStructClass; - err = chip::JniReferences::GetInstance().GetClassRef( - env, "chip/devicecontroller/ChipStructs$NetworkCommissioningClusterThreadInterfaceScanResultStruct", - threadInterfaceScanResultStructClass); + err = chip::JniReferences::GetInstance().GetClassRef(env, "chip/devicecontroller/ThreadScanResult", + threadInterfaceScanResultStructClass); if (err != CHIP_NO_ERROR) { - ChipLogError(Zcl, "Could not find class ChipStructs$NetworkCommissioningClusterThreadInterfaceScanResultStruct"); + ChipLogError(Zcl, "Could not find class ThreadScanResult"); return; } jmethodID threadInterfaceScanResultStructCtor = @@ -731,8 +728,7 @@ void AndroidDeviceControllerWrapper::OnScanNetworksSuccess( "Integer;[BLjava/lang/Integer;Ljava/lang/Integer;)V"); if (threadInterfaceScanResultStructCtor == nullptr) { - ChipLogError(Zcl, - "Could not find ChipStructs$NetworkCommissioningClusterThreadInterfaceScanResultStruct constructor"); + ChipLogError(Zcl, "Could not find ThreadScanResult constructor"); return; } diff --git a/src/controller/java/BUILD.gn b/src/controller/java/BUILD.gn index d6c78191e37fe0..7a3f2b5b41dac8 100644 --- a/src/controller/java/BUILD.gn +++ b/src/controller/java/BUILD.gn @@ -29,6 +29,8 @@ shared_library("jni") { output_extension = "dylib" } + defines = [] + # Temporary while we have circular dependencies between codegen.py and zap # generated files check_includes = false @@ -53,23 +55,14 @@ shared_library("jni") { "AttestationTrustStoreBridge.h", "BaseCHIPCluster-JNI.cpp", "CHIPAttributeTLVValueDecoder.h", - "CHIPDefaultCallbacks.cpp", - "CHIPDefaultCallbacks.h", "CHIPDeviceController-JNI.cpp", "CHIPTLVValueDecoder-JNI.cpp", "DeviceAttestationDelegateBridge.cpp", "DeviceAttestationDelegateBridge.h", - "zap-generated/CHIPAttributeTLVValueDecoder.cpp", - "zap-generated/CHIPClustersWrite-JNI.cpp", - "zap-generated/CHIPEventTLVValueDecoder.cpp", - "zap-generated/CHIPInvokeCallbacks.cpp", - "zap-generated/CHIPInvokeCallbacks.h", - "zap-generated/CHIPReadCallbacks.cpp", ] deps = [ "${chip_root}/src/controller/data_model", - "${chip_root}/src/controller/data_model:java-jni-sources", "${chip_root}/src/credentials:default_attestation_verifier", "${chip_root}/src/inet", "${chip_root}/src/lib", @@ -77,8 +70,25 @@ shared_library("jni") { "${chip_root}/src/platform", ] + if (matter_enable_java_generated_api) { + defines += [ "USE_JAVA_TLV_ENCODE_DECODE" ] + + sources += [ + "CHIPDefaultCallbacks.cpp", + "CHIPDefaultCallbacks.h", + "zap-generated/CHIPAttributeTLVValueDecoder.cpp", + "zap-generated/CHIPClustersWrite-JNI.cpp", + "zap-generated/CHIPEventTLVValueDecoder.cpp", + "zap-generated/CHIPInvokeCallbacks.cpp", + "zap-generated/CHIPInvokeCallbacks.h", + "zap-generated/CHIPReadCallbacks.cpp", + ] + + deps += [ "${chip_root}/src/controller/data_model:java-jni-sources" ] + } + if (matter_enable_java_compilation) { - defines = [ "JAVA_MATTER_CONTROLLER_TEST" ] + defines += [ "JAVA_MATTER_CONTROLLER_TEST" ] sources += [ "${chip_root}/src/controller/ExamplePersistentStorage.cpp", @@ -313,14 +323,6 @@ android_library("java") { sources = [ "generated/java/chip/devicecontroller/ClusterIDMapping.java", - "generated/java/chip/devicecontroller/ClusterReadMapping.java", - "generated/java/chip/devicecontroller/ClusterWriteMapping.java", - "src/chip/clusterinfo/ClusterCommandCallback.java", - "src/chip/clusterinfo/ClusterInfo.java", - "src/chip/clusterinfo/CommandParameterInfo.java", - "src/chip/clusterinfo/CommandResponseInfo.java", - "src/chip/clusterinfo/DelegatedClusterCallback.java", - "src/chip/clusterinfo/InteractionInfo.java", "src/chip/devicecontroller/AttestationInfo.java", "src/chip/devicecontroller/AttestationTrustStoreDelegate.java", "src/chip/devicecontroller/CSRInfo.java", @@ -348,7 +350,9 @@ android_library("java") { "src/chip/devicecontroller/ReportEventCallbackJni.java", "src/chip/devicecontroller/ResubscriptionAttemptCallback.java", "src/chip/devicecontroller/SubscriptionEstablishedCallback.java", + "src/chip/devicecontroller/ThreadScanResult.java", "src/chip/devicecontroller/UnpairDeviceCallback.java", + "src/chip/devicecontroller/WiFiScanResult.java", "src/chip/devicecontroller/WriteAttributesCallback.java", "src/chip/devicecontroller/WriteAttributesCallbackJni.java", "src/chip/devicecontroller/model/AttributeState.java", @@ -361,12 +365,25 @@ android_library("java") { "src/chip/devicecontroller/model/EventState.java", "src/chip/devicecontroller/model/InvokeElement.java", "src/chip/devicecontroller/model/NodeState.java", - "zap-generated/chip/devicecontroller/ChipClusters.java", - "zap-generated/chip/devicecontroller/ChipEventStructs.java", - "zap-generated/chip/devicecontroller/ChipStructs.java", - "zap-generated/chip/devicecontroller/ClusterInfoMapping.java", ] + if (matter_enable_java_generated_api) { + sources += [ + "generated/java/chip/devicecontroller/ClusterReadMapping.java", + "generated/java/chip/devicecontroller/ClusterWriteMapping.java", + "src/chip/clusterinfo/ClusterCommandCallback.java", + "src/chip/clusterinfo/ClusterInfo.java", + "src/chip/clusterinfo/CommandParameterInfo.java", + "src/chip/clusterinfo/CommandResponseInfo.java", + "src/chip/clusterinfo/DelegatedClusterCallback.java", + "src/chip/clusterinfo/InteractionInfo.java", + "zap-generated/chip/devicecontroller/ChipClusters.java", + "zap-generated/chip/devicecontroller/ChipEventStructs.java", + "zap-generated/chip/devicecontroller/ChipStructs.java", + "zap-generated/chip/devicecontroller/ClusterInfoMapping.java", + ] + } + if (matter_enable_java_compilation) { deps += [ "${chip_root}/third_party/java_deps:json", diff --git a/src/controller/java/src/chip/devicecontroller/ChipDeviceController.java b/src/controller/java/src/chip/devicecontroller/ChipDeviceController.java index 916f65fb270819..33cb7a9ff3ae6e 100644 --- a/src/controller/java/src/chip/devicecontroller/ChipDeviceController.java +++ b/src/controller/java/src/chip/devicecontroller/ChipDeviceController.java @@ -389,10 +389,8 @@ public void onScanNetworksFailure(int errorCode) { public void onScanNetworksSuccess( Integer networkingStatus, Optional debugText, - Optional> - wiFiScanResults, - Optional> - threadScanResults) { + Optional> wiFiScanResults, + Optional> threadScanResults) { if (scanNetworksListener != null) { scanNetworksListener.onScanNetworksSuccess( networkingStatus, debugText, wiFiScanResults, threadScanResults); @@ -1268,10 +1266,8 @@ public interface ScanNetworksListener { void onScanNetworksSuccess( Integer networkingStatus, Optional debugText, - Optional> - wiFiScanResults, - Optional> - threadScanResults); + Optional> wiFiScanResults, + Optional> threadScanResults); } /** Interface to listen for callbacks from CHIPDeviceController. */ diff --git a/src/controller/java/src/chip/devicecontroller/ThreadScanResult.java b/src/controller/java/src/chip/devicecontroller/ThreadScanResult.java new file mode 100644 index 00000000000000..64c837bdca411d --- /dev/null +++ b/src/controller/java/src/chip/devicecontroller/ThreadScanResult.java @@ -0,0 +1,82 @@ +/* + * Copyright (c) 2020-2022 Project CHIP Authors + * All rights reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ +package chip.devicecontroller; + +import java.util.Arrays; + +public class ThreadScanResult { + public Integer panId; + public Long extendedPanId; + public String networkName; + public Integer channel; + public Integer version; + public byte[] extendedAddress; + public Integer rssi; + public Integer lqi; + + public ThreadScanResult( + Integer panId, + Long extendedPanId, + String networkName, + Integer channel, + Integer version, + byte[] extendedAddress, + Integer rssi, + Integer lqi) { + this.panId = panId; + this.extendedPanId = extendedPanId; + this.networkName = networkName; + this.channel = channel; + this.version = version; + this.extendedAddress = extendedAddress; + this.rssi = rssi; + this.lqi = lqi; + } + + @Override + public String toString() { + StringBuilder output = new StringBuilder(); + output.append("ThreadScanResult {\n"); + output.append("\tpanId: "); + output.append(panId); + output.append("\n"); + output.append("\textendedPanId: "); + output.append(extendedPanId); + output.append("\n"); + output.append("\tnetworkName: "); + output.append(networkName); + output.append("\n"); + output.append("\tchannel: "); + output.append(channel); + output.append("\n"); + output.append("\tversion: "); + output.append(version); + output.append("\n"); + output.append("\textendedAddress: "); + output.append(Arrays.toString(extendedAddress)); + output.append("\n"); + output.append("\trssi: "); + output.append(rssi); + output.append("\n"); + output.append("\tlqi: "); + output.append(lqi); + output.append("\n"); + output.append("}\n"); + return output.toString(); + } +} diff --git a/src/controller/java/src/chip/devicecontroller/WiFiScanResult.java b/src/controller/java/src/chip/devicecontroller/WiFiScanResult.java new file mode 100644 index 00000000000000..bcc01e0af26874 --- /dev/null +++ b/src/controller/java/src/chip/devicecontroller/WiFiScanResult.java @@ -0,0 +1,70 @@ +/* + * Copyright (c) 2020-2022 Project CHIP Authors + * All rights reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ +package chip.devicecontroller; + +import java.util.Arrays; + +public class WiFiScanResult { + public Integer security; + public byte[] ssid; + public byte[] bssid; + public Integer channel; + public Integer wiFiBand; + public Integer rssi; + + public WiFiScanResult( + Integer security, + byte[] ssid, + byte[] bssid, + Integer channel, + Integer wiFiBand, + Integer rssi) { + this.security = security; + this.ssid = ssid; + this.bssid = bssid; + this.channel = channel; + this.wiFiBand = wiFiBand; + this.rssi = rssi; + } + + @Override + public String toString() { + StringBuilder output = new StringBuilder(); + output.append("WiFiScanResult {\n"); + output.append("\tsecurity: "); + output.append(security); + output.append("\n"); + output.append("\tssid: "); + output.append(Arrays.toString(ssid)); + output.append("\n"); + output.append("\tbssid: "); + output.append(Arrays.toString(bssid)); + output.append("\n"); + output.append("\tchannel: "); + output.append(channel); + output.append("\n"); + output.append("\twiFiBand: "); + output.append(wiFiBand); + output.append("\n"); + output.append("\trssi: "); + output.append(rssi); + output.append("\n"); + output.append("}\n"); + return output.toString(); + } +}