summaryrefslogtreecommitdiff
path: root/ipp
diff options
context:
space:
mode:
authorKevin Wallace <kevin@pentabarf.net>2026-04-26 08:18:53 -0700
committerKevin Wallace <kevin@pentabarf.net>2026-04-26 08:18:53 -0700
commit1c8205cff6432248212a160a7a86b8c896dd2a8d (patch)
treec2b33124837881e20becce13285c3b5a9a2a183b /ipp
parentwait for cvend at startup, only redraw on state changes (diff)
cvend wip
Diffstat (limited to 'ipp')
-rw-r--r--ipp/ipp.go40
1 files changed, 24 insertions, 16 deletions
diff --git a/ipp/ipp.go b/ipp/ipp.go
index 89036b0..c71b590 100644
--- a/ipp/ipp.go
+++ b/ipp/ipp.go
@@ -66,11 +66,16 @@ func Open(path string, handler Handler) (Session, error) {
if err != nil {
return nil, err
}
- termios.Iflag &^= unix.IGNBRK | unix.BRKINT | unix.PARMRK | unix.ISTRIP | unix.INLCR | unix.IGNCR | unix.ICRNL | unix.IXON
- termios.Oflag &^= unix.OPOST
- termios.Lflag &^= unix.ECHO | unix.ECHONL | unix.ICANON | unix.ISIG | unix.IEXTEN
- termios.Cflag &^= unix.CBAUD | unix.CSIZE | unix.PARENB | unix.CSTOPB
- termios.Cflag |= unix.B115200 | unix.CS8 | unix.CREAD | unix.CLOCAL
+ termios.Iflag = unix.IGNBRK | unix.IGNPAR
+ termios.Oflag = 0
+ termios.Lflag = 0
+ termios.Cflag = unix.B115200 | unix.CS8 | unix.CREAD | unix.CLOCAL
+ // when VMIN>0, reads can take unacceptably long.
+ // nx sets VMIN=1 VTIME=10 and uses select + FIONREAD + nonblocking read
+ // and somehow seems to avoid this but i cannot replicate it here.
+ // with VMIN=0 VTIME=1, up to 100ms delay, but no more
+ termios.Cc[unix.VMIN] = 0
+ termios.Cc[unix.VTIME] = 1
if err = unix.IoctlSetTermios(fd, unix.TCSETS, termios); err != nil {
return nil, err
}
@@ -84,26 +89,29 @@ func Open(path string, handler Handler) (Session, error) {
return s, nil
}
+func (s *session) isClosed() bool {
+ s.mu.Lock()
+ defer s.mu.Unlock()
+ return s.closed
+}
+
func (s *session) readTask() {
var bs [1 << 16]byte
var offset int
for {
- n, err := unix.Read(s.fd, bs[offset:])
+ nRead, err := unix.Read(s.fd, bs[offset:])
if err != nil {
- s.mu.Lock()
- closed := s.closed
- s.mu.Unlock()
- if closed {
+ if s.isClosed() {
break
}
panic(err)
}
- nRead := s.handleMsg(bs[:offset+n])
- offset = copy(bs[:], bs[nRead:offset+n])
+ nConsumed := s.handleMsg(bs[:offset+nRead])
+ offset = copy(bs[:], bs[nConsumed:offset+nRead])
}
}
-func (s *session) handleMsg(bs []byte) (nRead int) {
+func (s *session) handleMsg(bs []byte) (nConsumed int) {
expectedLen := 1
if len(bs) < expectedLen {
return
@@ -113,7 +121,7 @@ func (s *session) handleMsg(bs []byte) (nRead int) {
if *Trace {
log.Printf("skipping non-IPP message %s", hex.EncodeToString(bs))
}
- nRead = len(bs)
+ nConsumed = len(bs)
return
}
expectedLen += 6
@@ -128,7 +136,7 @@ func (s *session) handleMsg(bs []byte) (nRead int) {
expectedHdrCRC := crc8(bs[0:6]...)
if hdrCRC != expectedHdrCRC {
log.Printf("skipping IPP message %02x, expected crc8 %02x got %02x", ippType, expectedHdrCRC, hdrCRC)
- nRead = len(bs)
+ nConsumed = len(bs)
return
}
expectedLen += ippLen
@@ -139,7 +147,7 @@ func (s *session) handleMsg(bs []byte) (nRead int) {
if len(bs) < expectedLen {
return
}
- nRead = expectedLen
+ nConsumed = expectedLen
ippData := bs[7 : 7+ippLen]
if hasMsgCRC {
msgCRC := binary.LittleEndian.Uint32(bs[7+ippLen : 7+ippLen+4])