frame.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. package protocol6400
  2. import (
  3. "encoding/binary"
  4. "errors"
  5. "fmt"
  6. )
  7. const (
  8. Header0 byte = 0xA5
  9. Header1 byte = 0x5A
  10. CRCPoly uint16 = 0x1021
  11. CRCInit uint16 = 0xFFFF
  12. )
  13. var (
  14. ErrHeader = errors.New("invalid frame header")
  15. ErrLength = errors.New("invalid frame length")
  16. ErrCRC = errors.New("CRC mismatch")
  17. ErrType = errors.New("unknown frame type")
  18. ErrTargetRange = errors.New("target must be 0 or 20..70 C")
  19. ErrDutyRange = errors.New("duty must be 0..100")
  20. ErrDuration = errors.New("stimulus duration must be 1..255 seconds")
  21. ErrNotSupported = errors.New("operation not supported by firmware profile")
  22. )
  23. type Frame struct {
  24. Type byte
  25. Payload []byte
  26. }
  27. type Stimulus struct {
  28. DurationSeconds byte
  29. PTC [24]byte
  30. Front [4]byte
  31. Rear [4]byte
  32. }
  33. func CRC16(data []byte) uint16 {
  34. crc := CRCInit
  35. for _, value := range data {
  36. crc ^= uint16(value) << 8
  37. for bit := 0; bit < 8; bit++ {
  38. if crc&0x8000 != 0 {
  39. crc = (crc << 1) ^ CRCPoly
  40. } else {
  41. crc <<= 1
  42. }
  43. }
  44. }
  45. return crc
  46. }
  47. func expectedLength(frameType byte) (int, error) {
  48. switch frameType {
  49. case CmdSetTarget:
  50. return 1, nil
  51. case CmdGetSnapshot, RspSetTargetAck, RspGetSnapshotAck, RspStimulusAck:
  52. return 0, nil
  53. case CmdSetStimulus:
  54. return 33, nil
  55. case RspSnapshotData:
  56. return SnapshotPayloadLen, nil
  57. default:
  58. return 0, fmt.Errorf("%w: 0x%02X", ErrType, frameType)
  59. }
  60. }
  61. func Encode(frameType byte, payload []byte) ([]byte, error) {
  62. want, err := expectedLength(frameType)
  63. if err != nil {
  64. return nil, err
  65. }
  66. if len(payload) != want {
  67. return nil, fmt.Errorf("%w: type 0x%02X want %d got %d", ErrLength, frameType, want, len(payload))
  68. }
  69. body := make([]byte, 0, 2+len(payload))
  70. body = append(body, frameType, byte(len(payload)))
  71. body = append(body, payload...)
  72. crc := CRC16(body)
  73. out := []byte{Header0, Header1}
  74. out = append(out, body...)
  75. out = append(out, byte(crc), byte(crc>>8))
  76. return out, nil
  77. }
  78. func Decode(raw []byte) (Frame, error) {
  79. if len(raw) < 6 {
  80. return Frame{}, ErrLength
  81. }
  82. if raw[0] != Header0 || raw[1] != Header1 {
  83. return Frame{}, ErrHeader
  84. }
  85. n := int(raw[3])
  86. if len(raw) != n+6 {
  87. return Frame{}, ErrLength
  88. }
  89. want, err := expectedLength(raw[2])
  90. if err != nil || want != n {
  91. if err != nil {
  92. return Frame{}, err
  93. }
  94. return Frame{}, ErrLength
  95. }
  96. got := binary.LittleEndian.Uint16(raw[len(raw)-2:])
  97. if got != CRC16(raw[2:len(raw)-2]) {
  98. return Frame{}, ErrCRC
  99. }
  100. payload := append([]byte(nil), raw[4:len(raw)-2]...)
  101. return Frame{Type: raw[2], Payload: payload}, nil
  102. }
  103. func SetTargetRequest(target byte) ([]byte, error) {
  104. if target != 0 && (target < 20 || target > 70) {
  105. return nil, ErrTargetRange
  106. }
  107. return Encode(CmdSetTarget, []byte{target})
  108. }
  109. func GetSnapshotRequest() []byte {
  110. value, err := Encode(CmdGetSnapshot, nil)
  111. if err != nil {
  112. panic(err)
  113. }
  114. return value
  115. }
  116. func IsCanonicalProfile(profile string) bool {
  117. switch profile {
  118. case ProfileControl, ProfileIdentification, ProfileSimDebug:
  119. return true
  120. default:
  121. return false
  122. }
  123. }
  124. func StimulusSupported(profile string) bool {
  125. return profile == ProfileIdentification || profile == ProfileSimDebug
  126. }
  127. func StimulusRequest(profile string, value Stimulus) ([]byte, error) {
  128. if !StimulusSupported(profile) {
  129. return nil, ErrNotSupported
  130. }
  131. if value.DurationSeconds == 0 {
  132. return nil, ErrDuration
  133. }
  134. payload := make([]byte, 0, 33)
  135. payload = append(payload, value.DurationSeconds)
  136. for _, group := range [][]byte{value.PTC[:], value.Front[:], value.Rear[:]} {
  137. for _, duty := range group {
  138. if duty > 100 {
  139. return nil, ErrDutyRange
  140. }
  141. payload = append(payload, duty)
  142. }
  143. }
  144. return Encode(CmdSetStimulus, payload)
  145. }