-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement custom baud rates on Apple with the IOSSIOSPEED ioctl.
- Loading branch information
Showing
3 changed files
with
139 additions
and
48 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
use std::path::PathBuf; | ||
use std::os::unix::io::RawFd; | ||
|
||
const IOCTL_IOSSIOSPEED: u64 = 0x80045402; | ||
|
||
/// Set the baud rate of a serial port using the IOSSIOSPEED ioctl. | ||
/// | ||
/// The speed set this way applied to the input and the output speed. | ||
/// It is *not* reflected back in the `termios` struct. | ||
/// There is no ioctl to retrieve the real baud rate -.- | ||
/// | ||
/// This is Apple, so there is no public documentation (why would you?). | ||
/// This is the best I could find: | ||
/// * https://opensource.apple.com/source/IOSerialFamily/IOSerialFamily-91/tests/IOSerialTestLib.c.auto.html | ||
/// * https://developer.apple.com/library/archive/samplecode/SerialPortSample/Listings/SerialPortSample_SerialPortSample_c.html#//apple_ref/doc/uid/DTS10000454-SerialPortSample_SerialPortSample_c-DontLinkElementID_4 | ||
pub fn ioctl_iossiospeed(fd: RawFd, baud_rate: libc::speed_t) -> Result<(), std::io::Error> { | ||
unsafe { | ||
super::check(libc::ioctl(fd, IOCTL_IOSSIOSPEED, &baud_rate))?; | ||
Ok(()) | ||
} | ||
} | ||
|
||
pub fn enumerate() -> std::io::Result<Vec<PathBuf>> { | ||
use std::os::unix::ffi::OsStrExt; | ||
use std::os::unix::fs::FileTypeExt; | ||
|
||
let serial_ports = std::fs::read_dir("/dev")? | ||
.filter_map(|entry| { | ||
let entry = entry.ok()?; | ||
let kind = entry.metadata().ok()?.file_type(); | ||
if kind.is_char_device() && is_tty_name(entry.file_name().as_bytes()) { | ||
Some(entry.path()) | ||
} else { | ||
None | ||
} | ||
}) | ||
.collect(); | ||
Ok(serial_ports) | ||
} | ||
|
||
fn is_tty_name(name: &[u8]) -> bool { | ||
// Sigh, closed source doesn't have to mean undocumented. | ||
// Anyway: | ||
// https://stackoverflow.com/questions/14074413/serial-port-names-on-mac-os-x | ||
// https://learn.adafruit.com/ftdi-friend/com-slash-serial-port-name | ||
name.starts_with(b"tty.") || name.starts_with(b"cu.") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters