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)23static 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 -EBADMSG: 30 case -EINVAL: 31 case -EPROTO: 32 case -EUCLEAN: 33 rc = PLDM_ERROR_INVALID_DATA; 34 break; 35 case -ENOMSG: 36 rc = PLDM_ERROR_INVALID_PLDM_TYPE; 37 break; 38 case -EOVERFLOW: 39 rc = PLDM_ERROR_INVALID_LENGTH; 40 break; 41 case -ENOTSUP: 42 rc = PLDM_ERROR; 43 break; 44 default: 45 assert(false); 46 rc = PLDM_ERROR; 47 break; 48 } 49 50 assert(rc > 0); 51 return rc; 52 } 53 54 #endif 55