Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 24 additions & 5 deletions serial/open_darwin.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,22 @@ type termios struct {
c_ispeed speed_t
c_ospeed speed_t
}
type Port struct{
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Blank line before this

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This shouldn't be exported (i.e. capitalized).

fd int
}

func (p Port) Close() error {
return syscall.Close(p.fd)
}

func (p Port) Read(b []byte) (n int, err error) {
return syscall.Read(p.fd, b)
}

func (p Port) Write(b []byte) (n int, err error) {
return syscall.Write(p.fd,b)
}


// setTermios updates the termios struct associated with a serial port file
// descriptor. This sets appropriate options for how the OS interacts with the
Expand Down Expand Up @@ -199,8 +215,8 @@ func convertOptions(options OpenOptions) (*termios, error) {
func openInternal(options OpenOptions) (io.ReadWriteCloser, error) {
// Open the serial port in non-blocking mode, since otherwise the OS will
// wait for the CARRIER line to be asserted.
file, err :=
os.OpenFile(
fd, err :=
syscall.Open(
options.PortName,
syscall.O_RDWR|syscall.O_NOCTTY|syscall.O_NONBLOCK,
0600)
Expand All @@ -213,7 +229,7 @@ func openInternal(options OpenOptions) (io.ReadWriteCloser, error) {
r1, _, errno :=
syscall.Syscall(
syscall.SYS_FCNTL,
uintptr(file.Fd()),
uintptr(fd),
uintptr(syscall.F_SETFL),
uintptr(0))

Expand All @@ -231,7 +247,7 @@ func openInternal(options OpenOptions) (io.ReadWriteCloser, error) {
return nil, err
}

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You need to ensure that the FD isn't leaked if you return early below.

err = setTermios(file.Fd(), terminalOptions)
err = setTermios(uintptr(fd), terminalOptions)
if err != nil {
return nil, err
}
Expand All @@ -240,7 +256,7 @@ func openInternal(options OpenOptions) (io.ReadWriteCloser, error) {
// Set baud rate with the IOSSIOSPEED ioctl, to support non-standard speeds.
r2, _, errno2 := syscall.Syscall(
syscall.SYS_IOCTL,
uintptr(file.Fd()),
uintptr(fd),
uintptr(kIOSSIOSPEED),
uintptr(unsafe.Pointer(&options.BaudRate)))

Expand All @@ -253,6 +269,9 @@ func openInternal(options OpenOptions) (io.ReadWriteCloser, error) {
}
}

file := Port{
fd: fd,
}
// We're done.
return file, nil
}