KeypadInputManager.cpp 3.9 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 "KeypadInputManager.h"
  19. #include "TvApp-JNI.h"
  20. #include <app-common/zap-generated/attributes/Accessors.h>
  21. #include <app-common/zap-generated/ids/Clusters.h>
  22. #include <app/util/config.h>
  23. #include <lib/support/CHIPJNIError.h>
  24. #include <lib/support/JniReferences.h>
  25. using namespace chip;
  26. using namespace chip::app::Clusters::KeypadInput;
  27. void emberAfKeypadInputClusterInitCallback(EndpointId endpoint)
  28. {
  29. ChipLogProgress(Zcl, "TV Android App: KeypadInput::PostClusterInit");
  30. TvAppJNIMgr().PostClusterInit(chip::app::Clusters::KeypadInput::Id, endpoint);
  31. }
  32. void KeypadInputManager::NewManager(jint endpoint, jobject manager)
  33. {
  34. ChipLogProgress(Zcl, "TV Android App: KeypadInput::SetDefaultDelegate");
  35. KeypadInputManager * mgr = new KeypadInputManager();
  36. mgr->InitializeWithObjects(manager);
  37. chip::app::Clusters::KeypadInput::SetDefaultDelegate(static_cast<EndpointId>(endpoint), mgr);
  38. }
  39. void KeypadInputManager::HandleSendKey(CommandResponseHelper<SendKeyResponseType> & helper, const CecKeyCode & keyCode)
  40. {
  41. Commands::SendKeyResponse::Type response;
  42. jint ret = -1;
  43. CHIP_ERROR err = CHIP_NO_ERROR;
  44. JNIEnv * env = JniReferences::GetInstance().GetEnvForCurrentThread();
  45. ChipLogProgress(Zcl, "Received keypadInputClusterSendKey: %c", to_underlying(keyCode));
  46. VerifyOrExit(mKeypadInputManagerObject != nullptr, err = CHIP_ERROR_INCORRECT_STATE);
  47. VerifyOrExit(mSendKeyMethod != nullptr, err = CHIP_ERROR_INCORRECT_STATE);
  48. VerifyOrExit(env != NULL, err = CHIP_JNI_ERROR_NO_ENV);
  49. env->ExceptionClear();
  50. ret = env->CallIntMethod(mKeypadInputManagerObject, mSendKeyMethod, static_cast<jint>(keyCode));
  51. VerifyOrExit(!env->ExceptionCheck(), err = CHIP_JNI_ERROR_EXCEPTION_THROWN);
  52. exit:
  53. if (err != CHIP_NO_ERROR)
  54. {
  55. response.status = chip::app::Clusters::KeypadInput::KeypadInputStatusEnum::kInvalidKeyInCurrentState;
  56. }
  57. else
  58. {
  59. response.status = static_cast<chip::app::Clusters::KeypadInput::KeypadInputStatusEnum>(ret);
  60. }
  61. helper.Success(response);
  62. }
  63. void KeypadInputManager::InitializeWithObjects(jobject managerObject)
  64. {
  65. JNIEnv * env = JniReferences::GetInstance().GetEnvForCurrentThread();
  66. VerifyOrReturn(env != nullptr, ChipLogError(Zcl, "Failed to GetEnvForCurrentThread for KeypadInputManager"));
  67. mKeypadInputManagerObject = env->NewGlobalRef(managerObject);
  68. VerifyOrReturn(mKeypadInputManagerObject != nullptr, ChipLogError(Zcl, "Failed to NewGlobalRef KeypadInputManager"));
  69. jclass KeypadInputManagerClass = env->GetObjectClass(managerObject);
  70. VerifyOrReturn(KeypadInputManagerClass != nullptr, ChipLogError(Zcl, "Failed to get KeypadInputManager Java class"));
  71. mSendKeyMethod = env->GetMethodID(KeypadInputManagerClass, "sendKey", "(I)I");
  72. if (mSendKeyMethod == nullptr)
  73. {
  74. ChipLogError(Zcl, "Failed to access KeypadInputManager 'sendKey' method");
  75. env->ExceptionClear();
  76. }
  77. }
  78. uint32_t KeypadInputManager::GetFeatureMap(chip::EndpointId endpoint)
  79. {
  80. if (endpoint >= EMBER_AF_CONTENT_LAUNCHER_CLUSTER_SERVER_ENDPOINT_COUNT)
  81. {
  82. return mDynamicEndpointFeatureMap;
  83. }
  84. uint32_t featureMap = 0;
  85. Attributes::FeatureMap::Get(endpoint, &featureMap);
  86. return featureMap;
  87. }