forked from project-chip/connectedhomeip
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMTRCluster.mm
333 lines (268 loc) · 9.29 KB
/
MTRCluster.mm
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
/*
*
* Copyright (c) 2021-2023 Project CHIP Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#import "MTRBaseDevice.h"
#import "MTRCluster_Internal.h"
#import "NSDataSpanConversion.h"
#import "NSStringSpanConversion.h"
using namespace ::chip;
@implementation MTRCluster
- (instancetype)initWithEndpointID:(NSNumber *)endpointID queue:(dispatch_queue_t)queue;
{
if (self = [super init]) {
// TODO consider range-checking the incoming number to make sure it's
// actually in the EndpointId range
_endpointID = endpointID;
_callbackQueue = queue;
}
return self;
}
@end
@implementation MTRGenericBaseCluster
- (instancetype)initWithDevice:(MTRBaseDevice *)device endpointID:(NSNumber *)endpointID queue:(dispatch_queue_t)queue
{
if (self = [super initWithEndpointID:endpointID queue:queue]) {
_device = device;
}
return self;
}
@end
@implementation MTRGenericCluster
- (instancetype)initWithDevice:(MTRDevice *)device endpointID:(NSNumber *)endpointID queue:(dispatch_queue_t)queue
{
if (self = [super initWithEndpointID:endpointID queue:queue]) {
_device = device;
}
return self;
}
@end
@implementation MTRWriteParams
- (instancetype)init
{
if (self = [super init]) {
_timedWriteTimeout = nil;
}
return self;
}
#pragma mark - Copying
- (id)copyWithZone:(NSZone * _Nullable)zone
{
auto other = [[MTRWriteParams alloc] init];
other.timedWriteTimeout = self.timedWriteTimeout;
other.dataVersion = self.dataVersion;
return other;
}
#pragma mark - Coding
+ (BOOL)supportsSecureCoding
{
return YES;
}
static NSString * sTimedWriteTimeoutCodingKey = @"sTimedWriteTimeoutKey";
static NSString * sDataVersionCodingKey = @"sDataVersionKey";
- (nullable instancetype)initWithCoder:(NSCoder *)decoder
{
self = [super init];
if (self == nil) {
return nil;
}
self.timedWriteTimeout = [decoder decodeObjectOfClass:[NSNumber class] forKey:sTimedWriteTimeoutCodingKey];
self.dataVersion = [decoder decodeObjectOfClass:[NSNumber class] forKey:sDataVersionCodingKey];
return self;
}
- (void)encodeWithCoder:(NSCoder *)coder
{
if (self.timedWriteTimeout)
[coder encodeObject:self.timedWriteTimeout forKey:sTimedWriteTimeoutCodingKey];
if (self.dataVersion)
[coder encodeObject:self.dataVersion forKey:sDataVersionCodingKey];
}
@end
@implementation MTRReadParams
- (instancetype)init
{
if (self = [super init]) {
_filterByFabric = YES;
_assumeUnknownAttributesReportable = YES;
}
return self;
}
#pragma mark - Coding
+ (BOOL)supportsSecureCoding
{
return YES;
}
static NSString * sFilterByFabricCoderKey = @"sFilterByFabricKey";
static NSString * sMinEventNumberCoderKey = @"sMinEventNumberKey";
static NSString * sAssumeUnknownAttributesReportableCoderKey = @"sAssumeUnknownAttributesReportableKey";
- (nullable instancetype)initWithCoder:(NSCoder *)decoder
{
self = [super init];
if (self == nil) {
return nil;
}
self.filterByFabric = [decoder decodeBoolForKey:sFilterByFabricCoderKey];
self.assumeUnknownAttributesReportable = [decoder decodeBoolForKey:sAssumeUnknownAttributesReportableCoderKey];
self.minEventNumber = [decoder decodeObjectOfClass:[NSNumber class] forKey:sMinEventNumberCoderKey];
return self;
}
- (void)encodeWithCoder:(NSCoder *)coder
{
[coder encodeBool:self.filterByFabric forKey:sFilterByFabricCoderKey];
[coder encodeBool:self.assumeUnknownAttributesReportable forKey:sAssumeUnknownAttributesReportableCoderKey];
if (self.minEventNumber)
[coder encodeObject:self.minEventNumber forKey:sMinEventNumberCoderKey];
}
#pragma mark - Copying
- (id)copyWithZone:(NSZone * _Nullable)zone
{
auto other = [[MTRReadParams alloc] init];
other.filterByFabric = self.filterByFabric;
other.minEventNumber = self.minEventNumber;
other.assumeUnknownAttributesReportable = self.assumeUnknownAttributesReportable;
return other;
}
#pragma mark - Other
- (void)toReadPrepareParams:(chip::app::ReadPrepareParams &)readPrepareParams
{
readPrepareParams.mIsFabricFiltered = self.filterByFabric;
if (self.minEventNumber) {
readPrepareParams.mEventNumber.SetValue(static_cast<chip::EventNumber>([self.minEventNumber unsignedLongLongValue]));
}
}
@end
@implementation MTRSubscribeParams
- (instancetype)initWithMinInterval:(NSNumber *)minInterval maxInterval:(NSNumber *)maxInterval
{
if (self = [super init]) {
_reportEventsUrgently = YES;
_replaceExistingSubscriptions = YES;
_resubscribeAutomatically = YES;
_minInterval = [minInterval copy];
_maxInterval = [maxInterval copy];
}
return self;
}
#pragma mark - Copying
- (id)copyWithZone:(NSZone * _Nullable)zone
{
auto other = [[MTRSubscribeParams alloc] initWithMinInterval:self.minInterval maxInterval:self.maxInterval];
other.filterByFabric = self.filterByFabric;
other.minEventNumber = self.minEventNumber;
other.assumeUnknownAttributesReportable = self.assumeUnknownAttributesReportable;
other.replaceExistingSubscriptions = self.replaceExistingSubscriptions;
other.reportEventsUrgently = self.reportEventsUrgently;
other.resubscribeAutomatically = self.resubscribeAutomatically;
other.minInterval = self.minInterval;
other.maxInterval = self.maxInterval;
return other;
}
#pragma mark - Coding
+ (BOOL)supportsSecureCoding
{
return YES;
}
static NSString * sReplaceExistingSubscriptionsCoderKey = @"sFilterByFabricKey";
static NSString * sReportEventsUrgentlyCoderKey = @"sMinEventNumberKey";
static NSString * sResubscribeAutomaticallyCoderKey = @"sAssumeUnknownAttributesReportableKey";
static NSString * sMinIntervalKeyCoderKey = @"sMinIntervalKeyKey";
static NSString * sMaxIntervalKeyCoderKey = @"sMaxIntervalKeyKey";
- (nullable instancetype)initWithCoder:(NSCoder *)decoder
{
self = [super initWithCoder:decoder];
if (self == nil) {
return nil;
}
self.replaceExistingSubscriptions = [decoder decodeBoolForKey:sReplaceExistingSubscriptionsCoderKey];
self.reportEventsUrgently = [decoder decodeBoolForKey:sReportEventsUrgentlyCoderKey];
self.resubscribeAutomatically = [decoder decodeBoolForKey:sResubscribeAutomaticallyCoderKey];
self.minInterval = [decoder decodeObjectOfClass:[NSNumber class] forKey:sMinIntervalKeyCoderKey];
self.maxInterval = [decoder decodeObjectOfClass:[NSNumber class] forKey:sMaxIntervalKeyCoderKey];
return self;
}
- (void)encodeWithCoder:(NSCoder *)coder
{
[super encodeWithCoder:coder];
[coder encodeBool:self.replaceExistingSubscriptions forKey:sReplaceExistingSubscriptionsCoderKey];
[coder encodeBool:self.reportEventsUrgently forKey:sReportEventsUrgentlyCoderKey];
[coder encodeBool:self.resubscribeAutomatically forKey:sResubscribeAutomaticallyCoderKey];
if (self.minInterval)
[coder encodeObject:self.minInterval forKey:sMinIntervalKeyCoderKey];
if (self.maxInterval)
[coder encodeObject:self.maxInterval forKey:sMaxIntervalKeyCoderKey];
}
#pragma mark - Main
- (void)toReadPrepareParams:(chip::app::ReadPrepareParams &)readPrepareParams
{
[super toReadPrepareParams:readPrepareParams];
readPrepareParams.mMinIntervalFloorSeconds = self.minInterval.unsignedShortValue;
readPrepareParams.mMaxIntervalCeilingSeconds = self.maxInterval.unsignedShortValue;
readPrepareParams.mKeepSubscriptions = !self.replaceExistingSubscriptions;
}
@end
@implementation MTRReadParams (Deprecated)
- (void)setFabricFiltered:(nullable NSNumber *)fabricFiltered
{
if (fabricFiltered == nil) {
self.filterByFabric = YES;
} else {
self.filterByFabric = [fabricFiltered boolValue];
}
}
- (nullable NSNumber *)fabricFiltered
{
return @(self.filterByFabric);
}
@end
@implementation MTRSubscribeParams (Deprecated)
- (instancetype)init
{
if (self = [super init]) {
_replaceExistingSubscriptions = YES;
_resubscribeAutomatically = YES;
_minInterval = @(1);
_maxInterval = @(0);
}
return self;
}
+ (instancetype)new
{
return [[self alloc] init];
}
- (void)setKeepPreviousSubscriptions:(nullable NSNumber *)keepPreviousSubscriptions
{
if (keepPreviousSubscriptions == nil) {
self.replaceExistingSubscriptions = YES;
} else {
self.replaceExistingSubscriptions = ![keepPreviousSubscriptions boolValue];
}
}
- (nullable NSNumber *)keepPreviousSubscriptions
{
return @(!self.replaceExistingSubscriptions);
}
- (void)setAutoResubscribe:(nullable NSNumber *)autoResubscribe
{
if (autoResubscribe == nil) {
self.resubscribeAutomatically = YES;
} else {
self.resubscribeAutomatically = [autoResubscribe boolValue];
}
}
- (nullable NSNumber *)autoResubscribe
{
return @(self.resubscribeAutomatically);
}
@end