Skip to content

Commit

Permalink
Add a config define for Darwin per-controller storage. (#29037)
Browse files Browse the repository at this point in the history
It's not quite ready yet; make sure we don't accidentally ship it.
  • Loading branch information
bzbarsky-apple authored and pull[bot] committed Mar 26, 2024
1 parent 7960e84 commit 56c1253
Show file tree
Hide file tree
Showing 18 changed files with 149 additions and 3 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/darwin.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,8 @@ jobs:
# -enableUndefinedBehaviorSanitizer instruments the code in Matter.framework,
# but to instrument the code in the underlying libCHIP we need to pass CHIP_IS_UBSAN=YES
TEST_RUNNER_ASAN_OPTIONS=__CURRENT_VALUE__:detect_stack_use_after_return=1 xcodebuild test -target "Matter" -scheme "Matter Framework Tests" -sdk macosx -enableAddressSanitizer YES -enableUndefinedBehaviorSanitizer YES OTHER_CFLAGS='${inherited} -Werror -Wconversion -Wno-unguarded-availability-new' CHIP_IS_UBSAN=YES CHIP_IS_BLE=NO > >(tee /tmp/darwin/framework-tests/darwin-tests-asan.log) 2> >(tee /tmp/darwin/framework-tests/darwin-tests-asan-err.log >&2)
# And the same thing, but with MTR_PER_CONTROLLER_STORAGE_ENABLED turned on.
TEST_RUNNER_ASAN_OPTIONS=__CURRENT_VALUE__:detect_stack_use_after_return=1 xcodebuild test -target "Matter" -scheme "Matter Framework Tests" -sdk macosx -enableAddressSanitizer YES -enableUndefinedBehaviorSanitizer YES OTHER_CFLAGS='${inherited} -Werror -Wconversion -Wno-unguarded-availability-new' CHIP_IS_UBSAN=YES CHIP_IS_BLE=NO GCC_PREPROCESSOR_DEFINITIONS='${inherited} MTR_PER_CONTROLLER_STORAGE_ENABLED=1' > >(tee /tmp/darwin/framework-tests/darwin-tests-asan-provisional.log) 2> >(tee /tmp/darwin/framework-tests/darwin-tests-asan-provisional-err.log >&2)
# -enableThreadSanitizer instruments the code in Matter.framework,
# but to instrument the code in the underlying libCHIP we need to pass CHIP_IS_TSAN=YES
xcodebuild test -target "Matter" -scheme "Matter Framework Tests" -sdk macosx -enableThreadSanitizer YES OTHER_CFLAGS='${inherited} -Werror -Wconversion -Wno-unguarded-availability-new' CHIP_IS_TSAN=YES CHIP_IS_BLE=NO > >(tee /tmp/darwin/framework-tests/darwin-tests-tsan.log) 2> >(tee /tmp/darwin/framework-tests/darwin-tests-tsan-err.log >&2)
Expand Down
10 changes: 9 additions & 1 deletion src/darwin/Framework/CHIP/MTRDefines.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,20 @@
#define MTR_NEWLY_AVAILABLE
#endif

#if defined(MTR_ENABLE_PROVISIONAL) && MTR_ENABLE_PROVISIONAL
#if !defined(MTR_ENABLE_PROVISIONAL)
#define MTR_ENABLE_PROVISIONAL 0
#endif

#if MTR_ENABLE_PROVISIONAL
#define MTR_PROVISIONALLY_AVAILABLE MTR_NEWLY_AVAILABLE
#else
#define MTR_PROVISIONALLY_AVAILABLE NS_UNAVAILABLE MTR_HIDDEN
#endif

#ifndef MTR_PER_CONTROLLER_STORAGE_ENABLED
#define MTR_PER_CONTROLLER_STORAGE_ENABLED 0
#endif

#pragma mark - Types

typedef NSData * MTRTLVBytes;
Expand Down
2 changes: 2 additions & 0 deletions src/darwin/Framework/CHIP/MTRDeviceController.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,12 @@ typedef void (^MTRDeviceConnectionCallback)(MTRBaseDevice * _Nullable device, NS
*/
@property (readonly, nonatomic, getter=isRunning) BOOL running;

#if MTR_PER_CONTROLLER_STORAGE_ENABLED
/**
* The ID assigned to this controller at creation time.
*/
@property (readonly, nonatomic) NSUUID * uniqueIdentifier MTR_NEWLY_AVAILABLE;
#endif // MTR_PER_CONTROLLER_STORAGE_ENABLED

/**
* Return the Node ID assigned to the controller. Will return nil if the
Expand Down
4 changes: 4 additions & 0 deletions src/darwin/Framework/CHIP/MTRDeviceControllerDataStore.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,11 @@
#import <Foundation/Foundation.h>
#import <Matter/MTRDefines.h>
#import <Matter/MTRDeviceController.h>
#if MTR_PER_CONTROLLER_STORAGE_ENABLED
#import <Matter/MTRDeviceControllerStorageDelegate.h>
#else
#import "MTRDeviceControllerStorageDelegate_Wrapper.h"
#endif // MTR_PER_CONTROLLER_STORAGE_ENABLED

#include <lib/core/CHIPError.h>

Expand Down
10 changes: 10 additions & 0 deletions src/darwin/Framework/CHIP/MTRDeviceControllerFactory.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,12 @@

#import <Foundation/Foundation.h>
#import <Matter/MTRCertificates.h>
#import <Matter/MTRDefines.h>
#if MTR_PER_CONTROLLER_STORAGE_ENABLED
#import <Matter/MTRDeviceControllerStartupParameters.h>
#else
@class MTRDeviceControllerStartupParameters;
#endif // MTR_PER_CONTROLLER_STORAGE_ENABLED

NS_ASSUME_NONNULL_BEGIN

Expand Down Expand Up @@ -93,12 +98,15 @@ API_AVAILABLE(ios(16.4), macos(13.3), watchos(9.4), tvos(16.4))
*/
- (instancetype)initWithStorage:(id<MTRStorage>)storage;

#if MTR_PER_CONTROLLER_STORAGE_ENABLED
/*
* Initialize the device controller factory without storage. In this mode,
* device controllers will need to have per-controller storage provided to allow
* storing controller-specific information.
*/
- (instancetype)init MTR_NEWLY_AVAILABLE;
#endif // MTR_PER_CONTROLLER_STORAGE_ENABLED

@end

API_AVAILABLE(ios(16.4), macos(13.3), watchos(9.4), tvos(16.4))
Expand Down Expand Up @@ -180,6 +188,7 @@ API_AVAILABLE(ios(16.4), macos(13.3), watchos(9.4), tvos(16.4))
- (MTRDeviceController * _Nullable)createControllerOnNewFabric:(MTRDeviceControllerStartupParams *)startupParams
error:(NSError * __autoreleasing *)error;

#if MTR_PER_CONTROLLER_STORAGE_ENABLED
/**
* Create an MTRDeviceController. Returns nil on failure.
*
Expand All @@ -191,6 +200,7 @@ API_AVAILABLE(ios(16.4), macos(13.3), watchos(9.4), tvos(16.4))
*/
- (MTRDeviceController * _Nullable)createController:(MTRDeviceControllerStartupParameters *)startupParameters
error:(NSError * __autoreleasing *)error MTR_NEWLY_AVAILABLE;
#endif // MTR_PER_CONTROLLER_STORAGE_ENABLED

@end

Expand Down
3 changes: 3 additions & 0 deletions src/darwin/Framework/CHIP/MTRDeviceControllerFactory.mm
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@
#import "MTRPersistentStorageDelegateBridge.h"
#import "MTRSessionResumptionStorageBridge.h"
#import "NSDataSpanConversion.h"
#if !MTR_PER_CONTROLLER_STORAGE_ENABLED
#import "MTRDeviceControllerStartupParameters_Wrapper.h"
#endif // MTR_PER_CONTROLLER_STORAGE_ENABLED

#import <os/lock.h>

Expand Down
16 changes: 15 additions & 1 deletion src/darwin/Framework/CHIP/MTRDeviceControllerStartupParameters.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,22 @@
*/

#import <Foundation/Foundation.h>

#import <Matter/MTRDefines.h>

#if defined(MTR_INTERNAL_INCLUDE) && defined(MTR_INCLUDED_FROM_UMBRELLA_HEADER)
#error Internal includes should not happen from the umbrella header
#endif

#if MTR_PER_CONTROLLER_STORAGE_ENABLED || defined(MTR_INTERNAL_INCLUDE)

#import <Matter/MTRDeviceControllerStorageDelegate.h>
#import <Matter/MTROTAProviderDelegate.h>

NS_ASSUME_NONNULL_BEGIN

#if !MTR_PER_CONTROLLER_STORAGE_ENABLED
MTR_HIDDEN
#endif
MTR_NEWLY_AVAILABLE
@interface MTRDeviceControllerStartupParameters : NSObject

Expand Down Expand Up @@ -68,6 +77,9 @@ MTR_NEWLY_AVAILABLE

@end

#if !MTR_PER_CONTROLLER_STORAGE_ENABLED
MTR_HIDDEN
#endif
MTR_NEWLY_AVAILABLE
@interface MTRDeviceControllerExternalCertificateStartupParameters : MTRDeviceControllerStartupParameters

Expand Down Expand Up @@ -119,3 +131,5 @@ MTR_NEWLY_AVAILABLE
@end

NS_ASSUME_NONNULL_END

#endif // MTR_PER_CONTROLLER_STORAGE_ENABLED || defined(MTR_INTERNAL_INCLUDE)
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/**
* Copyright (c) 2023 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.
*/

#include <Matter/MTRDefines.h>

#if MTR_PER_CONTROLLER_STORAGE_ENABLED
#error Should be including Matter/MTRDeviceControllerStartupParameters.h
#endif // MTR_PER_CONTROLLER_STORAGE_ENABLED

#define MTR_INTERNAL_INCLUDE
#import <Matter/MTRDeviceControllerStartupParameters.h>
#undef MTR_INTERNAL_INCLUDE
4 changes: 4 additions & 0 deletions src/darwin/Framework/CHIP/MTRDeviceControllerStartupParams.mm
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,11 @@
#import "MTRP256KeypairBridge.h"
#import "NSDataSpanConversion.h"

#if MTR_PER_CONTROLLER_STORAGE_ENABLED
#import <Matter/MTRDeviceControllerStorageDelegate.h>
#else
#import "MTRDeviceControllerStorageDelegate_Wrapper.h"
#endif // MTR_PER_CONTROLLER_STORAGE_ENABLED

#include <controller/OperationalCredentialsDelegate.h>
#include <credentials/CHIPCert.h>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,11 @@
#import <Foundation/Foundation.h>
#import <Matter/MTRDefines.h>
#import <Matter/MTRDeviceController.h>
#if MTR_PER_CONTROLLER_STORAGE_ENABLED
#import <Matter/MTRDeviceControllerStartupParameters.h>
#else
#import "MTRDeviceControllerStartupParameters_Wrapper.h"
#endif // MTR_PER_CONTROLLER_STORAGE_ENABLED

#include <crypto/CHIPCryptoPAL.h>
#include <lib/core/DataModelTypes.h>
Expand Down
16 changes: 15 additions & 1 deletion src/darwin/Framework/CHIP/MTRDeviceControllerStorageDelegate.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,15 @@
*/

#import <Foundation/Foundation.h>
#import <Matter/MTRDefines.h>
#import <Matter/MTRDeviceController.h>

#if defined(MTR_INTERNAL_INCLUDE) && defined(MTR_INCLUDED_FROM_UMBRELLA_HEADER)
#error Internal includes should not happen from the umbrella header
#endif

#if MTR_PER_CONTROLLER_STORAGE_ENABLED || defined(MTR_INTERNAL_INCLUDE)

NS_ASSUME_NONNULL_BEGIN

typedef NS_ENUM(NSUInteger, MTRStorageSecurityLevel) {
Expand Down Expand Up @@ -105,6 +112,13 @@ MTR_NEWLY_AVAILABLE
@end

// TODO: FIXME: Is this a sane place to put this API?
MTR_EXTERN MTR_NEWLY_AVAILABLE NSSet<Class> * MTRDeviceControllerStorageClasses(void);
#if MTR_PER_CONTROLLER_STORAGE_ENABLED
MTR_EXTERN
#else
MTR_HIDDEN
#endif // MTR_PER_CONTROLLER_STORAGE_ENABLED
MTR_NEWLY_AVAILABLE NSSet<Class> * MTRDeviceControllerStorageClasses(void);

NS_ASSUME_NONNULL_END

#endif // MTR_PER_CONTROLLER_STORAGE_ENABLED || defined(MTR_INTERNAL_INCLUDE)
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/**
* Copyright (c) 2023 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.
*/

#include <Matter/MTRDefines.h>

#if MTR_PER_CONTROLLER_STORAGE_ENABLED
#error Should be including Matter/MTRDeviceControllerStorageDelegate.h
#endif // MTR_PER_CONTROLLER_STORAGE_ENABLED

#define MTR_INTERNAL_INCLUDE
#import <Matter/MTRDeviceControllerStorageDelegate.h>
#undef MTR_INTERNAL_INCLUDE
11 changes: 11 additions & 0 deletions src/darwin/Framework/CHIP/MTRDeviceController_Internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,11 @@
#import "MTRDeviceControllerDataStore.h"

#import <Matter/MTRDeviceControllerStartupParams.h>
#if MTR_PER_CONTROLLER_STORAGE_ENABLED
#import <Matter/MTRDeviceControllerStorageDelegate.h>
#else
#import "MTRDeviceControllerStorageDelegate_Wrapper.h"
#endif // MTR_PER_CONTROLLER_STORAGE_ENABLED
#import <Matter/MTROTAProviderDelegate.h>

@class MTRDeviceControllerStartupParamsInternal;
Expand All @@ -51,6 +55,13 @@ NS_ASSUME_NONNULL_BEGIN

@interface MTRDeviceController ()

#if !MTR_PER_CONTROLLER_STORAGE_ENABLED
/**
* The ID assigned to this controller at creation time.
*/
@property (readonly, nonatomic) NSUUID * uniqueIdentifier MTR_NEWLY_AVAILABLE;
#endif // MTR_PER_CONTROLLER_STORAGE_ENABLED

#pragma mark - MTRDeviceControllerFactory methods

/**
Expand Down
4 changes: 4 additions & 0 deletions src/darwin/Framework/CHIP/Matter.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@

#import <Foundation/Foundation.h>

#define MTR_INCLUDED_FROM_UMBRELLA_HEADER

#import <Matter/MTRAsyncCallbackWorkQueue.h>
#import <Matter/MTRBackwardsCompatShims.h>
#import <Matter/MTRBaseClusters.h>
Expand Down Expand Up @@ -57,3 +59,5 @@
#import <Matter/MTRStorage.h>
#import <Matter/MTRStructsObjc.h>
#import <Matter/MTRThreadOperationalDataset.h>

#undef MTR_INCLUDED_FROM_UMBRELLA_HEADER
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
#import "MTRTestKeys.h"
#import "MTRTestPerControllerStorage.h"

#if MTR_PER_CONTROLLER_STORAGE_ENABLED

static const uint16_t kTestVendorId = 0xFFF1u;
static const uint16_t kTimeoutInSeconds = 3;

Expand Down Expand Up @@ -295,3 +297,5 @@ - (void)test001_CheckAdvertisingAsExpected
}

@end

#endif // MTR_PER_CONTROLLER_STORAGE_ENABLED
4 changes: 4 additions & 0 deletions src/darwin/Framework/CHIPTests/MTRPerControllerStorageTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
#import "MTRTestPerControllerStorage.h"
#import "MTRTestResetCommissioneeHelper.h"

#if MTR_PER_CONTROLLER_STORAGE_ENABLED

static const uint16_t kPairingTimeoutInSeconds = 10;
static const uint16_t kTimeoutInSeconds = 3;
static NSString * kOnboardingPayload = @"MT:-24J0AFN00KA0648G00";
Expand Down Expand Up @@ -1055,3 +1057,5 @@ - (void)test007_TestMultipleControllers
}

@end

#endif // MTR_PER_CONTROLLER_STORAGE_ENABLED
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
#import <Foundation/Foundation.h>
#import <Matter/Matter.h>

#if MTR_PER_CONTROLLER_STORAGE_ENABLED

NS_ASSUME_NONNULL_BEGIN

@interface MTRTestPerControllerStorage : NSObject <MTRDeviceControllerStorageDelegate>
Expand All @@ -41,3 +43,5 @@ NS_ASSUME_NONNULL_BEGIN
@end

NS_ASSUME_NONNULL_END

#endif // MTR_PER_CONTROLLER_STORAGE_ENABLED
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@

#import "MTRTestPerControllerStorage.h"

#if MTR_PER_CONTROLLER_STORAGE_ENABLED

@interface MTRTestPerControllerStorage ()
@property (nonatomic, readonly) NSMutableDictionary<NSString *, NSData *> * storage;
@end
Expand Down Expand Up @@ -83,3 +85,5 @@ - (BOOL)controller:(MTRDeviceController *)controller
}

@end

#endif // MTR_PER_CONTROLLER_STORAGE_ENABLED

0 comments on commit 56c1253

Please sign in to comment.