• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# macOS / iOS
2
3Macs use the usual `termios` TTY implementation that other POSIXes use, but support non-standard
4baud rates through the `iossiospeed` ioctl (as of Mac OS X 10.4). To support non-standard baud rates
5on Mac, there are three main approaches:
6
7 1. Always use `iossiospeed`
8 2. Use `iossiospeed` for non-standard bauds, but `termios` with standard bauds
9 3. Use `iossiospeed` always by default and fail-over to the termios approach
10
11## Implementation notes
12
13This library uses the first approach. Given that macOS as far back as 10.4 supports it (2005), there
14seem to be no downsides. Internally, baud rates within the `termios` struct are kept at 9600 when
15that struct is read & written. This means that anytime the `termios` struct is written back (using
16`tcsetattr` a call to `iossiospeed` follows it. Additionally, the `termios` struct is not cached and
17instead retrieved on every settings adjustment. While this can increase the number of system calls
18when changing port settings, it removes the need to keep state consistent and instead the kernel's
19state can always be considered the canonical source.
20
21## Platform notes
22
23`iossiospeed` has no official documentation that can be found by searching
24https://developer.apple.com. However
25[IOSerialTestLib.c](https://opensource.apple.com/source/IOSerialFamily/IOSerialFamily-93/tests/IOSerialTestLib.c.auto.html)
26can be found on Apple's open source code repository and has some example code for using this API.
27
28Experimentation has shown that there are a few key features to using `iossiospeed`:
29
30 * `iossiospeed` should be called after setting the `termios` struct via `tcsetattr` as that resets
31   the baud rate and you cannot put custom baud rates in the `termios` struct.
32 * Calling `iossiospeed` will modify the `termios` struct in the kernel such that you can no longer
33   round-trip the `termios` struct. The following code will fail:
34   ```C
35   struct termios t;
36   tcgetattr(fd, &t);
37   tcsetattr(fd, TCSANOW, &t)
38   ```
39
40## Reference implementations
41
42 * [picocom](https://github.com/npat-efault/picocom) follows the second approach. However they also
43   cache the existing `termios` struct.
44
45# Additional References
46
47 * [Understanding UNIX termios VMIN and VTIME](http://unixwiz.net/techtips/termios-vmin-vtime.html)
48