LevelManager.cpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. /**
  2. *
  3. * Copyright (c) 2022 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 "LevelManager.h"
  18. #include "TvApp-JNI.h"
  19. #include <app-common/zap-generated/attributes/Accessors.h>
  20. #include <app-common/zap-generated/ids/Clusters.h>
  21. #include <app/util/af.h>
  22. #include <app/util/config.h>
  23. #include <jni.h>
  24. #include <lib/support/CHIPJNIError.h>
  25. #include <lib/support/JniReferences.h>
  26. #include <lib/support/JniTypeWrappers.h>
  27. using namespace chip;
  28. static constexpr size_t kLevelManagerTableSize = EMBER_AF_LEVEL_CONTROL_CLUSTER_SERVER_ENDPOINT_COUNT;
  29. namespace {
  30. LevelManager * gLevelManagerTable[kLevelManagerTableSize] = { nullptr };
  31. static_assert(kLevelManagerTableSize <= kEmberInvalidEndpointIndex, "gLevelManagerTable table size error");
  32. } // namespace
  33. void emberAfLevelControlClusterInitCallback(EndpointId endpoint)
  34. {
  35. ChipLogProgress(Zcl, "TV Android App::Level::PostClusterInit");
  36. TvAppJNIMgr().PostClusterInit(chip::app::Clusters::LevelControl::Id, endpoint);
  37. }
  38. void LevelManager::NewManager(jint endpoint, jobject manager)
  39. {
  40. ChipLogProgress(Zcl, "TV Android App: LevelManager::NewManager");
  41. uint16_t ep = emberAfGetClusterServerEndpointIndex(static_cast<chip::EndpointId>(endpoint), app::Clusters::LevelControl::Id,
  42. EMBER_AF_LEVEL_CONTROL_CLUSTER_SERVER_ENDPOINT_COUNT);
  43. VerifyOrReturn(ep < kLevelManagerTableSize,
  44. ChipLogError(Zcl, "TV Android App::Level::NewManager: endpoint %d not found", endpoint));
  45. VerifyOrReturn(gLevelManagerTable[ep] == nullptr,
  46. ChipLogError(Zcl, "TV Android App::Level::NewManager: endpoint %d already has a manager", endpoint));
  47. LevelManager * mgr = new LevelManager();
  48. CHIP_ERROR err = mgr->InitializeWithObjects(manager);
  49. if (err != CHIP_NO_ERROR)
  50. {
  51. ChipLogError(Zcl, "TV Android App::Level::NewManager: failed to initialize manager for endpoint %d", endpoint);
  52. delete mgr;
  53. }
  54. else
  55. {
  56. gLevelManagerTable[ep] = mgr;
  57. }
  58. }
  59. LevelManager * GetLevelManager(EndpointId endpoint)
  60. {
  61. uint16_t ep = emberAfGetClusterServerEndpointIndex(endpoint, app::Clusters::LevelControl::Id,
  62. EMBER_AF_LEVEL_CONTROL_CLUSTER_SERVER_ENDPOINT_COUNT);
  63. return ((ep >= kLevelManagerTableSize) ? nullptr : gLevelManagerTable[ep]);
  64. }
  65. void LevelManager::PostLevelChanged(chip::EndpointId endpoint, uint8_t value)
  66. {
  67. ChipLogProgress(Zcl, "TV Android App: LevelManager::PostLevelChanged");
  68. LevelManager * mgr = GetLevelManager(endpoint);
  69. VerifyOrReturn(mgr != nullptr, ChipLogError(Zcl, "LevelManager null"));
  70. mgr->HandleLevelChanged(value);
  71. }
  72. jboolean LevelManager::SetLevel(jint endpoint, jint value)
  73. {
  74. EmberAfStatus status = app::Clusters::LevelControl::Attributes::CurrentLevel::Set(static_cast<chip::EndpointId>(endpoint),
  75. static_cast<uint8_t>(value));
  76. return status == EMBER_ZCL_STATUS_SUCCESS;
  77. }
  78. CHIP_ERROR LevelManager::InitializeWithObjects(jobject managerObject)
  79. {
  80. JNIEnv * env = JniReferences::GetInstance().GetEnvForCurrentThread();
  81. VerifyOrReturnLogError(env != nullptr, CHIP_ERROR_INCORRECT_STATE);
  82. mLevelManagerObject = env->NewGlobalRef(managerObject);
  83. VerifyOrReturnLogError(mLevelManagerObject != nullptr, CHIP_ERROR_INVALID_ARGUMENT);
  84. jclass LevelManagerClass = env->GetObjectClass(managerObject);
  85. VerifyOrReturnLogError(LevelManagerClass != nullptr, CHIP_ERROR_INVALID_ARGUMENT);
  86. mHandleLevelChangedMethod = env->GetMethodID(LevelManagerClass, "HandleLevelChanged", "(I)V");
  87. if (mHandleLevelChangedMethod == nullptr)
  88. {
  89. ChipLogError(Zcl, "Failed to access LevelManager 'HandleLevelChanged' method");
  90. env->ExceptionClear();
  91. return CHIP_ERROR_INVALID_ARGUMENT;
  92. }
  93. return CHIP_NO_ERROR;
  94. }
  95. void LevelManager::HandleLevelChanged(uint8_t value)
  96. {
  97. ChipLogProgress(Zcl, "LevelManager::HandleLevelChanged");
  98. JNIEnv * env = JniReferences::GetInstance().GetEnvForCurrentThread();
  99. VerifyOrReturn(env != NULL, ChipLogProgress(Zcl, "env null"));
  100. VerifyOrReturn(mLevelManagerObject != nullptr, ChipLogProgress(Zcl, "mLevelManagerObject null"));
  101. VerifyOrReturn(mHandleLevelChangedMethod != nullptr, ChipLogProgress(Zcl, "mHandleLevelChangedMethod null"));
  102. env->ExceptionClear();
  103. env->CallVoidMethod(mLevelManagerObject, mHandleLevelChangedMethod, static_cast<jint>(value));
  104. if (env->ExceptionCheck())
  105. {
  106. ChipLogError(AppServer, "Java exception in LevelManager::HandleLevelChanged");
  107. env->ExceptionDescribe();
  108. env->ExceptionClear();
  109. }
  110. }