Skip to content

Commit

Permalink
Reject complex types in global reponse checker (#15005)
Browse files Browse the repository at this point in the history
  • Loading branch information
austinh0 authored Feb 10, 2022
1 parent d0e88bb commit dc99fb3
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 31 deletions.
17 changes: 6 additions & 11 deletions src/app/zap-templates/templates/chip/helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -163,21 +163,16 @@ function chip_server_global_responses(options)
return asBlocks.call(this, getServerGlobalAttributeResponses(this), options);
}

async function if_in_global_responses(options)
async function if_basic_global_response(options)
{
const attribute = this.response.arguments[0];
const globalResponses = await getServerGlobalAttributeResponses(this);
const responseTypeExists = globalResponses.find(
// Some fields of item/attribute here may be undefined.
item => item.isArray == attribute.isArray && item.isStruct == attribute.isStruct && item.chipType == attribute.chipType
&& item.isNullable == attribute.isNullable && item.isOptional == attribute.isOptional)
const complexType = attribute.isNullable || attribute.isOptional || attribute.isStruct || attribute.isArray;
const responseTypeExists = globalResponses.find(item => item.chipType == attribute.chipType);

if (responseTypeExists)
{
if (!complexType && responseTypeExists) {
return options.fn(this);
}
else
{
} else {
return options.inverse(this);
}
}
Expand Down Expand Up @@ -522,6 +517,6 @@ exports.chip_endpoints = chip_endpoints;
exports.chip_endpoint_clusters = chip_endpoint_clusters;
exports.if_chip_enum = if_chip_enum;
exports.if_chip_complex = if_chip_complex;
exports.if_in_global_responses = if_in_global_responses;
exports.if_basic_global_response = if_basic_global_response;
exports.chip_cluster_specific_structs = chip_cluster_specific_structs;
exports.chip_shared_structs = chip_shared_structs;
4 changes: 2 additions & 2 deletions src/controller/java/templates/CHIPClusters-JNI.zapt
Original file line number Diff line number Diff line change
Expand Up @@ -97,11 +97,11 @@ JNI_METHOD(void, {{asCamelCased ../name false}}Cluster, subscribe{{asCamelCased
{
chip::DeviceLayer::StackLock lock;
{{~#*inline "callbackName"~}}
{{~#if_in_global_responses~}}
{{~#if_basic_global_response~}}
CHIP{{chipCallback.name}}AttributeCallback
{{~else~}}
CHIP{{asCamelCased parent.name false}}{{asCamelCased name false}}AttributeCallback
{{~/if_in_global_responses~}}
{{~/if_basic_global_response~}}
{{~/inline}}

std::unique_ptr<{{>callbackName}}, void (*)({{>callbackName}} *)> onSuccess(Platform::New<{{>callbackName}}>(callback, true), chip::Platform::Delete<{{>callbackName}}>);
Expand Down
8 changes: 4 additions & 4 deletions src/controller/java/templates/CHIPReadCallbacks-src.zapt
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ void CHIP{{chipCallback.name}}AttributeCallback::CallbackFn(void * context, {{ch
{{! TODO: Add support for struct-typed attributes }}
{{#unless (isStrEqual chipCallback.name "Unsupported")}}

{{#if_in_global_responses}}
{{#if_basic_global_response}}
{{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 All @@ -114,7 +114,7 @@ CHIP{{asUpperCamelCase parent.name}}{{asUpperCamelCase name}}AttributeCallback::
}
env->DeleteGlobalRef(javaCallbackRef);
}
{{/if_in_global_responses}}
{{/if_basic_global_response}}

{{#if isArray}}
void CHIP{{asUpperCamelCase parent.name}}{{asUpperCamelCase name}}AttributeCallback::CallbackFn(void * context, {{zapTypeToDecodableClusterObjectType type ns=parent.name isArgument=true}} list)
Expand Down Expand Up @@ -142,7 +142,7 @@ void CHIP{{asUpperCamelCase parent.name}}{{asUpperCamelCase name}}AttributeCallb
env->CallVoidMethod(javaCallbackRef, javaMethod, arrayListObj);
}
{{else}}
{{#if_in_global_responses}}
{{#if_basic_global_response}}
{{else}}
void CHIP{{asUpperCamelCase parent.name}}{{asUpperCamelCase name}}AttributeCallback::CallbackFn(void * context, {{zapTypeToDecodableClusterObjectType type ns=parent.name isArgument=true}} value)
{
Expand All @@ -166,7 +166,7 @@ void CHIP{{asUpperCamelCase parent.name}}{{asUpperCamelCase name}}AttributeCallb

env->CallVoidMethod(javaCallbackRef, javaMethod, javaValue);
}
{{/if_in_global_responses}}
{{/if_basic_global_response}}
{{/if}}
{{/unless}}
{{/chip_server_cluster_attributes}}
Expand Down
4 changes: 2 additions & 2 deletions src/controller/java/templates/CHIPReadCallbacks.zapt
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ private:
{{#chip_client_clusters}}
{{#chip_server_cluster_attributes}}
{{#unless (isStrEqual chipCallback.name "Unsupported")}}
{{#if_in_global_responses}}
{{#if_basic_global_response}}
{{else}}
class CHIP{{asUpperCamelCase parent.name}}{{asUpperCamelCase name}}AttributeCallback : public chip::Callback::Callback<CHIP{{asUpperCamelCase parent.name}}Cluster{{asUpperCamelCase name}}AttributeCallbackType>
{
Expand All @@ -65,7 +65,7 @@ private:
jobject javaCallbackRef;
bool keepAlive;
};
{{/if_in_global_responses}}
{{/if_basic_global_response}}

{{/unless}}
{{/chip_server_cluster_attributes}}
Expand Down
20 changes: 10 additions & 10 deletions src/controller/java/templates/ChipClusters-java.zapt
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ public class ChipClusters {

{{#chip_server_cluster_attributes}}
{{#unless (isStrEqual chipCallback.name "Unsupported")}}
{{#if_in_global_responses}}
{{#if_basic_global_response}}
{{else}}
{{! NOTE: asJavaType ends up sniffing for isArray on the context. Since we want the type of our _entry_, force isArray to
false. }}
Expand All @@ -142,19 +142,19 @@ public class ChipClusters {
default void onSubscriptionEstablished() {}
}
{{/if}}
{{/if_in_global_responses}}
{{/if_basic_global_response}}
{{/unless}}
{{/chip_server_cluster_attributes}}
{{#chip_server_cluster_attributes}}
{{! TODO: Add support for struct-typed attributes }}
{{#unless (isStrEqual chipCallback.name "Unsupported")}}

public void read{{asUpperCamelCase name}}Attribute(
{{#if_in_global_responses}}
{{#if_basic_global_response}}
{{convertAttributeCallbackTypeToJavaName chipCallback.type}}AttributeCallback callback
{{else}}
{{asUpperCamelCase name}}AttributeCallback callback
{{/if_in_global_responses}}
{{/if_basic_global_response}}
) {
read{{asUpperCamelCase name}}Attribute(chipClusterPtr, callback);
}
Expand All @@ -171,11 +171,11 @@ public class ChipClusters {
{{/if}}
{{#if isReportableAttribute}}
public void subscribe{{asCamelCased name false}}Attribute(
{{#if_in_global_responses}}
{{#if_basic_global_response}}
{{convertAttributeCallbackTypeToJavaName chipCallback.type}}AttributeCallback callback
{{else}}
{{asUpperCamelCase name}}AttributeCallback callback
{{/if_in_global_responses}},
{{/if_basic_global_response}},
int minInterval, int maxInterval) {
subscribe{{asCamelCased name false}}Attribute(chipClusterPtr, callback, minInterval, maxInterval);
}
Expand All @@ -187,23 +187,23 @@ public class ChipClusters {
{{#unless (isStrEqual chipCallback.name "Unsupported")}}

private native void read{{asUpperCamelCase name}}Attribute(long chipClusterPtr,
{{#if_in_global_responses}}
{{#if_basic_global_response}}
{{convertAttributeCallbackTypeToJavaName chipCallback.type}}AttributeCallback callback
{{else}}
{{asUpperCamelCase name}}AttributeCallback callback
{{/if_in_global_responses}}
{{/if_basic_global_response}}
);
{{#if isWritableAttribute}}

private native void write{{asUpperCamelCase name}}Attribute(long chipClusterPtr, DefaultClusterCallback callback, {{asJavaType type chipType parent.name}} value, @Nullable Integer timedWriteTimeoutMs);
{{/if}}
{{#if isReportableAttribute}}
private native void subscribe{{asCamelCased name false}}Attribute(long chipClusterPtr,
{{#if_in_global_responses}}
{{#if_basic_global_response}}
{{convertAttributeCallbackTypeToJavaName chipCallback.type}}AttributeCallback callback
{{else}}
{{asUpperCamelCase name}}AttributeCallback callback
{{/if_in_global_responses}}, int minInterval, int maxInterval);
{{/if_basic_global_response}}, int minInterval, int maxInterval);
{{/if}}
{{/unless}}
{{/chip_server_cluster_attributes}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@ public class ClusterReadMapping {
InteractionInfo read{{asUpperCamelCase ../name}}{{asUpperCamelCase name}}AttributeInteractionInfo = new InteractionInfo(
(cluster, callback, commandArguments) -> {
((ChipClusters.{{asUpperCamelCase ../name}}Cluster) cluster).read{{asUpperCamelCase name}}Attribute(
({{#if_in_global_responses}}
({{#if_basic_global_response}}
ChipClusters.{{convertAttributeCallbackTypeToJavaName chipCallback.type}}AttributeCallback
{{else}}
ChipClusters.{{asUpperCamelCase ../name}}Cluster.{{asUpperCamelCase name}}AttributeCallback
{{/if_in_global_responses}}) callback
{{/if_basic_global_response}}) callback
);
},
{{#if isArray}}
Expand Down

0 comments on commit dc99fb3

Please sign in to comment.