ContentAppCommandDelegate.h 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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 <jni.h>
  23. #include <lib/core/DataModelTypes.h>
  24. #include <lib/support/JniReferences.h>
  25. namespace chip {
  26. namespace AppPlatform {
  27. class ContentAppCommandDelegate
  28. {
  29. public:
  30. ContentAppCommandDelegate(jobject manager)
  31. {
  32. if (manager == nullptr)
  33. {
  34. // To support the existing hardcoded sample apps.
  35. return;
  36. }
  37. JNIEnv * env = JniReferences::GetInstance().GetEnvForCurrentThread();
  38. VerifyOrReturn(env != nullptr, ChipLogError(Zcl, "Failed to GetEnvForCurrentThread for ContentAppEndpointManager"));
  39. mContentAppEndpointManager = env->NewGlobalRef(manager);
  40. VerifyOrReturn(mContentAppEndpointManager != nullptr,
  41. ChipLogError(Zcl, "Failed to NewGlobalRef ContentAppEndpointManager"));
  42. jclass ContentAppEndpointManagerClass = env->GetObjectClass(manager);
  43. VerifyOrReturn(ContentAppEndpointManagerClass != nullptr,
  44. ChipLogError(Zcl, "Failed to get ContentAppEndpointManager Java class"));
  45. mSendCommandMethod =
  46. env->GetMethodID(ContentAppEndpointManagerClass, "sendCommand", "(ILjava/lang/String;)Ljava/lang/String;");
  47. if (mSendCommandMethod == nullptr)
  48. {
  49. ChipLogError(Zcl, "Failed to access ContentAppEndpointManager 'sendCommand' method");
  50. env->ExceptionClear();
  51. }
  52. };
  53. const char * sendCommand(chip::EndpointId epID, std::string commandPayload);
  54. private:
  55. jobject mContentAppEndpointManager = nullptr;
  56. jmethodID mSendCommandMethod = nullptr;
  57. };
  58. } // namespace AppPlatform
  59. } // namespace chip