-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add JNI channel to dispatch device event (#18837)
* add device event to JNI channel * restyled * format changes * one more restyled
- Loading branch information
1 parent
3e37216
commit 3084893
Showing
7 changed files
with
183 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
/* | ||
* | ||
* Copyright (c) 2022 Project CHIP Authors | ||
* All rights reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
/** | ||
* @file DeviceCallbacks.cpp | ||
* | ||
* Implements all the callbacks to the application from the CHIP Stack | ||
* | ||
**/ | ||
#include "DeviceCallbacks.h" | ||
|
||
#include <app-common/zap-generated/attribute-id.h> | ||
#include <app-common/zap-generated/cluster-id.h> | ||
#include <app/CommandHandler.h> | ||
#include <app/server/Dnssd.h> | ||
#include <app/util/af.h> | ||
#include <app/util/basic-types.h> | ||
#include <app/util/util.h> | ||
#include <jni.h> | ||
#include <lib/dnssd/Advertiser.h> | ||
#include <lib/support/JniReferences.h> | ||
|
||
using namespace ::chip; | ||
using namespace ::chip::Inet; | ||
using namespace ::chip::System; | ||
using namespace ::chip::DeviceLayer; | ||
using namespace ::chip::Logging; | ||
namespace { | ||
void OnPlatformEventWrapper(const chip::DeviceLayer::ChipDeviceEvent * event, intptr_t arg) | ||
{ | ||
DeviceCallbacks * commissionMgr = reinterpret_cast<DeviceCallbacks *>(arg); | ||
commissionMgr->OnPlatformEvent(event); | ||
} | ||
} // namespace | ||
void DeviceCallbacks::NewManager(jobject manager) | ||
{ | ||
ChipLogProgress(AppServer, "TV Android App: set ChipDeviceEvent delegate"); | ||
DeviceCallbacks * mgr = new DeviceCallbacks(); | ||
PlatformMgr().AddEventHandler(OnPlatformEventWrapper, reinterpret_cast<intptr_t>(mgr)); | ||
mgr->InitializeWithObjects(manager); | ||
} | ||
|
||
void DeviceCallbacks::InitializeWithObjects(jobject provider) | ||
{ | ||
JNIEnv * env = JniReferences::GetInstance().GetEnvForCurrentThread(); | ||
VerifyOrReturn(env != nullptr, ChipLogError(AppServer, "Failed to GetEnvForCurrentThread for DeviceEventProvider")); | ||
|
||
mProvider = env->NewGlobalRef(provider); | ||
VerifyOrReturn(mProvider != nullptr, ChipLogError(AppServer, "Failed to NewGlobalRef DeviceEventProvider")); | ||
jclass deviceEventProviderCls = env->GetObjectClass(mProvider); | ||
VerifyOrReturn(deviceEventProviderCls != nullptr, ChipLogError(AppServer, "Failed to get KeypadInputManager Java class")); | ||
|
||
mCommissioningCompleteMethod = env->GetMethodID(deviceEventProviderCls, "onCommissioningComplete", "()V"); | ||
if (mCommissioningCompleteMethod == nullptr) | ||
{ | ||
ChipLogError(AppServer, "Failed to access DeviceEventProvider 'onCommissioningComplete' method"); | ||
env->ExceptionClear(); | ||
} | ||
} | ||
|
||
void DeviceCallbacks::OnPlatformEvent(const ChipDeviceEvent * event) | ||
{ | ||
switch (event->Type) | ||
{ | ||
case DeviceEventType::kCommissioningComplete: | ||
OnCommissioningComplete(event); | ||
break; | ||
case DeviceEventType::kSessionEstablished: | ||
OnSessionEstablished(event); | ||
break; | ||
} | ||
} | ||
|
||
void DeviceCallbacks::OnSessionEstablished(const ChipDeviceEvent * event) | ||
{ | ||
if (event->SessionEstablished.IsCommissioner) | ||
{ | ||
ChipLogProgress(AppServer, "Commissioner detected!"); | ||
} | ||
} | ||
void DeviceCallbacks::OnCommissioningComplete(const ChipDeviceEvent * event) | ||
{ | ||
JNIEnv * env = JniReferences::GetInstance().GetEnvForCurrentThread(); | ||
VerifyOrReturn(env != nullptr, ChipLogError(Zcl, "Failed to GetEnvForCurrentThread for DeviceEventProvider")); | ||
env->CallVoidMethod(mProvider, mCommissioningCompleteMethod); | ||
if (env->ExceptionCheck()) | ||
{ | ||
ChipLogError(AppServer, "Java exception in DeviceEventProvider::onCommissioningComplete"); | ||
env->ExceptionDescribe(); | ||
env->ExceptionClear(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
/* | ||
* | ||
* Copyright (c) 2022 Project CHIP Authors | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
#pragma once | ||
|
||
#include "lib/support/logging/CHIPLogging.h" | ||
#include <jni.h> | ||
|
||
class DeviceCallbacks | ||
{ | ||
|
||
public: | ||
static void NewManager(jobject manager); | ||
void OnPlatformEvent(const chip::DeviceLayer::ChipDeviceEvent * event); | ||
void InitializeWithObjects(jobject manager); | ||
|
||
private: | ||
jobject mProvider = nullptr; | ||
jmethodID mCommissioningCompleteMethod = nullptr; | ||
void OnSessionEstablished(const chip::DeviceLayer::ChipDeviceEvent * event); | ||
void OnCommissioningComplete(const chip::DeviceLayer::ChipDeviceEvent * event); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
22 changes: 22 additions & 0 deletions
22
examples/tv-app/android/java/src/com/tcl/chip/tvapp/DeviceEventProvider.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
/* | ||
* Copyright (c) 2022 Project CHIP Authors | ||
* All rights reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* | ||
*/ | ||
package com.tcl.chip.tvapp; | ||
|
||
public interface DeviceEventProvider { | ||
void onCommissioningComplete(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters