linuxsocketsembeddedbuffersocketcan

SocketCan: read() data after write()


I am implementing a small subset of a UDS client with SocketCAN on embedded linux. The communication between the client and server works on a request / response pattern. I would like to read() the response of the server after having used write() to send the UDS request. The problem is when there exists older data in the receive buffer (which was received before the write()-call) the read() returns this old data. How can I either a) clear the input buffer or b) determine the timestamp of the received response data to compare with the timestamp of the request? What is the cleanest solution for this? Because read() is blocking when there is no data to return it is not so easy to just dummy-read until the buffer is empty. I feel there must be a simple solution for this but I am new to socket() and its friends...

Thanks for your help!


Solution

  • Thanks for your comments. I have now found the best solution for my use case:

    By calling ioctl() with parameter SIOCGSTAMP (see https://linux.die.net/man/7/socket) after the read() I can check the timestamp of the received CAN data. If the timestamp is older than that of the last write() call I just repeat read()'ing until the timestamp is newer.