.. _program_listing_file__tmp_ws_src_ros2_socketcan_ros2_socketcan_include_ros2_socketcan_socket_can_sender.hpp: Program Listing for File socket_can_sender.hpp ============================================== |exhale_lsh| :ref:`Return to documentation for file ` (``/tmp/ws/src/ros2_socketcan/ros2_socketcan/include/ros2_socketcan/socket_can_sender.hpp``) .. |exhale_lsh| unicode:: U+021B0 .. UPWARDS ARROW WITH TIP LEFTWARDS .. code-block:: cpp // Copyright 2021 the Autoware Foundation // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // //    http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. // // Co-developed by Tier IV, Inc. and Apex.AI, Inc. #ifndef ROS2_SOCKETCAN__SOCKET_CAN_SENDER_HPP_ #define ROS2_SOCKETCAN__SOCKET_CAN_SENDER_HPP_ #include #include #include "ros2_socketcan/visibility_control.hpp" #include "ros2_socketcan/socket_can_id.hpp" namespace drivers { namespace socketcan { class SOCKETCAN_PUBLIC SocketCanSender { public: explicit SocketCanSender( const std::string & interface = "can0", const bool enable_fd = false, const CanId & default_id = CanId{}); ~SocketCanSender() noexcept; void send( const void * const data, const std::size_t length, const std::chrono::nanoseconds timeout = std::chrono::nanoseconds::zero()) const; void send( const void * const data, const std::size_t length, const CanId id, const std::chrono::nanoseconds timeout = std::chrono::nanoseconds::zero()) const; template::value>> void send( const T & data, const std::chrono::nanoseconds timeout = std::chrono::nanoseconds::zero()) const { send(data, m_default_id, timeout); } template::value>> void send( const T & data, const CanId id, const std::chrono::nanoseconds timeout = std::chrono::nanoseconds::zero()) const { static_assert(sizeof(data) <= MAX_DATA_LENGTH, "Data type too large for CAN"); //lint -e586 I have to use reinterpret cast because I'm operating on bytes, see below NOLINT send_impl(reinterpret_cast(&data), sizeof(data), id, timeout); // reinterpret_cast to byte, or (unsigned) char is well defined; // all pointers can implicitly convert to void * } void send_fd( const void * const data, const std::size_t length, const std::chrono::nanoseconds timeout = std::chrono::nanoseconds::zero()) const; void send_fd( const void * const data, const std::size_t length, const CanId id, const std::chrono::nanoseconds timeout = std::chrono::nanoseconds::zero()) const; template::value>> void send_fd( const T & data, const std::chrono::nanoseconds timeout = std::chrono::nanoseconds::zero()) const { send_fd(data, m_default_id, timeout); } template::value>> void send_fd( const T & data, const CanId id, const std::chrono::nanoseconds timeout = std::chrono::nanoseconds::zero()) const { static_assert(sizeof(data) <= MAX_FD_DATA_LENGTH, "Data type too large for CAN FD"); //lint -e586 I have to use reinterpret cast because I'm operating on bytes, see below NOLINT send_fd_impl(reinterpret_cast(&data), sizeof(data), id, timeout); // reinterpret_cast to byte, or (unsigned) char is well defined; // all pointers can implicitly convert to void * } CanId default_id() const noexcept; private: // Underlying implementation of sending, data is assumed to be of an appropriate length void send_impl( const void * const data, const std::size_t length, const CanId id, const std::chrono::nanoseconds timeout) const; // Underlying implementation of FD sending, data is assumed to be of an appropriate length void send_fd_impl( const void * const data, const std::size_t length, const CanId id, const std::chrono::nanoseconds timeout) const; // Wait for file descriptor to be available to send data via select() SOCKETCAN_LOCAL void wait(const std::chrono::nanoseconds timeout) const; bool m_enable_fd; int32_t m_file_descriptor{}; CanId m_default_id; }; // class SocketCanSender } // namespace socketcan } // namespace drivers #endif // ROS2_SOCKETCAN__SOCKET_CAN_SENDER_HPP_