From 502a6d983f981aff18b261a61feded2db6006ec6 Mon Sep 17 00:00:00 2001 From: Kevin Wallace Date: Mon, 2 Mar 2026 15:04:40 -0800 Subject: ipp -> {cvend,pic32}-ipp --- Makefile | 7 +- autorun.sh | 3 +- cmd/cvend-ipp/main.go | 58 ++++++++++++++ cmd/ipp/main.go | 87 --------------------- cmd/pic32-ipp/main.go | 58 ++++++++++++++ cvend/cvend.go | 207 ++++++++------------------------------------------ ipp/ipp.go | 191 ++++++++++++++++++++++++++++++++++++++++++++++ pic32/pic32.go | 21 +++++ 8 files changed, 367 insertions(+), 265 deletions(-) create mode 100644 cmd/cvend-ipp/main.go delete mode 100644 cmd/ipp/main.go create mode 100644 cmd/pic32-ipp/main.go create mode 100644 ipp/ipp.go create mode 100644 pic32/pic32.go diff --git a/Makefile b/Makefile index 354ad49..1620c85 100644 --- a/Makefile +++ b/Makefile @@ -6,7 +6,7 @@ USB_ROOT=/Volumes/MP_SD GOSOURCES:=$(shell find . -name *.go) -all: bin/ipp +all: bin/cvend-ipp bin/pic32-ipp clean: rm -r bin @@ -15,7 +15,8 @@ bin/%: ${GOSOURCES} mkdir -p bin go build -o $@ ./cmd/$* -usb: autorun.sh bin/ipp +usb: autorun.sh bin/cvend-ipp bin/pic32-ipp install autorun.sh ${USB_ROOT}/ install -d ${USB_ROOT}/bin/ - install bin/ipp ${USB_ROOT}/bin/ + install bin/cvend-ipp ${USB_ROOT}/bin/ + install bin/pic32-ipp ${USB_ROOT}/bin/ diff --git a/autorun.sh b/autorun.sh index 14b0fec..c67c48f 100644 --- a/autorun.sh +++ b/autorun.sh @@ -1,3 +1,4 @@ #!/bin/bash -x cd "$(dirname "$0")" -systemd-run -p StandardOutput=tty -d bin/ipp +systemd-run -p StandardOutput=tty -d bin/cvend-ipp +systemd-run -p StandardOutput=tty -d bin/pic32-ipp diff --git a/cmd/cvend-ipp/main.go b/cmd/cvend-ipp/main.go new file mode 100644 index 0000000..ce96732 --- /dev/null +++ b/cmd/cvend-ipp/main.go @@ -0,0 +1,58 @@ +package main + +import ( + "encoding/hex" + "flag" + "log" + "strconv" + "time" + + "pm3.dev/cvend" +) + +var wait = flag.Duration("w", 5*time.Second, "how long to wait for a response") +var follow = flag.Bool("f", false, "don't exit after response") + +func main() { + flag.Parse() + 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) { + cvend.LogIPP(msgType, msgData) + select { + case wc <- struct{}{}: + default: + } + }) + if err != nil { + panic(err) + } + defer s.Close() + for i := 0; i < flag.NArg(); i += 2 { + var msgType byte + var msgData []byte + if n, err := strconv.ParseUint(flag.Arg(i), 0, 8); err != nil { + panic(err) + } else { + msgType = byte(n) + } + if bs, err := hex.DecodeString(flag.Arg(i + 1)); err != nil { + panic(err) + } else { + msgData = bs + } + if err := s.SendIPP(msgType, msgData); err != nil { + panic(err) + } + if *wait > 0 { + select { + case <-wc: + case <-time.After(*wait): + } + } + } + if *follow || flag.NArg() == 0 { + select {} + } +} diff --git a/cmd/ipp/main.go b/cmd/ipp/main.go deleted file mode 100644 index 21cf0fc..0000000 --- a/cmd/ipp/main.go +++ /dev/null @@ -1,87 +0,0 @@ -package main - -import ( - "encoding/binary" - "encoding/hex" - "flag" - "log" - "strconv" - "time" - - "pm3.dev/cvend" -) - -var wait = flag.Duration("w", 5*time.Second, "how long to wait for a response") -var follow = flag.Bool("f", false, "don't exit after response") - -func main() { - flag.Parse() - log.SetFlags(log.Lshortfile | log.Ldate | log.Ltime | log.Lmicroseconds) - log.Println("start") - wc := make(chan struct{}, 1) - cv, err := cvend.Open(cvend.Path, func(msgType byte, msgData []byte) { - switch msgType { - case 0x07: - log.Printf("Heartbeat(%s)", hex.EncodeToString(msgData)) - case 0x0f: - log.Printf("Startup(%s)", hex.EncodeToString(msgData)) - case 0xed: - if len(msgData) == 0 { - log.Printf("Log()") - } else { - log.Printf("Log(%d) %q", msgData[0], string(msgData[1:])) - } - case 0xbe: - if len(msgData) < 22 || len(msgData) < 22+int(msgData[21]) { - log.Printf("PICCRead(short)\n%s", hex.Dump(msgData)) - } else { - _ = msgData[:11] // unknown - uid := msgData[11:18] - atqa := binary.LittleEndian.Uint16(msgData[18:20]) - sak := msgData[20] - atrLen := msgData[21] - atr := msgData[22 : 22+atrLen] - _ = msgData[22+atrLen:] // unknown - log.Printf("PICCRead(uid %s atqa 0x%04x sak %02x atr %s)\n%s", hex.EncodeToString(uid), atqa, sak, hex.EncodeToString(atr), hex.Dump(msgData)) - } - case 0xd1: - log.Printf("EMVTransactionSuccessUnk\n%s", hex.Dump(msgData)) - default: - log.Printf("ipp %02x\n%s", msgType, hex.Dump(msgData)) - } - select { - case wc <- struct{}{}: - default: - } - }) - if err != nil { - panic(err) - } - defer cv.Close() - for i := 0; i < flag.NArg(); i += 2 { - var msgType byte - var msgData []byte - if n, err := strconv.ParseUint(flag.Arg(i), 0, 8); err != nil { - panic(err) - } else { - msgType = byte(n) - } - if bs, err := hex.DecodeString(flag.Arg(i + 1)); err != nil { - panic(err) - } else { - msgData = bs - } - if err := cv.SendIPP(msgType, msgData); err != nil { - panic(err) - } - if *wait > 0 { - select { - case <-wc: - case <-time.After(*wait): - } - } - } - if *follow || flag.NArg() == 0 { - select {} - } -} diff --git a/cmd/pic32-ipp/main.go b/cmd/pic32-ipp/main.go new file mode 100644 index 0000000..34a1ec8 --- /dev/null +++ b/cmd/pic32-ipp/main.go @@ -0,0 +1,58 @@ +package main + +import ( + "encoding/hex" + "flag" + "log" + "strconv" + "time" + + "pm3.dev/pic32" +) + +var wait = flag.Duration("w", 5*time.Second, "how long to wait for a response") +var follow = flag.Bool("f", false, "don't exit after response") + +func main() { + flag.Parse() + 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) { + pic32.LogIPP(msgType, msgData) + select { + case wc <- struct{}{}: + default: + } + }) + if err != nil { + panic(err) + } + defer s.Close() + for i := 0; i < flag.NArg(); i += 2 { + var msgType byte + var msgData []byte + if n, err := strconv.ParseUint(flag.Arg(i), 0, 8); err != nil { + panic(err) + } else { + msgType = byte(n) + } + if bs, err := hex.DecodeString(flag.Arg(i + 1)); err != nil { + panic(err) + } else { + msgData = bs + } + if err := s.SendIPP(msgType, msgData); err != nil { + panic(err) + } + if *wait > 0 { + select { + case <-wc: + case <-time.After(*wait): + } + } + } + if *follow || flag.NArg() == 0 { + select {} + } +} diff --git a/cvend/cvend.go b/cvend/cvend.go index 52c7227..0881db1 100644 --- a/cvend/cvend.go +++ b/cvend/cvend.go @@ -1,189 +1,48 @@ package cvend import ( + "bytes" "encoding/binary" "encoding/hex" - "hash/crc32" - "io" "log" - "sync" - "golang.org/x/sys/unix" + "pm3.dev/ipp" ) const Path = "/dev/ttymxc3" -var crc8table = [256]byte{ - 0x00, 0x5e, 0xbc, 0xe2, 0x61, 0x3f, 0xdd, 0x83, 0xc2, 0x9c, 0x7e, 0x20, 0xa3, 0xfd, 0x1f, 0x41, - 0x9d, 0xc3, 0x21, 0x7f, 0xfc, 0xa2, 0x40, 0x1e, 0x5f, 0x01, 0xe3, 0xbd, 0x3e, 0x60, 0x82, 0xdc, - 0x23, 0x7d, 0x9f, 0xc1, 0x42, 0x1c, 0xfe, 0xa0, 0xe1, 0xbf, 0x5d, 0x03, 0x80, 0xde, 0x3c, 0x62, - 0xbe, 0xe0, 0x02, 0x5c, 0xdf, 0x81, 0x63, 0x3d, 0x7c, 0x22, 0xc0, 0x9e, 0x1d, 0x43, 0xa1, 0xff, - 0x46, 0x18, 0xfa, 0xa4, 0x27, 0x79, 0x9b, 0xc5, 0x84, 0xda, 0x38, 0x66, 0xe5, 0xbb, 0x59, 0x07, - 0xdb, 0x85, 0x67, 0x39, 0xba, 0xe4, 0x06, 0x58, 0x19, 0x47, 0xa5, 0xfb, 0x78, 0x26, 0xc4, 0x9a, - 0x65, 0x3b, 0xd9, 0x87, 0x04, 0x5a, 0xb8, 0xe6, 0xa7, 0xf9, 0x1b, 0x45, 0xc6, 0x98, 0x7a, 0x24, - 0xf8, 0xa6, 0x44, 0x1a, 0x99, 0xc7, 0x25, 0x7b, 0x3a, 0x64, 0x86, 0xd8, 0x5b, 0x05, 0xe7, 0xb9, - 0x8c, 0xd2, 0x30, 0x6e, 0xed, 0xb3, 0x51, 0x0f, 0x4e, 0x10, 0xf2, 0xac, 0x2f, 0x71, 0x93, 0xcd, - 0x11, 0x4f, 0xad, 0xf3, 0x70, 0x2e, 0xcc, 0x92, 0xd3, 0x8d, 0x6f, 0x31, 0xb2, 0xec, 0x0e, 0x50, - 0xaf, 0xf1, 0x13, 0x4d, 0xce, 0x90, 0x72, 0x2c, 0x6d, 0x33, 0xd1, 0x8f, 0x0c, 0x52, 0xb0, 0xee, - 0x32, 0x6c, 0x8e, 0xd0, 0x53, 0x0d, 0xef, 0xb1, 0xf0, 0xae, 0x4c, 0x12, 0x91, 0xcf, 0x2d, 0x73, - 0xca, 0x94, 0x76, 0x28, 0xab, 0xf5, 0x17, 0x49, 0x08, 0x56, 0xb4, 0xea, 0x69, 0x37, 0xd5, 0x8b, - 0x57, 0x09, 0xeb, 0xb5, 0x36, 0x68, 0x8a, 0xd4, 0x95, 0xcb, 0x29, 0x77, 0xf4, 0xaa, 0x48, 0x16, - 0xe9, 0xb7, 0x55, 0x0b, 0x88, 0xd6, 0x34, 0x6a, 0x2b, 0x75, 0x97, 0xc9, 0x4a, 0x14, 0xf6, 0xa8, - 0x74, 0x2a, 0xc8, 0x96, 0x15, 0x4b, 0xa9, 0xf7, 0xb6, 0xe8, 0x0a, 0x54, 0xd7, 0x89, 0x6b, 0x35, -} - -func crc8(bs ...byte) (x byte) { - for _, b := range bs { - x = crc8table[x^b] - } - return -} - -type IPPHandler func(byte, []byte) - -type Device interface { - io.Closer - io.Writer - SendIPP(ippType byte, ippData []byte) error -} - -type device struct { - fd int - handler IPPHandler - mu sync.Mutex - nextSeqOut byte // mu - closed bool // mu -} - -func Open(path string, handler IPPHandler) (Device, error) { - var d device - d.handler = handler - var err error - d.fd, err = unix.Open(path, unix.O_RDWR|unix.O_NOCTTY, 0) - if err != nil { - return nil, err - } - var termios *unix.Termios - termios, err = unix.IoctlGetTermios(d.fd, unix.TCGETS) - 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 - err = unix.IoctlSetTermios(d.fd, unix.TCSETS, termios) - if err != nil { - return nil, err - } - go d.readTask() - return &d, nil -} - -func (d *device) readTask() { - var bs [1 << 16]byte - var offset int - for { - n, err := unix.Read(d.fd, bs[offset:]) - if err != nil { - d.mu.Lock() - closed := d.closed - d.mu.Unlock() - if closed { - break - } - panic(err) +func OpenIPP(handler ipp.Handler) (ipp.Session, error) { + return ipp.Open(Path, handler) +} + +func LogIPP(msgType byte, msgData []byte) { + switch msgType { + case 0x07: + log.Printf("Heartbeat(%s)", hex.EncodeToString(msgData)) + case 0x0f: + log.Printf("Startup(%s)", hex.EncodeToString(msgData)) + case 0xed: + if len(msgData) == 0 { + log.Printf("Log()") + } else { + log.Printf("Log(%d) %s", msgData[0], string(bytes.TrimSuffix(msgData[1:], []byte{0}))) } - nRead := d.handleMsg(bs[:offset+n]) - offset = copy(bs[:], bs[nRead:offset+n]) - } -} - -func (d *device) handleMsg(bs []byte) (nRead int) { - expectedLen := 1 - if len(bs) < expectedLen { - log.Printf("skipping empty message") - return - } - msgType := bs[0] - if msgType != 0xbc { - log.Printf("skipping non-IPP message %s", hex.EncodeToString(bs)) - nRead = len(bs) - return - } - expectedLen += 6 - if len(bs) < expectedLen { - log.Printf("skipping truncated IPP message %s", hex.EncodeToString(bs)) - return - } - _ = bs[1] // seq - _ = bs[2] // unknown - ippType := bs[3] - ippLen := int(bs[4])<<8 + int(bs[5]) - hdrCRC := bs[6] - expectedHdrCRC := crc8(bs[0:6]...) - if hdrCRC != expectedHdrCRC { - log.Printf("skipping IPP message %02x, expected crc8 %02x got %02x", ippType, expectedHdrCRC, hdrCRC) - return - } - expectedLen += ippLen - hasMsgCRC := ippType&0x80 != 0 - if hasMsgCRC { - expectedLen += 4 - } - if len(bs) < expectedLen { - log.Printf("skipping truncated IPP message %s", hex.EncodeToString(bs)) - return - } - nRead = expectedLen - ippData := bs[7 : 7+ippLen] - if hasMsgCRC { - msgCRC := bs[7+ippLen : 7+ippLen+4] - expectedMsgCRC := ^crc32.ChecksumIEEE(ippData) - actualMsgCRC := binary.LittleEndian.Uint32(msgCRC) - if actualMsgCRC != expectedMsgCRC { - log.Printf("skipping IPP message %02x, expected crc32 %08x got %08x", expectedMsgCRC, actualMsgCRC) - return + case 0xbe: + if len(msgData) < 22 || len(msgData) < 22+int(msgData[21]) { + log.Printf("PICCRead(short)\n%s", hex.Dump(msgData)) + } else { + _ = msgData[:11] // unknown + uid := msgData[11:18] + atqa := binary.LittleEndian.Uint16(msgData[18:20]) + sak := msgData[20] + atrLen := msgData[21] + atr := msgData[22 : 22+atrLen] + _ = msgData[22+atrLen:] // unknown + log.Printf("PICCRead(uid %s atqa 0x%04x sak %02x atr %s)\n%s", hex.EncodeToString(uid), atqa, sak, hex.EncodeToString(atr), hex.Dump(msgData)) } + case 0xd1: + log.Printf("EMVStatus\n%s", hex.Dump(msgData)) + default: + log.Printf("ipp %02x\n%s", msgType, hex.Dump(msgData)) } - if d.handler != nil { - d.handler(ippType, ippData) - } - return -} - -func (d *device) Close() error { - d.mu.Lock() - defer d.mu.Unlock() - d.closed = true - return unix.Close(d.fd) -} - -func (d *device) Write(bs []byte) (int, error) { - log.Printf("send %s", hex.EncodeToString(bs)) - return unix.Write(d.fd, bs) -} - -func (d *device) SendIPP(ippType byte, ippData []byte) error { - d.mu.Lock() - d.nextSeqOut++ - if d.nextSeqOut == 0 { - d.nextSeqOut++ - } - seq := d.nextSeqOut - d.mu.Unlock() - ippLen := len(ippData) - ippHdr := []byte{0xbc, seq, 0, ippType, byte(ippLen >> 8), byte(ippLen), 0} - ippHdr[6] = crc8(ippHdr[:6]...) - var msgCRC []byte - if ippType&0x80 != 0 { - msgCRC = make([]byte, 4) - binary.LittleEndian.PutUint32(msgCRC, ^crc32.ChecksumIEEE(ippData)) - } - log.Printf("send %s%s%s", hex.EncodeToString(ippHdr), hex.EncodeToString(ippData), hex.EncodeToString(msgCRC)) - if _, err := unix.Writev(d.fd, [][]byte{ippHdr, ippData, msgCRC}); err != nil { - return err - } - return nil } diff --git a/ipp/ipp.go b/ipp/ipp.go new file mode 100644 index 0000000..e9c1430 --- /dev/null +++ b/ipp/ipp.go @@ -0,0 +1,191 @@ +package ipp + +import ( + "encoding/binary" + "encoding/hex" + "hash/crc32" + "io" + "log" + "sync" + + "golang.org/x/sys/unix" +) + +var crc8table = [256]byte{ + 0x00, 0x5e, 0xbc, 0xe2, 0x61, 0x3f, 0xdd, 0x83, 0xc2, 0x9c, 0x7e, 0x20, 0xa3, 0xfd, 0x1f, 0x41, + 0x9d, 0xc3, 0x21, 0x7f, 0xfc, 0xa2, 0x40, 0x1e, 0x5f, 0x01, 0xe3, 0xbd, 0x3e, 0x60, 0x82, 0xdc, + 0x23, 0x7d, 0x9f, 0xc1, 0x42, 0x1c, 0xfe, 0xa0, 0xe1, 0xbf, 0x5d, 0x03, 0x80, 0xde, 0x3c, 0x62, + 0xbe, 0xe0, 0x02, 0x5c, 0xdf, 0x81, 0x63, 0x3d, 0x7c, 0x22, 0xc0, 0x9e, 0x1d, 0x43, 0xa1, 0xff, + 0x46, 0x18, 0xfa, 0xa4, 0x27, 0x79, 0x9b, 0xc5, 0x84, 0xda, 0x38, 0x66, 0xe5, 0xbb, 0x59, 0x07, + 0xdb, 0x85, 0x67, 0x39, 0xba, 0xe4, 0x06, 0x58, 0x19, 0x47, 0xa5, 0xfb, 0x78, 0x26, 0xc4, 0x9a, + 0x65, 0x3b, 0xd9, 0x87, 0x04, 0x5a, 0xb8, 0xe6, 0xa7, 0xf9, 0x1b, 0x45, 0xc6, 0x98, 0x7a, 0x24, + 0xf8, 0xa6, 0x44, 0x1a, 0x99, 0xc7, 0x25, 0x7b, 0x3a, 0x64, 0x86, 0xd8, 0x5b, 0x05, 0xe7, 0xb9, + 0x8c, 0xd2, 0x30, 0x6e, 0xed, 0xb3, 0x51, 0x0f, 0x4e, 0x10, 0xf2, 0xac, 0x2f, 0x71, 0x93, 0xcd, + 0x11, 0x4f, 0xad, 0xf3, 0x70, 0x2e, 0xcc, 0x92, 0xd3, 0x8d, 0x6f, 0x31, 0xb2, 0xec, 0x0e, 0x50, + 0xaf, 0xf1, 0x13, 0x4d, 0xce, 0x90, 0x72, 0x2c, 0x6d, 0x33, 0xd1, 0x8f, 0x0c, 0x52, 0xb0, 0xee, + 0x32, 0x6c, 0x8e, 0xd0, 0x53, 0x0d, 0xef, 0xb1, 0xf0, 0xae, 0x4c, 0x12, 0x91, 0xcf, 0x2d, 0x73, + 0xca, 0x94, 0x76, 0x28, 0xab, 0xf5, 0x17, 0x49, 0x08, 0x56, 0xb4, 0xea, 0x69, 0x37, 0xd5, 0x8b, + 0x57, 0x09, 0xeb, 0xb5, 0x36, 0x68, 0x8a, 0xd4, 0x95, 0xcb, 0x29, 0x77, 0xf4, 0xaa, 0x48, 0x16, + 0xe9, 0xb7, 0x55, 0x0b, 0x88, 0xd6, 0x34, 0x6a, 0x2b, 0x75, 0x97, 0xc9, 0x4a, 0x14, 0xf6, 0xa8, + 0x74, 0x2a, 0xc8, 0x96, 0x15, 0x4b, 0xa9, 0xf7, 0xb6, 0xe8, 0x0a, 0x54, 0xd7, 0x89, 0x6b, 0x35, +} + +func crc8(bs ...byte) (x byte) { + for _, b := range bs { + x = crc8table[x^b] + } + return +} + +type Handler func(byte, []byte) + +type Session interface { + io.Closer + io.Writer + SendIPP(ippType byte, ippData []byte) error +} + +type session struct { + path string + fd int + handler Handler + mu sync.Mutex + nextSeqOut byte // mu + closed bool // mu +} + +func Open(path string, handler Handler) (Session, error) { + fd, err := unix.Open(path, unix.O_RDWR|unix.O_NOCTTY, 0) + if err != nil { + return nil, err + } + termios, err := unix.IoctlGetTermios(fd, unix.TCGETS) + 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 + if err = unix.IoctlSetTermios(fd, unix.TCSETS, termios); err != nil { + return nil, err + } + + s := &session{ + path: path, + fd: fd, + handler: handler, + } + go s.readTask() + return s, nil +} + +func (s *session) readTask() { + var bs [1 << 16]byte + var offset int + for { + n, err := unix.Read(s.fd, bs[offset:]) + if err != nil { + s.mu.Lock() + closed := s.closed + s.mu.Unlock() + if closed { + break + } + panic(err) + } + nRead := s.handleMsg(bs[:offset+n]) + offset = copy(bs[:], bs[nRead:offset+n]) + } +} + +func (s *session) handleMsg(bs []byte) (nRead int) { + expectedLen := 1 + if len(bs) < expectedLen { + return + } + msgType := bs[0] + if msgType != 0xbc { + log.Printf("skipping non-IPP message %s", hex.EncodeToString(bs)) + nRead = len(bs) + return + } + expectedLen += 6 + if len(bs) < expectedLen { + return + } + _ = bs[1] // seq + _ = bs[2] // unknown + ippType := bs[3] + ippLen := int(bs[4])<<8 + int(bs[5]) + hdrCRC := bs[6] + 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) + return + } + expectedLen += ippLen + hasMsgCRC := ippType&0x80 != 0 + if hasMsgCRC { + expectedLen += 4 + } + if len(bs) < expectedLen { + return + } + nRead = expectedLen + ippData := bs[7 : 7+ippLen] + if hasMsgCRC { + msgCRC := binary.LittleEndian.Uint32(bs[7+ippLen : 7+ippLen+4]) + expectedMsgCRC := ^crc32.ChecksumIEEE(ippData) + if msgCRC != expectedMsgCRC { + log.Printf("skipping IPP message %02x, expected crc32 %08x got %08x", expectedMsgCRC, msgCRC) + return + } + } + var ellipsis string + if ippLen > 0 { + ellipsis = "..." + } + 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) + } + return +} + +func (s *session) Close() error { + s.mu.Lock() + defer s.mu.Unlock() + s.closed = true + return unix.Close(s.fd) +} + +func (s *session) Write(bs []byte) (int, error) { + log.Printf("%s > %s", s.path, hex.EncodeToString(bs)) + return unix.Write(s.fd, bs) +} + +func (s *session) SendIPP(ippType byte, ippData []byte) error { + s.mu.Lock() + s.nextSeqOut++ + if s.nextSeqOut == 0 { + s.nextSeqOut++ + } + seq := s.nextSeqOut + s.mu.Unlock() + ippLen := len(ippData) + ippHdr := []byte{0xbc, seq, 0, ippType, byte(ippLen >> 8), byte(ippLen), 0} + ippHdr[6] = crc8(ippHdr[:6]...) + var msgCRC []byte + if ippType&0x80 != 0 { + msgCRC = make([]byte, 4) + binary.LittleEndian.PutUint32(msgCRC, ^crc32.ChecksumIEEE(ippData)) + } + log.Printf("%s > %s%s%s", s.path, hex.EncodeToString(ippHdr), hex.EncodeToString(ippData), hex.EncodeToString(msgCRC)) + if _, err := unix.Writev(s.fd, [][]byte{ippHdr, ippData, msgCRC}); err != nil { + return err + } + return nil +} diff --git a/pic32/pic32.go b/pic32/pic32.go new file mode 100644 index 0000000..8150ab3 --- /dev/null +++ b/pic32/pic32.go @@ -0,0 +1,21 @@ +package pic32 + +import ( + "encoding/hex" + "log" + + "pm3.dev/ipp" +) + +const Path = "/dev/ttymxc2" + +func OpenIPP(handler ipp.Handler) (ipp.Session, error) { + return ipp.Open(Path, handler) +} + +func LogIPP(msgType byte, msgData []byte) { + switch msgType { + default: + log.Printf("ipp %02x\n%s", msgType, hex.Dump(msgData)) + } +} -- cgit v1.2.3