Skip to content

Commit

Permalink
[ Java] Decouple generated API in build option (#27624)
Browse files Browse the repository at this point in the history
* 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 <[email protected]>
  • Loading branch information
2 people authored and pull[bot] committed Jan 8, 2024
1 parent 48a4628 commit 2492072
Show file tree
Hide file tree
Showing 7 changed files with 228 additions and 52 deletions.
4 changes: 4 additions & 0 deletions build/chip/java/config.gni
Original file line number Diff line number Diff line change
Expand Up @@ -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/" ]
Expand All @@ -27,6 +30,7 @@ declare_args() {
[ "${java_path}/include/linux/" ]
}

matter_enable_java_generated_api = false
matter_enable_java_compilation = true
}
}
31 changes: 21 additions & 10 deletions src/controller/java/AndroidCallbacks.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,10 @@
#include "AndroidCallbacks.h"
#include <controller/java/AndroidClusterExceptions.h>
#include <controller/java/AndroidControllerExceptions.h>
#if USE_JAVA_TLV_ENCODE_DECODE
#include <controller/java/CHIPAttributeTLVValueDecoder.h>
#include <controller/java/CHIPEventTLVValueDecoder.h>
#endif
#include <jni.h>
#include <lib/support/CHIPJNIError.h>
#include <lib/support/CodeUtils.h>
Expand Down Expand Up @@ -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)
{
Expand All @@ -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<uint8_t[]> buffer = std::unique_ptr<uint8_t[]>(new uint8_t[bufferLen]);
Expand Down Expand Up @@ -354,18 +359,20 @@ 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);

jlong eventNumber = static_cast<jlong>(aEventHeader.mEventNumber);
jlong priorityLevel = static_cast<jint>(aEventHeader.mPriorityLevel);
jlong timestamp = static_cast<jlong>(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)
{
Expand All @@ -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();
Expand Down Expand Up @@ -699,22 +707,25 @@ 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);

jlong eventNumber = static_cast<jlong>(aEventHeader.mEventNumber);
jlong priorityLevel = static_cast<jint>(aEventHeader.mPriorityLevel);
jlong timestamp = static_cast<jlong>(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();
Expand Down
20 changes: 8 additions & 12 deletions src/controller/java/AndroidDeviceControllerWrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -653,21 +653,19 @@ void AndroidDeviceControllerWrapper::OnScanNetworksSuccess(
chip::JniReferences::GetInstance().CreateBoxedObject<int8_t>("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 =
env->GetMethodID(wiFiInterfaceScanResultStructClass, "<init>",
"(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;
}

Expand Down Expand Up @@ -717,12 +715,11 @@ void AndroidDeviceControllerWrapper::OnScanNetworksSuccess(
chip::JniReferences::GetInstance().CreateBoxedObject<uint8_t>("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 =
Expand All @@ -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;
}

Expand Down
61 changes: 39 additions & 22 deletions src/controller/java/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -53,32 +55,40 @@ 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",
"${chip_root}/src/lib/support/jsontlv",
"${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",
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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",
Expand All @@ -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",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -389,10 +389,8 @@ public void onScanNetworksFailure(int errorCode) {
public void onScanNetworksSuccess(
Integer networkingStatus,
Optional<String> debugText,
Optional<ArrayList<ChipStructs.NetworkCommissioningClusterWiFiInterfaceScanResultStruct>>
wiFiScanResults,
Optional<ArrayList<ChipStructs.NetworkCommissioningClusterThreadInterfaceScanResultStruct>>
threadScanResults) {
Optional<ArrayList<WiFiScanResult>> wiFiScanResults,
Optional<ArrayList<ThreadScanResult>> threadScanResults) {
if (scanNetworksListener != null) {
scanNetworksListener.onScanNetworksSuccess(
networkingStatus, debugText, wiFiScanResults, threadScanResults);
Expand Down Expand Up @@ -1268,10 +1266,8 @@ public interface ScanNetworksListener {
void onScanNetworksSuccess(
Integer networkingStatus,
Optional<String> debugText,
Optional<ArrayList<ChipStructs.NetworkCommissioningClusterWiFiInterfaceScanResultStruct>>
wiFiScanResults,
Optional<ArrayList<ChipStructs.NetworkCommissioningClusterThreadInterfaceScanResultStruct>>
threadScanResults);
Optional<ArrayList<WiFiScanResult>> wiFiScanResults,
Optional<ArrayList<ThreadScanResult>> threadScanResults);
}

/** Interface to listen for callbacks from CHIPDeviceController. */
Expand Down
Original file line number Diff line number Diff line change
@@ -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();
}
}
Loading

0 comments on commit 2492072

Please sign in to comment.