POSIX specific implementation details¶
This document details implementation details specific to fully POSIX compliant platforms, including Linux distributions and macOS, OS X.
Note
POSIX compliant platforms are identified using the presence of the
__unix
macro, except Apple’s OS X platform, which is identified
separately since it doesn’t define the aforementioned macro.
Serial device management¶
Serial device management is mostly standardized on POSIX compliant platforms, except for serial device paths and advanced operations such as DTR/RTS management.
Serial device detection on POSIX compliant platforms¶
As described in SUSv4TC2 section 10, devices should be present in
/dev
. Therefore, we attempt at finding devices with the following
patterns in the filesystem, expressed using regular expressions:
/dev/ttyUSB[0-9]+
USB serial devices on Linux distributions; see USB serial on Linux for more information.
/dev/cua[dn][0-9]+
Call-out ports on FreeBSD; see Serial communications on FreeBSD for more information.
/dev/cu\..+
Call-out ports on macOS, OS X.
/dev/dty[0-9]+
Call-out ports on NetBSD; see tty(4) on NetBSD for more information.
Serial link handling on POSIX compliant platforms¶
Opening a serial link on POSIX compliant platforms is accomplished by
cahute_open_posix_serial_link()
, interpreting the given name or path as
a file path, using open(2)
.
This will obtain us a file descriptor (fd), which can then use for the following operations:
Closing using
close(2)
;Sending uses
write(2)
;Serial params setting uses
termios(3)
, includingtcdrain(3)
, andtty_ioctl(4)
if available, especiallyTIOCMGET
andTIOCMSET
.
File implementation¶
When creating or opening a file on POSIX compliant systems,
cahute_create_posix_file()
and cahute_open_posix_file()
use open(2)
. Then, depending on the situation:
On creation, we want to set the file size to the provided one.
In order to do this, we call
ftruncate(2)
.On reading, we want to get the current file size.
In order to do this, we call
lseek(2)
to seek 0 bytes fromSEEK_END
, which returns the current file size, then use the same function to seek 0 bytes fromSEEK_SET
.
In order to open the standard output, we just use file descriptor 1
.
Once a file or stdout is opened, we have a file descriptor we can then use for the following operations: