| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107 |
- /**
- *
- * Copyright (c) 2023 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.
- */
- #include "PowerSourceManager.h"
- #include "DeviceApp-JNI.h"
- #include <app-common/zap-generated/attributes/Accessors.h>
- #include <app-common/zap-generated/ids/Attributes.h>
- #include <app-common/zap-generated/ids/Clusters.h>
- #include <app/ConcreteAttributePath.h>
- #include <app/clusters/door-lock-server/door-lock-server.h>
- #include <app/reporting/reporting.h>
- #include <app/util/af.h>
- #include <jni.h>
- #include <lib/support/CHIPJNIError.h>
- #include <lib/support/JniReferences.h>
- #include <lib/support/JniTypeWrappers.h>
- #include <lib/support/ZclString.h>
- #include <platform/PlatformManager.h>
- using namespace chip;
- using namespace chip::app::Clusters;
- using namespace chip::app::Clusters::PowerSource;
- static constexpr size_t kPowerSourceManagerTableSize =
- EMBER_AF_POWER_SOURCE_CLUSTER_SERVER_ENDPOINT_COUNT + CHIP_DEVICE_CONFIG_DYNAMIC_ENDPOINT_COUNT;
- namespace {
- PowerSourceManager * gPowerSourceManagerTable[kPowerSourceManagerTableSize] = { nullptr };
- }
- void emberAfPowerSourceClusterInitCallback(EndpointId endpoint)
- {
- ChipLogProgress(Zcl, "Device App::PowerSource::PostClusterInit");
- DeviceAppJNIMgr().PostClusterInit(chip::app::Clusters::PowerSource::Id, endpoint);
- }
- void PowerSourceManager::NewManager(jint endpoint, jobject manager)
- {
- ChipLogProgress(Zcl, "Device App: PowerSourceManager::NewManager");
- uint16_t ep = emberAfGetClusterServerEndpointIndex(static_cast<chip::EndpointId>(endpoint), app::Clusters::PowerSource::Id,
- EMBER_AF_POWER_SOURCE_CLUSTER_SERVER_ENDPOINT_COUNT);
- VerifyOrReturn(ep < kPowerSourceManagerTableSize,
- ChipLogError(Zcl, "Device App::PowerSource::NewManager: endpoint %d not found", endpoint));
- VerifyOrReturn(gPowerSourceManagerTable[ep] == nullptr,
- ChipLogError(Zcl, "Device App::PowerSource::NewManager: endpoint %d already has a manager", endpoint));
- PowerSourceManager * mgr = new PowerSourceManager();
- CHIP_ERROR err = mgr->InitializeWithObjects(manager);
- if (err != CHIP_NO_ERROR)
- {
- ChipLogError(Zcl, "Device App::PowerSource::NewManager: failed to initialize manager for endpoint %d", endpoint);
- delete mgr;
- }
- else
- {
- gPowerSourceManagerTable[ep] = mgr;
- }
- }
- PowerSourceManager * GetPowerSourceManager(EndpointId endpoint)
- {
- uint16_t ep = emberAfGetClusterServerEndpointIndex(endpoint, app::Clusters::PowerSource::Id,
- EMBER_AF_POWER_SOURCE_CLUSTER_SERVER_ENDPOINT_COUNT);
- return ((ep >= kPowerSourceManagerTableSize) ? nullptr : gPowerSourceManagerTable[ep]);
- }
- jboolean PowerSourceManager::SetBatPercentRemaining(jint endpoint, jint value)
- {
- using namespace chip::app::Clusters;
- using namespace chip::DeviceLayer;
- EmberAfStatus status = EMBER_ZCL_STATUS_SUCCESS;
- status =
- PowerSource::Attributes::BatPercentRemaining::Set(static_cast<chip::EndpointId>(endpoint), static_cast<uint8_t>(value * 2));
- ChipLogDetail(Zcl, "Device App::PowerSource::SetBatPercentRemaining: endpoint:%d, percent:%d", endpoint, value);
- return status == EMBER_ZCL_STATUS_SUCCESS;
- }
- CHIP_ERROR PowerSourceManager::InitializeWithObjects(jobject managerObject)
- {
- JNIEnv * env = JniReferences::GetInstance().GetEnvForCurrentThread();
- VerifyOrReturnLogError(env != nullptr, CHIP_ERROR_INCORRECT_STATE);
- mPowerSourceManagerObject = env->NewGlobalRef(managerObject);
- VerifyOrReturnLogError(mPowerSourceManagerObject != nullptr, CHIP_ERROR_INVALID_ARGUMENT);
- jclass PowerSourceManagerClass = env->GetObjectClass(managerObject);
- VerifyOrReturnLogError(PowerSourceManagerClass != nullptr, CHIP_ERROR_INVALID_ARGUMENT);
- return CHIP_NO_ERROR;
- }
|