Decode APIs now accept const struct pldm_msg *msg

All the decode_xxx APIs were changed to accept the complete pldm_msg
instead of working with just a specific member (payload) making it
consistent with the encode_xxx APIs.

Following changes were made through out the code,
 - decode_xxx now requires to send the const struct pldm_msg *msg
   instead of a const uint8_t *msg.
 - Within the decode_xxx function the payload is extracted and used
   accordingly.
 - All the calls made to decode_xxx APIs have been modified to now
   pass the pointer to pldm_msg_hdr (struct pldm_msg *msg).
 - The test code was modified to now pass the appropriate data
   to all the encode_xxx APIs.

Change-Id: I16a3f6e554ea2c9fa44d55dc8f21f65022bd983a
Signed-off-by: Zahed Hossain <zahzahed@in.ibm.com>
diff --git a/test/libpldm_base_test.cpp b/test/libpldm_base_test.cpp
index faa4d90..f17bccb 100644
--- a/test/libpldm_base_test.cpp
+++ b/test/libpldm_base_test.cpp
@@ -6,6 +6,8 @@
 
 #include <gtest/gtest.h>
 
+constexpr auto hdrSize = sizeof(pldm_msg_hdr);
+
 TEST(PackPLDMMessage, BadPathTest)
 {
     struct pldm_header_info hdr;
@@ -208,12 +210,16 @@
     ver32_t version{0xFF, 0xFF, 0xFF, 0xFF};
     uint8_t pldmTypeOut{};
     ver32_t versionOut{0xFF, 0xFF, 0xFF, 0xFF};
-    std::array<uint8_t, PLDM_GET_COMMANDS_REQ_BYTES> requestMsg{};
+    std::array<uint8_t, hdrSize + PLDM_GET_COMMANDS_REQ_BYTES> requestMsg{};
 
-    memcpy(requestMsg.data(), &pldmType, sizeof(pldmType));
-    memcpy(requestMsg.data() + sizeof(pldmType), &version, sizeof(version));
-    auto rc = decode_get_commands_req(requestMsg.data(), requestMsg.size(),
+    memcpy(requestMsg.data() + hdrSize, &pldmType, sizeof(pldmType));
+    memcpy(requestMsg.data() + sizeof(pldmType) + hdrSize, &version,
+           sizeof(version));
+
+    auto request = reinterpret_cast<pldm_msg*>(requestMsg.data());
+    auto rc = decode_get_commands_req(request, requestMsg.size() - hdrSize,
                                       &pldmTypeOut, &versionOut);
+
     ASSERT_EQ(rc, PLDM_SUCCESS);
     ASSERT_EQ(pldmTypeOut, pldmType);
     ASSERT_EQ(0, memcmp(&versionOut, &version, sizeof(version)));
@@ -265,40 +271,44 @@
 
 TEST(GetPLDMTypes, testDecodeResponse)
 {
-    std::array<uint8_t, PLDM_GET_TYPES_RESP_BYTES> responseMsg{};
-    responseMsg[1] = 1;
-    responseMsg[2] = 2;
-    responseMsg[3] = 3;
+    std::array<uint8_t, hdrSize + PLDM_GET_TYPES_RESP_BYTES> responseMsg{};
+    responseMsg[1 + hdrSize] = 1;
+    responseMsg[2 + hdrSize] = 2;
+    responseMsg[3 + hdrSize] = 3;
     std::array<bitfield8_t, PLDM_MAX_TYPES / 8> outTypes{};
     uint8_t completion_code;
 
-    auto rc = decode_get_types_resp(responseMsg.data(), responseMsg.size(),
+    auto response = reinterpret_cast<pldm_msg*>(responseMsg.data());
+
+    auto rc = decode_get_types_resp(response, responseMsg.size() - hdrSize,
                                     &completion_code, outTypes.data());
 
     ASSERT_EQ(rc, PLDM_SUCCESS);
     ASSERT_EQ(completion_code, PLDM_SUCCESS);
-    ASSERT_EQ(responseMsg[1], outTypes[0].byte);
-    ASSERT_EQ(responseMsg[2], outTypes[1].byte);
-    ASSERT_EQ(responseMsg[3], outTypes[2].byte);
+    ASSERT_EQ(responseMsg[1 + hdrSize], outTypes[0].byte);
+    ASSERT_EQ(responseMsg[2 + hdrSize], outTypes[1].byte);
+    ASSERT_EQ(responseMsg[3 + hdrSize], outTypes[2].byte);
 }
 
 TEST(GetPLDMCommands, testDecodeResponse)
 {
-    std::array<uint8_t, PLDM_GET_COMMANDS_RESP_BYTES> responseMsg{};
-    responseMsg[1] = 1;
-    responseMsg[2] = 2;
-    responseMsg[3] = 3;
+    std::array<uint8_t, hdrSize + PLDM_GET_COMMANDS_RESP_BYTES> responseMsg{};
+    responseMsg[1 + hdrSize] = 1;
+    responseMsg[2 + hdrSize] = 2;
+    responseMsg[3 + hdrSize] = 3;
     std::array<bitfield8_t, PLDM_MAX_CMDS_PER_TYPE / 8> outTypes{};
     uint8_t completion_code;
 
-    auto rc = decode_get_commands_resp(responseMsg.data(), responseMsg.size(),
+    auto response = reinterpret_cast<pldm_msg*>(responseMsg.data());
+
+    auto rc = decode_get_commands_resp(response, responseMsg.size() - hdrSize,
                                        &completion_code, outTypes.data());
 
     ASSERT_EQ(rc, PLDM_SUCCESS);
     ASSERT_EQ(completion_code, PLDM_SUCCESS);
-    ASSERT_EQ(responseMsg[1], outTypes[0].byte);
-    ASSERT_EQ(responseMsg[2], outTypes[1].byte);
-    ASSERT_EQ(responseMsg[3], outTypes[2].byte);
+    ASSERT_EQ(responseMsg[1 + hdrSize], outTypes[0].byte);
+    ASSERT_EQ(responseMsg[2 + hdrSize], outTypes[1].byte);
+    ASSERT_EQ(responseMsg[3 + hdrSize], outTypes[2].byte);
 }
 
 TEST(GetPLDMVersion, testGoodEncodeRequest)
@@ -361,7 +371,7 @@
 
 TEST(GetPLDMVersion, testDecodeRequest)
 {
-    std::array<uint8_t, PLDM_GET_VERSION_REQ_BYTES> requestMsg{};
+    std::array<uint8_t, hdrSize + PLDM_GET_VERSION_REQ_BYTES> requestMsg{};
     uint32_t transferHandle = 0x0;
     uint32_t retTransferHandle = 0x0;
     uint8_t flag = PLDM_GET_FIRSTPART;
@@ -369,12 +379,16 @@
     uint8_t pldmType = PLDM_BASE;
     uint8_t retType = PLDM_BASE;
 
-    memcpy(requestMsg.data(), &transferHandle, sizeof(transferHandle));
-    memcpy(requestMsg.data() + sizeof(transferHandle), &flag, sizeof(flag));
-    memcpy(requestMsg.data() + sizeof(transferHandle) + sizeof(flag), &pldmType,
-           sizeof(pldmType));
+    memcpy(requestMsg.data() + hdrSize, &transferHandle,
+           sizeof(transferHandle));
+    memcpy(requestMsg.data() + sizeof(transferHandle) + hdrSize, &flag,
+           sizeof(flag));
+    memcpy(requestMsg.data() + sizeof(transferHandle) + sizeof(flag) + hdrSize,
+           &pldmType, sizeof(pldmType));
 
-    auto rc = decode_get_version_req(requestMsg.data(), requestMsg.size(),
+    auto request = reinterpret_cast<pldm_msg*>(requestMsg.data());
+
+    auto rc = decode_get_version_req(request, requestMsg.size() - hdrSize,
                                      &retTransferHandle, &retFlag, &retType);
 
     ASSERT_EQ(rc, PLDM_SUCCESS);
@@ -385,7 +399,8 @@
 
 TEST(GetPLDMVersion, testDecodeResponse)
 {
-    std::array<uint8_t, PLDM_GET_VERSION_RESP_BYTES> responseMsg{};
+    std::array<uint8_t, sizeof(pldm_msg_hdr) + PLDM_GET_VERSION_RESP_BYTES>
+        responseMsg{};
     uint32_t transferHandle = 0x0;
     uint32_t retTransferHandle = 0x0;
     uint8_t flag = PLDM_START_AND_END;
@@ -395,15 +410,18 @@
     ver32_t versionOut;
     uint8_t completion_code;
 
-    memcpy(responseMsg.data() + sizeof(completionCode), &transferHandle,
-           sizeof(transferHandle));
-    memcpy(responseMsg.data() + sizeof(completionCode) + sizeof(transferHandle),
+    memcpy(responseMsg.data() + sizeof(completionCode) + hdrSize,
+           &transferHandle, sizeof(transferHandle));
+    memcpy(responseMsg.data() + sizeof(completionCode) +
+               sizeof(transferHandle) + hdrSize,
            &flag, sizeof(flag));
     memcpy(responseMsg.data() + sizeof(completionCode) +
-               sizeof(transferHandle) + sizeof(flag),
+               sizeof(transferHandle) + sizeof(flag) + hdrSize,
            &version, sizeof(version));
 
-    auto rc = decode_get_version_resp(responseMsg.data(), responseMsg.size(),
+    auto response = reinterpret_cast<pldm_msg*>(responseMsg.data());
+
+    auto rc = decode_get_version_resp(response, responseMsg.size() - hdrSize,
                                       &completion_code, &retTransferHandle,
                                       &retFlag, &versionOut);
     ASSERT_EQ(rc, PLDM_SUCCESS);