DeviceCallbacks.cpp 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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/CommandHandler.h>
  26. #include <app/server/Dnssd.h>
  27. #include <app/util/af.h>
  28. #include <app/util/basic-types.h>
  29. #include <app/util/util.h>
  30. #include <jni.h>
  31. #include <lib/dnssd/Advertiser.h>
  32. #include <lib/support/JniReferences.h>
  33. using namespace ::chip;
  34. using namespace ::chip::Inet;
  35. using namespace ::chip::System;
  36. using namespace ::chip::DeviceLayer;
  37. using namespace ::chip::Logging;
  38. namespace {
  39. void OnPlatformEventWrapper(const chip::DeviceLayer::ChipDeviceEvent * event, intptr_t arg)
  40. {
  41. DeviceCallbacks * commissionMgr = reinterpret_cast<DeviceCallbacks *>(arg);
  42. commissionMgr->OnPlatformEvent(event);
  43. }
  44. } // namespace
  45. void DeviceCallbacks::NewManager(jobject manager)
  46. {
  47. ChipLogProgress(AppServer, "TV Android App: set ChipDeviceEvent delegate");
  48. DeviceCallbacks * mgr = new DeviceCallbacks();
  49. PlatformMgr().AddEventHandler(OnPlatformEventWrapper, reinterpret_cast<intptr_t>(mgr));
  50. mgr->InitializeWithObjects(manager);
  51. }
  52. void DeviceCallbacks::InitializeWithObjects(jobject provider)
  53. {
  54. JNIEnv * env = JniReferences::GetInstance().GetEnvForCurrentThread();
  55. VerifyOrReturn(env != nullptr, ChipLogError(AppServer, "Failed to GetEnvForCurrentThread for DeviceEventProvider"));
  56. mProvider = env->NewGlobalRef(provider);
  57. VerifyOrReturn(mProvider != nullptr, ChipLogError(AppServer, "Failed to NewGlobalRef DeviceEventProvider"));
  58. jclass deviceEventProviderCls = env->GetObjectClass(mProvider);
  59. VerifyOrReturn(deviceEventProviderCls != nullptr, ChipLogError(AppServer, "Failed to get KeypadInputManager Java class"));
  60. mCommissioningCompleteMethod = env->GetMethodID(deviceEventProviderCls, "onCommissioningComplete", "()V");
  61. if (mCommissioningCompleteMethod == nullptr)
  62. {
  63. ChipLogError(AppServer, "Failed to access DeviceEventProvider 'onCommissioningComplete' method");
  64. env->ExceptionClear();
  65. }
  66. }
  67. void DeviceCallbacks::OnPlatformEvent(const ChipDeviceEvent * event)
  68. {
  69. switch (event->Type)
  70. {
  71. case DeviceEventType::kCommissioningComplete:
  72. OnCommissioningComplete(event);
  73. break;
  74. }
  75. }
  76. void DeviceCallbacks::OnCommissioningComplete(const ChipDeviceEvent * event)
  77. {
  78. JNIEnv * env = JniReferences::GetInstance().GetEnvForCurrentThread();
  79. VerifyOrReturn(env != nullptr, ChipLogError(Zcl, "Failed to GetEnvForCurrentThread for DeviceEventProvider"));
  80. env->CallVoidMethod(mProvider, mCommissioningCompleteMethod);
  81. if (env->ExceptionCheck())
  82. {
  83. ChipLogError(AppServer, "Java exception in DeviceEventProvider::onCommissioningComplete");
  84. env->ExceptionDescribe();
  85. env->ExceptionClear();
  86. }
  87. }