Skip to main content

mcumgr_toolkit/
errno.rs

1use strum::{Display, FromRepr};
2
3/// See [`errno.h`](https://github.com/zephyrproject-rtos/zephyr/blob/main/lib/libc/minimal/include/errno.h).
4#[derive(FromRepr, Display, Debug, Copy, Clone, PartialEq, Eq)]
5#[repr(i32)]
6#[allow(non_camel_case_types)]
7pub enum Errno {
8    /** Not owner */
9    EPERM = 1,
10    /** No such file or directory */
11    ENOENT = 2,
12    /** No such context */
13    ESRCH = 3,
14    /** Interrupted system call */
15    EINTR = 4,
16    /** I/O error */
17    EIO = 5,
18    /** No such device or address */
19    ENXIO = 6,
20    /** Arg list too long */
21    E2BIG = 7,
22    /** Exec format error */
23    ENOEXEC = 8,
24    /** Bad file number */
25    EBADF = 9,
26    /** No children */
27    ECHILD = 10,
28    /** No more contexts */
29    EAGAIN = 11,
30    /** Not enough core */
31    ENOMEM = 12,
32    /** Permission denied */
33    EACCES = 13,
34    /** Bad address */
35    EFAULT = 14,
36    /** Block device required */
37    ENOTBLK = 15,
38    /** Mount device busy */
39    EBUSY = 16,
40    /** File exists */
41    EEXIST = 17,
42    /** Cross-device link */
43    EXDEV = 18,
44    /** No such device */
45    ENODEV = 19,
46    /** Not a directory */
47    ENOTDIR = 20,
48    /** Is a directory */
49    EISDIR = 21,
50    /** Invalid argument */
51    EINVAL = 22,
52    /** File table overflow */
53    ENFILE = 23,
54    /** Too many open files */
55    EMFILE = 24,
56    /** Not a typewriter */
57    ENOTTY = 25,
58    /** Text file busy */
59    ETXTBSY = 26,
60    /** File too large */
61    EFBIG = 27,
62    /** No space left on device */
63    ENOSPC = 28,
64    /** Illegal seek */
65    ESPIPE = 29,
66    /** Read-only file system */
67    EROFS = 30,
68    /** Too many links */
69    EMLINK = 31,
70    /** Broken pipe */
71    EPIPE = 32,
72    /** Argument too large */
73    EDOM = 33,
74    /** Result too large */
75    ERANGE = 34,
76    /** Unexpected message type */
77    ENOMSG = 35,
78    /** Resource deadlock avoided */
79    EDEADLK = 45,
80    /** No locks available */
81    ENOLCK = 46,
82    /** STREAMS device required */
83    ENOSTR = 60,
84    /** Missing expected message data */
85    ENODATA = 61,
86    /** STREAMS timeout occurred */
87    ETIME = 62,
88    /** Insufficient memory */
89    ENOSR = 63,
90    /** Generic STREAMS error */
91    EPROTO = 71,
92    /** Invalid STREAMS message */
93    EBADMSG = 77,
94    /** Function not implemented */
95    ENOSYS = 88,
96    /** Directory not empty */
97    ENOTEMPTY = 90,
98    /** File name too long */
99    ENAMETOOLONG = 91,
100    /** Too many levels of symbolic links */
101    ELOOP = 92,
102    /** Operation not supported on socket */
103    EOPNOTSUPP = 95,
104    /** Protocol family not supported */
105    EPFNOSUPPORT = 96,
106    /** Connection reset by peer */
107    ECONNRESET = 104,
108    /** No buffer space available */
109    ENOBUFS = 105,
110    /** Addr family not supported */
111    EAFNOSUPPORT = 106,
112    /** Protocol wrong type for socket */
113    EPROTOTYPE = 107,
114    /** Socket operation on non-socket */
115    ENOTSOCK = 108,
116    /** Protocol not available */
117    ENOPROTOOPT = 109,
118    /** Can't send after socket shutdown */
119    ESHUTDOWN = 110,
120    /** Connection refused */
121    ECONNREFUSED = 111,
122    /** Address already in use */
123    EADDRINUSE = 112,
124    /** Software caused connection abort */
125    ECONNABORTED = 113,
126    /** Network is unreachable */
127    ENETUNREACH = 114,
128    /** Network is down */
129    ENETDOWN = 115,
130    /** Connection timed out */
131    ETIMEDOUT = 116,
132    /** Host is down */
133    EHOSTDOWN = 117,
134    /** No route to host */
135    EHOSTUNREACH = 118,
136    /** Operation now in progress */
137    EINPROGRESS = 119,
138    /** Operation already in progress */
139    EALREADY = 120,
140    /** Destination address required */
141    EDESTADDRREQ = 121,
142    /** Message size */
143    EMSGSIZE = 122,
144    /** Protocol not supported */
145    EPROTONOSUPPORT = 123,
146    /** Socket type not supported */
147    ESOCKTNOSUPPORT = 124,
148    /** Can't assign requested address */
149    EADDRNOTAVAIL = 125,
150    /** Network dropped connection on reset */
151    ENETRESET = 126,
152    /** Socket is already connected */
153    EISCONN = 127,
154    /** Socket is not connected */
155    ENOTCONN = 128,
156    /** Too many references: can't splice */
157    ETOOMANYREFS = 129,
158    /** Unsupported value */
159    ENOTSUP = 134,
160    /** Illegal byte sequence */
161    EILSEQ = 138,
162    /** Value overflow */
163    EOVERFLOW = 139,
164    /** Operation canceled */
165    ECANCELED = 140,
166}
167
168impl Errno {
169    /// Converts a raw errno error code to a string
170    pub fn errno_to_string(err: i32) -> String {
171        if err >= 0 {
172            "EOK".to_string()
173        } else if let Some(err_enum) = Self::from_repr(-err) {
174            format!("{err_enum}")
175        } else {
176            format!("EUNKNOWN({err})")
177        }
178    }
179}
180
181#[cfg(test)]
182mod tests {
183    use super::*;
184
185    #[test]
186    fn test_errno_to_string_success() {
187        assert_eq!(Errno::errno_to_string(0), "EOK");
188        assert_eq!(Errno::errno_to_string(1), "EOK");
189    }
190
191    #[test]
192    fn test_errno_to_string_known_codes() {
193        assert_eq!(Errno::errno_to_string(-1), "EPERM");
194        assert_eq!(Errno::errno_to_string(-2), "ENOENT");
195        assert_eq!(Errno::errno_to_string(-22), "EINVAL");
196    }
197
198    #[test]
199    fn test_errno_to_string_unknown_code() {
200        assert_eq!(Errno::errno_to_string(-999), "EUNKNOWN(-999)");
201    }
202}