From patchwork Tue Sep 24 17:24:48 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jacopo Mondi X-Patchwork-Id: 2005 Return-Path: Received: from relay3-d.mail.gandi.net (relay3-d.mail.gandi.net [217.70.183.195]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id 84DBE62378 for ; Tue, 24 Sep 2019 19:23:32 +0200 (CEST) X-Originating-IP: 213.45.248.89 Received: from uno.homenet.telecomitalia.it (host89-248-dynamic.45-213-r.retail.telecomitalia.it [213.45.248.89]) (Authenticated sender: jacopo@jmondi.org) by relay3-d.mail.gandi.net (Postfix) with ESMTPSA id 2E07460005 for ; Tue, 24 Sep 2019 17:23:31 +0000 (UTC) From: Jacopo Mondi To: libcamera-devel@lists.libcamera.org Date: Tue, 24 Sep 2019 19:24:48 +0200 Message-Id: <20190924172503.30864-7-jacopo@jmondi.org> X-Mailer: git-send-email 2.23.0 In-Reply-To: <20190924172503.30864-1-jacopo@jmondi.org> References: <20190924172503.30864-1-jacopo@jmondi.org> MIME-Version: 1.0 Subject: [libcamera-devel] [PATCH 06/21] libcamera: controls: Implement ControlInfoMap serialization X-BeenThere: libcamera-devel@lists.libcamera.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Tue, 24 Sep 2019 17:23:32 -0000 Make ControlInfoMap class implement the Serializable interface and implement the serializable() and deserialize() operations that dumps all control info to a memory blob and re-create a ControlInfoMap from a memory buffer. Signed-off-by: Jacopo Mondi --- include/libcamera/controls.h | 5 ++- src/libcamera/controls.cpp | 67 ++++++++++++++++++++++++++++++++++++ 2 files changed, 71 insertions(+), 1 deletion(-) diff --git a/include/libcamera/controls.h b/include/libcamera/controls.h index 79731c4932ae..a5b5f245ef60 100644 --- a/include/libcamera/controls.h +++ b/include/libcamera/controls.h @@ -42,7 +42,7 @@ private: const struct ControlMetadata *meta_; }; -class ControlInfoMap +class ControlInfoMap : public Serializable { private: using InfoMap = std::unordered_map; @@ -62,6 +62,9 @@ public: void emplace(const ControlId id, const DataValue &min, const DataValue &max); + std::unique_ptr serialize() const override; + int deserialize(uint8_t *data, size_t len) override; + private: InfoMap map_; }; diff --git a/src/libcamera/controls.cpp b/src/libcamera/controls.cpp index ec7ac7ba9ed6..4d51efd53fab 100644 --- a/src/libcamera/controls.cpp +++ b/src/libcamera/controls.cpp @@ -239,6 +239,73 @@ void ControlInfoMap::emplace(const ControlId id, const DataValue &min, std::forward_as_tuple(id, min, max)); } +/** + * \brief Serialize a ControlInfoMap to a memory buffer + * + * Serialize the control info map to a DataBlob and return the ownership + * of the blob to the caller. + * + * The memory format used to serialize each control to memory is defined by + * the Serializer helper class. + * + * \sa Serializer::serialize() + * + * \return A unique pointer to a DataBlob containing the serialized control + * info map + */ +std::unique_ptr ControlInfoMap::serialize() const +{ + unsigned int bufferSize = 0; + for (auto it : map_) + bufferSize += Serializer::size(it.second); + + std::unique_ptr blob(new DataBlob(bufferSize)); + if (!blob->valid()) + return nullptr; + + uint8_t *b = blob->data(); + for (auto it : map_) + b += Serializer::serialize(it.first, it.second, b); + + return blob; +} + +/** + * \brief De-serialize a ControlInfoMap from a memory buffer + * \param[in] data The memory buffer containing serialized control info data + * \param[in] len The memory buffer length in bytes + * + * De-serialize the content of the control info map from a memory buffer. The + * memory buffer is expected to having been serialized from the + * ControlInfoMap::serialize() operation. + * + * \sa Serializer::deserializeInfo() + * + * \return 0 on success, a negative error code otherwise + */ +int ControlInfoMap::deserialize(uint8_t *data, size_t len) +{ + uint8_t *b = data; + size_t dataSize = 0; + + while (dataSize < len) { + unsigned int id; + DataInfo info; + size_t size; + std::tie(id, info, size) = Serializer::deserializeInfo(b); + + ControlId cId = static_cast(id); + map_.emplace(std::piecewise_construct, + std::forward_as_tuple(cId), + std::forward_as_tuple(cId, info.min(), info.max())); + + dataSize += size; + b += size; + } + + return 0; +} + /** * \typedef ControlValue * \brief A control value stored in the ControlList class