-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[ota] Add OTA requestor storage interface (#15483)
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
1 parent
1434c51
commit 16c7fea
Showing
7 changed files
with
435 additions
and
2 deletions.
There are no files selected for viewing
141 changes: 141 additions & 0 deletions
141
src/app/clusters/ota-requestor/DefaultOTARequestorStorage.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
47
src/app/clusters/ota-requestor/DefaultOTARequestorStorage.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.