Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions include/socketcan_adapter/socketcan_adapter.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,11 @@ class SocketcanAdapter : public std::enable_shared_from_this<SocketcanAdapter>
/// @return optional error string filled with an error message if any
std::optional<socket_error_string_t> send(const std::shared_ptr<const CanFrame> frame);

/// @brief Transmit a can frame via socket
/// @param frame Linux CAN frame to send
/// @return optional error string filled with an error message if any
std::optional<socket_error_string_t> send(const can_frame & frame);

/// @brief Set receive timeout
/// @param reecive_timeout_s std::chrono::duration<float> sets receive timeout in seconds
void set_receive_timeout(const std::chrono::duration<float> & recive_timeout_s);
Expand Down
25 changes: 13 additions & 12 deletions src/socketcan_adapter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

#include <cerrno>
#include <cstring>
#include <future>
#include <memory>
#include <optional>
#include <string>
Expand Down Expand Up @@ -226,6 +227,11 @@ std::optional<SocketcanAdapter::socket_error_string_t> SocketcanAdapter::send(
: std::nullopt;
}

std::optional<SocketcanAdapter::socket_error_string_t> SocketcanAdapter::send(const can_frame & frame)
{
return send(polymath::socketcan::CanFrame(frame));
}

std::optional<SocketcanAdapter::socket_error_string_t> SocketcanAdapter::sendFilters()
{
socket_error_string_t error_output("");
Expand Down Expand Up @@ -326,20 +332,15 @@ bool SocketcanAdapter::joinReceptionThread(const std::chrono::duration<float> &
{
stop_thread_requested_ = true;

auto start_time = std::chrono::steady_clock::now();
// Check thread running to tell when to join the thread
while (thread_running_ && ((std::chrono::steady_clock::now() - start_time) < timeout_s)) {
std::this_thread::sleep_for(std::chrono::milliseconds(100));
}

// Attempt to force joining at this point

if (can_receive_thread_.joinable()) {
can_receive_thread_.join();
return true;
} else {
return false;
// Use std::async to wait asynchronously for the thread to stop
std::future<void> join_future = std::async(std::launch::async, [this] { can_receive_thread_.join(); });

// Wait for the thread to stop within the timeout period
return join_future.wait_for(timeout_s) == std::future_status::ready;
}

return false;
}

std::string SocketcanAdapter::get_interface()
Expand Down