Skip to main content

ssh_stamp_hal/
error.rs

1// SPDX-FileCopyrightText: 2026 Roman Valls Guimera <brainstorm@nopcode.org>
2// SPDX-FileCopyrightText: 2026 Julio Beltran Ortega <jubeormk1@gmail.com>
3//
4// SPDX-License-Identifier: GPL-3.0-or-later
5
6//! HAL error types.
7//!
8//! This module defines error enums for all HAL operations. Each error type
9//! corresponds to a specific peripheral or operation category.
10
11use core::fmt;
12
13/// Unified HAL error type.
14///
15/// Aggregates all peripheral-specific errors into a single enum for
16/// ergonomic error handling across the HAL.
17#[derive(Debug)]
18pub enum HalError {
19    /// Configuration error (invalid settings).
20    Config,
21    /// UART peripheral error.
22    Uart(UartError),
23    /// `WiFi` peripheral error.
24    Wifi(WifiError),
25    /// Flash storage error.
26    Flash(FlashError),
27    /// Random number generator error.
28    Rng,
29    /// Hash/HMAC computation error.
30    Hash(HashError),
31    /// Timer error.
32    Timer,
33}
34
35/// UART-specific errors.
36#[derive(Debug)]
37pub enum UartError {
38    /// Invalid UART configuration.
39    Config,
40    /// Receive buffer overflow (data lost).
41    BufferOverflow,
42    /// Read operation failed.
43    Read,
44    /// Write operation failed.
45    Write,
46}
47
48/// WiFi-specific errors.
49#[derive(Debug)]
50pub enum WifiError {
51    /// `WiFi` hardware initialization failed.
52    Initialization,
53    /// Failed to create socket.
54    SocketCreate,
55    /// Failed to accept connection.
56    SocketAccept,
57    /// Socket read failed.
58    SocketRead,
59    /// Socket write failed.
60    SocketWrite,
61    /// Socket close failed.
62    SocketClose,
63    /// DHCP client error.
64    Dhcpc,
65    /// Station Mode error.
66    StationMode,
67}
68
69/// Flash storage errors.
70#[derive(Debug)]
71pub enum FlashError {
72    /// Read operation failed.
73    Read,
74    /// Write operation failed.
75    Write,
76    /// Erase operation failed.
77    Erase,
78    /// Requested partition not found.
79    PartitionNotFound,
80    /// OTA partition validation failed.
81    ValidationFailed,
82    /// Failed to load configuration from flash.
83    ConfigLoad,
84    /// Failed to save configuration to flash.
85    ConfigSave,
86    /// Internal flash controller error.
87    InternalError,
88}
89
90/// Hash/HMAC computation errors.
91#[derive(Debug)]
92pub enum HashError {
93    /// Invalid hash/HMAC configuration.
94    Config,
95    /// Hash computation failed.
96    Compute,
97}
98
99impl fmt::Display for HalError {
100    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
101        match self {
102            HalError::Config => write!(f, "configuration error"),
103            HalError::Uart(e) => write!(f, "UART error: {e:?}"),
104            HalError::Wifi(e) => write!(f, "WiFi error: {e:?}"),
105            HalError::Flash(e) => write!(f, "Flash error: {e:?}"),
106            HalError::Rng => write!(f, "RNG error"),
107            HalError::Hash(e) => write!(f, "Hash error: {e:?}"),
108            HalError::Timer => write!(f, "Timer error"),
109        }
110    }
111}