PowerSourceManager.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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 "PowerSourceManager.h"
  18. #include "DeviceApp-JNI.h"
  19. #include <app-common/zap-generated/attributes/Accessors.h>
  20. #include <app-common/zap-generated/ids/Attributes.h>
  21. #include <app-common/zap-generated/ids/Clusters.h>
  22. #include <app/ConcreteAttributePath.h>
  23. #include <app/clusters/door-lock-server/door-lock-server.h>
  24. #include <app/reporting/reporting.h>
  25. #include <app/util/af.h>
  26. #include <jni.h>
  27. #include <lib/support/CHIPJNIError.h>
  28. #include <lib/support/JniReferences.h>
  29. #include <lib/support/JniTypeWrappers.h>
  30. #include <lib/support/ZclString.h>
  31. #include <platform/PlatformManager.h>
  32. using namespace chip;
  33. using namespace chip::app::Clusters;
  34. using namespace chip::app::Clusters::PowerSource;
  35. static constexpr size_t kPowerSourceManagerTableSize =
  36. EMBER_AF_POWER_SOURCE_CLUSTER_SERVER_ENDPOINT_COUNT + CHIP_DEVICE_CONFIG_DYNAMIC_ENDPOINT_COUNT;
  37. namespace {
  38. PowerSourceManager * gPowerSourceManagerTable[kPowerSourceManagerTableSize] = { nullptr };
  39. }
  40. void emberAfPowerSourceClusterInitCallback(EndpointId endpoint)
  41. {
  42. ChipLogProgress(Zcl, "Device App::PowerSource::PostClusterInit");
  43. DeviceAppJNIMgr().PostClusterInit(chip::app::Clusters::PowerSource::Id, endpoint);
  44. }
  45. void PowerSourceManager::NewManager(jint endpoint, jobject manager)
  46. {
  47. ChipLogProgress(Zcl, "Device App: PowerSourceManager::NewManager");
  48. uint16_t ep = emberAfGetClusterServerEndpointIndex(static_cast<chip::EndpointId>(endpoint), app::Clusters::PowerSource::Id,
  49. EMBER_AF_POWER_SOURCE_CLUSTER_SERVER_ENDPOINT_COUNT);
  50. VerifyOrReturn(ep < kPowerSourceManagerTableSize,
  51. ChipLogError(Zcl, "Device App::PowerSource::NewManager: endpoint %d not found", endpoint));
  52. VerifyOrReturn(gPowerSourceManagerTable[ep] == nullptr,
  53. ChipLogError(Zcl, "Device App::PowerSource::NewManager: endpoint %d already has a manager", endpoint));
  54. PowerSourceManager * mgr = new PowerSourceManager();
  55. CHIP_ERROR err = mgr->InitializeWithObjects(manager);
  56. if (err != CHIP_NO_ERROR)
  57. {
  58. ChipLogError(Zcl, "Device App::PowerSource::NewManager: failed to initialize manager for endpoint %d", endpoint);
  59. delete mgr;
  60. }
  61. else
  62. {
  63. gPowerSourceManagerTable[ep] = mgr;
  64. }
  65. }
  66. PowerSourceManager * GetPowerSourceManager(EndpointId endpoint)
  67. {
  68. uint16_t ep = emberAfGetClusterServerEndpointIndex(endpoint, app::Clusters::PowerSource::Id,
  69. EMBER_AF_POWER_SOURCE_CLUSTER_SERVER_ENDPOINT_COUNT);
  70. return ((ep >= kPowerSourceManagerTableSize) ? nullptr : gPowerSourceManagerTable[ep]);
  71. }
  72. jboolean PowerSourceManager::SetBatPercentRemaining(jint endpoint, jint value)
  73. {
  74. using namespace chip::app::Clusters;
  75. using namespace chip::DeviceLayer;
  76. EmberAfStatus status = EMBER_ZCL_STATUS_SUCCESS;
  77. status =
  78. PowerSource::Attributes::BatPercentRemaining::Set(static_cast<chip::EndpointId>(endpoint), static_cast<uint8_t>(value * 2));
  79. ChipLogDetail(Zcl, "Device App::PowerSource::SetBatPercentRemaining: endpoint:%d, percent:%d", endpoint, value);
  80. return status == EMBER_ZCL_STATUS_SUCCESS;
  81. }
  82. CHIP_ERROR PowerSourceManager::InitializeWithObjects(jobject managerObject)
  83. {
  84. JNIEnv * env = JniReferences::GetInstance().GetEnvForCurrentThread();
  85. VerifyOrReturnLogError(env != nullptr, CHIP_ERROR_INCORRECT_STATE);
  86. mPowerSourceManagerObject = env->NewGlobalRef(managerObject);
  87. VerifyOrReturnLogError(mPowerSourceManagerObject != nullptr, CHIP_ERROR_INVALID_ARGUMENT);
  88. jclass PowerSourceManagerClass = env->GetObjectClass(managerObject);
  89. VerifyOrReturnLogError(PowerSourceManagerClass != nullptr, CHIP_ERROR_INVALID_ARGUMENT);
  90. return CHIP_NO_ERROR;
  91. }