| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134 |
- package backend
- import (
- "context"
- "errors"
- "time"
- "hth/internal/protocol"
- )
- var (
- ErrReadOnly = errors.New("backend is read-only")
- ErrNotConnected = errors.New("backend not connected")
- ErrReplaySerial = errors.New("replay backend refuses to open a real serial device")
- ErrStreamUntrusted = errors.New("uart stream untrusted; reconnect required")
- ErrWriteNotStarted = errors.New("serial write not started")
- // ErrResponseTimeout is a GET timeout after leftover drain; the stream is
- // still aligned so the actor must not onDrop / bump epoch.
- ErrResponseTimeout = errors.New("serial response timeout; stream still aligned")
- )
- type WriteOutcome int
- const (
- WriteNotStarted WriteOutcome = iota
- WriteFullyWritten
- WritePartiallyWritten
- WriteFullyWrittenWithError
- WriteOutcomeUnknown
- WriteDisconnected
- )
- // Historical aliases used by existing tests.
- const (
- WriteFull = WriteFullyWritten
- WriteShort = WritePartiallyWritten
- WriteAmbiguous = WriteFullyWrittenWithError
- )
- func (o WriteOutcome) String() string {
- switch o {
- case WriteNotStarted:
- return "NOT_STARTED"
- case WriteFullyWritten:
- return "FULLY_WRITTEN"
- case WritePartiallyWritten:
- return "PARTIALLY_WRITTEN"
- case WriteFullyWrittenWithError:
- return "FULLY_WRITTEN_WITH_ERROR"
- case WriteDisconnected:
- return "DISCONNECTED"
- default:
- return "OUTCOME_UNKNOWN"
- }
- }
- // WriteNeedsReconnect reports outcomes that may have left the UART parser
- // misaligned. The transport must be closed; the original action is never
- // auto-replayed.
- func WriteNeedsReconnect(wr WriteResult, err error) bool {
- if wr.Outcome == WriteNotStarted {
- return false
- }
- if err != nil && wr.Outcome == WriteFullyWritten {
- return true
- }
- switch wr.Outcome {
- case WritePartiallyWritten, WriteFullyWrittenWithError, WriteDisconnected, WriteOutcomeUnknown:
- return true
- default:
- return err != nil
- }
- }
- type WriteResult struct {
- Outcome WriteOutcome
- N int
- }
- type TimeWindow struct {
- RequestSent time.Time
- RequestComplete time.Time
- ResponseStart time.Time
- ResponseComplete time.Time
- HostReceive time.Time
- }
- type TempReport struct {
- Samples []protocol.TemperatureSample
- Window TimeWindow
- Raw []byte
- }
- type MotorReport struct {
- Status protocol.MotorStatus
- Window TimeWindow
- Raw []byte
- }
- type PollMode int
- const (
- PollUART PollMode = iota
- PollReplay
- )
- type Backend interface {
- Kind() string
- PollMode() PollMode
- Open(ctx context.Context) error
- Close() error
- WriteSetAll(ctx context.Context, s protocol.SetAll) (WriteResult, error)
- GetTemperature(ctx context.Context) (*TempReport, error)
- GetMotorStatus(ctx context.Context) (*MotorReport, error)
- }
- // Interrupter unblocks an in-flight Read without requiring the caller to hold
- // the transaction mutex. Serial Close/Interrupt must not wait for txnMu.
- type Interrupter interface {
- Interrupt()
- }
- type ReplayItem struct {
- Kind string
- EventID uint64
- WallTime time.Time
- Snapshot []byte
- Event []byte
- }
- type Replayer interface {
- Backend
- Next(ctx context.Context) (ReplayItem, error)
- }
|