PLDM: Implement encode response & decode request api for SetEventReceiver.

This commit implements the responder flow for SetEventReceiver.
PLDM SPEC: DSP248_1.2.0 TABLE 13

Signed-off-by: Sridevi Ramesh <sridevra@in.ibm.com>
Change-Id: I2e9fe2797ef2adb1f24f2a80bd19e7255ac80ce9
diff --git a/libpldm/platform.c b/libpldm/platform.c
index 804339d..135a422 100644
--- a/libpldm/platform.c
+++ b/libpldm/platform.c
@@ -1595,3 +1595,61 @@
 
 	return PLDM_SUCCESS;
 }
+
+int decode_set_event_receiver_req(const struct pldm_msg *msg,
+				  size_t payload_length,
+				  uint8_t *event_message_global_enable,
+				  uint8_t *transport_protocol_type,
+				  uint8_t *event_receiver_address_info,
+				  uint16_t *heartbeat_timer)
+
+{
+	if (msg == NULL || event_message_global_enable == NULL ||
+	    transport_protocol_type == NULL ||
+	    event_receiver_address_info == NULL || heartbeat_timer == NULL) {
+		return PLDM_ERROR_INVALID_DATA;
+	}
+
+	if (payload_length != PLDM_SET_EVENT_RECEIVER_REQ_BYTES) {
+		return PLDM_ERROR_INVALID_LENGTH;
+	}
+
+	struct pldm_set_event_receiver_req *request =
+	    (struct pldm_set_event_receiver_req *)msg->payload;
+
+	if ((*event_message_global_enable ==
+	     PLDM_EVENT_MESSAGE_GLOBAL_ENABLE_ASYNC_KEEP_ALIVE) &&
+	    (*heartbeat_timer == 0)) {
+		return PLDM_ERROR_INVALID_DATA;
+	}
+
+	*event_message_global_enable = request->event_message_global_enable,
+	*transport_protocol_type = request->transport_protocol_type,
+	*event_receiver_address_info = request->event_receiver_address_info,
+	*heartbeat_timer = le16toh(request->heartbeat_timer);
+
+	return PLDM_SUCCESS;
+}
+
+int encode_set_event_receiver_resp(uint8_t instance_id, uint8_t completion_code,
+				   struct pldm_msg *msg)
+
+{
+	struct pldm_header_info header = {0};
+	int rc = PLDM_SUCCESS;
+
+	if (msg == NULL) {
+		return PLDM_ERROR_INVALID_DATA;
+	}
+
+	msg->payload[0] = completion_code;
+
+	header.instance = instance_id;
+	header.msg_type = PLDM_RESPONSE;
+	header.pldm_type = PLDM_PLATFORM;
+	header.command = PLDM_SET_EVENT_RECEIVER;
+
+	rc = pack_pldm_header(&header, &(msg->hdr));
+
+	return rc;
+}
diff --git a/libpldm/platform.h b/libpldm/platform.h
index 15f00c2..0c392a8 100644
--- a/libpldm/platform.h
+++ b/libpldm/platform.h
@@ -1526,6 +1526,42 @@
 int decode_set_event_receiver_resp(const struct pldm_msg *msg,
 				   size_t payload_length,
 				   uint8_t *completion_code);
+
+/** @brief Decode the SetEventReceiver request message
+ *
+ * @param[in] msg - Request message
+ * @param[in] payload_length - Length of request message payload
+ * @param[out] event_message_global_enable - This value is used to enable or
+ *        disable event message generation from the terminus value: {
+ *        disable, enableAsync, enablePolling, enableAsyncKeepAlive }
+ * @param[out] transport_protocol_type - This value is provided in the request
+ *        to help the responder verify that the content of the
+ *        eventReceiverAddressInfo field used in this request is correct for
+ *        the messaging protocol supported by the terminus.
+ * @param[out] event_receiver_address_info - This value is a medium and
+ *        protocol-specific address that the responder should use when
+ *        transmitting event messages using the indicated protocol
+ * @param[out] heartbeat_timer - Amount of time in seconds after each elapsing
+ *        of which the terminus shall emit a heartbeat event to the receiver
+ * @return pldm_completion_codes
+ */
+int decode_set_event_receiver_req(const struct pldm_msg *msg,
+				  size_t payload_length,
+				  uint8_t *event_message_global_enable,
+				  uint8_t *transport_protocol_type,
+				  uint8_t *event_receiver_address_info,
+				  uint16_t *heartbeat_timer);
+
+/** @brief Encode the SetEventReceiver response message
+ *
+ *  @param[in] instance_id - Message's instance id
+ *  @param[in] completion_code - PLDM completion code
+ *  @param[out] msg - Argument to capture the Message
+ *  @return pldm_completion_codes
+ */
+int encode_set_event_receiver_resp(uint8_t instance_id, uint8_t completion_code,
+				   struct pldm_msg *msg);
+
 #ifdef __cplusplus
 }
 #endif
diff --git a/libpldm/tests/libpldm_platform_test.cpp b/libpldm/tests/libpldm_platform_test.cpp
index 11d6777..cab6a07 100644
--- a/libpldm/tests/libpldm_platform_test.cpp
+++ b/libpldm/tests/libpldm_platform_test.cpp
@@ -1942,3 +1942,95 @@
 
     EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
 }
+
+TEST(SetEventReceiver, testGoodEncodeResponse)
+{
+    std::array<uint8_t,
+               sizeof(pldm_msg_hdr) + PLDM_SET_EVENT_RECEIVER_RESP_BYTES>
+        responseMsg{};
+    auto response = reinterpret_cast<pldm_msg*>(responseMsg.data());
+    uint8_t completionCode = 0;
+
+    auto rc = encode_set_event_receiver_resp(0, PLDM_SUCCESS, response);
+
+    EXPECT_EQ(rc, PLDM_SUCCESS);
+    EXPECT_EQ(completionCode, response->payload[0]);
+}
+
+TEST(SetEventReceiver, testBadEncodeResponse)
+{
+    auto rc = encode_set_event_receiver_resp(0, PLDM_SUCCESS, NULL);
+    EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
+}
+
+TEST(SetEventReceiver, testGoodDecodeRequest)
+{
+
+    std::array<uint8_t, hdrSize + PLDM_SET_EVENT_RECEIVER_REQ_BYTES>
+        requestMsg{};
+
+    uint8_t eventMessageGlobalEnable =
+        PLDM_EVENT_MESSAGE_GLOBAL_ENABLE_ASYNC_KEEP_ALIVE;
+    uint8_t transportProtocolType = PLDM_TRANSPORT_PROTOCOL_TYPE_MCTP;
+    uint8_t eventReceiverAddressInfo = 0x08;
+    uint16_t heartbeatTimer = 0x78;
+
+    auto request = reinterpret_cast<pldm_msg*>(requestMsg.data());
+    struct pldm_set_event_receiver_req* req =
+        reinterpret_cast<struct pldm_set_event_receiver_req*>(request->payload);
+
+    req->event_message_global_enable = eventMessageGlobalEnable;
+    req->transport_protocol_type = transportProtocolType;
+    req->event_receiver_address_info = eventReceiverAddressInfo;
+    req->heartbeat_timer = htole16(heartbeatTimer);
+
+    uint8_t reteventMessageGlobalEnable;
+    uint8_t rettransportProtocolType;
+    uint8_t reteventReceiverAddressInfo;
+    uint16_t retheartbeatTimer;
+    auto rc = decode_set_event_receiver_req(
+        request, requestMsg.size() - hdrSize, &reteventMessageGlobalEnable,
+        &rettransportProtocolType, &reteventReceiverAddressInfo,
+        &retheartbeatTimer);
+
+    EXPECT_EQ(rc, PLDM_SUCCESS);
+    EXPECT_EQ(eventMessageGlobalEnable, reteventMessageGlobalEnable);
+    EXPECT_EQ(transportProtocolType, rettransportProtocolType);
+    EXPECT_EQ(eventReceiverAddressInfo, reteventReceiverAddressInfo);
+    EXPECT_EQ(heartbeatTimer, retheartbeatTimer);
+}
+
+TEST(SetEventReceiver, testBadDecodeRequest)
+{
+    std::array<uint8_t, hdrSize + PLDM_SET_EVENT_RECEIVER_REQ_BYTES>
+        requestMsg{};
+
+    auto rc = decode_set_event_receiver_req(NULL, requestMsg.size() - hdrSize,
+                                            NULL, NULL, NULL, NULL);
+    EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
+
+    uint8_t eventMessageGlobalEnable =
+        PLDM_EVENT_MESSAGE_GLOBAL_ENABLE_ASYNC_KEEP_ALIVE;
+    uint8_t transportProtocolType = PLDM_TRANSPORT_PROTOCOL_TYPE_MCTP;
+    uint8_t eventReceiverAddressInfo = 0x08;
+    uint16_t heartbeatTimer = 0x78;
+
+    auto request = reinterpret_cast<pldm_msg*>(requestMsg.data());
+    struct pldm_set_event_receiver_req* req =
+        reinterpret_cast<struct pldm_set_event_receiver_req*>(request->payload);
+
+    req->event_message_global_enable = eventMessageGlobalEnable;
+    req->transport_protocol_type = transportProtocolType;
+    req->event_receiver_address_info = eventReceiverAddressInfo;
+    req->heartbeat_timer = htole16(heartbeatTimer);
+
+    uint8_t reteventMessageGlobalEnable;
+    uint8_t rettransportProtocolType;
+    uint8_t reteventReceiverAddressInfo;
+    uint16_t retheartbeatTimer;
+    rc = decode_set_event_receiver_req(
+        request, requestMsg.size() - hdrSize - 1, &reteventMessageGlobalEnable,
+        &rettransportProtocolType, &reteventReceiverAddressInfo,
+        &retheartbeatTimer);
+    EXPECT_EQ(rc, PLDM_ERROR_INVALID_LENGTH);
+}