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

multi-language STRING Desc support #122

Merged
merged 10 commits into from
Jul 27, 2023
Merged

multi-language STRING Desc support #122

merged 10 commits into from
Jul 27, 2023

Conversation

eZioPan
Copy link
Contributor

@eZioPan eZioPan commented Jul 21, 2023

add a extra_lang_ids: Option<&'a [u16]> extra_lang_ids: Option<&'a [LangID]> to struct Config to store LANGIDs.

extend descriptor_type::STRING branch of fn get_descriptor(), so that it will send all support LANGID in first STRING Desc, and try responding every STRING Request with matched LANGID.

add a .extra_lang_ids() .set_extra_lang_ids() method to UsbDeviceBuilder, which will take a &[u16] &[LangID] to add extra support LANGID.

Breaking

UsbDeviceBuilder::manufacturer() UsbDeviceBuilder::product() UsbDeviceBuilder::serial_number() will ask a &[&str] as input, first &str in &[&str] will match English lang, the rest will match what have been put into .extra_lang_ids() .set_extra_lang_ids().

@eZioPan eZioPan marked this pull request as ready for review July 21, 2023 12:27
Copy link
Member

@ryan-summers ryan-summers left a comment

Choose a reason for hiding this comment

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

Looking really cool. Glad to see we're adding support for non-English language :)

Some minor feedback requested to make this more robust

src/device.rs Outdated Show resolved Hide resolved
src/device.rs Outdated Show resolved Hide resolved
src/device.rs Outdated Show resolved Hide resolved
src/device_builder.rs Outdated Show resolved Hide resolved
src/device_builder.rs Outdated Show resolved Hide resolved
src/device_builder.rs Outdated Show resolved Hide resolved
src/device_builder.rs Outdated Show resolved Hide resolved
src/device_builder.rs Outdated Show resolved Hide resolved
src/device_builder.rs Outdated Show resolved Hide resolved
Cross check the list length between `extra_lang_ids`, `manufacturer`, `product` and `serial_number` on constructing `UsbDeviceBuilder`.
Build `lang_id_bytes` with rust iterators in `descriptor_type::STRING` of `fn get_descriptor()`.
A quick impl `LangID` enum.
@eZioPan
Copy link
Contributor Author

eZioPan commented Jul 25, 2023

hi, I have do some modifications.
And LangID implementation is really ugly and buggy, would you give me some hints?

src/device.rs Outdated Show resolved Hide resolved
Replace `u16` to `LangID` impl with `num_enum::TryFromPrimitive`.
Add `defmt` formatter for `LangID`.
Reject any requests that asking lang_ids which is not supported by device.
Copy link
Member

@ryan-summers ryan-summers left a comment

Choose a reason for hiding this comment

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

Looking really good - one last request

src/device_builder.rs Outdated Show resolved Hide resolved
src/device_builder.rs Show resolved Hide resolved
src/device_builder.rs Show resolved Hide resolved
Co-authored-by: Ryan Summers <[email protected]>
@eZioPan
Copy link
Contributor Author

eZioPan commented Jul 26, 2023

hi, I still got a few concerns.

  1. Currently the max count of lang_id is limit to 16, which isn't forced by USB Spec. I pick this number "randomly". I'm not sure if it's a good idea.
    * If this limitation is ok, then maybe we need to specify this number as a const value, such that we can easily modify it when some one meets the limit. This number has been used in device_buider.rs and device.rs(to specify lang_id_bytes length), so maybe write a const in lib.rs file? What would you sugguest?
    thanks for @ithinuel , now it's not a problem!
  2. The variants of LangID enum is dumped from MS-LCID. I did some modification, I'm not very sure about my modification is ok. And if these are ok, then shall we doc something on LangID enum?
    1. I manually remove every LANGID mark as Reserved, Neither defined nor reserved, temporarily assigned, etc.
      I keep the LANGID both marked with a language and Reserved.
    2. I blindly replace hyphen with underscore, and make every character upper case(since 0x004D named as, happens to be a keyword of rust).
    3. ff-NG and ff-Latn-NG occupy the same LANGID 0x0467, I concat them to FF_NG__FF_LATN_NG
  3. Different Host USB stack may have different LANGID implemtations, and since LANGID has been marked as "deprecated" both by USB.org and MS. Should we doc something on LangID enum?
  4. shall we export LangID enum in prelude mod?

Copy link
Contributor

@ithinuel ithinuel left a comment

Choose a reason for hiding this comment

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

I wonder if it wouldn't be simpler/less costly to check that EN_US is part of the custom lang_ids list at the time it is set, rather than having constructs like [LangID::EN_US].iter().chain(extra_lang_ids.iter()).

See: https://github.com/eZioPan/usb-device/compare/multi-language-support...ithinuel:usb-device:PR122

@eZioPan
Copy link
Contributor Author

eZioPan commented Jul 26, 2023

Hi @ithinuel , thanks for the work. I think it's a good solution. Let see what @ryan-summers will say about this.

And I will sugguest to ensure that LangID::EN_US be the first lang_id in the list, rather only containing it.

@ithinuel
Copy link
Contributor

A modified my proposal to check LangID::EN_US is first.
I also added a commit that should lift the arbitrary limit to the number of languages.

@eZioPan
Copy link
Contributor Author

eZioPan commented Jul 27, 2023

@ithinuel it looks really cool. I have test on my device, and find that there could be a minor bug.
In src/device.rs Line 560, currently it is if len < buf.len(), I think it should be if buf.len() < len ?

Copy link
Member

@ryan-summers ryan-summers left a comment

Choose a reason for hiding this comment

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

I'd prefer if we just got the groundwork in here, and maybe @ithinuel wants to follow-up with a new PR with their improvements? :)

Thanks to both of you for the great work on this!

src/device_builder.rs Outdated Show resolved Hide resolved
src/device_builder.rs Outdated Show resolved Hide resolved
src/device_builder.rs Outdated Show resolved Hide resolved
@eZioPan
Copy link
Contributor Author

eZioPan commented Jul 27, 2023

I have reverse the change, keep 16 lang_ids limitation.

@ryan-summers
Copy link
Member

ryan-summers commented Jul 27, 2023

Different Host USB stack may have different LANGID implemtations, and since LANGID has been marked as "deprecated" both by USB.org and MS. Should we doc something on LangID enum?

Please do - if this is the case, I'm less concerned about the overall "correctness" of the various lang IDs - if people hit issues, they can submit PRs to fix it, I don't see a huge risk in a slightly incorrect lang ID outside of potentially failing to enumerate properly on some PC dialects, but given that lang ID is deprecated, I imagine this will go away in the future anyways.

shall we export LangID enum in prelude mod?

No, as long as LangID is publicly accessible from the crate. prelude is generally useful for traits that people aren't explicitly instantiating in their code.

@ryan-summers
Copy link
Member

ryan-summers commented Jul 27, 2023

If you want to add documentation to LangID, feel free. If not, I'll merge as-is in a little while.

In general, I think we could use a rewrite of the builder to be something like .extra_language([LangID::DE, [manufacturer_in_german, product_in_german, serial_in_german]]) - I think this would solve the weirdness of this current API, but it's also a niche usecase

@eZioPan
Copy link
Contributor Author

eZioPan commented Jul 27, 2023

Thanks, I would ask a merge. I'm afraid that I'm not very good at English, so maybe I just leave it.
What I can provide is that currently this LANGID is dumped from MS-LCID Protocol Revision 15.0, so if anyone find a bug, their can fetch some information from the MS side.

@ryan-summers ryan-summers merged commit 7ebb10f into rust-embedded-community:master Jul 27, 2023
3 checks passed
@ryan-summers
Copy link
Member

I understand :) Thanks a lot for working on this! It's been greatly appreciated

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.

3 participants