Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[linux] Enable SocketCAN to receive FDCan frames #1220

Merged
merged 2 commits into from
Nov 6, 2024
Merged
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
40 changes: 31 additions & 9 deletions src/modm/platform/can/socketcan/socketcan.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
* Copyright (c) 2017, Fabian Greif
* Copyright (c) 2017, Niklas Hauser
* Copyright (c) 2023, Christopher Durand
* Copyright (c) 2024, Michael Jossen
*
* This file is part of the modm project.
*
Expand Down Expand Up @@ -47,6 +48,16 @@ modm::platform::SocketCan::open(std::string deviceName)
return false;
}

/* Enable FDCAN support */
int recv_can_fd = 1;
if (setsockopt(skt, SOL_CAN_RAW, CAN_RAW_FD_FRAMES, &recv_can_fd, sizeof(recv_can_fd)) < 0)
{
MODM_LOG_ERROR << MODM_FILE_INFO;
MODM_LOG_ERROR << "Failed to enable FDCAN support: " << strerror(errno) << modm::endl;
close();
return false;
}

/* Locate the interface you wish to use */
struct ifreq ifr{};
if (deviceName.empty() || deviceName.size() > IFNAMSIZ - 1) {
Expand Down Expand Up @@ -103,8 +114,8 @@ modm::platform::SocketCan::getBusState()
bool
modm::platform::SocketCan::isMessageAvailable()
{
struct can_frame frame;
int nbytes = recv(skt, &frame, sizeof(struct can_frame), MSG_DONTWAIT | MSG_PEEK);
struct canfd_frame frame;
int nbytes = recv(skt, &frame, sizeof(struct canfd_frame), MSG_DONTWAIT | MSG_PEEK);

// recv returns 'Resource temporary not available' which is wired but ignored here.
/* if (nbytes < 0)
Expand All @@ -119,16 +130,22 @@ modm::platform::SocketCan::isMessageAvailable()
bool
modm::platform::SocketCan::getMessage(can::Message& message)
{
struct can_frame frame;
int nbytes = recv(skt, &frame, sizeof(struct can_frame), MSG_DONTWAIT);
struct canfd_frame frame;
int nbytes = recv(skt, &frame, sizeof(frame), MSG_DONTWAIT);

if (nbytes > 0)
{
if (frame.len > modm::can::Message::capacity)
{
MODM_LOG_ERROR << MODM_FILE_INFO;
MODM_LOG_ERROR << "Received can frame too big for configured buffer." << modm::endl;
return false;
}
message.identifier = frame.can_id;
message.setDataLengthCode(frame.can_dlc);
message.setLength(frame.len);
message.setExtended(frame.can_id & CAN_EFF_FLAG);
message.setRemoteTransmitRequest(frame.can_id & CAN_RTR_FLAG);
for (uint8_t ii = 0; ii < frame.can_dlc; ++ii) {
for (uint8_t ii = 0; ii < frame.len; ++ii) {
message.data[ii] = frame.data[ii];
}
return true;
Expand All @@ -139,8 +156,9 @@ modm::platform::SocketCan::getMessage(can::Message& message)
bool
modm::platform::SocketCan::sendMessage(const can::Message& message)
{
struct can_frame frame;
struct canfd_frame frame;

frame.flags = 0;
frame.can_id = message.identifier;
if (message.isExtended()) {
frame.can_id |= CAN_EFF_FLAG;
Expand All @@ -149,13 +167,17 @@ modm::platform::SocketCan::sendMessage(const can::Message& message)
frame.can_id |= CAN_RTR_FLAG;
}

frame.can_dlc = message.getLength();
frame.len = message.getLength();

for (uint8_t ii = 0; ii < message.getLength(); ++ii) {
frame.data[ii] = message.data[ii];
}

int bytes_sent = write( skt, &frame, sizeof(frame) );
// Send can_frame when length < 8, since other applications may not accept
// canfd_frame. Both structs intentionally share the same layout
// for this purpose
int size = message.getLength() > 8 ? sizeof(canfd_frame) : sizeof(can_frame);
int bytes_sent = write(skt, &frame, size);

return (bytes_sent > 0);
}
Loading