Skip to content

Commit

Permalink
[ota] Add OTA requestor storage interface (#15483)
Browse files Browse the repository at this point in the history
Add functions for storing and loading the default OTA
providers, the current OTA provider and the update token.
Also, provide the default implementation based on the
peristent storage delegate.
  • Loading branch information
Damian-Nordic authored Mar 8, 2022
1 parent 1434c51 commit 16c7fea
Show file tree
Hide file tree
Showing 7 changed files with 435 additions and 2 deletions.
141 changes: 141 additions & 0 deletions src/app/clusters/ota-requestor/DefaultOTARequestorStorage.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
/*
*
* 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 "DefaultOTARequestorStorage.h"
#include "OTARequestorInterface.h"

#include <lib/core/CHIPConfig.h>
#include <lib/core/CHIPPersistentStorageDelegate.h>
#include <lib/core/CHIPTLV.h>
#include <lib/support/DefaultStorageKeyAllocator.h>

#include <limits>

namespace chip {

// Calculated with Python code:
// w = TLVWriter()
// s = {1:uint(0xffffffffffffffff), 2:uint(0xffff), 254:uint(0xff)}
// w.put(None, s)
// len(w.encoding)
constexpr size_t kProviderMaxSerializedSize = 19u;

// Multiply the serialized provider size by the maximum number of fabrics and add 2 bytes for the array start and end.
constexpr size_t kProviderListMaxSerializedSize = kProviderMaxSerializedSize * CHIP_CONFIG_MAX_FABRICS + 2;

CHIP_ERROR DefaultOTARequestorStorage::StoreDefaultProviders(const ProviderLocationList & providers)
{
uint8_t buffer[kProviderListMaxSerializedSize];
TLV::TLVWriter writer;
TLV::TLVType outerType;

writer.Init(buffer);
ReturnErrorOnFailure(writer.StartContainer(TLV::AnonymousTag(), TLV::kTLVType_Array, outerType));

for (auto providerIter = providers.Begin(); providerIter.Next();)
{
const auto & provider = providerIter.GetValue();
ReturnErrorOnFailure(provider.EncodeForRead(writer, TLV::AnonymousTag(), provider.fabricIndex));
}

ReturnErrorOnFailure(writer.EndContainer(outerType));

return mPersistentStorage->SyncSetKeyValue(DefaultStorageKeyAllocator().OTADefaultProviders(), buffer,
static_cast<uint16_t>(writer.GetLengthWritten()));
}

CHIP_ERROR DefaultOTARequestorStorage::LoadDefaultProviders(ProviderLocationList & providers)
{
uint8_t buffer[kProviderListMaxSerializedSize];
uint16_t size = sizeof(buffer);

ReturnErrorOnFailure(mPersistentStorage->SyncGetKeyValue(DefaultStorageKeyAllocator().OTADefaultProviders(), buffer, size));

TLV::TLVReader reader;
TLV::TLVType outerType;

reader.Init(buffer, size);
ReturnErrorOnFailure(reader.Next(TLV::TLVType::kTLVType_Array, TLV::AnonymousTag()));
ReturnErrorOnFailure(reader.EnterContainer(outerType));

while (reader.Next() != CHIP_ERROR_END_OF_TLV)
{
ProviderLocationType provider;
ReturnErrorOnFailure(provider.Decode(reader));
providers.Add(provider);
}

ReturnErrorOnFailure(reader.ExitContainer(outerType));

return CHIP_NO_ERROR;
}

CHIP_ERROR DefaultOTARequestorStorage::StoreCurrentProviderLocation(const ProviderLocationType & provider)
{
uint8_t buffer[kProviderMaxSerializedSize];
TLV::TLVWriter writer;

writer.Init(buffer);
ReturnErrorOnFailure(provider.EncodeForRead(writer, TLV::AnonymousTag(), provider.fabricIndex));

return mPersistentStorage->SyncSetKeyValue(DefaultStorageKeyAllocator().OTACurrentProvider(), buffer,
static_cast<uint16_t>(writer.GetLengthWritten()));
}

CHIP_ERROR DefaultOTARequestorStorage::ClearCurrentProviderLocation()
{
return mPersistentStorage->SyncDeleteKeyValue(DefaultStorageKeyAllocator().OTACurrentProvider());
}

CHIP_ERROR DefaultOTARequestorStorage::LoadCurrentProviderLocation(ProviderLocationType & provider)
{
uint8_t buffer[kProviderMaxSerializedSize];
uint16_t size = sizeof(buffer);

ReturnErrorOnFailure(mPersistentStorage->SyncGetKeyValue(DefaultStorageKeyAllocator().OTACurrentProvider(), buffer, size));

TLV::TLVReader reader;

reader.Init(buffer, size);
ReturnErrorOnFailure(reader.Next(TLV::AnonymousTag()));
ReturnErrorOnFailure(provider.Decode(reader));

return CHIP_NO_ERROR;
}

CHIP_ERROR DefaultOTARequestorStorage::StoreUpdateToken(ByteSpan updateToken)
{
return mPersistentStorage->SyncSetKeyValue(DefaultStorageKeyAllocator().OTAUpdateToken(), updateToken.data(),
static_cast<uint16_t>(updateToken.size()));
}

CHIP_ERROR DefaultOTARequestorStorage::ClearUpdateToken()
{
return mPersistentStorage->SyncDeleteKeyValue(DefaultStorageKeyAllocator().OTAUpdateToken());
}

CHIP_ERROR DefaultOTARequestorStorage::LoadUpdateToken(MutableByteSpan & updateToken)
{
uint16_t size = static_cast<uint16_t>(updateToken.size());
CHIP_ERROR error = mPersistentStorage->SyncGetKeyValue(DefaultStorageKeyAllocator().OTAUpdateToken(), updateToken.data(), size);

updateToken.reduce_size(size);
return error;
}

} // namespace chip
47 changes: 47 additions & 0 deletions src/app/clusters/ota-requestor/DefaultOTARequestorStorage.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
*
* 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 "OTARequestorStorage.h"

namespace chip {

class PersistentStorageDelegate;

class DefaultOTARequestorStorage : public OTARequestorStorage
{
public:
void Init(PersistentStorageDelegate & persistentStorage) { mPersistentStorage = &persistentStorage; }

CHIP_ERROR StoreDefaultProviders(const ProviderLocationList & provider) override;
CHIP_ERROR LoadDefaultProviders(ProviderLocationList & providers) override;

CHIP_ERROR StoreCurrentProviderLocation(const ProviderLocationType & provider) override;
CHIP_ERROR ClearCurrentProviderLocation() override;
CHIP_ERROR LoadCurrentProviderLocation(ProviderLocationType & provider) override;

CHIP_ERROR StoreUpdateToken(ByteSpan updateToken) override;
CHIP_ERROR ClearUpdateToken() override;
CHIP_ERROR LoadUpdateToken(MutableByteSpan & updateToken) override;

private:
PersistentStorageDelegate * mPersistentStorage = nullptr;
};

} // namespace chip
7 changes: 5 additions & 2 deletions src/app/clusters/ota-requestor/OTARequestorInterface.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,18 @@
*/

#include <app-common/zap-generated/cluster-objects.h>
#include <app/AttributeAccessInterface.h>
#include <app/CommandHandler.h>
#include <app/util/af-enums.h>
#include <lib/core/ClusterEnums.h>

#pragma once

namespace chip {

namespace app {
class CommandHandler;
struct ConcreteCommandPath;
} // namespace app

/**
* A class to represent a list of the provider locations
*/
Expand Down
47 changes: 47 additions & 0 deletions src/app/clusters/ota-requestor/OTARequestorStorage.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
*
* 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 <app-common/zap-generated/cluster-objects.h>
#include <lib/support/Span.h>

namespace chip {

class ProviderLocationList;

class OTARequestorStorage
{
public:
using ProviderLocationType = app::Clusters::OtaSoftwareUpdateRequestor::Structs::ProviderLocation::Type;

virtual ~OTARequestorStorage() {}

virtual CHIP_ERROR StoreDefaultProviders(const ProviderLocationList & provider) = 0;
virtual CHIP_ERROR LoadDefaultProviders(ProviderLocationList & providers) = 0;

virtual CHIP_ERROR StoreCurrentProviderLocation(const ProviderLocationType & provider) = 0;
virtual CHIP_ERROR ClearCurrentProviderLocation() = 0;
virtual CHIP_ERROR LoadCurrentProviderLocation(ProviderLocationType & provider) = 0;

virtual CHIP_ERROR StoreUpdateToken(ByteSpan updateToken) = 0;
virtual CHIP_ERROR LoadUpdateToken(MutableByteSpan & updateToken) = 0;
virtual CHIP_ERROR ClearUpdateToken() = 0;
};

} // namespace chip
15 changes: 15 additions & 0 deletions src/app/tests/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,19 @@ source_set("binding-test-srcs") {
]
}

source_set("ota-requestor-test-srcs") {
sources = [
"${chip_root}/src/app/clusters/ota-requestor/DefaultOTARequestorStorage.cpp",
"${chip_root}/src/app/clusters/ota-requestor/DefaultOTARequestorStorage.h",
"${chip_root}/src/app/clusters/ota-requestor/OTARequestorStorage.h",
]

public_deps = [
"${chip_root}/src/app/common:cluster-objects",
"${chip_root}/src/lib/core",
]
}

chip_test_suite("tests") {
output_name = "libAppTests"

Expand All @@ -65,6 +78,7 @@ chip_test_suite("tests") {
"TestCommandInteraction.cpp",
"TestCommandPathParams.cpp",
"TestDataModelSerialization.cpp",
"TestDefaultOTARequestorStorage.cpp",
"TestEventLogging.cpp",
"TestEventOverflow.cpp",
"TestEventPathParams.cpp",
Expand Down Expand Up @@ -93,6 +107,7 @@ chip_test_suite("tests") {

public_deps = [
":binding-test-srcs",
":ota-requestor-test-srcs",
"${chip_root}/src/app",
"${chip_root}/src/app/common:cluster-objects",
"${chip_root}/src/app/tests:helpers",
Expand Down
Loading

0 comments on commit 16c7fea

Please sign in to comment.