DeviceCallbacks.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. /*
  2. *
  3. * Copyright (c) 2022 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. * @file DeviceCallbacks.cpp
  20. *
  21. * Implements all the callbacks to the application from the CHIP Stack
  22. *
  23. **/
  24. #include "DeviceCallbacks.h"
  25. #include <app-common/zap-generated/attribute-id.h>
  26. #include <app-common/zap-generated/cluster-id.h>
  27. #include <app/CommandHandler.h>
  28. #include <app/server/Dnssd.h>
  29. #include <app/util/af.h>
  30. #include <app/util/basic-types.h>
  31. #include <app/util/util.h>
  32. #include <jni.h>
  33. #include <lib/dnssd/Advertiser.h>
  34. #include <lib/support/JniReferences.h>
  35. using namespace ::chip;
  36. using namespace ::chip::Inet;
  37. using namespace ::chip::System;
  38. using namespace ::chip::DeviceLayer;
  39. using namespace ::chip::Logging;
  40. namespace {
  41. void OnPlatformEventWrapper(const chip::DeviceLayer::ChipDeviceEvent * event, intptr_t arg)
  42. {
  43. DeviceCallbacks * commissionMgr = reinterpret_cast<DeviceCallbacks *>(arg);
  44. commissionMgr->OnPlatformEvent(event);
  45. }
  46. } // namespace
  47. void DeviceCallbacks::NewManager(jobject manager)
  48. {
  49. ChipLogProgress(AppServer, "TV Android App: set ChipDeviceEvent delegate");
  50. DeviceCallbacks * mgr = new DeviceCallbacks();
  51. PlatformMgr().AddEventHandler(OnPlatformEventWrapper, reinterpret_cast<intptr_t>(mgr));
  52. mgr->InitializeWithObjects(manager);
  53. }
  54. void DeviceCallbacks::InitializeWithObjects(jobject provider)
  55. {
  56. JNIEnv * env = JniReferences::GetInstance().GetEnvForCurrentThread();
  57. VerifyOrReturn(env != nullptr, ChipLogError(AppServer, "Failed to GetEnvForCurrentThread for DeviceEventProvider"));
  58. mProvider = env->NewGlobalRef(provider);
  59. VerifyOrReturn(mProvider != nullptr, ChipLogError(AppServer, "Failed to NewGlobalRef DeviceEventProvider"));
  60. jclass deviceEventProviderCls = env->GetObjectClass(mProvider);
  61. VerifyOrReturn(deviceEventProviderCls != nullptr, ChipLogError(AppServer, "Failed to get KeypadInputManager Java class"));
  62. mCommissioningCompleteMethod = env->GetMethodID(deviceEventProviderCls, "onCommissioningComplete", "()V");
  63. if (mCommissioningCompleteMethod == nullptr)
  64. {
  65. ChipLogError(AppServer, "Failed to access DeviceEventProvider 'onCommissioningComplete' method");
  66. env->ExceptionClear();
  67. }
  68. }
  69. void DeviceCallbacks::OnPlatformEvent(const ChipDeviceEvent * event)
  70. {
  71. switch (event->Type)
  72. {
  73. case DeviceEventType::kCommissioningComplete:
  74. OnCommissioningComplete(event);
  75. break;
  76. case DeviceEventType::kSessionEstablished:
  77. OnSessionEstablished(event);
  78. break;
  79. }
  80. }
  81. void DeviceCallbacks::OnSessionEstablished(const ChipDeviceEvent * event)
  82. {
  83. if (event->SessionEstablished.IsCommissioner)
  84. {
  85. ChipLogProgress(AppServer, "Commissioner detected!");
  86. }
  87. }
  88. void DeviceCallbacks::OnCommissioningComplete(const ChipDeviceEvent * event)
  89. {
  90. JNIEnv * env = JniReferences::GetInstance().GetEnvForCurrentThread();
  91. VerifyOrReturn(env != nullptr, ChipLogError(Zcl, "Failed to GetEnvForCurrentThread for DeviceEventProvider"));
  92. env->CallVoidMethod(mProvider, mCommissioningCompleteMethod);
  93. if (env->ExceptionCheck())
  94. {
  95. ChipLogError(AppServer, "Java exception in DeviceEventProvider::onCommissioningComplete");
  96. env->ExceptionDescribe();
  97. env->ExceptionClear();
  98. }
  99. }