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) {