Skip to content

Commit

Permalink
Add ability to remove storage to chip-tools (#18512)
Browse files Browse the repository at this point in the history
  • Loading branch information
krypton36 authored and pull[bot] committed Jul 5, 2023
1 parent 368f817 commit 1040686
Show file tree
Hide file tree
Showing 14 changed files with 235 additions and 4 deletions.
2 changes: 2 additions & 0 deletions examples/chip-tool-darwin/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -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",
]

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
31 changes: 31 additions & 0 deletions examples/chip-tool-darwin/commands/storage/Commands.h
Original file line number Diff line number Diff line change
@@ -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 <commands/common/Commands.h>

void registerCommandsStorage(Commands & commands)
{
const char * clusterName = "storage";

commands_list clusterCommands = { make_unique<StorageClearAll>() };

commands.Register(clusterName, clusterCommands);
}
Original file line number Diff line number Diff line change
@@ -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 <commands/common/Command.h>

class StorageClearAll : public Command
{
public:
StorageClearAll() : Command("clear-all") {}

CHIP_ERROR Run() override;
};
Original file line number Diff line number Diff line change
@@ -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;
}
3 changes: 3 additions & 0 deletions examples/chip-tool-darwin/main.mm
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,16 @@

#include "commands/pairing/Commands.h"

#include "commands/storage/Commands.h"

#include <zap-generated/cluster/Commands.h>
#include <zap-generated/test/Commands.h>

int main(int argc, const char * argv[])
{
Commands commands;
registerCommandsPairing(commands);
registerCommandsStorage(commands);
registerCommandsTests(commands);
registerClusters(commands);
return commands.Run(argc, (char **) argv);
Expand Down
1 change: 1 addition & 0 deletions examples/chip-tool/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -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",
]

Expand Down
31 changes: 31 additions & 0 deletions examples/chip-tool/commands/storage/Commands.h
Original file line number Diff line number Diff line change
@@ -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 <commands/common/Commands.h>

void registerCommandsStorage(Commands & commands)
{
const char * clusterName = "storage";

commands_list clusterCommands = { make_unique<StorageClearAll>() };

commands.Register(clusterName, clusterCommands);
}
28 changes: 28 additions & 0 deletions examples/chip-tool/commands/storage/StorageManagementCommand.cpp
Original file line number Diff line number Diff line change
@@ -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();
}
29 changes: 29 additions & 0 deletions examples/chip-tool/commands/storage/StorageManagementCommand.h
Original file line number Diff line number Diff line change
@@ -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 <commands/common/Command.h>

class StorageClearAll : public Command
{
public:
StorageClearAll() : Command("clear-all") {}

CHIP_ERROR Run() override;
};
9 changes: 9 additions & 0 deletions examples/chip-tool/config/PersistentStorage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
3 changes: 3 additions & 0 deletions examples/chip-tool/config/PersistentStorage.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<char> mConfig;
Expand Down
2 changes: 2 additions & 0 deletions examples/chip-tool/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 <zap-generated/cluster/Commands.h>
#include <zap-generated/test/Commands.h>
Expand All @@ -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);
}

0 comments on commit 1040686

Please sign in to comment.