oem/libpldm: Implement requester flow for GetAlertStatus

For the requester, need to encode reqeuest data and send responder,
waiting for recive responder data to decode, so need to implement
encode/decode(Requester flow only) for GetAlertStatus.

The GetAlertStatus is a custom oem command to report enclosure ids of
system, alert status and flags, and config ID.

Request Data:
 Type - Request Data
  uint8 - Version of the command/response format. 0x0 for this format.

Response Data:
 Type - Response Data
  enum8 - completionCode.value{PLDM_BASE_CODES, UNSUPPORTED_FORMAT_VERSION=0x81}
  uint32 - Enclosure ID, AlertStatus, Flags, Config ID.(rack entry)
  uint32 - Enclosure ID, AlertStatus, Flags, Config ID.(pri cec node)

Signed-off-by: George Liu <liuxiwei@inspur.com>
Change-Id: I0a3d1d8f79fe80194a0b2208bce090783ad74f0b
diff --git a/libpldm/meson.build b/libpldm/meson.build
index b96503f..7c072a2 100644
--- a/libpldm/meson.build
+++ b/libpldm/meson.build
@@ -24,10 +24,12 @@
 
 if get_option('oem-ibm').enabled()
   headers += [
-    '../oem/ibm/libpldm/file_io.h'
+    '../oem/ibm/libpldm/file_io.h',
+    '../oem/ibm/libpldm/host.h'
   ]
   sources += [
-    '../oem/ibm/libpldm/file_io.c'
+    '../oem/ibm/libpldm/file_io.c',
+    '../oem/ibm/libpldm/host.c'
   ]
   libpldm_headers += ['../oem/ibm']
 endif
diff --git a/libpldm/tests/meson.build b/libpldm/tests/meson.build
index 08d2d19..5502820 100644
--- a/libpldm/tests/meson.build
+++ b/libpldm/tests/meson.build
@@ -28,6 +28,7 @@
 if get_option('oem-ibm').enabled()
   tests += [
     '../../oem/ibm/test/libpldm_fileio_test',
+    '../../oem/ibm/test/libpldm_host_test',
   ]
 endif
 
diff --git a/oem/ibm/libpldm/host.c b/oem/ibm/libpldm/host.c
new file mode 100644
index 0000000..6628bd5
--- /dev/null
+++ b/oem/ibm/libpldm/host.c
@@ -0,0 +1,56 @@
+#include <endian.h>
+#include <string.h>
+
+#include "host.h"
+
+int encode_get_alert_status_req(uint8_t instance_id, uint8_t version_id,
+				struct pldm_msg *msg, size_t payload_length)
+{
+	struct pldm_header_info header = {0};
+	int rc = PLDM_SUCCESS;
+
+	header.msg_type = PLDM_REQUEST;
+	header.instance = instance_id;
+	header.pldm_type = PLDM_OEM;
+	header.command = PLDM_HOST_GET_ALERT_STATUS;
+
+	if ((rc = pack_pldm_header(&header, &(msg->hdr))) > PLDM_SUCCESS) {
+		return rc;
+	}
+
+	if (payload_length != PLDM_GET_ALERT_STATUS_REQ_BYTES) {
+		return PLDM_ERROR_INVALID_LENGTH;
+	}
+
+	msg->payload[0] = version_id;
+
+	return PLDM_SUCCESS;
+}
+
+int decode_get_alert_status_resp(const struct pldm_msg *msg,
+				 size_t payload_length,
+				 uint8_t *completion_code, uint32_t *rack_entry,
+				 uint32_t *pri_cec_node)
+{
+	if (msg == NULL || completion_code == NULL || rack_entry == NULL ||
+	    pri_cec_node == NULL) {
+		return PLDM_ERROR_INVALID_DATA;
+	}
+
+	*completion_code = msg->payload[0];
+	if (PLDM_SUCCESS != *completion_code) {
+		return PLDM_SUCCESS;
+	}
+
+	if (payload_length != PLDM_GET_ALERT_STATUS_RESP_BYTES) {
+		return PLDM_ERROR_INVALID_LENGTH;
+	}
+
+	struct pldm_get_alert_status_resp *response =
+	    (struct pldm_get_alert_status_resp *)msg->payload;
+
+	*rack_entry = le32toh(response->rack_entry);
+	*pri_cec_node = le32toh(response->pri_cec_node);
+
+	return PLDM_SUCCESS;
+}
\ No newline at end of file
diff --git a/oem/ibm/libpldm/host.h b/oem/ibm/libpldm/host.h
new file mode 100644
index 0000000..700c3e6
--- /dev/null
+++ b/oem/ibm/libpldm/host.h
@@ -0,0 +1,78 @@
+#ifndef OEM_IBM_HOST_H
+#define OEM_IBM_HOST_H
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+#include <stddef.h>
+#include <stdint.h>
+
+#include "base.h"
+
+/* Maximum size for request */
+#define PLDM_GET_ALERT_STATUS_REQ_BYTES 1
+
+/* Response lengths are inclusive of completion code */
+#define PLDM_GET_ALERT_STATUS_RESP_BYTES 9
+
+enum pldm_host_commands {
+	PLDM_HOST_GET_ALERT_STATUS = 0xF0 // Custom oem cmd
+};
+
+/** @brief PLDM Command specific codes
+ */
+enum pldm_host_completion_codes { PLDM_HOST_UNSUPPORTED_FORMAT_VERSION = 0x81 };
+
+/** @struct pldm_get_alert_states_resp
+ *
+ * Structure representing GetAlertStatus response packet
+ */
+struct pldm_get_alert_status_resp {
+	uint8_t completion_code;
+	uint32_t rack_entry;
+	uint32_t pri_cec_node;
+} __attribute__((packed));
+
+/* Requester */
+
+/* GetAlertStatus */
+
+/** @brief Create a PLDM request message for GetAlertStatus
+ *
+ *  @param[in] instance_id - Message's instance id
+ *  @param[in] version_id - The command/response format. 0x00 for this format
+ *  @param[out] msg - Message will be written to this
+ *  @param[in] payload_length - Length of request message payload
+ *  @return pldm_completion_codes
+ *  @note  Caller is responsible for memory alloc and dealloc of param
+ *         'msg.payload'
+ */
+int encode_get_alert_status_req(uint8_t instance_id, uint8_t version_id,
+				struct pldm_msg *msg, size_t payload_length);
+
+/** @brief Decode GetAlertStatus response data
+ *
+ *  Note:
+ *  * If the return value is not PLDM_SUCCESS, it represents a
+ * transport layer error.
+ *  * If the completion_code value is not PLDM_SUCCESS, it represents a
+ * protocol layer error and all the out-parameters are invalid.
+ *
+ *  @param[in] msg - Request message
+ *  @param[in] payload_length - Length of request message payload
+ *  @param[out] completion_code - PLDM completion code
+ *  @param[out] rack_entry - Enclosure ID, Alert Status, Flags, Config ID
+ *  @param[out] pri_cec_node - Enclosure ID, Alert Status, Flags, Config ID
+ *  @return pldm_completion_codes
+ */
+int decode_get_alert_status_resp(const struct pldm_msg *msg,
+				 size_t payload_length,
+				 uint8_t *completion_code, uint32_t *rack_entry,
+				 uint32_t *pri_cec_node);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* OEM_IBM_HOST_H */
diff --git a/oem/ibm/test/libpldm_host_test.cpp b/oem/ibm/test/libpldm_host_test.cpp
new file mode 100644
index 0000000..6596f35
--- /dev/null
+++ b/oem/ibm/test/libpldm_host_test.cpp
@@ -0,0 +1,88 @@
+#include <string.h>
+
+#include <array>
+
+#include "oem/ibm/libpldm/host.h"
+
+#include <gtest/gtest.h>
+
+constexpr auto hdrSize = sizeof(pldm_msg_hdr);
+
+TEST(GetAlertStatus, testGoodEncodeRequest)
+{
+    std::array<uint8_t, hdrSize + PLDM_GET_ALERT_STATUS_REQ_BYTES> requestMsg{};
+
+    uint8_t versionId = 0x0;
+
+    auto request = reinterpret_cast<pldm_msg*>(requestMsg.data());
+    auto rc = encode_get_alert_status_req(0, versionId, request,
+                                          PLDM_GET_ALERT_STATUS_REQ_BYTES);
+    EXPECT_EQ(rc, PLDM_SUCCESS);
+    EXPECT_EQ(versionId, request->payload[0]);
+}
+
+TEST(GetAlertStatus, testBadEncodeRequest)
+{
+    std::array<uint8_t, hdrSize + PLDM_GET_ALERT_STATUS_REQ_BYTES> requestMsg{};
+
+    auto request = reinterpret_cast<pldm_msg*>(requestMsg.data());
+    auto rc = encode_get_alert_status_req(0, 0x0, request,
+                                          PLDM_GET_ALERT_STATUS_REQ_BYTES + 1);
+    EXPECT_EQ(rc, PLDM_ERROR_INVALID_LENGTH);
+}
+
+TEST(GetAlertStatus, testGoodDecodeResponse)
+{
+    uint8_t completionCode = PLDM_SUCCESS;
+    uint32_t rack_entry = 0xFF000030;
+    uint32_t pri_cec_node = 0x00008030;
+    std::array<uint8_t, hdrSize + PLDM_GET_ALERT_STATUS_RESP_BYTES>
+        responseMsg{};
+
+    uint8_t retCompletionCode = 0;
+    uint32_t retRack_entry = 0;
+    uint32_t retPri_cec_node = 0;
+
+    auto response = reinterpret_cast<pldm_msg*>(responseMsg.data());
+    struct pldm_get_alert_status_resp* resp =
+        reinterpret_cast<struct pldm_get_alert_status_resp*>(response->payload);
+    resp->completion_code = completionCode;
+    resp->rack_entry = htole32(rack_entry);
+    resp->pri_cec_node = htole32(pri_cec_node);
+
+    auto rc = decode_get_alert_status_resp(
+        response, responseMsg.size() - hdrSize, &retCompletionCode,
+        &retRack_entry, &retPri_cec_node);
+    EXPECT_EQ(rc, PLDM_SUCCESS);
+    EXPECT_EQ(retCompletionCode, completionCode);
+    EXPECT_EQ(retRack_entry, rack_entry);
+    EXPECT_EQ(retPri_cec_node, pri_cec_node);
+}
+
+TEST(GetAlertStatus, testBadDecodeResponse)
+{
+    auto rc = decode_get_alert_status_resp(NULL, 0, NULL, NULL, NULL);
+    EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
+
+    uint8_t completionCode = PLDM_SUCCESS;
+    uint32_t rack_entry = 0xFF000030;
+    uint32_t pri_cec_node = 0x00008030;
+    std::array<uint8_t, hdrSize + PLDM_GET_ALERT_STATUS_RESP_BYTES>
+        responseMsg{};
+
+    uint8_t retCompletionCode = 0;
+    uint32_t retRack_entry = 0;
+    uint32_t retPri_cec_node = 0;
+
+    auto response = reinterpret_cast<pldm_msg*>(responseMsg.data());
+    struct pldm_get_alert_status_resp* resp =
+        reinterpret_cast<struct pldm_get_alert_status_resp*>(response->payload);
+    resp->completion_code = completionCode;
+    resp->rack_entry = htole32(rack_entry);
+    resp->pri_cec_node = htole32(pri_cec_node);
+
+    rc = decode_get_alert_status_resp(
+        response, responseMsg.size() - hdrSize + 1, &retCompletionCode,
+        &retRack_entry, &retPri_cec_node);
+    EXPECT_EQ(rc, PLDM_ERROR_INVALID_LENGTH);
+}