Skip to content

Commit

Permalink
Add persistence for Viewport struct attribute
Browse files Browse the repository at this point in the history
  • Loading branch information
pidarped committed Dec 5, 2024
1 parent 202a4a1 commit a2f9d95
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
#include <app/util/attribute-storage.h>
#include <app/util/util.h>
#include <lib/core/CHIPSafeCasts.h>
#include <lib/support/DefaultStorageKeyAllocator.h>
#include <protocols/interaction_model/StatusCode.h>

using namespace chip;
Expand All @@ -44,7 +45,9 @@ namespace CameraAvStreamManagement {

CameraAVStreamMgmtServer::CameraAVStreamMgmtServer(CameraAVStreamMgmtDelegate & aDelegate, EndpointId aEndpointId,
ClusterId aClusterId, BitMask<Feature> aFeature,
OptionalAttributes aOptionalAttrs, uint8_t aMaxConcurrentVideoEncoders,
OptionalAttributes aOptionalAttrs,
PersistentStorageDelegate & aPersistentStorage,
uint8_t aMaxConcurrentVideoEncoders,
uint32_t aMaxEncodedPixelRate,
const VideoSensorParamsStruct & aVideoSensorParams, bool aNightVisionCapable,
const VideoResolutionStruct & aMinViewPort, uint32_t aMaxContentBufferSize,
Expand All @@ -53,7 +56,7 @@ CameraAVStreamMgmtServer::CameraAVStreamMgmtServer(CameraAVStreamMgmtDelegate &
TwoWayTalkSupportTypeEnum aTwoWayTalkSupport, uint32_t aMaxNetworkBandwidth) :
CommandHandlerInterface(MakeOptional(aEndpointId), aClusterId),
AttributeAccessInterface(MakeOptional(aEndpointId), aClusterId), mDelegate(aDelegate), mEndpointId(aEndpointId),
mClusterId(aClusterId), mFeature(aFeature), mOptionalAttrs(aOptionalAttrs),
mClusterId(aClusterId), mFeature(aFeature), mOptionalAttrs(aOptionalAttrs), mPersistentStorage(&aPersistentStorage),
mMaxConcurrentVideoEncoders(aMaxConcurrentVideoEncoders), mMaxEncodedPixelRate(aMaxEncodedPixelRate),
mVideoSensorParams(aVideoSensorParams), mNightVisionCapable(aNightVisionCapable), mMinViewPort(aMinViewPort),
mMaxContentBufferSize(aMaxContentBufferSize), mMicrophoneCapabilities(aMicrophoneCapabilities),
Expand Down Expand Up @@ -828,9 +831,7 @@ Status CameraAVStreamMgmtServer::SetViewport(const ViewportStruct & aViewport)
{
mViewport = aViewport;

TODO: Persist struct field
//ConcreteAttributePath path = ConcreteAttributePath(mEndpointId, mClusterId, Attributes::Viewport::Id);
//GetSafeAttributePersistenceProvider()->WriteScalarValue(path, mViewport);
StoreViewport(mViewport);
return Protocols::InteractionModel::Status::Success;
}

Expand Down Expand Up @@ -1167,9 +1168,7 @@ void CameraAVStreamMgmtServer::LoadPersistentAttributes()

// Load Viewport
ViewportStruct viewport;
// TODO : Read persisted struct value
//err = GetSafeAttributePersistenceProvider()->ReadScalarValue(
// ConcreteAttributePath(mEndpointId, mClusterId, Attributes::Viewport::Id), viewport);
err = LoadViewport(viewport);
if (err == CHIP_NO_ERROR)
{
mViewport = viewport;
Expand Down Expand Up @@ -1360,6 +1359,43 @@ void CameraAVStreamMgmtServer::LoadPersistentAttributes()
}
}

CHIP_ERROR CameraAVStreamMgmtServer::StoreViewport(const ViewportStruct & viewport)
{
uint8_t buffer[kViewportStructMaxSerializedSize];
TLV::TLVWriter writer;

writer.Init(buffer);
ReturnErrorOnFailure(viewport.Encode(writer, TLV::AnonymousTag()));

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

CHIP_ERROR CameraAVStreamMgmtServer::ClearViewport()
{

return CHIP_NO_ERROR;
}

CHIP_ERROR CameraAVStreamMgmtServer::LoadViewport(ViewportStruct & viewport)
{
uint8_t buffer[kViewportStructMaxSerializedSize];
MutableByteSpan bufferSpan(buffer);
uint16_t size = static_cast<uint16_t>(bufferSpan.size());

ReturnErrorOnFailure(mPersistentStorage->SyncGetKeyValue(DefaultStorageKeyAllocator::CameraAVStreamMgmtViewport().KeyName(),
bufferSpan.data(), size));
bufferSpan.reduce_size(size);

TLV::TLVReader reader;

reader.Init(bufferSpan);
ReturnErrorOnFailure(reader.Next(TLV::AnonymousTag()));
ReturnErrorOnFailure(viewport.Decode(reader));

return CHIP_NO_ERROR;
}

// CommandHandlerInterface
void CameraAVStreamMgmtServer::InvokeCommand(HandlerContext & handlerContext)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#include <app/AttributeAccessInterface.h>
#include <app/CommandHandlerInterface.h>

#include <lib/core/CHIPPersistentStorageDelegate.h>
#include <protocols/interaction_model/StatusCode.h>

namespace chip {
Expand All @@ -42,6 +43,9 @@ constexpr uint8_t kMaxSpeakerLevel = 254;
constexpr uint8_t kMaxMicrophoneLevel = 254;
constexpr uint16_t kMaxImageRotationDegrees = 359;

constexpr size_t kViewportStructMaxSerializedSize =
TLV::EstimateStructOverhead(sizeof(uint16_t) * 4);

class CameraAVStreamMgmtServer;

/** @brief
Expand Down Expand Up @@ -195,7 +199,9 @@ class CameraAVStreamMgmtServer : public CommandHandlerInterface, public Attribut
*/
CameraAVStreamMgmtServer(CameraAVStreamMgmtDelegate & aDelegate, EndpointId aEndpointId,
ClusterId aClusterId, BitMask<Feature> aFeature,
OptionalAttributes aOptionalAttrs, uint8_t aMaxConcurrentVideoEncoders,
OptionalAttributes aOptionalAttrs,
PersistentStorageDelegate & aPersistentStorage,
uint8_t aMaxConcurrentVideoEncoders,
uint32_t aMaxEncodedPixelRate,
const VideoSensorParamsStruct & aVideoSensorParams, bool aNightVisionCapable,
const VideoResolutionStruct & aMinViewPort, uint32_t aMaxContentBufferSize,
Expand Down Expand Up @@ -276,6 +282,7 @@ class CameraAVStreamMgmtServer : public CommandHandlerInterface, public Attribut
ClusterId mClusterId;
BitMask<Feature> mFeature;
BitMask<OptionalAttributes> mOptionalAttrs;
PersistentStorageDelegate * mPersistentStorage = nullptr;

// Attributes with F quality
const uint8_t mMaxConcurrentVideoEncoders;
Expand Down Expand Up @@ -342,6 +349,10 @@ class CameraAVStreamMgmtServer : public CommandHandlerInterface, public Attribut

CHIP_ERROR ReadAndEncodeRankedVideoStreamPrioritiesList(const AttributeValueEncoder::ListEncodeHelper & encoder);

CHIP_ERROR StoreViewport(const ViewportStruct & viewport);
CHIP_ERROR ClearViewport();
CHIP_ERROR LoadViewport(ViewportStruct & viewport);

/**
* @brief Inherited from CommandHandlerInterface
*/
Expand Down
3 changes: 3 additions & 0 deletions src/lib/support/DefaultStorageKeyAllocator.h
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,9 @@ class DefaultStorageKeyAllocator
// when new fabric is created, this list needs to be updated,
// when client init DefaultICDClientStorage, this table needs to be loaded.
static StorageKeyName ICDFabricList() { return StorageKeyName::FromConst("g/icdfl"); }

// CameraAvStreamManagement cluster
static StorageKeyName CameraAVStreamMgmtViewport() { return StorageKeyName::FromConst("g/cam/vprt"); }
};

} // namespace chip

0 comments on commit a2f9d95

Please sign in to comment.