Skip to content

Commit

Permalink
Add new function signature for params (#17706)
Browse files Browse the repository at this point in the history
* Add new functions that will accept parameter class.

* Generated code.

* Update src/darwin/Framework/CHIP/CHIPCluster.h

Co-authored-by: Boris Zbarsky <[email protected]>

* Update src/darwin/Framework/CHIP/CHIPCluster.h

Co-authored-by: Boris Zbarsky <[email protected]>

* Update src/darwin/Framework/CHIP/templates/CHIPClustersObjc-src.zapt

Co-authored-by: Boris Zbarsky <[email protected]>

* Update src/darwin/Framework/CHIP/templates/CHIPCommandPayloadsObjc.zapt

Co-authored-by: Boris Zbarsky <[email protected]>

* Update src/darwin/Framework/CHIP/templates/CHIPClustersObjc-src.zapt

Co-authored-by: Boris Zbarsky <[email protected]>

* Update src/darwin/Framework/CHIP/templates/CHIPClustersObjc-src.zapt

Co-authored-by: Boris Zbarsky <[email protected]>

* Update src/darwin/Framework/CHIP/templates/CHIPClustersObjc-src.zapt

Co-authored-by: Boris Zbarsky <[email protected]>

* Generated code.

* Restyled by whitespace

* Updated NOLINT position and fix comment.

* Regenerated code.

Co-authored-by: Boris Zbarsky <[email protected]>
Co-authored-by: Restyled.io <[email protected]>
  • Loading branch information
3 people authored May 3, 2022
1 parent 4d50b10 commit 91513eb
Show file tree
Hide file tree
Showing 11 changed files with 12,183 additions and 937 deletions.
27 changes: 27 additions & 0 deletions src/darwin/Framework/CHIP/CHIPCluster.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,33 @@ NS_ASSUME_NONNULL_BEGIN
+ (instancetype)new NS_UNAVAILABLE;
@end

/**
* CHIPWriteParams
* This is used to control the behavior of cluster writes.
* If not provided (i.e. nil passed for the CHIPWriteParams argument), will be
* treated as if a default-initialized object was passed in.
*/
@interface CHIPWriteParams : NSObject

/**
* Controls whether the write is a timed write.
*
* If nil (the default value), a regular write is done for attributes that do
* not require a timed write and a timed write with some default timed request
* timeout is done for attributes that require a timed write.
*
* If not nil, a timed write is done, with the provided value used as the timed
* request timeout. The value should be chosen small enough to provide the
* desired security properties but large enough that it will allow a round-trip
* from the sever to the client (for the status response and actual write
* request) within the timeout window.
*
*/
@property (strong, nonatomic, nullable) NSNumber * timedWriteTimeoutMs;

- (instancetype)init;
@end

/**
* CHIPReadParams
* This is used to control the behavior of attribute reads and subscribes.
Expand Down
11 changes: 11 additions & 0 deletions src/darwin/Framework/CHIP/CHIPCluster.mm
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,17 @@ - (instancetype)initWithDevice:(CHIPDevice *)device endpoint:(EndpointId)endpoin
}
@end

@implementation CHIPWriteParams
- (instancetype)init
{
if (self = [super init]) {
_timedWriteTimeoutMs = nil;
}
return self;
}

@end

@implementation CHIPReadParams
- (instancetype)init
{
Expand Down
53 changes: 39 additions & 14 deletions src/darwin/Framework/CHIP/templates/CHIPClustersObjc-src.zapt
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
using chip::Callback::Callback;
using chip::Callback::Cancelable;
using namespace chip::app::Clusters;

// NOLINTBEGIN(clang-analyzer-cplusplus.NewDeleteLeaks): Linter is unable to locate the delete on these objects.
{{#chip_client_clusters}}
@implementation CHIP{{asUpperCamelCase name}}

Expand All @@ -29,10 +29,27 @@ using namespace chip::app::Clusters;

{{#chip_cluster_commands}}
{{#*inline "callbackName"}}{{#if hasSpecificResponse}}{{asUpperCamelCase parent.name}}Cluster{{asUpperCamelCase responseName}}{{else}}CommandSuccess{{/if}}{{/inline}}
- (void){{asLowerCamelCase name}}With{{#if (hasArguments)}}Params:(CHIP{{asUpperCamelCase parent.name}}Cluster{{asUpperCamelCase name}}Params * {{#unless (commandHasRequiredField .)}}_Nullable{{/unless}})params completionHandler{{else}}CompletionHandler{{/if}}:({{>command_completion_type command=.}})completionHandler
{{#unless (hasArguments)}}
- (void){{asLowerCamelCase name}}WithCompletionHandler:({{>command_completion_type command=.}})completionHandler
{
[self {{asLowerCamelCase name}}WithParams:nil completionHandler:completionHandler];
}
{{/unless}}
- (void){{asLowerCamelCase name}}WithParams: (CHIP{{asUpperCamelCase parent.name}}Cluster{{asUpperCamelCase name}}Params * {{#unless (commandHasRequiredField .)}}_Nullable{{/unless}})params completionHandler:({{>command_completion_type command=.}})completionHandler
{
chip::Optional<uint16_t> timedInvokeTimeoutMs;
ListFreer listFreer;
{{asUpperCamelCase parent.name}}::Commands::{{asUpperCamelCase name}}::Type request;
if (params != nil) {
if (params.timedInvokeTimeoutMs != nil) {
timedInvokeTimeoutMs.SetValue(params.timedInvokeTimeoutMs.unsignedShortValue);
}
}
{{#if mustUseTimedInvoke}}
if (!timedInvokeTimeoutMs.HasValue()) {
timedInvokeTimeoutMs.SetValue(10000);
}
{{/if}}
{{#chip_cluster_command_arguments}}
{{#first}}
{{#unless (commandHasRequiredField parent)}}
Expand Down Expand Up @@ -63,12 +80,7 @@ using namespace chip::app::Clusters;
^(Cancelable * success, Cancelable * failure) {
auto successFn = Callback<CHIP{{>callbackName}}CallbackType>::FromCancelable(success);
auto failureFn = Callback<CHIPDefaultFailureCallbackType>::FromCancelable(failure);
return self.cppCluster.InvokeCommand(request, successFn->mContext, successFn->mCall, failureFn->mCall
{{#if mustUseTimedInvoke}}
{{!TODO Fix Darwin API to pass in this information. For now, 10 seconds.}}
, 10000
{{/if}}
);
return self.cppCluster.InvokeCommand(request, successFn->mContext, successFn->mCall, failureFn->mCall, timedInvokeTimeoutMs);
});
}
{{/chip_cluster_commands}}
Expand Down Expand Up @@ -103,6 +115,22 @@ using namespace chip::app::Clusters;
{{#*inline "callbackName"}}DefaultSuccess{{/inline}}
- (void)write{{>attribute}}WithValue:({{asObjectiveCType type parent.name}})value completionHandler:(StatusCompletion)completionHandler
{
[self write{{>attribute}}WithValue:({{asObjectiveCType type parent.name}})value params:nil completionHandler:completionHandler];
}
- (void)write{{>attribute}}WithValue:({{asObjectiveCType type parent.name}})value params:(CHIPWriteParams * _Nullable)params completionHandler:(StatusCompletion)completionHandler
{
chip::Optional<uint16_t> timedWriteTimeoutMs;
if (params != nil) {
if (params.timedWriteTimeoutMs != nil){
timedWriteTimeoutMs.SetValue(params.timedWriteTimeoutMs.unsignedShortValue);
}
}
{{#if mustUseTimedInvoke}}
if (!timedWriteTimeoutMs.HasValue()) {
timedWriteTimeoutMs.SetValue(10000);
}
{{/if}}

new CHIP{{>callbackName}}CallbackBridge(self.callbackQueue,
{{! For now, don't change the bridge API; instead just use an adapter
to invoke our completion handler. This is not great from a
Expand All @@ -117,13 +145,9 @@ using namespace chip::app::Clusters;
{{>encode_value target="cppValue" source="value" cluster=parent.name errorCode="return CHIP_ERROR_INVALID_ARGUMENT;" depth=0}}
auto successFn = Callback<CHIP{{>callbackName}}CallbackType>::FromCancelable(success);
auto failureFn = Callback<CHIPDefaultFailureCallbackType>::FromCancelable(failure);
return self.cppCluster.WriteAttribute<TypeInfo>(cppValue, successFn->mContext, successFn->mCall, failureFn->mCall
{{#if mustUseTimedWrite}}
{{!TODO Fix Darwin API to pass in this information. For now, 10 seconds.}}
, 10000
{{/if}}
);
return self.cppCluster.WriteAttribute<TypeInfo>(cppValue, successFn->mContext, successFn->mCall, failureFn->mCall, timedWriteTimeoutMs);
});

}

{{/if}}
Expand Down Expand Up @@ -181,4 +205,5 @@ subscriptionEstablished:(SubscriptionEstablishedHandler _Nullable)subscriptionEs
@end

{{/chip_client_clusters}}
// NOLINTEND(clang-analyzer-cplusplus.NewDeleteLeaks)
{{/if}}
6 changes: 5 additions & 1 deletion src/darwin/Framework/CHIP/templates/CHIPClustersObjc.zapt
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,10 @@ NS_ASSUME_NONNULL_BEGIN
@interface CHIP{{asUpperCamelCase name}} : CHIPCluster

{{#chip_cluster_commands}}
- (void){{asLowerCamelCase name}}With{{#if (hasArguments)}}Params:(CHIP{{asUpperCamelCase parent.name}}Cluster{{asUpperCamelCase name}}Params * {{#unless (commandHasRequiredField .)}}_Nullable{{/unless}})params completionHandler{{else}}CompletionHandler{{/if}}:({{>command_completion_type command=.}})completionHandler;
- (void){{asLowerCamelCase name}}WithParams:(CHIP{{asUpperCamelCase parent.name}}Cluster{{asUpperCamelCase name}}Params * {{#unless (commandHasRequiredField .)}}_Nullable{{/unless}})params completionHandler:({{>command_completion_type command=.}})completionHandler;
{{#unless (hasArguments)}}
- (void){{asLowerCamelCase name}}WithCompletionHandler:({{>command_completion_type command=.}})completionHandler;
{{/unless}}
{{/chip_cluster_commands}}

{{#chip_server_cluster_attributes}}
Expand All @@ -41,6 +44,7 @@ NS_ASSUME_NONNULL_BEGIN
(void (^)({{asObjectiveCClass type parent.name}} * _Nullable value, NSError * _Nullable error))completionHandler;
{{#if isWritableAttribute}}
- (void)write{{>attribute}}WithValue:({{asObjectiveCType type parent.name}})value completionHandler:(StatusCompletion)completionHandler;
- (void)write{{>attribute}}WithValue:({{asObjectiveCType type parent.name}})value params:(CHIPWriteParams * _Nullable)params completionHandler:(StatusCompletion)completionHandler;
{{/if}}
{{#if isReportableAttribute}}
/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,19 @@ NS_ASSUME_NONNULL_BEGIN

{{#zcl_clusters}}
{{#zcl_commands}}
{{#zcl_command_arguments}}
{{#first}}
@implementation CHIP{{asUpperCamelCase parent.parent.name}}Cluster{{asUpperCamelCase parent.name}}Params
@implementation CHIP{{asUpperCamelCase parent.name}}Cluster{{asUpperCamelCase name}}Params
- (instancetype)init
{
if (self = [super init]) {
{{/first}}
{{#zcl_command_arguments}}

{{>init_struct_member label=label type=type cluster=parent.parent.name}}
{{#last}}
{{/zcl_command_arguments}}
_timedInvokeTimeoutMs = nil;
}
return self;
}
@end
{{/last}}
{{/zcl_command_arguments}}

{{/zcl_commands}}
{{/zcl_clusters}}

Expand Down
26 changes: 19 additions & 7 deletions src/darwin/Framework/CHIP/templates/CHIPCommandPayloadsObjc.zapt
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,31 @@ NS_ASSUME_NONNULL_BEGIN

{{#zcl_clusters}}
{{#zcl_commands}}
@interface CHIP{{asUpperCamelCase parent.name}}Cluster{{asUpperCamelCase name}}Params : NSObject
{{#zcl_command_arguments}}
{{#first}}
@interface CHIP{{asUpperCamelCase parent.parent.name}}Cluster{{asUpperCamelCase parent.name}}Params : NSObject
{{/first}}

{{! Override the getter name because some of our properties start with things
like "new" or "init" }}
@property (strong, nonatomic{{#unless (isStrEqual (asGetterName label) (asStructPropertyName label))}}, getter={{asGetterName label}}{{/unless}}) {{asObjectiveCType type parent.parent.name}} {{asStructPropertyName label}};
{{#last}}
- (instancetype)init;
@end
{{/last}}
{{/zcl_command_arguments}}
/**
* Controls whether the command is a timed command (using Timed Invoke).
*
* If nil (the default value), a regular invoke is done for commands that do
* not require a timed invoke and a timed invoke with some default timed request
* timeout is done for commands that require a timed invoke.
*
* If not nil, a timed invoke is done, with the provided value used as the timed
* request timeout. The value should be chosen small enough to provide the
* desired security properties but large enough that it will allow a round-trip
* from the sever to the client (for the status response and actual invoke
* request) within the timeout window.
*
*/
@property (strong, nonatomic, nullable) NSNumber * timedInvokeTimeoutMs;

- (instancetype)init;
@end
{{/zcl_commands}}
{{/zcl_clusters}}

Expand Down
Loading

0 comments on commit 91513eb

Please sign in to comment.