Skip to content

Commit 55aecdd

Browse files
fix: make invalid LangID default to EN_US
1 parent 34b6016 commit 55aecdd

File tree

3 files changed

+17
-20
lines changed

3 files changed

+17
-20
lines changed

src/class.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,9 @@ pub trait UsbClass<B: UsbBus> {
4343
///
4444
/// * `index` - A string index allocated earlier with
4545
/// [`UsbAllocator`](crate::bus::UsbBusAllocator).
46-
/// * `lang_id` - The language ID for the string to retrieve.
47-
fn get_string(&self, index: StringIndex, lang_id: Option<LangID>) -> Option<&str> {
46+
/// * `lang_id` - The language ID for the string to retrieve. If the requested lang_id is not
47+
/// valid it will default to EN_US.
48+
fn get_string(&self, index: StringIndex, lang_id: LangID) -> Option<&str> {
4849
let _ = (index, lang_id);
4950
None
5051
}

src/device.rs

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -563,25 +563,21 @@ impl<B: UsbBus> UsbDevice<'_, B> {
563563

564564
// rest STRING Requests
565565
_ => {
566-
let lang_id = LangID::try_from(req.index);
566+
let lang_id = match LangID::try_from(req.index) {
567+
Err(_err) => {
568+
#[cfg(feature = "defmt")]
569+
defmt::warn!(
570+
"Receive unknown LANGID {:#06X}, default to EN_US",
571+
_err.number
572+
);
573+
LangID::EN_US
574+
}
567575

576+
Ok(req_lang_id) => req_lang_id,
577+
};
568578
let string = match index {
569579
// Manufacturer, product, and serial are handled directly here.
570580
1..=3 => {
571-
let lang_id = match lang_id {
572-
Err(_err) => {
573-
#[cfg(feature = "defmt")]
574-
defmt::warn!(
575-
"Receive unknown LANGID {:#06X}, reject the request",
576-
_err.number
577-
);
578-
xfer.reject().ok();
579-
return;
580-
}
581-
582-
Ok(req_lang_id) => req_lang_id,
583-
};
584-
585581
let Some(lang) = config
586582
.string_descriptors
587583
.iter()
@@ -602,7 +598,7 @@ impl<B: UsbBus> UsbDevice<'_, B> {
602598
let index = StringIndex::new(index);
603599
classes
604600
.iter()
605-
.find_map(|cls| cls.get_string(index, lang_id.ok()))
601+
.find_map(|cls| cls.get_string(index, lang_id))
606602
}
607603
};
608604

src/test_class.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -232,8 +232,8 @@ impl<B: UsbBus> UsbClass<B> for TestClass<'_, B> {
232232
Ok(())
233233
}
234234

235-
fn get_string(&self, index: StringIndex, lang_id: Option<LangID>) -> Option<&str> {
236-
if lang_id == Some(LangID::EN_US) {
235+
fn get_string(&self, index: StringIndex, lang_id: LangID) -> Option<&str> {
236+
if lang_id == LangID::EN_US {
237237
if index == self.custom_string {
238238
return Some(CUSTOM_STRING);
239239
} else if index == self.interface_string {

0 commit comments

Comments
 (0)