oem/libpldm: platform: implement responder flow for GetAlertStatus

For the responder, need to encode responder data and send requester,
waiting for recive requester data to decode, so need to implement
encode/decode(Responder 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: I968056bbabf5cc724a3b6a9646dc271df4847ed2
diff --git a/oem/ibm/libpldm/host.c b/oem/ibm/libpldm/host.c
index 6628bd5..454f6c3 100644
--- a/oem/ibm/libpldm/host.c
+++ b/oem/ibm/libpldm/host.c
@@ -53,4 +53,50 @@
 	*pri_cec_node = le32toh(response->pri_cec_node);
 
 	return PLDM_SUCCESS;
-}
\ No newline at end of file
+}
+
+int decode_get_alert_status_req(const struct pldm_msg *msg,
+				size_t payload_length, uint8_t *version_id)
+{
+	if (msg == NULL || version_id == NULL) {
+		return PLDM_ERROR_INVALID_DATA;
+	}
+
+	if (payload_length != PLDM_GET_ALERT_STATUS_REQ_BYTES) {
+		return PLDM_ERROR_INVALID_LENGTH;
+	}
+
+	*version_id = msg->payload[0];
+
+	return PLDM_SUCCESS;
+}
+
+int encode_get_alert_status_resp(uint8_t instance_id, uint8_t completion_code,
+				 uint32_t rack_entry, uint32_t pri_cec_node,
+				 struct pldm_msg *msg, size_t payload_length)
+{
+	struct pldm_header_info header = {0};
+	int rc = PLDM_SUCCESS;
+
+	header.msg_type = PLDM_RESPONSE;
+	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_RESP_BYTES) {
+		return PLDM_ERROR_INVALID_DATA;
+	}
+
+	struct pldm_get_alert_status_resp *response =
+	    (struct pldm_get_alert_status_resp *)msg->payload;
+
+	response->completion_code = completion_code;
+	response->rack_entry = htole32(rack_entry);
+	response->pri_cec_node = htole32(pri_cec_node);
+
+	return PLDM_SUCCESS;
+}
diff --git a/oem/ibm/libpldm/host.h b/oem/ibm/libpldm/host.h
index 700c3e6..ed121ae 100644
--- a/oem/ibm/libpldm/host.h
+++ b/oem/ibm/libpldm/host.h
@@ -71,6 +71,36 @@
 				 uint8_t *completion_code, uint32_t *rack_entry,
 				 uint32_t *pri_cec_node);
 
+/* Responder */
+
+/* GetAlertStatus */
+
+/** @brief Decode GetAlertStatus request data
+ *
+ *  @param[in] msg - Request message
+ *  @param[in] payload_length - Length of request message payload
+ *  @param[out] version_id - the command/response format. 0x00 for this format
+ *  @return pldm_completion_codes
+ */
+int decode_get_alert_status_req(const struct pldm_msg *msg,
+				size_t payload_length, uint8_t *version_id);
+
+/** @brief Create a PLDM OEM response message for GetAlertStatus
+ *
+ *  @param[in] instance_id - Message's instance id
+ *  @param[in] completion_code - PLDM completion code
+ *  @param[in] rack_entry - Enclosure ID, Alert Status, Flags, Config ID
+ *  @param[in] pri_cec_node - Enclosure ID, Alert Status, Flags, Config ID
+ *  @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.body.payload'
+ */
+int encode_get_alert_status_resp(uint8_t instance_id, uint8_t completion_code,
+				 uint32_t rack_entry, uint32_t pri_cec_node,
+				 struct pldm_msg *msg, size_t payload_length);
+
 #ifdef __cplusplus
 }
 #endif
diff --git a/oem/ibm/test/libpldm_host_test.cpp b/oem/ibm/test/libpldm_host_test.cpp
index 6596f35..26ae5aa 100644
--- a/oem/ibm/test/libpldm_host_test.cpp
+++ b/oem/ibm/test/libpldm_host_test.cpp
@@ -86,3 +86,77 @@
         &retRack_entry, &retPri_cec_node);
     EXPECT_EQ(rc, PLDM_ERROR_INVALID_LENGTH);
 }
+
+TEST(GetAlertStatus, testGoodEncodeResponse)
+{
+    uint8_t completionCode = 0;
+    uint32_t rack_entry = 0xFF000030;
+    uint32_t pri_cec_node = 0x00008030;
+
+    std::vector<uint8_t> responseMsg(hdrSize +
+                                     PLDM_GET_ALERT_STATUS_RESP_BYTES);
+    auto response = reinterpret_cast<pldm_msg*>(responseMsg.data());
+
+    auto rc =
+        encode_get_alert_status_resp(0, PLDM_SUCCESS, rack_entry, pri_cec_node,
+                                     response, responseMsg.size() - hdrSize);
+
+    EXPECT_EQ(rc, PLDM_SUCCESS);
+    struct pldm_get_alert_status_resp* resp =
+        reinterpret_cast<struct pldm_get_alert_status_resp*>(response->payload);
+
+    EXPECT_EQ(completionCode, resp->completion_code);
+    EXPECT_EQ(rack_entry, le32toh(resp->rack_entry));
+    EXPECT_EQ(pri_cec_node, le32toh(resp->pri_cec_node));
+}
+
+TEST(GetAlertStatus, testBadEncodeResponse)
+{
+    uint32_t rack_entry = 0xFF000030;
+    uint32_t pri_cec_node = 0x00008030;
+
+    std::vector<uint8_t> responseMsg(hdrSize +
+                                     PLDM_GET_ALERT_STATUS_RESP_BYTES);
+    auto response = reinterpret_cast<pldm_msg*>(responseMsg.data());
+
+    auto rc = encode_get_alert_status_resp(0, PLDM_SUCCESS, rack_entry,
+                                           pri_cec_node, response,
+                                           responseMsg.size() - hdrSize + 1);
+
+    EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
+}
+
+TEST(GetAlertStatus, testGoodDecodeRequest)
+{
+    std::array<uint8_t, hdrSize + PLDM_GET_ALERT_STATUS_REQ_BYTES> requestMsg{};
+
+    uint8_t versionId = 0x0;
+    uint8_t retVersionId;
+
+    auto req = reinterpret_cast<pldm_msg*>(requestMsg.data());
+
+    req->payload[0] = versionId;
+
+    auto rc = decode_get_alert_status_req(req, requestMsg.size() - hdrSize,
+                                          &retVersionId);
+
+    EXPECT_EQ(rc, PLDM_SUCCESS);
+    EXPECT_EQ(retVersionId, versionId);
+}
+
+TEST(GetAlertStatus, testBadDecodeRequest)
+{
+    std::array<uint8_t, hdrSize + PLDM_GET_ALERT_STATUS_REQ_BYTES> requestMsg{};
+
+    uint8_t versionId = 0x0;
+    uint8_t retVersionId;
+
+    auto req = reinterpret_cast<pldm_msg*>(requestMsg.data());
+
+    req->payload[0] = versionId;
+
+    auto rc = decode_get_alert_status_req(req, requestMsg.size() - hdrSize + 1,
+                                          &retVersionId);
+
+    EXPECT_EQ(rc, PLDM_ERROR_INVALID_LENGTH);
+}