Andrew Jeffery | d861a68 | 2024-06-03 21:43:09 +0930 | [diff] [blame] | 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 | */ |
| 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; |
Lora Lin | 0f5be28 | 2024-07-22 09:46:18 +0800 | [diff] [blame] | 35 | case -EBADMSG: |
| 36 | case -EOVERFLOW: |
| 37 | rc = PLDM_ERROR_INVALID_LENGTH; |
| 38 | break; |
Andrew Jeffery | d861a68 | 2024-06-03 21:43:09 +0930 | [diff] [blame] | 39 | default: |
| 40 | assert(false); |
| 41 | rc = PLDM_ERROR; |
| 42 | break; |
| 43 | } |
| 44 | |
| 45 | assert(rc > 0); |
| 46 | return rc; |
| 47 | } |
| 48 | |
| 49 | #endif |