Skip to content

socketcan: support use of SO_TIMESTAMPING for hardware timestamps - #1882

Open
pevsonic wants to merge 1 commit into
hardbyte:mainfrom
pevsonic:pevsonic/socketcan_enable_hw_timestamps
Open

socketcan: support use of SO_TIMESTAMPING for hardware timestamps#1882
pevsonic wants to merge 1 commit into
hardbyte:mainfrom
pevsonic:pevsonic/socketcan_enable_hw_timestamps

Conversation

@pevsonic

Copy link
Copy Markdown

The current implemenation of socketcan utilises SO_TIMESTAMPNS which only offers system timestamps.

I've looked at how can-utils candump.c configures hardware timestamping and implemented this in socketcan as an option which is disabled by default to avoid any potential adverse impact on existing usage. This is using the same param 'use_system_timestamp' as established by neovi_bus.py.

I've also modified logger.py to provide an additional '-H' flag in the same way that candump does in order to use this functionality.

@pevsonic
pevsonicforce-pushed the pevsonic/socketcan_enable_hw_timestamps branch 2 times, most recently from 0ad787e to deb6c79CompareOctober 30, 2024 16:46
Comment threadcan/interfaces/socketcan/socketcan.py Outdated
@pevsonic
pevsonicforce-pushed the pevsonic/socketcan_enable_hw_timestamps branch 3 times, most recently from e1a7050 to 84e002cCompareOctober 30, 2024 22:17
Comment threadcan/interfaces/socketcan/socketcan.py Outdated
Comment threadcan/logger.py Outdated
choices=sorted(can.VALID_INTERFACES),
)

parser.add_argument(

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not necessary, interface specific parameters can be passed like --can-hardware-timestamps=True

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

From my perspective, enabling hardware timestamping is a core feature I use and I imagine others might see it that way too. As an argument, it's availability is advertised by --help. Sending as you suggest would (I don't believe?) have any way for the user to see the args availability without understanding the mechanism and digging into the code to find the arg name. Additionally theres no validation on them so if I used --can-hard-we-ar-timestamps=True there'd be no detection of this, failure or feedback to the user.

Ive not got a big issue with this if you'd like me to drop the argument, but I think there's a solid case for leaving as an argument?

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If only one interface supports this argument, i'd prefer to remove it. Otherwise we could add 100 more interface specific "important" arguments.

Comment threadcan/interfaces/socketcan/socketcan.py Outdated
Comment threadcan/interfaces/socketcan/socketcan.py Outdated
@pevsonic
pevsonicforce-pushed the pevsonic/socketcan_enable_hw_timestamps branch from 84e002c to 865866dCompareNovember 1, 2024 12:35
Use raw hardware timestamp for can messages if available instead
of the system timestamp. By default we use the SO_TIMESTAMPNS
interface which provides ns resolution but low accuracy. If your
can hardware supports it you can use this parameter to

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
canhardwaresupportsityoucanusethisparameterto
CANhardwaresupportsityoucanusethisparameterto

Comment threadcan/logger.py Outdated
choices=sorted(can.VALID_INTERFACES),
)

parser.add_argument(

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If only one interface supports this argument, i'd prefer to remove it. Otherwise we could add 100 more interface specific "important" arguments.

alternatively use the SO_TIMESTAMPING interface and request raw
hardware timestamps. These are much higher precision but will
almost certainly not be referenced to the time of day. There
may be other pitfalls to such as loopback packets reporting with

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
maybeotherpitfallstosuchasloopbackpacketsreportingwith
maybeotherpitfallstoosuchasloopbackpacketsreportingwith

:param receive_own_messages:
If transmitted messages should also be received by this bus.
:param bool can_hardware_timestamps:
Use raw hardware timestamp for can messages if available instead

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Userawhardwaretimestampforcanmessagesifavailableinstead
UserawhardwaretimestampforCANmessagesifavailableinstead

@jayceslesar

Copy link
Copy Markdown

Any update on this?

@pevsonic

Copy link
Copy Markdown
Author

Any update on this?

I’d love to get it in as I’m using it in derived code we have here and I’m sure it’s helpful, but I’m not sure what changes I need to make to get accepted?

@jayceslesar

Copy link
Copy Markdown

I’d love to get it in as I’m using it in derived code we have here and I’m sure it’s helpful, but I’m not sure what changes I need to make to get accepted?

I think you can just remove the argparse code you added and things should just work as the library knows to just pass in kwargs to the bustype by using the following:

parser.add_argument(
"extra_args",
nargs=argparse.REMAINDER,
help="The remaining arguments will be used for the interface and ""logger/player initialisation. ""For example, `-i vector -c 1 --app-name=MyCanApp` is the equivalent ""to opening the bus with `Bus('vector', channel=1, app_name='MyCanApp')",
)

So you would just pass
--can-hardware-timestamps=True
like @zariiii9003 commented in the initial review.

This is beneficial because this library acts as an abstraction over many bustypes abstracted over and made to look like CAN buses even though they are not and allows users to pass in arbitrary "kwargs" to the underlying interface that actually gets initialized without having to worry about what arguments actually live on the argument parser object

The rest just look like minor docs changes

@pevsonic
pevsonicforce-pushed the pevsonic/socketcan_enable_hw_timestamps branch from 865866d to 3f148c2CompareApril 8, 2025 10:41
@pevsonic

Copy link
Copy Markdown
Author

I’d love to get it in as I’m using it in derived code we have here and I’m sure it’s helpful, but I’m not sure what changes I need to make to get accepted?

I think you can just remove the argparse code you added and things should just work as the library knows to just pass in kwargs to the bustype by using the following:

parser.add_argument(
"extra_args",
nargs=argparse.REMAINDER,
help="The remaining arguments will be used for the interface and ""logger/player initialisation. ""For example, `-i vector -c 1 --app-name=MyCanApp` is the equivalent ""to opening the bus with `Bus('vector', channel=1, app_name='MyCanApp')",
)

So you would just pass --can-hardware-timestamps=True like @zariiii9003 commented in the initial review.

This is beneficial because this library acts as an abstraction over many bustypes abstracted over and made to look like CAN buses even though they are not and allows users to pass in arbitrary "kwargs" to the underlying interface that actually gets initialized without having to worry about what arguments actually live on the argument parser object

The rest just look like minor docs changes

That's really helpful thanks! I didn't know that's how kwargs works - while I can get by with Python, I'm really a 'C' guy..! Hopefully that should be enough for a merge now...

@pevsonic
pevsonicforce-pushed the pevsonic/socketcan_enable_hw_timestamps branch from 3f148c2 to 623cda1CompareMay 6, 2026 14:25
The current implemenation of socketcan utilises SO_TIMESTAMPNS
which only offers system timestamps.
I've looked at how can-utils candump.c configures hardware
timestamping and implemented this in socketcan as a new option
'can_hardware_timestamps' which is disabled by default to avoid
any potential adverse impact on existing usage.
To test, pass using --bus-kwargs e.g. :
can_viewer --bus-kwargs can_hardware_timestamps=True
@pevsonic
pevsonicforce-pushed the pevsonic/socketcan_enable_hw_timestamps branch from 623cda1 to 42c5b68CompareMay 6, 2026 14:30
Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants

@pevsonic@jayceslesar@hartkopp@zariiii9003