Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

allow commissionee device read fabric list in Android #18664

Merged
merged 5 commits into from
May 25, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
130 changes: 130 additions & 0 deletions src/app/server/java/ChipFabricProvider-JNI.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
/*
* Copyright (c) 2021 Project CHIP Authors
nicelyjust marked this conversation as resolved.
Show resolved Hide resolved
* 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;
// jclass sFabricProviderCls = NULL;
nicelyjust marked this conversation as resolved.
Show resolved Hide resolved
} // 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;

// Get a JNI environment object.
env = JniReferences::GetInstance().GetEnvForCurrentThread();
nicelyjust marked this conversation as resolved.
Show resolved Hide resolved
VerifyOrExit(env != NULL, err = CHIP_JNI_ERROR_NO_ENV);

ChipLogProgress(DeviceLayer, "Loading Java class references.");
nicelyjust marked this conversation as resolved.
Show resolved Hide resolved

chip::InitializeTracing();

exit:
if (err != CHIP_NO_ERROR)
{
// JniReferences::GetInstance().ThrowError(env, sFabricProviderCls, err);
nicelyjust marked this conversation as resolved.
Show resolved Hide resolved
JNI_OnUnload(jvm, reserved);
}

return err;
}

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

int GetFabricCount()
{
return chip::Server::GetInstance().GetFabricTable().FabricCount();
}

jobject ReadFabricList(JNIEnv * env, jobject self)
{
jobject jFabricList;
JniReferences::GetInstance().CreateArrayList(jFabricList);
jclass jFabricCls = env->FindClass("chip/appserver/Fabric");
VerifyOrExit(jFabricCls != nullptr, ChipLogError(NotSpecified, "unfind Class Fabric"));
nicelyjust marked this conversation as resolved.
Show resolved Hide resolved
{
for (auto & fabricInfo : Server::GetInstance().GetFabricTable())
{
// see src/app/clusters/operational-credentials-server/operational-credentials-server.cpp#ReadFabricsList
if (!fabricInfo.IsInitialized())
nicelyjust marked this conversation as resolved.
Show resolved Hide resolved
continue;

jmethodID jFabricMethod = env->GetMethodID(jFabricCls, "<init>", "()V");
nicelyjust marked this conversation as resolved.
Show resolved Hide resolved
jobject jFabric = env->NewObject(jFabricCls, jFabricMethod);
jfieldID jvendorId = env->GetFieldID(jFabricCls, "vendorId", "I");
jfieldID jnodeId = env->GetFieldID(jFabricCls, "nodeId", "J");
jfieldID jfabricIndex = env->GetFieldID(jFabricCls, "fabricIndex", "S");
jfieldID jlabel = env->GetFieldID(jFabricCls, "label", "Ljava/lang/String;");
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(jFabricList, jFabric);
}
return jFabricList;
nicelyjust marked this conversation as resolved.
Show resolved Hide resolved
}

exit:
return jFabricList;
}

JNI_METHOD(jint, getFabricCount)(JNIEnv * env, jobject self)
{
return GetFabricCount();
nicelyjust marked this conversation as resolved.
Show resolved Hide resolved
}

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

// a simplified way to get fabric count,see /src/credentials/FabricTable.h#FabricCount
nicelyjust marked this conversation as resolved.
Show resolved Hide resolved
int GetFabricCount();

jobject 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) 2021 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) 2021 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
+ "]";
}
}