Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add NetBSD support #178

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
40 changes: 40 additions & 0 deletions enumerator/usb_netbsd.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
//
// 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 (
"strings"

"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 {
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
}
Comment on lines +33 to +40
Copy link
Member

Choose a reason for hiding this comment

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

It would be great to get the USB VID, PID, and ISerial number is this possible?

Copy link
Author

Choose a reason for hiding this comment

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

Indeed, they are just available precreated node. There is no sysfs on NetBSD yet, maybe have a better solution in the future.

Copy link

Choose a reason for hiding this comment

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

It is possible, what you need to do is to open /dev/usb0 and issue the DRVLISTDEV ioctl. example code in C
I can send a separate PR to add support for that after the basic support is merged.

79 changes: 79 additions & 0 deletions serial_netbsd.go
Copy link

Choose a reason for hiding this comment

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

Hi,

Please consider using this implementation instead. It supports arbitrary BAUD rates and allows to remove the mapping table.

const tcCMSPAR uint32 = 0 // not supported
const tcIUCLC uint32 = 0  // not supported

const tcCRTSCTS uint32 = unix.CRTSCTS

const ioctlTcgetattr = unix.TIOCGETA
const ioctlTcsetattr = unix.TIOCSETA
const ioctlTcflsh = unix.TIOCFLUSH
const ioctlTioccbrk = unix.TIOCCBRK
const ioctlTiocsbrk = unix.TIOCSBRK

func setTermSettingsBaudrate(speed int, settings *unix.Termios) (error, bool) {
	// see https://nxr.netbsd.org/xref/src/lib/libc/termios/cfsetspeed.c
	if speed < 50 || speed > math.MaxInt32 {
		return &PortError{code: InvalidSpeed}, true
	}
	settings.Ispeed = int32(speed)
	settings.Ospeed = int32(speed)
	return nil, false
}

func (port *unixPort) setSpecialBaudrate(speed uint32) error {
	if speed < 50 || speed > math.MaxInt32 {
		return &PortError{code: InvalidSpeed}
	}
	// see https://nxr.netbsd.org/xref/src/lib/libc/termios/tcgetattr.c
	settings, err := unix.IoctlGetTermios(port.handle, ioctlTcgetattr)
	if err != nil {
		return err
	}
	if err, ok := setTermSettingsBaudrate(int(speed), settings); !ok {
		return err
	}
	return unix.IoctlSetTermios(port.handle, ioctlTcsetattr, settings)
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
//
// 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|dtyU|ttyU)[0-9]{1,2}"
Copy link
Member

Choose a reason for hiding this comment

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

What's the difference between dty and tty?

Copy link
Author

Choose a reason for hiding this comment

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

From man tty(4) on NetBSD:
The /dev/ttyXX special file is used for dial-in modems and terminals.
The /dev/dtyXX special file is a SunOS-compatible dial-out device.


// 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 tcCRTSCTS uint32 = 0x00010000
const tcCCTS_OFLOW uint32 = tcCRTSCTS
const tcCRTS_IFLOW uint32 = tcCRTSCTS

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
}

settings.Ispeed = toTermiosSpeedType(baudrate)
settings.Ospeed = toTermiosSpeedType(baudrate)
return nil, false
}

func (port *unixPort) setSpecialBaudrate(speed uint32) error {
// TODO: unimplemented
return &PortError{code: InvalidSpeed}
}
2 changes: 1 addition & 1 deletion serial_resetbuf_linux_bsd.go
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion serial_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion unixutils/pipe.go
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion unixutils/select.go
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down