|
| 1 | +/* |
| 2 | + * Copyright (c) 2024 Project CHIP Authors |
| 3 | + * All rights reserved. |
| 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 | +#pragma once |
| 19 | + |
| 20 | +#import "../common/DeviceDelegate.h" |
| 21 | +#import <Matter/Matter.h> |
| 22 | + |
| 23 | +#include <commands/common/Command.h> |
| 24 | + |
| 25 | +class SetUpDeviceCommand : public CHIPCommandBridge { |
| 26 | +public: |
| 27 | + SetUpDeviceCommand() |
| 28 | + : CHIPCommandBridge("device", "Creates and configures an instance of a device.") |
| 29 | + { |
| 30 | + AddArgument("node-id", 0, UINT64_MAX, &mNodeId, "The Node ID of the device instance to create."); |
| 31 | + AddArgument("pretend-thread-enabled", 0, 1, &mPretendThreadEnabled, |
| 32 | + "When the device is configured using an MTRDevice (via -use-mtr-device), instructs the MTRDevice to treat the " |
| 33 | + "target device as a Thread device."); |
| 34 | + AddArgument("max-interval", 0, UINT32_MAX, &mMaxIntervalForSubscription, |
| 35 | + "When the device is configured using an MTRDevice (via --use-mtr-device), configure the maximum interval for the " |
| 36 | + "delegate subscription."); |
| 37 | + } |
| 38 | + |
| 39 | +protected: |
| 40 | + /////////// CHIPCommandBridge Interface ///////// |
| 41 | + CHIP_ERROR RunCommand() override |
| 42 | + { |
| 43 | + __auto_type * controller = CurrentCommissioner(); |
| 44 | + VerifyOrReturnError(nil != controller, CHIP_ERROR_INCORRECT_STATE); |
| 45 | + |
| 46 | + __auto_type * device = [MTRDevice deviceWithNodeID:@(mNodeId) controller:controller]; |
| 47 | + VerifyOrReturnError(nil != device, CHIP_ERROR_INCORRECT_STATE); |
| 48 | + |
| 49 | + __auto_type * delegate = ConfigureDelegate(); |
| 50 | + __auto_type queue = dispatch_queue_create("com.chip.devicedelegate", DISPATCH_QUEUE_SERIAL_WITH_AUTORELEASE_POOL); |
| 51 | + [device addDelegate:delegate queue:queue]; |
| 52 | + |
| 53 | + mDelegate = delegate; |
| 54 | + SetCommandExitStatus(CHIP_NO_ERROR); |
| 55 | + return CHIP_NO_ERROR; |
| 56 | + } |
| 57 | + |
| 58 | + // Our command is synchronous, so no need to wait. |
| 59 | + chip::System::Clock::Timeout GetWaitDuration() const override { return chip::System::Clock::kZero; } |
| 60 | + |
| 61 | +private: |
| 62 | + DeviceDelegate * ConfigureDelegate() |
| 63 | + { |
| 64 | + __auto_type * delegate = [[DeviceDelegate alloc] init]; |
| 65 | + |
| 66 | + if (mPretendThreadEnabled.ValueOr(false)) { |
| 67 | + [delegate setPretendThreadEnabled:YES]; |
| 68 | + } |
| 69 | + |
| 70 | + if (mMaxIntervalForSubscription.HasValue()) { |
| 71 | + [delegate setMaxIntervalForSubscription:@(mMaxIntervalForSubscription.Value())]; |
| 72 | + } |
| 73 | + |
| 74 | + return delegate; |
| 75 | + } |
| 76 | + |
| 77 | + DeviceDelegate * mDelegate; |
| 78 | + |
| 79 | + chip::NodeId mNodeId; |
| 80 | + chip::Optional<bool> mPretendThreadEnabled; |
| 81 | + chip::Optional<uint32_t> mMaxIntervalForSubscription; |
| 82 | +}; |
0 commit comments