Skip to content

Commit

Permalink
Add vendor Id attribute as a template exception
Browse files Browse the repository at this point in the history
Github: ZAP#898
  • Loading branch information
brdandu committed Mar 20, 2023
1 parent 65160b6 commit d83ae8a
Show file tree
Hide file tree
Showing 11 changed files with 1,531 additions and 887 deletions.
50 changes: 50 additions & 0 deletions src/controller/java/templates/CHIPReadCallbacks-src.zapt
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,32 @@ void CHIP{{chipCallback.name}}AttributeCallback::CallbackFn(void * context, {{ch
{{else}}

{{#if_basic_attribute type ../id}}
{{#if (is_str_equal 'vendor_id' type)}}
CHIP{{asUpperCamelCase parent.name}}{{asUpperCamelCase name}}AttributeCallback::CHIP{{asUpperCamelCase parent.name}}{{asUpperCamelCase name}}AttributeCallback(jobject javaCallback, bool keepAlive) :
chip::Callback::Callback<CHIP{{asUpperCamelCase parent.name}}Cluster{{asUpperCamelCase name}}AttributeCallbackType>(CallbackFn, this), keepAlive(keepAlive)
{
JNIEnv * env = chip::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}}{{asUpperCamelCase name}}AttributeCallback::~CHIP{{asUpperCamelCase parent.name}}{{asUpperCamelCase name}}AttributeCallback() {
JNIEnv * env = chip::JniReferences::GetInstance().GetEnvForCurrentThread();
if (env == nullptr)
{
ChipLogError(Zcl, "Could not delete global reference for Java callback");
return;
}
env->DeleteGlobalRef(javaCallbackRef);
}
{{/if}}
{{else}}
CHIP{{asUpperCamelCase parent.name}}{{asUpperCamelCase name}}AttributeCallback::CHIP{{asUpperCamelCase parent.name}}{{asUpperCamelCase name}}AttributeCallback(jobject javaCallback, bool keepAlive) :
chip::Callback::Callback<CHIP{{asUpperCamelCase parent.name}}Cluster{{asUpperCamelCase name}}AttributeCallbackType>(CallbackFn, this), keepAlive(keepAlive)
Expand Down Expand Up @@ -146,6 +172,30 @@ void CHIP{{asUpperCamelCase parent.name}}{{asUpperCamelCase name}}AttributeCallb
}
{{else}}
{{#if_basic_attribute type ../id}}
{{#if (is_str_equal 'vendor_id' type)}}
void CHIP{{asUpperCamelCase parent.name}}{{asUpperCamelCase name}}AttributeCallback::CallbackFn(void * context, {{zapTypeToDecodableClusterObjectType type ns=parent.name isArgument=true}} value)
{
chip::DeviceLayer::StackUnlock unlock;
CHIP_ERROR err = CHIP_NO_ERROR;
JNIEnv * env = chip::JniReferences::GetInstance().GetEnvForCurrentThread();
jobject javaCallbackRef;

VerifyOrReturn(env != nullptr, ChipLogError(Zcl, "Could not get JNI env"));
std::unique_ptr<CHIP{{asUpperCamelCase parent.name}}{{asUpperCamelCase name}}AttributeCallback, decltype(&maybeDestroy)> cppCallback(reinterpret_cast<CHIP{{asUpperCamelCase parent.name}}{{asUpperCamelCase name}}AttributeCallback *>(context), maybeDestroy);

// It's valid for javaCallbackRef to be nullptr if the Java code passed in a null callback.
javaCallbackRef = cppCallback.get()->javaCallbackRef;
VerifyOrReturn(javaCallbackRef != nullptr, ChipLogProgress(Zcl, "Early return from attribute callback since Java callback is null"));

jmethodID javaMethod;
err = chip::JniReferences::GetInstance().FindMethod(env, javaCallbackRef, "onSuccess", "({{#if isArray}}{{else if isStruct}}{{else if isOptional}}Ljava/util/Optional;{{else if (isOctetString type)}}[B{{else if (isCharString type)}}Ljava/lang/String;{{else}}{{asJniSignatureBasic type true}}{{/if}})V", &javaMethod);
VerifyOrReturn(err == CHIP_NO_ERROR, ChipLogError(Zcl, "Could not find onSuccess() method"));

{{>decode_value source="value" target="javaValue" cluster=(asUpperCamelCase parent.name) depth=0}}

env->CallVoidMethod(javaCallbackRef, javaMethod, javaValue);
}
{{/if}}
{{else}}
void CHIP{{asUpperCamelCase parent.name}}{{asUpperCamelCase name}}AttributeCallback::CallbackFn(void * context, {{zapTypeToDecodableClusterObjectType type ns=parent.name isArgument=true}} value)
{
Expand Down
26 changes: 26 additions & 0 deletions src/controller/java/templates/CHIPReadCallbacks.zapt
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,32 @@ private:
{{#if_unsupported_attribute_callback type isArray ../id}}
{{else}}
{{#if_basic_attribute type ../id}}
{{#if (is_str_equal 'vendor_id' type)}}
class CHIP{{asUpperCamelCase parent.name}}{{asUpperCamelCase name}}AttributeCallback : public chip::Callback::Callback<CHIP{{asUpperCamelCase parent.name}}Cluster{{asUpperCamelCase name}}AttributeCallbackType>
{
public:
CHIP{{asUpperCamelCase parent.name}}{{asUpperCamelCase name}}AttributeCallback(jobject javaCallback, bool keepAlive = false);

~CHIP{{asUpperCamelCase parent.name}}{{asUpperCamelCase name}}AttributeCallback();

static void maybeDestroy(CHIP{{asUpperCamelCase parent.name}}{{asUpperCamelCase name}}AttributeCallback * callback) {
if (!callback->keepAlive) {
callback->Cancel();
chip::Platform::Delete<CHIP{{asUpperCamelCase parent.name}}{{asUpperCamelCase name}}AttributeCallback>(callback);
}
}

static void CallbackFn(void * context, {{zapTypeToDecodableClusterObjectType type ns=parent.name isArgument=true}} {{#if isArray}}list{{else}}value{{/if}});
static void OnSubscriptionEstablished(void * context, chip::SubscriptionId subscriptionId) {
CHIP_ERROR err = chip::JniReferences::GetInstance().CallSubscriptionEstablished(reinterpret_cast<CHIP{{asUpperCamelCase parent.name}}{{asUpperCamelCase name}}AttributeCallback *>(context)->javaCallbackRef, subscriptionId);
VerifyOrReturn(err == CHIP_NO_ERROR, ChipLogError(Zcl, "Error calling onSubscriptionEstablished: %s", ErrorStr(err)));
};

private:
jobject javaCallbackRef;
bool keepAlive;
};
{{/if}}
{{else}}
class CHIP{{asUpperCamelCase parent.name}}{{asUpperCamelCase name}}AttributeCallback : public chip::Callback::Callback<CHIP{{asUpperCamelCase parent.name}}Cluster{{asUpperCamelCase name}}AttributeCallbackType>
{
Expand Down
34 changes: 34 additions & 0 deletions src/controller/java/templates/ChipClusters-java.zapt
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,24 @@ public class ChipClusters {
{{#if_unsupported_attribute_callback type isArray ../id}}
{{else}}
{{#if_basic_attribute type ../id}}
{{#if (is_str_equal 'vendor_id' type)}}
{{! NOTE: asJavaType ends up sniffing for isArray on the context. Since we want the type of our _entry_, force isArray to
false. }}
{{~#*inline "asJavaTypeForEntry"}}{{asJavaType type null parent.name forceNotList=true}}{{/inline~}}
{{#if isArray}}
public interface {{asUpperCamelCase name}}AttributeCallback {
void onSuccess({{#if isNullable}}@Nullable{{/if}} List<{{>asJavaTypeForEntry isArray=false}}> valueList);
void onError(Exception ex);
default void onSubscriptionEstablished(long subscriptionId) {}
}
{{else}}
public interface {{asUpperCamelCase name}}AttributeCallback {
void onSuccess({{#if isNullable}}@Nullable{{/if}} {{>asJavaTypeForEntry isArray=false}} value);
void onError(Exception ex);
default void onSubscriptionEstablished(long subscriptionId) {}
}
{{/if}}
{{/if}}
{{else}}
{{! NOTE: asJavaType ends up sniffing for isArray on the context. Since we want the type of our _entry_, force isArray to
false. }}
Expand Down Expand Up @@ -172,7 +190,11 @@ public class ChipClusters {

public void read{{asUpperCamelCase name}}Attribute(
{{#if_basic_attribute type ../id}}
{{#if (is_str_equal 'vendor_id' type)}}
{{asUpperCamelCase name}}AttributeCallback callback
{{else}}
{{as_underlying_java_zcl_type type ../id boolean="Boolean"}}AttributeCallback callback
{{/if}}
{{else}}
{{asUpperCamelCase name}}AttributeCallback callback
{{/if_basic_attribute}}
Expand All @@ -193,7 +215,11 @@ public class ChipClusters {
{{#if isReportableAttribute}}
public void subscribe{{asCamelCased name false}}Attribute(
{{#if_basic_attribute type ../id}}
{{#if (is_str_equal 'vendor_id' type)}}
{{asUpperCamelCase name}}AttributeCallback callback
{{else}}
{{as_underlying_java_zcl_type type ../id boolean="Boolean"}}AttributeCallback callback
{{/if}}
{{else}}
{{asUpperCamelCase name}}AttributeCallback callback
{{/if_basic_attribute}},
Expand All @@ -210,7 +236,11 @@ public class ChipClusters {

private native void read{{asUpperCamelCase name}}Attribute(long chipClusterPtr,
{{#if_basic_attribute type ../id}}
{{#if (is_str_equal 'vendor_id' type)}}
{{asUpperCamelCase name}}AttributeCallback callback
{{else}}
{{as_underlying_java_zcl_type type ../id boolean="Boolean"}}AttributeCallback callback
{{/if}}
{{else}}
{{asUpperCamelCase name}}AttributeCallback callback
{{/if_basic_attribute}}
Expand All @@ -222,7 +252,11 @@ public class ChipClusters {
{{#if isReportableAttribute}}
private native void subscribe{{asCamelCased name false}}Attribute(long chipClusterPtr,
{{#if_basic_attribute type ../id}}
{{#if (is_str_equal 'vendor_id' type)}}
{{asUpperCamelCase name}}AttributeCallback callback
{{else}}
{{as_underlying_java_zcl_type type ../id boolean="Boolean"}}AttributeCallback callback
{{/if}}
{{else}}
{{asUpperCamelCase name}}AttributeCallback callback
{{/if_basic_attribute}}, int minInterval, int maxInterval);
Expand Down
33 changes: 33 additions & 0 deletions src/controller/java/templates/ClusterInfo-java.zapt
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,39 @@ public class ClusterInfoMapping {
{{#if_unsupported_attribute_callback type isArray ../id}}
{{else}}
{{#if_basic_attribute type ../id}}
{{#if (is_str_equal 'vendor_id' type)}}
public static class Delegated{{asUpperCamelCase parent.name}}Cluster{{asUpperCamelCase name}}AttributeCallback implements ChipClusters.{{asUpperCamelCase ../name}}Cluster.{{asUpperCamelCase name}}AttributeCallback, DelegatedClusterCallback {
private ClusterCommandCallback callback;
@Override
public void setCallbackDelegate(ClusterCommandCallback callback) {
this.callback = callback;
}

{{! NOTE: asJavaType ends up sniffing for isArray on the context. Since we want the type of our _entry_, force isArray to
false. }}
{{~#*inline "asJavaTypeForEntry"}}{{asJavaType type null parent.name forceNotList=true}}{{/inline~}}
@Override
{{#if isArray}}
public void onSuccess({{#if isNullable}}@Nullable{{/if}} List<{{> asJavaTypeForEntry isArray=false}}> valueList) {
Map<CommandResponseInfo, Object> responseValues = new LinkedHashMap<>();
CommandResponseInfo commandResponseInfo = new CommandResponseInfo("valueList", "List<{{> asJavaTypeForEntry isArray=false}}>");
responseValues.put(commandResponseInfo, valueList);
callback.onSuccess(responseValues);
}
{{else}}
public void onSuccess({{#if isNullable}}@Nullable{{/if}} {{>asJavaTypeForEntry isArray=false}} value) {
Map<CommandResponseInfo, Object> responseValues = new LinkedHashMap<>();
CommandResponseInfo commandResponseInfo = new CommandResponseInfo("value", "{{> asJavaTypeForEntry isArray=false}}");
responseValues.put(commandResponseInfo, value);
callback.onSuccess(responseValues);
}
{{/if}}
@Override
public void onError(Exception ex) {
callback.onFailure(ex);
}
}
{{/if}}
{{else}}
public static class Delegated{{asUpperCamelCase parent.name}}Cluster{{asUpperCamelCase name}}AttributeCallback implements ChipClusters.{{asUpperCamelCase ../name}}Cluster.{{asUpperCamelCase name}}AttributeCallback, DelegatedClusterCallback {
private ClusterCommandCallback callback;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,22 @@ public class ClusterReadMapping {
(cluster, callback, commandArguments) -> {
((ChipClusters.{{asUpperCamelCase ../name}}Cluster) cluster).read{{asUpperCamelCase name}}Attribute(
({{#if_basic_attribute type ../id}}
{{#if (is_str_equal 'vendor_id' type)}}
ChipClusters.{{asUpperCamelCase ../name}}Cluster.{{asUpperCamelCase name}}AttributeCallback
{{else}}
ChipClusters.{{as_underlying_java_zcl_type type ../id boolean="Boolean"}}AttributeCallback
{{/if}}
{{else}}
ChipClusters.{{asUpperCamelCase ../name}}Cluster.{{asUpperCamelCase name}}AttributeCallback
{{/if_basic_attribute}}) callback
);
},
{{#if_basic_attribute type ../id}}
{{#if (is_str_equal 'vendor_id' type)}}
() -> new ClusterInfoMapping.Delegated{{asUpperCamelCase parent.name}}Cluster{{asUpperCamelCase name}}AttributeCallback(),
{{else}}
() -> new ClusterInfoMapping.Delegated{{as_underlying_java_zcl_type type ../id boolean="Boolean"}}AttributeCallback(),
{{/if}}
{{else}}
() -> new ClusterInfoMapping.Delegated{{asUpperCamelCase parent.name}}Cluster{{asUpperCamelCase name}}AttributeCallback(),
{{/if_basic_attribute}}
Expand Down
Loading

0 comments on commit d83ae8a

Please sign in to comment.