-
Couldn't load subscription status.
- Fork 606
Add kernel timestamp support #2412
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
base: main
Are you sure you want to change the base?
Conversation
which calls ioctl(SIOCGSTAMP) to get the timestamp of the last read message from the kernel
also fix some bugs: * time_t and suseconds_t are of type `long` on debian * print timestamp in sample project Tested on hardware on a Linux 6.12.33+deb13-amd64 x86_64 machine with an Innomaker USB2CAN dongle
|
@dotnet-policy-service agree |
|
[Triage] Rather than adding GetTimeStamp API add another overload of TryReadFrame which |
| return ((ulong)tv.tv_sec * 1000000 + (ulong)tv.tv_usec); // unix time in [us] | ||
| } | ||
|
|
||
| internal unsafe struct TimeVal |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
there should be a comment that long is correct type only because we're using this in conjunction with latest kernel. Please double check this actually works with 32-bit OS-es (if not then please make sure to display correct error message)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
note, you don't need to test it - just double check what type is defined in the latest kernel's .h file, if it's int64_t or equivalent we're good if it's platform specific like int then likely not
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Okay, I did some digging. In C, the ioctl call returns:
struct timeval {
time_t tv_sec; /* Seconds */
suseconds_t tv_usec; /* Microseconds */
};time_t can be a signed integer of 32 bit or 64 bit, depending on the platform.
In C#, I map this to a long type which is always a 64 bit signed integer.
If I run this on a 64 bit system, everything fits bit by bit.
If I run this on a 32-bit system, the C# runtime will promote the 32-bit return value to long properly.
Please correct me if I'm wrong.
I will look into testing this on a 32 bit system.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is DateTime the correct for this timestamp?
When I see a DateTime, I expect this represent the "real" moment when the event occurred. I also expect to be able to compare this value with other DateTime instances coming from other sensors.
If instead this timestamp represents the time elapsed since the OS started, IMO the TimeSpan type would be more appropriate.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The kernel does indeed return the absolute value of the system clock when the CAN frame was received (microseconds since the 1.1.1970). So I think DateTime is a good type for this.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good to know, thanks
* remove GetTimeStamp() * adapt samples/Program.cs * add can interface name as a CLI parameter to Program.cs
Okay, I did some research on this. So seems like, if SocketCAN works, there is a very good chance that getting timestamps will also work. |
The linux kernel provides high resolution timestamps for received CAN frames.
This pull requests adds
CanRaw.GetLastTimeStamp()to retrieve the timestamp of the last read frame.The timestamp is in micro-seconds unix time.
Tested with an inno-maker USB2CAN dongle:
Microsoft Reviewers: Open in CodeFlow