From 34605f5d638c5b2437951723038d640796e4c85a Mon Sep 17 00:00:00 2001 From: Rui-Xiang Guo Date: Tue, 30 Jan 2024 15:38:31 +0800 Subject: [PATCH 1/5] Add support for NetBSD. --- serial_netbsd.go | 87 ++++++++++++++++++++++++++++++++++++ serial_resetbuf_linux_bsd.go | 2 +- serial_unix.go | 2 +- unixutils/pipe.go | 2 +- unixutils/select.go | 2 +- 5 files changed, 91 insertions(+), 4 deletions(-) create mode 100644 serial_netbsd.go diff --git a/serial_netbsd.go b/serial_netbsd.go new file mode 100644 index 0000000..16ff938 --- /dev/null +++ b/serial_netbsd.go @@ -0,0 +1,87 @@ +// +// Copyright 2014-2023 Cristian Maglie. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. +// + +package serial + +import "golang.org/x/sys/unix" + +const devFolder = "/dev" +const regexFilter = "^(dty|tty)\\..*" + +// termios manipulation functions + +var baudrateMap = map[int]uint32{ + 0: unix.B9600, // Default to 9600 + 50: unix.B50, + 75: unix.B75, + 110: unix.B110, + 134: unix.B134, + 150: unix.B150, + 200: unix.B200, + 300: unix.B300, + 600: unix.B600, + 1200: unix.B1200, + 1800: unix.B1800, + 2400: unix.B2400, + 4800: unix.B4800, + 9600: unix.B9600, + 19200: unix.B19200, + 38400: unix.B38400, + 57600: unix.B57600, + 115200: unix.B115200, + 230400: unix.B230400, + 460800: unix.B460800, + 921600: unix.B921600, +} + +var databitsMap = map[int]uint32{ + 0: unix.CS8, // Default to 8 bits + 5: unix.CS5, + 6: unix.CS6, + 7: unix.CS7, + 8: unix.CS8, +} + +const tcCMSPAR uint32 = 0 // may be CMSPAR or PAREXT +const tcIUCLC uint32 = 0 + +const tcCCTS_OFLOW uint32 = 0x00010000 +const tcCRTS_IFLOW uint32 = 0x00020000 + +const tcCRTSCTS uint32 = tcCCTS_OFLOW + +const ioctlTcgetattr = unix.TIOCGETA +const ioctlTcsetattr = unix.TIOCSETA +const ioctlTcflsh = unix.TIOCFLUSH +const ioctlTioccbrk = unix.TIOCCBRK +const ioctlTiocsbrk = unix.TIOCSBRK + +func toTermiosSpeedType(speed uint32) int32 { + return int32(speed) +} + +func setTermSettingsBaudrate(speed int, settings *unix.Termios) (error, bool) { + baudrate, ok := baudrateMap[speed] + if !ok { + return nil, true + } + // XXX: Is Cflag really needed + // revert old baudrate + for _, rate := range baudrateMap { + settings.Cflag &^= rate + } + // set new baudrate + settings.Cflag |= baudrate + + settings.Ispeed = toTermiosSpeedType(baudrate) + settings.Ospeed = toTermiosSpeedType(baudrate) + return nil, false +} + +func (port *unixPort) setSpecialBaudrate(speed uint32) error { + // TODO: unimplemented + return &PortError{code: InvalidSpeed} +} diff --git a/serial_resetbuf_linux_bsd.go b/serial_resetbuf_linux_bsd.go index 105669d..97b0238 100644 --- a/serial_resetbuf_linux_bsd.go +++ b/serial_resetbuf_linux_bsd.go @@ -4,7 +4,7 @@ // license that can be found in the LICENSE file. // -//go:build linux || freebsd || openbsd +//go:build linux || freebsd || netbsd || openbsd package serial diff --git a/serial_unix.go b/serial_unix.go index f6ec896..8cc10d4 100644 --- a/serial_unix.go +++ b/serial_unix.go @@ -4,7 +4,7 @@ // license that can be found in the LICENSE file. // -//go:build linux || darwin || freebsd || openbsd +//go:build linux || darwin || freebsd || netbsd || openbsd package serial diff --git a/unixutils/pipe.go b/unixutils/pipe.go index 748de34..75eab68 100644 --- a/unixutils/pipe.go +++ b/unixutils/pipe.go @@ -4,7 +4,7 @@ // license that can be found in the LICENSE file. // -//go:build linux || darwin || freebsd || openbsd +//go:build linux || darwin || freebsd || netbsd || openbsd package unixutils diff --git a/unixutils/select.go b/unixutils/select.go index 9f0e214..26a8f3c 100644 --- a/unixutils/select.go +++ b/unixutils/select.go @@ -4,7 +4,7 @@ // license that can be found in the LICENSE file. // -//go:build linux || darwin || freebsd || openbsd +//go:build linux || darwin || freebsd || netbsd || openbsd package unixutils From 2a2e1da2ff2acd6e02ac98fdd1b573225a5b3a04 Mon Sep 17 00:00:00 2001 From: Rui-Xiang Guo Date: Wed, 7 Feb 2024 15:57:18 +0800 Subject: [PATCH 2/5] Fix serial device regexFilter and add enumerator/usb_netbsd.go for NetBSD. --- enumerator/usb_netbsd.go | 25 +++++++++++++++++++++++++ serial_netbsd.go | 2 +- 2 files changed, 26 insertions(+), 1 deletion(-) create mode 100644 enumerator/usb_netbsd.go diff --git a/enumerator/usb_netbsd.go b/enumerator/usb_netbsd.go new file mode 100644 index 0000000..87c37c8 --- /dev/null +++ b/enumerator/usb_netbsd.go @@ -0,0 +1,25 @@ +// +// Copyright 2014-2023 Cristian Maglie. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. +// + +package enumerator + +import ( + "go.bug.st/serial" +) + +func nativeGetDetailedPortsList() ([]*PortDetails, error) { + // Retrieve the port list + ports, err := serial.GetPortsList() + if err != nil { + return nil, &PortEnumerationError{causedBy: err} + } + + var res []*PortDetails + for _, port := range ports { + res = append(res, &PortDetails{Name: port}) + } + return res, nil +} diff --git a/serial_netbsd.go b/serial_netbsd.go index 16ff938..52a517c 100644 --- a/serial_netbsd.go +++ b/serial_netbsd.go @@ -9,7 +9,7 @@ package serial import "golang.org/x/sys/unix" const devFolder = "/dev" -const regexFilter = "^(dty|tty)\\..*" +const regexFilter = "(dty|tty|dtyU|ttyU)[0-9]{1,2}" // termios manipulation functions From b41d308c5781f93ff1761af4cdc2cb37bce2928f Mon Sep 17 00:00:00 2001 From: Rui-Xiang Guo Date: Sun, 11 Feb 2024 17:52:22 +0800 Subject: [PATCH 3/5] Change tcCCTS_OFLOW and tcCRTS_IFLOW to be consistent with termios.h; Remove Cflag settings of baudrate. --- serial_netbsd.go | 14 +++----------- 1 file changed, 3 insertions(+), 11 deletions(-) diff --git a/serial_netbsd.go b/serial_netbsd.go index 52a517c..95355c3 100644 --- a/serial_netbsd.go +++ b/serial_netbsd.go @@ -48,10 +48,9 @@ var databitsMap = map[int]uint32{ const tcCMSPAR uint32 = 0 // may be CMSPAR or PAREXT const tcIUCLC uint32 = 0 -const tcCCTS_OFLOW uint32 = 0x00010000 -const tcCRTS_IFLOW uint32 = 0x00020000 - -const tcCRTSCTS uint32 = tcCCTS_OFLOW +const tcCRTSCTS uint32 = 0x00010000 +const tcCCTS_OFLOW uint32 = tcCCTS_OFLOW +const tcCRTS_IFLOW uint32 = tcCCTS_OFLOW const ioctlTcgetattr = unix.TIOCGETA const ioctlTcsetattr = unix.TIOCSETA @@ -68,13 +67,6 @@ func setTermSettingsBaudrate(speed int, settings *unix.Termios) (error, bool) { if !ok { return nil, true } - // XXX: Is Cflag really needed - // revert old baudrate - for _, rate := range baudrateMap { - settings.Cflag &^= rate - } - // set new baudrate - settings.Cflag |= baudrate settings.Ispeed = toTermiosSpeedType(baudrate) settings.Ospeed = toTermiosSpeedType(baudrate) From 2d5c000e4fd402ed6c4aac2c3aa02525b4b2897a Mon Sep 17 00:00:00 2001 From: Rui-Xiang Guo Date: Mon, 12 Feb 2024 21:10:22 +0800 Subject: [PATCH 4/5] Set tcCCTS_OFLOW and tcCRTS_IFLOW to tcCRTSCTS, not themself. --- serial_netbsd.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/serial_netbsd.go b/serial_netbsd.go index 95355c3..87619cd 100644 --- a/serial_netbsd.go +++ b/serial_netbsd.go @@ -49,8 +49,8 @@ const tcCMSPAR uint32 = 0 // may be CMSPAR or PAREXT const tcIUCLC uint32 = 0 const tcCRTSCTS uint32 = 0x00010000 -const tcCCTS_OFLOW uint32 = tcCCTS_OFLOW -const tcCRTS_IFLOW uint32 = tcCCTS_OFLOW +const tcCCTS_OFLOW uint32 = tcCRTSCTS +const tcCRTS_IFLOW uint32 = tcCRTSCTS const ioctlTcgetattr = unix.TIOCGETA const ioctlTcsetattr = unix.TIOCSETA From 13f19178e778df6f3ff360e51eca024657589bf3 Mon Sep 17 00:00:00 2001 From: Rui-Xiang Guo Date: Sun, 18 Feb 2024 09:35:19 +0800 Subject: [PATCH 5/5] Add simple IsUSB condition --- enumerator/usb_netbsd.go | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/enumerator/usb_netbsd.go b/enumerator/usb_netbsd.go index 87c37c8..b6d7e8f 100644 --- a/enumerator/usb_netbsd.go +++ b/enumerator/usb_netbsd.go @@ -7,6 +7,8 @@ package enumerator import ( + "strings" + "go.bug.st/serial" ) @@ -19,7 +21,20 @@ func nativeGetDetailedPortsList() ([]*PortDetails, error) { var res []*PortDetails for _, port := range ports { - res = append(res, &PortDetails{Name: port}) + details, err := nativeGetPortDetails(port) + if err != nil { + return nil, &PortEnumerationError{causedBy: err} + } + res = append(res, details) } return res, nil } + +func nativeGetPortDetails(portPath string) (*PortDetails, error) { + result := &PortDetails{Name: portPath} + + if strings.Contains(result.Name, "U") { + result.IsUSB = true + } + return result, nil +}