diff --git a/src/controller/java/AndroidCallbacks-JNI.cpp b/src/controller/java/AndroidCallbacks-JNI.cpp index 57aadcc8c4bbcd..f0da85a3c6d29e 100644 --- a/src/controller/java/AndroidCallbacks-JNI.cpp +++ b/src/controller/java/AndroidCallbacks-JNI.cpp @@ -59,24 +59,6 @@ JNI_METHOD(void, ReportCallbackJni, deleteCallback)(JNIEnv * env, jobject self, chip::Platform::Delete(reportCallback); } -JNI_METHOD(jlong, ReportEventCallbackJni, newCallback) -(JNIEnv * env, jobject self, jobject subscriptionEstablishedCallbackJava, jobject reportCallbackJava, - jobject resubscriptionAttemptCallbackJava) -{ - chip::DeviceLayer::StackLock lock; - ReportEventCallback * reportCallback = chip::Platform::New( - self, subscriptionEstablishedCallbackJava, reportCallbackJava, resubscriptionAttemptCallbackJava); - return reinterpret_cast(reportCallback); -} - -JNI_METHOD(void, ReportEventCallbackJni, deleteCallback)(JNIEnv * env, jobject self, jlong callbackHandle) -{ - chip::DeviceLayer::StackLock lock; - ReportEventCallback * reportCallback = reinterpret_cast(callbackHandle); - VerifyOrReturn(reportCallback != nullptr, ChipLogError(Controller, "ReportCallback handle is nullptr")); - delete reportCallback; -} - JNI_METHOD(jlong, WriteAttributesCallbackJni, newCallback) (JNIEnv * env, jobject self, jobject writeAttributesCallbackJava) { diff --git a/src/controller/java/AndroidCallbacks.cpp b/src/controller/java/AndroidCallbacks.cpp index 4d8e536bfde4c5..5d787dc35c45cc 100644 --- a/src/controller/java/AndroidCallbacks.cpp +++ b/src/controller/java/AndroidCallbacks.cpp @@ -612,262 +612,6 @@ void ReportCallback::ReportError(jobject attributePath, jobject eventPath, const VerifyOrReturn(!env->ExceptionCheck(), env->ExceptionDescribe()); } -ReportEventCallback::ReportEventCallback(jobject wrapperCallback, jobject subscriptionEstablishedCallback, jobject reportCallback, - jobject resubscriptionAttemptCallback) : - mBufferedReadAdapter(*this) -{ - JNIEnv * env = JniReferences::GetInstance().GetEnvForCurrentThread(); - VerifyOrReturn(env != nullptr, ChipLogError(Controller, "Could not get JNIEnv for current thread")); - if (subscriptionEstablishedCallback != nullptr) - { - mSubscriptionEstablishedCallbackRef = env->NewGlobalRef(subscriptionEstablishedCallback); - if (mSubscriptionEstablishedCallbackRef == nullptr) - { - ChipLogError(Controller, "Could not create global reference for Java callback"); - } - } - mReportCallbackRef = env->NewGlobalRef(reportCallback); - if (mReportCallbackRef == nullptr) - { - ChipLogError(Controller, "Could not create global reference for Java callback"); - } - mWrapperCallbackRef = env->NewGlobalRef(wrapperCallback); - if (mWrapperCallbackRef == nullptr) - { - ChipLogError(Controller, "Could not create global reference for Java callback"); - } - if (resubscriptionAttemptCallback != nullptr) - { - mResubscriptionAttemptCallbackRef = env->NewGlobalRef(resubscriptionAttemptCallback); - if (mResubscriptionAttemptCallbackRef == nullptr) - { - ChipLogError(Controller, "Could not create global reference for Java callback"); - } - } -} - -ReportEventCallback::~ReportEventCallback() -{ - JNIEnv * env = JniReferences::GetInstance().GetEnvForCurrentThread(); - VerifyOrReturn(env != nullptr, ChipLogError(Controller, "Could not get JNIEnv for current thread")); - if (mSubscriptionEstablishedCallbackRef != nullptr) - { - env->DeleteGlobalRef(mSubscriptionEstablishedCallbackRef); - } - env->DeleteGlobalRef(mReportCallbackRef); - if (mReadClient != nullptr) - { - Platform::Delete(mReadClient); - } -} - -void ReportEventCallback::OnReportBegin() -{ - JNIEnv * env = JniReferences::GetInstance().GetEnvForCurrentThread(); - CHIP_ERROR err = CHIP_NO_ERROR; - - err = JniReferences::GetInstance().GetClassRef(env, "chip/devicecontroller/model/NodeState", mNodeStateCls); - VerifyOrReturn(err == CHIP_NO_ERROR, ChipLogError(Controller, "Could not get NodeState class")); - jmethodID nodeStateCtor = env->GetMethodID(mNodeStateCls, "", "(Ljava/util/Map;)V"); - VerifyOrReturn(nodeStateCtor != nullptr, ChipLogError(Controller, "Could not find NodeState constructor")); - - jobject map = nullptr; - err = JniReferences::GetInstance().CreateHashMap(map); - VerifyOrReturn(err == CHIP_NO_ERROR, ChipLogError(Controller, "Could not create HashMap")); - mNodeStateObj = env->NewObject(mNodeStateCls, nodeStateCtor, map); -} - -void ReportEventCallback::OnReportEnd() -{ - // Transform C++ jobject pair list to a Java HashMap, and call onReport() on the Java callback. - CHIP_ERROR err = CHIP_NO_ERROR; - JNIEnv * env = JniReferences::GetInstance().GetEnvForCurrentThread(); - - jmethodID onReportMethod; - err = JniReferences::GetInstance().FindMethod(env, mReportCallbackRef, "onReport", "(Lchip/devicecontroller/model/NodeState;)V", - &onReportMethod); - VerifyOrReturn(err == CHIP_NO_ERROR, ChipLogError(Controller, "Could not find onReport method")); - - DeviceLayer::StackUnlock unlock; - env->CallVoidMethod(mReportCallbackRef, onReportMethod, mNodeStateObj); -} - -void ReportEventCallback::OnEventData(const app::EventHeader & aEventHeader, TLV::TLVReader * apData, - const app::StatusIB * apStatus) -{ - CHIP_ERROR err = CHIP_NO_ERROR; - JNIEnv * env = JniReferences::GetInstance().GetEnvForCurrentThread(); - jobject eventPathObj = nullptr; - err = CreateChipEventPath(aEventHeader.mPath, eventPathObj); - VerifyOrReturn(err == CHIP_NO_ERROR, ChipLogError(Controller, "Unable to create Java ChipEventPath: %s", ErrorStr(err))); - - if (apData == nullptr) - { - ReportError(eventPathObj, CHIP_ERROR_INVALID_ARGUMENT); - return; - } - - TLV::TLVReader readerForJavaTLV; - TLV::TLVReader readerForJson; - readerForJavaTLV.Init(*apData); - readerForJson.Init(*apData); - - jlong eventNumber = static_cast(aEventHeader.mEventNumber); - jlong priorityLevel = static_cast(aEventHeader.mPriorityLevel); - jlong timestamp = static_cast(aEventHeader.mTimestamp.mValue); - - 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(); - std::unique_ptr buffer = std::unique_ptr(new uint8_t[bufferLen]); - uint32_t size = 0; - // The TLVReader's read head is not pointing to the first element in the container, instead of the container itself, use - // a TLVWriter to get a TLV with a normalized TLV buffer (Wrapped with an anonymous tag, no extra "end of container" tag - // at the end.) - TLV::TLVWriter writer; - writer.Init(buffer.get(), bufferLen); - err = writer.CopyElement(TLV::AnonymousTag(), readerForJavaTLV); - VerifyOrReturn(err == CHIP_NO_ERROR, ReportError(eventPathObj, err)); - size = writer.GetLengthWritten(); - chip::ByteArray jniByteArray(env, reinterpret_cast(buffer.get()), size); - - // Convert TLV to JSON - Json::Value json; - err = TlvToJson(readerForJson, json); - VerifyOrReturn(err == CHIP_NO_ERROR, ReportError(eventPathObj, err)); - - UtfString jsonString(env, JsonToString(json).c_str()); - - // Create EventState object - jclass eventStateCls; - err = JniReferences::GetInstance().GetClassRef(env, "chip/devicecontroller/model/EventState", eventStateCls); - VerifyOrReturn(err == CHIP_NO_ERROR, ChipLogError(Controller, "Could not find EventState class")); - VerifyOrReturn(eventStateCls != nullptr, ChipLogError(Controller, "Could not find EventState class")); - chip::JniClass eventStateJniCls(eventStateCls); - jmethodID eventStateCtor = env->GetMethodID(eventStateCls, "", "(JIJLjava/lang/Object;[BLjava/lang/String;)V"); - VerifyOrReturn(eventStateCtor != nullptr, ChipLogError(Controller, "Could not find EventState constructor")); - jobject eventStateObj = env->NewObject(eventStateCls, eventStateCtor, eventNumber, priorityLevel, timestamp, value, - jniByteArray.jniValue(), jsonString.jniValue()); - VerifyOrReturn(eventStateObj != nullptr, ChipLogError(Controller, "Could not create EventState object")); - - // Add EventState to NodeState - jmethodID addEventMethod; - err = JniReferences::GetInstance().FindMethod(env, mNodeStateObj, "addEvent", "(IJJLchip/devicecontroller/model/EventState;)V", - &addEventMethod); - VerifyOrReturn(err == CHIP_NO_ERROR, ChipLogError(Controller, "Could not find addEvent method")); - env->CallVoidMethod(mNodeStateObj, addEventMethod, static_cast(aEventHeader.mPath.mEndpointId), - static_cast(aEventHeader.mPath.mClusterId), static_cast(aEventHeader.mPath.mEventId), - eventStateObj); - VerifyOrReturn(!env->ExceptionCheck(), env->ExceptionDescribe()); -} - -CHIP_ERROR ReportEventCallback::CreateChipEventPath(const app::ConcreteEventPath & aPath, jobject & outObj) -{ - JNIEnv * env = JniReferences::GetInstance().GetEnvForCurrentThread(); - CHIP_ERROR err = CHIP_NO_ERROR; - - jclass eventPathCls = nullptr; - err = JniReferences::GetInstance().GetClassRef(env, "chip/devicecontroller/model/ChipEventPath", eventPathCls); - VerifyOrReturnError(err == CHIP_NO_ERROR, err); - JniClass eventPathJniCls(eventPathCls); - - jmethodID eventPathCtor = - env->GetStaticMethodID(eventPathCls, "newInstance", "(IJJ)Lchip/devicecontroller/model/ChipEventPath;"); - VerifyOrReturnError(eventPathCtor != nullptr, CHIP_JNI_ERROR_METHOD_NOT_FOUND); - - outObj = env->CallStaticObjectMethod(eventPathCls, eventPathCtor, static_cast(aPath.mEndpointId), - static_cast(aPath.mClusterId), static_cast(aPath.mEventId)); - VerifyOrReturnError(outObj != nullptr, CHIP_JNI_ERROR_NULL_OBJECT); - - return err; -} - -void ReportEventCallback::OnError(CHIP_ERROR aError) -{ - ReportError(nullptr, aError); -} - -void ReportEventCallback::OnDone(app::ReadClient *) -{ - CHIP_ERROR err = CHIP_NO_ERROR; - JNIEnv * env = JniReferences::GetInstance().GetEnvForCurrentThread(); - - jmethodID onDoneMethod; - err = JniReferences::GetInstance().FindMethod(env, mReportCallbackRef, "onDone", "()V", &onDoneMethod); - VerifyOrReturn(err == CHIP_NO_ERROR, ChipLogError(Controller, "Could not find onDone method")); - - DeviceLayer::StackUnlock unlock; - env->CallVoidMethod(mReportCallbackRef, onDoneMethod); - - JniReferences::GetInstance().GetEnvForCurrentThread()->DeleteGlobalRef(mWrapperCallbackRef); -} - -void ReportEventCallback::OnSubscriptionEstablished(SubscriptionId aSubscriptionId) -{ - chip::DeviceLayer::StackUnlock unlock; - JniReferences::GetInstance().CallSubscriptionEstablished(mSubscriptionEstablishedCallbackRef, aSubscriptionId); -} - -CHIP_ERROR ReportEventCallback::OnResubscriptionNeeded(app::ReadClient * apReadClient, CHIP_ERROR aTerminationCause) -{ - VerifyOrReturnLogError(mResubscriptionAttemptCallbackRef != nullptr, CHIP_ERROR_INVALID_ARGUMENT); - - JNIEnv * env = JniReferences::GetInstance().GetEnvForCurrentThread(); - - ReturnErrorOnFailure(app::ReadClient::Callback::OnResubscriptionNeeded(apReadClient, aTerminationCause)); - - jmethodID onResubscriptionAttemptMethod; - ReturnLogErrorOnFailure(JniReferences::GetInstance().FindMethod( - env, mResubscriptionAttemptCallbackRef, "onResubscriptionAttempt", "(JJ)V", &onResubscriptionAttemptMethod)); - - DeviceLayer::StackUnlock unlock; - env->CallVoidMethod(mResubscriptionAttemptCallbackRef, onResubscriptionAttemptMethod, - static_cast(aTerminationCause.AsInteger()), - static_cast(apReadClient->ComputeTimeTillNextSubscription())); - - return CHIP_NO_ERROR; -} - -void ReportEventCallback::ReportError(jobject eventPath, CHIP_ERROR err) -{ - ReportError(eventPath, ErrorStr(err), err.AsInteger()); -} - -void ReportEventCallback::ReportError(jobject eventPath, Protocols::InteractionModel::Status status) -{ - ReportError(eventPath, "IM Status", static_cast>(status)); -} - -void ReportEventCallback::ReportError(jobject eventPath, const char * message, ChipError::StorageType errorCode) -{ - CHIP_ERROR err = CHIP_NO_ERROR; - JNIEnv * env = JniReferences::GetInstance().GetEnvForCurrentThread(); - - jthrowable exception; - err = AndroidControllerExceptions::GetInstance().CreateAndroidControllerException(env, message, errorCode, exception); - VerifyOrReturn(err == CHIP_NO_ERROR, - ChipLogError(Controller, "Unable to create AndroidControllerException: %s on eportEventCallback::ReportError", - ErrorStr(err))); - - jmethodID onErrorMethod; - err = JniReferences::GetInstance().FindMethod( - env, mReportCallbackRef, "onError", "(Lchip/devicecontroller/model/ChipEventPath;Ljava/lang/Exception;)V", &onErrorMethod); - VerifyOrReturn(err == CHIP_NO_ERROR, ChipLogError(Controller, "Unable to find onError method: %s", ErrorStr(err))); - - DeviceLayer::StackUnlock unlock; - env->CallVoidMethod(mReportCallbackRef, onErrorMethod, eventPath, exception); -} - WriteAttributesCallback::WriteAttributesCallback(jobject wrapperCallback, jobject javaCallback) : mChunkedWriteCallback(this) { JNIEnv * env = JniReferences::GetInstance().GetEnvForCurrentThread(); diff --git a/src/controller/java/BUILD.gn b/src/controller/java/BUILD.gn index 39a8ab889d48ba..ee227cf1cf4c06 100644 --- a/src/controller/java/BUILD.gn +++ b/src/controller/java/BUILD.gn @@ -345,8 +345,6 @@ android_library("java") { "src/chip/devicecontroller/PaseVerifierParams.java", "src/chip/devicecontroller/ReportCallback.java", "src/chip/devicecontroller/ReportCallbackJni.java", - "src/chip/devicecontroller/ReportEventCallback.java", - "src/chip/devicecontroller/ReportEventCallbackJni.java", "src/chip/devicecontroller/ResubscriptionAttemptCallback.java", "src/chip/devicecontroller/SubscriptionEstablishedCallback.java", "src/chip/devicecontroller/ThreadScanResult.java", diff --git a/src/controller/java/src/chip/devicecontroller/ReportEventCallback.java b/src/controller/java/src/chip/devicecontroller/ReportEventCallback.java deleted file mode 100644 index b82a355f49c464..00000000000000 --- a/src/controller/java/src/chip/devicecontroller/ReportEventCallback.java +++ /dev/null @@ -1,30 +0,0 @@ -/* - * Copyright (c) 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 chip.devicecontroller.model.ChipEventPath; -import chip.devicecontroller.model.NodeState; - -/** An interface for receiving read/subscribe CHIP reports. */ -public interface ReportEventCallback { - void onError(ChipEventPath eventPath, Exception e); - - void onReport(NodeState nodeState); - - default void onDone() {} -} diff --git a/src/controller/java/src/chip/devicecontroller/ReportEventCallbackJni.java b/src/controller/java/src/chip/devicecontroller/ReportEventCallbackJni.java deleted file mode 100644 index 1f786060b84d93..00000000000000 --- a/src/controller/java/src/chip/devicecontroller/ReportEventCallbackJni.java +++ /dev/null @@ -1,61 +0,0 @@ -/* - * Copyright (c) 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 javax.annotation.Nullable; - -/** JNI wrapper callback class for {@link ReportCallback}. */ -public class ReportEventCallbackJni { - @Nullable private SubscriptionEstablishedCallback wrappedSubscriptionEstablishedCallback; - @Nullable private ResubscriptionAttemptCallback wrappedResubscriptionAttemptCallback; - private ReportEventCallback wrappedReportCallback; - private long callbackHandle; - - public ReportEventCallbackJni( - @Nullable SubscriptionEstablishedCallback subscriptionEstablishedCallback, - ReportEventCallback reportCallback, - ResubscriptionAttemptCallback resubscriptionAttemptCallback) { - this.wrappedSubscriptionEstablishedCallback = subscriptionEstablishedCallback; - this.wrappedReportCallback = reportCallback; - this.wrappedResubscriptionAttemptCallback = resubscriptionAttemptCallback; - this.callbackHandle = - newCallback(subscriptionEstablishedCallback, reportCallback, resubscriptionAttemptCallback); - } - - long getCallbackHandle() { - return callbackHandle; - } - - private native long newCallback( - @Nullable SubscriptionEstablishedCallback subscriptionEstablishedCallback, - ReportEventCallback wrappedCallback, - @Nullable ResubscriptionAttemptCallback resubscriptionAttemptCallback); - - private native void deleteCallback(long callbackHandle); - - // TODO(#8578): Replace finalizer with PhantomReference. - @SuppressWarnings("deprecation") - protected void finalize() throws Throwable { - super.finalize(); - - if (callbackHandle != 0) { - deleteCallback(callbackHandle); - callbackHandle = 0; - } - } -}