bios: Implement SetBiosAttributeCurrentValue encode/decode API

Implement encode/decode APIs for SetBiosAttributeCurrentValue
command for both request and responder flow. The unit tests
functions also have been added to check these APIs.

Signed-off-by: John Wang <wangzqbj@inspur.com>
Change-Id: I5ae03778f2c47df6fc7f41c2ccb41f040cd85359
diff --git a/libpldm/bios.c b/libpldm/bios.c
index 212ed79..017ae4a 100644
--- a/libpldm/bios.c
+++ b/libpldm/bios.c
@@ -208,3 +208,108 @@
 	}
 	return PLDM_SUCCESS;
 }
+int encode_set_bios_attribute_current_value_req(
+    uint8_t instance_id, uint32_t transfer_handle, uint8_t transfer_flag,
+    const uint8_t *attribute_data, size_t attribute_length,
+    struct pldm_msg *msg, size_t payload_lenth)
+{
+	if (msg == NULL || attribute_data == NULL) {
+		return PLDM_ERROR_INVALID_DATA;
+	}
+	if (PLDM_SET_BIOS_ATTR_CURR_VAL_MIN_REQ_BYTES + attribute_length !=
+	    payload_lenth) {
+		return PLDM_ERROR_INVALID_LENGTH;
+	}
+	struct pldm_header_info header = {0};
+	header.instance = instance_id;
+	header.msg_type = PLDM_REQUEST;
+	header.pldm_type = PLDM_BIOS;
+	header.command = PLDM_SET_BIOS_ATTRIBUTE_CURRENT_VALUE;
+	pack_pldm_header(&header, &msg->hdr);
+
+	struct pldm_set_bios_attribute_current_value_req *request =
+	    (struct pldm_set_bios_attribute_current_value_req *)msg->payload;
+	request->transfer_handle = htole32(transfer_handle);
+	request->transfer_flag = transfer_flag;
+	memcpy(request->attribute_data, attribute_data, attribute_length);
+
+	return PLDM_SUCCESS;
+}
+
+int decode_set_bios_attribute_current_value_resp(const struct pldm_msg *msg,
+						 size_t payload_length,
+						 uint8_t *completion_code,
+						 uint32_t *next_transfer_handle)
+{
+	if (msg == NULL || completion_code == NULL ||
+	    next_transfer_handle == NULL) {
+		return PLDM_ERROR_INVALID_DATA;
+	}
+	if (payload_length != PLDM_SET_BIOS_ATTR_CURR_VAL_RESP_BYTES) {
+		return PLDM_ERROR_INVALID_LENGTH;
+	}
+
+	struct pldm_set_bios_attribute_current_value_resp *response =
+	    (struct pldm_set_bios_attribute_current_value_resp *)msg->payload;
+
+	*completion_code = response->completion_code;
+	if (PLDM_SUCCESS != *completion_code) {
+		return PLDM_SUCCESS;
+	}
+	*next_transfer_handle = le32toh(response->next_transfer_handle);
+
+	return PLDM_SUCCESS;
+}
+
+int decode_set_bios_attribute_current_value_req(const struct pldm_msg *msg,
+						size_t payload_length,
+						uint32_t *transfer_handle,
+						uint8_t *transfer_flag,
+						uint8_t *attribute_data,
+						size_t *attribute_length)
+{
+	if (msg == NULL || transfer_handle == NULL || transfer_flag == NULL ||
+	    attribute_data == NULL || attribute_length == NULL) {
+		return PLDM_ERROR_INVALID_DATA;
+	}
+	if (payload_length < PLDM_SET_BIOS_ATTR_CURR_VAL_MIN_REQ_BYTES) {
+		return PLDM_ERROR_INVALID_LENGTH;
+	}
+
+	struct pldm_set_bios_attribute_current_value_req *request =
+	    (struct pldm_set_bios_attribute_current_value_req *)msg->payload;
+	*transfer_handle = le32toh(request->transfer_handle);
+	*transfer_flag = request->transfer_flag;
+	*attribute_length =
+	    payload_length - PLDM_SET_BIOS_ATTR_CURR_VAL_MIN_REQ_BYTES;
+	memcpy(attribute_data, request->attribute_data, *attribute_length);
+
+	return PLDM_SUCCESS;
+}
+
+int encode_set_bios_attribute_current_value_resp(uint8_t instance_id,
+						 uint8_t completion_code,
+						 uint32_t next_transfer_handle,
+						 struct pldm_msg *msg)
+{
+	if (msg == NULL) {
+		return PLDM_ERROR_INVALID_DATA;
+	}
+	struct pldm_header_info header = {0};
+	header.instance = instance_id;
+	header.msg_type = PLDM_RESPONSE;
+	header.pldm_type = PLDM_BIOS;
+	header.command = PLDM_SET_BIOS_ATTRIBUTE_CURRENT_VALUE;
+
+	int rc = pack_pldm_header(&header, &msg->hdr);
+	if (rc != PLDM_SUCCESS) {
+		return rc;
+	}
+
+	struct pldm_set_bios_attribute_current_value_resp *response =
+	    (struct pldm_set_bios_attribute_current_value_resp *)msg->payload;
+	response->completion_code = completion_code;
+	response->next_transfer_handle = htole32(next_transfer_handle);
+
+	return PLDM_SUCCESS;
+}
diff --git a/libpldm/bios.h b/libpldm/bios.h
index c018d07..1856b8d 100644
--- a/libpldm/bios.h
+++ b/libpldm/bios.h
@@ -16,6 +16,8 @@
 
 #define PLDM_GET_BIOS_TABLE_REQ_BYTES 6
 #define PLDM_GET_BIOS_TABLE_MIN_RESP_BYTES 6
+#define PLDM_SET_BIOS_ATTR_CURR_VAL_MIN_REQ_BYTES 5
+#define PLDM_SET_BIOS_ATTR_CURR_VAL_RESP_BYTES 5
 #define PLDM_GET_BIOS_ATTR_CURR_VAL_BY_HANDLE_REQ_BYTES 7
 
 enum pldm_bios_completion_codes {
@@ -25,6 +27,7 @@
 };
 enum pldm_bios_commands {
 	PLDM_GET_BIOS_TABLE = 0x01,
+	PLDM_SET_BIOS_ATTRIBUTE_CURRENT_VALUE = 0x07,
 	PLDM_GET_BIOS_ATTRIBUTE_CURRENT_VALUE_BY_HANDLE = 0x08,
 	PLDM_GET_DATE_TIME = 0x0c,
 };
@@ -126,6 +129,27 @@
 	uint8_t attribute_data[1];
 } __attribute__((packed));
 
+/** @struct pldm_set_bios_attribute_current_value_req
+ *
+ *  structure representing SetBiosAttributeCurrentValue request packet
+ *
+ */
+struct pldm_set_bios_attribute_current_value_req {
+	uint32_t transfer_handle;
+	uint8_t transfer_flag;
+	uint8_t attribute_data[1];
+} __attribute__((packed));
+
+/** @struct pldm_set_bios_attribute_current_value_resp
+ *
+ *  structure representing SetBiosCurrentValue response packet
+ *
+ */
+struct pldm_set_bios_attribute_current_value_resp {
+	uint8_t completion_code;
+	uint32_t next_transfer_handle;
+} __attribute__((packed));
+
 /* Requester */
 
 /* GetDateTime */
@@ -159,6 +183,40 @@
 			      uint8_t *minutes, uint8_t *hours, uint8_t *day,
 			      uint8_t *month, uint16_t *year);
 
+/* SetBiosAttributeCurrentValue */
+
+/** @brief Create a PLDM request message for SetBiosAttributeCurrentValue
+ *
+ *  @param[in] instance_id - Message's instance id
+ *  @param[in] transfer_handle - Handle to identify a BIOS table transfer
+ *  @param[in] transfer_flag - Flag to indicate what part of the transfer
+ * this request represents
+ *  @param[in] attribute_data - Contains current value of attribute
+ *  @param[in] attribute_length - Length of attribute
+ *  @param[out] msg - Message will be written to this
+ *  @param[in] payload_length - Length of message payload
+ *  @return pldm_completion_codes
+ *  @note  Caller is responsible for memory alloc and dealloc of params
+ *         'msg.payload'
+ */
+int encode_set_bios_attribute_current_value_req(
+    uint8_t instance_id, uint32_t transfer_handle, uint8_t transfer_flag,
+    const uint8_t *attribute_data, size_t attribute_length,
+    struct pldm_msg *msg, size_t payload_length);
+
+/** @brief Decode a SetBiosAttributeCurrentValue response message
+ *
+ *  @param[in] msg - Response message
+ *  @param[in] payload_length - Length of response message payload
+ *  @param[out] completion_code - Pointer to response msg's PLDM completion code
+ *  @param[out] next_transfer_handle - Pointer to a handle that identify the
+ * next portion of the transfer
+ *  @return pldm_completion_codes
+ */
+int decode_set_bios_attribute_current_value_resp(
+    const struct pldm_msg *msg, size_t payload_length, uint8_t *completion_code,
+    uint32_t *next_transfer_handle);
+
 /* Responder */
 
 /* GetDateTime */
@@ -254,6 +312,38 @@
     uint8_t transfer_flag, const uint8_t *attribute_data,
     size_t attribute_length, struct pldm_msg *msg);
 
+/* SetBiosAttributeCurrentValue */
+
+/** @brief Decode SetBIOSAttributeCurrentValue request packet
+ *
+ *  @param[in] msg - Request message
+ *  @param[in] payload_length - Length of request message payload
+ *  @param[out] transfer_handle - Handle to identify a BIOS table transfer
+ *  @param[out] transfer_flag - Flag to indicate what part of the transfer
+ * this request represents
+ *  @param[out] attribute_data - Contains current value of attribute
+ *  @param[out] attribute_length - Pointer to length of attribute
+ *  @return pldm_completion_codes
+ */
+int decode_set_bios_attribute_current_value_req(const struct pldm_msg *msg,
+						size_t payload_length,
+						uint32_t *transfer_handle,
+						uint8_t *transfer_flag,
+						uint8_t *attribute_data,
+						size_t *attribute_length);
+
+/** @brief Create a PLDM response message for SetBiosAttributeCurrentValue
+ *
+ *  @param[in] instance_id - Message's instance id
+ *  @param[in] completion_code - PLDM completion code
+ *  @param[in] next_transfer_handle - handle to identify the next portion of the
+ *  @param[out] msg - Message will be written to this
+ */
+int encode_set_bios_attribute_current_value_resp(uint8_t instance_id,
+						 uint8_t completion_code,
+						 uint32_t next_transfer_handle,
+						 struct pldm_msg *msg);
+
 #ifdef __cplusplus
 }
 #endif
diff --git a/test/libpldm_bios_test.cpp b/test/libpldm_bios_test.cpp
index 00a160f..0891142 100644
--- a/test/libpldm_bios_test.cpp
+++ b/test/libpldm_bios_test.cpp
@@ -329,3 +329,181 @@
         sizeof(attributeData), response);
     ASSERT_EQ(rc, PLDM_ERROR_INVALID_DATA);
 }
+
+TEST(SetBiosAttributeCurrentValue, testGoodEncodeRequest)
+{
+    uint8_t instanceId = 10;
+    uint32_t transferHandle = 32;
+    uint8_t transferFlag = 8;
+    uint32_t attributeData;
+    std::array<uint8_t, hdrSize + PLDM_SET_BIOS_ATTR_CURR_VAL_MIN_REQ_BYTES +
+                            sizeof(attributeData)>
+        requestMsg{};
+    auto request = reinterpret_cast<pldm_msg*>(requestMsg.data());
+    auto rc = encode_set_bios_attribute_current_value_req(
+        instanceId, transferHandle, transferFlag,
+        reinterpret_cast<uint8_t*>(&attributeData), sizeof(attributeData),
+        request, requestMsg.size() - hdrSize);
+
+    ASSERT_EQ(rc, PLDM_SUCCESS);
+
+    struct pldm_set_bios_attribute_current_value_req* req =
+        reinterpret_cast<struct pldm_set_bios_attribute_current_value_req*>(
+            request->payload);
+    ASSERT_EQ(htole32(transferHandle), req->transfer_handle);
+    ASSERT_EQ(transferFlag, req->transfer_flag);
+    ASSERT_EQ(
+        0, memcmp(&attributeData, req->attribute_data, sizeof(attributeData)));
+}
+
+TEST(SetBiosAttributeCurrentValue, testBadEncodeRequest)
+{
+    uint8_t instanceId = 10;
+    uint32_t transferHandle = 32;
+    uint8_t transferFlag = 8;
+    uint32_t attributeData;
+    std::array<uint8_t, hdrSize + PLDM_SET_BIOS_ATTR_CURR_VAL_MIN_REQ_BYTES>
+        requestMsg{};
+    auto request = reinterpret_cast<pldm_msg*>(requestMsg.data());
+
+    auto rc = encode_set_bios_attribute_current_value_req(
+        instanceId, transferHandle, transferFlag, nullptr, 0, nullptr, 0);
+    ASSERT_EQ(rc, PLDM_ERROR_INVALID_DATA);
+    rc = encode_set_bios_attribute_current_value_req(
+        instanceId, transferHandle, transferFlag,
+        reinterpret_cast<uint8_t*>(&attributeData), sizeof(attributeData),
+        request, requestMsg.size() - hdrSize);
+
+    ASSERT_EQ(rc, PLDM_ERROR_INVALID_LENGTH);
+}
+
+TEST(SetBiosAttributeCurrentValue, testGoodDecodeRequest)
+{
+    uint32_t transferHandle = 32;
+    uint8_t transferFlag;
+    uint32_t attributeData;
+
+    std::array<uint8_t, hdrSize + PLDM_SET_BIOS_ATTR_CURR_VAL_MIN_REQ_BYTES +
+                            sizeof(attributeData)>
+        requestMsg{};
+    auto request = reinterpret_cast<struct pldm_msg*>(requestMsg.data());
+    struct pldm_set_bios_attribute_current_value_req* req =
+        reinterpret_cast<struct pldm_set_bios_attribute_current_value_req*>(
+            request->payload);
+    req->transfer_handle = htole32(transferHandle);
+    req->transfer_flag = transferFlag;
+    memcpy(req->attribute_data, &attributeData, sizeof(attributeData));
+
+    uint32_t retTransferHandle;
+    uint8_t retTransferFlag;
+    uint32_t retAttributeData;
+    size_t retAttributeDataLength;
+    auto rc = decode_set_bios_attribute_current_value_req(
+        request, requestMsg.size() - hdrSize, &retTransferHandle,
+        &retTransferFlag, reinterpret_cast<uint8_t*>(&retAttributeData),
+        &retAttributeDataLength);
+
+    ASSERT_EQ(rc, PLDM_SUCCESS);
+    ASSERT_EQ(retTransferHandle, transferHandle);
+    ASSERT_EQ(retTransferFlag, transferFlag);
+    ASSERT_EQ(retAttributeDataLength, sizeof(attributeData));
+    ASSERT_EQ(0,
+              memcmp(&retAttributeData, &attributeData, sizeof(attributeData)));
+}
+
+TEST(SetBiosAttributeCurrentValue, testBadDecodeRequest)
+{
+    uint32_t transferHandle = 32;
+    uint8_t transferFlag;
+    uint32_t attributeData;
+    size_t attributeDataLength;
+    std::array<uint8_t, hdrSize + PLDM_SET_BIOS_ATTR_CURR_VAL_MIN_REQ_BYTES - 1>
+        requestMsg{};
+    auto request = reinterpret_cast<struct pldm_msg*>(requestMsg.data());
+
+    auto rc = decode_set_bios_attribute_current_value_req(
+        nullptr, 0, &transferHandle, &transferFlag,
+        reinterpret_cast<uint8_t*>(&attributeData), &attributeDataLength);
+    ASSERT_EQ(rc, PLDM_ERROR_INVALID_DATA);
+    rc = decode_set_bios_attribute_current_value_req(
+        request, requestMsg.size() - hdrSize, &transferHandle, &transferFlag,
+        reinterpret_cast<uint8_t*>(&attributeData), &attributeDataLength);
+    ASSERT_EQ(rc, PLDM_ERROR_INVALID_LENGTH);
+}
+
+TEST(SetBiosAttributeCurrentValue, testGoodEncodeResponse)
+{
+    uint8_t instanceId = 10;
+    uint32_t nextTransferHandle = 32;
+    uint8_t completionCode = PLDM_SUCCESS;
+
+    std::array<uint8_t, hdrSize + PLDM_SET_BIOS_ATTR_CURR_VAL_RESP_BYTES>
+        responseMsg{};
+    struct pldm_msg* response =
+        reinterpret_cast<struct pldm_msg*>(responseMsg.data());
+    auto rc = encode_set_bios_attribute_current_value_resp(
+        instanceId, completionCode, nextTransferHandle, response);
+    ASSERT_EQ(rc, PLDM_SUCCESS);
+
+    struct pldm_set_bios_attribute_current_value_resp* resp =
+        reinterpret_cast<struct pldm_set_bios_attribute_current_value_resp*>(
+            response->payload);
+    ASSERT_EQ(completionCode, resp->completion_code);
+    ASSERT_EQ(htole32(nextTransferHandle), resp->next_transfer_handle);
+}
+
+TEST(SetBiosAttributeCurrentValue, testBadEncodeResponse)
+{
+    uint8_t instanceId = 10;
+    uint32_t nextTransferHandle = 32;
+    uint8_t completionCode = PLDM_SUCCESS;
+    auto rc = encode_set_bios_attribute_current_value_resp(
+        instanceId, completionCode, nextTransferHandle, nullptr);
+
+    ASSERT_EQ(rc, PLDM_ERROR_INVALID_DATA);
+}
+TEST(SetBiosAttributeCurrentValue, testGoodDecodeResponse)
+{
+    uint32_t nextTransferHandle = 32;
+    uint8_t completionCode = PLDM_SUCCESS;
+    std::array<uint8_t, hdrSize + PLDM_SET_BIOS_ATTR_CURR_VAL_RESP_BYTES>
+        responseMsg{};
+    struct pldm_msg* response =
+        reinterpret_cast<struct pldm_msg*>(responseMsg.data());
+    struct pldm_set_bios_attribute_current_value_resp* resp =
+        reinterpret_cast<struct pldm_set_bios_attribute_current_value_resp*>(
+            response->payload);
+
+    resp->completion_code = completionCode;
+    resp->next_transfer_handle = htole32(nextTransferHandle);
+
+    uint8_t retCompletionCode;
+    uint32_t retNextTransferHandle;
+    auto rc = decode_set_bios_attribute_current_value_resp(
+        response, responseMsg.size() - hdrSize, &retCompletionCode,
+        &retNextTransferHandle);
+
+    ASSERT_EQ(rc, PLDM_SUCCESS);
+    ASSERT_EQ(completionCode, retCompletionCode);
+    ASSERT_EQ(nextTransferHandle, retNextTransferHandle);
+}
+
+TEST(SetBiosAttributeCurrentValue, testBadDecodeResponse)
+{
+    uint32_t nextTransferHandle = 32;
+    uint8_t completionCode = PLDM_SUCCESS;
+
+    std::array<uint8_t, hdrSize + PLDM_SET_BIOS_ATTR_CURR_VAL_RESP_BYTES - 1>
+        responseMsg{};
+    struct pldm_msg* response =
+        reinterpret_cast<struct pldm_msg*>(responseMsg.data());
+    auto rc = decode_set_bios_attribute_current_value_resp(
+        nullptr, 0, &completionCode, &nextTransferHandle);
+    ASSERT_EQ(rc, PLDM_ERROR_INVALID_DATA);
+
+    rc = decode_set_bios_attribute_current_value_resp(
+        response, responseMsg.size() - hdrSize, &completionCode,
+        &nextTransferHandle);
+
+    ASSERT_EQ(rc, PLDM_ERROR_INVALID_LENGTH);
+}
\ No newline at end of file