Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expand attribute persistence provider api #27611

Merged
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
48915f3
Modified the AttributePersistenceProvider ReadValue function signitur…
hicklin Jul 3, 2023
1f989a9
Expanded AttributePersistanceProvider API to include reading and wirt…
hicklin Jul 3, 2023
b5ed2d5
Templated the AttributePersistanceProvider read and wiret function to…
hicklin Jul 3, 2023
c58e9a2
Fixed AttributePersistanceProvider accepted types. Added read helper …
hicklin Jul 4, 2023
1b1abfe
Merge branch 'project-chip:master' into expand_AttributePersistancePr…
hicklin Jul 4, 2023
e398672
Restyled by clang-format
restyled-commits Jul 4, 2023
6e3d3c7
Fixed mismatched size return error of DefaultAttributePersistenceProv…
hicklin Jul 4, 2023
4630dc1
Started work on tests for the AttributePersistenceProvider.
hicklin Jul 5, 2023
d60a795
Added unit tests for AttributePersistenceProvider testing the storage…
hicklin Jul 5, 2023
1643373
Changed the type of aSize in ReadValue to size_t
hicklin Jul 6, 2023
e1ec87e
Removed the dependancy on generated code in the AttributePersistencez…
hicklin Jul 6, 2023
4574741
Added static funtctions to get the KVS null representation for differ…
hicklin Jul 6, 2023
b5d456c
Added functions to read and write nullable bools and accompanying tests.
hicklin Jul 6, 2023
15230fc
Incorporated boolean tests in the scalar test.
hicklin Jul 6, 2023
476bf9c
Merge branch 'master' into expand_AttributePersistanceProvider_API
hicklin Jul 6, 2023
76db4d3
Added failure before init test
hicklin Jul 6, 2023
333ac54
Restyled by clang-format
restyled-commits Jul 6, 2023
4f9c481
Merge branch 'master' into expand_AttributePersistanceProvider_API
hicklin Jul 7, 2023
2d6f26a
Fixed after merge.
hicklin Jul 7, 2023
2d4cca0
Removed the failure on init test as it may have been causing seg faul…
hicklin Jul 7, 2023
09488ec
Renamed GetNull -> GetNullValueForNullableType
hicklin Jul 7, 2023
582b92c
Added the initialisation of valueReadBack and added a new templated f…
hicklin Jul 7, 2023
7c1bf20
Added handline of signed ints and accompanying tests.
hicklin Jul 7, 2023
81d062f
Added handline of nullable signed ints and accompanying tests.
hicklin Jul 7, 2023
de3b1c9
Type cast null.
hicklin Jul 7, 2023
35070d1
Merge branch 'master' into expand_AttributePersistanceProvider_API
hicklin Jul 7, 2023
7f2423f
Restyled by clang-format
restyled-commits Jul 7, 2023
9ab6af9
Changed shift bit to be af the same type are the return val.
hicklin Jul 7, 2023
e289e60
Added tests got GetNull functions
hicklin Jul 7, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
105 changes: 102 additions & 3 deletions src/app/AttributePersistenceProvider.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,13 @@
*/
#pragma once

#include <app-common/zap-generated/attribute-type.h>
tcarmelveilleux marked this conversation as resolved.
Show resolved Hide resolved
#include <app/ConcreteAttributePath.h>
#include <app/data-model/Nullable.h>
#include <app/util/attribute-metadata.h>
#include <cstring>
#include <lib/support/BufferReader.h>
#include <lib/support/BufferWriter.h>
#include <lib/support/Span.h>

namespace chip {
Expand Down Expand Up @@ -56,17 +61,111 @@ class AttributePersistenceProvider
* Read an attribute value from non-volatile memory.
*
* @param [in] aPath the attribute path for the data being persisted.
* @param [in] aMetadata the attribute metadata, as a convenience.
* @param [in] aType the attribute type.
* @param [in] aSize the attribute size.
* @param [in,out] aValue where to place the data. The size of the buffer
* will be equal to `size` member of aMetadata.
* will be equal to `size`.
*
* The data is expected to be in native endianness for
* integers and floats. For strings, see the string
* representation description in the WriteValue
* documentation.
*/
virtual CHIP_ERROR ReadValue(const ConcreteAttributePath & aPath, const EmberAfAttributeMetadata * aMetadata,
virtual CHIP_ERROR ReadValue(const ConcreteAttributePath & aPath, EmberAfAttributeType aType, uint16_t aSize,
MutableByteSpan & aValue) = 0;

// The following API provides helper functions to simplify the access of commonly used types.
// The API may not be complete.
// Currently implemented write and read types are: uint8_t, uint16_t, uint32_t, unit64_t and
// their nullable varieties, and bool.

/**
* Write an attribute value of type unsigned intX or bool to non-volatile memory.
*
* @param [in] aPath the attribute path for the data being written.
* @param [in] aValue the data to write.
*/
template <typename T, std::enable_if_t<std::is_unsigned<T>::value, bool> = true>
tcarmelveilleux marked this conversation as resolved.
Show resolved Hide resolved
CHIP_ERROR WriteValue(const ConcreteAttributePath & aPath, T & aValue)
{
uint8_t value[sizeof(T)];
auto w = Encoding::LittleEndian::BufferWriter(value, sizeof(T));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will completely break on a big-endian system for non-one-byte values, no? The values passed to AttributePersistenceProvider are native-endian for integers! This is very clearly documented above.

w.EndianPut(aValue, sizeof(T));

return WriteValue(aPath, ByteSpan(value));
}

/**
* Read an attribute of type unsigned intX or bool from non-volatile memory.
*
* @param [in] aPath the attribute path for the data being persisted.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The data is not being persisted....

* @param [in,out] aValue where to place the data.
*/
template <typename T, std::enable_if_t<std::is_unsigned<T>::value, bool> = true>
CHIP_ERROR ReadValue(const ConcreteAttributePath & aPath, T & aValue)
{
uint8_t attrData[sizeof(T)];
MutableByteSpan tempVal(attrData, sizeof(T));
// **Note** aType in the ReadValue function is only used to check if the value is of a string type. Since this template
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, ReadValue is implemented by the application in general. You can't make assumptions about how it will use the data it's handed.

// function is only enabled for integral values, we know that this case will not occur, so we can pass the enum of an
// arbitrary integral type.
auto err = ReadValue(aPath, ZCL_INT8U_ATTRIBUTE_TYPE, sizeof(T), tempVal);
if (err != CHIP_NO_ERROR)
{
return err;
}

chip::Encoding::LittleEndian::Reader r(tempVal.data(), tempVal.size());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why the extra chip::? But again, the data here is native-endian.

r.RawRead(&aValue);
return r.StatusCode();
}

/**
* Write an attribute value of type nullable unsigned intX to non-volatile memory.
*
* @param [in] aPath the attribute path for the data being written.
* @param [in] aValue the data to write.
*/
template <typename T, std::enable_if_t<std::is_unsigned<T>::value && !std::is_same<bool, T>::value, bool> = true>
CHIP_ERROR WriteValue(const ConcreteAttributePath & aPath, DataModel::Nullable<T> & aValue)
{
if (aValue.IsNull())
{
T nullValue = 0;
nullValue = ~nullValue;
return WriteValue(aPath, nullValue);
}
return WriteValue(aPath, aValue.Value());
}

/**
* Read an attribute of type nullable unsigned intX from non-volatile memory.
*
* @param [in] aPath the attribute path for the data being persisted.
* @param [in,out] aValue where to place the data.
*/
template <typename T, std::enable_if_t<std::is_unsigned<T>::value && !std::is_same<bool, T>::value, bool> = true>
CHIP_ERROR ReadValue(const ConcreteAttributePath & aPath, DataModel::Nullable<T> & aValue)
{
T tempIntegral;
T nullValue = 0;
nullValue = ~nullValue;

CHIP_ERROR err = ReadValue(aPath, tempIntegral);
if (err != CHIP_NO_ERROR)
{
return err;
}

if (tempIntegral == nullValue)
{
aValue.SetNull();
return CHIP_NO_ERROR;
}

aValue.SetNonNull(tempIntegral);
return CHIP_NO_ERROR;
}
};

/**
Expand Down
8 changes: 4 additions & 4 deletions src/app/DefaultAttributePersistenceProvider.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,16 +38,16 @@ CHIP_ERROR DefaultAttributePersistenceProvider::WriteValue(const ConcreteAttribu
aValue.data(), static_cast<uint16_t>(aValue.size()));
}

CHIP_ERROR DefaultAttributePersistenceProvider::ReadValue(const ConcreteAttributePath & aPath,
const EmberAfAttributeMetadata * aMetadata, MutableByteSpan & aValue)
CHIP_ERROR DefaultAttributePersistenceProvider::ReadValue(const ConcreteAttributePath & aPath, EmberAfAttributeType aType,
uint16_t aSize, MutableByteSpan & aValue)
hicklin marked this conversation as resolved.
Show resolved Hide resolved
{
VerifyOrReturnError(mStorage != nullptr, CHIP_ERROR_INCORRECT_STATE);

uint16_t size = static_cast<uint16_t>(min(aValue.size(), static_cast<size_t>(UINT16_MAX)));
ReturnErrorOnFailure(mStorage->SyncGetKeyValue(
DefaultStorageKeyAllocator::AttributeValue(aPath.mEndpointId, aPath.mClusterId, aPath.mAttributeId).KeyName(),
aValue.data(), size));
EmberAfAttributeType type = aMetadata->attributeType;
EmberAfAttributeType type = aType;
if (emberAfIsStringAttributeType(type))
{
// Ensure that we've read enough bytes that we are not ending up with
Expand All @@ -65,7 +65,7 @@ CHIP_ERROR DefaultAttributePersistenceProvider::ReadValue(const ConcreteAttribut
else
{
// Ensure we got the expected number of bytes for all other types.
VerifyOrReturnError(size == aMetadata->size, CHIP_ERROR_INCORRECT_STATE);
VerifyOrReturnError(size == aSize, CHIP_ERROR_INCORRECT_STATE);
hicklin marked this conversation as resolved.
Show resolved Hide resolved
}
aValue.reduce_size(size);
return CHIP_NO_ERROR;
Expand Down
2 changes: 1 addition & 1 deletion src/app/DefaultAttributePersistenceProvider.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ class DefaultAttributePersistenceProvider : public AttributePersistenceProvider

// AttributePersistenceProvider implementation.
CHIP_ERROR WriteValue(const ConcreteAttributePath & aPath, const ByteSpan & aValue) override;
CHIP_ERROR ReadValue(const ConcreteAttributePath & aPath, const EmberAfAttributeMetadata * aMetadata,
CHIP_ERROR ReadValue(const ConcreteAttributePath & aPath, EmberAfAttributeType aType, uint16_t aSize,
MutableByteSpan & aValue) override;

protected:
Expand Down
14 changes: 7 additions & 7 deletions src/app/DeferredAttributePersistenceProvider.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,25 +42,25 @@ void DeferredAttribute::Flush(AttributePersistenceProvider & persister)
mValue.Release();
}

CHIP_ERROR DeferredAttributePersistenceProvider::WriteValue(const ConcreteAttributePath & path, const ByteSpan & value)
CHIP_ERROR DeferredAttributePersistenceProvider::WriteValue(const ConcreteAttributePath & aPath, const ByteSpan & aValue)
{
for (DeferredAttribute & da : mDeferredAttributes)
{
if (da.Matches(path))
if (da.Matches(aPath))
{
ReturnErrorOnFailure(da.PrepareWrite(System::SystemClock().GetMonotonicTimestamp() + mWriteDelay, value));
ReturnErrorOnFailure(da.PrepareWrite(System::SystemClock().GetMonotonicTimestamp() + mWriteDelay, aValue));
FlushAndScheduleNext();
return CHIP_NO_ERROR;
}
}

return mPersister.WriteValue(path, value);
return mPersister.WriteValue(aPath, aValue);
}

CHIP_ERROR DeferredAttributePersistenceProvider::ReadValue(const ConcreteAttributePath & path,
const EmberAfAttributeMetadata * metadata, MutableByteSpan & value)
CHIP_ERROR DeferredAttributePersistenceProvider::ReadValue(const ConcreteAttributePath & aPath, EmberAfAttributeType aType,
uint16_t aSize, MutableByteSpan & aValue)
{
return mPersister.ReadValue(path, metadata, value);
return mPersister.ReadValue(aPath, aType, aSize, aValue);
}

void DeferredAttributePersistenceProvider::FlushAndScheduleNext()
Expand Down
6 changes: 3 additions & 3 deletions src/app/DeferredAttributePersistenceProvider.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,9 @@ class DeferredAttributePersistenceProvider : public AttributePersistenceProvider
*
* For other attributes, immediately pass the write operation to the decorated persister.
*/
CHIP_ERROR WriteValue(const ConcreteAttributePath & path, const ByteSpan & value) override;
CHIP_ERROR ReadValue(const ConcreteAttributePath & path, const EmberAfAttributeMetadata * metadata,
MutableByteSpan & value) override;
CHIP_ERROR WriteValue(const ConcreteAttributePath & aPath, const ByteSpan & aValue) override;
CHIP_ERROR ReadValue(const ConcreteAttributePath & aPath, EmberAfAttributeType aType, uint16_t aSize,
MutableByteSpan & aValue) override;

private:
void FlushAndScheduleNext();
Expand Down
5 changes: 3 additions & 2 deletions src/app/util/attribute-storage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1215,8 +1215,9 @@ void emAfLoadAttributeDefaults(EndpointId endpoint, bool ignoreStorage, Optional
{
VerifyOrDie(attrStorage && "Attribute persistence needs a persistence provider");
MutableByteSpan bytes(attrData);
CHIP_ERROR err = attrStorage->ReadValue(
app::ConcreteAttributePath(de->endpoint, cluster->clusterId, am->attributeId), am, bytes);
CHIP_ERROR err =
attrStorage->ReadValue(app::ConcreteAttributePath(de->endpoint, cluster->clusterId, am->attributeId),
am->attributeType, am->size, bytes);
if (err == CHIP_NO_ERROR)
{
ptr = attrData;
Expand Down
5 changes: 5 additions & 0 deletions src/lib/support/BufferReader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ namespace {
// These helper methods return void and put the value being read into an
// outparam because that allows us to easily overload on the type of the
// thing being read.
void ReadHelper(const uint8_t *& p, bool * dest)
{
*dest = !!Read8(p);
}
void ReadHelper(const uint8_t *& p, uint8_t * dest)
{
*dest = Read8(p);
Expand Down Expand Up @@ -84,6 +88,7 @@ Reader & Reader::ReadBytes(uint8_t * dest, size_t size)
}

// Explicit Read instantiations for the data types we want to support.
template void Reader::RawRead(bool *);
template void Reader::RawRead(uint8_t *);
template void Reader::RawRead(uint16_t *);
template void Reader::RawRead(uint32_t *);
Expand Down