Skip to content

Commit

Permalink
Add a common base for Darwin MTR*Path interfaces. (project-chip#23490)
Browse files Browse the repository at this point in the history
This is a re-landing of PR project-chip#22591.

This change should be backwards-compatible with the existing API.
  • Loading branch information
bzbarsky-apple authored and adbridge committed Nov 17, 2022
1 parent 1a8e327 commit 3ecb083
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 22 deletions.
36 changes: 29 additions & 7 deletions src/darwin/Framework/CHIP/MTRBaseDevice.h
Original file line number Diff line number Diff line change
Expand Up @@ -298,9 +298,26 @@ extern NSString * const MTRArrayValueType;

@end

@interface MTRAttributePath : NSObject <NSCopying>
/**
* A path indicating a specific cluster on a device (i.e. without any
* wildcards).
*/
MTR_NEWLY_AVAILABLE
@interface MTRClusterPath : NSObject <NSCopying>
@property (nonatomic, readonly, copy) NSNumber * endpoint;
@property (nonatomic, readonly, copy) NSNumber * cluster;

+ (instancetype)clusterPathWithEndpointID:(NSNumber *)endpointID clusterID:(NSNumber *)clusterID;

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

/**
* A path indicating a specific attribute on a device (i.e. without any
* wildcards).
*/
@interface MTRAttributePath : MTRClusterPath <NSCopying>
@property (nonatomic, readonly, copy) NSNumber * attribute;

+ (instancetype)attributePathWithEndpointID:(NSNumber *)endpointID
Expand All @@ -311,9 +328,12 @@ extern NSString * const MTRArrayValueType;
+ (instancetype)new NS_UNAVAILABLE;
@end

@interface MTREventPath : NSObject
@property (nonatomic, readonly, copy) NSNumber * endpoint;
@property (nonatomic, readonly, copy) NSNumber * cluster;
/**
* A path indicating a specific event that can be emitted on a device
* (i.e. without any wildcards). There can be multiple instances of actual
* events for a given event path.
*/
@interface MTREventPath : MTRClusterPath <NSCopying>
@property (nonatomic, readonly, copy) NSNumber * event;

+ (instancetype)eventPathWithEndpointID:(NSNumber *)endpointID
Expand All @@ -324,9 +344,11 @@ extern NSString * const MTRArrayValueType;
+ (instancetype)new NS_UNAVAILABLE;
@end

@interface MTRCommandPath : NSObject
@property (nonatomic, readonly, copy) NSNumber * endpoint;
@property (nonatomic, readonly, copy) NSNumber * cluster;
/**
* A path indicating a specific command on a device (i.e. without any
* wildcards).
*/
@interface MTRCommandPath : MTRClusterPath <NSCopying>
@property (nonatomic, readonly, copy) NSNumber * command;

+ (instancetype)commandPathWithEndpointID:(NSNumber *)endpointID
Expand Down
72 changes: 57 additions & 15 deletions src/darwin/Framework/CHIP/MTRBaseDevice.mm
Original file line number Diff line number Diff line change
Expand Up @@ -1558,12 +1558,59 @@ - (void)deregisterReportHandlersWithClientQueue:(dispatch_queue_t)queue completi

@end

@implementation MTRAttributePath
- (instancetype)initWithPath:(const ConcreteDataAttributePath &)path
@implementation MTRClusterPath
- (instancetype)initWithPath:(const ConcreteClusterPath &)path
{
if (self = [super init]) {
_endpoint = @(path.mEndpointId);
_cluster = @(path.mClusterId);
}
return self;
}

- (NSString *)description
{
return [NSString stringWithFormat:@"<MTRClusterPath> endpoint %u cluster %u", (uint16_t) _endpoint.unsignedShortValue,
(uint32_t) _cluster.unsignedLongValue];
}

+ (instancetype)clusterPathWithEndpointID:(NSNumber *)endpointID clusterID:(NSNumber *)clusterID
{
ConcreteClusterPath path(static_cast<chip::EndpointId>([endpointID unsignedShortValue]),
static_cast<chip::ClusterId>([clusterID unsignedLongValue]));

return [[MTRClusterPath alloc] initWithPath:path];
}

- (BOOL)isEqualToClusterPath:(MTRClusterPath *)clusterPath
{
return [_endpoint isEqualToNumber:clusterPath.endpoint] && [_cluster isEqualToNumber:clusterPath.cluster];
}

- (BOOL)isEqual:(id)object
{
if (![object isKindOfClass:[self class]]) {
return NO;
}
return [self isEqualToClusterPath:object];
}

- (NSUInteger)hash
{
return _endpoint.unsignedShortValue ^ _cluster.unsignedLongValue;
}

- (id)copyWithZone:(NSZone *)zone
{
return [MTRClusterPath clusterPathWithEndpointID:_endpoint clusterID:_cluster];
}

@end

@implementation MTRAttributePath
- (instancetype)initWithPath:(const ConcreteDataAttributePath &)path
{
if (self = [super initWithPath:path]) {
_attribute = @(path.mAttributeId);
}
return self;
Expand All @@ -1572,7 +1619,7 @@ - (instancetype)initWithPath:(const ConcreteDataAttributePath &)path
- (NSString *)description
{
return [NSString stringWithFormat:@"<MTRAttributePath> endpoint %u cluster %u attribute %u",
(uint16_t) _endpoint.unsignedShortValue, (uint32_t) _cluster.unsignedLongValue,
(uint16_t) self.endpoint.unsignedShortValue, (uint32_t) self.cluster.unsignedLongValue,
(uint32_t) _attribute.unsignedLongValue];
}

Expand All @@ -1589,8 +1636,7 @@ ConcreteDataAttributePath path(static_cast<chip::EndpointId>([endpointID unsigne

- (BOOL)isEqualToAttributePath:(MTRAttributePath *)attributePath
{
return [_endpoint isEqualToNumber:attributePath.endpoint] && [_cluster isEqualToNumber:attributePath.cluster] &&
[_attribute isEqualToNumber:attributePath.attribute];
return [self isEqualToClusterPath:attributePath] && [_attribute isEqualToNumber:attributePath.attribute];
}

- (BOOL)isEqual:(id)object
Expand All @@ -1603,12 +1649,12 @@ - (BOOL)isEqual:(id)object

- (NSUInteger)hash
{
return _endpoint.unsignedShortValue ^ _cluster.unsignedLongValue ^ _attribute.unsignedLongValue;
return self.endpoint.unsignedShortValue ^ self.cluster.unsignedLongValue ^ _attribute.unsignedLongValue;
}

- (id)copyWithZone:(NSZone *)zone
{
return [MTRAttributePath attributePathWithEndpointID:_endpoint clusterID:_cluster attributeID:_attribute];
return [MTRAttributePath attributePathWithEndpointID:self.endpoint clusterID:self.cluster attributeID:_attribute];
}
@end

Expand All @@ -1624,9 +1670,7 @@ + (instancetype)attributePathWithEndpointId:(NSNumber *)endpointId
@implementation MTREventPath
- (instancetype)initWithPath:(const ConcreteEventPath &)path
{
if (self = [super init]) {
_endpoint = @(path.mEndpointId);
_cluster = @(path.mClusterId);
if (self = [super initWithPath:path]) {
_event = @(path.mEventId);
}
return self;
Expand All @@ -1642,7 +1686,7 @@ ConcreteEventPath path(static_cast<chip::EndpointId>([endpointID unsignedShortVa

- (id)copyWithZone:(NSZone *)zone
{
return [MTREventPath eventPathWithEndpointID:_endpoint clusterID:_cluster eventID:_event];
return [MTREventPath eventPathWithEndpointID:self.endpoint clusterID:self.cluster eventID:_event];
}
@end

Expand All @@ -1656,9 +1700,7 @@ + (instancetype)eventPathWithEndpointId:(NSNumber *)endpointId clusterId:(NSNumb
@implementation MTRCommandPath
- (instancetype)initWithPath:(const ConcreteCommandPath &)path
{
if (self = [super init]) {
_endpoint = @(path.mEndpointId);
_cluster = @(path.mClusterId);
if (self = [super initWithPath:path]) {
_command = @(path.mCommandId);
}
return self;
Expand All @@ -1674,7 +1716,7 @@ ConcreteCommandPath path(static_cast<chip::EndpointId>([endpointID unsignedShort

- (id)copyWithZone:(NSZone *)zone
{
return [MTRCommandPath commandPathWithEndpointID:_endpoint clusterID:_cluster commandID:_command];
return [MTRCommandPath commandPathWithEndpointID:self.endpoint clusterID:self.cluster commandID:_command];
}
@end

Expand Down
4 changes: 4 additions & 0 deletions src/darwin/Framework/CHIP/MTRBaseDevice_Internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,10 @@ NS_ASSUME_NONNULL_BEGIN

@end

@interface MTRClusterPath ()
- (instancetype)initWithPath:(const chip::app::ConcreteClusterPath &)path;
@end

@interface MTRAttributePath ()
- (instancetype)initWithPath:(const chip::app::ConcreteDataAttributePath &)path;
@end
Expand Down

0 comments on commit 3ecb083

Please sign in to comment.