diff --git a/examples/tv-casting-app/android/App/app/libs/README.md b/examples/tv-casting-app/android/App/app/libs/README.md new file mode 100644 index 00000000000000..ca16fbe7e23cf9 --- /dev/null +++ b/examples/tv-casting-app/android/App/app/libs/README.md @@ -0,0 +1,2 @@ +This directory will contain any .jar files required by the TV Casting demo app +for Android. 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 e0c326f93026c9..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 @@ -20,7 +20,6 @@ import android.content.Context; import android.util.Log; import chip.appserver.ChipAppServer; -import chip.devicecontroller.ChipInteractionClient; import chip.platform.AndroidBleManager; import chip.platform.AndroidChipPlatform; import chip.platform.ChipMdnsCallbackImpl; @@ -48,8 +47,7 @@ public final class CastingApp { private NsdManagerServiceResolver.NsdManagerResolverAvailState nsdManagerResolverAvailState; private ChipAppServer chipAppServer; - private CastingApp() { // ChipInteractionClient.loadJni(); - } + private CastingApp() {} public static CastingApp getInstance() { if (sInstance == null) { @@ -103,7 +101,6 @@ public MatterError initialize(AppParameters appParameters) { if (err.hasNoError()) { chipAppServer = new ChipAppServer(); // get a reference to the Matter server now - ChipInteractionClient.loadJni(); mState = CastingAppState.NOT_RUNNING; // initialization done, set state to NOT_RUNNING } return err; diff --git a/examples/tv-casting-app/android/App/app/src/main/jni/cpp/TvCastingApp-JNI.cpp b/examples/tv-casting-app/android/App/app/src/main/jni/cpp/TvCastingApp-JNI.cpp index 15cb211501f31d..2b4f274b687357 100644 --- a/examples/tv-casting-app/android/App/app/src/main/jni/cpp/TvCastingApp-JNI.cpp +++ b/examples/tv-casting-app/android/App/app/src/main/jni/cpp/TvCastingApp-JNI.cpp @@ -46,17 +46,14 @@ TvCastingAppJNI TvCastingAppJNI::sInstance; jint JNI_OnLoad(JavaVM * jvm, void * reserved) { jint ret = AndroidAppServerJNI_OnLoad(jvm, reserved); - if (ret != JNI_VERSION_1_6) - { - return ret; - } - + VerifyOrReturnValue(ret != JNI_ERR, ret); return AndroidChipInteractionJNI_OnLoad(jvm, reserved); } void JNI_OnUnload(JavaVM * jvm, void * reserved) { - return AndroidAppServerJNI_OnUnload(jvm, reserved); + AndroidChipInteractionJNI_OnUnload(jvm, reserved); + AndroidAppServerJNI_OnUnload(jvm, reserved); } JNI_METHOD(jboolean, preInitJni)(JNIEnv *, jobject, jobject jAppParameters) diff --git a/examples/tv-casting-app/android/App/app/src/main/jni/cpp/support/MatterCallback-JNI.h b/examples/tv-casting-app/android/App/app/src/main/jni/cpp/support/MatterCallback-JNI.h index 82894e075ea912..17e45546f5a9e9 100644 --- a/examples/tv-casting-app/android/App/app/src/main/jni/cpp/support/MatterCallback-JNI.h +++ b/examples/tv-casting-app/android/App/app/src/main/jni/cpp/support/MatterCallback-JNI.h @@ -43,7 +43,10 @@ class MatterCallbackJNI ChipLogProgress(AppServer, "MatterCallbackJNI::SetUp called"); VerifyOrReturnError(env != nullptr, CHIP_JNI_ERROR_NO_ENV, ChipLogError(AppServer, "JNIEnv was null!")); - mCallbackObject.Reset(); + if (mCallbackObject.HasValidObjectRef()) + { + mCallbackObject.Reset(); + } ReturnErrorOnFailure(mCallbackObject.Init(inCallback)); jclass mClazz = env->GetObjectClass(mCallbackObject.ObjectRef()); diff --git a/examples/tv-casting-app/android/BUILD.gn b/examples/tv-casting-app/android/BUILD.gn index 58ec925a6971ba..416570417b0794 100644 --- a/examples/tv-casting-app/android/BUILD.gn +++ b/examples/tv-casting-app/android/BUILD.gn @@ -55,7 +55,7 @@ shared_library("jni") { "${chip_root}/examples/tv-casting-app/tv-casting-common", "${chip_root}/src/app/data-model:heap", "${chip_root}/src/app/server/java:jni", - "${chip_root}/src/controller/java:android_chip_im_jni_src", + "${chip_root}/src/controller/java:android_chip_im_jni", "${chip_root}/src/lib", "${chip_root}/third_party/inipp", ] @@ -122,10 +122,7 @@ android_library("java") { "App/app/src/main/jni/com/matter/casting/support/MatterError.java", ] - javac_flags = [ - "-Xlint:deprecation", - "-parameters", # Store infomation about method parameters - ] + javac_flags = [ "-Xlint:deprecation" ] # TODO: add classpath support (we likely need to add something like # ..../platforms/android-26/android.jar to access BLE items) diff --git a/scripts/build/builders/android.py b/scripts/build/builders/android.py index 1abb50067ffbd4..bd2c90c28136d6 100644 --- a/scripts/build/builders/android.py +++ b/scripts/build/builders/android.py @@ -243,6 +243,7 @@ def copyToSrcAndroid(self): jars = { "CHIPController.jar": "src/controller/java/CHIPController.jar", + "CHIPInteractionModel.jar": "src/controller/java/CHIPInteractionModel.jar", "OnboardingPayload.jar": "src/controller/java/OnboardingPayload.jar", "AndroidPlatform.jar": "src/platform/android/AndroidPlatform.jar", "libMatterJson.jar": "src/controller/java/libMatterJson.jar", @@ -587,12 +588,6 @@ def build_outputs(self): self.output_dir, "content-app", "outputs", "apk", "debug", "content-app-debug.apk" ) } - elif self.app == AndroidApp.TV_CASTING_APP: - outputs = { - self.app.AppName() + "app-debug.apk": os.path.join( - self.output_dir, "tv-casting-app", "app", "outputs", "apk", "debug", "app-debug.apk" - ), - } elif self.app == AndroidApp.VIRTUAL_DEVICE_APP: outputs = { self.app.AppName() + "app-debug.apk": os.path.join( @@ -613,6 +608,9 @@ def build_outputs(self): "CHIPController.jar": os.path.join( self.output_dir, "lib", "src/controller/java/CHIPController.jar" ), + "CHIPInteractionModel.jar": os.path.join( + self.output_dir, "lib", "src/controller/java/CHIPInteractionModel.jar" + ), "libMatterTlv.jar": os.path.join( self.output_dir, "lib", "src/controller/java/libMatterTlv.jar" ), diff --git a/src/controller/java/BUILD.gn b/src/controller/java/BUILD.gn index 5714c91e877f0a..a84cb0cb8be835 100644 --- a/src/controller/java/BUILD.gn +++ b/src/controller/java/BUILD.gn @@ -42,9 +42,7 @@ if (!matter_enable_java_compilation) { import("${build_root}/config/android_abi.gni") } -source_set("android_chip_im_jni_src") { - output_name = "libCHIPInteractionModel" - +source_set("android_chip_im_jni") { sources = [ "AndroidCallbacks-JNI.cpp", "AndroidCallbacks.cpp", @@ -71,68 +69,10 @@ source_set("android_chip_im_jni_src") { ] } - public_deps = [ - "${chip_root}/src/app/data-model:heap", - "${chip_root}/src/app/server/java:jni", - "${chip_root}/src/lib", - "${chip_root}/src/lib/support/jsontlv", - "${chip_root}/third_party/inipp", - ] -} - -static_library("android_chip_im_jni") { - output_name = "libCHIPInteractionModel" - - public_deps = [ - "${chip_root}/src/app/data-model:heap", - "${chip_root}/src/app/server/java:jni", + deps = [ "${chip_root}/src/lib", "${chip_root}/src/lib/support/jsontlv", - "${chip_root}/third_party/inipp", ] - - deps = [ ":android_chip_im_jni_src" ] - - if (chip_build_controller_dynamic_server) { - defines += [ "CHIP_CONFIG_SKIP_APP_SPECIFIC_GENERATED_HEADER_INCLUDES=1" ] - - deps += [ "${chip_root}/src/controller:nodatamodel" ] - } else { - #deps += [ "${chip_root}/src/controller/data_model" ] - } - - if (matter_enable_java_compilation) { - defines += [ "JAVA_MATTER_CONTROLLER_TEST" ] - - include_dirs = java_matter_controller_dependent_paths - include_dirs += [ "${chip_root}/zzz_generated/controller-clusters" ] - - deps += [ "${chip_root}/third_party/inipp" ] - - if (current_os == "mac") { - deps += [ "${chip_root}/src/platform/Darwin" ] - } else { - deps += [ "${chip_root}/src/platform/Linux" ] - } - - cflags = [ - "-Wno-unknown-pragmas", - "-Wconversion", - ] - - #output_dir = "${root_out_dir}/lib/jni" - } else { - deps += [ "${chip_root}/src/platform/android" ] - #output_dir = "${root_out_dir}/lib/jni/${android_abi}" - } - - if (current_os == "mac") { - ldflags = [ "-Wl,-dead_strip" ] - } else { - ldflags = [ "-Wl,--gc-sections" ] - } - - public_configs = [ "${chip_root}/src:includes" ] } shared_library("jni") { @@ -173,7 +113,7 @@ shared_library("jni") { ] deps = [ - ":android_chip_im", + ":android_chip_im_jni", ":controller_config", "${chip_root}/src/app/icd/client:handler", "${chip_root}/src/app/icd/client:manager", @@ -534,12 +474,13 @@ android_library("chipclusterID") { ] } +# Android CHIP IM .java files android_library("android_chip_im") { output_name = "CHIPInteractionModel.jar" - # Android CHIP IM .java files sources = [ "${chip_root}/src/controller/java/src/chip/devicecontroller/ChipClusterException.java", + "${chip_root}/src/controller/java/src/chip/devicecontroller/ChipDeviceControllerException.java", "${chip_root}/src/controller/java/src/chip/devicecontroller/ChipInteractionClient.java", "${chip_root}/src/controller/java/src/chip/devicecontroller/ExtendableInvokeCallback.java", "${chip_root}/src/controller/java/src/chip/devicecontroller/ExtendableInvokeCallbackJni.java", @@ -608,8 +549,6 @@ android_library("android_chip_im") { data_deps = [ "${chip_root}/build/chip/java:shared_cpplib" ] } - data_deps += [ ":android_chip_im_jni" ] - javac_flags = [ "-Xlint:deprecation", "-parameters", # Store infomation about method parameters @@ -620,6 +559,7 @@ android_library("java") { output_name = "CHIPController.jar" deps = [ + ":android_chip_im", ":chipcluster", ":chipclusterID", "${chip_root}/third_party/java_deps:annotation", diff --git a/src/controller/java/CHIPInteractionClient-JNI.cpp b/src/controller/java/CHIPInteractionClient-JNI.cpp index 77f4c4edc69328..ea0a9176ced7dd 100644 --- a/src/controller/java/CHIPInteractionClient-JNI.cpp +++ b/src/controller/java/CHIPInteractionClient-JNI.cpp @@ -17,23 +17,20 @@ */ #include "CHIPInteractionClient-JNI.h" #include "AndroidInteractionClient.h" + +#include #include +#include #define JNI_METHOD(RETURN, METHOD_NAME) \ extern "C" JNIEXPORT RETURN JNICALL Java_chip_devicecontroller_ChipInteractionClient_##METHOD_NAME -/*jint JNI_OnLoad(JavaVM * jvm, void * reserved) -{ - return JNI_VERSION_1_6; - //return AndroidChipInteractionJNI_OnLoad(jvm, reserved); -}*/ - jint AndroidChipInteractionJNI_OnLoad(JavaVM * jvm, void * reserved) { CHIP_ERROR err = CHIP_NO_ERROR; JNIEnv * env; - ChipLogProgress(Controller, "ChipInteractionClient JNI_OnLoad() called"); + ChipLogProgress(Controller, "AndroidChipInteractionJNI_OnLoad called"); chip::Platform::MemoryInit(); @@ -42,33 +39,37 @@ jint AndroidChipInteractionJNI_OnLoad(JavaVM * jvm, void * reserved) // Get a JNI environment object. env = chip::JniReferences::GetInstance().GetEnvForCurrentThread(); - // temporarily commented out - // VerifyOrExit(env != nullptr, err = CHIP_JNI_ERROR_NO_ENV); - // VerifyOrExit(env != nullptr, err = CHIP_JNI_ERROR_NO_ENV); + VerifyOrExit(env != nullptr, err = CHIP_JNI_ERROR_NO_ENV); + VerifyOrExit(env != nullptr, err = CHIP_JNI_ERROR_NO_ENV); - ChipLogProgress(Controller, "ChipInteractionClient Loading Java class references."); + ChipLogProgress(Controller, "Loading Java class references."); // Get various class references need by the API. jclass controllerExceptionCls; - err = chip::JniReferences::GetInstance().GetLocalClassRef(env, "chip/devicecontroller/ChipClusterException", + err = chip::JniReferences::GetInstance().GetLocalClassRef(env, "chip/devicecontroller/ChipDeviceControllerException", controllerExceptionCls); - // temporarily commented out - // SuccessOrExit(err = sChipDeviceControllerExceptionCls.Init(controllerExceptionCls)); + VerifyOrReturnValue(err == CHIP_NO_ERROR, JNI_ERR); - ChipLogProgress(Controller, "ChipInteractionClient Java class references loaded."); + ChipLogProgress(Controller, "Java class references loaded."); exit: if (err != CHIP_NO_ERROR) { - // temporarily commented out - // JniReferences::GetInstance().ThrowError(env, sChipDeviceControllerExceptionCls, err); - // chip::DeviceLayer::StackUnlock unlock; - // JNI_OnUnload(jvm, reserved); + chip::JniReferences::GetInstance().ThrowError(env, controllerExceptionCls, err); + chip::DeviceLayer::StackUnlock unlock; + JNI_OnUnload(jvm, reserved); } return (err == CHIP_NO_ERROR) ? JNI_VERSION_1_6 : JNI_ERR; } +void AndroidChipInteractionJNI_OnUnload(JavaVM * jvm, void * reserved) +{ + chip::DeviceLayer::StackLock lock; + ChipLogProgress(AppServer, "AndroidChipInteractionJNI_OnUnload() called"); + chip::Platform::MemoryShutdown(); +} + JNI_METHOD(void, subscribe) (JNIEnv * env, jobject self, jlong handle, jlong callbackHandle, jlong devicePtr, jobject attributePathList, jobject eventPathList, jint minInterval, jint maxInterval, jboolean keepSubscriptions, jboolean isFabricFiltered, jint imTimeoutMs) diff --git a/src/controller/java/CHIPInteractionClient-JNI.h b/src/controller/java/CHIPInteractionClient-JNI.h index 968b8c124fc4bb..1944552d192b39 100644 --- a/src/controller/java/CHIPInteractionClient-JNI.h +++ b/src/controller/java/CHIPInteractionClient-JNI.h @@ -1,6 +1,6 @@ /* * - * Copyright (c) 2021 Project CHIP Authors + * Copyright (c) 2024 Project CHIP Authors * All rights reserved. * * Licensed under the Apache License, Version 2.0 (the "License"); @@ -20,3 +20,5 @@ #include jint AndroidChipInteractionJNI_OnLoad(JavaVM * jvm, void * reserved); + +void AndroidChipInteractionJNI_OnUnload(JavaVM * jvm, void * reserved); diff --git a/src/controller/java/src/chip/devicecontroller/ChipDeviceController.java b/src/controller/java/src/chip/devicecontroller/ChipDeviceController.java index 8928b8cc00b29e..3be1a69ca04b41 100644 --- a/src/controller/java/src/chip/devicecontroller/ChipDeviceController.java +++ b/src/controller/java/src/chip/devicecontroller/ChipDeviceController.java @@ -1369,6 +1369,15 @@ public PaseVerifierParams computePaseVerifier( return computePaseVerifier(deviceControllerPtr, devicePtr, setupPincode, iterations, salt); } + public void shutdownCommissioning() { + shutdownCommissioning(deviceControllerPtr); + } + + public static native byte[] validateAndExtractCSR(byte[] csrElements, byte[] csrNonce); + + private native PaseVerifierParams computePaseVerifier( + long deviceControllerPtr, long devicePtr, long setupPincode, long iterations, byte[] salt); + static native void subscribe( long deviceControllerPtr, long callbackHandle, @@ -1418,15 +1427,6 @@ static native void extendableInvoke( int timedRequestTimeoutMs, int imTimeoutMs); - public void shutdownCommissioning() { - shutdownCommissioning(deviceControllerPtr); - } - - public static native byte[] validateAndExtractCSR(byte[] csrElements, byte[] csrNonce); - - private native PaseVerifierParams computePaseVerifier( - long deviceControllerPtr, long devicePtr, long setupPincode, long iterations, byte[] salt); - private native long newDeviceController(ControllerParams params); private native void setDeviceAttestationDelegate( diff --git a/src/controller/java/src/chip/devicecontroller/ChipInteractionClient.java b/src/controller/java/src/chip/devicecontroller/ChipInteractionClient.java index ef37d5870e257e..0d316c5f4f609f 100644 --- a/src/controller/java/src/chip/devicecontroller/ChipInteractionClient.java +++ b/src/controller/java/src/chip/devicecontroller/ChipInteractionClient.java @@ -1,3 +1,19 @@ +/* + * + * Copyright (c) 2024 Project CHIP Authors + * + * 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.AttributeWriteRequest; @@ -61,10 +77,4 @@ static native void extendableInvoke( public static void loadJni() { System.out.println("ChipInteractionClient loadJni called"); } - - /*static { - System.out.println("Loading CHIPInteractionModel"); - System.loadLibrary("CHIPInteractionModel"); - System.out.println("Loading CHIPInteractionModel done"); - }*/ }