LowPowerManager.cpp 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. /*
  2. *
  3. * Copyright (c) 2021 Project CHIP Authors
  4. * All rights reserved.
  5. *
  6. * Licensed under the Apache License, Version 2.0 (the "License");
  7. * you may not use this file except in compliance with the License.
  8. * You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. */
  18. #include "LowPowerManager.h"
  19. #include "TvApp-JNI.h"
  20. #include <app-common/zap-generated/ids/Clusters.h>
  21. #include <lib/support/CHIPJNIError.h>
  22. #include <lib/support/CodeUtils.h>
  23. #include <lib/support/JniReferences.h>
  24. #include <lib/support/JniTypeWrappers.h>
  25. #include <lib/support/logging/CHIPLogging.h>
  26. using namespace chip;
  27. using namespace chip::app::Clusters::LowPower;
  28. void emberAfLowPowerClusterInitCallback(EndpointId endpoint)
  29. {
  30. ChipLogProgress(Zcl, "TV Android App: LowPower::PostClusterInit");
  31. TvAppJNIMgr().PostClusterInit(chip::app::Clusters::LowPower::Id, endpoint);
  32. }
  33. void LowPowerManager::NewManager(jint endpoint, jobject manager)
  34. {
  35. ChipLogProgress(Zcl, "TV Android App: LowPower::SetDefaultDelegate");
  36. LowPowerManager * mgr = new LowPowerManager();
  37. mgr->InitializeWithObjects(manager);
  38. chip::app::Clusters::LowPower::SetDefaultDelegate(static_cast<EndpointId>(endpoint), mgr);
  39. }
  40. void LowPowerManager::InitializeWithObjects(jobject managerObject)
  41. {
  42. JNIEnv * env = JniReferences::GetInstance().GetEnvForCurrentThread();
  43. VerifyOrReturn(env != nullptr, ChipLogError(Zcl, "Failed to GetEnvForCurrentThread for LowPowerManager"));
  44. mLowPowerManagerObject = env->NewGlobalRef(managerObject);
  45. VerifyOrReturn(mLowPowerManagerObject != nullptr, ChipLogError(Zcl, "Failed to NewGlobalRef LowPowerManager"));
  46. jclass LowPowerManagerClass = env->GetObjectClass(managerObject);
  47. VerifyOrReturn(LowPowerManagerClass != nullptr, ChipLogError(Zcl, "Failed to get LowPowerManager Java class"));
  48. mSleepMethod = env->GetMethodID(LowPowerManagerClass, "sleep", "()Z");
  49. if (mSleepMethod == nullptr)
  50. {
  51. ChipLogError(Zcl, "Failed to access LowPowerManager 'sleep' method");
  52. env->ExceptionClear();
  53. }
  54. }
  55. bool LowPowerManager::HandleSleep()
  56. {
  57. jboolean ret = JNI_FALSE;
  58. JNIEnv * env = JniReferences::GetInstance().GetEnvForCurrentThread();
  59. ChipLogProgress(Zcl, "Received LowPowerManager::Sleep");
  60. VerifyOrExit(mLowPowerManagerObject != nullptr, ChipLogError(Zcl, "mLowPowerManagerObject null"));
  61. VerifyOrExit(mSleepMethod != nullptr, ChipLogError(Zcl, "mSleepMethod null"));
  62. VerifyOrExit(env != NULL, ChipLogError(Zcl, "env null"));
  63. env->ExceptionClear();
  64. ret = env->CallBooleanMethod(mLowPowerManagerObject, mSleepMethod);
  65. if (env->ExceptionCheck())
  66. {
  67. ChipLogError(DeviceLayer, "Java exception in LowPowerManager::Sleep");
  68. env->ExceptionDescribe();
  69. env->ExceptionClear();
  70. return false;
  71. }
  72. exit:
  73. return static_cast<bool>(ret);
  74. }