From patchwork Tue Sep 24 17:24:50 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jacopo Mondi X-Patchwork-Id: 2007 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 A5AC162379 for ; Tue, 24 Sep 2019 19:23:33 +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 4DA0E60005 for ; Tue, 24 Sep 2019 17:23:33 +0000 (UTC) From: Jacopo Mondi To: libcamera-devel@lists.libcamera.org Date: Tue, 24 Sep 2019 19:24:50 +0200 Message-Id: <20190924172503.30864-9-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 08/21] libcamera: v4l2_controls: Implement V4L2ControlList 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:34 -0000 Make V4L2ControlList implement the Serializable interface and implement control list serialization and de-serialization operations. Signed-off-by: Jacopo Mondi --- src/libcamera/include/v4l2_controls.h | 6 ++- src/libcamera/v4l2_controls.cpp | 68 +++++++++++++++++++++++++++ 2 files changed, 73 insertions(+), 1 deletion(-) diff --git a/src/libcamera/include/v4l2_controls.h b/src/libcamera/include/v4l2_controls.h index 8b1a23f37204..739f9f131923 100644 --- a/src/libcamera/include/v4l2_controls.h +++ b/src/libcamera/include/v4l2_controls.h @@ -17,6 +17,7 @@ #include #include +#include namespace libcamera { @@ -46,7 +47,7 @@ private: unsigned int id_; }; -class V4L2ControlList +class V4L2ControlList : public Serializable { public: using iterator = std::vector::iterator; @@ -66,6 +67,9 @@ public: V4L2Control *getByIndex(unsigned int index); V4L2Control *operator[](unsigned int id); + std::unique_ptr serialize() const override; + int deserialize(uint8_t *data, size_t len) override; + private: std::vector controls_; }; diff --git a/src/libcamera/v4l2_controls.cpp b/src/libcamera/v4l2_controls.cpp index 864b0be1e96f..b386d313bc4a 100644 --- a/src/libcamera/v4l2_controls.cpp +++ b/src/libcamera/v4l2_controls.cpp @@ -7,6 +7,10 @@ #include "v4l2_controls.h" +#include + +#include "serializer.h" + /** * \file v4l2_controls.h * \brief Support for V4L2 Controls using the V4L2 Extended Controls APIs @@ -255,4 +259,68 @@ V4L2Control *V4L2ControlList::operator[](unsigned int id) return nullptr; } +/** + * \brief Serialize a V4L2ControlList to a memory buffer + * + * Serialize the control list 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 + * list + */ +std::unique_ptr V4L2ControlList::serialize() const +{ + unsigned int bufferSize = 0; + for (auto it : controls_) + bufferSize += Serializer::size(it); + + std::unique_ptr blob(new DataBlob(bufferSize)); + if (!blob->valid()) + return nullptr; + + uint8_t *b = blob->data(); + for (auto it : controls_) + b += Serializer::serialize(it.id(), it, b); + + return blob; +} + +/** + * \brief De-serialize a V4L2ControlList 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 list from a memory buffer. The memory + * buffer is expected to having been serialized from the + * V4L2ControlList::serialize() operation. + * + * \sa Serializer::deserializeData() + * + * \return 0 on success, a negative error code otherwise + */ +int V4L2ControlList::deserialize(uint8_t *data, size_t len) +{ + uint8_t *b = data; + size_t dataSize = 0; + + while (dataSize < len) { + unsigned int id; + DataValue value; + size_t size; + std::tie(id, value, size) = Serializer::deserializeValue(b); + + add(id, value.getInt()); + + dataSize += size; + b += size; + } + + return 0; +} + } /* namespace libcamera */