Skip to content

Commit

Permalink
added nunavut to the common lib
Browse files Browse the repository at this point in the history
  • Loading branch information
serges147 committed Dec 27, 2024
1 parent 831d23e commit 7ce6fae
Show file tree
Hide file tree
Showing 11 changed files with 150 additions and 30 deletions.
8 changes: 4 additions & 4 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ jobs:
sudo update-alternatives --install /usr/bin/clang-tidy clang-tidy /usr/bin/clang-tidy-$LLVM_VERSION 50
clang-tidy --version
- run: |
cmake --preset OCVSMD-Debian -DCMAKE_C_COMPILER=${{ matrix.c-compiler }} -DCMAKE_CXX_COMPILER=${{ matrix.cxx-compiler }}
cmake --build --preset OCVSMD-Debian-Debug
cmake --preset OCVSMD-Linux -DCMAKE_C_COMPILER=${{ matrix.c-compiler }} -DCMAKE_CXX_COMPILER=${{ matrix.cxx-compiler }}
cmake --build --preset OCVSMD-Linux-Debug
- uses: actions/upload-artifact@v4
if: always()
with:
Expand Down Expand Up @@ -58,8 +58,8 @@ jobs:
sudo apt update -y && sudo apt upgrade -y
sudo apt install gcc-multilib g++-multilib ninja-build
- run: |
cmake --preset OCVSMD-Debian -DCMAKE_C_COMPILER=${{ matrix.c-compiler }} -DCMAKE_CXX_COMPILER=${{ matrix.cxx-compiler }}
cmake --build --preset OCVSMD-Debian-Release
cmake --preset OCVSMD-Linux -DCMAKE_C_COMPILER=${{ matrix.c-compiler }} -DCMAKE_CXX_COMPILER=${{ matrix.cxx-compiler }}
cmake --build --preset OCVSMD-Linux-Release
- uses: actions/upload-artifact@v4
if: always()
with:
Expand Down
4 changes: 3 additions & 1 deletion include/ocvsmd/sdk/daemon.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
#ifndef OCVSMD_SDK_DAEMON_HPP_INCLUDED
#define OCVSMD_SDK_DAEMON_HPP_INCLUDED

#include <cetl/pf17/cetlpf.hpp>

#include <memory>

namespace ocvsmd
Expand All @@ -18,7 +20,7 @@ namespace sdk
class Daemon
{
public:
static std::unique_ptr<Daemon> make();
static std::unique_ptr<Daemon> make(cetl::pmr::memory_resource& memory);

Daemon(Daemon&&) = delete;
Daemon(const Daemon&) = delete;
Expand Down
6 changes: 5 additions & 1 deletion src/cli/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,16 @@

#include <ocvsmd/sdk/daemon.hpp>

#include <cetl/pf17/cetlpf.hpp>

int main(const int argc, const char** const argv)
{
(void) argc;
(void) argv;

if (auto daemon = ocvsmd::sdk::Daemon::make())
auto& memory = *cetl::pmr::new_delete_resource();

if (auto daemon = ocvsmd::sdk::Daemon::make(memory))
{
daemon->send_messages();
}
Expand Down
20 changes: 20 additions & 0 deletions src/common/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,34 @@

cmake_minimum_required(VERSION 3.27)

set(dsdl_ocvsmd_dir ${CMAKE_CURRENT_SOURCE_DIR}/dsdl/ocvsmd)
file(GLOB_RECURSE dsdl_ocvsmd_files CONFIGURE_DEPENDS ${dsdl_ocvsmd_dir}/*.dsdl)

add_cyphal_library(
NAME common
DSDL_FILES ${dsdl_ocvsmd_files}
DSDL_NAMESPACES ${dsdl_ocvsmd_dir}
ALLOW_EXPERIMENTAL_LANGUAGES
LANGUAGE cpp
LANGUAGE_STANDARD cetl++14-17
OUTPUT_DIR ${CMAKE_CURRENT_BINARY_DIR}/dsdl_transpiled
OUT_LIBRARY_TARGET common_transpiled
)

add_library(ocvsmd_common
ipc/unix_socket_client.cpp
ipc/unix_socket_server.cpp
)
target_link_libraries(ocvsmd_common
PUBLIC ${common_transpiled}
)
target_include_directories(ocvsmd_common
PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}
)
target_include_directories(ocvsmd_common SYSTEM
PUBLIC ${submodules_dir}/cetl/include
PUBLIC ${submodules_dir}/libcyphal/include
)
add_dependencies(ocvsmd_common
${common_transpiled}
)
4 changes: 4 additions & 0 deletions src/common/dsdl/ocvsmd/common/dsdl/Foo.1.0.dsdl
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
int8[<8] some_stuff

# reserve twice as much as we need.
@extent _offset_.max * 2
54 changes: 54 additions & 0 deletions src/common/dsdl_helpers.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
//
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: MIT
//

#ifndef OCVSMD_COMMON_DSDL_HELPERS_HPP_INCLUDED
#define OCVSMD_COMMON_DSDL_HELPERS_HPP_INCLUDED

#include <cetl/pf17/cetlpf.hpp>
#include <cetl/pf20/cetlpf.hpp>

#include <array>
#include <cstddef>
#include <cstdint>
#include <type_traits>

namespace ocvsmd
{
namespace common
{

template <typename Message, typename Result, std::size_t BufferSize, bool IsOnStack, typename Action>
static auto tryPerformOnSerialized(const Message& message, //
const cetl::pmr::memory_resource& memory,
Action&& action) -> std::enable_if_t<IsOnStack, Result>
{
// Not in use b/c we use stack buffer for small messages.
(void) memory;

// Try to serialize the message to raw payload buffer.
//
// Next nolint b/c we use a buffer to serialize the message, so no need to zero it (and performance better).
// NOLINTNEXTLINE(cppcoreguidelines-pro-type-member-init,hicpp-member-init)
std::array<cetl::byte, BufferSize> buffer;
//
const auto result_size = serialize( //
message,
// Next nolint & NOSONAR are currently unavoidable.
// NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
{reinterpret_cast<std::uint8_t*>(buffer.data()), BufferSize}); // NOSONAR cpp:S3630,

if (!result_size)
{
return result_size.error();
}

const cetl::span<const cetl::byte> bytes{buffer.data(), result_size.value()};
return std::forward<Action>(action)(bytes);
}

} // namespace common
} // namespace ocvsmd

#endif // OCVSMD_COMMON_DSDL_HELPERS_HPP_INCLUDED
37 changes: 26 additions & 11 deletions src/common/ipc/unix_socket_client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,15 @@

#include "unix_socket_client.hpp"

#include "dsdl_helpers.hpp"
#include "platform/posix_utils.hpp"

#include <nunavut/support/serialization.hpp>
#include <ocvsmd/common/dsdl/Foo_1_0.hpp>

#include <cetl/cetl.hpp>
#include <cetl/pf17/cetlpf.hpp>
#include <cetl/pf20/cetlpf.hpp>

#include <algorithm>
#include <array>
Expand All @@ -28,8 +34,9 @@ namespace common
namespace ipc
{

UnixSocketClient::UnixSocketClient(std::string socket_path)
: socket_path_{std::move(socket_path)}
UnixSocketClient::UnixSocketClient(cetl::pmr::memory_resource& memory, std::string socket_path)
: memory_{memory}
, socket_path_{std::move(socket_path)}
, client_fd_{-1}
{
}
Expand Down Expand Up @@ -82,16 +89,24 @@ bool UnixSocketClient::connect_to_server()
return true;
}

void UnixSocketClient::send_message(const std::string& message) const
void UnixSocketClient::send_message(const dsdl::Foo_1_0& foo_message) const
{
if (const auto err = platform::posixSyscallError([this, &message] {
//
return ::write(client_fd_, message.c_str(), message.size());
}))
{
std::cerr << "Failed to write: " << ::strerror(err) << "\n";
return;
}
using Failure = cetl::variant<int, nunavut::support::Error>;

tryPerformOnSerialized<dsdl::Foo_1_0,
Failure,
dsdl::Foo_1_0::_traits_::SerializationBufferSizeBytes,
true>(foo_message, memory_, [this](const cetl::span<const cetl::byte> msg_bytes) {
//
if (const auto err = platform::posixSyscallError([this, msg_bytes] {
//
return ::write(client_fd_, msg_bytes.data(), msg_bytes.size());
}))
{
std::cerr << "Failed to write: " << ::strerror(err) << "\n";
}
return 0;
});

constexpr std::size_t buf_size = 256;
std::array<char, buf_size> buffer{};
Expand Down
13 changes: 9 additions & 4 deletions src/common/ipc/unix_socket_client.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@
#ifndef OCVSMD_COMMON_IPC_UNIX_SOCKET_CLIENT_HPP_INCLUDED
#define OCVSMD_COMMON_IPC_UNIX_SOCKET_CLIENT_HPP_INCLUDED

#include "ocvsmd/common/dsdl/Foo_1_0.hpp"

#include <cetl/pf17/cetlpf.hpp>

#include <string>

namespace ocvsmd
Expand All @@ -18,7 +22,7 @@ namespace ipc
class UnixSocketClient final
{
public:
explicit UnixSocketClient(std::string socket_path);
UnixSocketClient(cetl::pmr::memory_resource& memory, std::string socket_path);

UnixSocketClient(UnixSocketClient&&) = delete;
UnixSocketClient(const UnixSocketClient&) = delete;
Expand All @@ -28,11 +32,12 @@ class UnixSocketClient final
~UnixSocketClient();

bool connect_to_server();
void send_message(const std::string& message) const;
void send_message(const dsdl::Foo_1_0& foo_message) const;

private:
std::string socket_path_;
int client_fd_;
cetl::pmr::memory_resource& memory_;
std::string socket_path_;
int client_fd_;

}; // UnixSocketClient

Expand Down
6 changes: 3 additions & 3 deletions src/daemon/engine/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ add_cyphal_library(
LANGUAGE cpp
LANGUAGE_STANDARD cetl++14-17
OUTPUT_DIR ${CMAKE_CURRENT_BINARY_DIR}/dsdl_transpiled
OUT_LIBRARY_TARGET dsdl_transpiled_headers
OUT_LIBRARY_TARGET engine_transpiled
)

add_library(udpard
Expand All @@ -38,7 +38,7 @@ add_library(ocvsmd_engine
)
target_link_libraries(ocvsmd_engine
PUBLIC udpard
PUBLIC ${dsdl_transpiled_headers}
PUBLIC ${engine_transpiled}
PUBLIC ocvsmd_common
)
target_include_directories(ocvsmd_engine
Expand All @@ -49,5 +49,5 @@ target_include_directories(ocvsmd_engine SYSTEM
PUBLIC ${submodules_dir}/libcyphal/include
)
add_dependencies(ocvsmd_engine
${dsdl_transpiled_headers}
${engine_transpiled}
)
3 changes: 3 additions & 0 deletions src/sdk/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,6 @@ target_link_libraries(ocvsmd_sdk
target_include_directories(ocvsmd_sdk
PUBLIC ${include_dir}
)
target_include_directories(ocvsmd_sdk SYSTEM
PUBLIC ${submodules_dir}/cetl/include
)
25 changes: 19 additions & 6 deletions src/sdk/daemon.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,13 @@
#include <ocvsmd/sdk/daemon.hpp>

#include "ipc/unix_socket_client.hpp"
#include "ocvsmd/common/dsdl/Foo_1_0.hpp"

#include <cetl/pf17/cetlpf.hpp>

#include <memory>
#include <utility>
#include <unistd.h>
#include <utility>

namespace ocvsmd
{
Expand All @@ -21,29 +24,39 @@ namespace
class DaemonImpl final : public Daemon
{
public:
explicit DaemonImpl(cetl::pmr::memory_resource& memory)
: memory_{memory}
{
}

bool connect()
{
return ipc_client_.connect_to_server();
}

void send_messages() const override
{
ipc_client_.send_message("Hello, world!");
common::dsdl::Foo_1_0 foo_message{&memory_};
foo_message.some_stuff.push_back('A'); // NOLINT
ipc_client_.send_message(foo_message);
::sleep(1);
ipc_client_.send_message("Goodbye, world!");

foo_message.some_stuff.push_back('Z'); // NOLINT
ipc_client_.send_message(foo_message);
::sleep(1);
}

private:
common::ipc::UnixSocketClient ipc_client_{"/var/run/ocvsmd/local.sock"};
cetl::pmr::memory_resource& memory_;
common::ipc::UnixSocketClient ipc_client_{memory_, "/var/run/ocvsmd/local.sock"};

}; // DaemonImpl

} // namespace

std::unique_ptr<Daemon> Daemon::make()
std::unique_ptr<Daemon> Daemon::make(cetl::pmr::memory_resource& memory)
{
auto daemon = std::make_unique<DaemonImpl>();
auto daemon = std::make_unique<DaemonImpl>(memory);
if (!daemon->connect())
{
return nullptr;
Expand Down

0 comments on commit 7ce6fae

Please sign in to comment.