OnOffManager.cpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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 "OnOffManager.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 kOnffManagerTableSize = EMBER_AF_ON_OFF_CLUSTER_SERVER_ENDPOINT_COUNT;
  29. namespace {
  30. OnOffManager * gOnOffManagerTable[kOnffManagerTableSize] = { nullptr };
  31. static_assert(kOnffManagerTableSize <= kEmberInvalidEndpointIndex, "gOnOffManagerTable table size error");
  32. } // namespace
  33. void emberAfOnOffClusterInitCallback(EndpointId endpoint)
  34. {
  35. ChipLogProgress(Zcl, "TV Android App::OnOff::PostClusterInit");
  36. TvAppJNIMgr().PostClusterInit(chip::app::Clusters::OnOff::Id, endpoint);
  37. }
  38. void OnOffManager::NewManager(jint endpoint, jobject manager)
  39. {
  40. ChipLogProgress(Zcl, "TV Android App: OnOffManager::NewManager");
  41. uint16_t ep = emberAfGetClusterServerEndpointIndex(static_cast<chip::EndpointId>(endpoint), app::Clusters::OnOff::Id,
  42. EMBER_AF_ON_OFF_CLUSTER_SERVER_ENDPOINT_COUNT);
  43. VerifyOrReturn(ep < kOnffManagerTableSize,
  44. ChipLogError(Zcl, "TV Android App::OnOff::NewManager: endpoint %d not found", endpoint));
  45. VerifyOrReturn(gOnOffManagerTable[ep] == nullptr,
  46. ChipLogError(Zcl, "TV Android App::OnOff::NewManager: endpoint %d already has a manager", endpoint));
  47. OnOffManager * mgr = new OnOffManager();
  48. CHIP_ERROR err = mgr->InitializeWithObjects(manager);
  49. if (err != CHIP_NO_ERROR)
  50. {
  51. ChipLogError(Zcl, "TV Android App::OnOff::NewManager: failed to initialize manager for endpoint %d", endpoint);
  52. delete mgr;
  53. }
  54. else
  55. {
  56. gOnOffManagerTable[ep] = mgr;
  57. }
  58. }
  59. OnOffManager * GetOnOffManager(EndpointId endpoint)
  60. {
  61. uint16_t ep =
  62. emberAfGetClusterServerEndpointIndex(endpoint, app::Clusters::OnOff::Id, EMBER_AF_ON_OFF_CLUSTER_SERVER_ENDPOINT_COUNT);
  63. return (ep >= kOnffManagerTableSize ? nullptr : gOnOffManagerTable[ep]);
  64. }
  65. void OnOffManager::PostOnOffChanged(chip::EndpointId endpoint, bool value)
  66. {
  67. ChipLogProgress(Zcl, "TV Android App: OnOffManager::PostOnOffChange");
  68. OnOffManager * mgr = GetOnOffManager(endpoint);
  69. VerifyOrReturn(mgr != nullptr, ChipLogError(Zcl, "OnOffManager null"));
  70. mgr->HandleOnOffChanged(value);
  71. }
  72. jboolean OnOffManager::SetOnOff(jint endpoint, bool value)
  73. {
  74. EmberAfStatus status = app::Clusters::OnOff::Attributes::OnOff::Set(static_cast<chip::EndpointId>(endpoint), value);
  75. return status == EMBER_ZCL_STATUS_SUCCESS;
  76. }
  77. CHIP_ERROR OnOffManager::InitializeWithObjects(jobject managerObject)
  78. {
  79. JNIEnv * env = JniReferences::GetInstance().GetEnvForCurrentThread();
  80. VerifyOrReturnLogError(env != nullptr, CHIP_ERROR_INCORRECT_STATE);
  81. mOnOffManagerObject = env->NewGlobalRef(managerObject);
  82. VerifyOrReturnLogError(mOnOffManagerObject != nullptr, CHIP_ERROR_INVALID_ARGUMENT);
  83. jclass OnOffManagerClass = env->GetObjectClass(managerObject);
  84. VerifyOrReturnLogError(OnOffManagerClass != nullptr, CHIP_ERROR_INVALID_ARGUMENT);
  85. mHandleOnOffChangedMethod = env->GetMethodID(OnOffManagerClass, "HandleOnOffChanged", "(Z)V");
  86. if (mHandleOnOffChangedMethod == nullptr)
  87. {
  88. ChipLogError(Zcl, "Failed to access OnOffManager 'HandleOnOffChanged' method");
  89. env->ExceptionClear();
  90. return CHIP_ERROR_INVALID_ARGUMENT;
  91. }
  92. return CHIP_NO_ERROR;
  93. }
  94. void OnOffManager::HandleOnOffChanged(bool value)
  95. {
  96. ChipLogProgress(Zcl, "OnOffManager::HandleOnOffChanged");
  97. JNIEnv * env = JniReferences::GetInstance().GetEnvForCurrentThread();
  98. VerifyOrReturn(env != NULL, ChipLogProgress(Zcl, "env null"));
  99. VerifyOrReturn(mOnOffManagerObject != nullptr, ChipLogProgress(Zcl, "mOnOffManagerObject null"));
  100. VerifyOrReturn(mHandleOnOffChangedMethod != nullptr, ChipLogProgress(Zcl, "mHandleOnOffChangedMethod null"));
  101. env->ExceptionClear();
  102. env->CallVoidMethod(mOnOffManagerObject, mHandleOnOffChangedMethod, static_cast<jboolean>(value));
  103. if (env->ExceptionCheck())
  104. {
  105. ChipLogError(AppServer, "Java exception in OnOffManager::HandleOnOffChanged");
  106. env->ExceptionDescribe();
  107. env->ExceptionClear();
  108. }
  109. }