From 8817dc24a26eb829930d0a3c3f4c8a04b748387f Mon Sep 17 00:00:00 2001 From: Romain Forlot Date: Sat, 22 Apr 2017 17:38:23 +0200 Subject: [PATCH] Use BCM socket as default cleaning up the old RAW socket code. Also use CAN FD frames by default. Change-Id: I5bad10e537653b1a96c2e3012d38dde8627d3caa Signed-off-by: Romain Forlot --- CAN-binder/low-can-binding/can/can-bus-dev.cpp | 27 ++++---------------------- CAN-binder/low-can-binding/can/can-bus-dev.hpp | 4 +--- CAN-binder/low-can-binding/can/can-bus.cpp | 2 +- CAN-binder/low-can-binding/utils/socket.cpp | 20 ++++--------------- CAN-binder/low-can-binding/utils/socket.hpp | 2 +- 5 files changed, 11 insertions(+), 44 deletions(-) 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 5c484ee..d097dc6 100644 --- a/CAN-binder/low-can-binding/can/can-bus-dev.cpp +++ b/CAN-binder/low-can-binding/can/can-bus-dev.cpp @@ -50,13 +50,10 @@ uint32_t can_bus_dev_t::get_address() const /// We try to open CAN socket and apply the following options /// timestamp received messages and pass the socket to FD mode. /// -/// @param[in] bcm boolean value indicating wether or not we initialize a Broadcast CAN Manager socket. -/// /// @return socket value or -1 if something wrong. -int can_bus_dev_t::open(bool bcm) +int can_bus_dev_t::open() { - DEBUG(binder_interface, "%s: CAN Handler using BCM socket ? %s", __FUNCTION__, bcm ? "true" : "false"); - return can_socket_.open(device_name_, bcm); + return can_socket_.open(device_name_); } /// @brief Set some option on the socket, timestamp and canfd frame usage. @@ -65,26 +62,10 @@ void can_bus_dev_t::configure() if (can_socket_) { const int timestamp_on = 1; - const int canfd_on = 1; DEBUG(binder_interface, "%s: CAN Handler socket correctly initialized : %d", __FUNCTION__, can_socket_.socket()); - - // Set timestamp for receveid frame if (can_socket_.setopt(SOL_SOCKET, SO_TIMESTAMP, ×tamp_on, sizeof(timestamp_on)) < 0) WARNING(binder_interface, "%s: setsockopt SO_TIMESTAMP error: %s", __FUNCTION__, ::strerror(errno)); - DEBUG(binder_interface, "%s: Switch CAN Handler socket to use fd mode", __FUNCTION__); - - // try to switch the socket into CAN_FD mode - if (can_socket_.setopt(SOL_CAN_RAW, CAN_RAW_FD_FRAMES, &canfd_on, sizeof(canfd_on)) < 0) - { - NOTICE(binder_interface, "%s: Can not switch into CAN Extended frame format.", __FUNCTION__); - is_fdmode_on_ = false; - } - else - { - DEBUG(binder_interface, "%s: Correctly set up CAN socket to use FD frames.", __FUNCTION__); - is_fdmode_on_ = true; - } } else { @@ -188,7 +169,7 @@ int can_bus_dev_t::send(can_message_t& can_msg) else { ERROR(binder_interface, "send_can_message: socket not initialized. Attempt to reopen can device socket."); - open(true); + open(); return -1; } return 0; @@ -226,7 +207,7 @@ bool can_bus_dev_t::shims_send(const uint32_t arbitration_id, const uint8_t* dat else { ERROR(binder_interface, "send_can_message: socket not initialized. Attempt to reopen can device socket."); - open(true); + open(); } return false; } diff --git a/CAN-binder/low-can-binding/can/can-bus-dev.hpp b/CAN-binder/low-can-binding/can/can-bus-dev.hpp index 96aa373..5961214 100644 --- a/CAN-binder/low-can-binding/can/can-bus-dev.hpp +++ b/CAN-binder/low-can-binding/can/can-bus-dev.hpp @@ -38,8 +38,6 @@ private: int32_t address_; ///< an identifier used through binding that refer to that device - bool is_fdmode_on_; ///< boolean telling if whether or not the can socket use fdmode. - std::thread th_reading_; ///< Thread handling read the socket can device filling can_message_q_ queue of can_bus_t bool is_running_ = false; ///< boolean telling whether or not reading is running or not void can_reader(can_bus_t& can_bus); @@ -50,7 +48,7 @@ public: std::string get_device_name() const; uint32_t get_address() const; - int open(bool bcm=false); + int open(); void configure(); int close(); diff --git a/CAN-binder/low-can-binding/can/can-bus.cpp b/CAN-binder/low-can-binding/can/can-bus.cpp index 6b3689d..6e6fba2 100644 --- a/CAN-binder/low-can-binding/can/can-bus.cpp +++ b/CAN-binder/low-can-binding/can/can-bus.cpp @@ -242,7 +242,7 @@ int can_bus_t::init_can_dev() for(const auto& device : devices_name) { can_bus_t::can_devices_[device] = std::make_shared(device, i); - if (can_bus_t::can_devices_[device]->open(true) >= 0) + if (can_bus_t::can_devices_[device]->open() >= 0) { can_bus_t::can_devices_[device]->configure(); DEBUG(binder_interface, "%s: Start reading thread", __FUNCTION__); diff --git a/CAN-binder/low-can-binding/utils/socket.cpp b/CAN-binder/low-can-binding/utils/socket.cpp index f4a3011..9decaa8 100644 --- a/CAN-binder/low-can-binding/utils/socket.cpp +++ b/CAN-binder/low-can-binding/utils/socket.cpp @@ -104,15 +104,12 @@ namespace utils /// @param[in] device_name is the kernel network device name of the CAN interface. /// /// @return Upon successful completion, shall return a non-negative integer, the socket file descriptor. Otherwise, a value of -1 shall be returned and errno set to indicate the error. - int socketcan_t::open(std::string device_name, bool bcm) + int socketcan_t::open(std::string device_name) { close(); struct ifreq ifr; - if(bcm) - socket_ = ::socket(PF_CAN, SOCK_DGRAM, CAN_BCM); - else - socket_ = ::socket(PF_CAN, SOCK_RAW, CAN_RAW); + socket_ = ::socket(PF_CAN, SOCK_DGRAM, CAN_BCM); // Attempts to open a socket to CAN bus ::strcpy(ifr.ifr_name, device_name.c_str()); @@ -127,18 +124,9 @@ namespace utils txAddress_.can_family = AF_CAN; txAddress_.can_ifindex = ifr.ifr_ifindex; - if(bcm) - { - if(connect((struct sockaddr *)&txAddress_, sizeof(txAddress_)) < 0) - { - ERROR(binder_interface, "%s: Connect failed. %s", __FUNCTION__, strerror(errno)); - close(); - } - } - // It's a RAW socket request, bind it to txAddress - else if(bind((struct sockaddr *)&txAddress_, sizeof(txAddress_)) < 0) + if(connect((struct sockaddr *)&txAddress_, sizeof(txAddress_)) < 0) { - ERROR(binder_interface, "%s: Bind failed. %s", __FUNCTION__, strerror(errno)); + ERROR(binder_interface, "%s: Connect failed. %s", __FUNCTION__, strerror(errno)); close(); } } diff --git a/CAN-binder/low-can-binding/utils/socket.hpp b/CAN-binder/low-can-binding/utils/socket.hpp index 2da2dfe..b981ad3 100644 --- a/CAN-binder/low-can-binding/utils/socket.hpp +++ b/CAN-binder/low-can-binding/utils/socket.hpp @@ -34,7 +34,7 @@ namespace utils explicit operator bool() const; int socket() const; - int open(std::string device_name, bool bcm=false); + 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(); -- 2.16.6