hth6400.go 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. package hth6400
  2. import (
  3. "context"
  4. "fmt"
  5. "prod2u6400-uartkit/internal/i2creg"
  6. "prod2u6400-uartkit/internal/protocol"
  7. "prod2u6400-uartkit/internal/transport"
  8. )
  9. type SetAll = protocol.SetAll
  10. type Frame = protocol.Frame
  11. type Ack = protocol.Ack
  12. type I2CTransaction = i2creg.Transaction
  13. type UARTTransport = transport.UARTTransport
  14. type I2CRegisterTransport interface {
  15. WriteSetAll(context.Context, SetAll) error
  16. GetSnapshot(context.Context) ([]byte, error)
  17. }
  18. type MockTransport = transport.MockTransport
  19. type ReplayTransport = transport.ReplayTransport
  20. // EncodeFrame is the shared A5 5A + CRC codec for response/replay fixtures.
  21. func EncodeFrame(frameType byte, payload []byte) []byte {
  22. return protocol.Encode(frameType, payload)
  23. }
  24. // DecodeFrame validates framing and CRC without assigning application meaning.
  25. func DecodeFrame(raw []byte) (Frame, error) {
  26. return protocol.Decode(raw)
  27. }
  28. func SetAllFrame(s SetAll) ([]byte, error) { return protocol.EncodeSetAll(s) }
  29. func GetSnapshotFrame() []byte { return protocol.EncodeGetSnapshot() }
  30. func DecodeAckNack(raw []byte) (Ack, error) {
  31. f, err := protocol.Decode(raw)
  32. if err != nil {
  33. return Ack{}, err
  34. }
  35. return protocol.DecodeAck(f)
  36. }
  37. func DecodeSnapshot(raw []byte) ([]byte, error) {
  38. f, err := protocol.Decode(raw)
  39. if err != nil {
  40. return nil, err
  41. }
  42. if f.Type != protocol.TypeSnapshot {
  43. return nil, fmt.Errorf("unexpected snapshot type 0x%02X", f.Type)
  44. }
  45. return f.Payload, nil
  46. }
  47. func SetAllI2C(s SetAll) ([]I2CTransaction, error) { return i2creg.SetAllTransactions(s) }
  48. func GetSnapshotI2C() I2CTransaction { return i2creg.GetSnapshotTransactions()[0] }
  49. func SnapshotReadI2C(length uint16, chunk int) ([]I2CTransaction, error) {
  50. return i2creg.SnapshotReads(length, chunk)
  51. }