Skip to content

Commit

Permalink
Loading ChipDeviceController JNI
Browse files Browse the repository at this point in the history
  • Loading branch information
sharadb-amazon committed Apr 3, 2024
1 parent fadc263 commit 6b8a9bc
Show file tree
Hide file tree
Showing 7 changed files with 98 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,5 @@ public MatterError stop() {

static {
System.loadLibrary("TvCastingApp");
System.loadLibrary("CHIPController");
}
}
1 change: 1 addition & 0 deletions examples/tv-casting-app/android/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ android_library("java") {
data_deps = [
":jni",
"${chip_root}/build/chip/java:shared_cpplib",
"${chip_root}/src/controller/java:jni",
]

sources = [
Expand Down
2 changes: 2 additions & 0 deletions src/app/server/java/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
3 changes: 3 additions & 0 deletions src/app/server/java/CHIPAppServer-JNI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
#include "ChipAppServerDelegate.h"
#include "ChipFabricProvider-JNI.h"
#include "ChipThreadWork.h"
#include <controller/java/CHIPDeviceController-JNI.h>
#include <jni.h>
#include <lib/core/CHIPError.h>
#include <lib/support/CHIPJNIError.h>
Expand Down Expand Up @@ -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)
Expand Down
1 change: 1 addition & 0 deletions src/controller/java/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
34 changes: 21 additions & 13 deletions src/controller/java/CHIPDeviceController-JNI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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;
Expand All @@ -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.");
Expand All @@ -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;
}

Expand Down
70 changes: 70 additions & 0 deletions src/controller/java/CHIPDeviceController-JNI.h
Original file line number Diff line number Diff line change
@@ -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 <lib/support/CHIPJNIError.h>
#include <lib/support/JniReferences.h>
#include <lib/support/JniTypeWrappers.h>

#include <app/AttributePathParams.h>
#include <app/DataVersionFilter.h>
#include <app/InteractionModelEngine.h>
#include <app/ReadClient.h>
#include <app/WriteClient.h>
#include <atomic>
#include <ble/BleUUID.h>
#include <controller/CHIPDeviceController.h>
#include <controller/CommissioningWindowOpener.h>
#include <controller/java/GroupDeviceProxy.h>
#include <credentials/CHIPCert.h>
#include <jni.h>
#include <lib/core/ErrorStr.h>
#include <lib/support/CHIPMem.h>
#include <lib/support/CodeUtils.h>
#include <lib/support/SafeInt.h>
#include <lib/support/ThreadOperationalDataset.h>
#include <lib/support/jsontlv/JsonToTlv.h>
#include <lib/support/jsontlv/TlvToJson.h>
#include <lib/support/logging/CHIPLogging.h>
#include <platform/KeyValueStoreManager.h>
#include <protocols/Protocols.h>
#include <protocols/secure_channel/SessionEstablishmentDelegate.h>
#include <pthread.h>
#include <system/SystemClock.h>
#include <vector>

#if CHIP_DEVICE_CONFIG_DYNAMIC_SERVER
#include <app/dynamic_server/AccessControl.h>
#endif // CHIP_DEVICE_CONFIG_DYNAMIC_SERVER

#ifdef JAVA_MATTER_CONTROLLER_TEST
#include <controller/ExampleOperationalCredentialsIssuer.h>
#else
#include <platform/android/AndroidChipPlatform-JNI.h>
#endif

CHIP_ERROR AndroidDeviceControllerJNI_OnLoad(JavaVM * jvm, void * reserved);

0 comments on commit 6b8a9bc

Please sign in to comment.