WakeOnLanManager.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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 "WakeOnLanManager.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/JniReferences.h>
  23. #include <lib/support/JniTypeWrappers.h>
  24. using namespace chip;
  25. using namespace chip::app::Clusters::WakeOnLan;
  26. /** @brief Wake On LAN Cluster Init
  27. *
  28. * This function is called when a specific cluster is initialized. It gives the
  29. * application an opportunity to take care of cluster initialization procedures.
  30. * It is called exactly once for each endpoint where cluster is present.
  31. *
  32. * @param endpoint Ver.: always
  33. *
  34. */
  35. void emberAfWakeOnLanClusterInitCallback(chip::EndpointId endpoint)
  36. {
  37. ChipLogProgress(Zcl, "TV Android App: WakeOnLan::PostClusterInit");
  38. TvAppJNIMgr().PostClusterInit(chip::app::Clusters::WakeOnLan::Id, endpoint);
  39. }
  40. void WakeOnLanManager::NewManager(jint endpoint, jobject manager)
  41. {
  42. ChipLogProgress(Zcl, "TV Android App: WakeOnLan::SetDefaultDelegate");
  43. WakeOnLanManager * mgr = new WakeOnLanManager();
  44. mgr->InitializeWithObjects(manager);
  45. chip::app::Clusters::WakeOnLan::SetDefaultDelegate(static_cast<EndpointId>(endpoint), mgr);
  46. }
  47. CHIP_ERROR WakeOnLanManager::HandleGetMacAddress(chip::app::AttributeValueEncoder & aEncoder)
  48. {
  49. jobject javaMac;
  50. CHIP_ERROR err = CHIP_NO_ERROR;
  51. JNIEnv * env = JniReferences::GetInstance().GetEnvForCurrentThread();
  52. chip::CharSpan macValue;
  53. ChipLogProgress(Zcl, "Received WakeOnLanManager::HandleGetMacAddress");
  54. VerifyOrExit(mWakeOnLanManagerObject != nullptr, err = CHIP_ERROR_INCORRECT_STATE);
  55. VerifyOrExit(mGetMacMethod != nullptr, err = CHIP_ERROR_INCORRECT_STATE);
  56. VerifyOrExit(env != NULL, err = CHIP_JNI_ERROR_NO_ENV);
  57. env->ExceptionClear();
  58. javaMac = env->CallObjectMethod(mWakeOnLanManagerObject, mGetMacMethod);
  59. if (env->ExceptionCheck())
  60. {
  61. ChipLogError(DeviceLayer, "Java exception in WakeOnLanManager::getMac");
  62. env->ExceptionDescribe();
  63. env->ExceptionClear();
  64. goto exit;
  65. }
  66. macValue = chip::JniUtfString(env, static_cast<jstring>(javaMac)).charSpan();
  67. exit:
  68. if (err != CHIP_NO_ERROR)
  69. {
  70. ChipLogError(Zcl, "WakeOnLanManager::HandleGetMacAddress status error: %s", err.AsString());
  71. }
  72. return aEncoder.Encode(macValue);
  73. }
  74. void WakeOnLanManager::InitializeWithObjects(jobject managerObject)
  75. {
  76. JNIEnv * env = JniReferences::GetInstance().GetEnvForCurrentThread();
  77. VerifyOrReturn(env != nullptr, ChipLogError(Zcl, "Failed to GetEnvForCurrentThread for WakeOnLanManager"));
  78. mWakeOnLanManagerObject = env->NewGlobalRef(managerObject);
  79. VerifyOrReturn(mWakeOnLanManagerObject != nullptr, ChipLogError(Zcl, "Failed to NewGlobalRef WakeOnLanManager"));
  80. jclass WakeOnLanManagerClass = env->GetObjectClass(managerObject);
  81. VerifyOrReturn(WakeOnLanManagerClass != nullptr, ChipLogError(Zcl, "Failed to get WakeOnLanManager Java class"));
  82. mGetMacMethod = env->GetMethodID(WakeOnLanManagerClass, "getMac", "()Ljava/lang/String;");
  83. if (mGetMacMethod == nullptr)
  84. {
  85. ChipLogError(Zcl, "Failed to access WakeOnLanManager 'getMac' method");
  86. env->ExceptionClear();
  87. }
  88. }