Skip to content

Commit c0071eb

Browse files
authored
[darwin-framework-tool] Add a command to initialize a MTRDevice such the the initial subscription is setted up without having to issue an additional command (e.g read or write) (project-chip#36287)
1 parent fe5ddda commit c0071eb

File tree

6 files changed

+97
-7
lines changed

6 files changed

+97
-7
lines changed

examples/darwin-framework-tool/commands/common/CHIPCommandBridge.h

-2
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,6 @@ class CHIPCommandBridge : public Command {
5252
AddArgument("commissioner-vendor-id", 0, UINT16_MAX, &mCommissionerVendorId,
5353
"The vendor id to use for darwin-framework-tool. If not provided, chip::VendorId::TestVendor1 (65521, 0xFFF1) will be "
5454
"used.");
55-
AddArgument("pretend-thread-enabled", 0, 1, &mPretendThreadEnabled, "When the command is issued using an MTRDevice (via -use-mtr-device), instructs the MTRDevice to treat the target device as a Thread device.");
5655
}
5756

5857
/////////// Command Interface /////////
@@ -165,5 +164,4 @@ class CHIPCommandBridge : public Command {
165164
chip::Optional<char *> mPaaTrustStorePath;
166165
chip::Optional<chip::VendorId> mCommissionerVendorId;
167166
std::string mCurrentIdentity;
168-
chip::Optional<bool> mPretendThreadEnabled;
169167
};

examples/darwin-framework-tool/commands/common/CHIPCommandBridge.mm

-5
Original file line numberDiff line numberDiff line change
@@ -306,14 +306,9 @@
306306
VerifyOrReturnValue(nil != device, nil);
307307

308308
// The device delegate is initialized only once, when the first MTRDevice is created.
309-
// As a result, subsequent commands using --use-mtr-device don’t need to specify the
310-
// `--pretend-thread-enabled 1` argument again. Any further attempts to set it to `0` will also be ignored.
311309
if (sDeviceDelegate == nil) {
312310
sDeviceDelegate = [[DeviceDelegate alloc] init];
313311
sDeviceDelegateDispatchQueue = dispatch_queue_create("com.chip.devicedelegate", DISPATCH_QUEUE_SERIAL_WITH_AUTORELEASE_POOL);
314-
if (mPretendThreadEnabled.ValueOr(false)) {
315-
[sDeviceDelegate setPretendThreadEnabled:YES];
316-
}
317312
}
318313
[device addDelegate:sDeviceDelegate queue:sDeviceDelegateDispatchQueue];
319314

examples/darwin-framework-tool/commands/common/DeviceDelegate.h

+1
Original file line numberDiff line numberDiff line change
@@ -19,5 +19,6 @@
1919
#import <Matter/Matter.h>
2020

2121
@interface DeviceDelegate : NSObject <MTRDeviceDelegate>
22+
- (void)setMaxIntervalForSubscription:(NSNumber *)maxInterval;
2223
- (void)setPretendThreadEnabled:(BOOL)threadEnabled;
2324
@end

examples/darwin-framework-tool/commands/common/DeviceDelegate.mm

+12
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,15 @@
2323
NS_ASSUME_NONNULL_BEGIN
2424

2525
@interface DeviceDelegate ()
26+
@property (nonatomic, readwrite) NSNumber * maxIntervalForSubscription;
2627
@property (nonatomic, readwrite) BOOL threadEnabled;
2728
@end
2829

2930
@implementation DeviceDelegate
3031
- (instancetype)init
3132
{
3233
if (self = [super init]) {
34+
_maxIntervalForSubscription = nil;
3335
_threadEnabled = NO;
3436
}
3537
return self;
@@ -55,6 +57,16 @@ - (void)deviceConfigurationChanged:(MTRDevice *)device
5557
{
5658
}
5759

60+
- (void)setMaxIntervalForSubscription:(NSNumber *)maxInterval
61+
{
62+
_maxIntervalForSubscription = maxInterval;
63+
}
64+
65+
- (NSNumber *)unitTestMaxIntervalOverrideForSubscription:(MTRDevice *)device
66+
{
67+
return _maxIntervalForSubscription;
68+
}
69+
5870
- (void)setPretendThreadEnabled:(BOOL)threadEnabled
5971
{
6072
_threadEnabled = threadEnabled;

examples/darwin-framework-tool/commands/configuration/Commands.h

+2
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,14 @@
2222

2323
#include "ResetMRPParametersCommand.h"
2424
#include "SetMRPParametersCommand.h"
25+
#include "SetUpDeviceCommand.h"
2526

2627
void registerCommandsConfiguration(Commands & commands)
2728
{
2829
const char * clusterName = "Configuration";
2930

3031
commands_list clusterCommands = {
32+
make_unique<SetUpDeviceCommand>(),
3133
make_unique<SetMRPParametersCommand>(),
3234
make_unique<ResetMRPParametersCommand>(),
3335
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
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

Comments
 (0)