Skip to main content

uart_fpl/
error.rs

1// Copyright 2026 The Fuchsia Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5//! Error types for the UART framing and protocol library.
6
7use thiserror::Error;
8
9/// Errors that can occur during Channel 0 protocol negotiation.
10#[derive(Debug, Error, PartialEq, Eq, Clone, Copy)]
11pub enum HandshakeError {
12    /// Handshake payload was malformed, truncated, or contained invalid fields.
13    #[error("Malformed or truncated handshake payload")]
14    MalformedPayload,
15
16    /// Selected protocol was not among the proposed protocols.
17    #[error("Selected protocol was not proposed by host")]
18    ProtocolNotProposed,
19
20    /// Target rejected handshake: no mutually supported protocol was found.
21    #[error("Target rejected handshake: no common protocol")]
22    NoCommonProtocol,
23
24    /// Unrecognized handshake status reported by peer.
25    #[error("Target reported unsupported handshake status: {0}")]
26    UnsupportedStatus(u8),
27}
28
29/// Errors that can occur during packet encoding or frame validation.
30#[derive(Debug, Error, PartialEq, Eq, Clone, Copy)]
31pub enum FrameError {
32    /// Payload exceeds maximum permissible size.
33    #[error("Payload length {0} exceeds MAX_PAYLOAD_SIZE")]
34    PayloadTooLarge(usize),
35
36    /// The sliding window is full; cannot enqueue further frames.
37    #[error("Sliding window is full (window size {window_size}, in-flight {in_flight})")]
38    WindowFull {
39        /// Configured window size.
40        window_size: u8,
41        /// Frames currently in flight.
42        in_flight: u8,
43    },
44}
45
46/// Error emitted when consecutive Go-Back-N retransmission timeouts exceed the configured limit.
47#[derive(Debug, Error, PartialEq, Eq, Clone, Copy)]
48#[error("Too many retransmission failures, base={base}, next_seq={next_seq}, attempts={attempts}")]
49pub struct RetransmissionLimitExceeded {
50    /// Base sequence number of oldest unacknowledged frame.
51    pub base: u8,
52    /// Next sequence number to be assigned.
53    pub next_seq: u8,
54    /// Number of consecutive retransmission attempts made.
55    pub attempts: usize,
56}
57
58/// Error emitted when an incoming sequence number does not match the expected sequence number.
59#[derive(Debug, Error, PartialEq, Eq, Clone, Copy)]
60#[error("Unexpected sequence number: expected {expected}, received {received}")]
61pub struct UnexpectedSeqError {
62    /// The sequence number expected by the receiver.
63    pub expected: u8,
64    /// The sequence number that was actually received.
65    pub received: u8,
66}