Skip to content

Commit

Permalink
[Darwin] Use InvokeCommand instead of CHIPClusters.cpp for commands (#…
Browse files Browse the repository at this point in the history
…11628)

* Use InvokeCommand instead of CHIPClusters.cpp for commands

* Update gen code
  • Loading branch information
vivien-apple authored and pull[bot] committed Jan 7, 2022
1 parent e5c09e4 commit 4b7cd59
Show file tree
Hide file tree
Showing 11 changed files with 2,399 additions and 914 deletions.
4 changes: 4 additions & 0 deletions src/darwin/Framework/CHIP.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
1ED276E026C57CF000547A89 /* CHIPCallbackBridge.mm in Sources */ = {isa = PBXBuildFile; fileRef = 1ED276DF26C57CF000547A89 /* CHIPCallbackBridge.mm */; };
1ED276E226C5812A00547A89 /* CHIPCluster.mm in Sources */ = {isa = PBXBuildFile; fileRef = 1ED276E126C5812A00547A89 /* CHIPCluster.mm */; };
1ED276E426C5832500547A89 /* CHIPCluster.h in Headers */ = {isa = PBXBuildFile; fileRef = 1ED276E326C5832500547A89 /* CHIPCluster.h */; settings = {ATTRIBUTES = (Public, ); }; };
1EF900A0273AC39C006A4018 /* CHIPClustersInvoke.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 1EF9009E273AC39C006A4018 /* CHIPClustersInvoke.cpp */; };
2C1B027A2641DB4E00780EF1 /* CHIPOperationalCredentialsDelegate.mm in Sources */ = {isa = PBXBuildFile; fileRef = 2C1B02782641DB4E00780EF1 /* CHIPOperationalCredentialsDelegate.mm */; };
2C1B027B2641DB4E00780EF1 /* CHIPOperationalCredentialsDelegate.h in Headers */ = {isa = PBXBuildFile; fileRef = 2C1B02792641DB4E00780EF1 /* CHIPOperationalCredentialsDelegate.h */; };
2C222AD0255C620600E446B9 /* CHIPDevice.h in Headers */ = {isa = PBXBuildFile; fileRef = 2C222ACE255C620600E446B9 /* CHIPDevice.h */; settings = {ATTRIBUTES = (Public, ); }; };
Expand Down Expand Up @@ -124,6 +125,7 @@
1ED276DF26C57CF000547A89 /* CHIPCallbackBridge.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; name = CHIPCallbackBridge.mm; path = "zap-generated/CHIPCallbackBridge.mm"; sourceTree = "<group>"; };
1ED276E126C5812A00547A89 /* CHIPCluster.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; path = CHIPCluster.mm; sourceTree = "<group>"; };
1ED276E326C5832500547A89 /* CHIPCluster.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = CHIPCluster.h; sourceTree = "<group>"; };
1EF9009E273AC39C006A4018 /* CHIPClustersInvoke.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = CHIPClustersInvoke.cpp; path = "../../../../zzz_generated/controller-clusters/zap-generated/CHIPClustersInvoke.cpp"; sourceTree = "<group>"; };
2C1B02782641DB4E00780EF1 /* CHIPOperationalCredentialsDelegate.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; path = CHIPOperationalCredentialsDelegate.mm; sourceTree = "<group>"; };
2C1B02792641DB4E00780EF1 /* CHIPOperationalCredentialsDelegate.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = CHIPOperationalCredentialsDelegate.h; sourceTree = "<group>"; };
2C222ACE255C620600E446B9 /* CHIPDevice.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = CHIPDevice.h; sourceTree = "<group>"; };
Expand Down Expand Up @@ -222,6 +224,7 @@
1EC4CE5825CC26AB00D7304F /* CHIPGeneratedFiles */ = {
isa = PBXGroup;
children = (
1EF9009E273AC39C006A4018 /* CHIPClustersInvoke.cpp */,
1EC3238C271999E2002A8BF0 /* cluster-objects.cpp */,
1E16A8F926B9835600683C53 /* CHIPTestClustersObjc.h */,
1E16A8FA26B9835700683C53 /* CHIPTestClustersObjc.mm */,
Expand Down Expand Up @@ -517,6 +520,7 @@
B2E0D7B6245B0B5C003C5B48 /* CHIPManualSetupPayloadParser.mm in Sources */,
1E85732826551A490050A4D9 /* attribute-table.cpp in Sources */,
1E85732626551A490050A4D9 /* attribute-storage.cpp in Sources */,
1EF900A0273AC39C006A4018 /* CHIPClustersInvoke.cpp in Sources */,
1E85732C26551A490050A4D9 /* DataModelHandler.cpp in Sources */,
1E85733126551A490050A4D9 /* chip-message-send.cpp in Sources */,
1E85730F265519AE0050A4D9 /* attribute-size.cpp in Sources */,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#import "zap-generated/CHIPClientCallbacks.h"
#import "zap-generated/CHIPClustersObjc.h"

#include <app/data-model/NullObject.h>
#include <platform/CHIPDeviceLayer.h>

typedef CHIP_ERROR (^CHIPActionBlock)(chip::Callback::Cancelable * success, chip::Callback::Cancelable * failure);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,15 @@
#include <app/data-model/DecodableList.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}}

{{#>CHIPCallbackBridge header="1" partial-type="" }}DefaultSuccessCallback{{/CHIPCallbackBridge}}
{{#>CHIPCallbackBridge header="1" partial-type="Octet_String"}}OctetStringAttributeCallback{{/CHIPCallbackBridge}}
{{#>CHIPCallbackBridge header="1" partial-type="Char_String" }}CharStringAttributeCallback{{/CHIPCallbackBridge}}
Expand Down
20 changes: 7 additions & 13 deletions src/darwin/Framework/CHIP/templates/CHIPClustersObjc-src.zapt
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,11 @@
#import "CHIPDevice.h"
#import "CHIPDevice_Internal.h"

using chip::Callback::Callback;
using chip::Callback::Cancelable;
using namespace chip::app::Clusters;

{{#chip_client_clusters}}

@implementation CHIP{{asUpperCamelCase name}}

- (chip::Controller::ClusterBase *)getCluster
Expand All @@ -22,25 +23,18 @@ using chip::Callback::Cancelable;

{{#chip_cluster_commands}}
{{#*inline "callbackName"}}{{#if hasSpecificResponse}}{{asUpperCamelCase parent.name}}Cluster{{asUpperCamelCase responseName}}{{else}}DefaultSuccess{{/if}}{{/inline}}
{{#*inline "callbackParams"}}{{#chip_cluster_command_arguments_with_structs_expanded}},
{{#if_chip_enum type}}
static_cast<{{chipType}}>({{asLowerCamelCase label}})
{{else if (isOctetString type)}}
[self asByteSpan:{{asLowerCamelCase label}}]
{{else if (isCharString type)}}
[self asCharSpan:{{asLowerCamelCase label}}]
{{else}}
{{asLowerCamelCase label}}
{{/if_chip_enum}}
{{/chip_cluster_command_arguments_with_structs_expanded}}{{/inline}}
{{#if (zcl_command_arguments_count this.id)}}
- (void){{asLowerCamelCase name}}:{{#chip_cluster_command_arguments_with_structs_expanded}}{{#not_first}}{{asLowerCamelCase label}}:{{/not_first}}({{asObjectiveCBasicType type}}){{asLowerCamelCase label}} {{/chip_cluster_command_arguments_with_structs_expanded}}responseHandler:(ResponseHandler)responseHandler
{{else}}
- (void){{asLowerCamelCase name}}:(ResponseHandler)responseHandler
{{/if}}
{
{{>encode_command}}

new CHIP{{>callbackName}}CallbackBridge(self.callbackQueue, responseHandler, ^(Cancelable * success, Cancelable * failure) {
return self.cppCluster.{{asUpperCamelCase name}}(success, failure{{>callbackParams}});
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);
});
}
{{/chip_cluster_commands}}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
{{#if header}}
class CHIP{{> @partial-block}}Bridge : public CHIPCallbackBridge<{{> @partial-block}}>
{{#*inline "callbackType"}}{{#if (isStrEqual partial-type "Command")}}CHIP{{> @partial-block}}Type{{else}}{{> @partial-block}}{{/if}}{{/inline}}
class CHIP{{> @partial-block}}Bridge : public CHIPCallbackBridge<{{>callbackType}}>
{
public:
CHIP{{> @partial-block}}Bridge(dispatch_queue_t queue, ResponseHandler handler, CHIPActionBlock action, bool keepAlive = false)
: CHIPCallbackBridge<{{> @partial-block}}>(queue, handler, action, OnSuccessFn, keepAlive)
: CHIPCallbackBridge<{{>callbackType}}>(queue, handler, action, OnSuccessFn, keepAlive)
{};

static void OnSuccessFn(void * context
{{#if (isStrEqual partial-type "Command")}}
{{#chip_cluster_response_arguments}}, {{asUnderlyingZclType type}} {{asSymbol label}}{{/chip_cluster_response_arguments}}
, const chip::app::Clusters::{{asUpperCamelCase parent.name}}::Commands::{{asUpperCamelCase name}}::DecodableType & data
{{else if (isStrEqual partial-type "List")}}
, {{zapTypeToDecodableClusterObjectType type ns=parent.name isArgument=true}} list
{{else if partial-type}}
Expand All @@ -20,7 +21,7 @@ public:
{{else}}
void CHIP{{> @partial-block}}Bridge::OnSuccessFn(void * context
{{#if (isStrEqual partial-type "Command")}}
{{#chip_cluster_response_arguments}}, {{asUnderlyingZclType type}} {{asSymbol label}}{{/chip_cluster_response_arguments}}
, const chip::app::Clusters::{{asUpperCamelCase parent.name}}::Commands::{{asUpperCamelCase name}}::DecodableType & data
{{else if (isStrEqual partial-type "List")}}
, {{zapTypeToDecodableClusterObjectType type ns=parent.name isArgument=true}} list
{{else if partial-type}}
Expand All @@ -30,20 +31,8 @@ void CHIP{{> @partial-block}}Bridge::OnSuccessFn(void * context
)
{
{{#if (isStrEqual partial-type "Command")}}
DispatchSuccess(context, @{
{{#chip_cluster_response_arguments}}
{{#if isArray}}
// {{asSymbol label}} : {{asUnderlyingZclType type}}
// Conversion from this type to Objc is not properly implemented yet
{{else if (isOctetString type)}}
@"{{asSymbol label}}" : [NSData dataWithBytes:{{asSymbol label}}.data() length:{{asSymbol label}}.size()],
{{else if (isCharString type)}}
@"{{asSymbol label}}" : [[NSString alloc] initWithBytes:{{asSymbol label}}.data() length:{{asSymbol label}}.size() encoding:NSUTF8StringEncoding],
{{else}}
@"{{asSymbol label}}" : [NSNumber numberWith{{asObjectiveCNumberType label type false}}:{{asSymbol label}}],
{{/if}}
{{/chip_cluster_response_arguments}}
});
{{>decode_response}}
DispatchSuccess(context, response);
{{else if (isStrEqual partial-type "List")}}
id array = [[NSMutableArray alloc] init];
auto iter = list.begin();
Expand Down
15 changes: 15 additions & 0 deletions src/darwin/Framework/CHIP/templates/partials/decode_response.zapt
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
id response = @{
{{#chip_cluster_response_arguments}}
@"{{asSymbol label}}": {{#if isOptional}}data.{{asLowerCamelCase label}}.HasValue() == false ? [NSNull null] : {{#if isNullable}}data.{{asLowerCamelCase label}}.Value().IsNull() ? [NSNull null] : {{/if}}{{/if}}
{{~#*inline "item"}}data.{{asLowerCamelCase name}}{{#if isOptional}}.Value(){{/if}}{{#if isNullable}}.Value(){{/if}}{{/inline}}
{{#if isArray}}
[NSNull null], /* Array - Conversion from this type to Objc is not properly implemented yet */
{{else if (isOctetString type)}}
[NSData dataWithBytes:{{>item}}.data() length:{{>item}}.size()],
{{else if (isCharString type)}}
[[NSString alloc] initWithBytes:{{>item}}.data() length:{{>item}}.size() encoding:NSUTF8StringEncoding],
{{else}}
[NSNumber numberWith{{asObjectiveCNumberType label type false}}:{{>item}}],
{{/if}}
{{/chip_cluster_response_arguments}}
};
24 changes: 24 additions & 0 deletions src/darwin/Framework/CHIP/templates/partials/encode_command.zapt
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{{asUpperCamelCase parent.name}}::Commands::{{asUpperCamelCase name}}::Type request;
{{#chip_cluster_command_arguments}}
{{#*inline "value"}}
{{! TODO Implement complex types parsing in order to properly set the request parameters }}
{{#if isArray}}
{{zapTypeToEncodableClusterObjectType type ns=parent.parent.name}}();
{{else if isStruct}}
{{zapTypeToEncodableClusterObjectType type ns=parent.parent.name}}();
{{else if (isOctetString type)}}
[self asByteSpan:{{asLowerCamelCase label}}]
{{else if (isCharString type)}}
[self asCharSpan:{{asLowerCamelCase label}}]
{{else if isBitmap}}
static_cast<decltype(request.{{asLowerCamelCase label}})>({{asLowerCamelCase label}})
{{else}}
{{#if_chip_enum type}}
static_cast<decltype(request.{{asLowerCamelCase label}})>({{asLowerCamelCase label}})
{{else}}
{{asLowerCamelCase label}}
{{/if_chip_enum}}
{{/if}}
{{/inline}}
request.{{asLowerCamelCase label}} = {{#if isOptional}}{{zapTypeToEncodableClusterObjectType type ns=parent.parent.name}}({{/if}}{{#if isNullable}}chip::app::DataModel::Nullable<{{chipType}}>({{/if}}{{>value}}{{#if isNullable}}){{/if}}{{#if isOptional}}){{/if}};
{{/chip_cluster_command_arguments}}
8 changes: 8 additions & 0 deletions src/darwin/Framework/CHIP/templates/templates.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,14 @@
{
"name": "CHIPCallbackBridge",
"path": "partials/CHIPCallbackBridge.zapt"
},
{
"name": "encode_command",
"path": "partials/encode_command.zapt"
},
{
"name": "decode_response",
"path": "partials/decode_response.zapt"
}
],
"templates": [
Expand Down
Loading

0 comments on commit 4b7cd59

Please sign in to comment.