1 /**
2  * Copyright (c) 2023-2024 Marcin Niestroj
3  * Copyright (c) 2025 Nordic Semiconductor ASA
4  *
5  * SPDX-License-Identifier: Apache-2.0
6  */
7 
8 #ifndef NSI_COMMON_SRC_NSI_ERRNO_H
9 #define NSI_COMMON_SRC_NSI_ERRNO_H
10 
11 /*
12  * Optional utility module to convert errno values from one libC to another,
13  * where one libC would normally be the embedded libC used to build the embedded code and the other
14  * the host libC.
15  *
16  * It works by converting the errno values from/to either libC into an intermediate set of values
17  * (NSI_ERRNO_MID_E*).
18  * When two components would like to exchange errors, they should include nsi_errno.h and build and
19  * link to nsi_errno.c in their context. And convert to/from these intermediate error values
20  * before passing them to/after receiving them from the other side by using
21  * nsi_errno_to_mid() / nsi_errno_from_mid().
22  */
23 
24 #include <errno.h>
25 
26 #define NSI_ERRNO_MID_EPERM             1 /**< Not owner */
27 #define NSI_ERRNO_MID_ENOENT            2 /**< No such file or directory */
28 #define NSI_ERRNO_MID_ESRCH             3 /**< No such context */
29 #define NSI_ERRNO_MID_EINTR             4 /**< Interrupted system call */
30 #define NSI_ERRNO_MID_EIO               5 /**< I/O error */
31 #define NSI_ERRNO_MID_ENXIO             6 /**< No such device or address */
32 #define NSI_ERRNO_MID_E2BIG             7 /**< Arg list too long */
33 #define NSI_ERRNO_MID_ENOEXEC           8 /**< Exec format error */
34 #define NSI_ERRNO_MID_EBADF             9 /**< Bad file number */
35 #define NSI_ERRNO_MID_ECHILD           10 /**< No children */
36 #define NSI_ERRNO_MID_EAGAIN           11 /**< No more contexts */
37 #define NSI_ERRNO_MID_ENOMEM           12 /**< Not enough core */
38 #define NSI_ERRNO_MID_EACCES           13 /**< Permission denied */
39 #define NSI_ERRNO_MID_EFAULT           14 /**< Bad address */
40 #define NSI_ERRNO_MID_ENOTBLK          15 /**< Block device required */
41 #define NSI_ERRNO_MID_EBUSY            16 /**< Mount device busy */
42 #define NSI_ERRNO_MID_EEXIST           17 /**< File exists */
43 #define NSI_ERRNO_MID_EXDEV            18 /**< Cross-device link */
44 #define NSI_ERRNO_MID_ENODEV           19 /**< No such device */
45 #define NSI_ERRNO_MID_ENOTDIR          20 /**< Not a directory */
46 #define NSI_ERRNO_MID_EISDIR           21 /**< Is a directory */
47 #define NSI_ERRNO_MID_EINVAL           22 /**< Invalid argument */
48 #define NSI_ERRNO_MID_ENFILE           23 /**< File table overflow */
49 #define NSI_ERRNO_MID_EMFILE           24 /**< Too many open files */
50 #define NSI_ERRNO_MID_ENOTTY           25 /**< Not a typewriter */
51 #define NSI_ERRNO_MID_ETXTBSY          26 /**< Text file busy */
52 #define NSI_ERRNO_MID_EFBIG            27 /**< File too large */
53 #define NSI_ERRNO_MID_ENOSPC           28 /**< No space left on device */
54 #define NSI_ERRNO_MID_ESPIPE           29 /**< Illegal seek */
55 #define NSI_ERRNO_MID_EROFS            30 /**< Read-only file system */
56 #define NSI_ERRNO_MID_EMLINK           31 /**< Too many links */
57 #define NSI_ERRNO_MID_EPIPE            32 /**< Broken pipe */
58 #define NSI_ERRNO_MID_EDOM             33 /**< Argument too large */
59 #define NSI_ERRNO_MID_ERANGE           34 /**< Result too large */
60 #define NSI_ERRNO_MID_ENOMSG           35 /**< Unexpected message type */
61 #define NSI_ERRNO_MID_EDEADLK          45 /**< Resource deadlock avoided */
62 #define NSI_ERRNO_MID_ENOLCK           46 /**< No locks available */
63 #define NSI_ERRNO_MID_ENOSTR           60 /**< STREAMS device required */
64 #define NSI_ERRNO_MID_ENODATA          61 /**< Missing expected message data */
65 #define NSI_ERRNO_MID_ETIME            62 /**< STREAMS timeout occurred */
66 #define NSI_ERRNO_MID_ENOSR            63 /**< Insufficient memory */
67 #define NSI_ERRNO_MID_EPROTO           71 /**< Generic STREAMS error */
68 #define NSI_ERRNO_MID_EBADMSG          77 /**< Invalid STREAMS message */
69 #define NSI_ERRNO_MID_ENOSYS           88 /**< Function not implemented */
70 #define NSI_ERRNO_MID_ENOTEMPTY        90 /**< Directory not empty */
71 #define NSI_ERRNO_MID_ENAMETOOLONG     91 /**< File name too long */
72 #define NSI_ERRNO_MID_ELOOP            92 /**< Too many levels of symbolic links */
73 #define NSI_ERRNO_MID_EOPNOTSUPP       95 /**< Operation not supported on socket */
74 #define NSI_ERRNO_MID_EPFNOSUPPORT     96 /**< Protocol family not supported */
75 #define NSI_ERRNO_MID_ECONNRESET      104 /**< Connection reset by peer */
76 #define NSI_ERRNO_MID_ENOBUFS         105 /**< No buffer space available */
77 #define NSI_ERRNO_MID_EAFNOSUPPORT    106 /**< Addr family not supported */
78 #define NSI_ERRNO_MID_EPROTOTYPE      107 /**< Protocol wrong type for socket */
79 #define NSI_ERRNO_MID_ENOTSOCK        108 /**< Socket operation on non-socket */
80 #define NSI_ERRNO_MID_ENOPROTOOPT     109 /**< Protocol not available */
81 #define NSI_ERRNO_MID_ESHUTDOWN       110 /**< Can't send after socket shutdown */
82 #define NSI_ERRNO_MID_ECONNREFUSED    111 /**< Connection refused */
83 #define NSI_ERRNO_MID_EADDRINUSE      112 /**< Address already in use */
84 #define NSI_ERRNO_MID_ECONNABORTED    113 /**< Software caused connection abort */
85 #define NSI_ERRNO_MID_ENETUNREACH     114 /**< Network is unreachable */
86 #define NSI_ERRNO_MID_ENETDOWN        115 /**< Network is down */
87 #define NSI_ERRNO_MID_ETIMEDOUT       116 /**< Connection timed out */
88 #define NSI_ERRNO_MID_EHOSTDOWN       117 /**< Host is down */
89 #define NSI_ERRNO_MID_EHOSTUNREACH    118 /**< No route to host */
90 #define NSI_ERRNO_MID_EINPROGRESS     119 /**< Operation now in progress */
91 #define NSI_ERRNO_MID_EALREADY        120 /**< Operation already in progress */
92 #define NSI_ERRNO_MID_EDESTADDRREQ    121 /**< Destination address required */
93 #define NSI_ERRNO_MID_EMSGSIZE        122 /**< Message size */
94 #define NSI_ERRNO_MID_EPROTONOSUPPORT 123 /**< Protocol not supported */
95 #define NSI_ERRNO_MID_ESOCKTNOSUPPORT 124 /**< Socket type not supported */
96 #define NSI_ERRNO_MID_EADDRNOTAVAIL   125 /**< Can't assign requested address */
97 #define NSI_ERRNO_MID_ENETRESET       126 /**< Network dropped connection on reset */
98 #define NSI_ERRNO_MID_EISCONN         127 /**< Socket is already connected */
99 #define NSI_ERRNO_MID_ENOTCONN        128 /**< Socket is not connected */
100 #define NSI_ERRNO_MID_ETOOMANYREFS    129 /**< Too many references: can't splice */
101 #define NSI_ERRNO_MID_ENOTSUP         134 /**< Unsupported value */
102 #define NSI_ERRNO_MID_EILSEQ          138 /**< Illegal byte sequence */
103 #define NSI_ERRNO_MID_EOVERFLOW       139 /**< Value overflow */
104 #define NSI_ERRNO_MID_ECANCELED       140 /**< Operation canceled */
105 
106 /* Convert a errno value to the intermediate represetation to pass it to the other side */
107 int nsi_errno_to_mid(int err);
108 /* Convert a errno value from the intermediate representation into the local libC value */
109 int nsi_errno_from_mid(int err);
110 /* Return the middleground representation of the current host libC errno */
111 int nsi_get_errno_in_mid(void);
112 /* Return the local representation of the current host libC errno */
113 int nsi_host_get_errno(void);
114 
115 #endif /* NSI_COMMON_SRC_NSI_ERRNO_H */
116