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") ) 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) }