Skip to content

Commit

Permalink
allow commissionee device read fabric list in Android (#18664)
Browse files Browse the repository at this point in the history
* allow commissionee device read fabric list

* restyled

* add NUll check

* remove not necessary logic
  • Loading branch information
nicelyjust authored and pull[bot] committed Jun 17, 2022
1 parent f26ab7e commit 4627962
Show file tree
Hide file tree
Showing 7 changed files with 242 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/app/server/java/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ static_library("jni") {
"AndroidAppServerWrapper.cpp",
"AndroidAppServerWrapper.h",
"CHIPAppServer-JNI.cpp",
"ChipFabricProvider-JNI.cpp",
"ChipFabricProvider-JNI.h",
"ChipThreadWork.cpp",
"ChipThreadWork.h",
]
Expand Down Expand Up @@ -59,6 +61,8 @@ android_library("java") {
sources = [
"src/chip/appserver/ChipAppServer.java",
"src/chip/appserver/ChipAppServerException.java",
"src/chip/appserver/ChipFabricProvider.java",
"src/chip/appserver/Fabric.java",
]

javac_flags = [ "-Xlint:deprecation" ]
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 @@ -22,6 +22,7 @@
*
*/
#include "AndroidAppServerWrapper.h"
#include "ChipFabricProvider-JNI.h"
#include "ChipThreadWork.h"
#include <jni.h>
#include <lib/core/CHIPError.h>
Expand Down Expand Up @@ -80,6 +81,8 @@ jint AndroidAppServerJNI_OnLoad(JavaVM * jvm, void * reserved)

err = AndroidChipPlatformJNI_OnLoad(jvm, reserved);
SuccessOrExit(err);
err = AndroidChipFabricProviderJNI_OnLoad(jvm, reserved);
SuccessOrExit(err);

exit:
if (err != CHIP_NO_ERROR)
Expand Down
126 changes: 126 additions & 0 deletions src/app/server/java/ChipFabricProvider-JNI.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
/*
* 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.
*
*/

/**
* @file
* Implementation of JNI bridge for CHIP App Server for Android TV apps
*
*/
#include "ChipFabricProvider-JNI.h"
#include "AndroidAppServerWrapper.h"
#include <app/server/Server.h>
#include <cstdlib>
#include <iostream>
#include <jni.h>
#include <lib/core/CHIPError.h>
#include <lib/support/CHIPJNIError.h>
#include <lib/support/JniTypeWrappers.h>
#include <platform/CHIPDeviceLayer.h>
#include <thread>
#include <trace/trace.h>

using namespace chip;

#define JNI_METHOD(RETURN, METHOD_NAME) extern "C" JNIEXPORT RETURN JNICALL Java_chip_appserver_ChipFabricProvider_##METHOD_NAME

namespace {
JavaVM * sJVM;
} // namespace

CHIP_ERROR AndroidChipFabricProviderJNI_OnLoad(JavaVM * jvm, void * reserved)
{
CHIP_ERROR err = CHIP_NO_ERROR;
JNIEnv * env;

ChipLogProgress(DeviceLayer, "ChipFabricProvider JNI_OnLoad() called");

chip::Platform::MemoryInit();

// Save a reference to the JVM. Will need this to call back into Java.
JniReferences::GetInstance().SetJavaVm(jvm, "chip/appserver/ChipFabricProvider");
sJVM = jvm;

// check if the JNI environment is correct
env = JniReferences::GetInstance().GetEnvForCurrentThread();
VerifyOrExit(env != NULL, err = CHIP_JNI_ERROR_NO_ENV);

chip::InitializeTracing();

exit:
if (err != CHIP_NO_ERROR)
{
JNI_OnUnload(jvm, reserved);
}

return err;
}

void AndroidChipFabricProviderJNI_OnUnload(JavaVM * jvm, void * reserved)
{
ChipLogProgress(DeviceLayer, "ChipFabricProvider JNI_OnUnload() called");
chip::Platform::MemoryShutdown();
}

CHIP_ERROR ReadFabricList(JNIEnv * env, jobject & self)
{
CHIP_ERROR err = CHIP_NO_ERROR;
jclass jFabricCls = env->FindClass("chip/appserver/Fabric");
VerifyOrExit(self != nullptr, err = CHIP_JNI_ERROR_NULL_OBJECT);
VerifyOrExit(jFabricCls != nullptr, ChipLogError(NotSpecified, "could not find Class Fabric"));
for (auto & fabricInfo : Server::GetInstance().GetFabricTable())
{

jmethodID constructor = env->GetMethodID(jFabricCls, "<init>", "()V");
VerifyOrExit(constructor != nullptr, err = CHIP_JNI_ERROR_METHOD_NOT_FOUND);
jobject jFabric = env->NewObject(jFabricCls, constructor);
VerifyOrExit(!env->ExceptionCheck(), err = CHIP_JNI_ERROR_EXCEPTION_THROWN);
jfieldID jvendorId = env->GetFieldID(jFabricCls, "vendorId", "I");
VerifyOrExit(jvendorId != nullptr, err = CHIP_JNI_ERROR_FIELD_NOT_FOUND);
jfieldID jnodeId = env->GetFieldID(jFabricCls, "nodeId", "J");
VerifyOrExit(jnodeId != nullptr, err = CHIP_JNI_ERROR_FIELD_NOT_FOUND);
jfieldID jfabricIndex = env->GetFieldID(jFabricCls, "fabricIndex", "S");
VerifyOrExit(jfabricIndex != nullptr, err = CHIP_JNI_ERROR_FIELD_NOT_FOUND);
jfieldID jlabel = env->GetFieldID(jFabricCls, "label", "Ljava/lang/String;");
VerifyOrExit(jlabel != nullptr, err = CHIP_JNI_ERROR_FIELD_NOT_FOUND);

env->SetIntField(jFabric, jvendorId, fabricInfo.GetVendorId());
env->SetLongField(jFabric, jnodeId, fabricInfo.GetNodeId());
env->SetShortField(jFabric, jfabricIndex, fabricInfo.GetFabricIndex());
UtfString jLabelStr(env, fabricInfo.GetFabricLabel());
env->SetObjectField(jFabric, jlabel, jLabelStr.jniValue());

JniReferences::GetInstance().AddToList(self, jFabric);
}

exit:
return err;
}

JNI_METHOD(jint, getFabricCount)(JNIEnv * env, jobject self)
{
// a simplified way to get fabric count,see /src/credentials/FabricTable.h#FabricCount
return chip::Server::GetInstance().GetFabricTable().FabricCount();
}

JNI_METHOD(jobject, getFabricList)(JNIEnv * env, jobject self)
{
jobject jFabricList;
JniReferences::GetInstance().CreateArrayList(jFabricList);
ReadFabricList(env, jFabricList);
return jFabricList;
}
28 changes: 28 additions & 0 deletions src/app/server/java/ChipFabricProvider-JNI.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
*
* Copyright (c) 2022 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.
*/

#pragma once

#include <cstdint>
#include <jni.h>
#include <lib/core/CHIPError.h>

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

void AndroidChipFabricProviderJNI_OnUnload(JavaVM * jvm, void * reserved);

CHIP_ERROR ReadFabricList(JNIEnv * env, jobject & self);
13 changes: 13 additions & 0 deletions src/app/server/java/src/chip/appserver/ChipAppServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,19 @@
public class ChipAppServer {
private static final String TAG = ChipAppServer.class.getSimpleName();

private volatile ChipFabricProvider mChipFabricProvider;

public ChipFabricProvider getFabricProvider() {

if (mChipFabricProvider == null) {
synchronized (this) {
if (mChipFabricProvider == null) mChipFabricProvider = new ChipFabricProvider();
}
}

return mChipFabricProvider;
}

public native boolean startApp();

public native boolean stopApp();
Expand Down
29 changes: 29 additions & 0 deletions src/app/server/java/src/chip/appserver/ChipFabricProvider.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
* 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.appserver;

import java.util.List;

/** read fabric count and list */
public class ChipFabricProvider {
public native int getFabricCount();

public native List<Fabric> getFabricList();

// todo support to remove fabric
}
39 changes: 39 additions & 0 deletions src/app/server/java/src/chip/appserver/Fabric.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* 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.appserver;

public class Fabric {

public int vendorId;
public long nodeId;
public short fabricIndex;
public String label;

@Override
public String toString() {
return "Fabric [fabricIndex="
+ fabricIndex
+ ", label="
+ label
+ ", nodeId="
+ nodeId
+ ", vendorId="
+ vendorId
+ "]";
}
}

0 comments on commit 4627962

Please sign in to comment.