Skip to content

Commit

Permalink
Clean up MTRClusters/MTRBaseClusters a bit: (#29241)
Browse files Browse the repository at this point in the history
* Clean up MTRClusters/MTRBaseClusters a bit:

1) Introduce MTRGenericBaseCluster/MTRGenericCluster base classes.
2) Move the same-for-every-type initializer implementation to the base class.
3) Move the class-specific initializer declaration (needed to get availability
   right) to a category so we don't get complaints about incomplete
   implementation.

* Address review comment.
  • Loading branch information
bzbarsky-apple authored and pull[bot] committed Mar 20, 2024
1 parent 67bf20c commit af11a23
Show file tree
Hide file tree
Showing 13 changed files with 2,111 additions and 4,481 deletions.
19 changes: 19 additions & 0 deletions src/darwin/Framework/CHIP/MTRCluster.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ typedef void (^MTRStatusCompletion)(NSError * _Nullable error);
typedef void (^MTRSubscriptionEstablishedHandler)(void);

@class MTRBaseDevice;
@class MTRDevice;

NS_ASSUME_NONNULL_BEGIN

Expand All @@ -46,6 +47,24 @@ NS_ASSUME_NONNULL_BEGIN
@property (nonatomic, readonly) NSNumber * endpointID NS_REFINED_FOR_SWIFT MTR_NEWLY_AVAILABLE;
@end

/**
* Base for all MTRBaseCluster* types.
*/
MTR_NEWLY_AVAILABLE
@interface MTRGenericBaseCluster : MTRCluster
@end

/**
* Base for all MTRCluster* types.
*/
MTR_NEWLY_AVAILABLE
@interface MTRGenericCluster : MTRCluster
/**
* The device this cluster object is associated with.
*/
@property (nonatomic, strong, readonly) MTRDevice * device;
@end

/**
* MTRWriteParams
* This is used to control the behavior of cluster writes.
Expand Down
24 changes: 24 additions & 0 deletions src/darwin/Framework/CHIP/MTRCluster.mm
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,30 @@ - (instancetype)initWithEndpointID:(NSNumber *)endpointID queue:(dispatch_queue_

@end

@implementation MTRGenericBaseCluster

- (instancetype)initWithDevice:(MTRBaseDevice *)device endpointID:(NSNumber *)endpointID queue:(dispatch_queue_t)queue
{
if (self = [super initWithEndpointID:endpointID queue:queue]) {
_device = device;
}
return self;
}

@end

@implementation MTRGenericCluster

- (instancetype)initWithDevice:(MTRDevice *)device endpointID:(NSNumber *)endpointID queue:(dispatch_queue_t)queue
{
if (self = [super initWithEndpointID:endpointID queue:queue]) {
_device = device;
}
return self;
}

@end

@implementation MTRWriteParams
- (instancetype)init
{
Expand Down
22 changes: 18 additions & 4 deletions src/darwin/Framework/CHIP/MTRCluster_Internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@

#import <Foundation/Foundation.h>

#import "MTRBaseDevice.h"
#import "MTRBaseDevice_Internal.h"
#import "MTRCluster.h"
#import <Matter/MTRBaseDevice.h>
#import <Matter/MTRCluster.h>
#import <Matter/MTRDevice.h>

#import "zap-generated/MTRBaseClusters.h"
#import "MTRBaseDevice_Internal.h"

#include <app/ReadPrepareParams.h>
#include <lib/core/DataModelTypes.h>
Expand All @@ -35,6 +35,20 @@ NS_ASSUME_NONNULL_BEGIN
- (instancetype)initWithEndpointID:(NSNumber *)endpointID queue:(dispatch_queue_t)queue;
@end

@interface MTRGenericBaseCluster ()
- (instancetype)initWithDevice:(MTRBaseDevice *)device
endpointID:(NSNumber *)endpointID
queue:(dispatch_queue_t)queue;

@property (nonatomic, strong, readonly) MTRBaseDevice * device;
@end

@interface MTRGenericCluster ()
- (instancetype)initWithDevice:(MTRDevice *)device
endpointID:(NSNumber *)endpointID
queue:(dispatch_queue_t)queue;
@end

@interface MTRReadParams ()
/**
* Copy state from this MTRReadParams to the ReadPreparaParams.
Expand Down
12 changes: 0 additions & 12 deletions src/darwin/Framework/CHIP/templates/MTRBaseClusters-src.zapt
Original file line number Diff line number Diff line change
Expand Up @@ -49,18 +49,6 @@ public:
{{#if (isSupported (asUpperCamelCase name preserveAcronyms=true))}}
@implementation MTRBaseCluster{{asUpperCamelCase name preserveAcronyms=true}}

- (instancetype)initWithDevice:(MTRBaseDevice *)device endpointID:(NSNumber *)endpointID queue:(dispatch_queue_t)queue
{
if (self = [super initWithEndpointID:endpointID queue:queue]) {
if (device == nil) {
return nil;
}

_device = device;
}
return self;
}

{{#zcl_commands}}
{{#if (is_str_equal source 'client')}}
{{! Takes two arguments: cluster name and command name, plus the ambient state where the command is "this" }}
Expand Down
22 changes: 13 additions & 9 deletions src/darwin/Framework/CHIP/templates/MTRBaseClusters.zapt
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,7 @@ NS_ASSUME_NONNULL_BEGIN
* {{description}}
*/
{{availability (asUpperCamelCase name preserveAcronyms=true)}}
@interface MTRBaseCluster{{asUpperCamelCase name preserveAcronyms=true}} : MTRCluster

/**
* For all instance methods (reads, writes, commands) that take a completion,
* the completion will be called on the provided queue.
*/
- (instancetype _Nullable)initWithDevice:(MTRBaseDevice *)device
endpointID:(NSNumber *)endpointID
queue:(dispatch_queue_t)queue NS_DESIGNATED_INITIALIZER {{availability (asUpperCamelCase name preserveAcronyms=true) minimalRelease="First major API revamp"}};
@interface MTRBaseCluster{{asUpperCamelCase name preserveAcronyms=true}} : MTRGenericBaseCluster

{{#zcl_commands}}
{{#if (is_str_equal source 'client')}}
Expand Down Expand Up @@ -81,6 +73,18 @@ subscriptionEstablished:(MTRSubscriptionEstablishedHandler _Nullable)subscriptio
- (instancetype)init NS_UNAVAILABLE;
+ (instancetype)new NS_UNAVAILABLE;

@end

@interface MTRBaseCluster{{asUpperCamelCase name preserveAcronyms=true}} (Availability)

/**
* For all instance methods (reads, writes, commands) that take a completion,
* the completion will be called on the provided queue.
*/
- (instancetype _Nullable)initWithDevice:(MTRBaseDevice *)device
endpointID:(NSNumber *)endpointID
queue:(dispatch_queue_t)queue {{availability (asUpperCamelCase name preserveAcronyms=true) minimalRelease="First major API revamp"}};

@end
{{/if}}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,5 @@
#import "MTRBaseClusters.h"
#import "MTRBaseDevice.h"

{{#zcl_clusters}}

{{#if (isSupported (asUpperCamelCase name preserveAcronyms=true))}}
@interface MTRBaseCluster{{asUpperCamelCase name preserveAcronyms=true}} ()
@property (nonatomic, strong, readonly) MTRBaseDevice * device;
@end
{{/if}}

{{/zcl_clusters}}
// Nothing here for now, but leaving this file in place in case we need to add
// something.
15 changes: 1 addition & 14 deletions src/darwin/Framework/CHIP/templates/MTRClusters-src.zapt
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
{{> header excludeZapComment=true}}

#import <Foundation/Foundation.h>
#import <Matter/MTRError.h>

#import "MTRClusterConstants.h"
#import "MTRClusters_Internal.h"
#import "MTRDevice_Internal.h"
#import "MTRCluster_Internal.h"
#import "MTRClusters_Internal.h"
#import "MTRStructsObjc.h"
#import "MTRCommandPayloadsObjc.h"
#import "MTRLogging_Internal.h"
Expand All @@ -31,18 +30,6 @@ using chip::System::Clock::Seconds16;
{{#if (isSupported (asUpperCamelCase name preserveAcronyms=true))}}
@implementation MTRCluster{{asUpperCamelCase name preserveAcronyms=true}}

- (instancetype)initWithDevice:(MTRDevice *)device endpointID:(NSNumber *)endpointID queue:(dispatch_queue_t)queue
{
if (self = [super initWithEndpointID:endpointID queue:queue]) {
if (device == nil) {
return nil;
}

_device = device;
}
return self;
}

{{#zcl_commands}}
{{! Takes two arguments: cluster name and command name, plus the ambient state where the command is "this" }}
{{#*inline "commandImpl"}}
Expand Down
41 changes: 20 additions & 21 deletions src/darwin/Framework/CHIP/templates/MTRClusters.zapt
Original file line number Diff line number Diff line change
Expand Up @@ -17,27 +17,7 @@ NS_ASSUME_NONNULL_BEGIN
* {{description}}
*/
{{availability (asUpperCamelCase name preserveAcronyms=true)}}
@interface MTRCluster{{asUpperCamelCase name preserveAcronyms=true}} : MTRCluster

/**
{{#zcl_commands}}
{{#first}}
* For all instance methods that take a completion (i.e. command invocations),
* the completion will be called on the provided queue.
{{/first}}
{{else}}
* The queue is currently unused, but may be used in the future for calling completions
* for command invocations if commands are added to this cluster.
{{/zcl_commands}}
*/
- (instancetype _Nullable)initWithDevice:(MTRDevice *)device
endpointID:(NSNumber *)endpointID
queue:(dispatch_queue_t)queue NS_DESIGNATED_INITIALIZER {{availability (asUpperCamelCase name preserveAcronyms=true) minimalRelease="First major API revamp"}};

/**
* The device this cluster object is associated with.
*/
@property (nonatomic, readonly) MTRDevice * device {{availability (asUpperCamelCase name preserveAcronyms=true) minimalRelease="Future"}};
@interface MTRCluster{{asUpperCamelCase name preserveAcronyms=true}} : MTRGenericCluster

{{#zcl_commands}}
{{#if (is_str_equal source 'client')}}
Expand Down Expand Up @@ -84,6 +64,25 @@ NS_ASSUME_NONNULL_BEGIN

@end

@interface MTRCluster{{asUpperCamelCase name preserveAcronyms=true}} (Availability)

/**
{{#zcl_commands}}
{{#first}}
* For all instance methods that take a completion (i.e. command invocations),
* the completion will be called on the provided queue.
{{/first}}
{{else}}
* The queue is currently unused, but may be used in the future for calling completions
* for command invocations if commands are added to this cluster.
{{/zcl_commands}}
*/
- (instancetype _Nullable)initWithDevice:(MTRDevice *)device
endpointID:(NSNumber *)endpointID
queue:(dispatch_queue_t)queue {{availability (asUpperCamelCase name preserveAcronyms=true) minimalRelease="First major API revamp"}};

@end

{{/if}}
{{/zcl_clusters}}

Expand Down
Loading

0 comments on commit af11a23

Please sign in to comment.