backend.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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. // ErrResponseTimeout is a GET timeout after leftover drain; the stream is
  15. // still aligned so the actor must not onDrop / bump epoch.
  16. ErrResponseTimeout = errors.New("serial response timeout; stream still aligned")
  17. )
  18. type WriteOutcome int
  19. const (
  20. WriteNotStarted WriteOutcome = iota
  21. WriteFullyWritten
  22. WritePartiallyWritten
  23. WriteFullyWrittenWithError
  24. WriteOutcomeUnknown
  25. WriteDisconnected
  26. )
  27. // Historical aliases used by existing tests.
  28. const (
  29. WriteFull = WriteFullyWritten
  30. WriteShort = WritePartiallyWritten
  31. WriteAmbiguous = WriteFullyWrittenWithError
  32. )
  33. func (o WriteOutcome) String() string {
  34. switch o {
  35. case WriteNotStarted:
  36. return "NOT_STARTED"
  37. case WriteFullyWritten:
  38. return "FULLY_WRITTEN"
  39. case WritePartiallyWritten:
  40. return "PARTIALLY_WRITTEN"
  41. case WriteFullyWrittenWithError:
  42. return "FULLY_WRITTEN_WITH_ERROR"
  43. case WriteDisconnected:
  44. return "DISCONNECTED"
  45. default:
  46. return "OUTCOME_UNKNOWN"
  47. }
  48. }
  49. // WriteNeedsReconnect reports outcomes that may have left the UART parser
  50. // misaligned. The transport must be closed; the original action is never
  51. // auto-replayed.
  52. func WriteNeedsReconnect(wr WriteResult, err error) bool {
  53. if wr.Outcome == WriteNotStarted {
  54. return false
  55. }
  56. if err != nil && wr.Outcome == WriteFullyWritten {
  57. return true
  58. }
  59. switch wr.Outcome {
  60. case WritePartiallyWritten, WriteFullyWrittenWithError, WriteDisconnected, WriteOutcomeUnknown:
  61. return true
  62. default:
  63. return err != nil
  64. }
  65. }
  66. type WriteResult struct {
  67. Outcome WriteOutcome
  68. N int
  69. }
  70. type TimeWindow struct {
  71. RequestSent time.Time
  72. RequestComplete time.Time
  73. ResponseStart time.Time
  74. ResponseComplete time.Time
  75. HostReceive time.Time
  76. }
  77. type TempReport struct {
  78. Samples []protocol.TemperatureSample
  79. Window TimeWindow
  80. Raw []byte
  81. }
  82. type MotorReport struct {
  83. Status protocol.MotorStatus
  84. Window TimeWindow
  85. Raw []byte
  86. }
  87. type PollMode int
  88. const (
  89. PollUART PollMode = iota
  90. PollReplay
  91. )
  92. type Backend interface {
  93. Kind() string
  94. PollMode() PollMode
  95. Open(ctx context.Context) error
  96. Close() error
  97. WriteSetAll(ctx context.Context, s protocol.SetAll) (WriteResult, error)
  98. GetTemperature(ctx context.Context) (*TempReport, error)
  99. GetMotorStatus(ctx context.Context) (*MotorReport, error)
  100. }
  101. // Interrupter unblocks an in-flight Read without requiring the caller to hold
  102. // the transaction mutex. Serial Close/Interrupt must not wait for txnMu.
  103. type Interrupter interface {
  104. Interrupt()
  105. }
  106. type ReplayItem struct {
  107. Kind string
  108. EventID uint64
  109. WallTime time.Time
  110. Snapshot []byte
  111. Event []byte
  112. }
  113. type Replayer interface {
  114. Backend
  115. Next(ctx context.Context) (ReplayItem, error)
  116. }