OEM IBM: Add file IO requester encode/decode APIs

Implement encode request and decode response and also
reduce the pointer and memcopy operations for ReadFileIntoMemory
and WriteFileIntoMemory requester and responser by
creating structures.

Signed-off-by: Priyanga <priyram1@in.ibm.com>
Change-Id: I8dc1d4684ae38b65b7ce8bb5b0f41ee31f36b2a5
diff --git a/libpldm/file_io.c b/libpldm/file_io.c
index f32892c..f5301f5 100644
--- a/libpldm/file_io.c
+++ b/libpldm/file_io.c
@@ -15,12 +15,13 @@
 		return PLDM_ERROR_INVALID_LENGTH;
 	}
 
-	*file_handle = le32toh(*((uint32_t *)msg));
-	*offset = le32toh(*((uint32_t *)(msg + sizeof(*file_handle))));
-	*length = le32toh(
-	    *((uint32_t *)(msg + sizeof(*file_handle) + sizeof(*offset))));
-	*address = le64toh(*((uint64_t *)(msg + sizeof(*file_handle) +
-					  sizeof(*offset) + sizeof(*length))));
+	struct pldm_read_write_file_memory_req *request =
+	    (struct pldm_read_write_file_memory_req *)msg;
+
+	*file_handle = le32toh(request->file_handle);
+	*offset = le32toh(request->offset);
+	*length = le32toh(request->length);
+	*address = le64toh(request->address);
 
 	return PLDM_SUCCESS;
 }
@@ -32,9 +33,6 @@
 	struct pldm_header_info header = {0};
 	int rc = PLDM_SUCCESS;
 
-	uint8_t *payload = msg->payload;
-	*payload = completion_code;
-
 	header.msg_type = PLDM_RESPONSE;
 	header.instance = instance_id;
 	header.pldm_type = PLDM_IBM_OEM_TYPE;
@@ -44,10 +42,61 @@
 		return rc;
 	}
 
-	if (msg->payload[0] == PLDM_SUCCESS) {
-		uint8_t *dst = msg->payload + sizeof(completion_code);
-		length = htole32(length);
-		memcpy(dst, &length, sizeof(length));
+	struct pldm_read_write_file_memory_resp *response =
+	    (struct pldm_read_write_file_memory_resp *)msg->payload;
+	response->completion_code = completion_code;
+	if (response->completion_code == PLDM_SUCCESS) {
+		response->length = htole32(length);
+	}
+
+	return PLDM_SUCCESS;
+}
+
+int encode_rw_file_memory_req(uint8_t instance_id, uint8_t command,
+			      uint32_t file_handle, uint32_t offset,
+			      uint32_t length, uint64_t address,
+			      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_IBM_OEM_TYPE;
+	header.command = command;
+
+	if ((rc = pack_pldm_header(&header, &(msg->hdr))) > PLDM_SUCCESS) {
+		return rc;
+	}
+
+	struct pldm_read_write_file_memory_req *req =
+	    (struct pldm_read_write_file_memory_req *)msg->payload;
+	req->file_handle = htole32(file_handle);
+	req->offset = htole32(offset);
+	req->length = htole32(length);
+	req->address = htole64(address);
+	return PLDM_SUCCESS;
+}
+
+int decode_rw_file_memory_resp(const uint8_t *msg, size_t payload_length,
+			       uint8_t *completion_code, uint32_t *length)
+{
+	if (msg == NULL || length == NULL || completion_code == NULL) {
+		return PLDM_ERROR_INVALID_DATA;
+	}
+
+	if (payload_length != PLDM_RW_FILE_MEM_RESP_BYTES) {
+		return PLDM_ERROR_INVALID_LENGTH;
+	}
+
+	struct pldm_read_write_file_memory_resp *response =
+	    (struct pldm_read_write_file_memory_resp *)msg;
+	*completion_code = response->completion_code;
+	if (*completion_code == PLDM_SUCCESS) {
+		*length = le32toh(response->length);
 	}
 
 	return PLDM_SUCCESS;
diff --git a/libpldm/file_io.h b/libpldm/file_io.h
index 6354f81..e138275 100644
--- a/libpldm/file_io.h
+++ b/libpldm/file_io.h
@@ -43,8 +43,30 @@
 #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
+/** @struct pldm_read_write_file_memory_req
+ *
+ *  Structure representing ReadFileIntoMemory request and WriteFileFromMemory
+ *  request
+ */
+struct pldm_read_write_file_memory_req {
+	uint32_t file_handle; //!< A Handle to the file
+	uint32_t offset;      //!< Offset to the file
+	uint32_t length;      //!< Number of bytes to be read/write
+	uint64_t address;     //!< Memory address of the file
+} __attribute__((packed));
+
+/** @struct pldm_read_write_file_memory_resp
+ *
+ *  Structure representing ReadFileIntoMemory response and WriteFileFromMemory
+ *  response
+ */
+struct pldm_read_write_file_memory_resp {
+	uint8_t completion_code; //!< completion code
+	uint32_t length;	 //!< Number of bytes read/written
+} __attribute__((packed));
+
+/** @brief Decode ReadFileIntoMemory and WriteFileFromMemory
+ *         commands request data
  *
  *  @param[in] msg - Pointer to PLDM request message payload
  *  @param[in] payload_length - Length of request payload
@@ -65,9 +87,9 @@
  *  @param[in] instance_id - Message's instance id
  *  @param[in] command - PLDM command
  *  @param[in] completion_code - PLDM completion code
- *  @param[in] length - Number of bytes read. This could be less than what the
-			 requester asked for.
- *  @param[in,out] msg - Message will be written to this
+ *  @param[in] length - Number of bytes read. This could be less than
+ *                      what the  requester asked for.
+ *  @param[out] msg - Message will be written to this
  *  @return pldm_completion_codes
  *  @note  Caller is responsible for memory alloc and dealloc of param 'msg'
  */
@@ -75,6 +97,36 @@
 			       uint8_t completion_code, uint32_t length,
 			       struct pldm_msg *msg);
 
+/** @brief Encode ReadFileIntoMemory and WriteFileFromMemory
+ *         commands request data
+ *
+ *  @param[in] instance_id - Message's instance id
+ *  @param[in] command - PLDM command
+ *  @param[in] file_handle - A handle to the file
+ *  @param[in] offset -  Offset to the file at which the read should begin
+ *  @param[in] length -  Number of bytes to be read/written
+ *  @param[in] address - Memory address where the file content has to be
+ *                       written to
+ *  @param[out] msg - Message will be written to this
+ *  @return pldm_completion_codes
+ */
+int encode_rw_file_memory_req(uint8_t instance_id, uint8_t command,
+			      uint32_t file_handle, uint32_t offset,
+			      uint32_t length, uint64_t address,
+			      struct pldm_msg *msg);
+
+/** @brief Decode ReadFileIntoMemory and WriteFileFromMemory
+ *         commands response data
+ *
+ *  @param[in] msg - pointer to PLDM response message payload
+ *  @param[in] payload_length - Length of response payload
+ *  @param[out] completion_code - PLDM completion code
+ *  @param[out] length - Number of bytes to be read/written
+ *  @return pldm_completion_codes
+ */
+int decode_rw_file_memory_resp(const uint8_t *msg, size_t payload_length,
+			       uint8_t *completion_code, uint32_t *length);
+
 /** @struct pldm_get_file_table_req
  *
  *  Structure representing GetFileTable request
diff --git a/test/libpldm_fileio_test.cpp b/test/libpldm_fileio_test.cpp
index 7eedb2d..d89d504 100644
--- a/test/libpldm_fileio_test.cpp
+++ b/test/libpldm_fileio_test.cpp
@@ -131,6 +131,92 @@
     ASSERT_EQ(response->payload[0], PLDM_ERROR);
 }
 
+TEST(ReadWriteFileIntoMemory, testGoodDecodeResponse)
+{
+    std::array<uint8_t, PLDM_RW_FILE_MEM_RESP_BYTES> responseMsg{};
+    // Random value for length
+    uint32_t length = 0xFF00EE12;
+    uint8_t completionCode = 0;
+
+    memcpy(responseMsg.data(), &completionCode, sizeof(completionCode));
+    memcpy(responseMsg.data() + sizeof(completionCode), &length,
+           sizeof(length));
+
+    uint8_t retCompletionCode = 0;
+    uint32_t retLength = 0;
+
+    // Invoke decode the read file memory response
+    auto rc = decode_rw_file_memory_resp(responseMsg.data(), responseMsg.size(),
+                                         &retCompletionCode, &retLength);
+    ASSERT_EQ(rc, PLDM_SUCCESS);
+    ASSERT_EQ(completionCode, retCompletionCode);
+    ASSERT_EQ(length, retLength);
+}
+
+TEST(ReadWriteFileIntoMemory, testBadDecodeResponse)
+{
+    uint32_t length = 0;
+    uint8_t completionCode = 0;
+
+    // Request payload message is missing
+    auto rc = decode_rw_file_memory_resp(NULL, 0, &completionCode, &length);
+    ASSERT_EQ(rc, PLDM_ERROR_INVALID_DATA);
+
+    std::array<uint8_t, PLDM_RW_FILE_MEM_RESP_BYTES> responseMsg{};
+
+    // Payload length is invalid
+    rc = decode_rw_file_memory_resp(responseMsg.data(), 0, &completionCode,
+                                    &length);
+    ASSERT_EQ(rc, PLDM_ERROR_INVALID_LENGTH);
+}
+
+TEST(ReadWriteFileIntoMemory, testGoodEncodeRequest)
+{
+    std::array<uint8_t, sizeof(pldm_msg_hdr) + PLDM_RW_FILE_MEM_REQ_BYTES>
+        requestMsg{};
+
+    uint32_t fileHandle = 0x12345678;
+    uint32_t offset = 0x87654321;
+    uint32_t length = 0x13245768;
+    uint64_t address = 0x124356879ACBDE0F;
+
+    pldm_msg* request = reinterpret_cast<pldm_msg*>(requestMsg.data());
+
+    auto rc =
+        encode_rw_file_memory_req(0, PLDM_READ_FILE_INTO_MEMORY, fileHandle,
+                                  offset, length, address, 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_IBM_OEM_TYPE);
+    ASSERT_EQ(request->hdr.command, PLDM_READ_FILE_INTO_MEMORY);
+
+    ASSERT_EQ(0, memcmp(request->payload, &fileHandle, sizeof(fileHandle)));
+
+    ASSERT_EQ(0, memcmp(request->payload + sizeof(fileHandle), &offset,
+                        sizeof(offset)));
+    ASSERT_EQ(0, memcmp(request->payload + sizeof(fileHandle) + sizeof(offset),
+                        &length, sizeof(length)));
+    ASSERT_EQ(0, memcmp(request->payload + sizeof(fileHandle) + sizeof(offset) +
+                            sizeof(length),
+                        &address, sizeof(address)));
+}
+
+TEST(ReadWriteFileIntoMemory, testBadEncodeRequest)
+{
+    uint32_t fileHandle = 0;
+    uint32_t offset = 0;
+    uint32_t length = 0;
+    uint64_t address = 0;
+
+    auto rc =
+        encode_rw_file_memory_req(0, PLDM_READ_FILE_INTO_MEMORY, fileHandle,
+                                  offset, length, address, NULL);
+
+    ASSERT_EQ(rc, PLDM_ERROR_INVALID_DATA);
+}
+
 TEST(GetFileTable, GoodDecodeRequest)
 {
     std::array<uint8_t, PLDM_GET_FILE_TABLE_REQ_BYTES> requestMsg{};