dsp: file: Let some encode APIs accept pointer to payload_length

In order to achieve [1], this commit updates some encoding APIs to
accept the length of the message as an in/out parameter (pointer to
size_t). The APIs are then updated to return the encoded payload length
through this parameter.

The unit tests for these APIs are updated accordingly. The change list
includes:

  - encode_pldm_file_df_open_req()
  - encode_pldm_file_df_close_req()
  - encode_pldm_file_df_heartbeat_req()

[1]: https://github.com/openbmc/libpldm/blob/main/CONTRIBUTING.md?plain=1#L270-L272

Change-Id: Ic81327438190bfa0541333f35e0b52a51010db91
Signed-off-by: Chau Ly <chaul@amperecomputing.com>
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 771a9cd..f262a39 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -44,6 +44,11 @@
   - `encode_pldm_base_multipart_receive_req()`
   - `encode_pldm_base_negotiate_transfer_params_req()`
 
+- file: Let `payload_length` be an in/out buffer for:
+  - `encode_pldm_file_df_open_req()`
+  - `encode_pldm_file_df_close_req()`
+  - `encode_pldm_file_df_heartbeat_req()`
+
 ### Deprecated
 
 ### Removed
diff --git a/include/libpldm/file.h b/include/libpldm/file.h
index 0c17daf..d9d4314 100644
--- a/include/libpldm/file.h
+++ b/include/libpldm/file.h
@@ -104,7 +104,7 @@
  *  @param[in] instance_id - Message's instance id
  *  @param[in] req - The pointer to the request message to be encoded
  *  @param[in,out] msg - Message will be written to this
- *  @param[in] payload_length - Length of the request message payload
+ *  @param[in, out] payload_length - Length of the request message payload
  *  @return 0 on success
  *          -EINVAL if the input parameters' memory are not allocated,
  *          or message type or instance in request header is invalid
@@ -115,7 +115,7 @@
  */
 int encode_pldm_file_df_open_req(uint8_t instance_id,
 				 const struct pldm_file_df_open_req *req,
-				 struct pldm_msg *msg, size_t payload_length);
+				 struct pldm_msg *msg, size_t *payload_length);
 
 /** @brief Decode DFOpen request data
  *
@@ -171,7 +171,7 @@
  *  @param[in] instance_id - Message's instance id
  *  @param[in] req - The pointer to the request message to be encoded
  *  @param[in,out] msg - Message will be written to this
- *  @param[in] payload_length - Length of the request message payload
+ *  @param[in, out] payload_length - Length of the request message payload
  *  @return 0 on success
  *          -EINVAL if the input parameters' memory are not allocated,
  *          or message type or instance in request header is invalid
@@ -182,7 +182,7 @@
  */
 int encode_pldm_file_df_close_req(uint8_t instance_id,
 				  const struct pldm_file_df_close_req *req,
-				  struct pldm_msg *msg, size_t payload_length);
+				  struct pldm_msg *msg, size_t *payload_length);
 
 /** @brief Decode DFClose request data
  *
@@ -235,7 +235,7 @@
  *  @param[in] instance_id - Message's instance id
  *  @param[in] req - The pointer to the request message to be encoded
  *  @param[in,out] msg - Message will be written to this
- *  @param[in] payload_length - Length of the request message payload
+ *  @param[in, out] payload_length - Length of the request message payload
  *  @return 0 on success
  *          -EINVAL if the input parameters' memory are not allocated,
  *          or message type or instance in request header is invalid
@@ -246,7 +246,7 @@
  */
 int encode_pldm_file_df_heartbeat_req(
 	uint8_t instance_id, const struct pldm_file_df_heartbeat_req *req,
-	struct pldm_msg *msg, size_t payload_length);
+	struct pldm_msg *msg, size_t *payload_length);
 
 /** @brief Decode DFHeartbeat response data
  *
diff --git a/src/dsp/file.c b/src/dsp/file.c
index 988d284..bdfc9c6 100644
--- a/src/dsp/file.c
+++ b/src/dsp/file.c
@@ -16,7 +16,7 @@
 LIBPLDM_ABI_TESTING
 int encode_pldm_file_df_open_req(uint8_t instance_id,
 				 const struct pldm_file_df_open_req *req,
-				 struct pldm_msg *msg, size_t payload_length)
+				 struct pldm_msg *msg, size_t *payload_length)
 {
 	PLDM_MSGBUF_DEFINE_P(buf);
 	int rc;
@@ -37,7 +37,7 @@
 	}
 
 	rc = pldm_msgbuf_init_errno(buf, PLDM_DF_OPEN_REQ_BYTES, msg->payload,
-				    payload_length);
+				    *payload_length);
 	if (rc) {
 		return rc;
 	}
@@ -45,7 +45,7 @@
 	pldm_msgbuf_insert(buf, req->file_identifier);
 	pldm_msgbuf_insert(buf, req->file_attribute.value);
 
-	return pldm_msgbuf_complete(buf);
+	return pldm_msgbuf_complete_used(buf, *payload_length, payload_length);
 }
 
 LIBPLDM_ABI_TESTING
@@ -147,7 +147,7 @@
 LIBPLDM_ABI_TESTING
 int encode_pldm_file_df_close_req(uint8_t instance_id,
 				  const struct pldm_file_df_close_req *req,
-				  struct pldm_msg *msg, size_t payload_length)
+				  struct pldm_msg *msg, size_t *payload_length)
 {
 	PLDM_MSGBUF_DEFINE_P(buf);
 	int rc;
@@ -168,7 +168,7 @@
 	}
 
 	rc = pldm_msgbuf_init_errno(buf, PLDM_DF_CLOSE_REQ_BYTES, msg->payload,
-				    payload_length);
+				    *payload_length);
 	if (rc) {
 		return rc;
 	}
@@ -176,7 +176,7 @@
 	pldm_msgbuf_insert(buf, req->file_descriptor);
 	pldm_msgbuf_insert(buf, req->df_close_options.value);
 
-	return pldm_msgbuf_complete(buf);
+	return pldm_msgbuf_complete_used(buf, *payload_length, payload_length);
 }
 
 LIBPLDM_ABI_TESTING
@@ -254,7 +254,7 @@
 LIBPLDM_ABI_TESTING
 int encode_pldm_file_df_heartbeat_req(
 	uint8_t instance_id, const struct pldm_file_df_heartbeat_req *req,
-	struct pldm_msg *msg, size_t payload_length)
+	struct pldm_msg *msg, size_t *payload_length)
 {
 	PLDM_MSGBUF_DEFINE_P(buf);
 	int rc;
@@ -275,7 +275,7 @@
 	}
 
 	rc = pldm_msgbuf_init_errno(buf, PLDM_DF_HEARTBEAT_REQ_BYTES,
-				    msg->payload, payload_length);
+				    msg->payload, *payload_length);
 	if (rc) {
 		return rc;
 	}
@@ -283,7 +283,7 @@
 	pldm_msgbuf_insert(buf, req->file_descriptor);
 	pldm_msgbuf_insert(buf, req->requester_max_interval);
 
-	return pldm_msgbuf_complete(buf);
+	return pldm_msgbuf_complete_used(buf, *payload_length, payload_length);
 }
 
 LIBPLDM_ABI_TESTING
diff --git a/tests/dsp/file.cpp b/tests/dsp/file.cpp
index 71dcce3..cf5b11d 100644
--- a/tests/dsp/file.cpp
+++ b/tests/dsp/file.cpp
@@ -18,19 +18,22 @@
     uint16_t file_identifier = 0x0100;
     bitfield16_t file_attribute;
     file_attribute.value = 0x0400;
-    std::array<uint8_t, PLDM_DF_OPEN_REQ_BYTES> requestMsg = {0x00, 0x01, 0x00,
-                                                              0x04};
+
+    static constexpr const size_t requestMsgLength = PLDM_DF_OPEN_REQ_BYTES;
+    std::array<uint8_t, requestMsgLength> requestMsg = {0x00, 0x01, 0x00, 0x04};
 
     const struct pldm_file_df_open_req req_data = {file_identifier,
                                                    file_attribute};
 
-    PLDM_MSG_DEFINE_P(requestPtr, PLDM_DF_OPEN_REQ_BYTES);
+    PLDM_MSG_DEFINE_P(requestPtr, requestMsgLength);
+    size_t payload_length = requestMsgLength;
     auto rc = encode_pldm_file_df_open_req(instance_id, &req_data, requestPtr,
-                                           PLDM_DF_OPEN_REQ_BYTES);
+                                           &payload_length);
 
     ASSERT_EQ(rc, 0);
     EXPECT_EQ(
         0, memcmp(requestPtr->payload, requestMsg.data(), sizeof(requestMsg)));
+    EXPECT_EQ(payload_length, requestMsgLength);
 }
 #endif
 
@@ -43,16 +46,19 @@
     file_attribute.value = 0x0400;
     int rc;
 
+    static constexpr const size_t requestMsgLength = PLDM_DF_OPEN_REQ_BYTES;
+
     const struct pldm_file_df_open_req req_data = {file_identifier,
                                                    file_attribute};
 
-    PLDM_MSG_DEFINE_P(requestPtr, PLDM_DF_OPEN_REQ_BYTES);
+    PLDM_MSG_DEFINE_P(requestPtr, requestMsgLength);
+    size_t payload_length = requestMsgLength;
     rc = encode_pldm_file_df_open_req(instance_id, &req_data, nullptr,
-                                      PLDM_DF_OPEN_REQ_BYTES);
+                                      &payload_length);
     EXPECT_EQ(rc, -EINVAL);
 
     rc = encode_pldm_file_df_open_req(instance_id, nullptr, requestPtr,
-                                      PLDM_DF_OPEN_REQ_BYTES);
+                                      &payload_length);
     EXPECT_EQ(rc, -EINVAL);
 }
 #endif
@@ -66,11 +72,15 @@
     file_attribute.value = 0x0400;
     int rc;
 
+    static constexpr const size_t requestMsgLength = PLDM_DF_OPEN_REQ_BYTES;
+
     const struct pldm_file_df_open_req req_data = {file_identifier,
                                                    file_attribute};
 
-    PLDM_MSG_DEFINE_P(requestPtr, PLDM_DF_OPEN_REQ_BYTES);
-    rc = encode_pldm_file_df_open_req(instance_id, &req_data, requestPtr, 1);
+    PLDM_MSG_DEFINE_P(requestPtr, requestMsgLength);
+    size_t payload_length = 1;
+    rc = encode_pldm_file_df_open_req(instance_id, &req_data, requestPtr,
+                                      &payload_length);
     EXPECT_EQ(rc, -EOVERFLOW);
 }
 #endif
@@ -172,19 +182,22 @@
     uint16_t file_descriptor = 0x0200;
     bitfield16_t df_close_options;
     df_close_options.value = 0x0100;
-    std::array<uint8_t, PLDM_DF_CLOSE_REQ_BYTES> requestMsg = {0x00, 0x02, 0x00,
-                                                               0x01};
+
+    static constexpr const size_t requestMsgLength = PLDM_DF_OPEN_REQ_BYTES;
+    std::array<uint8_t, requestMsgLength> requestMsg = {0x00, 0x02, 0x00, 0x01};
 
     const struct pldm_file_df_close_req req_data = {file_descriptor,
                                                     df_close_options};
 
-    PLDM_MSG_DEFINE_P(requestPtr, PLDM_DF_OPEN_REQ_BYTES);
+    PLDM_MSG_DEFINE_P(requestPtr, requestMsgLength);
+    size_t payload_length = requestMsgLength;
     auto rc = encode_pldm_file_df_close_req(instance_id, &req_data, requestPtr,
-                                            PLDM_DF_CLOSE_REQ_BYTES);
+                                            &payload_length);
 
     ASSERT_EQ(rc, 0);
     EXPECT_EQ(
         0, memcmp(requestPtr->payload, requestMsg.data(), sizeof(requestMsg)));
+    EXPECT_EQ(payload_length, requestMsgLength);
 }
 #endif
 
@@ -197,16 +210,19 @@
     df_close_options.value = 0x0100;
     int rc;
 
+    static constexpr const size_t requestMsgLength = PLDM_DF_OPEN_REQ_BYTES;
+
     const struct pldm_file_df_close_req req_data = {file_descriptor,
                                                     df_close_options};
 
-    PLDM_MSG_DEFINE_P(requestPtr, PLDM_DF_CLOSE_REQ_BYTES);
+    PLDM_MSG_DEFINE_P(requestPtr, requestMsgLength);
+    size_t payload_length = requestMsgLength;
     rc = encode_pldm_file_df_close_req(instance_id, &req_data, nullptr,
-                                       PLDM_DF_CLOSE_REQ_BYTES);
+                                       &payload_length);
     EXPECT_EQ(rc, -EINVAL);
 
     rc = encode_pldm_file_df_close_req(instance_id, nullptr, requestPtr,
-                                       PLDM_DF_CLOSE_REQ_BYTES);
+                                       &payload_length);
     EXPECT_EQ(rc, -EINVAL);
 }
 #endif
@@ -220,11 +236,15 @@
     df_close_options.value = 0x0100;
     int rc;
 
+    static constexpr const size_t requestMsgLength = PLDM_DF_OPEN_REQ_BYTES;
+
     const struct pldm_file_df_close_req req_data = {file_descriptor,
                                                     df_close_options};
 
-    PLDM_MSG_DEFINE_P(requestPtr, PLDM_DF_CLOSE_REQ_BYTES);
-    rc = encode_pldm_file_df_close_req(instance_id, &req_data, requestPtr, 1);
+    PLDM_MSG_DEFINE_P(requestPtr, requestMsgLength);
+    size_t payload_length = 1;
+    rc = encode_pldm_file_df_close_req(instance_id, &req_data, requestPtr,
+                                       &payload_length);
     EXPECT_EQ(rc, -EOVERFLOW);
 }
 #endif
@@ -293,19 +313,24 @@
     uint8_t instance_id = 0;
     uint16_t file_descriptor = 0x0200;
     uint32_t requester_max_interval = 0x88130000; // 5000 ms
-    std::array<uint8_t, PLDM_DF_HEARTBEAT_REQ_BYTES> requestMsg = {
-        0x00, 0x02, 0x00, 0x00, 0x13, 0x88};
+
+    static constexpr const size_t requestMsgLength =
+        PLDM_DF_HEARTBEAT_REQ_BYTES;
+    std::array<uint8_t, requestMsgLength> requestMsg = {0x00, 0x02, 0x00,
+                                                        0x00, 0x13, 0x88};
 
     const struct pldm_file_df_heartbeat_req req_data = {file_descriptor,
                                                         requester_max_interval};
 
-    PLDM_MSG_DEFINE_P(requestPtr, PLDM_DF_HEARTBEAT_REQ_BYTES);
-    auto rc = encode_pldm_file_df_heartbeat_req(
-        instance_id, &req_data, requestPtr, PLDM_DF_HEARTBEAT_REQ_BYTES);
+    PLDM_MSG_DEFINE_P(requestPtr, requestMsgLength);
+    size_t payload_length = requestMsgLength;
+    auto rc = encode_pldm_file_df_heartbeat_req(instance_id, &req_data,
+                                                requestPtr, &payload_length);
 
     ASSERT_EQ(rc, 0);
     EXPECT_EQ(
         0, memcmp(requestPtr->payload, requestMsg.data(), sizeof(requestMsg)));
+    EXPECT_EQ(payload_length, requestMsgLength);
 }
 #endif
 
@@ -317,16 +342,20 @@
     uint32_t requester_max_interval = 0x88130000; // 5000 ms
     int rc;
 
+    static constexpr const size_t requestMsgLength =
+        PLDM_DF_HEARTBEAT_REQ_BYTES;
+
     const struct pldm_file_df_heartbeat_req req_data = {file_descriptor,
                                                         requester_max_interval};
 
-    PLDM_MSG_DEFINE_P(requestPtr, PLDM_DF_HEARTBEAT_REQ_BYTES);
+    PLDM_MSG_DEFINE_P(requestPtr, requestMsgLength);
+    size_t payload_length = requestMsgLength;
     rc = encode_pldm_file_df_heartbeat_req(instance_id, &req_data, nullptr,
-                                           PLDM_DF_HEARTBEAT_REQ_BYTES);
+                                           &payload_length);
     EXPECT_EQ(rc, -EINVAL);
 
     rc = encode_pldm_file_df_heartbeat_req(instance_id, nullptr, requestPtr,
-                                           PLDM_DF_HEARTBEAT_REQ_BYTES);
+                                           &payload_length);
     EXPECT_EQ(rc, -EINVAL);
 }
 #endif
@@ -339,12 +368,16 @@
     uint32_t requester_max_interval = 0x88130000; // 5000 ms
     int rc;
 
+    static constexpr const size_t requestMsgLength =
+        PLDM_DF_HEARTBEAT_REQ_BYTES;
+
     const struct pldm_file_df_heartbeat_req req_data = {file_descriptor,
                                                         requester_max_interval};
 
-    PLDM_MSG_DEFINE_P(requestPtr, PLDM_DF_HEARTBEAT_REQ_BYTES);
+    PLDM_MSG_DEFINE_P(requestPtr, requestMsgLength);
+    size_t payload_length = 1;
     rc = encode_pldm_file_df_heartbeat_req(instance_id, &req_data, requestPtr,
-                                           1);
+                                           &payload_length);
     EXPECT_EQ(rc, -EOVERFLOW);
 }
 #endif
@@ -483,7 +516,7 @@
     EXPECT_EQ(rc, -EINVAL);
 
     rc = encode_pldm_file_df_open_req(instance_id, nullptr, responsePtr,
-                                      PLDM_DF_OPEN_RESP_BYTES);
+                                      &payload_length);
     EXPECT_EQ(rc, -EINVAL);
 }
 #endif