Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
swan-amazon committed Oct 7, 2024
1 parent b568518 commit 2c625b5
Show file tree
Hide file tree
Showing 3 changed files with 239 additions and 164 deletions.
187 changes: 98 additions & 89 deletions src/app/server/DefaultTermsAndConditionsProvider.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,18 +39,102 @@ constexpr size_t kEstimatedTlvBufferSize = chip::TLV::EstimateStructOverhead(siz
8 * sizeof(uint16_t); // Extra space for rollback compatibility
} // namespace

CHIP_ERROR
chip::app::DefaultTermsAndConditionsProvider::Init(chip::PersistentStorageDelegate * const inPersistentStorageDelegate,
const chip::Optional<chip::app::TermsAndConditions> & inRequiredAcknowledgements)
{
VerifyOrReturnError(nullptr != inPersistentStorageDelegate, CHIP_ERROR_INVALID_ARGUMENT);

CHIP_ERROR chip::app::DefaultTermsAndConditionsStorageDelegate::Init(PersistentStorageDelegate * const inPersistentStorageDelegate) {
VerifyOrReturnError(nullptr != inPersistentStorageDelegate, CHIP_ERROR_INVALID_ARGUMENT);
mPersistentStorageDelegate = inPersistentStorageDelegate;
mRequiredAcknowledgements = inRequiredAcknowledgements;
return CHIP_NO_ERROR;
}

CHIP_ERROR chip::app::DefaultTermsAndConditionsStorageDelegate::Delete() {
VerifyOrReturnError(nullptr != mPersistentStorageDelegate, CHIP_ERROR_UNINITIALIZED);

const chip::StorageKeyName storageKey = DefaultStorageKeyAllocator::TermsAndConditionsAcceptance();

return mPersistentStorageDelegate->SyncDeleteKeyValue(storageKey.KeyName());
}

CHIP_ERROR chip::app::DefaultTermsAndConditionsStorageDelegate::Get(Optional<TermsAndConditions> & outTermsAndConditions) {
uint8_t serializationVersion = 0;
uint16_t acknowledgements = 0;
uint16_t acknowledgementsVersion = 0;

chip::TLV::TLVReader tlvReader;
chip::TLV::TLVType tlvContainer;

uint8_t buffer[kEstimatedTlvBufferSize] = { 0 };
uint16_t bufferSize = sizeof(buffer);

VerifyOrReturnError(nullptr != mPersistentStorageDelegate, CHIP_ERROR_UNINITIALIZED);

const chip::StorageKeyName storageKey = DefaultStorageKeyAllocator::TermsAndConditionsAcceptance();
CHIP_ERROR err = mPersistentStorageDelegate->SyncGetKeyValue(storageKey.KeyName(), &buffer, bufferSize);
if (CHIP_ERROR_PERSISTED_STORAGE_VALUE_NOT_FOUND == err)
{
return CHIP_ERROR_PERSISTED_STORAGE_VALUE_NOT_FOUND;
}

VerifyOrReturnError(CHIP_NO_ERROR == err, err);

tlvReader.Init(buffer, bufferSize);
ReturnErrorOnFailure(tlvReader.Next(chip::TLV::kTLVType_Structure, chip::TLV::AnonymousTag()));
ReturnErrorOnFailure(tlvReader.EnterContainer(tlvContainer));
ReturnErrorOnFailure(tlvReader.Next(kSerializationVersionTag));
ReturnErrorOnFailure(tlvReader.Get(serializationVersion));
ReturnErrorOnFailure(tlvReader.Next(kAcceptedAcknowledgementsTag));
ReturnErrorOnFailure(tlvReader.Get(acknowledgements));
ReturnErrorOnFailure(tlvReader.Next(kAcceptedAcknowledgementsVersionTag));
ReturnErrorOnFailure(tlvReader.Get(acknowledgementsVersion));
ReturnErrorOnFailure(tlvReader.ExitContainer(tlvContainer));

if (kSerializationVersion != serializationVersion)
{
return CHIP_ERROR_VERSION_MISMATCH;
}

outTermsAndConditions.SetValue({
.value = acknowledgements,
.version = acknowledgementsVersion,
});

return CHIP_NO_ERROR;
}

CHIP_ERROR chip::app::DefaultTermsAndConditionsStorageDelegate::Set(const Optional<TermsAndConditions> & inTermsAndConditions) {
uint8_t buffer[kEstimatedTlvBufferSize] = { 0 };
chip::TLV::TLVWriter tlvWriter;
chip::TLV::TLVType tlvContainer;

VerifyOrReturnError(nullptr != mPersistentStorageDelegate, CHIP_ERROR_UNINITIALIZED);
VerifyOrReturnError(inTermsAndConditions.HasValue(), CHIP_ERROR_INCORRECT_STATE);

tlvWriter.Init(buffer, sizeof(buffer));
ReturnErrorOnFailure(tlvWriter.StartContainer(chip::TLV::AnonymousTag(), chip::TLV::kTLVType_Structure, tlvContainer));
ReturnErrorOnFailure(tlvWriter.Put(kSerializationVersionTag, kSerializationVersion));
ReturnErrorOnFailure(tlvWriter.Put(kAcceptedAcknowledgementsTag, inTermsAndConditions.Value().value));
ReturnErrorOnFailure(tlvWriter.Put(kAcceptedAcknowledgementsVersionTag, inTermsAndConditions.Value().version));
ReturnErrorOnFailure(tlvWriter.EndContainer(tlvContainer));
ReturnErrorOnFailure(tlvWriter.Finalize());
uint32_t lengthWritten = tlvWriter.GetLengthWritten();
VerifyOrReturnError(CanCastTo<uint16_t>(lengthWritten), CHIP_ERROR_BUFFER_TOO_SMALL);

const chip::StorageKeyName storageKey = DefaultStorageKeyAllocator::TermsAndConditionsAcceptance();
ReturnErrorOnFailure(
mPersistentStorageDelegate->SyncSetKeyValue(storageKey.KeyName(), buffer, static_cast<uint16_t>(lengthWritten)));

return CHIP_NO_ERROR;
}

CHIP_ERROR
chip::app::DefaultTermsAndConditionsProvider::Init(TermsAndConditionsStorageDelegate * const inStorageDelegate, const chip::Optional<chip::app::TermsAndConditions> & inRequiredTermsAndConditions)
{
VerifyOrReturnError(nullptr != inStorageDelegate, CHIP_ERROR_INVALID_ARGUMENT);
mTermsAndConditionsStorageDelegate = inStorageDelegate;
mRequiredAcknowledgements = inRequiredTermsAndConditions;

if (CHIP_NO_ERROR == LoadAcceptance(mLatchedAcceptance))
if (CHIP_NO_ERROR == mTermsAndConditionsStorageDelegate->Get(mLatchedTermsAndConditions))
{
mTemporalAcceptance.SetValue(mLatchedAcceptance.Value());
mTemporalAcceptance.SetValue(mLatchedTermsAndConditions.Value());
}

return CHIP_NO_ERROR;
Expand Down Expand Up @@ -104,8 +188,8 @@ CHIP_ERROR chip::app::DefaultTermsAndConditionsProvider::CommitAcceptance()
return CHIP_NO_ERROR;
}

mLatchedAcceptance.SetValue(mTemporalAcceptance.Value());
return StoreAcceptance(mLatchedAcceptance);
mLatchedTermsAndConditions.SetValue(mTemporalAcceptance.Value());
return mTermsAndConditionsStorageDelegate->Set(mLatchedTermsAndConditions);
}

CHIP_ERROR chip::app::DefaultTermsAndConditionsProvider::GetAcceptance(Optional<TermsAndConditions> & outTermsAndConditions) const
Expand All @@ -122,20 +206,18 @@ CHIP_ERROR chip::app::DefaultTermsAndConditionsProvider::GetRequirements(Optiona

CHIP_ERROR chip::app::DefaultTermsAndConditionsProvider::ResetAcceptance()
{
VerifyOrReturnError(nullptr != mPersistentStorageDelegate, CHIP_ERROR_UNINITIALIZED);

const chip::StorageKeyName storageKey = DefaultStorageKeyAllocator::TermsAndConditionsAcceptance();
VerifyOrReturnError(nullptr != mTermsAndConditionsStorageDelegate, CHIP_ERROR_UNINITIALIZED);

ReturnErrorOnFailure(mPersistentStorageDelegate->SyncDeleteKeyValue(storageKey.KeyName()));
mLatchedAcceptance.ClearValue();
ReturnErrorOnFailure(mTermsAndConditionsStorageDelegate->Delete());
mLatchedTermsAndConditions.ClearValue();
mTemporalAcceptance.ClearValue();

return CHIP_NO_ERROR;
}

CHIP_ERROR chip::app::DefaultTermsAndConditionsProvider::RevertAcceptance()
{
mTemporalAcceptance = mLatchedAcceptance;
mTemporalAcceptance = mLatchedTermsAndConditions;
return CHIP_NO_ERROR;
}

Expand All @@ -144,76 +226,3 @@ CHIP_ERROR chip::app::DefaultTermsAndConditionsProvider::SetAcceptance(const Opt
mTemporalAcceptance.SetValue(inTermsAndConditions.Value());
return CHIP_NO_ERROR;
}

CHIP_ERROR chip::app::DefaultTermsAndConditionsProvider::LoadAcceptance(Optional<TermsAndConditions> & outTermsAndConditions)
{
uint8_t serializationVersion = 0;
uint16_t acknowledgements = 0;
uint16_t acknowledgementsVersion = 0;

chip::TLV::TLVReader tlvReader;
chip::TLV::TLVType tlvContainer;

uint8_t buffer[kEstimatedTlvBufferSize] = { 0 };
uint16_t bufferSize = sizeof(buffer);

VerifyOrReturnError(nullptr != mPersistentStorageDelegate, CHIP_ERROR_UNINITIALIZED);

const chip::StorageKeyName storageKey = DefaultStorageKeyAllocator::TermsAndConditionsAcceptance();
CHIP_ERROR err = mPersistentStorageDelegate->SyncGetKeyValue(storageKey.KeyName(), &buffer, bufferSize);
if (CHIP_ERROR_PERSISTED_STORAGE_VALUE_NOT_FOUND == err)
{
return CHIP_ERROR_PERSISTED_STORAGE_VALUE_NOT_FOUND;
}

VerifyOrReturnError(CHIP_NO_ERROR == err, err);

tlvReader.Init(buffer, bufferSize);
ReturnErrorOnFailure(tlvReader.Next(chip::TLV::kTLVType_Structure, chip::TLV::AnonymousTag()));
ReturnErrorOnFailure(tlvReader.EnterContainer(tlvContainer));
ReturnErrorOnFailure(tlvReader.Next(kSerializationVersionTag));
ReturnErrorOnFailure(tlvReader.Get(serializationVersion));
ReturnErrorOnFailure(tlvReader.Next(kAcceptedAcknowledgementsTag));
ReturnErrorOnFailure(tlvReader.Get(acknowledgements));
ReturnErrorOnFailure(tlvReader.Next(kAcceptedAcknowledgementsVersionTag));
ReturnErrorOnFailure(tlvReader.Get(acknowledgementsVersion));
ReturnErrorOnFailure(tlvReader.ExitContainer(tlvContainer));

if (kSerializationVersion != serializationVersion)
{
return CHIP_ERROR_VERSION_MISMATCH;
}

outTermsAndConditions.SetValue({
.value = acknowledgements,
.version = acknowledgementsVersion,
});

return CHIP_NO_ERROR;
}

CHIP_ERROR chip::app::DefaultTermsAndConditionsProvider::StoreAcceptance(const Optional<TermsAndConditions> & inTermsAndConditions)
{
uint8_t buffer[kEstimatedTlvBufferSize] = { 0 };
chip::TLV::TLVWriter tlvWriter;
chip::TLV::TLVType tlvContainer;

VerifyOrReturnError(nullptr != mPersistentStorageDelegate, CHIP_ERROR_UNINITIALIZED);
VerifyOrReturnError(inTermsAndConditions.HasValue(), CHIP_ERROR_INCORRECT_STATE);

tlvWriter.Init(buffer, sizeof(buffer));
ReturnErrorOnFailure(tlvWriter.StartContainer(chip::TLV::AnonymousTag(), chip::TLV::kTLVType_Structure, tlvContainer));
ReturnErrorOnFailure(tlvWriter.Put(kSerializationVersionTag, kSerializationVersion));
ReturnErrorOnFailure(tlvWriter.Put(kAcceptedAcknowledgementsTag, inTermsAndConditions.Value().value));
ReturnErrorOnFailure(tlvWriter.Put(kAcceptedAcknowledgementsVersionTag, inTermsAndConditions.Value().version));
ReturnErrorOnFailure(tlvWriter.EndContainer(tlvContainer));
ReturnErrorOnFailure(tlvWriter.Finalize());
uint32_t lengthWritten = tlvWriter.GetLengthWritten();
VerifyOrReturnError(CanCastTo<uint16_t>(lengthWritten), CHIP_ERROR_BUFFER_TOO_SMALL);

const chip::StorageKeyName storageKey = DefaultStorageKeyAllocator::TermsAndConditionsAcceptance();
ReturnErrorOnFailure(
mPersistentStorageDelegate->SyncSetKeyValue(storageKey.KeyName(), buffer, static_cast<uint16_t>(lengthWritten)));

return CHIP_NO_ERROR;
}
45 changes: 33 additions & 12 deletions src/app/server/DefaultTermsAndConditionsProvider.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,19 +28,45 @@

namespace chip {
namespace app {

class TermsAndConditionsStorageDelegate
{
public:
virtual ~TermsAndConditionsStorageDelegate() = default;

virtual CHIP_ERROR Delete() = 0;

virtual CHIP_ERROR Get(Optional<TermsAndConditions> & inTermsAndConditions) = 0;

virtual CHIP_ERROR Set(const Optional<TermsAndConditions> & outTermsAndConditions) = 0;
};

class DefaultTermsAndConditionsStorageDelegate : public TermsAndConditionsStorageDelegate
{
public:
CHIP_ERROR Init(PersistentStorageDelegate * const inPersistentStorageDelegate);

CHIP_ERROR Delete() override;

CHIP_ERROR Get(Optional<TermsAndConditions> & inTermsAndConditions) override;

CHIP_ERROR Set(const Optional<TermsAndConditions> & outTermsAndConditions) override;

private:
PersistentStorageDelegate * mPersistentStorageDelegate;
};

class DefaultTermsAndConditionsProvider : public TermsAndConditionsProvider
{
public:
/**
* @brief Initializes the TermsAndConditionsProvider.
*
* @param[in] inPersistentStorageDelegate Persistent storage delegate dependency.
* @param[in] inStorageDelegate Storage delegate dependency.
*/
CHIP_ERROR Init(PersistentStorageDelegate * const inPersistentStorageDelegate,
const chip::Optional<chip::app::TermsAndConditions> & inRequiredTermsAndConditions);
CHIP_ERROR Init(TermsAndConditionsStorageDelegate * const inStorageDelegate, const chip::Optional<chip::app::TermsAndConditions> & inRequiredTermsAndConditions);

CHIP_ERROR CheckAcceptance(const Optional<TermsAndConditions> & inTermsAndConditions,
TermsAndConditionsState & outState) const override;
CHIP_ERROR CheckAcceptance(const Optional<TermsAndConditions> & inTermsAndConditions, TermsAndConditionsState & outState) const override;

CHIP_ERROR CommitAcceptance() override;

Expand All @@ -55,13 +81,8 @@ class DefaultTermsAndConditionsProvider : public TermsAndConditionsProvider
CHIP_ERROR SetAcceptance(const Optional<TermsAndConditions> & inTermsAndConditions) override;

private:
CHIP_ERROR LoadAcceptance(Optional<TermsAndConditions> & outTermsAndConditions);

CHIP_ERROR StoreAcceptance(const Optional<TermsAndConditions> & inTermsAndConditions);

PersistentStorageDelegate * mPersistentStorageDelegate;

Optional<TermsAndConditions> mLatchedAcceptance;
TermsAndConditionsStorageDelegate * mTermsAndConditionsStorageDelegate;
Optional<TermsAndConditions> mLatchedTermsAndConditions;
Optional<TermsAndConditions> mTemporalAcceptance;
Optional<TermsAndConditions> mRequiredAcknowledgements;
};
Expand Down
Loading

0 comments on commit 2c625b5

Please sign in to comment.