| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159 |
- package protocol6400
- import (
- "encoding/binary"
- "errors"
- "fmt"
- )
- const (
- Header0 byte = 0xA5
- Header1 byte = 0x5A
- CRCPoly uint16 = 0x1021
- CRCInit uint16 = 0xFFFF
- )
- var (
- ErrHeader = errors.New("invalid frame header")
- ErrLength = errors.New("invalid frame length")
- ErrCRC = errors.New("CRC mismatch")
- ErrType = errors.New("unknown frame type")
- ErrTargetRange = errors.New("target must be 0 or 20..70 C")
- ErrDutyRange = errors.New("duty must be 0..100")
- ErrDuration = errors.New("stimulus duration must be 1..255 seconds")
- ErrNotSupported = errors.New("operation not supported by firmware profile")
- )
- type Frame struct {
- Type byte
- Payload []byte
- }
- type Stimulus struct {
- DurationSeconds byte
- PTC [24]byte
- Front [4]byte
- Rear [4]byte
- }
- func CRC16(data []byte) uint16 {
- crc := CRCInit
- for _, value := range data {
- crc ^= uint16(value) << 8
- for bit := 0; bit < 8; bit++ {
- if crc&0x8000 != 0 {
- crc = (crc << 1) ^ CRCPoly
- } else {
- crc <<= 1
- }
- }
- }
- return crc
- }
- func expectedLength(frameType byte) (int, error) {
- switch frameType {
- case CmdSetTarget:
- return 1, nil
- case CmdGetSnapshot, RspSetTargetAck, RspGetSnapshotAck, RspStimulusAck:
- return 0, nil
- case CmdSetStimulus:
- return 33, nil
- case RspSnapshotData:
- return SnapshotPayloadLen, nil
- default:
- return 0, fmt.Errorf("%w: 0x%02X", ErrType, frameType)
- }
- }
- func Encode(frameType byte, payload []byte) ([]byte, error) {
- want, err := expectedLength(frameType)
- if err != nil {
- return nil, err
- }
- if len(payload) != want {
- return nil, fmt.Errorf("%w: type 0x%02X want %d got %d", ErrLength, frameType, want, len(payload))
- }
- body := make([]byte, 0, 2+len(payload))
- body = append(body, frameType, byte(len(payload)))
- body = append(body, payload...)
- crc := CRC16(body)
- out := []byte{Header0, Header1}
- out = append(out, body...)
- out = append(out, byte(crc), byte(crc>>8))
- return out, nil
- }
- func Decode(raw []byte) (Frame, error) {
- if len(raw) < 6 {
- return Frame{}, ErrLength
- }
- if raw[0] != Header0 || raw[1] != Header1 {
- return Frame{}, ErrHeader
- }
- n := int(raw[3])
- if len(raw) != n+6 {
- return Frame{}, ErrLength
- }
- want, err := expectedLength(raw[2])
- if err != nil || want != n {
- if err != nil {
- return Frame{}, err
- }
- return Frame{}, ErrLength
- }
- got := binary.LittleEndian.Uint16(raw[len(raw)-2:])
- if got != CRC16(raw[2:len(raw)-2]) {
- return Frame{}, ErrCRC
- }
- payload := append([]byte(nil), raw[4:len(raw)-2]...)
- return Frame{Type: raw[2], Payload: payload}, nil
- }
- func SetTargetRequest(target byte) ([]byte, error) {
- if target != 0 && (target < 20 || target > 70) {
- return nil, ErrTargetRange
- }
- return Encode(CmdSetTarget, []byte{target})
- }
- func GetSnapshotRequest() []byte {
- value, err := Encode(CmdGetSnapshot, nil)
- if err != nil {
- panic(err)
- }
- return value
- }
- func IsCanonicalProfile(profile string) bool {
- switch profile {
- case ProfileControl, ProfileIdentification, ProfileSimDebug:
- return true
- default:
- return false
- }
- }
- func StimulusSupported(profile string) bool {
- return profile == ProfileIdentification || profile == ProfileSimDebug
- }
- func StimulusRequest(profile string, value Stimulus) ([]byte, error) {
- if !StimulusSupported(profile) {
- return nil, ErrNotSupported
- }
- if value.DurationSeconds == 0 {
- return nil, ErrDuration
- }
- payload := make([]byte, 0, 33)
- payload = append(payload, value.DurationSeconds)
- for _, group := range [][]byte{value.PTC[:], value.Front[:], value.Rear[:]} {
- for _, duty := range group {
- if duty > 100 {
- return nil, ErrDutyRange
- }
- payload = append(payload, duty)
- }
- }
- return Encode(CmdSetStimulus, payload)
- }
|