OEM-IBM: Implement encode response & decode request API for
FileAckWithMetadata

Tested with latest SDK environment.

Change-Id: Ib62f1ea86f753e478ee7c820fda9bbb82bc2a745
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 262af47..6c045a9 100644
--- a/oem/ibm/libpldm/file_io.c
+++ b/oem/ibm/libpldm/file_io.c
@@ -839,3 +839,106 @@
 
 	return PLDM_SUCCESS;
 }
+
+int encode_file_ack_with_meta_data_req(
+    uint8_t instance_id, uint16_t file_type, uint32_t file_handle,
+    uint8_t file_status, 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_FILE_ACK_WITH_META_DATA;
+	uint8_t rc = pack_pldm_header(&header, &(msg->hdr));
+	if (rc != PLDM_SUCCESS) {
+		return rc;
+	}
+
+	struct pldm_file_ack_with_meta_data_req *req =
+	    (struct pldm_file_ack_with_meta_data_req *)msg->payload;
+	req->file_type = htole16(file_type);
+	req->file_handle = htole32(file_handle);
+	req->file_status = file_status;
+	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_file_ack_with_meta_data_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_FILE_ACK_WITH_META_DATA_RESP_BYTES) {
+		return PLDM_ERROR_INVALID_LENGTH;
+	}
+
+	struct pldm_file_ack_with_meta_data_resp *response =
+	    (struct pldm_file_ack_with_meta_data_resp *)msg->payload;
+	*completion_code = response->completion_code;
+
+	return PLDM_SUCCESS;
+}
+
+int decode_file_ack_with_meta_data_req(
+    const struct pldm_msg *msg, size_t payload_length, uint16_t *file_type,
+    uint32_t *file_handle, uint8_t *file_status, 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) {
+		return PLDM_ERROR_INVALID_DATA;
+	}
+
+	if (payload_length != PLDM_FILE_ACK_WITH_META_DATA_REQ_BYTES) {
+		return PLDM_ERROR_INVALID_LENGTH;
+	}
+
+	struct pldm_file_ack_with_meta_data_req *request =
+	    (struct pldm_file_ack_with_meta_data_req *)msg->payload;
+	*file_type = le16toh(request->file_type);
+	*file_handle = le32toh(request->file_handle);
+	*file_status = request->file_status;
+	*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_file_ack_with_meta_data_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_FILE_ACK_WITH_META_DATA;
+	uint8_t rc = pack_pldm_header(&header, &(msg->hdr));
+	if (rc != PLDM_SUCCESS) {
+		return rc;
+	}
+
+	struct pldm_file_ack_with_meta_data_resp *response =
+	    (struct pldm_file_ack_with_meta_data_resp *)msg->payload;
+	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 07db4ed..72892da 100644
--- a/oem/ibm/libpldm/file_io.h
+++ b/oem/ibm/libpldm/file_io.h
@@ -24,6 +24,8 @@
 	PLDM_READ_FILE_BY_TYPE = 0xB,
 	PLDM_WRITE_FILE_BY_TYPE = 0xC,
 	PLDM_FILE_ACK = 0xD,
+	PLDM_NEW_FILE_AVAILABLE_WITH_META_DATA = 0xE,
+	PLDM_FILE_ACK_WITH_META_DATA = 0xF,
 };
 
 /** @brief PLDM Command specific codes
@@ -75,6 +77,8 @@
 #define PLDM_RW_FILE_BY_TYPE_RESP_BYTES 5
 #define PLDM_FILE_ACK_REQ_BYTES 7
 #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
 
 /** @struct pldm_read_write_file_memory_req
  *
@@ -718,6 +722,89 @@
 int decode_file_ack_resp(const struct pldm_msg *msg, size_t payload_length,
 			 uint8_t *completion_code);
 
+/* FileAckWithMetadata */
+
+/** @struct pldm_file_ack_with_meta_data_req
+ *
+ *  Structure representing FileAckWithMetadata request
+ */
+struct pldm_file_ack_with_meta_data_req {
+	uint16_t file_type;	   //!< Type of file
+	uint32_t file_handle;	   //!< Handle to file
+	uint8_t file_status;	   //!< Status of file processing
+	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_file_ack_with_meta_data_resp
+ *
+ *  Structure representing FileAckWithMetadata response
+ */
+struct pldm_file_ack_with_meta_data_resp {
+	uint8_t completion_code; //!< Completion code
+} __attribute__((packed));
+
+/** @brief Encode FileAckWithMetadata 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] file_status - Status of file processing
+ *  @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_file_ack_with_meta_data_req(
+    uint8_t instance_id, uint16_t file_type, uint32_t file_handle,
+    uint8_t file_status, 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 FileAckWithMetadata command 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_file_ack_with_meta_data_resp(const struct pldm_msg *msg,
+					size_t payload_length,
+					uint8_t *completion_code);
+
+/** @brief Decode FileAckWithMetadata 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] file_status - Status of file processing
+ * @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_file_ack_with_meta_data_req(
+    const struct pldm_msg *msg, size_t payload_length, uint16_t *file_type,
+    uint32_t *file_handle, uint8_t *file_status, 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 message for FileAckWithMetadata
+ *
+ * @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
+ */
+int encode_file_ack_with_meta_data_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 94b7e1d..fcdbbf0 100644
--- a/oem/ibm/test/libpldm_fileio_test.cpp
+++ b/oem/ibm/test/libpldm_fileio_test.cpp
@@ -1628,3 +1628,247 @@
 
     ASSERT_EQ(rc, PLDM_ERROR_INVALID_DATA);
 }
+
+TEST(FileAckWithMetadata, testGoodEncodeResponse)
+{
+    std::array<uint8_t,
+               sizeof(pldm_msg_hdr) + PLDM_FILE_ACK_WITH_META_DATA_RESP_BYTES>
+        responseMsg{};
+
+    uint8_t completionCode = 0x0;
+
+    pldm_msg* response = reinterpret_cast<pldm_msg*>(responseMsg.data());
+
+    auto rc = encode_file_ack_with_meta_data_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_FILE_ACK_WITH_META_DATA);
+    ASSERT_EQ(
+        0, memcmp(response->payload, &completionCode, sizeof(completionCode)));
+}
+
+TEST(FileAckWithMetadata, testBadEncodeResponse)
+{
+    std::array<uint8_t,
+               sizeof(pldm_msg_hdr) + PLDM_FILE_ACK_WITH_META_DATA_RESP_BYTES>
+        responseMsg{};
+    pldm_msg* response = reinterpret_cast<pldm_msg*>(responseMsg.data());
+
+    // completion code is PLDM_ERROR
+    auto rc = encode_file_ack_with_meta_data_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_FILE_ACK_WITH_META_DATA);
+    ASSERT_EQ(response->payload[0], PLDM_ERROR);
+
+    // response is NULL pointer
+    rc = encode_file_ack_with_meta_data_resp(0, PLDM_SUCCESS, NULL);
+
+    ASSERT_EQ(rc, PLDM_ERROR_INVALID_DATA);
+}
+
+TEST(FileAckWithMetadata, testGoodDecodeResponse)
+{
+    std::array<uint8_t,
+               PLDM_FILE_ACK_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_file_ack_with_meta_data_resp(responsePtr, payload_length,
+                                                  &retCompletionCode);
+
+    ASSERT_EQ(rc, PLDM_SUCCESS);
+    ASSERT_EQ(completionCode, retCompletionCode);
+}
+
+TEST(FileAckWithMetadata, testBadDecodeResponse)
+{
+    uint8_t completionCode = 0;
+
+    // Request payload message is missing
+    auto rc = decode_file_ack_with_meta_data_resp(NULL, 0, &completionCode);
+    ASSERT_EQ(rc, PLDM_ERROR_INVALID_DATA);
+
+    std::array<uint8_t,
+               PLDM_FILE_ACK_WITH_META_DATA_RESP_BYTES + sizeof(pldm_msg_hdr)>
+        responseMsg{};
+
+    auto responsePtr = reinterpret_cast<pldm_msg*>(responseMsg.data());
+
+    // Payload length is invalid
+    rc = decode_file_ack_with_meta_data_resp(responsePtr, 0, &completionCode);
+    ASSERT_EQ(rc, PLDM_ERROR_INVALID_LENGTH);
+}
+
+TEST(FileAckWithMetadata, testGoodEncodeRequest)
+{
+    std::array<uint8_t,
+               sizeof(pldm_msg_hdr) + PLDM_FILE_ACK_WITH_META_DATA_REQ_BYTES>
+        requestMsg{};
+
+    uint16_t fileType = 0xFFFF;
+    uint32_t fileHandle = 0x12345678;
+    uint8_t fileStatus = 0xFF;
+    uint32_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);
+    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_file_ack_with_meta_data_req(
+        0, fileType, fileHandle, fileStatus, 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_FILE_ACK_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),
+                  &fileStatus, sizeof(fileStatus)));
+    ASSERT_EQ(0, memcmp(request->payload + sizeof(fileTypeLe) +
+                            sizeof(fileHandleLe) + sizeof(fileStatus),
+                        &fileMetaData1Le, sizeof(fileMetaData1Le)));
+    ASSERT_EQ(0, memcmp(request->payload + sizeof(fileTypeLe) +
+                            sizeof(fileHandleLe) + sizeof(fileStatus) +
+                            sizeof(fileMetaData1Le),
+                        &fileMetaData2Le, sizeof(fileMetaData2Le)));
+
+    ASSERT_EQ(0, memcmp(request->payload + sizeof(fileTypeLe) +
+                            sizeof(fileHandleLe) + sizeof(fileStatus) +
+                            sizeof(fileMetaData1Le) + sizeof(fileMetaData2Le),
+                        &fileMetaData3Le, sizeof(fileMetaData3Le)));
+
+    ASSERT_EQ(0, memcmp(request->payload + sizeof(fileTypeLe) +
+                            sizeof(fileHandleLe) + sizeof(fileStatus) +
+                            sizeof(fileMetaData1Le) + sizeof(fileMetaData2Le) +
+                            sizeof(fileMetaData3Le),
+                        &fileMetaData4Le, sizeof(fileMetaData4Le)));
+}
+
+TEST(FileAckWithMetadata, testBadEncodeRequest)
+{
+    uint8_t fileType = 0xFF;
+    uint32_t fileHandle = 0;
+    uint8_t fileStatus = 0;
+    uint32_t fileMetaData1 = 0;
+    uint32_t fileMetaData2 = 0;
+    uint32_t fileMetaData3 = 0;
+    uint32_t fileMetaData4 = 0;
+
+    // request is NULL pointer
+    auto rc = encode_file_ack_with_meta_data_req(
+        0, fileType, fileHandle, fileStatus, fileMetaData1, fileMetaData2,
+        fileMetaData3, fileMetaData4, nullptr);
+
+    ASSERT_EQ(rc, PLDM_ERROR_INVALID_DATA);
+}
+
+TEST(FileAckWithMetadata, testGoodDecodeRequest)
+{
+    std::array<uint8_t,
+               sizeof(pldm_msg_hdr) + PLDM_FILE_ACK_WITH_META_DATA_REQ_BYTES>
+        requestMsg{};
+
+    auto requestPtr = reinterpret_cast<pldm_msg*>(requestMsg.data());
+    size_t payload_length = requestMsg.size() - sizeof(pldm_msg_hdr);
+    auto request = reinterpret_cast<pldm_file_ack_with_meta_data_req*>(
+        requestPtr->payload);
+
+    uint16_t fileType = 0xFFFF;
+    uint32_t fileHandle = 0x12345678;
+    uint8_t fileStatus = 0xFF;
+    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->file_status = fileStatus;
+    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;
+    uint8_t retFileStatus = 0;
+    uint32_t retFileMetaData1 = 0;
+    uint32_t retFileMetaData2 = 0;
+    uint32_t retFileMetaData3 = 0;
+    uint32_t retFileMetaData4 = 0;
+
+    auto rc = decode_file_ack_with_meta_data_req(
+        requestPtr, payload_length, &retFileType, &retFileHandle,
+        &retFileStatus, &retFileMetaData1, &retFileMetaData2, &retFileMetaData3,
+        &retFileMetaData4);
+    ASSERT_EQ(rc, PLDM_SUCCESS);
+    ASSERT_EQ(fileType, retFileType);
+    ASSERT_EQ(fileHandle, retFileHandle);
+    ASSERT_EQ(fileStatus, retFileStatus);
+    ASSERT_EQ(fileMetaData1, retFileMetaData1);
+    ASSERT_EQ(fileMetaData2, retFileMetaData2);
+    ASSERT_EQ(fileMetaData3, retFileMetaData3);
+    ASSERT_EQ(fileMetaData4, retFileMetaData4);
+}
+
+TEST(FileAckWithMetadata, testBadDecodeRequest)
+{
+    uint16_t fileType = 0;
+    uint32_t fileHandle = 0;
+    uint8_t fileStatus = 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_file_ack_with_meta_data_req(
+        NULL, 0, &fileType, &fileHandle, &fileStatus, &fileMetaData1,
+        &fileMetaData2, &fileMetaData3, &fileMetaData4);
+
+    ASSERT_EQ(rc, PLDM_ERROR_INVALID_DATA);
+
+    std::array<uint8_t,
+               PLDM_FILE_ACK_WITH_META_DATA_REQ_BYTES + sizeof(pldm_msg_hdr)>
+        requestMsg{};
+
+    auto requestPtr = reinterpret_cast<pldm_msg*>(requestMsg.data());
+
+    // Payload length is invalid
+    rc = decode_file_ack_with_meta_data_req(
+        requestPtr, 0, &fileType, &fileHandle, &fileStatus, &fileMetaData1,
+        &fileMetaData2, &fileMetaData3, &fileMetaData4);
+    ASSERT_EQ(rc, PLDM_ERROR_INVALID_LENGTH);
+}