Added encode API for SetTID cmd

Added encode API for SetTID command(0x01) which is defined in
DSP0240 Version 1.1.0 sec:8.1.1.

Signed-off-by: Gilbert Chen <gilbertc@nvidia.com>
Change-Id: Ia2d5c76e8ad545e794c72f5963556f1f0e6357fc
diff --git a/include/libpldm/base.h b/include/libpldm/base.h
index be1e355..6c2e094 100644
--- a/include/libpldm/base.h
+++ b/include/libpldm/base.h
@@ -25,6 +25,7 @@
 /** @brief PLDM Commands
  */
 enum pldm_supported_commands {
+	PLDM_SET_TID = 0x1,
 	PLDM_GET_TID = 0x2,
 	PLDM_GET_PLDM_VERSION = 0x3,
 	PLDM_GET_PLDM_TYPES = 0x4,
@@ -94,6 +95,7 @@
 /* Response lengths are inclusive of completion code */
 #define PLDM_GET_TYPES_RESP_BYTES 9
 #define PLDM_GET_TID_RESP_BYTES 2
+#define PLDM_SET_TID_RESP_BYTES 1
 #define PLDM_GET_COMMANDS_RESP_BYTES 33
 /* Response data has only one version and does not contain the checksum */
 #define PLDM_GET_VERSION_RESP_BYTES 10
@@ -214,6 +216,15 @@
 	uint8_t version_data[1];       //!< PLDM GetVersion version field
 } __attribute__((packed));
 
+/** @struct pldm_set_tid_req
+ *
+ *  Structure representing PLDM set tid request.
+ */
+
+struct pldm_set_tid_req {
+	uint8_t tid; //!< PLDM SetTID TID field
+} __attribute__((packed));
+
 /** @struct pldm_get_tid_resp
  *
  *  Structure representing PLDM get tid response.
@@ -499,6 +510,17 @@
 int encode_get_tid_resp(uint8_t instance_id, uint8_t completion_code,
 			uint8_t tid, struct pldm_msg *msg);
 
+/** @brief Create a PLDM request message for SetTID
+ *
+ *  @param[in] instance_id - Message's instance id
+ *  @param[in] tid - Terminus ID
+ *  @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.payload'
+ */
+int encode_set_tid_req(uint8_t instance_id, uint8_t tid, struct pldm_msg *msg);
+
 /* Responder */
 
 /* MultipartRecieve */
diff --git a/src/base.c b/src/base.c
index 9a5ae09..86bacea 100644
--- a/src/base.c
+++ b/src/base.c
@@ -398,6 +398,33 @@
 	return PLDM_SUCCESS;
 }
 
+int encode_set_tid_req(uint8_t instance_id, uint8_t tid, struct pldm_msg *msg)
+{
+	if (msg == NULL) {
+		return PLDM_ERROR_INVALID_DATA;
+	}
+
+	if (tid == 0x0 || tid == 0xff) {
+		return PLDM_ERROR_INVALID_DATA;
+	}
+
+	struct pldm_header_info header = {0};
+	header.instance = instance_id;
+	header.msg_type = PLDM_REQUEST;
+	header.command = PLDM_SET_TID;
+
+	uint8_t rc = pack_pldm_header(&header, &(msg->hdr));
+	if (rc != PLDM_SUCCESS) {
+		return rc;
+	}
+
+	struct pldm_set_tid_req *request =
+	    (struct pldm_set_tid_req *)msg->payload;
+	request->tid = tid;
+
+	return PLDM_SUCCESS;
+}
+
 int decode_multipart_receive_req(
     const struct pldm_msg *msg, size_t payload_length, uint8_t *pldm_type,
     uint8_t *transfer_opflag, uint32_t *transfer_ctx, uint32_t *transfer_handle,
diff --git a/tests/libpldm_base_test.cpp b/tests/libpldm_base_test.cpp
index b3b1383..cb36663 100644
--- a/tests/libpldm_base_test.cpp
+++ b/tests/libpldm_base_test.cpp
@@ -758,3 +758,37 @@
     rc = encode_cc_only_resp(0, 1, 2, 3, nullptr);
     EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
 }
+
+TEST(SetTID, testGoodEncodeRequest)
+{
+    uint8_t instanceId = 0;
+    uint8_t tid = 0x01;
+    std::array<uint8_t, sizeof(pldm_msg_hdr) + sizeof(tid)> requestMsg{};
+    auto request = reinterpret_cast<pldm_msg*>(requestMsg.data());
+
+    auto rc = encode_set_tid_req(instanceId, tid, request);
+    ASSERT_EQ(rc, PLDM_SUCCESS);
+
+    EXPECT_EQ(request->hdr.command, PLDM_SET_TID);
+    EXPECT_EQ(request->hdr.type, PLDM_BASE);
+    EXPECT_EQ(request->hdr.request, 1);
+    EXPECT_EQ(request->hdr.datagram, 0);
+    EXPECT_EQ(request->hdr.instance_id, instanceId);
+    EXPECT_EQ(0, memcmp(request->payload, &tid, sizeof(tid)));
+}
+
+TEST(SetTID, testBadEncodeRequest)
+{
+    uint8_t tid = 0x01;
+    std::array<uint8_t, sizeof(pldm_msg_hdr) + sizeof(tid)> requestMsg{};
+    auto request = reinterpret_cast<pldm_msg*>(requestMsg.data());
+
+    auto rc = encode_set_tid_req(0, tid, nullptr);
+    EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
+
+    rc = encode_set_tid_req(0, 0, request);
+    EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
+
+    rc = encode_set_tid_req(0, 0xff, request);
+    EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
+}