//go:build !windows

package winkeyer

// linectl_other.go — non-Windows DTR/RTS line control via go.bug.st/serial. On
// Linux/macOS the SetDTR/SetRTS ioctls control the lines independently, so the
// CP210x-on-Windows problem that motivated the native path (see linectl_windows.go)
// doesn't apply. OpsLog ships on Windows; this keeps the package building elsewhere.

import (
	"fmt"
	"strings"

	"go.bug.st/serial"
)

type serialLineCtl struct {
	port serial.Port
}

func openLineCtl(port string) (lineCtl, error) {
	if strings.TrimSpace(port) == "" {
		return nil, fmt.Errorf("no serial port")
	}
	p, err := serial.Open(port, &serial.Mode{BaudRate: 9600, DataBits: 8, Parity: serial.NoParity, StopBits: serial.OneStopBit})
	if err != nil {
		return nil, fmt.Errorf("open %s: %w", port, err)
	}
	c := &serialLineCtl{port: p}
	_ = c.setDTR(false)
	_ = c.setRTS(false)
	return c, nil
}

func (c *serialLineCtl) setDTR(on bool) error { return c.port.SetDTR(on) }
func (c *serialLineCtl) setRTS(on bool) error { return c.port.SetRTS(on) }
func (c *serialLineCtl) close() error         { return c.port.Close() }
