From 104068670c4380bdb2654cedb3e529673bc39874 Mon Sep 17 00:00:00 2001 From: krypton36 Date: Wed, 18 May 2022 10:00:17 -0700 Subject: [PATCH] Add ability to remove storage to chip-tools (#18512) --- examples/chip-tool-darwin/BUILD.gn | 2 ++ .../common/CHIPCommandStorageDelegate.h | 1 + .../common/CHIPCommandStorageDelegate.mm | 36 ++++++++++++++++--- .../commands/storage/Commands.h | 31 ++++++++++++++++ .../storage/StorageManagementCommand.h | 31 ++++++++++++++++ .../storage/StorageManagementCommand.mm | 32 +++++++++++++++++ examples/chip-tool-darwin/main.mm | 3 ++ examples/chip-tool/BUILD.gn | 1 + .../chip-tool/commands/storage/Commands.h | 31 ++++++++++++++++ .../storage/StorageManagementCommand.cpp | 28 +++++++++++++++ .../storage/StorageManagementCommand.h | 29 +++++++++++++++ .../chip-tool/config/PersistentStorage.cpp | 9 +++++ examples/chip-tool/config/PersistentStorage.h | 3 ++ examples/chip-tool/main.cpp | 2 ++ 14 files changed, 235 insertions(+), 4 deletions(-) create mode 100644 examples/chip-tool-darwin/commands/storage/Commands.h create mode 100644 examples/chip-tool-darwin/commands/storage/StorageManagementCommand.h create mode 100644 examples/chip-tool-darwin/commands/storage/StorageManagementCommand.mm create mode 100644 examples/chip-tool/commands/storage/Commands.h create mode 100644 examples/chip-tool/commands/storage/StorageManagementCommand.cpp create mode 100644 examples/chip-tool/commands/storage/StorageManagementCommand.h diff --git a/examples/chip-tool-darwin/BUILD.gn b/examples/chip-tool-darwin/BUILD.gn index 8327e35cc6b031..890a97f43e42a7 100644 --- a/examples/chip-tool-darwin/BUILD.gn +++ b/examples/chip-tool-darwin/BUILD.gn @@ -48,6 +48,8 @@ executable("chip-tool-darwin") { "commands/pairing/Commands.h", "commands/pairing/PairingCommandBridge.mm", "commands/pairing/PairingDelegateBridge.mm", + "commands/storage/Commands.h", + "commands/storage/StorageManagementCommand.mm", "main.mm", ] diff --git a/examples/chip-tool-darwin/commands/common/CHIPCommandStorageDelegate.h b/examples/chip-tool-darwin/commands/common/CHIPCommandStorageDelegate.h index a2d12f11b8893d..e4e5e14a2011bd 100644 --- a/examples/chip-tool-darwin/commands/common/CHIPCommandStorageDelegate.h +++ b/examples/chip-tool-darwin/commands/common/CHIPCommandStorageDelegate.h @@ -7,6 +7,7 @@ NS_ASSUME_NONNULL_BEGIN - (nullable NSData *)storageDataForKey:(NSString *)key; - (BOOL)setStorageData:(NSData *)value forKey:(NSString *)key; - (BOOL)removeStorageDataForKey:(NSString *)key; +- (BOOL)deleteAllStorage; @end NS_ASSUME_NONNULL_END diff --git a/examples/chip-tool-darwin/commands/common/CHIPCommandStorageDelegate.mm b/examples/chip-tool-darwin/commands/common/CHIPCommandStorageDelegate.mm index 343601af187661..5d217ed56b4245 100644 --- a/examples/chip-tool-darwin/commands/common/CHIPCommandStorageDelegate.mm +++ b/examples/chip-tool-darwin/commands/common/CHIPCommandStorageDelegate.mm @@ -17,14 +17,43 @@ BOOL CHIPSetDomainValueForKey(NSString * domain, NSString * key, id value) return CFPreferencesAppSynchronize((CFStringRef) domain) == true; } -void CHIPRemoveDomainValueForKey(NSString * domain, NSString * key) +BOOL CHIPRemoveDomainValueForKey(NSString * domain, NSString * key) { CFPreferencesSetAppValue((CFStringRef) key, nullptr, (CFStringRef) domain); - CFPreferencesAppSynchronize((CFStringRef) domain); + return CFPreferencesAppSynchronize((CFStringRef) domain) == true; +} + +id CHIPGetDomainKeyList(NSString * domain) +{ + id value + = (id) CFBridgingRelease(CFPreferencesCopyKeyList((CFStringRef) domain, kCFPreferencesCurrentUser, kCFPreferencesAnyHost)); + if (value) { + return value; + } + return nil; +} + +BOOL CHIPClearAllDomain(NSString * domain) +{ + + NSArray * allKeys = CHIPGetDomainKeyList(domain); + NSLog(@"Removing keys: %@ %@", allKeys, domain); + for (id key in allKeys) { + NSLog(@"Removing key: %@", key); + if (!CHIPRemoveDomainValueForKey(domain, (NSString *) key)) { + return NO; + } + } + return YES; } @implementation CHIPToolPersistentStorageDelegate +- (BOOL)deleteAllStorage +{ + return CHIPClearAllDomain(kCHIPToolDefaultsDomain); +} + // MARK: CHIPPersistentStorageDelegate - (nullable NSData *)storageDataForKey:(NSString *)key @@ -44,8 +73,7 @@ - (BOOL)removeStorageDataForKey:(NSString *)key if (CHIPGetDomainValueForKey(kCHIPToolDefaultsDomain, key) == nil) { return NO; } - CHIPRemoveDomainValueForKey(kCHIPToolDefaultsDomain, key); - return YES; + return CHIPRemoveDomainValueForKey(kCHIPToolDefaultsDomain, key); } @end diff --git a/examples/chip-tool-darwin/commands/storage/Commands.h b/examples/chip-tool-darwin/commands/storage/Commands.h new file mode 100644 index 00000000000000..c53b324481a1ae --- /dev/null +++ b/examples/chip-tool-darwin/commands/storage/Commands.h @@ -0,0 +1,31 @@ +/* + * Copyright (c) 2020 Project CHIP Authors + * All rights reserved. + * + * 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. + * + */ + +#pragma once + +#include "StorageManagementCommand.h" +#include + +void registerCommandsStorage(Commands & commands) +{ + const char * clusterName = "storage"; + + commands_list clusterCommands = { make_unique() }; + + commands.Register(clusterName, clusterCommands); +} diff --git a/examples/chip-tool-darwin/commands/storage/StorageManagementCommand.h b/examples/chip-tool-darwin/commands/storage/StorageManagementCommand.h new file mode 100644 index 00000000000000..c2abddf18d0498 --- /dev/null +++ b/examples/chip-tool-darwin/commands/storage/StorageManagementCommand.h @@ -0,0 +1,31 @@ +/* + * Copyright (c) 2022 Project CHIP Authors + * All rights reserved. + * + * 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. + * + */ + +#pragma once + +#include "../common/CHIPCommandBridge.h" + +#include + +class StorageClearAll : public Command +{ +public: + StorageClearAll() : Command("clear-all") {} + + CHIP_ERROR Run() override; +}; diff --git a/examples/chip-tool-darwin/commands/storage/StorageManagementCommand.mm b/examples/chip-tool-darwin/commands/storage/StorageManagementCommand.mm new file mode 100644 index 00000000000000..c9cc0b87dca854 --- /dev/null +++ b/examples/chip-tool-darwin/commands/storage/StorageManagementCommand.mm @@ -0,0 +1,32 @@ +/* + * Copyright (c) 2022 Project CHIP Authors + * All rights reserved. + * + * 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 "../common/CHIPCommandStorageDelegate.h" + +#include "StorageManagementCommand.h" + +static CHIPToolPersistentStorageDelegate * storage = nil; + +CHIP_ERROR StorageClearAll::Run() +{ + storage = [[CHIPToolPersistentStorageDelegate alloc] init]; + if (![storage deleteAllStorage]) { + return CHIP_ERROR_INTERNAL; + } + return CHIP_NO_ERROR; +} diff --git a/examples/chip-tool-darwin/main.mm b/examples/chip-tool-darwin/main.mm index d5bfae7e74e648..1d78c781596fe4 100644 --- a/examples/chip-tool-darwin/main.mm +++ b/examples/chip-tool-darwin/main.mm @@ -20,6 +20,8 @@ #include "commands/pairing/Commands.h" +#include "commands/storage/Commands.h" + #include #include @@ -27,6 +29,7 @@ int main(int argc, const char * argv[]) { Commands commands; registerCommandsPairing(commands); + registerCommandsStorage(commands); registerCommandsTests(commands); registerClusters(commands); return commands.Run(argc, (char **) argv); diff --git a/examples/chip-tool/BUILD.gn b/examples/chip-tool/BUILD.gn index 1bd88ad2082426..c820b4a7cd0899 100644 --- a/examples/chip-tool/BUILD.gn +++ b/examples/chip-tool/BUILD.gn @@ -68,6 +68,7 @@ static_library("chip-tool-utils") { "commands/payload/SetupPayloadGenerateCommand.cpp", "commands/payload/SetupPayloadParseCommand.cpp", "commands/payload/SetupPayloadVerhoeff.cpp", + "commands/storage/StorageManagementCommand.cpp", "config/PersistentStorage.cpp", ] diff --git a/examples/chip-tool/commands/storage/Commands.h b/examples/chip-tool/commands/storage/Commands.h new file mode 100644 index 00000000000000..c53b324481a1ae --- /dev/null +++ b/examples/chip-tool/commands/storage/Commands.h @@ -0,0 +1,31 @@ +/* + * Copyright (c) 2020 Project CHIP Authors + * All rights reserved. + * + * 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. + * + */ + +#pragma once + +#include "StorageManagementCommand.h" +#include + +void registerCommandsStorage(Commands & commands) +{ + const char * clusterName = "storage"; + + commands_list clusterCommands = { make_unique() }; + + commands.Register(clusterName, clusterCommands); +} diff --git a/examples/chip-tool/commands/storage/StorageManagementCommand.cpp b/examples/chip-tool/commands/storage/StorageManagementCommand.cpp new file mode 100644 index 00000000000000..767e11c66cddf4 --- /dev/null +++ b/examples/chip-tool/commands/storage/StorageManagementCommand.cpp @@ -0,0 +1,28 @@ +/* + * Copyright (c) 2022 Project CHIP Authors + * All rights reserved. + * + * 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 "../../config/PersistentStorage.h" + +#include "StorageManagementCommand.h" + +CHIP_ERROR StorageClearAll::Run() +{ + PersistentStorage storage; + ReturnErrorOnFailure(storage.Init()); + return storage.SyncClearAll(); +} diff --git a/examples/chip-tool/commands/storage/StorageManagementCommand.h b/examples/chip-tool/commands/storage/StorageManagementCommand.h new file mode 100644 index 00000000000000..73b6d134a0ce0f --- /dev/null +++ b/examples/chip-tool/commands/storage/StorageManagementCommand.h @@ -0,0 +1,29 @@ +/* + * Copyright (c) 2022 Project CHIP Authors + * All rights reserved. + * + * 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. + * + */ + +#pragma once + +#include + +class StorageClearAll : public Command +{ +public: + StorageClearAll() : Command("clear-all") {} + + CHIP_ERROR Run() override; +}; diff --git a/examples/chip-tool/config/PersistentStorage.cpp b/examples/chip-tool/config/PersistentStorage.cpp index d580a80ece50a7..f0c3a8312b4c89 100644 --- a/examples/chip-tool/config/PersistentStorage.cpp +++ b/examples/chip-tool/config/PersistentStorage.cpp @@ -145,6 +145,15 @@ CHIP_ERROR PersistentStorage::SyncDeleteKeyValue(const char * key) return CommitConfig(mName); } +CHIP_ERROR PersistentStorage::SyncClearAll() +{ + ChipLogProgress(chipTool, "Clearing %s storage", kDefaultSectionName); + auto section = mConfig.sections[kDefaultSectionName]; + section.clear(); + mConfig.sections[kDefaultSectionName] = section; + return CommitConfig(mName); +} + CHIP_ERROR PersistentStorage::CommitConfig(const char * name) { CHIP_ERROR err = CHIP_NO_ERROR; diff --git a/examples/chip-tool/config/PersistentStorage.h b/examples/chip-tool/config/PersistentStorage.h index 35c5e8653d607f..d26582ea9b9dfa 100644 --- a/examples/chip-tool/config/PersistentStorage.h +++ b/examples/chip-tool/config/PersistentStorage.h @@ -48,6 +48,9 @@ class PersistentStorage : public chip::PersistentStorageDelegate // Store local CATs. CHIP_ERROR SetCommissionerCATs(const chip::CATValues & cats); + // Clear all of the persistent storage for running session. + CHIP_ERROR SyncClearAll(); + private: CHIP_ERROR CommitConfig(const char * name); inipp::Ini mConfig; diff --git a/examples/chip-tool/main.cpp b/examples/chip-tool/main.cpp index 8ebc770775db60..c9a391b25e5f98 100644 --- a/examples/chip-tool/main.cpp +++ b/examples/chip-tool/main.cpp @@ -24,6 +24,7 @@ #include "commands/interactive/Commands.h" #include "commands/pairing/Commands.h" #include "commands/payload/Commands.h" +#include "commands/storage/Commands.h" #include #include @@ -42,6 +43,7 @@ int main(int argc, char * argv[]) registerCommandsTests(commands, &credIssuerCommands); registerCommandsGroup(commands, &credIssuerCommands); registerClusters(commands, &credIssuerCommands); + registerCommandsStorage(commands); return commands.Run(argc, argv); }