Jeff Tung | 83fb7fd | 2022-08-02 19:29:07 -0700 | [diff] [blame] | 1 | /** |
| 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 | |
| 21 | NS_ASSUME_NONNULL_BEGIN |
| 22 | |
| 23 | @class MTRDeviceController; |
| 24 | @class MTRAsyncCallbackWorkQueue; |
| 25 | |
Jeff Tung | 5afff3c | 2022-08-15 13:34:46 -0700 | [diff] [blame] | 26 | typedef NS_ENUM(NSUInteger, MTRDeviceState) { |
| 27 | MTRDeviceStateUnknown = 0, |
| 28 | MTRDeviceStateReachable = 1, |
| 29 | MTRDeviceStateUnreachable = 2 |
| 30 | }; |
| 31 | |
| 32 | @protocol MTRDeviceDelegate; |
Jeff Tung | 83fb7fd | 2022-08-02 19:29:07 -0700 | [diff] [blame] | 33 | |
| 34 | @interface MTRDevice : NSObject |
| 35 | - (instancetype)init NS_UNAVAILABLE; |
| 36 | + (instancetype)new NS_UNAVAILABLE; |
| 37 | |
| 38 | /** |
Jeff Tung | 5afff3c | 2022-08-15 13:34:46 -0700 | [diff] [blame] | 39 | * TODO: Document usage better |
| 40 | * |
Jeff Tung | 83fb7fd | 2022-08-02 19:29:07 -0700 | [diff] [blame] | 41 | * 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 Zbarsky | 09acc29 | 2022-10-28 14:05:35 -0400 | [diff] [blame] | 47 | + (instancetype)deviceWithNodeID:(NSNumber *)nodeID controller:(MTRDeviceController *)controller MTR_NEWLY_AVAILABLE; |
Jeff Tung | 83fb7fd | 2022-08-02 19:29:07 -0700 | [diff] [blame] | 48 | |
| 49 | /** |
Jeff Tung | 5afff3c | 2022-08-15 13:34:46 -0700 | [diff] [blame] | 50 | * The current state of the device. |
Jeff Tung | 83fb7fd | 2022-08-02 19:29:07 -0700 | [diff] [blame] | 51 | * |
Jeff Tung | 5afff3c | 2022-08-15 13:34:46 -0700 | [diff] [blame] | 52 | * The three states: |
Boris Zbarsky | 2c9cd92 | 2022-10-13 00:49:05 -0400 | [diff] [blame] | 53 | * MTRDeviceStateUnknown |
Jeff Tung | 5afff3c | 2022-08-15 13:34:46 -0700 | [diff] [blame] | 54 | * 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 Tung | 83fb7fd | 2022-08-02 19:29:07 -0700 | [diff] [blame] | 61 | */ |
Jeff Tung | 5afff3c | 2022-08-15 13:34:46 -0700 | [diff] [blame] | 62 | @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 Tung | 83fb7fd | 2022-08-02 19:29:07 -0700 | [diff] [blame] | 70 | |
| 71 | /** |
| 72 | * Read attribute in a designated attribute path |
| 73 | * |
Jeff Tung | 5afff3c | 2022-08-15 13:34:46 -0700 | [diff] [blame] | 74 | * TODO: Need to document that this returns "the system's best guess" of attribute values. |
| 75 | * |
Jeff Tung | 83fb7fd | 2022-08-02 19:29:07 -0700 | [diff] [blame] | 76 | * @return a data-value dictionary of the attribute as described in MTRDeviceResponseHandler |
| 77 | */ |
Jeff Tung | 5afff3c | 2022-08-15 13:34:46 -0700 | [diff] [blame] | 78 | - (NSDictionary<NSString *, id> *)readAttributeWithEndpointID:(NSNumber *)endpointID |
| 79 | clusterID:(NSNumber *)clusterID |
| 80 | attributeID:(NSNumber *)attributeID |
Jeff Tung | 83fb7fd | 2022-08-02 19:29:07 -0700 | [diff] [blame] | 81 | 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 Tung | 5afff3c | 2022-08-15 13:34:46 -0700 | [diff] [blame] | 89 | * @param expectedValueInterval maximum interval in milliseconds during which reads of the attribute will return the value being |
Jeff Tung | 1195310 | 2022-10-26 14:56:51 -0700 | [diff] [blame] | 90 | * written. This value must be within [1, UINT32_MAX], and will be clamped to this range. |
Jeff Tung | 83fb7fd | 2022-08-02 19:29:07 -0700 | [diff] [blame] | 91 | * |
Jeff Tung | 5afff3c | 2022-08-15 13:34:46 -0700 | [diff] [blame] | 92 | * 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 Tung | 83fb7fd | 2022-08-02 19:29:07 -0700 | [diff] [blame] | 98 | * |
Jeff Tung | 1195310 | 2022-10-26 14:56:51 -0700 | [diff] [blame] | 99 | * @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 Tung | 5afff3c | 2022-08-15 13:34:46 -0700 | [diff] [blame] | 101 | * TODO: make timeout arguments uniform |
Jeff Tung | 83fb7fd | 2022-08-02 19:29:07 -0700 | [diff] [blame] | 102 | */ |
Jeff Tung | 5afff3c | 2022-08-15 13:34:46 -0700 | [diff] [blame] | 103 | - (void)writeAttributeWithEndpointID:(NSNumber *)endpointID |
| 104 | clusterID:(NSNumber *)clusterID |
| 105 | attributeID:(NSNumber *)attributeID |
Jeff Tung | 83fb7fd | 2022-08-02 19:29:07 -0700 | [diff] [blame] | 106 | value:(id)value |
Jeff Tung | 5afff3c | 2022-08-15 13:34:46 -0700 | [diff] [blame] | 107 | expectedValueInterval:(NSNumber *)expectedValueInterval |
| 108 | timedWriteTimeout:(NSNumber * _Nullable)timeout; |
Jeff Tung | 83fb7fd | 2022-08-02 19:29:07 -0700 | [diff] [blame] | 109 | |
| 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 Tung | 1195310 | 2022-10-26 14:56:51 -0700 | [diff] [blame] | 121 | * The expectedValues and expectedValueInterval arguments need to be both |
| 122 | * nil or both non-nil, or both will be both ignored. |
| 123 | * |
Jeff Tung | 5afff3c | 2022-08-15 13:34:46 -0700 | [diff] [blame] | 124 | * 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 Tung | 83fb7fd | 2022-08-02 19:29:07 -0700 | [diff] [blame] | 126 | * |
Jeff Tung | 5afff3c | 2022-08-15 13:34:46 -0700 | [diff] [blame] | 127 | * @param expectedValueInterval maximum interval in milliseconds during which reads of the attribute will return the value being |
Jeff Tung | 1195310 | 2022-10-26 14:56:51 -0700 | [diff] [blame] | 128 | * 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 Tung | 83fb7fd | 2022-08-02 19:29:07 -0700 | [diff] [blame] | 130 | * |
Jeff Tung | 1195310 | 2022-10-26 14:56:51 -0700 | [diff] [blame] | 131 | * @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 Tung | 83fb7fd | 2022-08-02 19:29:07 -0700 | [diff] [blame] | 133 | * |
| 134 | * @param completion response handler will receive either values or error. |
| 135 | */ |
Jeff Tung | 5afff3c | 2022-08-15 13:34:46 -0700 | [diff] [blame] | 136 | - (void)invokeCommandWithEndpointID:(NSNumber *)endpointID |
| 137 | clusterID:(NSNumber *)clusterID |
| 138 | commandID:(NSNumber *)commandID |
Jeff Tung | 83fb7fd | 2022-08-02 19:29:07 -0700 | [diff] [blame] | 139 | commandFields:(id)commandFields |
Jeff Tung | 5afff3c | 2022-08-15 13:34:46 -0700 | [diff] [blame] | 140 | expectedValues:(NSArray<NSDictionary<NSString *, id> *> * _Nullable)expectedValues |
| 141 | expectedValueInterval:(NSNumber * _Nullable)expectedValueInterval |
| 142 | timedInvokeTimeout:(NSNumber * _Nullable)timeout |
Boris Zbarsky | 3518760 | 2022-11-02 18:19:46 -0400 | [diff] [blame] | 143 | queue:(dispatch_queue_t)queue |
| 144 | completion:(MTRDeviceResponseHandler)completion MTR_NEWLY_AVAILABLE; |
Jeff Tung | 83fb7fd | 2022-08-02 19:29:07 -0700 | [diff] [blame] | 145 | |
Boris Zbarsky | 2c9cd92 | 2022-10-13 00:49:05 -0400 | [diff] [blame] | 146 | /** |
| 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 Zbarsky | e62498b | 2022-10-19 23:15:19 -0400 | [diff] [blame] | 164 | completion:(MTRDeviceOpenCommissioningWindowHandler)completion |
| 165 | API_AVAILABLE(ios(16.2), macos(13.1), watchos(9.2), tvos(16.2)); |
Boris Zbarsky | 2c9cd92 | 2022-10-13 00:49:05 -0400 | [diff] [blame] | 166 | |
Jeff Tung | 83fb7fd | 2022-08-02 19:29:07 -0700 | [diff] [blame] | 167 | @end |
| 168 | |
Jeff Tung | 5afff3c | 2022-08-15 13:34:46 -0700 | [diff] [blame] | 169 | @protocol MTRDeviceDelegate <NSObject> |
Jeff Tung | 83fb7fd | 2022-08-02 19:29:07 -0700 | [diff] [blame] | 170 | @required |
| 171 | /** |
Jeff Tung | 5afff3c | 2022-08-15 13:34:46 -0700 | [diff] [blame] | 172 | * device:stateChanged: |
Jeff Tung | 83fb7fd | 2022-08-02 19:29:07 -0700 | [diff] [blame] | 173 | * |
Jeff Tung | 5afff3c | 2022-08-15 13:34:46 -0700 | [diff] [blame] | 174 | * @param state The current state of the device |
Jeff Tung | 83fb7fd | 2022-08-02 19:29:07 -0700 | [diff] [blame] | 175 | */ |
Jeff Tung | 5afff3c | 2022-08-15 13:34:46 -0700 | [diff] [blame] | 176 | - (void)device:(MTRDevice *)device stateChanged:(MTRDeviceState)state; |
Jeff Tung | 83fb7fd | 2022-08-02 19:29:07 -0700 | [diff] [blame] | 177 | |
| 178 | /** |
Jeff Tung | 5afff3c | 2022-08-15 13:34:46 -0700 | [diff] [blame] | 179 | * device:receivedAttributeReport: |
Jeff Tung | 83fb7fd | 2022-08-02 19:29:07 -0700 | [diff] [blame] | 180 | * |
| 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 Tung | 5afff3c | 2022-08-15 13:34:46 -0700 | [diff] [blame] | 185 | - (void)device:(MTRDevice *)device receivedAttributeReport:(NSArray<NSDictionary<NSString *, id> *> *)attributeReport; |
Jeff Tung | 83fb7fd | 2022-08-02 19:29:07 -0700 | [diff] [blame] | 186 | |
| 187 | /** |
| 188 | * subscriptionReceivedEventReport: |
| 189 | * |
| 190 | * Notifies delegate of event reports from the MTRDevice |
| 191 | * |
Jeff Tung | 5afff3c | 2022-08-15 13:34:46 -0700 | [diff] [blame] | 192 | * @param eventReport An array of response-value objects as described in MTRDeviceResponseHandler |
Jeff Tung | 83fb7fd | 2022-08-02 19:29:07 -0700 | [diff] [blame] | 193 | */ |
Jeff Tung | 5afff3c | 2022-08-15 13:34:46 -0700 | [diff] [blame] | 194 | - (void)device:(MTRDevice *)device receivedEventReport:(NSArray<NSDictionary<NSString *, id> *> *)eventReport; |
Jeff Tung | 83fb7fd | 2022-08-02 19:29:07 -0700 | [diff] [blame] | 195 | |
Jeff Tung | 83fb7fd | 2022-08-02 19:29:07 -0700 | [diff] [blame] | 196 | @end |
| 197 | |
Boris Zbarsky | f88f7bb | 2022-11-01 18:10:00 -0400 | [diff] [blame] | 198 | @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 Zbarsky | 3518760 | 2022-11-02 18:19:46 -0400 | [diff] [blame] | 207 | - (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 Zbarsky | f88f7bb | 2022-11-01 18:10:00 -0400 | [diff] [blame] | 220 | @end |
| 221 | |
Jeff Tung | 83fb7fd | 2022-08-02 19:29:07 -0700 | [diff] [blame] | 222 | NS_ASSUME_NONNULL_END |