libpldm:encode/decode for PlatformEventMessage
This commit implements the encode request and
decode response of PlatformEventMessage command
which is defined in PLDM Platform spec
DSP0248 v1.2.0.
Tested: Unit tested
Change-Id: Id18a28f840dae0f1a65ee127abc6ef7c95170823
Signed-off-by: Pavithra Barithaya <pbaritha@in.ibm.com>
diff --git a/libpldm/platform.c b/libpldm/platform.c
index 97692a9..9ff6bc9 100644
--- a/libpldm/platform.c
+++ b/libpldm/platform.c
@@ -617,6 +617,82 @@
return PLDM_SUCCESS;
}
+int encode_platform_event_message_req(uint8_t instance_id,
+ uint8_t format_version, uint8_t tid,
+ uint8_t event_class,
+ const uint8_t *event_data,
+ size_t event_data_length,
+ struct pldm_msg *msg)
+
+{
+ struct pldm_header_info header = {0};
+ int rc = PLDM_SUCCESS;
+
+ header.msg_type = PLDM_REQUEST;
+ header.instance = instance_id;
+ header.pldm_type = PLDM_PLATFORM;
+ header.command = PLDM_PLATFORM_EVENT_MESSAGE;
+
+ if (format_version != 1) {
+ return PLDM_ERROR_INVALID_DATA;
+ }
+
+ if (msg == NULL || event_data == NULL) {
+ return PLDM_ERROR_INVALID_DATA;
+ }
+
+ if (event_data_length == 0) {
+ return PLDM_ERROR_INVALID_DATA;
+ }
+
+ if (event_class > PLDM_HEARTBEAT_TIMER_ELAPSED_EVENT &&
+ !(event_class >= 0xF0 && event_class <= 0xFE)) {
+ return PLDM_ERROR_INVALID_DATA;
+ }
+
+ if ((rc = pack_pldm_header(&header, &(msg->hdr))) > PLDM_SUCCESS) {
+ return rc;
+ }
+
+ struct pldm_platform_event_message_req *request =
+ (struct pldm_platform_event_message_req *)msg->payload;
+ request->format_version = format_version;
+ request->tid = tid;
+ request->event_class = event_class;
+ memcpy(request->event_data, event_data, event_data_length);
+
+ return PLDM_SUCCESS;
+}
+
+int decode_platform_event_message_resp(const struct pldm_msg *msg,
+ size_t payload_length,
+ uint8_t *completion_code,
+ uint8_t *platform_event_status)
+{
+ if (msg == NULL || completion_code == NULL ||
+ platform_event_status == NULL) {
+ return PLDM_ERROR_INVALID_DATA;
+ }
+
+ *completion_code = msg->payload[0];
+ if (PLDM_SUCCESS != *completion_code) {
+ return PLDM_SUCCESS;
+ }
+ if (payload_length != PLDM_PLATFORM_EVENT_MESSAGE_RESP_BYTES) {
+ return PLDM_ERROR_INVALID_LENGTH;
+ }
+
+ struct pldm_platform_event_message_resp *response =
+ (struct pldm_platform_event_message_resp *)msg->payload;
+ *platform_event_status = response->platform_event_status;
+
+ if (*platform_event_status > PLDM_EVENT_LOGGING_REJECTED) {
+ return PLDM_ERROR_INVALID_DATA;
+ }
+
+ 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/libpldm/platform.h b/libpldm/platform.h
index 1805f7d..b3b8eac 100644
--- a/libpldm/platform.h
+++ b/libpldm/platform.h
@@ -879,6 +879,39 @@
uint8_t platform_event_status,
struct pldm_msg *msg);
+/** @brief Encode PlatformEventMessage request data
+ * @param[in] instance_id - Message's instance id
+ * @param[in] format_version - Version of the event format
+ * @param[in] tid - Terminus ID for the terminus that originated the event
+ * message
+ * @param[in] event_class - The class of event being sent
+ * @param[in] event_data - the event data should be read from pldm msg
+ * @param[in] event_data_length - Length of the event data
+ * @param[out] msg - Request message
+ * @return pldm_completion_codes
+ * @note Caller is responsible for memory alloc and dealloc of param
+ * 'msg.payload'
+ */
+int encode_platform_event_message_req(uint8_t instance_id,
+ uint8_t format_version, uint8_t tid,
+ uint8_t event_class,
+ const uint8_t *event_data,
+ size_t event_data_length,
+ struct pldm_msg *msg);
+
+/** @brief Decode PlatformEventMessage 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] platform_event_status - Response status of the event message
+ * command
+ * @return pldm_completion_codes
+ */
+int decode_platform_event_message_resp(const struct pldm_msg *msg,
+ size_t payload_length,
+ uint8_t *completion_code,
+ uint8_t *platform_event_status);
+
/** @brief Decode sensorEventData response data
*
* @param[in] event_data - event data from the response message
diff --git a/libpldm/tests/libpldm_platform_test.cpp b/libpldm/tests/libpldm_platform_test.cpp
index 1958ca0..f8b9bc7 100644
--- a/libpldm/tests/libpldm_platform_test.cpp
+++ b/libpldm/tests/libpldm_platform_test.cpp
@@ -855,6 +855,113 @@
EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
}
+TEST(PlatformEventMessage, testGoodEncodeRequest)
+{
+ uint8_t formatVersion = 0x01;
+ uint8_t Tid = 0x03;
+ uint8_t eventClass = 0x00;
+ uint8_t eventData = 34;
+
+ std::array<uint8_t, hdrSize + PLDM_PLATFORM_EVENT_MESSAGE_MIN_REQ_BYTES +
+ sizeof(eventData)>
+ requestMsg{};
+
+ auto request = reinterpret_cast<pldm_msg*>(requestMsg.data());
+ auto rc = encode_platform_event_message_req(
+ 0, formatVersion, Tid, eventClass,
+ reinterpret_cast<uint8_t*>(&eventData), sizeof(eventData), request);
+
+ struct pldm_platform_event_message_req* req =
+ reinterpret_cast<struct pldm_platform_event_message_req*>(
+ request->payload);
+
+ EXPECT_EQ(rc, PLDM_SUCCESS);
+ EXPECT_EQ(formatVersion, req->format_version);
+ EXPECT_EQ(Tid, req->tid);
+ EXPECT_EQ(eventClass, req->event_class);
+ EXPECT_EQ(0, memcmp(&eventData, req->event_data, sizeof(eventData)));
+}
+
+TEST(PlatformEventMessage, testBadEncodeRequest)
+{
+ uint8_t Tid = 0x03;
+ uint8_t eventClass = 0x00;
+ uint8_t eventData = 34;
+ uint8_t formatVersion = 0x01;
+
+ std::array<uint8_t, hdrSize + PLDM_PLATFORM_EVENT_MESSAGE_MIN_REQ_BYTES +
+ sizeof(eventData)>
+ requestMsg{};
+ auto request = reinterpret_cast<pldm_msg*>(requestMsg.data());
+ auto rc = encode_platform_event_message_req(
+ 0, formatVersion, Tid, eventClass,
+ reinterpret_cast<uint8_t*>(&eventData), sizeof(eventData), nullptr);
+
+ EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
+ rc = encode_platform_event_message_req(
+ 0, 0, Tid, eventClass, reinterpret_cast<uint8_t*>(&eventData),
+ sizeof(eventData), request);
+ EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
+ rc = encode_platform_event_message_req(0, formatVersion, Tid, eventClass,
+ nullptr, 0, request);
+ EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
+}
+
+TEST(PlatformEventMessage, testGoodDecodeResponse)
+{
+ std::array<uint8_t, hdrSize + PLDM_PLATFORM_EVENT_MESSAGE_RESP_BYTES>
+ responseMsg{};
+
+ uint8_t completionCode = PLDM_SUCCESS;
+ uint8_t platformEventStatus = 0x01;
+
+ uint8_t retcompletionCode;
+ uint8_t retplatformEventStatus;
+
+ auto response = reinterpret_cast<pldm_msg*>(responseMsg.data());
+ struct pldm_platform_event_message_resp* resp =
+ reinterpret_cast<struct pldm_platform_event_message_resp*>(
+ response->payload);
+
+ resp->completion_code = completionCode;
+ resp->platform_event_status = platformEventStatus;
+
+ auto rc = decode_platform_event_message_resp(
+ response, responseMsg.size() - hdrSize, &retcompletionCode,
+ &retplatformEventStatus);
+
+ EXPECT_EQ(rc, PLDM_SUCCESS);
+ EXPECT_EQ(completionCode, retcompletionCode);
+ EXPECT_EQ(platformEventStatus, retplatformEventStatus);
+}
+
+TEST(PlatformEventMessage, testBadDecodeResponse)
+{
+ std::array<uint8_t, hdrSize + PLDM_PLATFORM_EVENT_MESSAGE_RESP_BYTES>
+ responseMsg{};
+
+ uint8_t completionCode = PLDM_SUCCESS;
+ uint8_t platformEventStatus = 0x01;
+
+ auto response = reinterpret_cast<pldm_msg*>(responseMsg.data());
+ struct pldm_platform_event_message_resp* resp =
+ reinterpret_cast<struct pldm_platform_event_message_resp*>(
+ response->payload);
+ resp->completion_code = completionCode;
+ resp->platform_event_status = platformEventStatus;
+
+ auto rc = decode_platform_event_message_resp(
+ nullptr, responseMsg.size() - hdrSize, nullptr, nullptr);
+
+ EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
+
+ rc = decode_platform_event_message_resp(
+ response, responseMsg.size() - hdrSize - 1, &completionCode,
+ &platformEventStatus);
+
+ EXPECT_EQ(rc, PLDM_ERROR_INVALID_LENGTH);
+}
+
TEST(PlatformEventMessage, testGoodSensorEventDataDecodeRequest)
{
std::array<uint8_t, PLDM_SENSOR_EVENT_SENSOR_OP_STATE_DATA_LENGTH +