Also use CAN FD frames by default.
Change-Id: I5bad10e537653b1a96c2e3012d38dde8627d3caa
Signed-off-by: Romain Forlot <romain.forlot@iot.bzh>
/// 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.
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
{
else
{
ERROR(binder_interface, "send_can_message: socket not initialized. Attempt to reopen can device socket.");
- open(true);
+ open();
return -1;
}
return 0;
else
{
ERROR(binder_interface, "send_can_message: socket not initialized. Attempt to reopen can device socket.");
- open(true);
+ open();
}
return false;
}
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);
std::string get_device_name() const;
uint32_t get_address() const;
- int open(bool bcm=false);
+ int open();
void configure();
int close();
for(const auto& device : devices_name)
{
can_bus_t::can_devices_[device] = std::make_shared<can_bus_dev_t>(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__);
/// @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());
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();
}
}
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();