[libcamera-devel,02/21] libcamera: Define interface for serializable data types

Message ID 20190924172503.30864-3-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
Define an interface to be implemented by data types that support
serialization and deserialization of their data to a memory buffer.

Signed-off-by: Jacopo Mondi <jacopo@jmondi.org>
---
 include/libcamera/meson.build    |   1 +
 include/libcamera/serializable.h |  44 +++++++++++++
 src/libcamera/meson.build        |   1 +
 src/libcamera/serializable.cpp   | 110 +++++++++++++++++++++++++++++++
 4 files changed, 156 insertions(+)
 create mode 100644 include/libcamera/serializable.h
 create mode 100644 src/libcamera/serializable.cpp

Patch

diff --git a/include/libcamera/meson.build b/include/libcamera/meson.build
index e3f3ad504446..7790bfb4907b 100644
--- a/include/libcamera/meson.build
+++ b/include/libcamera/meson.build
@@ -13,6 +13,7 @@  libcamera_api = files([
     'object.h',
     'request.h',
     'signal.h',
+    'serializable.h',
     'stream.h',
     'timer.h',
 ])
diff --git a/include/libcamera/serializable.h b/include/libcamera/serializable.h
new file mode 100644
index 000000000000..fae2ea0fb8fe
--- /dev/null
+++ b/include/libcamera/serializable.h
@@ -0,0 +1,44 @@ 
+/* SPDX-License-Identifier: LGPL-2.1-or-later */
+/*
+ * Copyright (C) 2019, Google Inc.
+ *
+ * serializable.h - Serializable data types
+ */
+#ifndef __LIBCAMERA_SERIALIZABLE_H__
+#define __LIBCAMERA_SERIALIZABLE_H__
+
+#include <cstdint>
+#include <memory>
+
+#include <libcamera/data_value.h>
+
+namespace libcamera {
+
+class DataBlob
+{
+public:
+	DataBlob(size_t blobSize);
+	~DataBlob();
+
+	uint8_t *data() const { return data_; }
+	size_t size() const { return size_; }
+	bool valid() const { return valid_; }
+
+private:
+	uint8_t *data_;
+	size_t size_;
+	bool valid_;
+};
+
+class Serializable
+{
+public:
+	virtual ~Serializable() {}
+
+	virtual std::unique_ptr<DataBlob> serialize() const = 0;
+	virtual int deserialize(uint8_t *data, size_t len) = 0;
+};
+
+} /* namespace libcamera */
+
+#endif /* __LIBCAMERA_SERIALIZABLE_H__ */
diff --git a/src/libcamera/meson.build b/src/libcamera/meson.build
index c4fcd0569bd7..973b20269943 100644
--- a/src/libcamera/meson.build
+++ b/src/libcamera/meson.build
@@ -27,6 +27,7 @@  libcamera_sources = files([
     'process.cpp',
     'request.cpp',
     'signal.cpp',
+    'serializable.cpp',
     'stream.cpp',
     'thread.cpp',
     'timer.cpp',
diff --git a/src/libcamera/serializable.cpp b/src/libcamera/serializable.cpp
new file mode 100644
index 000000000000..4e075baca56a
--- /dev/null
+++ b/src/libcamera/serializable.cpp
@@ -0,0 +1,110 @@ 
+/* SPDX-License-Identifier: LGPL-2.1-or-later */
+/*
+ * Copyright (C) 2019, Google Inc.
+ *
+ * serializable.cpp - Serializable data types
+ */
+
+#include <libcamera/serializable.h>
+
+#include <string.h>
+
+/**
+ * \file serializable.h
+ * \brief Interface for serializable data types
+ */
+
+namespace libcamera {
+
+/**
+ * \class DataBlob
+ * \brief Wraps a memory buffer containing serialized data to provide automatic
+ * allocation and deletion
+ */
+
+/**
+ * \brief Construct a DataBlob reserving a buffer of size \a blobSize
+ * \param[in] blobSize The data buffer memory size
+ *
+ * Constructing a DataBlob allocates the memory area where to serialize
+ * data. If memory allocation is successful the data blob is valid and its size
+ * is set to \a blobSize.
+ */
+DataBlob::DataBlob(size_t blobSize)
+	: data_(nullptr), size_(0), valid_(false)
+{
+	data_ = new uint8_t[blobSize];
+	if (!data_)
+		return;
+
+	::memset(data_, 0, blobSize);
+
+	valid_ = true;
+	size_ = blobSize;
+}
+
+/**
+ * \brief Destroy the DataBlob and release the memory area it wraps
+ */
+DataBlob::~DataBlob()
+{
+	if (data_)
+		delete[] data_;
+}
+
+/**
+ * \fn DataBlob::data()
+ * \brief Retrieve a pointer to the memory area the DataBlob wraps
+ * \return A pointer to the wrapped memory area
+ */
+
+/**
+ * \fn DataBlob::size()
+ * \brief Retrieve the size of the wrapped memory area
+ * \return The size of the wrapped memory area in bytes, 0 if memory reservation
+ * failed
+ */
+
+/**
+ * \fn DataBlob::valid()
+ * \brief Retrieve if a DataBlob is valid
+ * \return True if the memory area was correctly reserved, false otherwise
+ */
+
+/**
+ * \class Serializable
+ * \brief Interface for serializable data types
+ *
+ * Classes that can be serialized to a binary memory buffer shall implement
+ * the interface defined by this class.
+ */
+
+/**
+ * \fn Serializable::serialize()
+ * \brief Serialize data to a memory buffer
+ *
+ * Implementations of this pure virtual operation serialize the content of
+ * the derived class to a memory buffer.
+ *
+ * The ownership of the DataBlob containing the serialized data is returned to
+ * the caller, which is responsible for its life cycle management.
+ */
+
+/**
+ * \fn Serializable::deserialize()
+ * \brief Deserialize from data in a memory buffer
+ * \param[in] data The memory buffer containing serialized control info data
+ * \param[in] len The memory buffer length in bytes
+ *
+ *
+ * Implementations of this pure virtual operation de-serialize the content of
+ * the derived class from a memory buffer.
+ *
+ * The ownership of the DataBlob \a blob containing data to de-serialize is
+ * passed to this operation which destroys its content after having
+ * de-serialized it.
+ *
+ * \return 0 on success, a negative error coder otherwise
+ */
+
+} /* namespace libcamera */