From a2f9d9553a3b3c8ae9bb2d76eb539605376eacfb Mon Sep 17 00:00:00 2001 From: Pradip De Date: Thu, 5 Dec 2024 04:45:16 +0000 Subject: [PATCH] Add persistence for Viewport struct attribute --- .../camera-av-stream-management-server.cpp | 52 ++++++++++++++++--- .../camera-av-stream-management-server.h | 13 ++++- src/lib/support/DefaultStorageKeyAllocator.h | 3 ++ 3 files changed, 59 insertions(+), 9 deletions(-) diff --git a/src/app/clusters/camera-av-stream-management-server/camera-av-stream-management-server.cpp b/src/app/clusters/camera-av-stream-management-server/camera-av-stream-management-server.cpp index 0b3f2cbecbd1a6..0014942c3b8cf6 100644 --- a/src/app/clusters/camera-av-stream-management-server/camera-av-stream-management-server.cpp +++ b/src/app/clusters/camera-av-stream-management-server/camera-av-stream-management-server.cpp @@ -26,6 +26,7 @@ #include #include #include +#include #include using namespace chip; @@ -44,7 +45,9 @@ namespace CameraAvStreamManagement { CameraAVStreamMgmtServer::CameraAVStreamMgmtServer(CameraAVStreamMgmtDelegate & aDelegate, EndpointId aEndpointId, ClusterId aClusterId, BitMask 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, @@ -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), @@ -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; } @@ -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; @@ -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(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(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) { diff --git a/src/app/clusters/camera-av-stream-management-server/camera-av-stream-management-server.h b/src/app/clusters/camera-av-stream-management-server/camera-av-stream-management-server.h index b18fe2cddcb58f..11912244540ad9 100644 --- a/src/app/clusters/camera-av-stream-management-server/camera-av-stream-management-server.h +++ b/src/app/clusters/camera-av-stream-management-server/camera-av-stream-management-server.h @@ -22,6 +22,7 @@ #include #include +#include #include namespace chip { @@ -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 @@ -195,7 +199,9 @@ class CameraAVStreamMgmtServer : public CommandHandlerInterface, public Attribut */ CameraAVStreamMgmtServer(CameraAVStreamMgmtDelegate & aDelegate, EndpointId aEndpointId, ClusterId aClusterId, BitMask 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, @@ -276,6 +282,7 @@ class CameraAVStreamMgmtServer : public CommandHandlerInterface, public Attribut ClusterId mClusterId; BitMask mFeature; BitMask mOptionalAttrs; + PersistentStorageDelegate * mPersistentStorage = nullptr; // Attributes with F quality const uint8_t mMaxConcurrentVideoEncoders; @@ -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 */ diff --git a/src/lib/support/DefaultStorageKeyAllocator.h b/src/lib/support/DefaultStorageKeyAllocator.h index 9ed8a2f56cfd77..3f87614ffe3623 100644 --- a/src/lib/support/DefaultStorageKeyAllocator.h +++ b/src/lib/support/DefaultStorageKeyAllocator.h @@ -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