blob: 8faee0560df08f43c36cb25a34a1030481daad48 [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) {
Andrew Jeffery248b5ab2025-02-21 12:35:14 +103029 case -EBADMSG:
Andrew Jeffery150b06c2025-03-12 14:17:42 +103030 case -EINVAL:
31 case -EPROTO:
32 case -EUCLEAN:
Andrew Jefferyd861a682024-06-03 21:43:09 +093033 rc = PLDM_ERROR_INVALID_DATA;
34 break;
35 case -ENOMSG:
36 rc = PLDM_ERROR_INVALID_PLDM_TYPE;
37 break;
Lora Lin0f5be282024-07-22 09:46:18 +080038 case -EOVERFLOW:
39 rc = PLDM_ERROR_INVALID_LENGTH;
40 break;
Andrew Jeffery150b06c2025-03-12 14:17:42 +103041 case -ENOTSUP:
42 rc = PLDM_ERROR;
43 break;
Andrew Jefferyd861a682024-06-03 21:43:09 +093044 default:
45 assert(false);
46 rc = PLDM_ERROR;
47 break;
48 }
49
50 assert(rc > 0);
51 return rc;
52}
53
54#endif