From eb13241672e34b7d982f0644aa9bced34edcf493 Mon Sep 17 00:00:00 2001 From: Kevin Wallace Date: Sun, 26 Apr 2026 10:29:08 -0700 Subject: replyTo --- cmd/beep/main.go | 6 +++--- cmd/cvend-ipp/main.go | 4 ++-- cmd/orca/main.go | 2 +- cmd/pic32-ipp/main.go | 4 ++-- cvend/cvend.go | 10 +++++----- ipp/ipp.go | 22 ++++++++++++---------- 6 files changed, 25 insertions(+), 23 deletions(-) diff --git a/cmd/beep/main.go b/cmd/beep/main.go index 56263d5..7af1f56 100644 --- a/cmd/beep/main.go +++ b/cmd/beep/main.go @@ -19,13 +19,13 @@ func main() { cv := must(cvend.OpenIPP(nil)) defer cv.Close() var lastIgnition bool - pic := must(pic32.OpenIPP(func(msgType byte, msgData []byte) { + pic := must(pic32.OpenIPP(func(seq, replyTo byte, msgType byte, msgData []byte) { if msgType == 0x03 && binary.BigEndian.Uint16(msgData[:2]) == 0x03fb { ignition := msgData[2] != 0 if lastIgnition != ignition { lastIgnition = ignition if ignition { - if err := cv.SendIPP(0x22, []byte{0x06, 0x00, 0x01, 0x00}); err != nil { + if err := cv.SendIPP(0x22, 0, []byte{0x06, 0x00, 0x01, 0x00}); err != nil { panic(err) } } @@ -34,7 +34,7 @@ func main() { })) defer pic.Close() for { - if err := pic.SendIPP(0x02, []byte{0x03, 0xfb}); err != nil { + if err := pic.SendIPP(0x02, 0, []byte{0x03, 0xfb}); err != nil { panic(err) } time.Sleep(100 * time.Millisecond) diff --git a/cmd/cvend-ipp/main.go b/cmd/cvend-ipp/main.go index a985dc6..b8782d4 100644 --- a/cmd/cvend-ipp/main.go +++ b/cmd/cvend-ipp/main.go @@ -20,7 +20,7 @@ func main() { log.SetFlags(log.Lshortfile | log.Ldate | log.Ltime | log.Lmicroseconds) log.Println("cvend-ipp start") wc := make(chan struct{}, 1) - s, err := cvend.OpenIPP(func(msgType byte, msgData []byte) { + s, err := cvend.OpenIPP(func(seq, replyTo byte, msgType byte, msgData []byte) { cvend.LogIPP(msgType, msgData) select { case wc <- struct{}{}: @@ -44,7 +44,7 @@ func main() { } else { msgData = bs } - if err := s.SendIPP(msgType, msgData); err != nil { + if err := s.SendIPP(msgType, 0, msgData); err != nil { panic(err) } if *wait > 0 { diff --git a/cmd/orca/main.go b/cmd/orca/main.go index a068617..5d292e2 100644 --- a/cmd/orca/main.go +++ b/cmd/orca/main.go @@ -101,7 +101,7 @@ func cvendTask(out chan<- cvendState) { go func() { // send periodic status requests to keep cvend from going to sleep for { time.Sleep(30 * time.Second) - if err := cv.SendIPP(0x04, nil); err != nil { + if err := cv.SendIPP(0x04, 0, nil); err != nil { log.Printf("failed to send Status: %s", err) } } diff --git a/cmd/pic32-ipp/main.go b/cmd/pic32-ipp/main.go index c6d3d98..34bc571 100644 --- a/cmd/pic32-ipp/main.go +++ b/cmd/pic32-ipp/main.go @@ -20,7 +20,7 @@ func main() { log.SetFlags(log.Lshortfile | log.Ldate | log.Ltime | log.Lmicroseconds) log.Println("pic32-ipp start") wc := make(chan struct{}, 1) - s, err := pic32.OpenIPP(func(msgType byte, msgData []byte) { + s, err := pic32.OpenIPP(func(seq, replyTo byte, msgType byte, msgData []byte) { pic32.LogIPP(msgType, msgData) select { case wc <- struct{}{}: @@ -44,7 +44,7 @@ func main() { } else { msgData = bs } - if err := s.SendIPP(msgType, msgData); err != nil { + if err := s.SendIPP(msgType, 0, msgData); err != nil { panic(err) } if *wait > 0 { diff --git a/cvend/cvend.go b/cvend/cvend.go index b002629..08d84fc 100644 --- a/cvend/cvend.go +++ b/cvend/cvend.go @@ -101,7 +101,7 @@ func Open() (*Device, error) { proxCardFunctionReply: make(chan []byte), } var err error - d.Session, err = ipp.Open(Path, func(msgType byte, msgData []byte) { + d.Session, err = ipp.Open(Path, func(seq, replyTo byte, msgType byte, msgData []byte) { // if channel receiver waiting, deliver data to it, else log var ch chan []byte switch msgType { @@ -149,7 +149,7 @@ func (d *Device) Close() error { func (d *Device) AwaitStatus() ([]byte, error) { for { - if err := d.SendIPP(0x04, nil); err != nil { // Status + if err := d.SendIPP(0x04, 0, nil); err != nil { // Status return nil, err } select { @@ -172,7 +172,7 @@ func (d *Device) ProxCardFunction(cardType uint16, enable bool) ([]byte, error) if enable { payload[3] = 1 } - if err := d.SendIPP(0xe4, payload[:]); err != nil { + if err := d.SendIPP(0xe4, 0, payload[:]); err != nil { return nil, err } select { @@ -226,7 +226,7 @@ func (c *DESFireCard) Release() error { return ErrCardRemoved default: } - return c.d.SendIPP(0x32, nil) + return c.d.SendIPP(0x32, 0, nil) } func (c *DESFireCard) Command(cmd byte, data []byte) ([]byte, error) { @@ -235,7 +235,7 @@ func (c *DESFireCard) Command(cmd byte, data []byte) ([]byte, error) { return nil, ErrCardRemoved default: } - if err := c.d.SendIPP(0xbc, append([]byte{cmd}, data...)); err != nil { + if err := c.d.SendIPP(0xbc, 0, append([]byte{cmd}, data...)); err != nil { return nil, err } select { diff --git a/ipp/ipp.go b/ipp/ipp.go index c71b590..26eabce 100644 --- a/ipp/ipp.go +++ b/ipp/ipp.go @@ -13,6 +13,7 @@ import ( ) var Trace = flag.Bool("ipp-trace", false, "log IPP protocol traces") +var Seq = flag.Int("ipp-seq", 0, "IPP sequence counter start value") var crc8table = [256]byte{ 0x00, 0x5e, 0xbc, 0xe2, 0x61, 0x3f, 0xdd, 0x83, 0xc2, 0x9c, 0x7e, 0x20, 0xa3, 0xfd, 0x1f, 0x41, @@ -40,12 +41,12 @@ func crc8(bs ...byte) (x byte) { return } -type Handler func(byte, []byte) +type Handler func(seq, replyTo byte, msgType byte, msgData []byte) type Session interface { io.Closer io.Writer - SendIPP(ippType byte, ippData []byte) error + SendIPP(ippType byte, replyTo byte, ippData []byte) error } type session struct { @@ -81,9 +82,10 @@ func Open(path string, handler Handler) (Session, error) { } s := &session{ - path: path, - fd: fd, - handler: handler, + path: path, + fd: fd, + handler: handler, + nextSeqOut: byte(*Seq), } go s.readTask() return s, nil @@ -128,8 +130,8 @@ func (s *session) handleMsg(bs []byte) (nConsumed int) { if len(bs) < expectedLen { return } - _ = bs[1] // seq - _ = bs[2] // unknown + seq := bs[1] // seq + replyTo := bs[2] ippType := bs[3] ippLen := int(bs[4])<<8 + int(bs[5]) hdrCRC := bs[6] @@ -165,7 +167,7 @@ func (s *session) handleMsg(bs []byte) (nConsumed int) { log.Printf("%s < %s%s%s", s.path, hex.EncodeToString(bs[:7]), ellipsis, hex.EncodeToString(bs[7+ippLen:expectedLen])) } if s.handler != nil { - s.handler(ippType, ippData) + s.handler(seq, replyTo, ippType, ippData) } return } @@ -186,7 +188,7 @@ func (s *session) Write(bs []byte) (int, error) { return unix.Write(s.fd, bs) } -func (s *session) SendIPP(ippType byte, ippData []byte) error { +func (s *session) SendIPP(ippType byte, replyTo byte, ippData []byte) error { s.mu.Lock() s.nextSeqOut++ if s.nextSeqOut == 0 { @@ -195,7 +197,7 @@ func (s *session) SendIPP(ippType byte, ippData []byte) error { seq := s.nextSeqOut s.mu.Unlock() ippLen := len(ippData) - ippHdr := []byte{0xbc, seq, 0, ippType, byte(ippLen >> 8), byte(ippLen), 0} + ippHdr := []byte{0xbc, seq, replyTo, ippType, byte(ippLen >> 8), byte(ippLen), 0} ippHdr[6] = crc8(ippHdr[:6]...) var msgCRC []byte if ippType&0x80 != 0 { -- cgit v1.2.3