Skip to content

Commit

Permalink
Rename persistent storage APIs to be more unique (#5366)
Browse files Browse the repository at this point in the history
* Rename OnValue/OnStatus to OnPersistentStorageValue and OnPersistentStorageStatus

* Rename SetDelegfate to SetStorageDelegate

* Add missed rename for storage delegate

* Rename set/get/delete key with a sync/async prefix

* Fix typo

* Update one more name in the python contriller delegate
  • Loading branch information
andy31415 authored Mar 16, 2021
1 parent cf7aa2d commit d36c06d
Show file tree
Hide file tree
Showing 17 changed files with 119 additions and 117 deletions.
14 changes: 7 additions & 7 deletions examples/chip-tool/config/PersistentStorage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,11 @@ CHIP_ERROR PersistentStorage::Init()
return err;
}

void PersistentStorage::SetDelegate(PersistentStorageResultDelegate * delegate) {}
void PersistentStorage::SetStorageDelegate(PersistentStorageResultDelegate * delegate) {}

void PersistentStorage::GetKeyValue(const char * key) {}
void PersistentStorage::AsyncGetKeyValue(const char * key) {}

CHIP_ERROR PersistentStorage::GetKeyValue(const char * key, char * value, uint16_t & size)
CHIP_ERROR PersistentStorage::SyncGetKeyValue(const char * key, char * value, uint16_t & size)
{
CHIP_ERROR err = CHIP_NO_ERROR;
std::string iniValue;
Expand All @@ -79,7 +79,7 @@ CHIP_ERROR PersistentStorage::GetKeyValue(const char * key, char * value, uint16
return err;
}

void PersistentStorage::SetKeyValue(const char * key, const char * value)
void PersistentStorage::AsyncSetKeyValue(const char * key, const char * value)
{
auto section = mConfig.sections[kDefaultSectionName];
section[key] = std::string(value);
Expand All @@ -88,7 +88,7 @@ void PersistentStorage::SetKeyValue(const char * key, const char * value)
CommitConfig();
}

void PersistentStorage::DeleteKeyValue(const char * key)
void PersistentStorage::AsyncDeleteKeyValue(const char * key)
{
auto section = mConfig.sections[kDefaultSectionName];
section.erase(key);
Expand Down Expand Up @@ -126,7 +126,7 @@ uint16_t PersistentStorage::GetListenPort()

char value[6];
uint16_t size = static_cast<uint16_t>(sizeof(value));
err = GetKeyValue(kPortKey, value, size);
err = SyncGetKeyValue(kPortKey, value, size);
if (CHIP_NO_ERROR == err)
{
uint16_t tmpValue;
Expand All @@ -148,7 +148,7 @@ LogCategory PersistentStorage::GetLoggingLevel()

char value[9];
uint16_t size = static_cast<uint16_t>(sizeof(value));
err = GetKeyValue(kLoggingKey, value, size);
err = SyncGetKeyValue(kLoggingKey, value, size);
if (CHIP_NO_ERROR == err)
{
if (strcasecmp(value, "none") == 0)
Expand Down
10 changes: 5 additions & 5 deletions examples/chip-tool/config/PersistentStorage.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,11 @@ class PersistentStorage : public chip::PersistentStorageDelegate
CHIP_ERROR Init();

/////////// PersistentStorageDelegate Interface /////////
void SetDelegate(chip::PersistentStorageResultDelegate * delegate) override;
void GetKeyValue(const char * key) override;
CHIP_ERROR GetKeyValue(const char * key, char * value, uint16_t & size) override;
void SetKeyValue(const char * key, const char * value) override;
void DeleteKeyValue(const char * key) override;
void SetStorageDelegate(chip::PersistentStorageResultDelegate * delegate) override;
void AsyncGetKeyValue(const char * key) override;
CHIP_ERROR SyncGetKeyValue(const char * key, char * value, uint16_t & size) override;
void AsyncSetKeyValue(const char * key, const char * value) override;
void AsyncDeleteKeyValue(const char * key) override;

uint16_t GetListenPort();
chip::Logging::LogCategory GetLoggingLevel();
Expand Down
2 changes: 1 addition & 1 deletion src/app/server/RendezvousServer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ void RendezvousServer::OnRendezvousComplete()
ChipLogError(AppServer, "Failed to store the connection state"));

uint16_t nextKeyId = mRendezvousSession.GetNextKeyId();
mStorage->SetKeyValue(kStorablePeerConnectionCountKey, &nextKeyId, sizeof(nextKeyId));
mStorage->SyncSetKeyValue(kStorablePeerConnectionCountKey, &nextKeyId, sizeof(nextKeyId));
}

void RendezvousServer::OnRendezvousStatusUpdate(Status status, CHIP_ERROR err)
Expand Down
12 changes: 6 additions & 6 deletions src/app/server/Server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,35 +72,35 @@ constexpr bool useTestPairing()

class ServerStorageDelegate : public PersistentStorageDelegate
{
void SetDelegate(PersistentStorageResultDelegate * delegate) override
void SetStorageDelegate(PersistentStorageResultDelegate * delegate) override
{
ChipLogError(AppServer, "ServerStorageDelegate does not support async operations");
chipDie();
}

void GetKeyValue(const char * key) override
void AsyncGetKeyValue(const char * key) override
{
ChipLogError(AppServer, "ServerStorageDelegate does not support async operations");
chipDie();
}

void SetKeyValue(const char * key, const char * value) override
void AsyncSetKeyValue(const char * key, const char * value) override
{
ChipLogError(AppServer, "ServerStorageDelegate does not support async operations");
chipDie();
}

CHIP_ERROR GetKeyValue(const char * key, void * buffer, uint16_t & size) override
CHIP_ERROR SyncGetKeyValue(const char * key, void * buffer, uint16_t & size) override
{
return PersistedStorage::KeyValueStoreMgr().Get(key, buffer, size);
}

CHIP_ERROR SetKeyValue(const char * key, const void * value, uint16_t size) override
CHIP_ERROR SyncSetKeyValue(const char * key, const void * value, uint16_t size) override
{
return PersistedStorage::KeyValueStoreMgr().Put(key, value, size);
}

void DeleteKeyValue(const char * key) override { PersistedStorage::KeyValueStoreMgr().Delete(key); }
void AsyncDeleteKeyValue(const char * key) override { PersistedStorage::KeyValueStoreMgr().Delete(key); }
};

ServerStorageDelegate gServerStorage;
Expand Down
18 changes: 9 additions & 9 deletions src/controller/CHIPDeviceController.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ CHIP_ERROR DeviceController::Init(NodeId localDeviceId, PersistentStorageDelegat

if (mStorageDelegate != nullptr)
{
mStorageDelegate->SetDelegate(this);
mStorageDelegate->SetStorageDelegate(this);
}

mTransportMgr = chip::Platform::New<DeviceTransportMgr>();
Expand Down Expand Up @@ -193,7 +193,7 @@ CHIP_ERROR DeviceController::Shutdown()

if (mStorageDelegate != nullptr)
{
mStorageDelegate->SetDelegate(nullptr);
mStorageDelegate->SetStorageDelegate(nullptr);
mStorageDelegate = nullptr;
}

Expand Down Expand Up @@ -287,7 +287,7 @@ CHIP_ERROR DeviceController::GetDevice(NodeId deviceId, Device ** out_device)
VerifyOrExit(buffer != nullptr, err = CHIP_ERROR_INVALID_ARGUMENT);

PERSISTENT_KEY_OP(static_cast<uint64_t>(0), kPairedDeviceListKeyPrefix, key,
err = mStorageDelegate->GetKeyValue(key, buffer, size));
err = mStorageDelegate->SyncGetKeyValue(key, buffer, size));
SuccessOrExit(err);
VerifyOrExit(size <= max_size, err = CHIP_ERROR_INVALID_DEVICE_DESCRIPTOR);

Expand All @@ -306,7 +306,7 @@ CHIP_ERROR DeviceController::GetDevice(NodeId deviceId, Device ** out_device)
uint16_t size = sizeof(deviceInfo.inner);

PERSISTENT_KEY_OP(deviceId, kPairedDeviceKeyPrefix, key,
err = mStorageDelegate->GetKeyValue(key, Uint8::to_char(deviceInfo.inner), size));
err = mStorageDelegate->SyncGetKeyValue(key, Uint8::to_char(deviceInfo.inner), size));
SuccessOrExit(err);
VerifyOrExit(size <= sizeof(deviceInfo.inner), err = CHIP_ERROR_INVALID_DEVICE_DESCRIPTOR);

Expand Down Expand Up @@ -485,9 +485,9 @@ CHIP_ERROR DeviceController::SetPairedDeviceList(const char * serialized)
return err;
}

void DeviceController::OnValue(const char * key, const char * value) {}
void DeviceController::OnPersistentStorageValue(const char * key, const char * value) {}

void DeviceController::OnStatus(const char * key, Operation op, CHIP_ERROR err) {}
void DeviceController::OnPersistentStorageStatus(const char * key, Operation op, CHIP_ERROR err) {}

DeviceCommissioner::DeviceCommissioner()
{
Expand Down Expand Up @@ -671,7 +671,7 @@ CHIP_ERROR DeviceCommissioner::UnpairDevice(NodeId remoteDeviceId)

if (mStorageDelegate != nullptr)
{
PERSISTENT_KEY_OP(remoteDeviceId, kPairedDeviceKeyPrefix, key, mStorageDelegate->DeleteKeyValue(key));
PERSISTENT_KEY_OP(remoteDeviceId, kPairedDeviceKeyPrefix, key, mStorageDelegate->AsyncDeleteKeyValue(key));
}

mPairedDevices.Remove(remoteDeviceId);
Expand Down Expand Up @@ -733,7 +733,7 @@ void DeviceCommissioner::OnRendezvousComplete()
SerializedDevice serialized;
device->Serialize(serialized);
PERSISTENT_KEY_OP(device->GetDeviceId(), kPairedDeviceKeyPrefix, key,
mStorageDelegate->SetKeyValue(key, Uint8::to_const_char(serialized.inner)));
mStorageDelegate->AsyncSetKeyValue(key, Uint8::to_const_char(serialized.inner)));
}

RendezvousCleanup(CHIP_NO_ERROR);
Expand Down Expand Up @@ -796,7 +796,7 @@ void DeviceCommissioner::PersistDeviceList()
if (value != nullptr && requiredSize <= size)
{
PERSISTENT_KEY_OP(static_cast<uint64_t>(0), kPairedDeviceListKeyPrefix, key,
mStorageDelegate->SetKeyValue(key, value));
mStorageDelegate->AsyncSetKeyValue(key, value));
mPairedDevicesUpdated = false;
}
chip::Platform::MemoryFree(serialized);
Expand Down
4 changes: 2 additions & 2 deletions src/controller/CHIPDeviceController.h
Original file line number Diff line number Diff line change
Expand Up @@ -213,8 +213,8 @@ class DLL_EXPORT DeviceController : public SecureSessionMgrDelegate, public Pers
void OnConnectionExpired(SecureSessionHandle session, SecureSessionMgr * mgr) override;

//////////// PersistentStorageResultDelegate Implementation ///////////////
void OnValue(const char * key, const char * value) override;
void OnStatus(const char * key, Operation op, CHIP_ERROR err) override;
void OnPersistentStorageValue(const char * key, const char * value) override;
void OnPersistentStorageStatus(const char * key, Operation op, CHIP_ERROR err) override;

void ReleaseAllDevices();

Expand Down
48 changes: 25 additions & 23 deletions src/controller/java/AndroidDeviceControllerWrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -284,19 +284,19 @@ void AndroidDeviceControllerWrapper::OnMessage(chip::System::PacketBufferHandle

void AndroidDeviceControllerWrapper::OnStatusChange(void) {}

void AndroidDeviceControllerWrapper::SetDelegate(PersistentStorageResultDelegate * delegate)
void AndroidDeviceControllerWrapper::SetStorageDelegate(PersistentStorageResultDelegate * delegate)
{
mStorageResultDelegate = delegate;
}

void AndroidDeviceControllerWrapper::GetKeyValue(const char * key)
void AndroidDeviceControllerWrapper::AsyncGetKeyValue(const char * key)
{
jstring keyString = NULL;
jstring valueString = NULL;
const char * valueChars = nullptr;
CHIP_ERROR err = CHIP_NO_ERROR;
jclass storageCls = GetPersistentStorageClass();
jmethodID method = GetJavaEnv()->GetStaticMethodID(storageCls, "getKeyValue", "(Ljava/lang/String;)Ljava/lang/String;");
jstring keyString = NULL;
jstring valueString = NULL;
const char * valueChars = nullptr;
CHIP_ERROR err = CHIP_NO_ERROR;
jclass storageCls = GetPersistentStorageClass();
jmethodID method = GetJavaEnv()->GetStaticMethodID(storageCls, "getKeyValue", "(Ljava/lang/String;)Ljava/lang/String;");

GetJavaEnv()->ExceptionClear();

Expand All @@ -308,26 +308,27 @@ void AndroidDeviceControllerWrapper::GetKeyValue(const char * key)
if (mStorageResultDelegate)
{
valueChars = GetJavaEnv()->GetStringUTFChars(valueString, 0);
mStorageResultDelegate->OnValue(key, valueChars);
mStorageResultDelegate->OnPersistentStorageValue(key, valueChars);
}

exit:
GetJavaEnv()->ExceptionClear();
if (valueChars != nullptr) {
if (valueChars != nullptr)
{
GetJavaEnv()->ReleaseStringUTFChars(valueString, valueChars);
}
GetJavaEnv()->DeleteLocalRef(keyString);
GetJavaEnv()->DeleteLocalRef(valueString);
}

CHIP_ERROR AndroidDeviceControllerWrapper::GetKeyValue(const char * key, char * value, uint16_t & size)
CHIP_ERROR AndroidDeviceControllerWrapper::SyncGetKeyValue(const char * key, char * value, uint16_t & size)
{
jstring keyString = NULL;
jstring valueString = NULL;
const char * valueChars = nullptr;
CHIP_ERROR err = CHIP_NO_ERROR;
jclass storageCls = GetPersistentStorageClass();
jmethodID method = GetJavaEnv()->GetStaticMethodID(storageCls, "getKeyValue", "(Ljava/lang/String;)Ljava/lang/String;");
jstring keyString = NULL;
jstring valueString = NULL;
const char * valueChars = nullptr;
CHIP_ERROR err = CHIP_NO_ERROR;
jclass storageCls = GetPersistentStorageClass();
jmethodID method = GetJavaEnv()->GetStaticMethodID(storageCls, "getKeyValue", "(Ljava/lang/String;)Ljava/lang/String;");

GetJavaEnv()->ExceptionClear();

Expand All @@ -341,7 +342,7 @@ CHIP_ERROR AndroidDeviceControllerWrapper::GetKeyValue(const char * key, char *
if (value != nullptr)
{
valueChars = GetJavaEnv()->GetStringUTFChars(valueString, 0);
size = strlcpy(value, GetJavaEnv()->GetStringUTFChars(valueString, 0), size);
size = strlcpy(value, GetJavaEnv()->GetStringUTFChars(valueString, 0), size);
}
else
{
Expand All @@ -357,15 +358,16 @@ CHIP_ERROR AndroidDeviceControllerWrapper::GetKeyValue(const char * key, char *

exit:
GetJavaEnv()->ExceptionClear();
if (valueChars != nullptr) {
if (valueChars != nullptr)
{
GetJavaEnv()->ReleaseStringUTFChars(valueString, valueChars);
}
GetJavaEnv()->DeleteLocalRef(keyString);
GetJavaEnv()->DeleteLocalRef(valueString);
return err;
}

void AndroidDeviceControllerWrapper::SetKeyValue(const char * key, const char * value)
void AndroidDeviceControllerWrapper::AsyncSetKeyValue(const char * key, const char * value)
{
jclass storageCls = GetPersistentStorageClass();
jmethodID method = GetJavaEnv()->GetStaticMethodID(storageCls, "setKeyValue", "(Ljava/lang/String;Ljava/lang/String;)V");
Expand All @@ -385,7 +387,7 @@ void AndroidDeviceControllerWrapper::SetKeyValue(const char * key, const char *

if (mStorageResultDelegate)
{
mStorageResultDelegate->OnStatus(key, PersistentStorageResultDelegate::Operation::kSET, CHIP_NO_ERROR);
mStorageResultDelegate->OnPersistentStorageStatus(key, PersistentStorageResultDelegate::Operation::kSET, CHIP_NO_ERROR);
}

exit:
Expand All @@ -394,7 +396,7 @@ void AndroidDeviceControllerWrapper::SetKeyValue(const char * key, const char *
GetJavaEnv()->DeleteLocalRef(valueString);
}

void AndroidDeviceControllerWrapper::DeleteKeyValue(const char * key)
void AndroidDeviceControllerWrapper::AsyncDeleteKeyValue(const char * key)
{
jclass storageCls = GetPersistentStorageClass();
jmethodID method = GetJavaEnv()->GetStaticMethodID(storageCls, "deleteKeyValue", "(Ljava/lang/String;)V");
Expand All @@ -411,7 +413,7 @@ void AndroidDeviceControllerWrapper::DeleteKeyValue(const char * key)

if (mStorageResultDelegate)
{
mStorageResultDelegate->OnStatus(key, PersistentStorageResultDelegate::Operation::kDELETE, CHIP_NO_ERROR);
mStorageResultDelegate->OnPersistentStorageStatus(key, PersistentStorageResultDelegate::Operation::kDELETE, CHIP_NO_ERROR);
}

exit:
Expand Down
10 changes: 5 additions & 5 deletions src/controller/java/AndroidDeviceControllerWrapper.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,11 @@ class AndroidDeviceControllerWrapper : public chip::Controller::DevicePairingDel
void OnStatusChange(void) override;

// PersistentStorageDelegate implementation
void SetDelegate(chip::PersistentStorageResultDelegate * delegate) override;
void GetKeyValue(const char * key) override;
CHIP_ERROR GetKeyValue(const char * key, char * value, uint16_t & size) override;
void SetKeyValue(const char * key, const char * value) override;
void DeleteKeyValue(const char * key) override;
void SetStorageDelegate(chip::PersistentStorageResultDelegate * delegate) override;
void AsyncGetKeyValue(const char * key) override;
CHIP_ERROR SyncGetKeyValue(const char * key, char * value, uint16_t & size) override;
void AsyncSetKeyValue(const char * key, const char * value) override;
void AsyncDeleteKeyValue(const char * key) override;

jlong ToJNIHandle()
{
Expand Down
20 changes: 10 additions & 10 deletions src/controller/python/ChipDeviceController-StorageDelegate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,24 +29,24 @@
namespace chip {
namespace Controller {

void PythonPersistentStorageDelegate::SetDelegate(PersistentStorageResultDelegate * delegate)
void PythonPersistentStorageDelegate::SetStorageDelegate(PersistentStorageResultDelegate * delegate)
{
mDelegate = delegate;
}

void PythonPersistentStorageDelegate::GetKeyValue(const char * key)
void PythonPersistentStorageDelegate::AsyncGetKeyValue(const char * key)
{
auto val = mStorage.find(key);
if (val == mStorage.end())
{
mDelegate->OnStatus(key, PersistentStorageResultDelegate::Operation::kGET, CHIP_ERROR_KEY_NOT_FOUND);
mDelegate->OnPersistentStorageStatus(key, PersistentStorageResultDelegate::Operation::kGET, CHIP_ERROR_KEY_NOT_FOUND);
return;
}

mDelegate->OnValue(key, val->second.c_str());
mDelegate->OnPersistentStorageValue(key, val->second.c_str());
}

CHIP_ERROR PythonPersistentStorageDelegate::GetKeyValue(const char * key, char * value, uint16_t & size)
CHIP_ERROR PythonPersistentStorageDelegate::SyncGetKeyValue(const char * key, char * value, uint16_t & size)
{
auto val = mStorage.find(key);
if (val == mStorage.end())
Expand Down Expand Up @@ -79,17 +79,17 @@ CHIP_ERROR PythonPersistentStorageDelegate::GetKeyValue(const char * key, char *
return CHIP_NO_ERROR;
}

void PythonPersistentStorageDelegate::SetKeyValue(const char * key, const char * value)
void PythonPersistentStorageDelegate::AsyncSetKeyValue(const char * key, const char * value)
{
mStorage[key] = value;
ChipLogDetail(Controller, "SetKeyValue: %s=%s", key, value);
mDelegate->OnStatus(key, PersistentStorageResultDelegate::Operation::kSET, CHIP_NO_ERROR);
ChipLogDetail(Controller, "AsyncSetKeyValue: %s=%s", key, value);
mDelegate->OnPersistentStorageStatus(key, PersistentStorageResultDelegate::Operation::kSET, CHIP_NO_ERROR);
}

void PythonPersistentStorageDelegate::DeleteKeyValue(const char * key)
void PythonPersistentStorageDelegate::AsyncDeleteKeyValue(const char * key)
{
mStorage.erase(key);
mDelegate->OnStatus(key, PersistentStorageResultDelegate::Operation::kDELETE, CHIP_NO_ERROR);
mDelegate->OnPersistentStorageStatus(key, PersistentStorageResultDelegate::Operation::kDELETE, CHIP_NO_ERROR);
}

} // namespace Controller
Expand Down
Loading

0 comments on commit d36c06d

Please sign in to comment.