Skip to content

Commit cea1e36

Browse files
authored
[darwin-framework-tool] Set device delegate when using MTRDevice with –use-mtr-device (project-chip#36263)
1 parent bcdddaa commit cea1e36

File tree

7 files changed

+120
-3
lines changed

7 files changed

+120
-3
lines changed

examples/darwin-framework-tool/BUILD.gn

+2
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,8 @@ executable("darwin-framework-tool") {
194194
"commands/common/CertificateIssuer.mm",
195195
"commands/common/ControllerStorage.h",
196196
"commands/common/ControllerStorage.mm",
197+
"commands/common/DeviceDelegate.h",
198+
"commands/common/DeviceDelegate.mm",
197199
"commands/common/MTRDevice_Externs.h",
198200
"commands/common/MTRError.mm",
199201
"commands/common/MTRError_Utils.h",

examples/darwin-framework-tool/commands/clusters/ReportCommandBridge.h

+1-3
Original file line numberDiff line numberDiff line change
@@ -105,9 +105,7 @@ class ReadAttribute : public ModelCommand {
105105
LogNSError("Error reading attribute", error);
106106
RemoteDataModelLogger::LogAttributeErrorAsJSON(endpoint, cluster, attribute, error);
107107
} else {
108-
for (id item in values) {
109-
NSLog(@"Response Item: %@", [item description]);
110-
}
108+
NSLog(@"cluster (0x%08" PRIX32 ") ReadAttribute (0x%08" PRIX32 ") on endpoint %u: %@", mClusterId, mAttributeId, endpointId, values);
111109
RemoteDataModelLogger::LogAttributeAsJSON(endpoint, cluster, attribute, values);
112110
}
113111

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

+2
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ 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.");
5556
}
5657

5758
/////////// Command Interface /////////
@@ -164,4 +165,5 @@ class CHIPCommandBridge : public Command {
164165
chip::Optional<char *> mPaaTrustStorePath;
165166
chip::Optional<chip::VendorId> mCommissionerVendorId;
166167
std::string mCurrentIdentity;
168+
chip::Optional<bool> mPretendThreadEnabled;
167169
};

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

+15
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,15 @@
2828
#import "CHIPCommandStorageDelegate.h"
2929
#import "CertificateIssuer.h"
3030
#import "ControllerStorage.h"
31+
#import "DeviceDelegate.h"
3132
#include "MTRError_Utils.h"
3233

3334
#include <map>
3435
#include <string>
3536

3637
static CHIPToolPersistentStorageDelegate * storage = nil;
38+
static DeviceDelegate * sDeviceDelegate = nil;
39+
static dispatch_queue_t sDeviceDelegateDispatchQueue = nil;
3740
std::set<CHIPCommandBridge *> CHIPCommandBridge::sDeferredCleanups;
3841
std::map<std::string, MTRDeviceController *> CHIPCommandBridge::mControllers;
3942
dispatch_queue_t CHIPCommandBridge::mOTAProviderCallbackQueue;
@@ -302,6 +305,18 @@
302305
__auto_type * device = [MTRDevice deviceWithNodeID:@(nodeId) controller:controller];
303306
VerifyOrReturnValue(nil != device, nil);
304307

308+
// 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.
311+
if (sDeviceDelegate == nil) {
312+
sDeviceDelegate = [[DeviceDelegate alloc] init];
313+
sDeviceDelegateDispatchQueue = dispatch_queue_create("com.chip.devicedelegate", DISPATCH_QUEUE_SERIAL_WITH_AUTORELEASE_POOL);
314+
if (mPretendThreadEnabled.ValueOr(false)) {
315+
[sDeviceDelegate setPretendThreadEnabled:YES];
316+
}
317+
}
318+
[device addDelegate:sDeviceDelegate queue:sDeviceDelegateDispatchQueue];
319+
305320
return device;
306321
}
307322

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
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+
19+
#import <Matter/Matter.h>
20+
21+
@interface DeviceDelegate : NSObject <MTRDeviceDelegate>
22+
- (void)setPretendThreadEnabled:(BOOL)threadEnabled;
23+
@end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
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+
19+
#import "DeviceDelegate.h"
20+
21+
#include <lib/support/logging/CHIPLogging.h>
22+
23+
NS_ASSUME_NONNULL_BEGIN
24+
25+
@interface DeviceDelegate ()
26+
@property (nonatomic, readwrite) BOOL threadEnabled;
27+
@end
28+
29+
@implementation DeviceDelegate
30+
- (instancetype)init
31+
{
32+
if (self = [super init]) {
33+
_threadEnabled = NO;
34+
}
35+
return self;
36+
}
37+
38+
- (void)device:(MTRDevice *)device stateChanged:(MTRDeviceState)state
39+
{
40+
}
41+
42+
- (void)device:(MTRDevice *)device receivedAttributeReport:(NSArray<NSDictionary<NSString *, id> *> *)attributeReport
43+
{
44+
}
45+
46+
- (void)device:(MTRDevice *)device receivedEventReport:(NSArray<NSDictionary<NSString *, id> *> *)eventReport
47+
{
48+
}
49+
50+
- (void)deviceCachePrimed:(MTRDevice *)device
51+
{
52+
}
53+
54+
- (void)deviceConfigurationChanged:(MTRDevice *)device
55+
{
56+
}
57+
58+
- (void)setPretendThreadEnabled:(BOOL)threadEnabled
59+
{
60+
_threadEnabled = threadEnabled;
61+
}
62+
63+
- (BOOL)unitTestPretendThreadEnabled:(MTRDevice *)device
64+
{
65+
return _threadEnabled;
66+
}
67+
@end
68+
69+
NS_ASSUME_NONNULL_END

src/darwin/Framework/Matter.xcodeproj/project.pbxproj

+8
Original file line numberDiff line numberDiff line change
@@ -336,6 +336,8 @@
336336
B2E0D7B7245B0B5C003C5B48 /* MTRQRCodeSetupPayloadParser.mm in Sources */ = {isa = PBXBuildFile; fileRef = B2E0D7AE245B0B5C003C5B48 /* MTRQRCodeSetupPayloadParser.mm */; };
337337
B2E0D7B8245B0B5C003C5B48 /* MTRSetupPayload.h in Headers */ = {isa = PBXBuildFile; fileRef = B2E0D7AF245B0B5C003C5B48 /* MTRSetupPayload.h */; settings = {ATTRIBUTES = (Public, ); }; };
338338
B2E0D7B9245B0B5C003C5B48 /* MTRSetupPayload.mm in Sources */ = {isa = PBXBuildFile; fileRef = B2E0D7B0245B0B5C003C5B48 /* MTRSetupPayload.mm */; };
339+
B409D0AE2CCFB89600A7ED5A /* DeviceDelegate.h in Headers */ = {isa = PBXBuildFile; fileRef = B409D0AC2CCFB89600A7ED5A /* DeviceDelegate.h */; };
340+
B409D0AF2CCFB89600A7ED5A /* DeviceDelegate.mm in Sources */ = {isa = PBXBuildFile; fileRef = B409D0AD2CCFB89600A7ED5A /* DeviceDelegate.mm */; };
339341
B43B39EA2CB859A5006AA284 /* DumpMemoryGraphCommand.mm in Sources */ = {isa = PBXBuildFile; fileRef = B43B39E62CB859A5006AA284 /* DumpMemoryGraphCommand.mm */; };
340342
B43B39EB2CB859A5006AA284 /* LeaksTool.mm in Sources */ = {isa = PBXBuildFile; fileRef = B43B39E82CB859A5006AA284 /* LeaksTool.mm */; };
341343
B43B39EC2CB859A5006AA284 /* DumpMemoryGraphCommand.h in Headers */ = {isa = PBXBuildFile; fileRef = B43B39E52CB859A5006AA284 /* DumpMemoryGraphCommand.h */; };
@@ -800,6 +802,8 @@
800802
B2E0D7AE245B0B5C003C5B48 /* MTRQRCodeSetupPayloadParser.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; path = MTRQRCodeSetupPayloadParser.mm; sourceTree = "<group>"; };
801803
B2E0D7AF245B0B5C003C5B48 /* MTRSetupPayload.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = MTRSetupPayload.h; sourceTree = "<group>"; };
802804
B2E0D7B0245B0B5C003C5B48 /* MTRSetupPayload.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; path = MTRSetupPayload.mm; sourceTree = "<group>"; };
805+
B409D0AC2CCFB89600A7ED5A /* DeviceDelegate.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = DeviceDelegate.h; sourceTree = "<group>"; };
806+
B409D0AD2CCFB89600A7ED5A /* DeviceDelegate.mm */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.objcpp; path = DeviceDelegate.mm; sourceTree = "<group>"; };
803807
B43B39E42CB859A5006AA284 /* Commands.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = Commands.h; sourceTree = "<group>"; };
804808
B43B39E52CB859A5006AA284 /* DumpMemoryGraphCommand.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = DumpMemoryGraphCommand.h; sourceTree = "<group>"; };
805809
B43B39E62CB859A5006AA284 /* DumpMemoryGraphCommand.mm */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.objcpp; path = DumpMemoryGraphCommand.mm; sourceTree = "<group>"; };
@@ -1013,6 +1017,8 @@
10131017
037C3D9B2991BD4F00B7EEE2 /* common */ = {
10141018
isa = PBXGroup;
10151019
children = (
1020+
B409D0AC2CCFB89600A7ED5A /* DeviceDelegate.h */,
1021+
B409D0AD2CCFB89600A7ED5A /* DeviceDelegate.mm */,
10161022
B43B39EF2CB99090006AA284 /* CertificateIssuer.h */,
10171023
B43B39F02CB99090006AA284 /* CertificateIssuer.mm */,
10181024
B43B39F12CB99090006AA284 /* ControllerStorage.h */,
@@ -1673,6 +1679,7 @@
16731679
B4F773CA2CB54B61008C6B23 /* LeakChecker.h in Headers */,
16741680
B43B39F82CB99090006AA284 /* CertificateIssuer.h in Headers */,
16751681
B43B39F92CB99090006AA284 /* PreferencesStorage.h in Headers */,
1682+
B409D0AE2CCFB89600A7ED5A /* DeviceDelegate.h in Headers */,
16761683
B43B39FA2CB99090006AA284 /* ControllerStorage.h in Headers */,
16771684
037C3DB82991BD5000B7EEE2 /* ClusterCommandBridge.h in Headers */,
16781685
037C3DC82991BD5100B7EEE2 /* CHIPToolKeypair.h in Headers */,
@@ -2058,6 +2065,7 @@
20582065
0382FA302992F40C00247BBB /* ComplexArgumentParser.cpp in Sources */,
20592066
039145E12993102B00257B3E /* main.mm in Sources */,
20602067
037C3DD42991BD5200B7EEE2 /* logging.mm in Sources */,
2068+
B409D0AF2CCFB89600A7ED5A /* DeviceDelegate.mm in Sources */,
20612069
512431282BA0C8BF000BC136 /* SetMRPParametersCommand.mm in Sources */,
20622070
512431292BA0C8BF000BC136 /* ResetMRPParametersCommand.mm in Sources */,
20632071
037C3DB32991BD5000B7EEE2 /* OpenCommissioningWindowCommand.mm in Sources */,

0 commit comments

Comments
 (0)