linuxcnc_hal/
error.rs

1//! Error types
2
3use linuxcnc_hal_sys::HAL_NAME_LEN;
4
5/// Errors returned by LinuxCNC bindgen functions
6
7/// Pointer error
8#[derive(thiserror::Error, Debug, PartialEq)]
9pub enum StorageError {
10    /// Pointer is null
11    #[error("pointer is null")]
12    Null,
13
14    /// Pointer is not aligned
15    #[error("pointer is not aligned")]
16    Alignment,
17}
18
19/// Pin registration error
20#[derive(thiserror::Error, Debug, PartialEq)]
21pub enum PinRegisterError {
22    /// Pin name is too long
23    ///
24    /// The maximum length is dictated by the [`HAL_NAME_LEN`] constant
25    #[error("pin name is too long. Must be no longer than {} bytes", HAL_NAME_LEN)]
26    NameLength,
27
28    /// Pin name could not be converted to C string
29    #[error("pin name could not be converted to a valid C string")]
30    NameConversion,
31
32    /// An error occurred allocating the HAL shared memory storage backing the pin
33    #[error("failed to allocate shared memory storage for pin")]
34    Storage(StorageError),
35
36    /// An error occurred in the LinuxCNC HAL functions
37    ///
38    /// This variant is often returned when a HAL function returns
39    /// [`EINVAL`](linuxcnc_hal_sys::EINVAL). This error code is returned for various different
40    /// reasons. Check the LinuxCNC logs for error messages.
41    #[error("HAL method returned invalid (EINVAL) status code")]
42    Invalid,
43
44    /// The HAL is locked
45    ///
46    /// Resources cannot be registered after a component is created
47    #[error("HAL is locked")]
48    LockedHal,
49
50    /// There is not enough free memory available to allocate storage for this pin
51    #[error("not enough free memory to allocate storage")]
52    Memory,
53}
54
55/// Parameter registration error
56#[derive(thiserror::Error, Debug, PartialEq)]
57pub enum ParameterRegisterError {
58    /// Parameter name is too long
59    ///
60    /// The maximum length is dictated by the [`HAL_NAME_LEN`] constant
61    #[error(
62        "Parameter name is too long. Must be no longer than {} bytes",
63        HAL_NAME_LEN
64    )]
65    NameLength,
66
67    /// Parameter name could not be converted to C string
68    #[error("Parameter name could not be converted to a valid C string")]
69    NameConversion,
70
71    /// An error occurred allocating the HAL shared memory storage backing the parameter
72    #[error("failed to allocate shared memory storage for parameter")]
73    Storage(StorageError),
74
75    /// An error occurred in the LinuxCNC HAL functions
76    ///
77    /// This variant is often returned when a HAL function returns
78    /// [`EINVAL`](linuxcnc_hal_sys::EINVAL). This error code is returned for various different
79    /// reasons. Check the LinuxCNC logs for error messages.
80    #[error("HAL method returned invalid (EINVAL) status code")]
81    Invalid,
82
83    /// The HAL is locked
84    ///
85    /// Resources cannot be registered after a component is created
86    #[error("HAL is locked")]
87    LockedHal,
88
89    /// There is not enough free memory available to allocate storage for this parameter
90    #[error("not enough free memory to allocate storage")]
91    Memory,
92}
93
94/// HAL component initialisation error
95#[derive(thiserror::Error, Debug)]
96pub enum ComponentInitError {
97    /// Component name is too long
98    ///
99    /// The maximum length is dictated by the [`HAL_NAME_LEN`] constant
100    #[error(
101        "component name is too long. Must be no longer than {} bytes",
102        HAL_NAME_LEN
103    )]
104    NameLength,
105
106    /// Component name could not be converted to C type
107    #[error("component name cannot be converted to valid C string")]
108    InvalidName,
109
110    /// There is not enough free memory available to allocate storage for this pin
111    #[error("not enough free memory to allocate storage")]
112    Memory,
113
114    /// Failed to register signal handlers
115    #[error("failed to register signal handlers")]
116    Signals(std::io::Error),
117
118    /// Resource (pin, signal, etc) registration failed
119    #[error("failed to register resources with component")]
120    ResourceRegistration(ResourcesError),
121
122    /// An error occurred when initialising the component with
123    /// [`hal_init`](linuxcnc_hal_sys::hal_init)
124    #[error("failed to initialise component")]
125    Init,
126
127    /// An error occurred when calling [`hal_ready`](linuxcnc_hal_sys::hal_ready) on the component
128    #[error("failed to ready component")]
129    Ready,
130}
131
132/// Resources registration error
133#[derive(thiserror::Error, Debug)]
134pub enum ResourcesError {
135    /// Failed to register a pin with the HAL
136    #[error("pin registration failed")]
137    Pin(PinRegisterError),
138
139    /// Failed to register a pin with the HAL
140    #[error("parameter registration failed")]
141    Parameter(ParameterRegisterError),
142}
143
144impl From<PinRegisterError> for ResourcesError {
145    fn from(e: PinRegisterError) -> Self {
146        Self::Pin(e)
147    }
148}
149
150impl From<ParameterRegisterError> for ResourcesError {
151    fn from(e: ParameterRegisterError) -> Self {
152        Self::Parameter(e)
153    }
154}