From 6b8a9bc15405b7a3ab39c26f583e2e234fba70d7 Mon Sep 17 00:00:00 2001 From: Sharad Binjola Date: Tue, 2 Apr 2024 11:27:36 -0700 Subject: [PATCH] Loading ChipDeviceController JNI --- .../com/matter/casting/core/CastingApp.java | 1 - examples/tv-casting-app/android/BUILD.gn | 1 + src/app/server/java/BUILD.gn | 2 + src/app/server/java/CHIPAppServer-JNI.cpp | 3 + src/controller/java/BUILD.gn | 1 + .../java/CHIPDeviceController-JNI.cpp | 34 +++++---- .../java/CHIPDeviceController-JNI.h | 70 +++++++++++++++++++ 7 files changed, 98 insertions(+), 14 deletions(-) create mode 100644 src/controller/java/CHIPDeviceController-JNI.h diff --git a/examples/tv-casting-app/android/App/app/src/main/jni/com/matter/casting/core/CastingApp.java b/examples/tv-casting-app/android/App/app/src/main/jni/com/matter/casting/core/CastingApp.java index 9d88d19ed24d98..f79eab859a8ce3 100644 --- a/examples/tv-casting-app/android/App/app/src/main/jni/com/matter/casting/core/CastingApp.java +++ b/examples/tv-casting-app/android/App/app/src/main/jni/com/matter/casting/core/CastingApp.java @@ -172,6 +172,5 @@ public MatterError stop() { static { System.loadLibrary("TvCastingApp"); - System.loadLibrary("CHIPController"); } } diff --git a/examples/tv-casting-app/android/BUILD.gn b/examples/tv-casting-app/android/BUILD.gn index 399519f261be44..bc1d5d33f4297d 100644 --- a/examples/tv-casting-app/android/BUILD.gn +++ b/examples/tv-casting-app/android/BUILD.gn @@ -80,6 +80,7 @@ android_library("java") { data_deps = [ ":jni", "${chip_root}/build/chip/java:shared_cpplib", + "${chip_root}/src/controller/java:jni", ] sources = [ diff --git a/src/app/server/java/BUILD.gn b/src/app/server/java/BUILD.gn index c25548eb112549..83aec6a2341d7e 100644 --- a/src/app/server/java/BUILD.gn +++ b/src/app/server/java/BUILD.gn @@ -37,6 +37,8 @@ static_library("jni") { deps = [ "${chip_root}/src/app/server", + "${chip_root}/src/controller/java", + "${chip_root}/src/controller/java:jni", "${chip_root}/src/inet", "${chip_root}/src/lib", "${chip_root}/src/platform", diff --git a/src/app/server/java/CHIPAppServer-JNI.cpp b/src/app/server/java/CHIPAppServer-JNI.cpp index 4040d4d0d22ec2..562eae10814c90 100644 --- a/src/app/server/java/CHIPAppServer-JNI.cpp +++ b/src/app/server/java/CHIPAppServer-JNI.cpp @@ -25,6 +25,7 @@ #include "ChipAppServerDelegate.h" #include "ChipFabricProvider-JNI.h" #include "ChipThreadWork.h" +#include #include #include #include @@ -88,6 +89,8 @@ jint AndroidAppServerJNI_OnLoad(JavaVM * jvm, void * reserved) SuccessOrExit(err); err = AndroidChipFabricProviderJNI_OnLoad(jvm, reserved); SuccessOrExit(err); + err = AndroidDeviceControllerJNI_OnLoad(jvm, reserved); + SuccessOrExit(err); exit: if (err != CHIP_NO_ERROR) diff --git a/src/controller/java/BUILD.gn b/src/controller/java/BUILD.gn index 53b95f74ef9878..ab3c7df147fb1a 100644 --- a/src/controller/java/BUILD.gn +++ b/src/controller/java/BUILD.gn @@ -64,6 +64,7 @@ shared_library("jni") { "BaseCHIPCluster-JNI.cpp", "CHIPAttributeTLVValueDecoder.h", "CHIPDeviceController-JNI.cpp", + "CHIPDeviceController-JNI.h", "CHIPEventTLVValueDecoder.h", "DeviceAttestation-JNI.cpp", "DeviceAttestationDelegateBridge.cpp", diff --git a/src/controller/java/CHIPDeviceController-JNI.cpp b/src/controller/java/CHIPDeviceController-JNI.cpp index a70fb188d2e196..05a600fbb592eb 100644 --- a/src/controller/java/CHIPDeviceController-JNI.cpp +++ b/src/controller/java/CHIPDeviceController-JNI.cpp @@ -21,6 +21,8 @@ * Implementation of JNI bridge for CHIP Device Controller for Android apps * */ +#include "CHIPDeviceController-JNI.h" + #include "AndroidCallbacks.h" #include "AndroidCommissioningWindowOpener.h" #include "AndroidCurrentFabricRemover.h" @@ -72,6 +74,12 @@ #define PTHREAD_NULL 0 #endif // PTHREAD_NULL +namespace { +JavaVM * sJVM = nullptr; +pthread_t sIOThread = PTHREAD_NULL; +chip::JniGlobalReference sChipDeviceControllerExceptionCls; +} // namespace + using namespace chip; using namespace chip::Inet; using namespace chip::Controller; @@ -87,41 +95,35 @@ static CHIP_ERROR StopIOThread(); static CHIP_ERROR N2J_PaseVerifierParams(JNIEnv * env, jlong setupPincode, jbyteArray pakeVerifier, jobject & outParams); static CHIP_ERROR N2J_NetworkLocation(JNIEnv * env, jstring ipAddress, jint port, jint interfaceIndex, jobject & outLocation); -namespace { -JavaVM * sJVM = nullptr; -pthread_t sIOThread = PTHREAD_NULL; -chip::JniGlobalReference sChipDeviceControllerExceptionCls; -} // namespace - // NOTE: Remote device ID is in sync with the echo server device id // At some point, we may want to add an option to connect to a device without // knowing its id, because the ID can be learned on the first response that is received. chip::NodeId kLocalDeviceId = chip::kTestControllerNodeId; chip::NodeId kRemoteDeviceId = chip::kTestDeviceNodeId; -jint JNI_OnLoad(JavaVM * jvm, void * reserved) +CHIP_ERROR AndroidDeviceControllerJNI_OnLoad(JavaVM * jvm, void * reserved) { CHIP_ERROR err = CHIP_NO_ERROR; JNIEnv * env; - ChipLogProgress(Controller, "JNI_OnLoad() called"); + ChipLogProgress(Controller, "AndroidDeviceControllerJNI_OnLoad() called"); chip::Platform::MemoryInit(); // Save a reference to the JVM. Will need this to call back into Java. - JniReferences::GetInstance().SetJavaVm(jvm, "chip/devicecontroller/ChipDeviceController"); + chip::JniReferences::GetInstance().SetJavaVm(jvm, "chip/devicecontroller/ChipDeviceController"); sJVM = jvm; // Get a JNI environment object. - env = JniReferences::GetInstance().GetEnvForCurrentThread(); + env = chip::JniReferences::GetInstance().GetEnvForCurrentThread(); VerifyOrExit(env != nullptr, err = CHIP_JNI_ERROR_NO_ENV); ChipLogProgress(Controller, "Loading Java class references."); // Get various class references need by the API. jclass controllerExceptionCls; - err = JniReferences::GetInstance().GetLocalClassRef(env, "chip/devicecontroller/ChipDeviceControllerException", - controllerExceptionCls); + err = chip::JniReferences::GetInstance().GetLocalClassRef(env, "chip/devicecontroller/ChipDeviceControllerException", + controllerExceptionCls); SuccessOrExit(err = sChipDeviceControllerExceptionCls.Init(controllerExceptionCls)); ChipLogProgress(Controller, "Java class references loaded."); @@ -138,11 +140,17 @@ jint JNI_OnLoad(JavaVM * jvm, void * reserved) exit: if (err != CHIP_NO_ERROR) { - JniReferences::GetInstance().ThrowError(env, sChipDeviceControllerExceptionCls, err); + chip::JniReferences::GetInstance().ThrowError(env, sChipDeviceControllerExceptionCls, err); chip::DeviceLayer::StackUnlock unlock; JNI_OnUnload(jvm, reserved); } + return err; +} + +jint JNI_OnLoad(JavaVM * jvm, void * reserved) +{ + CHIP_ERROR err = AndroidDeviceControllerJNI_OnLoad(jvm, reserved); return (err == CHIP_NO_ERROR) ? JNI_VERSION_1_6 : JNI_ERR; } diff --git a/src/controller/java/CHIPDeviceController-JNI.h b/src/controller/java/CHIPDeviceController-JNI.h new file mode 100644 index 00000000000000..c1c4fbd6e41601 --- /dev/null +++ b/src/controller/java/CHIPDeviceController-JNI.h @@ -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. + * + */ + +/** + * @file + * Implementation of JNI bridge for CHIP Device Controller for Android apps + * + */ +#include "AndroidCallbacks.h" +#include "AndroidCommissioningWindowOpener.h" +#include "AndroidCurrentFabricRemover.h" +#include "AndroidDeviceControllerWrapper.h" +#include "AndroidInteractionClient.h" +#include +#include +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#if CHIP_DEVICE_CONFIG_DYNAMIC_SERVER +#include +#endif // CHIP_DEVICE_CONFIG_DYNAMIC_SERVER + +#ifdef JAVA_MATTER_CONTROLLER_TEST +#include +#else +#include +#endif + +CHIP_ERROR AndroidDeviceControllerJNI_OnLoad(JavaVM * jvm, void * reserved);