-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
CHIPClustersObjc-src.zapt
174 lines (159 loc) · 7.4 KB
/
CHIPClustersObjc-src.zapt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
{{> header}}
{{#if (chip_has_client_clusters)}}
#import <Foundation/Foundation.h>
#import "CHIPCallbackBridge_internal.h"
#import "CHIPCluster_internal.h"
#import "CHIPClustersObjc_internal.h"
#import "CHIPDevice.h"
#import "CHIPDevice_Internal.h"
#import "CHIPStructsObjc.h"
#import "CHIPCommandPayloadsObjc.h"
#import "CHIPListUtils_internal.h"
#include <type_traits>
using chip::Callback::Callback;
using chip::Callback::Cancelable;
using namespace chip::app::Clusters;
{{#chip_client_clusters}}
@implementation CHIP{{asUpperCamelCase name}}
- (chip::Controller::ClusterBase *)getCluster
{
return &_cppCluster;
}
{{#chip_cluster_commands}}
{{#*inline "callbackName"}}{{#if hasSpecificResponse}}{{asUpperCamelCase parent.name}}Cluster{{asUpperCamelCase responseName}}{{else}}CommandSuccess{{/if}}{{/inline}}
{{! zcl_command_arguments_count is unreliable when used in a conditional. As
a workaround, just use iteration over the arguments directly to see if we
have any. }}
{{#chip_cluster_command_arguments}}
{{#first}}
- (void){{asLowerCamelCase parent.name}}WithParams:(CHIP{{asUpperCamelCase parent.parent.name}}Cluster{{asUpperCamelCase parent.name}}Params * {{#unless (commandHasRequiredField parent)}}_Nullable{{/unless}})params completionHandler:({{>command_completion_type command=parent}})completionHandler
{{/first}}
{{else}}
- (void){{asLowerCamelCase parent.name}}WithCompletionHandler:({{>command_completion_type command=parent}})completionHandler
{{/chip_cluster_command_arguments}}
{
ListFreer listFreer;
{{asUpperCamelCase parent.name}}::Commands::{{asUpperCamelCase name}}::Type request;
{{#chip_cluster_command_arguments}}
{{#first}}
{{#unless (commandHasRequiredField parent)}}
if (params != nil) {
{{/unless}}
{{/first}}
{{>encode_value target=(concat "request." (asLowerCamelCase label)) source=(concat "params." (asStructPropertyName label)) cluster=parent.parent.name errorCode="return;" depth=0}}
{{#last}}
{{#unless (commandHasRequiredField parent)}}
}
{{/unless}}
{{/last}}
{{/chip_cluster_command_arguments}}
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
type-safety perspective, of course. }}
{{#if hasSpecificResponse}}
^(NSError * _Nullable error, id _Nullable value) {
completionHandler(value, error);
},
{{else}}
^(NSError * _Nullable error, id _Nullable value) {
completionHandler(error);
},
{{/if}}
^(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}}
);
});
}
{{/chip_cluster_commands}}
{{#chip_server_cluster_attributes}}
{{! TODO: Need to add support for struct-type attibutes here, and enum-typed ones }}
{{#unless (isStrEqual chipCallback.name "Unsupported")}}
{{#*inline "attribute"}}Attribute{{asUpperCamelCase name}}{{/inline}}
{{#*inline "callbackName"}}
{{~#if isList~}}
{{asUpperCamelCase ../name}}{{asUpperCamelCase name}}List
{{~else~}}
{{~#if isNullable}}Nullable{{/if~}}
{{~#if (isStrEqual (asUpperCamelCase type) (asUpperCamelCase "vendor_id"))~}}
VendorId
{{~else~}}
{{chipCallback.name}}
{{~/if~}}
{{~/if~}}
Attribute
{{~/inline}}
- (void)read{{>attribute}}WithCompletionHandler:(void (^)({{asObjectiveCClass type parent.name}} * _Nullable value, NSError * _Nullable error))completionHandler
{
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
type-safety perspective, of course. }}
^(NSError * _Nullable error, id _Nullable value) {
completionHandler(value, error);
},
^(Cancelable * success, Cancelable * failure) {
using TypeInfo = {{asUpperCamelCase parent.name}}::Attributes::{{asUpperCamelCase name}}::TypeInfo;
auto successFn = Callback<{{>callbackName}}Callback>::FromCancelable(success);
auto failureFn = Callback<CHIPDefaultFailureCallbackType>::FromCancelable(failure);
return self.cppCluster.ReadAttribute<TypeInfo>(successFn->mContext, successFn->mCall, failureFn->mCall);
});
}
{{#if isWritableAttribute}}
{{#*inline "callbackName"}}DefaultSuccess{{/inline}}
- (void)write{{>attribute}}WithValue:({{asObjectiveCType type parent.name}})value completionHandler:(StatusCompletion)completionHandler
{
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
type-safety perspective, of course. }}
^(NSError * _Nullable error, id _Nullable ignored) {
completionHandler(error);
},
^(Cancelable * success, Cancelable * failure) {
ListFreer listFreer;
using TypeInfo = {{asUpperCamelCase parent.name}}::Attributes::{{asUpperCamelCase name}}::TypeInfo;
TypeInfo::Type cppValue;
{{>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}}
{{#if isReportableAttribute}}
{{#unless isList}}
{{#*inline "callbackName"}}DefaultSuccess{{/inline}}
{{#*inline "callbackParams"}}, minInterval, maxInterval{{#if isAnalog}}, change{{/if}}{{/inline}}
- (void) subscribe{{>attribute}}WithMinInterval:(uint16_t)minInterval maxInterval:(uint16_t)maxInterval responseHandler:(ResponseHandler)responseHandler
{
new CHIP{{>callbackName}}CallbackBridge(self.callbackQueue, responseHandler, ^(Cancelable * success, Cancelable * failure) {
return self.cppCluster.Subscribe{{>attribute}}(success, failure{{>callbackParams}});
});
}
{{#*inline "callbackName"}}{{#if isNullable}}Nullable{{/if}}{{chipCallback.name}}Attribute{{/inline}}
- (void) report{{>attribute}}WithResponseHandler:(void (^)({{asObjectiveCClass type parent.name}} * _Nullable value, NSError * _Nullable error))responseHandler {
new CHIP{{>callbackName}}CallbackBridge(self.callbackQueue,
{{! For now, don't change the bridge API; instead just use an adapter
to invoke our response handler. This is not great from a
type-safety perspective, of course. }}
^(NSError * _Nullable error, id _Nullable value) {
responseHandler(value, error);
},
^(Cancelable * success, Cancelable * failure) {
return self.cppCluster.Report{{>attribute}}(success);
}, true);
}
{{/unless}}
{{/if}}
{{/unless}}
{{/chip_server_cluster_attributes}}
@end
{{/chip_client_clusters}}
{{/if}}