|
| 1 | +/** |
| 2 | + * |
| 3 | + * Copyright (c) 2023 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 | +#include "DoorLockManager.h" |
| 18 | +#include "DeviceApp-JNI.h" |
| 19 | +#include <app-common/zap-generated/attributes/Accessors.h> |
| 20 | +#include <app-common/zap-generated/ids/Clusters.h> |
| 21 | +#include <app/ConcreteAttributePath.h> |
| 22 | +#include <app/clusters/door-lock-server/door-lock-server.h> |
| 23 | +#include <app/reporting/reporting.h> |
| 24 | +#include <app/util/af.h> |
| 25 | +#include <jni.h> |
| 26 | +#include <lib/support/CHIPJNIError.h> |
| 27 | +#include <lib/support/JniReferences.h> |
| 28 | +#include <lib/support/JniTypeWrappers.h> |
| 29 | +#include <lib/support/ZclString.h> |
| 30 | +#include <platform/PlatformManager.h> |
| 31 | +#include <vector> |
| 32 | + |
| 33 | +using namespace chip; |
| 34 | +using namespace chip::app::Clusters; |
| 35 | +using namespace chip::app::Clusters::DoorLock; |
| 36 | +using namespace chip::DeviceLayer; |
| 37 | + |
| 38 | +static constexpr size_t kDoorLockManagerTableSize = |
| 39 | + EMBER_AF_DOOR_LOCK_CLUSTER_SERVER_ENDPOINT_COUNT + CHIP_DEVICE_CONFIG_DYNAMIC_ENDPOINT_COUNT; |
| 40 | + |
| 41 | +namespace { |
| 42 | + |
| 43 | +DoorLockManager * gDoorLockManagerTable[kDoorLockManagerTableSize] = { nullptr }; |
| 44 | + |
| 45 | +} |
| 46 | + |
| 47 | +void emberAfDoorLockClusterInitCallback(EndpointId endpoint) |
| 48 | +{ |
| 49 | + ChipLogProgress(Zcl, "Device App::DoorLock::PostClusterInit"); |
| 50 | + DeviceAppJNIMgr().PostClusterInit(chip::app::Clusters::DoorLock::Id, endpoint); |
| 51 | + DoorLockServer::Instance().InitServer(endpoint); |
| 52 | + EmberAfStatus status = DoorLock::Attributes::FeatureMap::Set(endpoint, 0); |
| 53 | + if (status != EMBER_ZCL_STATUS_SUCCESS) |
| 54 | + { |
| 55 | + ChipLogProgress(Zcl, "Device App::DoorLock::emberAfDoorLockClusterInitCallback()::Updating feature map %x", status); |
| 56 | + } |
| 57 | +} |
| 58 | + |
| 59 | +bool emberAfPluginDoorLockOnDoorLockCommand(EndpointId endpointId, const Nullable<chip::FabricIndex> & fabricIdx, |
| 60 | + const Nullable<chip::NodeId> & nodeId, const Optional<ByteSpan> & pinCode, |
| 61 | + OperationErrorEnum & err) |
| 62 | +{ |
| 63 | + ChipLogProgress(Zcl, "Device App::DoorLock::emberAfPluginDoorLockOnDoorLockCommand"); |
| 64 | + return DoorLockServer::Instance().SetLockState(endpointId, DlLockState::kLocked); |
| 65 | +} |
| 66 | + |
| 67 | +bool emberAfPluginDoorLockOnDoorUnlockCommand(EndpointId endpointId, const Nullable<chip::FabricIndex> & fabricIdx, |
| 68 | + const Nullable<chip::NodeId> & nodeId, const Optional<ByteSpan> & pinCode, |
| 69 | + OperationErrorEnum & err) |
| 70 | +{ |
| 71 | + ChipLogProgress(Zcl, "Device App::DoorLock::emberAfPluginDoorLockOnDoorUnlockCommand"); |
| 72 | + return DoorLockServer::Instance().SetLockState(endpointId, DlLockState::kUnlocked); |
| 73 | +} |
| 74 | + |
| 75 | +void DoorLockManager::NewManager(jint endpoint, jobject manager) |
| 76 | +{ |
| 77 | + ChipLogProgress(Zcl, "Device App: DoorLockManager::NewManager"); |
| 78 | + uint16_t ep = emberAfGetClusterServerEndpointIndex(static_cast<chip::EndpointId>(endpoint), app::Clusters::DoorLock::Id, |
| 79 | + EMBER_AF_DOOR_LOCK_CLUSTER_SERVER_ENDPOINT_COUNT); |
| 80 | + VerifyOrReturn(ep < kDoorLockManagerTableSize, |
| 81 | + ChipLogError(Zcl, "Device App::DoorLock::NewManager: endpoint %d not found", endpoint)); |
| 82 | + |
| 83 | + VerifyOrReturn(gDoorLockManagerTable[ep] == nullptr, |
| 84 | + ChipLogError(Zcl, "Device App::DoorLock::NewManager: endpoint %d already has a manager", endpoint)); |
| 85 | + DoorLockManager * mgr = new DoorLockManager(); |
| 86 | + CHIP_ERROR err = mgr->InitializeWithObjects(manager); |
| 87 | + if (err != CHIP_NO_ERROR) |
| 88 | + { |
| 89 | + ChipLogError(Zcl, "Device App::DoorLock::NewManager: failed to initialize manager for endpoint %d", endpoint); |
| 90 | + delete mgr; |
| 91 | + } |
| 92 | + else |
| 93 | + { |
| 94 | + gDoorLockManagerTable[ep] = mgr; |
| 95 | + } |
| 96 | +} |
| 97 | + |
| 98 | +DoorLockManager * GetDoorLockManager(EndpointId endpoint) |
| 99 | +{ |
| 100 | + uint16_t ep = emberAfGetClusterServerEndpointIndex(endpoint, app::Clusters::DoorLock::Id, |
| 101 | + EMBER_AF_DOOR_LOCK_CLUSTER_SERVER_ENDPOINT_COUNT); |
| 102 | + return ((ep >= kDoorLockManagerTableSize) ? nullptr : gDoorLockManagerTable[ep]); |
| 103 | +} |
| 104 | + |
| 105 | +jboolean DoorLockManager::SetLockType(jint endpoint, jint value) |
| 106 | +{ |
| 107 | + EmberAfStatus status = app::Clusters::DoorLock::Attributes::LockType::Set( |
| 108 | + static_cast<chip::EndpointId>(endpoint), static_cast<app::Clusters::DoorLock::DlLockType>(value)); |
| 109 | + return status == EMBER_ZCL_STATUS_SUCCESS; |
| 110 | +} |
| 111 | + |
| 112 | +jboolean DoorLockManager::SetLockState(jint endpoint, jint value) |
| 113 | +{ |
| 114 | + return DoorLockServer::Instance().SetLockState(static_cast<chip::EndpointId>(endpoint), |
| 115 | + static_cast<app::Clusters::DoorLock::DlLockState>(value)); |
| 116 | +} |
| 117 | + |
| 118 | +jboolean DoorLockManager::SetActuatorEnabled(jint endpoint, jboolean value) |
| 119 | +{ |
| 120 | + return DoorLockServer::Instance().SetActuatorEnabled(static_cast<chip::EndpointId>(endpoint), value); |
| 121 | +} |
| 122 | + |
| 123 | +jboolean DoorLockManager::SetAutoRelockTime(jint endpoint, jint value) |
| 124 | +{ |
| 125 | + return DoorLockServer::Instance().SetAutoRelockTime(static_cast<chip::EndpointId>(endpoint), static_cast<uint32_t>(value)); |
| 126 | +} |
| 127 | + |
| 128 | +jboolean DoorLockManager::SetOperatingMode(jint endpoint, jint value) |
| 129 | +{ |
| 130 | + EmberAfStatus status = app::Clusters::DoorLock::Attributes::OperatingMode::Set( |
| 131 | + static_cast<chip::EndpointId>(endpoint), static_cast<app::Clusters::DoorLock::OperatingModeEnum>(value)); |
| 132 | + return status == EMBER_ZCL_STATUS_SUCCESS; |
| 133 | +} |
| 134 | + |
| 135 | +jboolean DoorLockManager::SetSupportedOperatingModes(jint endpoint, jint value) |
| 136 | +{ |
| 137 | + EmberAfStatus status = app::Clusters::DoorLock::Attributes::SupportedOperatingModes::Set( |
| 138 | + static_cast<chip::EndpointId>(endpoint), static_cast<app::Clusters::DoorLock::DlSupportedOperatingModes>(value)); |
| 139 | + return status == EMBER_ZCL_STATUS_SUCCESS; |
| 140 | +} |
| 141 | + |
| 142 | +jboolean DoorLockManager::SendLockAlarmEvent(jint endpoint) |
| 143 | +{ |
| 144 | + return DoorLockServer::Instance().SendLockAlarmEvent(static_cast<chip::EndpointId>(endpoint), AlarmCodeEnum::kDoorForcedOpen); |
| 145 | +} |
| 146 | + |
| 147 | +void DoorLockManager::PostLockStateChanged(chip::EndpointId endpoint, int value) |
| 148 | +{ |
| 149 | + ChipLogProgress(Zcl, "Device App: DoorLockManager::PostLockStateChanged:%d", value); |
| 150 | + DoorLockManager * mgr = GetDoorLockManager(endpoint); |
| 151 | + VerifyOrReturn(mgr != nullptr, ChipLogError(Zcl, "DoorLockManager null")); |
| 152 | + |
| 153 | + mgr->HandleLockStateChanged(static_cast<int>(endpoint), value); |
| 154 | +} |
| 155 | + |
| 156 | +CHIP_ERROR DoorLockManager::InitializeWithObjects(jobject managerObject) |
| 157 | +{ |
| 158 | + JNIEnv * env = JniReferences::GetInstance().GetEnvForCurrentThread(); |
| 159 | + VerifyOrReturnLogError(env != nullptr, CHIP_ERROR_INCORRECT_STATE); |
| 160 | + |
| 161 | + mDoorLockManagerObject = env->NewGlobalRef(managerObject); |
| 162 | + VerifyOrReturnLogError(mDoorLockManagerObject != nullptr, CHIP_ERROR_INVALID_ARGUMENT); |
| 163 | + |
| 164 | + jclass DoorLockManagerClass = env->GetObjectClass(managerObject); |
| 165 | + VerifyOrReturnLogError(DoorLockManagerClass != nullptr, CHIP_ERROR_INVALID_ARGUMENT); |
| 166 | + |
| 167 | + mHandleLockStateChangedMethod = env->GetMethodID(DoorLockManagerClass, "handleLockStateChanged", "(I)V"); |
| 168 | + if (mHandleLockStateChangedMethod == nullptr) |
| 169 | + { |
| 170 | + ChipLogError(Zcl, "Failed to access DoorLockManager 'handleLockStateChanged' method"); |
| 171 | + env->ExceptionClear(); |
| 172 | + return CHIP_ERROR_INVALID_ARGUMENT; |
| 173 | + } |
| 174 | + |
| 175 | + return CHIP_NO_ERROR; |
| 176 | +} |
| 177 | + |
| 178 | +void DoorLockManager::HandleLockStateChanged(jint endpoint, jint value) |
| 179 | +{ |
| 180 | + ChipLogProgress(Zcl, "DoorLockManager::HandleLockStateChanged:%d", value); |
| 181 | + |
| 182 | + JNIEnv * env = JniReferences::GetInstance().GetEnvForCurrentThread(); |
| 183 | + VerifyOrReturn(env != NULL, ChipLogProgress(Zcl, "env null")); |
| 184 | + VerifyOrReturn(mDoorLockManagerObject != nullptr, ChipLogProgress(Zcl, "mDoorLockManagerObject null")); |
| 185 | + VerifyOrReturn(mHandleLockStateChangedMethod != nullptr, ChipLogProgress(Zcl, "mHandleLockStateChangedMethod null")); |
| 186 | + |
| 187 | + env->ExceptionClear(); |
| 188 | + env->CallVoidMethod(mDoorLockManagerObject, mHandleLockStateChangedMethod, value); |
| 189 | + if (env->ExceptionCheck()) |
| 190 | + { |
| 191 | + ChipLogError(AppServer, "Java exception in DoorLockManager::HandleLockStateChanged"); |
| 192 | + env->ExceptionDescribe(); |
| 193 | + env->ExceptionClear(); |
| 194 | + } |
| 195 | +} |
0 commit comments