Skip to content

Commit

Permalink
introduce libcyphal config #verification #sonar #docs
Browse files Browse the repository at this point in the history
  • Loading branch information
serges147 committed Nov 29, 2024
1 parent 215b27c commit 9ab3f9d
Show file tree
Hide file tree
Showing 17 changed files with 177 additions and 33 deletions.
5 changes: 3 additions & 2 deletions include/libcyphal/application/node/heartbeat_producer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#ifndef LIBCYPHAL_APPLICATION_NODE_HEARTBEAT_PRODUCER_HPP_INCLUDED
#define LIBCYPHAL_APPLICATION_NODE_HEARTBEAT_PRODUCER_HPP_INCLUDED

#include "libcyphal/config.hpp"
#include "libcyphal/executor.hpp"
#include "libcyphal/presentation/presentation.hpp"
#include "libcyphal/presentation/publisher.hpp"
Expand Down Expand Up @@ -112,8 +113,8 @@ class HeartbeatProducer final // NOSONAR cpp:S4963
///
/// The size of the function is arbitrary (4 pointers), but should be enough for simple lambdas.
///
static constexpr std::size_t FunctionSize = sizeof(void*) * 4;
using Function = cetl::pmr::function<void(const Arg& arg), FunctionSize>;
static constexpr auto FunctionSize = config::application::node::HeartbeatProducer_UpdateCallback_FunctionSize;
using Function = cetl::pmr::function<void(const Arg& arg), FunctionSize>;
};

/// @brief Sets the message update callback for the heartbeat.
Expand Down
115 changes: 115 additions & 0 deletions include/libcyphal/config.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
/// @copyright
/// Copyright (C) OpenCyphal Development Team <opencyphal.org>
/// Copyright Amazon.com Inc. or its affiliates.
/// SPDX-License-Identifier: MIT

#ifndef LIBCYPHAL_CONFIG_HPP_INCLUDED
#define LIBCYPHAL_CONFIG_HPP_INCLUDED

#include <cstddef>

namespace libcyphal
{
namespace config
{

/// Defines max footprint of a callback function in use by the executor.
/// Size is chosen arbitrary, but it should be enough to store any lambda or function pointer.
///
constexpr std::size_t IExecutor_Callback_FunctionMaxSize = sizeof(void*) * 8;

/// Defines footprint size reserved for a callback implementation.
/// The actual max footprint for the callback implementation is `sizeof(IExecutor::Function)` bigger,
/// and it depends on `Cfg_IExecutor_Callback_FunctionMaxSize`.
///
constexpr std::size_t IExecutor_Callback_ReserveSize = sizeof(void*) * 16;

namespace application
{
namespace node
{

/// Defines max footprint of a callback function in use by the heartbeat producer.
/// Size is chosen arbitrary, but it should be enough to store any lambda or function pointer.
///
constexpr std::size_t HeartbeatProducer_UpdateCallback_FunctionSize = sizeof(void*) * 4;

} // namespace node
} // namespace application

namespace presentation
{

/// Defines the size of a payload which is considered as a small one,
/// and therefore could be used with stack buffer. Any payload larger than this size will be PMR allocated.
///
constexpr std::size_t SmallPayloadSize = 256;

/// Defines max footprint of a callback function in use by the RPC client response promise.
/// Size is chosen arbitrary, but it should be enough to store any lambda or function pointer.
///
constexpr std::size_t ResponsePromiseBase_Callback_FunctionSize = sizeof(void*) * 4;

/// Defines max footprint of a callback function in use by the RPC server response continuation.
/// Size is chosen arbitrary, but it should be enough to store any lambda or function pointer.
///
constexpr std::size_t ServerBase_ContinuationImpl_FunctionMaxSize = sizeof(void*) * 5;

/// Defines max footprint of a callback function in use by the RPC server request notification.
/// Size is chosen arbitrary, but it should be enough to store any lambda or function pointer.
///
constexpr std::size_t ServerBase_OnRequestCallback_FunctionMaxSize = sizeof(void*) * 5;

/// Defines max footprint of a callback function in use by the message subscriber receive notification.
/// Size is chosen arbitrary, but it should be enough to store any lambda or function pointer.
///
constexpr std::size_t Subscriber_OnReceiveCallback_FunctionMaxSize = sizeof(void*) * 4;

} // namespace presentation

namespace transport
{

/// Defines max footprint of a platform-specific error implementation.
///
constexpr std::size_t PlatformErrorMaxSize = sizeof(void*) * 3;

/// Defines max footprint of a callback function in use by the message RX session notification.
/// Size is chosen arbitrary, but it should be enough to store any lambda or function pointer.
///
constexpr std::size_t IMessageRxSession_OnReceiveCallback_FunctionMaxSize = sizeof(void*) * 4;

/// Defines max footprint of a callback function in use by the service RX session notification.
/// Size is chosen arbitrary, but it should be enough to store any lambda or function pointer.
///
constexpr std::size_t ISvcRxSession_OnReceiveCallback_FunctionMaxSize = sizeof(void*) * 4;

/// Defines max footprint of a storage implementation used by the scattered buffer.
///
constexpr std::size_t ScatteredBuffer_StorageVariantFootprint = sizeof(void*) * 8;

namespace can
{

/// Defines max footprint of a callback function in use by the CAN transport transient error handler.
/// Size is chosen arbitrary, but it should be enough to store any lambda or function pointer.
///
constexpr std::size_t ICanTransport_TransientErrorHandlerMaxSize = sizeof(void*) * 3;

} // namespace can

namespace udp
{

/// Defines max footprint of a callback function in use by the UDP transport transient error handler.
/// Size is chosen arbitrary, but it should be enough to store any lambda or function pointer.
///
constexpr std::size_t IUdpTransport_TransientErrorHandlerMaxSize = sizeof(void*) * 3;

} // namespace udp
} // namespace transport

} // namespace config
} // namespace libcyphal

#endif // LIBCYPHAL_CONFIG_HPP_INCLUDED
5 changes: 3 additions & 2 deletions include/libcyphal/executor.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#ifndef LIBCYPHAL_EXECUTOR_HPP_INCLUDED
#define LIBCYPHAL_EXECUTOR_HPP_INCLUDED

#include "config.hpp"
#include "time_provider.hpp"
#include "types.hpp"

Expand Down Expand Up @@ -71,7 +72,7 @@ class IExecutor : public cetl::rtti::rtti, public ITimeProvider
///
/// Size is chosen arbitrary, but it should be enough to store any lambda or function pointer.
///
static constexpr std::size_t FunctionMaxSize = sizeof(void*) * 8;
static constexpr auto FunctionMaxSize = config::IExecutor_Callback_FunctionMaxSize;

/// @brief Defines type of callback `Function` single argument.
///
Expand Down Expand Up @@ -103,7 +104,7 @@ class IExecutor : public cetl::rtti::rtti, public ITimeProvider
///
/// Size is chosen arbitrary, but it should be enough to store any callback implementation.
///
static constexpr std::size_t MaxSize = (sizeof(void*) * 16) + sizeof(Function);
static constexpr auto MaxSize = config::IExecutor_Callback_ReserveSize + sizeof(Function);

class Interface : public rtti
{
Expand Down
12 changes: 6 additions & 6 deletions include/libcyphal/presentation/common_helpers.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#ifndef LIBCYPHAL_PRESENTATION_COMMON_HELPERS_HPP_INCLUDED
#define LIBCYPHAL_PRESENTATION_COMMON_HELPERS_HPP_INCLUDED

#include "libcyphal/config.hpp"
#include "libcyphal/errors.hpp"
#include "libcyphal/transport/scattered_buffer.hpp"

Expand All @@ -29,8 +30,6 @@ namespace detail

using DeserializationFailure = cetl::variant<MemoryError, nunavut::support::Error>;

constexpr std::size_t SmallPayloadSize = 256;

template <typename Message>
static cetl::optional<DeserializationFailure> tryDeserializePayload(const transport::ScatteredBuffer& payload,
cetl::pmr::memory_resource& memory,
Expand All @@ -42,13 +41,14 @@ static cetl::optional<DeserializationFailure> tryDeserializePayload(const transp
// (using `Message::_traits_::ExtentBytes` as the maximum possible size for the Message).
// But this might be dangerous (stack overflow!) in case of large messages, so it's done only for small ones.
//
if (payload.size() <= SmallPayloadSize)
if (payload.size() <= config::presentation::SmallPayloadSize)
{
// Next nolint b/c we initialize buffer with payload copying.
// NOLINTNEXTLINE(cppcoreguidelines-pro-type-member-init,hicpp-member-init)
std::array<std::uint8_t, SmallPayloadSize> small_buffer;
const auto data_size = payload.copy(0, small_buffer.data(), payload.size());
const nunavut::support::const_bitspan bitspan{small_buffer.data(), data_size};
std::array<std::uint8_t, config::presentation::SmallPayloadSize> small_buffer;
//
const auto data_size = payload.copy(0, small_buffer.data(), payload.size());
const nunavut::support::const_bitspan bitspan{small_buffer.data(), data_size};

const nunavut::support::SerializeResult result = deserialize(out_message, bitspan);
return result ? cetl::nullopt : cetl::optional<DeserializationFailure>(result.error());
Expand Down
5 changes: 3 additions & 2 deletions include/libcyphal/presentation/response_promise.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include "client_impl.hpp"
#include "common_helpers.hpp"

#include "libcyphal/config.hpp"
#include "libcyphal/errors.hpp"
#include "libcyphal/transport/scattered_buffer.hpp"
#include "libcyphal/transport/types.hpp"
Expand Down Expand Up @@ -99,8 +100,8 @@ class ResponsePromiseBase : public detail::SharedClient::CallbackNode // NOSONA
/// Holds the approximate time when the callback was called. Useful for minimizing `now()` calls.
TimePoint approx_now;
};
static constexpr std::size_t FunctionSize = sizeof(void*) * 4;
using Function = cetl::pmr::function<void(const Arg& arg), FunctionSize>;
static constexpr auto FunctionSize = config::presentation::ResponsePromiseBase_Callback_FunctionSize;
using Function = cetl::pmr::function<void(const Arg& arg), FunctionSize>;
};

/// @brief Constructs a new promise by moving `other` promise into this one.
Expand Down
13 changes: 8 additions & 5 deletions include/libcyphal/presentation/server.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include "presentation_delegate.hpp"
#include "server_impl.hpp"

#include "libcyphal/config.hpp"
#include "libcyphal/transport/errors.hpp"
#include "libcyphal/transport/scattered_buffer.hpp"
#include "libcyphal/transport/types.hpp"
Expand Down Expand Up @@ -71,7 +72,7 @@ class ServerBase : ServerImpl::Callback // NOSONAR cpp:S4963
template <typename Response, typename SomeFailure>
class ContinuationImpl final
{
constexpr static std::size_t FunctionMaxSize = sizeof(void*) * 5;
static constexpr auto FunctionMaxSize = config::presentation::ServerBase_ContinuationImpl_FunctionMaxSize;
using FunctionSignature = cetl::optional<SomeFailure>(const TimePoint deadline, const Response& response);

public:
Expand Down Expand Up @@ -186,8 +187,9 @@ class Server final : public detail::ServerBase
TimePoint approx_now;
};
/// Defines continuation functor for sending a strong-typed response.
using Continuation = ContinuationImpl<Response, Failure>;
using Function = cetl::pmr::function<void(const Arg&, Continuation), sizeof(void*) * 4>;
using Continuation = ContinuationImpl<Response, Failure>;
static constexpr auto FunctionMaxSize = config::presentation::ServerBase_OnRequestCallback_FunctionMaxSize;
using Function = cetl::pmr::function<void(const Arg&, Continuation), FunctionMaxSize>;
};

/// @brief Sets function which will be called on each request reception.
Expand Down Expand Up @@ -305,8 +307,9 @@ class RawServiceServer final : public detail::ServerBase
TimePoint approx_now;
};
/// Defines continuation functor for sending raw (untyped) response bytes (aka pre-serialized).
using Continuation = ContinuationImpl<transport::PayloadFragments, Failure>;
using Function = cetl::pmr::function<void(const Arg&, Continuation), sizeof(void*) * 4>;
using Continuation = ContinuationImpl<transport::PayloadFragments, Failure>;
static constexpr auto FunctionMaxSize = config::presentation::ServerBase_OnRequestCallback_FunctionMaxSize;
using Function = cetl::pmr::function<void(const Arg&, Continuation), FunctionMaxSize>;
};

/// @brief Sets function which will be called on each request reception.
Expand Down
7 changes: 5 additions & 2 deletions include/libcyphal/presentation/subscriber.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

#include "subscriber_impl.hpp"

#include "libcyphal/config.hpp"
#include "libcyphal/transport/errors.hpp"
#include "libcyphal/transport/scattered_buffer.hpp"
#include "libcyphal/transport/types.hpp"
Expand Down Expand Up @@ -125,7 +126,8 @@ class Subscriber final : public detail::SubscriberBase
Message message;
transport::MessageRxMetadata metadata;
};
using Function = cetl::pmr::function<void(const Arg&), sizeof(void*) * 4>;
static constexpr auto FunctionMaxSize = config::presentation::Subscriber_OnReceiveCallback_FunctionMaxSize;
using Function = cetl::pmr::function<void(const Arg&), FunctionMaxSize>;
};

/// @brief Sets function which will be called on each message reception.
Expand Down Expand Up @@ -188,7 +190,8 @@ class Subscriber<void> final : public detail::SubscriberBase
const transport::ScatteredBuffer& raw_message;
transport::MessageRxMetadata metadata;
};
using Function = cetl::pmr::function<void(const Arg&), sizeof(void*) * 4>;
static constexpr auto FunctionMaxSize = config::presentation::Subscriber_OnReceiveCallback_FunctionMaxSize;
using Function = cetl::pmr::function<void(const Arg&), FunctionMaxSize>;
};

/// @brief Sets function which will be called on each message reception.
Expand Down
6 changes: 5 additions & 1 deletion include/libcyphal/transport/can/can_transport.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

#include "media.hpp"

#include "libcyphal/config.hpp"
#include "libcyphal/transport/errors.hpp"
#include "libcyphal/transport/transport.hpp"

Expand Down Expand Up @@ -117,8 +118,11 @@ class ICanTransport : public ITransport
/// - If a failure is returned, the transport will immediately stop current process, won't process any
/// other media (if any), and propagate the returned failure to the user (as result of `run` or etc).
///
static constexpr auto TransientErrorHandlerMaxSize =
config::transport::can::ICanTransport_TransientErrorHandlerMaxSize;
using TransientErrorHandler =
cetl::pmr::function<cetl::optional<AnyFailure>(TransientErrorReport::Variant& report_var), sizeof(void*) * 3>;
cetl::pmr::function<cetl::optional<AnyFailure>(TransientErrorReport::Variant& report_var),
TransientErrorHandlerMaxSize>;

ICanTransport(const ICanTransport&) = delete;
ICanTransport(ICanTransport&&) noexcept = delete;
Expand Down
4 changes: 3 additions & 1 deletion include/libcyphal/transport/errors.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#ifndef LIBCYPHAL_TRANSPORT_ERRORS_HPP_INCLUDED
#define LIBCYPHAL_TRANSPORT_ERRORS_HPP_INCLUDED

#include "libcyphal/config.hpp"
#include "libcyphal/errors.hpp"
#include "libcyphal/types.hpp"

Expand Down Expand Up @@ -57,7 +58,8 @@ class IPlatformError
IPlatformError& operator=(const IPlatformError&) = default;
IPlatformError& operator=(IPlatformError&&) noexcept = default;
};
using PlatformError = ImplementationCell<IPlatformError, cetl::unbounded_variant<sizeof(void*) * 3>>;
using PlatformError =
ImplementationCell<IPlatformError, cetl::unbounded_variant<config::transport::PlatformErrorMaxSize>>;

struct AlreadyExistsError final
{};
Expand Down
7 changes: 4 additions & 3 deletions include/libcyphal/transport/msg_sessions.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include "session.hpp"
#include "types.hpp"

#include "libcyphal/config.hpp"
#include "libcyphal/types.hpp"

#include <cetl/pf17/cetlpf.hpp>
Expand Down Expand Up @@ -76,9 +77,9 @@ class IMessageRxSession : public IRxSession

/// @brief Defines signature of the data reception callback function.
///
/// Size of the function is arbitrary (4 pointers), but should be enough for simple lambdas.
///
using Function = cetl::pmr::function<void(const Arg&), sizeof(void*) * 4>;
static constexpr std::size_t FunctionMaxSize =
config::transport::IMessageRxSession_OnReceiveCallback_FunctionMaxSize;
using Function = cetl::pmr::function<void(const Arg&), FunctionMaxSize>;

}; // OnReceiveCallback

Expand Down
4 changes: 3 additions & 1 deletion include/libcyphal/transport/scattered_buffer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
#ifndef LIBCYPHAL_TRANSPORT_SCATTERED_BUFFER_HPP_INCLUDED
#define LIBCYPHAL_TRANSPORT_SCATTERED_BUFFER_HPP_INCLUDED

#include "libcyphal/config.hpp"

#include <cetl/pf17/cetlpf.hpp>
#include <cetl/rtti.hpp>
#include <cetl/unbounded_variant.hpp>
Expand All @@ -29,7 +31,7 @@ class ScatteredBuffer final // NOSONAR : cpp:S4963 - we do directly handle reso
public:
/// @brief Defines maximum size (aka footprint) of the storage variant.
///
static constexpr std::size_t StorageVariantFootprint = sizeof(void*) * 8;
static constexpr std::size_t StorageVariantFootprint = config::transport::ScatteredBuffer_StorageVariantFootprint;

/// @brief Defines storage interface for the scattered buffer.
///
Expand Down
8 changes: 5 additions & 3 deletions include/libcyphal/transport/svc_sessions.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
#include "session.hpp"
#include "types.hpp"

#include "libcyphal/config.hpp"

#include <cetl/pf17/cetlpf.hpp>
#include <cetl/pmr/function.hpp>

Expand Down Expand Up @@ -81,9 +83,9 @@ class ISvcRxSession : public IRxSession

/// @brief Defines signature of the data reception callback function.
///
/// Size of the function is arbitrary (4 pointers), but should be enough for simple lambdas.
///
using Function = cetl::pmr::function<void(const Arg&), sizeof(void*) * 4>;
static constexpr std::size_t FunctionMaxSize =
config::transport::ISvcRxSession_OnReceiveCallback_FunctionMaxSize;
using Function = cetl::pmr::function<void(const Arg&), FunctionMaxSize>;

}; // OnReceiveCallback

Expand Down
Loading

0 comments on commit 9ab3f9d

Please sign in to comment.