Add encode/decode for EventMessageSupported

Added encode API for EventMessageSupported command(0x0C) which
is defined in DSP0248 Version 1.2.2 sec:16.8.

Signed-off-by: Dung Cao <dung@os.amperecomputing.com>
Signed-off-by: Thu Nguyen <thu@os.amperecomputing.com>
Change-Id: Id19ea795d233091841b5654164b66eea59df0806
diff --git a/include/libpldm/platform.h b/include/libpldm/platform.h
index c595052..4350ed4 100644
--- a/include/libpldm/platform.h
+++ b/include/libpldm/platform.h
@@ -32,6 +32,9 @@
 #define PLDM_EVENT_MESSAGE_BUFFER_SIZE_REQ_BYTES 2
 #define PLDM_EVENT_MESSAGE_BUFFER_SIZE_RESP_BYTES 3
 
+#define PLDM_EVENT_MESSAGE_SUPPORTED_REQ_BYTES 1
+#define PLDM_EVENT_MESSAGE_SUPPORTED_MIN_RESP_BYTES 4
+
 /* Minimum response length */
 #define PLDM_GET_PDR_MIN_RESP_BYTES 12
 #define PLDM_GET_NUMERIC_EFFECTER_VALUE_MIN_RESP_BYTES 5
@@ -122,7 +125,8 @@
 enum pldm_platform_commands {
 	PLDM_SET_EVENT_RECEIVER = 0x04,
 	PLDM_PLATFORM_EVENT_MESSAGE = 0x0A,
-	PLDM_EVENT_MESSAGE_BUFFER_SIZE = 0xD,
+	PLDM_EVENT_MESSAGE_SUPPORTED = 0x0C,
+	PLDM_EVENT_MESSAGE_BUFFER_SIZE = 0x0D,
 	PLDM_GET_SENSOR_READING = 0x11,
 	PLDM_GET_STATE_SENSOR_READINGS = 0x21,
 	PLDM_SET_NUMERIC_EFFECTER_VALUE = 0x31,
@@ -405,6 +409,14 @@
 	PLDM_DEFALUT_MINIMUM_TIMEOUT
 };
 
+/** @brief PLDM event message type */
+enum pldm_event_message_type {
+	PLDM_MESSAGE_TYPE_NOT_CONFIGURED = 0x00,
+	PLDM_MESSAGE_TYPE_ASYNCHRONOUS = 0x01,
+	PLDM_MESSAGE_TYPE_SYNCHRONOUS = 0x02,
+	PLDM_MESSAGE_TYPE_ASYNCHRONOUS_WITH_HEARTBEAT = 0x03
+};
+
 /** @struct pldm_pdr_hdr
  *
  *  Structure representing PLDM common PDR header
@@ -771,6 +783,26 @@
 	uint16_t terminus_max_buffer_size;
 } __attribute__((packed));
 
+/** @struct pldm_platform_event_message_supported_req
+ *
+ *  structure representing PlatformEventMessageSupported command request data
+ */
+struct pldm_event_message_supported_req {
+	uint8_t format_version;
+} __attribute__((packed));
+
+/** @struct pldm_event_message_supported_response
+ *
+ *  structure representing EventMessageSupported command response data
+ */
+struct pldm_event_message_supported_resp {
+	uint8_t completion_code;
+	uint8_t synchrony_configuration;
+	bitfield8_t synchrony_configuration_supported;
+	uint8_t number_event_class_returned;
+	uint8_t event_class[1];
+} __attribute__((packed));
+
 /** @struct pldm_set_numeric_effecter_value_req
  *
  *  structure representing SetNumericEffecterValue request packet
@@ -1488,6 +1520,39 @@
     uint8_t instance_id, uint16_t event_receiver_max_buffer_size,
     struct pldm_msg *msg);
 
+/** @brief Encode EventMessageSupported request data
+ *
+ *  @param[in] instance_id - Message's instance id
+ *  @param[in] format_version - Version of the event format
+ *  @param[out] msg - Request message
+ *
+ *  @return pldm_completion_codes
+ *  @note Caller is responsible for memory alloc and dealloc of param
+ *  'msg.payload'
+ */
+int encode_event_message_supported_req(uint8_t instance_id,
+				       uint8_t format_version,
+				       struct pldm_msg *msg);
+
+/** @brief Decode EventMessageSupported response data
+ *
+ *  @param[in] msg - Request message
+ *  @param[in] payload_length - Length of Response message payload
+ *  @param[out] completion_code - PLDM completion code
+ *  @param[out] synchrony_config - the synchrony configuration
+ *  @param[out] synchrony_config_support - the synchrony configuration support
+ *  @param[out] number_event_class_returned - PLDM completion code
+ *  @param[out] event_class - the event classes
+ *  @param[in] event_class_count - the event class count
+ *
+ *  @return pldm_completion_codes
+ */
+int decode_event_message_supported_resp(
+    const struct pldm_msg *msg, size_t payload_length, uint8_t *completion_code,
+    uint8_t *synchrony_config, bitfield8_t *synchrony_config_support,
+    uint8_t *number_event_class_returned, uint8_t *event_class,
+    uint8_t event_class_count);
+
 /** @brief Decode sensorEventData response data
  *
  *  @param[in] event_data - event data from the response message
diff --git a/src/platform.c b/src/platform.c
index 00dc019..93ca6e9 100644
--- a/src/platform.c
+++ b/src/platform.c
@@ -1016,6 +1016,78 @@
 	return PLDM_SUCCESS;
 }
 
+int encode_event_message_supported_req(uint8_t instance_id,
+				       uint8_t format_version,
+				       struct pldm_msg *msg)
+{
+	if (format_version != 1) {
+		return PLDM_ERROR_INVALID_DATA;
+	}
+
+	if (msg == NULL) {
+		return PLDM_ERROR_INVALID_DATA;
+	}
+
+	struct pldm_header_info header = {0};
+	header.msg_type = PLDM_REQUEST;
+	header.instance = instance_id;
+	header.pldm_type = PLDM_PLATFORM;
+	header.command = PLDM_EVENT_MESSAGE_SUPPORTED;
+
+	uint8_t rc = pack_pldm_header(&header, &(msg->hdr));
+	if (rc != PLDM_SUCCESS) {
+		return rc;
+	}
+
+	struct pldm_event_message_supported_req *request =
+	    (struct pldm_event_message_supported_req *)msg->payload;
+	request->format_version = format_version;
+
+	return PLDM_SUCCESS;
+}
+
+int decode_event_message_supported_resp(
+    const struct pldm_msg *msg, size_t payload_length, uint8_t *completion_code,
+    uint8_t *synchrony_config, bitfield8_t *synchrony_config_support,
+    uint8_t *number_event_class_returned, uint8_t *event_class,
+    uint8_t event_class_count)
+{
+	if (msg == NULL || completion_code == NULL ||
+	    synchrony_config == NULL || synchrony_config_support == NULL ||
+	    number_event_class_returned == NULL || event_class == NULL) {
+		return PLDM_ERROR_INVALID_DATA;
+	}
+
+	*completion_code = msg->payload[0];
+	if (PLDM_SUCCESS != *completion_code) {
+		return PLDM_SUCCESS;
+	}
+	if (payload_length < PLDM_EVENT_MESSAGE_SUPPORTED_MIN_RESP_BYTES) {
+		return PLDM_ERROR_INVALID_LENGTH;
+	}
+
+	struct pldm_event_message_supported_resp *response =
+	    (struct pldm_event_message_supported_resp *)msg->payload;
+
+	*synchrony_config = response->synchrony_configuration;
+	if (*synchrony_config > PLDM_MESSAGE_TYPE_ASYNCHRONOUS_WITH_HEARTBEAT) {
+		return PLDM_ERROR_INVALID_DATA;
+	}
+
+	*synchrony_config_support = response->synchrony_configuration_supported;
+	*number_event_class_returned = response->number_event_class_returned;
+
+	if (*number_event_class_returned > 0) {
+		if (event_class_count < *number_event_class_returned) {
+			return PLDM_ERROR_INVALID_LENGTH;
+		}
+		memcpy(event_class, response->event_class,
+		       *number_event_class_returned);
+	}
+
+	return PLDM_SUCCESS;
+}
+
 int decode_sensor_event_data(const uint8_t *event_data,
 			     size_t event_data_length, uint16_t *sensor_id,
 			     uint8_t *sensor_event_class_type,
diff --git a/tests/libpldm_platform_test.cpp b/tests/libpldm_platform_test.cpp
index b62125b..396e9d9 100644
--- a/tests/libpldm_platform_test.cpp
+++ b/tests/libpldm_platform_test.cpp
@@ -1013,6 +1013,173 @@
     EXPECT_EQ(rc, PLDM_ERROR_INVALID_LENGTH);
 }
 
+TEST(PlatformEventMessageSupported, testGoodEncodeRequest)
+{
+    uint8_t formatVersion = 0x01;
+
+    std::array<uint8_t, hdrSize + PLDM_EVENT_MESSAGE_SUPPORTED_REQ_BYTES>
+        requestMsg{};
+
+    auto request = reinterpret_cast<pldm_msg*>(requestMsg.data());
+
+    auto rc = encode_event_message_supported_req(0, formatVersion, request);
+
+    struct pldm_event_message_supported_req* req =
+        reinterpret_cast<struct pldm_event_message_supported_req*>(
+            request->payload);
+
+    EXPECT_EQ(rc, PLDM_SUCCESS);
+    EXPECT_EQ(formatVersion, req->format_version);
+}
+
+TEST(PlatformEventMessageSupported, testBadEncodeRequest)
+{
+    uint8_t eventData = 34;
+    uint8_t formatVersion = 0x0;
+
+    std::array<uint8_t, hdrSize + PLDM_EVENT_MESSAGE_SUPPORTED_REQ_BYTES +
+                            sizeof(eventData)>
+        requestMsg{};
+    auto request = reinterpret_cast<pldm_msg*>(requestMsg.data());
+
+    auto rc = encode_event_message_supported_req(0, formatVersion, request);
+    EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
+
+    rc = encode_event_message_supported_req(0, formatVersion, nullptr);
+    EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
+}
+
+TEST(PlatformEventMessageSupported, testGoodDecodeRespond)
+{
+    uint8_t completionCode = PLDM_SUCCESS;
+    uint8_t synchConfiguration = PLDM_MESSAGE_TYPE_SYNCHRONOUS;
+    bitfield8_t synchConfigSupported;
+    synchConfigSupported.byte = 0xe;
+    uint8_t numberEventClassReturned = 0x3;
+    std::vector<uint8_t> eventClass{0x0, 0x5, 0xfa};
+    constexpr uint8_t eventClassCount = 3;
+
+    std::array<uint8_t, hdrSize + PLDM_EVENT_MESSAGE_SUPPORTED_MIN_RESP_BYTES +
+                            eventClassCount>
+        responseMsg{};
+
+    uint8_t retCompletionCode;
+    uint8_t retSynchConfig = 0;
+    uint8_t retNumberEventClass = 0;
+    bitfield8_t retSynchConfigSupport;
+    uint8_t retEventClass[eventClassCount] = {0};
+
+    auto response = reinterpret_cast<pldm_msg*>(responseMsg.data());
+    struct pldm_event_message_supported_resp* resp =
+        reinterpret_cast<struct pldm_event_message_supported_resp*>(
+            response->payload);
+
+    resp->completion_code = completionCode;
+    resp->synchrony_configuration = synchConfiguration;
+    resp->synchrony_configuration_supported.byte = synchConfigSupported.byte;
+    resp->number_event_class_returned = numberEventClassReturned;
+    memcpy(resp->event_class, eventClass.data(), numberEventClassReturned);
+
+    auto rc = decode_event_message_supported_resp(
+        response, responseMsg.size() - hdrSize, &retCompletionCode,
+        &retSynchConfig, &retSynchConfigSupport, &retNumberEventClass,
+        retEventClass, eventClassCount);
+
+    EXPECT_EQ(rc, PLDM_SUCCESS);
+    EXPECT_EQ(retCompletionCode, completionCode);
+    EXPECT_EQ(retSynchConfig, synchConfiguration);
+    EXPECT_EQ(retNumberEventClass, numberEventClassReturned);
+    EXPECT_EQ(retSynchConfigSupport.byte, synchConfigSupported.byte);
+    EXPECT_EQ(0, memcmp(eventClass.data(), resp->event_class,
+                        numberEventClassReturned));
+}
+
+TEST(PlatformEventMessageSupported, testBadSynchConfiguration)
+{
+    uint8_t completionCode = PLDM_SUCCESS;
+    uint8_t synchConfiguration = 0x4;
+    bitfield8_t synchConfigSupported;
+    synchConfigSupported.byte = 0xe;
+    uint8_t numberEventClassReturned = 0x3;
+    std::vector<uint8_t> eventClass{0x0, 0x5, 0xfa};
+    constexpr uint8_t eventClassCount = 3;
+
+    std::array<uint8_t, hdrSize + PLDM_EVENT_MESSAGE_SUPPORTED_MIN_RESP_BYTES +
+                            eventClassCount>
+        responseMsg{};
+
+    uint8_t retCompletionCode;
+    uint8_t retSynchConfig = 0;
+    uint8_t retNumberEventClass = 0;
+    bitfield8_t retSynchConfigSupport;
+    uint8_t retEventClass[eventClassCount] = {0};
+
+    auto response = reinterpret_cast<pldm_msg*>(responseMsg.data());
+    struct pldm_event_message_supported_resp* resp =
+        reinterpret_cast<struct pldm_event_message_supported_resp*>(
+            response->payload);
+
+    resp->completion_code = completionCode;
+    resp->synchrony_configuration = synchConfiguration;
+    resp->synchrony_configuration_supported.byte = synchConfigSupported.byte;
+    resp->number_event_class_returned = numberEventClassReturned;
+    memcpy(resp->event_class, eventClass.data(), numberEventClassReturned);
+
+    auto rc = decode_event_message_supported_resp(
+        response, responseMsg.size() - hdrSize, &retCompletionCode,
+        &retSynchConfig, &retSynchConfigSupport, &retNumberEventClass,
+        retEventClass, eventClassCount);
+
+    EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
+}
+
+TEST(PlatformEventMessageSupported, testBadDecodeRespond)
+{
+    uint8_t completionCode = PLDM_SUCCESS;
+    uint8_t synchConfiguration = PLDM_MESSAGE_TYPE_SYNCHRONOUS;
+    bitfield8_t synchConfigSupported;
+    synchConfigSupported.byte = 0xe;
+    uint8_t numberEventClassReturned = 0x3;
+    std::vector<uint8_t> eventClass{0x0, 0x5, 0xfa};
+    constexpr uint8_t eventClassCount = 3;
+
+    std::array<uint8_t, hdrSize + PLDM_EVENT_MESSAGE_SUPPORTED_MIN_RESP_BYTES +
+                            eventClassCount>
+        responseMsg{};
+
+    uint8_t retCompletionCode;
+    uint8_t retSynchConfig = 0;
+    uint8_t retNumberEventClass = 0;
+    bitfield8_t retSynchConfigSupport;
+    uint8_t retEventClass[eventClassCount] = {0};
+
+    auto response = reinterpret_cast<pldm_msg*>(responseMsg.data());
+    struct pldm_event_message_supported_resp* resp =
+        reinterpret_cast<struct pldm_event_message_supported_resp*>(
+            response->payload);
+    resp->completion_code = completionCode;
+    resp->synchrony_configuration = synchConfiguration;
+    resp->synchrony_configuration_supported.byte = synchConfigSupported.byte;
+    resp->number_event_class_returned = numberEventClassReturned;
+    memcpy(resp->event_class, eventClass.data(), numberEventClassReturned);
+
+    auto rc = decode_event_message_supported_resp(response, 0, nullptr, nullptr,
+                                                  nullptr, nullptr, nullptr, 0);
+    EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
+
+    rc = decode_event_message_supported_resp(
+        response, PLDM_EVENT_MESSAGE_SUPPORTED_MIN_RESP_BYTES - 1,
+        &retCompletionCode, &retSynchConfig, &retSynchConfigSupport,
+        &retNumberEventClass, retEventClass, eventClassCount);
+    EXPECT_EQ(rc, PLDM_ERROR_INVALID_LENGTH);
+
+    rc = decode_event_message_supported_resp(
+        response, responseMsg.size() - hdrSize, &retCompletionCode,
+        &retSynchConfig, &retSynchConfigSupport, &retNumberEventClass,
+        retEventClass, 1);
+    EXPECT_EQ(rc, PLDM_ERROR_INVALID_LENGTH);
+}
+
 TEST(PlatformEventMessage, testGoodStateSensorDecodeRequest)
 {
     std::array<uint8_t,