From 65fb5331016d4681491f65c03a24a85adf5d3d7e Mon Sep 17 00:00:00 2001 From: Vivien Nicolas Date: Thu, 24 Oct 2024 11:24:38 +0200 Subject: [PATCH] [darwin-framework-tool] Add an optional parameter to the storage view command to only show the entries for a given commissioner --- .../commands/common/ControllerStorage.h | 1 + .../commands/common/ControllerStorage.mm | 14 ++++++++++++++ .../storage/StorageManagementCommand.h | 10 +++++++++- .../storage/StorageManagementCommand.mm | 19 +++++++++++++++---- 4 files changed, 39 insertions(+), 5 deletions(-) diff --git a/examples/darwin-framework-tool/commands/common/ControllerStorage.h b/examples/darwin-framework-tool/commands/common/ControllerStorage.h index f70081fee095a7..76811bcbb7e92e 100644 --- a/examples/darwin-framework-tool/commands/common/ControllerStorage.h +++ b/examples/darwin-framework-tool/commands/common/ControllerStorage.h @@ -44,6 +44,7 @@ extern NSString * const kDarwinFrameworkToolControllerDomain; - (NSData *)valueForKey:(NSString *)key; - (void)storeValue:(NSData *)value forKey:key; +- (void)print; @end NS_ASSUME_NONNULL_END diff --git a/examples/darwin-framework-tool/commands/common/ControllerStorage.mm b/examples/darwin-framework-tool/commands/common/ControllerStorage.mm index 058f00a6b302ae..785318ec71ee3b 100644 --- a/examples/darwin-framework-tool/commands/common/ControllerStorage.mm +++ b/examples/darwin-framework-tool/commands/common/ControllerStorage.mm @@ -143,6 +143,20 @@ - (void)storeValue:(NSData *)value forKey:key self.storage[controllerKey] = value; } +- (void)print +{ + NSLog(@"%@ (%@)", kDarwinFrameworkToolControllerDomain, _keyScopingPrefix); + for (NSString * controllerKey in self.storage) { + if (![self _isControllerScopedKey:controllerKey]) { + continue; + } + + __auto_type * key = [self _controllerScopedKeyToKey:controllerKey]; + __auto_type * data = [self.storage objectForKeyedSubscript:controllerKey]; + NSLog(@" * %@: %@", key, data); + } +} + - (NSString *)_keyToControllerScopedKey:(NSString *)key { return [NSString stringWithFormat:@"%@%@", _keyScopingPrefix, key]; diff --git a/examples/darwin-framework-tool/commands/storage/StorageManagementCommand.h b/examples/darwin-framework-tool/commands/storage/StorageManagementCommand.h index 8c3ed69184d742..35e111e92e7125 100644 --- a/examples/darwin-framework-tool/commands/storage/StorageManagementCommand.h +++ b/examples/darwin-framework-tool/commands/storage/StorageManagementCommand.h @@ -33,7 +33,15 @@ class StorageClearAll : public Command class StorageViewAll : public Command { public: - StorageViewAll() : Command("view-all") {} + StorageViewAll() : Command("view") + { + AddArgument("commissioner-name", &mCommissionerName, + "If specified, only the keys associated with the given commissioner will be displayed. Valid options are: " + "‘alpha’, ‘beta’, ‘gamma’."); + } CHIP_ERROR Run() override; + +private: + chip::Optional mCommissionerName; }; diff --git a/examples/darwin-framework-tool/commands/storage/StorageManagementCommand.mm b/examples/darwin-framework-tool/commands/storage/StorageManagementCommand.mm index 007d2821bda5a3..9e6e4b61f88a95 100644 --- a/examples/darwin-framework-tool/commands/storage/StorageManagementCommand.mm +++ b/examples/darwin-framework-tool/commands/storage/StorageManagementCommand.mm @@ -49,11 +49,22 @@ CHIP_ERROR StorageViewAll::Run() { - __auto_type * domains = GetDomains(); - for (NSString * domain in domains) { - __auto_type * storage = [[PreferencesStorage alloc] initWithDomain:domain]; - [storage print]; + if (!mCommissionerName.HasValue()) { + __auto_type * domains = GetDomains(); + for (NSString * domain in domains) { + __auto_type * storage = [[PreferencesStorage alloc] initWithDomain:domain]; + [storage print]; + } + + return CHIP_NO_ERROR; } + const char * commissionerName = mCommissionerName.Value(); + __auto_type * fabricId = CHIPCommandBridge::GetCommissionerFabricId(commissionerName); + __auto_type * uuidString = [NSString stringWithFormat:@"%@%@", @(kControllerIdPrefix), fabricId]; + __auto_type * controllerId = [[NSUUID alloc] initWithUUIDString:uuidString]; + __auto_type * storage = [[ControllerStorage alloc] initWithControllerID:controllerId]; + [storage print]; + return CHIP_NO_ERROR; }