Skip to content

Commit 4627962

Browse files
nicelyjustpull[bot]
authored andcommitted
allow commissionee device read fabric list in Android (#18664)
* allow commissionee device read fabric list * restyled * add NUll check * remove not necessary logic
1 parent f26ab7e commit 4627962

File tree

7 files changed

+242
-0
lines changed

7 files changed

+242
-0
lines changed

src/app/server/java/BUILD.gn

+4
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ static_library("jni") {
2727
"AndroidAppServerWrapper.cpp",
2828
"AndroidAppServerWrapper.h",
2929
"CHIPAppServer-JNI.cpp",
30+
"ChipFabricProvider-JNI.cpp",
31+
"ChipFabricProvider-JNI.h",
3032
"ChipThreadWork.cpp",
3133
"ChipThreadWork.h",
3234
]
@@ -59,6 +61,8 @@ android_library("java") {
5961
sources = [
6062
"src/chip/appserver/ChipAppServer.java",
6163
"src/chip/appserver/ChipAppServerException.java",
64+
"src/chip/appserver/ChipFabricProvider.java",
65+
"src/chip/appserver/Fabric.java",
6266
]
6367

6468
javac_flags = [ "-Xlint:deprecation" ]

src/app/server/java/CHIPAppServer-JNI.cpp

+3
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
*
2323
*/
2424
#include "AndroidAppServerWrapper.h"
25+
#include "ChipFabricProvider-JNI.h"
2526
#include "ChipThreadWork.h"
2627
#include <jni.h>
2728
#include <lib/core/CHIPError.h>
@@ -80,6 +81,8 @@ jint AndroidAppServerJNI_OnLoad(JavaVM * jvm, void * reserved)
8081

8182
err = AndroidChipPlatformJNI_OnLoad(jvm, reserved);
8283
SuccessOrExit(err);
84+
err = AndroidChipFabricProviderJNI_OnLoad(jvm, reserved);
85+
SuccessOrExit(err);
8386

8487
exit:
8588
if (err != CHIP_NO_ERROR)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
/*
2+
* Copyright (c) 2022 Project CHIP Authors
3+
* All rights reserved.
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*
17+
*/
18+
19+
/**
20+
* @file
21+
* Implementation of JNI bridge for CHIP App Server for Android TV apps
22+
*
23+
*/
24+
#include "ChipFabricProvider-JNI.h"
25+
#include "AndroidAppServerWrapper.h"
26+
#include <app/server/Server.h>
27+
#include <cstdlib>
28+
#include <iostream>
29+
#include <jni.h>
30+
#include <lib/core/CHIPError.h>
31+
#include <lib/support/CHIPJNIError.h>
32+
#include <lib/support/JniTypeWrappers.h>
33+
#include <platform/CHIPDeviceLayer.h>
34+
#include <thread>
35+
#include <trace/trace.h>
36+
37+
using namespace chip;
38+
39+
#define JNI_METHOD(RETURN, METHOD_NAME) extern "C" JNIEXPORT RETURN JNICALL Java_chip_appserver_ChipFabricProvider_##METHOD_NAME
40+
41+
namespace {
42+
JavaVM * sJVM;
43+
} // namespace
44+
45+
CHIP_ERROR AndroidChipFabricProviderJNI_OnLoad(JavaVM * jvm, void * reserved)
46+
{
47+
CHIP_ERROR err = CHIP_NO_ERROR;
48+
JNIEnv * env;
49+
50+
ChipLogProgress(DeviceLayer, "ChipFabricProvider JNI_OnLoad() called");
51+
52+
chip::Platform::MemoryInit();
53+
54+
// Save a reference to the JVM. Will need this to call back into Java.
55+
JniReferences::GetInstance().SetJavaVm(jvm, "chip/appserver/ChipFabricProvider");
56+
sJVM = jvm;
57+
58+
// check if the JNI environment is correct
59+
env = JniReferences::GetInstance().GetEnvForCurrentThread();
60+
VerifyOrExit(env != NULL, err = CHIP_JNI_ERROR_NO_ENV);
61+
62+
chip::InitializeTracing();
63+
64+
exit:
65+
if (err != CHIP_NO_ERROR)
66+
{
67+
JNI_OnUnload(jvm, reserved);
68+
}
69+
70+
return err;
71+
}
72+
73+
void AndroidChipFabricProviderJNI_OnUnload(JavaVM * jvm, void * reserved)
74+
{
75+
ChipLogProgress(DeviceLayer, "ChipFabricProvider JNI_OnUnload() called");
76+
chip::Platform::MemoryShutdown();
77+
}
78+
79+
CHIP_ERROR ReadFabricList(JNIEnv * env, jobject & self)
80+
{
81+
CHIP_ERROR err = CHIP_NO_ERROR;
82+
jclass jFabricCls = env->FindClass("chip/appserver/Fabric");
83+
VerifyOrExit(self != nullptr, err = CHIP_JNI_ERROR_NULL_OBJECT);
84+
VerifyOrExit(jFabricCls != nullptr, ChipLogError(NotSpecified, "could not find Class Fabric"));
85+
for (auto & fabricInfo : Server::GetInstance().GetFabricTable())
86+
{
87+
88+
jmethodID constructor = env->GetMethodID(jFabricCls, "<init>", "()V");
89+
VerifyOrExit(constructor != nullptr, err = CHIP_JNI_ERROR_METHOD_NOT_FOUND);
90+
jobject jFabric = env->NewObject(jFabricCls, constructor);
91+
VerifyOrExit(!env->ExceptionCheck(), err = CHIP_JNI_ERROR_EXCEPTION_THROWN);
92+
jfieldID jvendorId = env->GetFieldID(jFabricCls, "vendorId", "I");
93+
VerifyOrExit(jvendorId != nullptr, err = CHIP_JNI_ERROR_FIELD_NOT_FOUND);
94+
jfieldID jnodeId = env->GetFieldID(jFabricCls, "nodeId", "J");
95+
VerifyOrExit(jnodeId != nullptr, err = CHIP_JNI_ERROR_FIELD_NOT_FOUND);
96+
jfieldID jfabricIndex = env->GetFieldID(jFabricCls, "fabricIndex", "S");
97+
VerifyOrExit(jfabricIndex != nullptr, err = CHIP_JNI_ERROR_FIELD_NOT_FOUND);
98+
jfieldID jlabel = env->GetFieldID(jFabricCls, "label", "Ljava/lang/String;");
99+
VerifyOrExit(jlabel != nullptr, err = CHIP_JNI_ERROR_FIELD_NOT_FOUND);
100+
101+
env->SetIntField(jFabric, jvendorId, fabricInfo.GetVendorId());
102+
env->SetLongField(jFabric, jnodeId, fabricInfo.GetNodeId());
103+
env->SetShortField(jFabric, jfabricIndex, fabricInfo.GetFabricIndex());
104+
UtfString jLabelStr(env, fabricInfo.GetFabricLabel());
105+
env->SetObjectField(jFabric, jlabel, jLabelStr.jniValue());
106+
107+
JniReferences::GetInstance().AddToList(self, jFabric);
108+
}
109+
110+
exit:
111+
return err;
112+
}
113+
114+
JNI_METHOD(jint, getFabricCount)(JNIEnv * env, jobject self)
115+
{
116+
// a simplified way to get fabric count,see /src/credentials/FabricTable.h#FabricCount
117+
return chip::Server::GetInstance().GetFabricTable().FabricCount();
118+
}
119+
120+
JNI_METHOD(jobject, getFabricList)(JNIEnv * env, jobject self)
121+
{
122+
jobject jFabricList;
123+
JniReferences::GetInstance().CreateArrayList(jFabricList);
124+
ReadFabricList(env, jFabricList);
125+
return jFabricList;
126+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/*
2+
*
3+
* Copyright (c) 2022 Project CHIP Authors
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
#pragma once
19+
20+
#include <cstdint>
21+
#include <jni.h>
22+
#include <lib/core/CHIPError.h>
23+
24+
CHIP_ERROR AndroidChipFabricProviderJNI_OnLoad(JavaVM * jvm, void * reserved);
25+
26+
void AndroidChipFabricProviderJNI_OnUnload(JavaVM * jvm, void * reserved);
27+
28+
CHIP_ERROR ReadFabricList(JNIEnv * env, jobject & self);

src/app/server/java/src/chip/appserver/ChipAppServer.java

+13
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,19 @@
2121
public class ChipAppServer {
2222
private static final String TAG = ChipAppServer.class.getSimpleName();
2323

24+
private volatile ChipFabricProvider mChipFabricProvider;
25+
26+
public ChipFabricProvider getFabricProvider() {
27+
28+
if (mChipFabricProvider == null) {
29+
synchronized (this) {
30+
if (mChipFabricProvider == null) mChipFabricProvider = new ChipFabricProvider();
31+
}
32+
}
33+
34+
return mChipFabricProvider;
35+
}
36+
2437
public native boolean startApp();
2538

2639
public native boolean stopApp();
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/*
2+
* Copyright (c) 2022 Project CHIP Authors
3+
* All rights reserved.
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*
17+
*/
18+
package chip.appserver;
19+
20+
import java.util.List;
21+
22+
/** read fabric count and list */
23+
public class ChipFabricProvider {
24+
public native int getFabricCount();
25+
26+
public native List<Fabric> getFabricList();
27+
28+
// todo support to remove fabric
29+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/*
2+
* Copyright (c) 2022 Project CHIP Authors
3+
* All rights reserved.
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*
17+
*/
18+
package chip.appserver;
19+
20+
public class Fabric {
21+
22+
public int vendorId;
23+
public long nodeId;
24+
public short fabricIndex;
25+
public String label;
26+
27+
@Override
28+
public String toString() {
29+
return "Fabric [fabricIndex="
30+
+ fabricIndex
31+
+ ", label="
32+
+ label
33+
+ ", nodeId="
34+
+ nodeId
35+
+ ", vendorId="
36+
+ vendorId
37+
+ "]";
38+
}
39+
}

0 commit comments

Comments
 (0)