blob: 643fbff3db776023390de2dc748950cbbf04f800 [file] [log] [blame]
Andrew Jefferyd861a682024-06-03 21:43:09 +09301// 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 */
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 -EINVAL:
Andrew Jeffery248b5ab2025-02-21 12:35:14 +103030 case -EBADMSG:
Andrew Jefferyd861a682024-06-03 21:43:09 +093031 rc = PLDM_ERROR_INVALID_DATA;
32 break;
33 case -ENOMSG:
34 rc = PLDM_ERROR_INVALID_PLDM_TYPE;
35 break;
Lora Lin0f5be282024-07-22 09:46:18 +080036 case -EOVERFLOW:
37 rc = PLDM_ERROR_INVALID_LENGTH;
38 break;
Andrew Jefferyd861a682024-06-03 21:43:09 +093039 default:
40 assert(false);
41 rc = PLDM_ERROR;
42 break;
43 }
44
45 assert(rc > 0);
46 return rc;
47}
48
49#endif