Add cc only response

For a PLDM operation resulting in an error,
unless otherwise specified, the responder shall not
return any additional parametric data.

It is useful to implement a cc-only response

Signed-off-by: John Wang <wangzqbj@inspur.com>
Change-Id: Iec5299f4c4043491aa62e0b94de7f9c12d0bee2e
diff --git a/handler.hpp b/handler.hpp
index 67ad471..3906ec1 100644
--- a/handler.hpp
+++ b/handler.hpp
@@ -1,7 +1,9 @@
 #pragma once
 
+#include <cassert>
 #include <functional>
 #include <map>
+#include <vector>
 
 #include "libpldm/base.h"
 
@@ -34,6 +36,23 @@
         return handlers.at(pldmCommand)(request, reqMsgLen);
     }
 
+    /** @brief Create a response message containing only cc
+     *
+     *  @param[in] request - PLDM request message
+     *  @param[in] cc - Completion Code
+     *  @return PLDM response message
+     */
+    static Response ccOnlyResponse(const pldm_msg* request, uint8_t cc)
+    {
+        Response response(sizeof(pldm_msg), 0);
+        auto ptr = reinterpret_cast<pldm_msg*>(response.data());
+        auto rc =
+            encode_cc_only_resp(request->hdr.instance_id, request->hdr.type,
+                                request->hdr.command, cc, ptr);
+        assert(rc == PLDM_SUCCESS);
+        return response;
+    }
+
   protected:
     /** @brief map of PLDM command code to handler - to be populated by derived
      *         classes.
diff --git a/libpldm/base.c b/libpldm/base.c
index 5927ba1..b34045c 100644
--- a/libpldm/base.c
+++ b/libpldm/base.c
@@ -367,3 +367,26 @@
 
 	return PLDM_SUCCESS;
 }
+
+int encode_cc_only_resp(uint8_t instance_id, uint8_t type, uint8_t command,
+			uint8_t cc, struct pldm_msg *msg)
+{
+	struct pldm_header_info header = {0};
+
+	if (msg == NULL) {
+		return PLDM_ERROR_INVALID_DATA;
+	}
+
+	header.instance = instance_id;
+	header.msg_type = PLDM_RESPONSE;
+	header.pldm_type = type;
+	header.command = command;
+	int rc = pack_pldm_header(&header, &msg->hdr);
+	if (rc != PLDM_SUCCESS) {
+		return rc;
+	}
+
+	msg->payload[0] = cc;
+
+	return PLDM_SUCCESS;
+}
diff --git a/libpldm/base.h b/libpldm/base.h
index 0738046..9d7961a 100644
--- a/libpldm/base.h
+++ b/libpldm/base.h
@@ -412,6 +412,18 @@
 int encode_get_tid_resp(uint8_t instance_id, uint8_t completion_code,
 			uint8_t tid, struct pldm_msg *msg);
 
+/** @brief Create a PLDM response message containing only cc
+ *
+ *  @param[in] instance_id - Message's instance id
+ *  @param[in] type - PLDM Type
+ *  @param[in] command - PLDM Command
+ *  @param[in] cc - PLDM Completion Code
+ *  @param[out] msg - Message will be written to this
+ *  @return pldm_completion_codes
+ */
+int encode_cc_only_resp(uint8_t instance_id, uint8_t type, uint8_t command,
+			uint8_t cc, struct pldm_msg *msg);
+
 #ifdef __cplusplus
 }
 #endif
diff --git a/test/libpldm_base_test.cpp b/test/libpldm_base_test.cpp
index 7aaf7e4..af3a2af 100644
--- a/test/libpldm_base_test.cpp
+++ b/test/libpldm_base_test.cpp
@@ -1,11 +1,16 @@
 #include <string.h>
 
 #include <array>
+#include <cstring>
+#include <vector>
 
 #include "libpldm/base.h"
 
+#include <gmock/gmock.h>
 #include <gtest/gtest.h>
 
+using testing::ElementsAreArray;
+
 constexpr auto hdrSize = sizeof(pldm_msg_hdr);
 
 TEST(PackPLDMMessage, BadPathTest)
@@ -466,3 +471,23 @@
     ASSERT_EQ(completion_code, PLDM_SUCCESS);
     ASSERT_EQ(tid, 1);
 }
+
+TEST(CcOnlyResponse, testEncode)
+{
+    struct pldm_msg responseMsg;
+
+    auto rc =
+        encode_cc_only_resp(0 /*instance id*/, 1 /*pldm type*/, 2 /*command*/,
+                            3 /*complection code*/, &responseMsg);
+    EXPECT_EQ(rc, PLDM_SUCCESS);
+
+    auto p = reinterpret_cast<uint8_t*>(&responseMsg);
+    EXPECT_THAT(std::vector<uint8_t>(p, p + sizeof(responseMsg)),
+                ElementsAreArray({0, 1, 2, 3}));
+
+    rc = encode_cc_only_resp(PLDM_INSTANCE_MAX + 1, 1, 2, 3, &responseMsg);
+    EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
+
+    rc = encode_cc_only_resp(0, 1, 2, 3, nullptr);
+    EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
+}
diff --git a/test/pldmd_registration_test.cpp b/test/pldmd_registration_test.cpp
index d51efdd..d37156b 100644
--- a/test/pldmd_registration_test.cpp
+++ b/test/pldmd_registration_test.cpp
@@ -28,6 +28,17 @@
     }
 };
 
+TEST(CcOnlyResponse, testEncode)
+{
+    std::vector<uint8_t> requestMsg(sizeof(pldm_msg_hdr));
+    auto request = reinterpret_cast<pldm_msg*>(requestMsg.data());
+    encode_get_types_req(0, request);
+
+    auto responseMsg = CmdHandler::ccOnlyResponse(request, PLDM_ERROR);
+    std::vector<uint8_t> expectMsg = {0, 0, 4, 1};
+    EXPECT_EQ(responseMsg, expectMsg);
+}
+
 TEST(Registration, testSuccess)
 {
     Invoker invoker{};