combine multi byte fields into one field

This commit is contained in:
Joel Wetzell
2026-05-26 07:26:53 -05:00
parent 225fb8b79a
commit 76189580db
5 changed files with 52 additions and 73 deletions
+15 -15
View File
@@ -6,15 +6,20 @@ import (
"slices" "slices"
) )
type Command string
const (
SwoutText Command = "SwoutText"
SwinText Command = "SwinText"
)
type ArtCommand struct { type ArtCommand struct {
ID [8]uint8 ID [8]uint8
OpCode uint16 OpCode uint16
ProtVerHi uint8 ProtVerHi uint8
ProtVerLo uint8 ProtVerLo uint8
EstaManHi uint8 EstaMan uint16
EstaManLo uint8 Length uint16
LengthHi uint8
LengthLo uint8
Data []uint8 Data []uint8
} }
@@ -36,8 +41,7 @@ func (ap *ArtCommand) SetData(data string) error {
} }
ap.Data = make([]uint8, len(data)) ap.Data = make([]uint8, len(data))
copy(ap.Data, data) copy(ap.Data, data)
ap.LengthHi = uint8(len(data) >> 8) ap.Length = uint16(len(data))
ap.LengthLo = uint8(len(data) & 0xff)
return nil return nil
} }
@@ -58,12 +62,10 @@ func (ap *ArtCommand) UnmarshalBinary(data []byte) error {
ap.ProtVerLo = data[11] ap.ProtVerLo = data[11]
offset := 12 offset := 12
ap.EstaManHi = data[offset] ap.EstaMan = binary.LittleEndian.Uint16(data[offset : offset+2])
ap.EstaManLo = data[offset+1] ap.Length = binary.LittleEndian.Uint16(data[offset+2 : offset+4])
ap.LengthHi = data[offset+2]
ap.LengthLo = data[offset+3]
dataLength := int(ap.LengthHi)<<8 + int(ap.LengthLo) dataLength := int(ap.Length)
if len(data[offset+4:]) < dataLength { if len(data[offset+4:]) < dataLength {
return errors.New("[]byte length not long enough to contain data length specified in packet") return errors.New("[]byte length not long enough to contain data length specified in packet")
@@ -79,10 +81,8 @@ func (ap *ArtCommand) MarshalBinary() ([]byte, error) {
binary.LittleEndian.PutUint16(data[8:10], ap.OpCode) binary.LittleEndian.PutUint16(data[8:10], ap.OpCode)
data[10] = ap.ProtVerHi data[10] = ap.ProtVerHi
data[11] = ap.ProtVerLo data[11] = ap.ProtVerLo
data[12] = ap.EstaManHi binary.LittleEndian.PutUint16(data[12:14], ap.EstaMan)
data[13] = ap.EstaManLo binary.LittleEndian.PutUint16(data[14:16], ap.Length)
data[14] = ap.LengthHi
data[15] = ap.LengthLo
data = append(data, ap.Data...) data = append(data, ap.Data...)
return data, nil return data, nil
} }
+9 -22
View File
@@ -22,12 +22,9 @@ type ArtDataRequest struct {
OpCode uint16 OpCode uint16
ProtVerHi uint8 ProtVerHi uint8
ProtVerLo uint8 ProtVerLo uint8
EstaManHi uint8 EstaMan uint16
EstaManLo uint8 Oem uint16
OemHi uint8 Request uint16
OemLo uint8
RequestHi uint8
RequestLo uint8
spare [22]byte spare [22]byte
} }
@@ -43,10 +40,6 @@ func (ap *ArtDataRequest) GetID() [8]uint8 {
return ap.ID return ap.ID
} }
func (ap *ArtDataRequest) GetDataRequestType() DataRequest {
return DataRequest(uint16(ap.RequestHi)<<8 + uint16(ap.RequestLo))
}
func (ap *ArtDataRequest) UnmarshalBinary(data []byte) error { func (ap *ArtDataRequest) UnmarshalBinary(data []byte) error {
if len(data) < 40 { if len(data) < 40 {
@@ -64,12 +57,9 @@ func (ap *ArtDataRequest) UnmarshalBinary(data []byte) error {
ap.ProtVerLo = data[11] ap.ProtVerLo = data[11]
offset := 12 offset := 12
ap.EstaManHi = data[offset] ap.EstaMan = binary.LittleEndian.Uint16(data[offset : offset+2])
ap.EstaManLo = data[offset+1] ap.Oem = binary.LittleEndian.Uint16(data[offset+2 : offset+4])
ap.OemHi = data[offset+2] ap.Request = binary.LittleEndian.Uint16(data[offset+4 : offset+6])
ap.OemLo = data[offset+3]
ap.RequestHi = data[offset+4]
ap.RequestLo = data[offset+5]
copy(ap.spare[:], data[offset+6:offset+28]) copy(ap.spare[:], data[offset+6:offset+28])
return nil return nil
} }
@@ -80,12 +70,9 @@ func (ap *ArtDataRequest) MarshalBinary() ([]byte, error) {
binary.LittleEndian.PutUint16(data[8:10], ap.OpCode) binary.LittleEndian.PutUint16(data[8:10], ap.OpCode)
data[10] = ap.ProtVerHi data[10] = ap.ProtVerHi
data[11] = ap.ProtVerLo data[11] = ap.ProtVerLo
data[12] = ap.EstaManHi binary.LittleEndian.PutUint16(data[12:14], ap.EstaMan)
data[13] = ap.EstaManLo binary.LittleEndian.PutUint16(data[14:16], ap.Oem)
data[14] = ap.OemHi binary.LittleEndian.PutUint16(data[16:18], ap.Request)
data[15] = ap.OemLo
data[16] = ap.RequestHi
data[17] = ap.RequestLo
copy(data[18:40], ap.spare[:]) copy(data[18:40], ap.spare[:])
return data, nil return data, nil
} }
+6 -11
View File
@@ -25,8 +25,7 @@ type ArtDiagData struct {
DiagPriority DiagPriority DiagPriority DiagPriority
LogicalPort uint8 LogicalPort uint8
filler3 uint8 filler3 uint8
LengthHi uint8 Length uint16
LengthLo uint8
Data []uint8 Data []uint8
} }
@@ -63,16 +62,13 @@ func (ap *ArtDiagData) UnmarshalBinary(data []byte) error {
ap.DiagPriority = DiagPriority(data[offset+1]) ap.DiagPriority = DiagPriority(data[offset+1])
ap.LogicalPort = data[offset+2] ap.LogicalPort = data[offset+2]
ap.filler3 = data[offset+3] ap.filler3 = data[offset+3]
ap.LengthHi = data[offset+4] ap.Length = binary.LittleEndian.Uint16(data[offset+4 : offset+6])
ap.LengthLo = data[offset+5]
dataLength := int(ap.LengthHi)<<8 + int(ap.LengthLo) if len(data[offset+6:]) < int(ap.Length) {
if len(data[offset+6:]) < dataLength {
return errors.New("[]byte length not long enough to contain data length specified in packet") return errors.New("[]byte length not long enough to contain data length specified in packet")
} }
ap.Data = make([]uint8, dataLength) ap.Data = make([]uint8, ap.Length)
copy(ap.Data, data[offset+6:offset+6+dataLength]) copy(ap.Data, data[offset+6:offset+6+int(ap.Length)])
return nil return nil
} }
@@ -86,8 +82,7 @@ func (ap *ArtDiagData) MarshalBinary() ([]byte, error) {
data[13] = uint8(ap.DiagPriority) data[13] = uint8(ap.DiagPriority)
data[14] = ap.LogicalPort data[14] = ap.LogicalPort
data[15] = ap.filler3 data[15] = ap.filler3
data[16] = ap.LengthHi binary.LittleEndian.PutUint16(data[16:18], ap.Length)
data[17] = ap.LengthLo
data = append(data, ap.Data...) data = append(data, ap.Data...)
return data, nil return data, nil
} }
+7 -16
View File
@@ -7,20 +7,13 @@ import (
) )
type ArtPoll struct { type ArtPoll struct {
ID [8]uint8 ID [8]uint8
OpCode uint16 OpCode uint16
ProtVerHi uint8 ProtVerHi uint8
ProtVerLo uint8 ProtVerLo uint8
Flags uint8 Flags uint8
DiagPriority uint8 DiagPriority uint8
AddressTopHi uint8 // TODO(jwetzell): support extended poll fields
AddressTopLo uint8
AddressBottomHi uint8
AddressBottomLo uint8
EstaManHi uint8
EstaManLo uint8
OemHi uint8
OemLo uint8
} }
func (ap *ArtPoll) GetOpCode() uint16 { func (ap *ArtPoll) GetOpCode() uint16 {
@@ -54,7 +47,6 @@ func (ap *ArtPoll) UnmarshalBinary(data []byte) error {
offset := 12 offset := 12
ap.Flags = data[offset] ap.Flags = data[offset]
ap.DiagPriority = data[offset+1] ap.DiagPriority = data[offset+1]
//TODO(jwetzell): unpack extended poll fields
return nil return nil
} }
@@ -66,6 +58,5 @@ func (ap *ArtPoll) MarshalBinary() ([]byte, error) {
data[11] = ap.ProtVerLo data[11] = ap.ProtVerLo
data[12] = ap.Flags data[12] = ap.Flags
data[13] = ap.DiagPriority data[13] = ap.DiagPriority
//TODO(jwetzell): pack extended poll fields
return data, nil return data, nil
} }
+15 -9
View File
@@ -6,6 +6,15 @@ import (
"slices" "slices"
) )
type Key uint8
const (
KeyASCII Key = 0
KeyMacro Key = 1
KeySoft Key = 2
KeyShow Key = 3
)
type ArtTrigger struct { type ArtTrigger struct {
ID [8]uint8 ID [8]uint8
OpCode uint16 OpCode uint16
@@ -13,9 +22,8 @@ type ArtTrigger struct {
ProtVerLo uint8 ProtVerLo uint8
filler1 uint8 filler1 uint8
filler2 uint8 filler2 uint8
OemHi uint8 Oem uint16
OemLo uint8 Key Key
Key uint8
SubKey uint8 SubKey uint8
Data []uint8 Data []uint8
} }
@@ -51,9 +59,8 @@ func (ap *ArtTrigger) UnmarshalBinary(data []byte) error {
offset := 12 offset := 12
ap.filler1 = data[offset] ap.filler1 = data[offset]
ap.filler2 = data[offset+1] ap.filler2 = data[offset+1]
ap.OemHi = data[offset+2] ap.Oem = binary.LittleEndian.Uint16(data[offset+2 : offset+4])
ap.OemLo = data[offset+3] ap.Key = Key(data[offset+4])
ap.Key = data[offset+4]
ap.SubKey = data[offset+5] ap.SubKey = data[offset+5]
ap.Data = make([]uint8, len(data[offset+6:])) ap.Data = make([]uint8, len(data[offset+6:]))
copy(ap.Data, data[offset+6:]) copy(ap.Data, data[offset+6:])
@@ -68,9 +75,8 @@ func (ap *ArtTrigger) MarshalBinary() ([]byte, error) {
data[11] = ap.ProtVerLo data[11] = ap.ProtVerLo
data[12] = ap.filler1 data[12] = ap.filler1
data[13] = ap.filler2 data[13] = ap.filler2
data[14] = ap.OemHi binary.LittleEndian.PutUint16(data[14:16], ap.Oem)
data[15] = ap.OemLo data[16] = uint8(ap.Key)
data[16] = ap.Key
data[17] = ap.SubKey data[17] = ap.SubKey
data = append(data, ap.Data...) data = append(data, ap.Data...)
return data, nil return data, nil