Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Darwin] New XPC Service #34961

Merged
merged 75 commits into from
Aug 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
75 commits
Select commit Hold shift + click to select a range
7207241
empty xpc service files
kiel-apple Aug 13, 2024
9434b68
XPC service needs to be C++ to use MTRLogging
kiel-apple Aug 13, 2024
f5e3165
add tests; stub method signature
kiel-apple Aug 13, 2024
cb9c9a8
start configuring test XPC listener
kiel-apple Aug 13, 2024
325169d
add first test method to XPC service protocol
kiel-apple Aug 13, 2024
1e88fab
tests: remove peformance test; add headers
kiel-apple Aug 13, 2024
205ccc3
make MTRXPCService testable
kiel-apple Aug 13, 2024
94eccf4
more working lines of XPC listener setup
kiel-apple Aug 13, 2024
2bee8fb
add dummy XPC service for testing
kiel-apple Aug 13, 2024
bd74185
add replying method to service protocol / implementation
kiel-apple Aug 13, 2024
633c6d8
remove log - reply contents can be asserted
kiel-apple Aug 13, 2024
5fa0502
make test interesting
kiel-apple Aug 13, 2024
d21d1b1
linty formatty
kiel-apple Aug 13, 2024
82667bb
remove personal development team from pbxproj
kiel-apple Aug 13, 2024
b1b6ba0
fix new file headers
kiel-apple Aug 13, 2024
6988477
less whitespace
kiel-apple Aug 13, 2024
3470b65
Restyled by whitespace
restyled-commits Aug 13, 2024
b00bec3
Restyled by clang-format
restyled-commits Aug 13, 2024
109d81d
add new XPC device controller
kiel-apple Aug 14, 2024
46cd30b
sketch out a first XPC method
kiel-apple Aug 14, 2024
1e4db9d
store WIP for rebase
kiel-apple Aug 15, 2024
ff7d8b9
fix pbxproj merge mistake
kiel-apple Aug 15, 2024
e180756
make WIP slightly more sensible
kiel-apple Aug 15, 2024
12867fa
WIP: remote proxy obj in `MTRDeviceController_XPC`
kiel-apple Aug 15, 2024
b717259
more experiments
kiel-apple Aug 15, 2024
8304c4e
WIP: return type issue
kiel-apple Aug 15, 2024
19f0487
add `MTRDevice_XPC`
kiel-apple Aug 15, 2024
4a1c25b
add device controller over XPC parameters stub
kiel-apple Aug 16, 2024
7a05546
add init split for XPC device controllers
kiel-apple Aug 16, 2024
cc0312e
rename new XPC parameters class
kiel-apple Aug 16, 2024
7ff8f0a
Restyled by whitespace
restyled-commits Aug 16, 2024
b4a2c42
Restyled by clang-format
restyled-commits Aug 16, 2024
a150893
remove failing exploratory test
kiel-apple Aug 16, 2024
7af4e94
lintfix
kiel-apple Aug 16, 2024
724f712
Examples
woody-apple Aug 16, 2024
82b739a
quickly attempt to fix test
kiel-apple Aug 16, 2024
1da3a86
Restyled by whitespace
restyled-commits Aug 16, 2024
6a9ff07
Restyled by clang-format
restyled-commits Aug 16, 2024
76b57e4
inevitably the file will need to be obj-C++
kiel-apple Aug 16, 2024
71905d2
prepare to use mach service name to create XPC connection
kiel-apple Aug 16, 2024
a5a1b9e
allow for other XPC controller connection types for now
kiel-apple Aug 16, 2024
44da0ed
constant for well-known matter plugin service name
kiel-apple Aug 16, 2024
82346bf
note to self and re-ordering of this code now what `XPCParameters` is…
kiel-apple Aug 16, 2024
2f51348
XPC parameters should support secure coding
kiel-apple Aug 16, 2024
c14a279
don't keep around object proxy; make a new one when needed. try call…
kiel-apple Aug 16, 2024
33556ea
that doesn't need to be there.
kiel-apple Aug 16, 2024
71b33e7
Restyled by whitespace
restyled-commits Aug 16, 2024
2fa09b8
Restyled by clang-format
restyled-commits Aug 16, 2024
0ae822f
no longer fighting with compiler, have some `instancetype`
kiel-apple Aug 16, 2024
12adef2
extremely normal object construction
kiel-apple Aug 16, 2024
9e131f0
somehow this header got thrown out
kiel-apple Aug 16, 2024
1627974
Restyled by whitespace
restyled-commits Aug 16, 2024
bd58fe8
Restyled by clang-format
restyled-commits Aug 16, 2024
a834633
Updating XPC interfaces
woody-apple Aug 16, 2024
5a975c9
hide `initWithMachServiceName:options:` constructor
kiel-apple Aug 17, 2024
0ef283d
make `MTRDeviceController_XPC` an XPC client
kiel-apple Aug 17, 2024
9af2a81
remove initial tests
kiel-apple Aug 17, 2024
ae5f284
call mach service constructor when appropriate
kiel-apple Aug 17, 2024
a681706
remove some obsolete test methods
kiel-apple Aug 17, 2024
94bd0bb
more obsoletes fixed
kiel-apple Aug 17, 2024
526aab8
more obsolete removal and logging tweaks
kiel-apple Aug 17, 2024
79e8df6
buildability tweaks
kiel-apple Aug 17, 2024
a2aaa22
Moving along
woody-apple Aug 17, 2024
2101318
Fixing header
woody-apple Aug 17, 2024
4e78ef0
Fixing macros and codable
woody-apple Aug 17, 2024
c04f2bf
Adding invoke?
woody-apple Aug 17, 2024
8e8d4d5
Restyled by whitespace
restyled-commits Aug 17, 2024
19a222e
Restyled by clang-format
restyled-commits Aug 17, 2024
e7f7587
Actually hooking up to XPCConnection
woody-apple Aug 17, 2024
6cc892a
Actually hooking up to XPCConnection
woody-apple Aug 17, 2024
baf7f15
Adding XPC Parameters
woody-apple Aug 17, 2024
1552018
Adding XPC Parameter hookup
woody-apple Aug 17, 2024
713bf5c
Restyled by whitespace
restyled-commits Aug 17, 2024
bffca71
Restyled by clang-format
restyled-commits Aug 17, 2024
d083e00
true => YES
woody-apple Aug 17, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/darwin/Framework/CHIP/MTRCluster.h
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ MTR_AVAILABLE(ios(17.4), macos(14.4), watchos(10.4), tvos(17.4))
* treated as if a default-initialized object was passed in.
*/
MTR_AVAILABLE(ios(16.1), macos(13.0), watchos(9.1), tvos(16.1))
@interface MTRWriteParams : NSObject <NSCopying>
@interface MTRWriteParams : NSObject <NSCopying, NSSecureCoding>

/**
* Controls whether the write is a timed write.
Expand Down Expand Up @@ -109,7 +109,7 @@ MTR_AVAILABLE(ios(16.1), macos(13.0), watchos(9.1), tvos(16.1))
* treated as if a default-initialized object was passed in.
*/
MTR_AVAILABLE(ios(16.1), macos(13.0), watchos(9.1), tvos(16.1))
@interface MTRReadParams : NSObject <NSCopying>
@interface MTRReadParams : NSObject <NSCopying, NSSecureCoding>

/**
* Whether the read/subscribe is fabric-filtered. The default is YES.
Expand Down Expand Up @@ -152,7 +152,7 @@ MTR_AVAILABLE(ios(16.1), macos(13.0), watchos(9.1), tvos(16.1))
* treated as if a default-initialized object was passed in.
*/
MTR_AVAILABLE(ios(16.1), macos(13.0), watchos(9.1), tvos(16.1))
@interface MTRSubscribeParams : MTRReadParams
@interface MTRSubscribeParams : MTRReadParams <NSCopying, NSSecureCoding>

/**
* Whether the subscribe should replace already-existing
Expand Down
123 changes: 123 additions & 0 deletions src/darwin/Framework/CHIP/MTRCluster.mm
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@ - (instancetype)init
return self;
}

#pragma mark - Copying

- (id)copyWithZone:(NSZone * _Nullable)zone
{
auto other = [[MTRWriteParams alloc] init];
Expand All @@ -77,6 +79,38 @@ - (id)copyWithZone:(NSZone * _Nullable)zone
return other;
}

#pragma mark - Coding

+ (BOOL)supportsSecureCoding
{
return YES;
}

static NSString * sTimedWriteTimeoutCodingKey = @"sTimedWriteTimeoutKey";
static NSString * sDataVersionCodingKey = @"sDataVersionKey";

- (nullable instancetype)initWithCoder:(NSCoder *)decoder
{
self = [super init];

if (self == nil) {
return nil;
}

self.timedWriteTimeout = [decoder decodeObjectOfClass:[NSNumber class] forKey:sTimedWriteTimeoutCodingKey];
self.dataVersion = [decoder decodeObjectOfClass:[NSNumber class] forKey:sDataVersionCodingKey];

return self;
}

- (void)encodeWithCoder:(NSCoder *)coder
{
if (self.timedWriteTimeout)
[coder encodeObject:self.timedWriteTimeout forKey:sTimedWriteTimeoutCodingKey];
if (self.dataVersion)
[coder encodeObject:self.dataVersion forKey:sDataVersionCodingKey];
}

@end

@implementation MTRReadParams
Expand All @@ -89,6 +123,43 @@ - (instancetype)init
return self;
}

#pragma mark - Coding

+ (BOOL)supportsSecureCoding
{
return YES;
}

static NSString * sFilterByFabricCoderKey = @"sFilterByFabricKey";
static NSString * sMinEventNumberCoderKey = @"sMinEventNumberKey";
static NSString * sAssumeUnknownAttributesReportableCoderKey = @"sAssumeUnknownAttributesReportableKey";

- (nullable instancetype)initWithCoder:(NSCoder *)decoder
{
self = [super init];

if (self == nil) {
return nil;
}

self.filterByFabric = [decoder decodeBoolForKey:sFilterByFabricCoderKey];
self.assumeUnknownAttributesReportable = [decoder decodeBoolForKey:sAssumeUnknownAttributesReportableCoderKey];
self.minEventNumber = [decoder decodeObjectOfClass:[NSNumber class] forKey:sMinEventNumberCoderKey];

return self;
}

- (void)encodeWithCoder:(NSCoder *)coder
{
[coder encodeBool:self.filterByFabric forKey:sFilterByFabricCoderKey];
[coder encodeBool:self.assumeUnknownAttributesReportable forKey:sAssumeUnknownAttributesReportableCoderKey];

if (self.minEventNumber)
[coder encodeObject:self.minEventNumber forKey:sMinEventNumberCoderKey];
}

#pragma mark - Copying

- (id)copyWithZone:(NSZone * _Nullable)zone
{
auto other = [[MTRReadParams alloc] init];
Expand All @@ -98,6 +169,8 @@ - (id)copyWithZone:(NSZone * _Nullable)zone
return other;
}

#pragma mark - Other

- (void)toReadPrepareParams:(chip::app::ReadPrepareParams &)readPrepareParams
{
readPrepareParams.mIsFabricFiltered = self.filterByFabric;
Expand All @@ -121,18 +194,68 @@ - (instancetype)initWithMinInterval:(NSNumber *)minInterval maxInterval:(NSNumbe
return self;
}

#pragma mark - Copying
- (id)copyWithZone:(NSZone * _Nullable)zone
{
auto other = [[MTRSubscribeParams alloc] initWithMinInterval:self.minInterval maxInterval:self.maxInterval];

other.filterByFabric = self.filterByFabric;
other.minEventNumber = self.minEventNumber;
other.assumeUnknownAttributesReportable = self.assumeUnknownAttributesReportable;
other.replaceExistingSubscriptions = self.replaceExistingSubscriptions;
other.reportEventsUrgently = self.reportEventsUrgently;
other.resubscribeAutomatically = self.resubscribeAutomatically;
other.minInterval = self.minInterval;
other.maxInterval = self.maxInterval;

return other;
}

#pragma mark - Coding
+ (BOOL)supportsSecureCoding
{
return YES;
}

static NSString * sReplaceExistingSubscriptionsCoderKey = @"sFilterByFabricKey";
static NSString * sReportEventsUrgentlyCoderKey = @"sMinEventNumberKey";
static NSString * sResubscribeAutomaticallyCoderKey = @"sAssumeUnknownAttributesReportableKey";
static NSString * sMinIntervalKeyCoderKey = @"sMinIntervalKeyKey";
static NSString * sMaxIntervalKeyCoderKey = @"sMaxIntervalKeyKey";

- (nullable instancetype)initWithCoder:(NSCoder *)decoder
{
self = [super initWithCoder:decoder];

if (self == nil) {
return nil;
}

self.replaceExistingSubscriptions = [decoder decodeBoolForKey:sReplaceExistingSubscriptionsCoderKey];
self.reportEventsUrgently = [decoder decodeBoolForKey:sReportEventsUrgentlyCoderKey];
self.resubscribeAutomatically = [decoder decodeBoolForKey:sResubscribeAutomaticallyCoderKey];
self.minInterval = [decoder decodeObjectOfClass:[NSNumber class] forKey:sMinIntervalKeyCoderKey];
self.maxInterval = [decoder decodeObjectOfClass:[NSNumber class] forKey:sMaxIntervalKeyCoderKey];

return self;
}

- (void)encodeWithCoder:(NSCoder *)coder
{
[super encodeWithCoder:coder];

[coder encodeBool:self.replaceExistingSubscriptions forKey:sReplaceExistingSubscriptionsCoderKey];
[coder encodeBool:self.reportEventsUrgently forKey:sReportEventsUrgentlyCoderKey];
[coder encodeBool:self.resubscribeAutomatically forKey:sResubscribeAutomaticallyCoderKey];

if (self.minInterval)
[coder encodeObject:self.minInterval forKey:sMinIntervalKeyCoderKey];
if (self.maxInterval)
[coder encodeObject:self.maxInterval forKey:sMaxIntervalKeyCoderKey];
}

#pragma mark - Main

- (void)toReadPrepareParams:(chip::app::ReadPrepareParams &)readPrepareParams
{
[super toReadPrepareParams:readPrepareParams];
Expand Down
47 changes: 47 additions & 0 deletions src/darwin/Framework/CHIP/MTRDefines_Internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,3 +64,50 @@ typedef struct {} variable_hidden_by_mtr_hide;

// Default timed interaction timeout, in ms, if another one is not provided.
#define MTR_DEFAULT_TIMED_INTERACTION_TIMEOUT_MS 10000

#pragma mark - XPC Defines

#define MTR_SIMPLE_REMOTE_XPC_GETTER(XPC_CONNECTION, NAME, TYPE, DEFAULT_VALUE, GETTER_NAME, PREFIX) \
\
-(TYPE) NAME \
{ \
__block TYPE outValue = DEFAULT_VALUE; \
\
NSXPCConnection * xpcConnection = XPC_CONNECTION; \
\
[[xpcConnection synchronousRemoteObjectProxyWithErrorHandler:^(NSError * _Nonnull error) { \
MTR_LOG_ERROR("Error: %@", error); \
}] PREFIX \
GETTER_NAME:^(TYPE returnValue) { \
outValue = returnValue; \
}]; \
\
return outValue; \
}

#define MTR_SIMPLE_REMOTE_XPC_COMMAND(XPC_CONNECTION, METHOD_SIGNATURE, ADDITIONAL_ARGUMENTS, PREFIX) \
\
-(void) METHOD_SIGNATURE \
{ \
NSXPCConnection * xpcConnection = XPC_CONNECTION; \
\
[[xpcConnection synchronousRemoteObjectProxyWithErrorHandler:^(NSError * _Nonnull error) { \
MTR_LOG_ERROR("Error: %@", error); \
}] PREFIX ADDITIONAL_ARGUMENTS]; \
}

#define MTR_COMPLEX_REMOTE_XPC_GETTER(XPC_CONNECTION, SIGNATURE, TYPE, DEFAULT_VALUE, ADDITIONAL_ARGUMENTS, PREFIX) \
-(TYPE) SIGNATURE \
{ \
__block TYPE outValue = DEFAULT_VALUE; \
\
NSXPCConnection * xpcConnection = XPC_CONNECTION; \
\
[[xpcConnection synchronousRemoteObjectProxyWithErrorHandler:^(NSError * _Nonnull error) { \
MTR_LOG_ERROR("Error: %@", error); \
}] PREFIX ADDITIONAL_ARGUMENTS:^(TYPE returnValue) { \
outValue = returnValue; \
}]; \
\
return outValue; \
}
7 changes: 6 additions & 1 deletion src/darwin/Framework/CHIP/MTRDeviceController.mm
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
#import "MTRDeviceControllerLocalTestStorage.h"
#import "MTRDeviceControllerStartupParams.h"
#import "MTRDeviceControllerStartupParams_Internal.h"
#import "MTRDeviceController_XPC.h"
#import "MTRDevice_Concrete.h"
#import "MTRDevice_Internal.h"
#import "MTRError_Internal.h"
Expand Down Expand Up @@ -144,7 +145,11 @@ - (instancetype)initForSubclasses

- (nullable MTRDeviceController *)initWithParameters:(MTRDeviceControllerAbstractParameters *)parameters error:(NSError * __autoreleasing *)error
{
if (![parameters isKindOfClass:MTRDeviceControllerParameters.class]) {
if ([parameters isKindOfClass:MTRXPCDeviceControllerParameters.class]) {
MTRXPCDeviceControllerParameters * resolvedParameters = (MTRXPCDeviceControllerParameters *) parameters;
MTR_LOG("Starting up with XPC Device Controller Parameters: %@", parameters);
return [[MTRDeviceController_XPC alloc] initWithUniqueIdentifier:resolvedParameters.uniqueIdentifier xpConnectionBlock:resolvedParameters.xpcConnectionBlock];
} else if (![parameters isKindOfClass:MTRDeviceControllerParameters.class]) {
MTR_LOG_ERROR("Unsupported type of MTRDeviceControllerAbstractParameters: %@", parameters);
if (error) {
*error = [MTRError errorForCHIPErrorCode:CHIP_ERROR_INVALID_ARGUMENT];
Expand Down
22 changes: 22 additions & 0 deletions src/darwin/Framework/CHIP/MTRDeviceControllerParameters.h
Original file line number Diff line number Diff line change
Expand Up @@ -145,4 +145,26 @@ MTR_AVAILABLE(ios(17.6), macos(14.6), watchos(10.6), tvos(17.6))

@end

MTR_NEWLY_AVAILABLE
@interface MTRXPCDeviceControllerParameters : MTRDeviceControllerAbstractParameters

- (instancetype)init NS_UNAVAILABLE;
+ (instancetype)new NS_UNAVAILABLE;

/**
* A controller created from this way will connect to a remote instance of an MTRDeviceController loaded in an XPC Service
*
* @param xpcConnectionBlock The XPC Connection block that will return an NSXPCConnection to the indended listener.
*
* @param uniqueIdentifier The unique id to assign to the controller.
*
*/
- (instancetype)initWithXPConnectionBlock:(NSXPCConnection * (^)(void) )xpcConnectionBlock
uniqueIdentifier:(NSUUID *)uniqueIdentifier;

@property (atomic, readonly, retain) NSUUID * uniqueIdentifier MTR_NEWLY_AVAILABLE;
@property (readonly, strong, nonatomic) NSXPCConnection * (^xpcConnectionBlock)(void) MTR_NEWLY_AVAILABLE;

@end

NS_ASSUME_NONNULL_END
17 changes: 17 additions & 0 deletions src/darwin/Framework/CHIP/MTRDeviceControllerStartupParams.mm
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,23 @@ - (instancetype)initWithStorageDelegate:(id<MTRDeviceControllerStorageDelegate>)
}
@end

@implementation MTRXPCDeviceControllerParameters

@synthesize uniqueIdentifier = _uniqueIdentifier;
@synthesize xpcConnectionBlock = _xpcConnectionBlock;

- (instancetype)initWithXPConnectionBlock:(NSXPCConnection * (^)(void) )xpcConnectionBlock
uniqueIdentifier:(NSUUID *)uniqueIdentifier;
{
if (self = [super _initInternal]) {
_xpcConnectionBlock = [xpcConnectionBlock copy];
_uniqueIdentifier = [uniqueIdentifier copy];
}

return self;
}
@end

@implementation MTRDeviceControllerStartupParamsInternal

- (instancetype)initWithParams:(MTRDeviceControllerStartupParams *)params
Expand Down
31 changes: 31 additions & 0 deletions src/darwin/Framework/CHIP/MTRDeviceControllerXPCParameters.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/**
* Copyright (c) 2024 Project CHIP Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#import <Matter/Matter.h>

NS_ASSUME_NONNULL_BEGIN

@interface MTRDeviceControllerXPCParameters : MTRDeviceControllerParameters
@end

@interface MTRDeviceControllerMachServiceXPCParameters : MTRDeviceControllerXPCParameters

@property (atomic, retain) NSString * machServiceName;
@property (atomic, readwrite) NSXPCConnectionOptions connectionOptions;

@end

NS_ASSUME_NONNULL_END
35 changes: 35 additions & 0 deletions src/darwin/Framework/CHIP/MTRDeviceControllerXPCParameters.mm
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/**
* Copyright (c) 2024 Project CHIP Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#import "MTRDeviceControllerXPCParameters.h"

@implementation MTRDeviceControllerXPCParameters

+ (BOOL)supportsSecureCoding
{
return YES;
}

@end

@implementation MTRDeviceControllerMachServiceXPCParameters

+ (BOOL)supportsSecureCoding
{
return YES;
}

@end
Loading
Loading