backend.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. package backend
  2. import (
  3. "context"
  4. "errors"
  5. "time"
  6. "hth/internal/protocol"
  7. )
  8. var (
  9. ErrReadOnly = errors.New("backend is read-only")
  10. ErrNotConnected = errors.New("backend not connected")
  11. ErrReplaySerial = errors.New("replay backend refuses to open a real serial device")
  12. ErrStreamUntrusted = errors.New("uart stream untrusted; reconnect required")
  13. ErrWriteNotStarted = errors.New("serial write not started")
  14. )
  15. type WriteOutcome int
  16. const (
  17. WriteNotStarted WriteOutcome = iota
  18. WriteFullyWritten
  19. WritePartiallyWritten
  20. WriteFullyWrittenWithError
  21. WriteOutcomeUnknown
  22. WriteDisconnected
  23. )
  24. // Historical aliases used by existing tests.
  25. const (
  26. WriteFull = WriteFullyWritten
  27. WriteShort = WritePartiallyWritten
  28. WriteAmbiguous = WriteFullyWrittenWithError
  29. )
  30. func (o WriteOutcome) String() string {
  31. switch o {
  32. case WriteNotStarted:
  33. return "NOT_STARTED"
  34. case WriteFullyWritten:
  35. return "FULLY_WRITTEN"
  36. case WritePartiallyWritten:
  37. return "PARTIALLY_WRITTEN"
  38. case WriteFullyWrittenWithError:
  39. return "FULLY_WRITTEN_WITH_ERROR"
  40. case WriteDisconnected:
  41. return "DISCONNECTED"
  42. default:
  43. return "OUTCOME_UNKNOWN"
  44. }
  45. }
  46. // WriteNeedsReconnect reports outcomes that may have left the UART parser
  47. // misaligned. The transport must be closed; the original action is never
  48. // auto-replayed.
  49. func WriteNeedsReconnect(wr WriteResult, err error) bool {
  50. if wr.Outcome == WriteNotStarted {
  51. return false
  52. }
  53. if err != nil && wr.Outcome == WriteFullyWritten {
  54. return true
  55. }
  56. switch wr.Outcome {
  57. case WritePartiallyWritten, WriteFullyWrittenWithError, WriteDisconnected, WriteOutcomeUnknown:
  58. return true
  59. default:
  60. return err != nil
  61. }
  62. }
  63. type WriteResult struct {
  64. Outcome WriteOutcome
  65. N int
  66. }
  67. type TimeWindow struct {
  68. RequestSent time.Time
  69. RequestComplete time.Time
  70. ResponseStart time.Time
  71. ResponseComplete time.Time
  72. HostReceive time.Time
  73. }
  74. type TempReport struct {
  75. Samples []protocol.TemperatureSample
  76. Window TimeWindow
  77. Raw []byte
  78. }
  79. type MotorReport struct {
  80. Status protocol.MotorStatus
  81. Window TimeWindow
  82. Raw []byte
  83. }
  84. type PollMode int
  85. const (
  86. PollUART PollMode = iota
  87. PollReplay
  88. )
  89. type Backend interface {
  90. Kind() string
  91. PollMode() PollMode
  92. Open(ctx context.Context) error
  93. Close() error
  94. WriteSetAll(ctx context.Context, s protocol.SetAll) (WriteResult, error)
  95. GetTemperature(ctx context.Context) (*TempReport, error)
  96. GetMotorStatus(ctx context.Context) (*MotorReport, error)
  97. }
  98. // Interrupter unblocks an in-flight Read without requiring the caller to hold
  99. // the transaction mutex. Serial Close/Interrupt must not wait for txnMu.
  100. type Interrupter interface {
  101. Interrupt()
  102. }
  103. type ReplayItem struct {
  104. Kind string
  105. EventID uint64
  106. WallTime time.Time
  107. Snapshot []byte
  108. Event []byte
  109. }
  110. type Replayer interface {
  111. Backend
  112. Next(ctx context.Context) (ReplayItem, error)
  113. }