ContentAppAttributeDelegate.h 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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. /**
  19. * @brief Facilitates communication with the application handlers in Java
  20. */
  21. #pragma once
  22. #include <app-common/zap-generated/ids/Clusters.h>
  23. #include <app/AttributeAccessInterface.h>
  24. #include <jni.h>
  25. #include <lib/core/DataModelTypes.h>
  26. #include <lib/support/JniReferences.h>
  27. namespace chip {
  28. namespace AppPlatform {
  29. class ContentAppAttributeDelegate
  30. {
  31. public:
  32. ContentAppAttributeDelegate(jobject manager)
  33. {
  34. if (manager == nullptr)
  35. {
  36. // To support the existing hardcoded sample apps.
  37. return;
  38. }
  39. InitializeJNIObjects(manager);
  40. }
  41. ~ContentAppAttributeDelegate()
  42. {
  43. JNIEnv * env = JniReferences::GetInstance().GetEnvForCurrentThread();
  44. VerifyOrReturn(env != nullptr, ChipLogError(Zcl, "Failed to GetEnvForCurrentThread for ContentAppEndpointManager"));
  45. env->DeleteGlobalRef(mContentAppEndpointManager);
  46. }
  47. std::string Read(const chip::app::ConcreteReadAttributePath & aPath);
  48. private:
  49. void InitializeJNIObjects(jobject manager)
  50. {
  51. JNIEnv * env = JniReferences::GetInstance().GetEnvForCurrentThread();
  52. VerifyOrReturn(env != nullptr, ChipLogError(Zcl, "Failed to GetEnvForCurrentThread for ContentAppEndpointManager"));
  53. mContentAppEndpointManager = env->NewGlobalRef(manager);
  54. VerifyOrReturn(mContentAppEndpointManager != nullptr,
  55. ChipLogError(Zcl, "Failed to NewGlobalRef ContentAppEndpointManager"));
  56. jclass ContentAppEndpointManagerClass = env->GetObjectClass(manager);
  57. VerifyOrReturn(ContentAppEndpointManagerClass != nullptr,
  58. ChipLogError(Zcl, "Failed to get ContentAppEndpointManager Java class"));
  59. mReadAttributeMethod = env->GetMethodID(ContentAppEndpointManagerClass, "readAttribute", "(IJJ)Ljava/lang/String;");
  60. if (mReadAttributeMethod == nullptr)
  61. {
  62. ChipLogError(Zcl, "Failed to access ContentAppEndpointManager 'readAttribute' method");
  63. env->ExceptionClear();
  64. }
  65. }
  66. jobject mContentAppEndpointManager = nullptr;
  67. jmethodID mReadAttributeMethod = nullptr;
  68. };
  69. } // namespace AppPlatform
  70. } // namespace chip