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

add set_bitrate method #50

Merged
merged 1 commit into from
Oct 10, 2023
Merged

add set_bitrate method #50

merged 1 commit into from
Oct 10, 2023

Conversation

jackyzjk
Copy link

add set_bitrate method using netlink API

@fpagliughi
Copy link
Collaborator

Wow, thanks so much! I've wanted to get more of the nl interface implemented, but haven't had the time to get into it.

I have a few questions and comments inline, but either way, as long as this works to set the bitrate, I'll get it merged right away.

Also. the things you marked as "missing from libc"... did you PR them to libc to try to get them included there? If not, that's something we should do to get them in their proper place.

Copy link
Collaborator

@fpagliughi fpagliughi left a comment

Choose a reason for hiding this comment

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

I tested it on my setup and it seems to work to set the bit rate. Nice.

If you want to get these few little updates, that would be great. Otherwise I can merge and get it cleaned up.

///
#[repr(C)]
#[allow(non_camel_case_types)]
#[derive(Default, Clone)]
Copy link
Collaborator

Choose a reason for hiding this comment

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

It's nice to derive Debug as well if there are not problems doing so.

Copy link
Author

Choose a reason for hiding this comment

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

The struct can_bittiming is only used to make some very simple settings, so I don't think it's necessary to derive Debug.

Copy link
Collaborator

Choose a reason for hiding this comment

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

But when you write a library, it's better not to limit what you think users might need or want to do. Someone learning the library might want to simply dump the data to see what it is, or use the data structures in a larger collection that they want to dump. A single object that doesn't implement debug would prevent a user from simply deriving their own Debug.

#[allow(non_camel_case_types)]
#[derive(Default, Clone)]
pub struct can_bittiming {
pub(crate) bitrate: u32, /* Bit-rate in bits/second */
Copy link
Collaborator

Choose a reason for hiding this comment

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

These can all just be pub - instead of pub(crate) - like they do in libc.

Copy link
Author

Choose a reason for hiding this comment

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

It's better.

///
/// Note: Cannot use repr(C) here, as this will not actually make the underlying type a u32
/// as it would be in C (which checks which data type is necessary, notices that i32 does not
/// work in this case, and goes for u32 next).
Copy link
Collaborator

Choose a reason for hiding this comment

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

Sorry, I'm confused. What do we want for the underlying type? It looks like we convert to and from u16. So shouldn't we ask for that as the underlying type?

#[repr(u16)]

It probably doesn't matter, though, since we're just using these on the Rust side; as long as we can convert to and from the underlying type. But it's a nice reminder of what the underlying type should be.

Copy link
Author

Choose a reason for hiding this comment

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

It's better.

/// work in this case, and goes for u32 next).
///
pub enum IflaCan {
Unspec = 0,
Copy link
Collaborator

Choose a reason for hiding this comment

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

Nit: We probably don't need to specify all the discriminants if they just use the defaults, but I guess it doesn't hurt.

Copy link
Author

Choose a reason for hiding this comment

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

In order to correspond to the netlink API, it's better to specify it so that it's easier to understand.

/// as it would be in C (which checks which data type is necessary, notices that i32 does not
/// work in this case, and goes for u32 next).
///
pub enum IflaCan {
Copy link
Collaborator

Choose a reason for hiding this comment

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

For purely numeric enumeratrions like this, it's nice to make them Copy and derive the comparison traits for equality and ordering:

#[derive(Debug, Default, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]

Copy link
Author

Choose a reason for hiding this comment

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

Yes, it is.

@fpagliughi
Copy link
Collaborator

Actually, I have some time to work on this today, so I'll just merge and clean up those nits...

Thanks, again. This is awesome.

@fpagliughi fpagliughi merged commit 69ca005 into socketcan-rs:master Oct 10, 2023
6 of 8 checks passed
fpagliughi added a commit that referenced this pull request Oct 10, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants