Skip to content

Commit

Permalink
Add Darwin backwards-compat shims for the TestCluster rename. (projec…
Browse files Browse the repository at this point in the history
…t-chip#23611)

project-chip#23495 renamed "TestCluster"
to "UnitTesting", which changed a bunch of Darwin APIs.  This PR puts in place
backwards-compat shims to avoid API changes.  Specifically, compared to revision
9a41c9c (right before project-chip#23495 landed), the
changes to Darwin code end up looking like this:

1. MTRAttributeTLVValueDecoder.mm: internal-only changes to cluster ids.
2. MTRBaseClusters.h:
   * MTRBaseClusterTestCluster renamed to MTRBaseClusterUnitTesting, which is
     marked MTR_NEWLY_AVAILABLE.
   * Various modifications to methods on MTRBaseClusterUnitTesting that are
     marked MTR_NEWLY_AVAILABLE, so these changes are OK.
   * MTRBaseClusterTestCluster is added as a subclass of
     MTRBaseClusterUnitTesting.
   * The MTRBaseClusterTestCluster (Deprecated) bits are not changed at all.
   * The MTRUnitTesting enums/bitmaps are added as MTR_NEWLY_AVAILABLE.
   * The MTRTestCluster enums/bitmaps are marked as MTR_NEWLY_DEPRECATED.
3. MTRBaseClusters.mm: The various (Deprecated) bits get shims on the
   completion handlers to handle the fact that for MTRBaseClusterTestCluster
   (Deprecated) the types in the completions are TestCluster but we want to
   call things that expect UnitTesting types, so we have to cast between them.
   We could restrict these shims to just TestCluster with enough work, but it
   does not seem worth it.
4. MTRBaseClusters_internal.h: just follows the renaming from MTRBaseClusters.h
5. MTRCallbackBridge.mm: just follows the renamings of the various struct types,
   commands, etc..
6. MTRCallbackBridge_internal.h: just follows the renaming of the various
   struct types.
7. MTRClusterConstants.h: marks the old constants as MTR_NEWLY_DEPRECATED and
   adds the new constants as MTR_NEWLY_AVAILABLE.
8. MTRClusters.h:
   * MTRClusterTestCluster renamed to MTRClusterUnitTesting, which is marked
     MTR_NEWLY_AVAILABLE.
   * Various modifications to methods on MTRClusterUnitTesting that are marked
     MTR_NEWLY_AVAILABLE, so these changes are OK.
   * MTRClusterTestCluster is added as a subclass of MTRClusterUnitTesting.
   * The MTRClusterTestCluster (Deprecated) bits are not changed at all.
9. MTRClusters.mm: The various (Deprecated) bits get shims on the
   completion handlers, like for MTRBaseClusters.mm above.
10. MTRClusters_internal.h: Just follows the renaming from MTRClusters.h
11. MTRCommandPayloadsObjc.h:
    * Renames payload structs to UnitTesting, marks them MTR_NEWLY_AVAILABLE
    * Adds subclasses of the payload structs with the old name that
      are marked as MTR_NEWLY_DEPRECATED and have no extra selectors.
    * Changes types of struct members of payloads to have the UnitTesting types.
12. MTRCommandPayloadsObjc.mm: just follows the header changes.
13. MTREventTLVValueDecoder.mm: Internal changes to ID names.
14. MTRStructsObjc.h:
    * Renames structs defined in the unit testing cluster to UnitTesting names,
      marks them MTR_NEWLY_AVAILABLE.
    * Adds subclasses of the structs with the old name that are marked as
      MTR_NEWLY_DEPRECATED and have no extra selectors.
15. MTRStructsObjc.mm: just follows the header changes.
  • Loading branch information
bzbarsky-apple authored and adbridge committed Nov 18, 2022
1 parent 3302741 commit 7d0f91f
Show file tree
Hide file tree
Showing 19 changed files with 15,630 additions and 3,953 deletions.
53 changes: 40 additions & 13 deletions src/darwin/Framework/CHIP/templates/MTRBaseClusters-src.zapt
Original file line number Diff line number Diff line change
Expand Up @@ -237,18 +237,32 @@ reportHandler:(void (^)({{asObjectiveCClass type parent.name}} * _Nullable value
{{/chip_server_cluster_attributes}}

@end
{{#if (isStrEqual (asUpperCamelCase name) "UnitTesting")}}

@implementation MTRBaseCluster{{asUpperCamelCase name}} (Deprecated)
@implementation MTRBaseClusterTestCluster
@end
{{/if}}

@implementation MTRBaseCluster{{compatClusterNameRemapping name}} (Deprecated)

{{#chip_cluster_commands}}
- (void){{asLowerCamelCase name}}WithParams:(MTR{{asUpperCamelCase parent.name}}Cluster{{asUpperCamelCase name}}Params * {{#unless (commandHasRequiredField .)}}_Nullable{{/unless}})params completionHandler:({{>command_completion_type command=.}})completionHandler
- (void){{asLowerCamelCase name}}WithParams:(MTR{{compatClusterNameRemapping parent.name}}Cluster{{asUpperCamelCase name}}Params * {{#unless (commandHasRequiredField .)}}_Nullable{{/unless}})params completionHandler:({{>command_completion_type command=. compatRemapClusterName=true}})completionHandler
{
[self {{asLowerCamelCase name}}WithParams:params completion:completionHandler];
[self {{asLowerCamelCase name}}WithParams:params completion:
{{#if hasSpecificResponse}}
^(MTR{{asUpperCamelCase parent.name}}Cluster{{asUpperCamelCase responseName}}Params * _Nullable data, NSError * _Nullable error) {
// Cast is safe because subclass does not add any selectors.
completionHandler(static_cast<MTR{{compatClusterNameRemapping parent.name}}Cluster{{asUpperCamelCase responseName}}Params *>(data), error);
}
{{else}}
completionHandler
{{/if}}
];
}
{{#unless (hasArguments)}}
- (void){{asLowerCamelCase name}}WithCompletionHandler:({{>command_completion_type command=.}})completionHandler
- (void){{asLowerCamelCase name}}WithCompletionHandler:({{>command_completion_type command=. compatRemapClusterName=true}})completionHandler
{
[self {{asLowerCamelCase name}}WithParams:nil completion:completionHandler];
[self {{asLowerCamelCase name}}WithParams:nil completionHandler:completionHandler];
}
{{/unless}}
{{/chip_cluster_commands}}
Expand All @@ -263,24 +277,28 @@ reportHandler:(void (^)({{asObjectiveCClass type parent.name}} * _Nullable value
{{~else~}}
CompletionHandler:
{{~/if_is_fabric_scoped_struct~}}
(void (^)({{asObjectiveCClass type parent.name}} * _Nullable value, NSError * _Nullable error))completionHandler
(void (^)({{asObjectiveCClass type parent.name compatRemapClusterName=true}} * _Nullable value, NSError * _Nullable error))completionHandler
{
[self readAttribute{{asUpperCamelCase name}}With{{#if_is_fabric_scoped_struct type}}Params:params completion:{{else}}Completion:{{/if_is_fabric_scoped_struct}}completionHandler];
[self readAttribute{{asUpperCamelCase name}}With{{#if_is_fabric_scoped_struct type}}Params:params completion:{{else}}Completion:{{/if_is_fabric_scoped_struct}}
^({{asObjectiveCClass type parent.name}} * _Nullable value, NSError * _Nullable error) {
// Cast is safe because subclass does not add any selectors.
completionHandler(static_cast<{{asObjectiveCClass type parent.name compatRemapClusterName=true}} *>(value), error);
}];
}
{{#if isWritableAttribute}}
- (void)write{{>attribute}}WithValue:({{asObjectiveCType type parent.name}})value completionHandler:(MTRStatusCompletion)completionHandler
- (void)write{{>attribute}}WithValue:({{asObjectiveCType type parent.name compatRemapClusterName=true}})value completionHandler:(MTRStatusCompletion)completionHandler
{
[self writeAttribute{{asUpperCamelCase name}}WithValue:value params:nil completion:completionHandler];
}
- (void)write{{>attribute}}WithValue:({{asObjectiveCType type parent.name}})value params:(MTRWriteParams * _Nullable)params completionHandler:(MTRStatusCompletion)completionHandler
- (void)write{{>attribute}}WithValue:({{asObjectiveCType type parent.name compatRemapClusterName=true}})value params:(MTRWriteParams * _Nullable)params completionHandler:(MTRStatusCompletion)completionHandler
{
[self writeAttribute{{asUpperCamelCase name}}WithValue:value params:params completion:completionHandler];
}
{{/if}}
{{#if isReportableAttribute}}
- (void) subscribe{{>attribute}}WithMinInterval:(NSNumber * _Nonnull)minInterval maxInterval:(NSNumber * _Nonnull)maxInterval
params:(MTRSubscribeParams * _Nullable)params
subscriptionEstablished:(MTRSubscriptionEstablishedHandler _Nullable)subscriptionEstablishedHandler reportHandler:(void (^)({{asObjectiveCClass type parent.name}} * _Nullable value, NSError * _Nullable error))reportHandler
subscriptionEstablished:(MTRSubscriptionEstablishedHandler _Nullable)subscriptionEstablishedHandler reportHandler:(void (^)({{asObjectiveCClass type parent.name compatRemapClusterName=true}} * _Nullable value, NSError * _Nullable error))reportHandler
{
MTRSubscribeParams * _Nullable subscribeParams = [params copy];
if (subscribeParams == nil) {
Expand All @@ -289,11 +307,19 @@ subscriptionEstablished:(MTRSubscriptionEstablishedHandler _Nullable)subscriptio
subscribeParams.minInterval = minInterval;
subscribeParams.maxInterval = maxInterval;
}
[self subscribeAttribute{{asUpperCamelCase name}}WithParams:subscribeParams subscriptionEstablished:subscriptionEstablishedHandler reportHandler:reportHandler];
[self subscribeAttribute{{asUpperCamelCase name}}WithParams:subscribeParams subscriptionEstablished:subscriptionEstablishedHandler reportHandler:
^({{asObjectiveCClass type parent.name}} * _Nullable value, NSError * _Nullable error) {
// Cast is safe because subclass does not add any selectors.
reportHandler(static_cast<{{asObjectiveCClass type parent.name compatRemapClusterName=true}} *>(value), error) ;
}];
}
+ (void) read{{>attribute}}WithAttributeCache:(MTRAttributeCacheContainer *)attributeCacheContainer endpoint:(NSNumber *)endpoint queue:(dispatch_queue_t)queue completionHandler:(void (^)({{asObjectiveCClass type parent.name}} * _Nullable value, NSError * _Nullable error))completionHandler
+ (void) read{{>attribute}}WithAttributeCache:(MTRAttributeCacheContainer *)attributeCacheContainer endpoint:(NSNumber *)endpoint queue:(dispatch_queue_t)queue completionHandler:(void (^)({{asObjectiveCClass type parent.name compatRemapClusterName=true}} * _Nullable value, NSError * _Nullable error))completionHandler
{
[self readAttribute{{asUpperCamelCase name}}WithAttributeCache:attributeCacheContainer endpoint:endpoint queue:queue completion:completionHandler];
[self readAttribute{{asUpperCamelCase name}}WithAttributeCache:attributeCacheContainer endpoint:endpoint queue:queue completion:
^({{asObjectiveCClass type parent.name}} * _Nullable value, NSError * _Nullable error) {
// Cast is safe because subclass does not add any selectors.
completionHandler(static_cast<{{asObjectiveCClass type parent.name compatRemapClusterName=true}} *>(value), error);
}];
}
{{/if}}
{{/chip_server_cluster_attributes}}
Expand All @@ -308,4 +334,5 @@ subscriptionEstablished:(MTRSubscriptionEstablishedHandler _Nullable)subscriptio
@end

{{/chip_client_clusters}}

// NOLINTEND(clang-analyzer-cplusplus.NewDeleteLeaks)
58 changes: 45 additions & 13 deletions src/darwin/Framework/CHIP/templates/MTRBaseClusters.zapt
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ NS_ASSUME_NONNULL_BEGIN
* Cluster {{name}}
* {{description}}
*/
{{#if (isStrEqual (asUpperCamelCase name) "UnitTesting")}}
MTR_NEWLY_AVAILABLE
{{/if}}
@interface MTRBaseCluster{{asUpperCamelCase name}} : MTRCluster

- (instancetype _Nullable)initWithDevice:(MTRBaseDevice *)device
Expand Down Expand Up @@ -60,19 +63,37 @@ subscriptionEstablished:(MTRSubscriptionEstablishedHandler _Nullable)subscriptio

{{/chip_client_clusters}}

MTR_NEWLY_DEPRECATED("Please use MTRBaseClusterUnitTesting")
@interface MTRBaseClusterTestCluster : MTRBaseClusterUnitTesting
@end

{{#zcl_clusters}}
{{#zcl_enums}}
typedef NS_ENUM({{asUnderlyingZclType name}}, {{objCEnumName ../name label}}) {
{{#*inline "enumDef"}}
typedef NS_ENUM({{asUnderlyingZclType name}}, {{objCEnumName clusterName label}}) {
{{#zcl_enum_items}}
{{objCEnumName ../../name ../label}}{{objCEnumItemLabel label}} = {{asHex value 2}},
{{objCEnumName ../clusterName ../label}}{{objCEnumItemLabel label}} = {{asHex value 2}},
{{/zcl_enum_items}}
};
}
{{#if (isStrEqual (asUpperCamelCase clusterName) "UnitTesting")}}
MTR_NEWLY_AVAILABLE
{{else if (isStrEqual (asUpperCamelCase clusterName) "TestCluster")}}
MTR_NEWLY_DEPRECATED("Please use {{objCEnumName "UnitTesting" label}}")
{{/if}}
;
{{/inline}}
{{> enumDef name=name clusterName=../name label=label}}

{{#if (isStrEqual (asUpperCamelCase ../name) "UnitTesting")}}
{{> enumDef name=name clusterName="TestCluster" label=label}}

{{/if}}
{{/zcl_enums}}
{{#zcl_bitmaps}}
typedef NS_OPTIONS({{asUnderlyingZclType name}}, {{objCEnumName ../name label}}) {
{{#*inline "bitmapDef"}}
typedef NS_OPTIONS({{asUnderlyingZclType name}}, {{objCEnumName clusterName label}}) {
{{#zcl_bitmap_items}}
{{objCEnumName ../../name ../label}}{{objCEnumItemLabel label}} = {{asHex mask}},
{{objCEnumName ../clusterName ../label}}{{objCEnumItemLabel label}} = {{asHex mask}},
{{/zcl_bitmap_items}}
}
{{! TODO: We need a better setup for the API_AVALABLE annotations here; this does not scale at all sanely. }}
Expand All @@ -86,23 +107,34 @@ API_AVAILABLE(ios(16.2), macos(13.1), watchos(9.2), tvos(16.2))
API_AVAILABLE(ios(16.2), macos(13.1), watchos(9.2), tvos(16.2))
{{/if}}
{{/if}}
{{#if (isStrEqual (asUpperCamelCase clusterName) "UnitTesting")}}
MTR_NEWLY_AVAILABLE
{{else if (isStrEqual (asUpperCamelCase clusterName) "TestCluster")}}
MTR_NEWLY_DEPRECATED("Please use {{objCEnumName "UnitTesting" label}}")
{{/if}}
;
{{/inline}}
{{> bitmapDef name=name clusterName=../name label=label}}

{{#if (isStrEqual (asUpperCamelCase ../name) "UnitTesting")}}
{{> bitmapDef name=name clusterName="TestCluster" label=label}}

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

{{#chip_client_clusters includeAll=true}}
@interface MTRBaseCluster{{asUpperCamelCase name}} (Deprecated)
@interface MTRBaseCluster{{compatClusterNameRemapping name}} (Deprecated)

- (nullable instancetype)initWithDevice:(MTRBaseDevice *)device
endpoint:(uint16_t)endpoint
queue:(dispatch_queue_t)queue MTR_NEWLY_DEPRECATED("Please use initWithDevice:endpointID:queue:");

{{#chip_cluster_commands}}
- (void){{asLowerCamelCase name}}WithParams:(MTR{{asUpperCamelCase parent.name}}Cluster{{asUpperCamelCase name}}Params * {{#unless (commandHasRequiredField .)}}_Nullable{{/unless}})params completionHandler:({{>command_completion_type command=.}})completionHandler
- (void){{asLowerCamelCase name}}WithParams:(MTR{{compatClusterNameRemapping parent.name}}Cluster{{asUpperCamelCase name}}Params * {{#unless (commandHasRequiredField .)}}_Nullable{{/unless}})params completionHandler:({{>command_completion_type command=. compatRemapClusterName=true}})completionHandler
MTR_NEWLY_DEPRECATED("Please use {{asLowerCamelCase name}}WithParams:completion:");
{{#unless (hasArguments)}}
- (void){{asLowerCamelCase name}}WithCompletionHandler:({{>command_completion_type command=.}})completionHandler
- (void){{asLowerCamelCase name}}WithCompletionHandler:({{>command_completion_type command=. compatRemapClusterName=true}})completionHandler
MTR_NEWLY_DEPRECATED("Please use {{asLowerCamelCase name}}WithCompletion:");
{{/unless}}
{{/chip_cluster_commands}}
Expand All @@ -117,21 +149,21 @@ API_AVAILABLE(ios(16.2), macos(13.1), watchos(9.2), tvos(16.2))
{{~else~}}
CompletionHandler:
{{~/if_is_fabric_scoped_struct~}}
(void (^)({{asObjectiveCClass type parent.name}} * _Nullable value, NSError * _Nullable error))completionHandler {{#if (isStrEqual (asUpperCamelCase parent.name) "TestCluster")}}{{#if (isStrEqual (asUpperCamelCase name) "WriteOnlyInt8u")}}API_AVAILABLE(ios(16.2), macos(13.1), watchos(9.2), tvos(16.2)){{/if}}{{/if}}
(void (^)({{asObjectiveCClass type parent.name compatRemapClusterName=true}} * _Nullable value, NSError * _Nullable error))completionHandler {{#if (isStrEqual (asUpperCamelCase parent.name) "UnitTesting")}}{{#if (isStrEqual (asUpperCamelCase name) "WriteOnlyInt8u")}}API_AVAILABLE(ios(16.2), macos(13.1), watchos(9.2), tvos(16.2)){{/if}}{{/if}}
MTR_NEWLY_DEPRECATED("Please use read{{>attribute}}With{{#if_is_fabric_scoped_struct type}}Params:completion:{{else}}Completion:{{/if_is_fabric_scoped_struct}}");
{{#if isWritableAttribute}}
- (void)write{{>attribute}}WithValue:({{asObjectiveCType type parent.name}})value completionHandler:(MTRStatusCompletion)completionHandler {{#if (isStrEqual (asUpperCamelCase parent.name) "TestCluster")}}{{#if (isStrEqual (asUpperCamelCase name) "WriteOnlyInt8u")}}API_AVAILABLE(ios(16.2), macos(13.1), watchos(9.2), tvos(16.2)){{/if}}{{/if}}
- (void)write{{>attribute}}WithValue:({{asObjectiveCType type parent.name compatRemapClusterName=true}})value completionHandler:(MTRStatusCompletion)completionHandler {{#if (isStrEqual (asUpperCamelCase parent.name) "UnitTesting")}}{{#if (isStrEqual (asUpperCamelCase name) "WriteOnlyInt8u")}}API_AVAILABLE(ios(16.2), macos(13.1), watchos(9.2), tvos(16.2)){{/if}}{{/if}}
MTR_NEWLY_DEPRECATED("Please use write{{>attribute}}WithValue:completion:");
- (void)write{{>attribute}}WithValue:({{asObjectiveCType type parent.name}})value params:(MTRWriteParams * _Nullable)params completionHandler:(MTRStatusCompletion)completionHandler {{#if (isStrEqual (asUpperCamelCase parent.name) "TestCluster")}}{{#if (isStrEqual (asUpperCamelCase name) "WriteOnlyInt8u")}}API_AVAILABLE(ios(16.2), macos(13.1), watchos(9.2), tvos(16.2)){{/if}}{{/if}}
- (void)write{{>attribute}}WithValue:({{asObjectiveCType type parent.name compatRemapClusterName=true}})value params:(MTRWriteParams * _Nullable)params completionHandler:(MTRStatusCompletion)completionHandler {{#if (isStrEqual (asUpperCamelCase parent.name) "UnitTesting")}}{{#if (isStrEqual (asUpperCamelCase name) "WriteOnlyInt8u")}}API_AVAILABLE(ios(16.2), macos(13.1), watchos(9.2), tvos(16.2)){{/if}}{{/if}}
MTR_NEWLY_DEPRECATED("Please use write{{>attribute}}WithValue:params:completion:");
{{/if}}
{{#if isReportableAttribute}}
{{! TODO: We need a better setup for the API_AVALABLE annotations here; this does not scale at all sanely. }}
- (void) subscribe{{>attribute}}WithMinInterval:(NSNumber * _Nonnull)minInterval maxInterval:(NSNumber * _Nonnull)maxInterval
params:(MTRSubscribeParams * _Nullable)params
subscriptionEstablished:(MTRSubscriptionEstablishedHandler _Nullable)subscriptionEstablishedHandler reportHandler:(void (^)({{asObjectiveCClass type parent.name}} * _Nullable value, NSError * _Nullable error))reportHandler {{#if (isStrEqual (asUpperCamelCase parent.name) "TestCluster")}}{{#if (isStrEqual (asUpperCamelCase name) "WriteOnlyInt8u")}}API_AVAILABLE(ios(16.2), macos(13.1), watchos(9.2), tvos(16.2)){{/if}}{{/if}}
subscriptionEstablished:(MTRSubscriptionEstablishedHandler _Nullable)subscriptionEstablishedHandler reportHandler:(void (^)({{asObjectiveCClass type parent.name compatRemapClusterName=true}} * _Nullable value, NSError * _Nullable error))reportHandler {{#if (isStrEqual (asUpperCamelCase parent.name) "UnitTesting")}}{{#if (isStrEqual (asUpperCamelCase name) "WriteOnlyInt8u")}}API_AVAILABLE(ios(16.2), macos(13.1), watchos(9.2), tvos(16.2)){{/if}}{{/if}}
MTR_NEWLY_DEPRECATED("Please use subscribe{{>attribute}}WithParams:subscriptionEstablished:");
+ (void) read{{>attribute}}WithAttributeCache:(MTRAttributeCacheContainer *)attributeCacheContainer endpoint:(NSNumber *)endpoint queue:(dispatch_queue_t)queue completionHandler:(void (^)({{asObjectiveCClass type parent.name}} * _Nullable value, NSError * _Nullable error))completionHandler {{#if (isStrEqual (asUpperCamelCase parent.name) "TestCluster")}}{{#if (isStrEqual (asUpperCamelCase name) "WriteOnlyInt8u")}}API_AVAILABLE(ios(16.2), macos(13.1), watchos(9.2), tvos(16.2)){{/if}}{{/if}}
+ (void) read{{>attribute}}WithAttributeCache:(MTRAttributeCacheContainer *)attributeCacheContainer endpoint:(NSNumber *)endpoint queue:(dispatch_queue_t)queue completionHandler:(void (^)({{asObjectiveCClass type parent.name compatRemapClusterName=true}} * _Nullable value, NSError * _Nullable error))completionHandler {{#if (isStrEqual (asUpperCamelCase parent.name) "UnitTesting")}}{{#if (isStrEqual (asUpperCamelCase name) "WriteOnlyInt8u")}}API_AVAILABLE(ios(16.2), macos(13.1), watchos(9.2), tvos(16.2)){{/if}}{{/if}}
MTR_NEWLY_DEPRECATED("Please use read{{>attribute}}WithAttributeCache:endpoint:queue:completion:");
{{/if}}
{{/chip_server_cluster_attributes}}
Expand Down
Loading

0 comments on commit 7d0f91f

Please sign in to comment.