CAN arrival timestamps not conveyed
Our application uses the timestamps from when frames arrive (setsocketopt with SO_TIMESTAMPING). This seems to not be implemented. For us it would be great if it was.
Can you please go into detail how this could work? Please also see #34
I don't know enough about the Linux kernel to understand how It could be implemented.
Our code for reading CAN frames via SocketCAN looks roughly like
const int nbytes = recvmsg(s, &rxMsg, 0);
if (nbytes > 0)
{
// frame received
*frameId = frame.can_id;
*numRecievedBytes = frame.can_dlc;
memcpy(data, frame.data, std::min(static_cast<size_t>(datalen), sizeof(frame.data)));
bool timestampValid = false;
for (rxCmsg = CMSG_FIRSTHDR(&rxMsg); rxCmsg && (rxCmsg->cmsg_level == SOL_SOCKET); rxCmsg = CMSG_NXTHDR(&rxMsg,rxCmsg))
{
if (rxCmsg->cmsg_type == SO_TIMESTAMPING)
{
struct timespec *stamp = (struct timespec *)CMSG_DATA(rxCmsg);
*timestamp = stamp[2].tv_sec * 1000000U + stamp[2].tv_nsec/1000;
timestampValid = true;
break;
}
}
It would be great if the actual timestamp recorded on the actual physical CAN device was delivered to the listener so that it can be read the same way as you would do with a normal SocketCAN.
Probably there is no easy way to do this in userspace as you would need to overwrite the time the kernel at which the kernel received the packet. If you find a way to do that, the timestamp could be added to the protocol with some effort.