oem-ibm/libpldm : Implement encode/decode for GetFileTable

This commit implements the requester flow (encode request
and decode response) of GetFileTable command as defined
in the spec.

Tested: Unit tested

Change-Id: I9813fb9f26791c9913690dfc31a0189be339b582
Signed-off-by: Pavithra Barithaya <pbaritha@in.ibm.com>
diff --git a/oem/ibm/libpldm/file_io.c b/oem/ibm/libpldm/file_io.c
index 02adc80..788f480 100644
--- a/oem/ibm/libpldm/file_io.c
+++ b/oem/ibm/libpldm/file_io.c
@@ -156,6 +156,72 @@
 	return PLDM_SUCCESS;
 }
 
+int encode_get_file_table_req(uint8_t instance_id, uint32_t transfer_handle,
+			      uint8_t transfer_opflag, uint8_t table_type,
+			      struct pldm_msg *msg)
+{
+	struct pldm_header_info header = {0};
+	int rc = PLDM_SUCCESS;
+
+	if (msg == NULL) {
+		return PLDM_ERROR_INVALID_DATA;
+	}
+
+	header.msg_type = PLDM_REQUEST;
+	header.instance = instance_id;
+	header.pldm_type = PLDM_OEM;
+	header.command = PLDM_GET_FILE_TABLE;
+
+	if ((rc = pack_pldm_header(&header, &(msg->hdr))) > PLDM_SUCCESS) {
+		return rc;
+	}
+
+	struct pldm_get_file_table_req *request =
+	    (struct pldm_get_file_table_req *)msg->payload;
+
+	request->transfer_handle = htole32(transfer_handle);
+	request->operation_flag = transfer_opflag;
+	request->table_type = table_type;
+	return PLDM_SUCCESS;
+}
+
+int decode_get_file_table_resp(const struct pldm_msg *msg,
+			       size_t payload_length, uint8_t *completion_code,
+			       uint32_t *next_transfer_handle,
+			       uint8_t *transfer_flag,
+			       uint8_t *file_table_data_start_offset,
+			       size_t *file_table_length)
+{
+	if (msg == NULL || transfer_flag == NULL ||
+	    next_transfer_handle == NULL || completion_code == NULL ||
+	    file_table_data_start_offset == NULL || file_table_length == NULL) {
+		return PLDM_ERROR_INVALID_DATA;
+	}
+
+	if (payload_length <= PLDM_GET_FILE_TABLE_MIN_RESP_BYTES) {
+		return PLDM_ERROR_INVALID_LENGTH;
+	}
+
+	*completion_code = msg->payload[0];
+
+	if (PLDM_SUCCESS != *completion_code) {
+		return PLDM_SUCCESS;
+	}
+
+	struct pldm_get_file_table_resp *response =
+	    (struct pldm_get_file_table_resp *)msg->payload;
+
+	*next_transfer_handle = le32toh(response->next_transfer_handle);
+	*transfer_flag = response->transfer_flag;
+	*file_table_data_start_offset = sizeof(*completion_code) +
+					sizeof(*next_transfer_handle) +
+					sizeof(*transfer_flag);
+	*file_table_length =
+	    payload_length - PLDM_GET_FILE_TABLE_MIN_RESP_BYTES;
+
+	return PLDM_SUCCESS;
+}
+
 int decode_read_file_req(const struct pldm_msg *msg, size_t payload_length,
 			 uint32_t *file_handle, uint32_t *offset,
 			 uint32_t *length)
diff --git a/oem/ibm/libpldm/file_io.h b/oem/ibm/libpldm/file_io.h
index 4d00358..38e50ca 100644
--- a/oem/ibm/libpldm/file_io.h
+++ b/oem/ibm/libpldm/file_io.h
@@ -209,6 +209,38 @@
 			       uint8_t transfer_flag, const uint8_t *table_data,
 			       size_t table_size, struct pldm_msg *msg);
 
+/** @brief Encode GetFileTable command request data
+ *
+ * @param[in] instance_id - Message's instance id
+ * @param[in] transfer_handle - the handle of data
+ * @param[in] transfer_opflag - Transfer operation flag
+ * @param[in] table_type - the type of file table
+ * @param[out] msg - Message will be written to this
+ * @return pldm_completion_codes
+ */
+int encode_get_file_table_req(uint8_t instance_id, uint32_t transfer_handle,
+			      uint8_t transfer_opflag, uint8_t table_type,
+			      struct pldm_msg *msg);
+
+/** @brief Decode GetFileTable command response data
+ * @param[in] msg - Response message
+ * @param[in] payload_length - length of response message payload
+ * @param[out] completion_code - PLDM completion code
+ * @param[out] next_transfer_handle -  Handle to identify next portion of data
+ * transfer
+ * @param[out] transfer_flag - Represents the part of transfer
+ * @param[out] file_table_data_start_offset - This data is a portion of the
+ * overall File Table
+ * @param[out] file_table_length - Length of the File table data
+ * @return pldm_completion_codes
+ */
+int decode_get_file_table_resp(const struct pldm_msg *msg,
+			       size_t payload_length, uint8_t *completion_code,
+			       uint32_t *next_transfer_handle,
+			       uint8_t *transfer_flag,
+			       uint8_t *file_table_data_start_offset,
+			       size_t *file_table_length);
+
 /** @struct pldm_read_file_req
  *
  *  Structure representing ReadFile request
diff --git a/oem/ibm/test/libpldm_fileio_test.cpp b/oem/ibm/test/libpldm_fileio_test.cpp
index 10ba83f..94b7e1d 100644
--- a/oem/ibm/test/libpldm_fileio_test.cpp
+++ b/oem/ibm/test/libpldm_fileio_test.cpp
@@ -365,6 +365,104 @@
     ASSERT_EQ(response->payload[0], PLDM_ERROR);
 }
 
+TEST(GetFileTable, GoodEncodeRequest)
+{
+    std::array<uint8_t, sizeof(pldm_msg_hdr) + PLDM_GET_FILE_TABLE_REQ_BYTES>
+        requestMsg{};
+    uint32_t transferHandle = 0x0;
+    uint8_t transferOpFlag = 0x01;
+    uint8_t tableType = PLDM_FILE_ATTRIBUTE_TABLE;
+
+    auto request = reinterpret_cast<pldm_msg*>(requestMsg.data());
+    auto rc = encode_get_file_table_req(0, transferHandle, transferOpFlag,
+                                        tableType, request);
+    EXPECT_EQ(rc, PLDM_SUCCESS);
+
+    struct pldm_get_file_table_req* req =
+        reinterpret_cast<struct pldm_get_file_table_req*>(request->payload);
+    EXPECT_EQ(transferHandle, le32toh(req->transfer_handle));
+    EXPECT_EQ(transferOpFlag, req->operation_flag);
+    EXPECT_EQ(tableType, req->table_type);
+}
+
+TEST(GetFileTable, BadEncodeRequest)
+{
+    uint32_t transferHandle = 0x0;
+    uint8_t transferOpFlag = 0x01;
+    uint8_t tableType = PLDM_FILE_ATTRIBUTE_TABLE;
+
+    auto rc = encode_get_file_table_req(0, transferHandle, transferOpFlag,
+                                        tableType, nullptr);
+
+    EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
+}
+
+TEST(GetFileTable, GoodDecodeResponse)
+{
+    uint32_t nextTransferHandle = 32;
+    uint8_t completionCode = PLDM_SUCCESS;
+    uint8_t transferFlag = PLDM_START_AND_END;
+    std::vector<uint8_t> fileTableData = {1, 2, 3, 4, 5, 6, 7, 8, 9};
+
+    std::vector<uint8_t> responseMsg(
+        hdrSize + PLDM_GET_FILE_TABLE_MIN_RESP_BYTES + fileTableData.size());
+
+    auto responsePtr = reinterpret_cast<pldm_msg*>(responseMsg.data());
+    size_t payload_length = responseMsg.size() - hdrSize;
+
+    auto resp = reinterpret_cast<struct pldm_get_file_table_resp*>(
+        responsePtr->payload);
+
+    resp->completion_code = completionCode;
+    resp->next_transfer_handle = htole32(nextTransferHandle);
+    resp->transfer_flag = transferFlag;
+    memcpy(resp->table_data, fileTableData.data(), fileTableData.size());
+
+    uint8_t retCompletionCode;
+    uint32_t retNextTransferHandle;
+    uint8_t retTransferFlag;
+    std::vector<uint8_t> retFileTableData(9, 0);
+    size_t retFileTableDataLength = 0;
+
+    auto rc = decode_get_file_table_resp(
+        responsePtr, payload_length, &retCompletionCode, &retNextTransferHandle,
+        &retTransferFlag, retFileTableData.data(), &retFileTableDataLength);
+
+    ASSERT_EQ(rc, PLDM_SUCCESS);
+    ASSERT_EQ(completionCode, retCompletionCode);
+    ASSERT_EQ(nextTransferHandle, retNextTransferHandle);
+    ASSERT_EQ(transferFlag, retTransferFlag);
+    ASSERT_EQ(0, memcmp(fileTableData.data(), resp->table_data,
+                        retFileTableDataLength));
+    ASSERT_EQ(fileTableData.size(), retFileTableDataLength);
+}
+
+TEST(GetFileTable, BadDecodeResponse)
+{
+    uint32_t nextTransferHandle = 32;
+    uint8_t completionCode = PLDM_SUCCESS;
+    uint8_t transferFlag = PLDM_START_AND_END;
+    std::vector<uint8_t> fileTableData(9, 0);
+    size_t file_table_data_length = 0;
+
+    std::vector<uint8_t> responseMsg(
+        hdrSize + PLDM_GET_FILE_TABLE_MIN_RESP_BYTES + fileTableData.size());
+
+    auto responsePtr = reinterpret_cast<pldm_msg*>(responseMsg.data());
+
+    auto rc = decode_get_file_table_resp(
+        nullptr, 0, &completionCode, &nextTransferHandle, &transferFlag,
+        fileTableData.data(), &file_table_data_length);
+
+    EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
+
+    rc = decode_get_file_table_resp(
+        responsePtr, 0, &completionCode, &nextTransferHandle, &transferFlag,
+        fileTableData.data(), &file_table_data_length);
+
+    EXPECT_EQ(rc, PLDM_ERROR_INVALID_LENGTH);
+}
+
 TEST(ReadFile, testGoodDecodeRequest)
 {
     std::array<uint8_t, PLDM_READ_FILE_REQ_BYTES + sizeof(pldm_msg_hdr)>