forked from project-chip/connectedhomeip
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMTRBaseDevice_Internal.h
270 lines (234 loc) · 12.3 KB
/
MTRBaseDevice_Internal.h
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
/**
*
* Copyright (c) 2020 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 <Foundation/Foundation.h>
#import "MTRDefines_Internal.h"
#import "MTRDeviceController_Concrete.h"
#include <app/AttributePathParams.h>
#include <app/ConcreteAttributePath.h>
#include <app/ConcreteCommandPath.h>
#include <app/ConcreteEventPath.h>
#include <app/DeviceProxy.h>
#include <app/EventHeader.h>
#include <app/EventLoggingTypes.h>
#include <app/EventPathParams.h>
#include <lib/core/CHIPVendorIdentifiers.hpp>
#include <lib/core/TLVTags.h>
#include <system/SystemPacketBuffer.h>
@class MTRDeviceController;
// An AttestationResponse command needs to have an attestationChallenge
// to make sense of the results. Encode that with a profile-specific tag under
// the Apple vendor id. Let's select profile 0xFFFF just because, and use 0xFF
// for the actual tag number, so that if someone accidentally casts it to a
// uint8 (aka context tag) that will not collide with anything interesting.
inline constexpr chip::TLV::Tag kAttestationChallengeTag = chip::TLV::ProfileTag(chip::VendorId::Apple, 0xFFFF, 0xFF);
// We have no way to extract the tag value as a single thing, so just do it
// manually.
inline constexpr unsigned kProfileIdShift = 32;
inline constexpr uint64_t kAttestationChallengeTagProfile = chip::TLV::ProfileIdFromTag(kAttestationChallengeTag);
inline constexpr uint64_t kAttestationChallengeTagNumber = chip::TLV::TagNumFromTag(kAttestationChallengeTag);
inline constexpr uint64_t kAttestationChallengeTagValue = (kAttestationChallengeTagProfile << kProfileIdShift) | kAttestationChallengeTagNumber;
NS_ASSUME_NONNULL_BEGIN
static inline MTRTransportType MTRMakeTransportType(chip::Transport::Type type)
{
static_assert(MTRTransportTypeUndefined == (uint8_t) chip::Transport::Type::kUndefined, "MTRTransportType != Transport::Type");
static_assert(MTRTransportTypeUDP == (uint8_t) chip::Transport::Type::kUdp, "MTRTransportType != Transport::Type");
static_assert(MTRTransportTypeBLE == (uint8_t) chip::Transport::Type::kBle, "MTRTransportType != Transport::Type");
static_assert(MTRTransportTypeTCP == (uint8_t) chip::Transport::Type::kTcp, "MTRTransportType != Transport::Type");
return static_cast<MTRTransportType>(type);
}
@interface MTRBaseDevice ()
- (instancetype)initWithPASEDevice:(chip::DeviceProxy *)device controller:(MTRDeviceController *)controller;
/**
* Invalidate the CASE session, so an attempt to getConnectedDevice for this
* device id will have to create a new CASE session. Ideally this API will go
* away.
*/
- (void)invalidateCASESession;
/**
* Whether this device represents a PASE session or not.
*/
@property (nonatomic, assign, readonly) BOOL isPASEDevice;
/**
* Controller that that this MTRDevice was gotten from.
*/
@property (nonatomic, strong, readonly) MTRDeviceController * deviceController;
/**
* Node id for this MTRDevice. If this device represents a CASE session, this
* is set to the node ID of the target node. If this device represents a PASE
* session, this is set to the device id of the PASE device.
*/
@property (nonatomic, assign, readonly) chip::NodeId nodeID;
/**
* Initialize the device object as a CASE device with the given node id and
* controller. This will always succeed, even if there is no such node id on
* the controller's fabric, but attempts to actually use the MTRBaseDevice will
* fail (asynchronously) in that case.
*/
- (instancetype)initWithNodeID:(NSNumber *)nodeID controller:(MTRDeviceController *)controller;
/**
* Create a report, suitable in including in the sort of data structure that
* gets passed to MTRDeviceResponseHandler, from a given event header and
* already-decoded event data. The data is allowed to be nil in error cases
* (e.g. when TLV decoding failed).
*/
+ (NSDictionary *)eventReportForHeader:(const chip::app::EventHeader &)header andData:(id _Nullable)data;
/**
* Extract a data-value for the given response command from the given response-value
* dictionary, encode it to TLV, and return a System::PacketBufferHandle with
* the encoded data.
*
* Will return a null handle and an error if the given response-value does not represent a
* data command response or is the wrong response command, or if encoding to TLV fails.
*/
+ (chip::System::PacketBufferHandle)_responseDataForCommand:(NSDictionary<NSString *, id> *)responseValue
clusterID:(chip::ClusterId)clusterID
commandID:(chip::CommandId)commandID
error:(NSError * __autoreleasing *)error;
/**
* Like the public invokeCommandWithEndpointID but allows passing through a
* serverSideProcessingTimeout and controlling whether we log the call (so we
* can not log when the call is not actually originating with MTRBaseDevice).
*/
- (void)_invokeCommandWithEndpointID:(NSNumber *)endpointID
clusterID:(NSNumber *)clusterID
commandID:(NSNumber *)commandID
commandFields:(id)commandFields
timedInvokeTimeout:(NSNumber * _Nullable)timeoutMs
serverSideProcessingTimeout:(NSNumber * _Nullable)serverSideProcessingTimeout
logCall:(BOOL)logCall
queue:(dispatch_queue_t)queue
completion:(MTRDeviceResponseHandler)completion;
/**
* Like the public invokeCommandWithEndpointID but:
*
* 1) Allows passing through a serverSideProcessingTimeout.
* 2) Expects one of the command payload structs as commandPayload
* 3) On success, returns an instance of responseClass via the completion (or
* nil if there is no responseClass, which indicates a status-only command).
*/
- (void)_invokeKnownCommandWithEndpointID:(NSNumber *)endpointID
clusterID:(NSNumber *)clusterID
commandID:(NSNumber *)commandID
commandPayload:(id)commandPayload
timedInvokeTimeout:(NSNumber * _Nullable)timeout
serverSideProcessingTimeout:(NSNumber * _Nullable)serverSideProcessingTimeout
responseClass:(Class _Nullable)responseClass
queue:(dispatch_queue_t)queue
completion:(void (^)(id _Nullable response, NSError * _Nullable error))completion;
/**
* Like the public subscribeToAttributesWithEndpointID but:
*
* 1) Takes a concrete attribute path (not nullable).
* 2) Requires non-nil MTRSubscribeParams (since that's what MTRBaseClusters
* have anyway).
* 3) For the report handler, hands out the right type of value for the given
* attribute path. This means we have to know the type ofthe attribute.
*
* The attribute path is not represented as MTRAttributePath just because it's
* probably less code to pass in the three numbers instead of creating an object
* at all the (numerous) callsites.
*/
- (void)_subscribeToKnownAttributeWithEndpointID:(NSNumber *)endpointID
clusterID:(NSNumber *)clusterID
attributeID:(NSNumber *)attributeID
params:(MTRSubscribeParams *)params
queue:(dispatch_queue_t)queue
reportHandler:(void (^)(id _Nullable value, NSError * _Nullable error))reportHandler
subscriptionEstablished:(MTRSubscriptionEstablishedHandler _Nullable)subscriptionEstablished;
/**
* Like the public readAttributesWithEndpointID but:
*
* 1) Takes a concrete attribute path (not nullable).
* 2) For the completion handler, hands out the right type of value for the given
* attribute path. This means we have to know the type ofthe attribute.
* 3) Only calls the completion with a single value.
*
* The attribute path is not represented as MTRAttributePath just because it's
* probably less code to pass in the three numbers instead of creating an object
* at all the (numerous) callsites.
*/
- (void)_readKnownAttributeWithEndpointID:(NSNumber *)endpointID
clusterID:(NSNumber *)clusterID
attributeID:(NSNumber *)attributeID
params:(MTRReadParams * _Nullable)params
queue:(dispatch_queue_t)queue
completion:(void (^)(id _Nullable value, NSError * _Nullable error))completion;
/**
* Same as the public -readAttributePaths:eventPaths:params:queue:completion: except also include the data version in the data-value dictionary in the response dictionary, if the includeDataVersion argument is set to YES.
*/
- (void)readAttributePaths:(NSArray<MTRAttributeRequestPath *> * _Nullable)attributePaths
eventPaths:(NSArray<MTREventRequestPath *> * _Nullable)eventPaths
params:(MTRReadParams * _Nullable)params
includeDataVersion:(BOOL)includeDataVersion
queue:(dispatch_queue_t)queue
completion:(MTRDeviceResponseHandler)completion;
/**
* Same as the public version, except for logging. For use from MTRDevice only.
*/
- (void)_writeAttributeWithEndpointID:(NSNumber *)endpointID
clusterID:(NSNumber *)clusterID
attributeID:(NSNumber *)attributeID
value:(id)value
timedWriteTimeout:(NSNumber * _Nullable)timeoutMs
queue:(dispatch_queue_t)queue
completion:(MTRDeviceResponseHandler)completion;
/**
* Will return nil if our controller is not in fact a concrete controller.
*/
@property (nullable, nonatomic, strong, readonly) MTRDeviceController_Concrete * concreteController;
@end
@interface MTRClusterPath ()
- (instancetype)initWithPath:(const chip::app::ConcreteClusterPath &)path;
@end
@interface MTRAttributePath ()
- (instancetype)initWithPath:(const chip::app::ConcreteDataAttributePath &)path;
@end
@interface MTREventPath ()
- (instancetype)initWithPath:(const chip::app::ConcreteEventPath &)path;
@end
@interface MTRCommandPath ()
- (instancetype)initWithPath:(const chip::app::ConcreteCommandPath &)path;
@end
@interface MTRAttributeReport ()
- (instancetype)initWithPath:(const chip::app::ConcreteDataAttributePath &)path
value:(id _Nullable)value
error:(NSError * _Nullable)error;
@end
@interface MTREventReport ()
- (instancetype)initWithPath:(const chip::app::ConcreteEventPath &)path error:(NSError *)error;
- (instancetype)initWithPath:(const chip::app::ConcreteEventPath &)path
eventNumber:(NSNumber *)eventNumber
priority:(chip::app::PriorityLevel)priority
timestamp:(const chip::app::Timestamp &)timestamp
value:(id)value;
@end
@interface MTRAttributeRequestPath ()
- (void)convertToAttributePathParams:(chip::app::AttributePathParams &)params;
@end
@interface MTREventRequestPath ()
- (void)convertToEventPathParams:(chip::app::EventPathParams &)params;
@end
// Exported utility function
// Convert TLV data into data-value dictionary as described in MTRDeviceResponseHandler
NSDictionary<NSString *, id> * _Nullable MTRDecodeDataValueDictionaryFromCHIPTLV(chip::TLV::TLVReader * data, NSNumber * _Nullable dataVersion = nil);
// Convert a data-value dictionary as described in MTRDeviceResponseHandler into
// TLV Data with an anonymous tag. This method assumes the encoding of the
// value fits in a single UDP MTU; for lists this method might need to be used
// on each list item separately.
NSData * _Nullable MTREncodeTLVFromDataValueDictionary(MTRDeviceDataValueDictionary value, NSError * __autoreleasing * error);
NS_ASSUME_NONNULL_END