-
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.
[Android] Use InvokeCommand and support nullable/optional for commands (
#11873) * Use InvokeCommand and support nullable/optional for commands. * Regenerate * Restyled by whitespace * Restyled by clang-format * Account for array/struct in encode_value/decode_value. * Add TODO for if_is_struct Co-authored-by: Restyled.io <[email protected]>
- Loading branch information
Showing
23 changed files
with
12,308 additions
and
12,283 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
{{> header}} | ||
{{#if (chip_has_client_clusters)}} | ||
#include <app/util/af-enums.h> | ||
#include <app-common/zap-generated/cluster-objects.h> | ||
|
||
typedef void (*CHIPDefaultSuccessCallbackType)(void *, const chip::app::DataModel::NullObjectType &); | ||
typedef void (*CHIPDefaultFailureCallbackType)(void *, EmberAfStatus); | ||
|
||
{{#chip_client_clusters}} | ||
{{#chip_cluster_responses}} | ||
typedef void (*CHIP{{asUpperCamelCase parent.name}}Cluster{{asUpperCamelCase name}}CallbackType)(void *, const chip::app::Clusters::{{asUpperCamelCase parent.name}}::Commands::{{asUpperCamelCase name}}::DecodableType &); | ||
{{/chip_cluster_responses}} | ||
{{/chip_client_clusters}} | ||
{{/if}} |
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
76 changes: 76 additions & 0 deletions
76
src/controller/java/templates/CHIPInvokeCallbacks-src.zapt
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,76 @@ | ||
{{> header}} | ||
{{#if (chip_has_client_clusters)}} | ||
#include "CHIPCallbackTypes.h" | ||
#include "CHIPInvokeCallbacks.h" | ||
|
||
#include <app-common/zap-generated/cluster-objects.h> | ||
#include <jni.h> | ||
#include <lib/support/CodeUtils.h> | ||
#include <lib/support/CHIPJNIError.h> | ||
#include <lib/support/JniReferences.h> | ||
#include <lib/support/JniTypeWrappers.h> | ||
#include <platform/PlatformManager.h> | ||
|
||
{{! TODO(#8773): Clean up callbacks. }} | ||
|
||
namespace chip { | ||
|
||
{{#chip_client_clusters}} | ||
{{#chip_cluster_responses}} | ||
CHIP{{asUpperCamelCase parent.name}}Cluster{{asUpperCamelCase name}}Callback::CHIP{{asUpperCamelCase parent.name}}Cluster{{asUpperCamelCase name}}Callback(jobject javaCallback): Callback::Callback<CHIP{{asUpperCamelCase parent.name}}Cluster{{asUpperCamelCase name}}CallbackType>(CallbackFn, this) | ||
{ | ||
JNIEnv * env = JniReferences::GetInstance().GetEnvForCurrentThread(); | ||
if (env == nullptr) { | ||
ChipLogError(Zcl, "Could not create global reference for Java callback"); | ||
return; | ||
} | ||
|
||
javaCallbackRef = env->NewGlobalRef(javaCallback); | ||
if (javaCallbackRef == nullptr) { | ||
ChipLogError(Zcl, "Could not create global reference for Java callback"); | ||
} | ||
} | ||
|
||
CHIP{{asUpperCamelCase parent.name}}Cluster{{asUpperCamelCase name}}Callback::~CHIP{{asUpperCamelCase parent.name}}Cluster{{asUpperCamelCase name}}Callback() | ||
{ | ||
JNIEnv * env = JniReferences::GetInstance().GetEnvForCurrentThread(); | ||
if (env == nullptr) { | ||
ChipLogError(Zcl, "Could not delete global reference for Java callback"); | ||
return; | ||
} | ||
env->DeleteGlobalRef(javaCallbackRef); | ||
}; | ||
|
||
void CHIP{{asUpperCamelCase parent.name}}Cluster{{asUpperCamelCase name}}Callback::CallbackFn(void * context, const chip::app::Clusters::{{asUpperCamelCase parent.name}}::Commands::{{asUpperCamelCase name}}::DecodableType & dataResponse) | ||
{ | ||
chip::DeviceLayer::StackUnlock unlock; | ||
CHIP_ERROR err = CHIP_NO_ERROR; | ||
JNIEnv * env = JniReferences::GetInstance().GetEnvForCurrentThread(); | ||
jobject javaCallbackRef; | ||
jmethodID javaMethod; | ||
|
||
VerifyOrReturn(env != nullptr, ChipLogError(Zcl, "Error invoking Java callback: no JNIEnv")); | ||
|
||
std::unique_ptr<CHIP{{asUpperCamelCase parent.name}}Cluster{{asUpperCamelCase name}}Callback, void (*)(CHIP{{asUpperCamelCase parent.name}}Cluster{{asUpperCamelCase name}}Callback *)> cppCallback( | ||
reinterpret_cast<CHIP{{asUpperCamelCase parent.name}}Cluster{{asUpperCamelCase name}}Callback *>(context), | ||
chip::Platform::Delete<CHIP{{asUpperCamelCase parent.name}}Cluster{{asUpperCamelCase name}}Callback> | ||
); | ||
VerifyOrReturn(cppCallback != nullptr, ChipLogError(Zcl, "Error invoking Java callback: failed to cast native callback")); | ||
|
||
javaCallbackRef = cppCallback->javaCallbackRef; | ||
// Java callback is allowed to be null, exit early if this is the case. | ||
VerifyOrReturn(javaCallbackRef != nullptr); | ||
|
||
err = JniReferences::GetInstance().FindMethod(env, javaCallbackRef, "onSuccess", "({{#chip_cluster_response_arguments}}{{#if isArray}}{{else if isOptional}}Ljava/util/Optional;{{else if (isOctetString type)}}[B{{else if (isShortString type)}}Ljava/lang/String;{{else}}{{asJniSignature type true}}{{/if}}{{/chip_cluster_response_arguments}})V", &javaMethod); | ||
VerifyOrReturn(err == CHIP_NO_ERROR, ChipLogError(Zcl, "Error invoking Java callback: %s", ErrorStr(err))); | ||
|
||
{{#chip_cluster_response_arguments}} | ||
{{>decode_value}} | ||
{{/chip_cluster_response_arguments}} | ||
|
||
env->CallVoidMethod(javaCallbackRef, javaMethod{{#chip_cluster_response_arguments}}, {{asSymbol label}}{{/chip_cluster_response_arguments}}); | ||
} | ||
{{/chip_cluster_responses}} | ||
{{/chip_client_clusters}} | ||
} // namespace chip | ||
{{/if}} |
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,29 @@ | ||
{{> header}} | ||
{{#if (chip_has_client_clusters)}} | ||
#include "CHIPCallbackTypes.h" | ||
|
||
#include <app-common/zap-generated/cluster-objects.h> | ||
#include <jni.h> | ||
#include <zap-generated/CHIPClientCallbacks.h> | ||
|
||
namespace chip { | ||
|
||
{{#chip_client_clusters}} | ||
{{#chip_cluster_responses}} | ||
class CHIP{{asUpperCamelCase parent.name}}Cluster{{asUpperCamelCase name}}Callback : public Callback::Callback<CHIP{{asUpperCamelCase parent.name}}Cluster{{asUpperCamelCase name}}CallbackType> | ||
{ | ||
public: | ||
CHIP{{asUpperCamelCase parent.name}}Cluster{{asUpperCamelCase name}}Callback(jobject javaCallback); | ||
|
||
~CHIP{{asUpperCamelCase parent.name}}Cluster{{asUpperCamelCase name}}Callback(); | ||
|
||
static void CallbackFn(void * context, const chip::app::Clusters::{{asUpperCamelCase parent.name}}::Commands::{{asUpperCamelCase name}}::DecodableType & data); | ||
|
||
private: | ||
jobject javaCallbackRef; | ||
}; | ||
|
||
{{/chip_cluster_responses}} | ||
{{/chip_client_clusters}} | ||
} // namespace chip | ||
{{/if}} |
Oops, something went wrong.