blob: e76ec45889a4fc466ec27724196bbc238e80828b [file] [log] [blame]
Jeff Tung83fb7fd2022-08-02 19:29:07 -07001/**
2 *
3 * Copyright (c) 2022 Project CHIP Authors
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18#import <Foundation/Foundation.h>
19#import <Matter/MTRBaseDevice.h>
20
21NS_ASSUME_NONNULL_BEGIN
22
23@class MTRDeviceController;
24@class MTRAsyncCallbackWorkQueue;
25
Jeff Tung5afff3c2022-08-15 13:34:46 -070026typedef NS_ENUM(NSUInteger, MTRDeviceState) {
27 MTRDeviceStateUnknown = 0,
28 MTRDeviceStateReachable = 1,
29 MTRDeviceStateUnreachable = 2
30};
31
32@protocol MTRDeviceDelegate;
Jeff Tung83fb7fd2022-08-02 19:29:07 -070033
34@interface MTRDevice : NSObject
35- (instancetype)init NS_UNAVAILABLE;
36+ (instancetype)new NS_UNAVAILABLE;
37
38/**
Jeff Tung5afff3c2022-08-15 13:34:46 -070039 * TODO: Document usage better
40 *
Jeff Tung83fb7fd2022-08-02 19:29:07 -070041 * Directly instantiate a MTRDevice with a MTRDeviceController as a shim.
42 *
43 * All device-specific information would be stored on the device controller, and
44 * retrieved when performing actions using a combination of MTRBaseDevice
45 * and MTRAsyncCallbackQueue.
46 */
Boris Zbarsky09acc292022-10-28 14:05:35 -040047+ (instancetype)deviceWithNodeID:(NSNumber *)nodeID controller:(MTRDeviceController *)controller MTR_NEWLY_AVAILABLE;
Jeff Tung83fb7fd2022-08-02 19:29:07 -070048
49/**
Jeff Tung5afff3c2022-08-15 13:34:46 -070050 * The current state of the device.
Jeff Tung83fb7fd2022-08-02 19:29:07 -070051 *
Jeff Tung5afff3c2022-08-15 13:34:46 -070052 * The three states:
Boris Zbarsky2c9cd922022-10-13 00:49:05 -040053 * MTRDeviceStateUnknown
Jeff Tung5afff3c2022-08-15 13:34:46 -070054 * Unable to determine the state of the device at the moment.
55 *
56 * MTRDeviceStateReachable
57 * Communication with the device is expected to succeed.
58 *
59 * MTRDeviceStateUnreachable
60 * The device is currently unreachable.
Jeff Tung83fb7fd2022-08-02 19:29:07 -070061 */
Jeff Tung5afff3c2022-08-15 13:34:46 -070062@property (nonatomic, readonly) MTRDeviceState state;
63
64/**
65 * Set the delegate to receive asynchronous callbacks about the device.
66 *
67 * The delegate will be called on the provided queue, for attribute reports, event reports, and device state changes.
68 */
69- (void)setDelegate:(id<MTRDeviceDelegate>)delegate queue:(dispatch_queue_t)queue;
Jeff Tung83fb7fd2022-08-02 19:29:07 -070070
71/**
72 * Read attribute in a designated attribute path
73 *
Jeff Tung5afff3c2022-08-15 13:34:46 -070074 * TODO: Need to document that this returns "the system's best guess" of attribute values.
75 *
Jeff Tung83fb7fd2022-08-02 19:29:07 -070076 * @return a data-value dictionary of the attribute as described in MTRDeviceResponseHandler
77 */
Jeff Tung5afff3c2022-08-15 13:34:46 -070078- (NSDictionary<NSString *, id> *)readAttributeWithEndpointID:(NSNumber *)endpointID
79 clusterID:(NSNumber *)clusterID
80 attributeID:(NSNumber *)attributeID
Jeff Tung83fb7fd2022-08-02 19:29:07 -070081 params:(MTRReadParams * _Nullable)params;
82
83/**
84 * Write to attribute in a designated attribute path
85 *
86 * @param value A data-value NSDictionary object as described in
87 * MTRDeviceResponseHandler.
88 *
Jeff Tung5afff3c2022-08-15 13:34:46 -070089 * @param expectedValueInterval maximum interval in milliseconds during which reads of the attribute will return the value being
Jeff Tung11953102022-10-26 14:56:51 -070090 * written. This value must be within [1, UINT32_MAX], and will be clamped to this range.
Jeff Tung83fb7fd2022-08-02 19:29:07 -070091 *
Jeff Tung5afff3c2022-08-15 13:34:46 -070092 * TODO: document that -readAttribute... will return the expected value for the [endpoint,cluster,attribute] until one of the
93 * following:
94 * 1. Another write for the same attribute happens.
95 * 2. expectedValueIntervalMs (clamped) expires. Need to figure out phrasing here.
96 * 3. We succeed at writing the attribute.
97 * 4. We fail at writing the attribute and give up on the write
Jeff Tung83fb7fd2022-08-02 19:29:07 -070098 *
Jeff Tung11953102022-10-26 14:56:51 -070099 * @param timeout timeout in milliseconds for timed write, or nil. This value must be within [1, UINT16_MAX], and will be clamped
100 * to this range.
Jeff Tung5afff3c2022-08-15 13:34:46 -0700101 * TODO: make timeout arguments uniform
Jeff Tung83fb7fd2022-08-02 19:29:07 -0700102 */
Jeff Tung5afff3c2022-08-15 13:34:46 -0700103- (void)writeAttributeWithEndpointID:(NSNumber *)endpointID
104 clusterID:(NSNumber *)clusterID
105 attributeID:(NSNumber *)attributeID
Jeff Tung83fb7fd2022-08-02 19:29:07 -0700106 value:(id)value
Jeff Tung5afff3c2022-08-15 13:34:46 -0700107 expectedValueInterval:(NSNumber *)expectedValueInterval
108 timedWriteTimeout:(NSNumber * _Nullable)timeout;
Jeff Tung83fb7fd2022-08-02 19:29:07 -0700109
110/**
111 * Invoke a command with a designated command path
112 *
113 * @param commandFields command fields object. The object must be a data-value NSDictionary object
114 * as described in the MTRDeviceResponseHandler.
115 * The attribute must be a Structure, i.e.,
116 * the NSDictionary MTRTypeKey key must have the value MTRStructureValueType.
117 *
118 * @param expectedValues array of dictionaries containing the expected values in the same format as
119 * attribute read completion handler. Requires MTRAttributePathKey values.
120 * See MTRDeviceResponseHandler definition for dictionary details.
Jeff Tung11953102022-10-26 14:56:51 -0700121 * The expectedValues and expectedValueInterval arguments need to be both
122 * nil or both non-nil, or both will be both ignored.
123 *
Jeff Tung5afff3c2022-08-15 13:34:46 -0700124 * TODO: document better the expectedValues is how this command is expected to change attributes when read, and that the next
125 * readAttribute will get these values
Jeff Tung83fb7fd2022-08-02 19:29:07 -0700126 *
Jeff Tung5afff3c2022-08-15 13:34:46 -0700127 * @param expectedValueInterval maximum interval in milliseconds during which reads of the attribute will return the value being
Jeff Tung11953102022-10-26 14:56:51 -0700128 * written. If the value is less than 1, both this value and expectedValues will be ignored.
129 If this value is greater than UINT32_MAX, it will be clamped to UINT32_MAX.
Jeff Tung83fb7fd2022-08-02 19:29:07 -0700130 *
Jeff Tung11953102022-10-26 14:56:51 -0700131 * @param timeout timeout in milliseconds for timed invoke, or nil. This value must be within [1, UINT16_MAX], and will be clamped
132 * to this range.
Jeff Tung83fb7fd2022-08-02 19:29:07 -0700133 *
134 * @param completion response handler will receive either values or error.
135 */
Jeff Tung5afff3c2022-08-15 13:34:46 -0700136- (void)invokeCommandWithEndpointID:(NSNumber *)endpointID
137 clusterID:(NSNumber *)clusterID
138 commandID:(NSNumber *)commandID
Jeff Tung83fb7fd2022-08-02 19:29:07 -0700139 commandFields:(id)commandFields
Jeff Tung5afff3c2022-08-15 13:34:46 -0700140 expectedValues:(NSArray<NSDictionary<NSString *, id> *> * _Nullable)expectedValues
141 expectedValueInterval:(NSNumber * _Nullable)expectedValueInterval
142 timedInvokeTimeout:(NSNumber * _Nullable)timeout
Boris Zbarsky35187602022-11-02 18:19:46 -0400143 queue:(dispatch_queue_t)queue
144 completion:(MTRDeviceResponseHandler)completion MTR_NEWLY_AVAILABLE;
Jeff Tung83fb7fd2022-08-02 19:29:07 -0700145
Boris Zbarsky2c9cd922022-10-13 00:49:05 -0400146/**
147 * Open a commissioning window on the device.
148 *
149 * On success, completion will be called on queue with the MTRSetupPayload that
150 * can be used to commission the device.
151 *
152 * @param setupPasscode The setup passcode to use for the commissioning window.
153 * See MTRSetupPayload's generateRandomSetupPasscode for
154 * generating a valid random passcode.
155 * @param discriminator The discriminator to use for the commissionable
156 * advertisement.
157 * @param duration Duration, in seconds, during which the commissioning
158 * window will be open.
159 */
160- (void)openCommissioningWindowWithSetupPasscode:(NSNumber *)setupPasscode
161 discriminator:(NSNumber *)discriminator
162 duration:(NSNumber *)duration
163 queue:(dispatch_queue_t)queue
Boris Zbarskye62498b2022-10-19 23:15:19 -0400164 completion:(MTRDeviceOpenCommissioningWindowHandler)completion
165 API_AVAILABLE(ios(16.2), macos(13.1), watchos(9.2), tvos(16.2));
Boris Zbarsky2c9cd922022-10-13 00:49:05 -0400166
Jeff Tung83fb7fd2022-08-02 19:29:07 -0700167@end
168
Jeff Tung5afff3c2022-08-15 13:34:46 -0700169@protocol MTRDeviceDelegate <NSObject>
Jeff Tung83fb7fd2022-08-02 19:29:07 -0700170@required
171/**
Jeff Tung5afff3c2022-08-15 13:34:46 -0700172 * device:stateChanged:
Jeff Tung83fb7fd2022-08-02 19:29:07 -0700173 *
Jeff Tung5afff3c2022-08-15 13:34:46 -0700174 * @param state The current state of the device
Jeff Tung83fb7fd2022-08-02 19:29:07 -0700175 */
Jeff Tung5afff3c2022-08-15 13:34:46 -0700176- (void)device:(MTRDevice *)device stateChanged:(MTRDeviceState)state;
Jeff Tung83fb7fd2022-08-02 19:29:07 -0700177
178/**
Jeff Tung5afff3c2022-08-15 13:34:46 -0700179 * device:receivedAttributeReport:
Jeff Tung83fb7fd2022-08-02 19:29:07 -0700180 *
181 * Notifies delegate of attribute reports from the MTRDevice
182 *
183 * @param attributeReport An array of response-value objects as described in MTRDeviceResponseHandler
184 */
Jeff Tung5afff3c2022-08-15 13:34:46 -0700185- (void)device:(MTRDevice *)device receivedAttributeReport:(NSArray<NSDictionary<NSString *, id> *> *)attributeReport;
Jeff Tung83fb7fd2022-08-02 19:29:07 -0700186
187/**
188 * subscriptionReceivedEventReport:
189 *
190 * Notifies delegate of event reports from the MTRDevice
191 *
Jeff Tung5afff3c2022-08-15 13:34:46 -0700192 * @param eventReport An array of response-value objects as described in MTRDeviceResponseHandler
Jeff Tung83fb7fd2022-08-02 19:29:07 -0700193 */
Jeff Tung5afff3c2022-08-15 13:34:46 -0700194- (void)device:(MTRDevice *)device receivedEventReport:(NSArray<NSDictionary<NSString *, id> *> *)eventReport;
Jeff Tung83fb7fd2022-08-02 19:29:07 -0700195
Jeff Tung83fb7fd2022-08-02 19:29:07 -0700196@end
197
Boris Zbarskyf88f7bb2022-11-01 18:10:00 -0400198@interface MTRDevice (Deprecated)
199
200/**
201 * Deprecated MTRDevice APIs.
202 */
203+ (instancetype)deviceWithNodeID:(uint64_t)nodeID
204 deviceController:(MTRDeviceController *)deviceController
205 MTR_NEWLY_DEPRECATED("Please use deviceWithNodeID:controller:");
206
Boris Zbarsky35187602022-11-02 18:19:46 -0400207- (void)invokeCommandWithEndpointID:(NSNumber *)endpointID
208 clusterID:(NSNumber *)clusterID
209 commandID:(NSNumber *)commandID
210 commandFields:(id)commandFields
211 expectedValues:(NSArray<NSDictionary<NSString *, id> *> * _Nullable)expectedValues
212 expectedValueInterval:(NSNumber * _Nullable)expectedValueInterval
213 timedInvokeTimeout:(NSNumber * _Nullable)timeout
214 clientQueue:(dispatch_queue_t)queue
215 completion:(MTRDeviceResponseHandler)completion
216 MTR_NEWLY_DEPRECATED("Please use "
217 "invokeCommandWithEndpointID:clusterID:commandID:commandFields:expectedValues:expectedValueInterval:"
218 "timedInvokeTimeout:queue:completion:");
219
Boris Zbarskyf88f7bb2022-11-01 18:10:00 -0400220@end
221
Jeff Tung83fb7fd2022-08-02 19:29:07 -0700222NS_ASSUME_NONNULL_END