From patchwork Tue Sep 24 17:24:44 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jacopo Mondi X-Patchwork-Id: 2001 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 7C93762379 for ; Tue, 24 Sep 2019 19:23:29 +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 0FD0360005 for ; Tue, 24 Sep 2019 17:23:28 +0000 (UTC) From: Jacopo Mondi To: libcamera-devel@lists.libcamera.org Date: Tue, 24 Sep 2019 19:24:44 +0200 Message-Id: <20190924172503.30864-3-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 02/21] libcamera: Define interface for serializable data types 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:29 -0000 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 --- 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 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 +#include + +#include + +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 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 + +#include + +/** + * \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 */