|
| 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 | +} |
0 commit comments