libpldm: implement responder flow for SetBIOSTable

For the responder, need to encode responder data and send requester,
waiting for recive requester data to decode, so need to implement
encode/decode(Responder flow only) for SetBIOSTable.

Tested: the unit tests functions have been added to check these APIs.

Signed-off-by: George Liu <liuxiwei@inspur.com>
Change-Id: I74b736adc3c78a8197eadfa66804971b57ca42fd
diff --git a/libpldm/bios.c b/libpldm/bios.c
index bae2d37..6dbc0b0 100644
--- a/libpldm/bios.c
+++ b/libpldm/bios.c
@@ -606,3 +606,55 @@
 
 	return PLDM_SUCCESS;
 }
+
+int encode_set_bios_table_resp(uint8_t instance_id, uint8_t completion_code,
+			       uint32_t next_transfer_handle,
+			       struct pldm_msg *msg)
+{
+	int rc = PLDM_SUCCESS;
+
+	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_TABLE;
+
+	if ((rc = pack_pldm_header(&header, &(msg->hdr))) > PLDM_SUCCESS) {
+		return rc;
+	}
+
+	struct pldm_set_bios_table_resp *response =
+	    (struct pldm_set_bios_table_resp *)msg->payload;
+	response->completion_code = completion_code;
+	response->next_transfer_handle = htole32(next_transfer_handle);
+
+	return PLDM_SUCCESS;
+}
+
+int decode_set_bios_table_req(const struct pldm_msg *msg, size_t payload_length,
+			      uint32_t *transfer_handle, uint8_t *transfer_flag,
+			      uint8_t *table_type, struct variable_field *table)
+{
+	if (msg == NULL || transfer_handle == NULL || transfer_flag == NULL ||
+	    table_type == NULL || table == NULL) {
+		return PLDM_ERROR_INVALID_DATA;
+	}
+
+	if (payload_length < PLDM_SET_BIOS_TABLE_MIN_REQ_BYTES) {
+		return PLDM_ERROR_INVALID_LENGTH;
+	}
+
+	struct pldm_set_bios_table_req *request =
+	    (struct pldm_set_bios_table_req *)msg->payload;
+	*transfer_handle = le32toh(request->transfer_handle);
+	*transfer_flag = request->transfer_flag;
+	*table_type = request->table_type;
+	table->length = payload_length - PLDM_SET_BIOS_TABLE_MIN_REQ_BYTES;
+	table->ptr = request->table_data;
+
+	return PLDM_SUCCESS;
+}
diff --git a/libpldm/bios.h b/libpldm/bios.h
index 89182e3..a23a671 100644
--- a/libpldm/bios.h
+++ b/libpldm/bios.h
@@ -578,6 +578,39 @@
 int decode_set_date_time_resp(const struct pldm_msg *msg, size_t payload_length,
 			      uint8_t *completion_code);
 
+/* SetBIOSTable */
+
+/** @brief Create a PLDM response message for SetBIOSTable
+ *
+ *  @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
+ *             transfer
+ *  @param[out] msg - Message will be written to this
+ */
+int encode_set_bios_table_resp(uint8_t instance_id, uint8_t completion_code,
+			       uint32_t next_transfer_handle,
+			       struct pldm_msg *msg);
+
+/** @brief Decode SetBIOSTable 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] table_type - Indicates what table is being transferred
+ *             {BIOSStringTable=0x0, BIOSAttributeTable=0x1,
+ *              BIOSAttributeValueTable=0x2}
+ *  @param[out] table - Struct variable_field, contains data specific to the
+ * 				table type and the length of table data.
+ *  @return pldm_completion_codes
+ */
+int decode_set_bios_table_req(const struct pldm_msg *msg, size_t payload_length,
+			      uint32_t *transfer_handle, uint8_t *transfer_flag,
+			      uint8_t *table_type,
+			      struct variable_field *table);
+
 #ifdef __cplusplus
 }
 #endif
diff --git a/libpldm/tests/libpldm_bios_test.cpp b/libpldm/tests/libpldm_bios_test.cpp
index bfaefc8..4733f2d 100644
--- a/libpldm/tests/libpldm_bios_test.cpp
+++ b/libpldm/tests/libpldm_bios_test.cpp
@@ -998,3 +998,96 @@
                                     &retCompletionCode, &retNextTransferHandle);
     EXPECT_EQ(rc, PLDM_ERROR_INVALID_LENGTH);
 }
+
+TEST(SetBIOSTable, testGoodEncodeResponse)
+{
+    uint8_t instanceId = 10;
+    uint32_t nextTransferHandle = 32;
+    uint8_t completionCode = PLDM_SUCCESS;
+
+    std::array<uint8_t, hdrSize + PLDM_SET_BIOS_TABLE_RESP_BYTES> responseMsg{};
+    struct pldm_msg* response =
+        reinterpret_cast<struct pldm_msg*>(responseMsg.data());
+    auto rc = encode_set_bios_table_resp(instanceId, completionCode,
+                                         nextTransferHandle, response);
+    EXPECT_EQ(rc, PLDM_SUCCESS);
+
+    struct pldm_set_bios_table_resp* resp =
+        reinterpret_cast<struct pldm_set_bios_table_resp*>(response->payload);
+    EXPECT_EQ(completionCode, resp->completion_code);
+    EXPECT_EQ(htole32(nextTransferHandle), resp->next_transfer_handle);
+}
+
+TEST(SetBIOSTable, testBadEncodeResponse)
+{
+    auto rc = encode_set_bios_table_resp(0, PLDM_SUCCESS, 1, NULL);
+    EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
+}
+
+TEST(SetBIOSTable, testGoodDecodeRequest)
+{
+    uint32_t transferHandle = 32;
+    uint8_t transferFlag = PLDM_START_AND_END;
+    uint8_t tableType = PLDM_BIOS_STRING_TABLE;
+    uint32_t tableData = 44;
+
+    std::array<uint8_t,
+               hdrSize + PLDM_SET_BIOS_TABLE_MIN_REQ_BYTES + sizeof(tableData)>
+        requestMsg{};
+    auto request = reinterpret_cast<struct pldm_msg*>(requestMsg.data());
+    struct pldm_set_bios_table_req* req =
+        reinterpret_cast<struct pldm_set_bios_table_req*>(request->payload);
+    req->transfer_handle = htole32(transferHandle);
+    req->transfer_flag = transferFlag;
+    req->table_type = tableType;
+    memcpy(req->table_data, &tableData, sizeof(tableData));
+
+    uint32_t retTransferHandle;
+    uint8_t retTransferFlag;
+    uint8_t retTableType;
+    struct variable_field table;
+    auto rc = decode_set_bios_table_req(request, requestMsg.size() - hdrSize,
+                                        &retTransferHandle, &retTransferFlag,
+                                        &retTableType, &table);
+
+    EXPECT_EQ(rc, PLDM_SUCCESS);
+    EXPECT_EQ(retTransferHandle, transferHandle);
+    EXPECT_EQ(retTransferFlag, transferFlag);
+    EXPECT_EQ(retTableType, tableType);
+    EXPECT_EQ(table.length, sizeof(tableData));
+    EXPECT_EQ(0, memcmp(table.ptr, &tableData, sizeof(tableData)));
+}
+
+TEST(SetBIOSTable, testBadDecodeRequest)
+{
+    uint32_t transferHandle = 32;
+    uint8_t transferFlag = PLDM_START_AND_END;
+    uint8_t tableType = PLDM_BIOS_STRING_TABLE;
+    uint32_t tableData = 44;
+
+    std::array<uint8_t,
+               hdrSize + PLDM_SET_BIOS_TABLE_MIN_REQ_BYTES + sizeof(tableData)>
+        requestMsg{};
+    auto request = reinterpret_cast<struct pldm_msg*>(requestMsg.data());
+    struct pldm_set_bios_table_req* req =
+        reinterpret_cast<struct pldm_set_bios_table_req*>(request->payload);
+    req->transfer_handle = htole32(transferHandle);
+    req->transfer_flag = transferFlag;
+    req->table_type = tableType;
+    memcpy(req->table_data, &tableData, sizeof(tableData));
+
+    uint32_t retTransferHandle;
+    uint8_t retTransferFlag;
+    uint8_t retTableType;
+
+    auto rc = decode_set_bios_table_req(request, requestMsg.size() - hdrSize,
+                                        &retTransferHandle, &retTransferFlag,
+                                        &retTableType, NULL);
+    EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
+
+    struct variable_field table;
+    rc = decode_set_bios_table_req(
+        request, requestMsg.size() - hdrSize - sizeof(tableData) - 1,
+        &retTransferHandle, &retTransferFlag, &retTableType, &table);
+    EXPECT_EQ(rc, PLDM_ERROR_INVALID_LENGTH);
+}
\ No newline at end of file