OEM-IBM: Implement encode & decode APIs for NewFileAvailableWithMetaData.
This commit implements requester and responder flow for
NewFileAvailableWithMetaData.
Tested: Unit tests passed.
Change-Id: I3dbcdd354a9e89529125125333aa413c733daf3c
Signed-off-by: Sridevi Ramesh <sridevra@in.ibm.com>
diff --git a/oem/ibm/libpldm/file_io.c b/oem/ibm/libpldm/file_io.c
index 6c045a9..fae3a9c 100644
--- a/oem/ibm/libpldm/file_io.c
+++ b/oem/ibm/libpldm/file_io.c
@@ -942,3 +942,115 @@
return PLDM_SUCCESS;
}
+
+int encode_new_file_with_metadata_req(
+ uint8_t instance_id, uint16_t file_type, uint32_t file_handle,
+ uint64_t length, uint32_t file_meta_data_1, uint32_t file_meta_data_2,
+ uint32_t file_meta_data_3, uint32_t file_meta_data_4, struct pldm_msg *msg)
+{
+ 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_OEM;
+ header.command = PLDM_NEW_FILE_AVAILABLE_WITH_META_DATA;
+ uint8_t rc = pack_pldm_header(&header, &(msg->hdr));
+ if (rc != PLDM_SUCCESS) {
+ return rc;
+ }
+
+ struct pldm_new_file_with_metadata_req *req =
+ (struct pldm_new_file_with_metadata_req *)msg->payload;
+ req->file_type = htole16(file_type);
+ req->file_handle = htole32(file_handle);
+ req->length = htole64(length);
+ req->file_meta_data_1 = htole32(file_meta_data_1);
+ req->file_meta_data_2 = htole32(file_meta_data_2);
+ req->file_meta_data_3 = htole32(file_meta_data_3);
+ req->file_meta_data_4 = htole32(file_meta_data_4);
+
+ return PLDM_SUCCESS;
+}
+
+int decode_new_file_with_metadata_resp(const struct pldm_msg *msg,
+ size_t payload_length,
+ uint8_t *completion_code)
+{
+ if (msg == NULL || completion_code == NULL) {
+ return PLDM_ERROR_INVALID_DATA;
+ }
+
+ if (payload_length !=
+ PLDM_NEW_FILE_AVAILABLE_WITH_META_DATA_RESP_BYTES) {
+ return PLDM_ERROR_INVALID_LENGTH;
+ }
+
+ struct pldm_new_file_with_metadata_resp *response =
+ (struct pldm_new_file_with_metadata_resp *)msg->payload;
+
+ *completion_code = msg->payload[0];
+ if (*completion_code == PLDM_SUCCESS) {
+ *completion_code = response->completion_code;
+ }
+ return PLDM_SUCCESS;
+}
+
+int decode_new_file_with_metadata_req(
+ const struct pldm_msg *msg, size_t payload_length, uint16_t *file_type,
+ uint32_t *file_handle, uint64_t *length, uint32_t *file_meta_data_1,
+ uint32_t *file_meta_data_2, uint32_t *file_meta_data_3,
+ uint32_t *file_meta_data_4)
+{
+ if (msg == NULL || file_type == NULL || file_handle == NULL ||
+ length == NULL) {
+ return PLDM_ERROR_INVALID_DATA;
+ }
+
+ if (payload_length !=
+ PLDM_NEW_FILE_AVAILABLE_WITH_META_DATA_REQ_BYTES) {
+ return PLDM_ERROR_INVALID_LENGTH;
+ }
+
+ struct pldm_new_file_with_metadata_req *request =
+ (struct pldm_new_file_with_metadata_req *)msg->payload;
+ *file_type = le16toh(request->file_type);
+ *file_handle = le32toh(request->file_handle);
+ *length = le64toh(request->length);
+ *file_meta_data_1 = le32toh(request->file_meta_data_1);
+ *file_meta_data_2 = le32toh(request->file_meta_data_2);
+ *file_meta_data_3 = le32toh(request->file_meta_data_3);
+ *file_meta_data_4 = le32toh(request->file_meta_data_4);
+
+ return PLDM_SUCCESS;
+}
+
+int encode_new_file_with_metadata_resp(uint8_t instance_id,
+ uint8_t completion_code,
+ struct pldm_msg *msg)
+{
+ if (msg == NULL) {
+ return PLDM_ERROR_INVALID_DATA;
+ }
+
+ struct pldm_header_info header = {0};
+ header.msg_type = PLDM_RESPONSE;
+ header.instance = instance_id;
+ header.pldm_type = PLDM_OEM;
+ header.command = PLDM_NEW_FILE_AVAILABLE_WITH_META_DATA;
+ uint8_t rc = pack_pldm_header(&header, &(msg->hdr));
+ if (rc != PLDM_SUCCESS) {
+ return rc;
+ }
+
+ struct pldm_new_file_with_metadata_resp *response =
+ (struct pldm_new_file_with_metadata_resp *)msg->payload;
+
+ if (response->completion_code == PLDM_SUCCESS) {
+ response->completion_code = completion_code;
+ }
+
+ return PLDM_SUCCESS;
+}
diff --git a/oem/ibm/libpldm/file_io.h b/oem/ibm/libpldm/file_io.h
index 72892da..e5765a6 100644
--- a/oem/ibm/libpldm/file_io.h
+++ b/oem/ibm/libpldm/file_io.h
@@ -79,6 +79,8 @@
#define PLDM_FILE_ACK_RESP_BYTES 1
#define PLDM_FILE_ACK_WITH_META_DATA_REQ_BYTES 23
#define PLDM_FILE_ACK_WITH_META_DATA_RESP_BYTES 1
+#define PLDM_NEW_FILE_AVAILABLE_WITH_META_DATA_REQ_BYTES 30
+#define PLDM_NEW_FILE_AVAILABLE_WITH_META_DATA_RESP_BYTES 1
/** @struct pldm_read_write_file_memory_req
*
@@ -805,6 +807,91 @@
uint8_t completion_code,
struct pldm_msg *msg);
+/* NewFileAvailableWithMetaData */
+
+/** @struct pldm_new_file_with_metadata_req
+ *
+ * Structure representing NewFileAvailableWithMetaData request
+ */
+
+struct pldm_new_file_with_metadata_req {
+ uint16_t file_type; //!< Type of file
+ uint32_t file_handle; //!< Handle to file
+ uint64_t length; //!< Number of bytes in new file
+ uint32_t file_meta_data_1; //!< Meta data specific to file type 1
+ uint32_t file_meta_data_2; //!< Meta data specific to file type 2
+ uint32_t file_meta_data_3; //!< Meta data specific to file type 3
+ uint32_t file_meta_data_4; //!< Meta data specific to file type 4
+} __attribute__((packed));
+
+/** @struct pldm_new_file_with_metadata_resp
+ *
+ * Structure representing NewFileAvailableWithMetaData response data
+ */
+struct pldm_new_file_with_metadata_resp {
+ uint8_t completion_code; //!< Completion code
+} __attribute__((packed));
+
+/** @brief Encode NewFileAvailableWithMetaData request data
+ *
+ * @param[in] instance_id - Message's instance id
+ * @param[in] file_type - Type of the file
+ * @param[in] file_handle - A handle to the file
+ * @param[in] length - Number of bytes in new file
+ * @param[in] file_meta_data_1 - Meta data specific to file type 1
+ * @param[in] file_meta_data_2 - Meta data specific to file type 2
+ * @param[in] file_meta_data_3 - Meta data specific to file type 3
+ * @param[in] file_meta_data_4 - Meta data specific to file type 4
+ * @param[out] msg - Message will be written to this
+ * @return pldm_completion_codes
+ */
+int encode_new_file_with_metadata_req(
+ uint8_t instance_id, uint16_t file_type, uint32_t file_handle,
+ uint64_t length, uint32_t file_meta_data_1, uint32_t file_meta_data_2,
+ uint32_t file_meta_data_3, uint32_t file_meta_data_4, struct pldm_msg *msg);
+
+/** @brief Decode NewFileAvailableWithMetaData response data
+ *
+ * @param[in] msg - pointer to PLDM response message
+ * @param[in] payload_length - Length of response payload
+ * @param[out] completion_code - PLDM completion code
+ * @return pldm_completion_codes
+ */
+int decode_new_file_with_metadata_resp(const struct pldm_msg *msg,
+ size_t payload_length,
+ uint8_t *completion_code);
+
+/** @brief Decode NewFileAvailableWithMetaData request data
+ *
+ * @param[in] msg - Pointer to PLDM request message
+ * @param[in] payload_length - Length of request payload
+ * @param[out] file_type - Type of the file
+ * @param[out] file_handle - A handle to the file
+ * @param[out] length - Number of bytes in new file
+ * @param[out] file_meta_data_1 - Meta data specific to file type 1
+ * @param[out] file_meta_data_2 - Meta data specific to file type 2
+ * @param[out] file_meta_data_3 - Meta data specific to file type 3
+ * @param[out] file_meta_data_4 - Meta data specific to file type 4
+ * @return pldm_completion_codes
+ */
+int decode_new_file_with_metadata_req(
+ const struct pldm_msg *msg, size_t payload_length, uint16_t *file_type,
+ uint32_t *file_handle, uint64_t *length, uint32_t *file_meta_data_1,
+ uint32_t *file_meta_data_2, uint32_t *file_meta_data_3,
+ uint32_t *file_meta_data_4);
+
+/** @brief Create a PLDM response for NewFileAvailableWithMetaData
+ *
+ * @param[in] instance_id - Message's instance id
+ * @param[in] completion_code - PLDM completion code
+ * @param[in,out] msg - Message will be written to this
+ * @return pldm_completion_codes
+ * @note Caller is responsible for memory alloc and dealloc of param 'msg'
+ */
+int encode_new_file_with_metadata_resp(uint8_t instance_id,
+ uint8_t completion_code,
+ struct pldm_msg *msg);
+
#ifdef __cplusplus
}
#endif
diff --git a/oem/ibm/test/libpldm_fileio_test.cpp b/oem/ibm/test/libpldm_fileio_test.cpp
index fcdbbf0..1693b9e 100644
--- a/oem/ibm/test/libpldm_fileio_test.cpp
+++ b/oem/ibm/test/libpldm_fileio_test.cpp
@@ -1872,3 +1872,253 @@
&fileMetaData2, &fileMetaData3, &fileMetaData4);
ASSERT_EQ(rc, PLDM_ERROR_INVALID_LENGTH);
}
+
+TEST(NewFileAvailableWithMetaData, testGoodEncodeResponse)
+{
+ std::array<uint8_t, sizeof(pldm_msg_hdr) +
+ PLDM_NEW_FILE_AVAILABLE_WITH_META_DATA_RESP_BYTES>
+ responseMsg{};
+
+ uint8_t completionCode = 0x0;
+
+ pldm_msg* response = reinterpret_cast<pldm_msg*>(responseMsg.data());
+
+ auto rc = encode_new_file_with_metadata_resp(0, completionCode, response);
+
+ ASSERT_EQ(rc, PLDM_SUCCESS);
+ ASSERT_EQ(response->hdr.request, PLDM_RESPONSE);
+ ASSERT_EQ(response->hdr.instance_id, 0);
+ ASSERT_EQ(response->hdr.type, PLDM_OEM);
+ ASSERT_EQ(response->hdr.command, PLDM_NEW_FILE_AVAILABLE_WITH_META_DATA);
+ ASSERT_EQ(
+ 0, memcmp(response->payload, &completionCode, sizeof(completionCode)));
+}
+
+TEST(NewFileAvailableWithMetaData, testBadEncodeResponse)
+{
+ std::array<uint8_t, sizeof(pldm_msg_hdr) +
+ PLDM_NEW_FILE_AVAILABLE_WITH_META_DATA_RESP_BYTES>
+ responseMsg{};
+ pldm_msg* response = reinterpret_cast<pldm_msg*>(responseMsg.data());
+
+ // completion code is PLDM_ERROR
+ auto rc = encode_new_file_with_metadata_resp(0, PLDM_ERROR, response);
+
+ ASSERT_EQ(rc, PLDM_SUCCESS);
+ ASSERT_EQ(response->hdr.request, PLDM_RESPONSE);
+ ASSERT_EQ(response->hdr.instance_id, 0);
+ ASSERT_EQ(response->hdr.type, PLDM_OEM);
+ ASSERT_EQ(response->hdr.command, PLDM_NEW_FILE_AVAILABLE_WITH_META_DATA);
+ ASSERT_EQ(response->payload[0], PLDM_ERROR);
+
+ // response is NULL pointer
+ rc = encode_new_file_resp(0, PLDM_SUCCESS, NULL);
+
+ ASSERT_EQ(rc, PLDM_ERROR_INVALID_DATA);
+}
+
+TEST(NewFileAvailableWithMetaData, testGoodDecodeResponse)
+{
+ std::array<uint8_t, PLDM_NEW_FILE_AVAILABLE_WITH_META_DATA_RESP_BYTES +
+ sizeof(pldm_msg_hdr)>
+ responseMsg{};
+
+ auto responsePtr = reinterpret_cast<pldm_msg*>(responseMsg.data());
+ size_t payload_length = responseMsg.size() - sizeof(pldm_msg_hdr);
+ auto response = reinterpret_cast<pldm_file_ack_with_meta_data_resp*>(
+ responsePtr->payload);
+
+ // Random value for completion code
+ uint8_t completionCode = 0x0;
+
+ response->completion_code = completionCode;
+
+ uint8_t retCompletionCode = PLDM_SUCCESS;
+
+ // Invoke decode the read/write file response
+ auto rc = decode_new_file_with_metadata_resp(responsePtr, payload_length,
+ &retCompletionCode);
+
+ ASSERT_EQ(rc, PLDM_SUCCESS);
+ ASSERT_EQ(completionCode, retCompletionCode);
+}
+
+TEST(NewFileAvailableWithMetaData, testBadDecodeResponse)
+{
+ uint8_t completionCode = 0;
+
+ // Request payload message is missing
+ auto rc = decode_new_file_with_metadata_resp(NULL, 0, &completionCode);
+ ASSERT_EQ(rc, PLDM_ERROR_INVALID_DATA);
+
+ std::array<uint8_t, PLDM_NEW_FILE_AVAILABLE_WITH_META_DATA_RESP_BYTES +
+ sizeof(pldm_msg_hdr)>
+ responseMsg{};
+
+ auto responsePtr = reinterpret_cast<pldm_msg*>(responseMsg.data());
+
+ // Payload length is invalid
+ rc = decode_new_file_with_metadata_resp(responsePtr, 0, &completionCode);
+ ASSERT_EQ(rc, PLDM_ERROR_INVALID_LENGTH);
+}
+
+TEST(NewFileAvailableWithMetaData, testGoodEncodeRequest)
+{
+ std::array<uint8_t, sizeof(pldm_msg_hdr) +
+ PLDM_NEW_FILE_AVAILABLE_WITH_META_DATA_REQ_BYTES>
+ requestMsg{};
+
+ uint16_t fileType = 0xFFFF;
+ uint32_t fileHandle = 0x12345678;
+ uint32_t length = 0x13245768;
+ uint64_t fileMetaData1 = 0xFFFFFFFF;
+ uint32_t fileMetaData2 = 0xFFFFFFFF;
+ uint32_t fileMetaData3 = 0xFFFFFFFF;
+ uint32_t fileMetaData4 = 0xFFFFFFFF;
+
+ uint16_t fileTypeLe = htole16(fileType);
+ uint32_t fileHandleLe = htole32(fileHandle);
+ uint64_t lengthLe = htole64(length);
+ uint32_t fileMetaData1Le = htole32(fileMetaData1);
+ uint32_t fileMetaData2Le = htole32(fileMetaData2);
+ uint32_t fileMetaData3Le = htole32(fileMetaData3);
+ uint32_t fileMetaData4Le = htole32(fileMetaData4);
+
+ pldm_msg* request = reinterpret_cast<pldm_msg*>(requestMsg.data());
+
+ auto rc = encode_new_file_with_metadata_req(
+ 0, fileType, fileHandle, length, fileMetaData1, fileMetaData2,
+ fileMetaData3, fileMetaData4, request);
+
+ ASSERT_EQ(rc, PLDM_SUCCESS);
+ ASSERT_EQ(request->hdr.request, PLDM_REQUEST);
+ ASSERT_EQ(request->hdr.instance_id, 0);
+ ASSERT_EQ(request->hdr.type, PLDM_OEM);
+ ASSERT_EQ(request->hdr.command, PLDM_NEW_FILE_AVAILABLE_WITH_META_DATA);
+ ASSERT_EQ(0, memcmp(request->payload, &fileTypeLe, sizeof(fileTypeLe)));
+ ASSERT_EQ(0, memcmp(request->payload + sizeof(fileTypeLe), &fileHandleLe,
+ sizeof(fileHandleLe)));
+ ASSERT_EQ(
+ 0, memcmp(request->payload + sizeof(fileTypeLe) + sizeof(fileHandleLe),
+ &lengthLe, sizeof(lengthLe)));
+
+ ASSERT_EQ(0, memcmp(request->payload + sizeof(fileTypeLe) +
+ sizeof(fileHandleLe) + sizeof(lengthLe),
+ &fileMetaData1Le, sizeof(fileMetaData1Le)));
+
+ ASSERT_EQ(0, memcmp(request->payload + sizeof(fileTypeLe) +
+ sizeof(fileHandleLe) + sizeof(lengthLe) +
+ sizeof(fileMetaData1Le),
+ &fileMetaData2Le, sizeof(fileMetaData2Le)));
+
+ ASSERT_EQ(0, memcmp(request->payload + sizeof(fileTypeLe) +
+ sizeof(fileHandleLe) + sizeof(lengthLe) +
+ sizeof(fileMetaData1Le) + sizeof(fileMetaData2Le),
+ &fileMetaData3Le, sizeof(fileMetaData3Le)));
+
+ ASSERT_EQ(0, memcmp(request->payload + sizeof(fileTypeLe) +
+ sizeof(fileHandleLe) + sizeof(lengthLe) +
+ sizeof(fileMetaData1Le) + sizeof(fileMetaData2Le) +
+ sizeof(fileMetaData3Le),
+ &fileMetaData4Le, sizeof(fileMetaData4Le)));
+}
+
+TEST(NewFileAvailableWithMetaData, testBadEncodeRequest)
+{
+ uint8_t fileType = 0xFF;
+ uint32_t fileHandle = 0;
+ uint32_t length = 0;
+ uint32_t fileMetaData1 = 0;
+ uint32_t fileMetaData2 = 0;
+ uint32_t fileMetaData3 = 0;
+ uint32_t fileMetaData4 = 0;
+
+ // request is NULL pointer
+ auto rc = encode_new_file_with_metadata_req(
+ 0, fileType, fileHandle, length, fileMetaData1, fileMetaData2,
+ fileMetaData3, fileMetaData4, NULL);
+
+ ASSERT_EQ(rc, PLDM_ERROR_INVALID_DATA);
+}
+
+TEST(NewFileAvailableWithMetaData, testGoodDecodeRequest)
+{
+ std::array<uint8_t, PLDM_NEW_FILE_AVAILABLE_WITH_META_DATA_REQ_BYTES +
+ sizeof(pldm_msg_hdr)>
+ requestMsg{};
+
+ auto requestPtr = reinterpret_cast<pldm_msg*>(requestMsg.data());
+ size_t payload_length = requestMsg.size() - sizeof(pldm_msg_hdr);
+ auto request =
+ reinterpret_cast<pldm_new_file_with_metadata_req*>(requestPtr->payload);
+
+ // Random value for fileHandle and length
+ uint16_t fileType = 0xFFFF;
+ uint32_t fileHandle = 0x12345678;
+ uint64_t length = 0x13245768;
+ uint32_t fileMetaData1 = 0x12345678;
+ uint32_t fileMetaData2 = 0x87654321;
+ uint32_t fileMetaData3 = 0x22121117;
+ uint32_t fileMetaData4 = 0x12334345;
+
+ request->file_type = htole16(fileType);
+ request->file_handle = htole32(fileHandle);
+ request->length = htole64(length);
+ request->file_meta_data_1 = htole32(fileMetaData1);
+ request->file_meta_data_2 = htole32(fileMetaData2);
+ request->file_meta_data_3 = htole32(fileMetaData3);
+ request->file_meta_data_4 = htole32(fileMetaData4);
+
+ uint16_t retFileType = 0xFFFF;
+ uint32_t retFileHandle = 0;
+ uint64_t retLength = 0;
+ uint32_t retFileMetaData1 = 0;
+ uint32_t retFileMetaData2 = 0;
+ uint32_t retFileMetaData3 = 0;
+ uint32_t retFileMetaData4 = 0;
+
+ // Invoke decode the read file request
+ auto rc = decode_new_file_with_metadata_req(
+ requestPtr, payload_length, &retFileType, &retFileHandle, &retLength,
+ &retFileMetaData1, &retFileMetaData2, &retFileMetaData3,
+ &retFileMetaData4);
+
+ ASSERT_EQ(rc, PLDM_SUCCESS);
+ ASSERT_EQ(fileType, retFileType);
+ ASSERT_EQ(fileHandle, retFileHandle);
+ ASSERT_EQ(length, retLength);
+ ASSERT_EQ(fileMetaData1, retFileMetaData1);
+ ASSERT_EQ(fileMetaData2, retFileMetaData2);
+ ASSERT_EQ(fileMetaData3, retFileMetaData3);
+ ASSERT_EQ(fileMetaData4, retFileMetaData4);
+}
+
+TEST(NewFileAvailableWithMetaData, testBadDecodeRequest)
+{
+ uint16_t fileType = 0;
+ uint32_t fileHandle = 0;
+ uint64_t length = 0;
+ uint32_t fileMetaData1 = 0;
+ uint32_t fileMetaData2 = 0;
+ uint32_t fileMetaData3 = 0;
+ uint32_t fileMetaData4 = 0;
+
+ // Request payload message is missing
+ auto rc = decode_new_file_with_metadata_req(
+ NULL, 0, &fileType, &fileHandle, &length, &fileMetaData1,
+ &fileMetaData2, &fileMetaData3, &fileMetaData4);
+
+ ASSERT_EQ(rc, PLDM_ERROR_INVALID_DATA);
+
+ std::array<uint8_t, PLDM_NEW_FILE_AVAILABLE_WITH_META_DATA_REQ_BYTES +
+ sizeof(pldm_msg_hdr)>
+ requestMsg{};
+
+ auto requestPtr = reinterpret_cast<pldm_msg*>(requestMsg.data());
+
+ // Payload length is invalid
+ rc = decode_new_file_with_metadata_req(
+ requestPtr, 0, &fileType, &fileHandle, &length, &fileMetaData1,
+ &fileMetaData2, &fileMetaData3, &fileMetaData4);
+ ASSERT_EQ(rc, PLDM_ERROR_INVALID_LENGTH);
+}