Implement API to pack and unpack PLDM header
- The pack_pldm_header API packs the PLDM header into the
PLDM message.
- The unpack_pldm_header API unpacks the PLDM header from the
PLDM message.
Change-Id: I60b96954395cdcd1cecf306b7aee4c4de6c0f35a
Signed-off-by: Tom Joseph <tomjoseph@in.ibm.com>
diff --git a/libpldm/base.c b/libpldm/base.c
index db32ca4..c85aead 100644
--- a/libpldm/base.c
+++ b/libpldm/base.c
@@ -3,6 +3,65 @@
#include "base.h"
+int pack_pldm_header(const struct pldm_header_info *hdr,
+ struct pldm_msg_hdr *msg)
+{
+ if (msg == NULL || hdr == NULL) {
+ return PLDM_ERROR_INVALID_DATA;
+ }
+
+ if (hdr->msg_type != PLDM_RESPONSE && hdr->msg_type != PLDM_REQUEST &&
+ hdr->msg_type != PLDM_ASYNC_REQUEST_NOTIFY) {
+ return PLDM_ERROR_INVALID_DATA;
+ }
+
+ if (hdr->instance > PLDM_INSTANCE_MAX) {
+ return PLDM_ERROR_INVALID_DATA;
+ }
+
+ if (hdr->pldm_type > (PLDM_MAX_TYPES - 1)) {
+ return PLDM_ERROR_INVALID_PLDM_TYPE;
+ }
+
+ uint8_t datagram = (hdr->msg_type == PLDM_ASYNC_REQUEST_NOTIFY) ? 1 : 0;
+
+ if (hdr->msg_type == PLDM_RESPONSE) {
+ msg->request = PLDM_RESPONSE;
+ } else if (hdr->msg_type == PLDM_REQUEST ||
+ hdr->msg_type == PLDM_ASYNC_REQUEST_NOTIFY) {
+ msg->request = PLDM_REQUEST;
+ }
+ msg->datagram = datagram;
+ msg->reserved = 0;
+ msg->instance_id = hdr->instance;
+ msg->header_ver = 0;
+ msg->type = hdr->pldm_type;
+ msg->command = hdr->command;
+
+ return PLDM_SUCCESS;
+}
+
+int unpack_pldm_header(const struct pldm_msg_hdr *msg,
+ struct pldm_header_info *hdr)
+{
+ if (msg == NULL) {
+ return PLDM_ERROR_INVALID_DATA;
+ }
+
+ if (msg->request == PLDM_RESPONSE) {
+ hdr->msg_type = PLDM_RESPONSE;
+ } else {
+ hdr->msg_type =
+ msg->datagram ? PLDM_ASYNC_REQUEST_NOTIFY : PLDM_REQUEST;
+ }
+
+ hdr->instance = msg->instance_id;
+ hdr->pldm_type = msg->type;
+ hdr->command = msg->command;
+
+ return PLDM_SUCCESS;
+}
+
int encode_get_types_req(uint8_t instance_id, struct pldm_msg *msg)
{
if (msg == NULL) {
diff --git a/libpldm/base.h b/libpldm/base.h
index 29b2c87..65be648 100644
--- a/libpldm/base.h
+++ b/libpldm/base.h
@@ -34,6 +34,18 @@
PLDM_ERROR_INVALID_PLDM_TYPE = 0x20
};
+/** @enum MessageType
+ *
+ * The different message types supported by the PLDM specification.
+ */
+typedef enum {
+ PLDM_RESPONSE, //!< PLDM response
+ PLDM_REQUEST, //!< PLDM request
+ PLDM_RESERVED, //!< Reserved
+ PLDM_ASYNC_REQUEST_NOTIFY, //!< Unacknowledged PLDM request messages
+} MessageType;
+
+#define PLDM_INSTANCE_MAX 31
#define PLDM_MAX_TYPES 64
#define PLDM_MAX_CMDS_PER_TYPE 256
@@ -100,6 +112,43 @@
uint8_t alpha;
} __attribute__((packed));
+/** @struct pldm_header_info
+ *
+ * The information needed to prepare PLDM header and this is passed to the
+ * pack_pldm_header and unpack_pldm_header API.
+ */
+struct pldm_header_info {
+ MessageType msg_type; //!< PLDM message type
+ uint8_t instance; //!< PLDM instance id
+ uint8_t pldm_type; //!< PLDM type
+ uint8_t command; //!< PLDM command code
+ uint8_t completion_code; //!< PLDM completion code, applies for response
+};
+
+/**
+ * @brief Populate the PLDM message with the PLDM header.The caller of this API
+ * allocates buffer for the PLDM header when forming the PLDM message.
+ * The buffer is passed to this API to pack the PLDM header.
+ *
+ * @param[in] hdr - Pointer to the PLDM header information
+ * @param[out] msg - Pointer to PLDM message header
+ *
+ * @return 0 on success, otherwise PLDM error codes.
+ */
+int pack_pldm_header(const struct pldm_header_info *hdr,
+ struct pldm_msg_hdr *msg);
+
+/**
+ * @brief Unpack the PLDM header from the PLDM message.
+ *
+ * @param[in] msg - Pointer to the PLDM message header
+ * @param[out] hdr - Pointer to the PLDM header information
+ *
+ * @return 0 on success, otherwise PLDM error codes.
+ */
+int unpack_pldm_header(const struct pldm_msg_hdr *msg,
+ struct pldm_header_info *hdr);
+
/* Requester */
/* GetPLDMTypes */