[libcamera-devel,08/21] libcamera: v4l2_controls: Implement V4L2ControlList serialization

Message ID 20190924172503.30864-9-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 V4L2ControlList implement the Serializable interface and implement
control list serialization and de-serialization operations.

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

Patch

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 <linux/videodev2.h>
 
 #include <libcamera/data_value.h>
+#include <libcamera/serializable.h>
 
 namespace libcamera {
 
@@ -46,7 +47,7 @@  private:
 	unsigned int id_;
 };
 
-class V4L2ControlList
+class V4L2ControlList : public Serializable
 {
 public:
 	using iterator = std::vector<V4L2Control>::iterator;
@@ -66,6 +67,9 @@  public:
 	V4L2Control *getByIndex(unsigned int index);
 	V4L2Control *operator[](unsigned int id);
 
+	std::unique_ptr<DataBlob> serialize() const override;
+	int deserialize(uint8_t *data, size_t len) override;
+
 private:
 	std::vector<V4L2Control> 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 <libcamera/serializable.h>
+
+#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<DataBlob> V4L2ControlList::serialize() const
+{
+	unsigned int bufferSize = 0;
+	for (auto it : controls_)
+		bufferSize += Serializer::size(it);
+
+	std::unique_ptr<DataBlob> 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 */