[libcamera-devel,10/21] libcamera: v4l2_controls: Implement V4L2ControlInfoMap serialization

Message ID 20190924172503.30864-11-jacopo@jmondi.org
State Superseded
Headers show
Series
  • Implement control serialization
Related show

Commit Message

Jacopo Mondi Sept. 24, 2019, 5:24 p.m. UTC
Make V4L2ControlInfoMap implement the serializable interface and
implement the serialization and deserialization operations.

Signed-off-by: Jacopo Mondi <jacopo@jmondi.org>
---
 src/libcamera/include/v4l2_controls.h |  5 +-
 src/libcamera/v4l2_controls.cpp       | 66 +++++++++++++++++++++++++++
 2 files changed, 70 insertions(+), 1 deletion(-)

Patch

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<unsigned int, V4L2ControlInfo>;
@@ -52,6 +52,9 @@  public:
 
 	void emplace(unsigned int id, const struct v4l2_query_ext_ctrl &ctrl);
 
+	std::unique_ptr<DataBlob> 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<DataBlob> V4L2ControlInfoMap::serialize() const
+{
+	unsigned int bufferSize = 0;
+	for (auto it : map_)
+		bufferSize += Serializer::size(it.second);
+
+	std::unique_ptr<DataBlob> 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