diff --git a/command.go b/command.go index 9d79c1f..06e1e9c 100644 --- a/command.go +++ b/command.go @@ -6,15 +6,20 @@ import ( "slices" ) +type Command string + +const ( + SwoutText Command = "SwoutText" + SwinText Command = "SwinText" +) + type ArtCommand struct { ID [8]uint8 OpCode uint16 ProtVerHi uint8 ProtVerLo uint8 - EstaManHi uint8 - EstaManLo uint8 - LengthHi uint8 - LengthLo uint8 + EstaMan uint16 + Length uint16 Data []uint8 } @@ -36,8 +41,7 @@ func (ap *ArtCommand) SetData(data string) error { } ap.Data = make([]uint8, len(data)) copy(ap.Data, data) - ap.LengthHi = uint8(len(data) >> 8) - ap.LengthLo = uint8(len(data) & 0xff) + ap.Length = uint16(len(data)) return nil } @@ -58,12 +62,10 @@ func (ap *ArtCommand) UnmarshalBinary(data []byte) error { ap.ProtVerLo = data[11] offset := 12 - ap.EstaManHi = data[offset] - ap.EstaManLo = data[offset+1] - ap.LengthHi = data[offset+2] - ap.LengthLo = data[offset+3] + ap.EstaMan = binary.LittleEndian.Uint16(data[offset : offset+2]) + ap.Length = binary.LittleEndian.Uint16(data[offset+2 : offset+4]) - dataLength := int(ap.LengthHi)<<8 + int(ap.LengthLo) + dataLength := int(ap.Length) if len(data[offset+4:]) < dataLength { 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) data[10] = ap.ProtVerHi data[11] = ap.ProtVerLo - data[12] = ap.EstaManHi - data[13] = ap.EstaManLo - data[14] = ap.LengthHi - data[15] = ap.LengthLo + binary.LittleEndian.PutUint16(data[12:14], ap.EstaMan) + binary.LittleEndian.PutUint16(data[14:16], ap.Length) data = append(data, ap.Data...) return data, nil } diff --git a/data_request.go b/datarequest.go similarity index 71% rename from data_request.go rename to datarequest.go index cf93ada..cabfddb 100644 --- a/data_request.go +++ b/datarequest.go @@ -22,12 +22,9 @@ type ArtDataRequest struct { OpCode uint16 ProtVerHi uint8 ProtVerLo uint8 - EstaManHi uint8 - EstaManLo uint8 - OemHi uint8 - OemLo uint8 - RequestHi uint8 - RequestLo uint8 + EstaMan uint16 + Oem uint16 + Request uint16 spare [22]byte } @@ -43,10 +40,6 @@ func (ap *ArtDataRequest) GetID() [8]uint8 { return ap.ID } -func (ap *ArtDataRequest) GetDataRequestType() DataRequest { - return DataRequest(uint16(ap.RequestHi)<<8 + uint16(ap.RequestLo)) -} - func (ap *ArtDataRequest) UnmarshalBinary(data []byte) error { if len(data) < 40 { @@ -64,12 +57,9 @@ func (ap *ArtDataRequest) UnmarshalBinary(data []byte) error { ap.ProtVerLo = data[11] offset := 12 - ap.EstaManHi = data[offset] - ap.EstaManLo = data[offset+1] - ap.OemHi = data[offset+2] - ap.OemLo = data[offset+3] - ap.RequestHi = data[offset+4] - ap.RequestLo = data[offset+5] + ap.EstaMan = binary.LittleEndian.Uint16(data[offset : offset+2]) + ap.Oem = binary.LittleEndian.Uint16(data[offset+2 : offset+4]) + ap.Request = binary.LittleEndian.Uint16(data[offset+4 : offset+6]) copy(ap.spare[:], data[offset+6:offset+28]) return nil } @@ -80,12 +70,9 @@ func (ap *ArtDataRequest) MarshalBinary() ([]byte, error) { binary.LittleEndian.PutUint16(data[8:10], ap.OpCode) data[10] = ap.ProtVerHi data[11] = ap.ProtVerLo - data[12] = ap.EstaManHi - data[13] = ap.EstaManLo - data[14] = ap.OemHi - data[15] = ap.OemLo - data[16] = ap.RequestHi - data[17] = ap.RequestLo + binary.LittleEndian.PutUint16(data[12:14], ap.EstaMan) + binary.LittleEndian.PutUint16(data[14:16], ap.Oem) + binary.LittleEndian.PutUint16(data[16:18], ap.Request) copy(data[18:40], ap.spare[:]) return data, nil } diff --git a/diag_data.go b/diagdata.go similarity index 84% rename from diag_data.go rename to diagdata.go index 081239b..85357c4 100644 --- a/diag_data.go +++ b/diagdata.go @@ -25,8 +25,7 @@ type ArtDiagData struct { DiagPriority DiagPriority LogicalPort uint8 filler3 uint8 - LengthHi uint8 - LengthLo uint8 + Length uint16 Data []uint8 } @@ -63,16 +62,13 @@ func (ap *ArtDiagData) UnmarshalBinary(data []byte) error { ap.DiagPriority = DiagPriority(data[offset+1]) ap.LogicalPort = data[offset+2] ap.filler3 = data[offset+3] - ap.LengthHi = data[offset+4] - ap.LengthLo = data[offset+5] + ap.Length = binary.LittleEndian.Uint16(data[offset+4 : offset+6]) - dataLength := int(ap.LengthHi)<<8 + int(ap.LengthLo) - - if len(data[offset+6:]) < dataLength { + if len(data[offset+6:]) < int(ap.Length) { return errors.New("[]byte length not long enough to contain data length specified in packet") } - ap.Data = make([]uint8, dataLength) - copy(ap.Data, data[offset+6:offset+6+dataLength]) + ap.Data = make([]uint8, ap.Length) + copy(ap.Data, data[offset+6:offset+6+int(ap.Length)]) return nil } @@ -86,8 +82,7 @@ func (ap *ArtDiagData) MarshalBinary() ([]byte, error) { data[13] = uint8(ap.DiagPriority) data[14] = ap.LogicalPort data[15] = ap.filler3 - data[16] = ap.LengthHi - data[17] = ap.LengthLo + binary.LittleEndian.PutUint16(data[16:18], ap.Length) data = append(data, ap.Data...) return data, nil } diff --git a/poll.go b/poll.go index 6254003..4543992 100644 --- a/poll.go +++ b/poll.go @@ -7,20 +7,13 @@ import ( ) type ArtPoll struct { - ID [8]uint8 - OpCode uint16 - ProtVerHi uint8 - ProtVerLo uint8 - Flags uint8 - DiagPriority uint8 - AddressTopHi uint8 - AddressTopLo uint8 - AddressBottomHi uint8 - AddressBottomLo uint8 - EstaManHi uint8 - EstaManLo uint8 - OemHi uint8 - OemLo uint8 + ID [8]uint8 + OpCode uint16 + ProtVerHi uint8 + ProtVerLo uint8 + Flags uint8 + DiagPriority uint8 + // TODO(jwetzell): support extended poll fields } func (ap *ArtPoll) GetOpCode() uint16 { @@ -54,7 +47,6 @@ func (ap *ArtPoll) UnmarshalBinary(data []byte) error { offset := 12 ap.Flags = data[offset] ap.DiagPriority = data[offset+1] - //TODO(jwetzell): unpack extended poll fields return nil } @@ -66,6 +58,5 @@ func (ap *ArtPoll) MarshalBinary() ([]byte, error) { data[11] = ap.ProtVerLo data[12] = ap.Flags data[13] = ap.DiagPriority - //TODO(jwetzell): pack extended poll fields return data, nil } diff --git a/trigger.go b/trigger.go index 170636f..75a487f 100644 --- a/trigger.go +++ b/trigger.go @@ -6,6 +6,15 @@ import ( "slices" ) +type Key uint8 + +const ( + KeyASCII Key = 0 + KeyMacro Key = 1 + KeySoft Key = 2 + KeyShow Key = 3 +) + type ArtTrigger struct { ID [8]uint8 OpCode uint16 @@ -13,9 +22,8 @@ type ArtTrigger struct { ProtVerLo uint8 filler1 uint8 filler2 uint8 - OemHi uint8 - OemLo uint8 - Key uint8 + Oem uint16 + Key Key SubKey uint8 Data []uint8 } @@ -51,9 +59,8 @@ func (ap *ArtTrigger) UnmarshalBinary(data []byte) error { offset := 12 ap.filler1 = data[offset] ap.filler2 = data[offset+1] - ap.OemHi = data[offset+2] - ap.OemLo = data[offset+3] - ap.Key = data[offset+4] + ap.Oem = binary.LittleEndian.Uint16(data[offset+2 : offset+4]) + ap.Key = Key(data[offset+4]) ap.SubKey = data[offset+5] ap.Data = make([]uint8, len(data[offset+6:])) copy(ap.Data, data[offset+6:]) @@ -68,9 +75,8 @@ func (ap *ArtTrigger) MarshalBinary() ([]byte, error) { data[11] = ap.ProtVerLo data[12] = ap.filler1 data[13] = ap.filler2 - data[14] = ap.OemHi - data[15] = ap.OemLo - data[16] = ap.Key + binary.LittleEndian.PutUint16(data[14:16], ap.Oem) + data[16] = uint8(ap.Key) data[17] = ap.SubKey data = append(data, ap.Data...) return data, nil