Skip to content

Commit 26524aa

Browse files
committed
Add new nsi_errno optional component
Which allows converting errno values between two libCs Signed-off-by: Alberto Escolar Piedras <[email protected]>
1 parent 007a1c8 commit 26524aa

File tree

2 files changed

+239
-0
lines changed

2 files changed

+239
-0
lines changed

common/src/include/nsi_errno.h

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
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+
int nsi_errno_to_mid(int err);
107+
int nsi_errno_from_mid(int err);
108+
109+
#endif /* NSI_COMMON_SRC_NSI_ERRNO_H */

common/src/nsi_errno.c

Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
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+
#include "nsi_errno.h"
9+
#include "nsi_utils.h"
10+
11+
struct nsi_errno_mid_map {
12+
/** Embedded/host error code */
13+
int err;
14+
/** NSI_errno middleground error code */
15+
int mid_err;
16+
};
17+
18+
#define ERR(_name) {_name, NSI_ERRNO_MID_##_name}
19+
20+
static const struct nsi_errno_mid_map map[] = {
21+
ERR(EPERM),
22+
ERR(ENOENT),
23+
ERR(ESRCH),
24+
ERR(EINTR),
25+
ERR(EIO),
26+
ERR(ENXIO),
27+
ERR(E2BIG),
28+
ERR(ENOEXEC),
29+
ERR(EBADF),
30+
ERR(ECHILD),
31+
ERR(EAGAIN),
32+
ERR(ENOMEM),
33+
ERR(EACCES),
34+
ERR(EFAULT),
35+
ERR(ENOTBLK),
36+
ERR(EBUSY),
37+
ERR(EEXIST),
38+
ERR(EXDEV),
39+
ERR(ENODEV),
40+
ERR(ENOTDIR),
41+
ERR(EISDIR),
42+
ERR(EINVAL),
43+
ERR(ENFILE),
44+
ERR(EMFILE),
45+
ERR(ENOTTY),
46+
ERR(ETXTBSY),
47+
ERR(EFBIG),
48+
ERR(ENOSPC),
49+
ERR(ESPIPE),
50+
ERR(EROFS),
51+
ERR(EMLINK),
52+
ERR(EPIPE),
53+
ERR(EDOM),
54+
ERR(ERANGE),
55+
ERR(ENOMSG),
56+
ERR(EDEADLK),
57+
ERR(ENOLCK),
58+
ERR(ENOSTR),
59+
ERR(ENODATA),
60+
ERR(ETIME),
61+
ERR(ENOSR),
62+
ERR(EPROTO),
63+
ERR(EBADMSG),
64+
ERR(ENOSYS),
65+
ERR(ENOTEMPTY),
66+
ERR(ENAMETOOLONG),
67+
ERR(ELOOP),
68+
ERR(EOPNOTSUPP),
69+
ERR(EPFNOSUPPORT),
70+
ERR(ECONNRESET),
71+
ERR(ENOBUFS),
72+
ERR(EAFNOSUPPORT),
73+
ERR(EPROTOTYPE),
74+
ERR(ENOTSOCK),
75+
ERR(ENOPROTOOPT),
76+
ERR(ESHUTDOWN),
77+
ERR(ECONNREFUSED),
78+
ERR(EADDRINUSE),
79+
ERR(ECONNABORTED),
80+
ERR(ENETUNREACH),
81+
ERR(ENETDOWN),
82+
ERR(ETIMEDOUT),
83+
ERR(EHOSTDOWN),
84+
ERR(EHOSTUNREACH),
85+
ERR(EINPROGRESS),
86+
ERR(EALREADY),
87+
ERR(EDESTADDRREQ),
88+
ERR(EMSGSIZE),
89+
ERR(EPROTONOSUPPORT),
90+
ERR(ESOCKTNOSUPPORT),
91+
ERR(EADDRNOTAVAIL),
92+
ERR(ENETRESET),
93+
ERR(EISCONN),
94+
ERR(ENOTCONN),
95+
ERR(ETOOMANYREFS),
96+
ERR(ENOTSUP),
97+
ERR(EILSEQ),
98+
ERR(EOVERFLOW),
99+
ERR(ECANCELED),
100+
};
101+
102+
int nsi_errno_to_mid(int err)
103+
{
104+
if (err == 0) {
105+
return err;
106+
}
107+
108+
for (int i = 0; i < NSI_ARRAY_SIZE(map); i++) {
109+
if (map[i].err == err) {
110+
return map[i].mid_err;
111+
}
112+
}
113+
114+
return err;
115+
}
116+
117+
int nsi_errno_from_mid(int err)
118+
{
119+
if (err == 0) {
120+
return err;
121+
}
122+
123+
for (int i = 0; i < NSI_ARRAY_SIZE(map); i++) {
124+
if (map[i].mid_err == err) {
125+
return map[i].err;
126+
}
127+
}
128+
129+
return err;
130+
}

0 commit comments

Comments
 (0)