base: GetTID responder implementation

A pldm terminus is defined as the point of communication termination
for PLDM messages and the PLDM functions associated with those messages.

The Terminus ID(TID) is a value that identifies a PLDM terminus.

This commit assignes 1 to the BMC as the TID.

Signed-off-by: John Wang <wangzqbj@inspur.com>
Change-Id: I7adb0e1274f326fe6cf148771f230f530c9a567c
diff --git a/libpldm/base.c b/libpldm/base.c
index 36df1b0..5927ba1 100644
--- a/libpldm/base.c
+++ b/libpldm/base.c
@@ -322,3 +322,48 @@
 
 	return PLDM_SUCCESS;
 }
+
+int encode_get_tid_resp(uint8_t instance_id, uint8_t completion_code,
+			uint8_t tid, struct pldm_msg *msg)
+{
+	if (msg == NULL) {
+		return PLDM_ERROR_INVALID_DATA;
+	}
+
+	struct pldm_get_tid_resp *response =
+	    (struct pldm_get_tid_resp *)msg->payload;
+
+	response->completion_code = completion_code;
+	struct pldm_header_info header = {0};
+	header.instance = instance_id;
+	header.msg_type = PLDM_RESPONSE;
+	header.command = PLDM_GET_TID;
+	pack_pldm_header(&header, &(msg->hdr));
+
+	response->tid = tid;
+
+	return PLDM_SUCCESS;
+}
+
+int decode_get_tid_resp(const struct pldm_msg *msg, size_t payload_length,
+			uint8_t *completion_code, uint8_t *tid)
+{
+	if (msg == NULL || tid == NULL || completion_code == NULL) {
+		return PLDM_ERROR_INVALID_DATA;
+	}
+
+	if (payload_length != PLDM_GET_TID_RESP_BYTES) {
+		return PLDM_ERROR_INVALID_LENGTH;
+	}
+
+	struct pldm_get_tid_resp *response =
+	    (struct pldm_get_tid_resp *)msg->payload;
+	*completion_code = response->completion_code;
+	if (PLDM_SUCCESS != *completion_code) {
+		return PLDM_SUCCESS;
+	};
+
+	*tid = response->tid;
+
+	return PLDM_SUCCESS;
+}
diff --git a/libpldm/base.h b/libpldm/base.h
index f15ca7e..d30a300 100644
--- a/libpldm/base.h
+++ b/libpldm/base.h
@@ -23,6 +23,7 @@
 /** @brief PLDM Commands
  */
 enum pldm_supported_commands {
+	PLDM_GET_TID = 0x2,
 	PLDM_GET_PLDM_VERSION = 0x3,
 	PLDM_GET_PLDM_TYPES = 0x4,
 	PLDM_GET_PLDM_COMMANDS = 0x5
@@ -73,6 +74,7 @@
 
 /* Response lengths are inclusive of completion code */
 #define PLDM_GET_TYPES_RESP_BYTES 9
+#define PLDM_GET_TID_RESP_BYTES 2
 #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
@@ -181,6 +183,16 @@
 	uint8_t version_data[1];       //!< PLDM GetVersion version field
 } __attribute__((packed));
 
+/** @struct pldm_get_tid_resp
+ *
+ *  Structure representing PLDM get tid response.
+ */
+
+struct pldm_get_tid_resp {
+	uint8_t completion_code; //!< completion code
+	uint8_t tid;		 //!< PLDM GetTID TID field
+} __attribute__((packed));
+
 /**
  * @brief Populate the PLDM message with the PLDM header.The caller of this API
  *        allocates buffer for the PLDM header when forming the PLDM message.
@@ -292,6 +304,19 @@
 			    uint32_t *next_transfer_handle,
 			    uint8_t *transfer_flag, ver32_t *version);
 
+/* GetTID */
+
+/** @brief Decode a GetTID 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] tid - Pointer to the terminus id
+ *  @return pldm_completion_codes
+ */
+int decode_get_tid_resp(const struct pldm_msg *msg, size_t payload_length,
+			uint8_t *completion_code, uint8_t *tid);
+
 /* Responder */
 
 /* GetPLDMTypes */
@@ -371,6 +396,21 @@
 			   uint32_t *transfer_handle, uint8_t *transfer_opflag,
 			   uint8_t *type);
 
+/* GetTID */
+
+/** @brief Create a PLDM response message for GetTID
+ *
+ *  @param[in] instance_id - Message's instance id
+ *  @param[in] completion_code - PLDM completion code
+ *  @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_get_tid_resp(uint8_t instance_id, uint8_t completion_code,
+			uint8_t tid, struct pldm_msg *msg);
+
 #ifdef __cplusplus
 }
 #endif
diff --git a/libpldmresponder/base.cpp b/libpldmresponder/base.cpp
index fc4ddf6..d0c3551 100644
--- a/libpldmresponder/base.cpp
+++ b/libpldmresponder/base.cpp
@@ -21,7 +21,8 @@
 
 static const std::map<Type, Cmd> capabilities{
     {PLDM_BASE,
-     {PLDM_GET_PLDM_VERSION, PLDM_GET_PLDM_TYPES, PLDM_GET_PLDM_COMMANDS}},
+     {PLDM_GET_TID, PLDM_GET_PLDM_VERSION, PLDM_GET_PLDM_TYPES,
+      PLDM_GET_PLDM_COMMANDS}},
     {PLDM_PLATFORM, {PLDM_SET_STATE_EFFECTER_STATES}},
     {PLDM_BIOS, {PLDM_GET_DATE_TIME}}};
 
@@ -41,6 +42,7 @@
                     std::move(getPLDMCommands));
     registerHandler(PLDM_BASE, PLDM_GET_PLDM_VERSION,
                     std::move(getPLDMVersion));
+    registerHandler(PLDM_BASE, PLDM_GET_TID, std::move(getTID));
 }
 
 } // namespace base
@@ -144,5 +146,18 @@
     return response;
 }
 
+Response getTID(const pldm_msg* request, size_t payloadLength)
+{
+    // assigned 1 to the bmc as the PLDM terminus
+    uint8_t tid = 1;
+
+    Response response(sizeof(pldm_msg_hdr) + PLDM_GET_TID_RESP_BYTES, 0);
+    auto responsePtr = reinterpret_cast<pldm_msg*>(response.data());
+    encode_get_tid_resp(request->hdr.instance_id, PLDM_SUCCESS, tid,
+                        responsePtr);
+
+    return response;
+}
+
 } // namespace responder
 } // namespace pldm
diff --git a/libpldmresponder/base.hpp b/libpldmresponder/base.hpp
index c4afa18..a6e5abd 100644
--- a/libpldmresponder/base.hpp
+++ b/libpldmresponder/base.hpp
@@ -46,5 +46,13 @@
  *  @param[return] Response - PLDM Response message
  */
 Response getPLDMVersion(const pldm_msg* request, size_t payloadLength);
+
+/** @brief Handler for getTID
+ *
+ *  @param[in] request - Request message payload
+ *  @param[in] payload_length - Request message payload length
+ *  @param[return] Response - PLDM Response message
+ */
+Response getTID(const pldm_msg* request, size_t payloadLength);
 } // namespace responder
 } // namespace pldm
diff --git a/test/libpldm_base_test.cpp b/test/libpldm_base_test.cpp
index f17bccb..7aaf7e4 100644
--- a/test/libpldm_base_test.cpp
+++ b/test/libpldm_base_test.cpp
@@ -433,3 +433,36 @@
     ASSERT_EQ(versionOut.update, version.update);
     ASSERT_EQ(versionOut.alpha, version.alpha);
 }
+
+TEST(GetTID, testEncodeResponse)
+{
+    uint8_t completionCode = 0;
+    std::array<uint8_t, sizeof(pldm_msg_hdr) + PLDM_GET_TID_RESP_BYTES>
+        responseMsg{};
+    auto response = reinterpret_cast<pldm_msg*>(responseMsg.data());
+    uint8_t tid = 1;
+
+    auto rc = encode_get_tid_resp(0, PLDM_SUCCESS, tid, response);
+    ASSERT_EQ(rc, PLDM_SUCCESS);
+    uint8_t* payload = response->payload;
+    ASSERT_EQ(completionCode, payload[0]);
+    ASSERT_EQ(1, payload[sizeof(completionCode)]);
+}
+
+TEST(GetTID, testDecodeResponse)
+{
+    std::array<uint8_t, hdrSize + PLDM_GET_TID_RESP_BYTES> responseMsg{};
+    responseMsg[1 + hdrSize] = 1;
+
+    uint8_t tid;
+    uint8_t completion_code;
+
+    auto response = reinterpret_cast<pldm_msg*>(responseMsg.data());
+
+    auto rc = decode_get_tid_resp(response, responseMsg.size() - hdrSize,
+                                  &completion_code, &tid);
+
+    ASSERT_EQ(rc, PLDM_SUCCESS);
+    ASSERT_EQ(completion_code, PLDM_SUCCESS);
+    ASSERT_EQ(tid, 1);
+}
diff --git a/test/libpldmresponder_base_test.cpp b/test/libpldmresponder_base_test.cpp
index 6138341..bbd986a 100644
--- a/test/libpldmresponder_base_test.cpp
+++ b/test/libpldmresponder_base_test.cpp
@@ -37,7 +37,7 @@
     auto responsePtr = reinterpret_cast<pldm_msg*>(response.data());
     uint8_t* payload_ptr = responsePtr->payload;
     ASSERT_EQ(payload_ptr[0], 0);
-    ASSERT_EQ(payload_ptr[1], 56); // 56 = 0b111000
+    ASSERT_EQ(payload_ptr[1], 60); // 60 = 0b111100
     ASSERT_EQ(payload_ptr[2], 0);
 }
 
@@ -118,3 +118,18 @@
 
     ASSERT_EQ(responsePtr->payload[0], PLDM_ERROR_INVALID_PLDM_TYPE);
 }
+
+TEST(GetTID, testGoodRequest)
+{
+    std::array<uint8_t, sizeof(pldm_msg_hdr)> requestPayload{};
+    auto request = reinterpret_cast<pldm_msg*>(requestPayload.data());
+    size_t requestPayloadLength = 0;
+
+    auto response = getTID(request, requestPayloadLength);
+
+    auto responsePtr = reinterpret_cast<pldm_msg*>(response.data());
+    uint8_t* payload = responsePtr->payload;
+
+    ASSERT_EQ(payload[0], 0);
+    ASSERT_EQ(payload[1], 1);
+}