-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
allow commissionee device read fabric list in Android (#18664)
* allow commissionee device read fabric list * restyled * add NUll check * remove not necessary logic
- Loading branch information
1 parent
f26ab7e
commit 4627962
Showing
7 changed files
with
242 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
29 changes: 29 additions & 0 deletions
29
src/app/server/java/src/chip/appserver/ChipFabricProvider.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
+ "]"; | ||
} | ||
} |