-
Notifications
You must be signed in to change notification settings - Fork 78
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
Make USB device config public #69
Open
nickray
wants to merge
1
commit into
rust-embedded-community:master
Choose a base branch
from
nickray:public-config
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,178 @@ | ||
/// A USB vendor ID and product ID pair. | ||
pub struct UsbVidPid(pub u16, pub u16); | ||
|
||
macro_rules! builder_fields { | ||
( $( $(#[$meta:meta])* $name:ident: $type:ty, )* ) => { | ||
$( | ||
$(#[$meta])* | ||
pub fn $name(mut self, $name: $type) -> Self { | ||
self.$name = $name; | ||
self | ||
} | ||
)* | ||
} | ||
} | ||
|
||
/// A USB device configuration. | ||
#[derive(Clone, Copy, Debug)] | ||
pub struct Config<'a> { | ||
/// USB device class | ||
pub device_class: u8, | ||
/// USB device subclass | ||
pub device_sub_class: u8, | ||
/// USB device protocol | ||
pub device_protocol: u8, | ||
/// USB control endpoint maximum packet size | ||
pub max_packet_size_0: u8, | ||
/// USB vendor ID | ||
pub vendor_id: u16, | ||
/// USB product ID | ||
pub product_id: u16, | ||
/// BCD encoded device release | ||
pub device_release: u16, | ||
/// Manufacturer string | ||
pub manufacturer: Option<&'a str>, | ||
/// Product string | ||
pub product: Option<&'a str>, | ||
/// Serial number | ||
pub serial_number: Option<&'a str>, | ||
/// Is device self-powered? | ||
pub self_powered: bool, | ||
/// Does device support remote wakeup? | ||
pub supports_remote_wakeup: bool, | ||
/// Is device composite with IADs? | ||
pub composite_with_iads: bool, | ||
/// Maximum power consumption of device | ||
pub max_power: u8, | ||
} | ||
|
||
impl<'a> Config<'a> { | ||
|
||
/// Create a USB device configuration with given vendor and product ID, | ||
/// using defaults. | ||
pub fn new(vid_pid: UsbVidPid) -> Self { | ||
Self { | ||
device_class: 0x00, | ||
device_sub_class: 0x00, | ||
device_protocol: 0x00, | ||
max_packet_size_0: 8, | ||
vendor_id: vid_pid.0, | ||
product_id: vid_pid.1, | ||
device_release: 0x0010, | ||
manufacturer: None, | ||
product: None, | ||
serial_number: None, | ||
self_powered: false, | ||
supports_remote_wakeup: false, | ||
composite_with_iads: false, | ||
max_power: 50, | ||
} | ||
} | ||
|
||
builder_fields! { | ||
/// Sets the device class code assigned by USB.org. Set to `0xff` for vendor-specific | ||
/// devices that do not conform to any class. | ||
/// | ||
/// Default: `0x00` (class code specified by interfaces) | ||
device_class: u8, | ||
|
||
/// Sets the device sub-class code. Depends on class. | ||
/// | ||
/// Default: `0x00` | ||
device_sub_class: u8, | ||
|
||
/// Sets the device protocol code. Depends on class and sub-class. | ||
/// | ||
/// Default: `0x00` | ||
device_protocol: u8, | ||
|
||
/// Sets the device release version in BCD. | ||
/// | ||
/// Default: `0x0010` ("0.1") | ||
device_release: u16, | ||
|
||
/// Sets whether the device may have an external power source. | ||
/// | ||
/// This should be set to `true` even if the device is sometimes self-powered and may not | ||
/// always draw power from the USB bus. | ||
/// | ||
/// Default: `false` | ||
/// | ||
/// See also: `max_power` | ||
self_powered: bool, | ||
|
||
/// Sets whether the device supports remotely waking up the host is requested. | ||
/// | ||
/// Default: `false` | ||
supports_remote_wakeup: bool, | ||
} | ||
|
||
/// Configures the device as a composite device with interface association descriptors. | ||
pub fn composite_with_iads(mut self) -> Self { | ||
// Magic values specified in USB-IF ECN on IADs. | ||
self.device_class = 0xEF; | ||
self.device_sub_class = 0x02; | ||
self.device_protocol = 0x01; | ||
|
||
self.composite_with_iads = true; | ||
self | ||
} | ||
|
||
/// Sets the manufacturer name string descriptor. | ||
/// | ||
/// Default: (none) | ||
pub fn manufacturer(mut self, manufacturer: &'a str) -> Self { | ||
self.manufacturer = Some(manufacturer); | ||
self | ||
} | ||
|
||
/// Sets the product name string descriptor. | ||
/// | ||
/// Default: (none) | ||
pub fn product(mut self, product: &'a str) -> Self { | ||
self.product = Some(product); | ||
self | ||
} | ||
|
||
/// Sets the serial number string descriptor. | ||
/// | ||
/// Default: (none) | ||
pub fn serial_number(mut self, serial_number: &'a str) -> Self { | ||
self.serial_number = Some(serial_number); | ||
self | ||
} | ||
|
||
/// Sets the maximum packet size in bytes for the control endpoint 0. | ||
/// | ||
/// Valid values are 8, 16, 32 and 64. There's generally no need to change this from the default | ||
/// value of 8 bytes unless a class uses control transfers for sending large amounts of data, in | ||
/// which case using a larger packet size may be more efficient. | ||
/// | ||
/// Default: 8 bytes | ||
pub fn max_packet_size_0(mut self, max_packet_size_0: u8) -> Self { | ||
match max_packet_size_0 { | ||
8 | 16 | 32 | 64 => {} | ||
_ => panic!("invalid max_packet_size_0"), | ||
} | ||
|
||
self.max_packet_size_0 = max_packet_size_0; | ||
self | ||
} | ||
|
||
/// Sets the maximum current drawn from the USB bus by the device in milliamps. | ||
/// | ||
/// The default is 100 mA. If your device always uses an external power source and never draws | ||
/// power from the USB bus, this can be set to 0. | ||
/// | ||
/// See also: `self_powered` | ||
/// | ||
/// Default: 100mA | ||
pub fn max_power(mut self, max_power_ma: usize) -> Self { | ||
if max_power_ma > 500 { | ||
panic!("max_power is too much") | ||
} | ||
|
||
self.max_power = (max_power_ma / 2) as u8; | ||
self | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
As much as I like the builder semantics, making struct fields public is problematic . See https://rust-lang.github.io/api-guidelines/future-proofing.html#structs-have-private-fields-c-struct-private for a discussion on why this is problematic for a maintainability perspective.
Other than that, I like the builder config API