oem-ibm: Implement en/decode for NewFileAvailable

Enode/Decode to new file available request and response message
on PLDM.

Change-Id: Ia538d65fd6d9d2ee55407e0c10c1f060bce62ad2
Signed-off-by: vkaverap <vkaverap@in.ibm.com>
diff --git a/oem/ibm/libpldm/file_io.c b/oem/ibm/libpldm/file_io.c
index 9610967..8b42ce0 100644
--- a/oem/ibm/libpldm/file_io.c
+++ b/oem/ibm/libpldm/file_io.c
@@ -492,3 +492,98 @@
 
 	return PLDM_SUCCESS;
 }
+
+int decode_new_file_req(const struct pldm_msg *msg, size_t payload_length,
+			uint16_t *file_type, uint32_t *file_handle,
+			uint32_t *length)
+{
+	if (msg == NULL || file_type == NULL || file_handle == NULL ||
+	    length == NULL) {
+		return PLDM_ERROR_INVALID_DATA;
+	}
+
+	if (payload_length != PLDM_NEW_FILE_REQ_BYTES) {
+		return PLDM_ERROR_INVALID_LENGTH;
+	}
+
+	struct pldm_new_file_req *request =
+	    (struct pldm_new_file_req *)msg->payload;
+	*file_type = le16toh(request->file_type);
+	*file_handle = le32toh(request->file_handle);
+	*length = le32toh(request->length);
+
+	return PLDM_SUCCESS;
+}
+
+int encode_new_file_resp(uint8_t instance_id, uint8_t completion_code,
+			 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_RESPONSE;
+	header.instance = instance_id;
+	header.pldm_type = PLDM_OEM;
+	header.command = PLDM_NEW_FILE_AVAILABLE;
+
+	if ((rc = pack_pldm_header(&header, &(msg->hdr))) > PLDM_SUCCESS) {
+		return rc;
+	}
+
+	struct pldm_new_file_resp *response =
+	    (struct pldm_new_file_resp *)msg->payload;
+	response->completion_code = completion_code;
+
+	return PLDM_SUCCESS;
+}
+
+int encode_new_file_req(uint8_t instance_id, uint16_t file_type,
+			uint32_t file_handle, uint32_t length,
+			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_NEW_FILE_AVAILABLE;
+
+	if ((rc = pack_pldm_header(&header, &(msg->hdr))) > PLDM_SUCCESS) {
+		return rc;
+	}
+
+	struct pldm_new_file_req *req =
+	    (struct pldm_new_file_req *)msg->payload;
+	req->file_type = htole16(file_type);
+	req->file_handle = htole32(file_handle);
+	req->length = htole32(length);
+
+	return PLDM_SUCCESS;
+}
+
+int decode_new_file_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_RESP_BYTES) {
+		return PLDM_ERROR_INVALID_LENGTH;
+	}
+
+	struct pldm_new_file_resp *response =
+	    (struct pldm_new_file_resp *)msg->payload;
+	*completion_code = response->completion_code;
+
+	return PLDM_SUCCESS;
+}
diff --git a/oem/ibm/libpldm/file_io.h b/oem/ibm/libpldm/file_io.h
index c7b4a7c..d6c9b6a 100644
--- a/oem/ibm/libpldm/file_io.h
+++ b/oem/ibm/libpldm/file_io.h
@@ -20,6 +20,7 @@
 	PLDM_WRITE_FILE_FROM_MEMORY = 0x7,
 	PLDM_READ_FILE_BY_TYPE_INTO_MEMORY = 0x8,
 	PLDM_WRITE_FILE_BY_TYPE_FROM_MEMORY = 0x9,
+	PLDM_NEW_FILE_AVAILABLE = 0xA,
 };
 
 /** @brief PLDM Command specific codes
@@ -57,6 +58,8 @@
 #define PLDM_WRITE_FILE_RESP_BYTES 5
 #define PLDM_RW_FILE_BY_TYPE_MEM_REQ_BYTES 22
 #define PLDM_RW_FILE_BY_TYPE_MEM_RESP_BYTES 5
+#define PLDM_NEW_FILE_REQ_BYTES 10
+#define PLDM_NEW_FILE_RESP_BYTES 1
 
 /** @struct pldm_read_write_file_memory_req
  *
@@ -442,6 +445,71 @@
 				       uint8_t *completion_code,
 				       uint32_t *length);
 
+/** @struct pldm_new_file_req
+ *
+ *  Structure representing NewFile request
+ */
+struct pldm_new_file_req {
+	uint16_t file_type;   //!< Type of file
+	uint32_t file_handle; //!< Handle to file
+	uint32_t length;      //!< Number of bytes in new file
+} __attribute__((packed));
+
+/** @struct pldm_new_file_resp
+ *
+ *  Structure representing NewFile response data
+ */
+struct pldm_new_file_resp {
+	uint8_t completion_code; //!< Completion code
+} __attribute__((packed));
+
+/** @brief Decode NewFileAvailable command request data
+ *
+ *  @param[in] msg - Pointer to PLDM request message
+ *  @param[in] payload_length - Length of request payload
+ *  @param[in] file_type - Type of the file
+ *  @param[out] file_handle - A handle to the file
+ *  @param[out] length - Number of bytes in new file
+ *  @return pldm_completion_codes
+ */
+int decode_new_file_req(const struct pldm_msg *msg, size_t payload_length,
+			uint16_t *file_type, uint32_t *file_handle,
+			uint32_t *length);
+
+/** @brief Create a PLDM response for NewFileAvailable
+ *
+ *  @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_resp(uint8_t instance_id, uint8_t completion_code,
+			 struct pldm_msg *msg);
+
+/** @brief Encode NewFileAvailable command 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[out] msg - Message will be written to this
+ *  @return pldm_completion_codes
+ */
+int encode_new_file_req(uint8_t instance_id, uint16_t file_type,
+			uint32_t file_handle, uint32_t length,
+			struct pldm_msg *msg);
+
+/** @brief Decode NewFileAvailable 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_new_file_resp(const struct pldm_msg *msg, size_t payload_length,
+			 uint8_t *completion_code);
+
 #ifdef __cplusplus
 }
 #endif
diff --git a/oem/ibm/test/libpldm_fileio_test.cpp b/oem/ibm/test/libpldm_fileio_test.cpp
index 979f0b7..a796bfe 100644
--- a/oem/ibm/test/libpldm_fileio_test.cpp
+++ b/oem/ibm/test/libpldm_fileio_test.cpp
@@ -943,3 +943,178 @@
 
     ASSERT_EQ(rc, PLDM_ERROR_INVALID_DATA);
 }
+
+TEST(NewFile, testGoodDecodeRequest)
+{
+    std::array<uint8_t, PLDM_NEW_FILE_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_req*>(requestPtr->payload);
+
+    // Random value for fileHandle and length
+    uint16_t fileType = 0xFF;
+    uint32_t fileHandle = 0x12345678;
+    uint32_t length = 0x13245768;
+
+    request->file_type = fileType;
+    request->file_handle = fileHandle;
+    request->length = length;
+
+    uint16_t retFileType = 0xFF;
+    uint32_t retFileHandle = 0;
+    uint32_t retLength = 0;
+
+    // Invoke decode the read file request
+    auto rc = decode_new_file_req(requestPtr, payload_length, &retFileType,
+                                  &retFileHandle, &retLength);
+
+    ASSERT_EQ(rc, PLDM_SUCCESS);
+    ASSERT_EQ(fileType, retFileType);
+    ASSERT_EQ(fileHandle, retFileHandle);
+    ASSERT_EQ(length, retLength);
+}
+
+TEST(NewFile, testGoodDecodeResponse)
+{
+    std::array<uint8_t, PLDM_NEW_FILE_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_new_file_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_resp(responsePtr, payload_length, &retCompletionCode);
+
+    ASSERT_EQ(rc, PLDM_SUCCESS);
+    ASSERT_EQ(completionCode, retCompletionCode);
+}
+
+TEST(NewFile, testBadDecodeRequest)
+{
+    uint16_t fileType = 0;
+    uint32_t fileHandle = 0;
+    uint32_t length = 0;
+
+    // Request payload message is missing
+    auto rc = decode_new_file_req(NULL, 0, &fileType, &fileHandle, &length);
+
+    ASSERT_EQ(rc, PLDM_ERROR_INVALID_DATA);
+
+    std::array<uint8_t, PLDM_NEW_FILE_REQ_BYTES + sizeof(pldm_msg_hdr)>
+        requestMsg{};
+
+    auto requestPtr = reinterpret_cast<pldm_msg*>(requestMsg.data());
+
+    // Payload length is invalid
+    rc = decode_new_file_req(requestPtr, 0, &fileType, &fileHandle, &length);
+    ASSERT_EQ(rc, PLDM_ERROR_INVALID_LENGTH);
+}
+
+TEST(NewFile, testBadDecodeResponse)
+{
+    uint8_t completionCode = 0;
+
+    // Request payload message is missing
+    auto rc = decode_new_file_resp(NULL, 0, &completionCode);
+    ASSERT_EQ(rc, PLDM_ERROR_INVALID_DATA);
+
+    std::array<uint8_t, PLDM_NEW_FILE_RESP_BYTES + sizeof(pldm_msg_hdr)>
+        responseMsg{};
+
+    auto responsePtr = reinterpret_cast<pldm_msg*>(responseMsg.data());
+
+    // Payload length is invalid
+    rc = decode_new_file_resp(responsePtr, 0, &completionCode);
+    ASSERT_EQ(rc, PLDM_ERROR_INVALID_LENGTH);
+}
+
+TEST(NewFile, testGoodEncodeRequest)
+{
+    std::array<uint8_t, sizeof(pldm_msg_hdr) + PLDM_NEW_FILE_REQ_BYTES>
+        requestMsg{};
+
+    uint16_t fileType = 0xFF;
+    uint32_t fileHandle = 0x12345678;
+    uint32_t length = 0x13245768;
+
+    pldm_msg* request = reinterpret_cast<pldm_msg*>(requestMsg.data());
+
+    auto rc = encode_new_file_req(0, fileType, fileHandle, length, 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);
+    ASSERT_EQ(0, memcmp(request->payload, &fileType, sizeof(fileType)));
+    ASSERT_EQ(0, memcmp(request->payload + sizeof(fileType), &fileHandle,
+                        sizeof(fileHandle)));
+    ASSERT_EQ(0,
+              memcmp(request->payload + sizeof(fileType) + sizeof(fileHandle),
+                     &length, sizeof(length)));
+}
+
+TEST(NewFile, testGoodEncodeResponse)
+{
+    std::array<uint8_t, sizeof(pldm_msg_hdr) + PLDM_NEW_FILE_RESP_BYTES>
+        responseMsg{};
+
+    uint8_t completionCode = 0x0;
+
+    pldm_msg* response = reinterpret_cast<pldm_msg*>(responseMsg.data());
+
+    auto rc = encode_new_file_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);
+    ASSERT_EQ(
+        0, memcmp(response->payload, &completionCode, sizeof(completionCode)));
+}
+
+TEST(NewFile, testBadEncodeResponse)
+{
+    std::array<uint8_t, sizeof(pldm_msg_hdr) + PLDM_NEW_FILE_RESP_BYTES>
+        responseMsg{};
+    pldm_msg* response = reinterpret_cast<pldm_msg*>(responseMsg.data());
+
+    // completion code is PLDM_ERROR
+    auto rc = encode_new_file_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);
+    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(NewFile, testBadEncodeRequest)
+{
+    uint8_t fileType = 0xFF;
+    uint32_t fileHandle = 0;
+    uint32_t length = 0;
+
+    // request is NULL pointer
+    auto rc = encode_new_file_req(0, fileType, fileHandle, length, NULL);
+
+    ASSERT_EQ(rc, PLDM_ERROR_INVALID_DATA);
+}