Skip to content

Commit

Permalink
fix: make invalid LangID default to EN_US
Browse files Browse the repository at this point in the history
  • Loading branch information
jose-acevedoflores committed Feb 25, 2024
1 parent 34b6016 commit 1111f49
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 20 deletions.
5 changes: 3 additions & 2 deletions src/class.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,9 @@ pub trait UsbClass<B: UsbBus> {
///
/// * `index` - A string index allocated earlier with
/// [`UsbAllocator`](crate::bus::UsbBusAllocator).
/// * `lang_id` - The language ID for the string to retrieve.
fn get_string(&self, index: StringIndex, lang_id: Option<LangID>) -> Option<&str> {
/// * `lang_id` - The language ID for the string to retrieve. If the requested lang_id is not
/// valid it will default to EN_US.
fn get_string(&self, index: StringIndex, lang_id: LangID) -> Option<&str> {
let _ = (index, lang_id);
None
}
Expand Down
29 changes: 13 additions & 16 deletions src/device.rs
Original file line number Diff line number Diff line change
Expand Up @@ -563,25 +563,22 @@ impl<B: UsbBus> UsbDevice<'_, B> {

// rest STRING Requests
_ => {
let lang_id = LangID::try_from(req.index);
let lang_id = match LangID::try_from(req.index) {
Err(_err) => {
#[cfg(feature = "defmt")]
defmt::warn!(
"Receive unknown LANGID {:#06X}, default to EN_US",
_err.number
);
LangID::EN_US
}

Ok(req_lang_id) => req_lang_id,
};

let string = match index {
// Manufacturer, product, and serial are handled directly here.
1..=3 => {
let lang_id = match lang_id {
Err(_err) => {
#[cfg(feature = "defmt")]
defmt::warn!(
"Receive unknown LANGID {:#06X}, reject the request",
_err.number
);
xfer.reject().ok();
return;
}

Ok(req_lang_id) => req_lang_id,
};

let Some(lang) = config
.string_descriptors
.iter()
Expand All @@ -602,7 +599,7 @@ impl<B: UsbBus> UsbDevice<'_, B> {
let index = StringIndex::new(index);
classes
.iter()
.find_map(|cls| cls.get_string(index, lang_id.ok()))
.find_map(|cls| cls.get_string(index, lang_id))
}
};

Expand Down
4 changes: 2 additions & 2 deletions src/test_class.rs
Original file line number Diff line number Diff line change
Expand Up @@ -232,8 +232,8 @@ impl<B: UsbBus> UsbClass<B> for TestClass<'_, B> {
Ok(())
}

fn get_string(&self, index: StringIndex, lang_id: Option<LangID>) -> Option<&str> {
if lang_id == Some(LangID::EN_US) {
fn get_string(&self, index: StringIndex, lang_id: LangID) -> Option<&str> {
if lang_id == LangID::EN_US {
if index == self.custom_string {
return Some(CUSTOM_STRING);
} else if index == self.interface_string {
Expand Down

0 comments on commit 1111f49

Please sign in to comment.