xref: /openbmc/libpldm/src/api.h (revision d861a681)
1 // SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
2 #ifndef LIBPLDM_SRC_API_H
3 #define LIBPLDM_SRC_API_H
4 
5 #include <libpldm/base.h>
6 
7 #include <assert.h>
8 #include <errno.h>
9 
10 /**
11  * @brief Translate a negative errno value to a PLDM completion code
12  *
13  * Existing stable APIs often return errors in the form of PLDM completion
14  * codes, which confuses the problems of the protocol with the problems of
15  * the implementation. We're shifting to using negative errno values to signal
16  * implementation errors. However, for existing stable APIs, provide a means to
17  * translate between the two.
18  *
19  * @param[in] err - The negative errno to translate to a completion code
20  *
21  * @return An equivalent PLDM completion code for @p err
22  */
pldm_xlate_errno(int err)23 static inline enum pldm_completion_codes pldm_xlate_errno(int err)
24 {
25 	enum pldm_completion_codes rc;
26 
27 	assert(err < 0);
28 	switch (err) {
29 	case -EINVAL:
30 		rc = PLDM_ERROR_INVALID_DATA;
31 		break;
32 	case -ENOMSG:
33 		rc = PLDM_ERROR_INVALID_PLDM_TYPE;
34 		break;
35 	default:
36 		assert(false);
37 		rc = PLDM_ERROR;
38 		break;
39 	}
40 
41 	assert(rc > 0);
42 	return rc;
43 }
44 
45 #endif
46