OnOffManager.cpp 4.8 KB

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