More development. More types. Fixed attribute groups in requests. Started on client. Saving data to file. More types. Printing from chromeos works a little bit. More types. Spelling corrections. WIP: Fix keyword handling Move request to a separate file and add test. Co-authored-by: Henrik Sölver <henrik.solver@gmail.com> Reviewed-on: #6 Co-Authored-By: henrik <henrik.solver@gmail.com> Co-Committed-By: henrik <henrik.solver@gmail.com>
116 lines
2.8 KiB
Go
116 lines
2.8 KiB
Go
package ipp
|
|
|
|
import (
|
|
"encoding/binary"
|
|
"fmt"
|
|
)
|
|
|
|
type ippResponseHeader struct {
|
|
versionNumber versionNumber
|
|
statusCode statusCode
|
|
requestId uint32
|
|
}
|
|
|
|
func (h ippResponseHeader) String() string {
|
|
return fmt.Sprintf("Version number: %v Status code: %v Request Id: %v", h.versionNumber, h.statusCode, h.requestId)
|
|
}
|
|
|
|
func (h *ippResponseHeader) marshal() []byte {
|
|
a := make([]byte, 8, 8)
|
|
binary.BigEndian.PutUint16(a[0:2], uint16(h.versionNumber))
|
|
binary.BigEndian.PutUint16(a[2:4], uint16(h.statusCode))
|
|
binary.BigEndian.PutUint32(a[4:8], h.requestId)
|
|
|
|
return a
|
|
}
|
|
|
|
type Response struct {
|
|
operationAttributes []Attribute
|
|
jobAttributes []Attribute
|
|
printerAttributes []Attribute
|
|
header ippResponseHeader
|
|
}
|
|
|
|
func NewResponse(code statusCode, requestId uint32) *Response {
|
|
r := new(Response)
|
|
r.header.versionNumber = 0x0101
|
|
r.header.requestId = requestId
|
|
r.header.statusCode = code
|
|
r.operationAttributes = make([]Attribute, 0)
|
|
r.printerAttributes = make([]Attribute, 0)
|
|
r.jobAttributes = make([]Attribute, 0)
|
|
return r
|
|
}
|
|
|
|
// func (r *Response) length() int {
|
|
// l := 8 + 1
|
|
// if len(r.jobAttributes) > 0 {
|
|
// l += 1 //
|
|
// for _, e := range r.jobAttributes {
|
|
// l += e.length()
|
|
// }
|
|
// }
|
|
// for _, e := range r.operationAttributes {
|
|
// l += e.length()
|
|
// }
|
|
// for _, e := range r.printerAttributes {
|
|
// l += e.length()
|
|
// }
|
|
|
|
// }
|
|
|
|
func (r Response) String() string {
|
|
|
|
s := r.header.String() + "\n" + " OperationAttributes" + "\n"
|
|
for _, a := range r.operationAttributes {
|
|
s = s + fmt.Sprintf(" %v (%v)\n", a, a.valueTag())
|
|
}
|
|
s = s + " PrinterAttributes" + "\n"
|
|
for _, a := range r.printerAttributes {
|
|
s = s + fmt.Sprintf(" %v (%v)\n", a, a.valueTag())
|
|
}
|
|
s = s + " JobAttributes" + "\n"
|
|
for _, a := range r.jobAttributes {
|
|
s = s + fmt.Sprintf(" %v (%v)\n", a, a.valueTag())
|
|
}
|
|
return s
|
|
}
|
|
|
|
func (r *Response) Marshal() []byte {
|
|
a := make([]byte, 0, 20)
|
|
a = append(a, r.header.marshal()...)
|
|
if len(r.operationAttributes) > 0 {
|
|
a = append(a, byte(operationAttributes))
|
|
for _, e := range r.operationAttributes {
|
|
a = append(a, e.marshal()...)
|
|
}
|
|
}
|
|
if len(r.jobAttributes) > 0 {
|
|
a = append(a, byte(jobAttributes))
|
|
for _, e := range r.jobAttributes {
|
|
a = append(a, e.marshal()...)
|
|
}
|
|
}
|
|
if len(r.printerAttributes) > 0 {
|
|
a = append(a, byte(printerAttributes))
|
|
for _, e := range r.printerAttributes {
|
|
a = append(a, e.marshal()...)
|
|
}
|
|
|
|
}
|
|
a = append(a, byte(endOfAttributes))
|
|
return a
|
|
}
|
|
|
|
func (r *Response) AddPrinterAttribute(a Attribute) {
|
|
r.printerAttributes = append(r.printerAttributes, a)
|
|
}
|
|
|
|
func (r *Response) AddOperatonAttribute(a Attribute) {
|
|
r.operationAttributes = append(r.operationAttributes, a)
|
|
}
|
|
|
|
func (r *Response) AddJobAttribute(a Attribute) {
|
|
r.jobAttributes = append(r.jobAttributes, a)
|
|
}
|