Skip to content

Commit

Permalink
[linux] Use generic PersistentStorageDelegate instead of ChipLinuxSto… (
Browse files Browse the repository at this point in the history
#17371)

* [linux] Use generic PersistentStorageDelegate instead of ChipLinuxStorage for userlabel

* Update src/platform/Linux/DeviceInfoProviderImpl.cpp

Co-authored-by: Boris Zbarsky <bzbarsky@apple.com>

Co-authored-by: Boris Zbarsky <bzbarsky@apple.com>
  • Loading branch information
2 people authored and pull[bot] committed Jul 22, 2023
1 parent 293c89a commit 3535643
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 32 deletions.
1 change: 0 additions & 1 deletion src/platform/Darwin/DeviceInfoProviderImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
#include <lib/support/CodeUtils.h>
#include <lib/support/DefaultStorageKeyAllocator.h>
#include <platform/Darwin/DeviceInfoProviderImpl.h>
#include <platform/KeyValueStoreManager.h>
#include <platform/internal/CHIPDeviceLayerInternal.h>

#include <stdlib.h>
Expand Down
51 changes: 32 additions & 19 deletions src/platform/Linux/DeviceInfoProviderImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,6 @@ constexpr TLV::Tag kLabelNameTag = TLV::ContextTag(0);
constexpr TLV::Tag kLabelValueTag = TLV::ContextTag(1);
} // anonymous namespace

CHIP_ERROR DeviceInfoProviderImpl::Init()
{
return mStorage.Init(CHIP_DEVICE_INFO_PATH);
}

DeviceInfoProviderImpl & DeviceInfoProviderImpl::GetDefaultInstance()
{
static DeviceInfoProviderImpl sInstance;
Expand All @@ -62,6 +57,8 @@ size_t DeviceInfoProviderImpl::FixedLabelIteratorImpl::Count()

bool DeviceInfoProviderImpl::FixedLabelIteratorImpl::Next(FixedLabelType & output)
{
bool retval = true;

// In Linux Simulation, use the following hardcoded labelList on all endpoints.
CHIP_ERROR err = CHIP_NO_ERROR;

Expand Down Expand Up @@ -108,26 +105,29 @@ bool DeviceInfoProviderImpl::FixedLabelIteratorImpl::Next(FixedLabelType & outpu

mIndex++;

return true;
retval = true;
}
else
{
retval = false;
}

return false;
return retval;
}

CHIP_ERROR DeviceInfoProviderImpl::SetUserLabelLength(EndpointId endpoint, size_t val)
{
DefaultStorageKeyAllocator keyAlloc;

ReturnErrorOnFailure(mStorage.WriteValue(keyAlloc.UserLabelLengthKey(endpoint), val));

return mStorage.Commit();
return mStorage->SyncSetKeyValue(keyAlloc.UserLabelLengthKey(endpoint), &val, static_cast<uint16_t>(sizeof(val)));
}

CHIP_ERROR DeviceInfoProviderImpl::GetUserLabelLength(EndpointId endpoint, size_t & val)
{
DefaultStorageKeyAllocator keyAlloc;
uint16_t len = static_cast<uint16_t>(sizeof(val));

return mStorage.ReadValue(keyAlloc.UserLabelLengthKey(endpoint), val);
return mStorage->SyncGetKeyValue(keyAlloc.UserLabelLengthKey(endpoint), &val, len);
}

CHIP_ERROR DeviceInfoProviderImpl::SetUserLabelAt(EndpointId endpoint, size_t index, const UserLabelType & userLabel)
Expand All @@ -142,9 +142,9 @@ CHIP_ERROR DeviceInfoProviderImpl::SetUserLabelAt(EndpointId endpoint, size_t in
ReturnErrorOnFailure(writer.PutString(kLabelNameTag, userLabel.label));
ReturnErrorOnFailure(writer.PutString(kLabelValueTag, userLabel.value));
ReturnErrorOnFailure(writer.EndContainer(outerType));
ReturnErrorOnFailure(mStorage.WriteValueBin(keyAlloc.UserLabelIndexKey(endpoint, index), buf, writer.GetLengthWritten()));

return mStorage.Commit();
return mStorage->SyncSetKeyValue(keyAlloc.UserLabelIndexKey(endpoint, index), buf,
static_cast<uint16_t>(writer.GetLengthWritten()));
}

DeviceInfoProvider::UserLabelIterator * DeviceInfoProviderImpl::IterateUserLabel(EndpointId endpoint)
Expand All @@ -170,8 +170,9 @@ bool DeviceInfoProviderImpl::UserLabelIteratorImpl::Next(UserLabelType & output)

DefaultStorageKeyAllocator keyAlloc;
uint8_t buf[UserLabelTLVMaxSize()];
size_t outLen;
err = mProvider.mStorage.ReadValueBin(keyAlloc.UserLabelIndexKey(mEndpoint, mIndex), buf, sizeof(buf), outLen);
uint16_t len = static_cast<uint16_t>(sizeof(buf));

err = mProvider.mStorage->SyncGetKeyValue(keyAlloc.UserLabelIndexKey(mEndpoint, mIndex), buf, len);
VerifyOrReturnError(err == CHIP_NO_ERROR, false);

TLV::ContiguousBufferTLVReader reader;
Expand Down Expand Up @@ -220,6 +221,8 @@ size_t DeviceInfoProviderImpl::SupportedLocalesIteratorImpl::Count()

bool DeviceInfoProviderImpl::SupportedLocalesIteratorImpl::Next(CharSpan & output)
{
bool retval = true;

// In Linux simulation, return following hardcoded list of Strings that are valid values for the ActiveLocale.
CHIP_ERROR err = CHIP_NO_ERROR;

Expand Down Expand Up @@ -268,10 +271,14 @@ bool DeviceInfoProviderImpl::SupportedLocalesIteratorImpl::Next(CharSpan & outpu

mIndex++;

return true;
retval = true;
}
else
{
retval = false;
}

return false;
return retval;
}

DeviceInfoProvider::SupportedCalendarTypesIterator * DeviceInfoProviderImpl::IterateSupportedCalendarTypes()
Expand All @@ -290,6 +297,8 @@ size_t DeviceInfoProviderImpl::SupportedCalendarTypesIteratorImpl::Count()

bool DeviceInfoProviderImpl::SupportedCalendarTypesIteratorImpl::Next(CalendarType & output)
{
bool retval = true;

// In Linux Simulation, return following hardcoded list of Strings that are valid values for the Calendar Types.
CHIP_ERROR err = CHIP_NO_ERROR;

Expand Down Expand Up @@ -341,10 +350,14 @@ bool DeviceInfoProviderImpl::SupportedCalendarTypesIteratorImpl::Next(CalendarTy
if (err == CHIP_NO_ERROR)
{
mIndex++;
return true;
retval = true;
}
else
{
retval = false;
}

return false;
return retval;
}

} // namespace DeviceLayer
Expand Down
11 changes: 0 additions & 11 deletions src/platform/Linux/DeviceInfoProviderImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,6 @@ class DeviceInfoProviderImpl : public DeviceInfoProvider
DeviceInfoProviderImpl() = default;
~DeviceInfoProviderImpl() override {}

/**
* Initialize the DeviceInfoProvider, including possibly any persistent
* data store initialization done by the implementation. Must be called once
* before any other API succeeds.
*
* @retval #CHIP_NO_ERROR on success
*/
CHIP_ERROR Init();

// Iterators
FixedLabelIterator * IterateFixedLabel(EndpointId endpoint) override;
UserLabelIterator * IterateUserLabel(EndpointId endpoint) override;
Expand Down Expand Up @@ -109,8 +100,6 @@ class DeviceInfoProviderImpl : public DeviceInfoProvider
CHIP_ERROR SetUserLabelAt(EndpointId endpoint, size_t index, const UserLabelType & userLabel) override;

private:
DeviceLayer::Internal::ChipLinuxStorage mStorage;

static constexpr size_t UserLabelTLVMaxSize() { return TLV::EstimateStructOverhead(kMaxLabelNameLength, kMaxLabelValueLength); }
};

Expand Down
1 change: 0 additions & 1 deletion src/platform/Linux/PlatformManagerImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,6 @@ CHIP_ERROR PlatformManagerImpl::_InitChipStack()
SetConfigurationMgr(&ConfigurationManagerImpl::GetDefaultInstance());
SetDiagnosticDataProvider(&DiagnosticDataProviderImpl::GetDefaultInstance());
SetDeviceInfoProvider(&DeviceInfoProviderImpl::GetDefaultInstance());
ReturnErrorOnFailure(DeviceInfoProviderImpl::GetDefaultInstance().Init());

// Call _InitChipStack() on the generic implementation base class
// to finish the initialization process.
Expand Down

0 comments on commit 3535643

Please sign in to comment.