aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--CAN-binder/low-can-binding/CMakeLists.txt2
-rw-r--r--CAN-binder/low-can-binding/can/can-bus-dev.cpp20
-rw-r--r--CAN-binder/low-can-binding/utils/socketcan.cpp (renamed from CAN-binder/low-can-binding/utils/socket.cpp)56
-rw-r--r--CAN-binder/low-can-binding/utils/socketcan.hpp (renamed from CAN-binder/low-can-binding/utils/socket.hpp)35
4 files changed, 85 insertions, 28 deletions
diff --git a/CAN-binder/low-can-binding/CMakeLists.txt b/CAN-binder/low-can-binding/CMakeLists.txt
index 84abfafe..a68ea2a2 100644
--- a/CAN-binder/low-can-binding/CMakeLists.txt
+++ b/CAN-binder/low-can-binding/CMakeLists.txt
@@ -24,7 +24,7 @@ PROJECT_TARGET_ADD(low-can-binding)
add_library(${TARGET_NAME} MODULE ${TARGET_NAME}.cpp configuration.cpp configuration-generated.cpp
can/can-bus.cpp can/can-bus-dev.cpp can/can-message-set.cpp can/can-message-definition.cpp can/can-message.cpp can/can-signals.cpp can/can-decoder.cpp
diagnostic/diagnostic-message.cpp diagnostic/diagnostic-manager.cpp diagnostic/active-diagnostic-request.cpp
- utils/signals.cpp utils/openxc-utils.cpp utils/timer.cpp utils/socket.cpp utils/config-parser.cpp)
+ utils/signals.cpp utils/openxc-utils.cpp utils/timer.cpp utils/socketcan.cpp utils/config-parser.cpp)
# Binder exposes a unique public entry point
SET_TARGET_PROPERTIES(${TARGET_NAME} PROPERTIES
diff --git a/CAN-binder/low-can-binding/can/can-bus-dev.cpp b/CAN-binder/low-can-binding/can/can-bus-dev.cpp
index d097dc66..8470156f 100644
--- a/CAN-binder/low-can-binding/can/can-bus-dev.cpp
+++ b/CAN-binder/low-can-binding/can/can-bus-dev.cpp
@@ -151,24 +151,21 @@ void can_bus_dev_t::can_reader(can_bus_t& can_bus)
/// @return 0 if message snet, -1 if something wrong.
int can_bus_dev_t::send(can_message_t& can_msg)
{
- ssize_t nbytes;
canfd_frame f;
-
f = can_msg.convert_to_canfd_frame();
if(can_socket_)
{
- nbytes = can_socket_.send(f);
- if (nbytes == -1)
+ can_socket_ << f;
+ if(!can_socket_)
{
- ERROR(binder_interface, "send_can_message: Sending CAN frame failed.");
+ ERROR(binder_interface, "%s: Sending CAN frame failed.", __FUNCTION__);
return -1;
}
- return (int)nbytes;
}
else
{
- ERROR(binder_interface, "send_can_message: socket not initialized. Attempt to reopen can device socket.");
+ ERROR(binder_interface, "%s: socket not initialized. Attempt to reopen can device socket.", __FUNCTION__);
open();
return -1;
}
@@ -187,7 +184,6 @@ int can_bus_dev_t::send(can_message_t& can_msg)
/// @return True if message sent, false if not.
bool can_bus_dev_t::shims_send(const uint32_t arbitration_id, const uint8_t* data, const uint8_t size)
{
- ssize_t nbytes;
canfd_frame f;
f.can_id = arbitration_id;
@@ -196,18 +192,18 @@ bool can_bus_dev_t::shims_send(const uint32_t arbitration_id, const uint8_t* dat
if(can_socket_)
{
- nbytes = can_socket_.send(f);
- if (nbytes == -1)
+ can_socket_ << f;
+ if (!can_socket_)
{
ERROR(binder_interface, "send_can_message: Sending CAN frame failed.");
return false;
}
- return true;
}
else
{
ERROR(binder_interface, "send_can_message: socket not initialized. Attempt to reopen can device socket.");
open();
+ return false;
}
- return false;
+ return true;
}
diff --git a/CAN-binder/low-can-binding/utils/socket.cpp b/CAN-binder/low-can-binding/utils/socketcan.cpp
index 9decaa8e..17ce6ce7 100644
--- a/CAN-binder/low-can-binding/utils/socket.cpp
+++ b/CAN-binder/low-can-binding/utils/socketcan.cpp
@@ -22,11 +22,12 @@
#include <net/if.h>
#include <sys/ioctl.h>
-#include "socket.hpp"
+#include "socketcan.hpp"
#include "low-can-binding.hpp"
namespace utils
{
+
/// @brief Construct a default, invalid, socket.
socketcan_t::socketcan_t()
: socket_{INVALID_SOCKET}
@@ -46,6 +47,11 @@ namespace utils
::close(socket_);
}
+ const struct sockaddr_can& socketcan_t::get_tx_address() const
+ {
+ return tx_address_;
+ }
+
/// @brief Test if socket is valid.
/// @return true if valid, false otherwise.
socketcan_t::operator bool() const
@@ -121,10 +127,10 @@ namespace utils
}
else
{
- txAddress_.can_family = AF_CAN;
- txAddress_.can_ifindex = ifr.ifr_ifindex;
+ tx_address_.can_family = AF_CAN;
+ tx_address_.can_ifindex = ifr.ifr_ifindex;
- if(connect((struct sockaddr *)&txAddress_, sizeof(txAddress_)) < 0)
+ if(connect((struct sockaddr *)&tx_address_, sizeof(tx_address_)) < 0)
{
ERROR(binder_interface, "%s: Connect failed. %s", __FUNCTION__, strerror(errno));
close();
@@ -133,14 +139,38 @@ namespace utils
return socket_;
}
- /// @brief Send a CAN message through the socket.
- ///
- /// @param[in] f - the CAN FD frame to send
- ///
- /// @return number of sent bytes if message sent, 0 on invalid socket and -1 if something wrong.
- ssize_t socketcan_t::send(const struct canfd_frame& f)
+ socketcan_t& operator<<(socketcan_t& s, const struct bcm_msg_head& obj)
+ {
+ struct sockaddr_can addr = s.get_tx_address();
+ ::sendto(s.socket(), &obj, sizeof(bcm_msg_head), 0, (struct sockaddr*)&addr, sizeof(addr));
+ return s;
+ }
+
+ socketcan_t& operator<<(socketcan_t& s, const struct canfd_frame& obj)
+ {
+ struct sockaddr_can addr = s.get_tx_address();
+ ::sendto(s.socket(), &obj, sizeof(canfd_frame), 0, (struct sockaddr*)&addr, sizeof(addr));
+ return s;
+ }
+
+ socketcan_t& operator<<(socketcan_t& s, const struct can_frame& obj)
+ {
+ struct sockaddr_can addr = s.get_tx_address();
+ ::sendto(s.socket(), &obj, sizeof(can_frame), 0, (struct sockaddr*)&addr, sizeof(addr));
+ return s;
+ }
+
+ socketcan_t& operator<<(socketcan_t& s, const struct basic_bcm_msg<struct can_frame>& obj)
+ {
+ s << obj.msg_head;
+ s << obj.frames;
+ return s;
+ }
+
+ socketcan_t& operator<<(socketcan_t& s, const struct canfd_bcm_msg& obj)
{
- return socket_ != INVALID_SOCKET ? ::sendto(socket_, &f, sizeof(struct canfd_frame), 0,
- (struct sockaddr*)&txAddress_, sizeof(txAddress_)) : 0;
+ s << obj.msg_head;
+ s << obj.frames;
+ return s;
}
-}
+} \ No newline at end of file
diff --git a/CAN-binder/low-can-binding/utils/socket.hpp b/CAN-binder/low-can-binding/utils/socketcan.hpp
index b981ad3e..2c790364 100644
--- a/CAN-binder/low-can-binding/utils/socket.hpp
+++ b/CAN-binder/low-can-binding/utils/socketcan.hpp
@@ -18,11 +18,27 @@
*/
#include <sys/socket.h>
+#include <linux/can/bcm.h>
+
+#include <vector>
#define INVALID_SOCKET -1
namespace utils
{
+
+ template <typename T>
+ struct basic_bcm_msg
+ {
+ bcm_msg_head msg_head;
+ std::vector<T> frames;
+ };
+
+ struct canfd_bcm_msg : public basic_bcm_msg<canfd_frame>
+ {
+ canfd_bcm_msg() { msg_head.flags |= CAN_FD_FRAME; }
+ };
+
class socketcan_t
{
public:
@@ -31,19 +47,34 @@ namespace utils
socketcan_t(socketcan_t&&);
~socketcan_t();
+ const struct sockaddr_can& get_tx_address() const;
explicit operator bool() const;
int socket() const;
int open(std::string device_name);
int setopt(int level, int optname, const void* optval, socklen_t optlen);
- ssize_t send(const struct canfd_frame& f);
int close();
+
private:
int socket_;
- struct sockaddr_can txAddress_; /// < internal member using to bind to the socket
+ struct sockaddr_can tx_address_;
int open(int domain, int type, int protocol);
int bind(const struct sockaddr* addr, socklen_t len);
int connect(const struct sockaddr* addr, socklen_t len);
};
+
+ template <typename T>
+ socketcan_t& operator<<(socketcan_t& s, const std::vector<T>& vobj)
+ {
+ for(const auto& obj : vobj)
+ s << obj;
+ return s;
+ }
+
+ socketcan_t& operator<<(socketcan_t& s, const canfd_frame& frame);
+ socketcan_t& operator<<(socketcan_t& s, const can_frame& frame);
+ socketcan_t& operator<<(socketcan_t& s, const struct basic_bcm_msg<can_frame>& obj);
+ socketcan_t& operator<<(socketcan_t& s, const struct canfd_bcm_msg& obj);
+ socketcan_t& operator<<(socketcan_t& s, const struct bcm_msg_head& obj);
}