ContentAppCommandDelegate.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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/cluster-objects.h>
  23. #include <app-common/zap-generated/ids/Clusters.h>
  24. #include <app/CommandHandlerInterface.h>
  25. #include <jni.h>
  26. #include <json/json.h>
  27. #include <lib/core/DataModelTypes.h>
  28. #include <lib/support/JniReferences.h>
  29. namespace chip {
  30. namespace AppPlatform {
  31. using CommandHandlerInterface = chip::app::CommandHandlerInterface;
  32. using Status = Protocols::InteractionModel::Status;
  33. using LaunchResponseType = chip::app::Clusters::ContentLauncher::Commands::LauncherResponse::Type;
  34. using PlaybackResponseType = chip::app::Clusters::MediaPlayback::Commands::PlaybackResponse::Type;
  35. using NavigateTargetResponseType = chip::app::Clusters::TargetNavigator::Commands::NavigateTargetResponse::Type;
  36. using GetSetupPINResponseType = chip::app::Clusters::AccountLogin::Commands::GetSetupPINResponse::Type;
  37. class ContentAppCommandDelegate : public CommandHandlerInterface
  38. {
  39. public:
  40. ContentAppCommandDelegate(jobject manager, ClusterId aClusterId) : CommandHandlerInterface(Optional<EndpointId>(), aClusterId)
  41. {
  42. if (manager == nullptr)
  43. {
  44. // To support the existing hardcoded sample apps.
  45. return;
  46. }
  47. InitializeJNIObjects(manager);
  48. };
  49. ContentAppCommandDelegate(jobject manager) : CommandHandlerInterface(Optional<EndpointId>(), app::Clusters::ContentLauncher::Id)
  50. {
  51. if (manager == nullptr)
  52. {
  53. // To support the existing hardcoded sample apps.
  54. return;
  55. }
  56. InitializeJNIObjects(manager);
  57. };
  58. ~ContentAppCommandDelegate()
  59. {
  60. JNIEnv * env = JniReferences::GetInstance().GetEnvForCurrentThread();
  61. VerifyOrReturn(env != nullptr, ChipLogError(Zcl, "Failed to GetEnvForCurrentThread for ContentAppEndpointManager"));
  62. env->DeleteGlobalRef(mContentAppEndpointManager);
  63. }
  64. void InvokeCommand(CommandHandlerInterface::HandlerContext & handlerContext) override;
  65. Status InvokeCommand(EndpointId epId, ClusterId clusterId, CommandId commandId, std::string payload, bool & commandHandled,
  66. Json::Value & value);
  67. GetSetupPINResponseType FormatGetSetupPINResponse(Json::Value value, Status & status);
  68. LaunchResponseType FormatContentLauncherResponse(Json::Value value, Status & status);
  69. NavigateTargetResponseType FormatNavigateTargetResponse(Json::Value value, Status & status);
  70. PlaybackResponseType FormatMediaPlaybackResponse(Json::Value value, Status & status);
  71. private:
  72. void InitializeJNIObjects(jobject manager)
  73. {
  74. JNIEnv * env = JniReferences::GetInstance().GetEnvForCurrentThread();
  75. VerifyOrReturn(env != nullptr, ChipLogError(Zcl, "Failed to GetEnvForCurrentThread for ContentAppEndpointManager"));
  76. mContentAppEndpointManager = env->NewGlobalRef(manager);
  77. VerifyOrReturn(mContentAppEndpointManager != nullptr,
  78. ChipLogError(Zcl, "Failed to NewGlobalRef ContentAppEndpointManager"));
  79. jclass ContentAppEndpointManagerClass = env->GetObjectClass(manager);
  80. VerifyOrReturn(ContentAppEndpointManagerClass != nullptr,
  81. ChipLogError(Zcl, "Failed to get ContentAppEndpointManager Java class"));
  82. mSendCommandMethod =
  83. env->GetMethodID(ContentAppEndpointManagerClass, "sendCommand", "(IJJLjava/lang/String;)Ljava/lang/String;");
  84. if (mSendCommandMethod == nullptr)
  85. {
  86. ChipLogError(Zcl, "Failed to access ContentAppEndpointManager 'sendCommand' method");
  87. env->ExceptionClear();
  88. }
  89. }
  90. void FormatResponseData(CommandHandlerInterface::HandlerContext & handlerContext, const char * response);
  91. jobject mContentAppEndpointManager = nullptr;
  92. jmethodID mSendCommandMethod = nullptr;
  93. };
  94. } // namespace AppPlatform
  95. } // namespace chip