Implement decode request/encode response for GetFileTable

Change-Id: Iad27a1168da23cb1ae29c8e2fdab3ab8bee6dc0a
Signed-off-by: Tom Joseph <tomjoseph@in.ibm.com>
diff --git a/libpldm/file_io.c b/libpldm/file_io.c
index ca33cef..f32892c 100644
--- a/libpldm/file_io.c
+++ b/libpldm/file_io.c
@@ -52,3 +52,56 @@
 
 	return PLDM_SUCCESS;
 }
+
+int decode_get_file_table_req(const uint8_t *msg, size_t payload_length,
+			      uint32_t *transfer_handle,
+			      uint8_t *transfer_opflag, uint8_t *table_type)
+{
+	if (msg == NULL || transfer_handle == NULL || transfer_opflag == NULL ||
+	    table_type == NULL) {
+		return PLDM_ERROR_INVALID_DATA;
+	}
+
+	if (payload_length != PLDM_GET_FILE_TABLE_REQ_BYTES) {
+		return PLDM_ERROR_INVALID_LENGTH;
+	}
+
+	struct pldm_get_file_table_req *request =
+	    (struct pldm_get_file_table_req *)msg;
+
+	*transfer_handle = le32toh(request->transfer_handle);
+	*transfer_opflag = request->operation_flag;
+	*table_type = request->table_type;
+
+	return PLDM_SUCCESS;
+}
+
+int encode_get_file_table_resp(uint8_t instance_id, uint8_t completion_code,
+			       uint32_t next_transfer_handle,
+			       uint8_t transfer_flag, const uint8_t *table_data,
+			       size_t table_size, struct pldm_msg *msg)
+{
+	struct pldm_header_info header = {0};
+	int rc = PLDM_SUCCESS;
+
+	header.msg_type = PLDM_RESPONSE;
+	header.instance = instance_id;
+	header.pldm_type = PLDM_IBM_OEM_TYPE;
+	header.command = PLDM_GET_FILE_TABLE;
+
+	if ((rc = pack_pldm_header(&header, &(msg->hdr))) > PLDM_SUCCESS) {
+		return rc;
+	}
+
+	struct pldm_get_file_table_resp *response =
+	    (struct pldm_get_file_table_resp *)msg->payload;
+	response->completion_code = completion_code;
+
+	if (completion_code == PLDM_SUCCESS) {
+		response->next_transfer_handle = htole32(next_transfer_handle);
+		response->transfer_flag = transfer_flag;
+		memcpy(response->table_data, table_data, table_size);
+	}
+
+	return PLDM_SUCCESS;
+}
diff --git a/libpldm/file_io.h b/libpldm/file_io.h
index 345b363..6354f81 100644
--- a/libpldm/file_io.h
+++ b/libpldm/file_io.h
@@ -15,6 +15,7 @@
 /** @brief PLDM Commands in IBM OEM type
  */
 enum pldm_fileio_commands {
+	PLDM_GET_FILE_TABLE = 0x1,
 	PLDM_READ_FILE_INTO_MEMORY = 0x6,
 	PLDM_WRITE_FILE_FROM_MEMORY = 0x7,
 };
@@ -26,10 +27,21 @@
 	PLDM_DATA_OUT_OF_RANGE = 0x81,
 	PLDM_INVALID_READ_LENGTH = 0x82,
 	PLDM_INVALID_WRITE_LENGTH = 0x83,
+	PLDM_FILE_TABLE_UNAVAILABLE = 0x84,
+	PLDM_INVALID_FILE_TABLE_TYPE = 0x85,
+};
+
+/** @brief PLDM File I/O table types
+ */
+enum pldm_fileio_table_type {
+	PLDM_FILE_ATTRIBUTE_TABLE = 0,
+	PLDM_OEM_FILE_ATTRIBUTE_TABLE = 1,
 };
 
 #define PLDM_RW_FILE_MEM_REQ_BYTES 20
 #define PLDM_RW_FILE_MEM_RESP_BYTES 5
+#define PLDM_GET_FILE_TABLE_REQ_BYTES 6
+#define PLDM_GET_FILE_TABLE_MIN_RESP_BYTES 6
 
 /** @brief Decode ReadFileIntoMemory and WriteFileFromMemory commands request
  *         data
@@ -63,6 +75,58 @@
 			       uint8_t completion_code, uint32_t length,
 			       struct pldm_msg *msg);
 
+/** @struct pldm_get_file_table_req
+ *
+ *  Structure representing GetFileTable request
+ */
+struct pldm_get_file_table_req {
+	uint32_t transfer_handle; //!< Data transfer handle
+	uint8_t operation_flag;   //!< Transfer operation flag
+	uint8_t table_type;       //!< Table type
+} __attribute__((packed));
+
+/** @struct pldm_get_file_table_resp
+ *
+ *  Structure representing GetFileTable response fixed data
+ */
+struct pldm_get_file_table_resp {
+	uint8_t completion_code;       //!< Completion code
+	uint32_t next_transfer_handle; //!< Next data transfer handle
+	uint8_t transfer_flag;	 //!< Transfer flag
+	uint8_t table_data[1];	 //!< Table Data
+} __attribute__((packed));
+
+/** @brief Decode GetFileTable command request data
+ *
+ *  @param[in] msg - Pointer to PLDM request message payload
+ *  @param[in] payload_length - Length of request payload
+ *  @param[out] trasnfer_handle - the handle of data
+ *  @param[out] transfer_opflag - Transfer operation flag
+ *  @param[out] table_type - the type of file table
+ *  @return pldm_completion_codes
+ */
+int decode_get_file_table_req(const uint8_t *msg, size_t payload_length,
+			      uint32_t *transfer_handle,
+			      uint8_t *transfer_opflag, uint8_t *table_type);
+
+/** @brief Create a PLDM response for GetFileTable command
+ *
+ *  @param[in] instance_id - Message's instance id
+ *  @param[in] completion_code - PLDM completion code
+ *  @param[in] next_transfer_handle - Handle to identify next portion of
+ *              data transfer
+ *  @param[in] transfer_flag - Represents the part of transfer
+ *  @param[in] table_data - pointer to file table data
+ *  @param[in] table_size - file table size
+ *  @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_get_file_table_resp(uint8_t instance_id, uint8_t completion_code,
+			       uint32_t next_transfer_handle,
+			       uint8_t transfer_flag, const uint8_t *table_data,
+			       size_t table_size, struct pldm_msg *msg);
+
 #ifdef __cplusplus
 }
 #endif
diff --git a/test/libpldm_fileio_test.cpp b/test/libpldm_fileio_test.cpp
index 2a5dce1..7eedb2d 100644
--- a/test/libpldm_fileio_test.cpp
+++ b/test/libpldm_fileio_test.cpp
@@ -130,3 +130,120 @@
     ASSERT_EQ(response->hdr.command, PLDM_WRITE_FILE_FROM_MEMORY);
     ASSERT_EQ(response->payload[0], PLDM_ERROR);
 }
+
+TEST(GetFileTable, GoodDecodeRequest)
+{
+    std::array<uint8_t, PLDM_GET_FILE_TABLE_REQ_BYTES> requestMsg{};
+
+    // Random value for DataTransferHandle, TransferOperationFlag, TableType
+    uint32_t transferHandle = 0x12345678;
+    uint8_t transferOpFlag = 1;
+    uint8_t tableType = 1;
+
+    memcpy(requestMsg.data(), &transferHandle, sizeof(transferHandle));
+    memcpy(requestMsg.data() + sizeof(transferHandle), &transferOpFlag,
+           sizeof(transferOpFlag));
+    memcpy(requestMsg.data() + sizeof(transferHandle) + sizeof(transferOpFlag),
+           &tableType, sizeof(tableType));
+
+    uint32_t retTransferHandle = 0;
+    uint8_t retTransferOpFlag = 0;
+    uint8_t retTableType = 0;
+
+    // Invoke decode get file table request
+    auto rc = decode_get_file_table_req(requestMsg.data(), requestMsg.size(),
+                                        &retTransferHandle, &retTransferOpFlag,
+                                        &retTableType);
+
+    ASSERT_EQ(rc, PLDM_SUCCESS);
+    ASSERT_EQ(transferHandle, retTransferHandle);
+    ASSERT_EQ(transferOpFlag, retTransferOpFlag);
+    ASSERT_EQ(tableType, retTableType);
+}
+
+TEST(GetFileTable, BadDecodeRequest)
+{
+    uint32_t transferHandle = 0;
+    uint8_t transferOpFlag = 0;
+    uint8_t tableType = 0;
+
+    // Request payload message is missing
+    auto rc = decode_get_file_table_req(nullptr, 0, &transferHandle,
+                                        &transferOpFlag, &tableType);
+    ASSERT_EQ(rc, PLDM_ERROR_INVALID_DATA);
+
+    std::array<uint8_t, PLDM_GET_FILE_TABLE_REQ_BYTES> requestMsg{};
+
+    // TableType is NULL
+    rc = decode_get_file_table_req(requestMsg.data(), requestMsg.size(),
+                                   &transferHandle, &transferOpFlag, nullptr);
+    ASSERT_EQ(rc, PLDM_ERROR_INVALID_DATA);
+
+    // Payload length is invalid
+    rc = decode_get_file_table_req(requestMsg.data(), 0, &transferHandle,
+                                   &transferOpFlag, &tableType);
+    ASSERT_EQ(rc, PLDM_ERROR_INVALID_LENGTH);
+}
+
+TEST(GetFileTable, GoodEncodeResponse)
+{
+    // Random value for NextDataTransferHandle and TransferFlag
+    uint8_t completionCode = 0;
+    uint32_t nextTransferHandle = 0x87654321;
+    uint8_t transferFlag = 5;
+    // Mock file table contents of size 5
+    std::array<uint8_t, 5> fileTable = {1, 2, 3, 4, 5};
+    constexpr size_t responseSize = sizeof(completionCode) +
+                                    sizeof(nextTransferHandle) +
+                                    sizeof(transferFlag) + fileTable.size();
+
+    std::array<uint8_t, sizeof(pldm_msg_hdr) + responseSize> responseMsg{};
+    pldm_msg* response = reinterpret_cast<pldm_msg*>(responseMsg.data());
+
+    // GetFileTable
+    auto rc = encode_get_file_table_resp(0, PLDM_SUCCESS, nextTransferHandle,
+                                         transferFlag, fileTable.data(),
+                                         fileTable.size(), 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_IBM_OEM_TYPE);
+    ASSERT_EQ(response->hdr.command, PLDM_GET_FILE_TABLE);
+    ASSERT_EQ(response->payload[0], PLDM_SUCCESS);
+    ASSERT_EQ(0, memcmp(response->payload + sizeof(response->payload[0]),
+                        &nextTransferHandle, sizeof(nextTransferHandle)));
+    ASSERT_EQ(0, memcmp(response->payload + sizeof(response->payload[0]) +
+                            sizeof(nextTransferHandle),
+                        &transferFlag, sizeof(transferFlag)));
+    ASSERT_EQ(0, memcmp(response->payload + sizeof(response->payload[0]) +
+                            sizeof(nextTransferHandle),
+                        &transferFlag, sizeof(transferFlag)));
+    ASSERT_EQ(0, memcmp(response->payload + sizeof(response->payload[0]) +
+                            sizeof(nextTransferHandle) + sizeof(transferFlag),
+                        fileTable.data(), fileTable.size()));
+}
+
+TEST(GetFileTable, BadEncodeResponse)
+{
+    uint8_t completionCode = 0;
+    uint32_t nextTransferHandle = 0;
+    uint8_t transferFlag = 0;
+    constexpr size_t responseSize = sizeof(completionCode) +
+                                    sizeof(nextTransferHandle) +
+                                    sizeof(transferFlag);
+
+    std::array<uint8_t, sizeof(pldm_msg_hdr) + responseSize> responseMsg{};
+    pldm_msg* response = reinterpret_cast<pldm_msg*>(responseMsg.data());
+
+    // GetFileTable
+    auto rc = encode_get_file_table_resp(0, PLDM_ERROR, nextTransferHandle,
+                                         transferFlag, nullptr, 0, 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_IBM_OEM_TYPE);
+    ASSERT_EQ(response->hdr.command, PLDM_GET_FILE_TABLE);
+    ASSERT_EQ(response->payload[0], PLDM_ERROR);
+}