summaryrefslogtreecommitdiff
path: root/ipp/ipp.go
diff options
context:
space:
mode:
Diffstat (limited to 'ipp/ipp.go')
-rw-r--r--ipp/ipp.go22
1 files changed, 12 insertions, 10 deletions
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 {