[libcamera-devel,06/21] libcamera: controls: Implement ControlInfoMap serialization

Message ID 20190924172503.30864-7-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 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 <jacopo@jmondi.org>
---
 include/libcamera/controls.h |  5 ++-
 src/libcamera/controls.cpp   | 67 ++++++++++++++++++++++++++++++++++++
 2 files changed, 71 insertions(+), 1 deletion(-)

Patch

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<ControlId, ControlInfo>;
@@ -62,6 +62,9 @@  public:
 	void emplace(const ControlId id, const DataValue &min,
 		     const DataValue &max);
 
+	std::unique_ptr<DataBlob> 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<DataBlob> ControlInfoMap::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 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<ControlId>(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