@@ -105,73 +105,7 @@ fn find_serial_port(ports: &[SerialPortInfo], name: &str) -> Result<SerialPortIn
105105 }
106106}
107107
108- /// Serialport's auto-detect doesn't provide any port information when using
109- /// MUSL Linux we can do some manual parsing of sysfs to get the relevant bits
110- /// without udev
111- #[ cfg( all( target_os = "linux" , target_env = "musl" ) ) ]
112- fn detect_usb_serial_ports ( _list_all_ports : bool ) -> Result < Vec < SerialPortInfo > > {
113- use std:: {
114- fs:: { read_link, read_to_string} ,
115- path:: { Path , PathBuf } ,
116- } ;
117-
118- use serialport:: UsbPortInfo ;
119-
120- let ports = available_ports ( ) . into_diagnostic ( ) ?;
121- let ports = ports
122- . into_iter ( )
123- . filter_map ( |port_info| {
124- // With musl, the paths we get are `/sys/class/tty/*` or `/dev/*`
125- // In case of `/dev/*` we transform them into `/sys/class/tty/*`
126- let path = match AsRef :: < Path > :: as_ref ( & port_info. port_name ) . strip_prefix ( "/dev/" ) {
127- Ok ( rem) => PathBuf :: from ( "/sys/class/tty/" ) . join ( rem) ,
128- Err ( _) => PathBuf :: from ( & port_info. port_name ) ,
129- } ;
130-
131- // This will give something like:
132- // `/sys/devices/pci0000:00/0000:00:07.1/0000:0c:00.3/usb5/5-3/5-3.1/5-3.1:1.0/
133- // ttyUSB0/tty/ttyUSB0`
134- let mut parent_dev = path. canonicalize ( ) . ok ( ) ?;
135-
136- // Walk up 3 dirs to get to the device hosting the tty:
137- // `/sys/devices/pci0000:00/0000:00:07.1/0000:0c:00.3/usb5/5-3/5-3.1/5-3.1:1.0`
138- parent_dev. pop ( ) ;
139- parent_dev. pop ( ) ;
140- parent_dev. pop ( ) ;
141-
142- // Check that the device is using the usb subsystem
143- read_link ( parent_dev. join ( "subsystem" ) )
144- . ok ( )
145- . filter ( |subsystem| subsystem. ends_with ( "usb" ) ) ?;
146-
147- let interface = read_to_string ( parent_dev. join ( "interface" ) )
148- . ok ( )
149- . map ( |s| s. trim ( ) . to_string ( ) ) ;
150-
151- // /sys/devices/pci0000:00/0000:00:07.1/0000:0c:00.3/usb5/5-3/5-3.1
152- parent_dev. pop ( ) ;
153-
154- let vid = read_to_string ( parent_dev. join ( "idVendor" ) ) . ok ( ) ?;
155- let pid = read_to_string ( parent_dev. join ( "idProduct" ) ) . ok ( ) ?;
156-
157- Some ( SerialPortInfo {
158- port_type : SerialPortType :: UsbPort ( UsbPortInfo {
159- vid : u16:: from_str_radix ( vid. trim ( ) , 16 ) . ok ( ) ?,
160- pid : u16:: from_str_radix ( pid. trim ( ) , 16 ) . ok ( ) ?,
161- product : interface,
162- serial_number : None ,
163- manufacturer : None ,
164- } ) ,
165- port_name : format ! ( "/dev/{}" , path. file_name( ) ?. to_str( ) ?) ,
166- } )
167- } )
168- . collect :: < Vec < _ > > ( ) ;
169-
170- Ok ( ports)
171- }
172-
173108/// Returns a vector with available USB serial ports.
174- #[ cfg( not( all( target_os = "linux" , target_env = "musl" ) ) ) ]
175109fn detect_usb_serial_ports ( list_all_ports : bool ) -> Result < Vec < SerialPortInfo > > {
176110 let ports = available_ports ( ) . into_diagnostic ( ) ?;
177111 let ports = ports
0 commit comments