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

macOS: rework list_ports to support unicode product descriptors. #364

Merged
merged 1 commit into from
Aug 13, 2019
Merged

macOS: rework list_ports to support unicode product descriptors. #364

merged 1 commit into from
Aug 13, 2019

Conversation

torque
Copy link
Contributor

@torque torque commented Jul 13, 2018

A number of Arm-based microcontrollers, such as the kinetis-based Teensy 3 family, provide a native USB interface that allows the user to control a number of attributes, such as the product descriptor string. However, macOS exhibits some weird behavior where it mangles specifically the product descriptor string (and as far as I can tell, only the product descriptor string) on USB serial devices (this also does not seem to happen with other types of USB device). The mangling, which replaces all non-alphanumeric characters with underscores, appears to happen somewhere deep in the kernel's BSD serial subsystem. In order to work around this, the device's product name can be queried with a different IOKit call, specifically IORegistryEntryGetName, which appears to be aliased directly to the (unmangled) USB product descriptor string for all USB devices.

Note: I'm not sure what versions of macOS pyserial aims to support, but I have tested this (briefly) on both 10.11 and 10.13.

This PR also attempts to make a few correctness fixes to the way pyserial interfaces with IOKit. These have been squashed into the same commit for the sake of brevity.

This commit makes some changes to try to improve the behavior of
serial.tools.list_ports on macOS and to reduce the amount of magic
numbers in this code. These changes include a better adherence to iokit
function type declarations as taken from the iokit header files, as
well as switching all iokit calls to use UTF-8 encoding rather than
mac_roman.

The main functional change of this commit is that the devicename is
now retrieved through IORegistryEntryName, which avoids some weird
USB descriptor mangling that happens somewhere deep within the BSD
serial device subsystem in XNU. In particular, on serial devices, all
USB product descriptor characters that are not in the basic
alphanumeric set are replaced with underscores, which is silly as USB
descriptors are unicode strings. This mangling ONLY happens to the
product descriptor string, so the vendor and serial number strings do
not need to be handled differently than before.
@zsquareplusc zsquareplusc merged commit 3835ccb into pyserial:master Aug 13, 2019
@zsquareplusc
Copy link
Member

Thank you

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