libpldm: Add encode API for CancelUpdateComponent request

Update agent send this command to FD/FDP during firmware component
transfer process. The FD/FDP, upon receiving this command shall stop
sending RequestFirmwareData commands to the UA, and cancel the
current component update procedure. The FD/FDP controller shall
transition to the READY XFER state of update mode and be ready to
accept another UpdateComponent command. This implementation works
with DSP0267_1.1.0, DSP0267_1.0.1 and DSP0267_1.0.0.

Tested: Unit tests passed

Signed-off-by: gokulsanker <gokul.sanker.v.g@intel.com>
Change-Id: I95563734c9d22c435a59b697fb784b7acfa897a7
diff --git a/libpldm/firmware_update.c b/libpldm/firmware_update.c
index 370f37a..0df6aaf 100644
--- a/libpldm/firmware_update.c
+++ b/libpldm/firmware_update.c
@@ -1442,3 +1442,28 @@
 

 	return PLDM_SUCCESS;

 }

+

+int encode_cancel_update_component_req(uint8_t instance_id,

+				       struct pldm_msg *msg,

+				       size_t payload_length)

+{

+	if (msg == NULL) {

+		return PLDM_ERROR_INVALID_DATA;

+	}

+

+	if (payload_length != PLDM_CANCEL_UPDATE_COMPONENT_REQ_BYTES) {

+		return PLDM_ERROR_INVALID_LENGTH;

+	}

+

+	struct pldm_header_info header = {0};

+	header.instance = instance_id;

+	header.msg_type = PLDM_REQUEST;

+	header.pldm_type = PLDM_FWUP;

+	header.command = PLDM_CANCEL_UPDATE_COMPONENT;

+	uint8_t rc = pack_pldm_header(&header, &(msg->hdr));

+	if (rc) {

+		return rc;

+	}

+

+	return PLDM_SUCCESS;

+}

diff --git a/libpldm/firmware_update.h b/libpldm/firmware_update.h
index a7e5023..b66e2e1 100644
--- a/libpldm/firmware_update.h
+++ b/libpldm/firmware_update.h
@@ -21,6 +21,7 @@
 #define PLDM_GET_STATUS_REQ_BYTES 0

 /* Maximum progress percentage value*/

 #define PLDM_FWUP_MAX_PROGRESS_PERCENT 0x65

+#define PLDM_CANCEL_UPDATE_COMPONENT_REQ_BYTES 0

 

 /** @brief PLDM Firmware update commands

  */

@@ -35,7 +36,8 @@
 	PLDM_VERIFY_COMPLETE = 0x17,

 	PLDM_APPLY_COMPLETE = 0x18,

 	PLDM_ACTIVATE_FIRMWARE = 0x1A,

-	PLDM_GET_STATUS = 0x1B

+	PLDM_GET_STATUS = 0x1B,

+	PLDM_CANCEL_UPDATE_COMPONENT = 0x1C

 };

 

 /** @brief PLDM Firmware update completion codes

@@ -1044,6 +1046,21 @@
 			   uint8_t *reason_code,

 			   bitfield32_t *update_option_flags_enabled);

 

+/** @brief Create PLDM request message for CancelUpdateComponent

+ *

+ *  @param[in] instance_id - Message's instance id

+ *  @param[in,out] msg - Message will be written to this

+ *  @param[in] payload_length - Length of request message payload

+ *

+ *  @return pldm_completion_codes

+ *

+ *  @note  Caller is responsible for memory alloc and dealloc of param

+ *         'msg.payload'

+ */

+int encode_cancel_update_component_req(uint8_t instance_id,

+				       struct pldm_msg *msg,

+				       size_t payload_length);

+

 #ifdef __cplusplus

 }

 #endif

diff --git a/libpldm/tests/libpldm_firmware_update_test.cpp b/libpldm/tests/libpldm_firmware_update_test.cpp
index c8e2954..22ee122 100644
--- a/libpldm/tests/libpldm_firmware_update_test.cpp
+++ b/libpldm/tests/libpldm_firmware_update_test.cpp
@@ -2714,3 +2714,31 @@
         &progressPercent, &reasonCode, &updateOptionFlagsEnabled);

     EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);

 }

+

+TEST(CancelUpdateComponent, goodPathEncodeRequest)

+{

+    constexpr uint8_t instanceId = 9;

+    std::array<uint8_t, hdrSize> request{};

+    auto requestMsg = reinterpret_cast<pldm_msg*>(request.data());

+

+    auto rc = encode_cancel_update_component_req(

+        instanceId, requestMsg, PLDM_CANCEL_UPDATE_COMPONENT_REQ_BYTES);

+    EXPECT_EQ(rc, PLDM_SUCCESS);

+

+    constexpr std::array<uint8_t, hdrSize> outRequest{0x89, 0x05, 0x1C};

+    EXPECT_EQ(request, outRequest);

+}

+

+TEST(CancelUpdateComponent, errorPathEncodeRequest)

+{

+    std::array<uint8_t, hdrSize + sizeof(uint8_t)> request{};

+    auto requestMsg = reinterpret_cast<pldm_msg*>(request.data());

+

+    auto rc = encode_cancel_update_component_req(

+        0, nullptr, PLDM_CANCEL_UPDATE_COMPONENT_REQ_BYTES);

+    EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);

+

+    rc = encode_cancel_update_component_req(

+        0, requestMsg, PLDM_CANCEL_UPDATE_COMPONENT_REQ_BYTES + 1);

+    EXPECT_EQ(rc, PLDM_ERROR_INVALID_LENGTH);

+}