Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implementation of CAN Msg Rx Session #347

Merged
merged 26 commits into from
Apr 26, 2024

Conversation

serges147
Copy link
Collaborator

@serges147 serges147 commented Apr 19, 2024

  • Implemented MessageRxSession
  • Added intermediate can::TransportDelegate entity:
    • needed for all sessions to be able reference their parent transport
    • contains canard related (kinda) mixin logic (like "canard" memory management or errors translation)

Also:

  • switch to toolshed 22.4.7
  • disable asserts from "Coverage" build flavor - to not pollute branch coverage

@serges147 serges147 self-assigned this Apr 19, 2024
Copy link
Contributor

@thirtytwobits thirtytwobits left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I really need some high-level comments on the classes to comprehend your intentions.

cmake/modules/Findlibcanard.cmake Show resolved Hide resolved
namespace detail
{

struct TransportDelegate
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

let's not fall behind on the comments in this code base. Please ensure all classes, interfaces, and public methods have some level of documentation. Especially as things first go in it helps to know what you are thinking

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

all classes, interfaces, and public methods

See my comment here about omitting documentation in obvious cases (I am not saying that this case in particular is obvious): #343 (comment)

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, documentation will come, just a bit later - when api will be more stable. For now I'm not documenting yet to faster get something to play with.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As in the other review, my main concern is interface/class/struct documentation. It doesn't have to be complete at the start but I need some help understanding your design intention in these early reviews.

include/libcyphal/transport/can/delegate.hpp Show resolved Hide resolved
include/libcyphal/transport/can/delegate.hpp Outdated Show resolved Hide resolved
include/libcyphal/transport/can/delegate.hpp Show resolved Hide resolved
{
auto& self = getSelfFrom(ins);

const auto memory_size = sizeof(CanardMemoryHeader) + amount;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's follow AUTOSAR A7-1-5 and only use auto to:

  1. declare that a variable has the same type as return type of a function call
  2. declare that a variable has the same type as initializer of non-fundamental type
  3. declare parameters of a generic lambda expression
  4. declare a function template using trailing return type syntax.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will read this A7-1-5 carefully I try fix obvious cases.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe/hope it's fine to use more auto-s in unit tests.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure. Just remember humans need to read code too so sometimes auto helps by simplifying a statement and sometimes it obfuscates by making the type inscrutable. You can always use github co-pilot to reduce the amount of typing ;-)

include/libcyphal/transport/can/delegate.hpp Show resolved Hide resolved
include/libcyphal/transport/can/delegate.hpp Outdated Show resolved Hide resolved
@@ -65,6 +65,18 @@ using PmrAllocator = cetl::pmr::polymorphic_allocator<T>;
template <typename T>
using VarArray = cetl::VariableLengthArray<T, PmrAllocator<T>>;

template <typename Tag, typename... Args>
CETL_NODISCARD UniquePtr<typename Tag::Interface> makeUniquePtr(cetl::pmr::memory_resource& memory, Args&&... args)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd like to promote this pattern up into cetl::pmr::Factory at some point. As such, let's adopt a more appropriate name that distinguishes this from unique_ptr. Pavel and I had discussed calling this technique: dark_ptr since it hides the concrete type from the user of the smart-pointer-to-interface type. So dark_ptr and make_dark

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought it's voldemort_ptr and make_voldemort

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about interface_ptr and makeInterfacePtr ? Both "dark" and "voldemort" are funny, but IMHO doesn't look serious...

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

interface_ptr seems reasonable. I'm fairly certain voldemort is a trademark anyway.

@thirtytwobits
Copy link
Contributor

I think my biggest sticking point here is CETL_NODISCARD. I was really hoping to avoid a macro on every single method in this code base.

Copy link
Contributor

@thirtytwobits thirtytwobits left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As discussed on call: we have the right issues captured now to ensure my overarching concerns with this PR will be addressed moving forward

canard_instance().user_reference = this;
}

CETL_NODISCARD inline CanardInstance& canard_instance() noexcept
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
CETL_NODISCARD inline CanardInstance& canard_instance() noexcept
CETL_NODISCARD CanardInstance& canard_instance() noexcept

inline is redundant and should be removed; see the other methods as well

include/libcyphal/transport/can/delegate.hpp Show resolved Hide resolved
type_id_type<0x11, 0x41, 0xF5, 0xC0, 0x2E, 0x61, 0x44, 0xBF, 0x9F, 0x0E, 0xFA, 0x1C, 0x51, 0x8C, 0xD5, 0x17>;

public:
class CanardMemory final : public cetl::rtti_helper<CanardMemoryTypeIdType, ScatteredBuffer::Interface>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe CanardScatteredBuffer makes a better name? The current name is a bit too generic.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe... in the next pr I will already have ScatteredBuffer (renamed DynamicBuffer)

}

const auto bytes_to_copy = std::min(length_bytes, payload_size_ - offset_bytes);
memcpy(destination, static_cast<cetl::byte*>(buffer_) + offset_bytes, bytes_to_copy);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
memcpy(destination, static_cast<cetl::byte*>(buffer_) + offset_bytes, bytes_to_copy);
std::memmove(destination, static_cast<cetl::byte*>(buffer_) + offset_bytes, bytes_to_copy);

{
std::uint32_t code;
};

struct SessionAlreadyExistsError final
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
struct SessionAlreadyExistsError final
struct AlreadyExistsError final

This way, the error type will be reusable and whether it's related to a session or anything else will be evident from context.

@@ -0,0 +1,53 @@
/// @file
/// libcyphal common header.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is it

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

copy/paste error

CanardRxSubscription* out_subscription{};

// TODO: Handle errors.
const auto result = canardRxAccept(&canard_instance(),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we need an assertion check here that we never get an argument error back from libcanard

const auto result = canardRxAccept(&canard_instance(),
static_cast<CanardMicrosecond>(timestamp_us.count()),
&canard_frame,
static_cast<uint8_t>(media_index),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
static_cast<uint8_t>(media_index),
static_cast<std::uint8_t>(media_index),

AFAIK C++ does not define symbols in the global namespace, this thing comes from C.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My thinking was that in such conversions I covert to terms/types Canard is using... in this particular case the 4th parameter of ::canardRxAccept.


CETL_NODISCARD static inline TransportImpl& getSelfFrom(const CanardInstance* const ins)
CETL_NODISCARD inline TransportDelegate& asDelegate()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Another redundant inline here

@serges147 serges147 merged commit dadf7d7 into issue/346_can_msg_sessions Apr 26, 2024
18 checks passed
@serges147 serges147 deleted the sshirokov/346_msg_rx_session branch April 26, 2024 05:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants