From patchwork Tue Sep 24 17:24:52 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jacopo Mondi X-Patchwork-Id: 2009 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 CE04262387 for ; Tue, 24 Sep 2019 19:23:34 +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 7610D60005 for ; Tue, 24 Sep 2019 17:23:34 +0000 (UTC) From: Jacopo Mondi To: libcamera-devel@lists.libcamera.org Date: Tue, 24 Sep 2019 19:24:52 +0200 Message-Id: <20190924172503.30864-11-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 10/21] libcamera: v4l2_controls: Implement V4L2ControlInfoMap 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:35 -0000 Make V4L2ControlInfoMap implement the serializable interface and implement the serialization and deserialization operations. Signed-off-by: Jacopo Mondi --- src/libcamera/include/v4l2_controls.h | 5 +- src/libcamera/v4l2_controls.cpp | 66 +++++++++++++++++++++++++++ 2 files changed, 70 insertions(+), 1 deletion(-) diff --git a/src/libcamera/include/v4l2_controls.h b/src/libcamera/include/v4l2_controls.h index 5b583b9dfda6..3694bb888f2a 100644 --- a/src/libcamera/include/v4l2_controls.h +++ b/src/libcamera/include/v4l2_controls.h @@ -33,7 +33,7 @@ private: unsigned int id_; }; -class V4L2ControlInfoMap +class V4L2ControlInfoMap : public Serializable { private: using InfoMap = std::map; @@ -52,6 +52,9 @@ public: void emplace(unsigned int id, const struct v4l2_query_ext_ctrl &ctrl); + std::unique_ptr serialize() const override; + int deserialize(uint8_t *data, size_t len) override; + private: InfoMap map_; }; diff --git a/src/libcamera/v4l2_controls.cpp b/src/libcamera/v4l2_controls.cpp index c046f88bc7d3..5e1db79e1b96 100644 --- a/src/libcamera/v4l2_controls.cpp +++ b/src/libcamera/v4l2_controls.cpp @@ -167,6 +167,72 @@ void V4L2ControlInfoMap::emplace(unsigned int id, std::forward_as_tuple(ctrl)); } +/** + * \brief Serialize a V4L2ControlInfoMap to a memory buffer + * + * Serialize the v4l2 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 V4L2ControlInfoMap::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 V4L2ControlInfoMap 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 + * V4L2ControlInfoMap::serialize() operation. + * + * \sa Serializer::deserializeInfo() + * + * \return 0 on success, a negative error code otherwise + */ +int V4L2ControlInfoMap::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); + + map_.emplace(std::piecewise_construct, + std::forward_as_tuple(id), + std::forward_as_tuple(id, info.min(), info.max())); + + dataSize += size; + b += size; + } + + return 0; +} + /** * \class V4L2Control * \brief A V4L2 control value