LevelManager.cpp 4.9 KB

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