dsp: base: Rework {un,}pack_pldm_header() error handling

The current preference is that library APIs return negative errno values
to signal implementation errors. That doesn't jive with existing stable
APIs returning PLDM completion codes, so provide a means to translate
between the two.

The first users are the {un,}pack_pldm_header() functions.

Change-Id: I7b7cb97a1d8b96ec0fec1c0a5fbd8503da834d86
Signed-off-by: Andrew Jeffery <andrew@codeconstruct.com.au>
diff --git a/src/api.h b/src/api.h
new file mode 100644
index 0000000..3f1fed0
--- /dev/null
+++ b/src/api.h
@@ -0,0 +1,45 @@
+// SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
+#ifndef LIBPLDM_SRC_API_H
+#define LIBPLDM_SRC_API_H
+
+#include <libpldm/base.h>
+
+#include <assert.h>
+#include <errno.h>
+
+/**
+ * @brief Translate a negative errno value to a PLDM completion code
+ *
+ * Existing stable APIs often return errors in the form of PLDM completion
+ * codes, which confuses the problems of the protocol with the problems of
+ * the implementation. We're shifting to using negative errno values to signal
+ * implementation errors. However, for existing stable APIs, provide a means to
+ * translate between the two.
+ *
+ * @param[in] err - The negative errno to translate to a completion code
+ *
+ * @return An equivalent PLDM completion code for @p err
+ */
+static inline enum pldm_completion_codes pldm_xlate_errno(int err)
+{
+	enum pldm_completion_codes rc;
+
+	assert(err < 0);
+	switch (err) {
+	case -EINVAL:
+		rc = PLDM_ERROR_INVALID_DATA;
+		break;
+	case -ENOMSG:
+		rc = PLDM_ERROR_INVALID_PLDM_TYPE;
+		break;
+	default:
+		assert(false);
+		rc = PLDM_ERROR;
+		break;
+	}
+
+	assert(rc > 0);
+	return rc;
+}
+
+#endif